blob: 1d542585e58a87b0aa427135819179d2a32541bd [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020085#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
118
119/*******************************/
120/* stick table sync mesg types */
121/* Note: ids >= 128 contains */
122/* id message cotains data */
123/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200124#define PEER_MSG_STKT_UPDATE 0x80
125#define PEER_MSG_STKT_INCUPDATE 0x81
126#define PEER_MSG_STKT_DEFINE 0x82
127#define PEER_MSG_STKT_SWITCH 0x83
128#define PEER_MSG_STKT_ACK 0x84
129#define PEER_MSG_STKT_UPDATE_TIMED 0x85
130#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200131
132/**********************************/
133/* Peer Session IO handler states */
134/**********************************/
135
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100136enum {
137 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
138 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
139 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
140 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100141 /* after this point, data were possibly exchanged */
142 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
143 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
144 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
145 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
146 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200147 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
148 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149 PEER_SESS_ST_END, /* Killed session */
150};
Emeric Brun2b920a12010-09-23 18:30:22 +0200151
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100152/***************************************************/
153/* Peer Session status code - part of the protocol */
154/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
157#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200158
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100159#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100161#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200162
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100163#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
164#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
165#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
166#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200169#define PEER_MAJOR_VER 2
170#define PEER_MINOR_VER 1
171#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200172
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200173struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100174static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Emeric Brun18928af2017-03-29 16:32:53 +0200176/* This function encode an uint64 to 'dynamic' length format.
177 The encoded value is written at address *str, and the
178 caller must assure that size after *str is large enought.
179 At return, the *str is set at the next Byte after then
180 encoded integer. The function returns then length of the
181 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200182int intencode(uint64_t i, char **str) {
183 int idx = 0;
184 unsigned char *msg;
185
186 if (!*str)
187 return 0;
188
189 msg = (unsigned char *)*str;
190 if (i < 240) {
191 msg[0] = (unsigned char)i;
192 *str = (char *)&msg[idx+1];
193 return (idx+1);
194 }
195
196 msg[idx] =(unsigned char)i | 240;
197 i = (i - 240) >> 4;
198 while (i >= 128) {
199 msg[++idx] = (unsigned char)i | 128;
200 i = (i - 128) >> 7;
201 }
202 msg[++idx] = (unsigned char)i;
203 *str = (char *)&msg[idx+1];
204 return (idx+1);
205}
206
207
208/* This function returns the decoded integer or 0
209 if decode failed
210 *str point on the beginning of the integer to decode
211 at the end of decoding *str point on the end of the
212 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200213uint64_t intdecode(char **str, char *end)
214{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200215 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200216 uint64_t i;
217 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200218
219 if (!*str)
220 return 0;
221
222 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200223 if (msg >= (unsigned char *)end)
224 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200225
Emeric Brun18928af2017-03-29 16:32:53 +0200226 i = *(msg++);
227 if (i >= 240) {
228 shift = 4;
229 do {
230 if (msg >= (unsigned char *)end)
231 goto fail;
232 i += (uint64_t)*msg << shift;
233 shift += 7;
234 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200235 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100236 *str = (char *)msg;
237 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200238
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100239 fail:
240 *str = NULL;
241 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200242}
Emeric Brun2b920a12010-09-23 18:30:22 +0200243
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200244/* Set the stick-table UPDATE message type byte at <msg_type> address,
245 * depending on <use_identifier> and <use_timed> boolean parameters.
246 * Always successful.
247 */
248static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
249{
250 if (use_timed) {
251 if (use_identifier)
252 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
253 else
254 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
255 }
256 else {
257 if (use_identifier)
258 *msg_type = PEER_MSG_STKT_UPDATE;
259 else
260 *msg_type = PEER_MSG_STKT_INCUPDATE;
261 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200262}
Emeric Brun2b920a12010-09-23 18:30:22 +0200263/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200264 * This prepare the data update message on the stick session <ts>, <st> is the considered
265 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800266 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200267 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
268 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200269 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100270static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
271 unsigned int updateid, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200272{
273 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274 unsigned short datalen;
275 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200276 unsigned int data_type;
277 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 cursor = datamsg = msg + 1 + 5;
280
Emeric Brun2b920a12010-09-23 18:30:22 +0200281 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200282
283 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200284 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200285 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287
288 /* encode update identifier if needed */
289 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200291 memcpy(cursor, &netinteger, sizeof(netinteger));
292 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200293 }
294
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200295 if (use_timed) {
296 netinteger = htonl(tick_remain(now_ms, ts->expire));
297 memcpy(cursor, &netinteger, sizeof(netinteger));
298 cursor += sizeof(netinteger);
299 }
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200302 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200303 int stlen = strlen((char *)ts->key.key);
304
Emeric Brunb3971ab2015-05-12 18:49:09 +0200305 intencode(stlen, &cursor);
306 memcpy(cursor, ts->key.key, stlen);
307 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200308 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200309 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200310 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, &netinteger, sizeof(netinteger));
312 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 memcpy(cursor, ts->key.key, st->table->key_size);
316 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200317 }
318
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200320 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200321 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200322
Emeric Brun94900952015-06-11 18:25:54 +0200323 data_ptr = stktable_data_ptr(st->table, ts, data_type);
324 if (data_ptr) {
325 switch (stktable_data_types[data_type].std_type) {
326 case STD_T_SINT: {
327 int data;
328
329 data = stktable_data_cast(data_ptr, std_t_sint);
330 intencode(data, &cursor);
331 break;
332 }
333 case STD_T_UINT: {
334 unsigned int data;
335
336 data = stktable_data_cast(data_ptr, std_t_uint);
337 intencode(data, &cursor);
338 break;
339 }
340 case STD_T_ULL: {
341 unsigned long long data;
342
343 data = stktable_data_cast(data_ptr, std_t_ull);
344 intencode(data, &cursor);
345 break;
346 }
347 case STD_T_FRQP: {
348 struct freq_ctr_period *frqp;
349
350 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
351 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
352 intencode(frqp->curr_ctr, &cursor);
353 intencode(frqp->prev_ctr, &cursor);
354 break;
355 }
356 }
357 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360
361 /* Compute datalen */
362 datalen = (cursor - datamsg);
363
364 /* prepare message header */
365 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200366 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367 cursor = &msg[2];
368 intencode(datalen, &cursor);
369
370 /* move data after header */
371 memmove(cursor, datamsg, datalen);
372
373 /* return header size + data_len */
374 return (cursor - msg) + datalen;
375}
376
377/*
378 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800379 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200380 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
381 * check size)
382 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100383static int peer_prepare_switchmsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
384 unsigned int param1, int param2, int param3)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200385{
386 int len;
387 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200388 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200389 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200391 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200392
393 cursor = datamsg = msg + 2 + 5;
394
395 /* Encode data */
396
397 /* encode local id */
398 intencode(st->local_id, &cursor);
399
400 /* encode table name */
401 len = strlen(st->table->id);
402 intencode(len, &cursor);
403 memcpy(cursor, st->table->id, len);
404 cursor += len;
405
406 /* encode table type */
407
408 intencode(st->table->type, &cursor);
409
410 /* encode table key size */
411 intencode(st->table->key_size, &cursor);
412
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200413 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200415 /* encode available known data types in table */
416 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
417 if (st->table->data_ofs[data_type]) {
418 switch (stktable_data_types[data_type].std_type) {
419 case STD_T_SINT:
420 case STD_T_UINT:
421 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200422 data |= 1 << data_type;
423 break;
Emeric Brun94900952015-06-11 18:25:54 +0200424 case STD_T_FRQP:
425 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200426 intencode(data_type, &chunkq);
427 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200428 break;
429 }
430 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200431 }
432 intencode(data, &cursor);
433
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200434 /* Encode stick-table entries duration. */
435 intencode(st->table->expire, &cursor);
436
437 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200438 chunk->data = chunkq - chunkp;
439 memcpy(cursor, chunk->area, chunk->data);
440 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200441 }
442
Emeric Brunb3971ab2015-05-12 18:49:09 +0200443 /* Compute datalen */
444 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200445
Emeric Brunb3971ab2015-05-12 18:49:09 +0200446 /* prepare message header */
447 msg[0] = PEER_MSG_CLASS_STICKTABLE;
448 msg[1] = PEER_MSG_STKT_DEFINE;
449 cursor = &msg[2];
450 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200451
Emeric Brunb3971ab2015-05-12 18:49:09 +0200452 /* move data after header */
453 memmove(cursor, datamsg, datalen);
454
455 /* return header size + data_len */
456 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200457}
458
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459/*
460 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
461 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800462 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200463 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
464 * check size)
465 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100466static int peer_prepare_ackmsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
467 unsigned int param1, int param2, int param3)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200468{
469 unsigned short datalen;
470 char *cursor, *datamsg;
471 uint32_t netinteger;
472
Emeric Brunb058f1c2015-09-22 15:50:18 +0200473 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474
475 intencode(st->remote_id, &cursor);
476 netinteger = htonl(st->last_get);
477 memcpy(cursor, &netinteger, sizeof(netinteger));
478 cursor += sizeof(netinteger);
479
480 /* Compute datalen */
481 datalen = (cursor - datamsg);
482
483 /* prepare message header */
484 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200485 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200486 cursor = &msg[2];
487 intencode(datalen, &cursor);
488
489 /* move data after header */
490 memmove(cursor, datamsg, datalen);
491
492 /* return header size + data_len */
493 return (cursor - msg) + datalen;
494}
Emeric Brun2b920a12010-09-23 18:30:22 +0200495
496/*
497 * Callback to release a session with a peer
498 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200500{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200501 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200502 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200503 struct peer *peer = appctx->ctx.peers.ptr;
504 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200505
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100506 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100507 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200508 return;
509
510 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200511 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100512 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
513 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100514 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100515 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100516 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200517 /* Re-init current table pointers to force announcement on re-connect */
518 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 peer->appctx = NULL;
520 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200521 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
523 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200524
525 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200527 }
528 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200529 peer->flags &= PEER_TEACH_RESET;
530 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200531 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100532 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200533 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200534 }
535}
536
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200537/* Retrieve the major and minor versions of peers protocol
538 * announced by a remote peer. <str> is a null-terminated
539 * string with the following format: "<maj_ver>.<min_ver>".
540 */
541static int peer_get_version(const char *str,
542 unsigned int *maj_ver, unsigned int *min_ver)
543{
544 unsigned int majv, minv;
545 const char *pos, *saved;
546 const char *end;
547
548 saved = pos = str;
549 end = str + strlen(str);
550
551 majv = read_uint(&pos, end);
552 if (saved == pos || *pos++ != '.')
553 return -1;
554
555 saved = pos;
556 minv = read_uint(&pos, end);
557 if (saved == pos || pos != end)
558 return -1;
559
560 *maj_ver = majv;
561 *min_ver = minv;
562
563 return 0;
564}
Emeric Brun2b920a12010-09-23 18:30:22 +0200565
566/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100567 * Parse a line terminated by an optional '\r' character, followed by a mandatory
568 * '\n' character.
569 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
570 * a line could not be read because the communication channel is closed.
571 */
572static inline int peer_getline(struct appctx *appctx)
573{
574 int n;
575 struct stream_interface *si = appctx->owner;
576
577 n = co_getline(si_oc(si), trash.area, trash.size);
578 if (!n)
579 return 0;
580
581 if (n < 0 || trash.area[n - 1] != '\n') {
582 appctx->st0 = PEER_SESS_ST_END;
583 return -1;
584 }
585
586 if (n > 1 && (trash.area[n - 2] == '\r'))
587 trash.area[n - 2] = 0;
588 else
589 trash.area[n - 1] = 0;
590
591 co_skip(si_oc(si), n);
592
593 return n;
594}
595
596/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100597 * Send a message after having called <peer_prepare_msg> to build it.
598 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
599 * Returns -1 if there was not enough room left to send the message,
600 * any other negative returned value must be considered as an error with an appcxt st0
601 * returned value equal to PEER_SESS_ST_END.
602 */
603static inline int peer_send_msg(struct shared_table *st, struct appctx *appctx,
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100604 int (*peer_prepare_msg)(struct stksess *, struct shared_table *,
605 char *, size_t,
606 unsigned int, int, int),
607 struct stksess *ts, unsigned int param1, int param2, int param3)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100608{
609 int ret, msglen;
610 struct stream_interface *si = appctx->owner;
611
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100612 msglen = peer_prepare_msg(ts, st, trash.area, trash.size, param1, param2, param3);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100613 if (!msglen) {
614 /* internal error: message does not fit in trash */
615 appctx->st0 = PEER_SESS_ST_END;
616 return 0;
617 }
618
619 /* message to buffer */
620 ret = ci_putblk(si_ic(si), trash.area, msglen);
621 if (ret <= 0) {
622 if (ret == -1) {
623 /* No more write possible */
624 si_rx_room_blk(si);
625 return -1;
626 }
627 appctx->st0 = PEER_SESS_ST_END;
628 }
629
630 return ret;
631}
632
633/*
634 * Send a stick-table switch message.
635 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
636 * Returns -1 if there was not enough room left to send the message,
637 * any other negative returned value must be considered as an error with an appcxt st0
638 * returned value equal to PEER_SESS_ST_END.
639 */
640static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
641{
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100642 return peer_send_msg(st, appctx, peer_prepare_switchmsg, NULL, -1, -1, -1);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100643}
644
645/*
646 * Send a stick-table update acknowledgement message.
647 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
648 * Returns -1 if there was not enough room left to send the message,
649 * any other negative returned value must be considered as an error with an appcxt st0
650 * returned value equal to PEER_SESS_ST_END.
651 */
652static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
653{
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100654 return peer_send_msg(st, appctx, peer_prepare_ackmsg, NULL, -1, -1, -1);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100655}
656
657/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100658 * Send a stick-table update message.
659 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
660 * Returns -1 if there was not enough room left to send the message,
661 * any other negative returned value must be considered as an error with an appcxt st0
662 * returned value equal to PEER_SESS_ST_END.
663 */
664static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
665 unsigned int updateid, int use_identifier, int use_timed)
666{
667 return peer_send_msg(st, appctx, peer_prepare_updatemsg, ts, updateid, use_identifier, use_timed);
668}
669
670/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200671 * IO Handler to handle message exchance with a peer
672 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200673static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200674{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200675 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200676 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200677 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200678 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200679 int reql = 0;
680 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200681 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
682 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100683 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +0200684
Joseph Herlant82b2f542018-11-15 12:19:14 -0800685 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200686 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100687 goto full;
688
Emeric Brun2b920a12010-09-23 18:30:22 +0200689 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100690 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200691switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200692 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100693 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100694 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100695 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100697 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100699 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100700 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200701
Frédéric Lécaillece025572019-01-21 13:38:06 +0100702 reql = peer_getline(appctx);
703 if (!reql)
704 goto out;
705
706 if (reql < 0)
707 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200708
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200709 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200711 appctx->st0 = PEER_SESS_ST_EXIT;
712 appctx->st1 = PEER_SESS_SC_ERRPROTO;
713 goto switchstate;
714 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200716 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100717 appctx->st0 = PEER_SESS_ST_EXIT;
718 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200719 goto switchstate;
720 }
721
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100722 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200723 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100724 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100725 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200726
Frédéric Lécaillece025572019-01-21 13:38:06 +0100727 reql = peer_getline(appctx);
728 if (!reql)
729 goto out;
730
731 if (reql < 0)
732 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200733
734 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100736 appctx->st0 = PEER_SESS_ST_EXIT;
737 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200738 goto switchstate;
739 }
740
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100741 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200742 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100743 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200744 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100745
746 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200747
Frédéric Lécaillece025572019-01-21 13:38:06 +0100748 reql = peer_getline(appctx);
749 if (!reql)
750 goto out;
751
752 if (reql < 0)
753 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200754
Emeric Brunb3971ab2015-05-12 18:49:09 +0200755 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200757 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100758 appctx->st0 = PEER_SESS_ST_EXIT;
759 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200760 goto switchstate;
761 }
762 *p = 0;
763
764 /* lookup known peer */
765 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200767 break;
768 }
769
770 /* if unknown peer */
771 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100772 appctx->st0 = PEER_SESS_ST_EXIT;
773 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200774 goto switchstate;
775 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200776
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100777 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100778 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200779 if (curpeer->local) {
780 /* Local connection, reply a retry */
781 appctx->st0 = PEER_SESS_ST_EXIT;
782 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
783 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200784 }
Emeric Brun80527f52017-06-19 17:46:37 +0200785
786 /* we're killing a connection, we must apply a random delay before
787 * retrying otherwise the other end will do the same and we can loop
788 * for a while.
789 */
790 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100791 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200792 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200793 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
794 if (min_ver == PEER_DWNGRD_MINOR_VER) {
795 curpeer->flags |= PEER_F_DWNGRD;
796 }
797 else {
798 curpeer->flags &= ~PEER_F_DWNGRD;
799 }
800 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 curpeer->appctx = appctx;
802 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100803 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +0100804 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +0200805 /* fall through */
806 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100807 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200808 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200809
Willy Tarreau2d372c22018-11-05 17:12:27 +0100810 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200811 if (!curpeer) {
812 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100813 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200814 if (curpeer->appctx != appctx) {
815 appctx->st0 = PEER_SESS_ST_END;
816 goto switchstate;
817 }
818 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 repl = snprintf(trash.area, trash.size,
820 "%d\n",
821 PEER_SESS_SC_SUCCESSCODE);
822 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200823 if (repl <= 0) {
824 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100825 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100826 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200827 goto switchstate;
828 }
829
830 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200831 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200832
833 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200834 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200835
836 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200837 curpeer->confirm = 0;
838
839 /* Init cursors */
840 for (st = curpeer->tables; st ; st = st->next) {
841 st->last_get = st->last_acked = 0;
842 st->teaching_origin = st->last_pushed = st->update;
843 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200844
845 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200846 curpeer->flags &= PEER_TEACH_RESET;
847 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200848
849 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100850 if (curpeer->local) {
851 /* if current host need resyncfrom local and no process assined */
852 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
853 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
854 /* assign local peer for a lesson, consider lesson already requested */
855 curpeer->flags |= PEER_F_LEARN_ASSIGN;
856 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
857 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200858
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100859 }
860 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
861 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
862 /* assign peer for a lesson */
863 curpeer->flags |= PEER_F_LEARN_ASSIGN;
864 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
865 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200866
867
Emeric Brun2b920a12010-09-23 18:30:22 +0200868 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100869 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100870 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200871 goto switchstate;
872 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100873 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100874 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200875 if (!curpeer) {
876 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100877 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200878 if (curpeer->appctx != appctx) {
879 appctx->st0 = PEER_SESS_ST_END;
880 goto switchstate;
881 }
882 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200883
884 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200885 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200886 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
887 PEER_MAJOR_VER,
888 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200889 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200890 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100891 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200893
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100894 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100895 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200896 goto switchstate;
897 }
898
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200899 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200900 if (repl <= 0) {
901 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100902 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100903 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200904 goto switchstate;
905 }
906
907 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100908 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200909 /* fall through */
910 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100911 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200912 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200913
Willy Tarreau2d372c22018-11-05 17:12:27 +0100914 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200915 if (!curpeer) {
916 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100917 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200918 if (curpeer->appctx != appctx) {
919 appctx->st0 = PEER_SESS_ST_END;
920 goto switchstate;
921 }
922 }
923
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100924 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200925 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200926
Frédéric Lécaillece025572019-01-21 13:38:06 +0100927 reql = peer_getline(appctx);
928 if (!reql)
929 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +0200930
Frédéric Lécaillece025572019-01-21 13:38:06 +0100931 if (reql < 0)
932 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200933
934 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200935 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +0200936
937 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200938 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200939
940 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200941 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200942 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200943 for (st = curpeer->tables; st ; st = st->next) {
944 st->last_get = st->last_acked = 0;
945 st->teaching_origin = st->last_pushed = st->update;
946 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200947
948 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100949 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200950
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100951 /* reset teaching and learning flags to 0 */
952 curpeer->flags &= PEER_TEACH_RESET;
953 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200954
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100955 /* If current peer is local */
956 if (curpeer->local) {
957 /* flag to start to teach lesson */
958 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200959
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100960 }
961 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
962 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
963 /* If peer is remote and resync from remote is needed,
964 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200965
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100966 /* assign peer for a lesson */
967 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200968 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200969 }
970
971 }
972 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200973 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
974 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200975 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100976 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200977 goto switchstate;
978 }
Willy Tarreau2d372c22018-11-05 17:12:27 +0100979 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100980 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200981 /* fall through */
982 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100983 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200984 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200986 char *msg_cur = trash.area;
987 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200988 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200989 int totl = 0;
990
Willy Tarreau2d372c22018-11-05 17:12:27 +0100991 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200992 if (!curpeer) {
993 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100994 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200995 if (curpeer->appctx != appctx) {
996 appctx->st0 = PEER_SESS_ST_END;
997 goto switchstate;
998 }
999 }
1000
Willy Tarreau06d80a92017-10-19 14:32:15 +02001001 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +02001002 if (reql <= 0) /* closed or EOL not found */
1003 goto incomplete;
1004
Emeric Brun2b920a12010-09-23 18:30:22 +02001005 totl += reql;
1006
Emeric Brunb3971ab2015-05-12 18:49:09 +02001007 if (msg_head[1] >= 128) {
1008 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001009 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010 if (reql <= 0) /* closed */
1011 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +02001012
Emeric Brunb3971ab2015-05-12 18:49:09 +02001013 totl += reql;
1014
1015 if (msg_head[2] < 240) {
1016 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001017 }
1018 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001019 int i;
1020 char *cur;
1021 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001022
Emeric Brunb3971ab2015-05-12 18:49:09 +02001023 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02001024 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001025 if (reql <= 0) /* closed */
1026 goto incomplete;
1027
1028 totl += reql;
1029
1030 if (!(msg_head[i] & 0x80))
1031 break;
1032 }
1033
1034 if (i == sizeof(msg_head)) {
1035 /* malformed message */
1036 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1037 goto switchstate;
1038
1039 }
1040 end = (char *)msg_head + sizeof(msg_head);
1041 cur = (char *)&msg_head[2];
1042 msg_len = intdecode(&cur, end);
1043 if (!cur) {
1044 /* malformed message */
1045 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1046 goto switchstate;
1047 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001048 }
1049
Willy Tarreau86a446e2013-11-25 23:02:37 +01001050
Emeric Brunb3971ab2015-05-12 18:49:09 +02001051 /* Read message content */
1052 if (msg_len) {
1053 if (msg_len > trash.size) {
1054 /* Status code is not success, abort */
1055 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1056 goto switchstate;
1057 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001058
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001059 reql = co_getblk(si_oc(si),
1060 trash.area,
1061 msg_len,
1062 totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001063 if (reql <= 0) /* closed */
1064 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +02001065 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001066
Emeric Brunb3971ab2015-05-12 18:49:09 +02001067 msg_end += msg_len;
1068 }
1069 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001070
Emeric Brunb3971ab2015-05-12 18:49:09 +02001071 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1072 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1073 struct shared_table *st;
1074 /* Reset message: remote need resync */
1075
1076 /* prepare tables fot a global push */
1077 for (st = curpeer->tables; st; st = st->next) {
1078 st->teaching_origin = st->last_pushed = st->table->update;
1079 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001080 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001081
Emeric Brunb3971ab2015-05-12 18:49:09 +02001082 /* reset teaching flags to 0 */
1083 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001084
Emeric Brunb3971ab2015-05-12 18:49:09 +02001085 /* flag to start to teach lesson */
1086 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001087 }
1088 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001089 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1090 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001091 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1092 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001093 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001094 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001095 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001096 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001097 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1098 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001099 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001100
1101 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001102 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1103 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001104 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001105 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001106 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001107 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001108 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001109
1110 /* If stopping state */
1111 if (stopping) {
1112 /* Close session, push resync no more needed */
1113 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1114 appctx->st0 = PEER_SESS_ST_END;
1115 goto switchstate;
1116 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001117 for (st = curpeer->tables; st; st = st->next) {
1118 st->update = st->last_pushed = st->teaching_origin;
1119 st->flags = 0;
1120 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001121
1122 /* reset teaching flags to 0 */
1123 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001124 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001125 }
1126 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1127 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1128 int table_id_len;
1129 struct shared_table *st;
1130 int table_type;
1131 int table_keylen;
1132 int table_id;
1133 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001134
Emeric Brunb3971ab2015-05-12 18:49:09 +02001135 table_id = intdecode(&msg_cur, msg_end);
1136 if (!msg_cur) {
1137 /* malformed message */
1138 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1139 goto switchstate;
1140 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001141
Emeric Brunb3971ab2015-05-12 18:49:09 +02001142 table_id_len = intdecode(&msg_cur, msg_end);
1143 if (!msg_cur) {
1144 /* malformed message */
1145 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1146 goto switchstate;
1147 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001148
Emeric Brunb3971ab2015-05-12 18:49:09 +02001149 curpeer->remote_table = NULL;
1150 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1151 /* malformed message */
1152 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1153 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001154 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001155
Emeric Brunb3971ab2015-05-12 18:49:09 +02001156 for (st = curpeer->tables; st; st = st->next) {
1157 /* Reset IDs */
1158 if (st->remote_id == table_id)
1159 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001160
Emeric Brunb3971ab2015-05-12 18:49:09 +02001161 if (!curpeer->remote_table
1162 && (table_id_len == strlen(st->table->id))
1163 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1164 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001165 }
1166 }
1167
Emeric Brunb3971ab2015-05-12 18:49:09 +02001168 if (!curpeer->remote_table) {
1169 goto ignore_msg;
1170 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001171
Emeric Brunb3971ab2015-05-12 18:49:09 +02001172 msg_cur += table_id_len;
1173 if (msg_cur >= msg_end) {
1174 /* malformed message */
1175 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1176 goto switchstate;
1177 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001178
Emeric Brunb3971ab2015-05-12 18:49:09 +02001179 table_type = intdecode(&msg_cur, msg_end);
1180 if (!msg_cur) {
1181 /* malformed message */
1182 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1183 goto switchstate;
1184 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001185
Emeric Brunb3971ab2015-05-12 18:49:09 +02001186 table_keylen = intdecode(&msg_cur, msg_end);
1187 if (!msg_cur) {
1188 /* malformed message */
1189 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1190 goto switchstate;
1191 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001192
Emeric Brunb3971ab2015-05-12 18:49:09 +02001193 table_data = intdecode(&msg_cur, msg_end);
1194 if (!msg_cur) {
1195 /* malformed message */
1196 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1197 goto switchstate;
1198 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001199
Emeric Brunb3971ab2015-05-12 18:49:09 +02001200 if (curpeer->remote_table->table->type != table_type
1201 || curpeer->remote_table->table->key_size != table_keylen) {
1202 curpeer->remote_table = NULL;
1203 goto ignore_msg;
1204 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001205
Emeric Brunb3971ab2015-05-12 18:49:09 +02001206 curpeer->remote_table->remote_data = table_data;
1207 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001208 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001209 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1210 struct shared_table *st;
1211 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001212
Emeric Brunb3971ab2015-05-12 18:49:09 +02001213 table_id = intdecode(&msg_cur, msg_end);
1214 if (!msg_cur) {
1215 /* malformed message */
1216 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1217 goto switchstate;
1218 }
1219 curpeer->remote_table = NULL;
1220 for (st = curpeer->tables; st; st = st->next) {
1221 if (st->remote_id == table_id) {
1222 curpeer->remote_table = st;
1223 break;
1224 }
1225 }
1226
Emeric Brun2b920a12010-09-23 18:30:22 +02001227 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001228 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001229 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1230 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1231 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001232 struct shared_table *st = curpeer->remote_table;
1233 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001234 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001235 unsigned int data_type;
1236 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001237
Emeric Brunb3971ab2015-05-12 18:49:09 +02001238 /* Here we have data message */
1239 if (!st)
1240 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001241
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001242 expire = MS_TO_TICKS(st->table->expire);
1243
1244 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1245 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001246 if (msg_len < sizeof(update)) {
1247 /* malformed message */
1248 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1249 goto switchstate;
1250 }
1251 memcpy(&update, msg_cur, sizeof(update));
1252 msg_cur += sizeof(update);
1253 st->last_get = htonl(update);
1254 }
1255 else {
1256 st->last_get++;
1257 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001258
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001259 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1260 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1261 size_t expire_sz = sizeof expire;
1262
1263 if (msg_cur + expire_sz > msg_end) {
1264 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1265 goto switchstate;
1266 }
1267 memcpy(&expire, msg_cur, expire_sz);
1268 msg_cur += expire_sz;
1269 expire = ntohl(expire);
1270 }
1271
Emeric Brunb3971ab2015-05-12 18:49:09 +02001272 newts = stksess_new(st->table, NULL);
1273 if (!newts)
1274 goto ignore_msg;
Emeric Brun55482912018-01-22 15:10:08 +01001275
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001276 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001277 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001278
Emeric Brunb3971ab2015-05-12 18:49:09 +02001279 to_read = intdecode(&msg_cur, msg_end);
1280 if (!msg_cur) {
1281 /* malformed message */
1282 stksess_free(st->table, newts);
1283 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1284 goto switchstate;
1285 }
1286 to_store = MIN(to_read, st->table->key_size - 1);
1287 if (msg_cur + to_store > msg_end) {
1288 /* malformed message */
1289 stksess_free(st->table, newts);
1290 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1291 goto switchstate;
1292 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001293
Emeric Brunb3971ab2015-05-12 18:49:09 +02001294 memcpy(newts->key.key, msg_cur, to_store);
1295 newts->key.key[to_store] = 0;
1296 msg_cur += to_read;
1297 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001298 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001299 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001300
Emeric Brunb3971ab2015-05-12 18:49:09 +02001301 if (msg_cur + sizeof(netinteger) > msg_end) {
1302 /* malformed message */
1303 stksess_free(st->table, newts);
1304 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1305 goto switchstate;
1306 }
1307 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1308 netinteger = ntohl(netinteger);
1309 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1310 msg_cur += sizeof(netinteger);
1311 }
1312 else {
1313 if (msg_cur + st->table->key_size > msg_end) {
1314 /* malformed message */
1315 stksess_free(st->table, newts);
1316 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1317 goto switchstate;
1318 }
1319 memcpy(newts->key.key, msg_cur, st->table->key_size);
1320 msg_cur += st->table->key_size;
1321 }
1322
1323 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001324 ts = stktable_set_entry(st->table, newts);
1325 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001326 stksess_free(st->table, newts);
1327 newts = NULL;
1328 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001329
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001330 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001331
Emeric Brun94900952015-06-11 18:25:54 +02001332 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001333
Emeric Brun94900952015-06-11 18:25:54 +02001334 if ((1 << data_type) & st->remote_data) {
1335 switch (stktable_data_types[data_type].std_type) {
1336 case STD_T_SINT: {
1337 int data;
1338
1339 data = intdecode(&msg_cur, msg_end);
1340 if (!msg_cur) {
1341 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001342 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001343 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001344 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1345 goto switchstate;
1346 }
1347
1348 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1349 if (data_ptr)
1350 stktable_data_cast(data_ptr, std_t_sint) = data;
1351 break;
1352 }
1353 case STD_T_UINT: {
1354 unsigned int data;
1355
1356 data = intdecode(&msg_cur, msg_end);
1357 if (!msg_cur) {
1358 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001359 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001360 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001361 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1362 goto switchstate;
1363 }
1364
1365 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1366 if (data_ptr)
1367 stktable_data_cast(data_ptr, std_t_uint) = data;
1368 break;
1369 }
1370 case STD_T_ULL: {
1371 unsigned long long data;
1372
1373 data = intdecode(&msg_cur, msg_end);
1374 if (!msg_cur) {
1375 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001376 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001377 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001378 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1379 goto switchstate;
1380 }
1381
1382 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1383 if (data_ptr)
1384 stktable_data_cast(data_ptr, std_t_ull) = data;
1385 break;
1386 }
1387 case STD_T_FRQP: {
1388 struct freq_ctr_period data;
1389
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01001390 /* First bit is reserved for the freq_ctr_period lock
1391 Note: here we're still protected by the stksess lock
1392 so we don't need to update the update the freq_ctr_period
1393 using its internal lock */
1394
1395 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)) & ~0x1;
Emeric Brun94900952015-06-11 18:25:54 +02001396 if (!msg_cur) {
1397 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001398 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001399 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001400 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1401 goto switchstate;
1402 }
1403 data.curr_ctr = intdecode(&msg_cur, msg_end);
1404 if (!msg_cur) {
1405 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001406 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001407 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001408 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1409 goto switchstate;
1410 }
1411 data.prev_ctr = intdecode(&msg_cur, msg_end);
1412 if (!msg_cur) {
1413 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001414 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001415 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001416 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1417 goto switchstate;
1418 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001419
Emeric Brun94900952015-06-11 18:25:54 +02001420 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1421 if (data_ptr)
1422 stktable_data_cast(data_ptr, std_t_frqp) = data;
1423 break;
1424 }
1425 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001426 }
1427 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001428
Emeric Brun55482912018-01-22 15:10:08 +01001429 /* Force new expiration */
1430 ts->expire = tick_add(now_ms, expire);
1431
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001432 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001433 stktable_touch_remote(st->table, ts, 1);
1434
Emeric Brunb3971ab2015-05-12 18:49:09 +02001435 }
1436 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1437 /* ack message */
1438 uint32_t table_id ;
1439 uint32_t update;
1440 struct shared_table *st;
1441
1442 table_id = intdecode(&msg_cur, msg_end);
1443 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1444 /* malformed message */
1445 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1446 goto switchstate;
1447 }
1448 memcpy(&update, msg_cur, sizeof(update));
1449 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001450
Emeric Brunb3971ab2015-05-12 18:49:09 +02001451 for (st = curpeer->tables; st; st = st->next) {
1452 if (st->local_id == table_id) {
1453 st->update = update;
1454 break;
1455 }
1456 }
1457 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001458 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001459 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1460 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001461 goto switchstate;
1462 }
1463
Emeric Brunb3971ab2015-05-12 18:49:09 +02001464ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001465 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001466 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001467 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001468 goto switchstate;
1469
Emeric Brun2b920a12010-09-23 18:30:22 +02001470incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001471 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001472
Willy Tarreau72d6c162013-04-11 16:14:13 +02001473 if (reql < 0) {
1474 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001475 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001476 goto switchstate;
1477 }
1478
Emeric Brun2b920a12010-09-23 18:30:22 +02001479
Emeric Brun2b920a12010-09-23 18:30:22 +02001480
Emeric Brunb3971ab2015-05-12 18:49:09 +02001481
Emeric Brun2b920a12010-09-23 18:30:22 +02001482 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001483 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1484 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1485 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001486 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001487
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001488 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001489 msg[0] = PEER_MSG_CLASS_CONTROL;
1490 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001491
Emeric Brunb3971ab2015-05-12 18:49:09 +02001492 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001493 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001494 if (repl <= 0) {
1495 /* no more write possible */
1496 if (repl == -1)
1497 goto full;
1498 appctx->st0 = PEER_SESS_ST_END;
1499 goto switchstate;
1500 }
1501 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
1502 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001503
Emeric Brunb3971ab2015-05-12 18:49:09 +02001504 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 if (curpeer->tables) {
1506 struct shared_table *st;
1507 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001508
Emeric Brunb3971ab2015-05-12 18:49:09 +02001509 last_local_table = curpeer->last_local_table;
1510 if (!last_local_table)
1511 last_local_table = curpeer->tables;
1512 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001513
Emeric Brunb3971ab2015-05-12 18:49:09 +02001514 while (1) {
1515 if (!st)
1516 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001517
Emeric Brunb3971ab2015-05-12 18:49:09 +02001518 /* It remains some updates to ack */
1519 if (st->last_get != st->last_acked) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001520 repl = peer_send_ackmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001521 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001522 if (repl == -1)
1523 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001524 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001525 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001526 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001527 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001528
Emeric Brunb3971ab2015-05-12 18:49:09 +02001529 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001530 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001531 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1532 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1533 struct eb32_node *eb;
1534 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001535
Emeric Brunb3971ab2015-05-12 18:49:09 +02001536 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001537 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001538 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001539 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001540 if (repl == -1)
1541 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001542 goto switchstate;
1543 }
1544 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001545 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001546
1547 /* We force new pushed to 1 to force identifier in update message */
1548 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001549 while (1) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001550 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001551 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001552
1553 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001554 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001555 if (!eb) {
1556 eb = eb32_first(&st->table->updates);
1557 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001558 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001559 break;
1560 }
1561 }
1562
1563 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001564 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001565 break;
1566 }
1567
1568 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001569 updateid = ts->upd.key;
1570 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001571 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001572
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001573 repl = peer_send_updatemsg(st, appctx, ts,
1574 updateid, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001575 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001576 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001577 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001578 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001579 if (repl == -1)
1580 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001581 goto switchstate;
1582 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001583
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001584 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001585 ts->ref_cnt--;
1586 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001587 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1588 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001589 /* identifier may not needed in next update message */
1590 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001591 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001592 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001593 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001594 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001595 else {
1596 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1597 struct eb32_node *eb;
1598 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001599
Emeric Brunb3971ab2015-05-12 18:49:09 +02001600 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001601 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001602 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001603 if (repl == -1)
1604 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001605 goto switchstate;
1606 }
1607 curpeer->last_local_table = st;
1608 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001609
Emeric Brunb3971ab2015-05-12 18:49:09 +02001610 /* We force new pushed to 1 to force identifier in update message */
1611 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001612 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001613 while (1) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001614 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001615 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001616 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001617
Emeric Brunb3971ab2015-05-12 18:49:09 +02001618 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001619 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001620 if (!eb) {
1621 st->flags |= SHTABLE_F_TEACH_STAGE1;
1622 eb = eb32_first(&st->table->updates);
1623 if (eb)
1624 st->last_pushed = eb->key - 1;
1625 break;
1626 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001627
Emeric Brunb3971ab2015-05-12 18:49:09 +02001628 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001629 updateid = ts->upd.key;
1630 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001631 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001632
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001633 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001634 repl = peer_send_updatemsg(st, appctx, ts,
1635 updateid, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001636 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001637 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001638 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001639 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001640 if (repl == -1)
1641 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001642 goto switchstate;
1643 }
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001644
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001645 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001646 ts->ref_cnt--;
1647 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001648 /* identifier may not needed in next update message */
1649 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001650 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001651 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001652 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001653
Emeric Brunb3971ab2015-05-12 18:49:09 +02001654 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1655 struct eb32_node *eb;
1656 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001657
Emeric Brunb3971ab2015-05-12 18:49:09 +02001658 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001659 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001660 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001661 if (repl == -1)
1662 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001663 goto switchstate;
1664 }
1665 curpeer->last_local_table = st;
1666 }
1667
1668 /* We force new pushed to 1 to force identifier in update message */
1669 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001670 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001671 while (1) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001672 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001673 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001674 unsigned updateid;
1675
1676 /* push local updates */
1677 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001678
1679 /* push local updates */
1680 if (!eb || eb->key > st->teaching_origin) {
1681 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001682 break;
1683 }
1684
1685 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001686 updateid = ts->upd.key;
1687 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001688 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001689
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001690 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001691 repl = peer_send_updatemsg(st, appctx, ts,
1692 updateid, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001693 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001694 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001695 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001696 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001697 if (repl == -1)
1698 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001699 goto switchstate;
1700 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001701
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001702 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001703 ts->ref_cnt--;
1704 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001705 /* identifier may not needed in next update message */
1706 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001707 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001708 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001709 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001710 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001711
1712 if (st == last_local_table)
1713 break;
1714 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001715 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001716 }
1717
1718
1719 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1720 unsigned char msg[2];
1721
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001722 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001723 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001724 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 +02001725 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001726 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001727 if (repl <= 0) {
1728 /* no more write possible */
1729 if (repl == -1)
1730 goto full;
1731 appctx->st0 = PEER_SESS_ST_END;
1732 goto switchstate;
1733 }
1734 /* flag finished message sent */
1735 curpeer->flags |= PEER_F_TEACH_FINISHED;
1736 }
1737
Emeric Brun597b26e2016-08-12 11:23:31 +02001738 /* Confirm finished or partial messages */
1739 while (curpeer->confirm) {
1740 unsigned char msg[2];
1741
1742 /* There is a confirm messages to send */
1743 msg[0] = PEER_MSG_CLASS_CONTROL;
1744 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1745
1746 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001747 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001748 if (repl <= 0) {
1749 /* no more write possible */
1750 if (repl == -1)
1751 goto full;
1752 appctx->st0 = PEER_SESS_ST_END;
1753 goto switchstate;
1754 }
1755 curpeer->confirm--;
1756 }
1757
Emeric Brun2b920a12010-09-23 18:30:22 +02001758 /* noting more to do */
1759 goto out;
1760 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001761 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001762 if (prev_state == PEER_SESS_ST_WAITMSG)
1763 HA_ATOMIC_SUB(&connected_peers, 1);
1764 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001765 repl = snprintf(trash.area, trash.size,
1766 "%d\n", appctx->st1);
1767 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001768 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001769 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001770 goto switchstate;
1771 case PEER_SESS_ST_ERRSIZE: {
1772 unsigned char msg[2];
1773
Willy Tarreau2d372c22018-11-05 17:12:27 +01001774 if (prev_state == PEER_SESS_ST_WAITMSG)
1775 HA_ATOMIC_SUB(&connected_peers, 1);
1776 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001777 msg[0] = PEER_MSG_CLASS_ERROR;
1778 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1779
Willy Tarreau06d80a92017-10-19 14:32:15 +02001780 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781 goto full;
1782 appctx->st0 = PEER_SESS_ST_END;
1783 goto switchstate;
1784 }
1785 case PEER_SESS_ST_ERRPROTO: {
1786 unsigned char msg[2];
1787
Willy Tarreau2d372c22018-11-05 17:12:27 +01001788 if (prev_state == PEER_SESS_ST_WAITMSG)
1789 HA_ATOMIC_SUB(&connected_peers, 1);
1790 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001791 msg[0] = PEER_MSG_CLASS_ERROR;
1792 msg[1] = PEER_MSG_ERR_PROTOCOL;
1793
Willy Tarreau06d80a92017-10-19 14:32:15 +02001794 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001795 goto full;
1796 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001797 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001798 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001799 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001800 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001801 if (prev_state == PEER_SESS_ST_WAITMSG)
1802 HA_ATOMIC_SUB(&connected_peers, 1);
1803 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001804 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001805 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001806 curpeer = NULL;
1807 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001808 si_shutw(si);
1809 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001810 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001811 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001812 }
1813 }
1814 }
1815out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001816 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001817
1818 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001819 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001820 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001821full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001822 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001823 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001824}
1825
Willy Tarreau30576452015-04-13 13:50:30 +02001826static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001827 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001828 .name = "<PEER>", /* used for logging */
1829 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001830 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001831};
Emeric Brun2b920a12010-09-23 18:30:22 +02001832
1833/*
1834 * Use this function to force a close of a peer session
1835 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001836static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001837{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001838 /* Note that the peer sessions which have just been created
1839 * (->st0 == PEER_SESS_ST_CONNECT) must not
1840 * be shutdown, if not, the TCP session will never be closed
1841 * and stay in CLOSE_WAIT state after having been closed by
1842 * the remote side.
1843 */
1844 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001845 return;
1846
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001847 if (appctx->applet != &peer_applet)
1848 return;
1849
Willy Tarreau2d372c22018-11-05 17:12:27 +01001850 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
1851 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001852 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001853 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001854}
1855
Willy Tarreau91d96282015-03-13 15:47:26 +01001856/* Pre-configures a peers frontend to accept incoming connections */
1857void peers_setup_frontend(struct proxy *fe)
1858{
1859 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001860 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01001861 fe->maxconn = 0;
1862 fe->conn_retries = CONN_RETRIES;
1863 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001864 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001865 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001866 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001867 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001868}
1869
Emeric Brun2b920a12010-09-23 18:30:22 +02001870/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001871 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001872 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001873static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001874{
Willy Tarreau04b92862017-09-15 11:01:04 +02001875 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001876 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001877 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001878 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001879 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001880 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001881
Emeric Brunb3971ab2015-05-12 18:49:09 +02001882 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1883 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001884 s = NULL;
1885
Emeric Brun1138fd02017-06-19 12:38:55 +02001886 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001887 if (!appctx)
1888 goto out_close;
1889
1890 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001891 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001892
Willy Tarreau04b92862017-09-15 11:01:04 +02001893 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001894 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001895 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001896 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001897 }
1898
Willy Tarreau87787ac2017-08-28 16:22:54 +02001899 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001900 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001901 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001902 }
1903
Willy Tarreau342bfb12015-04-05 01:35:34 +02001904 /* The tasks below are normally what is supposed to be done by
1905 * fe->accept().
1906 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001907 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001908
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001909 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001910 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001911 appctx_wakeup(appctx);
1912
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001913 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001914 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001915 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001916
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001917 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001918 * pre-initialized connection in si->conn.
1919 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001920 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001921 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001922
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001923 if (unlikely((cs = cs_new(conn)) == NULL))
1924 goto out_free_conn;
1925
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001926 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02001927 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
1928
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001929 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01001930 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001931 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001932
Emeric Brun2b920a12010-09-23 18:30:22 +02001933 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001934 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001935
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001936 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001937
Emeric Brunb3971ab2015-05-12 18:49:09 +02001938 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001939 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01001940 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001941 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001942
1943 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001944 out_free_conn:
1945 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001946 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001947 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001948 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001949 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001950 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001951 out_free_appctx:
1952 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001953 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001954 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001955}
1956
1957/*
1958 * Task processing function to manage re-connect and peer session
1959 * tasks wakeup on local update.
1960 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001961static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02001962{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001963 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001964 struct peer *ps;
1965 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001966
1967 task->expire = TICK_ETERNITY;
1968
Emeric Brunb3971ab2015-05-12 18:49:09 +02001969 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001970 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001971 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001972 task_delete(peers->sync_task);
1973 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001974 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001975 return NULL;
1976 }
1977
Emeric Brun80527f52017-06-19 17:46:37 +02001978 /* Acquire lock for all peers of the section */
1979 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001980 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001981
Emeric Brun2b920a12010-09-23 18:30:22 +02001982 if (!stopping) {
1983 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001984
1985 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1986 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1987 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001988 /* Resync from local peer needed
1989 no peer was assigned for the lesson
1990 and no old local peer found
1991 or resync timeout expire */
1992
1993 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001994 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001995
1996 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001997 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001998 }
1999
2000 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002001 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002002 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002003 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002004 if (!ps->appctx) {
2005 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002006 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002007 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002008 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002009 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002010 tick_is_expired(ps->reconnect, now_ms))) {
2011 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002012 * or previous peer connection established with success
2013 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002014 * and reconnection timer is expired */
2015
2016 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002017 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002018 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002019 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002020 /* If previous session failed during connection
2021 * but reconnection timer is not expired */
2022
2023 /* reschedule task for reconnect */
2024 task->expire = tick_first(task->expire, ps->reconnect);
2025 }
2026 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002027 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002028 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002029 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002030 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2031 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002032 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2033 /* Resync from a remote is needed
2034 * and no peer was assigned for lesson
2035 * and current peer may be up2date */
2036
2037 /* assign peer for the lesson */
2038 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002039 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002040
Willy Tarreau9df94c22016-10-31 18:42:52 +01002041 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002042 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002043 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002044 else {
2045 /* Awake session if there is data to push */
2046 for (st = ps->tables; st ; st = st->next) {
2047 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002048 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002049 appctx_wakeup(ps->appctx);
2050 break;
2051 }
2052 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 }
2054 /* else do nothing */
2055 } /* SUCCESSCODE */
2056 } /* !ps->peer->local */
2057 } /* for */
2058
2059 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002060 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2061 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2062 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002063 /* Resync from remote peer needed
2064 * no peer was assigned for the lesson
2065 * and resync timeout expire */
2066
2067 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002069 }
2070
Emeric Brunb3971ab2015-05-12 18:49:09 +02002071 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002073 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2074 if (!tick_is_expired(peers->resync_timeout, now_ms))
2075 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002076 }
2077 } /* !stopping */
2078 else {
2079 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002080 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002081 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002082 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002083 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002084 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002085 peers->flags |= PEERS_F_DONOTSTOP;
2086 ps = peers->local;
2087 for (st = ps->tables; st ; st = st->next)
2088 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 }
2090
2091 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002092 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002093 /* we're killing a connection, we must apply a random delay before
2094 * retrying otherwise the other end will do the same and we can loop
2095 * for a while.
2096 */
2097 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002098 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002099 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002100 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002101 }
2102 }
2103 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002104
Emeric Brunb3971ab2015-05-12 18:49:09 +02002105 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002107 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002108 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002109 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110 peers->flags &= ~PEERS_F_DONOTSTOP;
2111 for (st = ps->tables; st ; st = st->next)
2112 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002113 }
2114 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002115 else if (!ps->appctx) {
2116 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002117 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002118 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2119 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2120 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002121 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002122 * or previous peer connection was successfully established
2123 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002124 * or during previous connect, peer replies a try again statuscode */
2125
2126 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002127 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 }
2129 else {
2130 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002131 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002132 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002133 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002134 peers->flags &= ~PEERS_F_DONOTSTOP;
2135 for (st = ps->tables; st ; st = st->next)
2136 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002137 }
2138 }
2139 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002140 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002141 /* current peer connection is active and established
2142 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002143 for (st = ps->tables; st ; st = st->next) {
2144 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002145 appctx_wakeup(ps->appctx);
2146 break;
2147 }
2148 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002149 }
2150 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002151
2152 /* Release lock for all peers of the section */
2153 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002154 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002155
Emeric Brun2b920a12010-09-23 18:30:22 +02002156 /* Wakeup for re-connect */
2157 return task;
2158}
2159
Emeric Brunb3971ab2015-05-12 18:49:09 +02002160
Emeric Brun2b920a12010-09-23 18:30:22 +02002161/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002162 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002163 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002164int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002165{
Emeric Brun2b920a12010-09-23 18:30:22 +02002166 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002167 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002168
Emeric Brun2b920a12010-09-23 18:30:22 +02002169 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002170 peers->peers_fe->maxconn += 3;
2171 }
2172
Willy Tarreau4348fad2012-09-20 16:48:07 +02002173 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2174 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002175 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002176 if (!peers->sync_task)
2177 return 0;
2178
Emeric Brunb3971ab2015-05-12 18:49:09 +02002179 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002180 peers->sync_task->context = (void *)peers;
2181 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2182 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002183 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002184}
2185
2186
2187
2188/*
2189 * Function used to register a table for sync on a group of peers
2190 *
2191 */
2192void peers_register_table(struct peers *peers, struct stktable *table)
2193{
2194 struct shared_table *st;
2195 struct peer * curpeer;
2196 int id = 0;
2197
2198 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002199 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002200 st->table = table;
2201 st->next = curpeer->tables;
2202 if (curpeer->tables)
2203 id = curpeer->tables->local_id;
2204 st->local_id = id + 1;
2205
2206 curpeer->tables = st;
2207 }
2208
2209 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002210}
2211