blob: 10045c62c8f8cba1cbd97624fc4872f3a306044f [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
64 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020065
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020070
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010074#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010080#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
81#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
85#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
86#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
87#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 +020088
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010089#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
90#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020091
Frédéric Lécaille54bff832019-03-26 10:25:20 +010092#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
93#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010094#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
95
Emeric Brunb3971ab2015-05-12 18:49:09 +020096/*****************************/
97/* Sync message class */
98/*****************************/
99enum {
100 PEER_MSG_CLASS_CONTROL = 0,
101 PEER_MSG_CLASS_ERROR,
102 PEER_MSG_CLASS_STICKTABLE = 10,
103 PEER_MSG_CLASS_RESERVED = 255,
104};
105
106/*****************************/
107/* control message types */
108/*****************************/
109enum {
110 PEER_MSG_CTRL_RESYNCREQ = 0,
111 PEER_MSG_CTRL_RESYNCFINISHED,
112 PEER_MSG_CTRL_RESYNCPARTIAL,
113 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100114 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200115};
116
117/*****************************/
118/* error message types */
119/*****************************/
120enum {
121 PEER_MSG_ERR_PROTOCOL = 0,
122 PEER_MSG_ERR_SIZELIMIT,
123};
124
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100125/*
126 * Parameters used by functions to build peer protocol messages. */
127struct peer_prep_params {
128 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100129 struct peer *peer;
130 } hello;
131 struct {
132 unsigned int st1;
133 } error_status;
134 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100135 struct stksess *stksess;
136 struct shared_table *shared_table;
137 unsigned int updateid;
138 int use_identifier;
139 int use_timed;
140 } updt;
141 struct {
142 struct shared_table *shared_table;
143 } swtch;
144 struct {
145 struct shared_table *shared_table;
146 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100147 struct {
148 unsigned char head[2];
149 } control;
150 struct {
151 unsigned char head[2];
152 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100153};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200154
155/*******************************/
156/* stick table sync mesg types */
157/* Note: ids >= 128 contains */
158/* id message cotains data */
159/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200160#define PEER_MSG_STKT_UPDATE 0x80
161#define PEER_MSG_STKT_INCUPDATE 0x81
162#define PEER_MSG_STKT_DEFINE 0x82
163#define PEER_MSG_STKT_SWITCH 0x83
164#define PEER_MSG_STKT_ACK 0x84
165#define PEER_MSG_STKT_UPDATE_TIMED 0x85
166#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168/**********************************/
169/* Peer Session IO handler states */
170/**********************************/
171
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100172enum {
173 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
174 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
175 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
176 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100177 /* after this point, data were possibly exchanged */
178 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
179 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
180 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
181 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
182 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200183 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
184 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100185 PEER_SESS_ST_END, /* Killed session */
186};
Emeric Brun2b920a12010-09-23 18:30:22 +0200187
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100188/***************************************************/
189/* Peer Session status code - part of the protocol */
190/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200191
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100192#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
193#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200194
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100195#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200196
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100197#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200198
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100199#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
200#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
201#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
202#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200203
204#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200205#define PEER_MAJOR_VER 2
206#define PEER_MINOR_VER 1
207#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200208
Willy Tarreau6254a922019-01-29 17:45:23 +0100209static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200210struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100211static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200212
Emeric Brun18928af2017-03-29 16:32:53 +0200213/* This function encode an uint64 to 'dynamic' length format.
214 The encoded value is written at address *str, and the
215 caller must assure that size after *str is large enought.
216 At return, the *str is set at the next Byte after then
217 encoded integer. The function returns then length of the
218 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200219int intencode(uint64_t i, char **str) {
220 int idx = 0;
221 unsigned char *msg;
222
Emeric Brunb3971ab2015-05-12 18:49:09 +0200223 msg = (unsigned char *)*str;
224 if (i < 240) {
225 msg[0] = (unsigned char)i;
226 *str = (char *)&msg[idx+1];
227 return (idx+1);
228 }
229
230 msg[idx] =(unsigned char)i | 240;
231 i = (i - 240) >> 4;
232 while (i >= 128) {
233 msg[++idx] = (unsigned char)i | 128;
234 i = (i - 128) >> 7;
235 }
236 msg[++idx] = (unsigned char)i;
237 *str = (char *)&msg[idx+1];
238 return (idx+1);
239}
240
241
242/* This function returns the decoded integer or 0
243 if decode failed
244 *str point on the beginning of the integer to decode
245 at the end of decoding *str point on the end of the
246 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200247uint64_t intdecode(char **str, char *end)
248{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200249 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200250 uint64_t i;
251 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200252
253 if (!*str)
254 return 0;
255
256 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200257 if (msg >= (unsigned char *)end)
258 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200259
Emeric Brun18928af2017-03-29 16:32:53 +0200260 i = *(msg++);
261 if (i >= 240) {
262 shift = 4;
263 do {
264 if (msg >= (unsigned char *)end)
265 goto fail;
266 i += (uint64_t)*msg << shift;
267 shift += 7;
268 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200269 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100270 *str = (char *)msg;
271 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200272
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100273 fail:
274 *str = NULL;
275 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276}
Emeric Brun2b920a12010-09-23 18:30:22 +0200277
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100278/*
279 * Build a "hello" peer protocol message.
280 * Return the number of written bytes written to build this messages if succeeded,
281 * 0 if not.
282 */
283static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
284{
285 int min_ver, ret;
286 struct peer *peer;
287
288 peer = p->hello.peer;
289 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
290 /* Prepare headers */
291 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
292 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
293 if (ret >= size)
294 return 0;
295
296 return ret;
297}
298
299/*
300 * Build a "handshake succeeded" status message.
301 * Return the number of written bytes written to build this messages if succeeded,
302 * 0 if not.
303 */
304static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
305{
306 int ret;
307
308 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
309 if (ret >= size)
310 return 0;
311
312 return ret;
313}
314
315/*
316 * Build an error status message.
317 * Return the number of written bytes written to build this messages if succeeded,
318 * 0 if not.
319 */
320static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
321{
322 int ret;
323 unsigned int st1;
324
325 st1 = p->error_status.st1;
326 ret = snprintf(msg, size, "%d\n", st1);
327 if (ret >= size)
328 return 0;
329
330 return ret;
331}
332
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200333/* Set the stick-table UPDATE message type byte at <msg_type> address,
334 * depending on <use_identifier> and <use_timed> boolean parameters.
335 * Always successful.
336 */
337static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
338{
339 if (use_timed) {
340 if (use_identifier)
341 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
342 else
343 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
344 }
345 else {
346 if (use_identifier)
347 *msg_type = PEER_MSG_STKT_UPDATE;
348 else
349 *msg_type = PEER_MSG_STKT_INCUPDATE;
350 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200351}
Emeric Brun2b920a12010-09-23 18:30:22 +0200352/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200353 * This prepare the data update message on the stick session <ts>, <st> is the considered
354 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800355 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200356 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
357 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200358 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100359static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200360{
361 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200362 unsigned short datalen;
363 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200364 unsigned int data_type;
365 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100366 struct stksess *ts;
367 struct shared_table *st;
368 unsigned int updateid;
369 int use_identifier;
370 int use_timed;
371
372 ts = p->updt.stksess;
373 st = p->updt.shared_table;
374 updateid = p->updt.updateid;
375 use_identifier = p->updt.use_identifier;
376 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200377
378 cursor = datamsg = msg + 1 + 5;
379
Emeric Brun2b920a12010-09-23 18:30:22 +0200380 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200381
382 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200383 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200384 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200385 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200386
387 /* encode update identifier if needed */
388 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200389 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390 memcpy(cursor, &netinteger, sizeof(netinteger));
391 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200392 }
393
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200394 if (use_timed) {
395 netinteger = htonl(tick_remain(now_ms, ts->expire));
396 memcpy(cursor, &netinteger, sizeof(netinteger));
397 cursor += sizeof(netinteger);
398 }
399
Emeric Brunb3971ab2015-05-12 18:49:09 +0200400 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200401 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200402 int stlen = strlen((char *)ts->key.key);
403
Emeric Brunb3971ab2015-05-12 18:49:09 +0200404 intencode(stlen, &cursor);
405 memcpy(cursor, ts->key.key, stlen);
406 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200407 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200408 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200409 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200410 memcpy(cursor, &netinteger, sizeof(netinteger));
411 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200412 }
413 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200414 memcpy(cursor, ts->key.key, st->table->key_size);
415 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200416 }
417
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100418 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200419 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200420 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200421
Emeric Brun94900952015-06-11 18:25:54 +0200422 data_ptr = stktable_data_ptr(st->table, ts, data_type);
423 if (data_ptr) {
424 switch (stktable_data_types[data_type].std_type) {
425 case STD_T_SINT: {
426 int data;
427
428 data = stktable_data_cast(data_ptr, std_t_sint);
429 intencode(data, &cursor);
430 break;
431 }
432 case STD_T_UINT: {
433 unsigned int data;
434
435 data = stktable_data_cast(data_ptr, std_t_uint);
436 intencode(data, &cursor);
437 break;
438 }
439 case STD_T_ULL: {
440 unsigned long long data;
441
442 data = stktable_data_cast(data_ptr, std_t_ull);
443 intencode(data, &cursor);
444 break;
445 }
446 case STD_T_FRQP: {
447 struct freq_ctr_period *frqp;
448
449 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
450 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
451 intencode(frqp->curr_ctr, &cursor);
452 intencode(frqp->prev_ctr, &cursor);
453 break;
454 }
455 }
456 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200457 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100458 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459
460 /* Compute datalen */
461 datalen = (cursor - datamsg);
462
463 /* prepare message header */
464 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200465 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200466 cursor = &msg[2];
467 intencode(datalen, &cursor);
468
469 /* move data after header */
470 memmove(cursor, datamsg, datalen);
471
472 /* return header size + data_len */
473 return (cursor - msg) + datalen;
474}
475
476/*
477 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800478 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200479 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
480 * check size)
481 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100482static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483{
484 int len;
485 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200486 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200487 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200488 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200489 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100490 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100492 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200493 cursor = datamsg = msg + 2 + 5;
494
495 /* Encode data */
496
497 /* encode local id */
498 intencode(st->local_id, &cursor);
499
500 /* encode table name */
501 len = strlen(st->table->id);
502 intencode(len, &cursor);
503 memcpy(cursor, st->table->id, len);
504 cursor += len;
505
506 /* encode table type */
507
508 intencode(st->table->type, &cursor);
509
510 /* encode table key size */
511 intencode(st->table->key_size, &cursor);
512
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200513 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200515 /* encode available known data types in table */
516 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
517 if (st->table->data_ofs[data_type]) {
518 switch (stktable_data_types[data_type].std_type) {
519 case STD_T_SINT:
520 case STD_T_UINT:
521 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200522 data |= 1 << data_type;
523 break;
Emeric Brun94900952015-06-11 18:25:54 +0200524 case STD_T_FRQP:
525 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200526 intencode(data_type, &chunkq);
527 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200528 break;
529 }
530 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 }
532 intencode(data, &cursor);
533
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200534 /* Encode stick-table entries duration. */
535 intencode(st->table->expire, &cursor);
536
537 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 chunk->data = chunkq - chunkp;
539 memcpy(cursor, chunk->area, chunk->data);
540 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200541 }
542
Emeric Brunb3971ab2015-05-12 18:49:09 +0200543 /* Compute datalen */
544 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200545
Emeric Brunb3971ab2015-05-12 18:49:09 +0200546 /* prepare message header */
547 msg[0] = PEER_MSG_CLASS_STICKTABLE;
548 msg[1] = PEER_MSG_STKT_DEFINE;
549 cursor = &msg[2];
550 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200551
Emeric Brunb3971ab2015-05-12 18:49:09 +0200552 /* move data after header */
553 memmove(cursor, datamsg, datalen);
554
555 /* return header size + data_len */
556 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200557}
558
Emeric Brunb3971ab2015-05-12 18:49:09 +0200559/*
560 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
561 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800562 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200563 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
564 * check size)
565 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100566static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200567{
568 unsigned short datalen;
569 char *cursor, *datamsg;
570 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100571 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572
Emeric Brunb058f1c2015-09-22 15:50:18 +0200573 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200574
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100575 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200576 intencode(st->remote_id, &cursor);
577 netinteger = htonl(st->last_get);
578 memcpy(cursor, &netinteger, sizeof(netinteger));
579 cursor += sizeof(netinteger);
580
581 /* Compute datalen */
582 datalen = (cursor - datamsg);
583
584 /* prepare message header */
585 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200586 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200587 cursor = &msg[2];
588 intencode(datalen, &cursor);
589
590 /* move data after header */
591 memmove(cursor, datamsg, datalen);
592
593 /* return header size + data_len */
594 return (cursor - msg) + datalen;
595}
Emeric Brun2b920a12010-09-23 18:30:22 +0200596
597/*
598 * Callback to release a session with a peer
599 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200600static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200601{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200602 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200603 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200604 struct peer *peer = appctx->ctx.peers.ptr;
605 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200606
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100607 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100608 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200609 return;
610
611 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200612 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100613 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +0100614 _HA_ATOMIC_SUB(&connected_peers, 1);
615 _HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100616 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100617 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200618 /* Re-init current table pointers to force announcement on re-connect */
619 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200620 peer->appctx = NULL;
621 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200622 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200623 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
624 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200625
626 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100627 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 }
629 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200630 peer->flags &= PEER_TEACH_RESET;
631 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200632 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200634 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200635 }
636}
637
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200638/* Retrieve the major and minor versions of peers protocol
639 * announced by a remote peer. <str> is a null-terminated
640 * string with the following format: "<maj_ver>.<min_ver>".
641 */
642static int peer_get_version(const char *str,
643 unsigned int *maj_ver, unsigned int *min_ver)
644{
645 unsigned int majv, minv;
646 const char *pos, *saved;
647 const char *end;
648
649 saved = pos = str;
650 end = str + strlen(str);
651
652 majv = read_uint(&pos, end);
653 if (saved == pos || *pos++ != '.')
654 return -1;
655
656 saved = pos;
657 minv = read_uint(&pos, end);
658 if (saved == pos || pos != end)
659 return -1;
660
661 *maj_ver = majv;
662 *min_ver = minv;
663
664 return 0;
665}
Emeric Brun2b920a12010-09-23 18:30:22 +0200666
667/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100668 * Parse a line terminated by an optional '\r' character, followed by a mandatory
669 * '\n' character.
670 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
671 * a line could not be read because the communication channel is closed.
672 */
673static inline int peer_getline(struct appctx *appctx)
674{
675 int n;
676 struct stream_interface *si = appctx->owner;
677
678 n = co_getline(si_oc(si), trash.area, trash.size);
679 if (!n)
680 return 0;
681
682 if (n < 0 || trash.area[n - 1] != '\n') {
683 appctx->st0 = PEER_SESS_ST_END;
684 return -1;
685 }
686
687 if (n > 1 && (trash.area[n - 2] == '\r'))
688 trash.area[n - 2] = 0;
689 else
690 trash.area[n - 1] = 0;
691
692 co_skip(si_oc(si), n);
693
694 return n;
695}
696
697/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100698 * Send a message after having called <peer_prepare_msg> to build it.
699 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
700 * Returns -1 if there was not enough room left to send the message,
701 * any other negative returned value must be considered as an error with an appcxt st0
702 * returned value equal to PEER_SESS_ST_END.
703 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100704static inline int peer_send_msg(struct appctx *appctx,
705 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
706 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100707{
708 int ret, msglen;
709 struct stream_interface *si = appctx->owner;
710
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100711 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100712 if (!msglen) {
713 /* internal error: message does not fit in trash */
714 appctx->st0 = PEER_SESS_ST_END;
715 return 0;
716 }
717
718 /* message to buffer */
719 ret = ci_putblk(si_ic(si), trash.area, msglen);
720 if (ret <= 0) {
721 if (ret == -1) {
722 /* No more write possible */
723 si_rx_room_blk(si);
724 return -1;
725 }
726 appctx->st0 = PEER_SESS_ST_END;
727 }
728
729 return ret;
730}
731
732/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100733 * Send a hello message.
734 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
735 * Returns -1 if there was not enough room left to send the message,
736 * any other negative returned value must be considered as an error with an appcxt st0
737 * returned value equal to PEER_SESS_ST_END.
738 */
739static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
740{
741 struct peer_prep_params p = {
742 .hello.peer = peer,
743 };
744
745 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
746}
747
748/*
749 * Send a success peer handshake status message.
750 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
751 * Returns -1 if there was not enough room left to send the message,
752 * any other negative returned value must be considered as an error with an appcxt st0
753 * returned value equal to PEER_SESS_ST_END.
754 */
755static inline int peer_send_status_successmsg(struct appctx *appctx)
756{
757 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
758}
759
760/*
761 * Send a peer handshake status error message.
762 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
763 * Returns -1 if there was not enough room left to send the message,
764 * any other negative returned value must be considered as an error with an appcxt st0
765 * returned value equal to PEER_SESS_ST_END.
766 */
767static inline int peer_send_status_errormsg(struct appctx *appctx)
768{
769 struct peer_prep_params p = {
770 .error_status.st1 = appctx->st1,
771 };
772
773 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
774}
775
776/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100777 * Send a stick-table switch message.
778 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
779 * Returns -1 if there was not enough room left to send the message,
780 * any other negative returned value must be considered as an error with an appcxt st0
781 * returned value equal to PEER_SESS_ST_END.
782 */
783static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
784{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100785 struct peer_prep_params p = {
786 .swtch.shared_table = st,
787 };
788
789 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100790}
791
792/*
793 * Send a stick-table update acknowledgement message.
794 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
795 * Returns -1 if there was not enough room left to send the message,
796 * any other negative returned value must be considered as an error with an appcxt st0
797 * returned value equal to PEER_SESS_ST_END.
798 */
799static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
800{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100801 struct peer_prep_params p = {
802 .ack.shared_table = st,
803 };
804
805 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100806}
807
808/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100809 * Send a stick-table update message.
810 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
811 * Returns -1 if there was not enough room left to send the message,
812 * any other negative returned value must be considered as an error with an appcxt st0
813 * returned value equal to PEER_SESS_ST_END.
814 */
815static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
816 unsigned int updateid, int use_identifier, int use_timed)
817{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100818 struct peer_prep_params p = {
819 .updt.stksess = ts,
820 .updt.shared_table = st,
821 .updt.updateid = updateid,
822 .updt.use_identifier = use_identifier,
823 .updt.use_timed = use_timed,
824 };
825
826 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100827}
828
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100829/*
830 * Build a peer protocol control class message.
831 * Returns the number of written bytes used to build the message if succeeded,
832 * 0 if not.
833 */
834static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
835{
836 if (size < sizeof p->control.head)
837 return 0;
838
839 msg[0] = p->control.head[0];
840 msg[1] = p->control.head[1];
841
842 return 2;
843}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100844
845/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100846 * Send a stick-table synchronization request message.
847 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
848 * Returns -1 if there was not enough room left to send the message,
849 * any other negative returned value must be considered as an error with an appctx st0
850 * returned value equal to PEER_SESS_ST_END.
851 */
852static inline int peer_send_resync_reqmsg(struct appctx *appctx)
853{
854 struct peer_prep_params p = {
855 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
856 };
857
858 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
859}
860
861/*
862 * Send a stick-table synchronization confirmation message.
863 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
864 * Returns -1 if there was not enough room left to send the message,
865 * any other negative returned value must be considered as an error with an appctx st0
866 * returned value equal to PEER_SESS_ST_END.
867 */
868static inline int peer_send_resync_confirmsg(struct appctx *appctx)
869{
870 struct peer_prep_params p = {
871 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
872 };
873
874 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
875}
876
877/*
878 * Send a stick-table synchronization finished message.
879 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
880 * Returns -1 if there was not enough room left to send the message,
881 * any other negative returned value must be considered as an error with an appctx st0
882 * returned value equal to PEER_SESS_ST_END.
883 */
884static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
885{
886 struct peer_prep_params p = {
887 .control.head = { PEER_MSG_CLASS_CONTROL, },
888 };
889
890 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
891 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
892
893 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
894}
895
896/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100897 * Send a heartbeat message.
898 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
899 * Returns -1 if there was not enough room left to send the message,
900 * any other negative returned value must be considered as an error with an appctx st0
901 * returned value equal to PEER_SESS_ST_END.
902 */
903static inline int peer_send_heartbeatmsg(struct appctx *appctx)
904{
905 struct peer_prep_params p = {
906 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
907 };
908
909 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
910}
911
912/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100913 * Build a peer protocol error class message.
914 * Returns the number of written bytes used to build the message if succeeded,
915 * 0 if not.
916 */
917static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
918{
919 if (size < sizeof p->error.head)
920 return 0;
921
922 msg[0] = p->error.head[0];
923 msg[1] = p->error.head[1];
924
925 return 2;
926}
927
928/*
929 * Send a "size limit reached" error message.
930 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
931 * Returns -1 if there was not enough room left to send the message,
932 * any other negative returned value must be considered as an error with an appctx st0
933 * returned value equal to PEER_SESS_ST_END.
934 */
935static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
936{
937 struct peer_prep_params p = {
938 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
939 };
940
941 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
942}
943
944/*
945 * Send a "peer protocol" error message.
946 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
947 * Returns -1 if there was not enough room left to send the message,
948 * any other negative returned value must be considered as an error with an appctx st0
949 * returned value equal to PEER_SESS_ST_END.
950 */
951static inline int peer_send_error_protomsg(struct appctx *appctx)
952{
953 struct peer_prep_params p = {
954 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
955 };
956
957 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
958}
959
960/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100961 * Function used to lookup for recent stick-table updates associated with
962 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
963 */
964static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
965{
966 struct eb32_node *eb;
967
968 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
969 if (!eb) {
970 eb = eb32_first(&st->table->updates);
971 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
972 st->table->commitupdate = st->last_pushed = st->table->localupdate;
973 return NULL;
974 }
975 }
976
977 if ((int)(eb->key - st->table->localupdate) > 0) {
978 st->table->commitupdate = st->last_pushed = st->table->localupdate;
979 return NULL;
980 }
981
982 return eb32_entry(eb, struct stksess, upd);
983}
984
985/*
986 * Function used to lookup for recent stick-table updates associated with
987 * <st> shared stick-table during teach state 1 step.
988 */
989static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
990{
991 struct eb32_node *eb;
992
993 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
994 if (!eb) {
995 st->flags |= SHTABLE_F_TEACH_STAGE1;
996 eb = eb32_first(&st->table->updates);
997 if (eb)
998 st->last_pushed = eb->key - 1;
999 return NULL;
1000 }
1001
1002 return eb32_entry(eb, struct stksess, upd);
1003}
1004
1005/*
1006 * Function used to lookup for recent stick-table updates associated with
1007 * <st> shared stick-table during teach state 2 step.
1008 */
1009static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1010{
1011 struct eb32_node *eb;
1012
1013 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1014 if (!eb || eb->key > st->teaching_origin) {
1015 st->flags |= SHTABLE_F_TEACH_STAGE2;
1016 return NULL;
1017 }
1018
1019 return eb32_entry(eb, struct stksess, upd);
1020}
1021
1022/*
1023 * Generic function to emit update messages for <st> stick-table when a lesson must
1024 * be taught to the peer <p>.
1025 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1026 * this function, 0 if not.
1027 *
1028 * This function temporary unlock/lock <st> when it sends stick-table updates or
1029 * when decrementing its refcount in case of any error when it sends this updates.
1030 *
1031 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1032 * Returns -1 if there was not enough room left to send the message,
1033 * any other negative returned value must be considered as an error with an appcxt st0
1034 * returned value equal to PEER_SESS_ST_END.
1035 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1036 * unlocked if not already locked when entering this function.
1037 */
1038static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1039 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1040 struct shared_table *st, int locked)
1041{
1042 int ret, new_pushed, use_timed;
1043
1044 ret = 1;
1045 use_timed = 0;
1046 if (st != p->last_local_table) {
1047 ret = peer_send_switchmsg(st, appctx);
1048 if (ret <= 0)
1049 return ret;
1050
1051 p->last_local_table = st;
1052 }
1053
1054 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1055 use_timed = !(p->flags & PEER_F_DWNGRD);
1056
1057 /* We force new pushed to 1 to force identifier in update message */
1058 new_pushed = 1;
1059
1060 if (!locked)
1061 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1062
1063 while (1) {
1064 struct stksess *ts;
1065 unsigned updateid;
1066
1067 /* push local updates */
1068 ts = peer_stksess_lookup(st);
1069 if (!ts)
1070 break;
1071
1072 updateid = ts->upd.key;
1073 ts->ref_cnt++;
1074 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1075
1076 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1077 if (ret <= 0) {
1078 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1079 ts->ref_cnt--;
1080 if (!locked)
1081 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1082 return ret;
1083 }
1084
1085 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1086 ts->ref_cnt--;
1087 st->last_pushed = updateid;
1088
1089 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1090 (int)(st->last_pushed - st->table->commitupdate) > 0)
1091 st->table->commitupdate = st->last_pushed;
1092
1093 /* identifier may not needed in next update message */
1094 new_pushed = 0;
1095 }
1096
1097 out:
1098 if (!locked)
1099 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1100 return 1;
1101}
1102
1103/*
1104 * Function to emit update messages for <st> stick-table when a lesson must
1105 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1106 *
1107 * Note that <st> shared stick-table is locked when calling this function.
1108 *
1109 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1110 * Returns -1 if there was not enough room left to send the message,
1111 * any other negative returned value must be considered as an error with an appcxt st0
1112 * returned value equal to PEER_SESS_ST_END.
1113 */
1114static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1115 struct shared_table *st)
1116{
1117 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1118}
1119
1120/*
1121 * Function to emit update messages for <st> stick-table when a lesson must
1122 * be taught to the peer <p> during teach state 1 step.
1123 *
1124 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1125 * Returns -1 if there was not enough room left to send the message,
1126 * any other negative returned value must be considered as an error with an appcxt st0
1127 * returned value equal to PEER_SESS_ST_END.
1128 */
1129static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1130 struct shared_table *st)
1131{
1132 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1133}
1134
1135/*
1136 * Function to emit update messages for <st> stick-table when a lesson must
1137 * be taught to the peer <p> during teach state 1 step.
1138 *
1139 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1140 * Returns -1 if there was not enough room left to send the message,
1141 * any other negative returned value must be considered as an error with an appcxt st0
1142 * returned value equal to PEER_SESS_ST_END.
1143 */
1144static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1145 struct shared_table *st)
1146{
1147 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1148}
1149
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001150
1151/*
1152 * Function used to parse a stick-table update message after it has been received
1153 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1154 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1155 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1156 * was encountered.
1157 * <exp> must be set if the stick-table entry expires.
1158 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1159 * messages, in this case the stick-table udpate message is received with a stick-table
1160 * update ID.
1161 * <totl> is the length of the stick-table update message computed upon receipt.
1162 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001163static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1164 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001165{
1166 struct stream_interface *si = appctx->owner;
1167 struct shared_table *st = p->remote_table;
1168 struct stksess *ts, *newts;
1169 uint32_t update;
1170 int expire;
1171 unsigned int data_type;
1172 void *data_ptr;
1173
1174 /* Here we have data message */
1175 if (!st)
1176 goto ignore_msg;
1177
1178 expire = MS_TO_TICKS(st->table->expire);
1179
1180 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001181 if (msg_len < sizeof(update))
1182 goto malformed_exit;
1183
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001184 memcpy(&update, *msg_cur, sizeof(update));
1185 *msg_cur += sizeof(update);
1186 st->last_get = htonl(update);
1187 }
1188 else {
1189 st->last_get++;
1190 }
1191
1192 if (exp) {
1193 size_t expire_sz = sizeof expire;
1194
Willy Tarreau1e82a142019-01-29 11:08:06 +01001195 if (*msg_cur + expire_sz > msg_end)
1196 goto malformed_exit;
1197
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001198 memcpy(&expire, *msg_cur, expire_sz);
1199 *msg_cur += expire_sz;
1200 expire = ntohl(expire);
1201 }
1202
1203 newts = stksess_new(st->table, NULL);
1204 if (!newts)
1205 goto ignore_msg;
1206
1207 if (st->table->type == SMP_T_STR) {
1208 unsigned int to_read, to_store;
1209
1210 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001211 if (!*msg_cur)
1212 goto malformed_free_newts;
1213
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001214 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001215 if (*msg_cur + to_store > msg_end)
1216 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001217
1218 memcpy(newts->key.key, *msg_cur, to_store);
1219 newts->key.key[to_store] = 0;
1220 *msg_cur += to_read;
1221 }
1222 else if (st->table->type == SMP_T_SINT) {
1223 unsigned int netinteger;
1224
Willy Tarreau1e82a142019-01-29 11:08:06 +01001225 if (*msg_cur + sizeof(netinteger) > msg_end)
1226 goto malformed_free_newts;
1227
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001228 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1229 netinteger = ntohl(netinteger);
1230 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1231 *msg_cur += sizeof(netinteger);
1232 }
1233 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001234 if (*msg_cur + st->table->key_size > msg_end)
1235 goto malformed_free_newts;
1236
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001237 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1238 *msg_cur += st->table->key_size;
1239 }
1240
1241 /* lookup for existing entry */
1242 ts = stktable_set_entry(st->table, newts);
1243 if (ts != newts) {
1244 stksess_free(st->table, newts);
1245 newts = NULL;
1246 }
1247
1248 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1249
1250 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001251 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001252
1253 if (!((1 << data_type) & st->remote_data))
1254 continue;
1255
Willy Tarreau1e82a142019-01-29 11:08:06 +01001256 decoded_int = intdecode(msg_cur, msg_end);
1257 if (!*msg_cur)
1258 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001259
Willy Tarreau1e82a142019-01-29 11:08:06 +01001260 switch (stktable_data_types[data_type].std_type) {
1261 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001262 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1263 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001264 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001265 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001266
Willy Tarreau1e82a142019-01-29 11:08:06 +01001267 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001268 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1269 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001270 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001271 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001272
Willy Tarreau1e82a142019-01-29 11:08:06 +01001273 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001274 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1275 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001276 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001277 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001278
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001279 case STD_T_FRQP: {
1280 struct freq_ctr_period data;
1281
1282 /* First bit is reserved for the freq_ctr_period lock
1283 Note: here we're still protected by the stksess lock
1284 so we don't need to update the update the freq_ctr_period
1285 using its internal lock */
1286
Willy Tarreau1e82a142019-01-29 11:08:06 +01001287 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001288 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001289 if (!*msg_cur)
1290 goto malformed_unlock;
1291
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001292 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001293 if (!*msg_cur)
1294 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001295
1296 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1297 if (data_ptr)
1298 stktable_data_cast(data_ptr, std_t_frqp) = data;
1299 break;
1300 }
1301 }
1302 }
1303 /* Force new expiration */
1304 ts->expire = tick_add(now_ms, expire);
1305
1306 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1307 stktable_touch_remote(st->table, ts, 1);
1308 return 1;
1309
1310 ignore_msg:
1311 /* skip consumed message */
1312 co_skip(si_oc(si), totl);
1313 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001314
1315 malformed_unlock:
1316 /* malformed message */
1317 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1318 stktable_touch_remote(st->table, ts, 1);
1319 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1320 return 0;
1321
1322 malformed_free_newts:
1323 /* malformed message */
1324 stksess_free(st->table, newts);
1325 malformed_exit:
1326 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1327 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001328}
1329
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001330/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001331 * Function used to parse a stick-table update acknowledgement message after it
1332 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1333 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1334 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1335 * was encountered.
1336 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1337 */
1338static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1339 char **msg_cur, char *msg_end)
1340{
1341 /* ack message */
1342 uint32_t table_id ;
1343 uint32_t update;
1344 struct shared_table *st;
1345
1346 table_id = intdecode(msg_cur, msg_end);
1347 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1348 /* malformed message */
1349 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1350 return 0;
1351 }
1352
1353 memcpy(&update, *msg_cur, sizeof(update));
1354 update = ntohl(update);
1355
1356 for (st = p->tables; st; st = st->next) {
1357 if (st->local_id == table_id) {
1358 st->update = update;
1359 break;
1360 }
1361 }
1362
1363 return 1;
1364}
1365
1366/*
1367 * Function used to parse a stick-table switch message after it has been received
1368 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1369 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1370 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1371 * was encountered.
1372 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1373 */
1374static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1375 char **msg_cur, char *msg_end)
1376{
1377 struct shared_table *st;
1378 int table_id;
1379
1380 table_id = intdecode(msg_cur, msg_end);
1381 if (!*msg_cur) {
1382 /* malformed message */
1383 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1384 return 0;
1385 }
1386
1387 p->remote_table = NULL;
1388 for (st = p->tables; st; st = st->next) {
1389 if (st->remote_id == table_id) {
1390 p->remote_table = st;
1391 break;
1392 }
1393 }
1394
1395 return 1;
1396}
1397
1398/*
1399 * Function used to parse a stick-table definition message after it has been received
1400 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1401 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1402 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1403 * was encountered.
1404 * <totl> is the length of the stick-table update message computed upon receipt.
1405 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1406 */
1407static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1408 char **msg_cur, char *msg_end, int totl)
1409{
1410 struct stream_interface *si = appctx->owner;
1411 int table_id_len;
1412 struct shared_table *st;
1413 int table_type;
1414 int table_keylen;
1415 int table_id;
1416 uint64_t table_data;
1417
1418 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001419 if (!*msg_cur)
1420 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001421
1422 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001423 if (!*msg_cur)
1424 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001425
1426 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001427 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1428 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001429
1430 for (st = p->tables; st; st = st->next) {
1431 /* Reset IDs */
1432 if (st->remote_id == table_id)
1433 st->remote_id = 0;
1434
1435 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1436 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1437 p->remote_table = st;
1438 }
1439
1440 if (!p->remote_table)
1441 goto ignore_msg;
1442
1443 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001444 if (*msg_cur >= msg_end)
1445 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001446
1447 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001448 if (!*msg_cur)
1449 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001450
1451 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001452 if (!*msg_cur)
1453 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001454
1455 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001456 if (!*msg_cur)
1457 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001458
1459 if (p->remote_table->table->type != table_type
1460 || p->remote_table->table->key_size != table_keylen) {
1461 p->remote_table = NULL;
1462 goto ignore_msg;
1463 }
1464
1465 p->remote_table->remote_data = table_data;
1466 p->remote_table->remote_id = table_id;
1467 return 1;
1468
1469 ignore_msg:
1470 co_skip(si_oc(si), totl);
1471 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001472
1473 malformed_exit:
1474 /* malformed message */
1475 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1476 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001477}
1478
1479/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001480 * Receive a stick-table message.
1481 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1482 * -1 if there was an error updating the appctx state st0 accordingly.
1483 */
1484static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1485 uint32_t *msg_len, int *totl)
1486{
1487 int reql;
1488 struct stream_interface *si = appctx->owner;
1489
1490 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1491 if (reql <= 0) /* closed or EOL not found */
1492 goto incomplete;
1493
1494 *totl += reql;
1495
1496 if ((unsigned int)msg_head[1] < 128)
1497 return 1;
1498
1499 /* Read and Decode message length */
1500 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1501 if (reql <= 0) /* closed */
1502 goto incomplete;
1503
1504 *totl += reql;
1505
1506 if ((unsigned int)msg_head[2] < 240) {
1507 *msg_len = msg_head[2];
1508 }
1509 else {
1510 int i;
1511 char *cur;
1512 char *end;
1513
1514 for (i = 3 ; i < msg_head_sz ; i++) {
1515 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1516 if (reql <= 0) /* closed */
1517 goto incomplete;
1518
1519 *totl += reql;
1520
1521 if (!(msg_head[i] & 0x80))
1522 break;
1523 }
1524
1525 if (i == msg_head_sz) {
1526 /* malformed message */
1527 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1528 return -1;
1529 }
1530 end = msg_head + msg_head_sz;
1531 cur = &msg_head[2];
1532 *msg_len = intdecode(&cur, end);
1533 if (!cur) {
1534 /* malformed message */
1535 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1536 return -1;
1537 }
1538 }
1539
1540 /* Read message content */
1541 if (*msg_len) {
1542 if (*msg_len > trash.size) {
1543 /* Status code is not success, abort */
1544 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1545 return -1;
1546 }
1547
1548 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1549 if (reql <= 0) /* closed */
1550 goto incomplete;
1551 *totl += reql;
1552 }
1553
1554 return 1;
1555
1556 incomplete:
1557 if (reql < 0) {
1558 /* there was an error */
1559 appctx->st0 = PEER_SESS_ST_END;
1560 return -1;
1561 }
1562
1563 return 0;
1564}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001565
1566/*
1567 * Treat the awaited message with <msg_head> as header.*
1568 * Return 1 if succeeded, 0 if not.
1569 */
1570static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1571 char **msg_cur, char *msg_end, int msg_len, int totl)
1572{
1573 struct stream_interface *si = appctx->owner;
1574 struct stream *s = si_strm(si);
1575 struct peers *peers = strm_fe(s)->parent;
1576
1577 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1578 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1579 struct shared_table *st;
1580 /* Reset message: remote need resync */
1581
1582 /* prepare tables fot a global push */
1583 for (st = peer->tables; st; st = st->next) {
1584 st->teaching_origin = st->last_pushed = st->table->update;
1585 st->flags = 0;
1586 }
1587
1588 /* reset teaching flags to 0 */
1589 peer->flags &= PEER_TEACH_RESET;
1590
1591 /* flag to start to teach lesson */
1592 peer->flags |= PEER_F_TEACH_PROCESS;
1593 }
1594 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1595 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1596 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1597 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1598 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1599 }
1600 peer->confirm++;
1601 }
1602 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1603 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1604 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1605 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1606
1607 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001608 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001609 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1610 }
1611 peer->confirm++;
1612 }
1613 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1614 struct shared_table *st;
1615
1616 /* If stopping state */
1617 if (stopping) {
1618 /* Close session, push resync no more needed */
1619 peer->flags |= PEER_F_TEACH_COMPLETE;
1620 appctx->st0 = PEER_SESS_ST_END;
1621 return 0;
1622 }
1623 for (st = peer->tables; st; st = st->next) {
1624 st->update = st->last_pushed = st->teaching_origin;
1625 st->flags = 0;
1626 }
1627
1628 /* reset teaching flags to 0 */
1629 peer->flags &= PEER_TEACH_RESET;
1630 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001631 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001632 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001633 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001634 }
1635 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1636 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1637 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1638 return 0;
1639 }
1640 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1641 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1642 return 0;
1643 }
1644 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1645 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1646 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1647 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1648 int update, expire;
1649
1650 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1651 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1652 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1653 msg_cur, msg_end, msg_len, totl))
1654 return 0;
1655
1656 }
1657 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1658 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1659 return 0;
1660 }
1661 }
1662 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1663 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1664 return 0;
1665 }
1666
1667 return 1;
1668}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001669
1670
1671/*
1672 * Send any message to <peer> peer.
1673 * Returns 1 if succeeded, or -1 or 0 if failed.
1674 * -1 means an internal error occured, 0 is for a peer protocol error leading
1675 * to a peer state change (from the peer I/O handler point of view).
1676 */
1677static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1678{
1679 int repl;
1680 struct stream_interface *si = appctx->owner;
1681 struct stream *s = si_strm(si);
1682 struct peers *peers = strm_fe(s)->parent;
1683
1684 /* Need to request a resync */
1685 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1686 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1687 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1688
1689 repl = peer_send_resync_reqmsg(appctx);
1690 if (repl <= 0)
1691 return repl;
1692
1693 peers->flags |= PEERS_F_RESYNC_PROCESS;
1694 }
1695
1696 /* Nothing to read, now we start to write */
1697 if (peer->tables) {
1698 struct shared_table *st;
1699 struct shared_table *last_local_table;
1700
1701 last_local_table = peer->last_local_table;
1702 if (!last_local_table)
1703 last_local_table = peer->tables;
1704 st = last_local_table->next;
1705
1706 while (1) {
1707 if (!st)
1708 st = peer->tables;
1709
1710 /* It remains some updates to ack */
1711 if (st->last_get != st->last_acked) {
1712 repl = peer_send_ackmsg(st, appctx);
1713 if (repl <= 0)
1714 return repl;
1715
1716 st->last_acked = st->last_get;
1717 }
1718
1719 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1720 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1721 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1722 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1723
1724 repl = peer_send_teach_process_msgs(appctx, peer, st);
1725 if (repl <= 0) {
1726 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1727 return repl;
1728 }
1729 }
1730 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1731 }
1732 else {
1733 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1734 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1735 if (repl <= 0)
1736 return repl;
1737 }
1738
1739 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1740 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1741 if (repl <= 0)
1742 return repl;
1743 }
1744 }
1745
1746 if (st == last_local_table)
1747 break;
1748 st = st->next;
1749 }
1750 }
1751
1752 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1753 repl = peer_send_resync_finishedmsg(appctx, peer);
1754 if (repl <= 0)
1755 return repl;
1756
1757 /* flag finished message sent */
1758 peer->flags |= PEER_F_TEACH_FINISHED;
1759 }
1760
1761 /* Confirm finished or partial messages */
1762 while (peer->confirm) {
1763 repl = peer_send_resync_confirmsg(appctx);
1764 if (repl <= 0)
1765 return repl;
1766
1767 peer->confirm--;
1768 }
1769
1770 return 1;
1771}
1772
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001773/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001774 * Read and parse a first line of a "hello" peer protocol message.
1775 * Returns 0 if could not read a line, -1 if there was a read error or
1776 * the line is malformed, 1 if succeeded.
1777 */
1778static inline int peer_getline_version(struct appctx *appctx,
1779 unsigned int *maj_ver, unsigned int *min_ver)
1780{
1781 int reql;
1782
1783 reql = peer_getline(appctx);
1784 if (!reql)
1785 return 0;
1786
1787 if (reql < 0)
1788 return -1;
1789
1790 /* test protocol */
1791 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1792 appctx->st0 = PEER_SESS_ST_EXIT;
1793 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1794 return -1;
1795 }
1796 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1797 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1798 appctx->st0 = PEER_SESS_ST_EXIT;
1799 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1800 return -1;
1801 }
1802
1803 return 1;
1804}
1805
1806/*
1807 * Read and parse a second line of a "hello" peer protocol message.
1808 * Returns 0 if could not read a line, -1 if there was a read error or
1809 * the line is malformed, 1 if succeeded.
1810 */
1811static inline int peer_getline_host(struct appctx *appctx)
1812{
1813 int reql;
1814
1815 reql = peer_getline(appctx);
1816 if (!reql)
1817 return 0;
1818
1819 if (reql < 0)
1820 return -1;
1821
1822 /* test hostname match */
1823 if (strcmp(localpeer, trash.area) != 0) {
1824 appctx->st0 = PEER_SESS_ST_EXIT;
1825 appctx->st1 = PEER_SESS_SC_ERRHOST;
1826 return -1;
1827 }
1828
1829 return 1;
1830}
1831
1832/*
1833 * Read and parse a last line of a "hello" peer protocol message.
1834 * Returns 0 if could not read a character, -1 if there was a read error or
1835 * the line is malformed, 1 if succeeded.
1836 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1837 */
1838static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1839{
1840 char *p;
1841 int reql;
1842 struct peer *peer;
1843 struct stream_interface *si = appctx->owner;
1844 struct stream *s = si_strm(si);
1845 struct peers *peers = strm_fe(s)->parent;
1846
1847 reql = peer_getline(appctx);
1848 if (!reql)
1849 return 0;
1850
1851 if (reql < 0)
1852 return -1;
1853
1854 /* parse line "<peer name> <pid> <relative_pid>" */
1855 p = strchr(trash.area, ' ');
1856 if (!p) {
1857 appctx->st0 = PEER_SESS_ST_EXIT;
1858 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1859 return -1;
1860 }
1861 *p = 0;
1862
1863 /* lookup known peer */
1864 for (peer = peers->remote; peer; peer = peer->next) {
1865 if (strcmp(peer->id, trash.area) == 0)
1866 break;
1867 }
1868
1869 /* if unknown peer */
1870 if (!peer) {
1871 appctx->st0 = PEER_SESS_ST_EXIT;
1872 appctx->st1 = PEER_SESS_SC_ERRPEER;
1873 return -1;
1874 }
1875 *curpeer = peer;
1876
1877 return 1;
1878}
1879
1880/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001881 * Init <peer> peer after having accepted it at peer protocol level.
1882 */
1883static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1884{
1885 struct shared_table *st;
1886
1887 /* Register status code */
1888 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1889
1890 /* Awake main task */
1891 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1892
1893 /* Init confirm counter */
1894 peer->confirm = 0;
1895
1896 /* Init cursors */
1897 for (st = peer->tables; st ; st = st->next) {
1898 st->last_get = st->last_acked = 0;
1899 st->teaching_origin = st->last_pushed = st->update;
1900 }
1901
1902 /* reset teaching and learning flags to 0 */
1903 peer->flags &= PEER_TEACH_RESET;
1904 peer->flags &= PEER_LEARN_RESET;
1905
1906 /* if current peer is local */
1907 if (peer->local) {
1908 /* if current host need resyncfrom local and no process assined */
1909 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1910 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1911 /* assign local peer for a lesson, consider lesson already requested */
1912 peer->flags |= PEER_F_LEARN_ASSIGN;
1913 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1914 }
1915
1916 }
1917 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1918 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1919 /* assign peer for a lesson */
1920 peer->flags |= PEER_F_LEARN_ASSIGN;
1921 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1922 }
1923}
1924
1925/*
1926 * Init <peer> peer after having connected it at peer protocol level.
1927 */
1928static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1929{
1930 struct shared_table *st;
1931
1932 /* Init cursors */
1933 for (st = peer->tables; st ; st = st->next) {
1934 st->last_get = st->last_acked = 0;
1935 st->teaching_origin = st->last_pushed = st->update;
1936 }
1937
1938 /* Init confirm counter */
1939 peer->confirm = 0;
1940
1941 /* reset teaching and learning flags to 0 */
1942 peer->flags &= PEER_TEACH_RESET;
1943 peer->flags &= PEER_LEARN_RESET;
1944
1945 /* If current peer is local */
1946 if (peer->local) {
1947 /* flag to start to teach lesson */
1948 peer->flags |= PEER_F_TEACH_PROCESS;
1949 }
1950 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1951 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1952 /* If peer is remote and resync from remote is needed,
1953 and no peer currently assigned */
1954
1955 /* assign peer for a lesson */
1956 peer->flags |= PEER_F_LEARN_ASSIGN;
1957 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1958 }
1959}
1960
1961/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001962 * IO Handler to handle message exchance with a peer
1963 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001964static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001965{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001966 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001967 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001968 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001969 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001970 int reql = 0;
1971 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001972 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001973 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001974
Joseph Herlant82b2f542018-11-15 12:19:14 -08001975 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001976 if (si_ic(si)->buf.size == 0) {
1977 si_rx_room_blk(si);
1978 goto out;
1979 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001980
Emeric Brun2b920a12010-09-23 18:30:22 +02001981 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001982 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001983switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001984 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001985 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001986 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001987 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001988 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001989 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001990 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001991 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001992 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001993 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
1994 if (reql <= 0) {
1995 if (!reql)
1996 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001997 goto switchstate;
1998 }
1999
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002000 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002001 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002002 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002003 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002004 reql = peer_getline_host(appctx);
2005 if (reql <= 0) {
2006 if (!reql)
2007 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002008 goto switchstate;
2009 }
2010
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002011 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002012 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002013 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002014 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002015 reql = peer_getline_last(appctx, &curpeer);
2016 if (reql <= 0) {
2017 if (!reql)
2018 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002019 goto switchstate;
2020 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002021
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002023 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002024 if (curpeer->local) {
2025 /* Local connection, reply a retry */
2026 appctx->st0 = PEER_SESS_ST_EXIT;
2027 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2028 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002029 }
Emeric Brun80527f52017-06-19 17:46:37 +02002030
2031 /* we're killing a connection, we must apply a random delay before
2032 * retrying otherwise the other end will do the same and we can loop
2033 * for a while.
2034 */
2035 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002036 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002038 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2039 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2040 curpeer->flags |= PEER_F_DWNGRD;
2041 }
2042 else {
2043 curpeer->flags &= ~PEER_F_DWNGRD;
2044 }
2045 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002046 curpeer->appctx = appctx;
2047 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002048 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002049 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002050 /* fall through */
2051 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002052 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002053 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002054 if (!curpeer) {
2055 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002056 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002057 if (curpeer->appctx != appctx) {
2058 appctx->st0 = PEER_SESS_ST_END;
2059 goto switchstate;
2060 }
2061 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002062
2063 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002064 if (repl <= 0) {
2065 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002066 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002067 goto switchstate;
2068 }
2069
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002070 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002071
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002073 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002074 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002075 goto switchstate;
2076 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002077 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002078 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002079 if (!curpeer) {
2080 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002081 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002082 if (curpeer->appctx != appctx) {
2083 appctx->st0 = PEER_SESS_ST_END;
2084 goto switchstate;
2085 }
2086 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002087
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002088 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 if (repl <= 0) {
2090 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002091 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002092 goto switchstate;
2093 }
2094
2095 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002096 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002097 /* fall through */
2098 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002099 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002100 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002101 if (!curpeer) {
2102 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002103 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002104 if (curpeer->appctx != appctx) {
2105 appctx->st0 = PEER_SESS_ST_END;
2106 goto switchstate;
2107 }
2108 }
2109
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002110 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002111 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002112
Frédéric Lécaillece025572019-01-21 13:38:06 +01002113 reql = peer_getline(appctx);
2114 if (!reql)
2115 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002116
Frédéric Lécaillece025572019-01-21 13:38:06 +01002117 if (reql < 0)
2118 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002119
2120 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002121 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002122
2123 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002124 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002125
2126 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002127 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002128 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002129 }
2130 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002131 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2132 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002133 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002134 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002135 goto switchstate;
2136 }
Olivier Houcharded879892019-03-08 18:53:43 +01002137 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002138 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002139 /* fall through */
2140 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002141 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002142 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002143 char *msg_cur = trash.area;
2144 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002145 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002146 int totl = 0;
2147
Willy Tarreau2d372c22018-11-05 17:12:27 +01002148 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002149 if (!curpeer) {
2150 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002151 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002152 if (curpeer->appctx != appctx) {
2153 appctx->st0 = PEER_SESS_ST_END;
2154 goto switchstate;
2155 }
2156 }
2157
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002158 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2159 if (reql <= 0) {
2160 if (reql == -1)
2161 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002162 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002163 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002164
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002165 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002166 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002167 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002168
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002169 curpeer->flags |= PEER_F_ALIVE;
2170
Emeric Brun2b920a12010-09-23 18:30:22 +02002171 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002172 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002173 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002174 goto switchstate;
2175
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002176send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002177 if (curpeer->flags & PEER_F_HEARTBEAT) {
2178 curpeer->flags &= ~PEER_F_HEARTBEAT;
2179 repl = peer_send_heartbeatmsg(appctx);
2180 if (repl <= 0) {
2181 if (repl == -1)
2182 goto out;
2183 goto switchstate;
2184 }
2185 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002186 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002187 repl = peer_send_msgs(appctx, curpeer);
2188 if (repl <= 0) {
2189 if (repl == -1)
2190 goto out;
2191 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002192 }
2193
Emeric Brun2b920a12010-09-23 18:30:22 +02002194 /* noting more to do */
2195 goto out;
2196 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002197 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002198 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002199 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002200 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002201 if (peer_send_status_errormsg(appctx) == -1)
2202 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002203 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002204 goto switchstate;
2205 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002206 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002207 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002208 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002209 if (peer_send_error_size_limitmsg(appctx) == -1)
2210 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002211 appctx->st0 = PEER_SESS_ST_END;
2212 goto switchstate;
2213 }
2214 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002215 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002216 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002217 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002218 if (peer_send_error_protomsg(appctx) == -1)
2219 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002220 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002221 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002222 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002223 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002224 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002225 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002226 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002227 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002228 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002229 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002230 curpeer = NULL;
2231 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002232 si_shutw(si);
2233 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002234 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002235 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002236 }
2237 }
2238 }
2239out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002240 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002241
2242 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002243 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002244 return;
2245}
2246
Willy Tarreau30576452015-04-13 13:50:30 +02002247static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002248 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002249 .name = "<PEER>", /* used for logging */
2250 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002251 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002252};
Emeric Brun2b920a12010-09-23 18:30:22 +02002253
2254/*
2255 * Use this function to force a close of a peer session
2256 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002257static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002258{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002259 /* Note that the peer sessions which have just been created
2260 * (->st0 == PEER_SESS_ST_CONNECT) must not
2261 * be shutdown, if not, the TCP session will never be closed
2262 * and stay in CLOSE_WAIT state after having been closed by
2263 * the remote side.
2264 */
2265 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 return;
2267
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002268 if (appctx->applet != &peer_applet)
2269 return;
2270
Willy Tarreau2d372c22018-11-05 17:12:27 +01002271 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002272 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002273 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002274 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002275}
2276
Willy Tarreau91d96282015-03-13 15:47:26 +01002277/* Pre-configures a peers frontend to accept incoming connections */
2278void peers_setup_frontend(struct proxy *fe)
2279{
2280 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002281 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002282 fe->maxconn = 0;
2283 fe->conn_retries = CONN_RETRIES;
2284 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002285 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002286 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002287 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002288 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002289}
2290
Emeric Brun2b920a12010-09-23 18:30:22 +02002291/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002292 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002293 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002294static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002295{
Willy Tarreau04b92862017-09-15 11:01:04 +02002296 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002298 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002299 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002300 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002301 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002302
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002303 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002304 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002305 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002306 s = NULL;
2307
Emeric Brun1138fd02017-06-19 12:38:55 +02002308 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002309 if (!appctx)
2310 goto out_close;
2311
2312 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002313 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002314
Willy Tarreau04b92862017-09-15 11:01:04 +02002315 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002316 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002317 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002318 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002319 }
2320
Willy Tarreau87787ac2017-08-28 16:22:54 +02002321 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002322 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002323 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002324 }
2325
Willy Tarreau342bfb12015-04-05 01:35:34 +02002326 /* The tasks below are normally what is supposed to be done by
2327 * fe->accept().
2328 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002329 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002330
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002331 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002332 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002333 appctx_wakeup(appctx);
2334
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002335 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002336 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002337 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002338
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002339 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002340 * pre-initialized connection in si->conn.
2341 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002342 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002343 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002344
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002345 if (unlikely((cs = cs_new(conn)) == NULL))
2346 goto out_free_conn;
2347
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002348 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002349 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2350
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002351 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002352 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2353 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002354 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002355
Emeric Brun2b920a12010-09-23 18:30:22 +02002356 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002357 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002358
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002359 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002360
Emeric Brunb3971ab2015-05-12 18:49:09 +02002361 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002362 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002363 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002364 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002365
2366 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002367out_free_cs:
2368 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002369 out_free_conn:
2370 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002371 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002372 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002373 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002374 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002375 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002376 out_free_appctx:
2377 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002378 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002379 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002380}
2381
2382/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002383 * Task processing function to manage re-connect, peer session
2384 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002385 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002386static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002387{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002388 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002389 struct peer *ps;
2390 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002391
2392 task->expire = TICK_ETERNITY;
2393
Emeric Brunb3971ab2015-05-12 18:49:09 +02002394 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002395 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002396 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002397 task_delete(peers->sync_task);
2398 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002399 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002400 return NULL;
2401 }
2402
Emeric Brun80527f52017-06-19 17:46:37 +02002403 /* Acquire lock for all peers of the section */
2404 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002405 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002406
Emeric Brun2b920a12010-09-23 18:30:22 +02002407 if (!stopping) {
2408 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002409
2410 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2411 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2412 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002413 /* Resync from local peer needed
2414 no peer was assigned for the lesson
2415 and no old local peer found
2416 or resync timeout expire */
2417
2418 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002419 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002420
2421 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002422 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002423 }
2424
2425 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002426 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002427 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002428 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002429 if (!ps->appctx) {
2430 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002431 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002432 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002433 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002434 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002435 tick_is_expired(ps->reconnect, now_ms))) {
2436 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002437 * or previous peer connection established with success
2438 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002439 * and reconnection timer is expired */
2440
2441 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002442 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002443 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002444 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002445 /* If previous session failed during connection
2446 * but reconnection timer is not expired */
2447
2448 /* reschedule task for reconnect */
2449 task->expire = tick_first(task->expire, ps->reconnect);
2450 }
2451 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002452 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002453 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002454 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002455 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2456 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002457 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2458 /* Resync from a remote is needed
2459 * and no peer was assigned for lesson
2460 * and current peer may be up2date */
2461
2462 /* assign peer for the lesson */
2463 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002464 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002465
Willy Tarreau9df94c22016-10-31 18:42:52 +01002466 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002467 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002468 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002469 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002470 int update_to_push = 0;
2471
Emeric Brunb3971ab2015-05-12 18:49:09 +02002472 /* Awake session if there is data to push */
2473 for (st = ps->tables; st ; st = st->next) {
2474 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002475 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002476 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002477 /* There is no need to send a heartbeat message
2478 * when some updates must be pushed. The remote
2479 * peer will consider <ps> peer as alive when it will
2480 * receive these updates.
2481 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002482 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002483 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002484 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002485 /* We are going to send updates, let's ensure we will
2486 * come back to send heartbeat messages or to reconnect.
2487 */
2488 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002489 appctx_wakeup(ps->appctx);
2490 break;
2491 }
2492 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002493 /* When there are updates to send we do not reconnect
2494 * and do not send heartbeat message either.
2495 */
2496 if (!update_to_push) {
2497 if (tick_is_expired(ps->reconnect, now_ms)) {
2498 if (ps->flags & PEER_F_ALIVE) {
2499 /* This peer was alive during a 'reconnect' period.
2500 * Flag it as not alive again for the next period.
2501 */
2502 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002503 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002504 }
2505 else {
2506 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
2507 peer_session_forceshutdown(ps->appctx);
2508 ps->appctx = NULL;
2509 }
2510 }
2511 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2512 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2513 ps->flags |= PEER_F_HEARTBEAT;
2514 appctx_wakeup(ps->appctx);
2515 }
2516 if (ps->appctx)
2517 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002518 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002519 }
2520 /* else do nothing */
2521 } /* SUCCESSCODE */
2522 } /* !ps->peer->local */
2523 } /* for */
2524
2525 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002526 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2527 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2528 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002529 /* Resync from remote peer needed
2530 * no peer was assigned for the lesson
2531 * and resync timeout expire */
2532
2533 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002534 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002535 }
2536
Emeric Brunb3971ab2015-05-12 18:49:09 +02002537 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002538 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002539 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2540 if (!tick_is_expired(peers->resync_timeout, now_ms))
2541 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002542 }
2543 } /* !stopping */
2544 else {
2545 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002546 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002547 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002548 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002549 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002550 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002551 peers->flags |= PEERS_F_DONOTSTOP;
2552 ps = peers->local;
2553 for (st = ps->tables; st ; st = st->next)
2554 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 }
2556
2557 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002558 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002559 /* we're killing a connection, we must apply a random delay before
2560 * retrying otherwise the other end will do the same and we can loop
2561 * for a while.
2562 */
2563 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002564 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002565 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002566 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002567 }
2568 }
2569 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002570
Emeric Brunb3971ab2015-05-12 18:49:09 +02002571 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002572 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002573 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002574 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002575 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002576 peers->flags &= ~PEERS_F_DONOTSTOP;
2577 for (st = ps->tables; st ; st = st->next)
2578 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002579 }
2580 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002581 else if (!ps->appctx) {
2582 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002583 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002584 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2585 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2586 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002587 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002588 * or previous peer connection was successfully established
2589 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002590 * or during previous connect, peer replies a try again statuscode */
2591
2592 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002593 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002594 }
2595 else {
2596 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002597 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002598 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002599 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002600 peers->flags &= ~PEERS_F_DONOTSTOP;
2601 for (st = ps->tables; st ; st = st->next)
2602 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002603 }
2604 }
2605 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002606 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002607 /* current peer connection is active and established
2608 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002609 for (st = ps->tables; st ; st = st->next) {
2610 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002611 appctx_wakeup(ps->appctx);
2612 break;
2613 }
2614 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002615 }
2616 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002617
2618 /* Release lock for all peers of the section */
2619 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002620 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002621
Emeric Brun2b920a12010-09-23 18:30:22 +02002622 /* Wakeup for re-connect */
2623 return task;
2624}
2625
Emeric Brunb3971ab2015-05-12 18:49:09 +02002626
Emeric Brun2b920a12010-09-23 18:30:22 +02002627/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002628 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002629 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002630int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002631{
Emeric Brun2b920a12010-09-23 18:30:22 +02002632 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002633
Emeric Brun2b920a12010-09-23 18:30:22 +02002634 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002635 peers->peers_fe->maxconn += 3;
2636 }
2637
Emeric Brunc60def82017-09-27 14:59:38 +02002638 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002639 if (!peers->sync_task)
2640 return 0;
2641
Emeric Brunb3971ab2015-05-12 18:49:09 +02002642 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002643 peers->sync_task->context = (void *)peers;
2644 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2645 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002646 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002647}
2648
2649
2650
2651/*
2652 * Function used to register a table for sync on a group of peers
2653 *
2654 */
2655void peers_register_table(struct peers *peers, struct stktable *table)
2656{
2657 struct shared_table *st;
2658 struct peer * curpeer;
2659 int id = 0;
2660
2661 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002662 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002663 st->table = table;
2664 st->next = curpeer->tables;
2665 if (curpeer->tables)
2666 id = curpeer->tables->local_id;
2667 st->local_id = id + 1;
2668
2669 curpeer->tables = st;
2670 }
2671
2672 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002673}
2674