blob: 1a80ab34a4fae54b63654eea01f181141db022a7 [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 Tarreau81bc3b02016-10-31 17:37:39 +0100175static void peer_session_forceshutdown(struct appctx *appctx);
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) {
Willy Tarreau9df94c22016-10-31 18:42:52 +0100488 if (peer->appctx == appctx) {
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->appctx = NULL;
492 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200493 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200494 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
495 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200496
497 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200499 }
500 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200501 peer->flags &= PEER_TEACH_RESET;
502 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200505 }
506}
507
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200508/* Retrieve the major and minor versions of peers protocol
509 * announced by a remote peer. <str> is a null-terminated
510 * string with the following format: "<maj_ver>.<min_ver>".
511 */
512static int peer_get_version(const char *str,
513 unsigned int *maj_ver, unsigned int *min_ver)
514{
515 unsigned int majv, minv;
516 const char *pos, *saved;
517 const char *end;
518
519 saved = pos = str;
520 end = str + strlen(str);
521
522 majv = read_uint(&pos, end);
523 if (saved == pos || *pos++ != '.')
524 return -1;
525
526 saved = pos;
527 minv = read_uint(&pos, end);
528 if (saved == pos || pos != end)
529 return -1;
530
531 *maj_ver = majv;
532 *min_ver = minv;
533
534 return 0;
535}
Emeric Brun2b920a12010-09-23 18:30:22 +0200536
537/*
538 * IO Handler to handle message exchance with a peer
539 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200540static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200541{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200542 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200543 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200544 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200545 int reql = 0;
546 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200547 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
548 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200549
550 while (1) {
551switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200552 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100553 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100554 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100555 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100556 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200557 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100558 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100559 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200560 if (reql <= 0) { /* closed or EOL not found */
561 if (reql == 0)
562 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100563 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200564 goto switchstate;
565 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100566 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100567 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200568 goto switchstate;
569 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100570 else if (reql > 1 && (trash.str[reql-2] == '\r'))
571 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200572 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100573 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200574
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100575 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200576
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200577 /* test protocol */
578 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
579 appctx->st0 = PEER_SESS_ST_EXIT;
580 appctx->st1 = PEER_SESS_SC_ERRPROTO;
581 goto switchstate;
582 }
583 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
584 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100585 appctx->st0 = PEER_SESS_ST_EXIT;
586 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200587 goto switchstate;
588 }
589
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100590 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200591 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100592 case PEER_SESS_ST_GETHOST:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100593 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200594 if (reql <= 0) { /* closed or EOL not found */
595 if (reql == 0)
596 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100597 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200598 goto switchstate;
599 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100600 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100601 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602 goto switchstate;
603 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 else if (reql > 1 && (trash.str[reql-2] == '\r'))
605 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200606 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100607 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200608
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100609 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200610
611 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100612 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100613 appctx->st0 = PEER_SESS_ST_EXIT;
614 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200615 goto switchstate;
616 }
617
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100618 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200619 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100620 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200621 struct peer *curpeer;
622 char *p;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100623 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200624 if (reql <= 0) { /* closed or EOL not found */
625 if (reql == 0)
626 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100627 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 goto switchstate;
629 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100630 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200631 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100632 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200633 goto switchstate;
634 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100635 else if (reql > 1 && (trash.str[reql-2] == '\r'))
636 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200637 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100638 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200639
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100640 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200641
Emeric Brunb3971ab2015-05-12 18:49:09 +0200642 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100643 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200644 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100645 appctx->st0 = PEER_SESS_ST_EXIT;
646 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647 goto switchstate;
648 }
649 *p = 0;
650
651 /* lookup known peer */
652 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100653 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200654 break;
655 }
656
657 /* if unknown peer */
658 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100659 appctx->st0 = PEER_SESS_ST_EXIT;
660 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200661 goto switchstate;
662 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200663
Willy Tarreau9df94c22016-10-31 18:42:52 +0100664 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200665 if (curpeer->local) {
666 /* Local connection, reply a retry */
667 appctx->st0 = PEER_SESS_ST_EXIT;
668 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
669 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200670 }
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100671 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200672 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200673 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
674 if (min_ver == PEER_DWNGRD_MINOR_VER) {
675 curpeer->flags |= PEER_F_DWNGRD;
676 }
677 else {
678 curpeer->flags &= ~PEER_F_DWNGRD;
679 }
680 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681 curpeer->appctx = appctx;
682 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100683 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200684 /* fall through */
685 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100686 case PEER_SESS_ST_SENDSUCCESS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200687 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200688 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200689
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100690 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100691 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200692 if (repl <= 0) {
693 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100694 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100695 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200696 goto switchstate;
697 }
698
699 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200701
702 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200703 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200704
705 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200706 curpeer->confirm = 0;
707
708 /* Init cursors */
709 for (st = curpeer->tables; st ; st = st->next) {
710 st->last_get = st->last_acked = 0;
711 st->teaching_origin = st->last_pushed = st->update;
712 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200713
714 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200715 curpeer->flags &= PEER_TEACH_RESET;
716 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200717
718 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200719 if (curpeer->local) {
720 /* if current host need resyncfrom local and no process assined */
721 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
722 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
723 /* assign local peer for a lesson, consider lesson already requested */
724 curpeer->flags |= PEER_F_LEARN_ASSIGN;
725 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
726 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200727
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728 }
729 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
730 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
731 /* assign peer for a lesson */
732 curpeer->flags |= PEER_F_LEARN_ASSIGN;
733 peers->flags |= PEERS_F_RESYNC_ASSIGN;
734 }
735
736
Emeric Brun2b920a12010-09-23 18:30:22 +0200737 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100738 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200739 goto switchstate;
740 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100741 case PEER_SESS_ST_CONNECT: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200742 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200743
744 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100745 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200746 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
747 PEER_MAJOR_VER,
748 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200749 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200750 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100751 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200752 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200753
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100754 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100755 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200756 goto switchstate;
757 }
758
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100759 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200760 if (repl <= 0) {
761 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100762 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100763 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200764 goto switchstate;
765 }
766
767 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100768 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200769 /* fall through */
770 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100771 case PEER_SESS_ST_GETSTATUS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200772 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200773 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200774
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100775 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200776 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200777
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100778 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200779 if (reql <= 0) { /* closed or EOL not found */
780 if (reql == 0)
781 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100782 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200783 goto switchstate;
784 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100785 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200786 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100787 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200788 goto switchstate;
789 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100790 else if (reql > 1 && (trash.str[reql-2] == '\r'))
791 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200792 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100793 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200794
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100795 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200796
797 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200798 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200799
800 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200802
803 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200804 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200805 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200806 for (st = curpeer->tables; st ; st = st->next) {
807 st->last_get = st->last_acked = 0;
808 st->teaching_origin = st->last_pushed = st->update;
809 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200810
811 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200812 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200813
Emeric Brunb3971ab2015-05-12 18:49:09 +0200814 /* reset teaching and learning flags to 0 */
815 curpeer->flags &= PEER_TEACH_RESET;
816 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200817
Emeric Brunb3971ab2015-05-12 18:49:09 +0200818 /* If current peer is local */
819 if (curpeer->local) {
820 /* flag to start to teach lesson */
821 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200822
Emeric Brunb3971ab2015-05-12 18:49:09 +0200823 }
824 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
825 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
826 /* If peer is remote and resync from remote is needed,
827 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200828
Emeric Brunb3971ab2015-05-12 18:49:09 +0200829 /* assign peer for a lesson */
830 curpeer->flags |= PEER_F_LEARN_ASSIGN;
831 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200832 }
833
834 }
835 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200836 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
837 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200838 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100839 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200840 goto switchstate;
841 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100842 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200843 /* fall through */
844 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100845 case PEER_SESS_ST_WAITMSG: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200846 struct peer *curpeer = appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200847 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200848 uint32_t msg_len = 0;
849 char *msg_cur = trash.str;
850 char *msg_end = trash.str;
851 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200852 int totl = 0;
853
Emeric Brunb3971ab2015-05-12 18:49:09 +0200854 reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200855 if (reql <= 0) /* closed or EOL not found */
856 goto incomplete;
857
Emeric Brun2b920a12010-09-23 18:30:22 +0200858 totl += reql;
859
Emeric Brunb3971ab2015-05-12 18:49:09 +0200860 if (msg_head[1] >= 128) {
861 /* Read and Decode message length */
862 reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
863 if (reql <= 0) /* closed */
864 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200865
Emeric Brunb3971ab2015-05-12 18:49:09 +0200866 totl += reql;
867
868 if (msg_head[2] < 240) {
869 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200870 }
871 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200872 int i;
873 char *cur;
874 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200875
Emeric Brunb3971ab2015-05-12 18:49:09 +0200876 for (i = 3 ; i < sizeof(msg_head) ; i++) {
877 reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
878 if (reql <= 0) /* closed */
879 goto incomplete;
880
881 totl += reql;
882
883 if (!(msg_head[i] & 0x80))
884 break;
885 }
886
887 if (i == sizeof(msg_head)) {
888 /* malformed message */
889 appctx->st0 = PEER_SESS_ST_ERRPROTO;
890 goto switchstate;
891
892 }
893 end = (char *)msg_head + sizeof(msg_head);
894 cur = (char *)&msg_head[2];
895 msg_len = intdecode(&cur, end);
896 if (!cur) {
897 /* malformed message */
898 appctx->st0 = PEER_SESS_ST_ERRPROTO;
899 goto switchstate;
900 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200901 }
902
Willy Tarreau86a446e2013-11-25 23:02:37 +0100903
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 /* Read message content */
905 if (msg_len) {
906 if (msg_len > trash.size) {
907 /* Status code is not success, abort */
908 appctx->st0 = PEER_SESS_ST_ERRSIZE;
909 goto switchstate;
910 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200911
Emeric Brunb3971ab2015-05-12 18:49:09 +0200912 reql = bo_getblk(si_oc(si), trash.str, msg_len, totl);
913 if (reql <= 0) /* closed */
914 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200915 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100916
Emeric Brunb3971ab2015-05-12 18:49:09 +0200917 msg_end += msg_len;
918 }
919 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100920
Emeric Brunb3971ab2015-05-12 18:49:09 +0200921 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
922 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
923 struct shared_table *st;
924 /* Reset message: remote need resync */
925
926 /* prepare tables fot a global push */
927 for (st = curpeer->tables; st; st = st->next) {
928 st->teaching_origin = st->last_pushed = st->table->update;
929 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100930 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200931
Emeric Brunb3971ab2015-05-12 18:49:09 +0200932 /* reset teaching flags to 0 */
933 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200934
Emeric Brunb3971ab2015-05-12 18:49:09 +0200935 /* flag to start to teach lesson */
936 curpeer->flags |= PEER_F_TEACH_PROCESS;
937
938
939 }
940 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
941
942 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
943 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
944 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
945 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100946 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200947 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200948 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200949 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
950
951 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
952 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
953 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
954
955 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
956 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
957 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100958 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200959 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200960 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200961 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +0200962 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200963
964 /* If stopping state */
965 if (stopping) {
966 /* Close session, push resync no more needed */
967 curpeer->flags |= PEER_F_TEACH_COMPLETE;
968 appctx->st0 = PEER_SESS_ST_END;
969 goto switchstate;
970 }
Emeric Brun597b26e2016-08-12 11:23:31 +0200971 for (st = curpeer->tables; st; st = st->next) {
972 st->update = st->last_pushed = st->teaching_origin;
973 st->flags = 0;
974 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200975
976 /* reset teaching flags to 0 */
977 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200978 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200979 }
980 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
981 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
982 int table_id_len;
983 struct shared_table *st;
984 int table_type;
985 int table_keylen;
986 int table_id;
987 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +0200988
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989 table_id = intdecode(&msg_cur, msg_end);
990 if (!msg_cur) {
991 /* malformed message */
992 appctx->st0 = PEER_SESS_ST_ERRPROTO;
993 goto switchstate;
994 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200995
Emeric Brunb3971ab2015-05-12 18:49:09 +0200996 table_id_len = intdecode(&msg_cur, msg_end);
997 if (!msg_cur) {
998 /* malformed message */
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 curpeer->remote_table = NULL;
1004 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1005 /* malformed message */
1006 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1007 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001008 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001009
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010 for (st = curpeer->tables; st; st = st->next) {
1011 /* Reset IDs */
1012 if (st->remote_id == table_id)
1013 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001014
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015 if (!curpeer->remote_table
1016 && (table_id_len == strlen(st->table->id))
1017 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1018 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001019 }
1020 }
1021
Emeric Brunb3971ab2015-05-12 18:49:09 +02001022 if (!curpeer->remote_table) {
1023 goto ignore_msg;
1024 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001025
Emeric Brunb3971ab2015-05-12 18:49:09 +02001026 msg_cur += table_id_len;
1027 if (msg_cur >= msg_end) {
1028 /* malformed message */
1029 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1030 goto switchstate;
1031 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001032
Emeric Brunb3971ab2015-05-12 18:49:09 +02001033 table_type = intdecode(&msg_cur, msg_end);
1034 if (!msg_cur) {
1035 /* malformed message */
1036 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1037 goto switchstate;
1038 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001039
Emeric Brunb3971ab2015-05-12 18:49:09 +02001040 table_keylen = intdecode(&msg_cur, msg_end);
1041 if (!msg_cur) {
1042 /* malformed message */
1043 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1044 goto switchstate;
1045 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001046
Emeric Brunb3971ab2015-05-12 18:49:09 +02001047 table_data = intdecode(&msg_cur, msg_end);
1048 if (!msg_cur) {
1049 /* malformed message */
1050 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1051 goto switchstate;
1052 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001053
Emeric Brunb3971ab2015-05-12 18:49:09 +02001054 if (curpeer->remote_table->table->type != table_type
1055 || curpeer->remote_table->table->key_size != table_keylen) {
1056 curpeer->remote_table = NULL;
1057 goto ignore_msg;
1058 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001059
Emeric Brunb3971ab2015-05-12 18:49:09 +02001060 curpeer->remote_table->remote_data = table_data;
1061 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001062 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001063 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1064 struct shared_table *st;
1065 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001066
Emeric Brunb3971ab2015-05-12 18:49:09 +02001067 table_id = intdecode(&msg_cur, msg_end);
1068 if (!msg_cur) {
1069 /* malformed message */
1070 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1071 goto switchstate;
1072 }
1073 curpeer->remote_table = NULL;
1074 for (st = curpeer->tables; st; st = st->next) {
1075 if (st->remote_id == table_id) {
1076 curpeer->remote_table = st;
1077 break;
1078 }
1079 }
1080
Emeric Brun2b920a12010-09-23 18:30:22 +02001081 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001082 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001083 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1084 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1085 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001086 struct shared_table *st = curpeer->remote_table;
1087 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001088 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001089 unsigned int data_type;
1090 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001091
Emeric Brunb3971ab2015-05-12 18:49:09 +02001092 /* Here we have data message */
1093 if (!st)
1094 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001095
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001096 expire = MS_TO_TICKS(st->table->expire);
1097
1098 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1099 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001100 if (msg_len < sizeof(update)) {
1101 /* malformed message */
1102 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1103 goto switchstate;
1104 }
1105 memcpy(&update, msg_cur, sizeof(update));
1106 msg_cur += sizeof(update);
1107 st->last_get = htonl(update);
1108 }
1109 else {
1110 st->last_get++;
1111 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001112
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001113 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1114 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1115 size_t expire_sz = sizeof expire;
1116
1117 if (msg_cur + expire_sz > msg_end) {
1118 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1119 goto switchstate;
1120 }
1121 memcpy(&expire, msg_cur, expire_sz);
1122 msg_cur += expire_sz;
1123 expire = ntohl(expire);
1124 }
1125
Emeric Brunb3971ab2015-05-12 18:49:09 +02001126 newts = stksess_new(st->table, NULL);
1127 if (!newts)
1128 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001129
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001130 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001131 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001132
Emeric Brunb3971ab2015-05-12 18:49:09 +02001133 to_read = intdecode(&msg_cur, msg_end);
1134 if (!msg_cur) {
1135 /* malformed message */
1136 stksess_free(st->table, newts);
1137 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1138 goto switchstate;
1139 }
1140 to_store = MIN(to_read, st->table->key_size - 1);
1141 if (msg_cur + to_store > msg_end) {
1142 /* malformed message */
1143 stksess_free(st->table, newts);
1144 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1145 goto switchstate;
1146 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001147
Emeric Brunb3971ab2015-05-12 18:49:09 +02001148 memcpy(newts->key.key, msg_cur, to_store);
1149 newts->key.key[to_store] = 0;
1150 msg_cur += to_read;
1151 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001152 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001153 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001154
Emeric Brunb3971ab2015-05-12 18:49:09 +02001155 if (msg_cur + sizeof(netinteger) > msg_end) {
1156 /* malformed message */
1157 stksess_free(st->table, newts);
1158 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1159 goto switchstate;
1160 }
1161 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1162 netinteger = ntohl(netinteger);
1163 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1164 msg_cur += sizeof(netinteger);
1165 }
1166 else {
1167 if (msg_cur + st->table->key_size > msg_end) {
1168 /* malformed message */
1169 stksess_free(st->table, newts);
1170 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1171 goto switchstate;
1172 }
1173 memcpy(newts->key.key, msg_cur, st->table->key_size);
1174 msg_cur += st->table->key_size;
1175 }
1176
1177 /* lookup for existing entry */
1178 ts = stktable_lookup(st->table, newts);
1179 if (ts) {
1180 /* the entry already exist, we can free ours */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001181 stktable_touch_with_exp(st->table, ts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001182 stksess_free(st->table, newts);
1183 newts = NULL;
1184 }
1185 else {
1186 struct eb32_node *eb;
1187
1188 /* create new entry */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001189 ts = stktable_store_with_exp(st->table, newts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001190 newts = NULL; /* don't reuse it */
1191
Emeric Brun234fc3c2015-12-16 15:16:46 +01001192 ts->upd.key= (++st->table->update)+(2147483648U);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001193 eb = eb32_insert(&st->table->updates, &ts->upd);
1194 if (eb != &ts->upd) {
1195 eb32_delete(eb);
1196 eb32_insert(&st->table->updates, &ts->upd);
1197 }
1198 }
1199
Emeric Brun94900952015-06-11 18:25:54 +02001200 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001201
Emeric Brun94900952015-06-11 18:25:54 +02001202 if ((1 << data_type) & st->remote_data) {
1203 switch (stktable_data_types[data_type].std_type) {
1204 case STD_T_SINT: {
1205 int data;
1206
1207 data = intdecode(&msg_cur, msg_end);
1208 if (!msg_cur) {
1209 /* malformed message */
1210 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1211 goto switchstate;
1212 }
1213
1214 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1215 if (data_ptr)
1216 stktable_data_cast(data_ptr, std_t_sint) = data;
1217 break;
1218 }
1219 case STD_T_UINT: {
1220 unsigned int data;
1221
1222 data = intdecode(&msg_cur, msg_end);
1223 if (!msg_cur) {
1224 /* malformed message */
1225 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1226 goto switchstate;
1227 }
1228
1229 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1230 if (data_ptr)
1231 stktable_data_cast(data_ptr, std_t_uint) = data;
1232 break;
1233 }
1234 case STD_T_ULL: {
1235 unsigned long long data;
1236
1237 data = intdecode(&msg_cur, msg_end);
1238 if (!msg_cur) {
1239 /* malformed message */
1240 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1241 goto switchstate;
1242 }
1243
1244 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1245 if (data_ptr)
1246 stktable_data_cast(data_ptr, std_t_ull) = data;
1247 break;
1248 }
1249 case STD_T_FRQP: {
1250 struct freq_ctr_period data;
1251
Willy Tarreau3bb46172016-03-25 18:17:47 +01001252 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001253 if (!msg_cur) {
1254 /* malformed message */
1255 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1256 goto switchstate;
1257 }
1258 data.curr_ctr = intdecode(&msg_cur, msg_end);
1259 if (!msg_cur) {
1260 /* malformed message */
1261 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1262 goto switchstate;
1263 }
1264 data.prev_ctr = intdecode(&msg_cur, msg_end);
1265 if (!msg_cur) {
1266 /* malformed message */
1267 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1268 goto switchstate;
1269 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001270
Emeric Brun94900952015-06-11 18:25:54 +02001271 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1272 if (data_ptr)
1273 stktable_data_cast(data_ptr, std_t_frqp) = data;
1274 break;
1275 }
1276 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001277 }
1278 }
1279 }
1280 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1281 /* ack message */
1282 uint32_t table_id ;
1283 uint32_t update;
1284 struct shared_table *st;
1285
1286 table_id = intdecode(&msg_cur, msg_end);
1287 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1288 /* malformed message */
1289 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1290 goto switchstate;
1291 }
1292 memcpy(&update, msg_cur, sizeof(update));
1293 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001294
Emeric Brunb3971ab2015-05-12 18:49:09 +02001295 for (st = curpeer->tables; st; st = st->next) {
1296 if (st->local_id == table_id) {
1297 st->update = update;
1298 break;
1299 }
1300 }
1301 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001302 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001303 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1304 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001305 goto switchstate;
1306 }
1307
Emeric Brunb3971ab2015-05-12 18:49:09 +02001308ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001309 /* skip consumed message */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001310 bo_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001311 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001312 goto switchstate;
1313
Emeric Brun2b920a12010-09-23 18:30:22 +02001314incomplete:
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001315 /* we get here when a bo_getblk() returns <= 0 in reql */
1316
Willy Tarreau72d6c162013-04-11 16:14:13 +02001317 if (reql < 0) {
1318 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001319 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001320 goto switchstate;
1321 }
1322
Emeric Brun2b920a12010-09-23 18:30:22 +02001323
Emeric Brun2b920a12010-09-23 18:30:22 +02001324
Emeric Brunb3971ab2015-05-12 18:49:09 +02001325
Emeric Brun2b920a12010-09-23 18:30:22 +02001326 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001327 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1328 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1329 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1330 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001331
Emeric Brunb3971ab2015-05-12 18:49:09 +02001332 /* Current peer was elected to request a resync */
1333 msg[0] = PEER_MSG_CLASS_CONTROL;
1334 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001335
Emeric Brunb3971ab2015-05-12 18:49:09 +02001336 /* message to buffer */
1337 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1338 if (repl <= 0) {
1339 /* no more write possible */
1340 if (repl == -1)
1341 goto full;
1342 appctx->st0 = PEER_SESS_ST_END;
1343 goto switchstate;
1344 }
1345 peers->flags |= PEERS_F_RESYNC_PROCESS;
1346 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001347
Emeric Brunb3971ab2015-05-12 18:49:09 +02001348 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001349
Emeric Brunb3971ab2015-05-12 18:49:09 +02001350 if (curpeer->tables) {
1351 struct shared_table *st;
1352 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001353
Emeric Brunb3971ab2015-05-12 18:49:09 +02001354 last_local_table = curpeer->last_local_table;
1355 if (!last_local_table)
1356 last_local_table = curpeer->tables;
1357 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001358
Emeric Brunb3971ab2015-05-12 18:49:09 +02001359 while (1) {
1360 if (!st)
1361 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001362
Emeric Brunb3971ab2015-05-12 18:49:09 +02001363 /* It remains some updates to ack */
1364 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001365 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001366
Emeric Brunb3971ab2015-05-12 18:49:09 +02001367 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1368 if (!msglen) {
1369 /* internal error: message does not fit in trash */
1370 appctx->st0 = PEER_SESS_ST_END;
1371 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001372 }
1373
Emeric Brunb3971ab2015-05-12 18:49:09 +02001374 /* message to buffer */
1375 repl = bi_putblk(si_ic(si), trash.str, msglen);
1376 if (repl <= 0) {
1377 /* no more write possible */
1378 if (repl == -1) {
1379 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001380 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001381 appctx->st0 = PEER_SESS_ST_END;
1382 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001383 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001384 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001385 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001386
Emeric Brunb3971ab2015-05-12 18:49:09 +02001387 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1388 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1389 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1390 struct eb32_node *eb;
1391 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001392
Emeric Brunb3971ab2015-05-12 18:49:09 +02001393 if (st != curpeer->last_local_table) {
1394 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001395
Emeric Brunb3971ab2015-05-12 18:49:09 +02001396 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1397 if (!msglen) {
1398 /* internal error: message does not fit in trash */
1399 appctx->st0 = PEER_SESS_ST_END;
1400 goto switchstate;
1401 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001402
Emeric Brunb3971ab2015-05-12 18:49:09 +02001403 /* message to buffer */
1404 repl = bi_putblk(si_ic(si), trash.str, msglen);
1405 if (repl <= 0) {
1406 /* no more write possible */
1407 if (repl == -1) {
1408 goto full;
1409 }
1410 appctx->st0 = PEER_SESS_ST_END;
1411 goto switchstate;
1412 }
1413 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001414 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001415
1416 /* We force new pushed to 1 to force identifier in update message */
1417 new_pushed = 1;
1418 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1419 while (1) {
1420 uint32_t msglen;
1421 struct stksess *ts;
1422
1423 /* push local updates */
1424 if (!eb) {
1425 eb = eb32_first(&st->table->updates);
1426 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001427 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001428 break;
1429 }
1430 }
1431
1432 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001433 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001434 break;
1435 }
1436
1437 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001438 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001439 if (!msglen) {
1440 /* internal error: message does not fit in trash */
1441 appctx->st0 = PEER_SESS_ST_END;
1442 goto switchstate;
1443 }
1444
1445 /* message to buffer */
1446 repl = bi_putblk(si_ic(si), trash.str, msglen);
1447 if (repl <= 0) {
1448 /* no more write possible */
1449 if (repl == -1) {
1450 goto full;
1451 }
1452 appctx->st0 = PEER_SESS_ST_END;
1453 goto switchstate;
1454 }
1455 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001456 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1457 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001458 /* identifier may not needed in next update message */
1459 new_pushed = 0;
1460
1461 eb = eb32_next(eb);
1462 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001463 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001464 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001465 else {
1466 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1467 struct eb32_node *eb;
1468 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001469
Emeric Brunb3971ab2015-05-12 18:49:09 +02001470 if (st != curpeer->last_local_table) {
1471 int msglen;
1472
1473 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1474 if (!msglen) {
1475 /* internal error: message does not fit in trash */
1476 appctx->st0 = PEER_SESS_ST_END;
1477 goto switchstate;
1478 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001479
Emeric Brunb3971ab2015-05-12 18:49:09 +02001480 /* message to buffer */
1481 repl = bi_putblk(si_ic(si), trash.str, msglen);
1482 if (repl <= 0) {
1483 /* no more write possible */
1484 if (repl == -1) {
1485 goto full;
1486 }
1487 appctx->st0 = PEER_SESS_ST_END;
1488 goto switchstate;
1489 }
1490 curpeer->last_local_table = st;
1491 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001492
Emeric Brunb3971ab2015-05-12 18:49:09 +02001493 /* We force new pushed to 1 to force identifier in update message */
1494 new_pushed = 1;
1495 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1496 while (1) {
1497 uint32_t msglen;
1498 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001499 int use_timed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001500
Emeric Brunb3971ab2015-05-12 18:49:09 +02001501 /* push local updates */
1502 if (!eb) {
1503 st->flags |= SHTABLE_F_TEACH_STAGE1;
1504 eb = eb32_first(&st->table->updates);
1505 if (eb)
1506 st->last_pushed = eb->key - 1;
1507 break;
1508 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001509
Emeric Brunb3971ab2015-05-12 18:49:09 +02001510 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001511 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1512 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001513 if (!msglen) {
1514 /* internal error: message does not fit in trash */
1515 appctx->st0 = PEER_SESS_ST_END;
1516 goto switchstate;
1517 }
1518
1519 /* message to buffer */
1520 repl = bi_putblk(si_ic(si), trash.str, msglen);
1521 if (repl <= 0) {
1522 /* no more write possible */
1523 if (repl == -1) {
1524 goto full;
1525 }
1526 appctx->st0 = PEER_SESS_ST_END;
1527 goto switchstate;
1528 }
1529 st->last_pushed = ts->upd.key;
1530 /* identifier may not needed in next update message */
1531 new_pushed = 0;
1532
1533 eb = eb32_next(eb);
1534 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001535 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001536
Emeric Brunb3971ab2015-05-12 18:49:09 +02001537 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1538 struct eb32_node *eb;
1539 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001540
Emeric Brunb3971ab2015-05-12 18:49:09 +02001541 if (st != curpeer->last_local_table) {
1542 int msglen;
1543
1544 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1545 if (!msglen) {
1546 /* internal error: message does not fit in trash */
1547 appctx->st0 = PEER_SESS_ST_END;
1548 goto switchstate;
1549 }
1550
1551 /* message to buffer */
1552 repl = bi_putblk(si_ic(si), trash.str, msglen);
1553 if (repl <= 0) {
1554 /* no more write possible */
1555 if (repl == -1) {
1556 goto full;
1557 }
1558 appctx->st0 = PEER_SESS_ST_END;
1559 goto switchstate;
1560 }
1561 curpeer->last_local_table = st;
1562 }
1563
1564 /* We force new pushed to 1 to force identifier in update message */
1565 new_pushed = 1;
1566 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1567 while (1) {
1568 uint32_t msglen;
1569 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001570 int use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001571
1572 /* push local updates */
1573 if (!eb || eb->key > st->teaching_origin) {
1574 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001575 break;
1576 }
1577
1578 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001579 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1580 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001581 if (!msglen) {
1582 /* internal error: message does not fit in trash */
1583 appctx->st0 = PEER_SESS_ST_END;
1584 goto switchstate;
1585 }
1586
1587 /* message to buffer */
1588 repl = bi_putblk(si_ic(si), trash.str, msglen);
1589 if (repl <= 0) {
1590 /* no more write possible */
1591 if (repl == -1) {
1592 goto full;
1593 }
1594 appctx->st0 = PEER_SESS_ST_END;
1595 goto switchstate;
1596 }
1597 st->last_pushed = ts->upd.key;
1598 /* identifier may not needed in next update message */
1599 new_pushed = 0;
1600
1601 eb = eb32_next(eb);
1602 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001603 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001604 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001605
1606 if (st == last_local_table)
1607 break;
1608 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001609 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001610 }
1611
1612
1613 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1614 unsigned char msg[2];
1615
1616 /* Current peer was elected to request a resync */
1617 msg[0] = PEER_MSG_CLASS_CONTROL;
1618 msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1619 /* process final lesson message */
1620 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1621 if (repl <= 0) {
1622 /* no more write possible */
1623 if (repl == -1)
1624 goto full;
1625 appctx->st0 = PEER_SESS_ST_END;
1626 goto switchstate;
1627 }
1628 /* flag finished message sent */
1629 curpeer->flags |= PEER_F_TEACH_FINISHED;
1630 }
1631
Emeric Brun597b26e2016-08-12 11:23:31 +02001632 /* Confirm finished or partial messages */
1633 while (curpeer->confirm) {
1634 unsigned char msg[2];
1635
1636 /* There is a confirm messages to send */
1637 msg[0] = PEER_MSG_CLASS_CONTROL;
1638 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1639
1640 /* message to buffer */
1641 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1642 if (repl <= 0) {
1643 /* no more write possible */
1644 if (repl == -1)
1645 goto full;
1646 appctx->st0 = PEER_SESS_ST_END;
1647 goto switchstate;
1648 }
1649 curpeer->confirm--;
1650 }
1651
Emeric Brun2b920a12010-09-23 18:30:22 +02001652 /* noting more to do */
1653 goto out;
1654 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001655 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001656 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001657 if (bi_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001658 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001659 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001660 goto switchstate;
1661 case PEER_SESS_ST_ERRSIZE: {
1662 unsigned char msg[2];
1663
1664 msg[0] = PEER_MSG_CLASS_ERROR;
1665 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1666
1667 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1668 goto full;
1669 appctx->st0 = PEER_SESS_ST_END;
1670 goto switchstate;
1671 }
1672 case PEER_SESS_ST_ERRPROTO: {
1673 unsigned char msg[2];
1674
1675 msg[0] = PEER_MSG_CLASS_ERROR;
1676 msg[1] = PEER_MSG_ERR_PROTOCOL;
1677
1678 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1679 goto full;
1680 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001681 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001682 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001683 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001684 si_shutw(si);
1685 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001686 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001687 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001688 }
1689 }
1690 }
1691out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001692 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001693 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001694full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001695 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001696 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001697}
1698
Willy Tarreau30576452015-04-13 13:50:30 +02001699static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001700 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001701 .name = "<PEER>", /* used for logging */
1702 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001703 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001704};
Emeric Brun2b920a12010-09-23 18:30:22 +02001705
1706/*
1707 * Use this function to force a close of a peer session
1708 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001709static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001710{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001711 struct peer *ps;
1712
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001713 if (!appctx)
1714 return;
1715
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001716 if (appctx->applet != &peer_applet)
1717 return;
1718
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001719 ps = appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001720 /* we're killing a connection, we must apply a random delay before
1721 * retrying otherwise the other end will do the same and we can loop
1722 * for a while.
1723 */
1724 if (ps)
1725 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1726
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001727 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001728 appctx->ctx.peers.ptr = NULL;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001729 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001730}
1731
Willy Tarreau91d96282015-03-13 15:47:26 +01001732/* Pre-configures a peers frontend to accept incoming connections */
1733void peers_setup_frontend(struct proxy *fe)
1734{
1735 fe->last_change = now.tv_sec;
1736 fe->cap = PR_CAP_FE;
1737 fe->maxconn = 0;
1738 fe->conn_retries = CONN_RETRIES;
1739 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001740 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001741 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001742 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001743 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001744}
1745
Emeric Brun2b920a12010-09-23 18:30:22 +02001746/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001747 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001748 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001749static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001750{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001751 struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001752 struct proxy *p = l->frontend; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001753 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001754 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001755 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001756 struct task *t;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001757 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001758
Emeric Brunb3971ab2015-05-12 18:49:09 +02001759 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1760 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001761 s = NULL;
1762
1763 appctx = appctx_new(&peer_applet);
1764 if (!appctx)
1765 goto out_close;
1766
1767 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001768 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001769
Willy Tarreau4099a7c2015-04-05 00:39:55 +02001770 sess = session_new(p, l, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001771 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001772 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001773 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001774 }
1775
Willy Tarreau8baf9062015-04-05 00:46:36 +02001776 if ((t = task_new()) == NULL) {
Willy Tarreau15b5e142015-04-04 14:38:25 +02001777 Alert("out of memory in peer_session_create().\n");
1778 goto out_free_sess;
1779 }
Willy Tarreau8baf9062015-04-05 00:46:36 +02001780 t->nice = l->nice;
1781
Willy Tarreau73b65ac2015-04-08 18:26:29 +02001782 if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001783 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau8baf9062015-04-05 00:46:36 +02001784 goto out_free_task;
1785 }
1786
Willy Tarreau342bfb12015-04-05 01:35:34 +02001787 /* The tasks below are normally what is supposed to be done by
1788 * fe->accept().
1789 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001790 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001791
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001792 /* applet is waiting for data */
1793 si_applet_cant_get(&s->si[0]);
1794 appctx_wakeup(appctx);
1795
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001796 /* initiate an outgoing connection */
1797 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001798
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001799 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001800 * pre-initialized connection in si->conn.
1801 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001802 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001803 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001804
1805 conn_prepare(conn, peer->proto, peer->xprt);
1806 si_attach_conn(&s->si[1], conn);
1807
1808 conn->target = s->target = &s->be->obj_type;
1809 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001810 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001811 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001812
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001813 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001814
Emeric Brun2b920a12010-09-23 18:30:22 +02001815 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1816 p->feconn++;/* beconn will be increased later */
1817 jobs++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001818 if (!(s->sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001819 actconn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001820 totalconn++;
1821
Emeric Brunb3971ab2015-05-12 18:49:09 +02001822 peer->appctx = appctx;
Willy Tarreau9df94c22016-10-31 18:42:52 +01001823 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001824
1825 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001826 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001827 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001828 pool_free2(pool2_stream, s);
Willy Tarreau8baf9062015-04-05 00:46:36 +02001829 out_free_task:
1830 task_free(t);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001831 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001832 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001833 out_free_appctx:
1834 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001835 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001836 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001837}
1838
1839/*
1840 * Task processing function to manage re-connect and peer session
1841 * tasks wakeup on local update.
1842 */
Simon Horman96553772011-06-08 09:18:51 +09001843static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001844{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001845 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001846 struct peer *ps;
1847 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001848
1849 task->expire = TICK_ETERNITY;
1850
Emeric Brunb3971ab2015-05-12 18:49:09 +02001851 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001852 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001853 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001854 task_delete(peers->sync_task);
1855 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001856 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001857 return NULL;
1858 }
1859
Emeric Brun2b920a12010-09-23 18:30:22 +02001860 if (!stopping) {
1861 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001862
1863 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1864 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1865 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001866 /* Resync from local peer needed
1867 no peer was assigned for the lesson
1868 and no old local peer found
1869 or resync timeout expire */
1870
1871 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001872 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001873
1874 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001875 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001876 }
1877
1878 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001879 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001880 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001881 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001882 if (!ps->appctx) {
1883 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001884 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001885 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001886 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001887 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001888 tick_is_expired(ps->reconnect, now_ms))) {
1889 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001890 * or previous peer connection established with success
1891 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02001892 * and reconnection timer is expired */
1893
1894 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001895 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001896 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001897 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001898 /* If previous session failed during connection
1899 * but reconnection timer is not expired */
1900
1901 /* reschedule task for reconnect */
1902 task->expire = tick_first(task->expire, ps->reconnect);
1903 }
1904 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001905 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001906 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001907 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001908 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1909 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001910 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1911 /* Resync from a remote is needed
1912 * and no peer was assigned for lesson
1913 * and current peer may be up2date */
1914
1915 /* assign peer for the lesson */
1916 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001917 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001918
Willy Tarreau9df94c22016-10-31 18:42:52 +01001919 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001920 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001921 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001922 else {
1923 /* Awake session if there is data to push */
1924 for (st = ps->tables; st ; st = st->next) {
1925 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001926 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001927 appctx_wakeup(ps->appctx);
1928 break;
1929 }
1930 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001931 }
1932 /* else do nothing */
1933 } /* SUCCESSCODE */
1934 } /* !ps->peer->local */
1935 } /* for */
1936
1937 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001938 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1939 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1940 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001941 /* Resync from remote peer needed
1942 * no peer was assigned for the lesson
1943 * and resync timeout expire */
1944
1945 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001946 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001947 }
1948
Emeric Brunb3971ab2015-05-12 18:49:09 +02001949 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001950 /* Resync not finished*/
1951 /* reschedule task to resync timeout, to ended resync if needed */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001952 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001953 }
1954 } /* !stopping */
1955 else {
1956 /* soft stop case */
1957 if (task->state & TASK_WOKEN_SIGNAL) {
1958 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001959 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001960 /* add DO NOT STOP flag if not present */
1961 jobs++;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001962 peers->flags |= PEERS_F_DONOTSTOP;
1963 ps = peers->local;
1964 for (st = ps->tables; st ; st = st->next)
1965 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001966 }
1967
1968 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001969 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001970 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001971 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02001972 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001973 }
1974 }
1975 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001976
Emeric Brunb3971ab2015-05-12 18:49:09 +02001977 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001978 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001979 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001980 /* resync of new process was complete, current process can die now */
1981 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001982 peers->flags &= ~PEERS_F_DONOTSTOP;
1983 for (st = ps->tables; st ; st = st->next)
1984 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001985 }
1986 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01001987 else if (!ps->appctx) {
1988 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001989 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001990 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
1991 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
1992 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001993 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001994 * or previous peer connection was successfully established
1995 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02001996 * or during previous connect, peer replies a try again statuscode */
1997
1998 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001999 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002000 }
2001 else {
2002 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002003 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002004 /* unable to resync new process, current process can die now */
2005 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002006 peers->flags &= ~PEERS_F_DONOTSTOP;
2007 for (st = ps->tables; st ; st = st->next)
2008 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002009 }
2010 }
2011 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002012 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002013 /* current peer connection is active and established
2014 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002015 for (st = ps->tables; st ; st = st->next) {
2016 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002017 appctx_wakeup(ps->appctx);
2018 break;
2019 }
2020 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002021 }
2022 } /* stopping */
2023 /* Wakeup for re-connect */
2024 return task;
2025}
2026
Emeric Brunb3971ab2015-05-12 18:49:09 +02002027
Emeric Brun2b920a12010-09-23 18:30:22 +02002028/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002029 *
2030 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002031void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002032{
Emeric Brun2b920a12010-09-23 18:30:22 +02002033 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002034 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002035
Emeric Brun2b920a12010-09-23 18:30:22 +02002036 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 peers->peers_fe->maxconn += 3;
2038 }
2039
Willy Tarreau4348fad2012-09-20 16:48:07 +02002040 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2041 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002042 peers->sync_task = task_new();
2043 peers->sync_task->process = process_peer_sync;
2044 peers->sync_task->expire = TICK_ETERNITY;
2045 peers->sync_task->context = (void *)peers;
2046 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2047 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2048}
2049
2050
2051
2052/*
2053 * Function used to register a table for sync on a group of peers
2054 *
2055 */
2056void peers_register_table(struct peers *peers, struct stktable *table)
2057{
2058 struct shared_table *st;
2059 struct peer * curpeer;
2060 int id = 0;
2061
2062 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002063 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002064 st->table = table;
2065 st->next = curpeer->tables;
2066 if (curpeer->tables)
2067 id = curpeer->tables->local_id;
2068 st->local_id = id + 1;
2069
2070 curpeer->tables = st;
2071 }
2072
2073 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002074}
2075