blob: d997f52eaefe3e692d00e2390e4b95241605fa62 [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 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200270static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, unsigned int updateid, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200271{
272 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200273 unsigned short datalen;
274 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200275 unsigned int data_type;
276 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200277
278 cursor = datamsg = msg + 1 + 5;
279
Emeric Brun2b920a12010-09-23 18:30:22 +0200280 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200281
282 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200283 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200284 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200285 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200286
287 /* encode update identifier if needed */
288 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200289 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200290 memcpy(cursor, &netinteger, sizeof(netinteger));
291 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200292 }
293
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200294 if (use_timed) {
295 netinteger = htonl(tick_remain(now_ms, ts->expire));
296 memcpy(cursor, &netinteger, sizeof(netinteger));
297 cursor += sizeof(netinteger);
298 }
299
Emeric Brunb3971ab2015-05-12 18:49:09 +0200300 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200301 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200302 int stlen = strlen((char *)ts->key.key);
303
Emeric Brunb3971ab2015-05-12 18:49:09 +0200304 intencode(stlen, &cursor);
305 memcpy(cursor, ts->key.key, stlen);
306 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200307 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200308 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200309 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200310 memcpy(cursor, &netinteger, sizeof(netinteger));
311 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200312 }
313 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200314 memcpy(cursor, ts->key.key, st->table->key_size);
315 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200316 }
317
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200319 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200320 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200321
Emeric Brun94900952015-06-11 18:25:54 +0200322 data_ptr = stktable_data_ptr(st->table, ts, data_type);
323 if (data_ptr) {
324 switch (stktable_data_types[data_type].std_type) {
325 case STD_T_SINT: {
326 int data;
327
328 data = stktable_data_cast(data_ptr, std_t_sint);
329 intencode(data, &cursor);
330 break;
331 }
332 case STD_T_UINT: {
333 unsigned int data;
334
335 data = stktable_data_cast(data_ptr, std_t_uint);
336 intencode(data, &cursor);
337 break;
338 }
339 case STD_T_ULL: {
340 unsigned long long data;
341
342 data = stktable_data_cast(data_ptr, std_t_ull);
343 intencode(data, &cursor);
344 break;
345 }
346 case STD_T_FRQP: {
347 struct freq_ctr_period *frqp;
348
349 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
350 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
351 intencode(frqp->curr_ctr, &cursor);
352 intencode(frqp->prev_ctr, &cursor);
353 break;
354 }
355 }
356 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200357 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100358 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200359
360 /* Compute datalen */
361 datalen = (cursor - datamsg);
362
363 /* prepare message header */
364 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200365 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200366 cursor = &msg[2];
367 intencode(datalen, &cursor);
368
369 /* move data after header */
370 memmove(cursor, datamsg, datalen);
371
372 /* return header size + data_len */
373 return (cursor - msg) + datalen;
374}
375
376/*
377 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800378 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200379 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
380 * check size)
381 */
382static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
383{
384 int len;
385 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200386 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200387 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200388 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200389 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390
391 cursor = datamsg = msg + 2 + 5;
392
393 /* Encode data */
394
395 /* encode local id */
396 intencode(st->local_id, &cursor);
397
398 /* encode table name */
399 len = strlen(st->table->id);
400 intencode(len, &cursor);
401 memcpy(cursor, st->table->id, len);
402 cursor += len;
403
404 /* encode table type */
405
406 intencode(st->table->type, &cursor);
407
408 /* encode table key size */
409 intencode(st->table->key_size, &cursor);
410
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200411 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200413 /* encode available known data types in table */
414 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
415 if (st->table->data_ofs[data_type]) {
416 switch (stktable_data_types[data_type].std_type) {
417 case STD_T_SINT:
418 case STD_T_UINT:
419 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200420 data |= 1 << data_type;
421 break;
Emeric Brun94900952015-06-11 18:25:54 +0200422 case STD_T_FRQP:
423 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200424 intencode(data_type, &chunkq);
425 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200426 break;
427 }
428 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200429 }
430 intencode(data, &cursor);
431
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200432 /* Encode stick-table entries duration. */
433 intencode(st->table->expire, &cursor);
434
435 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200436 chunk->data = chunkq - chunkp;
437 memcpy(cursor, chunk->area, chunk->data);
438 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200439 }
440
Emeric Brunb3971ab2015-05-12 18:49:09 +0200441 /* Compute datalen */
442 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200443
Emeric Brunb3971ab2015-05-12 18:49:09 +0200444 /* prepare message header */
445 msg[0] = PEER_MSG_CLASS_STICKTABLE;
446 msg[1] = PEER_MSG_STKT_DEFINE;
447 cursor = &msg[2];
448 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200449
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450 /* move data after header */
451 memmove(cursor, datamsg, datalen);
452
453 /* return header size + data_len */
454 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200455}
456
Emeric Brunb3971ab2015-05-12 18:49:09 +0200457/*
458 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
459 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800460 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200461 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
462 * check size)
463 */
464static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
465{
466 unsigned short datalen;
467 char *cursor, *datamsg;
468 uint32_t netinteger;
469
Emeric Brunb058f1c2015-09-22 15:50:18 +0200470 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200471
472 intencode(st->remote_id, &cursor);
473 netinteger = htonl(st->last_get);
474 memcpy(cursor, &netinteger, sizeof(netinteger));
475 cursor += sizeof(netinteger);
476
477 /* Compute datalen */
478 datalen = (cursor - datamsg);
479
480 /* prepare message header */
481 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200482 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483 cursor = &msg[2];
484 intencode(datalen, &cursor);
485
486 /* move data after header */
487 memmove(cursor, datamsg, datalen);
488
489 /* return header size + data_len */
490 return (cursor - msg) + datalen;
491}
Emeric Brun2b920a12010-09-23 18:30:22 +0200492
493/*
494 * Callback to release a session with a peer
495 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200496static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200497{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200498 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200499 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200500 struct peer *peer = appctx->ctx.peers.ptr;
501 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200502
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100503 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100504 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200505 return;
506
507 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200508 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100509 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
510 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100511 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100512 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100513 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200514 /* Re-init current table pointers to force announcement on re-connect */
515 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 peer->appctx = NULL;
517 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200518 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
520 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200521
522 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200523 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200524 }
525 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 peer->flags &= PEER_TEACH_RESET;
527 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200528 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100529 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200530 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200531 }
532}
533
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200534/* Retrieve the major and minor versions of peers protocol
535 * announced by a remote peer. <str> is a null-terminated
536 * string with the following format: "<maj_ver>.<min_ver>".
537 */
538static int peer_get_version(const char *str,
539 unsigned int *maj_ver, unsigned int *min_ver)
540{
541 unsigned int majv, minv;
542 const char *pos, *saved;
543 const char *end;
544
545 saved = pos = str;
546 end = str + strlen(str);
547
548 majv = read_uint(&pos, end);
549 if (saved == pos || *pos++ != '.')
550 return -1;
551
552 saved = pos;
553 minv = read_uint(&pos, end);
554 if (saved == pos || pos != end)
555 return -1;
556
557 *maj_ver = majv;
558 *min_ver = minv;
559
560 return 0;
561}
Emeric Brun2b920a12010-09-23 18:30:22 +0200562
563/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100564 * Parse a line terminated by an optional '\r' character, followed by a mandatory
565 * '\n' character.
566 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
567 * a line could not be read because the communication channel is closed.
568 */
569static inline int peer_getline(struct appctx *appctx)
570{
571 int n;
572 struct stream_interface *si = appctx->owner;
573
574 n = co_getline(si_oc(si), trash.area, trash.size);
575 if (!n)
576 return 0;
577
578 if (n < 0 || trash.area[n - 1] != '\n') {
579 appctx->st0 = PEER_SESS_ST_END;
580 return -1;
581 }
582
583 if (n > 1 && (trash.area[n - 2] == '\r'))
584 trash.area[n - 2] = 0;
585 else
586 trash.area[n - 1] = 0;
587
588 co_skip(si_oc(si), n);
589
590 return n;
591}
592
593/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100594 * Send a message after having called <peer_prepare_msg> to build it.
595 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
596 * Returns -1 if there was not enough room left to send the message,
597 * any other negative returned value must be considered as an error with an appcxt st0
598 * returned value equal to PEER_SESS_ST_END.
599 */
600static inline int peer_send_msg(struct shared_table *st, struct appctx *appctx,
601 int (*peer_prepare_msg)(struct shared_table *, char *, size_t))
602{
603 int ret, msglen;
604 struct stream_interface *si = appctx->owner;
605
606 msglen = peer_prepare_msg(st, trash.area, trash.size);
607 if (!msglen) {
608 /* internal error: message does not fit in trash */
609 appctx->st0 = PEER_SESS_ST_END;
610 return 0;
611 }
612
613 /* message to buffer */
614 ret = ci_putblk(si_ic(si), trash.area, msglen);
615 if (ret <= 0) {
616 if (ret == -1) {
617 /* No more write possible */
618 si_rx_room_blk(si);
619 return -1;
620 }
621 appctx->st0 = PEER_SESS_ST_END;
622 }
623
624 return ret;
625}
626
627/*
628 * Send a stick-table switch message.
629 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
630 * Returns -1 if there was not enough room left to send the message,
631 * any other negative returned value must be considered as an error with an appcxt st0
632 * returned value equal to PEER_SESS_ST_END.
633 */
634static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
635{
636 return peer_send_msg(st, appctx, peer_prepare_switchmsg);
637}
638
639/*
640 * Send a stick-table update acknowledgement message.
641 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
642 * Returns -1 if there was not enough room left to send the message,
643 * any other negative returned value must be considered as an error with an appcxt st0
644 * returned value equal to PEER_SESS_ST_END.
645 */
646static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
647{
648 return peer_send_msg(st, appctx, peer_prepare_ackmsg);
649}
650
651/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 * IO Handler to handle message exchance with a peer
653 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200654static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200655{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200656 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200657 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200658 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200659 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200660 int reql = 0;
661 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200662 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
663 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100664 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +0200665
Joseph Herlant82b2f542018-11-15 12:19:14 -0800666 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200667 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100668 goto full;
669
Emeric Brun2b920a12010-09-23 18:30:22 +0200670 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100671 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200672switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200673 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100674 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100675 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100676 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100677 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100678 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200679 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100680 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100681 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200682
Frédéric Lécaillece025572019-01-21 13:38:06 +0100683 reql = peer_getline(appctx);
684 if (!reql)
685 goto out;
686
687 if (reql < 0)
688 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200689
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200690 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200691 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200692 appctx->st0 = PEER_SESS_ST_EXIT;
693 appctx->st1 = PEER_SESS_SC_ERRPROTO;
694 goto switchstate;
695 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200697 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100698 appctx->st0 = PEER_SESS_ST_EXIT;
699 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200700 goto switchstate;
701 }
702
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100703 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200704 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100705 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100706 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200707
Frédéric Lécaillece025572019-01-21 13:38:06 +0100708 reql = peer_getline(appctx);
709 if (!reql)
710 goto out;
711
712 if (reql < 0)
713 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200714
715 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100717 appctx->st0 = PEER_SESS_ST_EXIT;
718 appctx->st1 = PEER_SESS_SC_ERRHOST;
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_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200723 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100724 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200725 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100726
727 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200728
Frédéric Lécaillece025572019-01-21 13:38:06 +0100729 reql = peer_getline(appctx);
730 if (!reql)
731 goto out;
732
733 if (reql < 0)
734 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200735
Emeric Brunb3971ab2015-05-12 18:49:09 +0200736 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200738 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100739 appctx->st0 = PEER_SESS_ST_EXIT;
740 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200741 goto switchstate;
742 }
743 *p = 0;
744
745 /* lookup known peer */
746 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200748 break;
749 }
750
751 /* if unknown peer */
752 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100753 appctx->st0 = PEER_SESS_ST_EXIT;
754 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200755 goto switchstate;
756 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200757
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100758 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100759 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200760 if (curpeer->local) {
761 /* Local connection, reply a retry */
762 appctx->st0 = PEER_SESS_ST_EXIT;
763 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
764 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200765 }
Emeric Brun80527f52017-06-19 17:46:37 +0200766
767 /* we're killing a connection, we must apply a random delay before
768 * retrying otherwise the other end will do the same and we can loop
769 * for a while.
770 */
771 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100772 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200773 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200774 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
775 if (min_ver == PEER_DWNGRD_MINOR_VER) {
776 curpeer->flags |= PEER_F_DWNGRD;
777 }
778 else {
779 curpeer->flags &= ~PEER_F_DWNGRD;
780 }
781 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200782 curpeer->appctx = appctx;
783 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100784 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +0100785 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +0200786 /* fall through */
787 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100788 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200789 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200790
Willy Tarreau2d372c22018-11-05 17:12:27 +0100791 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200792 if (!curpeer) {
793 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100794 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200795 if (curpeer->appctx != appctx) {
796 appctx->st0 = PEER_SESS_ST_END;
797 goto switchstate;
798 }
799 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200800 repl = snprintf(trash.area, trash.size,
801 "%d\n",
802 PEER_SESS_SC_SUCCESSCODE);
803 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200804 if (repl <= 0) {
805 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100806 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100807 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200808 goto switchstate;
809 }
810
811 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200812 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200813
814 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200815 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200816
817 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200818 curpeer->confirm = 0;
819
820 /* Init cursors */
821 for (st = curpeer->tables; st ; st = st->next) {
822 st->last_get = st->last_acked = 0;
823 st->teaching_origin = st->last_pushed = st->update;
824 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200825
826 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200827 curpeer->flags &= PEER_TEACH_RESET;
828 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200829
830 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100831 if (curpeer->local) {
832 /* if current host need resyncfrom local and no process assined */
833 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
834 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
835 /* assign local peer for a lesson, consider lesson already requested */
836 curpeer->flags |= PEER_F_LEARN_ASSIGN;
837 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
838 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200839
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100840 }
841 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
842 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
843 /* assign peer for a lesson */
844 curpeer->flags |= PEER_F_LEARN_ASSIGN;
845 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
846 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200847
848
Emeric Brun2b920a12010-09-23 18:30:22 +0200849 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100850 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100851 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200852 goto switchstate;
853 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100854 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100855 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200856 if (!curpeer) {
857 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100858 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200859 if (curpeer->appctx != appctx) {
860 appctx->st0 = PEER_SESS_ST_END;
861 goto switchstate;
862 }
863 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200864
865 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200866 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200867 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
868 PEER_MAJOR_VER,
869 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200870 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200871 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100872 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200873 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200874
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100875 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100876 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200877 goto switchstate;
878 }
879
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200880 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200881 if (repl <= 0) {
882 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100883 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100884 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200885 goto switchstate;
886 }
887
888 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100889 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200890 /* fall through */
891 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100892 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200893 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200894
Willy Tarreau2d372c22018-11-05 17:12:27 +0100895 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200896 if (!curpeer) {
897 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100898 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200899 if (curpeer->appctx != appctx) {
900 appctx->st0 = PEER_SESS_ST_END;
901 goto switchstate;
902 }
903 }
904
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100905 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200906 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200907
Frédéric Lécaillece025572019-01-21 13:38:06 +0100908 reql = peer_getline(appctx);
909 if (!reql)
910 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +0200911
Frédéric Lécaillece025572019-01-21 13:38:06 +0100912 if (reql < 0)
913 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200914
915 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200916 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +0200917
918 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200919 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200920
921 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200922 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200923 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200924 for (st = curpeer->tables; st ; st = st->next) {
925 st->last_get = st->last_acked = 0;
926 st->teaching_origin = st->last_pushed = st->update;
927 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200928
929 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100930 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200931
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100932 /* reset teaching and learning flags to 0 */
933 curpeer->flags &= PEER_TEACH_RESET;
934 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200935
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100936 /* If current peer is local */
937 if (curpeer->local) {
938 /* flag to start to teach lesson */
939 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200940
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100941 }
942 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
943 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
944 /* If peer is remote and resync from remote is needed,
945 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200946
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100947 /* assign peer for a lesson */
948 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200949 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200950 }
951
952 }
953 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200954 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
955 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200956 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100957 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200958 goto switchstate;
959 }
Willy Tarreau2d372c22018-11-05 17:12:27 +0100960 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100961 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200962 /* fall through */
963 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100964 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200965 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200966 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200967 char *msg_cur = trash.area;
968 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200969 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200970 int totl = 0;
971
Willy Tarreau2d372c22018-11-05 17:12:27 +0100972 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200973 if (!curpeer) {
974 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100975 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200976 if (curpeer->appctx != appctx) {
977 appctx->st0 = PEER_SESS_ST_END;
978 goto switchstate;
979 }
980 }
981
Willy Tarreau06d80a92017-10-19 14:32:15 +0200982 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200983 if (reql <= 0) /* closed or EOL not found */
984 goto incomplete;
985
Emeric Brun2b920a12010-09-23 18:30:22 +0200986 totl += reql;
987
Emeric Brunb3971ab2015-05-12 18:49:09 +0200988 if (msg_head[1] >= 128) {
989 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200990 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200991 if (reql <= 0) /* closed */
992 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200993
Emeric Brunb3971ab2015-05-12 18:49:09 +0200994 totl += reql;
995
996 if (msg_head[2] < 240) {
997 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200998 }
999 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001000 int i;
1001 char *cur;
1002 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001003
Emeric Brunb3971ab2015-05-12 18:49:09 +02001004 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02001005 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001006 if (reql <= 0) /* closed */
1007 goto incomplete;
1008
1009 totl += reql;
1010
1011 if (!(msg_head[i] & 0x80))
1012 break;
1013 }
1014
1015 if (i == sizeof(msg_head)) {
1016 /* malformed message */
1017 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1018 goto switchstate;
1019
1020 }
1021 end = (char *)msg_head + sizeof(msg_head);
1022 cur = (char *)&msg_head[2];
1023 msg_len = intdecode(&cur, end);
1024 if (!cur) {
1025 /* malformed message */
1026 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1027 goto switchstate;
1028 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001029 }
1030
Willy Tarreau86a446e2013-11-25 23:02:37 +01001031
Emeric Brunb3971ab2015-05-12 18:49:09 +02001032 /* Read message content */
1033 if (msg_len) {
1034 if (msg_len > trash.size) {
1035 /* Status code is not success, abort */
1036 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1037 goto switchstate;
1038 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001039
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001040 reql = co_getblk(si_oc(si),
1041 trash.area,
1042 msg_len,
1043 totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001044 if (reql <= 0) /* closed */
1045 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +02001046 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001047
Emeric Brunb3971ab2015-05-12 18:49:09 +02001048 msg_end += msg_len;
1049 }
1050 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001051
Emeric Brunb3971ab2015-05-12 18:49:09 +02001052 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1053 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1054 struct shared_table *st;
1055 /* Reset message: remote need resync */
1056
1057 /* prepare tables fot a global push */
1058 for (st = curpeer->tables; st; st = st->next) {
1059 st->teaching_origin = st->last_pushed = st->table->update;
1060 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001061 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001062
Emeric Brunb3971ab2015-05-12 18:49:09 +02001063 /* reset teaching flags to 0 */
1064 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001065
Emeric Brunb3971ab2015-05-12 18:49:09 +02001066 /* flag to start to teach lesson */
1067 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001068 }
1069 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001070 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1071 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001072 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1073 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001074 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001075 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001076 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001077 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001078 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1079 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001080 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001081
1082 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001083 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1084 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001085 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001086 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001087 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001088 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001089 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001090
1091 /* If stopping state */
1092 if (stopping) {
1093 /* Close session, push resync no more needed */
1094 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1095 appctx->st0 = PEER_SESS_ST_END;
1096 goto switchstate;
1097 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001098 for (st = curpeer->tables; st; st = st->next) {
1099 st->update = st->last_pushed = st->teaching_origin;
1100 st->flags = 0;
1101 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001102
1103 /* reset teaching flags to 0 */
1104 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001105 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001106 }
1107 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1108 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1109 int table_id_len;
1110 struct shared_table *st;
1111 int table_type;
1112 int table_keylen;
1113 int table_id;
1114 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001115
Emeric Brunb3971ab2015-05-12 18:49:09 +02001116 table_id = intdecode(&msg_cur, msg_end);
1117 if (!msg_cur) {
1118 /* malformed message */
1119 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1120 goto switchstate;
1121 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001122
Emeric Brunb3971ab2015-05-12 18:49:09 +02001123 table_id_len = intdecode(&msg_cur, msg_end);
1124 if (!msg_cur) {
1125 /* malformed message */
1126 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1127 goto switchstate;
1128 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001129
Emeric Brunb3971ab2015-05-12 18:49:09 +02001130 curpeer->remote_table = NULL;
1131 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1132 /* malformed message */
1133 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1134 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001135 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001136
Emeric Brunb3971ab2015-05-12 18:49:09 +02001137 for (st = curpeer->tables; st; st = st->next) {
1138 /* Reset IDs */
1139 if (st->remote_id == table_id)
1140 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001141
Emeric Brunb3971ab2015-05-12 18:49:09 +02001142 if (!curpeer->remote_table
1143 && (table_id_len == strlen(st->table->id))
1144 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1145 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001146 }
1147 }
1148
Emeric Brunb3971ab2015-05-12 18:49:09 +02001149 if (!curpeer->remote_table) {
1150 goto ignore_msg;
1151 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001152
Emeric Brunb3971ab2015-05-12 18:49:09 +02001153 msg_cur += table_id_len;
1154 if (msg_cur >= msg_end) {
1155 /* malformed message */
1156 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1157 goto switchstate;
1158 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001159
Emeric Brunb3971ab2015-05-12 18:49:09 +02001160 table_type = intdecode(&msg_cur, msg_end);
1161 if (!msg_cur) {
1162 /* malformed message */
1163 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1164 goto switchstate;
1165 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001166
Emeric Brunb3971ab2015-05-12 18:49:09 +02001167 table_keylen = intdecode(&msg_cur, msg_end);
1168 if (!msg_cur) {
1169 /* malformed message */
1170 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1171 goto switchstate;
1172 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001173
Emeric Brunb3971ab2015-05-12 18:49:09 +02001174 table_data = intdecode(&msg_cur, msg_end);
1175 if (!msg_cur) {
1176 /* malformed message */
1177 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1178 goto switchstate;
1179 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001180
Emeric Brunb3971ab2015-05-12 18:49:09 +02001181 if (curpeer->remote_table->table->type != table_type
1182 || curpeer->remote_table->table->key_size != table_keylen) {
1183 curpeer->remote_table = NULL;
1184 goto ignore_msg;
1185 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001186
Emeric Brunb3971ab2015-05-12 18:49:09 +02001187 curpeer->remote_table->remote_data = table_data;
1188 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001189 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001190 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1191 struct shared_table *st;
1192 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001193
Emeric Brunb3971ab2015-05-12 18:49:09 +02001194 table_id = intdecode(&msg_cur, msg_end);
1195 if (!msg_cur) {
1196 /* malformed message */
1197 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1198 goto switchstate;
1199 }
1200 curpeer->remote_table = NULL;
1201 for (st = curpeer->tables; st; st = st->next) {
1202 if (st->remote_id == table_id) {
1203 curpeer->remote_table = st;
1204 break;
1205 }
1206 }
1207
Emeric Brun2b920a12010-09-23 18:30:22 +02001208 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001209 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001210 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1211 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1212 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001213 struct shared_table *st = curpeer->remote_table;
1214 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001215 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001216 unsigned int data_type;
1217 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001218
Emeric Brunb3971ab2015-05-12 18:49:09 +02001219 /* Here we have data message */
1220 if (!st)
1221 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001222
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001223 expire = MS_TO_TICKS(st->table->expire);
1224
1225 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1226 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001227 if (msg_len < sizeof(update)) {
1228 /* malformed message */
1229 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1230 goto switchstate;
1231 }
1232 memcpy(&update, msg_cur, sizeof(update));
1233 msg_cur += sizeof(update);
1234 st->last_get = htonl(update);
1235 }
1236 else {
1237 st->last_get++;
1238 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001239
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001240 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1241 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1242 size_t expire_sz = sizeof expire;
1243
1244 if (msg_cur + expire_sz > msg_end) {
1245 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1246 goto switchstate;
1247 }
1248 memcpy(&expire, msg_cur, expire_sz);
1249 msg_cur += expire_sz;
1250 expire = ntohl(expire);
1251 }
1252
Emeric Brunb3971ab2015-05-12 18:49:09 +02001253 newts = stksess_new(st->table, NULL);
1254 if (!newts)
1255 goto ignore_msg;
Emeric Brun55482912018-01-22 15:10:08 +01001256
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001257 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001258 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001259
Emeric Brunb3971ab2015-05-12 18:49:09 +02001260 to_read = intdecode(&msg_cur, msg_end);
1261 if (!msg_cur) {
1262 /* malformed message */
1263 stksess_free(st->table, newts);
1264 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1265 goto switchstate;
1266 }
1267 to_store = MIN(to_read, st->table->key_size - 1);
1268 if (msg_cur + to_store > msg_end) {
1269 /* malformed message */
1270 stksess_free(st->table, newts);
1271 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1272 goto switchstate;
1273 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001274
Emeric Brunb3971ab2015-05-12 18:49:09 +02001275 memcpy(newts->key.key, msg_cur, to_store);
1276 newts->key.key[to_store] = 0;
1277 msg_cur += to_read;
1278 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001279 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001280 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001281
Emeric Brunb3971ab2015-05-12 18:49:09 +02001282 if (msg_cur + sizeof(netinteger) > msg_end) {
1283 /* malformed message */
1284 stksess_free(st->table, newts);
1285 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1286 goto switchstate;
1287 }
1288 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1289 netinteger = ntohl(netinteger);
1290 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1291 msg_cur += sizeof(netinteger);
1292 }
1293 else {
1294 if (msg_cur + st->table->key_size > msg_end) {
1295 /* malformed message */
1296 stksess_free(st->table, newts);
1297 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1298 goto switchstate;
1299 }
1300 memcpy(newts->key.key, msg_cur, st->table->key_size);
1301 msg_cur += st->table->key_size;
1302 }
1303
1304 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001305 ts = stktable_set_entry(st->table, newts);
1306 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001307 stksess_free(st->table, newts);
1308 newts = NULL;
1309 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001310
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001311 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001312
Emeric Brun94900952015-06-11 18:25:54 +02001313 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001314
Emeric Brun94900952015-06-11 18:25:54 +02001315 if ((1 << data_type) & st->remote_data) {
1316 switch (stktable_data_types[data_type].std_type) {
1317 case STD_T_SINT: {
1318 int data;
1319
1320 data = intdecode(&msg_cur, msg_end);
1321 if (!msg_cur) {
1322 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001323 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001324 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001325 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1326 goto switchstate;
1327 }
1328
1329 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1330 if (data_ptr)
1331 stktable_data_cast(data_ptr, std_t_sint) = data;
1332 break;
1333 }
1334 case STD_T_UINT: {
1335 unsigned int data;
1336
1337 data = intdecode(&msg_cur, msg_end);
1338 if (!msg_cur) {
1339 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001340 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001341 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001342 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1343 goto switchstate;
1344 }
1345
1346 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1347 if (data_ptr)
1348 stktable_data_cast(data_ptr, std_t_uint) = data;
1349 break;
1350 }
1351 case STD_T_ULL: {
1352 unsigned long long data;
1353
1354 data = intdecode(&msg_cur, msg_end);
1355 if (!msg_cur) {
1356 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001357 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001358 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001359 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1360 goto switchstate;
1361 }
1362
1363 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1364 if (data_ptr)
1365 stktable_data_cast(data_ptr, std_t_ull) = data;
1366 break;
1367 }
1368 case STD_T_FRQP: {
1369 struct freq_ctr_period data;
1370
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01001371 /* First bit is reserved for the freq_ctr_period lock
1372 Note: here we're still protected by the stksess lock
1373 so we don't need to update the update the freq_ctr_period
1374 using its internal lock */
1375
1376 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)) & ~0x1;
Emeric Brun94900952015-06-11 18:25:54 +02001377 if (!msg_cur) {
1378 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001379 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001380 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001381 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1382 goto switchstate;
1383 }
1384 data.curr_ctr = intdecode(&msg_cur, msg_end);
1385 if (!msg_cur) {
1386 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001387 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001388 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001389 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1390 goto switchstate;
1391 }
1392 data.prev_ctr = intdecode(&msg_cur, msg_end);
1393 if (!msg_cur) {
1394 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001395 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001396 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001397 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1398 goto switchstate;
1399 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001400
Emeric Brun94900952015-06-11 18:25:54 +02001401 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1402 if (data_ptr)
1403 stktable_data_cast(data_ptr, std_t_frqp) = data;
1404 break;
1405 }
1406 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001407 }
1408 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001409
Emeric Brun55482912018-01-22 15:10:08 +01001410 /* Force new expiration */
1411 ts->expire = tick_add(now_ms, expire);
1412
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001413 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001414 stktable_touch_remote(st->table, ts, 1);
1415
Emeric Brunb3971ab2015-05-12 18:49:09 +02001416 }
1417 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1418 /* ack message */
1419 uint32_t table_id ;
1420 uint32_t update;
1421 struct shared_table *st;
1422
1423 table_id = intdecode(&msg_cur, msg_end);
1424 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1425 /* malformed message */
1426 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1427 goto switchstate;
1428 }
1429 memcpy(&update, msg_cur, sizeof(update));
1430 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001431
Emeric Brunb3971ab2015-05-12 18:49:09 +02001432 for (st = curpeer->tables; st; st = st->next) {
1433 if (st->local_id == table_id) {
1434 st->update = update;
1435 break;
1436 }
1437 }
1438 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001439 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001440 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1441 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001442 goto switchstate;
1443 }
1444
Emeric Brunb3971ab2015-05-12 18:49:09 +02001445ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001446 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001447 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001448 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001449 goto switchstate;
1450
Emeric Brun2b920a12010-09-23 18:30:22 +02001451incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001452 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001453
Willy Tarreau72d6c162013-04-11 16:14:13 +02001454 if (reql < 0) {
1455 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001456 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001457 goto switchstate;
1458 }
1459
Emeric Brun2b920a12010-09-23 18:30:22 +02001460
Emeric Brun2b920a12010-09-23 18:30:22 +02001461
Emeric Brunb3971ab2015-05-12 18:49:09 +02001462
Emeric Brun2b920a12010-09-23 18:30:22 +02001463 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001464 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1465 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1466 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001467 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001468
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001469 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001470 msg[0] = PEER_MSG_CLASS_CONTROL;
1471 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001472
Emeric Brunb3971ab2015-05-12 18:49:09 +02001473 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001474 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001475 if (repl <= 0) {
1476 /* no more write possible */
1477 if (repl == -1)
1478 goto full;
1479 appctx->st0 = PEER_SESS_ST_END;
1480 goto switchstate;
1481 }
1482 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
1483 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001484
Emeric Brunb3971ab2015-05-12 18:49:09 +02001485 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001486 if (curpeer->tables) {
1487 struct shared_table *st;
1488 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001489
Emeric Brunb3971ab2015-05-12 18:49:09 +02001490 last_local_table = curpeer->last_local_table;
1491 if (!last_local_table)
1492 last_local_table = curpeer->tables;
1493 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001494
Emeric Brunb3971ab2015-05-12 18:49:09 +02001495 while (1) {
1496 if (!st)
1497 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001498
Emeric Brunb3971ab2015-05-12 18:49:09 +02001499 /* It remains some updates to ack */
1500 if (st->last_get != st->last_acked) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001501 repl = peer_send_ackmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001502 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001503 if (repl == -1)
1504 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001506 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001507 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001508 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001509
Emeric Brunb3971ab2015-05-12 18:49:09 +02001510 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001511 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001512 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1513 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1514 struct eb32_node *eb;
1515 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001516
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001518 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001519 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001520 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001521 if (repl == -1)
1522 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001523 goto switchstate;
1524 }
1525 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001526 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001527
1528 /* We force new pushed to 1 to force identifier in update message */
1529 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001530 while (1) {
1531 uint32_t msglen;
1532 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001533 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001534
1535 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001536 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001537 if (!eb) {
1538 eb = eb32_first(&st->table->updates);
1539 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001540 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001541 break;
1542 }
1543 }
1544
1545 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001546 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001547 break;
1548 }
1549
1550 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001551 updateid = ts->upd.key;
1552 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001553 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001554
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 msglen = peer_prepare_updatemsg(ts, st, updateid,
1556 trash.area,
1557 trash.size,
1558 new_pushed,
1559 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001560 if (!msglen) {
1561 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001562 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001563 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001564 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001565 appctx->st0 = PEER_SESS_ST_END;
1566 goto switchstate;
1567 }
1568
1569 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001570 repl = ci_putblk(si_ic(si),
1571 trash.area,
1572 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001573 if (repl <= 0) {
1574 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001575 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001576 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001577 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001578 if (repl == -1) {
1579 goto full;
1580 }
1581 appctx->st0 = PEER_SESS_ST_END;
1582 goto switchstate;
1583 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001584
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001585 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001586 ts->ref_cnt--;
1587 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001588 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1589 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 /* identifier may not needed in next update message */
1591 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001592 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001593 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001594 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001595 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001596 else {
1597 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1598 struct eb32_node *eb;
1599 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001600
Emeric Brunb3971ab2015-05-12 18:49:09 +02001601 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001602 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001603 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001604 if (repl == -1)
1605 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001606 goto switchstate;
1607 }
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001608
Emeric Brunb3971ab2015-05-12 18:49:09 +02001609 curpeer->last_local_table = st;
1610 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001611
Emeric Brunb3971ab2015-05-12 18:49:09 +02001612 /* We force new pushed to 1 to force identifier in update message */
1613 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001614 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001615 while (1) {
1616 uint32_t msglen;
1617 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001618 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001619 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001620
Emeric Brunb3971ab2015-05-12 18:49:09 +02001621 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001622 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001623 if (!eb) {
1624 st->flags |= SHTABLE_F_TEACH_STAGE1;
1625 eb = eb32_first(&st->table->updates);
1626 if (eb)
1627 st->last_pushed = eb->key - 1;
1628 break;
1629 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001630
Emeric Brunb3971ab2015-05-12 18:49:09 +02001631 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001632 updateid = ts->upd.key;
1633 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001634 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001635
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001636 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001637 msglen = peer_prepare_updatemsg(ts, st, updateid,
1638 trash.area,
1639 trash.size,
1640 new_pushed,
1641 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001642 if (!msglen) {
1643 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001644 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001645 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001646 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001647 appctx->st0 = PEER_SESS_ST_END;
1648 goto switchstate;
1649 }
1650
1651 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001652 repl = ci_putblk(si_ic(si),
1653 trash.area,
1654 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001655 if (repl <= 0) {
1656 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001657 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001658 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001659 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001660 if (repl == -1) {
1661 goto full;
1662 }
1663 appctx->st0 = PEER_SESS_ST_END;
1664 goto switchstate;
1665 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001666 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001667 ts->ref_cnt--;
1668 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001669 /* identifier may not needed in next update message */
1670 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001671 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001672 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001673 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001674
Emeric Brunb3971ab2015-05-12 18:49:09 +02001675 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1676 struct eb32_node *eb;
1677 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001678
Emeric Brunb3971ab2015-05-12 18:49:09 +02001679 if (st != curpeer->last_local_table) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001680 repl = peer_send_switchmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001681 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001682 if (repl == -1)
1683 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001684 goto switchstate;
1685 }
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001686
Emeric Brunb3971ab2015-05-12 18:49:09 +02001687 curpeer->last_local_table = st;
1688 }
1689
1690 /* We force new pushed to 1 to force identifier in update message */
1691 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001692 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001693 while (1) {
1694 uint32_t msglen;
1695 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001696 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001697 unsigned updateid;
1698
1699 /* push local updates */
1700 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001701
1702 /* push local updates */
1703 if (!eb || eb->key > st->teaching_origin) {
1704 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001705 break;
1706 }
1707
1708 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001709 updateid = ts->upd.key;
1710 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001711 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001712
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001713 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 msglen = peer_prepare_updatemsg(ts, st, updateid,
1715 trash.area,
1716 trash.size,
1717 new_pushed,
1718 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001719 if (!msglen) {
1720 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001721 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001722 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001723 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001724 appctx->st0 = PEER_SESS_ST_END;
1725 goto switchstate;
1726 }
1727
1728 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001729 repl = ci_putblk(si_ic(si),
1730 trash.area,
1731 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001732 if (repl <= 0) {
1733 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001734 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001735 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001736 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001737 if (repl == -1) {
1738 goto full;
1739 }
1740 appctx->st0 = PEER_SESS_ST_END;
1741 goto switchstate;
1742 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001743
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001744 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001745 ts->ref_cnt--;
1746 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001747 /* identifier may not needed in next update message */
1748 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001749 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001750 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001751 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001752 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001753
1754 if (st == last_local_table)
1755 break;
1756 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001757 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001758 }
1759
1760
1761 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1762 unsigned char msg[2];
1763
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001764 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001765 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001766 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 +02001767 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001768 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001769 if (repl <= 0) {
1770 /* no more write possible */
1771 if (repl == -1)
1772 goto full;
1773 appctx->st0 = PEER_SESS_ST_END;
1774 goto switchstate;
1775 }
1776 /* flag finished message sent */
1777 curpeer->flags |= PEER_F_TEACH_FINISHED;
1778 }
1779
Emeric Brun597b26e2016-08-12 11:23:31 +02001780 /* Confirm finished or partial messages */
1781 while (curpeer->confirm) {
1782 unsigned char msg[2];
1783
1784 /* There is a confirm messages to send */
1785 msg[0] = PEER_MSG_CLASS_CONTROL;
1786 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1787
1788 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001789 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001790 if (repl <= 0) {
1791 /* no more write possible */
1792 if (repl == -1)
1793 goto full;
1794 appctx->st0 = PEER_SESS_ST_END;
1795 goto switchstate;
1796 }
1797 curpeer->confirm--;
1798 }
1799
Emeric Brun2b920a12010-09-23 18:30:22 +02001800 /* noting more to do */
1801 goto out;
1802 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001803 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001804 if (prev_state == PEER_SESS_ST_WAITMSG)
1805 HA_ATOMIC_SUB(&connected_peers, 1);
1806 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001807 repl = snprintf(trash.area, trash.size,
1808 "%d\n", appctx->st1);
1809 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001810 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001811 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001812 goto switchstate;
1813 case PEER_SESS_ST_ERRSIZE: {
1814 unsigned char msg[2];
1815
Willy Tarreau2d372c22018-11-05 17:12:27 +01001816 if (prev_state == PEER_SESS_ST_WAITMSG)
1817 HA_ATOMIC_SUB(&connected_peers, 1);
1818 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001819 msg[0] = PEER_MSG_CLASS_ERROR;
1820 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1821
Willy Tarreau06d80a92017-10-19 14:32:15 +02001822 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001823 goto full;
1824 appctx->st0 = PEER_SESS_ST_END;
1825 goto switchstate;
1826 }
1827 case PEER_SESS_ST_ERRPROTO: {
1828 unsigned char msg[2];
1829
Willy Tarreau2d372c22018-11-05 17:12:27 +01001830 if (prev_state == PEER_SESS_ST_WAITMSG)
1831 HA_ATOMIC_SUB(&connected_peers, 1);
1832 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001833 msg[0] = PEER_MSG_CLASS_ERROR;
1834 msg[1] = PEER_MSG_ERR_PROTOCOL;
1835
Willy Tarreau06d80a92017-10-19 14:32:15 +02001836 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001837 goto full;
1838 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001839 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001840 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001841 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001842 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001843 if (prev_state == PEER_SESS_ST_WAITMSG)
1844 HA_ATOMIC_SUB(&connected_peers, 1);
1845 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001846 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001847 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001848 curpeer = NULL;
1849 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001850 si_shutw(si);
1851 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001852 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001853 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001854 }
1855 }
1856 }
1857out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001858 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001859
1860 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001861 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001862 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001863full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001864 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001865 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001866}
1867
Willy Tarreau30576452015-04-13 13:50:30 +02001868static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001869 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001870 .name = "<PEER>", /* used for logging */
1871 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001872 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001873};
Emeric Brun2b920a12010-09-23 18:30:22 +02001874
1875/*
1876 * Use this function to force a close of a peer session
1877 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001878static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001879{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001880 /* Note that the peer sessions which have just been created
1881 * (->st0 == PEER_SESS_ST_CONNECT) must not
1882 * be shutdown, if not, the TCP session will never be closed
1883 * and stay in CLOSE_WAIT state after having been closed by
1884 * the remote side.
1885 */
1886 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001887 return;
1888
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001889 if (appctx->applet != &peer_applet)
1890 return;
1891
Willy Tarreau2d372c22018-11-05 17:12:27 +01001892 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
1893 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001894 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001895 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001896}
1897
Willy Tarreau91d96282015-03-13 15:47:26 +01001898/* Pre-configures a peers frontend to accept incoming connections */
1899void peers_setup_frontend(struct proxy *fe)
1900{
1901 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001902 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01001903 fe->maxconn = 0;
1904 fe->conn_retries = CONN_RETRIES;
1905 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001906 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001907 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001908 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001909 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001910}
1911
Emeric Brun2b920a12010-09-23 18:30:22 +02001912/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001913 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001914 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001915static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001916{
Willy Tarreau04b92862017-09-15 11:01:04 +02001917 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001918 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001919 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001920 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001921 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001922 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001923
Emeric Brunb3971ab2015-05-12 18:49:09 +02001924 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1925 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001926 s = NULL;
1927
Emeric Brun1138fd02017-06-19 12:38:55 +02001928 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001929 if (!appctx)
1930 goto out_close;
1931
1932 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001933 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001934
Willy Tarreau04b92862017-09-15 11:01:04 +02001935 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001936 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001937 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001938 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001939 }
1940
Willy Tarreau87787ac2017-08-28 16:22:54 +02001941 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001942 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001943 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001944 }
1945
Willy Tarreau342bfb12015-04-05 01:35:34 +02001946 /* The tasks below are normally what is supposed to be done by
1947 * fe->accept().
1948 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001949 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001950
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001951 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001952 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001953 appctx_wakeup(appctx);
1954
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001955 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001956 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001957 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001958
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001959 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001960 * pre-initialized connection in si->conn.
1961 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001962 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001963 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001964
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001965 if (unlikely((cs = cs_new(conn)) == NULL))
1966 goto out_free_conn;
1967
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001968 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02001969 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
1970
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001971 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01001972 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001973 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001974
Emeric Brun2b920a12010-09-23 18:30:22 +02001975 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001976 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001977
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001978 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001979
Emeric Brunb3971ab2015-05-12 18:49:09 +02001980 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001981 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01001982 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001983 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001984
1985 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001986 out_free_conn:
1987 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001988 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001989 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001990 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001991 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001992 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001993 out_free_appctx:
1994 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001995 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001996 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001997}
1998
1999/*
2000 * Task processing function to manage re-connect and peer session
2001 * tasks wakeup on local update.
2002 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002003static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002004{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002005 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002006 struct peer *ps;
2007 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002008
2009 task->expire = TICK_ETERNITY;
2010
Emeric Brunb3971ab2015-05-12 18:49:09 +02002011 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002012 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002013 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002014 task_delete(peers->sync_task);
2015 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002016 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002017 return NULL;
2018 }
2019
Emeric Brun80527f52017-06-19 17:46:37 +02002020 /* Acquire lock for all peers of the section */
2021 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002023
Emeric Brun2b920a12010-09-23 18:30:22 +02002024 if (!stopping) {
2025 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002026
2027 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2028 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2029 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002030 /* Resync from local peer needed
2031 no peer was assigned for the lesson
2032 and no old local peer found
2033 or resync timeout expire */
2034
2035 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002036 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002037
2038 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002039 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002040 }
2041
2042 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002044 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002045 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002046 if (!ps->appctx) {
2047 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002048 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002049 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002050 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002051 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002052 tick_is_expired(ps->reconnect, now_ms))) {
2053 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002054 * or previous peer connection established with success
2055 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002056 * and reconnection timer is expired */
2057
2058 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002059 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002060 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002061 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002062 /* If previous session failed during connection
2063 * but reconnection timer is not expired */
2064
2065 /* reschedule task for reconnect */
2066 task->expire = tick_first(task->expire, ps->reconnect);
2067 }
2068 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002069 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002070 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002071 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002072 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2073 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002074 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2075 /* Resync from a remote is needed
2076 * and no peer was assigned for lesson
2077 * and current peer may be up2date */
2078
2079 /* assign peer for the lesson */
2080 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002081 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002082
Willy Tarreau9df94c22016-10-31 18:42:52 +01002083 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002084 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002085 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002086 else {
2087 /* Awake session if there is data to push */
2088 for (st = ps->tables; st ; st = st->next) {
2089 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002090 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002091 appctx_wakeup(ps->appctx);
2092 break;
2093 }
2094 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002095 }
2096 /* else do nothing */
2097 } /* SUCCESSCODE */
2098 } /* !ps->peer->local */
2099 } /* for */
2100
2101 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002102 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2103 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2104 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002105 /* Resync from remote peer needed
2106 * no peer was assigned for the lesson
2107 * and resync timeout expire */
2108
2109 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002111 }
2112
Emeric Brunb3971ab2015-05-12 18:49:09 +02002113 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002114 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002115 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2116 if (!tick_is_expired(peers->resync_timeout, now_ms))
2117 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002118 }
2119 } /* !stopping */
2120 else {
2121 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002122 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002123 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002124 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002125 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002126 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002127 peers->flags |= PEERS_F_DONOTSTOP;
2128 ps = peers->local;
2129 for (st = ps->tables; st ; st = st->next)
2130 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002131 }
2132
2133 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002134 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002135 /* we're killing a connection, we must apply a random delay before
2136 * retrying otherwise the other end will do the same and we can loop
2137 * for a while.
2138 */
2139 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002140 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002141 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002142 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002143 }
2144 }
2145 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002146
Emeric Brunb3971ab2015-05-12 18:49:09 +02002147 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002148 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002149 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002150 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002151 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002152 peers->flags &= ~PEERS_F_DONOTSTOP;
2153 for (st = ps->tables; st ; st = st->next)
2154 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002155 }
2156 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002157 else if (!ps->appctx) {
2158 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002159 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002160 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2161 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2162 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002163 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002164 * or previous peer connection was successfully established
2165 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002166 * or during previous connect, peer replies a try again statuscode */
2167
2168 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002169 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002170 }
2171 else {
2172 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002173 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002174 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002175 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002176 peers->flags &= ~PEERS_F_DONOTSTOP;
2177 for (st = ps->tables; st ; st = st->next)
2178 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002179 }
2180 }
2181 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002182 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002183 /* current peer connection is active and established
2184 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002185 for (st = ps->tables; st ; st = st->next) {
2186 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002187 appctx_wakeup(ps->appctx);
2188 break;
2189 }
2190 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002191 }
2192 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002193
2194 /* Release lock for all peers of the section */
2195 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002196 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002197
Emeric Brun2b920a12010-09-23 18:30:22 +02002198 /* Wakeup for re-connect */
2199 return task;
2200}
2201
Emeric Brunb3971ab2015-05-12 18:49:09 +02002202
Emeric Brun2b920a12010-09-23 18:30:22 +02002203/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002204 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002205 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002206int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002207{
Emeric Brun2b920a12010-09-23 18:30:22 +02002208 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002209 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002210
Emeric Brun2b920a12010-09-23 18:30:22 +02002211 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002212 peers->peers_fe->maxconn += 3;
2213 }
2214
Willy Tarreau4348fad2012-09-20 16:48:07 +02002215 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2216 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002217 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002218 if (!peers->sync_task)
2219 return 0;
2220
Emeric Brunb3971ab2015-05-12 18:49:09 +02002221 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002222 peers->sync_task->context = (void *)peers;
2223 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2224 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002225 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002226}
2227
2228
2229
2230/*
2231 * Function used to register a table for sync on a group of peers
2232 *
2233 */
2234void peers_register_table(struct peers *peers, struct stktable *table)
2235{
2236 struct shared_table *st;
2237 struct peer * curpeer;
2238 int id = 0;
2239
2240 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002241 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002242 st->table = table;
2243 st->next = curpeer->tables;
2244 if (curpeer->tables)
2245 id = curpeer->tables->local_id;
2246 st->local_id = id + 1;
2247
2248 curpeer->tables = st;
2249 }
2250
2251 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002252}
2253