blob: d11792f918dc79abeb43f191bd4ed72fe544eff8 [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>
Emeric Brun2b920a12010-09-23 18:30:22 +020027
28#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010029#include <types/listener.h>
30#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020031#include <types/peers.h>
32
33#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020034#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020036#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010037#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020038#include <proto/log.h>
39#include <proto/hdr_idx.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020040#include <proto/proto_tcp.h>
41#include <proto/proto_http.h>
42#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020043#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020044#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010045#include <proto/signal.h>
46#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020047#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049
50
51/*******************************/
52/* Current peer learning state */
53/*******************************/
54
55/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020056/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020057/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
59#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
60#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
61#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
62#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020063 to push data to new process */
64
Emeric Brunb3971ab2015-05-12 18:49:09 +020065#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
66#define PEERS_RESYNC_FROMLOCAL 0x00000000
67#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
68#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
69
70/***********************************/
71/* Current shared table sync state */
72/***********************************/
73#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
74#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020075
76/******************************/
77/* Remote peer teaching state */
78/******************************/
79#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020080#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
81#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
82#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
83#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 +020084#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 +020085
Emeric Brunb3971ab2015-05-12 18:49:09 +020086#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 +020087#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
88
Emeric Brunb3971ab2015-05-12 18:49:09 +020089/*****************************/
90/* Sync message class */
91/*****************************/
92enum {
93 PEER_MSG_CLASS_CONTROL = 0,
94 PEER_MSG_CLASS_ERROR,
95 PEER_MSG_CLASS_STICKTABLE = 10,
96 PEER_MSG_CLASS_RESERVED = 255,
97};
98
99/*****************************/
100/* control message types */
101/*****************************/
102enum {
103 PEER_MSG_CTRL_RESYNCREQ = 0,
104 PEER_MSG_CTRL_RESYNCFINISHED,
105 PEER_MSG_CTRL_RESYNCPARTIAL,
106 PEER_MSG_CTRL_RESYNCCONFIRM,
107};
108
109/*****************************/
110/* error message types */
111/*****************************/
112enum {
113 PEER_MSG_ERR_PROTOCOL = 0,
114 PEER_MSG_ERR_SIZELIMIT,
115};
116
117
118/*******************************/
119/* stick table sync mesg types */
120/* Note: ids >= 128 contains */
121/* id message cotains data */
122/*******************************/
123enum {
124 PEER_MSG_STKT_UPDATE = 128,
125 PEER_MSG_STKT_INCUPDATE,
126 PEER_MSG_STKT_DEFINE,
127 PEER_MSG_STKT_SWITCH,
128 PEER_MSG_STKT_ACK,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200129 PEER_MSG_STKT_UPDATE_TIMED,
130 PEER_MSG_STKT_INCUPDATE_TIMED,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200131};
Emeric Brun2b920a12010-09-23 18:30:22 +0200132
133/**********************************/
134/* Peer Session IO handler states */
135/**********************************/
136
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100137enum {
138 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
139 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
140 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
141 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100142 /* after this point, data were possibly exchanged */
143 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
144 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
145 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
146 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
147 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200148 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
149 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100150 PEER_SESS_ST_END, /* Killed session */
151};
Emeric Brun2b920a12010-09-23 18:30:22 +0200152
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100153/***************************************************/
154/* Peer Session status code - part of the protocol */
155/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200156
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100157#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
158#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200159
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100160#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200161
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100162#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200163
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100164#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
165#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
166#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
167#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200168
169#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200170#define PEER_MAJOR_VER 2
171#define PEER_MINOR_VER 1
172#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200173
174struct peers *peers = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +0200175static void peer_session_forceshutdown(struct stream * stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200176
Emeric Brunb3971ab2015-05-12 18:49:09 +0200177int intencode(uint64_t i, char **str) {
178 int idx = 0;
179 unsigned char *msg;
180
181 if (!*str)
182 return 0;
183
184 msg = (unsigned char *)*str;
185 if (i < 240) {
186 msg[0] = (unsigned char)i;
187 *str = (char *)&msg[idx+1];
188 return (idx+1);
189 }
190
191 msg[idx] =(unsigned char)i | 240;
192 i = (i - 240) >> 4;
193 while (i >= 128) {
194 msg[++idx] = (unsigned char)i | 128;
195 i = (i - 128) >> 7;
196 }
197 msg[++idx] = (unsigned char)i;
198 *str = (char *)&msg[idx+1];
199 return (idx+1);
200}
201
202
203/* This function returns the decoded integer or 0
204 if decode failed
205 *str point on the beginning of the integer to decode
206 at the end of decoding *str point on the end of the
207 encoded integer or to null if end is reached */
208uint64_t intdecode(char **str, char *end) {
209 uint64_t i;
210 int idx = 0;
211 unsigned char *msg;
212
213 if (!*str)
214 return 0;
215
216 msg = (unsigned char *)*str;
217 if (msg >= (unsigned char *)end) {
218 *str = NULL;
219 return 0;
220 }
221
222 if (msg[idx] < 240) {
223 *str = (char *)&msg[idx+1];
224 return msg[idx];
225 }
226 i = msg[idx];
227 do {
228 idx++;
229 if (msg >= (unsigned char *)end) {
230 *str = NULL;
231 return 0;
232 }
233 i += (uint64_t)msg[idx] << (4 + 7*(idx-1));
234 }
Frédéric Lécaille22fc3202016-07-19 14:04:36 +0200235 while (msg[idx] >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200236 *str = (char *)&msg[idx+1];
237 return i;
238}
Emeric Brun2b920a12010-09-23 18:30:22 +0200239
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200240/* Set the stick-table UPDATE message type byte at <msg_type> address,
241 * depending on <use_identifier> and <use_timed> boolean parameters.
242 * Always successful.
243 */
244static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
245{
246 if (use_timed) {
247 if (use_identifier)
248 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
249 else
250 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
251 }
252 else {
253 if (use_identifier)
254 *msg_type = PEER_MSG_STKT_UPDATE;
255 else
256 *msg_type = PEER_MSG_STKT_INCUPDATE;
257 }
258
259}
Emeric Brun2b920a12010-09-23 18:30:22 +0200260/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200261 * This prepare the data update message on the stick session <ts>, <st> is the considered
262 * stick table.
263 * <msg> is a buffer of <size> to recieve data message content
264 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
265 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200266 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200267static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200268{
269 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200270 unsigned short datalen;
271 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200272 unsigned int data_type;
273 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274
275 cursor = datamsg = msg + 1 + 5;
276
Emeric Brun2b920a12010-09-23 18:30:22 +0200277 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 /* check if we need to send the update identifer */
Emeric Bruna6a09982015-09-22 15:34:19 +0200280 if (!st->last_pushed || ts->upd.key < st->last_pushed || ((ts->upd.key - st->last_pushed) != 1)) {
281 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200282 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200283
284 /* encode update identifier if needed */
285 if (use_identifier) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 netinteger = htonl(ts->upd.key);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287 memcpy(cursor, &netinteger, sizeof(netinteger));
288 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200289 }
290
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200291 if (use_timed) {
292 netinteger = htonl(tick_remain(now_ms, ts->expire));
293 memcpy(cursor, &netinteger, sizeof(netinteger));
294 cursor += sizeof(netinteger);
295 }
296
Emeric Brunb3971ab2015-05-12 18:49:09 +0200297 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200298 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200299 int stlen = strlen((char *)ts->key.key);
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 intencode(stlen, &cursor);
302 memcpy(cursor, ts->key.key, stlen);
303 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200304 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200305 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200306 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200307 memcpy(cursor, &netinteger, sizeof(netinteger));
308 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200309 }
310 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, ts->key.key, st->table->key_size);
312 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200316 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200317
Emeric Brun94900952015-06-11 18:25:54 +0200318 data_ptr = stktable_data_ptr(st->table, ts, data_type);
319 if (data_ptr) {
320 switch (stktable_data_types[data_type].std_type) {
321 case STD_T_SINT: {
322 int data;
323
324 data = stktable_data_cast(data_ptr, std_t_sint);
325 intencode(data, &cursor);
326 break;
327 }
328 case STD_T_UINT: {
329 unsigned int data;
330
331 data = stktable_data_cast(data_ptr, std_t_uint);
332 intencode(data, &cursor);
333 break;
334 }
335 case STD_T_ULL: {
336 unsigned long long data;
337
338 data = stktable_data_cast(data_ptr, std_t_ull);
339 intencode(data, &cursor);
340 break;
341 }
342 case STD_T_FRQP: {
343 struct freq_ctr_period *frqp;
344
345 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
346 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
347 intencode(frqp->curr_ctr, &cursor);
348 intencode(frqp->prev_ctr, &cursor);
349 break;
350 }
351 }
352 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200353 }
354
355 /* Compute datalen */
356 datalen = (cursor - datamsg);
357
358 /* prepare message header */
359 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200360 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200361 cursor = &msg[2];
362 intencode(datalen, &cursor);
363
364 /* move data after header */
365 memmove(cursor, datamsg, datalen);
366
367 /* return header size + data_len */
368 return (cursor - msg) + datalen;
369}
370
371/*
372 * This prepare the switch table message to targeted share table <st>.
373 * <msg> is a buffer of <size> to recieve data message content
374 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
375 * check size)
376 */
377static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
378{
379 int len;
380 unsigned short datalen;
381 char *cursor, *datamsg;
382 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200383 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200384
385 cursor = datamsg = msg + 2 + 5;
386
387 /* Encode data */
388
389 /* encode local id */
390 intencode(st->local_id, &cursor);
391
392 /* encode table name */
393 len = strlen(st->table->id);
394 intencode(len, &cursor);
395 memcpy(cursor, st->table->id, len);
396 cursor += len;
397
398 /* encode table type */
399
400 intencode(st->table->type, &cursor);
401
402 /* encode table key size */
403 intencode(st->table->key_size, &cursor);
404
Emeric Brun94900952015-06-11 18:25:54 +0200405 /* encode available known data types in table */
406 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
407 if (st->table->data_ofs[data_type]) {
408 switch (stktable_data_types[data_type].std_type) {
409 case STD_T_SINT:
410 case STD_T_UINT:
411 case STD_T_ULL:
412 case STD_T_FRQP:
413 data |= 1 << data_type;
414 break;
415 }
416 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200417 }
418 intencode(data, &cursor);
419
420 /* Compute datalen */
421 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200422
Emeric Brunb3971ab2015-05-12 18:49:09 +0200423 /* prepare message header */
424 msg[0] = PEER_MSG_CLASS_STICKTABLE;
425 msg[1] = PEER_MSG_STKT_DEFINE;
426 cursor = &msg[2];
427 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200428
Emeric Brunb3971ab2015-05-12 18:49:09 +0200429 /* move data after header */
430 memmove(cursor, datamsg, datalen);
431
432 /* return header size + data_len */
433 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200434}
435
Emeric Brunb3971ab2015-05-12 18:49:09 +0200436/*
437 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
438 * stick table.
439 * <msg> is a buffer of <size> to recieve data message content
440 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
441 * check size)
442 */
443static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
444{
445 unsigned short datalen;
446 char *cursor, *datamsg;
447 uint32_t netinteger;
448
Emeric Brunb058f1c2015-09-22 15:50:18 +0200449 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450
451 intencode(st->remote_id, &cursor);
452 netinteger = htonl(st->last_get);
453 memcpy(cursor, &netinteger, sizeof(netinteger));
454 cursor += sizeof(netinteger);
455
456 /* Compute datalen */
457 datalen = (cursor - datamsg);
458
459 /* prepare message header */
460 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200461 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 cursor = &msg[2];
463 intencode(datalen, &cursor);
464
465 /* move data after header */
466 memmove(cursor, datamsg, datalen);
467
468 /* return header size + data_len */
469 return (cursor - msg) + datalen;
470}
Emeric Brun2b920a12010-09-23 18:30:22 +0200471
472/*
473 * Callback to release a session with a peer
474 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200475static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200476{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200477 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200478 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200479 struct peer *peer = appctx->ctx.peers.ptr;
480 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200481
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100482 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100483 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200484 return;
485
486 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487 if (peer) {
488 if (peer->stream == s) {
Emeric Brunb157d732015-08-21 12:00:30 +0200489 /* Re-init current table pointers to force announcement on re-connect */
490 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491 peer->stream = NULL;
492 peer->appctx = NULL;
493 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200494 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200495 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
496 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200497
498 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200499 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200500 }
501 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502 peer->flags &= PEER_TEACH_RESET;
503 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200504 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200505 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200506 }
507}
508
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200509/* Retrieve the major and minor versions of peers protocol
510 * announced by a remote peer. <str> is a null-terminated
511 * string with the following format: "<maj_ver>.<min_ver>".
512 */
513static int peer_get_version(const char *str,
514 unsigned int *maj_ver, unsigned int *min_ver)
515{
516 unsigned int majv, minv;
517 const char *pos, *saved;
518 const char *end;
519
520 saved = pos = str;
521 end = str + strlen(str);
522
523 majv = read_uint(&pos, end);
524 if (saved == pos || *pos++ != '.')
525 return -1;
526
527 saved = pos;
528 minv = read_uint(&pos, end);
529 if (saved == pos || pos != end)
530 return -1;
531
532 *maj_ver = majv;
533 *min_ver = minv;
534
535 return 0;
536}
Emeric Brun2b920a12010-09-23 18:30:22 +0200537
538/*
539 * IO Handler to handle message exchance with a peer
540 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200541static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200542{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200543 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200544 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200545 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200546 int reql = 0;
547 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200548 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
549 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200550
551 while (1) {
552switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200553 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100554 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100555 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100556 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100557 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200558 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100559 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100560 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200561 if (reql <= 0) { /* closed or EOL not found */
562 if (reql == 0)
563 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100564 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200565 goto switchstate;
566 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100567 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100568 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200569 goto switchstate;
570 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100571 else if (reql > 1 && (trash.str[reql-2] == '\r'))
572 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200573 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100574 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200575
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100576 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200577
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200578 /* test protocol */
579 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
580 appctx->st0 = PEER_SESS_ST_EXIT;
581 appctx->st1 = PEER_SESS_SC_ERRPROTO;
582 goto switchstate;
583 }
584 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
585 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100586 appctx->st0 = PEER_SESS_ST_EXIT;
587 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200588 goto switchstate;
589 }
590
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100591 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200592 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100593 case PEER_SESS_ST_GETHOST:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100594 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200595 if (reql <= 0) { /* closed or EOL not found */
596 if (reql == 0)
597 goto out;
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 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100602 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200603 goto switchstate;
604 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100605 else if (reql > 1 && (trash.str[reql-2] == '\r'))
606 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200607 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100608 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200609
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100610 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200611
612 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100613 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100614 appctx->st0 = PEER_SESS_ST_EXIT;
615 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200616 goto switchstate;
617 }
618
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100619 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200620 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100621 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 struct peer *curpeer;
623 char *p;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100624 reql = bo_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') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200632 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100633 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200634 goto switchstate;
635 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100636 else if (reql > 1 && (trash.str[reql-2] == '\r'))
637 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200638 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100639 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200640
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100641 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200642
Emeric Brunb3971ab2015-05-12 18:49:09 +0200643 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100644 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200645 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100646 appctx->st0 = PEER_SESS_ST_EXIT;
647 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200648 goto switchstate;
649 }
650 *p = 0;
651
652 /* lookup known peer */
653 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100654 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200655 break;
656 }
657
658 /* if unknown peer */
659 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100660 appctx->st0 = PEER_SESS_ST_EXIT;
661 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 goto switchstate;
663 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200664
Emeric Brunb3971ab2015-05-12 18:49:09 +0200665 if (curpeer->stream && curpeer->stream != s) {
666 if (curpeer->local) {
667 /* Local connection, reply a retry */
668 appctx->st0 = PEER_SESS_ST_EXIT;
669 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
670 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200671 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672 peer_session_forceshutdown(curpeer->stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200673 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200674 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
675 if (min_ver == PEER_DWNGRD_MINOR_VER) {
676 curpeer->flags |= PEER_F_DWNGRD;
677 }
678 else {
679 curpeer->flags &= ~PEER_F_DWNGRD;
680 }
681 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200682 curpeer->stream = s;
683 curpeer->appctx = appctx;
684 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100685 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200686 /* fall through */
687 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100688 case PEER_SESS_ST_SENDSUCCESS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200689 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200690 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200691
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100692 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100693 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200694 if (repl <= 0) {
695 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100696 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100697 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 goto switchstate;
699 }
700
701 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200702 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200703
704 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200705 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200706
707 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200708 curpeer->confirm = 0;
709
710 /* Init cursors */
711 for (st = curpeer->tables; st ; st = st->next) {
712 st->last_get = st->last_acked = 0;
713 st->teaching_origin = st->last_pushed = st->update;
714 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200715
716 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200717 curpeer->flags &= PEER_TEACH_RESET;
718 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200719
720 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200721 if (curpeer->local) {
722 /* if current host need resyncfrom local and no process assined */
723 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
724 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
725 /* assign local peer for a lesson, consider lesson already requested */
726 curpeer->flags |= PEER_F_LEARN_ASSIGN;
727 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
728 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200729
Emeric Brunb3971ab2015-05-12 18:49:09 +0200730 }
731 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
732 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
733 /* assign peer for a lesson */
734 curpeer->flags |= PEER_F_LEARN_ASSIGN;
735 peers->flags |= PEERS_F_RESYNC_ASSIGN;
736 }
737
738
Emeric Brun2b920a12010-09-23 18:30:22 +0200739 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100740 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200741 goto switchstate;
742 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100743 case PEER_SESS_ST_CONNECT: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200744 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200745
746 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100747 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200748 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
749 PEER_MAJOR_VER,
750 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200751 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200752 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100753 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200754 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200755
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100756 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100757 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200758 goto switchstate;
759 }
760
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100761 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200762 if (repl <= 0) {
763 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100764 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100765 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200766 goto switchstate;
767 }
768
769 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100770 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200771 /* fall through */
772 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100773 case PEER_SESS_ST_GETSTATUS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200774 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200775 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200776
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100777 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200778 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200779
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100780 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200781 if (reql <= 0) { /* closed or EOL not found */
782 if (reql == 0)
783 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100784 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200785 goto switchstate;
786 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100787 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200788 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100789 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200790 goto switchstate;
791 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100792 else if (reql > 1 && (trash.str[reql-2] == '\r'))
793 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200794 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100795 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200796
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100797 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200798
799 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200800 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200801
802 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200803 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200804
805 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200806 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200807 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200808 for (st = curpeer->tables; st ; st = st->next) {
809 st->last_get = st->last_acked = 0;
810 st->teaching_origin = st->last_pushed = st->update;
811 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200812
813 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200814 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200815
Emeric Brunb3971ab2015-05-12 18:49:09 +0200816 /* reset teaching and learning flags to 0 */
817 curpeer->flags &= PEER_TEACH_RESET;
818 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200819
Emeric Brunb3971ab2015-05-12 18:49:09 +0200820 /* If current peer is local */
821 if (curpeer->local) {
822 /* flag to start to teach lesson */
823 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200824
Emeric Brunb3971ab2015-05-12 18:49:09 +0200825 }
826 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
827 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
828 /* If peer is remote and resync from remote is needed,
829 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200830
Emeric Brunb3971ab2015-05-12 18:49:09 +0200831 /* assign peer for a lesson */
832 curpeer->flags |= PEER_F_LEARN_ASSIGN;
833 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200834 }
835
836 }
837 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200838 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
839 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200840 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100841 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200842 goto switchstate;
843 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100844 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200845 /* fall through */
846 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100847 case PEER_SESS_ST_WAITMSG: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200848 struct peer *curpeer = appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200849 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200850 uint32_t msg_len = 0;
851 char *msg_cur = trash.str;
852 char *msg_end = trash.str;
853 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200854 int totl = 0;
855
Emeric Brunb3971ab2015-05-12 18:49:09 +0200856 reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200857 if (reql <= 0) /* closed or EOL not found */
858 goto incomplete;
859
Emeric Brun2b920a12010-09-23 18:30:22 +0200860 totl += reql;
861
Emeric Brunb3971ab2015-05-12 18:49:09 +0200862 if (msg_head[1] >= 128) {
863 /* Read and Decode message length */
864 reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
865 if (reql <= 0) /* closed */
866 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200867
Emeric Brunb3971ab2015-05-12 18:49:09 +0200868 totl += reql;
869
870 if (msg_head[2] < 240) {
871 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200872 }
873 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200874 int i;
875 char *cur;
876 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200877
Emeric Brunb3971ab2015-05-12 18:49:09 +0200878 for (i = 3 ; i < sizeof(msg_head) ; i++) {
879 reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
880 if (reql <= 0) /* closed */
881 goto incomplete;
882
883 totl += reql;
884
885 if (!(msg_head[i] & 0x80))
886 break;
887 }
888
889 if (i == sizeof(msg_head)) {
890 /* malformed message */
891 appctx->st0 = PEER_SESS_ST_ERRPROTO;
892 goto switchstate;
893
894 }
895 end = (char *)msg_head + sizeof(msg_head);
896 cur = (char *)&msg_head[2];
897 msg_len = intdecode(&cur, end);
898 if (!cur) {
899 /* malformed message */
900 appctx->st0 = PEER_SESS_ST_ERRPROTO;
901 goto switchstate;
902 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200903 }
904
Willy Tarreau86a446e2013-11-25 23:02:37 +0100905
Emeric Brunb3971ab2015-05-12 18:49:09 +0200906 /* Read message content */
907 if (msg_len) {
908 if (msg_len > trash.size) {
909 /* Status code is not success, abort */
910 appctx->st0 = PEER_SESS_ST_ERRSIZE;
911 goto switchstate;
912 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200913
Emeric Brunb3971ab2015-05-12 18:49:09 +0200914 reql = bo_getblk(si_oc(si), trash.str, msg_len, totl);
915 if (reql <= 0) /* closed */
916 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200917 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100918
Emeric Brunb3971ab2015-05-12 18:49:09 +0200919 msg_end += msg_len;
920 }
921 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100922
Emeric Brunb3971ab2015-05-12 18:49:09 +0200923 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
924 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
925 struct shared_table *st;
926 /* Reset message: remote need resync */
927
928 /* prepare tables fot a global push */
929 for (st = curpeer->tables; st; st = st->next) {
930 st->teaching_origin = st->last_pushed = st->table->update;
931 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100932 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200933
Emeric Brunb3971ab2015-05-12 18:49:09 +0200934 /* reset teaching flags to 0 */
935 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200936
Emeric Brunb3971ab2015-05-12 18:49:09 +0200937 /* flag to start to teach lesson */
938 curpeer->flags |= PEER_F_TEACH_PROCESS;
939
940
941 }
942 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
943
944 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
945 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
946 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
947 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100948 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200949 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200950 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200951 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
952
953 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
954 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
955 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
956
957 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
958 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
959 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100960 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200961 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200962 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200963 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +0200964 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200965
966 /* If stopping state */
967 if (stopping) {
968 /* Close session, push resync no more needed */
969 curpeer->flags |= PEER_F_TEACH_COMPLETE;
970 appctx->st0 = PEER_SESS_ST_END;
971 goto switchstate;
972 }
Emeric Brun597b26e2016-08-12 11:23:31 +0200973 for (st = curpeer->tables; st; st = st->next) {
974 st->update = st->last_pushed = st->teaching_origin;
975 st->flags = 0;
976 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200977
978 /* reset teaching flags to 0 */
979 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200980 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200981 }
982 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
983 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
984 int table_id_len;
985 struct shared_table *st;
986 int table_type;
987 int table_keylen;
988 int table_id;
989 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +0200990
Emeric Brunb3971ab2015-05-12 18:49:09 +0200991 table_id = intdecode(&msg_cur, msg_end);
992 if (!msg_cur) {
993 /* malformed message */
994 appctx->st0 = PEER_SESS_ST_ERRPROTO;
995 goto switchstate;
996 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200997
Emeric Brunb3971ab2015-05-12 18:49:09 +0200998 table_id_len = intdecode(&msg_cur, msg_end);
999 if (!msg_cur) {
1000 /* malformed message */
1001 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1002 goto switchstate;
1003 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001004
Emeric Brunb3971ab2015-05-12 18:49:09 +02001005 curpeer->remote_table = NULL;
1006 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1007 /* malformed message */
1008 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1009 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001010 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001011
Emeric Brunb3971ab2015-05-12 18:49:09 +02001012 for (st = curpeer->tables; st; st = st->next) {
1013 /* Reset IDs */
1014 if (st->remote_id == table_id)
1015 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001016
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017 if (!curpeer->remote_table
1018 && (table_id_len == strlen(st->table->id))
1019 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1020 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001021 }
1022 }
1023
Emeric Brunb3971ab2015-05-12 18:49:09 +02001024 if (!curpeer->remote_table) {
1025 goto ignore_msg;
1026 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001027
Emeric Brunb3971ab2015-05-12 18:49:09 +02001028 msg_cur += table_id_len;
1029 if (msg_cur >= msg_end) {
1030 /* malformed message */
1031 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1032 goto switchstate;
1033 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001034
Emeric Brunb3971ab2015-05-12 18:49:09 +02001035 table_type = intdecode(&msg_cur, msg_end);
1036 if (!msg_cur) {
1037 /* malformed message */
1038 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1039 goto switchstate;
1040 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001041
Emeric Brunb3971ab2015-05-12 18:49:09 +02001042 table_keylen = intdecode(&msg_cur, msg_end);
1043 if (!msg_cur) {
1044 /* malformed message */
1045 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1046 goto switchstate;
1047 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001048
Emeric Brunb3971ab2015-05-12 18:49:09 +02001049 table_data = intdecode(&msg_cur, msg_end);
1050 if (!msg_cur) {
1051 /* malformed message */
1052 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1053 goto switchstate;
1054 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001055
Emeric Brunb3971ab2015-05-12 18:49:09 +02001056 if (curpeer->remote_table->table->type != table_type
1057 || curpeer->remote_table->table->key_size != table_keylen) {
1058 curpeer->remote_table = NULL;
1059 goto ignore_msg;
1060 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001061
Emeric Brunb3971ab2015-05-12 18:49:09 +02001062 curpeer->remote_table->remote_data = table_data;
1063 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001064 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001065 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1066 struct shared_table *st;
1067 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001068
Emeric Brunb3971ab2015-05-12 18:49:09 +02001069 table_id = intdecode(&msg_cur, msg_end);
1070 if (!msg_cur) {
1071 /* malformed message */
1072 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1073 goto switchstate;
1074 }
1075 curpeer->remote_table = NULL;
1076 for (st = curpeer->tables; st; st = st->next) {
1077 if (st->remote_id == table_id) {
1078 curpeer->remote_table = st;
1079 break;
1080 }
1081 }
1082
Emeric Brun2b920a12010-09-23 18:30:22 +02001083 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001084 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001085 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1086 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1087 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001088 struct shared_table *st = curpeer->remote_table;
1089 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001090 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001091 unsigned int data_type;
1092 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001093
Emeric Brunb3971ab2015-05-12 18:49:09 +02001094 /* Here we have data message */
1095 if (!st)
1096 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001097
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001098 expire = MS_TO_TICKS(st->table->expire);
1099
1100 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1101 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001102 if (msg_len < sizeof(update)) {
1103 /* malformed message */
1104 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1105 goto switchstate;
1106 }
1107 memcpy(&update, msg_cur, sizeof(update));
1108 msg_cur += sizeof(update);
1109 st->last_get = htonl(update);
1110 }
1111 else {
1112 st->last_get++;
1113 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001114
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001115 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1116 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1117 size_t expire_sz = sizeof expire;
1118
1119 if (msg_cur + expire_sz > msg_end) {
1120 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1121 goto switchstate;
1122 }
1123 memcpy(&expire, msg_cur, expire_sz);
1124 msg_cur += expire_sz;
1125 expire = ntohl(expire);
1126 }
1127
Emeric Brunb3971ab2015-05-12 18:49:09 +02001128 newts = stksess_new(st->table, NULL);
1129 if (!newts)
1130 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001131
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001132 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001133 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001134
Emeric Brunb3971ab2015-05-12 18:49:09 +02001135 to_read = intdecode(&msg_cur, msg_end);
1136 if (!msg_cur) {
1137 /* malformed message */
1138 stksess_free(st->table, newts);
1139 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1140 goto switchstate;
1141 }
1142 to_store = MIN(to_read, st->table->key_size - 1);
1143 if (msg_cur + to_store > msg_end) {
1144 /* malformed message */
1145 stksess_free(st->table, newts);
1146 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1147 goto switchstate;
1148 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001149
Emeric Brunb3971ab2015-05-12 18:49:09 +02001150 memcpy(newts->key.key, msg_cur, to_store);
1151 newts->key.key[to_store] = 0;
1152 msg_cur += to_read;
1153 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001154 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001155 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001156
Emeric Brunb3971ab2015-05-12 18:49:09 +02001157 if (msg_cur + sizeof(netinteger) > msg_end) {
1158 /* malformed message */
1159 stksess_free(st->table, newts);
1160 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1161 goto switchstate;
1162 }
1163 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1164 netinteger = ntohl(netinteger);
1165 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1166 msg_cur += sizeof(netinteger);
1167 }
1168 else {
1169 if (msg_cur + st->table->key_size > msg_end) {
1170 /* malformed message */
1171 stksess_free(st->table, newts);
1172 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1173 goto switchstate;
1174 }
1175 memcpy(newts->key.key, msg_cur, st->table->key_size);
1176 msg_cur += st->table->key_size;
1177 }
1178
1179 /* lookup for existing entry */
1180 ts = stktable_lookup(st->table, newts);
1181 if (ts) {
1182 /* the entry already exist, we can free ours */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001183 stktable_touch_with_exp(st->table, ts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001184 stksess_free(st->table, newts);
1185 newts = NULL;
1186 }
1187 else {
1188 struct eb32_node *eb;
1189
1190 /* create new entry */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001191 ts = stktable_store_with_exp(st->table, newts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001192 newts = NULL; /* don't reuse it */
1193
Emeric Brun234fc3c2015-12-16 15:16:46 +01001194 ts->upd.key= (++st->table->update)+(2147483648U);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001195 eb = eb32_insert(&st->table->updates, &ts->upd);
1196 if (eb != &ts->upd) {
1197 eb32_delete(eb);
1198 eb32_insert(&st->table->updates, &ts->upd);
1199 }
1200 }
1201
Emeric Brun94900952015-06-11 18:25:54 +02001202 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001203
Emeric Brun94900952015-06-11 18:25:54 +02001204 if ((1 << data_type) & st->remote_data) {
1205 switch (stktable_data_types[data_type].std_type) {
1206 case STD_T_SINT: {
1207 int data;
1208
1209 data = intdecode(&msg_cur, msg_end);
1210 if (!msg_cur) {
1211 /* malformed message */
1212 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1213 goto switchstate;
1214 }
1215
1216 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1217 if (data_ptr)
1218 stktable_data_cast(data_ptr, std_t_sint) = data;
1219 break;
1220 }
1221 case STD_T_UINT: {
1222 unsigned int data;
1223
1224 data = intdecode(&msg_cur, msg_end);
1225 if (!msg_cur) {
1226 /* malformed message */
1227 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1228 goto switchstate;
1229 }
1230
1231 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1232 if (data_ptr)
1233 stktable_data_cast(data_ptr, std_t_uint) = data;
1234 break;
1235 }
1236 case STD_T_ULL: {
1237 unsigned long long data;
1238
1239 data = intdecode(&msg_cur, msg_end);
1240 if (!msg_cur) {
1241 /* malformed message */
1242 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1243 goto switchstate;
1244 }
1245
1246 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1247 if (data_ptr)
1248 stktable_data_cast(data_ptr, std_t_ull) = data;
1249 break;
1250 }
1251 case STD_T_FRQP: {
1252 struct freq_ctr_period data;
1253
Willy Tarreau3bb46172016-03-25 18:17:47 +01001254 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001255 if (!msg_cur) {
1256 /* malformed message */
1257 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1258 goto switchstate;
1259 }
1260 data.curr_ctr = intdecode(&msg_cur, msg_end);
1261 if (!msg_cur) {
1262 /* malformed message */
1263 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1264 goto switchstate;
1265 }
1266 data.prev_ctr = intdecode(&msg_cur, msg_end);
1267 if (!msg_cur) {
1268 /* malformed message */
1269 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1270 goto switchstate;
1271 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001272
Emeric Brun94900952015-06-11 18:25:54 +02001273 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1274 if (data_ptr)
1275 stktable_data_cast(data_ptr, std_t_frqp) = data;
1276 break;
1277 }
1278 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001279 }
1280 }
1281 }
1282 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1283 /* ack message */
1284 uint32_t table_id ;
1285 uint32_t update;
1286 struct shared_table *st;
1287
1288 table_id = intdecode(&msg_cur, msg_end);
1289 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1290 /* malformed message */
1291 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1292 goto switchstate;
1293 }
1294 memcpy(&update, msg_cur, sizeof(update));
1295 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001296
Emeric Brunb3971ab2015-05-12 18:49:09 +02001297 for (st = curpeer->tables; st; st = st->next) {
1298 if (st->local_id == table_id) {
1299 st->update = update;
1300 break;
1301 }
1302 }
1303 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001304 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001305 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1306 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001307 goto switchstate;
1308 }
1309
Emeric Brunb3971ab2015-05-12 18:49:09 +02001310ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001311 /* skip consumed message */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001312 bo_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001313 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001314 goto switchstate;
1315
Emeric Brun2b920a12010-09-23 18:30:22 +02001316incomplete:
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001317 /* we get here when a bo_getblk() returns <= 0 in reql */
1318
Willy Tarreau72d6c162013-04-11 16:14:13 +02001319 if (reql < 0) {
1320 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001321 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001322 goto switchstate;
1323 }
1324
Emeric Brun2b920a12010-09-23 18:30:22 +02001325
Emeric Brun2b920a12010-09-23 18:30:22 +02001326
Emeric Brunb3971ab2015-05-12 18:49:09 +02001327
Emeric Brun2b920a12010-09-23 18:30:22 +02001328 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001329 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1330 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1331 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1332 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001333
Emeric Brunb3971ab2015-05-12 18:49:09 +02001334 /* Current peer was elected to request a resync */
1335 msg[0] = PEER_MSG_CLASS_CONTROL;
1336 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001337
Emeric Brunb3971ab2015-05-12 18:49:09 +02001338 /* message to buffer */
1339 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1340 if (repl <= 0) {
1341 /* no more write possible */
1342 if (repl == -1)
1343 goto full;
1344 appctx->st0 = PEER_SESS_ST_END;
1345 goto switchstate;
1346 }
1347 peers->flags |= PEERS_F_RESYNC_PROCESS;
1348 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001349
Emeric Brunb3971ab2015-05-12 18:49:09 +02001350 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001351
Emeric Brunb3971ab2015-05-12 18:49:09 +02001352 if (curpeer->tables) {
1353 struct shared_table *st;
1354 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001355
Emeric Brunb3971ab2015-05-12 18:49:09 +02001356 last_local_table = curpeer->last_local_table;
1357 if (!last_local_table)
1358 last_local_table = curpeer->tables;
1359 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001360
Emeric Brunb3971ab2015-05-12 18:49:09 +02001361 while (1) {
1362 if (!st)
1363 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001364
Emeric Brunb3971ab2015-05-12 18:49:09 +02001365 /* It remains some updates to ack */
1366 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001367 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001368
Emeric Brunb3971ab2015-05-12 18:49:09 +02001369 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1370 if (!msglen) {
1371 /* internal error: message does not fit in trash */
1372 appctx->st0 = PEER_SESS_ST_END;
1373 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001374 }
1375
Emeric Brunb3971ab2015-05-12 18:49:09 +02001376 /* message to buffer */
1377 repl = bi_putblk(si_ic(si), trash.str, msglen);
1378 if (repl <= 0) {
1379 /* no more write possible */
1380 if (repl == -1) {
1381 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001382 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001383 appctx->st0 = PEER_SESS_ST_END;
1384 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001385 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001386 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001387 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001388
Emeric Brunb3971ab2015-05-12 18:49:09 +02001389 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1390 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1391 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1392 struct eb32_node *eb;
1393 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001394
Emeric Brunb3971ab2015-05-12 18:49:09 +02001395 if (st != curpeer->last_local_table) {
1396 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001397
Emeric Brunb3971ab2015-05-12 18:49:09 +02001398 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1399 if (!msglen) {
1400 /* internal error: message does not fit in trash */
1401 appctx->st0 = PEER_SESS_ST_END;
1402 goto switchstate;
1403 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001404
Emeric Brunb3971ab2015-05-12 18:49:09 +02001405 /* message to buffer */
1406 repl = bi_putblk(si_ic(si), trash.str, msglen);
1407 if (repl <= 0) {
1408 /* no more write possible */
1409 if (repl == -1) {
1410 goto full;
1411 }
1412 appctx->st0 = PEER_SESS_ST_END;
1413 goto switchstate;
1414 }
1415 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001416 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001417
1418 /* We force new pushed to 1 to force identifier in update message */
1419 new_pushed = 1;
1420 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1421 while (1) {
1422 uint32_t msglen;
1423 struct stksess *ts;
1424
1425 /* push local updates */
1426 if (!eb) {
1427 eb = eb32_first(&st->table->updates);
1428 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001429 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001430 break;
1431 }
1432 }
1433
1434 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001435 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001436 break;
1437 }
1438
1439 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001440 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001441 if (!msglen) {
1442 /* internal error: message does not fit in trash */
1443 appctx->st0 = PEER_SESS_ST_END;
1444 goto switchstate;
1445 }
1446
1447 /* message to buffer */
1448 repl = bi_putblk(si_ic(si), trash.str, msglen);
1449 if (repl <= 0) {
1450 /* no more write possible */
1451 if (repl == -1) {
1452 goto full;
1453 }
1454 appctx->st0 = PEER_SESS_ST_END;
1455 goto switchstate;
1456 }
1457 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001458 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1459 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001460 /* identifier may not needed in next update message */
1461 new_pushed = 0;
1462
1463 eb = eb32_next(eb);
1464 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001465 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001466 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001467 else {
1468 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1469 struct eb32_node *eb;
1470 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001471
Emeric Brunb3971ab2015-05-12 18:49:09 +02001472 if (st != curpeer->last_local_table) {
1473 int msglen;
1474
1475 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1476 if (!msglen) {
1477 /* internal error: message does not fit in trash */
1478 appctx->st0 = PEER_SESS_ST_END;
1479 goto switchstate;
1480 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001481
Emeric Brunb3971ab2015-05-12 18:49:09 +02001482 /* message to buffer */
1483 repl = bi_putblk(si_ic(si), trash.str, msglen);
1484 if (repl <= 0) {
1485 /* no more write possible */
1486 if (repl == -1) {
1487 goto full;
1488 }
1489 appctx->st0 = PEER_SESS_ST_END;
1490 goto switchstate;
1491 }
1492 curpeer->last_local_table = st;
1493 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001494
Emeric Brunb3971ab2015-05-12 18:49:09 +02001495 /* We force new pushed to 1 to force identifier in update message */
1496 new_pushed = 1;
1497 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1498 while (1) {
1499 uint32_t msglen;
1500 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001501 int use_timed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001502
Emeric Brunb3971ab2015-05-12 18:49:09 +02001503 /* push local updates */
1504 if (!eb) {
1505 st->flags |= SHTABLE_F_TEACH_STAGE1;
1506 eb = eb32_first(&st->table->updates);
1507 if (eb)
1508 st->last_pushed = eb->key - 1;
1509 break;
1510 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001511
Emeric Brunb3971ab2015-05-12 18:49:09 +02001512 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001513 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1514 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001515 if (!msglen) {
1516 /* internal error: message does not fit in trash */
1517 appctx->st0 = PEER_SESS_ST_END;
1518 goto switchstate;
1519 }
1520
1521 /* message to buffer */
1522 repl = bi_putblk(si_ic(si), trash.str, msglen);
1523 if (repl <= 0) {
1524 /* no more write possible */
1525 if (repl == -1) {
1526 goto full;
1527 }
1528 appctx->st0 = PEER_SESS_ST_END;
1529 goto switchstate;
1530 }
1531 st->last_pushed = ts->upd.key;
1532 /* identifier may not needed in next update message */
1533 new_pushed = 0;
1534
1535 eb = eb32_next(eb);
1536 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001537 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001538
Emeric Brunb3971ab2015-05-12 18:49:09 +02001539 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1540 struct eb32_node *eb;
1541 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001542
Emeric Brunb3971ab2015-05-12 18:49:09 +02001543 if (st != curpeer->last_local_table) {
1544 int msglen;
1545
1546 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1547 if (!msglen) {
1548 /* internal error: message does not fit in trash */
1549 appctx->st0 = PEER_SESS_ST_END;
1550 goto switchstate;
1551 }
1552
1553 /* message to buffer */
1554 repl = bi_putblk(si_ic(si), trash.str, msglen);
1555 if (repl <= 0) {
1556 /* no more write possible */
1557 if (repl == -1) {
1558 goto full;
1559 }
1560 appctx->st0 = PEER_SESS_ST_END;
1561 goto switchstate;
1562 }
1563 curpeer->last_local_table = st;
1564 }
1565
1566 /* We force new pushed to 1 to force identifier in update message */
1567 new_pushed = 1;
1568 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1569 while (1) {
1570 uint32_t msglen;
1571 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001572 int use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001573
1574 /* push local updates */
1575 if (!eb || eb->key > st->teaching_origin) {
1576 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001577 break;
1578 }
1579
1580 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001581 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1582 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001583 if (!msglen) {
1584 /* internal error: message does not fit in trash */
1585 appctx->st0 = PEER_SESS_ST_END;
1586 goto switchstate;
1587 }
1588
1589 /* message to buffer */
1590 repl = bi_putblk(si_ic(si), trash.str, msglen);
1591 if (repl <= 0) {
1592 /* no more write possible */
1593 if (repl == -1) {
1594 goto full;
1595 }
1596 appctx->st0 = PEER_SESS_ST_END;
1597 goto switchstate;
1598 }
1599 st->last_pushed = ts->upd.key;
1600 /* identifier may not needed in next update message */
1601 new_pushed = 0;
1602
1603 eb = eb32_next(eb);
1604 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001605 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001606 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001607
1608 if (st == last_local_table)
1609 break;
1610 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001611 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001612 }
1613
1614
1615 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1616 unsigned char msg[2];
1617
1618 /* Current peer was elected to request a resync */
1619 msg[0] = PEER_MSG_CLASS_CONTROL;
1620 msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1621 /* process final lesson message */
1622 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1623 if (repl <= 0) {
1624 /* no more write possible */
1625 if (repl == -1)
1626 goto full;
1627 appctx->st0 = PEER_SESS_ST_END;
1628 goto switchstate;
1629 }
1630 /* flag finished message sent */
1631 curpeer->flags |= PEER_F_TEACH_FINISHED;
1632 }
1633
Emeric Brun597b26e2016-08-12 11:23:31 +02001634 /* Confirm finished or partial messages */
1635 while (curpeer->confirm) {
1636 unsigned char msg[2];
1637
1638 /* There is a confirm messages to send */
1639 msg[0] = PEER_MSG_CLASS_CONTROL;
1640 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1641
1642 /* message to buffer */
1643 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1644 if (repl <= 0) {
1645 /* no more write possible */
1646 if (repl == -1)
1647 goto full;
1648 appctx->st0 = PEER_SESS_ST_END;
1649 goto switchstate;
1650 }
1651 curpeer->confirm--;
1652 }
1653
Emeric Brun2b920a12010-09-23 18:30:22 +02001654 /* noting more to do */
1655 goto out;
1656 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001657 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001658 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001659 if (bi_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001660 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001661 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001662 goto switchstate;
1663 case PEER_SESS_ST_ERRSIZE: {
1664 unsigned char msg[2];
1665
1666 msg[0] = PEER_MSG_CLASS_ERROR;
1667 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1668
1669 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1670 goto full;
1671 appctx->st0 = PEER_SESS_ST_END;
1672 goto switchstate;
1673 }
1674 case PEER_SESS_ST_ERRPROTO: {
1675 unsigned char msg[2];
1676
1677 msg[0] = PEER_MSG_CLASS_ERROR;
1678 msg[1] = PEER_MSG_ERR_PROTOCOL;
1679
1680 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1681 goto full;
1682 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001683 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001684 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001685 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001686 si_shutw(si);
1687 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001688 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001689 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001690 }
1691 }
1692 }
1693out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001694 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001695 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001696full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001697 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001698 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001699}
1700
Willy Tarreau30576452015-04-13 13:50:30 +02001701static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001702 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001703 .name = "<PEER>", /* used for logging */
1704 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001705 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001706};
Emeric Brun2b920a12010-09-23 18:30:22 +02001707
1708/*
1709 * Use this function to force a close of a peer session
1710 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001711static void peer_session_forceshutdown(struct stream * stream)
Emeric Brun2b920a12010-09-23 18:30:22 +02001712{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001713 struct appctx *appctx = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001714 struct peer *ps;
1715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001716 int i;
Emeric Brun2b920a12010-09-23 18:30:22 +02001717
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001718 for (i = 0; i <= 1; i++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001719 appctx = objt_appctx(stream->si[i].end);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001720 if (!appctx)
1721 continue;
1722 if (appctx->applet != &peer_applet)
1723 continue;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001724 break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001725 }
1726
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001727 if (!appctx)
1728 return;
1729
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001730 ps = appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001731 /* we're killing a connection, we must apply a random delay before
1732 * retrying otherwise the other end will do the same and we can loop
1733 * for a while.
1734 */
1735 if (ps)
1736 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1737
Emeric Brun2b920a12010-09-23 18:30:22 +02001738 /* call release to reinit resync states if needed */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001739 peer_session_release(appctx);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001740 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001741 appctx->ctx.peers.ptr = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001742 task_wakeup(stream->task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001743}
1744
Willy Tarreau91d96282015-03-13 15:47:26 +01001745/* Pre-configures a peers frontend to accept incoming connections */
1746void peers_setup_frontend(struct proxy *fe)
1747{
1748 fe->last_change = now.tv_sec;
1749 fe->cap = PR_CAP_FE;
1750 fe->maxconn = 0;
1751 fe->conn_retries = CONN_RETRIES;
1752 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001753 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001754 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001755 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001756 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001757}
1758
Emeric Brun2b920a12010-09-23 18:30:22 +02001759/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001760 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001761 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001762static struct stream *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001763{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001764 struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001765 struct proxy *p = l->frontend; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001766 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001767 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001769 struct task *t;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001770 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001771
Emeric Brunb3971ab2015-05-12 18:49:09 +02001772 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1773 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001774 s = NULL;
1775
1776 appctx = appctx_new(&peer_applet);
1777 if (!appctx)
1778 goto out_close;
1779
1780 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001782
Willy Tarreau4099a7c2015-04-05 00:39:55 +02001783 sess = session_new(p, l, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001784 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001785 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001786 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001787 }
1788
Willy Tarreau8baf9062015-04-05 00:46:36 +02001789 if ((t = task_new()) == NULL) {
Willy Tarreau15b5e142015-04-04 14:38:25 +02001790 Alert("out of memory in peer_session_create().\n");
1791 goto out_free_sess;
1792 }
Willy Tarreau8baf9062015-04-05 00:46:36 +02001793 t->nice = l->nice;
1794
Willy Tarreau73b65ac2015-04-08 18:26:29 +02001795 if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001796 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau8baf9062015-04-05 00:46:36 +02001797 goto out_free_task;
1798 }
1799
Willy Tarreau342bfb12015-04-05 01:35:34 +02001800 /* The tasks below are normally what is supposed to be done by
1801 * fe->accept().
1802 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001803 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001804
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001805 /* applet is waiting for data */
1806 si_applet_cant_get(&s->si[0]);
1807 appctx_wakeup(appctx);
1808
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001809 /* initiate an outgoing connection */
1810 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001811
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001812 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001813 * pre-initialized connection in si->conn.
1814 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001815 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001816 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001817
1818 conn_prepare(conn, peer->proto, peer->xprt);
1819 si_attach_conn(&s->si[1], conn);
1820
1821 conn->target = s->target = &s->be->obj_type;
1822 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001823 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001824 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001825
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001826 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001827
Emeric Brun2b920a12010-09-23 18:30:22 +02001828 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1829 p->feconn++;/* beconn will be increased later */
1830 jobs++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001831 if (!(s->sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001832 actconn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001833 totalconn++;
1834
Emeric Brunb3971ab2015-05-12 18:49:09 +02001835 peer->appctx = appctx;
1836 peer->stream = s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001837 return s;
1838
1839 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001840 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001841 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001842 pool_free2(pool2_stream, s);
Willy Tarreau8baf9062015-04-05 00:46:36 +02001843 out_free_task:
1844 task_free(t);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001845 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001846 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001847 out_free_appctx:
1848 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001849 out_close:
1850 return s;
1851}
1852
1853/*
1854 * Task processing function to manage re-connect and peer session
1855 * tasks wakeup on local update.
1856 */
Simon Horman96553772011-06-08 09:18:51 +09001857static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001858{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001859 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001860 struct peer *ps;
1861 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001862
1863 task->expire = TICK_ETERNITY;
1864
Emeric Brunb3971ab2015-05-12 18:49:09 +02001865 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001866 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001867 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001868 task_delete(peers->sync_task);
1869 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001870 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001871 return NULL;
1872 }
1873
Emeric Brun2b920a12010-09-23 18:30:22 +02001874 if (!stopping) {
1875 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001876
1877 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1878 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1879 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001880 /* Resync from local peer needed
1881 no peer was assigned for the lesson
1882 and no old local peer found
1883 or resync timeout expire */
1884
1885 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001886 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001887
1888 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001889 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001890 }
1891
1892 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001893 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001894 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001895 if (!ps->local) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001896 if (!ps->stream) {
1897 /* no active stream */
Emeric Brun2b920a12010-09-23 18:30:22 +02001898 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001899 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001900 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001901 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001902 tick_is_expired(ps->reconnect, now_ms))) {
1903 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02001904 * or previous stream established with success
1905 * or previous stream failed during connection
Emeric Brun2b920a12010-09-23 18:30:22 +02001906 * and reconnection timer is expired */
1907
1908 /* retry a connect */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001909 ps->stream = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001910 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001911 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001912 /* If previous session failed during connection
1913 * but reconnection timer is not expired */
1914
1915 /* reschedule task for reconnect */
1916 task->expire = tick_first(task->expire, ps->reconnect);
1917 }
1918 /* else do nothing */
Willy Tarreau87b09662015-04-03 00:22:06 +02001919 } /* !ps->stream */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001920 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001921 /* current stream is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001922 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1923 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001924 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1925 /* Resync from a remote is needed
1926 * and no peer was assigned for lesson
1927 * and current peer may be up2date */
1928
1929 /* assign peer for the lesson */
1930 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001931 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001932
Willy Tarreau87b09662015-04-03 00:22:06 +02001933 /* awake peer stream task to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001934 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001935 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001936 else {
1937 /* Awake session if there is data to push */
1938 for (st = ps->tables; st ; st = st->next) {
1939 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
1940 /* awake peer stream task to push local updates */
1941 appctx_wakeup(ps->appctx);
1942 break;
1943 }
1944 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001945 }
1946 /* else do nothing */
1947 } /* SUCCESSCODE */
1948 } /* !ps->peer->local */
1949 } /* for */
1950
1951 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001952 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1953 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1954 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001955 /* Resync from remote peer needed
1956 * no peer was assigned for the lesson
1957 * and resync timeout expire */
1958
1959 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001960 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001961 }
1962
Emeric Brunb3971ab2015-05-12 18:49:09 +02001963 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001964 /* Resync not finished*/
1965 /* reschedule task to resync timeout, to ended resync if needed */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001966 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001967 }
1968 } /* !stopping */
1969 else {
1970 /* soft stop case */
1971 if (task->state & TASK_WOKEN_SIGNAL) {
1972 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001973 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001974 /* add DO NOT STOP flag if not present */
1975 jobs++;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001976 peers->flags |= PEERS_F_DONOTSTOP;
1977 ps = peers->local;
1978 for (st = ps->tables; st ; st = st->next)
1979 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001980 }
1981
1982 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001983 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001984 if (ps->stream) {
1985 peer_session_forceshutdown(ps->stream);
1986 ps->stream = NULL;
Willy Tarreaue5843b32015-04-27 18:40:14 +02001987 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001988 }
1989 }
1990 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001991
Emeric Brunb3971ab2015-05-12 18:49:09 +02001992 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001993 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001994 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001995 /* resync of new process was complete, current process can die now */
1996 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001997 peers->flags &= ~PEERS_F_DONOTSTOP;
1998 for (st = ps->tables; st ; st = st->next)
1999 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002000 }
2001 }
Willy Tarreau87b09662015-04-03 00:22:06 +02002002 else if (!ps->stream) {
2003 /* If stream is not active */
Emeric Brun2b920a12010-09-23 18:30:22 +02002004 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002005 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2006 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2007 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002008 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02002009 * or previous stream was successfully established
2010 * or previous stream tcp connect success but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002011 * or during previous connect, peer replies a try again statuscode */
2012
2013 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002014 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002015 }
2016 else {
2017 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002018 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002019 /* unable to resync new process, current process can die now */
2020 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002021 peers->flags &= ~PEERS_F_DONOTSTOP;
2022 for (st = ps->tables; st ; st = st->next)
2023 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002024 }
2025 }
2026 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002027 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002028 /* current stream active and established
2029 awake stream to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002030 for (st = ps->tables; st ; st = st->next) {
2031 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
2032 /* awake peer stream task to push local updates */
2033 appctx_wakeup(ps->appctx);
2034 break;
2035 }
2036 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 }
2038 } /* stopping */
2039 /* Wakeup for re-connect */
2040 return task;
2041}
2042
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043
Emeric Brun2b920a12010-09-23 18:30:22 +02002044/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002045 *
2046 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002047void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002048{
Emeric Brun2b920a12010-09-23 18:30:22 +02002049 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002050 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002051
Emeric Brun2b920a12010-09-23 18:30:22 +02002052 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 peers->peers_fe->maxconn += 3;
2054 }
2055
Willy Tarreau4348fad2012-09-20 16:48:07 +02002056 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2057 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002058 peers->sync_task = task_new();
2059 peers->sync_task->process = process_peer_sync;
2060 peers->sync_task->expire = TICK_ETERNITY;
2061 peers->sync_task->context = (void *)peers;
2062 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2063 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2064}
2065
2066
2067
2068/*
2069 * Function used to register a table for sync on a group of peers
2070 *
2071 */
2072void peers_register_table(struct peers *peers, struct stktable *table)
2073{
2074 struct shared_table *st;
2075 struct peer * curpeer;
2076 int id = 0;
2077
2078 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002079 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002080 st->table = table;
2081 st->next = curpeer->tables;
2082 if (curpeer->tables)
2083 id = curpeer->tables->local_id;
2084 st->local_id = id + 1;
2085
2086 curpeer->tables = st;
2087 }
2088
2089 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002090}
2091