blob: d05d653469a0c7e3aa01cdf9b01cffa550143bec [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>
26
27#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010028#include <types/listener.h>
29#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020030#include <types/peers.h>
31
32#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020033#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020035#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010036#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/log.h>
38#include <proto/hdr_idx.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/proto_tcp.h>
40#include <proto/proto_http.h>
41#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020042#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010044#include <proto/signal.h>
45#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020047#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048
49
50/*******************************/
51/* Current peer learning state */
52/*******************************/
53
54/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020055/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020056/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
58#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
59#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
60#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
61#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020062 to push data to new process */
63
Emeric Brunb3971ab2015-05-12 18:49:09 +020064#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
65#define PEERS_RESYNC_FROMLOCAL 0x00000000
66#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
67#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
68
69/***********************************/
70/* Current shared table sync state */
71/***********************************/
72#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
73#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020074
75/******************************/
76/* Remote peer teaching state */
77/******************************/
78#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020079#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
80#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
81#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
82#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
83
Emeric Brunb3971ab2015-05-12 18:49:09 +020084#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 +020085#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
86
Emeric Brunb3971ab2015-05-12 18:49:09 +020087/*****************************/
88/* Sync message class */
89/*****************************/
90enum {
91 PEER_MSG_CLASS_CONTROL = 0,
92 PEER_MSG_CLASS_ERROR,
93 PEER_MSG_CLASS_STICKTABLE = 10,
94 PEER_MSG_CLASS_RESERVED = 255,
95};
96
97/*****************************/
98/* control message types */
99/*****************************/
100enum {
101 PEER_MSG_CTRL_RESYNCREQ = 0,
102 PEER_MSG_CTRL_RESYNCFINISHED,
103 PEER_MSG_CTRL_RESYNCPARTIAL,
104 PEER_MSG_CTRL_RESYNCCONFIRM,
105};
106
107/*****************************/
108/* error message types */
109/*****************************/
110enum {
111 PEER_MSG_ERR_PROTOCOL = 0,
112 PEER_MSG_ERR_SIZELIMIT,
113};
114
115
116/*******************************/
117/* stick table sync mesg types */
118/* Note: ids >= 128 contains */
119/* id message cotains data */
120/*******************************/
121enum {
122 PEER_MSG_STKT_UPDATE = 128,
123 PEER_MSG_STKT_INCUPDATE,
124 PEER_MSG_STKT_DEFINE,
125 PEER_MSG_STKT_SWITCH,
126 PEER_MSG_STKT_ACK,
127};
Emeric Brun2b920a12010-09-23 18:30:22 +0200128
129/**********************************/
130/* Peer Session IO handler states */
131/**********************************/
132
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100133enum {
134 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
135 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
136 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
137 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100138 /* after this point, data were possibly exchanged */
139 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
140 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
141 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
142 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
143 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200144 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
145 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100146 PEER_SESS_ST_END, /* Killed session */
147};
Emeric Brun2b920a12010-09-23 18:30:22 +0200148
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149/***************************************************/
150/* Peer Session status code - part of the protocol */
151/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200152
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100153#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
154#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200157
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100158#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200159
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100160#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
161#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
162#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
163#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200164
165#define PEER_SESSION_PROTO_NAME "HAProxyS"
166
167struct peers *peers = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +0200168static void peer_session_forceshutdown(struct stream * stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200169
Emeric Brunb3971ab2015-05-12 18:49:09 +0200170int intencode(uint64_t i, char **str) {
171 int idx = 0;
172 unsigned char *msg;
173
174 if (!*str)
175 return 0;
176
177 msg = (unsigned char *)*str;
178 if (i < 240) {
179 msg[0] = (unsigned char)i;
180 *str = (char *)&msg[idx+1];
181 return (idx+1);
182 }
183
184 msg[idx] =(unsigned char)i | 240;
185 i = (i - 240) >> 4;
186 while (i >= 128) {
187 msg[++idx] = (unsigned char)i | 128;
188 i = (i - 128) >> 7;
189 }
190 msg[++idx] = (unsigned char)i;
191 *str = (char *)&msg[idx+1];
192 return (idx+1);
193}
194
195
196/* This function returns the decoded integer or 0
197 if decode failed
198 *str point on the beginning of the integer to decode
199 at the end of decoding *str point on the end of the
200 encoded integer or to null if end is reached */
201uint64_t intdecode(char **str, char *end) {
202 uint64_t i;
203 int idx = 0;
204 unsigned char *msg;
205
206 if (!*str)
207 return 0;
208
209 msg = (unsigned char *)*str;
210 if (msg >= (unsigned char *)end) {
211 *str = NULL;
212 return 0;
213 }
214
215 if (msg[idx] < 240) {
216 *str = (char *)&msg[idx+1];
217 return msg[idx];
218 }
219 i = msg[idx];
220 do {
221 idx++;
222 if (msg >= (unsigned char *)end) {
223 *str = NULL;
224 return 0;
225 }
226 i += (uint64_t)msg[idx] << (4 + 7*(idx-1));
227 }
228 while (msg[idx] > 128);
229 *str = (char *)&msg[idx+1];
230 return i;
231}
Emeric Brun2b920a12010-09-23 18:30:22 +0200232
233/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200234 * This prepare the data update message on the stick session <ts>, <st> is the considered
235 * stick table.
236 * <msg> is a buffer of <size> to recieve data message content
237 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
238 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200239 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200240static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier)
Emeric Brun2b920a12010-09-23 18:30:22 +0200241{
242 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200243 unsigned short datalen;
244 char *cursor, *datamsg;
245
246 cursor = datamsg = msg + 1 + 5;
247
Emeric Brun2b920a12010-09-23 18:30:22 +0200248 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200249
250 /* check if we need to send the update identifer */
251 if (st->last_pushed && ts->upd.key > st->last_pushed && (ts->upd.key - st->last_pushed) == 1) {
252 use_identifier = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200253 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200254
255 /* encode update identifier if needed */
256 if (use_identifier) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200257 netinteger = htonl(ts->upd.key);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200258 memcpy(cursor, &netinteger, sizeof(netinteger));
259 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200260 }
261
Emeric Brunb3971ab2015-05-12 18:49:09 +0200262 /* encode the key */
263 if (st->table->type == STKTABLE_TYPE_STRING) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200264 int stlen = strlen((char *)ts->key.key);
265
Emeric Brunb3971ab2015-05-12 18:49:09 +0200266 intencode(stlen, &cursor);
267 memcpy(cursor, ts->key.key, stlen);
268 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200269 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200270 else if (st->table->type == STKTABLE_TYPE_INTEGER) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200271 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200272 memcpy(cursor, &netinteger, sizeof(netinteger));
273 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200274 }
275 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276 memcpy(cursor, ts->key.key, st->table->key_size);
277 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200278 }
279
Emeric Brunb3971ab2015-05-12 18:49:09 +0200280 /* encode values */
281 if (stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID)) {
282 int srvid;
283
284 srvid = stktable_data_cast(stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID), server_id);
285 intencode(srvid, &cursor);
286 }
287
288 /* Compute datalen */
289 datalen = (cursor - datamsg);
290
291 /* prepare message header */
292 msg[0] = PEER_MSG_CLASS_STICKTABLE;
293 if (use_identifier)
294 msg[1] = PEER_MSG_STKT_UPDATE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200295 else
Emeric Brunb3971ab2015-05-12 18:49:09 +0200296 msg[1] = PEER_MSG_STKT_INCUPDATE;
297
298 cursor = &msg[2];
299 intencode(datalen, &cursor);
300
301 /* move data after header */
302 memmove(cursor, datamsg, datalen);
303
304 /* return header size + data_len */
305 return (cursor - msg) + datalen;
306}
307
308/*
309 * This prepare the switch table message to targeted share table <st>.
310 * <msg> is a buffer of <size> to recieve data message content
311 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
312 * check size)
313 */
314static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
315{
316 int len;
317 unsigned short datalen;
318 char *cursor, *datamsg;
319 uint64_t data = 0;
320
321 cursor = datamsg = msg + 2 + 5;
322
323 /* Encode data */
324
325 /* encode local id */
326 intencode(st->local_id, &cursor);
327
328 /* encode table name */
329 len = strlen(st->table->id);
330 intencode(len, &cursor);
331 memcpy(cursor, st->table->id, len);
332 cursor += len;
333
334 /* encode table type */
335
336 intencode(st->table->type, &cursor);
337
338 /* encode table key size */
339 intencode(st->table->key_size, &cursor);
340
341 /* encode available data types in table */
342 if (st->table->data_ofs[STKTABLE_DT_SERVER_ID]) {
343 data |= 1 << STKTABLE_DT_SERVER_ID;
344 }
345 intencode(data, &cursor);
346
347 /* Compute datalen */
348 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200349
Emeric Brunb3971ab2015-05-12 18:49:09 +0200350 /* prepare message header */
351 msg[0] = PEER_MSG_CLASS_STICKTABLE;
352 msg[1] = PEER_MSG_STKT_DEFINE;
353 cursor = &msg[2];
354 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200355
Emeric Brunb3971ab2015-05-12 18:49:09 +0200356 /* move data after header */
357 memmove(cursor, datamsg, datalen);
358
359 /* return header size + data_len */
360 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200361}
362
Emeric Brunb3971ab2015-05-12 18:49:09 +0200363/*
364 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
365 * stick table.
366 * <msg> is a buffer of <size> to recieve data message content
367 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
368 * check size)
369 */
370static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
371{
372 unsigned short datalen;
373 char *cursor, *datamsg;
374 uint32_t netinteger;
375
376 cursor = datamsg = trash.str + 2 + 5;
377
378 intencode(st->remote_id, &cursor);
379 netinteger = htonl(st->last_get);
380 memcpy(cursor, &netinteger, sizeof(netinteger));
381 cursor += sizeof(netinteger);
382
383 /* Compute datalen */
384 datalen = (cursor - datamsg);
385
386 /* prepare message header */
387 msg[0] = PEER_MSG_CLASS_STICKTABLE;
388 msg[1] = PEER_MSG_STKT_DEFINE;
389 cursor = &msg[2];
390 intencode(datalen, &cursor);
391
392 /* move data after header */
393 memmove(cursor, datamsg, datalen);
394
395 /* return header size + data_len */
396 return (cursor - msg) + datalen;
397}
Emeric Brun2b920a12010-09-23 18:30:22 +0200398
399/*
400 * Callback to release a session with a peer
401 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200402static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200403{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200404 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200405 struct stream *s = si_strm(si);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200406 struct peer *peer = (struct peer *)appctx->ctx.peers.ptr;
407 struct peers *peers = (struct peers *)strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200408
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100409 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100410 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200411 return;
412
413 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200414 if (peer) {
415 if (peer->stream == s) {
416 peer->stream = NULL;
417 peer->appctx = NULL;
418 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200419 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200420 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
421 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200422
423 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200424 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200425 }
426 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200427 peer->flags &= PEER_TEACH_RESET;
428 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200429 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200430 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200431 }
432}
433
434
435/*
436 * IO Handler to handle message exchance with a peer
437 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200438static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200439{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200440 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200441 struct stream *s = si_strm(si);
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200442 struct peers *curpeers = (struct peers *)strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200443 int reql = 0;
444 int repl = 0;
445
446 while (1) {
447switchstate:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100448 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100449 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100450 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100451 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200452 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100453 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100454 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200455 if (reql <= 0) { /* closed or EOL not found */
456 if (reql == 0)
457 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100458 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200459 goto switchstate;
460 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100461 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100462 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200463 goto switchstate;
464 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100465 else if (reql > 1 && (trash.str[reql-2] == '\r'))
466 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200467 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100468 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200469
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100470 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200471
472 /* test version */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200473 if (strcmp(PEER_SESSION_PROTO_NAME " 2.0", trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100474 appctx->st0 = PEER_SESS_ST_EXIT;
475 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200476 /* test protocol */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100477 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, strlen(PEER_SESSION_PROTO_NAME)+1) != 0)
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100478 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200479 goto switchstate;
480 }
481
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100482 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200483 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100484 case PEER_SESS_ST_GETHOST:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100485 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200486 if (reql <= 0) { /* closed or EOL not found */
487 if (reql == 0)
488 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100489 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200490 goto switchstate;
491 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100492 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100493 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200494 goto switchstate;
495 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100496 else if (reql > 1 && (trash.str[reql-2] == '\r'))
497 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200498 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100499 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200500
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100501 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200502
503 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100504 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100505 appctx->st0 = PEER_SESS_ST_EXIT;
506 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200507 goto switchstate;
508 }
509
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100510 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200511 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100512 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200513 struct peer *curpeer;
514 char *p;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100515 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200516 if (reql <= 0) { /* closed or EOL not found */
517 if (reql == 0)
518 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100519 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200520 goto switchstate;
521 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100522 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200523 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100524 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 goto switchstate;
526 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100527 else if (reql > 1 && (trash.str[reql-2] == '\r'))
528 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200529 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100530 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200531
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100532 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200533
Emeric Brunb3971ab2015-05-12 18:49:09 +0200534 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100535 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200536 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100537 appctx->st0 = PEER_SESS_ST_EXIT;
538 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200539 goto switchstate;
540 }
541 *p = 0;
542
543 /* lookup known peer */
544 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100545 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200546 break;
547 }
548
549 /* if unknown peer */
550 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100551 appctx->st0 = PEER_SESS_ST_EXIT;
552 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200553 goto switchstate;
554 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200555
Emeric Brunb3971ab2015-05-12 18:49:09 +0200556 if (curpeer->stream && curpeer->stream != s) {
557 if (curpeer->local) {
558 /* Local connection, reply a retry */
559 appctx->st0 = PEER_SESS_ST_EXIT;
560 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
561 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200562 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200563 peer_session_forceshutdown(curpeer->stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200564 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200565 curpeer->stream = s;
566 curpeer->appctx = appctx;
567 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100568 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200569 /* fall through */
570 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100571 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
573 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200574
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100575 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100576 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200577 if (repl <= 0) {
578 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100579 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100580 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200581 goto switchstate;
582 }
583
584 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200585 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200586
587 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200588 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200589
590 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200591 curpeer->confirm = 0;
592
593 /* Init cursors */
594 for (st = curpeer->tables; st ; st = st->next) {
595 st->last_get = st->last_acked = 0;
596 st->teaching_origin = st->last_pushed = st->update;
597 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200598
599 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200600 curpeer->flags &= PEER_TEACH_RESET;
601 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602
603 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200604 if (curpeer->local) {
605 /* if current host need resyncfrom local and no process assined */
606 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
607 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
608 /* assign local peer for a lesson, consider lesson already requested */
609 curpeer->flags |= PEER_F_LEARN_ASSIGN;
610 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
611 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200612
Emeric Brunb3971ab2015-05-12 18:49:09 +0200613 }
614 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
615 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
616 /* assign peer for a lesson */
617 curpeer->flags |= PEER_F_LEARN_ASSIGN;
618 peers->flags |= PEERS_F_RESYNC_ASSIGN;
619 }
620
621
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100623 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200624 goto switchstate;
625 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100626 case PEER_SESS_ST_CONNECT: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200627 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200628
629 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100630 repl = snprintf(trash.str, trash.size,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200631 PEER_SESSION_PROTO_NAME " 2.0\n%s\n%s %d %d\n",
632 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200633 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100634 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200635 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200636
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100637 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100638 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200639 goto switchstate;
640 }
641
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100642 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200643 if (repl <= 0) {
644 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100645 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100646 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 goto switchstate;
648 }
649
650 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100651 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 /* fall through */
653 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100654 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200655 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
656 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200657
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100658 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200659 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200660
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100661 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 if (reql <= 0) { /* closed or EOL not found */
663 if (reql == 0)
664 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100665 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200666 goto switchstate;
667 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200669 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100670 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200671 goto switchstate;
672 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100673 else if (reql > 1 && (trash.str[reql-2] == '\r'))
674 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100676 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200677
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100678 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200679
680 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200682
683 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200684 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200685
686 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200687 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200688 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200689 for (st = curpeer->tables; st ; st = st->next) {
690 st->last_get = st->last_acked = 0;
691 st->teaching_origin = st->last_pushed = st->update;
692 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200693
694 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200695 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200696
Emeric Brunb3971ab2015-05-12 18:49:09 +0200697 /* reset teaching and learning flags to 0 */
698 curpeer->flags &= PEER_TEACH_RESET;
699 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200700
Emeric Brunb3971ab2015-05-12 18:49:09 +0200701 /* If current peer is local */
702 if (curpeer->local) {
703 /* flag to start to teach lesson */
704 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200705
Emeric Brunb3971ab2015-05-12 18:49:09 +0200706 }
707 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
708 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
709 /* If peer is remote and resync from remote is needed,
710 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200711
Emeric Brunb3971ab2015-05-12 18:49:09 +0200712 /* assign peer for a lesson */
713 curpeer->flags |= PEER_F_LEARN_ASSIGN;
714 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200715 }
716
717 }
718 else {
719 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100720 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200721 goto switchstate;
722 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100723 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200724 /* fall through */
725 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100726 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200727 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200728 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200729 uint32_t msg_len = 0;
730 char *msg_cur = trash.str;
731 char *msg_end = trash.str;
732 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200733 int totl = 0;
734
Emeric Brunb3971ab2015-05-12 18:49:09 +0200735 reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200736 if (reql <= 0) /* closed or EOL not found */
737 goto incomplete;
738
Emeric Brun2b920a12010-09-23 18:30:22 +0200739 totl += reql;
740
Emeric Brunb3971ab2015-05-12 18:49:09 +0200741 if (msg_head[1] >= 128) {
742 /* Read and Decode message length */
743 reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
744 if (reql <= 0) /* closed */
745 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200746
Emeric Brunb3971ab2015-05-12 18:49:09 +0200747 totl += reql;
748
749 if (msg_head[2] < 240) {
750 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200751 }
752 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200753 int i;
754 char *cur;
755 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200756
Emeric Brunb3971ab2015-05-12 18:49:09 +0200757 for (i = 3 ; i < sizeof(msg_head) ; i++) {
758 reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
759 if (reql <= 0) /* closed */
760 goto incomplete;
761
762 totl += reql;
763
764 if (!(msg_head[i] & 0x80))
765 break;
766 }
767
768 if (i == sizeof(msg_head)) {
769 /* malformed message */
770 appctx->st0 = PEER_SESS_ST_ERRPROTO;
771 goto switchstate;
772
773 }
774 end = (char *)msg_head + sizeof(msg_head);
775 cur = (char *)&msg_head[2];
776 msg_len = intdecode(&cur, end);
777 if (!cur) {
778 /* malformed message */
779 appctx->st0 = PEER_SESS_ST_ERRPROTO;
780 goto switchstate;
781 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200782 }
783
Willy Tarreau86a446e2013-11-25 23:02:37 +0100784
Emeric Brunb3971ab2015-05-12 18:49:09 +0200785 /* Read message content */
786 if (msg_len) {
787 if (msg_len > trash.size) {
788 /* Status code is not success, abort */
789 appctx->st0 = PEER_SESS_ST_ERRSIZE;
790 goto switchstate;
791 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200792
Emeric Brunb3971ab2015-05-12 18:49:09 +0200793 reql = bo_getblk(si_oc(si), trash.str, msg_len, totl);
794 if (reql <= 0) /* closed */
795 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200796 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100797
Emeric Brunb3971ab2015-05-12 18:49:09 +0200798 msg_end += msg_len;
799 }
800 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100801
Emeric Brunb3971ab2015-05-12 18:49:09 +0200802 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
803 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
804 struct shared_table *st;
805 /* Reset message: remote need resync */
806
807 /* prepare tables fot a global push */
808 for (st = curpeer->tables; st; st = st->next) {
809 st->teaching_origin = st->last_pushed = st->table->update;
810 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100811 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200812
Emeric Brunb3971ab2015-05-12 18:49:09 +0200813 /* reset teaching flags to 0 */
814 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200815
Emeric Brunb3971ab2015-05-12 18:49:09 +0200816 /* flag to start to teach lesson */
817 curpeer->flags |= PEER_F_TEACH_PROCESS;
818
819
820 }
821 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
822
823 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
824 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
825 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
826 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100827 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200828 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200829 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200830 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
831
832 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
833 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
834 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
835
836 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
837 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
838 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100839 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200840 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200841 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200842 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
843
844 /* If stopping state */
845 if (stopping) {
846 /* Close session, push resync no more needed */
847 curpeer->flags |= PEER_F_TEACH_COMPLETE;
848 appctx->st0 = PEER_SESS_ST_END;
849 goto switchstate;
850 }
851
852 /* reset teaching flags to 0 */
853 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200854 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200855 }
856 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
857 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
858 int table_id_len;
859 struct shared_table *st;
860 int table_type;
861 int table_keylen;
862 int table_id;
863 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +0200864
Emeric Brunb3971ab2015-05-12 18:49:09 +0200865 table_id = intdecode(&msg_cur, msg_end);
866 if (!msg_cur) {
867 /* malformed message */
868 appctx->st0 = PEER_SESS_ST_ERRPROTO;
869 goto switchstate;
870 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200871
Emeric Brunb3971ab2015-05-12 18:49:09 +0200872 table_id_len = intdecode(&msg_cur, msg_end);
873 if (!msg_cur) {
874 /* malformed message */
875 appctx->st0 = PEER_SESS_ST_ERRPROTO;
876 goto switchstate;
877 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200878
Emeric Brunb3971ab2015-05-12 18:49:09 +0200879 curpeer->remote_table = NULL;
880 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
881 /* malformed message */
882 appctx->st0 = PEER_SESS_ST_ERRPROTO;
883 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200884 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200885
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 for (st = curpeer->tables; st; st = st->next) {
887 /* Reset IDs */
888 if (st->remote_id == table_id)
889 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200890
Emeric Brunb3971ab2015-05-12 18:49:09 +0200891 if (!curpeer->remote_table
892 && (table_id_len == strlen(st->table->id))
893 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
894 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200895 }
896 }
897
Emeric Brunb3971ab2015-05-12 18:49:09 +0200898 if (!curpeer->remote_table) {
899 goto ignore_msg;
900 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200901
Emeric Brunb3971ab2015-05-12 18:49:09 +0200902 msg_cur += table_id_len;
903 if (msg_cur >= msg_end) {
904 /* malformed message */
905 appctx->st0 = PEER_SESS_ST_ERRPROTO;
906 goto switchstate;
907 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200908
Emeric Brunb3971ab2015-05-12 18:49:09 +0200909 table_type = intdecode(&msg_cur, msg_end);
910 if (!msg_cur) {
911 /* malformed message */
912 appctx->st0 = PEER_SESS_ST_ERRPROTO;
913 goto switchstate;
914 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200915
Emeric Brunb3971ab2015-05-12 18:49:09 +0200916 table_keylen = intdecode(&msg_cur, msg_end);
917 if (!msg_cur) {
918 /* malformed message */
919 appctx->st0 = PEER_SESS_ST_ERRPROTO;
920 goto switchstate;
921 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200922
Emeric Brunb3971ab2015-05-12 18:49:09 +0200923 table_data = intdecode(&msg_cur, msg_end);
924 if (!msg_cur) {
925 /* malformed message */
926 appctx->st0 = PEER_SESS_ST_ERRPROTO;
927 goto switchstate;
928 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200929
Emeric Brunb3971ab2015-05-12 18:49:09 +0200930 if (curpeer->remote_table->table->type != table_type
931 || curpeer->remote_table->table->key_size != table_keylen) {
932 curpeer->remote_table = NULL;
933 goto ignore_msg;
934 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200935
Emeric Brunb3971ab2015-05-12 18:49:09 +0200936 curpeer->remote_table->remote_data = table_data;
937 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +0200938 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200939 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
940 struct shared_table *st;
941 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +0200942
Emeric Brunb3971ab2015-05-12 18:49:09 +0200943 table_id = intdecode(&msg_cur, msg_end);
944 if (!msg_cur) {
945 /* malformed message */
946 appctx->st0 = PEER_SESS_ST_ERRPROTO;
947 goto switchstate;
948 }
949 curpeer->remote_table = NULL;
950 for (st = curpeer->tables; st; st = st->next) {
951 if (st->remote_id == table_id) {
952 curpeer->remote_table = st;
953 break;
954 }
955 }
956
Emeric Brun2b920a12010-09-23 18:30:22 +0200957 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200958 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
959 || msg_head[1] == PEER_MSG_STKT_INCUPDATE) {
960 struct shared_table *st = curpeer->remote_table;
961 uint32_t update;
Emeric Brun2b920a12010-09-23 18:30:22 +0200962
Emeric Brunb3971ab2015-05-12 18:49:09 +0200963 /* Here we have data message */
964 if (!st)
965 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +0200966
Emeric Brunb3971ab2015-05-12 18:49:09 +0200967 if (msg_head[1] == PEER_MSG_STKT_UPDATE) {
968 if (msg_len < sizeof(update)) {
969 /* malformed message */
970 appctx->st0 = PEER_SESS_ST_ERRPROTO;
971 goto switchstate;
972 }
973 memcpy(&update, msg_cur, sizeof(update));
974 msg_cur += sizeof(update);
975 st->last_get = htonl(update);
976 }
977 else {
978 st->last_get++;
979 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200980
Emeric Brunb3971ab2015-05-12 18:49:09 +0200981 newts = stksess_new(st->table, NULL);
982 if (!newts)
983 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +0200984
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 if (st->table->type == STKTABLE_TYPE_STRING) {
986 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +0200987
Emeric Brunb3971ab2015-05-12 18:49:09 +0200988 to_read = intdecode(&msg_cur, msg_end);
989 if (!msg_cur) {
990 /* malformed message */
991 stksess_free(st->table, newts);
992 appctx->st0 = PEER_SESS_ST_ERRPROTO;
993 goto switchstate;
994 }
995 to_store = MIN(to_read, st->table->key_size - 1);
996 if (msg_cur + to_store > msg_end) {
997 /* malformed message */
998 stksess_free(st->table, newts);
999 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1000 goto switchstate;
1001 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001002
Emeric Brunb3971ab2015-05-12 18:49:09 +02001003 memcpy(newts->key.key, msg_cur, to_store);
1004 newts->key.key[to_store] = 0;
1005 msg_cur += to_read;
1006 }
1007 else if (st->table->type == STKTABLE_TYPE_INTEGER) {
1008 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001009
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010 if (msg_cur + sizeof(netinteger) > msg_end) {
1011 /* malformed message */
1012 stksess_free(st->table, newts);
1013 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1014 goto switchstate;
1015 }
1016 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1017 netinteger = ntohl(netinteger);
1018 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1019 msg_cur += sizeof(netinteger);
1020 }
1021 else {
1022 if (msg_cur + st->table->key_size > msg_end) {
1023 /* malformed message */
1024 stksess_free(st->table, newts);
1025 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1026 goto switchstate;
1027 }
1028 memcpy(newts->key.key, msg_cur, st->table->key_size);
1029 msg_cur += st->table->key_size;
1030 }
1031
1032 /* lookup for existing entry */
1033 ts = stktable_lookup(st->table, newts);
1034 if (ts) {
1035 /* the entry already exist, we can free ours */
1036 stktable_touch(st->table, ts, 0);
1037 stksess_free(st->table, newts);
1038 newts = NULL;
1039 }
1040 else {
1041 struct eb32_node *eb;
1042
1043 /* create new entry */
1044 ts = stktable_store(st->table, newts, 0);
1045 newts = NULL; /* don't reuse it */
1046
1047 ts->upd.key= (++st->table->update)+(2^31);
1048 eb = eb32_insert(&st->table->updates, &ts->upd);
1049 if (eb != &ts->upd) {
1050 eb32_delete(eb);
1051 eb32_insert(&st->table->updates, &ts->upd);
1052 }
1053 }
1054
1055 if ((1 << STKTABLE_DT_SERVER_ID) & st->remote_data) {
1056 int srvid;
1057
1058 srvid = intdecode(&msg_cur, msg_end);
1059 if (!msg_cur) {
1060 /* malformed message */
1061 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1062 goto switchstate;
1063 }
1064
1065 if (stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID)) {
1066 stktable_data_cast(stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid;
1067 }
1068 }
1069 }
1070 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1071 /* ack message */
1072 uint32_t table_id ;
1073 uint32_t update;
1074 struct shared_table *st;
1075
1076 table_id = intdecode(&msg_cur, msg_end);
1077 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1078 /* malformed message */
1079 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1080 goto switchstate;
1081 }
1082 memcpy(&update, msg_cur, sizeof(update));
1083 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001084
Emeric Brunb3971ab2015-05-12 18:49:09 +02001085 for (st = curpeer->tables; st; st = st->next) {
1086 if (st->local_id == table_id) {
1087 st->update = update;
1088 break;
1089 }
1090 }
1091 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001092 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001093 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1094 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001095 goto switchstate;
1096 }
1097
Emeric Brunb3971ab2015-05-12 18:49:09 +02001098ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001099 /* skip consumed message */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001100 bo_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001101 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001102 goto switchstate;
1103
Emeric Brun2b920a12010-09-23 18:30:22 +02001104incomplete:
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001105 /* we get here when a bo_getblk() returns <= 0 in reql */
1106
Willy Tarreau72d6c162013-04-11 16:14:13 +02001107 if (reql < 0) {
1108 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001109 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001110 goto switchstate;
1111 }
1112
Emeric Brun2b920a12010-09-23 18:30:22 +02001113
1114 /* Confirm finished or partial messages */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001115 while (curpeer->confirm) {
1116 unsigned char msg[2];
1117
Emeric Brun2b920a12010-09-23 18:30:22 +02001118 /* There is a confirm messages to send */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001119 msg[0] = PEER_MSG_CLASS_CONTROL;
1120 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1121
1122 /* message to buffer */
1123 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun2b920a12010-09-23 18:30:22 +02001124 if (repl <= 0) {
1125 /* no more write possible */
1126 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001127 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001128 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001129 goto switchstate;
1130 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001131 curpeer->confirm--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001132 }
1133
Emeric Brunb3971ab2015-05-12 18:49:09 +02001134
Emeric Brun2b920a12010-09-23 18:30:22 +02001135 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001136 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1137 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1138 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1139 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001140
Emeric Brunb3971ab2015-05-12 18:49:09 +02001141 /* Current peer was elected to request a resync */
1142 msg[0] = PEER_MSG_CLASS_CONTROL;
1143 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001144
Emeric Brunb3971ab2015-05-12 18:49:09 +02001145 /* message to buffer */
1146 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1147 if (repl <= 0) {
1148 /* no more write possible */
1149 if (repl == -1)
1150 goto full;
1151 appctx->st0 = PEER_SESS_ST_END;
1152 goto switchstate;
1153 }
1154 peers->flags |= PEERS_F_RESYNC_PROCESS;
1155 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001156
Emeric Brunb3971ab2015-05-12 18:49:09 +02001157 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001158
Emeric Brunb3971ab2015-05-12 18:49:09 +02001159 if (curpeer->tables) {
1160 struct shared_table *st;
1161 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001162
Emeric Brunb3971ab2015-05-12 18:49:09 +02001163 last_local_table = curpeer->last_local_table;
1164 if (!last_local_table)
1165 last_local_table = curpeer->tables;
1166 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001167
Emeric Brunb3971ab2015-05-12 18:49:09 +02001168 while (1) {
1169 if (!st)
1170 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001171
Emeric Brunb3971ab2015-05-12 18:49:09 +02001172 /* It remains some updates to ack */
1173 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001174 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001175
Emeric Brunb3971ab2015-05-12 18:49:09 +02001176 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1177 if (!msglen) {
1178 /* internal error: message does not fit in trash */
1179 appctx->st0 = PEER_SESS_ST_END;
1180 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001181 }
1182
Emeric Brunb3971ab2015-05-12 18:49:09 +02001183 /* message to buffer */
1184 repl = bi_putblk(si_ic(si), trash.str, msglen);
1185 if (repl <= 0) {
1186 /* no more write possible */
1187 if (repl == -1) {
1188 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001189 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001190 appctx->st0 = PEER_SESS_ST_END;
1191 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001192 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001193 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001194 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001195
Emeric Brunb3971ab2015-05-12 18:49:09 +02001196 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1197 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1198 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1199 struct eb32_node *eb;
1200 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001201
Emeric Brunb3971ab2015-05-12 18:49:09 +02001202 if (st != curpeer->last_local_table) {
1203 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001204
Emeric Brunb3971ab2015-05-12 18:49:09 +02001205 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1206 if (!msglen) {
1207 /* internal error: message does not fit in trash */
1208 appctx->st0 = PEER_SESS_ST_END;
1209 goto switchstate;
1210 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001211
Emeric Brunb3971ab2015-05-12 18:49:09 +02001212 /* message to buffer */
1213 repl = bi_putblk(si_ic(si), trash.str, msglen);
1214 if (repl <= 0) {
1215 /* no more write possible */
1216 if (repl == -1) {
1217 goto full;
1218 }
1219 appctx->st0 = PEER_SESS_ST_END;
1220 goto switchstate;
1221 }
1222 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001223 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001224
1225 /* We force new pushed to 1 to force identifier in update message */
1226 new_pushed = 1;
1227 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1228 while (1) {
1229 uint32_t msglen;
1230 struct stksess *ts;
1231
1232 /* push local updates */
1233 if (!eb) {
1234 eb = eb32_first(&st->table->updates);
1235 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001236 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001237 break;
1238 }
1239 }
1240
1241 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001242 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001243 break;
1244 }
1245
1246 ts = eb32_entry(eb, struct stksess, upd);
1247 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1248 if (!msglen) {
1249 /* internal error: message does not fit in trash */
1250 appctx->st0 = PEER_SESS_ST_END;
1251 goto switchstate;
1252 }
1253
1254 /* message to buffer */
1255 repl = bi_putblk(si_ic(si), trash.str, msglen);
1256 if (repl <= 0) {
1257 /* no more write possible */
1258 if (repl == -1) {
1259 goto full;
1260 }
1261 appctx->st0 = PEER_SESS_ST_END;
1262 goto switchstate;
1263 }
1264 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001265 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1266 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001267 /* identifier may not needed in next update message */
1268 new_pushed = 0;
1269
1270 eb = eb32_next(eb);
1271 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001272 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001273 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001274 else {
1275 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1276 struct eb32_node *eb;
1277 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001278
Emeric Brunb3971ab2015-05-12 18:49:09 +02001279 if (st != curpeer->last_local_table) {
1280 int msglen;
1281
1282 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1283 if (!msglen) {
1284 /* internal error: message does not fit in trash */
1285 appctx->st0 = PEER_SESS_ST_END;
1286 goto switchstate;
1287 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001288
Emeric Brunb3971ab2015-05-12 18:49:09 +02001289 /* message to buffer */
1290 repl = bi_putblk(si_ic(si), trash.str, msglen);
1291 if (repl <= 0) {
1292 /* no more write possible */
1293 if (repl == -1) {
1294 goto full;
1295 }
1296 appctx->st0 = PEER_SESS_ST_END;
1297 goto switchstate;
1298 }
1299 curpeer->last_local_table = st;
1300 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001301
Emeric Brunb3971ab2015-05-12 18:49:09 +02001302 /* We force new pushed to 1 to force identifier in update message */
1303 new_pushed = 1;
1304 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1305 while (1) {
1306 uint32_t msglen;
1307 struct stksess *ts;
Emeric Brun2b920a12010-09-23 18:30:22 +02001308
Emeric Brunb3971ab2015-05-12 18:49:09 +02001309 /* push local updates */
1310 if (!eb) {
1311 st->flags |= SHTABLE_F_TEACH_STAGE1;
1312 eb = eb32_first(&st->table->updates);
1313 if (eb)
1314 st->last_pushed = eb->key - 1;
1315 break;
1316 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001317
Emeric Brunb3971ab2015-05-12 18:49:09 +02001318 ts = eb32_entry(eb, struct stksess, upd);
1319 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1320 if (!msglen) {
1321 /* internal error: message does not fit in trash */
1322 appctx->st0 = PEER_SESS_ST_END;
1323 goto switchstate;
1324 }
1325
1326 /* message to buffer */
1327 repl = bi_putblk(si_ic(si), trash.str, msglen);
1328 if (repl <= 0) {
1329 /* no more write possible */
1330 if (repl == -1) {
1331 goto full;
1332 }
1333 appctx->st0 = PEER_SESS_ST_END;
1334 goto switchstate;
1335 }
1336 st->last_pushed = ts->upd.key;
1337 /* identifier may not needed in next update message */
1338 new_pushed = 0;
1339
1340 eb = eb32_next(eb);
1341 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001342 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001343
Emeric Brunb3971ab2015-05-12 18:49:09 +02001344 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1345 struct eb32_node *eb;
1346 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001347
Emeric Brunb3971ab2015-05-12 18:49:09 +02001348 if (st != curpeer->last_local_table) {
1349 int msglen;
1350
1351 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1352 if (!msglen) {
1353 /* internal error: message does not fit in trash */
1354 appctx->st0 = PEER_SESS_ST_END;
1355 goto switchstate;
1356 }
1357
1358 /* message to buffer */
1359 repl = bi_putblk(si_ic(si), trash.str, msglen);
1360 if (repl <= 0) {
1361 /* no more write possible */
1362 if (repl == -1) {
1363 goto full;
1364 }
1365 appctx->st0 = PEER_SESS_ST_END;
1366 goto switchstate;
1367 }
1368 curpeer->last_local_table = st;
1369 }
1370
1371 /* We force new pushed to 1 to force identifier in update message */
1372 new_pushed = 1;
1373 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1374 while (1) {
1375 uint32_t msglen;
1376 struct stksess *ts;
1377
1378 /* push local updates */
1379 if (!eb || eb->key > st->teaching_origin) {
1380 st->flags |= SHTABLE_F_TEACH_STAGE2;
1381 eb = eb32_first(&st->table->updates);
1382 if (eb)
1383 st->last_pushed = eb->key - 1;
1384 break;
1385 }
1386
1387 ts = eb32_entry(eb, struct stksess, upd);
1388 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1389 if (!msglen) {
1390 /* internal error: message does not fit in trash */
1391 appctx->st0 = PEER_SESS_ST_END;
1392 goto switchstate;
1393 }
1394
1395 /* message to buffer */
1396 repl = bi_putblk(si_ic(si), trash.str, msglen);
1397 if (repl <= 0) {
1398 /* no more write possible */
1399 if (repl == -1) {
1400 goto full;
1401 }
1402 appctx->st0 = PEER_SESS_ST_END;
1403 goto switchstate;
1404 }
1405 st->last_pushed = ts->upd.key;
1406 /* identifier may not needed in next update message */
1407 new_pushed = 0;
1408
1409 eb = eb32_next(eb);
1410 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001411 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001412 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001413
1414 if (st == last_local_table)
1415 break;
1416 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001417 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001418 }
1419
1420
1421 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1422 unsigned char msg[2];
1423
1424 /* Current peer was elected to request a resync */
1425 msg[0] = PEER_MSG_CLASS_CONTROL;
1426 msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1427 /* process final lesson message */
1428 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1429 if (repl <= 0) {
1430 /* no more write possible */
1431 if (repl == -1)
1432 goto full;
1433 appctx->st0 = PEER_SESS_ST_END;
1434 goto switchstate;
1435 }
1436 /* flag finished message sent */
1437 curpeer->flags |= PEER_F_TEACH_FINISHED;
1438 }
1439
Emeric Brun2b920a12010-09-23 18:30:22 +02001440 /* noting more to do */
1441 goto out;
1442 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001443 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001444 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001445
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001446 if (bi_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001447 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001448 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001449 goto switchstate;
1450 case PEER_SESS_ST_ERRSIZE: {
1451 unsigned char msg[2];
1452
1453 msg[0] = PEER_MSG_CLASS_ERROR;
1454 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1455
1456 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1457 goto full;
1458 appctx->st0 = PEER_SESS_ST_END;
1459 goto switchstate;
1460 }
1461 case PEER_SESS_ST_ERRPROTO: {
1462 unsigned char msg[2];
1463
1464 msg[0] = PEER_MSG_CLASS_ERROR;
1465 msg[1] = PEER_MSG_ERR_PROTOCOL;
1466
1467 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1468 goto full;
1469 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001470 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001471 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001472 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001473 si_shutw(si);
1474 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001475 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001476 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001477 }
1478 }
1479 }
1480out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001481 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001482 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001483full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001484 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001485 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001486}
1487
Willy Tarreau30576452015-04-13 13:50:30 +02001488static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001489 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001490 .name = "<PEER>", /* used for logging */
1491 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001492 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001493};
Emeric Brun2b920a12010-09-23 18:30:22 +02001494
1495/*
1496 * Use this function to force a close of a peer session
1497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001498static void peer_session_forceshutdown(struct stream * stream)
Emeric Brun2b920a12010-09-23 18:30:22 +02001499{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001500 struct appctx *appctx = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001501 struct peer *ps;
1502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001503 int i;
Emeric Brun2b920a12010-09-23 18:30:22 +02001504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001505 for (i = 0; i <= 1; i++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001506 appctx = objt_appctx(stream->si[i].end);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001507 if (!appctx)
1508 continue;
1509 if (appctx->applet != &peer_applet)
1510 continue;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001511 break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001512 }
1513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001514 if (!appctx)
1515 return;
1516
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517 ps = (struct peer *)appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001518 /* we're killing a connection, we must apply a random delay before
1519 * retrying otherwise the other end will do the same and we can loop
1520 * for a while.
1521 */
1522 if (ps)
1523 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1524
Emeric Brun2b920a12010-09-23 18:30:22 +02001525 /* call release to reinit resync states if needed */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001526 peer_session_release(appctx);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001527 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001528 appctx->ctx.peers.ptr = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001529 task_wakeup(stream->task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001530}
1531
Willy Tarreau91d96282015-03-13 15:47:26 +01001532/* Pre-configures a peers frontend to accept incoming connections */
1533void peers_setup_frontend(struct proxy *fe)
1534{
1535 fe->last_change = now.tv_sec;
1536 fe->cap = PR_CAP_FE;
1537 fe->maxconn = 0;
1538 fe->conn_retries = CONN_RETRIES;
1539 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001540 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001541 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001542 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001543 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001544}
1545
Emeric Brun2b920a12010-09-23 18:30:22 +02001546/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001547 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001548 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001549static struct stream *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001550{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001551 struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe);
Emeric Brun2b920a12010-09-23 18:30:22 +02001552 struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001554 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001555 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001556 struct task *t;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001557 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001558
Emeric Brunb3971ab2015-05-12 18:49:09 +02001559 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1560 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001561 s = NULL;
1562
1563 appctx = appctx_new(&peer_applet);
1564 if (!appctx)
1565 goto out_close;
1566
1567 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001568 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001569
Willy Tarreau4099a7c2015-04-05 00:39:55 +02001570 sess = session_new(p, l, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001571 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001572 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001573 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001574 }
1575
Willy Tarreau8baf9062015-04-05 00:46:36 +02001576 if ((t = task_new()) == NULL) {
Willy Tarreau15b5e142015-04-04 14:38:25 +02001577 Alert("out of memory in peer_session_create().\n");
1578 goto out_free_sess;
1579 }
Willy Tarreau8baf9062015-04-05 00:46:36 +02001580 t->nice = l->nice;
1581
Willy Tarreau73b65ac2015-04-08 18:26:29 +02001582 if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001583 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau8baf9062015-04-05 00:46:36 +02001584 goto out_free_task;
1585 }
1586
Willy Tarreau342bfb12015-04-05 01:35:34 +02001587 /* The tasks below are normally what is supposed to be done by
1588 * fe->accept().
1589 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001590 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001591
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001592 /* applet is waiting for data */
1593 si_applet_cant_get(&s->si[0]);
1594 appctx_wakeup(appctx);
1595
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001596 /* initiate an outgoing connection */
1597 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001598
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001599 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001600 * pre-initialized connection in si->conn.
1601 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001602 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001603 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001604
1605 conn_prepare(conn, peer->proto, peer->xprt);
1606 si_attach_conn(&s->si[1], conn);
1607
1608 conn->target = s->target = &s->be->obj_type;
1609 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001610 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001611 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001612
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001613 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001614
Emeric Brun2b920a12010-09-23 18:30:22 +02001615 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1616 p->feconn++;/* beconn will be increased later */
1617 jobs++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001618 if (!(s->sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001619 actconn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001620 totalconn++;
1621
Emeric Brunb3971ab2015-05-12 18:49:09 +02001622 peer->appctx = appctx;
1623 peer->stream = s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001624 return s;
1625
1626 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001627 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001628 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001629 pool_free2(pool2_stream, s);
Willy Tarreau8baf9062015-04-05 00:46:36 +02001630 out_free_task:
1631 task_free(t);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001632 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001633 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001634 out_free_appctx:
1635 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001636 out_close:
1637 return s;
1638}
1639
1640/*
1641 * Task processing function to manage re-connect and peer session
1642 * tasks wakeup on local update.
1643 */
Simon Horman96553772011-06-08 09:18:51 +09001644static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001645{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001646 struct peers *peers = (struct peers *)task->context;
1647 struct peer *ps;
1648 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001649
1650 task->expire = TICK_ETERNITY;
1651
Emeric Brunb3971ab2015-05-12 18:49:09 +02001652 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001653 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001654 signal_unregister_handler(peers->sighandler);
1655 peers->sync_task = NULL;
1656 task_delete(peers->sync_task);
1657 task_free(peers->sync_task);
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001658 return NULL;
1659 }
1660
Emeric Brun2b920a12010-09-23 18:30:22 +02001661 if (!stopping) {
1662 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001663
1664 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1665 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1666 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001667 /* Resync from local peer needed
1668 no peer was assigned for the lesson
1669 and no old local peer found
1670 or resync timeout expire */
1671
1672 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001673 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001674
1675 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001676 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001677 }
1678
1679 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001680 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001681 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001682 if (!ps->local) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 if (!ps->stream) {
1684 /* no active stream */
Emeric Brun2b920a12010-09-23 18:30:22 +02001685 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001686 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001687 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001688 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001689 tick_is_expired(ps->reconnect, now_ms))) {
1690 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02001691 * or previous stream established with success
1692 * or previous stream failed during connection
Emeric Brun2b920a12010-09-23 18:30:22 +02001693 * and reconnection timer is expired */
1694
1695 /* retry a connect */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001696 ps->stream = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001697 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001698 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001699 /* If previous session failed during connection
1700 * but reconnection timer is not expired */
1701
1702 /* reschedule task for reconnect */
1703 task->expire = tick_first(task->expire, ps->reconnect);
1704 }
1705 /* else do nothing */
Willy Tarreau87b09662015-04-03 00:22:06 +02001706 } /* !ps->stream */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001707 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001708 /* current stream is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001709 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1710 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001711 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1712 /* Resync from a remote is needed
1713 * and no peer was assigned for lesson
1714 * and current peer may be up2date */
1715
1716 /* assign peer for the lesson */
1717 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001718 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001719
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 /* awake peer stream task to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001721 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001722 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001723 else {
1724 /* Awake session if there is data to push */
1725 for (st = ps->tables; st ; st = st->next) {
1726 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
1727 /* awake peer stream task to push local updates */
1728 appctx_wakeup(ps->appctx);
1729 break;
1730 }
1731 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001732 }
1733 /* else do nothing */
1734 } /* SUCCESSCODE */
1735 } /* !ps->peer->local */
1736 } /* for */
1737
1738 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001739 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1740 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1741 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001742 /* Resync from remote peer needed
1743 * no peer was assigned for the lesson
1744 * and resync timeout expire */
1745
1746 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001747 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001748 }
1749
Emeric Brunb3971ab2015-05-12 18:49:09 +02001750 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001751 /* Resync not finished*/
1752 /* reschedule task to resync timeout, to ended resync if needed */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001753 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001754 }
1755 } /* !stopping */
1756 else {
1757 /* soft stop case */
1758 if (task->state & TASK_WOKEN_SIGNAL) {
1759 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001760 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001761 /* add DO NOT STOP flag if not present */
1762 jobs++;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001763 peers->flags |= PEERS_F_DONOTSTOP;
1764 ps = peers->local;
1765 for (st = ps->tables; st ; st = st->next)
1766 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001767 }
1768
1769 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001770 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001771 if (ps->stream) {
1772 peer_session_forceshutdown(ps->stream);
1773 ps->stream = NULL;
Willy Tarreaue5843b32015-04-27 18:40:14 +02001774 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001775 }
1776 }
1777 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001778
Emeric Brunb3971ab2015-05-12 18:49:09 +02001779 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001780 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001782 /* resync of new process was complete, current process can die now */
1783 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001784 peers->flags &= ~PEERS_F_DONOTSTOP;
1785 for (st = ps->tables; st ; st = st->next)
1786 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001787 }
1788 }
Willy Tarreau87b09662015-04-03 00:22:06 +02001789 else if (!ps->stream) {
1790 /* If stream is not active */
Emeric Brun2b920a12010-09-23 18:30:22 +02001791 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001792 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
1793 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
1794 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001795 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02001796 * or previous stream was successfully established
1797 * or previous stream tcp connect success but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02001798 * or during previous connect, peer replies a try again statuscode */
1799
1800 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001801 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001802 }
1803 else {
1804 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001805 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001806 /* unable to resync new process, current process can die now */
1807 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001808 peers->flags &= ~PEERS_F_DONOTSTOP;
1809 for (st = ps->tables; st ; st = st->next)
1810 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001811 }
1812 }
1813 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001814 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001815 /* current stream active and established
1816 awake stream to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001817 for (st = ps->tables; st ; st = st->next) {
1818 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
1819 /* awake peer stream task to push local updates */
1820 appctx_wakeup(ps->appctx);
1821 break;
1822 }
1823 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001824 }
1825 } /* stopping */
1826 /* Wakeup for re-connect */
1827 return task;
1828}
1829
Emeric Brunb3971ab2015-05-12 18:49:09 +02001830
Emeric Brun2b920a12010-09-23 18:30:22 +02001831/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001832 *
1833 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001834void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02001835{
Emeric Brun2b920a12010-09-23 18:30:22 +02001836 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001837 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02001838
Emeric Brun2b920a12010-09-23 18:30:22 +02001839 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001840 peers->peers_fe->maxconn += 3;
1841 }
1842
Willy Tarreau4348fad2012-09-20 16:48:07 +02001843 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
1844 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001845 peers->sync_task = task_new();
1846 peers->sync_task->process = process_peer_sync;
1847 peers->sync_task->expire = TICK_ETERNITY;
1848 peers->sync_task->context = (void *)peers;
1849 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
1850 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
1851}
1852
1853
1854
1855/*
1856 * Function used to register a table for sync on a group of peers
1857 *
1858 */
1859void peers_register_table(struct peers *peers, struct stktable *table)
1860{
1861 struct shared_table *st;
1862 struct peer * curpeer;
1863 int id = 0;
1864
1865 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
1866 st = (struct shared_table *)calloc(1,sizeof(struct shared_table));
1867 st->table = table;
1868 st->next = curpeer->tables;
1869 if (curpeer->tables)
1870 id = curpeer->tables->local_id;
1871 st->local_id = id + 1;
1872
1873 curpeer->tables = st;
1874 }
1875
1876 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02001877}
1878