blob: ef332eba233e422a5b6a77aa216d39071b367654 [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 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200273static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, unsigned int updateid, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +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 Brun819fc6f2017-06-13 19:37:32 +0200286 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200287 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 Brun819fc6f2017-06-13 19:37:32 +0200292 netinteger = htonl(updateid);
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 Brun819fc6f2017-06-13 19:37:32 +0200321 RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200322 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200323 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200324
Emeric Brun94900952015-06-11 18:25:54 +0200325 data_ptr = stktable_data_ptr(st->table, ts, data_type);
326 if (data_ptr) {
327 switch (stktable_data_types[data_type].std_type) {
328 case STD_T_SINT: {
329 int data;
330
331 data = stktable_data_cast(data_ptr, std_t_sint);
332 intencode(data, &cursor);
333 break;
334 }
335 case STD_T_UINT: {
336 unsigned int data;
337
338 data = stktable_data_cast(data_ptr, std_t_uint);
339 intencode(data, &cursor);
340 break;
341 }
342 case STD_T_ULL: {
343 unsigned long long data;
344
345 data = stktable_data_cast(data_ptr, std_t_ull);
346 intencode(data, &cursor);
347 break;
348 }
349 case STD_T_FRQP: {
350 struct freq_ctr_period *frqp;
351
352 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
353 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
354 intencode(frqp->curr_ctr, &cursor);
355 intencode(frqp->prev_ctr, &cursor);
356 break;
357 }
358 }
359 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200361 RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200362
363 /* Compute datalen */
364 datalen = (cursor - datamsg);
365
366 /* prepare message header */
367 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200368 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200369 cursor = &msg[2];
370 intencode(datalen, &cursor);
371
372 /* move data after header */
373 memmove(cursor, datamsg, datalen);
374
375 /* return header size + data_len */
376 return (cursor - msg) + datalen;
377}
378
379/*
380 * This prepare the switch table message to targeted share table <st>.
381 * <msg> is a buffer of <size> to recieve data message content
382 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
383 * check size)
384 */
385static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
386{
387 int len;
388 unsigned short datalen;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200389 struct chunk *chunk;
390 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200391 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200392 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200393
394 cursor = datamsg = msg + 2 + 5;
395
396 /* Encode data */
397
398 /* encode local id */
399 intencode(st->local_id, &cursor);
400
401 /* encode table name */
402 len = strlen(st->table->id);
403 intencode(len, &cursor);
404 memcpy(cursor, st->table->id, len);
405 cursor += len;
406
407 /* encode table type */
408
409 intencode(st->table->type, &cursor);
410
411 /* encode table key size */
412 intencode(st->table->key_size, &cursor);
413
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200414 chunk = get_trash_chunk();
415 chunkp = chunkq = chunk->str;
Emeric Brun94900952015-06-11 18:25:54 +0200416 /* encode available known data types in table */
417 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
418 if (st->table->data_ofs[data_type]) {
419 switch (stktable_data_types[data_type].std_type) {
420 case STD_T_SINT:
421 case STD_T_UINT:
422 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200423 data |= 1 << data_type;
424 break;
Emeric Brun94900952015-06-11 18:25:54 +0200425 case STD_T_FRQP:
426 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200427 intencode(data_type, &chunkq);
428 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200429 break;
430 }
431 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200432 }
433 intencode(data, &cursor);
434
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200435 /* Encode stick-table entries duration. */
436 intencode(st->table->expire, &cursor);
437
438 if (chunkq > chunkp) {
439 chunk->len = chunkq - chunkp;
440 memcpy(cursor, chunk->str, chunk->len);
441 cursor += chunk->len;
442 }
443
Emeric Brunb3971ab2015-05-12 18:49:09 +0200444 /* Compute datalen */
445 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200446
Emeric Brunb3971ab2015-05-12 18:49:09 +0200447 /* prepare message header */
448 msg[0] = PEER_MSG_CLASS_STICKTABLE;
449 msg[1] = PEER_MSG_STKT_DEFINE;
450 cursor = &msg[2];
451 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200452
Emeric Brunb3971ab2015-05-12 18:49:09 +0200453 /* move data after header */
454 memmove(cursor, datamsg, datalen);
455
456 /* return header size + data_len */
457 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200458}
459
Emeric Brunb3971ab2015-05-12 18:49:09 +0200460/*
461 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
462 * stick table.
463 * <msg> is a buffer of <size> to recieve data message content
464 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
465 * check size)
466 */
467static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
468{
469 unsigned short datalen;
470 char *cursor, *datamsg;
471 uint32_t netinteger;
472
Emeric Brunb058f1c2015-09-22 15:50:18 +0200473 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474
475 intencode(st->remote_id, &cursor);
476 netinteger = htonl(st->last_get);
477 memcpy(cursor, &netinteger, sizeof(netinteger));
478 cursor += sizeof(netinteger);
479
480 /* Compute datalen */
481 datalen = (cursor - datamsg);
482
483 /* prepare message header */
484 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200485 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200486 cursor = &msg[2];
487 intencode(datalen, &cursor);
488
489 /* move data after header */
490 memmove(cursor, datamsg, datalen);
491
492 /* return header size + data_len */
493 return (cursor - msg) + datalen;
494}
Emeric Brun2b920a12010-09-23 18:30:22 +0200495
496/*
497 * Callback to release a session with a peer
498 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200500{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200501 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200502 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200503 struct peer *peer = appctx->ctx.peers.ptr;
504 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200505
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100506 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100507 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200508 return;
509
510 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200511 if (peer) {
Willy Tarreau9df94c22016-10-31 18:42:52 +0100512 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200513 /* Re-init current table pointers to force announcement on re-connect */
514 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200515 peer->appctx = NULL;
516 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200517 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200518 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
519 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200520
521 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200523 }
524 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200525 peer->flags &= PEER_TEACH_RESET;
526 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200527 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200528 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200529 }
530}
531
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200532/* Retrieve the major and minor versions of peers protocol
533 * announced by a remote peer. <str> is a null-terminated
534 * string with the following format: "<maj_ver>.<min_ver>".
535 */
536static int peer_get_version(const char *str,
537 unsigned int *maj_ver, unsigned int *min_ver)
538{
539 unsigned int majv, minv;
540 const char *pos, *saved;
541 const char *end;
542
543 saved = pos = str;
544 end = str + strlen(str);
545
546 majv = read_uint(&pos, end);
547 if (saved == pos || *pos++ != '.')
548 return -1;
549
550 saved = pos;
551 minv = read_uint(&pos, end);
552 if (saved == pos || pos != end)
553 return -1;
554
555 *maj_ver = majv;
556 *min_ver = minv;
557
558 return 0;
559}
Emeric Brun2b920a12010-09-23 18:30:22 +0200560
561/*
562 * IO Handler to handle message exchance with a peer
563 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200564static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200565{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200566 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200567 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200568 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200569 int reql = 0;
570 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200571 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
572 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200573
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100574 /* Check if the input buffer is avalaible. */
575 if (si_ic(si)->buf->size == 0)
576 goto full;
577
Emeric Brun2b920a12010-09-23 18:30:22 +0200578 while (1) {
579switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200580 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100581 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100582 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100583 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100584 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200585 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100586 case PEER_SESS_ST_GETVERSION:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200587 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200588 if (reql <= 0) { /* closed or EOL not found */
589 if (reql == 0)
590 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100591 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200592 goto switchstate;
593 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100594 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100595 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200596 goto switchstate;
597 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100598 else if (reql > 1 && (trash.str[reql-2] == '\r'))
599 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200600 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100601 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602
Willy Tarreau06d80a92017-10-19 14:32:15 +0200603 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200604
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200605 /* test protocol */
606 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
607 appctx->st0 = PEER_SESS_ST_EXIT;
608 appctx->st1 = PEER_SESS_SC_ERRPROTO;
609 goto switchstate;
610 }
611 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
612 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100613 appctx->st0 = PEER_SESS_ST_EXIT;
614 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200615 goto switchstate;
616 }
617
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100618 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200619 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100620 case PEER_SESS_ST_GETHOST:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200621 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 if (reql <= 0) { /* closed or EOL not found */
623 if (reql == 0)
624 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100625 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200626 goto switchstate;
627 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100628 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100629 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200630 goto switchstate;
631 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100632 else if (reql > 1 && (trash.str[reql-2] == '\r'))
633 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200634 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100635 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200636
Willy Tarreau06d80a92017-10-19 14:32:15 +0200637 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200638
639 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100640 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100641 appctx->st0 = PEER_SESS_ST_EXIT;
642 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200643 goto switchstate;
644 }
645
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100646 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100648 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200649 struct peer *curpeer;
650 char *p;
Willy Tarreau06d80a92017-10-19 14:32:15 +0200651 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 if (reql <= 0) { /* closed or EOL not found */
653 if (reql == 0)
654 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100655 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200656 goto switchstate;
657 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100658 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200659 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100660 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200661 goto switchstate;
662 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100663 else if (reql > 1 && (trash.str[reql-2] == '\r'))
664 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200665 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100666 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200667
Willy Tarreau06d80a92017-10-19 14:32:15 +0200668 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200669
Emeric Brunb3971ab2015-05-12 18:49:09 +0200670 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100671 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200672 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100673 appctx->st0 = PEER_SESS_ST_EXIT;
674 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 goto switchstate;
676 }
677 *p = 0;
678
679 /* lookup known peer */
680 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100681 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200682 break;
683 }
684
685 /* if unknown peer */
686 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100687 appctx->st0 = PEER_SESS_ST_EXIT;
688 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200689 goto switchstate;
690 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200691
Willy Tarreau9df94c22016-10-31 18:42:52 +0100692 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200693 if (curpeer->local) {
694 /* Local connection, reply a retry */
695 appctx->st0 = PEER_SESS_ST_EXIT;
696 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
697 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 }
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100699 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200700 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200701 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
702 if (min_ver == PEER_DWNGRD_MINOR_VER) {
703 curpeer->flags |= PEER_F_DWNGRD;
704 }
705 else {
706 curpeer->flags &= ~PEER_F_DWNGRD;
707 }
708 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200709 curpeer->appctx = appctx;
710 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100711 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200712 /* fall through */
713 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100714 case PEER_SESS_ST_SENDSUCCESS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200715 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200716 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200717
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100718 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau06d80a92017-10-19 14:32:15 +0200719 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200720 if (repl <= 0) {
721 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100722 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100723 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200724 goto switchstate;
725 }
726
727 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200729
730 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200731 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200732
733 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200734 curpeer->confirm = 0;
735
736 /* Init cursors */
737 for (st = curpeer->tables; st ; st = st->next) {
738 st->last_get = st->last_acked = 0;
739 st->teaching_origin = st->last_pushed = st->update;
740 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200741
742 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200743 curpeer->flags &= PEER_TEACH_RESET;
744 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200745
746 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200747 if (curpeer->local) {
748 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200749 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
750 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200751 /* assign local peer for a lesson, consider lesson already requested */
752 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200753 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200754 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200755
Emeric Brunb3971ab2015-05-12 18:49:09 +0200756 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200757 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
758 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200759 /* assign peer for a lesson */
760 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200761 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200762 }
763
764
Emeric Brun2b920a12010-09-23 18:30:22 +0200765 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100766 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200767 goto switchstate;
768 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100769 case PEER_SESS_ST_CONNECT: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200770 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200771
772 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100773 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200774 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
775 PEER_MAJOR_VER,
776 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200777 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200778 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100779 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200780 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200781
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100782 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100783 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200784 goto switchstate;
785 }
786
Willy Tarreau06d80a92017-10-19 14:32:15 +0200787 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200788 if (repl <= 0) {
789 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100790 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100791 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200792 goto switchstate;
793 }
794
795 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100796 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200797 /* fall through */
798 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100799 case PEER_SESS_ST_GETSTATUS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200800 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200802
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100803 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200804 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200805
Willy Tarreau06d80a92017-10-19 14:32:15 +0200806 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200807 if (reql <= 0) { /* closed or EOL not found */
808 if (reql == 0)
809 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100810 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200811 goto switchstate;
812 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100813 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200814 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100815 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200816 goto switchstate;
817 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 else if (reql > 1 && (trash.str[reql-2] == '\r'))
819 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200820 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100821 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200822
Willy Tarreau06d80a92017-10-19 14:32:15 +0200823 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200824
825 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200826 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200827
828 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200829 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200830
831 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200832 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200833 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200834 for (st = curpeer->tables; st ; st = st->next) {
835 st->last_get = st->last_acked = 0;
836 st->teaching_origin = st->last_pushed = st->update;
837 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200838
839 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200840 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200841
Emeric Brunb3971ab2015-05-12 18:49:09 +0200842 /* reset teaching and learning flags to 0 */
843 curpeer->flags &= PEER_TEACH_RESET;
844 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200845
Emeric Brunb3971ab2015-05-12 18:49:09 +0200846 /* If current peer is local */
847 if (curpeer->local) {
848 /* flag to start to teach lesson */
849 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200850
Emeric Brunb3971ab2015-05-12 18:49:09 +0200851 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200852 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
853 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200854 /* If peer is remote and resync from remote is needed,
855 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200856
Emeric Brunb3971ab2015-05-12 18:49:09 +0200857 /* assign peer for a lesson */
858 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200859 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200860 }
861
862 }
863 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200864 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
865 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200866 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100867 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200868 goto switchstate;
869 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100870 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200871 /* fall through */
872 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100873 case PEER_SESS_ST_WAITMSG: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200874 struct peer *curpeer = appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200875 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200876 uint32_t msg_len = 0;
877 char *msg_cur = trash.str;
878 char *msg_end = trash.str;
879 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200880 int totl = 0;
881
Willy Tarreau06d80a92017-10-19 14:32:15 +0200882 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200883 if (reql <= 0) /* closed or EOL not found */
884 goto incomplete;
885
Emeric Brun2b920a12010-09-23 18:30:22 +0200886 totl += reql;
887
Emeric Brunb3971ab2015-05-12 18:49:09 +0200888 if (msg_head[1] >= 128) {
889 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200890 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200891 if (reql <= 0) /* closed */
892 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200893
Emeric Brunb3971ab2015-05-12 18:49:09 +0200894 totl += reql;
895
896 if (msg_head[2] < 240) {
897 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200898 }
899 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200900 int i;
901 char *cur;
902 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200903
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200905 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200906 if (reql <= 0) /* closed */
907 goto incomplete;
908
909 totl += reql;
910
911 if (!(msg_head[i] & 0x80))
912 break;
913 }
914
915 if (i == sizeof(msg_head)) {
916 /* malformed message */
917 appctx->st0 = PEER_SESS_ST_ERRPROTO;
918 goto switchstate;
919
920 }
921 end = (char *)msg_head + sizeof(msg_head);
922 cur = (char *)&msg_head[2];
923 msg_len = intdecode(&cur, end);
924 if (!cur) {
925 /* malformed message */
926 appctx->st0 = PEER_SESS_ST_ERRPROTO;
927 goto switchstate;
928 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200929 }
930
Willy Tarreau86a446e2013-11-25 23:02:37 +0100931
Emeric Brunb3971ab2015-05-12 18:49:09 +0200932 /* Read message content */
933 if (msg_len) {
934 if (msg_len > trash.size) {
935 /* Status code is not success, abort */
936 appctx->st0 = PEER_SESS_ST_ERRSIZE;
937 goto switchstate;
938 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200939
Willy Tarreau06d80a92017-10-19 14:32:15 +0200940 reql = co_getblk(si_oc(si), trash.str, msg_len, totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200941 if (reql <= 0) /* closed */
942 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200943 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100944
Emeric Brunb3971ab2015-05-12 18:49:09 +0200945 msg_end += msg_len;
946 }
947 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100948
Emeric Brunb3971ab2015-05-12 18:49:09 +0200949 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
950 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
951 struct shared_table *st;
952 /* Reset message: remote need resync */
953
954 /* prepare tables fot a global push */
955 for (st = curpeer->tables; st; st = st->next) {
956 st->teaching_origin = st->last_pushed = st->table->update;
957 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100958 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200959
Emeric Brunb3971ab2015-05-12 18:49:09 +0200960 /* reset teaching flags to 0 */
961 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200962
Emeric Brunb3971ab2015-05-12 18:49:09 +0200963 /* flag to start to teach lesson */
964 curpeer->flags |= PEER_F_TEACH_PROCESS;
965
966
967 }
968 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
969
970 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
971 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200972 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
973 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100974 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200975 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200976 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200977 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
978
979 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
980 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200981 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200982
983 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200984 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
985 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100986 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200988 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +0200990 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200991
992 /* If stopping state */
993 if (stopping) {
994 /* Close session, push resync no more needed */
995 curpeer->flags |= PEER_F_TEACH_COMPLETE;
996 appctx->st0 = PEER_SESS_ST_END;
997 goto switchstate;
998 }
Emeric Brun597b26e2016-08-12 11:23:31 +0200999 for (st = curpeer->tables; st; st = st->next) {
1000 st->update = st->last_pushed = st->teaching_origin;
1001 st->flags = 0;
1002 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001003
1004 /* reset teaching flags to 0 */
1005 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001006 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001007 }
1008 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1009 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1010 int table_id_len;
1011 struct shared_table *st;
1012 int table_type;
1013 int table_keylen;
1014 int table_id;
1015 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001016
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017 table_id = intdecode(&msg_cur, msg_end);
1018 if (!msg_cur) {
1019 /* malformed message */
1020 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1021 goto switchstate;
1022 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001023
Emeric Brunb3971ab2015-05-12 18:49:09 +02001024 table_id_len = intdecode(&msg_cur, msg_end);
1025 if (!msg_cur) {
1026 /* malformed message */
1027 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1028 goto switchstate;
1029 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001030
Emeric Brunb3971ab2015-05-12 18:49:09 +02001031 curpeer->remote_table = NULL;
1032 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1033 /* malformed message */
1034 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1035 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001036 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001037
Emeric Brunb3971ab2015-05-12 18:49:09 +02001038 for (st = curpeer->tables; st; st = st->next) {
1039 /* Reset IDs */
1040 if (st->remote_id == table_id)
1041 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001042
Emeric Brunb3971ab2015-05-12 18:49:09 +02001043 if (!curpeer->remote_table
1044 && (table_id_len == strlen(st->table->id))
1045 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1046 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001047 }
1048 }
1049
Emeric Brunb3971ab2015-05-12 18:49:09 +02001050 if (!curpeer->remote_table) {
1051 goto ignore_msg;
1052 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001053
Emeric Brunb3971ab2015-05-12 18:49:09 +02001054 msg_cur += table_id_len;
1055 if (msg_cur >= msg_end) {
1056 /* malformed message */
1057 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1058 goto switchstate;
1059 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001060
Emeric Brunb3971ab2015-05-12 18:49:09 +02001061 table_type = intdecode(&msg_cur, msg_end);
1062 if (!msg_cur) {
1063 /* malformed message */
1064 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1065 goto switchstate;
1066 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001067
Emeric Brunb3971ab2015-05-12 18:49:09 +02001068 table_keylen = intdecode(&msg_cur, msg_end);
1069 if (!msg_cur) {
1070 /* malformed message */
1071 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1072 goto switchstate;
1073 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001074
Emeric Brunb3971ab2015-05-12 18:49:09 +02001075 table_data = intdecode(&msg_cur, msg_end);
1076 if (!msg_cur) {
1077 /* malformed message */
1078 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1079 goto switchstate;
1080 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001081
Emeric Brunb3971ab2015-05-12 18:49:09 +02001082 if (curpeer->remote_table->table->type != table_type
1083 || curpeer->remote_table->table->key_size != table_keylen) {
1084 curpeer->remote_table = NULL;
1085 goto ignore_msg;
1086 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001087
Emeric Brunb3971ab2015-05-12 18:49:09 +02001088 curpeer->remote_table->remote_data = table_data;
1089 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001090 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001091 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1092 struct shared_table *st;
1093 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001094
Emeric Brunb3971ab2015-05-12 18:49:09 +02001095 table_id = intdecode(&msg_cur, msg_end);
1096 if (!msg_cur) {
1097 /* malformed message */
1098 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1099 goto switchstate;
1100 }
1101 curpeer->remote_table = NULL;
1102 for (st = curpeer->tables; st; st = st->next) {
1103 if (st->remote_id == table_id) {
1104 curpeer->remote_table = st;
1105 break;
1106 }
1107 }
1108
Emeric Brun2b920a12010-09-23 18:30:22 +02001109 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001110 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001111 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1112 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1113 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001114 struct shared_table *st = curpeer->remote_table;
1115 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001116 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001117 unsigned int data_type;
1118 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001119
Emeric Brunb3971ab2015-05-12 18:49:09 +02001120 /* Here we have data message */
1121 if (!st)
1122 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001123
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001124 expire = MS_TO_TICKS(st->table->expire);
1125
1126 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1127 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001128 if (msg_len < sizeof(update)) {
1129 /* malformed message */
1130 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1131 goto switchstate;
1132 }
1133 memcpy(&update, msg_cur, sizeof(update));
1134 msg_cur += sizeof(update);
1135 st->last_get = htonl(update);
1136 }
1137 else {
1138 st->last_get++;
1139 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001140
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001141 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1142 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1143 size_t expire_sz = sizeof expire;
1144
1145 if (msg_cur + expire_sz > msg_end) {
1146 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1147 goto switchstate;
1148 }
1149 memcpy(&expire, msg_cur, expire_sz);
1150 msg_cur += expire_sz;
1151 expire = ntohl(expire);
1152 }
1153
Emeric Brunb3971ab2015-05-12 18:49:09 +02001154 newts = stksess_new(st->table, NULL);
1155 if (!newts)
1156 goto ignore_msg;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001157 /* Force expiratiion to remote date
1158 in case of first insert */
1159 newts->expire = tick_add(now_ms, expire);
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001160 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001161 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001162
Emeric Brunb3971ab2015-05-12 18:49:09 +02001163 to_read = intdecode(&msg_cur, msg_end);
1164 if (!msg_cur) {
1165 /* malformed message */
1166 stksess_free(st->table, newts);
1167 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1168 goto switchstate;
1169 }
1170 to_store = MIN(to_read, st->table->key_size - 1);
1171 if (msg_cur + to_store > msg_end) {
1172 /* malformed message */
1173 stksess_free(st->table, newts);
1174 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1175 goto switchstate;
1176 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001177
Emeric Brunb3971ab2015-05-12 18:49:09 +02001178 memcpy(newts->key.key, msg_cur, to_store);
1179 newts->key.key[to_store] = 0;
1180 msg_cur += to_read;
1181 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001182 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001183 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001184
Emeric Brunb3971ab2015-05-12 18:49:09 +02001185 if (msg_cur + sizeof(netinteger) > msg_end) {
1186 /* malformed message */
1187 stksess_free(st->table, newts);
1188 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1189 goto switchstate;
1190 }
1191 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1192 netinteger = ntohl(netinteger);
1193 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1194 msg_cur += sizeof(netinteger);
1195 }
1196 else {
1197 if (msg_cur + st->table->key_size > msg_end) {
1198 /* malformed message */
1199 stksess_free(st->table, newts);
1200 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1201 goto switchstate;
1202 }
1203 memcpy(newts->key.key, msg_cur, st->table->key_size);
1204 msg_cur += st->table->key_size;
1205 }
1206
1207 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001208 ts = stktable_set_entry(st->table, newts);
1209 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001210 stksess_free(st->table, newts);
1211 newts = NULL;
1212 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001213
Emeric Brun819fc6f2017-06-13 19:37:32 +02001214 RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001215
Emeric Brun94900952015-06-11 18:25:54 +02001216 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001217
Emeric Brun94900952015-06-11 18:25:54 +02001218 if ((1 << data_type) & st->remote_data) {
1219 switch (stktable_data_types[data_type].std_type) {
1220 case STD_T_SINT: {
1221 int data;
1222
1223 data = intdecode(&msg_cur, msg_end);
1224 if (!msg_cur) {
1225 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001226 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1227 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001228 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1229 goto switchstate;
1230 }
1231
1232 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1233 if (data_ptr)
1234 stktable_data_cast(data_ptr, std_t_sint) = data;
1235 break;
1236 }
1237 case STD_T_UINT: {
1238 unsigned int data;
1239
1240 data = intdecode(&msg_cur, msg_end);
1241 if (!msg_cur) {
1242 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001243 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1244 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001245 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1246 goto switchstate;
1247 }
1248
1249 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1250 if (data_ptr)
1251 stktable_data_cast(data_ptr, std_t_uint) = data;
1252 break;
1253 }
1254 case STD_T_ULL: {
1255 unsigned long long data;
1256
1257 data = intdecode(&msg_cur, msg_end);
1258 if (!msg_cur) {
1259 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001260 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1261 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001262 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1263 goto switchstate;
1264 }
1265
1266 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1267 if (data_ptr)
1268 stktable_data_cast(data_ptr, std_t_ull) = data;
1269 break;
1270 }
1271 case STD_T_FRQP: {
1272 struct freq_ctr_period data;
1273
Willy Tarreau3bb46172016-03-25 18:17:47 +01001274 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001275 if (!msg_cur) {
1276 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001277 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1278 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001279 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1280 goto switchstate;
1281 }
1282 data.curr_ctr = intdecode(&msg_cur, msg_end);
1283 if (!msg_cur) {
1284 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001285 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1286 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001287 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 */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001293 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1294 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001295 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1296 goto switchstate;
1297 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001298
Emeric Brun94900952015-06-11 18:25:54 +02001299 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1300 if (data_ptr)
1301 stktable_data_cast(data_ptr, std_t_frqp) = data;
1302 break;
1303 }
1304 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001305 }
1306 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001307
1308 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1309 stktable_touch_remote(st->table, ts, 1);
1310
Emeric Brunb3971ab2015-05-12 18:49:09 +02001311 }
1312 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1313 /* ack message */
1314 uint32_t table_id ;
1315 uint32_t update;
1316 struct shared_table *st;
1317
1318 table_id = intdecode(&msg_cur, msg_end);
1319 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1320 /* malformed message */
1321 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1322 goto switchstate;
1323 }
1324 memcpy(&update, msg_cur, sizeof(update));
1325 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001326
Emeric Brunb3971ab2015-05-12 18:49:09 +02001327 for (st = curpeer->tables; st; st = st->next) {
1328 if (st->local_id == table_id) {
1329 st->update = update;
1330 break;
1331 }
1332 }
1333 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001334 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001335 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1336 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001337 goto switchstate;
1338 }
1339
Emeric Brunb3971ab2015-05-12 18:49:09 +02001340ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001341 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001342 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001343 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001344 goto switchstate;
1345
Emeric Brun2b920a12010-09-23 18:30:22 +02001346incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001347 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001348
Willy Tarreau72d6c162013-04-11 16:14:13 +02001349 if (reql < 0) {
1350 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001351 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001352 goto switchstate;
1353 }
1354
Emeric Brun2b920a12010-09-23 18:30:22 +02001355
Emeric Brun2b920a12010-09-23 18:30:22 +02001356
Emeric Brunb3971ab2015-05-12 18:49:09 +02001357
Emeric Brun2b920a12010-09-23 18:30:22 +02001358 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001359 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001360 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1361 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001362 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001363
Emeric Brunb3971ab2015-05-12 18:49:09 +02001364 /* Current peer was elected to request a resync */
1365 msg[0] = PEER_MSG_CLASS_CONTROL;
1366 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001367
Emeric Brunb3971ab2015-05-12 18:49:09 +02001368 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001369 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001370 if (repl <= 0) {
1371 /* no more write possible */
1372 if (repl == -1)
1373 goto full;
1374 appctx->st0 = PEER_SESS_ST_END;
1375 goto switchstate;
1376 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001377 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001378 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001379
Emeric Brunb3971ab2015-05-12 18:49:09 +02001380 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001381
Emeric Brunb3971ab2015-05-12 18:49:09 +02001382 if (curpeer->tables) {
1383 struct shared_table *st;
1384 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001385
Emeric Brunb3971ab2015-05-12 18:49:09 +02001386 last_local_table = curpeer->last_local_table;
1387 if (!last_local_table)
1388 last_local_table = curpeer->tables;
1389 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001390
Emeric Brunb3971ab2015-05-12 18:49:09 +02001391 while (1) {
1392 if (!st)
1393 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001394
Emeric Brunb3971ab2015-05-12 18:49:09 +02001395 /* It remains some updates to ack */
1396 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001397 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001398
Emeric Brunb3971ab2015-05-12 18:49:09 +02001399 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1400 if (!msglen) {
1401 /* internal error: message does not fit in trash */
1402 appctx->st0 = PEER_SESS_ST_END;
1403 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001404 }
1405
Emeric Brunb3971ab2015-05-12 18:49:09 +02001406 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001407 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001408 if (repl <= 0) {
1409 /* no more write possible */
1410 if (repl == -1) {
1411 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001412 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001413 appctx->st0 = PEER_SESS_ST_END;
1414 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001415 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001416 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001417 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001418
Emeric Brunb3971ab2015-05-12 18:49:09 +02001419 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1420 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1421 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1422 struct eb32_node *eb;
1423 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001424
Emeric Brunb3971ab2015-05-12 18:49:09 +02001425 if (st != curpeer->last_local_table) {
1426 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001427
Emeric Brunb3971ab2015-05-12 18:49:09 +02001428 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1429 if (!msglen) {
1430 /* internal error: message does not fit in trash */
1431 appctx->st0 = PEER_SESS_ST_END;
1432 goto switchstate;
1433 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001434
Emeric Brunb3971ab2015-05-12 18:49:09 +02001435 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001436 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001437 if (repl <= 0) {
1438 /* no more write possible */
1439 if (repl == -1) {
1440 goto full;
1441 }
1442 appctx->st0 = PEER_SESS_ST_END;
1443 goto switchstate;
1444 }
1445 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001446 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001447
1448 /* We force new pushed to 1 to force identifier in update message */
1449 new_pushed = 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001450 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001451 while (1) {
1452 uint32_t msglen;
1453 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001454 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001455
1456 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001457 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001458 if (!eb) {
1459 eb = eb32_first(&st->table->updates);
1460 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001461 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001462 break;
1463 }
1464 }
1465
1466 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001467 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001468 break;
1469 }
1470
1471 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001472 updateid = ts->upd.key;
1473 ts->ref_cnt++;
1474 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1475
1476 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001477 if (!msglen) {
1478 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001479 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1480 ts->ref_cnt--;
1481 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001482 appctx->st0 = PEER_SESS_ST_END;
1483 goto switchstate;
1484 }
1485
1486 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001487 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001488 if (repl <= 0) {
1489 /* no more write possible */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001490 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1491 ts->ref_cnt--;
1492 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001493 if (repl == -1) {
1494 goto full;
1495 }
1496 appctx->st0 = PEER_SESS_ST_END;
1497 goto switchstate;
1498 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001499
1500 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1501 ts->ref_cnt--;
1502 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001503 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1504 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 /* identifier may not needed in next update message */
1506 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001507 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001508 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001509 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001510 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001511 else {
1512 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1513 struct eb32_node *eb;
1514 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001515
Emeric Brunb3971ab2015-05-12 18:49:09 +02001516 if (st != curpeer->last_local_table) {
1517 int msglen;
1518
1519 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1520 if (!msglen) {
1521 /* internal error: message does not fit in trash */
1522 appctx->st0 = PEER_SESS_ST_END;
1523 goto switchstate;
1524 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001525
Emeric Brunb3971ab2015-05-12 18:49:09 +02001526 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001527 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001528 if (repl <= 0) {
1529 /* no more write possible */
1530 if (repl == -1) {
1531 goto full;
1532 }
1533 appctx->st0 = PEER_SESS_ST_END;
1534 goto switchstate;
1535 }
1536 curpeer->last_local_table = st;
1537 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001538
Emeric Brunb3971ab2015-05-12 18:49:09 +02001539 /* We force new pushed to 1 to force identifier in update message */
1540 new_pushed = 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001541 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001542 while (1) {
1543 uint32_t msglen;
1544 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001545 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001546 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001547
Emeric Brunb3971ab2015-05-12 18:49:09 +02001548 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001549 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001550 if (!eb) {
1551 st->flags |= SHTABLE_F_TEACH_STAGE1;
1552 eb = eb32_first(&st->table->updates);
1553 if (eb)
1554 st->last_pushed = eb->key - 1;
1555 break;
1556 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001557
Emeric Brunb3971ab2015-05-12 18:49:09 +02001558 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001559 updateid = ts->upd.key;
1560 ts->ref_cnt++;
1561 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1562
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001563 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001564 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001565 if (!msglen) {
1566 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001567 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1568 ts->ref_cnt--;
1569 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001570 appctx->st0 = PEER_SESS_ST_END;
1571 goto switchstate;
1572 }
1573
1574 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001575 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001576 if (repl <= 0) {
1577 /* no more write possible */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001578 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1579 ts->ref_cnt--;
1580 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001581 if (repl == -1) {
1582 goto full;
1583 }
1584 appctx->st0 = PEER_SESS_ST_END;
1585 goto switchstate;
1586 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001587 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1588 ts->ref_cnt--;
1589 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 /* identifier may not needed in next update message */
1591 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001592 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001593 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001594 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001595
Emeric Brunb3971ab2015-05-12 18:49:09 +02001596 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1597 struct eb32_node *eb;
1598 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001599
Emeric Brunb3971ab2015-05-12 18:49:09 +02001600 if (st != curpeer->last_local_table) {
1601 int msglen;
1602
1603 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1604 if (!msglen) {
1605 /* internal error: message does not fit in trash */
1606 appctx->st0 = PEER_SESS_ST_END;
1607 goto switchstate;
1608 }
1609
1610 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001611 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001612 if (repl <= 0) {
1613 /* no more write possible */
1614 if (repl == -1) {
1615 goto full;
1616 }
1617 appctx->st0 = PEER_SESS_ST_END;
1618 goto switchstate;
1619 }
1620 curpeer->last_local_table = st;
1621 }
1622
1623 /* We force new pushed to 1 to force identifier in update message */
1624 new_pushed = 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001625 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001626 while (1) {
1627 uint32_t msglen;
1628 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001629 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001630 unsigned updateid;
1631
1632 /* push local updates */
1633 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001634
1635 /* push local updates */
1636 if (!eb || eb->key > st->teaching_origin) {
1637 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001638 break;
1639 }
1640
1641 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001642 updateid = ts->upd.key;
1643 ts->ref_cnt++;
1644 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1645
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001646 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001647 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001648 if (!msglen) {
1649 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001650 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1651 ts->ref_cnt--;
1652 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001653 appctx->st0 = PEER_SESS_ST_END;
1654 goto switchstate;
1655 }
1656
1657 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001658 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001659 if (repl <= 0) {
1660 /* no more write possible */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001661 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1662 ts->ref_cnt--;
1663 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001664 if (repl == -1) {
1665 goto full;
1666 }
1667 appctx->st0 = PEER_SESS_ST_END;
1668 goto switchstate;
1669 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001670
1671 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1672 ts->ref_cnt--;
1673 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001674 /* identifier may not needed in next update message */
1675 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001676 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001677 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001678 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001679 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001680
1681 if (st == last_local_table)
1682 break;
1683 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001684 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001685 }
1686
1687
1688 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1689 unsigned char msg[2];
1690
1691 /* Current peer was elected to request a resync */
1692 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001693 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 +02001694 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001695 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001696 if (repl <= 0) {
1697 /* no more write possible */
1698 if (repl == -1)
1699 goto full;
1700 appctx->st0 = PEER_SESS_ST_END;
1701 goto switchstate;
1702 }
1703 /* flag finished message sent */
1704 curpeer->flags |= PEER_F_TEACH_FINISHED;
1705 }
1706
Emeric Brun597b26e2016-08-12 11:23:31 +02001707 /* Confirm finished or partial messages */
1708 while (curpeer->confirm) {
1709 unsigned char msg[2];
1710
1711 /* There is a confirm messages to send */
1712 msg[0] = PEER_MSG_CLASS_CONTROL;
1713 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1714
1715 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001716 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001717 if (repl <= 0) {
1718 /* no more write possible */
1719 if (repl == -1)
1720 goto full;
1721 appctx->st0 = PEER_SESS_ST_END;
1722 goto switchstate;
1723 }
1724 curpeer->confirm--;
1725 }
1726
Emeric Brun2b920a12010-09-23 18:30:22 +02001727 /* noting more to do */
1728 goto out;
1729 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001730 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001731 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001732 if (ci_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001733 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001734 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001735 goto switchstate;
1736 case PEER_SESS_ST_ERRSIZE: {
1737 unsigned char msg[2];
1738
1739 msg[0] = PEER_MSG_CLASS_ERROR;
1740 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1741
Willy Tarreau06d80a92017-10-19 14:32:15 +02001742 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001743 goto full;
1744 appctx->st0 = PEER_SESS_ST_END;
1745 goto switchstate;
1746 }
1747 case PEER_SESS_ST_ERRPROTO: {
1748 unsigned char msg[2];
1749
1750 msg[0] = PEER_MSG_CLASS_ERROR;
1751 msg[1] = PEER_MSG_ERR_PROTOCOL;
1752
Willy Tarreau06d80a92017-10-19 14:32:15 +02001753 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001754 goto full;
1755 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001756 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001757 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001758 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001759 si_shutw(si);
1760 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001761 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001762 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001763 }
1764 }
1765 }
1766out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001767 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001768 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001769full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001770 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001771 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001772}
1773
Willy Tarreau30576452015-04-13 13:50:30 +02001774static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001775 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001776 .name = "<PEER>", /* used for logging */
1777 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001778 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001779};
Emeric Brun2b920a12010-09-23 18:30:22 +02001780
1781/*
1782 * Use this function to force a close of a peer session
1783 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001784static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001785{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001786 struct peer *ps;
1787
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001788 /* Note that the peer sessions which have just been created
1789 * (->st0 == PEER_SESS_ST_CONNECT) must not
1790 * be shutdown, if not, the TCP session will never be closed
1791 * and stay in CLOSE_WAIT state after having been closed by
1792 * the remote side.
1793 */
1794 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001795 return;
1796
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001797 if (appctx->applet != &peer_applet)
1798 return;
1799
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001800 ps = appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001801 /* we're killing a connection, we must apply a random delay before
1802 * retrying otherwise the other end will do the same and we can loop
1803 * for a while.
1804 */
1805 if (ps)
1806 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1807
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001808 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001809 appctx->ctx.peers.ptr = NULL;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001810 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001811}
1812
Willy Tarreau91d96282015-03-13 15:47:26 +01001813/* Pre-configures a peers frontend to accept incoming connections */
1814void peers_setup_frontend(struct proxy *fe)
1815{
1816 fe->last_change = now.tv_sec;
1817 fe->cap = PR_CAP_FE;
1818 fe->maxconn = 0;
1819 fe->conn_retries = CONN_RETRIES;
1820 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001821 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001822 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001823 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001824 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001825}
1826
Emeric Brun2b920a12010-09-23 18:30:22 +02001827/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001828 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001829 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001830static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001831{
Willy Tarreau04b92862017-09-15 11:01:04 +02001832 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001833 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001834 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001835 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001836 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001837
Emeric Brunb3971ab2015-05-12 18:49:09 +02001838 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1839 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001840 s = NULL;
1841
Emeric Brun1138fd02017-06-19 12:38:55 +02001842 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001843 if (!appctx)
1844 goto out_close;
1845
1846 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001847 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001848
Willy Tarreau04b92862017-09-15 11:01:04 +02001849 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001850 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001851 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001852 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001853 }
1854
Willy Tarreau87787ac2017-08-28 16:22:54 +02001855 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001856 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001857 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001858 }
1859
Willy Tarreau342bfb12015-04-05 01:35:34 +02001860 /* The tasks below are normally what is supposed to be done by
1861 * fe->accept().
1862 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001863 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001864
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001865 /* applet is waiting for data */
1866 si_applet_cant_get(&s->si[0]);
1867 appctx_wakeup(appctx);
1868
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001869 /* initiate an outgoing connection */
1870 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001871
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001872 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001873 * pre-initialized connection in si->conn.
1874 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001875 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001876 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001877
1878 conn_prepare(conn, peer->proto, peer->xprt);
1879 si_attach_conn(&s->si[1], conn);
1880
1881 conn->target = s->target = &s->be->obj_type;
1882 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001883 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001884 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001885
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001886 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001887
Emeric Brunb3971ab2015-05-12 18:49:09 +02001888 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001889 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001890 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001891
1892 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001893 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001894 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001895 pool_free2(pool2_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001896 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001897 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001898 out_free_appctx:
1899 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001900 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001901 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001902}
1903
1904/*
1905 * Task processing function to manage re-connect and peer session
1906 * tasks wakeup on local update.
1907 */
Simon Horman96553772011-06-08 09:18:51 +09001908static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001909{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001910 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001911 struct peer *ps;
1912 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001913
1914 task->expire = TICK_ETERNITY;
1915
Emeric Brunb3971ab2015-05-12 18:49:09 +02001916 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001917 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001918 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001919 task_delete(peers->sync_task);
1920 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001921 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001922 return NULL;
1923 }
1924
Emeric Brun2b920a12010-09-23 18:30:22 +02001925 if (!stopping) {
1926 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001927
1928 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1929 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1930 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001931 /* Resync from local peer needed
1932 no peer was assigned for the lesson
1933 and no old local peer found
1934 or resync timeout expire */
1935
1936 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001937 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001938
1939 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001940 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001941 }
1942
1943 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001944 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001945 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001946 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001947 if (!ps->appctx) {
1948 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001949 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001950 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001951 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001952 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001953 tick_is_expired(ps->reconnect, now_ms))) {
1954 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001955 * or previous peer connection established with success
1956 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02001957 * and reconnection timer is expired */
1958
1959 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001960 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001961 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001962 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001963 /* If previous session failed during connection
1964 * but reconnection timer is not expired */
1965
1966 /* reschedule task for reconnect */
1967 task->expire = tick_first(task->expire, ps->reconnect);
1968 }
1969 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001970 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001971 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001972 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001973 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1974 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001975 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1976 /* Resync from a remote is needed
1977 * and no peer was assigned for lesson
1978 * and current peer may be up2date */
1979
1980 /* assign peer for the lesson */
1981 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001982 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001983
Willy Tarreau9df94c22016-10-31 18:42:52 +01001984 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001985 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001986 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001987 else {
1988 /* Awake session if there is data to push */
1989 for (st = ps->tables; st ; st = st->next) {
1990 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001991 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001992 appctx_wakeup(ps->appctx);
1993 break;
1994 }
1995 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001996 }
1997 /* else do nothing */
1998 } /* SUCCESSCODE */
1999 } /* !ps->peer->local */
2000 } /* for */
2001
2002 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002003 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2004 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2005 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002006 /* Resync from remote peer needed
2007 * no peer was assigned for the lesson
2008 * and resync timeout expire */
2009
2010 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002011 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002012 }
2013
Emeric Brunb3971ab2015-05-12 18:49:09 +02002014 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002015 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002016 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2017 if (!tick_is_expired(peers->resync_timeout, now_ms))
2018 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002019 }
2020 } /* !stopping */
2021 else {
2022 /* soft stop case */
2023 if (task->state & TASK_WOKEN_SIGNAL) {
2024 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002025 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002027 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002028 peers->flags |= PEERS_F_DONOTSTOP;
2029 ps = peers->local;
2030 for (st = ps->tables; st ; st = st->next)
2031 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002032 }
2033
2034 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002035 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002036 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002037 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002038 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002039 }
2040 }
2041 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002042
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002044 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002045 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002046 /* resync of new process was complete, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002047 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002048 peers->flags &= ~PEERS_F_DONOTSTOP;
2049 for (st = ps->tables; st ; st = st->next)
2050 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002051 }
2052 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002053 else if (!ps->appctx) {
2054 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002055 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002056 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2057 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2058 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002059 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002060 * or previous peer connection was successfully established
2061 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002062 * or during previous connect, peer replies a try again statuscode */
2063
2064 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002065 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002066 }
2067 else {
2068 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002069 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002070 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002071 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002072 peers->flags &= ~PEERS_F_DONOTSTOP;
2073 for (st = ps->tables; st ; st = st->next)
2074 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002075 }
2076 }
2077 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002078 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002079 /* current peer connection is active and established
2080 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002081 for (st = ps->tables; st ; st = st->next) {
2082 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002083 appctx_wakeup(ps->appctx);
2084 break;
2085 }
2086 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002087 }
2088 } /* stopping */
2089 /* Wakeup for re-connect */
2090 return task;
2091}
2092
Emeric Brunb3971ab2015-05-12 18:49:09 +02002093
Emeric Brun2b920a12010-09-23 18:30:22 +02002094/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002095 *
2096 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002097void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002098{
Emeric Brun2b920a12010-09-23 18:30:22 +02002099 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002100 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002101
Emeric Brun2b920a12010-09-23 18:30:22 +02002102 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002103 peers->peers_fe->maxconn += 3;
2104 }
2105
Willy Tarreau4348fad2012-09-20 16:48:07 +02002106 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2107 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002108 peers->sync_task = task_new(MAX_THREADS_MASK);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002109 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110 peers->sync_task->context = (void *)peers;
2111 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2112 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2113}
2114
2115
2116
2117/*
2118 * Function used to register a table for sync on a group of peers
2119 *
2120 */
2121void peers_register_table(struct peers *peers, struct stktable *table)
2122{
2123 struct shared_table *st;
2124 struct peer * curpeer;
2125 int id = 0;
2126
2127 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002128 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002129 st->table = table;
2130 st->next = curpeer->tables;
2131 if (curpeer->tables)
2132 id = curpeer->tables->local_id;
2133 st->local_id = id + 1;
2134
2135 curpeer->tables = st;
2136 }
2137
2138 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002139}
2140