blob: 579e096d71ebea62bc911c77ca34c1493b97551e [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>
Emeric Brun2b920a12010-09-23 18:30:22 +020041#include <proto/proto_tcp.h>
42#include <proto/proto_http.h>
43#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020085#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
118
119/*******************************/
120/* stick table sync mesg types */
121/* Note: ids >= 128 contains */
122/* id message cotains data */
123/*******************************/
124enum {
125 PEER_MSG_STKT_UPDATE = 128,
126 PEER_MSG_STKT_INCUPDATE,
127 PEER_MSG_STKT_DEFINE,
128 PEER_MSG_STKT_SWITCH,
129 PEER_MSG_STKT_ACK,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200130 PEER_MSG_STKT_UPDATE_TIMED,
131 PEER_MSG_STKT_INCUPDATE_TIMED,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200132};
Emeric Brun2b920a12010-09-23 18:30:22 +0200133
134/**********************************/
135/* Peer Session IO handler states */
136/**********************************/
137
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100138enum {
139 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
140 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
141 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
142 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100143 /* after this point, data were possibly exchanged */
144 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
145 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
146 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
147 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
148 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200149 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
150 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100151 PEER_SESS_ST_END, /* Killed session */
152};
Emeric Brun2b920a12010-09-23 18:30:22 +0200153
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100154/***************************************************/
155/* Peer Session status code - part of the protocol */
156/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200157
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100158#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
159#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100161#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200162
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100163#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200164
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100165#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
166#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
167#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
168#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200169
170#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200171#define PEER_MAJOR_VER 2
172#define PEER_MINOR_VER 1
173#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200174
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200175struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100176static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200177
Emeric Brun18928af2017-03-29 16:32:53 +0200178/* This function encode an uint64 to 'dynamic' length format.
179 The encoded value is written at address *str, and the
180 caller must assure that size after *str is large enought.
181 At return, the *str is set at the next Byte after then
182 encoded integer. The function returns then length of the
183 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200184int intencode(uint64_t i, char **str) {
185 int idx = 0;
186 unsigned char *msg;
187
188 if (!*str)
189 return 0;
190
191 msg = (unsigned char *)*str;
192 if (i < 240) {
193 msg[0] = (unsigned char)i;
194 *str = (char *)&msg[idx+1];
195 return (idx+1);
196 }
197
198 msg[idx] =(unsigned char)i | 240;
199 i = (i - 240) >> 4;
200 while (i >= 128) {
201 msg[++idx] = (unsigned char)i | 128;
202 i = (i - 128) >> 7;
203 }
204 msg[++idx] = (unsigned char)i;
205 *str = (char *)&msg[idx+1];
206 return (idx+1);
207}
208
209
210/* This function returns the decoded integer or 0
211 if decode failed
212 *str point on the beginning of the integer to decode
213 at the end of decoding *str point on the end of the
214 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200215uint64_t intdecode(char **str, char *end)
216{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200217 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200218 uint64_t i;
219 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200220
221 if (!*str)
222 return 0;
223
224 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200225 if (msg >= (unsigned char *)end)
226 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200227
Emeric Brun18928af2017-03-29 16:32:53 +0200228 i = *(msg++);
229 if (i >= 240) {
230 shift = 4;
231 do {
232 if (msg >= (unsigned char *)end)
233 goto fail;
234 i += (uint64_t)*msg << shift;
235 shift += 7;
236 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200237 }
Emeric Brun18928af2017-03-29 16:32:53 +0200238 *str = (char *)msg;
239 return i;
240
241 fail:
242 *str = NULL;
243 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200244}
Emeric Brun2b920a12010-09-23 18:30:22 +0200245
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200246/* Set the stick-table UPDATE message type byte at <msg_type> address,
247 * depending on <use_identifier> and <use_timed> boolean parameters.
248 * Always successful.
249 */
250static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
251{
252 if (use_timed) {
253 if (use_identifier)
254 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
255 else
256 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
257 }
258 else {
259 if (use_identifier)
260 *msg_type = PEER_MSG_STKT_UPDATE;
261 else
262 *msg_type = PEER_MSG_STKT_INCUPDATE;
263 }
264
265}
Emeric Brun2b920a12010-09-23 18:30:22 +0200266/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200267 * This prepare the data update message on the stick session <ts>, <st> is the considered
268 * stick table.
269 * <msg> is a buffer of <size> to recieve data message content
270 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
271 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200272 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200273static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200274{
275 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276 unsigned short datalen;
277 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200278 unsigned int data_type;
279 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200280
281 cursor = datamsg = msg + 1 + 5;
282
Emeric Brun2b920a12010-09-23 18:30:22 +0200283 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200284
285 /* check if we need to send the update identifer */
Emeric Bruna6a09982015-09-22 15:34:19 +0200286 if (!st->last_pushed || ts->upd.key < st->last_pushed || ((ts->upd.key - st->last_pushed) != 1)) {
287 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200288 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200289
290 /* encode update identifier if needed */
291 if (use_identifier) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200292 netinteger = htonl(ts->upd.key);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200293 memcpy(cursor, &netinteger, sizeof(netinteger));
294 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200295 }
296
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200297 if (use_timed) {
298 netinteger = htonl(tick_remain(now_ms, ts->expire));
299 memcpy(cursor, &netinteger, sizeof(netinteger));
300 cursor += sizeof(netinteger);
301 }
302
Emeric Brunb3971ab2015-05-12 18:49:09 +0200303 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200304 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200305 int stlen = strlen((char *)ts->key.key);
306
Emeric Brunb3971ab2015-05-12 18:49:09 +0200307 intencode(stlen, &cursor);
308 memcpy(cursor, ts->key.key, stlen);
309 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200310 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200311 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200312 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200313 memcpy(cursor, &netinteger, sizeof(netinteger));
314 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200315 }
316 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200317 memcpy(cursor, ts->key.key, st->table->key_size);
318 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200319 }
320
Emeric Brunb3971ab2015-05-12 18:49:09 +0200321 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200322 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200323
Emeric Brun94900952015-06-11 18:25:54 +0200324 data_ptr = stktable_data_ptr(st->table, ts, data_type);
325 if (data_ptr) {
326 switch (stktable_data_types[data_type].std_type) {
327 case STD_T_SINT: {
328 int data;
329
330 data = stktable_data_cast(data_ptr, std_t_sint);
331 intencode(data, &cursor);
332 break;
333 }
334 case STD_T_UINT: {
335 unsigned int data;
336
337 data = stktable_data_cast(data_ptr, std_t_uint);
338 intencode(data, &cursor);
339 break;
340 }
341 case STD_T_ULL: {
342 unsigned long long data;
343
344 data = stktable_data_cast(data_ptr, std_t_ull);
345 intencode(data, &cursor);
346 break;
347 }
348 case STD_T_FRQP: {
349 struct freq_ctr_period *frqp;
350
351 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
352 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
353 intencode(frqp->curr_ctr, &cursor);
354 intencode(frqp->prev_ctr, &cursor);
355 break;
356 }
357 }
358 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200359 }
360
361 /* Compute datalen */
362 datalen = (cursor - datamsg);
363
364 /* prepare message header */
365 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200366 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367 cursor = &msg[2];
368 intencode(datalen, &cursor);
369
370 /* move data after header */
371 memmove(cursor, datamsg, datalen);
372
373 /* return header size + data_len */
374 return (cursor - msg) + datalen;
375}
376
377/*
378 * This prepare the switch table message to targeted share table <st>.
379 * <msg> is a buffer of <size> to recieve data message content
380 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
381 * check size)
382 */
383static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
384{
385 int len;
386 unsigned short datalen;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200387 struct chunk *chunk;
388 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200389 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200390 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200391
392 cursor = datamsg = msg + 2 + 5;
393
394 /* Encode data */
395
396 /* encode local id */
397 intencode(st->local_id, &cursor);
398
399 /* encode table name */
400 len = strlen(st->table->id);
401 intencode(len, &cursor);
402 memcpy(cursor, st->table->id, len);
403 cursor += len;
404
405 /* encode table type */
406
407 intencode(st->table->type, &cursor);
408
409 /* encode table key size */
410 intencode(st->table->key_size, &cursor);
411
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200412 chunk = get_trash_chunk();
413 chunkp = chunkq = chunk->str;
Emeric Brun94900952015-06-11 18:25:54 +0200414 /* encode available known data types in table */
415 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
416 if (st->table->data_ofs[data_type]) {
417 switch (stktable_data_types[data_type].std_type) {
418 case STD_T_SINT:
419 case STD_T_UINT:
420 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200421 data |= 1 << data_type;
422 break;
Emeric Brun94900952015-06-11 18:25:54 +0200423 case STD_T_FRQP:
424 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200425 intencode(data_type, &chunkq);
426 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200427 break;
428 }
429 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200430 }
431 intencode(data, &cursor);
432
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200433 /* Encode stick-table entries duration. */
434 intencode(st->table->expire, &cursor);
435
436 if (chunkq > chunkp) {
437 chunk->len = chunkq - chunkp;
438 memcpy(cursor, chunk->str, chunk->len);
439 cursor += chunk->len;
440 }
441
Emeric Brunb3971ab2015-05-12 18:49:09 +0200442 /* Compute datalen */
443 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200444
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 /* prepare message header */
446 msg[0] = PEER_MSG_CLASS_STICKTABLE;
447 msg[1] = PEER_MSG_STKT_DEFINE;
448 cursor = &msg[2];
449 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200450
Emeric Brunb3971ab2015-05-12 18:49:09 +0200451 /* move data after header */
452 memmove(cursor, datamsg, datalen);
453
454 /* return header size + data_len */
455 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200456}
457
Emeric Brunb3971ab2015-05-12 18:49:09 +0200458/*
459 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
460 * stick table.
461 * <msg> is a buffer of <size> to recieve data message content
462 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
463 * check size)
464 */
465static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
466{
467 unsigned short datalen;
468 char *cursor, *datamsg;
469 uint32_t netinteger;
470
Emeric Brunb058f1c2015-09-22 15:50:18 +0200471 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200472
473 intencode(st->remote_id, &cursor);
474 netinteger = htonl(st->last_get);
475 memcpy(cursor, &netinteger, sizeof(netinteger));
476 cursor += sizeof(netinteger);
477
478 /* Compute datalen */
479 datalen = (cursor - datamsg);
480
481 /* prepare message header */
482 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200483 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200484 cursor = &msg[2];
485 intencode(datalen, &cursor);
486
487 /* move data after header */
488 memmove(cursor, datamsg, datalen);
489
490 /* return header size + data_len */
491 return (cursor - msg) + datalen;
492}
Emeric Brun2b920a12010-09-23 18:30:22 +0200493
494/*
495 * Callback to release a session with a peer
496 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200497static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200498{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200500 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200501 struct peer *peer = appctx->ctx.peers.ptr;
502 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100504 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100505 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200506 return;
507
508 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509 if (peer) {
Willy Tarreau9df94c22016-10-31 18:42:52 +0100510 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200511 /* Re-init current table pointers to force announcement on re-connect */
512 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200513 peer->appctx = NULL;
514 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200515 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
517 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200518
519 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200520 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200521 }
522 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200523 peer->flags &= PEER_TEACH_RESET;
524 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200527 }
528}
529
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200530/* Retrieve the major and minor versions of peers protocol
531 * announced by a remote peer. <str> is a null-terminated
532 * string with the following format: "<maj_ver>.<min_ver>".
533 */
534static int peer_get_version(const char *str,
535 unsigned int *maj_ver, unsigned int *min_ver)
536{
537 unsigned int majv, minv;
538 const char *pos, *saved;
539 const char *end;
540
541 saved = pos = str;
542 end = str + strlen(str);
543
544 majv = read_uint(&pos, end);
545 if (saved == pos || *pos++ != '.')
546 return -1;
547
548 saved = pos;
549 minv = read_uint(&pos, end);
550 if (saved == pos || pos != end)
551 return -1;
552
553 *maj_ver = majv;
554 *min_ver = minv;
555
556 return 0;
557}
Emeric Brun2b920a12010-09-23 18:30:22 +0200558
559/*
560 * IO Handler to handle message exchance with a peer
561 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200562static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200563{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200564 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200565 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200566 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200567 int reql = 0;
568 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200569 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
570 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200571
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100572 /* Check if the input buffer is avalaible. */
573 if (si_ic(si)->buf->size == 0)
574 goto full;
575
Emeric Brun2b920a12010-09-23 18:30:22 +0200576 while (1) {
577switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200578 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100579 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100580 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100581 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100582 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200583 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100584 case PEER_SESS_ST_GETVERSION:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200585 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200586 if (reql <= 0) { /* closed or EOL not found */
587 if (reql == 0)
588 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100589 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200590 goto switchstate;
591 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100592 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100593 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200594 goto switchstate;
595 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100596 else if (reql > 1 && (trash.str[reql-2] == '\r'))
597 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200598 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100599 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200600
Willy Tarreau06d80a92017-10-19 14:32:15 +0200601 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200602
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200603 /* test protocol */
604 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
605 appctx->st0 = PEER_SESS_ST_EXIT;
606 appctx->st1 = PEER_SESS_SC_ERRPROTO;
607 goto switchstate;
608 }
609 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
610 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100611 appctx->st0 = PEER_SESS_ST_EXIT;
612 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200613 goto switchstate;
614 }
615
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100616 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200617 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100618 case PEER_SESS_ST_GETHOST:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200619 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200620 if (reql <= 0) { /* closed or EOL not found */
621 if (reql == 0)
622 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100623 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200624 goto switchstate;
625 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100626 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100627 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 goto switchstate;
629 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100630 else if (reql > 1 && (trash.str[reql-2] == '\r'))
631 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200632 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100633 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200634
Willy Tarreau06d80a92017-10-19 14:32:15 +0200635 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200636
637 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100638 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100639 appctx->st0 = PEER_SESS_ST_EXIT;
640 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200641 goto switchstate;
642 }
643
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100644 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200645 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100646 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 struct peer *curpeer;
648 char *p;
Willy Tarreau06d80a92017-10-19 14:32:15 +0200649 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200650 if (reql <= 0) { /* closed or EOL not found */
651 if (reql == 0)
652 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100653 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200654 goto switchstate;
655 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100656 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200657 /* Incomplete line, we quit */
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 else if (reql > 1 && (trash.str[reql-2] == '\r'))
662 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200663 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100664 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200665
Willy Tarreau06d80a92017-10-19 14:32:15 +0200666 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200667
Emeric Brunb3971ab2015-05-12 18:49:09 +0200668 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200670 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100671 appctx->st0 = PEER_SESS_ST_EXIT;
672 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200673 goto switchstate;
674 }
675 *p = 0;
676
677 /* lookup known peer */
678 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100679 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200680 break;
681 }
682
683 /* if unknown peer */
684 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100685 appctx->st0 = PEER_SESS_ST_EXIT;
686 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200687 goto switchstate;
688 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200689
Willy Tarreau9df94c22016-10-31 18:42:52 +0100690 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200691 if (curpeer->local) {
692 /* Local connection, reply a retry */
693 appctx->st0 = PEER_SESS_ST_EXIT;
694 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
695 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200696 }
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100697 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200699 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
700 if (min_ver == PEER_DWNGRD_MINOR_VER) {
701 curpeer->flags |= PEER_F_DWNGRD;
702 }
703 else {
704 curpeer->flags &= ~PEER_F_DWNGRD;
705 }
706 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200707 curpeer->appctx = appctx;
708 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100709 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200710 /* fall through */
711 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100712 case PEER_SESS_ST_SENDSUCCESS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200713 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200714 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200715
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100716 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau06d80a92017-10-19 14:32:15 +0200717 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200718 if (repl <= 0) {
719 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100720 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100721 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200722 goto switchstate;
723 }
724
725 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200726 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200727
728 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200729 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200730
731 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732 curpeer->confirm = 0;
733
734 /* Init cursors */
735 for (st = curpeer->tables; st ; st = st->next) {
736 st->last_get = st->last_acked = 0;
737 st->teaching_origin = st->last_pushed = st->update;
738 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200739
740 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200741 curpeer->flags &= PEER_TEACH_RESET;
742 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200743
744 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200745 if (curpeer->local) {
746 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200747 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
748 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200749 /* assign local peer for a lesson, consider lesson already requested */
750 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200751 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200752 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200753
Emeric Brunb3971ab2015-05-12 18:49:09 +0200754 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200755 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
756 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200757 /* assign peer for a lesson */
758 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200759 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200760 }
761
762
Emeric Brun2b920a12010-09-23 18:30:22 +0200763 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100764 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200765 goto switchstate;
766 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100767 case PEER_SESS_ST_CONNECT: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200768 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200769
770 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100771 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200772 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
773 PEER_MAJOR_VER,
774 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200775 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200776 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100777 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200778 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200779
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100780 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100781 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200782 goto switchstate;
783 }
784
Willy Tarreau06d80a92017-10-19 14:32:15 +0200785 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200786 if (repl <= 0) {
787 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100788 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100789 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200790 goto switchstate;
791 }
792
793 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100794 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200795 /* fall through */
796 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100797 case PEER_SESS_ST_GETSTATUS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200798 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200799 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200800
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100801 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200802 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200803
Willy Tarreau06d80a92017-10-19 14:32:15 +0200804 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200805 if (reql <= 0) { /* closed or EOL not found */
806 if (reql == 0)
807 goto out;
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 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100811 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200812 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100813 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200814 goto switchstate;
815 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100816 else if (reql > 1 && (trash.str[reql-2] == '\r'))
817 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200818 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100819 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200820
Willy Tarreau06d80a92017-10-19 14:32:15 +0200821 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200822
823 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200824 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200825
826 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200827 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200828
829 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200830 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200831 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200832 for (st = curpeer->tables; st ; st = st->next) {
833 st->last_get = st->last_acked = 0;
834 st->teaching_origin = st->last_pushed = st->update;
835 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200836
837 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200838 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200839
Emeric Brunb3971ab2015-05-12 18:49:09 +0200840 /* reset teaching and learning flags to 0 */
841 curpeer->flags &= PEER_TEACH_RESET;
842 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200843
Emeric Brunb3971ab2015-05-12 18:49:09 +0200844 /* If current peer is local */
845 if (curpeer->local) {
846 /* flag to start to teach lesson */
847 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200848
Emeric Brunb3971ab2015-05-12 18:49:09 +0200849 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200850 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
851 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200852 /* If peer is remote and resync from remote is needed,
853 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200854
Emeric Brunb3971ab2015-05-12 18:49:09 +0200855 /* assign peer for a lesson */
856 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200857 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200858 }
859
860 }
861 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200862 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
863 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200864 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100865 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200866 goto switchstate;
867 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100868 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200869 /* fall through */
870 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100871 case PEER_SESS_ST_WAITMSG: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200872 struct peer *curpeer = appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200873 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200874 uint32_t msg_len = 0;
875 char *msg_cur = trash.str;
876 char *msg_end = trash.str;
877 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200878 int totl = 0;
879
Willy Tarreau06d80a92017-10-19 14:32:15 +0200880 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200881 if (reql <= 0) /* closed or EOL not found */
882 goto incomplete;
883
Emeric Brun2b920a12010-09-23 18:30:22 +0200884 totl += reql;
885
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 if (msg_head[1] >= 128) {
887 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200888 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200889 if (reql <= 0) /* closed */
890 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200891
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892 totl += reql;
893
894 if (msg_head[2] < 240) {
895 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200896 }
897 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200898 int i;
899 char *cur;
900 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200901
Emeric Brunb3971ab2015-05-12 18:49:09 +0200902 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200903 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 if (reql <= 0) /* closed */
905 goto incomplete;
906
907 totl += reql;
908
909 if (!(msg_head[i] & 0x80))
910 break;
911 }
912
913 if (i == sizeof(msg_head)) {
914 /* malformed message */
915 appctx->st0 = PEER_SESS_ST_ERRPROTO;
916 goto switchstate;
917
918 }
919 end = (char *)msg_head + sizeof(msg_head);
920 cur = (char *)&msg_head[2];
921 msg_len = intdecode(&cur, end);
922 if (!cur) {
923 /* malformed message */
924 appctx->st0 = PEER_SESS_ST_ERRPROTO;
925 goto switchstate;
926 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200927 }
928
Willy Tarreau86a446e2013-11-25 23:02:37 +0100929
Emeric Brunb3971ab2015-05-12 18:49:09 +0200930 /* Read message content */
931 if (msg_len) {
932 if (msg_len > trash.size) {
933 /* Status code is not success, abort */
934 appctx->st0 = PEER_SESS_ST_ERRSIZE;
935 goto switchstate;
936 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200937
Willy Tarreau06d80a92017-10-19 14:32:15 +0200938 reql = co_getblk(si_oc(si), trash.str, msg_len, totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200939 if (reql <= 0) /* closed */
940 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200941 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100942
Emeric Brunb3971ab2015-05-12 18:49:09 +0200943 msg_end += msg_len;
944 }
945 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100946
Emeric Brunb3971ab2015-05-12 18:49:09 +0200947 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
948 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
949 struct shared_table *st;
950 /* Reset message: remote need resync */
951
952 /* prepare tables fot a global push */
953 for (st = curpeer->tables; st; st = st->next) {
954 st->teaching_origin = st->last_pushed = st->table->update;
955 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100956 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200957
Emeric Brunb3971ab2015-05-12 18:49:09 +0200958 /* reset teaching flags to 0 */
959 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200960
Emeric Brunb3971ab2015-05-12 18:49:09 +0200961 /* flag to start to teach lesson */
962 curpeer->flags |= PEER_F_TEACH_PROCESS;
963
964
965 }
966 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
967
968 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
969 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200970 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
971 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100972 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200973 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200974 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200975 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
976
977 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
978 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200979 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200980
981 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200982 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
983 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100984 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200986 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +0200988 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989
990 /* If stopping state */
991 if (stopping) {
992 /* Close session, push resync no more needed */
993 curpeer->flags |= PEER_F_TEACH_COMPLETE;
994 appctx->st0 = PEER_SESS_ST_END;
995 goto switchstate;
996 }
Emeric Brun597b26e2016-08-12 11:23:31 +0200997 for (st = curpeer->tables; st; st = st->next) {
998 st->update = st->last_pushed = st->teaching_origin;
999 st->flags = 0;
1000 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001001
1002 /* reset teaching flags to 0 */
1003 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001004 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001005 }
1006 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1007 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1008 int table_id_len;
1009 struct shared_table *st;
1010 int table_type;
1011 int table_keylen;
1012 int table_id;
1013 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001014
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015 table_id = intdecode(&msg_cur, msg_end);
1016 if (!msg_cur) {
1017 /* malformed message */
1018 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1019 goto switchstate;
1020 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001021
Emeric Brunb3971ab2015-05-12 18:49:09 +02001022 table_id_len = intdecode(&msg_cur, msg_end);
1023 if (!msg_cur) {
1024 /* malformed message */
1025 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1026 goto switchstate;
1027 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001028
Emeric Brunb3971ab2015-05-12 18:49:09 +02001029 curpeer->remote_table = NULL;
1030 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1031 /* malformed message */
1032 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1033 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001034 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001035
Emeric Brunb3971ab2015-05-12 18:49:09 +02001036 for (st = curpeer->tables; st; st = st->next) {
1037 /* Reset IDs */
1038 if (st->remote_id == table_id)
1039 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001040
Emeric Brunb3971ab2015-05-12 18:49:09 +02001041 if (!curpeer->remote_table
1042 && (table_id_len == strlen(st->table->id))
1043 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1044 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001045 }
1046 }
1047
Emeric Brunb3971ab2015-05-12 18:49:09 +02001048 if (!curpeer->remote_table) {
1049 goto ignore_msg;
1050 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001051
Emeric Brunb3971ab2015-05-12 18:49:09 +02001052 msg_cur += table_id_len;
1053 if (msg_cur >= msg_end) {
1054 /* malformed message */
1055 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1056 goto switchstate;
1057 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001058
Emeric Brunb3971ab2015-05-12 18:49:09 +02001059 table_type = intdecode(&msg_cur, msg_end);
1060 if (!msg_cur) {
1061 /* malformed message */
1062 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1063 goto switchstate;
1064 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001065
Emeric Brunb3971ab2015-05-12 18:49:09 +02001066 table_keylen = intdecode(&msg_cur, msg_end);
1067 if (!msg_cur) {
1068 /* malformed message */
1069 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1070 goto switchstate;
1071 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001072
Emeric Brunb3971ab2015-05-12 18:49:09 +02001073 table_data = intdecode(&msg_cur, msg_end);
1074 if (!msg_cur) {
1075 /* malformed message */
1076 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1077 goto switchstate;
1078 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001079
Emeric Brunb3971ab2015-05-12 18:49:09 +02001080 if (curpeer->remote_table->table->type != table_type
1081 || curpeer->remote_table->table->key_size != table_keylen) {
1082 curpeer->remote_table = NULL;
1083 goto ignore_msg;
1084 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001085
Emeric Brunb3971ab2015-05-12 18:49:09 +02001086 curpeer->remote_table->remote_data = table_data;
1087 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001088 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001089 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1090 struct shared_table *st;
1091 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001092
Emeric Brunb3971ab2015-05-12 18:49:09 +02001093 table_id = intdecode(&msg_cur, msg_end);
1094 if (!msg_cur) {
1095 /* malformed message */
1096 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1097 goto switchstate;
1098 }
1099 curpeer->remote_table = NULL;
1100 for (st = curpeer->tables; st; st = st->next) {
1101 if (st->remote_id == table_id) {
1102 curpeer->remote_table = st;
1103 break;
1104 }
1105 }
1106
Emeric Brun2b920a12010-09-23 18:30:22 +02001107 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001108 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001109 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1110 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1111 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001112 struct shared_table *st = curpeer->remote_table;
1113 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001114 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001115 unsigned int data_type;
1116 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001117
Emeric Brunb3971ab2015-05-12 18:49:09 +02001118 /* Here we have data message */
1119 if (!st)
1120 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001121
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001122 expire = MS_TO_TICKS(st->table->expire);
1123
1124 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1125 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001126 if (msg_len < sizeof(update)) {
1127 /* malformed message */
1128 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1129 goto switchstate;
1130 }
1131 memcpy(&update, msg_cur, sizeof(update));
1132 msg_cur += sizeof(update);
1133 st->last_get = htonl(update);
1134 }
1135 else {
1136 st->last_get++;
1137 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001138
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001139 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1140 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1141 size_t expire_sz = sizeof expire;
1142
1143 if (msg_cur + expire_sz > msg_end) {
1144 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1145 goto switchstate;
1146 }
1147 memcpy(&expire, msg_cur, expire_sz);
1148 msg_cur += expire_sz;
1149 expire = ntohl(expire);
1150 }
1151
Emeric Brunb3971ab2015-05-12 18:49:09 +02001152 newts = stksess_new(st->table, NULL);
1153 if (!newts)
1154 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001155
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001156 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001157 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001158
Emeric Brunb3971ab2015-05-12 18:49:09 +02001159 to_read = intdecode(&msg_cur, msg_end);
1160 if (!msg_cur) {
1161 /* malformed message */
1162 stksess_free(st->table, newts);
1163 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1164 goto switchstate;
1165 }
1166 to_store = MIN(to_read, st->table->key_size - 1);
1167 if (msg_cur + to_store > msg_end) {
1168 /* malformed message */
1169 stksess_free(st->table, newts);
1170 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1171 goto switchstate;
1172 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001173
Emeric Brunb3971ab2015-05-12 18:49:09 +02001174 memcpy(newts->key.key, msg_cur, to_store);
1175 newts->key.key[to_store] = 0;
1176 msg_cur += to_read;
1177 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001178 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001179 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001180
Emeric Brunb3971ab2015-05-12 18:49:09 +02001181 if (msg_cur + sizeof(netinteger) > msg_end) {
1182 /* malformed message */
1183 stksess_free(st->table, newts);
1184 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1185 goto switchstate;
1186 }
1187 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1188 netinteger = ntohl(netinteger);
1189 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1190 msg_cur += sizeof(netinteger);
1191 }
1192 else {
1193 if (msg_cur + st->table->key_size > msg_end) {
1194 /* malformed message */
1195 stksess_free(st->table, newts);
1196 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1197 goto switchstate;
1198 }
1199 memcpy(newts->key.key, msg_cur, st->table->key_size);
1200 msg_cur += st->table->key_size;
1201 }
1202
1203 /* lookup for existing entry */
1204 ts = stktable_lookup(st->table, newts);
1205 if (ts) {
1206 /* the entry already exist, we can free ours */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001207 stktable_touch_with_exp(st->table, ts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001208 stksess_free(st->table, newts);
1209 newts = NULL;
1210 }
1211 else {
1212 struct eb32_node *eb;
1213
1214 /* create new entry */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001215 ts = stktable_store_with_exp(st->table, newts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001216 newts = NULL; /* don't reuse it */
1217
Emeric Brun234fc3c2015-12-16 15:16:46 +01001218 ts->upd.key= (++st->table->update)+(2147483648U);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001219 eb = eb32_insert(&st->table->updates, &ts->upd);
1220 if (eb != &ts->upd) {
1221 eb32_delete(eb);
1222 eb32_insert(&st->table->updates, &ts->upd);
1223 }
1224 }
1225
Emeric Brun94900952015-06-11 18:25:54 +02001226 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001227
Emeric Brun94900952015-06-11 18:25:54 +02001228 if ((1 << data_type) & st->remote_data) {
1229 switch (stktable_data_types[data_type].std_type) {
1230 case STD_T_SINT: {
1231 int data;
1232
1233 data = intdecode(&msg_cur, msg_end);
1234 if (!msg_cur) {
1235 /* malformed message */
1236 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1237 goto switchstate;
1238 }
1239
1240 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1241 if (data_ptr)
1242 stktable_data_cast(data_ptr, std_t_sint) = data;
1243 break;
1244 }
1245 case STD_T_UINT: {
1246 unsigned int data;
1247
1248 data = intdecode(&msg_cur, msg_end);
1249 if (!msg_cur) {
1250 /* malformed message */
1251 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1252 goto switchstate;
1253 }
1254
1255 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1256 if (data_ptr)
1257 stktable_data_cast(data_ptr, std_t_uint) = data;
1258 break;
1259 }
1260 case STD_T_ULL: {
1261 unsigned long long data;
1262
1263 data = intdecode(&msg_cur, msg_end);
1264 if (!msg_cur) {
1265 /* malformed message */
1266 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1267 goto switchstate;
1268 }
1269
1270 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1271 if (data_ptr)
1272 stktable_data_cast(data_ptr, std_t_ull) = data;
1273 break;
1274 }
1275 case STD_T_FRQP: {
1276 struct freq_ctr_period data;
1277
Willy Tarreau3bb46172016-03-25 18:17:47 +01001278 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001279 if (!msg_cur) {
1280 /* malformed message */
1281 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1282 goto switchstate;
1283 }
1284 data.curr_ctr = intdecode(&msg_cur, msg_end);
1285 if (!msg_cur) {
1286 /* malformed message */
1287 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1288 goto switchstate;
1289 }
1290 data.prev_ctr = intdecode(&msg_cur, msg_end);
1291 if (!msg_cur) {
1292 /* malformed message */
1293 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1294 goto switchstate;
1295 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001296
Emeric Brun94900952015-06-11 18:25:54 +02001297 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1298 if (data_ptr)
1299 stktable_data_cast(data_ptr, std_t_frqp) = data;
1300 break;
1301 }
1302 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001303 }
1304 }
1305 }
1306 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1307 /* ack message */
1308 uint32_t table_id ;
1309 uint32_t update;
1310 struct shared_table *st;
1311
1312 table_id = intdecode(&msg_cur, msg_end);
1313 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1314 /* malformed message */
1315 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1316 goto switchstate;
1317 }
1318 memcpy(&update, msg_cur, sizeof(update));
1319 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001320
Emeric Brunb3971ab2015-05-12 18:49:09 +02001321 for (st = curpeer->tables; st; st = st->next) {
1322 if (st->local_id == table_id) {
1323 st->update = update;
1324 break;
1325 }
1326 }
1327 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001328 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001329 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1330 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001331 goto switchstate;
1332 }
1333
Emeric Brunb3971ab2015-05-12 18:49:09 +02001334ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001335 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001336 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001337 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001338 goto switchstate;
1339
Emeric Brun2b920a12010-09-23 18:30:22 +02001340incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001341 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001342
Willy Tarreau72d6c162013-04-11 16:14:13 +02001343 if (reql < 0) {
1344 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001345 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001346 goto switchstate;
1347 }
1348
Emeric Brun2b920a12010-09-23 18:30:22 +02001349
Emeric Brun2b920a12010-09-23 18:30:22 +02001350
Emeric Brunb3971ab2015-05-12 18:49:09 +02001351
Emeric Brun2b920a12010-09-23 18:30:22 +02001352 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001353 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001354 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1355 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001356 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001357
Emeric Brunb3971ab2015-05-12 18:49:09 +02001358 /* Current peer was elected to request a resync */
1359 msg[0] = PEER_MSG_CLASS_CONTROL;
1360 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001361
Emeric Brunb3971ab2015-05-12 18:49:09 +02001362 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001363 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001364 if (repl <= 0) {
1365 /* no more write possible */
1366 if (repl == -1)
1367 goto full;
1368 appctx->st0 = PEER_SESS_ST_END;
1369 goto switchstate;
1370 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001371 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001372 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001373
Emeric Brunb3971ab2015-05-12 18:49:09 +02001374 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001375
Emeric Brunb3971ab2015-05-12 18:49:09 +02001376 if (curpeer->tables) {
1377 struct shared_table *st;
1378 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001379
Emeric Brunb3971ab2015-05-12 18:49:09 +02001380 last_local_table = curpeer->last_local_table;
1381 if (!last_local_table)
1382 last_local_table = curpeer->tables;
1383 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001384
Emeric Brunb3971ab2015-05-12 18:49:09 +02001385 while (1) {
1386 if (!st)
1387 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001388
Emeric Brunb3971ab2015-05-12 18:49:09 +02001389 /* It remains some updates to ack */
1390 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001391 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001392
Emeric Brunb3971ab2015-05-12 18:49:09 +02001393 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1394 if (!msglen) {
1395 /* internal error: message does not fit in trash */
1396 appctx->st0 = PEER_SESS_ST_END;
1397 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001398 }
1399
Emeric Brunb3971ab2015-05-12 18:49:09 +02001400 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001401 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001402 if (repl <= 0) {
1403 /* no more write possible */
1404 if (repl == -1) {
1405 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001406 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001407 appctx->st0 = PEER_SESS_ST_END;
1408 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001409 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001410 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001411 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001412
Emeric Brunb3971ab2015-05-12 18:49:09 +02001413 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1414 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1415 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1416 struct eb32_node *eb;
1417 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001418
Emeric Brunb3971ab2015-05-12 18:49:09 +02001419 if (st != curpeer->last_local_table) {
1420 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001421
Emeric Brunb3971ab2015-05-12 18:49:09 +02001422 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1423 if (!msglen) {
1424 /* internal error: message does not fit in trash */
1425 appctx->st0 = PEER_SESS_ST_END;
1426 goto switchstate;
1427 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001428
Emeric Brunb3971ab2015-05-12 18:49:09 +02001429 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001430 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001431 if (repl <= 0) {
1432 /* no more write possible */
1433 if (repl == -1) {
1434 goto full;
1435 }
1436 appctx->st0 = PEER_SESS_ST_END;
1437 goto switchstate;
1438 }
1439 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001440 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001441
1442 /* We force new pushed to 1 to force identifier in update message */
1443 new_pushed = 1;
1444 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1445 while (1) {
1446 uint32_t msglen;
1447 struct stksess *ts;
1448
1449 /* push local updates */
1450 if (!eb) {
1451 eb = eb32_first(&st->table->updates);
1452 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001453 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001454 break;
1455 }
1456 }
1457
1458 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001459 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001460 break;
1461 }
1462
1463 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001464 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001465 if (!msglen) {
1466 /* internal error: message does not fit in trash */
1467 appctx->st0 = PEER_SESS_ST_END;
1468 goto switchstate;
1469 }
1470
1471 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001472 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001473 if (repl <= 0) {
1474 /* no more write possible */
1475 if (repl == -1) {
1476 goto full;
1477 }
1478 appctx->st0 = PEER_SESS_ST_END;
1479 goto switchstate;
1480 }
1481 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001482 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1483 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001484 /* identifier may not needed in next update message */
1485 new_pushed = 0;
1486
1487 eb = eb32_next(eb);
1488 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001489 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001490 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001491 else {
1492 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1493 struct eb32_node *eb;
1494 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001495
Emeric Brunb3971ab2015-05-12 18:49:09 +02001496 if (st != curpeer->last_local_table) {
1497 int msglen;
1498
1499 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1500 if (!msglen) {
1501 /* internal error: message does not fit in trash */
1502 appctx->st0 = PEER_SESS_ST_END;
1503 goto switchstate;
1504 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001505
Emeric Brunb3971ab2015-05-12 18:49:09 +02001506 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001507 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001508 if (repl <= 0) {
1509 /* no more write possible */
1510 if (repl == -1) {
1511 goto full;
1512 }
1513 appctx->st0 = PEER_SESS_ST_END;
1514 goto switchstate;
1515 }
1516 curpeer->last_local_table = st;
1517 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001518
Emeric Brunb3971ab2015-05-12 18:49:09 +02001519 /* We force new pushed to 1 to force identifier in update message */
1520 new_pushed = 1;
1521 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1522 while (1) {
1523 uint32_t msglen;
1524 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001525 int use_timed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001526
Emeric Brunb3971ab2015-05-12 18:49:09 +02001527 /* push local updates */
1528 if (!eb) {
1529 st->flags |= SHTABLE_F_TEACH_STAGE1;
1530 eb = eb32_first(&st->table->updates);
1531 if (eb)
1532 st->last_pushed = eb->key - 1;
1533 break;
1534 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001535
Emeric Brunb3971ab2015-05-12 18:49:09 +02001536 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001537 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1538 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001539 if (!msglen) {
1540 /* internal error: message does not fit in trash */
1541 appctx->st0 = PEER_SESS_ST_END;
1542 goto switchstate;
1543 }
1544
1545 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001546 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001547 if (repl <= 0) {
1548 /* no more write possible */
1549 if (repl == -1) {
1550 goto full;
1551 }
1552 appctx->st0 = PEER_SESS_ST_END;
1553 goto switchstate;
1554 }
1555 st->last_pushed = ts->upd.key;
1556 /* identifier may not needed in next update message */
1557 new_pushed = 0;
1558
1559 eb = eb32_next(eb);
1560 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001561 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001562
Emeric Brunb3971ab2015-05-12 18:49:09 +02001563 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1564 struct eb32_node *eb;
1565 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001566
Emeric Brunb3971ab2015-05-12 18:49:09 +02001567 if (st != curpeer->last_local_table) {
1568 int msglen;
1569
1570 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1571 if (!msglen) {
1572 /* internal error: message does not fit in trash */
1573 appctx->st0 = PEER_SESS_ST_END;
1574 goto switchstate;
1575 }
1576
1577 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001578 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001579 if (repl <= 0) {
1580 /* no more write possible */
1581 if (repl == -1) {
1582 goto full;
1583 }
1584 appctx->st0 = PEER_SESS_ST_END;
1585 goto switchstate;
1586 }
1587 curpeer->last_local_table = st;
1588 }
1589
1590 /* We force new pushed to 1 to force identifier in update message */
1591 new_pushed = 1;
1592 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1593 while (1) {
1594 uint32_t msglen;
1595 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001596 int use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001597
1598 /* push local updates */
1599 if (!eb || eb->key > st->teaching_origin) {
1600 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001601 break;
1602 }
1603
1604 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001605 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1606 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001607 if (!msglen) {
1608 /* internal error: message does not fit in trash */
1609 appctx->st0 = PEER_SESS_ST_END;
1610 goto switchstate;
1611 }
1612
1613 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001614 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001615 if (repl <= 0) {
1616 /* no more write possible */
1617 if (repl == -1) {
1618 goto full;
1619 }
1620 appctx->st0 = PEER_SESS_ST_END;
1621 goto switchstate;
1622 }
1623 st->last_pushed = ts->upd.key;
1624 /* identifier may not needed in next update message */
1625 new_pushed = 0;
1626
1627 eb = eb32_next(eb);
1628 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001629 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001630 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001631
1632 if (st == last_local_table)
1633 break;
1634 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001635 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001636 }
1637
1638
1639 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1640 unsigned char msg[2];
1641
1642 /* Current peer was elected to request a resync */
1643 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001644 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 +02001645 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001646 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001647 if (repl <= 0) {
1648 /* no more write possible */
1649 if (repl == -1)
1650 goto full;
1651 appctx->st0 = PEER_SESS_ST_END;
1652 goto switchstate;
1653 }
1654 /* flag finished message sent */
1655 curpeer->flags |= PEER_F_TEACH_FINISHED;
1656 }
1657
Emeric Brun597b26e2016-08-12 11:23:31 +02001658 /* Confirm finished or partial messages */
1659 while (curpeer->confirm) {
1660 unsigned char msg[2];
1661
1662 /* There is a confirm messages to send */
1663 msg[0] = PEER_MSG_CLASS_CONTROL;
1664 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1665
1666 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001667 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001668 if (repl <= 0) {
1669 /* no more write possible */
1670 if (repl == -1)
1671 goto full;
1672 appctx->st0 = PEER_SESS_ST_END;
1673 goto switchstate;
1674 }
1675 curpeer->confirm--;
1676 }
1677
Emeric Brun2b920a12010-09-23 18:30:22 +02001678 /* noting more to do */
1679 goto out;
1680 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001681 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001682 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001683 if (ci_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001684 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001685 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001686 goto switchstate;
1687 case PEER_SESS_ST_ERRSIZE: {
1688 unsigned char msg[2];
1689
1690 msg[0] = PEER_MSG_CLASS_ERROR;
1691 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1692
Willy Tarreau06d80a92017-10-19 14:32:15 +02001693 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001694 goto full;
1695 appctx->st0 = PEER_SESS_ST_END;
1696 goto switchstate;
1697 }
1698 case PEER_SESS_ST_ERRPROTO: {
1699 unsigned char msg[2];
1700
1701 msg[0] = PEER_MSG_CLASS_ERROR;
1702 msg[1] = PEER_MSG_ERR_PROTOCOL;
1703
Willy Tarreau06d80a92017-10-19 14:32:15 +02001704 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001705 goto full;
1706 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001707 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001708 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001709 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001710 si_shutw(si);
1711 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001712 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001713 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001714 }
1715 }
1716 }
1717out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001718 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001719 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001720full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001721 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001722 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001723}
1724
Willy Tarreau30576452015-04-13 13:50:30 +02001725static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001726 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001727 .name = "<PEER>", /* used for logging */
1728 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001729 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001730};
Emeric Brun2b920a12010-09-23 18:30:22 +02001731
1732/*
1733 * Use this function to force a close of a peer session
1734 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001735static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001736{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001737 struct peer *ps;
1738
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001739 /* Note that the peer sessions which have just been created
1740 * (->st0 == PEER_SESS_ST_CONNECT) must not
1741 * be shutdown, if not, the TCP session will never be closed
1742 * and stay in CLOSE_WAIT state after having been closed by
1743 * the remote side.
1744 */
1745 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001746 return;
1747
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001748 if (appctx->applet != &peer_applet)
1749 return;
1750
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001751 ps = appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001752 /* we're killing a connection, we must apply a random delay before
1753 * retrying otherwise the other end will do the same and we can loop
1754 * for a while.
1755 */
1756 if (ps)
1757 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1758
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001759 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001760 appctx->ctx.peers.ptr = NULL;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001761 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001762}
1763
Willy Tarreau91d96282015-03-13 15:47:26 +01001764/* Pre-configures a peers frontend to accept incoming connections */
1765void peers_setup_frontend(struct proxy *fe)
1766{
1767 fe->last_change = now.tv_sec;
1768 fe->cap = PR_CAP_FE;
1769 fe->maxconn = 0;
1770 fe->conn_retries = CONN_RETRIES;
1771 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001772 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001773 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001774 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001775 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001776}
1777
Emeric Brun2b920a12010-09-23 18:30:22 +02001778/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001779 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001780 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001781static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001782{
Willy Tarreau04b92862017-09-15 11:01:04 +02001783 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001784 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001785 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001786 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001787 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001788
Emeric Brunb3971ab2015-05-12 18:49:09 +02001789 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1790 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001791 s = NULL;
1792
1793 appctx = appctx_new(&peer_applet);
1794 if (!appctx)
1795 goto out_close;
1796
1797 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001798 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001799
Willy Tarreau04b92862017-09-15 11:01:04 +02001800 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001801 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001802 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001803 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001804 }
1805
Willy Tarreau87787ac2017-08-28 16:22:54 +02001806 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001807 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001808 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001809 }
1810
Willy Tarreau342bfb12015-04-05 01:35:34 +02001811 /* The tasks below are normally what is supposed to be done by
1812 * fe->accept().
1813 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001814 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001815
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001816 /* applet is waiting for data */
1817 si_applet_cant_get(&s->si[0]);
1818 appctx_wakeup(appctx);
1819
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001820 /* initiate an outgoing connection */
1821 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001822
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001823 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001824 * pre-initialized connection in si->conn.
1825 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001826 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001827 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001828
1829 conn_prepare(conn, peer->proto, peer->xprt);
1830 si_attach_conn(&s->si[1], conn);
1831
1832 conn->target = s->target = &s->be->obj_type;
1833 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001834 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001835 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001836
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001837 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001838
Emeric Brunb3971ab2015-05-12 18:49:09 +02001839 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001840 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001841 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001842
1843 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001844 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001845 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001846 pool_free2(pool2_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001847 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001848 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001849 out_free_appctx:
1850 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001851 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001852 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001853}
1854
1855/*
1856 * Task processing function to manage re-connect and peer session
1857 * tasks wakeup on local update.
1858 */
Simon Horman96553772011-06-08 09:18:51 +09001859static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001860{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001861 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001862 struct peer *ps;
1863 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001864
1865 task->expire = TICK_ETERNITY;
1866
Emeric Brunb3971ab2015-05-12 18:49:09 +02001867 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001868 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001869 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001870 task_delete(peers->sync_task);
1871 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001872 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001873 return NULL;
1874 }
1875
Emeric Brun2b920a12010-09-23 18:30:22 +02001876 if (!stopping) {
1877 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001878
1879 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1880 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1881 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001882 /* Resync from local peer needed
1883 no peer was assigned for the lesson
1884 and no old local peer found
1885 or resync timeout expire */
1886
1887 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001888 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001889
1890 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001891 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001892 }
1893
1894 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001895 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001896 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001897 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001898 if (!ps->appctx) {
1899 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001900 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001901 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001902 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001903 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001904 tick_is_expired(ps->reconnect, now_ms))) {
1905 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001906 * or previous peer connection established with success
1907 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02001908 * and reconnection timer is expired */
1909
1910 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001911 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001912 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001913 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001914 /* If previous session failed during connection
1915 * but reconnection timer is not expired */
1916
1917 /* reschedule task for reconnect */
1918 task->expire = tick_first(task->expire, ps->reconnect);
1919 }
1920 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001921 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001922 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001923 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001924 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1925 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001926 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1927 /* Resync from a remote is needed
1928 * and no peer was assigned for lesson
1929 * and current peer may be up2date */
1930
1931 /* assign peer for the lesson */
1932 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001933 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001934
Willy Tarreau9df94c22016-10-31 18:42:52 +01001935 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001936 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001937 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001938 else {
1939 /* Awake session if there is data to push */
1940 for (st = ps->tables; st ; st = st->next) {
1941 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001942 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001943 appctx_wakeup(ps->appctx);
1944 break;
1945 }
1946 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001947 }
1948 /* else do nothing */
1949 } /* SUCCESSCODE */
1950 } /* !ps->peer->local */
1951 } /* for */
1952
1953 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001954 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1955 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1956 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001957 /* Resync from remote peer needed
1958 * no peer was assigned for the lesson
1959 * and resync timeout expire */
1960
1961 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001962 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001963 }
1964
Emeric Brunb3971ab2015-05-12 18:49:09 +02001965 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001966 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02001967 /* reschedule task to resync timeout if not expired, to ended resync if needed */
1968 if (!tick_is_expired(peers->resync_timeout, now_ms))
1969 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001970 }
1971 } /* !stopping */
1972 else {
1973 /* soft stop case */
1974 if (task->state & TASK_WOKEN_SIGNAL) {
1975 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001976 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001977 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001978 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001979 peers->flags |= PEERS_F_DONOTSTOP;
1980 ps = peers->local;
1981 for (st = ps->tables; st ; st = st->next)
1982 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001983 }
1984
1985 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001986 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001987 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001988 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02001989 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001990 }
1991 }
1992 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001993
Emeric Brunb3971ab2015-05-12 18:49:09 +02001994 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001995 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001996 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001997 /* resync of new process was complete, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001998 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001999 peers->flags &= ~PEERS_F_DONOTSTOP;
2000 for (st = ps->tables; st ; st = st->next)
2001 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002002 }
2003 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002004 else if (!ps->appctx) {
2005 /* If there's 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_SUCCESSCODE ||
2008 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2009 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002010 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002011 * or previous peer connection was successfully established
2012 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002013 * or during previous connect, peer replies a try again statuscode */
2014
2015 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002016 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002017 }
2018 else {
2019 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002020 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002021 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002022 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002023 peers->flags &= ~PEERS_F_DONOTSTOP;
2024 for (st = ps->tables; st ; st = st->next)
2025 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 }
2027 }
2028 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002029 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002030 /* current peer connection is active and established
2031 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002032 for (st = ps->tables; st ; st = st->next) {
2033 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002034 appctx_wakeup(ps->appctx);
2035 break;
2036 }
2037 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002038 }
2039 } /* stopping */
2040 /* Wakeup for re-connect */
2041 return task;
2042}
2043
Emeric Brunb3971ab2015-05-12 18:49:09 +02002044
Emeric Brun2b920a12010-09-23 18:30:22 +02002045/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002046 *
2047 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002048void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002049{
Emeric Brun2b920a12010-09-23 18:30:22 +02002050 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002051 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002052
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002054 peers->peers_fe->maxconn += 3;
2055 }
2056
Willy Tarreau4348fad2012-09-20 16:48:07 +02002057 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2058 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002059 peers->sync_task = task_new(MAX_THREADS_MASK);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002060 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002061 peers->sync_task->context = (void *)peers;
2062 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2063 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2064}
2065
2066
2067
2068/*
2069 * Function used to register a table for sync on a group of peers
2070 *
2071 */
2072void peers_register_table(struct peers *peers, struct stktable *table)
2073{
2074 struct shared_table *st;
2075 struct peer * curpeer;
2076 int id = 0;
2077
2078 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002079 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002080 st->table = table;
2081 st->next = curpeer->tables;
2082 if (curpeer->tables)
2083 id = curpeer->tables->local_id;
2084 st->local_id = id + 1;
2085
2086 curpeer->tables = st;
2087 }
2088
2089 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002090}
2091