blob: 2ca08fe7c5079c339aff1a3ada0f0e44320b5bb4 [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) {
Emeric Brun80527f52017-06-19 17:46:37 +0200512 SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100513 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200514 /* Re-init current table pointers to force announcement on re-connect */
515 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 peer->appctx = NULL;
517 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200518 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
520 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200521
522 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200523 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200524 }
525 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 peer->flags &= PEER_TEACH_RESET;
527 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200528 }
Emeric Brun80527f52017-06-19 17:46:37 +0200529 SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200530 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200531 }
532}
533
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200534/* Retrieve the major and minor versions of peers protocol
535 * announced by a remote peer. <str> is a null-terminated
536 * string with the following format: "<maj_ver>.<min_ver>".
537 */
538static int peer_get_version(const char *str,
539 unsigned int *maj_ver, unsigned int *min_ver)
540{
541 unsigned int majv, minv;
542 const char *pos, *saved;
543 const char *end;
544
545 saved = pos = str;
546 end = str + strlen(str);
547
548 majv = read_uint(&pos, end);
549 if (saved == pos || *pos++ != '.')
550 return -1;
551
552 saved = pos;
553 minv = read_uint(&pos, end);
554 if (saved == pos || pos != end)
555 return -1;
556
557 *maj_ver = majv;
558 *min_ver = minv;
559
560 return 0;
561}
Emeric Brun2b920a12010-09-23 18:30:22 +0200562
563/*
564 * IO Handler to handle message exchance with a peer
565 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200566static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200567{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200568 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200569 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200570 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200571 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200572 int reql = 0;
573 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200574 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
575 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200576
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100577 /* Check if the input buffer is avalaible. */
578 if (si_ic(si)->buf->size == 0)
579 goto full;
580
Emeric Brun2b920a12010-09-23 18:30:22 +0200581 while (1) {
582switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200583 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100584 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100585 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100586 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100587 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200588 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100589 case PEER_SESS_ST_GETVERSION:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200590 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200591 if (reql <= 0) { /* closed or EOL not found */
592 if (reql == 0)
593 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100594 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200595 goto switchstate;
596 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100597 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100598 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200599 goto switchstate;
600 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100601 else if (reql > 1 && (trash.str[reql-2] == '\r'))
602 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200603 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200605
Willy Tarreau06d80a92017-10-19 14:32:15 +0200606 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200607
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200608 /* test protocol */
609 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
610 appctx->st0 = PEER_SESS_ST_EXIT;
611 appctx->st1 = PEER_SESS_SC_ERRPROTO;
612 goto switchstate;
613 }
614 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
615 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100616 appctx->st0 = PEER_SESS_ST_EXIT;
617 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200618 goto switchstate;
619 }
620
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100621 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100623 case PEER_SESS_ST_GETHOST:
Willy Tarreau06d80a92017-10-19 14:32:15 +0200624 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200625 if (reql <= 0) { /* closed or EOL not found */
626 if (reql == 0)
627 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100628 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200629 goto switchstate;
630 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100631 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100632 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200633 goto switchstate;
634 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100635 else if (reql > 1 && (trash.str[reql-2] == '\r'))
636 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200637 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100638 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200639
Willy Tarreau06d80a92017-10-19 14:32:15 +0200640 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200641
642 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100643 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100644 appctx->st0 = PEER_SESS_ST_EXIT;
645 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200646 goto switchstate;
647 }
648
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100649 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200650 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100651 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 char *p;
Willy Tarreau06d80a92017-10-19 14:32:15 +0200653 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200654 if (reql <= 0) { /* closed or EOL not found */
655 if (reql == 0)
656 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100657 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200658 goto switchstate;
659 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100660 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200661 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100662 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200663 goto switchstate;
664 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 else if (reql > 1 && (trash.str[reql-2] == '\r'))
666 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200667 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200669
Willy Tarreau06d80a92017-10-19 14:32:15 +0200670 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200671
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100673 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200674 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100675 appctx->st0 = PEER_SESS_ST_EXIT;
676 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200677 goto switchstate;
678 }
679 *p = 0;
680
681 /* lookup known peer */
682 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100683 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200684 break;
685 }
686
687 /* if unknown peer */
688 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100689 appctx->st0 = PEER_SESS_ST_EXIT;
690 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200691 goto switchstate;
692 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200693
Emeric Brun80527f52017-06-19 17:46:37 +0200694 SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100695 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200696 if (curpeer->local) {
697 /* Local connection, reply a retry */
698 appctx->st0 = PEER_SESS_ST_EXIT;
699 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
700 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200701 }
Emeric Brun80527f52017-06-19 17:46:37 +0200702
703 /* we're killing a connection, we must apply a random delay before
704 * retrying otherwise the other end will do the same and we can loop
705 * for a while.
706 */
707 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100708 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200709 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200710 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
711 if (min_ver == PEER_DWNGRD_MINOR_VER) {
712 curpeer->flags |= PEER_F_DWNGRD;
713 }
714 else {
715 curpeer->flags &= ~PEER_F_DWNGRD;
716 }
717 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200718 curpeer->appctx = appctx;
719 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100720 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200721 /* fall through */
722 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100723 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200724 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200725
Emeric Brun80527f52017-06-19 17:46:37 +0200726 if (!curpeer) {
727 curpeer = appctx->ctx.peers.ptr;
728 SPIN_LOCK(PEER_LOCK, &curpeer->lock);
729 if (curpeer->appctx != appctx) {
730 appctx->st0 = PEER_SESS_ST_END;
731 goto switchstate;
732 }
733 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100734 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau06d80a92017-10-19 14:32:15 +0200735 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200736 if (repl <= 0) {
737 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100738 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100739 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200740 goto switchstate;
741 }
742
743 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200744 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200745
746 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200747 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200748
749 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200750 curpeer->confirm = 0;
751
752 /* Init cursors */
753 for (st = curpeer->tables; st ; st = st->next) {
754 st->last_get = st->last_acked = 0;
755 st->teaching_origin = st->last_pushed = st->update;
756 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200757
758 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200759 curpeer->flags &= PEER_TEACH_RESET;
760 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200761
762 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200763 if (curpeer->local) {
764 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200765 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
766 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200767 /* assign local peer for a lesson, consider lesson already requested */
768 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200769 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200770 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200771
Emeric Brunb3971ab2015-05-12 18:49:09 +0200772 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200773 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
774 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200775 /* assign peer for a lesson */
776 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200777 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200778 }
779
780
Emeric Brun2b920a12010-09-23 18:30:22 +0200781 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100782 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200783 goto switchstate;
784 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100785 case PEER_SESS_ST_CONNECT: {
Emeric Brun80527f52017-06-19 17:46:37 +0200786
787 if (!curpeer) {
788 curpeer = appctx->ctx.peers.ptr;
789 SPIN_LOCK(PEER_LOCK, &curpeer->lock);
790 if (curpeer->appctx != appctx) {
791 appctx->st0 = PEER_SESS_ST_END;
792 goto switchstate;
793 }
794 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200795
796 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100797 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200798 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
799 PEER_MAJOR_VER,
800 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200802 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100803 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200804 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200805
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100806 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100807 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200808 goto switchstate;
809 }
810
Willy Tarreau06d80a92017-10-19 14:32:15 +0200811 repl = ci_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200812 if (repl <= 0) {
813 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100814 goto full;
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 }
818
819 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100820 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200821 /* fall through */
822 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100823 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200824 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200825
Emeric Brun80527f52017-06-19 17:46:37 +0200826 if (!curpeer) {
827 curpeer = appctx->ctx.peers.ptr;
828 SPIN_LOCK(PEER_LOCK, &curpeer->lock);
829 if (curpeer->appctx != appctx) {
830 appctx->st0 = PEER_SESS_ST_END;
831 goto switchstate;
832 }
833 }
834
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100835 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200836 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200837
Willy Tarreau06d80a92017-10-19 14:32:15 +0200838 reql = co_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200839 if (reql <= 0) { /* closed or EOL not found */
840 if (reql == 0)
841 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100842 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200843 goto switchstate;
844 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100845 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200846 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100847 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200848 goto switchstate;
849 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100850 else if (reql > 1 && (trash.str[reql-2] == '\r'))
851 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200852 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100853 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200854
Willy Tarreau06d80a92017-10-19 14:32:15 +0200855 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200856
857 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200858 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200859
860 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200861 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200862
863 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200864 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200865 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200866 for (st = curpeer->tables; st ; st = st->next) {
867 st->last_get = st->last_acked = 0;
868 st->teaching_origin = st->last_pushed = st->update;
869 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200870
871 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200872 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200873
Emeric Brunb3971ab2015-05-12 18:49:09 +0200874 /* reset teaching and learning flags to 0 */
875 curpeer->flags &= PEER_TEACH_RESET;
876 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200877
Emeric Brunb3971ab2015-05-12 18:49:09 +0200878 /* If current peer is local */
879 if (curpeer->local) {
880 /* flag to start to teach lesson */
881 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200882
Emeric Brunb3971ab2015-05-12 18:49:09 +0200883 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200884 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
885 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 /* If peer is remote and resync from remote is needed,
887 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200888
Emeric Brunb3971ab2015-05-12 18:49:09 +0200889 /* assign peer for a lesson */
890 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200891 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200892 }
893
894 }
895 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200896 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
897 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200898 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100899 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200900 goto switchstate;
901 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100902 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200903 /* fall through */
904 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100905 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200906 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200907 uint32_t msg_len = 0;
908 char *msg_cur = trash.str;
909 char *msg_end = trash.str;
910 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200911 int totl = 0;
912
Emeric Brun80527f52017-06-19 17:46:37 +0200913 if (!curpeer) {
914 curpeer = appctx->ctx.peers.ptr;
915 SPIN_LOCK(PEER_LOCK, &curpeer->lock);
916 if (curpeer->appctx != appctx) {
917 appctx->st0 = PEER_SESS_ST_END;
918 goto switchstate;
919 }
920 }
921
Willy Tarreau06d80a92017-10-19 14:32:15 +0200922 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200923 if (reql <= 0) /* closed or EOL not found */
924 goto incomplete;
925
Emeric Brun2b920a12010-09-23 18:30:22 +0200926 totl += reql;
927
Emeric Brunb3971ab2015-05-12 18:49:09 +0200928 if (msg_head[1] >= 128) {
929 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200930 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200931 if (reql <= 0) /* closed */
932 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200933
Emeric Brunb3971ab2015-05-12 18:49:09 +0200934 totl += reql;
935
936 if (msg_head[2] < 240) {
937 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200938 }
939 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200940 int i;
941 char *cur;
942 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200943
Emeric Brunb3971ab2015-05-12 18:49:09 +0200944 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200945 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200946 if (reql <= 0) /* closed */
947 goto incomplete;
948
949 totl += reql;
950
951 if (!(msg_head[i] & 0x80))
952 break;
953 }
954
955 if (i == sizeof(msg_head)) {
956 /* malformed message */
957 appctx->st0 = PEER_SESS_ST_ERRPROTO;
958 goto switchstate;
959
960 }
961 end = (char *)msg_head + sizeof(msg_head);
962 cur = (char *)&msg_head[2];
963 msg_len = intdecode(&cur, end);
964 if (!cur) {
965 /* malformed message */
966 appctx->st0 = PEER_SESS_ST_ERRPROTO;
967 goto switchstate;
968 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200969 }
970
Willy Tarreau86a446e2013-11-25 23:02:37 +0100971
Emeric Brunb3971ab2015-05-12 18:49:09 +0200972 /* Read message content */
973 if (msg_len) {
974 if (msg_len > trash.size) {
975 /* Status code is not success, abort */
976 appctx->st0 = PEER_SESS_ST_ERRSIZE;
977 goto switchstate;
978 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200979
Willy Tarreau06d80a92017-10-19 14:32:15 +0200980 reql = co_getblk(si_oc(si), trash.str, msg_len, totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200981 if (reql <= 0) /* closed */
982 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200983 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100984
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 msg_end += msg_len;
986 }
987 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100988
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
990 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
991 struct shared_table *st;
992 /* Reset message: remote need resync */
993
994 /* prepare tables fot a global push */
995 for (st = curpeer->tables; st; st = st->next) {
996 st->teaching_origin = st->last_pushed = st->table->update;
997 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100998 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200999
Emeric Brunb3971ab2015-05-12 18:49:09 +02001000 /* reset teaching flags to 0 */
1001 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001002
Emeric Brunb3971ab2015-05-12 18:49:09 +02001003 /* flag to start to teach lesson */
1004 curpeer->flags |= PEER_F_TEACH_PROCESS;
1005
1006
1007 }
1008 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1009
1010 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1011 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001012 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1013 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001014 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001016 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1018
1019 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1020 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001021 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001022
1023 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001024 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1025 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001026 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001027 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001028 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001029 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001030 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001031
1032 /* If stopping state */
1033 if (stopping) {
1034 /* Close session, push resync no more needed */
1035 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1036 appctx->st0 = PEER_SESS_ST_END;
1037 goto switchstate;
1038 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001039 for (st = curpeer->tables; st; st = st->next) {
1040 st->update = st->last_pushed = st->teaching_origin;
1041 st->flags = 0;
1042 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001043
1044 /* reset teaching flags to 0 */
1045 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001046 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001047 }
1048 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1049 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1050 int table_id_len;
1051 struct shared_table *st;
1052 int table_type;
1053 int table_keylen;
1054 int table_id;
1055 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001056
Emeric Brunb3971ab2015-05-12 18:49:09 +02001057 table_id = intdecode(&msg_cur, msg_end);
1058 if (!msg_cur) {
1059 /* malformed message */
1060 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1061 goto switchstate;
1062 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001063
Emeric Brunb3971ab2015-05-12 18:49:09 +02001064 table_id_len = intdecode(&msg_cur, msg_end);
1065 if (!msg_cur) {
1066 /* malformed message */
1067 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1068 goto switchstate;
1069 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001070
Emeric Brunb3971ab2015-05-12 18:49:09 +02001071 curpeer->remote_table = NULL;
1072 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1073 /* malformed message */
1074 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1075 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001076 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001077
Emeric Brunb3971ab2015-05-12 18:49:09 +02001078 for (st = curpeer->tables; st; st = st->next) {
1079 /* Reset IDs */
1080 if (st->remote_id == table_id)
1081 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001082
Emeric Brunb3971ab2015-05-12 18:49:09 +02001083 if (!curpeer->remote_table
1084 && (table_id_len == strlen(st->table->id))
1085 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1086 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001087 }
1088 }
1089
Emeric Brunb3971ab2015-05-12 18:49:09 +02001090 if (!curpeer->remote_table) {
1091 goto ignore_msg;
1092 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001093
Emeric Brunb3971ab2015-05-12 18:49:09 +02001094 msg_cur += table_id_len;
1095 if (msg_cur >= msg_end) {
1096 /* malformed message */
1097 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1098 goto switchstate;
1099 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001100
Emeric Brunb3971ab2015-05-12 18:49:09 +02001101 table_type = intdecode(&msg_cur, msg_end);
1102 if (!msg_cur) {
1103 /* malformed message */
1104 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1105 goto switchstate;
1106 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001107
Emeric Brunb3971ab2015-05-12 18:49:09 +02001108 table_keylen = intdecode(&msg_cur, msg_end);
1109 if (!msg_cur) {
1110 /* malformed message */
1111 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1112 goto switchstate;
1113 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001114
Emeric Brunb3971ab2015-05-12 18:49:09 +02001115 table_data = intdecode(&msg_cur, msg_end);
1116 if (!msg_cur) {
1117 /* malformed message */
1118 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1119 goto switchstate;
1120 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001121
Emeric Brunb3971ab2015-05-12 18:49:09 +02001122 if (curpeer->remote_table->table->type != table_type
1123 || curpeer->remote_table->table->key_size != table_keylen) {
1124 curpeer->remote_table = NULL;
1125 goto ignore_msg;
1126 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001127
Emeric Brunb3971ab2015-05-12 18:49:09 +02001128 curpeer->remote_table->remote_data = table_data;
1129 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001130 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001131 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1132 struct shared_table *st;
1133 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001134
Emeric Brunb3971ab2015-05-12 18:49:09 +02001135 table_id = intdecode(&msg_cur, msg_end);
1136 if (!msg_cur) {
1137 /* malformed message */
1138 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1139 goto switchstate;
1140 }
1141 curpeer->remote_table = NULL;
1142 for (st = curpeer->tables; st; st = st->next) {
1143 if (st->remote_id == table_id) {
1144 curpeer->remote_table = st;
1145 break;
1146 }
1147 }
1148
Emeric Brun2b920a12010-09-23 18:30:22 +02001149 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001150 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001151 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1152 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1153 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001154 struct shared_table *st = curpeer->remote_table;
1155 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001156 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001157 unsigned int data_type;
1158 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001159
Emeric Brunb3971ab2015-05-12 18:49:09 +02001160 /* Here we have data message */
1161 if (!st)
1162 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001163
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001164 expire = MS_TO_TICKS(st->table->expire);
1165
1166 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1167 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001168 if (msg_len < sizeof(update)) {
1169 /* malformed message */
1170 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1171 goto switchstate;
1172 }
1173 memcpy(&update, msg_cur, sizeof(update));
1174 msg_cur += sizeof(update);
1175 st->last_get = htonl(update);
1176 }
1177 else {
1178 st->last_get++;
1179 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001180
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001181 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1182 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1183 size_t expire_sz = sizeof expire;
1184
1185 if (msg_cur + expire_sz > msg_end) {
1186 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1187 goto switchstate;
1188 }
1189 memcpy(&expire, msg_cur, expire_sz);
1190 msg_cur += expire_sz;
1191 expire = ntohl(expire);
1192 }
1193
Emeric Brunb3971ab2015-05-12 18:49:09 +02001194 newts = stksess_new(st->table, NULL);
1195 if (!newts)
1196 goto ignore_msg;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001197 /* Force expiratiion to remote date
1198 in case of first insert */
1199 newts->expire = tick_add(now_ms, expire);
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001200 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001201 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001202
Emeric Brunb3971ab2015-05-12 18:49:09 +02001203 to_read = intdecode(&msg_cur, msg_end);
1204 if (!msg_cur) {
1205 /* malformed message */
1206 stksess_free(st->table, newts);
1207 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1208 goto switchstate;
1209 }
1210 to_store = MIN(to_read, st->table->key_size - 1);
1211 if (msg_cur + to_store > msg_end) {
1212 /* malformed message */
1213 stksess_free(st->table, newts);
1214 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1215 goto switchstate;
1216 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001217
Emeric Brunb3971ab2015-05-12 18:49:09 +02001218 memcpy(newts->key.key, msg_cur, to_store);
1219 newts->key.key[to_store] = 0;
1220 msg_cur += to_read;
1221 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001222 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001223 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001224
Emeric Brunb3971ab2015-05-12 18:49:09 +02001225 if (msg_cur + sizeof(netinteger) > msg_end) {
1226 /* malformed message */
1227 stksess_free(st->table, newts);
1228 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1229 goto switchstate;
1230 }
1231 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1232 netinteger = ntohl(netinteger);
1233 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1234 msg_cur += sizeof(netinteger);
1235 }
1236 else {
1237 if (msg_cur + st->table->key_size > msg_end) {
1238 /* malformed message */
1239 stksess_free(st->table, newts);
1240 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1241 goto switchstate;
1242 }
1243 memcpy(newts->key.key, msg_cur, st->table->key_size);
1244 msg_cur += st->table->key_size;
1245 }
1246
1247 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001248 ts = stktable_set_entry(st->table, newts);
1249 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001250 stksess_free(st->table, newts);
1251 newts = NULL;
1252 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001253
Emeric Brun819fc6f2017-06-13 19:37:32 +02001254 RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001255
Emeric Brun94900952015-06-11 18:25:54 +02001256 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001257
Emeric Brun94900952015-06-11 18:25:54 +02001258 if ((1 << data_type) & st->remote_data) {
1259 switch (stktable_data_types[data_type].std_type) {
1260 case STD_T_SINT: {
1261 int data;
1262
1263 data = intdecode(&msg_cur, msg_end);
1264 if (!msg_cur) {
1265 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001266 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1267 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001268 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1269 goto switchstate;
1270 }
1271
1272 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1273 if (data_ptr)
1274 stktable_data_cast(data_ptr, std_t_sint) = data;
1275 break;
1276 }
1277 case STD_T_UINT: {
1278 unsigned int data;
1279
1280 data = intdecode(&msg_cur, msg_end);
1281 if (!msg_cur) {
1282 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001283 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1284 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001285 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1286 goto switchstate;
1287 }
1288
1289 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1290 if (data_ptr)
1291 stktable_data_cast(data_ptr, std_t_uint) = data;
1292 break;
1293 }
1294 case STD_T_ULL: {
1295 unsigned long long data;
1296
1297 data = intdecode(&msg_cur, msg_end);
1298 if (!msg_cur) {
1299 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001300 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1301 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001302 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1303 goto switchstate;
1304 }
1305
1306 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1307 if (data_ptr)
1308 stktable_data_cast(data_ptr, std_t_ull) = data;
1309 break;
1310 }
1311 case STD_T_FRQP: {
1312 struct freq_ctr_period data;
1313
Willy Tarreau3bb46172016-03-25 18:17:47 +01001314 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001315 if (!msg_cur) {
1316 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001317 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1318 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001319 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1320 goto switchstate;
1321 }
1322 data.curr_ctr = intdecode(&msg_cur, msg_end);
1323 if (!msg_cur) {
1324 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001325 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1326 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001327 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1328 goto switchstate;
1329 }
1330 data.prev_ctr = intdecode(&msg_cur, msg_end);
1331 if (!msg_cur) {
1332 /* malformed message */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001333 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1334 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001335 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1336 goto switchstate;
1337 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001338
Emeric Brun94900952015-06-11 18:25:54 +02001339 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1340 if (data_ptr)
1341 stktable_data_cast(data_ptr, std_t_frqp) = data;
1342 break;
1343 }
1344 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001345 }
1346 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001347
1348 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1349 stktable_touch_remote(st->table, ts, 1);
1350
Emeric Brunb3971ab2015-05-12 18:49:09 +02001351 }
1352 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1353 /* ack message */
1354 uint32_t table_id ;
1355 uint32_t update;
1356 struct shared_table *st;
1357
1358 table_id = intdecode(&msg_cur, msg_end);
1359 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1360 /* malformed message */
1361 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1362 goto switchstate;
1363 }
1364 memcpy(&update, msg_cur, sizeof(update));
1365 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001366
Emeric Brunb3971ab2015-05-12 18:49:09 +02001367 for (st = curpeer->tables; st; st = st->next) {
1368 if (st->local_id == table_id) {
1369 st->update = update;
1370 break;
1371 }
1372 }
1373 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001374 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001375 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1376 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001377 goto switchstate;
1378 }
1379
Emeric Brunb3971ab2015-05-12 18:49:09 +02001380ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001381 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001382 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001383 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001384 goto switchstate;
1385
Emeric Brun2b920a12010-09-23 18:30:22 +02001386incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001387 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001388
Willy Tarreau72d6c162013-04-11 16:14:13 +02001389 if (reql < 0) {
1390 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001391 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001392 goto switchstate;
1393 }
1394
Emeric Brun2b920a12010-09-23 18:30:22 +02001395
Emeric Brun2b920a12010-09-23 18:30:22 +02001396
Emeric Brunb3971ab2015-05-12 18:49:09 +02001397
Emeric Brun2b920a12010-09-23 18:30:22 +02001398 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001399 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001400 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1401 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001402 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001403
Emeric Brunb3971ab2015-05-12 18:49:09 +02001404 /* Current peer was elected to request a resync */
1405 msg[0] = PEER_MSG_CLASS_CONTROL;
1406 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001407
Emeric Brunb3971ab2015-05-12 18:49:09 +02001408 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001409 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001410 if (repl <= 0) {
1411 /* no more write possible */
1412 if (repl == -1)
1413 goto full;
1414 appctx->st0 = PEER_SESS_ST_END;
1415 goto switchstate;
1416 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001417 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001418 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001419
Emeric Brunb3971ab2015-05-12 18:49:09 +02001420 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001421
Emeric Brunb3971ab2015-05-12 18:49:09 +02001422 if (curpeer->tables) {
1423 struct shared_table *st;
1424 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001425
Emeric Brunb3971ab2015-05-12 18:49:09 +02001426 last_local_table = curpeer->last_local_table;
1427 if (!last_local_table)
1428 last_local_table = curpeer->tables;
1429 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001430
Emeric Brunb3971ab2015-05-12 18:49:09 +02001431 while (1) {
1432 if (!st)
1433 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001434
Emeric Brunb3971ab2015-05-12 18:49:09 +02001435 /* It remains some updates to ack */
1436 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001437 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001438
Emeric Brunb3971ab2015-05-12 18:49:09 +02001439 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1440 if (!msglen) {
1441 /* internal error: message does not fit in trash */
1442 appctx->st0 = PEER_SESS_ST_END;
1443 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001444 }
1445
Emeric Brunb3971ab2015-05-12 18:49:09 +02001446 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001447 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001448 if (repl <= 0) {
1449 /* no more write possible */
1450 if (repl == -1) {
1451 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001452 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001453 appctx->st0 = PEER_SESS_ST_END;
1454 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001455 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001456 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001457 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001458
Emeric Brunb3971ab2015-05-12 18:49:09 +02001459 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Emeric Brun80527f52017-06-19 17:46:37 +02001460 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001461 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1462 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1463 struct eb32_node *eb;
1464 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001465
Emeric Brunb3971ab2015-05-12 18:49:09 +02001466 if (st != curpeer->last_local_table) {
1467 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001468
Emeric Brunb3971ab2015-05-12 18:49:09 +02001469 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1470 if (!msglen) {
1471 /* internal error: message does not fit in trash */
1472 appctx->st0 = PEER_SESS_ST_END;
1473 goto switchstate;
1474 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001475
Emeric Brunb3971ab2015-05-12 18:49:09 +02001476 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001477 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001478 if (repl <= 0) {
1479 /* no more write possible */
1480 if (repl == -1) {
1481 goto full;
1482 }
1483 appctx->st0 = PEER_SESS_ST_END;
1484 goto switchstate;
1485 }
1486 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001487 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001488
1489 /* We force new pushed to 1 to force identifier in update message */
1490 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001491 while (1) {
1492 uint32_t msglen;
1493 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001494 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001495
1496 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001497 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001498 if (!eb) {
1499 eb = eb32_first(&st->table->updates);
1500 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001501 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001502 break;
1503 }
1504 }
1505
1506 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001507 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001508 break;
1509 }
1510
1511 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001512 updateid = ts->upd.key;
1513 ts->ref_cnt++;
1514 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1515
1516 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517 if (!msglen) {
1518 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001519 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1520 ts->ref_cnt--;
1521 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001522 appctx->st0 = PEER_SESS_ST_END;
1523 goto switchstate;
1524 }
1525
1526 /* 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 */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001530 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1531 ts->ref_cnt--;
1532 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001533 if (repl == -1) {
1534 goto full;
1535 }
1536 appctx->st0 = PEER_SESS_ST_END;
1537 goto switchstate;
1538 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001539
1540 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1541 ts->ref_cnt--;
1542 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001543 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1544 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001545 /* identifier may not needed in next update message */
1546 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001547 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001548 }
Emeric Brun80527f52017-06-19 17:46:37 +02001549 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001550 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001551 else {
1552 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1553 struct eb32_node *eb;
1554 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001555
Emeric Brunb3971ab2015-05-12 18:49:09 +02001556 if (st != curpeer->last_local_table) {
1557 int msglen;
1558
1559 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1560 if (!msglen) {
1561 /* internal error: message does not fit in trash */
1562 appctx->st0 = PEER_SESS_ST_END;
1563 goto switchstate;
1564 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001565
Emeric Brunb3971ab2015-05-12 18:49:09 +02001566 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001567 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001568 if (repl <= 0) {
1569 /* no more write possible */
1570 if (repl == -1) {
1571 goto full;
1572 }
1573 appctx->st0 = PEER_SESS_ST_END;
1574 goto switchstate;
1575 }
1576 curpeer->last_local_table = st;
1577 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001578
Emeric Brunb3971ab2015-05-12 18:49:09 +02001579 /* We force new pushed to 1 to force identifier in update message */
1580 new_pushed = 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001581 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001582 while (1) {
1583 uint32_t msglen;
1584 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001585 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001586 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001587
Emeric Brunb3971ab2015-05-12 18:49:09 +02001588 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001589 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 if (!eb) {
1591 st->flags |= SHTABLE_F_TEACH_STAGE1;
1592 eb = eb32_first(&st->table->updates);
1593 if (eb)
1594 st->last_pushed = eb->key - 1;
1595 break;
1596 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001597
Emeric Brunb3971ab2015-05-12 18:49:09 +02001598 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001599 updateid = ts->upd.key;
1600 ts->ref_cnt++;
1601 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1602
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001603 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001604 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001605 if (!msglen) {
1606 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001607 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1608 ts->ref_cnt--;
1609 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001610 appctx->st0 = PEER_SESS_ST_END;
1611 goto switchstate;
1612 }
1613
1614 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001615 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001616 if (repl <= 0) {
1617 /* no more write possible */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001618 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1619 ts->ref_cnt--;
1620 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001621 if (repl == -1) {
1622 goto full;
1623 }
1624 appctx->st0 = PEER_SESS_ST_END;
1625 goto switchstate;
1626 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001627 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1628 ts->ref_cnt--;
1629 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001630 /* identifier may not needed in next update message */
1631 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001632 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001633 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001634 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001635
Emeric Brunb3971ab2015-05-12 18:49:09 +02001636 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1637 struct eb32_node *eb;
1638 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001639
Emeric Brunb3971ab2015-05-12 18:49:09 +02001640 if (st != curpeer->last_local_table) {
1641 int msglen;
1642
1643 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1644 if (!msglen) {
1645 /* internal error: message does not fit in trash */
1646 appctx->st0 = PEER_SESS_ST_END;
1647 goto switchstate;
1648 }
1649
1650 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001651 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001652 if (repl <= 0) {
1653 /* no more write possible */
1654 if (repl == -1) {
1655 goto full;
1656 }
1657 appctx->st0 = PEER_SESS_ST_END;
1658 goto switchstate;
1659 }
1660 curpeer->last_local_table = st;
1661 }
1662
1663 /* We force new pushed to 1 to force identifier in update message */
1664 new_pushed = 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001665 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001666 while (1) {
1667 uint32_t msglen;
1668 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001669 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001670 unsigned updateid;
1671
1672 /* push local updates */
1673 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001674
1675 /* push local updates */
1676 if (!eb || eb->key > st->teaching_origin) {
1677 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001678 break;
1679 }
1680
1681 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001682 updateid = ts->upd.key;
1683 ts->ref_cnt++;
1684 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1685
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001686 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001687 msglen = peer_prepare_updatemsg(ts, st, updateid, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001688 if (!msglen) {
1689 /* internal error: message does not fit in trash */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001690 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1691 ts->ref_cnt--;
1692 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001693 appctx->st0 = PEER_SESS_ST_END;
1694 goto switchstate;
1695 }
1696
1697 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001698 repl = ci_putblk(si_ic(si), trash.str, msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001699 if (repl <= 0) {
1700 /* no more write possible */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001701 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1702 ts->ref_cnt--;
1703 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001704 if (repl == -1) {
1705 goto full;
1706 }
1707 appctx->st0 = PEER_SESS_ST_END;
1708 goto switchstate;
1709 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001710
1711 SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1712 ts->ref_cnt--;
1713 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001714 /* identifier may not needed in next update message */
1715 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001716 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001717 SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001718 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001719 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001720
1721 if (st == last_local_table)
1722 break;
1723 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001724 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001725 }
1726
1727
1728 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1729 unsigned char msg[2];
1730
1731 /* Current peer was elected to request a resync */
1732 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001733 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 +02001734 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001735 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001736 if (repl <= 0) {
1737 /* no more write possible */
1738 if (repl == -1)
1739 goto full;
1740 appctx->st0 = PEER_SESS_ST_END;
1741 goto switchstate;
1742 }
1743 /* flag finished message sent */
1744 curpeer->flags |= PEER_F_TEACH_FINISHED;
1745 }
1746
Emeric Brun597b26e2016-08-12 11:23:31 +02001747 /* Confirm finished or partial messages */
1748 while (curpeer->confirm) {
1749 unsigned char msg[2];
1750
1751 /* There is a confirm messages to send */
1752 msg[0] = PEER_MSG_CLASS_CONTROL;
1753 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1754
1755 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001756 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001757 if (repl <= 0) {
1758 /* no more write possible */
1759 if (repl == -1)
1760 goto full;
1761 appctx->st0 = PEER_SESS_ST_END;
1762 goto switchstate;
1763 }
1764 curpeer->confirm--;
1765 }
1766
Emeric Brun2b920a12010-09-23 18:30:22 +02001767 /* noting more to do */
1768 goto out;
1769 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001770 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001771 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001772 if (ci_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001773 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001774 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001775 goto switchstate;
1776 case PEER_SESS_ST_ERRSIZE: {
1777 unsigned char msg[2];
1778
1779 msg[0] = PEER_MSG_CLASS_ERROR;
1780 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1781
Willy Tarreau06d80a92017-10-19 14:32:15 +02001782 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001783 goto full;
1784 appctx->st0 = PEER_SESS_ST_END;
1785 goto switchstate;
1786 }
1787 case PEER_SESS_ST_ERRPROTO: {
1788 unsigned char msg[2];
1789
1790 msg[0] = PEER_MSG_CLASS_ERROR;
1791 msg[1] = PEER_MSG_ERR_PROTOCOL;
1792
Willy Tarreau06d80a92017-10-19 14:32:15 +02001793 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001794 goto full;
1795 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001796 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001797 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001798 case PEER_SESS_ST_END: {
Emeric Brun80527f52017-06-19 17:46:37 +02001799 if (curpeer) {
1800 SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
1801 curpeer = NULL;
1802 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001803 si_shutw(si);
1804 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001805 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001806 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001807 }
1808 }
1809 }
1810out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001811 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001812
1813 if (curpeer)
1814 SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001815 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001816full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001817 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001818 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001819}
1820
Willy Tarreau30576452015-04-13 13:50:30 +02001821static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001822 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001823 .name = "<PEER>", /* used for logging */
1824 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001825 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001826};
Emeric Brun2b920a12010-09-23 18:30:22 +02001827
1828/*
1829 * Use this function to force a close of a peer session
1830 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001831static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001832{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001833 /* Note that the peer sessions which have just been created
1834 * (->st0 == PEER_SESS_ST_CONNECT) must not
1835 * be shutdown, if not, the TCP session will never be closed
1836 * and stay in CLOSE_WAIT state after having been closed by
1837 * the remote side.
1838 */
1839 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001840 return;
1841
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001842 if (appctx->applet != &peer_applet)
1843 return;
1844
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001845 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001846 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001847}
1848
Willy Tarreau91d96282015-03-13 15:47:26 +01001849/* Pre-configures a peers frontend to accept incoming connections */
1850void peers_setup_frontend(struct proxy *fe)
1851{
1852 fe->last_change = now.tv_sec;
1853 fe->cap = PR_CAP_FE;
1854 fe->maxconn = 0;
1855 fe->conn_retries = CONN_RETRIES;
1856 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001857 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001858 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001859 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001860 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001861}
1862
Emeric Brun2b920a12010-09-23 18:30:22 +02001863/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001864 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001865 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001866static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001867{
Willy Tarreau04b92862017-09-15 11:01:04 +02001868 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001869 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001870 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001871 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001872 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001873
Emeric Brunb3971ab2015-05-12 18:49:09 +02001874 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1875 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001876 s = NULL;
1877
Emeric Brun1138fd02017-06-19 12:38:55 +02001878 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001879 if (!appctx)
1880 goto out_close;
1881
1882 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001883 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001884
Willy Tarreau04b92862017-09-15 11:01:04 +02001885 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001886 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001887 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001888 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001889 }
1890
Willy Tarreau87787ac2017-08-28 16:22:54 +02001891 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001892 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001893 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001894 }
1895
Willy Tarreau342bfb12015-04-05 01:35:34 +02001896 /* The tasks below are normally what is supposed to be done by
1897 * fe->accept().
1898 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001899 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001900
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001901 /* applet is waiting for data */
1902 si_applet_cant_get(&s->si[0]);
1903 appctx_wakeup(appctx);
1904
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001905 /* initiate an outgoing connection */
1906 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001907
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001908 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001909 * pre-initialized connection in si->conn.
1910 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001911 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001912 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001913
1914 conn_prepare(conn, peer->proto, peer->xprt);
1915 si_attach_conn(&s->si[1], conn);
1916
1917 conn->target = s->target = &s->be->obj_type;
1918 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001919 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001920 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001921
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001922 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001923
Emeric Brunb3971ab2015-05-12 18:49:09 +02001924 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02001925 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001926 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001927
1928 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001929 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001930 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001931 pool_free2(pool2_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001932 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001933 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001934 out_free_appctx:
1935 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001936 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001937 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001938}
1939
1940/*
1941 * Task processing function to manage re-connect and peer session
1942 * tasks wakeup on local update.
1943 */
Simon Horman96553772011-06-08 09:18:51 +09001944static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001945{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001946 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001947 struct peer *ps;
1948 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001949
1950 task->expire = TICK_ETERNITY;
1951
Emeric Brunb3971ab2015-05-12 18:49:09 +02001952 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001953 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001954 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001955 task_delete(peers->sync_task);
1956 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001957 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001958 return NULL;
1959 }
1960
Emeric Brun80527f52017-06-19 17:46:37 +02001961 /* Acquire lock for all peers of the section */
1962 for (ps = peers->remote; ps; ps = ps->next)
1963 SPIN_LOCK(PEER_LOCK, &ps->lock);
1964
Emeric Brun2b920a12010-09-23 18:30:22 +02001965 if (!stopping) {
1966 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001967
1968 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1969 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1970 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001971 /* Resync from local peer needed
1972 no peer was assigned for the lesson
1973 and no old local peer found
1974 or resync timeout expire */
1975
1976 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001977 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001978
1979 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001980 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001981 }
1982
1983 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001984 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001985 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001986 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001987 if (!ps->appctx) {
1988 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001989 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001990 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001991 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001992 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001993 tick_is_expired(ps->reconnect, now_ms))) {
1994 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001995 * or previous peer connection established with success
1996 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02001997 * and reconnection timer is expired */
1998
1999 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002000 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002001 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002002 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002003 /* If previous session failed during connection
2004 * but reconnection timer is not expired */
2005
2006 /* reschedule task for reconnect */
2007 task->expire = tick_first(task->expire, ps->reconnect);
2008 }
2009 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002010 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002011 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002012 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002013 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2014 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002015 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2016 /* Resync from a remote is needed
2017 * and no peer was assigned for lesson
2018 * and current peer may be up2date */
2019
2020 /* assign peer for the lesson */
2021 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002022 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002023
Willy Tarreau9df94c22016-10-31 18:42:52 +01002024 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002025 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002027 else {
2028 /* Awake session if there is data to push */
2029 for (st = ps->tables; st ; st = st->next) {
2030 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002031 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002032 appctx_wakeup(ps->appctx);
2033 break;
2034 }
2035 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002036 }
2037 /* else do nothing */
2038 } /* SUCCESSCODE */
2039 } /* !ps->peer->local */
2040 } /* for */
2041
2042 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2044 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2045 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002046 /* Resync from remote peer needed
2047 * no peer was assigned for the lesson
2048 * and resync timeout expire */
2049
2050 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002051 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002052 }
2053
Emeric Brunb3971ab2015-05-12 18:49:09 +02002054 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002055 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002056 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2057 if (!tick_is_expired(peers->resync_timeout, now_ms))
2058 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002059 }
2060 } /* !stopping */
2061 else {
2062 /* soft stop case */
2063 if (task->state & TASK_WOKEN_SIGNAL) {
2064 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002065 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002066 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002067 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 peers->flags |= PEERS_F_DONOTSTOP;
2069 ps = peers->local;
2070 for (st = ps->tables; st ; st = st->next)
2071 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 }
2073
2074 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002075 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002076 /* we're killing a connection, we must apply a random delay before
2077 * retrying otherwise the other end will do the same and we can loop
2078 * for a while.
2079 */
2080 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002081 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002082 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002083 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002084 }
2085 }
2086 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002087
Emeric Brunb3971ab2015-05-12 18:49:09 +02002088 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002090 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002091 /* resync of new process was complete, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002092 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002093 peers->flags &= ~PEERS_F_DONOTSTOP;
2094 for (st = ps->tables; st ; st = st->next)
2095 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002096 }
2097 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002098 else if (!ps->appctx) {
2099 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002100 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002101 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2102 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2103 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002104 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002105 * or previous peer connection was successfully established
2106 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002107 * or during previous connect, peer replies a try again statuscode */
2108
2109 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002111 }
2112 else {
2113 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002114 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002115 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002116 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002117 peers->flags &= ~PEERS_F_DONOTSTOP;
2118 for (st = ps->tables; st ; st = st->next)
2119 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002120 }
2121 }
2122 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002123 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002124 /* current peer connection is active and established
2125 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002126 for (st = ps->tables; st ; st = st->next) {
2127 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002128 appctx_wakeup(ps->appctx);
2129 break;
2130 }
2131 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002132 }
2133 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002134
2135 /* Release lock for all peers of the section */
2136 for (ps = peers->remote; ps; ps = ps->next)
2137 SPIN_UNLOCK(PEER_LOCK, &ps->lock);
2138
Emeric Brun2b920a12010-09-23 18:30:22 +02002139 /* Wakeup for re-connect */
2140 return task;
2141}
2142
Emeric Brunb3971ab2015-05-12 18:49:09 +02002143
Emeric Brun2b920a12010-09-23 18:30:22 +02002144/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002145 *
2146 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002147void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002148{
Emeric Brun2b920a12010-09-23 18:30:22 +02002149 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002150 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002151
Emeric Brun2b920a12010-09-23 18:30:22 +02002152 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002153 peers->peers_fe->maxconn += 3;
2154 }
2155
Willy Tarreau4348fad2012-09-20 16:48:07 +02002156 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2157 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002158 peers->sync_task = task_new(MAX_THREADS_MASK);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002159 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002160 peers->sync_task->context = (void *)peers;
2161 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2162 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2163}
2164
2165
2166
2167/*
2168 * Function used to register a table for sync on a group of peers
2169 *
2170 */
2171void peers_register_table(struct peers *peers, struct stktable *table)
2172{
2173 struct shared_table *st;
2174 struct peer * curpeer;
2175 int id = 0;
2176
2177 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002178 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002179 st->table = table;
2180 st->next = curpeer->tables;
2181 if (curpeer->tables)
2182 id = curpeer->tables->local_id;
2183 st->local_id = id + 1;
2184
2185 curpeer->tables = st;
2186 }
2187
2188 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002189}
2190