blob: 6d5a5561a170e941c568f60c9bce29ca56676078 [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>
Emeric Brun2b920a12010-09-23 18:30:22 +020042#include <proto/proto_tcp.h>
43#include <proto/proto_http.h>
44#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020045#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020046#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010047#include <proto/signal.h>
48#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051
52
53/*******************************/
54/* Current peer learning state */
55/*******************************/
56
57/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020059/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020060#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
61#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
62#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
63#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
64#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020065 to push data to new process */
66
Emeric Brunb3971ab2015-05-12 18:49:09 +020067#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
68#define PEERS_RESYNC_FROMLOCAL 0x00000000
69#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
70#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
71
72/***********************************/
73/* Current shared table sync state */
74/***********************************/
75#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
76#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020077
78/******************************/
79/* Remote peer teaching state */
80/******************************/
81#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020082#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
83#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
84#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
85#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 +020086#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 +020087
Emeric Brunb3971ab2015-05-12 18:49:09 +020088#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 +020089#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
90
Emeric Brunb3971ab2015-05-12 18:49:09 +020091/*****************************/
92/* Sync message class */
93/*****************************/
94enum {
95 PEER_MSG_CLASS_CONTROL = 0,
96 PEER_MSG_CLASS_ERROR,
97 PEER_MSG_CLASS_STICKTABLE = 10,
98 PEER_MSG_CLASS_RESERVED = 255,
99};
100
101/*****************************/
102/* control message types */
103/*****************************/
104enum {
105 PEER_MSG_CTRL_RESYNCREQ = 0,
106 PEER_MSG_CTRL_RESYNCFINISHED,
107 PEER_MSG_CTRL_RESYNCPARTIAL,
108 PEER_MSG_CTRL_RESYNCCONFIRM,
109};
110
111/*****************************/
112/* error message types */
113/*****************************/
114enum {
115 PEER_MSG_ERR_PROTOCOL = 0,
116 PEER_MSG_ERR_SIZELIMIT,
117};
118
119
120/*******************************/
121/* stick table sync mesg types */
122/* Note: ids >= 128 contains */
123/* id message cotains data */
124/*******************************/
125enum {
126 PEER_MSG_STKT_UPDATE = 128,
127 PEER_MSG_STKT_INCUPDATE,
128 PEER_MSG_STKT_DEFINE,
129 PEER_MSG_STKT_SWITCH,
130 PEER_MSG_STKT_ACK,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200131 PEER_MSG_STKT_UPDATE_TIMED,
132 PEER_MSG_STKT_INCUPDATE_TIMED,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200133};
Emeric Brun2b920a12010-09-23 18:30:22 +0200134
135/**********************************/
136/* Peer Session IO handler states */
137/**********************************/
138
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100139enum {
140 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
141 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
142 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
143 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100144 /* after this point, data were possibly exchanged */
145 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
146 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
147 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
148 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
149 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200150 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
151 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100152 PEER_SESS_ST_END, /* Killed session */
153};
Emeric Brun2b920a12010-09-23 18:30:22 +0200154
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100155/***************************************************/
156/* Peer Session status code - part of the protocol */
157/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200158
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100159#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
160#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200161
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100162#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200163
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100164#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200165
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100166#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
167#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
168#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
169#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200170
171#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200172#define PEER_MAJOR_VER 2
173#define PEER_MINOR_VER 1
174#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200176struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100177static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200178
Emeric Brun18928af2017-03-29 16:32:53 +0200179/* This function encode an uint64 to 'dynamic' length format.
180 The encoded value is written at address *str, and the
181 caller must assure that size after *str is large enought.
182 At return, the *str is set at the next Byte after then
183 encoded integer. The function returns then length of the
184 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200185int intencode(uint64_t i, char **str) {
186 int idx = 0;
187 unsigned char *msg;
188
189 if (!*str)
190 return 0;
191
192 msg = (unsigned char *)*str;
193 if (i < 240) {
194 msg[0] = (unsigned char)i;
195 *str = (char *)&msg[idx+1];
196 return (idx+1);
197 }
198
199 msg[idx] =(unsigned char)i | 240;
200 i = (i - 240) >> 4;
201 while (i >= 128) {
202 msg[++idx] = (unsigned char)i | 128;
203 i = (i - 128) >> 7;
204 }
205 msg[++idx] = (unsigned char)i;
206 *str = (char *)&msg[idx+1];
207 return (idx+1);
208}
209
210
211/* This function returns the decoded integer or 0
212 if decode failed
213 *str point on the beginning of the integer to decode
214 at the end of decoding *str point on the end of the
215 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200216uint64_t intdecode(char **str, char *end)
217{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200218 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200219 uint64_t i;
220 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200221
222 if (!*str)
223 return 0;
224
225 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200226 if (msg >= (unsigned char *)end)
227 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200228
Emeric Brun18928af2017-03-29 16:32:53 +0200229 i = *(msg++);
230 if (i >= 240) {
231 shift = 4;
232 do {
233 if (msg >= (unsigned char *)end)
234 goto fail;
235 i += (uint64_t)*msg << shift;
236 shift += 7;
237 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200238 }
Emeric Brun18928af2017-03-29 16:32:53 +0200239 *str = (char *)msg;
240 return i;
241
242 fail:
243 *str = NULL;
244 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200245}
Emeric Brun2b920a12010-09-23 18:30:22 +0200246
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200247/* Set the stick-table UPDATE message type byte at <msg_type> address,
248 * depending on <use_identifier> and <use_timed> boolean parameters.
249 * Always successful.
250 */
251static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
252{
253 if (use_timed) {
254 if (use_identifier)
255 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
256 else
257 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
258 }
259 else {
260 if (use_identifier)
261 *msg_type = PEER_MSG_STKT_UPDATE;
262 else
263 *msg_type = PEER_MSG_STKT_INCUPDATE;
264 }
265
266}
Emeric Brun2b920a12010-09-23 18:30:22 +0200267/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200268 * This prepare the data update message on the stick session <ts>, <st> is the considered
269 * stick table.
270 * <msg> is a buffer of <size> to recieve data message content
271 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
272 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200273 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200274static 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 +0200275{
276 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200277 unsigned short datalen;
278 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200279 unsigned int data_type;
280 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200281
282 cursor = datamsg = msg + 1 + 5;
283
Emeric Brun2b920a12010-09-23 18:30:22 +0200284 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200285
286 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200287 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200288 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200289 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200290
291 /* encode update identifier if needed */
292 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200293 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200294 memcpy(cursor, &netinteger, sizeof(netinteger));
295 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200296 }
297
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200298 if (use_timed) {
299 netinteger = htonl(tick_remain(now_ms, ts->expire));
300 memcpy(cursor, &netinteger, sizeof(netinteger));
301 cursor += sizeof(netinteger);
302 }
303
Emeric Brunb3971ab2015-05-12 18:49:09 +0200304 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200305 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200306 int stlen = strlen((char *)ts->key.key);
307
Emeric Brunb3971ab2015-05-12 18:49:09 +0200308 intencode(stlen, &cursor);
309 memcpy(cursor, ts->key.key, stlen);
310 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200311 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200312 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200314 memcpy(cursor, &netinteger, sizeof(netinteger));
315 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200316 }
317 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200318 memcpy(cursor, ts->key.key, st->table->key_size);
319 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200320 }
321
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100322 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200323 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200324 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200325
Emeric Brun94900952015-06-11 18:25:54 +0200326 data_ptr = stktable_data_ptr(st->table, ts, data_type);
327 if (data_ptr) {
328 switch (stktable_data_types[data_type].std_type) {
329 case STD_T_SINT: {
330 int data;
331
332 data = stktable_data_cast(data_ptr, std_t_sint);
333 intencode(data, &cursor);
334 break;
335 }
336 case STD_T_UINT: {
337 unsigned int data;
338
339 data = stktable_data_cast(data_ptr, std_t_uint);
340 intencode(data, &cursor);
341 break;
342 }
343 case STD_T_ULL: {
344 unsigned long long data;
345
346 data = stktable_data_cast(data_ptr, std_t_ull);
347 intencode(data, &cursor);
348 break;
349 }
350 case STD_T_FRQP: {
351 struct freq_ctr_period *frqp;
352
353 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
354 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
355 intencode(frqp->curr_ctr, &cursor);
356 intencode(frqp->prev_ctr, &cursor);
357 break;
358 }
359 }
360 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200361 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200363
364 /* Compute datalen */
365 datalen = (cursor - datamsg);
366
367 /* prepare message header */
368 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200369 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200370 cursor = &msg[2];
371 intencode(datalen, &cursor);
372
373 /* move data after header */
374 memmove(cursor, datamsg, datalen);
375
376 /* return header size + data_len */
377 return (cursor - msg) + datalen;
378}
379
380/*
381 * This prepare the switch table message to targeted share table <st>.
382 * <msg> is a buffer of <size> to recieve data message content
383 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
384 * check size)
385 */
386static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
387{
388 int len;
389 unsigned short datalen;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200390 struct chunk *chunk;
391 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200392 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200393 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200394
395 cursor = datamsg = msg + 2 + 5;
396
397 /* Encode data */
398
399 /* encode local id */
400 intencode(st->local_id, &cursor);
401
402 /* encode table name */
403 len = strlen(st->table->id);
404 intencode(len, &cursor);
405 memcpy(cursor, st->table->id, len);
406 cursor += len;
407
408 /* encode table type */
409
410 intencode(st->table->type, &cursor);
411
412 /* encode table key size */
413 intencode(st->table->key_size, &cursor);
414
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200415 chunk = get_trash_chunk();
416 chunkp = chunkq = chunk->str;
Emeric Brun94900952015-06-11 18:25:54 +0200417 /* encode available known data types in table */
418 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
419 if (st->table->data_ofs[data_type]) {
420 switch (stktable_data_types[data_type].std_type) {
421 case STD_T_SINT:
422 case STD_T_UINT:
423 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200424 data |= 1 << data_type;
425 break;
Emeric Brun94900952015-06-11 18:25:54 +0200426 case STD_T_FRQP:
427 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200428 intencode(data_type, &chunkq);
429 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200430 break;
431 }
432 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200433 }
434 intencode(data, &cursor);
435
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200436 /* Encode stick-table entries duration. */
437 intencode(st->table->expire, &cursor);
438
439 if (chunkq > chunkp) {
440 chunk->len = chunkq - chunkp;
441 memcpy(cursor, chunk->str, chunk->len);
442 cursor += chunk->len;
443 }
444
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 /* Compute datalen */
446 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200447
Emeric Brunb3971ab2015-05-12 18:49:09 +0200448 /* prepare message header */
449 msg[0] = PEER_MSG_CLASS_STICKTABLE;
450 msg[1] = PEER_MSG_STKT_DEFINE;
451 cursor = &msg[2];
452 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200453
Emeric Brunb3971ab2015-05-12 18:49:09 +0200454 /* move data after header */
455 memmove(cursor, datamsg, datalen);
456
457 /* return header size + data_len */
458 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200459}
460
Emeric Brunb3971ab2015-05-12 18:49:09 +0200461/*
462 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
463 * stick table.
464 * <msg> is a buffer of <size> to recieve data message content
465 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
466 * check size)
467 */
468static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
469{
470 unsigned short datalen;
471 char *cursor, *datamsg;
472 uint32_t netinteger;
473
Emeric Brunb058f1c2015-09-22 15:50:18 +0200474 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200475
476 intencode(st->remote_id, &cursor);
477 netinteger = htonl(st->last_get);
478 memcpy(cursor, &netinteger, sizeof(netinteger));
479 cursor += sizeof(netinteger);
480
481 /* Compute datalen */
482 datalen = (cursor - datamsg);
483
484 /* prepare message header */
485 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200486 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487 cursor = &msg[2];
488 intencode(datalen, &cursor);
489
490 /* move data after header */
491 memmove(cursor, datamsg, datalen);
492
493 /* return header size + data_len */
494 return (cursor - msg) + datalen;
495}
Emeric Brun2b920a12010-09-23 18:30:22 +0200496
497/*
498 * Callback to release a session with a peer
499 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200500static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200501{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200502 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200503 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200504 struct peer *peer = appctx->ctx.peers.ptr;
505 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200506
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100507 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100508 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200509 return;
510
511 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200512 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100513 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100514 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200515 /* Re-init current table pointers to force announcement on re-connect */
516 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517 peer->appctx = NULL;
518 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200519 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200520 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
521 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200522
523 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200524 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 }
526 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 peer->flags &= PEER_TEACH_RESET;
528 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200529 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100530 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200532 }
533}
534
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200535/* Retrieve the major and minor versions of peers protocol
536 * announced by a remote peer. <str> is a null-terminated
537 * string with the following format: "<maj_ver>.<min_ver>".
538 */
539static int peer_get_version(const char *str,
540 unsigned int *maj_ver, unsigned int *min_ver)
541{
542 unsigned int majv, minv;
543 const char *pos, *saved;
544 const char *end;
545
546 saved = pos = str;
547 end = str + strlen(str);
548
549 majv = read_uint(&pos, end);
550 if (saved == pos || *pos++ != '.')
551 return -1;
552
553 saved = pos;
554 minv = read_uint(&pos, end);
555 if (saved == pos || pos != end)
556 return -1;
557
558 *maj_ver = majv;
559 *min_ver = minv;
560
561 return 0;
562}
Emeric Brun2b920a12010-09-23 18:30:22 +0200563
564/*
565 * IO Handler to handle message exchance with a peer
566 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200567static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200568{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200569 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200570 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200571 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200572 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200573 int reql = 0;
574 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200575 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
576 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200577
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100578 /* Check if the input buffer is avalaible. */
579 if (si_ic(si)->buf->size == 0)
580 goto full;
581
Emeric Brun2b920a12010-09-23 18:30:22 +0200582 while (1) {
583switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200584 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100585 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100586 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100587 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100588 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200589 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100590 case PEER_SESS_ST_GETVERSION:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200591 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200592 if (reql <= 0) { /* closed or EOL not found */
593 if (reql == 0)
594 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100595 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200596 goto switchstate;
597 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100598 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100599 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200600 goto switchstate;
601 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100602 else if (reql > 1 && (trash.str[reql-2] == '\r'))
603 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200604 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100605 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200606
Willy Tarreau06d80a92017-10-19 14:32:15 +0200607 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200608
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200609 /* test protocol */
610 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
611 appctx->st0 = PEER_SESS_ST_EXIT;
612 appctx->st1 = PEER_SESS_SC_ERRPROTO;
613 goto switchstate;
614 }
615 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
616 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100617 appctx->st0 = PEER_SESS_ST_EXIT;
618 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200619 goto switchstate;
620 }
621
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100622 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200623 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100624 case PEER_SESS_ST_GETHOST:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200625 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200626 if (reql <= 0) { /* closed or EOL not found */
627 if (reql == 0)
628 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100629 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200630 goto switchstate;
631 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100632 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100633 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200634 goto switchstate;
635 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100636 else if (reql > 1 && (trash.str[reql-2] == '\r'))
637 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200638 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100639 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200640
Willy Tarreau06d80a92017-10-19 14:32:15 +0200641 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200642
643 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100644 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100645 appctx->st0 = PEER_SESS_ST_EXIT;
646 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 goto switchstate;
648 }
649
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100650 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200651 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100652 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200653 char *p;
Willy Tarreau06d80a92017-10-19 14:32:15 +0200654 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200655 if (reql <= 0) { /* closed or EOL not found */
656 if (reql == 0)
657 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100658 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200659 goto switchstate;
660 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100661 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100663 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200664 goto switchstate;
665 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100666 else if (reql > 1 && (trash.str[reql-2] == '\r'))
667 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200668 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200670
Willy Tarreau06d80a92017-10-19 14:32:15 +0200671 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200672
Emeric Brunb3971ab2015-05-12 18:49:09 +0200673 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100674 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100676 appctx->st0 = PEER_SESS_ST_EXIT;
677 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200678 goto switchstate;
679 }
680 *p = 0;
681
682 /* lookup known peer */
683 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100684 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200685 break;
686 }
687
688 /* if unknown peer */
689 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100690 appctx->st0 = PEER_SESS_ST_EXIT;
691 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200692 goto switchstate;
693 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200694
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100695 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100696 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200697 if (curpeer->local) {
698 /* Local connection, reply a retry */
699 appctx->st0 = PEER_SESS_ST_EXIT;
700 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
701 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200702 }
Emeric Brun80527f52017-06-19 17:46:37 +0200703
704 /* we're killing a connection, we must apply a random delay before
705 * retrying otherwise the other end will do the same and we can loop
706 * for a while.
707 */
708 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100709 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200710 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200711 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
712 if (min_ver == PEER_DWNGRD_MINOR_VER) {
713 curpeer->flags |= PEER_F_DWNGRD;
714 }
715 else {
716 curpeer->flags &= ~PEER_F_DWNGRD;
717 }
718 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200719 curpeer->appctx = appctx;
720 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100721 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200722 /* fall through */
723 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100724 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200725 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200726
Emeric Brun80527f52017-06-19 17:46:37 +0200727 if (!curpeer) {
728 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100729 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200730 if (curpeer->appctx != appctx) {
731 appctx->st0 = PEER_SESS_ST_END;
732 goto switchstate;
733 }
734 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100735 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau06d80a92017-10-19 14:32:15 +0200736 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200737 if (repl <= 0) {
738 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100739 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100740 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200741 goto switchstate;
742 }
743
744 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200745 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200746
747 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200748 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200749
750 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200751 curpeer->confirm = 0;
752
753 /* Init cursors */
754 for (st = curpeer->tables; st ; st = st->next) {
755 st->last_get = st->last_acked = 0;
756 st->teaching_origin = st->last_pushed = st->update;
757 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200758
759 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200760 curpeer->flags &= PEER_TEACH_RESET;
761 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200762
763 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200764 if (curpeer->local) {
765 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200766 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
767 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200768 /* assign local peer for a lesson, consider lesson already requested */
769 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200770 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200771 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200772
Emeric Brunb3971ab2015-05-12 18:49:09 +0200773 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200774 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
775 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200776 /* assign peer for a lesson */
777 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200778 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200779 }
780
781
Emeric Brun2b920a12010-09-23 18:30:22 +0200782 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100783 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200784 goto switchstate;
785 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100786 case PEER_SESS_ST_CONNECT: {
Emeric Brun80527f52017-06-19 17:46:37 +0200787
788 if (!curpeer) {
789 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100790 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200791 if (curpeer->appctx != appctx) {
792 appctx->st0 = PEER_SESS_ST_END;
793 goto switchstate;
794 }
795 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200796
797 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100798 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200799 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
800 PEER_MAJOR_VER,
801 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200802 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200803 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100804 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200805 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200806
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100807 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100808 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200809 goto switchstate;
810 }
811
Willy Tarreau06d80a92017-10-19 14:32:15 +0200812 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200813 if (repl <= 0) {
814 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100815 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100816 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200817 goto switchstate;
818 }
819
820 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100821 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200822 /* fall through */
823 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100824 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200825 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200826
Emeric Brun80527f52017-06-19 17:46:37 +0200827 if (!curpeer) {
828 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100829 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200830 if (curpeer->appctx != appctx) {
831 appctx->st0 = PEER_SESS_ST_END;
832 goto switchstate;
833 }
834 }
835
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100836 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200837 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200838
Willy Tarreau06d80a92017-10-19 14:32:15 +0200839 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200840 if (reql <= 0) { /* closed or EOL not found */
841 if (reql == 0)
842 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100843 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200844 goto switchstate;
845 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100846 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200847 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100848 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200849 goto switchstate;
850 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100851 else if (reql > 1 && (trash.str[reql-2] == '\r'))
852 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200853 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100854 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200855
Willy Tarreau06d80a92017-10-19 14:32:15 +0200856 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200857
858 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200859 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200860
861 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200862 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200863
864 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200865 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200866 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200867 for (st = curpeer->tables; st ; st = st->next) {
868 st->last_get = st->last_acked = 0;
869 st->teaching_origin = st->last_pushed = st->update;
870 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200871
872 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200873 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200874
Emeric Brunb3971ab2015-05-12 18:49:09 +0200875 /* reset teaching and learning flags to 0 */
876 curpeer->flags &= PEER_TEACH_RESET;
877 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200878
Emeric Brunb3971ab2015-05-12 18:49:09 +0200879 /* If current peer is local */
880 if (curpeer->local) {
881 /* flag to start to teach lesson */
882 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200883
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200885 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
886 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887 /* If peer is remote and resync from remote is needed,
888 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200889
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890 /* assign peer for a lesson */
891 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200892 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200893 }
894
895 }
896 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200897 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
898 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200899 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100900 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200901 goto switchstate;
902 }
Willy 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;
909 char *msg_cur = trash.str;
910 char *msg_end = trash.str;
911 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200912 int totl = 0;
913
Emeric Brun80527f52017-06-19 17:46:37 +0200914 if (!curpeer) {
915 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100916 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200917 if (curpeer->appctx != appctx) {
918 appctx->st0 = PEER_SESS_ST_END;
919 goto switchstate;
920 }
921 }
922
Willy Tarreau06d80a92017-10-19 14:32:15 +0200923 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200924 if (reql <= 0) /* closed or EOL not found */
925 goto incomplete;
926
Emeric Brun2b920a12010-09-23 18:30:22 +0200927 totl += reql;
928
Emeric Brunb3971ab2015-05-12 18:49:09 +0200929 if (msg_head[1] >= 128) {
930 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200931 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200932 if (reql <= 0) /* closed */
933 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200934
Emeric Brunb3971ab2015-05-12 18:49:09 +0200935 totl += reql;
936
937 if (msg_head[2] < 240) {
938 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200939 }
940 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200941 int i;
942 char *cur;
943 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200944
Emeric Brunb3971ab2015-05-12 18:49:09 +0200945 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200946 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200947 if (reql <= 0) /* closed */
948 goto incomplete;
949
950 totl += reql;
951
952 if (!(msg_head[i] & 0x80))
953 break;
954 }
955
956 if (i == sizeof(msg_head)) {
957 /* malformed message */
958 appctx->st0 = PEER_SESS_ST_ERRPROTO;
959 goto switchstate;
960
961 }
962 end = (char *)msg_head + sizeof(msg_head);
963 cur = (char *)&msg_head[2];
964 msg_len = intdecode(&cur, end);
965 if (!cur) {
966 /* malformed message */
967 appctx->st0 = PEER_SESS_ST_ERRPROTO;
968 goto switchstate;
969 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200970 }
971
Willy Tarreau86a446e2013-11-25 23:02:37 +0100972
Emeric Brunb3971ab2015-05-12 18:49:09 +0200973 /* Read message content */
974 if (msg_len) {
975 if (msg_len > trash.size) {
976 /* Status code is not success, abort */
977 appctx->st0 = PEER_SESS_ST_ERRSIZE;
978 goto switchstate;
979 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200980
Willy Tarreau06d80a92017-10-19 14:32:15 +0200981 reql = co_getblk(si_oc(si), trash.str, msg_len, totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200982 if (reql <= 0) /* closed */
983 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200984 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100985
Emeric Brunb3971ab2015-05-12 18:49:09 +0200986 msg_end += msg_len;
987 }
988 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100989
Emeric Brunb3971ab2015-05-12 18:49:09 +0200990 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
991 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
992 struct shared_table *st;
993 /* Reset message: remote need resync */
994
995 /* prepare tables fot a global push */
996 for (st = curpeer->tables; st; st = st->next) {
997 st->teaching_origin = st->last_pushed = st->table->update;
998 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100999 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001000
Emeric Brunb3971ab2015-05-12 18:49:09 +02001001 /* reset teaching flags to 0 */
1002 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001003
Emeric Brunb3971ab2015-05-12 18:49:09 +02001004 /* flag to start to teach lesson */
1005 curpeer->flags |= PEER_F_TEACH_PROCESS;
1006
1007
1008 }
1009 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1010
1011 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1012 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001013 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1014 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001015 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001016 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001017 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001018 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1019
1020 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 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001406 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001407 (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
Emeric Brunb3971ab2015-05-12 18:49:09 +02001411 /* Current peer was elected to request a resync */
1412 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));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001417 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 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001424 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001425 }
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 Brun2b920a12010-09-23 18:30:22 +02001428
Emeric Brunb3971ab2015-05-12 18:49:09 +02001429 if (curpeer->tables) {
1430 struct shared_table *st;
1431 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001432
Emeric Brunb3971ab2015-05-12 18:49:09 +02001433 last_local_table = curpeer->last_local_table;
1434 if (!last_local_table)
1435 last_local_table = curpeer->tables;
1436 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001437
Emeric Brunb3971ab2015-05-12 18:49:09 +02001438 while (1) {
1439 if (!st)
1440 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001441
Emeric Brunb3971ab2015-05-12 18:49:09 +02001442 /* It remains some updates to ack */
1443 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001444 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001445
Emeric Brunb3971ab2015-05-12 18:49:09 +02001446 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1447 if (!msglen) {
1448 /* internal error: message does not fit in trash */
1449 appctx->st0 = PEER_SESS_ST_END;
1450 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001451 }
1452
Emeric Brunb3971ab2015-05-12 18:49:09 +02001453 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001454 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001455 if (repl <= 0) {
1456 /* no more write possible */
1457 if (repl == -1) {
1458 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001459 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001460 appctx->st0 = PEER_SESS_ST_END;
1461 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001462 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001463 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001464 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001465
Emeric Brunb3971ab2015-05-12 18:49:09 +02001466 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001467 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001468 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1469 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1470 struct eb32_node *eb;
1471 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001472
Emeric Brunb3971ab2015-05-12 18:49:09 +02001473 if (st != curpeer->last_local_table) {
1474 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001475
Emeric Brunb3971ab2015-05-12 18:49:09 +02001476 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1477 if (!msglen) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001478 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001479 /* internal error: message does not fit in trash */
1480 appctx->st0 = PEER_SESS_ST_END;
1481 goto switchstate;
1482 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001483
Emeric Brunb3971ab2015-05-12 18:49:09 +02001484 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001485 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001486 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001487 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001488 /* no more write possible */
1489 if (repl == -1) {
1490 goto full;
1491 }
1492 appctx->st0 = PEER_SESS_ST_END;
1493 goto switchstate;
1494 }
1495 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001496 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001497
1498 /* We force new pushed to 1 to force identifier in update message */
1499 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001500 while (1) {
1501 uint32_t msglen;
1502 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001503 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001504
1505 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001506 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001507 if (!eb) {
1508 eb = eb32_first(&st->table->updates);
1509 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001510 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001511 break;
1512 }
1513 }
1514
1515 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001516 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517 break;
1518 }
1519
1520 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001521 updateid = ts->upd.key;
1522 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001523 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001524
1525 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001526 if (!msglen) {
1527 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001528 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001529 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001530 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001531 appctx->st0 = PEER_SESS_ST_END;
1532 goto switchstate;
1533 }
1534
1535 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001536 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001537 if (repl <= 0) {
1538 /* no more write possible */
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 if (repl == -1) {
1543 goto full;
1544 }
1545 appctx->st0 = PEER_SESS_ST_END;
1546 goto switchstate;
1547 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001548
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001549 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001550 ts->ref_cnt--;
1551 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001552 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1553 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001554 /* identifier may not needed in next update message */
1555 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001556 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001557 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001558 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001559 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001560 else {
1561 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1562 struct eb32_node *eb;
1563 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001564
Emeric Brunb3971ab2015-05-12 18:49:09 +02001565 if (st != curpeer->last_local_table) {
1566 int msglen;
1567
1568 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1569 if (!msglen) {
1570 /* internal error: message does not fit in trash */
1571 appctx->st0 = PEER_SESS_ST_END;
1572 goto switchstate;
1573 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001574
Emeric Brunb3971ab2015-05-12 18:49:09 +02001575 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001576 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001577 if (repl <= 0) {
1578 /* no more write possible */
1579 if (repl == -1) {
1580 goto full;
1581 }
1582 appctx->st0 = PEER_SESS_ST_END;
1583 goto switchstate;
1584 }
1585 curpeer->last_local_table = st;
1586 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001587
Emeric Brunb3971ab2015-05-12 18:49:09 +02001588 /* We force new pushed to 1 to force identifier in update message */
1589 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001590 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001591 while (1) {
1592 uint32_t msglen;
1593 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001594 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001595 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001596
Emeric Brunb3971ab2015-05-12 18:49:09 +02001597 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001598 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001599 if (!eb) {
1600 st->flags |= SHTABLE_F_TEACH_STAGE1;
1601 eb = eb32_first(&st->table->updates);
1602 if (eb)
1603 st->last_pushed = eb->key - 1;
1604 break;
1605 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001606
Emeric Brunb3971ab2015-05-12 18:49:09 +02001607 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001608 updateid = ts->upd.key;
1609 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001610 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001611
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001612 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001613 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001614 if (!msglen) {
1615 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001616 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001617 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001618 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001619 appctx->st0 = PEER_SESS_ST_END;
1620 goto switchstate;
1621 }
1622
1623 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001624 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001625 if (repl <= 0) {
1626 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001627 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001628 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001629 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001630 if (repl == -1) {
1631 goto full;
1632 }
1633 appctx->st0 = PEER_SESS_ST_END;
1634 goto switchstate;
1635 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001636 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001637 ts->ref_cnt--;
1638 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001639 /* identifier may not needed in next update message */
1640 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001641 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001642 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001643 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001644
Emeric Brunb3971ab2015-05-12 18:49:09 +02001645 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1646 struct eb32_node *eb;
1647 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001648
Emeric Brunb3971ab2015-05-12 18:49:09 +02001649 if (st != curpeer->last_local_table) {
1650 int msglen;
1651
1652 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1653 if (!msglen) {
1654 /* internal error: message does not fit in trash */
1655 appctx->st0 = PEER_SESS_ST_END;
1656 goto switchstate;
1657 }
1658
1659 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001660 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001661 if (repl <= 0) {
1662 /* no more write possible */
1663 if (repl == -1) {
1664 goto full;
1665 }
1666 appctx->st0 = PEER_SESS_ST_END;
1667 goto switchstate;
1668 }
1669 curpeer->last_local_table = st;
1670 }
1671
1672 /* We force new pushed to 1 to force identifier in update message */
1673 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001674 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001675 while (1) {
1676 uint32_t msglen;
1677 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001678 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001679 unsigned updateid;
1680
1681 /* push local updates */
1682 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001683
1684 /* push local updates */
1685 if (!eb || eb->key > st->teaching_origin) {
1686 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001687 break;
1688 }
1689
1690 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001691 updateid = ts->upd.key;
1692 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001693 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001694
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001695 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001696 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001697 if (!msglen) {
1698 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001699 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001700 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001701 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001702 appctx->st0 = PEER_SESS_ST_END;
1703 goto switchstate;
1704 }
1705
1706 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001707 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001708 if (repl <= 0) {
1709 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001710 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001711 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001712 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001713 if (repl == -1) {
1714 goto full;
1715 }
1716 appctx->st0 = PEER_SESS_ST_END;
1717 goto switchstate;
1718 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001719
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001720 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001721 ts->ref_cnt--;
1722 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001723 /* identifier may not needed in next update message */
1724 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001725 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001726 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001727 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001728 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001729
1730 if (st == last_local_table)
1731 break;
1732 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001733 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001734 }
1735
1736
1737 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1738 unsigned char msg[2];
1739
1740 /* Current peer was elected to request a resync */
1741 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001742 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 +02001743 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001744 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001745 if (repl <= 0) {
1746 /* no more write possible */
1747 if (repl == -1)
1748 goto full;
1749 appctx->st0 = PEER_SESS_ST_END;
1750 goto switchstate;
1751 }
1752 /* flag finished message sent */
1753 curpeer->flags |= PEER_F_TEACH_FINISHED;
1754 }
1755
Emeric Brun597b26e2016-08-12 11:23:31 +02001756 /* Confirm finished or partial messages */
1757 while (curpeer->confirm) {
1758 unsigned char msg[2];
1759
1760 /* There is a confirm messages to send */
1761 msg[0] = PEER_MSG_CLASS_CONTROL;
1762 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1763
1764 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001765 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001766 if (repl <= 0) {
1767 /* no more write possible */
1768 if (repl == -1)
1769 goto full;
1770 appctx->st0 = PEER_SESS_ST_END;
1771 goto switchstate;
1772 }
1773 curpeer->confirm--;
1774 }
1775
Emeric Brun2b920a12010-09-23 18:30:22 +02001776 /* noting more to do */
1777 goto out;
1778 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001779 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001780 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001781 if (ci_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001782 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001783 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001784 goto switchstate;
1785 case PEER_SESS_ST_ERRSIZE: {
1786 unsigned char msg[2];
1787
1788 msg[0] = PEER_MSG_CLASS_ERROR;
1789 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1790
Willy Tarreau06d80a92017-10-19 14:32:15 +02001791 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001792 goto full;
1793 appctx->st0 = PEER_SESS_ST_END;
1794 goto switchstate;
1795 }
1796 case PEER_SESS_ST_ERRPROTO: {
1797 unsigned char msg[2];
1798
1799 msg[0] = PEER_MSG_CLASS_ERROR;
1800 msg[1] = PEER_MSG_ERR_PROTOCOL;
1801
Willy Tarreau06d80a92017-10-19 14:32:15 +02001802 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001803 goto full;
1804 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001805 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001806 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001807 case PEER_SESS_ST_END: {
Emeric Brun80527f52017-06-19 17:46:37 +02001808 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001809 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001810 curpeer = NULL;
1811 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001812 si_shutw(si);
1813 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001814 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001815 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001816 }
1817 }
1818 }
1819out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001820 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001821
1822 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001823 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001824 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001825full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001826 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001827 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001828}
1829
Willy Tarreau30576452015-04-13 13:50:30 +02001830static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001831 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001832 .name = "<PEER>", /* used for logging */
1833 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001834 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001835};
Emeric Brun2b920a12010-09-23 18:30:22 +02001836
1837/*
1838 * Use this function to force a close of a peer session
1839 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001840static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001841{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001842 /* Note that the peer sessions which have just been created
1843 * (->st0 == PEER_SESS_ST_CONNECT) must not
1844 * be shutdown, if not, the TCP session will never be closed
1845 * and stay in CLOSE_WAIT state after having been closed by
1846 * the remote side.
1847 */
1848 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001849 return;
1850
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001851 if (appctx->applet != &peer_applet)
1852 return;
1853
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001854 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001855 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001856}
1857
Willy Tarreau91d96282015-03-13 15:47:26 +01001858/* Pre-configures a peers frontend to accept incoming connections */
1859void peers_setup_frontend(struct proxy *fe)
1860{
1861 fe->last_change = now.tv_sec;
1862 fe->cap = PR_CAP_FE;
1863 fe->maxconn = 0;
1864 fe->conn_retries = CONN_RETRIES;
1865 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001866 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001867 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001868 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001869 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001870}
1871
Emeric Brun2b920a12010-09-23 18:30:22 +02001872/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001873 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001874 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001875static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001876{
Willy Tarreau04b92862017-09-15 11:01:04 +02001877 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001878 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001879 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001880 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001881 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001882 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001883
Emeric Brunb3971ab2015-05-12 18:49:09 +02001884 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1885 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001886 s = NULL;
1887
Emeric Brun1138fd02017-06-19 12:38:55 +02001888 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001889 if (!appctx)
1890 goto out_close;
1891
1892 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001893 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001894
Willy Tarreau04b92862017-09-15 11:01:04 +02001895 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001896 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001897 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001898 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001899 }
1900
Willy Tarreau87787ac2017-08-28 16:22:54 +02001901 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001902 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001903 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001904 }
1905
Willy Tarreau342bfb12015-04-05 01:35:34 +02001906 /* The tasks below are normally what is supposed to be done by
1907 * fe->accept().
1908 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001909 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001910
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001911 /* applet is waiting for data */
1912 si_applet_cant_get(&s->si[0]);
1913 appctx_wakeup(appctx);
1914
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001915 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001916 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001917 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001918
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001919 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001920 * pre-initialized connection in si->conn.
1921 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001922 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001923 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001924
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001925 if (unlikely((cs = cs_new(conn)) == NULL))
1926 goto out_free_conn;
1927
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001928 conn_prepare(conn, peer->proto, peer->xprt);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001929 conn_install_mux(conn, &mux_pt_ops, cs);
1930 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001931
1932 conn->target = s->target = &s->be->obj_type;
1933 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001934 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001935 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001936
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001937 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001938
Emeric Brunb3971ab2015-05-12 18:49:09 +02001939 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001940 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001941 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001942
1943 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001944 out_free_conn:
1945 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001946 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001947 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001948 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001949 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001950 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001951 out_free_appctx:
1952 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001953 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001954 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001955}
1956
1957/*
1958 * Task processing function to manage re-connect and peer session
1959 * tasks wakeup on local update.
1960 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001961static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02001962{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001963 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001964 struct peer *ps;
1965 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001966
1967 task->expire = TICK_ETERNITY;
1968
Emeric Brunb3971ab2015-05-12 18:49:09 +02001969 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001970 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001971 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001972 task_delete(peers->sync_task);
1973 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001974 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001975 return NULL;
1976 }
1977
Emeric Brun80527f52017-06-19 17:46:37 +02001978 /* Acquire lock for all peers of the section */
1979 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001980 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001981
Emeric Brun2b920a12010-09-23 18:30:22 +02001982 if (!stopping) {
1983 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001984
1985 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1986 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1987 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001988 /* Resync from local peer needed
1989 no peer was assigned for the lesson
1990 and no old local peer found
1991 or resync timeout expire */
1992
1993 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001994 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001995
1996 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001997 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001998 }
1999
2000 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002001 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002002 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002003 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002004 if (!ps->appctx) {
2005 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002006 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002007 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002008 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002009 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002010 tick_is_expired(ps->reconnect, now_ms))) {
2011 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002012 * or previous peer connection established with success
2013 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002014 * and reconnection timer is expired */
2015
2016 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002017 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002018 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002019 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002020 /* If previous session failed during connection
2021 * but reconnection timer is not expired */
2022
2023 /* reschedule task for reconnect */
2024 task->expire = tick_first(task->expire, ps->reconnect);
2025 }
2026 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002027 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002028 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002029 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002030 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2031 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002032 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2033 /* Resync from a remote is needed
2034 * and no peer was assigned for lesson
2035 * and current peer may be up2date */
2036
2037 /* assign peer for the lesson */
2038 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002039 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002040
Willy Tarreau9df94c22016-10-31 18:42:52 +01002041 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002042 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002043 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002044 else {
2045 /* Awake session if there is data to push */
2046 for (st = ps->tables; st ; st = st->next) {
2047 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002048 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002049 appctx_wakeup(ps->appctx);
2050 break;
2051 }
2052 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 }
2054 /* else do nothing */
2055 } /* SUCCESSCODE */
2056 } /* !ps->peer->local */
2057 } /* for */
2058
2059 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002060 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2061 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2062 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002063 /* Resync from remote peer needed
2064 * no peer was assigned for the lesson
2065 * and resync timeout expire */
2066
2067 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002069 }
2070
Emeric Brunb3971ab2015-05-12 18:49:09 +02002071 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002073 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2074 if (!tick_is_expired(peers->resync_timeout, now_ms))
2075 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002076 }
2077 } /* !stopping */
2078 else {
2079 /* soft stop case */
2080 if (task->state & TASK_WOKEN_SIGNAL) {
2081 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002082 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002083 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002084 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002085 peers->flags |= PEERS_F_DONOTSTOP;
2086 ps = peers->local;
2087 for (st = ps->tables; st ; st = st->next)
2088 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 }
2090
2091 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002092 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002093 /* we're killing a connection, we must apply a random delay before
2094 * retrying otherwise the other end will do the same and we can loop
2095 * for a while.
2096 */
2097 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002098 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002099 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002100 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002101 }
2102 }
2103 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002104
Emeric Brunb3971ab2015-05-12 18:49:09 +02002105 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002107 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002108 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002109 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110 peers->flags &= ~PEERS_F_DONOTSTOP;
2111 for (st = ps->tables; st ; st = st->next)
2112 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002113 }
2114 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002115 else if (!ps->appctx) {
2116 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002117 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002118 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2119 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2120 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002121 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002122 * or previous peer connection was successfully established
2123 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002124 * or during previous connect, peer replies a try again statuscode */
2125
2126 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002127 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 }
2129 else {
2130 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002131 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002132 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002133 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002134 peers->flags &= ~PEERS_F_DONOTSTOP;
2135 for (st = ps->tables; st ; st = st->next)
2136 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002137 }
2138 }
2139 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002140 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002141 /* current peer connection is active and established
2142 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002143 for (st = ps->tables; st ; st = st->next) {
2144 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002145 appctx_wakeup(ps->appctx);
2146 break;
2147 }
2148 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002149 }
2150 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002151
2152 /* Release lock for all peers of the section */
2153 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002154 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002155
Emeric Brun2b920a12010-09-23 18:30:22 +02002156 /* Wakeup for re-connect */
2157 return task;
2158}
2159
Emeric Brunb3971ab2015-05-12 18:49:09 +02002160
Emeric Brun2b920a12010-09-23 18:30:22 +02002161/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002162 *
2163 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002164void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002165{
Emeric Brun2b920a12010-09-23 18:30:22 +02002166 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002167 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002168
Emeric Brun2b920a12010-09-23 18:30:22 +02002169 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002170 peers->peers_fe->maxconn += 3;
2171 }
2172
Willy Tarreau4348fad2012-09-20 16:48:07 +02002173 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2174 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002175 peers->sync_task = task_new(MAX_THREADS_MASK);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002176 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002177 peers->sync_task->context = (void *)peers;
2178 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2179 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2180}
2181
2182
2183
2184/*
2185 * Function used to register a table for sync on a group of peers
2186 *
2187 */
2188void peers_register_table(struct peers *peers, struct stktable *table)
2189{
2190 struct shared_table *st;
2191 struct peer * curpeer;
2192 int id = 0;
2193
2194 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002195 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002196 st->table = table;
2197 st->next = curpeer->tables;
2198 if (curpeer->tables)
2199 id = curpeer->tables->local_id;
2200 st->local_id = id + 1;
2201
2202 curpeer->tables = st;
2203 }
2204
2205 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002206}
2207