blob: 3a6394624cdfcca7b1a1aebba5c38e4e0c6fe28a [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 }
Emeric Brun18928af2017-03-29 16:32:53 +0200236 *str = (char *)msg;
237 return i;
238
239 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 }
262
263}
Emeric Brun2b920a12010-09-23 18:30:22 +0200264/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200265 * This prepare the data update message on the stick session <ts>, <st> is the considered
266 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800267 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200268 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
269 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200270 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200271static 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 +0200272{
273 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274 unsigned short datalen;
275 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200276 unsigned int data_type;
277 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 cursor = datamsg = msg + 1 + 5;
280
Emeric Brun2b920a12010-09-23 18:30:22 +0200281 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200282
283 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200284 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200285 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287
288 /* encode update identifier if needed */
289 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200291 memcpy(cursor, &netinteger, sizeof(netinteger));
292 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200293 }
294
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200295 if (use_timed) {
296 netinteger = htonl(tick_remain(now_ms, ts->expire));
297 memcpy(cursor, &netinteger, sizeof(netinteger));
298 cursor += sizeof(netinteger);
299 }
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200302 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200303 int stlen = strlen((char *)ts->key.key);
304
Emeric Brunb3971ab2015-05-12 18:49:09 +0200305 intencode(stlen, &cursor);
306 memcpy(cursor, ts->key.key, stlen);
307 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200308 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200309 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200310 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, &netinteger, sizeof(netinteger));
312 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 memcpy(cursor, ts->key.key, st->table->key_size);
316 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200317 }
318
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200320 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200321 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200322
Emeric Brun94900952015-06-11 18:25:54 +0200323 data_ptr = stktable_data_ptr(st->table, ts, data_type);
324 if (data_ptr) {
325 switch (stktable_data_types[data_type].std_type) {
326 case STD_T_SINT: {
327 int data;
328
329 data = stktable_data_cast(data_ptr, std_t_sint);
330 intencode(data, &cursor);
331 break;
332 }
333 case STD_T_UINT: {
334 unsigned int data;
335
336 data = stktable_data_cast(data_ptr, std_t_uint);
337 intencode(data, &cursor);
338 break;
339 }
340 case STD_T_ULL: {
341 unsigned long long data;
342
343 data = stktable_data_cast(data_ptr, std_t_ull);
344 intencode(data, &cursor);
345 break;
346 }
347 case STD_T_FRQP: {
348 struct freq_ctr_period *frqp;
349
350 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
351 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
352 intencode(frqp->curr_ctr, &cursor);
353 intencode(frqp->prev_ctr, &cursor);
354 break;
355 }
356 }
357 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360
361 /* Compute datalen */
362 datalen = (cursor - datamsg);
363
364 /* prepare message header */
365 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200366 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367 cursor = &msg[2];
368 intencode(datalen, &cursor);
369
370 /* move data after header */
371 memmove(cursor, datamsg, datalen);
372
373 /* return header size + data_len */
374 return (cursor - msg) + datalen;
375}
376
377/*
378 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800379 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200380 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
381 * check size)
382 */
383static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
384{
385 int len;
386 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200387 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200388 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200389 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200390 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200391
392 cursor = datamsg = msg + 2 + 5;
393
394 /* Encode data */
395
396 /* encode local id */
397 intencode(st->local_id, &cursor);
398
399 /* encode table name */
400 len = strlen(st->table->id);
401 intencode(len, &cursor);
402 memcpy(cursor, st->table->id, len);
403 cursor += len;
404
405 /* encode table type */
406
407 intencode(st->table->type, &cursor);
408
409 /* encode table key size */
410 intencode(st->table->key_size, &cursor);
411
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200412 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200414 /* encode available known data types in table */
415 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
416 if (st->table->data_ofs[data_type]) {
417 switch (stktable_data_types[data_type].std_type) {
418 case STD_T_SINT:
419 case STD_T_UINT:
420 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200421 data |= 1 << data_type;
422 break;
Emeric Brun94900952015-06-11 18:25:54 +0200423 case STD_T_FRQP:
424 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200425 intencode(data_type, &chunkq);
426 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200427 break;
428 }
429 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200430 }
431 intencode(data, &cursor);
432
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200433 /* Encode stick-table entries duration. */
434 intencode(st->table->expire, &cursor);
435
436 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200437 chunk->data = chunkq - chunkp;
438 memcpy(cursor, chunk->area, chunk->data);
439 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200440 }
441
Emeric Brunb3971ab2015-05-12 18:49:09 +0200442 /* Compute datalen */
443 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200444
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 /* prepare message header */
446 msg[0] = PEER_MSG_CLASS_STICKTABLE;
447 msg[1] = PEER_MSG_STKT_DEFINE;
448 cursor = &msg[2];
449 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200450
Emeric Brunb3971ab2015-05-12 18:49:09 +0200451 /* move data after header */
452 memmove(cursor, datamsg, datalen);
453
454 /* return header size + data_len */
455 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200456}
457
Emeric Brunb3971ab2015-05-12 18:49:09 +0200458/*
459 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
460 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800461 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
463 * check size)
464 */
465static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
466{
467 unsigned short datalen;
468 char *cursor, *datamsg;
469 uint32_t netinteger;
470
Emeric Brunb058f1c2015-09-22 15:50:18 +0200471 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200472
473 intencode(st->remote_id, &cursor);
474 netinteger = htonl(st->last_get);
475 memcpy(cursor, &netinteger, sizeof(netinteger));
476 cursor += sizeof(netinteger);
477
478 /* Compute datalen */
479 datalen = (cursor - datamsg);
480
481 /* prepare message header */
482 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200483 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200484 cursor = &msg[2];
485 intencode(datalen, &cursor);
486
487 /* move data after header */
488 memmove(cursor, datamsg, datalen);
489
490 /* return header size + data_len */
491 return (cursor - msg) + datalen;
492}
Emeric Brun2b920a12010-09-23 18:30:22 +0200493
494/*
495 * Callback to release a session with a peer
496 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200497static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200498{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200500 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200501 struct peer *peer = appctx->ctx.peers.ptr;
502 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100504 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100505 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200506 return;
507
508 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100510 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
511 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100512 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100513 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100514 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200515 /* Re-init current table pointers to force announcement on re-connect */
516 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517 peer->appctx = NULL;
518 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200519 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200520 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
521 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200522
523 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200524 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 }
526 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 peer->flags &= PEER_TEACH_RESET;
528 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200529 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100530 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200532 }
533}
534
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200535/* Retrieve the major and minor versions of peers protocol
536 * announced by a remote peer. <str> is a null-terminated
537 * string with the following format: "<maj_ver>.<min_ver>".
538 */
539static int peer_get_version(const char *str,
540 unsigned int *maj_ver, unsigned int *min_ver)
541{
542 unsigned int majv, minv;
543 const char *pos, *saved;
544 const char *end;
545
546 saved = pos = str;
547 end = str + strlen(str);
548
549 majv = read_uint(&pos, end);
550 if (saved == pos || *pos++ != '.')
551 return -1;
552
553 saved = pos;
554 minv = read_uint(&pos, end);
555 if (saved == pos || pos != end)
556 return -1;
557
558 *maj_ver = majv;
559 *min_ver = minv;
560
561 return 0;
562}
Emeric Brun2b920a12010-09-23 18:30:22 +0200563
564/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100565 * Parse a line terminated by an optional '\r' character, followed by a mandatory
566 * '\n' character.
567 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
568 * a line could not be read because the communication channel is closed.
569 */
570static inline int peer_getline(struct appctx *appctx)
571{
572 int n;
573 struct stream_interface *si = appctx->owner;
574
575 n = co_getline(si_oc(si), trash.area, trash.size);
576 if (!n)
577 return 0;
578
579 if (n < 0 || trash.area[n - 1] != '\n') {
580 appctx->st0 = PEER_SESS_ST_END;
581 return -1;
582 }
583
584 if (n > 1 && (trash.area[n - 2] == '\r'))
585 trash.area[n - 2] = 0;
586 else
587 trash.area[n - 1] = 0;
588
589 co_skip(si_oc(si), n);
590
591 return n;
592}
593
594/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200595 * IO Handler to handle message exchance with a peer
596 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200597static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200598{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200599 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200600 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200601 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200602 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200603 int reql = 0;
604 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200605 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
606 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100607 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +0200608
Joseph Herlant82b2f542018-11-15 12:19:14 -0800609 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200610 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100611 goto full;
612
Emeric Brun2b920a12010-09-23 18:30:22 +0200613 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100614 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200615switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200616 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100617 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100618 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100619 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100620 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100621 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100623 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100624 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200625
Frédéric Lécaillece025572019-01-21 13:38:06 +0100626 reql = peer_getline(appctx);
627 if (!reql)
628 goto out;
629
630 if (reql < 0)
631 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200632
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200633 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200635 appctx->st0 = PEER_SESS_ST_EXIT;
636 appctx->st1 = PEER_SESS_SC_ERRPROTO;
637 goto switchstate;
638 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200640 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100641 appctx->st0 = PEER_SESS_ST_EXIT;
642 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200643 goto switchstate;
644 }
645
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100646 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100648 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100649 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200650
Frédéric Lécaillece025572019-01-21 13:38:06 +0100651 reql = peer_getline(appctx);
652 if (!reql)
653 goto out;
654
655 if (reql < 0)
656 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200657
658 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100660 appctx->st0 = PEER_SESS_ST_EXIT;
661 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 goto switchstate;
663 }
664
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100665 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200666 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100667 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200668 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100669
670 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200671
Frédéric Lécaillece025572019-01-21 13:38:06 +0100672 reql = peer_getline(appctx);
673 if (!reql)
674 goto out;
675
676 if (reql < 0)
677 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200678
Emeric Brunb3971ab2015-05-12 18:49:09 +0200679 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200680 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200681 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100682 appctx->st0 = PEER_SESS_ST_EXIT;
683 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200684 goto switchstate;
685 }
686 *p = 0;
687
688 /* lookup known peer */
689 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200691 break;
692 }
693
694 /* if unknown peer */
695 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100696 appctx->st0 = PEER_SESS_ST_EXIT;
697 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 goto switchstate;
699 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200700
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100701 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100702 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200703 if (curpeer->local) {
704 /* Local connection, reply a retry */
705 appctx->st0 = PEER_SESS_ST_EXIT;
706 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
707 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200708 }
Emeric Brun80527f52017-06-19 17:46:37 +0200709
710 /* we're killing a connection, we must apply a random delay before
711 * retrying otherwise the other end will do the same and we can loop
712 * for a while.
713 */
714 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100715 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200716 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200717 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
718 if (min_ver == PEER_DWNGRD_MINOR_VER) {
719 curpeer->flags |= PEER_F_DWNGRD;
720 }
721 else {
722 curpeer->flags &= ~PEER_F_DWNGRD;
723 }
724 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200725 curpeer->appctx = appctx;
726 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100727 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +0100728 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +0200729 /* fall through */
730 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100731 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200733
Willy Tarreau2d372c22018-11-05 17:12:27 +0100734 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200735 if (!curpeer) {
736 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100737 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200738 if (curpeer->appctx != appctx) {
739 appctx->st0 = PEER_SESS_ST_END;
740 goto switchstate;
741 }
742 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 repl = snprintf(trash.area, trash.size,
744 "%d\n",
745 PEER_SESS_SC_SUCCESSCODE);
746 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200747 if (repl <= 0) {
748 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100749 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100750 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200751 goto switchstate;
752 }
753
754 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200755 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200756
757 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200758 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200759
760 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200761 curpeer->confirm = 0;
762
763 /* Init cursors */
764 for (st = curpeer->tables; st ; st = st->next) {
765 st->last_get = st->last_acked = 0;
766 st->teaching_origin = st->last_pushed = st->update;
767 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200768
769 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200770 curpeer->flags &= PEER_TEACH_RESET;
771 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200772
773 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200774 if (curpeer->local) {
775 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200776 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
777 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200778 /* assign local peer for a lesson, consider lesson already requested */
779 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200780 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200781 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200782
Emeric Brunb3971ab2015-05-12 18:49:09 +0200783 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200784 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
785 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200786 /* assign peer for a lesson */
787 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200788 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200789 }
790
791
Emeric Brun2b920a12010-09-23 18:30:22 +0200792 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100793 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100794 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200795 goto switchstate;
796 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100797 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100798 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200799 if (!curpeer) {
800 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100801 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200802 if (curpeer->appctx != appctx) {
803 appctx->st0 = PEER_SESS_ST_END;
804 goto switchstate;
805 }
806 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200807
808 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200809 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200810 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
811 PEER_MAJOR_VER,
812 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200813 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200814 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100815 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200816 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200817
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100819 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200820 goto switchstate;
821 }
822
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200823 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200824 if (repl <= 0) {
825 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100826 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100827 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200828 goto switchstate;
829 }
830
831 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100832 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200833 /* fall through */
834 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100835 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200836 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200837
Willy Tarreau2d372c22018-11-05 17:12:27 +0100838 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200839 if (!curpeer) {
840 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100841 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200842 if (curpeer->appctx != appctx) {
843 appctx->st0 = PEER_SESS_ST_END;
844 goto switchstate;
845 }
846 }
847
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100848 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200849 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200850
Frédéric Lécaillece025572019-01-21 13:38:06 +0100851 reql = peer_getline(appctx);
852 if (!reql)
853 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +0200854
Frédéric Lécaillece025572019-01-21 13:38:06 +0100855 if (reql < 0)
856 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200857
858 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200859 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +0200860
861 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200862 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200863
864 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200865 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200866 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200867 for (st = curpeer->tables; st ; st = st->next) {
868 st->last_get = st->last_acked = 0;
869 st->teaching_origin = st->last_pushed = st->update;
870 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200871
872 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200873 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200874
Emeric Brunb3971ab2015-05-12 18:49:09 +0200875 /* reset teaching and learning flags to 0 */
876 curpeer->flags &= PEER_TEACH_RESET;
877 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200878
Emeric Brunb3971ab2015-05-12 18:49:09 +0200879 /* If current peer is local */
880 if (curpeer->local) {
881 /* flag to start to teach lesson */
882 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200883
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200885 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
886 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887 /* If peer is remote and resync from remote is needed,
888 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200889
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890 /* assign peer for a lesson */
891 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200892 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200893 }
894
895 }
896 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200897 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
898 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200899 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100900 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200901 goto switchstate;
902 }
Willy Tarreau2d372c22018-11-05 17:12:27 +0100903 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100904 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200905 /* fall through */
906 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100907 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200908 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200909 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200910 char *msg_cur = trash.area;
911 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200912 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200913 int totl = 0;
914
Willy Tarreau2d372c22018-11-05 17:12:27 +0100915 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200916 if (!curpeer) {
917 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100918 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200919 if (curpeer->appctx != appctx) {
920 appctx->st0 = PEER_SESS_ST_END;
921 goto switchstate;
922 }
923 }
924
Willy Tarreau06d80a92017-10-19 14:32:15 +0200925 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200926 if (reql <= 0) /* closed or EOL not found */
927 goto incomplete;
928
Emeric Brun2b920a12010-09-23 18:30:22 +0200929 totl += reql;
930
Emeric Brunb3971ab2015-05-12 18:49:09 +0200931 if (msg_head[1] >= 128) {
932 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200933 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200934 if (reql <= 0) /* closed */
935 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200936
Emeric Brunb3971ab2015-05-12 18:49:09 +0200937 totl += reql;
938
939 if (msg_head[2] < 240) {
940 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200941 }
942 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200943 int i;
944 char *cur;
945 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200946
Emeric Brunb3971ab2015-05-12 18:49:09 +0200947 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200948 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200949 if (reql <= 0) /* closed */
950 goto incomplete;
951
952 totl += reql;
953
954 if (!(msg_head[i] & 0x80))
955 break;
956 }
957
958 if (i == sizeof(msg_head)) {
959 /* malformed message */
960 appctx->st0 = PEER_SESS_ST_ERRPROTO;
961 goto switchstate;
962
963 }
964 end = (char *)msg_head + sizeof(msg_head);
965 cur = (char *)&msg_head[2];
966 msg_len = intdecode(&cur, end);
967 if (!cur) {
968 /* malformed message */
969 appctx->st0 = PEER_SESS_ST_ERRPROTO;
970 goto switchstate;
971 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200972 }
973
Willy Tarreau86a446e2013-11-25 23:02:37 +0100974
Emeric Brunb3971ab2015-05-12 18:49:09 +0200975 /* Read message content */
976 if (msg_len) {
977 if (msg_len > trash.size) {
978 /* Status code is not success, abort */
979 appctx->st0 = PEER_SESS_ST_ERRSIZE;
980 goto switchstate;
981 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200982
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200983 reql = co_getblk(si_oc(si),
984 trash.area,
985 msg_len,
986 totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 if (reql <= 0) /* closed */
988 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200989 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100990
Emeric Brunb3971ab2015-05-12 18:49:09 +0200991 msg_end += msg_len;
992 }
993 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100994
Emeric Brunb3971ab2015-05-12 18:49:09 +0200995 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
996 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
997 struct shared_table *st;
998 /* Reset message: remote need resync */
999
1000 /* prepare tables fot a global push */
1001 for (st = curpeer->tables; st; st = st->next) {
1002 st->teaching_origin = st->last_pushed = st->table->update;
1003 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001004 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001005
Emeric Brunb3971ab2015-05-12 18:49:09 +02001006 /* reset teaching flags to 0 */
1007 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001008
Emeric Brunb3971ab2015-05-12 18:49:09 +02001009 /* flag to start to teach lesson */
1010 curpeer->flags |= PEER_F_TEACH_PROCESS;
1011
1012
1013 }
1014 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1015
1016 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1017 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001018 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1019 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001020 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001021 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001022 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001023 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1024
1025 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1026 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001027 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001028
1029 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001030 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1031 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001032 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001033 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001034 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001035 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001036 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001037
1038 /* If stopping state */
1039 if (stopping) {
1040 /* Close session, push resync no more needed */
1041 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1042 appctx->st0 = PEER_SESS_ST_END;
1043 goto switchstate;
1044 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001045 for (st = curpeer->tables; st; st = st->next) {
1046 st->update = st->last_pushed = st->teaching_origin;
1047 st->flags = 0;
1048 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001049
1050 /* reset teaching flags to 0 */
1051 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001052 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001053 }
1054 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1055 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1056 int table_id_len;
1057 struct shared_table *st;
1058 int table_type;
1059 int table_keylen;
1060 int table_id;
1061 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001062
Emeric Brunb3971ab2015-05-12 18:49:09 +02001063 table_id = intdecode(&msg_cur, msg_end);
1064 if (!msg_cur) {
1065 /* malformed message */
1066 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1067 goto switchstate;
1068 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001069
Emeric Brunb3971ab2015-05-12 18:49:09 +02001070 table_id_len = intdecode(&msg_cur, msg_end);
1071 if (!msg_cur) {
1072 /* malformed message */
1073 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1074 goto switchstate;
1075 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001076
Emeric Brunb3971ab2015-05-12 18:49:09 +02001077 curpeer->remote_table = NULL;
1078 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1079 /* malformed message */
1080 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1081 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001082 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001083
Emeric Brunb3971ab2015-05-12 18:49:09 +02001084 for (st = curpeer->tables; st; st = st->next) {
1085 /* Reset IDs */
1086 if (st->remote_id == table_id)
1087 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001088
Emeric Brunb3971ab2015-05-12 18:49:09 +02001089 if (!curpeer->remote_table
1090 && (table_id_len == strlen(st->table->id))
1091 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1092 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001093 }
1094 }
1095
Emeric Brunb3971ab2015-05-12 18:49:09 +02001096 if (!curpeer->remote_table) {
1097 goto ignore_msg;
1098 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001099
Emeric Brunb3971ab2015-05-12 18:49:09 +02001100 msg_cur += table_id_len;
1101 if (msg_cur >= msg_end) {
1102 /* malformed message */
1103 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1104 goto switchstate;
1105 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001106
Emeric Brunb3971ab2015-05-12 18:49:09 +02001107 table_type = intdecode(&msg_cur, msg_end);
1108 if (!msg_cur) {
1109 /* malformed message */
1110 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1111 goto switchstate;
1112 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001113
Emeric Brunb3971ab2015-05-12 18:49:09 +02001114 table_keylen = intdecode(&msg_cur, msg_end);
1115 if (!msg_cur) {
1116 /* malformed message */
1117 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1118 goto switchstate;
1119 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001120
Emeric Brunb3971ab2015-05-12 18:49:09 +02001121 table_data = intdecode(&msg_cur, msg_end);
1122 if (!msg_cur) {
1123 /* malformed message */
1124 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1125 goto switchstate;
1126 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001127
Emeric Brunb3971ab2015-05-12 18:49:09 +02001128 if (curpeer->remote_table->table->type != table_type
1129 || curpeer->remote_table->table->key_size != table_keylen) {
1130 curpeer->remote_table = NULL;
1131 goto ignore_msg;
1132 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001133
Emeric Brunb3971ab2015-05-12 18:49:09 +02001134 curpeer->remote_table->remote_data = table_data;
1135 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001136 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001137 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1138 struct shared_table *st;
1139 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001140
Emeric Brunb3971ab2015-05-12 18:49:09 +02001141 table_id = intdecode(&msg_cur, msg_end);
1142 if (!msg_cur) {
1143 /* malformed message */
1144 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1145 goto switchstate;
1146 }
1147 curpeer->remote_table = NULL;
1148 for (st = curpeer->tables; st; st = st->next) {
1149 if (st->remote_id == table_id) {
1150 curpeer->remote_table = st;
1151 break;
1152 }
1153 }
1154
Emeric Brun2b920a12010-09-23 18:30:22 +02001155 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001156 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001157 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1158 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1159 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001160 struct shared_table *st = curpeer->remote_table;
1161 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001162 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001163 unsigned int data_type;
1164 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001165
Emeric Brunb3971ab2015-05-12 18:49:09 +02001166 /* Here we have data message */
1167 if (!st)
1168 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001169
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001170 expire = MS_TO_TICKS(st->table->expire);
1171
1172 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1173 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001174 if (msg_len < sizeof(update)) {
1175 /* malformed message */
1176 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1177 goto switchstate;
1178 }
1179 memcpy(&update, msg_cur, sizeof(update));
1180 msg_cur += sizeof(update);
1181 st->last_get = htonl(update);
1182 }
1183 else {
1184 st->last_get++;
1185 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001186
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001187 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1188 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1189 size_t expire_sz = sizeof expire;
1190
1191 if (msg_cur + expire_sz > msg_end) {
1192 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1193 goto switchstate;
1194 }
1195 memcpy(&expire, msg_cur, expire_sz);
1196 msg_cur += expire_sz;
1197 expire = ntohl(expire);
1198 }
1199
Emeric Brunb3971ab2015-05-12 18:49:09 +02001200 newts = stksess_new(st->table, NULL);
1201 if (!newts)
1202 goto ignore_msg;
Emeric Brun55482912018-01-22 15:10:08 +01001203
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001204 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001205 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001206
Emeric Brunb3971ab2015-05-12 18:49:09 +02001207 to_read = intdecode(&msg_cur, msg_end);
1208 if (!msg_cur) {
1209 /* malformed message */
1210 stksess_free(st->table, newts);
1211 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1212 goto switchstate;
1213 }
1214 to_store = MIN(to_read, st->table->key_size - 1);
1215 if (msg_cur + to_store > msg_end) {
1216 /* malformed message */
1217 stksess_free(st->table, newts);
1218 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1219 goto switchstate;
1220 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001221
Emeric Brunb3971ab2015-05-12 18:49:09 +02001222 memcpy(newts->key.key, msg_cur, to_store);
1223 newts->key.key[to_store] = 0;
1224 msg_cur += to_read;
1225 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001226 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001227 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001228
Emeric Brunb3971ab2015-05-12 18:49:09 +02001229 if (msg_cur + sizeof(netinteger) > msg_end) {
1230 /* malformed message */
1231 stksess_free(st->table, newts);
1232 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1233 goto switchstate;
1234 }
1235 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1236 netinteger = ntohl(netinteger);
1237 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1238 msg_cur += sizeof(netinteger);
1239 }
1240 else {
1241 if (msg_cur + st->table->key_size > msg_end) {
1242 /* malformed message */
1243 stksess_free(st->table, newts);
1244 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1245 goto switchstate;
1246 }
1247 memcpy(newts->key.key, msg_cur, st->table->key_size);
1248 msg_cur += st->table->key_size;
1249 }
1250
1251 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001252 ts = stktable_set_entry(st->table, newts);
1253 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001254 stksess_free(st->table, newts);
1255 newts = NULL;
1256 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001257
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001258 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001259
Emeric Brun94900952015-06-11 18:25:54 +02001260 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001261
Emeric Brun94900952015-06-11 18:25:54 +02001262 if ((1 << data_type) & st->remote_data) {
1263 switch (stktable_data_types[data_type].std_type) {
1264 case STD_T_SINT: {
1265 int data;
1266
1267 data = intdecode(&msg_cur, msg_end);
1268 if (!msg_cur) {
1269 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001270 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001271 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001272 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1273 goto switchstate;
1274 }
1275
1276 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1277 if (data_ptr)
1278 stktable_data_cast(data_ptr, std_t_sint) = data;
1279 break;
1280 }
1281 case STD_T_UINT: {
1282 unsigned int data;
1283
1284 data = intdecode(&msg_cur, msg_end);
1285 if (!msg_cur) {
1286 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001287 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001288 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001289 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1290 goto switchstate;
1291 }
1292
1293 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1294 if (data_ptr)
1295 stktable_data_cast(data_ptr, std_t_uint) = data;
1296 break;
1297 }
1298 case STD_T_ULL: {
1299 unsigned long long data;
1300
1301 data = intdecode(&msg_cur, msg_end);
1302 if (!msg_cur) {
1303 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001304 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001305 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001306 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1307 goto switchstate;
1308 }
1309
1310 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1311 if (data_ptr)
1312 stktable_data_cast(data_ptr, std_t_ull) = data;
1313 break;
1314 }
1315 case STD_T_FRQP: {
1316 struct freq_ctr_period data;
1317
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01001318 /* First bit is reserved for the freq_ctr_period lock
1319 Note: here we're still protected by the stksess lock
1320 so we don't need to update the update the freq_ctr_period
1321 using its internal lock */
1322
1323 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)) & ~0x1;
Emeric Brun94900952015-06-11 18:25:54 +02001324 if (!msg_cur) {
1325 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001326 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001327 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001328 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1329 goto switchstate;
1330 }
1331 data.curr_ctr = intdecode(&msg_cur, msg_end);
1332 if (!msg_cur) {
1333 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001334 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001335 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001336 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1337 goto switchstate;
1338 }
1339 data.prev_ctr = intdecode(&msg_cur, msg_end);
1340 if (!msg_cur) {
1341 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001342 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001343 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001344 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1345 goto switchstate;
1346 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001347
Emeric Brun94900952015-06-11 18:25:54 +02001348 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1349 if (data_ptr)
1350 stktable_data_cast(data_ptr, std_t_frqp) = data;
1351 break;
1352 }
1353 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001354 }
1355 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001356
Emeric Brun55482912018-01-22 15:10:08 +01001357 /* Force new expiration */
1358 ts->expire = tick_add(now_ms, expire);
1359
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001360 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001361 stktable_touch_remote(st->table, ts, 1);
1362
Emeric Brunb3971ab2015-05-12 18:49:09 +02001363 }
1364 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1365 /* ack message */
1366 uint32_t table_id ;
1367 uint32_t update;
1368 struct shared_table *st;
1369
1370 table_id = intdecode(&msg_cur, msg_end);
1371 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1372 /* malformed message */
1373 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1374 goto switchstate;
1375 }
1376 memcpy(&update, msg_cur, sizeof(update));
1377 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001378
Emeric Brunb3971ab2015-05-12 18:49:09 +02001379 for (st = curpeer->tables; st; st = st->next) {
1380 if (st->local_id == table_id) {
1381 st->update = update;
1382 break;
1383 }
1384 }
1385 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001386 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001387 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1388 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001389 goto switchstate;
1390 }
1391
Emeric Brunb3971ab2015-05-12 18:49:09 +02001392ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001393 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001394 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001395 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001396 goto switchstate;
1397
Emeric Brun2b920a12010-09-23 18:30:22 +02001398incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001399 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001400
Willy Tarreau72d6c162013-04-11 16:14:13 +02001401 if (reql < 0) {
1402 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001403 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001404 goto switchstate;
1405 }
1406
Emeric Brun2b920a12010-09-23 18:30:22 +02001407
Emeric Brun2b920a12010-09-23 18:30:22 +02001408
Emeric Brunb3971ab2015-05-12 18:49:09 +02001409
Emeric Brun2b920a12010-09-23 18:30:22 +02001410 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001411 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001412 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1413 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001414 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001415
Emeric Brunb3971ab2015-05-12 18:49:09 +02001416 /* Current peer was elected to request a resync */
1417 msg[0] = PEER_MSG_CLASS_CONTROL;
1418 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001419
Emeric Brunb3971ab2015-05-12 18:49:09 +02001420 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001421 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001422 if (repl <= 0) {
1423 /* no more write possible */
1424 if (repl == -1)
1425 goto full;
1426 appctx->st0 = PEER_SESS_ST_END;
1427 goto switchstate;
1428 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001429 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001430 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001431
Emeric Brunb3971ab2015-05-12 18:49:09 +02001432 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001433
Emeric Brunb3971ab2015-05-12 18:49:09 +02001434 if (curpeer->tables) {
1435 struct shared_table *st;
1436 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001437
Emeric Brunb3971ab2015-05-12 18:49:09 +02001438 last_local_table = curpeer->last_local_table;
1439 if (!last_local_table)
1440 last_local_table = curpeer->tables;
1441 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001442
Emeric Brunb3971ab2015-05-12 18:49:09 +02001443 while (1) {
1444 if (!st)
1445 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001446
Emeric Brunb3971ab2015-05-12 18:49:09 +02001447 /* It remains some updates to ack */
1448 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001449 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001450
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001451 msglen = peer_prepare_ackmsg(st,
1452 trash.area,
1453 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001454 if (!msglen) {
1455 /* internal error: message does not fit in trash */
1456 appctx->st0 = PEER_SESS_ST_END;
1457 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001458 }
1459
Emeric Brunb3971ab2015-05-12 18:49:09 +02001460 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001461 repl = ci_putblk(si_ic(si),
1462 trash.area,
1463 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001464 if (repl <= 0) {
1465 /* no more write possible */
1466 if (repl == -1) {
1467 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001468 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001469 appctx->st0 = PEER_SESS_ST_END;
1470 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001471 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001472 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001473 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001474
Emeric Brunb3971ab2015-05-12 18:49:09 +02001475 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001476 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001477 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1478 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1479 struct eb32_node *eb;
1480 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001481
Emeric Brunb3971ab2015-05-12 18:49:09 +02001482 if (st != curpeer->last_local_table) {
1483 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001484
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001485 msglen = peer_prepare_switchmsg(st,
1486 trash.area,
1487 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001488 if (!msglen) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001489 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001490 /* internal error: message does not fit in trash */
1491 appctx->st0 = PEER_SESS_ST_END;
1492 goto switchstate;
1493 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001494
Emeric Brunb3971ab2015-05-12 18:49:09 +02001495 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001496 repl = ci_putblk(si_ic(si),
1497 trash.area,
1498 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001499 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001500 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001501 /* no more write possible */
1502 if (repl == -1) {
1503 goto full;
1504 }
1505 appctx->st0 = PEER_SESS_ST_END;
1506 goto switchstate;
1507 }
1508 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001509 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001510
1511 /* We force new pushed to 1 to force identifier in update message */
1512 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001513 while (1) {
1514 uint32_t msglen;
1515 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001516 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517
1518 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001519 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001520 if (!eb) {
1521 eb = eb32_first(&st->table->updates);
1522 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001523 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001524 break;
1525 }
1526 }
1527
1528 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001529 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001530 break;
1531 }
1532
1533 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001534 updateid = ts->upd.key;
1535 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001536 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001537
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001538 msglen = peer_prepare_updatemsg(ts, st, updateid,
1539 trash.area,
1540 trash.size,
1541 new_pushed,
1542 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001543 if (!msglen) {
1544 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001545 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001546 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001547 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001548 appctx->st0 = PEER_SESS_ST_END;
1549 goto switchstate;
1550 }
1551
1552 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001553 repl = ci_putblk(si_ic(si),
1554 trash.area,
1555 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001556 if (repl <= 0) {
1557 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001558 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001559 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001560 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001561 if (repl == -1) {
1562 goto full;
1563 }
1564 appctx->st0 = PEER_SESS_ST_END;
1565 goto switchstate;
1566 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001567
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001568 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001569 ts->ref_cnt--;
1570 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001571 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1572 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001573 /* identifier may not needed in next update message */
1574 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001575 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001576 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001577 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001578 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001579 else {
1580 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1581 struct eb32_node *eb;
1582 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001583
Emeric Brunb3971ab2015-05-12 18:49:09 +02001584 if (st != curpeer->last_local_table) {
1585 int msglen;
1586
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001587 msglen = peer_prepare_switchmsg(st,
1588 trash.area,
1589 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 if (!msglen) {
1591 /* internal error: message does not fit in trash */
1592 appctx->st0 = PEER_SESS_ST_END;
1593 goto switchstate;
1594 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001595
Emeric Brunb3971ab2015-05-12 18:49:09 +02001596 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001597 repl = ci_putblk(si_ic(si),
1598 trash.area,
1599 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001600 if (repl <= 0) {
1601 /* no more write possible */
1602 if (repl == -1) {
1603 goto full;
1604 }
1605 appctx->st0 = PEER_SESS_ST_END;
1606 goto switchstate;
1607 }
1608 curpeer->last_local_table = st;
1609 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001610
Emeric Brunb3971ab2015-05-12 18:49:09 +02001611 /* We force new pushed to 1 to force identifier in update message */
1612 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001613 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001614 while (1) {
1615 uint32_t msglen;
1616 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001617 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001618 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001619
Emeric Brunb3971ab2015-05-12 18:49:09 +02001620 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001621 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001622 if (!eb) {
1623 st->flags |= SHTABLE_F_TEACH_STAGE1;
1624 eb = eb32_first(&st->table->updates);
1625 if (eb)
1626 st->last_pushed = eb->key - 1;
1627 break;
1628 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001629
Emeric Brunb3971ab2015-05-12 18:49:09 +02001630 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001631 updateid = ts->upd.key;
1632 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001633 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001634
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001635 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001636 msglen = peer_prepare_updatemsg(ts, st, updateid,
1637 trash.area,
1638 trash.size,
1639 new_pushed,
1640 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001641 if (!msglen) {
1642 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001643 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001644 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001645 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001646 appctx->st0 = PEER_SESS_ST_END;
1647 goto switchstate;
1648 }
1649
1650 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001651 repl = ci_putblk(si_ic(si),
1652 trash.area,
1653 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001654 if (repl <= 0) {
1655 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001656 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001657 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001658 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001659 if (repl == -1) {
1660 goto full;
1661 }
1662 appctx->st0 = PEER_SESS_ST_END;
1663 goto switchstate;
1664 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001665 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001666 ts->ref_cnt--;
1667 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001668 /* identifier may not needed in next update message */
1669 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001670 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001671 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001672 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001673
Emeric Brunb3971ab2015-05-12 18:49:09 +02001674 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1675 struct eb32_node *eb;
1676 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001677
Emeric Brunb3971ab2015-05-12 18:49:09 +02001678 if (st != curpeer->last_local_table) {
1679 int msglen;
1680
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001681 msglen = peer_prepare_switchmsg(st,
1682 trash.area,
1683 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001684 if (!msglen) {
1685 /* internal error: message does not fit in trash */
1686 appctx->st0 = PEER_SESS_ST_END;
1687 goto switchstate;
1688 }
1689
1690 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001691 repl = ci_putblk(si_ic(si),
1692 trash.area,
1693 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001694 if (repl <= 0) {
1695 /* no more write possible */
1696 if (repl == -1) {
1697 goto full;
1698 }
1699 appctx->st0 = PEER_SESS_ST_END;
1700 goto switchstate;
1701 }
1702 curpeer->last_local_table = st;
1703 }
1704
1705 /* We force new pushed to 1 to force identifier in update message */
1706 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001707 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001708 while (1) {
1709 uint32_t msglen;
1710 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001711 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001712 unsigned updateid;
1713
1714 /* push local updates */
1715 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001716
1717 /* push local updates */
1718 if (!eb || eb->key > st->teaching_origin) {
1719 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001720 break;
1721 }
1722
1723 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001724 updateid = ts->upd.key;
1725 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001726 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001727
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001728 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001729 msglen = peer_prepare_updatemsg(ts, st, updateid,
1730 trash.area,
1731 trash.size,
1732 new_pushed,
1733 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001734 if (!msglen) {
1735 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001736 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001737 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001738 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001739 appctx->st0 = PEER_SESS_ST_END;
1740 goto switchstate;
1741 }
1742
1743 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001744 repl = ci_putblk(si_ic(si),
1745 trash.area,
1746 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001747 if (repl <= 0) {
1748 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001749 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001750 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001751 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001752 if (repl == -1) {
1753 goto full;
1754 }
1755 appctx->st0 = PEER_SESS_ST_END;
1756 goto switchstate;
1757 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001758
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001759 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001760 ts->ref_cnt--;
1761 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001762 /* identifier may not needed in next update message */
1763 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001764 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001765 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001766 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001767 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001768
1769 if (st == last_local_table)
1770 break;
1771 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001772 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001773 }
1774
1775
1776 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1777 unsigned char msg[2];
1778
1779 /* Current peer was elected to request a resync */
1780 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001781 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 +02001782 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001783 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001784 if (repl <= 0) {
1785 /* no more write possible */
1786 if (repl == -1)
1787 goto full;
1788 appctx->st0 = PEER_SESS_ST_END;
1789 goto switchstate;
1790 }
1791 /* flag finished message sent */
1792 curpeer->flags |= PEER_F_TEACH_FINISHED;
1793 }
1794
Emeric Brun597b26e2016-08-12 11:23:31 +02001795 /* Confirm finished or partial messages */
1796 while (curpeer->confirm) {
1797 unsigned char msg[2];
1798
1799 /* There is a confirm messages to send */
1800 msg[0] = PEER_MSG_CLASS_CONTROL;
1801 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1802
1803 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001804 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001805 if (repl <= 0) {
1806 /* no more write possible */
1807 if (repl == -1)
1808 goto full;
1809 appctx->st0 = PEER_SESS_ST_END;
1810 goto switchstate;
1811 }
1812 curpeer->confirm--;
1813 }
1814
Emeric Brun2b920a12010-09-23 18:30:22 +02001815 /* noting more to do */
1816 goto out;
1817 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001818 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001819 if (prev_state == PEER_SESS_ST_WAITMSG)
1820 HA_ATOMIC_SUB(&connected_peers, 1);
1821 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 repl = snprintf(trash.area, trash.size,
1823 "%d\n", appctx->st1);
1824 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001825 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001826 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001827 goto switchstate;
1828 case PEER_SESS_ST_ERRSIZE: {
1829 unsigned char msg[2];
1830
Willy Tarreau2d372c22018-11-05 17:12:27 +01001831 if (prev_state == PEER_SESS_ST_WAITMSG)
1832 HA_ATOMIC_SUB(&connected_peers, 1);
1833 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001834 msg[0] = PEER_MSG_CLASS_ERROR;
1835 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1836
Willy Tarreau06d80a92017-10-19 14:32:15 +02001837 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001838 goto full;
1839 appctx->st0 = PEER_SESS_ST_END;
1840 goto switchstate;
1841 }
1842 case PEER_SESS_ST_ERRPROTO: {
1843 unsigned char msg[2];
1844
Willy Tarreau2d372c22018-11-05 17:12:27 +01001845 if (prev_state == PEER_SESS_ST_WAITMSG)
1846 HA_ATOMIC_SUB(&connected_peers, 1);
1847 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001848 msg[0] = PEER_MSG_CLASS_ERROR;
1849 msg[1] = PEER_MSG_ERR_PROTOCOL;
1850
Willy Tarreau06d80a92017-10-19 14:32:15 +02001851 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001852 goto full;
1853 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001854 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001855 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001856 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001857 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001858 if (prev_state == PEER_SESS_ST_WAITMSG)
1859 HA_ATOMIC_SUB(&connected_peers, 1);
1860 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001861 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001862 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001863 curpeer = NULL;
1864 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001865 si_shutw(si);
1866 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001867 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001868 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001869 }
1870 }
1871 }
1872out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001873 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001874
1875 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001876 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001877 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001878full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001879 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001880 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001881}
1882
Willy Tarreau30576452015-04-13 13:50:30 +02001883static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001884 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001885 .name = "<PEER>", /* used for logging */
1886 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001887 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001888};
Emeric Brun2b920a12010-09-23 18:30:22 +02001889
1890/*
1891 * Use this function to force a close of a peer session
1892 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001893static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001894{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001895 /* Note that the peer sessions which have just been created
1896 * (->st0 == PEER_SESS_ST_CONNECT) must not
1897 * be shutdown, if not, the TCP session will never be closed
1898 * and stay in CLOSE_WAIT state after having been closed by
1899 * the remote side.
1900 */
1901 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001902 return;
1903
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001904 if (appctx->applet != &peer_applet)
1905 return;
1906
Willy Tarreau2d372c22018-11-05 17:12:27 +01001907 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
1908 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001909 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001910 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001911}
1912
Willy Tarreau91d96282015-03-13 15:47:26 +01001913/* Pre-configures a peers frontend to accept incoming connections */
1914void peers_setup_frontend(struct proxy *fe)
1915{
1916 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001917 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01001918 fe->maxconn = 0;
1919 fe->conn_retries = CONN_RETRIES;
1920 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001921 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001922 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001923 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001924 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001925}
1926
Emeric Brun2b920a12010-09-23 18:30:22 +02001927/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001928 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001929 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001930static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001931{
Willy Tarreau04b92862017-09-15 11:01:04 +02001932 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001933 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001934 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001935 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001936 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001937 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001938
Emeric Brunb3971ab2015-05-12 18:49:09 +02001939 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1940 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001941 s = NULL;
1942
Emeric Brun1138fd02017-06-19 12:38:55 +02001943 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001944 if (!appctx)
1945 goto out_close;
1946
1947 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001948 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001949
Willy Tarreau04b92862017-09-15 11:01:04 +02001950 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001951 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001952 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001953 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001954 }
1955
Willy Tarreau87787ac2017-08-28 16:22:54 +02001956 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001957 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001958 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001959 }
1960
Willy Tarreau342bfb12015-04-05 01:35:34 +02001961 /* The tasks below are normally what is supposed to be done by
1962 * fe->accept().
1963 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001964 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001965
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001966 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001967 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001968 appctx_wakeup(appctx);
1969
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001970 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001971 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001972 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001973
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001974 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001975 * pre-initialized connection in si->conn.
1976 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001977 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001978 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001979
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001980 if (unlikely((cs = cs_new(conn)) == NULL))
1981 goto out_free_conn;
1982
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001983 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02001984 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
1985
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001986 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01001987 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001988 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001989
Emeric Brun2b920a12010-09-23 18:30:22 +02001990 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001991 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001992
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001993 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001994
Emeric Brunb3971ab2015-05-12 18:49:09 +02001995 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001996 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01001997 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001998 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001999
2000 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002001 out_free_conn:
2002 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002003 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002004 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002005 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002006 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002007 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002008 out_free_appctx:
2009 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002010 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002011 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002012}
2013
2014/*
2015 * Task processing function to manage re-connect and peer session
2016 * tasks wakeup on local update.
2017 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002018static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002019{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002020 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002021 struct peer *ps;
2022 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002023
2024 task->expire = TICK_ETERNITY;
2025
Emeric Brunb3971ab2015-05-12 18:49:09 +02002026 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002027 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002028 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002029 task_delete(peers->sync_task);
2030 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002031 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002032 return NULL;
2033 }
2034
Emeric Brun80527f52017-06-19 17:46:37 +02002035 /* Acquire lock for all peers of the section */
2036 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002037 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002038
Emeric Brun2b920a12010-09-23 18:30:22 +02002039 if (!stopping) {
2040 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002041
2042 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2043 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2044 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002045 /* Resync from local peer needed
2046 no peer was assigned for the lesson
2047 and no old local peer found
2048 or resync timeout expire */
2049
2050 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002051 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002052
2053 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002054 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002055 }
2056
2057 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002058 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002059 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002060 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002061 if (!ps->appctx) {
2062 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002063 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002064 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002065 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002066 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002067 tick_is_expired(ps->reconnect, now_ms))) {
2068 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002069 * or previous peer connection established with success
2070 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002071 * and reconnection timer is expired */
2072
2073 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002074 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002075 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002076 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002077 /* If previous session failed during connection
2078 * but reconnection timer is not expired */
2079
2080 /* reschedule task for reconnect */
2081 task->expire = tick_first(task->expire, ps->reconnect);
2082 }
2083 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002084 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002085 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002086 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002087 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2088 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2090 /* Resync from a remote is needed
2091 * and no peer was assigned for lesson
2092 * and current peer may be up2date */
2093
2094 /* assign peer for the lesson */
2095 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002096 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002097
Willy Tarreau9df94c22016-10-31 18:42:52 +01002098 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002099 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002100 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002101 else {
2102 /* Awake session if there is data to push */
2103 for (st = ps->tables; st ; st = st->next) {
2104 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002105 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002106 appctx_wakeup(ps->appctx);
2107 break;
2108 }
2109 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002110 }
2111 /* else do nothing */
2112 } /* SUCCESSCODE */
2113 } /* !ps->peer->local */
2114 } /* for */
2115
2116 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002117 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2118 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2119 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002120 /* Resync from remote peer needed
2121 * no peer was assigned for the lesson
2122 * and resync timeout expire */
2123
2124 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002125 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002126 }
2127
Emeric Brunb3971ab2015-05-12 18:49:09 +02002128 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002129 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002130 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2131 if (!tick_is_expired(peers->resync_timeout, now_ms))
2132 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002133 }
2134 } /* !stopping */
2135 else {
2136 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002137 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002138 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002139 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002140 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002141 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002142 peers->flags |= PEERS_F_DONOTSTOP;
2143 ps = peers->local;
2144 for (st = ps->tables; st ; st = st->next)
2145 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002146 }
2147
2148 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002149 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002150 /* we're killing a connection, we must apply a random delay before
2151 * retrying otherwise the other end will do the same and we can loop
2152 * for a while.
2153 */
2154 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002155 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002156 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002157 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002158 }
2159 }
2160 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002161
Emeric Brunb3971ab2015-05-12 18:49:09 +02002162 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002163 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002164 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002165 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002166 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002167 peers->flags &= ~PEERS_F_DONOTSTOP;
2168 for (st = ps->tables; st ; st = st->next)
2169 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002170 }
2171 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002172 else if (!ps->appctx) {
2173 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002174 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002175 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2176 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2177 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002178 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002179 * or previous peer connection was successfully established
2180 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002181 * or during previous connect, peer replies a try again statuscode */
2182
2183 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002184 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002185 }
2186 else {
2187 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002188 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002189 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002190 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002191 peers->flags &= ~PEERS_F_DONOTSTOP;
2192 for (st = ps->tables; st ; st = st->next)
2193 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002194 }
2195 }
2196 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002197 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002198 /* current peer connection is active and established
2199 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002200 for (st = ps->tables; st ; st = st->next) {
2201 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002202 appctx_wakeup(ps->appctx);
2203 break;
2204 }
2205 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002206 }
2207 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002208
2209 /* Release lock for all peers of the section */
2210 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002211 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002212
Emeric Brun2b920a12010-09-23 18:30:22 +02002213 /* Wakeup for re-connect */
2214 return task;
2215}
2216
Emeric Brunb3971ab2015-05-12 18:49:09 +02002217
Emeric Brun2b920a12010-09-23 18:30:22 +02002218/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002219 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002220 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002221int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002222{
Emeric Brun2b920a12010-09-23 18:30:22 +02002223 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002224 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002225
Emeric Brun2b920a12010-09-23 18:30:22 +02002226 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002227 peers->peers_fe->maxconn += 3;
2228 }
2229
Willy Tarreau4348fad2012-09-20 16:48:07 +02002230 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2231 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002232 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002233 if (!peers->sync_task)
2234 return 0;
2235
Emeric Brunb3971ab2015-05-12 18:49:09 +02002236 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002237 peers->sync_task->context = (void *)peers;
2238 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2239 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002240 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002241}
2242
2243
2244
2245/*
2246 * Function used to register a table for sync on a group of peers
2247 *
2248 */
2249void peers_register_table(struct peers *peers, struct stktable *table)
2250{
2251 struct shared_table *st;
2252 struct peer * curpeer;
2253 int id = 0;
2254
2255 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002256 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002257 st->table = table;
2258 st->next = curpeer->tables;
2259 if (curpeer->tables)
2260 id = curpeer->tables->local_id;
2261 st->local_id = id + 1;
2262
2263 curpeer->tables = st;
2264 }
2265
2266 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002267}
2268