blob: 948ff7bcae60ff93502231c29bcd09eca318f821 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020085#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
118
119/*******************************/
120/* stick table sync mesg types */
121/* Note: ids >= 128 contains */
122/* id message cotains data */
123/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200124#define PEER_MSG_STKT_UPDATE 0x80
125#define PEER_MSG_STKT_INCUPDATE 0x81
126#define PEER_MSG_STKT_DEFINE 0x82
127#define PEER_MSG_STKT_SWITCH 0x83
128#define PEER_MSG_STKT_ACK 0x84
129#define PEER_MSG_STKT_UPDATE_TIMED 0x85
130#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200131
132/**********************************/
133/* Peer Session IO handler states */
134/**********************************/
135
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100136enum {
137 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
138 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
139 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
140 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100141 /* after this point, data were possibly exchanged */
142 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
143 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
144 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
145 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
146 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200147 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
148 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149 PEER_SESS_ST_END, /* Killed session */
150};
Emeric Brun2b920a12010-09-23 18:30:22 +0200151
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100152/***************************************************/
153/* Peer Session status code - part of the protocol */
154/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
157#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200158
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100159#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100161#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200162
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100163#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
164#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
165#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
166#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200169#define PEER_MAJOR_VER 2
170#define PEER_MINOR_VER 1
171#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200172
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200173struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100174static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Emeric Brun18928af2017-03-29 16:32:53 +0200176/* This function encode an uint64 to 'dynamic' length format.
177 The encoded value is written at address *str, and the
178 caller must assure that size after *str is large enought.
179 At return, the *str is set at the next Byte after then
180 encoded integer. The function returns then length of the
181 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200182int intencode(uint64_t i, char **str) {
183 int idx = 0;
184 unsigned char *msg;
185
186 if (!*str)
187 return 0;
188
189 msg = (unsigned char *)*str;
190 if (i < 240) {
191 msg[0] = (unsigned char)i;
192 *str = (char *)&msg[idx+1];
193 return (idx+1);
194 }
195
196 msg[idx] =(unsigned char)i | 240;
197 i = (i - 240) >> 4;
198 while (i >= 128) {
199 msg[++idx] = (unsigned char)i | 128;
200 i = (i - 128) >> 7;
201 }
202 msg[++idx] = (unsigned char)i;
203 *str = (char *)&msg[idx+1];
204 return (idx+1);
205}
206
207
208/* This function returns the decoded integer or 0
209 if decode failed
210 *str point on the beginning of the integer to decode
211 at the end of decoding *str point on the end of the
212 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200213uint64_t intdecode(char **str, char *end)
214{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200215 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200216 uint64_t i;
217 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200218
219 if (!*str)
220 return 0;
221
222 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200223 if (msg >= (unsigned char *)end)
224 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200225
Emeric Brun18928af2017-03-29 16:32:53 +0200226 i = *(msg++);
227 if (i >= 240) {
228 shift = 4;
229 do {
230 if (msg >= (unsigned char *)end)
231 goto fail;
232 i += (uint64_t)*msg << shift;
233 shift += 7;
234 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200235 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100236 *str = (char *)msg;
237 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200238
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100239 fail:
240 *str = NULL;
241 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200242}
Emeric Brun2b920a12010-09-23 18:30:22 +0200243
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200244/* Set the stick-table UPDATE message type byte at <msg_type> address,
245 * depending on <use_identifier> and <use_timed> boolean parameters.
246 * Always successful.
247 */
248static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
249{
250 if (use_timed) {
251 if (use_identifier)
252 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
253 else
254 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
255 }
256 else {
257 if (use_identifier)
258 *msg_type = PEER_MSG_STKT_UPDATE;
259 else
260 *msg_type = PEER_MSG_STKT_INCUPDATE;
261 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200262}
Emeric Brun2b920a12010-09-23 18:30:22 +0200263/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200264 * This prepare the data update message on the stick session <ts>, <st> is the considered
265 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800266 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200267 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
268 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200269 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200270static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, unsigned int updateid, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200271{
272 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200273 unsigned short datalen;
274 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200275 unsigned int data_type;
276 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200277
278 cursor = datamsg = msg + 1 + 5;
279
Emeric Brun2b920a12010-09-23 18:30:22 +0200280 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200281
282 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200283 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200284 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200285 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200286
287 /* encode update identifier if needed */
288 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200289 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200290 memcpy(cursor, &netinteger, sizeof(netinteger));
291 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200292 }
293
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200294 if (use_timed) {
295 netinteger = htonl(tick_remain(now_ms, ts->expire));
296 memcpy(cursor, &netinteger, sizeof(netinteger));
297 cursor += sizeof(netinteger);
298 }
299
Emeric Brunb3971ab2015-05-12 18:49:09 +0200300 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200301 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200302 int stlen = strlen((char *)ts->key.key);
303
Emeric Brunb3971ab2015-05-12 18:49:09 +0200304 intencode(stlen, &cursor);
305 memcpy(cursor, ts->key.key, stlen);
306 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200307 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200308 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200309 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200310 memcpy(cursor, &netinteger, sizeof(netinteger));
311 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200312 }
313 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200314 memcpy(cursor, ts->key.key, st->table->key_size);
315 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200316 }
317
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200319 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200320 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200321
Emeric Brun94900952015-06-11 18:25:54 +0200322 data_ptr = stktable_data_ptr(st->table, ts, data_type);
323 if (data_ptr) {
324 switch (stktable_data_types[data_type].std_type) {
325 case STD_T_SINT: {
326 int data;
327
328 data = stktable_data_cast(data_ptr, std_t_sint);
329 intencode(data, &cursor);
330 break;
331 }
332 case STD_T_UINT: {
333 unsigned int data;
334
335 data = stktable_data_cast(data_ptr, std_t_uint);
336 intencode(data, &cursor);
337 break;
338 }
339 case STD_T_ULL: {
340 unsigned long long data;
341
342 data = stktable_data_cast(data_ptr, std_t_ull);
343 intencode(data, &cursor);
344 break;
345 }
346 case STD_T_FRQP: {
347 struct freq_ctr_period *frqp;
348
349 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
350 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
351 intencode(frqp->curr_ctr, &cursor);
352 intencode(frqp->prev_ctr, &cursor);
353 break;
354 }
355 }
356 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200357 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100358 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200359
360 /* Compute datalen */
361 datalen = (cursor - datamsg);
362
363 /* prepare message header */
364 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200365 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200366 cursor = &msg[2];
367 intencode(datalen, &cursor);
368
369 /* move data after header */
370 memmove(cursor, datamsg, datalen);
371
372 /* return header size + data_len */
373 return (cursor - msg) + datalen;
374}
375
376/*
377 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800378 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200379 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
380 * check size)
381 */
382static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
383{
384 int len;
385 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200386 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200387 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200388 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200389 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390
391 cursor = datamsg = msg + 2 + 5;
392
393 /* Encode data */
394
395 /* encode local id */
396 intencode(st->local_id, &cursor);
397
398 /* encode table name */
399 len = strlen(st->table->id);
400 intencode(len, &cursor);
401 memcpy(cursor, st->table->id, len);
402 cursor += len;
403
404 /* encode table type */
405
406 intencode(st->table->type, &cursor);
407
408 /* encode table key size */
409 intencode(st->table->key_size, &cursor);
410
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200411 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200413 /* encode available known data types in table */
414 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
415 if (st->table->data_ofs[data_type]) {
416 switch (stktable_data_types[data_type].std_type) {
417 case STD_T_SINT:
418 case STD_T_UINT:
419 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200420 data |= 1 << data_type;
421 break;
Emeric Brun94900952015-06-11 18:25:54 +0200422 case STD_T_FRQP:
423 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200424 intencode(data_type, &chunkq);
425 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200426 break;
427 }
428 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200429 }
430 intencode(data, &cursor);
431
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200432 /* Encode stick-table entries duration. */
433 intencode(st->table->expire, &cursor);
434
435 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200436 chunk->data = chunkq - chunkp;
437 memcpy(cursor, chunk->area, chunk->data);
438 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200439 }
440
Emeric Brunb3971ab2015-05-12 18:49:09 +0200441 /* Compute datalen */
442 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200443
Emeric Brunb3971ab2015-05-12 18:49:09 +0200444 /* prepare message header */
445 msg[0] = PEER_MSG_CLASS_STICKTABLE;
446 msg[1] = PEER_MSG_STKT_DEFINE;
447 cursor = &msg[2];
448 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200449
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450 /* move data after header */
451 memmove(cursor, datamsg, datalen);
452
453 /* return header size + data_len */
454 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200455}
456
Emeric Brunb3971ab2015-05-12 18:49:09 +0200457/*
458 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
459 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800460 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200461 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
462 * check size)
463 */
464static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
465{
466 unsigned short datalen;
467 char *cursor, *datamsg;
468 uint32_t netinteger;
469
Emeric Brunb058f1c2015-09-22 15:50:18 +0200470 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200471
472 intencode(st->remote_id, &cursor);
473 netinteger = htonl(st->last_get);
474 memcpy(cursor, &netinteger, sizeof(netinteger));
475 cursor += sizeof(netinteger);
476
477 /* Compute datalen */
478 datalen = (cursor - datamsg);
479
480 /* prepare message header */
481 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200482 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483 cursor = &msg[2];
484 intencode(datalen, &cursor);
485
486 /* move data after header */
487 memmove(cursor, datamsg, datalen);
488
489 /* return header size + data_len */
490 return (cursor - msg) + datalen;
491}
Emeric Brun2b920a12010-09-23 18:30:22 +0200492
493/*
494 * Callback to release a session with a peer
495 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200496static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200497{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200498 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200499 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200500 struct peer *peer = appctx->ctx.peers.ptr;
501 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200502
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100503 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100504 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200505 return;
506
507 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200508 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100509 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
510 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100511 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100512 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100513 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200514 /* Re-init current table pointers to force announcement on re-connect */
515 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 peer->appctx = NULL;
517 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200518 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
520 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200521
522 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200523 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200524 }
525 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 peer->flags &= PEER_TEACH_RESET;
527 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200528 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100529 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200530 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200531 }
532}
533
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200534/* Retrieve the major and minor versions of peers protocol
535 * announced by a remote peer. <str> is a null-terminated
536 * string with the following format: "<maj_ver>.<min_ver>".
537 */
538static int peer_get_version(const char *str,
539 unsigned int *maj_ver, unsigned int *min_ver)
540{
541 unsigned int majv, minv;
542 const char *pos, *saved;
543 const char *end;
544
545 saved = pos = str;
546 end = str + strlen(str);
547
548 majv = read_uint(&pos, end);
549 if (saved == pos || *pos++ != '.')
550 return -1;
551
552 saved = pos;
553 minv = read_uint(&pos, end);
554 if (saved == pos || pos != end)
555 return -1;
556
557 *maj_ver = majv;
558 *min_ver = minv;
559
560 return 0;
561}
Emeric Brun2b920a12010-09-23 18:30:22 +0200562
563/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100564 * Parse a line terminated by an optional '\r' character, followed by a mandatory
565 * '\n' character.
566 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
567 * a line could not be read because the communication channel is closed.
568 */
569static inline int peer_getline(struct appctx *appctx)
570{
571 int n;
572 struct stream_interface *si = appctx->owner;
573
574 n = co_getline(si_oc(si), trash.area, trash.size);
575 if (!n)
576 return 0;
577
578 if (n < 0 || trash.area[n - 1] != '\n') {
579 appctx->st0 = PEER_SESS_ST_END;
580 return -1;
581 }
582
583 if (n > 1 && (trash.area[n - 2] == '\r'))
584 trash.area[n - 2] = 0;
585 else
586 trash.area[n - 1] = 0;
587
588 co_skip(si_oc(si), n);
589
590 return n;
591}
592
593/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200594 * IO Handler to handle message exchance with a peer
595 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200596static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200597{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200598 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200599 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200600 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200601 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602 int reql = 0;
603 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200604 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
605 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100606 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +0200607
Joseph Herlant82b2f542018-11-15 12:19:14 -0800608 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200609 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100610 goto full;
611
Emeric Brun2b920a12010-09-23 18:30:22 +0200612 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100613 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200614switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200615 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100616 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100617 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100618 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100619 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100620 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200621 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100622 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100623 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200624
Frédéric Lécaillece025572019-01-21 13:38:06 +0100625 reql = peer_getline(appctx);
626 if (!reql)
627 goto out;
628
629 if (reql < 0)
630 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200631
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200632 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200634 appctx->st0 = PEER_SESS_ST_EXIT;
635 appctx->st1 = PEER_SESS_SC_ERRPROTO;
636 goto switchstate;
637 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200639 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100640 appctx->st0 = PEER_SESS_ST_EXIT;
641 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200642 goto switchstate;
643 }
644
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100645 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200646 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100647 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100648 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200649
Frédéric Lécaillece025572019-01-21 13:38:06 +0100650 reql = peer_getline(appctx);
651 if (!reql)
652 goto out;
653
654 if (reql < 0)
655 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200656
657 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100659 appctx->st0 = PEER_SESS_ST_EXIT;
660 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200661 goto switchstate;
662 }
663
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100664 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200665 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100666 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200667 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100668
669 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200670
Frédéric Lécaillece025572019-01-21 13:38:06 +0100671 reql = peer_getline(appctx);
672 if (!reql)
673 goto out;
674
675 if (reql < 0)
676 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200677
Emeric Brunb3971ab2015-05-12 18:49:09 +0200678 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200680 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100681 appctx->st0 = PEER_SESS_ST_EXIT;
682 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200683 goto switchstate;
684 }
685 *p = 0;
686
687 /* lookup known peer */
688 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200690 break;
691 }
692
693 /* if unknown peer */
694 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100695 appctx->st0 = PEER_SESS_ST_EXIT;
696 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200697 goto switchstate;
698 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200699
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100700 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100701 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200702 if (curpeer->local) {
703 /* Local connection, reply a retry */
704 appctx->st0 = PEER_SESS_ST_EXIT;
705 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
706 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200707 }
Emeric Brun80527f52017-06-19 17:46:37 +0200708
709 /* we're killing a connection, we must apply a random delay before
710 * retrying otherwise the other end will do the same and we can loop
711 * for a while.
712 */
713 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100714 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200715 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200716 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
717 if (min_ver == PEER_DWNGRD_MINOR_VER) {
718 curpeer->flags |= PEER_F_DWNGRD;
719 }
720 else {
721 curpeer->flags &= ~PEER_F_DWNGRD;
722 }
723 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200724 curpeer->appctx = appctx;
725 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100726 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +0100727 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +0200728 /* fall through */
729 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100730 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200731 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200732
Willy Tarreau2d372c22018-11-05 17:12:27 +0100733 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200734 if (!curpeer) {
735 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100736 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200737 if (curpeer->appctx != appctx) {
738 appctx->st0 = PEER_SESS_ST_END;
739 goto switchstate;
740 }
741 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200742 repl = snprintf(trash.area, trash.size,
743 "%d\n",
744 PEER_SESS_SC_SUCCESSCODE);
745 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200746 if (repl <= 0) {
747 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100748 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100749 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200750 goto switchstate;
751 }
752
753 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200754 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200755
756 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200757 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200758
759 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200760 curpeer->confirm = 0;
761
762 /* Init cursors */
763 for (st = curpeer->tables; st ; st = st->next) {
764 st->last_get = st->last_acked = 0;
765 st->teaching_origin = st->last_pushed = st->update;
766 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200767
768 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200769 curpeer->flags &= PEER_TEACH_RESET;
770 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200771
772 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100773 if (curpeer->local) {
774 /* if current host need resyncfrom local and no process assined */
775 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
776 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
777 /* assign local peer for a lesson, consider lesson already requested */
778 curpeer->flags |= PEER_F_LEARN_ASSIGN;
779 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
780 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200781
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100782 }
783 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
784 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
785 /* assign peer for a lesson */
786 curpeer->flags |= PEER_F_LEARN_ASSIGN;
787 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
788 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200789
790
Emeric Brun2b920a12010-09-23 18:30:22 +0200791 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100792 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100793 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200794 goto switchstate;
795 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100796 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100797 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200798 if (!curpeer) {
799 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100800 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200801 if (curpeer->appctx != appctx) {
802 appctx->st0 = PEER_SESS_ST_END;
803 goto switchstate;
804 }
805 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200806
807 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200809 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
810 PEER_MAJOR_VER,
811 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200812 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200813 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100814 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200815 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200816
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100817 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100818 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200819 goto switchstate;
820 }
821
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200822 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200823 if (repl <= 0) {
824 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100825 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100826 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200827 goto switchstate;
828 }
829
830 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100831 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200832 /* fall through */
833 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100834 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200835 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200836
Willy Tarreau2d372c22018-11-05 17:12:27 +0100837 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200838 if (!curpeer) {
839 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100840 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200841 if (curpeer->appctx != appctx) {
842 appctx->st0 = PEER_SESS_ST_END;
843 goto switchstate;
844 }
845 }
846
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100847 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200848 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200849
Frédéric Lécaillece025572019-01-21 13:38:06 +0100850 reql = peer_getline(appctx);
851 if (!reql)
852 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +0200853
Frédéric Lécaillece025572019-01-21 13:38:06 +0100854 if (reql < 0)
855 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200856
857 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200858 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +0200859
860 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200861 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200862
863 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200864 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200865 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200866 for (st = curpeer->tables; st ; st = st->next) {
867 st->last_get = st->last_acked = 0;
868 st->teaching_origin = st->last_pushed = st->update;
869 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200870
871 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100872 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200873
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100874 /* reset teaching and learning flags to 0 */
875 curpeer->flags &= PEER_TEACH_RESET;
876 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200877
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100878 /* If current peer is local */
879 if (curpeer->local) {
880 /* flag to start to teach lesson */
881 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200882
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100883 }
884 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
885 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
886 /* If peer is remote and resync from remote is needed,
887 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200888
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100889 /* assign peer for a lesson */
890 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200891 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200892 }
893
894 }
895 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200896 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
897 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200898 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100899 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200900 goto switchstate;
901 }
Willy Tarreau2d372c22018-11-05 17:12:27 +0100902 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100903 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200904 /* fall through */
905 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100906 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200907 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200908 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200909 char *msg_cur = trash.area;
910 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200911 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200912 int totl = 0;
913
Willy Tarreau2d372c22018-11-05 17:12:27 +0100914 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200915 if (!curpeer) {
916 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100917 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200918 if (curpeer->appctx != appctx) {
919 appctx->st0 = PEER_SESS_ST_END;
920 goto switchstate;
921 }
922 }
923
Willy Tarreau06d80a92017-10-19 14:32:15 +0200924 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200925 if (reql <= 0) /* closed or EOL not found */
926 goto incomplete;
927
Emeric Brun2b920a12010-09-23 18:30:22 +0200928 totl += reql;
929
Emeric Brunb3971ab2015-05-12 18:49:09 +0200930 if (msg_head[1] >= 128) {
931 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200932 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200933 if (reql <= 0) /* closed */
934 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200935
Emeric Brunb3971ab2015-05-12 18:49:09 +0200936 totl += reql;
937
938 if (msg_head[2] < 240) {
939 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200940 }
941 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200942 int i;
943 char *cur;
944 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200945
Emeric Brunb3971ab2015-05-12 18:49:09 +0200946 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200947 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200948 if (reql <= 0) /* closed */
949 goto incomplete;
950
951 totl += reql;
952
953 if (!(msg_head[i] & 0x80))
954 break;
955 }
956
957 if (i == sizeof(msg_head)) {
958 /* malformed message */
959 appctx->st0 = PEER_SESS_ST_ERRPROTO;
960 goto switchstate;
961
962 }
963 end = (char *)msg_head + sizeof(msg_head);
964 cur = (char *)&msg_head[2];
965 msg_len = intdecode(&cur, end);
966 if (!cur) {
967 /* malformed message */
968 appctx->st0 = PEER_SESS_ST_ERRPROTO;
969 goto switchstate;
970 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200971 }
972
Willy Tarreau86a446e2013-11-25 23:02:37 +0100973
Emeric Brunb3971ab2015-05-12 18:49:09 +0200974 /* Read message content */
975 if (msg_len) {
976 if (msg_len > trash.size) {
977 /* Status code is not success, abort */
978 appctx->st0 = PEER_SESS_ST_ERRSIZE;
979 goto switchstate;
980 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200981
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200982 reql = co_getblk(si_oc(si),
983 trash.area,
984 msg_len,
985 totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200986 if (reql <= 0) /* closed */
987 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200988 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100989
Emeric Brunb3971ab2015-05-12 18:49:09 +0200990 msg_end += msg_len;
991 }
992 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100993
Emeric Brunb3971ab2015-05-12 18:49:09 +0200994 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
995 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
996 struct shared_table *st;
997 /* Reset message: remote need resync */
998
999 /* prepare tables fot a global push */
1000 for (st = curpeer->tables; st; st = st->next) {
1001 st->teaching_origin = st->last_pushed = st->table->update;
1002 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001003 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001004
Emeric Brunb3971ab2015-05-12 18:49:09 +02001005 /* reset teaching flags to 0 */
1006 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001007
Emeric Brunb3971ab2015-05-12 18:49:09 +02001008 /* flag to start to teach lesson */
1009 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010 }
1011 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001012 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1013 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001014 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1015 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001016 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001018 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001019 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001020 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1021 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001022 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001023
1024 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001025 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1026 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001027 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001028 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001029 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001030 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001031 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001032
1033 /* If stopping state */
1034 if (stopping) {
1035 /* Close session, push resync no more needed */
1036 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1037 appctx->st0 = PEER_SESS_ST_END;
1038 goto switchstate;
1039 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001040 for (st = curpeer->tables; st; st = st->next) {
1041 st->update = st->last_pushed = st->teaching_origin;
1042 st->flags = 0;
1043 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001044
1045 /* reset teaching flags to 0 */
1046 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001047 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001048 }
1049 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1050 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1051 int table_id_len;
1052 struct shared_table *st;
1053 int table_type;
1054 int table_keylen;
1055 int table_id;
1056 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001057
Emeric Brunb3971ab2015-05-12 18:49:09 +02001058 table_id = intdecode(&msg_cur, msg_end);
1059 if (!msg_cur) {
1060 /* malformed message */
1061 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1062 goto switchstate;
1063 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001064
Emeric Brunb3971ab2015-05-12 18:49:09 +02001065 table_id_len = intdecode(&msg_cur, msg_end);
1066 if (!msg_cur) {
1067 /* malformed message */
1068 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1069 goto switchstate;
1070 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001071
Emeric Brunb3971ab2015-05-12 18:49:09 +02001072 curpeer->remote_table = NULL;
1073 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1074 /* malformed message */
1075 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1076 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001077 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001078
Emeric Brunb3971ab2015-05-12 18:49:09 +02001079 for (st = curpeer->tables; st; st = st->next) {
1080 /* Reset IDs */
1081 if (st->remote_id == table_id)
1082 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001083
Emeric Brunb3971ab2015-05-12 18:49:09 +02001084 if (!curpeer->remote_table
1085 && (table_id_len == strlen(st->table->id))
1086 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1087 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001088 }
1089 }
1090
Emeric Brunb3971ab2015-05-12 18:49:09 +02001091 if (!curpeer->remote_table) {
1092 goto ignore_msg;
1093 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001094
Emeric Brunb3971ab2015-05-12 18:49:09 +02001095 msg_cur += table_id_len;
1096 if (msg_cur >= msg_end) {
1097 /* malformed message */
1098 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1099 goto switchstate;
1100 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001101
Emeric Brunb3971ab2015-05-12 18:49:09 +02001102 table_type = intdecode(&msg_cur, msg_end);
1103 if (!msg_cur) {
1104 /* malformed message */
1105 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1106 goto switchstate;
1107 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001108
Emeric Brunb3971ab2015-05-12 18:49:09 +02001109 table_keylen = intdecode(&msg_cur, msg_end);
1110 if (!msg_cur) {
1111 /* malformed message */
1112 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1113 goto switchstate;
1114 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001115
Emeric Brunb3971ab2015-05-12 18:49:09 +02001116 table_data = intdecode(&msg_cur, msg_end);
1117 if (!msg_cur) {
1118 /* malformed message */
1119 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1120 goto switchstate;
1121 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001122
Emeric Brunb3971ab2015-05-12 18:49:09 +02001123 if (curpeer->remote_table->table->type != table_type
1124 || curpeer->remote_table->table->key_size != table_keylen) {
1125 curpeer->remote_table = NULL;
1126 goto ignore_msg;
1127 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001128
Emeric Brunb3971ab2015-05-12 18:49:09 +02001129 curpeer->remote_table->remote_data = table_data;
1130 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001131 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001132 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1133 struct shared_table *st;
1134 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001135
Emeric Brunb3971ab2015-05-12 18:49:09 +02001136 table_id = intdecode(&msg_cur, msg_end);
1137 if (!msg_cur) {
1138 /* malformed message */
1139 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1140 goto switchstate;
1141 }
1142 curpeer->remote_table = NULL;
1143 for (st = curpeer->tables; st; st = st->next) {
1144 if (st->remote_id == table_id) {
1145 curpeer->remote_table = st;
1146 break;
1147 }
1148 }
1149
Emeric Brun2b920a12010-09-23 18:30:22 +02001150 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001151 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001152 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1153 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1154 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001155 struct shared_table *st = curpeer->remote_table;
1156 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001157 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001158 unsigned int data_type;
1159 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001160
Emeric Brunb3971ab2015-05-12 18:49:09 +02001161 /* Here we have data message */
1162 if (!st)
1163 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001164
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001165 expire = MS_TO_TICKS(st->table->expire);
1166
1167 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1168 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001169 if (msg_len < sizeof(update)) {
1170 /* malformed message */
1171 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1172 goto switchstate;
1173 }
1174 memcpy(&update, msg_cur, sizeof(update));
1175 msg_cur += sizeof(update);
1176 st->last_get = htonl(update);
1177 }
1178 else {
1179 st->last_get++;
1180 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001181
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001182 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1183 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1184 size_t expire_sz = sizeof expire;
1185
1186 if (msg_cur + expire_sz > msg_end) {
1187 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1188 goto switchstate;
1189 }
1190 memcpy(&expire, msg_cur, expire_sz);
1191 msg_cur += expire_sz;
1192 expire = ntohl(expire);
1193 }
1194
Emeric Brunb3971ab2015-05-12 18:49:09 +02001195 newts = stksess_new(st->table, NULL);
1196 if (!newts)
1197 goto ignore_msg;
Emeric Brun55482912018-01-22 15:10:08 +01001198
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001199 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001200 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001201
Emeric Brunb3971ab2015-05-12 18:49:09 +02001202 to_read = intdecode(&msg_cur, msg_end);
1203 if (!msg_cur) {
1204 /* malformed message */
1205 stksess_free(st->table, newts);
1206 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1207 goto switchstate;
1208 }
1209 to_store = MIN(to_read, st->table->key_size - 1);
1210 if (msg_cur + to_store > msg_end) {
1211 /* malformed message */
1212 stksess_free(st->table, newts);
1213 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1214 goto switchstate;
1215 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001216
Emeric Brunb3971ab2015-05-12 18:49:09 +02001217 memcpy(newts->key.key, msg_cur, to_store);
1218 newts->key.key[to_store] = 0;
1219 msg_cur += to_read;
1220 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001221 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001222 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001223
Emeric Brunb3971ab2015-05-12 18:49:09 +02001224 if (msg_cur + sizeof(netinteger) > msg_end) {
1225 /* malformed message */
1226 stksess_free(st->table, newts);
1227 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1228 goto switchstate;
1229 }
1230 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1231 netinteger = ntohl(netinteger);
1232 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1233 msg_cur += sizeof(netinteger);
1234 }
1235 else {
1236 if (msg_cur + st->table->key_size > msg_end) {
1237 /* malformed message */
1238 stksess_free(st->table, newts);
1239 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1240 goto switchstate;
1241 }
1242 memcpy(newts->key.key, msg_cur, st->table->key_size);
1243 msg_cur += st->table->key_size;
1244 }
1245
1246 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001247 ts = stktable_set_entry(st->table, newts);
1248 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001249 stksess_free(st->table, newts);
1250 newts = NULL;
1251 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001252
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001253 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001254
Emeric Brun94900952015-06-11 18:25:54 +02001255 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001256
Emeric Brun94900952015-06-11 18:25:54 +02001257 if ((1 << data_type) & st->remote_data) {
1258 switch (stktable_data_types[data_type].std_type) {
1259 case STD_T_SINT: {
1260 int data;
1261
1262 data = intdecode(&msg_cur, msg_end);
1263 if (!msg_cur) {
1264 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001265 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001266 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001267 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1268 goto switchstate;
1269 }
1270
1271 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1272 if (data_ptr)
1273 stktable_data_cast(data_ptr, std_t_sint) = data;
1274 break;
1275 }
1276 case STD_T_UINT: {
1277 unsigned int data;
1278
1279 data = intdecode(&msg_cur, msg_end);
1280 if (!msg_cur) {
1281 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001282 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001283 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001284 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1285 goto switchstate;
1286 }
1287
1288 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1289 if (data_ptr)
1290 stktable_data_cast(data_ptr, std_t_uint) = data;
1291 break;
1292 }
1293 case STD_T_ULL: {
1294 unsigned long long data;
1295
1296 data = intdecode(&msg_cur, msg_end);
1297 if (!msg_cur) {
1298 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001299 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001300 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001301 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1302 goto switchstate;
1303 }
1304
1305 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1306 if (data_ptr)
1307 stktable_data_cast(data_ptr, std_t_ull) = data;
1308 break;
1309 }
1310 case STD_T_FRQP: {
1311 struct freq_ctr_period data;
1312
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01001313 /* First bit is reserved for the freq_ctr_period lock
1314 Note: here we're still protected by the stksess lock
1315 so we don't need to update the update the freq_ctr_period
1316 using its internal lock */
1317
1318 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)) & ~0x1;
Emeric Brun94900952015-06-11 18:25:54 +02001319 if (!msg_cur) {
1320 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001322 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001323 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1324 goto switchstate;
1325 }
1326 data.curr_ctr = intdecode(&msg_cur, msg_end);
1327 if (!msg_cur) {
1328 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001329 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001330 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001331 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1332 goto switchstate;
1333 }
1334 data.prev_ctr = intdecode(&msg_cur, msg_end);
1335 if (!msg_cur) {
1336 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001337 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001338 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001339 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1340 goto switchstate;
1341 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001342
Emeric Brun94900952015-06-11 18:25:54 +02001343 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1344 if (data_ptr)
1345 stktable_data_cast(data_ptr, std_t_frqp) = data;
1346 break;
1347 }
1348 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001349 }
1350 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001351
Emeric Brun55482912018-01-22 15:10:08 +01001352 /* Force new expiration */
1353 ts->expire = tick_add(now_ms, expire);
1354
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001355 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001356 stktable_touch_remote(st->table, ts, 1);
1357
Emeric Brunb3971ab2015-05-12 18:49:09 +02001358 }
1359 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1360 /* ack message */
1361 uint32_t table_id ;
1362 uint32_t update;
1363 struct shared_table *st;
1364
1365 table_id = intdecode(&msg_cur, msg_end);
1366 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1367 /* malformed message */
1368 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1369 goto switchstate;
1370 }
1371 memcpy(&update, msg_cur, sizeof(update));
1372 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001373
Emeric Brunb3971ab2015-05-12 18:49:09 +02001374 for (st = curpeer->tables; st; st = st->next) {
1375 if (st->local_id == table_id) {
1376 st->update = update;
1377 break;
1378 }
1379 }
1380 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001381 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001382 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1383 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001384 goto switchstate;
1385 }
1386
Emeric Brunb3971ab2015-05-12 18:49:09 +02001387ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001388 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001389 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001390 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001391 goto switchstate;
1392
Emeric Brun2b920a12010-09-23 18:30:22 +02001393incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001394 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001395
Willy Tarreau72d6c162013-04-11 16:14:13 +02001396 if (reql < 0) {
1397 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001398 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001399 goto switchstate;
1400 }
1401
Emeric Brun2b920a12010-09-23 18:30:22 +02001402
Emeric Brun2b920a12010-09-23 18:30:22 +02001403
Emeric Brunb3971ab2015-05-12 18:49:09 +02001404
Emeric Brun2b920a12010-09-23 18:30:22 +02001405 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001406 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1407 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1408 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001409 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001410
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001411 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001412 msg[0] = PEER_MSG_CLASS_CONTROL;
1413 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001414
Emeric Brunb3971ab2015-05-12 18:49:09 +02001415 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001416 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001417 if (repl <= 0) {
1418 /* no more write possible */
1419 if (repl == -1)
1420 goto full;
1421 appctx->st0 = PEER_SESS_ST_END;
1422 goto switchstate;
1423 }
1424 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
1425 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001426
Emeric Brunb3971ab2015-05-12 18:49:09 +02001427 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001428 if (curpeer->tables) {
1429 struct shared_table *st;
1430 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001431
Emeric Brunb3971ab2015-05-12 18:49:09 +02001432 last_local_table = curpeer->last_local_table;
1433 if (!last_local_table)
1434 last_local_table = curpeer->tables;
1435 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001436
Emeric Brunb3971ab2015-05-12 18:49:09 +02001437 while (1) {
1438 if (!st)
1439 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001440
Emeric Brunb3971ab2015-05-12 18:49:09 +02001441 /* It remains some updates to ack */
1442 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001443 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001444
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001445 msglen = peer_prepare_ackmsg(st,
1446 trash.area,
1447 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001448 if (!msglen) {
1449 /* internal error: message does not fit in trash */
1450 appctx->st0 = PEER_SESS_ST_END;
1451 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001452 }
1453
Emeric Brunb3971ab2015-05-12 18:49:09 +02001454 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001455 repl = ci_putblk(si_ic(si),
1456 trash.area,
1457 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001458 if (repl <= 0) {
1459 /* no more write possible */
1460 if (repl == -1) {
1461 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001462 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001463 appctx->st0 = PEER_SESS_ST_END;
1464 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001465 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001466 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001467 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001468
Emeric Brunb3971ab2015-05-12 18:49:09 +02001469 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001470 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001471 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1472 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1473 struct eb32_node *eb;
1474 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001475
Emeric Brunb3971ab2015-05-12 18:49:09 +02001476 if (st != curpeer->last_local_table) {
1477 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001478
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001479 msglen = peer_prepare_switchmsg(st,
1480 trash.area,
1481 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001482 if (!msglen) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001483 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001484 /* internal error: message does not fit in trash */
1485 appctx->st0 = PEER_SESS_ST_END;
1486 goto switchstate;
1487 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001488
Emeric Brunb3971ab2015-05-12 18:49:09 +02001489 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 repl = ci_putblk(si_ic(si),
1491 trash.area,
1492 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001493 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001494 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001495 /* no more write possible */
1496 if (repl == -1) {
1497 goto full;
1498 }
1499 appctx->st0 = PEER_SESS_ST_END;
1500 goto switchstate;
1501 }
1502 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001503 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001504
1505 /* We force new pushed to 1 to force identifier in update message */
1506 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001507 while (1) {
1508 uint32_t msglen;
1509 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001510 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001511
1512 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001513 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001514 if (!eb) {
1515 eb = eb32_first(&st->table->updates);
1516 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001517 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001518 break;
1519 }
1520 }
1521
1522 if ((int)(eb->key - st->table->localupdate) > 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 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001528 updateid = ts->upd.key;
1529 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001530 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001532 msglen = peer_prepare_updatemsg(ts, st, updateid,
1533 trash.area,
1534 trash.size,
1535 new_pushed,
1536 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001537 if (!msglen) {
1538 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001539 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001540 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001541 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001542 appctx->st0 = PEER_SESS_ST_END;
1543 goto switchstate;
1544 }
1545
1546 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001547 repl = ci_putblk(si_ic(si),
1548 trash.area,
1549 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001550 if (repl <= 0) {
1551 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001552 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001553 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001554 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001555 if (repl == -1) {
1556 goto full;
1557 }
1558 appctx->st0 = PEER_SESS_ST_END;
1559 goto switchstate;
1560 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001561
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001562 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001563 ts->ref_cnt--;
1564 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001565 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1566 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001567 /* identifier may not needed in next update message */
1568 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001569 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001570 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001571 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001572 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001573 else {
1574 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1575 struct eb32_node *eb;
1576 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001577
Emeric Brunb3971ab2015-05-12 18:49:09 +02001578 if (st != curpeer->last_local_table) {
1579 int msglen;
1580
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001581 msglen = peer_prepare_switchmsg(st,
1582 trash.area,
1583 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001584 if (!msglen) {
1585 /* internal error: message does not fit in trash */
1586 appctx->st0 = PEER_SESS_ST_END;
1587 goto switchstate;
1588 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001589
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001591 repl = ci_putblk(si_ic(si),
1592 trash.area,
1593 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001594 if (repl <= 0) {
1595 /* no more write possible */
1596 if (repl == -1) {
1597 goto full;
1598 }
1599 appctx->st0 = PEER_SESS_ST_END;
1600 goto switchstate;
1601 }
1602 curpeer->last_local_table = st;
1603 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001604
Emeric Brunb3971ab2015-05-12 18:49:09 +02001605 /* We force new pushed to 1 to force identifier in update message */
1606 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001607 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001608 while (1) {
1609 uint32_t msglen;
1610 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001611 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001612 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001613
Emeric Brunb3971ab2015-05-12 18:49:09 +02001614 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001615 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001616 if (!eb) {
1617 st->flags |= SHTABLE_F_TEACH_STAGE1;
1618 eb = eb32_first(&st->table->updates);
1619 if (eb)
1620 st->last_pushed = eb->key - 1;
1621 break;
1622 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001623
Emeric Brunb3971ab2015-05-12 18:49:09 +02001624 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001625 updateid = ts->upd.key;
1626 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001627 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001628
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001629 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001630 msglen = peer_prepare_updatemsg(ts, st, updateid,
1631 trash.area,
1632 trash.size,
1633 new_pushed,
1634 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001635 if (!msglen) {
1636 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001637 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001638 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001639 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001640 appctx->st0 = PEER_SESS_ST_END;
1641 goto switchstate;
1642 }
1643
1644 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001645 repl = ci_putblk(si_ic(si),
1646 trash.area,
1647 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001648 if (repl <= 0) {
1649 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001650 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001651 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001652 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001653 if (repl == -1) {
1654 goto full;
1655 }
1656 appctx->st0 = PEER_SESS_ST_END;
1657 goto switchstate;
1658 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001659 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001660 ts->ref_cnt--;
1661 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001662 /* identifier may not needed in next update message */
1663 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001664 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001665 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001666 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001667
Emeric Brunb3971ab2015-05-12 18:49:09 +02001668 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1669 struct eb32_node *eb;
1670 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001671
Emeric Brunb3971ab2015-05-12 18:49:09 +02001672 if (st != curpeer->last_local_table) {
1673 int msglen;
1674
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001675 msglen = peer_prepare_switchmsg(st,
1676 trash.area,
1677 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001678 if (!msglen) {
1679 /* internal error: message does not fit in trash */
1680 appctx->st0 = PEER_SESS_ST_END;
1681 goto switchstate;
1682 }
1683
1684 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001685 repl = ci_putblk(si_ic(si),
1686 trash.area,
1687 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001688 if (repl <= 0) {
1689 /* no more write possible */
1690 if (repl == -1) {
1691 goto full;
1692 }
1693 appctx->st0 = PEER_SESS_ST_END;
1694 goto switchstate;
1695 }
1696 curpeer->last_local_table = st;
1697 }
1698
1699 /* We force new pushed to 1 to force identifier in update message */
1700 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001701 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001702 while (1) {
1703 uint32_t msglen;
1704 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001705 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001706 unsigned updateid;
1707
1708 /* push local updates */
1709 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001710
1711 /* push local updates */
1712 if (!eb || eb->key > st->teaching_origin) {
1713 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001714 break;
1715 }
1716
1717 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001718 updateid = ts->upd.key;
1719 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001720 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001721
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001722 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001723 msglen = peer_prepare_updatemsg(ts, st, updateid,
1724 trash.area,
1725 trash.size,
1726 new_pushed,
1727 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001728 if (!msglen) {
1729 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001730 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001731 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001732 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001733 appctx->st0 = PEER_SESS_ST_END;
1734 goto switchstate;
1735 }
1736
1737 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001738 repl = ci_putblk(si_ic(si),
1739 trash.area,
1740 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001741 if (repl <= 0) {
1742 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001743 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001744 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001745 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001746 if (repl == -1) {
1747 goto full;
1748 }
1749 appctx->st0 = PEER_SESS_ST_END;
1750 goto switchstate;
1751 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001752
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001753 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001754 ts->ref_cnt--;
1755 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001756 /* identifier may not needed in next update message */
1757 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001758 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001759 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001760 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001761 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001762
1763 if (st == last_local_table)
1764 break;
1765 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001766 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001767 }
1768
1769
1770 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1771 unsigned char msg[2];
1772
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001773 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001774 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001775 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 +02001776 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001777 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001778 if (repl <= 0) {
1779 /* no more write possible */
1780 if (repl == -1)
1781 goto full;
1782 appctx->st0 = PEER_SESS_ST_END;
1783 goto switchstate;
1784 }
1785 /* flag finished message sent */
1786 curpeer->flags |= PEER_F_TEACH_FINISHED;
1787 }
1788
Emeric Brun597b26e2016-08-12 11:23:31 +02001789 /* Confirm finished or partial messages */
1790 while (curpeer->confirm) {
1791 unsigned char msg[2];
1792
1793 /* There is a confirm messages to send */
1794 msg[0] = PEER_MSG_CLASS_CONTROL;
1795 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1796
1797 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001798 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001799 if (repl <= 0) {
1800 /* no more write possible */
1801 if (repl == -1)
1802 goto full;
1803 appctx->st0 = PEER_SESS_ST_END;
1804 goto switchstate;
1805 }
1806 curpeer->confirm--;
1807 }
1808
Emeric Brun2b920a12010-09-23 18:30:22 +02001809 /* noting more to do */
1810 goto out;
1811 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001812 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001813 if (prev_state == PEER_SESS_ST_WAITMSG)
1814 HA_ATOMIC_SUB(&connected_peers, 1);
1815 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001816 repl = snprintf(trash.area, trash.size,
1817 "%d\n", appctx->st1);
1818 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001819 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001820 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001821 goto switchstate;
1822 case PEER_SESS_ST_ERRSIZE: {
1823 unsigned char msg[2];
1824
Willy Tarreau2d372c22018-11-05 17:12:27 +01001825 if (prev_state == PEER_SESS_ST_WAITMSG)
1826 HA_ATOMIC_SUB(&connected_peers, 1);
1827 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001828 msg[0] = PEER_MSG_CLASS_ERROR;
1829 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1830
Willy Tarreau06d80a92017-10-19 14:32:15 +02001831 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001832 goto full;
1833 appctx->st0 = PEER_SESS_ST_END;
1834 goto switchstate;
1835 }
1836 case PEER_SESS_ST_ERRPROTO: {
1837 unsigned char msg[2];
1838
Willy Tarreau2d372c22018-11-05 17:12:27 +01001839 if (prev_state == PEER_SESS_ST_WAITMSG)
1840 HA_ATOMIC_SUB(&connected_peers, 1);
1841 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001842 msg[0] = PEER_MSG_CLASS_ERROR;
1843 msg[1] = PEER_MSG_ERR_PROTOCOL;
1844
Willy Tarreau06d80a92017-10-19 14:32:15 +02001845 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001846 goto full;
1847 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001848 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001849 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001850 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001851 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001852 if (prev_state == PEER_SESS_ST_WAITMSG)
1853 HA_ATOMIC_SUB(&connected_peers, 1);
1854 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001855 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001856 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001857 curpeer = NULL;
1858 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001859 si_shutw(si);
1860 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001861 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001862 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001863 }
1864 }
1865 }
1866out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001867 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001868
1869 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001870 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001871 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001872full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001873 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001874 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001875}
1876
Willy Tarreau30576452015-04-13 13:50:30 +02001877static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001878 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001879 .name = "<PEER>", /* used for logging */
1880 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001881 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001882};
Emeric Brun2b920a12010-09-23 18:30:22 +02001883
1884/*
1885 * Use this function to force a close of a peer session
1886 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001887static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001888{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001889 /* Note that the peer sessions which have just been created
1890 * (->st0 == PEER_SESS_ST_CONNECT) must not
1891 * be shutdown, if not, the TCP session will never be closed
1892 * and stay in CLOSE_WAIT state after having been closed by
1893 * the remote side.
1894 */
1895 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001896 return;
1897
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001898 if (appctx->applet != &peer_applet)
1899 return;
1900
Willy Tarreau2d372c22018-11-05 17:12:27 +01001901 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
1902 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001903 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001904 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001905}
1906
Willy Tarreau91d96282015-03-13 15:47:26 +01001907/* Pre-configures a peers frontend to accept incoming connections */
1908void peers_setup_frontend(struct proxy *fe)
1909{
1910 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001911 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01001912 fe->maxconn = 0;
1913 fe->conn_retries = CONN_RETRIES;
1914 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001915 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001916 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001917 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001918 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001919}
1920
Emeric Brun2b920a12010-09-23 18:30:22 +02001921/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001922 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001923 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001924static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001925{
Willy Tarreau04b92862017-09-15 11:01:04 +02001926 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001927 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001928 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001929 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001930 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001931 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001932
Emeric Brunb3971ab2015-05-12 18:49:09 +02001933 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1934 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001935 s = NULL;
1936
Emeric Brun1138fd02017-06-19 12:38:55 +02001937 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001938 if (!appctx)
1939 goto out_close;
1940
1941 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001942 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001943
Willy Tarreau04b92862017-09-15 11:01:04 +02001944 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001945 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001946 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001947 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001948 }
1949
Willy Tarreau87787ac2017-08-28 16:22:54 +02001950 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001951 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001952 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001953 }
1954
Willy Tarreau342bfb12015-04-05 01:35:34 +02001955 /* The tasks below are normally what is supposed to be done by
1956 * fe->accept().
1957 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001958 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001959
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001960 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001961 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001962 appctx_wakeup(appctx);
1963
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001964 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001965 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001966 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001967
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001968 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001969 * pre-initialized connection in si->conn.
1970 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001971 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001972 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001973
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001974 if (unlikely((cs = cs_new(conn)) == NULL))
1975 goto out_free_conn;
1976
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001977 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02001978 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
1979
Frédéric Lécaille1055e682018-04-26 14:35:21 +02001980 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01001981 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001982 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001983
Emeric Brun2b920a12010-09-23 18:30:22 +02001984 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001985 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001986
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001987 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001988
Emeric Brunb3971ab2015-05-12 18:49:09 +02001989 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001990 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01001991 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001992 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001993
1994 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001995 out_free_conn:
1996 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001997 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001998 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001999 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002000 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002001 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002002 out_free_appctx:
2003 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002004 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002005 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002006}
2007
2008/*
2009 * Task processing function to manage re-connect and peer session
2010 * tasks wakeup on local update.
2011 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002012static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002013{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002014 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002015 struct peer *ps;
2016 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002017
2018 task->expire = TICK_ETERNITY;
2019
Emeric Brunb3971ab2015-05-12 18:49:09 +02002020 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002021 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002022 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002023 task_delete(peers->sync_task);
2024 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002025 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002026 return NULL;
2027 }
2028
Emeric Brun80527f52017-06-19 17:46:37 +02002029 /* Acquire lock for all peers of the section */
2030 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002031 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002032
Emeric Brun2b920a12010-09-23 18:30:22 +02002033 if (!stopping) {
2034 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002035
2036 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2037 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2038 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002039 /* Resync from local peer needed
2040 no peer was assigned for the lesson
2041 and no old local peer found
2042 or resync timeout expire */
2043
2044 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002045 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002046
2047 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002048 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002049 }
2050
2051 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002052 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002054 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002055 if (!ps->appctx) {
2056 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002057 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002058 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002059 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002060 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002061 tick_is_expired(ps->reconnect, now_ms))) {
2062 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002063 * or previous peer connection established with success
2064 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002065 * and reconnection timer is expired */
2066
2067 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002068 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002069 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002070 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002071 /* If previous session failed during connection
2072 * but reconnection timer is not expired */
2073
2074 /* reschedule task for reconnect */
2075 task->expire = tick_first(task->expire, ps->reconnect);
2076 }
2077 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002078 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002079 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002080 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002081 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2082 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002083 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2084 /* Resync from a remote is needed
2085 * and no peer was assigned for lesson
2086 * and current peer may be up2date */
2087
2088 /* assign peer for the lesson */
2089 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002090 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002091
Willy Tarreau9df94c22016-10-31 18:42:52 +01002092 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002093 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002094 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002095 else {
2096 /* Awake session if there is data to push */
2097 for (st = ps->tables; st ; st = st->next) {
2098 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002099 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002100 appctx_wakeup(ps->appctx);
2101 break;
2102 }
2103 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002104 }
2105 /* else do nothing */
2106 } /* SUCCESSCODE */
2107 } /* !ps->peer->local */
2108 } /* for */
2109
2110 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002111 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2112 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2113 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002114 /* Resync from remote peer needed
2115 * no peer was assigned for the lesson
2116 * and resync timeout expire */
2117
2118 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002119 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002120 }
2121
Emeric Brunb3971ab2015-05-12 18:49:09 +02002122 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002123 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002124 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2125 if (!tick_is_expired(peers->resync_timeout, now_ms))
2126 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002127 }
2128 } /* !stopping */
2129 else {
2130 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002131 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002132 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002133 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002134 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002135 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002136 peers->flags |= PEERS_F_DONOTSTOP;
2137 ps = peers->local;
2138 for (st = ps->tables; st ; st = st->next)
2139 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002140 }
2141
2142 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002143 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002144 /* we're killing a connection, we must apply a random delay before
2145 * retrying otherwise the other end will do the same and we can loop
2146 * for a while.
2147 */
2148 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002149 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002150 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002151 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002152 }
2153 }
2154 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002155
Emeric Brunb3971ab2015-05-12 18:49:09 +02002156 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002157 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002158 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002159 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002160 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002161 peers->flags &= ~PEERS_F_DONOTSTOP;
2162 for (st = ps->tables; st ; st = st->next)
2163 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002164 }
2165 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002166 else if (!ps->appctx) {
2167 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002168 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002169 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2170 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2171 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002172 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002173 * or previous peer connection was successfully established
2174 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002175 * or during previous connect, peer replies a try again statuscode */
2176
2177 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002178 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002179 }
2180 else {
2181 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002182 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002183 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002184 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002185 peers->flags &= ~PEERS_F_DONOTSTOP;
2186 for (st = ps->tables; st ; st = st->next)
2187 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002188 }
2189 }
2190 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002191 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002192 /* current peer connection is active and established
2193 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002194 for (st = ps->tables; st ; st = st->next) {
2195 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002196 appctx_wakeup(ps->appctx);
2197 break;
2198 }
2199 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002200 }
2201 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002202
2203 /* Release lock for all peers of the section */
2204 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002205 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002206
Emeric Brun2b920a12010-09-23 18:30:22 +02002207 /* Wakeup for re-connect */
2208 return task;
2209}
2210
Emeric Brunb3971ab2015-05-12 18:49:09 +02002211
Emeric Brun2b920a12010-09-23 18:30:22 +02002212/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002213 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002214 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002215int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002216{
Emeric Brun2b920a12010-09-23 18:30:22 +02002217 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002218 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002219
Emeric Brun2b920a12010-09-23 18:30:22 +02002220 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002221 peers->peers_fe->maxconn += 3;
2222 }
2223
Willy Tarreau4348fad2012-09-20 16:48:07 +02002224 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2225 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002226 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002227 if (!peers->sync_task)
2228 return 0;
2229
Emeric Brunb3971ab2015-05-12 18:49:09 +02002230 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002231 peers->sync_task->context = (void *)peers;
2232 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2233 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002234 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002235}
2236
2237
2238
2239/*
2240 * Function used to register a table for sync on a group of peers
2241 *
2242 */
2243void peers_register_table(struct peers *peers, struct stktable *table)
2244{
2245 struct shared_table *st;
2246 struct peer * curpeer;
2247 int id = 0;
2248
2249 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002250 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002251 st->table = table;
2252 st->next = curpeer->tables;
2253 if (curpeer->tables)
2254 id = curpeer->tables->local_id;
2255 st->local_id = id + 1;
2256
2257 curpeer->tables = st;
2258 }
2259
2260 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002261}
2262