blob: 1cb7fe9fe7ac5a5998c2ada7803a72a308932a99 [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>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020033#include <types/stats.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020034
35#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020036#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020037#include <proto/channel.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020038#include <proto/cli.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010040#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020041#include <proto/log.h>
42#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020043#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020044#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020045#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020046#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020047#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010048#include <proto/signal.h>
49#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020052
53
54/*******************************/
55/* Current peer learning state */
56/*******************************/
57
58/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020060/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010061#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
62#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
63#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
64#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
65#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
66 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020067
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010068#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
69#define PEERS_RESYNC_FROMLOCAL 0x00000000
70#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
71#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020072
73/***********************************/
74/* Current shared table sync state */
75/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010076#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
77#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020078
79/******************************/
80/* Remote peer teaching state */
81/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010082#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
83#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
84#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
85#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
86#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
87#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
88#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
89#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 +020090
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010091#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
92#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020093
Frédéric Lécaille54bff832019-03-26 10:25:20 +010094#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
95#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010096#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
97
Emeric Brunb3971ab2015-05-12 18:49:09 +020098/*****************************/
99/* Sync message class */
100/*****************************/
101enum {
102 PEER_MSG_CLASS_CONTROL = 0,
103 PEER_MSG_CLASS_ERROR,
104 PEER_MSG_CLASS_STICKTABLE = 10,
105 PEER_MSG_CLASS_RESERVED = 255,
106};
107
108/*****************************/
109/* control message types */
110/*****************************/
111enum {
112 PEER_MSG_CTRL_RESYNCREQ = 0,
113 PEER_MSG_CTRL_RESYNCFINISHED,
114 PEER_MSG_CTRL_RESYNCPARTIAL,
115 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100116 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200117};
118
119/*****************************/
120/* error message types */
121/*****************************/
122enum {
123 PEER_MSG_ERR_PROTOCOL = 0,
124 PEER_MSG_ERR_SIZELIMIT,
125};
126
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100127/*
128 * Parameters used by functions to build peer protocol messages. */
129struct peer_prep_params {
130 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100131 struct peer *peer;
132 } hello;
133 struct {
134 unsigned int st1;
135 } error_status;
136 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100137 struct stksess *stksess;
138 struct shared_table *shared_table;
139 unsigned int updateid;
140 int use_identifier;
141 int use_timed;
142 } updt;
143 struct {
144 struct shared_table *shared_table;
145 } swtch;
146 struct {
147 struct shared_table *shared_table;
148 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100149 struct {
150 unsigned char head[2];
151 } control;
152 struct {
153 unsigned char head[2];
154 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100155};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200156
157/*******************************/
158/* stick table sync mesg types */
159/* Note: ids >= 128 contains */
160/* id message cotains data */
161/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200162#define PEER_MSG_STKT_UPDATE 0x80
163#define PEER_MSG_STKT_INCUPDATE 0x81
164#define PEER_MSG_STKT_DEFINE 0x82
165#define PEER_MSG_STKT_SWITCH 0x83
166#define PEER_MSG_STKT_ACK 0x84
167#define PEER_MSG_STKT_UPDATE_TIMED 0x85
168#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200169
170/**********************************/
171/* Peer Session IO handler states */
172/**********************************/
173
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100174enum {
175 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
176 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
177 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
178 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100179 /* after this point, data were possibly exchanged */
180 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
181 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
182 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
183 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
184 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200185 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
186 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100187 PEER_SESS_ST_END, /* Killed session */
188};
Emeric Brun2b920a12010-09-23 18:30:22 +0200189
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100190/***************************************************/
191/* Peer Session status code - part of the protocol */
192/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200193
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100194#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
195#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200196
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100197#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200198
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100199#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200200
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100201#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
202#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
203#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
204#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200205
206#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200207#define PEER_MAJOR_VER 2
208#define PEER_MINOR_VER 1
209#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200210
Willy Tarreau6254a922019-01-29 17:45:23 +0100211static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200212struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200213static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200214
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200215static const char *statuscode_str(int statuscode)
216{
217 switch (statuscode) {
218 case PEER_SESS_SC_CONNECTCODE:
219 return "CONN";
220 case PEER_SESS_SC_CONNECTEDCODE:
221 return "HSHK";
222 case PEER_SESS_SC_SUCCESSCODE:
223 return "ESTA";
224 case PEER_SESS_SC_TRYAGAIN:
225 return "RETR";
226 case PEER_SESS_SC_ERRPROTO:
227 return "PROT";
228 case PEER_SESS_SC_ERRVERSION:
229 return "VERS";
230 case PEER_SESS_SC_ERRHOST:
231 return "NAME";
232 case PEER_SESS_SC_ERRPEER:
233 return "UNKN";
234 default:
235 return "NONE";
236 }
237}
238
Emeric Brun18928af2017-03-29 16:32:53 +0200239/* This function encode an uint64 to 'dynamic' length format.
240 The encoded value is written at address *str, and the
241 caller must assure that size after *str is large enought.
242 At return, the *str is set at the next Byte after then
243 encoded integer. The function returns then length of the
244 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200245int intencode(uint64_t i, char **str) {
246 int idx = 0;
247 unsigned char *msg;
248
Emeric Brunb3971ab2015-05-12 18:49:09 +0200249 msg = (unsigned char *)*str;
250 if (i < 240) {
251 msg[0] = (unsigned char)i;
252 *str = (char *)&msg[idx+1];
253 return (idx+1);
254 }
255
256 msg[idx] =(unsigned char)i | 240;
257 i = (i - 240) >> 4;
258 while (i >= 128) {
259 msg[++idx] = (unsigned char)i | 128;
260 i = (i - 128) >> 7;
261 }
262 msg[++idx] = (unsigned char)i;
263 *str = (char *)&msg[idx+1];
264 return (idx+1);
265}
266
267
268/* This function returns the decoded integer or 0
269 if decode failed
270 *str point on the beginning of the integer to decode
271 at the end of decoding *str point on the end of the
272 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200273uint64_t intdecode(char **str, char *end)
274{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200275 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200276 uint64_t i;
277 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 if (!*str)
280 return 0;
281
282 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200283 if (msg >= (unsigned char *)end)
284 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200285
Emeric Brun18928af2017-03-29 16:32:53 +0200286 i = *(msg++);
287 if (i >= 240) {
288 shift = 4;
289 do {
290 if (msg >= (unsigned char *)end)
291 goto fail;
292 i += (uint64_t)*msg << shift;
293 shift += 7;
294 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200295 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100296 *str = (char *)msg;
297 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200298
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100299 fail:
300 *str = NULL;
301 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200302}
Emeric Brun2b920a12010-09-23 18:30:22 +0200303
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100304/*
305 * Build a "hello" peer protocol message.
306 * Return the number of written bytes written to build this messages if succeeded,
307 * 0 if not.
308 */
309static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
310{
311 int min_ver, ret;
312 struct peer *peer;
313
314 peer = p->hello.peer;
315 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
316 /* Prepare headers */
317 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
318 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
319 if (ret >= size)
320 return 0;
321
322 return ret;
323}
324
325/*
326 * Build a "handshake succeeded" status message.
327 * Return the number of written bytes written to build this messages if succeeded,
328 * 0 if not.
329 */
330static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
331{
332 int ret;
333
334 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
335 if (ret >= size)
336 return 0;
337
338 return ret;
339}
340
341/*
342 * Build an error status message.
343 * Return the number of written bytes written to build this messages if succeeded,
344 * 0 if not.
345 */
346static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
347{
348 int ret;
349 unsigned int st1;
350
351 st1 = p->error_status.st1;
352 ret = snprintf(msg, size, "%d\n", st1);
353 if (ret >= size)
354 return 0;
355
356 return ret;
357}
358
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200359/* Set the stick-table UPDATE message type byte at <msg_type> address,
360 * depending on <use_identifier> and <use_timed> boolean parameters.
361 * Always successful.
362 */
363static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
364{
365 if (use_timed) {
366 if (use_identifier)
367 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
368 else
369 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
370 }
371 else {
372 if (use_identifier)
373 *msg_type = PEER_MSG_STKT_UPDATE;
374 else
375 *msg_type = PEER_MSG_STKT_INCUPDATE;
376 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200377}
Emeric Brun2b920a12010-09-23 18:30:22 +0200378/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200379 * This prepare the data update message on the stick session <ts>, <st> is the considered
380 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800381 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200382 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
383 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200384 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100385static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200386{
387 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200388 unsigned short datalen;
389 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200390 unsigned int data_type;
391 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100392 struct stksess *ts;
393 struct shared_table *st;
394 unsigned int updateid;
395 int use_identifier;
396 int use_timed;
397
398 ts = p->updt.stksess;
399 st = p->updt.shared_table;
400 updateid = p->updt.updateid;
401 use_identifier = p->updt.use_identifier;
402 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200403
404 cursor = datamsg = msg + 1 + 5;
405
Emeric Brun2b920a12010-09-23 18:30:22 +0200406 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200407
408 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200409 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200410 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200411 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200412
413 /* encode update identifier if needed */
414 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200415 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200416 memcpy(cursor, &netinteger, sizeof(netinteger));
417 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200418 }
419
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200420 if (use_timed) {
421 netinteger = htonl(tick_remain(now_ms, ts->expire));
422 memcpy(cursor, &netinteger, sizeof(netinteger));
423 cursor += sizeof(netinteger);
424 }
425
Emeric Brunb3971ab2015-05-12 18:49:09 +0200426 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200427 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200428 int stlen = strlen((char *)ts->key.key);
429
Emeric Brunb3971ab2015-05-12 18:49:09 +0200430 intencode(stlen, &cursor);
431 memcpy(cursor, ts->key.key, stlen);
432 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200433 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200434 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200435 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200436 memcpy(cursor, &netinteger, sizeof(netinteger));
437 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200438 }
439 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200440 memcpy(cursor, ts->key.key, st->table->key_size);
441 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200442 }
443
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100444 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200446 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200447
Emeric Brun94900952015-06-11 18:25:54 +0200448 data_ptr = stktable_data_ptr(st->table, ts, data_type);
449 if (data_ptr) {
450 switch (stktable_data_types[data_type].std_type) {
451 case STD_T_SINT: {
452 int data;
453
454 data = stktable_data_cast(data_ptr, std_t_sint);
455 intencode(data, &cursor);
456 break;
457 }
458 case STD_T_UINT: {
459 unsigned int data;
460
461 data = stktable_data_cast(data_ptr, std_t_uint);
462 intencode(data, &cursor);
463 break;
464 }
465 case STD_T_ULL: {
466 unsigned long long data;
467
468 data = stktable_data_cast(data_ptr, std_t_ull);
469 intencode(data, &cursor);
470 break;
471 }
472 case STD_T_FRQP: {
473 struct freq_ctr_period *frqp;
474
475 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
476 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
477 intencode(frqp->curr_ctr, &cursor);
478 intencode(frqp->prev_ctr, &cursor);
479 break;
480 }
481 }
482 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100484 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200485
486 /* Compute datalen */
487 datalen = (cursor - datamsg);
488
489 /* prepare message header */
490 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200491 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200492 cursor = &msg[2];
493 intencode(datalen, &cursor);
494
495 /* move data after header */
496 memmove(cursor, datamsg, datalen);
497
498 /* return header size + data_len */
499 return (cursor - msg) + datalen;
500}
501
502/*
503 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800504 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200505 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
506 * check size)
507 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100508static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509{
510 int len;
511 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200512 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200513 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200514 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200515 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100516 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100518 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 cursor = datamsg = msg + 2 + 5;
520
521 /* Encode data */
522
523 /* encode local id */
524 intencode(st->local_id, &cursor);
525
526 /* encode table name */
527 len = strlen(st->table->id);
528 intencode(len, &cursor);
529 memcpy(cursor, st->table->id, len);
530 cursor += len;
531
532 /* encode table type */
533
534 intencode(st->table->type, &cursor);
535
536 /* encode table key size */
537 intencode(st->table->key_size, &cursor);
538
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200539 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200540 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200541 /* encode available known data types in table */
542 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
543 if (st->table->data_ofs[data_type]) {
544 switch (stktable_data_types[data_type].std_type) {
545 case STD_T_SINT:
546 case STD_T_UINT:
547 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200548 data |= 1 << data_type;
549 break;
Emeric Brun94900952015-06-11 18:25:54 +0200550 case STD_T_FRQP:
551 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200552 intencode(data_type, &chunkq);
553 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200554 break;
555 }
556 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200557 }
558 intencode(data, &cursor);
559
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200560 /* Encode stick-table entries duration. */
561 intencode(st->table->expire, &cursor);
562
563 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200564 chunk->data = chunkq - chunkp;
565 memcpy(cursor, chunk->area, chunk->data);
566 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200567 }
568
Emeric Brunb3971ab2015-05-12 18:49:09 +0200569 /* Compute datalen */
570 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200571
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572 /* prepare message header */
573 msg[0] = PEER_MSG_CLASS_STICKTABLE;
574 msg[1] = PEER_MSG_STKT_DEFINE;
575 cursor = &msg[2];
576 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200577
Emeric Brunb3971ab2015-05-12 18:49:09 +0200578 /* move data after header */
579 memmove(cursor, datamsg, datalen);
580
581 /* return header size + data_len */
582 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200583}
584
Emeric Brunb3971ab2015-05-12 18:49:09 +0200585/*
586 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
587 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800588 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200589 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
590 * check size)
591 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100592static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200593{
594 unsigned short datalen;
595 char *cursor, *datamsg;
596 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100597 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200598
Emeric Brunb058f1c2015-09-22 15:50:18 +0200599 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200600
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100601 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200602 intencode(st->remote_id, &cursor);
603 netinteger = htonl(st->last_get);
604 memcpy(cursor, &netinteger, sizeof(netinteger));
605 cursor += sizeof(netinteger);
606
607 /* Compute datalen */
608 datalen = (cursor - datamsg);
609
610 /* prepare message header */
611 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200612 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200613 cursor = &msg[2];
614 intencode(datalen, &cursor);
615
616 /* move data after header */
617 memmove(cursor, datamsg, datalen);
618
619 /* return header size + data_len */
620 return (cursor - msg) + datalen;
621}
Emeric Brun2b920a12010-09-23 18:30:22 +0200622
623/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200624 * Function to deinit connected peer
625 */
626void __peer_session_deinit(struct peer *peer)
627{
628 struct stream_interface *si;
629 struct stream *s;
630 struct peers *peers;
631
632 if (!peer->appctx)
633 return;
634
635 si = peer->appctx->owner;
636 if (!si)
637 return;
638
639 s = si_strm(si);
640 if (!s)
641 return;
642
643 peers = strm_fe(s)->parent;
644 if (!peers)
645 return;
646
647 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
648 HA_ATOMIC_SUB(&connected_peers, 1);
649
650 HA_ATOMIC_SUB(&active_peers, 1);
651
652 /* Re-init current table pointers to force announcement on re-connect */
653 peer->remote_table = peer->last_local_table = NULL;
654 peer->appctx = NULL;
655 if (peer->flags & PEER_F_LEARN_ASSIGN) {
656 /* unassign current peer for learning */
657 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
658 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
659
660 /* reschedule a resync */
661 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
662 }
663 /* reset teaching and learning flags to 0 */
664 peer->flags &= PEER_TEACH_RESET;
665 peer->flags &= PEER_LEARN_RESET;
666 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
667}
668
669/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200670 * Callback to release a session with a peer
671 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200672static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200673{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200674 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200675
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100677 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200678 return;
679
680 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100682 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200683 if (peer->appctx == appctx)
684 __peer_session_deinit(peer);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100685 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200686 }
687}
688
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200689/* Retrieve the major and minor versions of peers protocol
690 * announced by a remote peer. <str> is a null-terminated
691 * string with the following format: "<maj_ver>.<min_ver>".
692 */
693static int peer_get_version(const char *str,
694 unsigned int *maj_ver, unsigned int *min_ver)
695{
696 unsigned int majv, minv;
697 const char *pos, *saved;
698 const char *end;
699
700 saved = pos = str;
701 end = str + strlen(str);
702
703 majv = read_uint(&pos, end);
704 if (saved == pos || *pos++ != '.')
705 return -1;
706
707 saved = pos;
708 minv = read_uint(&pos, end);
709 if (saved == pos || pos != end)
710 return -1;
711
712 *maj_ver = majv;
713 *min_ver = minv;
714
715 return 0;
716}
Emeric Brun2b920a12010-09-23 18:30:22 +0200717
718/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100719 * Parse a line terminated by an optional '\r' character, followed by a mandatory
720 * '\n' character.
721 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
722 * a line could not be read because the communication channel is closed.
723 */
724static inline int peer_getline(struct appctx *appctx)
725{
726 int n;
727 struct stream_interface *si = appctx->owner;
728
729 n = co_getline(si_oc(si), trash.area, trash.size);
730 if (!n)
731 return 0;
732
733 if (n < 0 || trash.area[n - 1] != '\n') {
734 appctx->st0 = PEER_SESS_ST_END;
735 return -1;
736 }
737
738 if (n > 1 && (trash.area[n - 2] == '\r'))
739 trash.area[n - 2] = 0;
740 else
741 trash.area[n - 1] = 0;
742
743 co_skip(si_oc(si), n);
744
745 return n;
746}
747
748/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100749 * Send a message after having called <peer_prepare_msg> to build it.
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 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100755static inline int peer_send_msg(struct appctx *appctx,
756 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
757 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100758{
759 int ret, msglen;
760 struct stream_interface *si = appctx->owner;
761
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100762 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100763 if (!msglen) {
764 /* internal error: message does not fit in trash */
765 appctx->st0 = PEER_SESS_ST_END;
766 return 0;
767 }
768
769 /* message to buffer */
770 ret = ci_putblk(si_ic(si), trash.area, msglen);
771 if (ret <= 0) {
772 if (ret == -1) {
773 /* No more write possible */
774 si_rx_room_blk(si);
775 return -1;
776 }
777 appctx->st0 = PEER_SESS_ST_END;
778 }
779
780 return ret;
781}
782
783/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100784 * Send a hello message.
785 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
786 * Returns -1 if there was not enough room left to send the message,
787 * any other negative returned value must be considered as an error with an appcxt st0
788 * returned value equal to PEER_SESS_ST_END.
789 */
790static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
791{
792 struct peer_prep_params p = {
793 .hello.peer = peer,
794 };
795
796 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
797}
798
799/*
800 * Send a success peer handshake status message.
801 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
802 * Returns -1 if there was not enough room left to send the message,
803 * any other negative returned value must be considered as an error with an appcxt st0
804 * returned value equal to PEER_SESS_ST_END.
805 */
806static inline int peer_send_status_successmsg(struct appctx *appctx)
807{
808 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
809}
810
811/*
812 * Send a peer handshake status error message.
813 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
814 * Returns -1 if there was not enough room left to send the message,
815 * any other negative returned value must be considered as an error with an appcxt st0
816 * returned value equal to PEER_SESS_ST_END.
817 */
818static inline int peer_send_status_errormsg(struct appctx *appctx)
819{
820 struct peer_prep_params p = {
821 .error_status.st1 = appctx->st1,
822 };
823
824 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
825}
826
827/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100828 * Send a stick-table switch message.
829 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
830 * Returns -1 if there was not enough room left to send the message,
831 * any other negative returned value must be considered as an error with an appcxt st0
832 * returned value equal to PEER_SESS_ST_END.
833 */
834static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
835{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100836 struct peer_prep_params p = {
837 .swtch.shared_table = st,
838 };
839
840 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100841}
842
843/*
844 * Send a stick-table update acknowledgement message.
845 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
846 * Returns -1 if there was not enough room left to send the message,
847 * any other negative returned value must be considered as an error with an appcxt st0
848 * returned value equal to PEER_SESS_ST_END.
849 */
850static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
851{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100852 struct peer_prep_params p = {
853 .ack.shared_table = st,
854 };
855
856 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100857}
858
859/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100860 * Send a stick-table update message.
861 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
862 * Returns -1 if there was not enough room left to send the message,
863 * any other negative returned value must be considered as an error with an appcxt st0
864 * returned value equal to PEER_SESS_ST_END.
865 */
866static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
867 unsigned int updateid, int use_identifier, int use_timed)
868{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100869 struct peer_prep_params p = {
870 .updt.stksess = ts,
871 .updt.shared_table = st,
872 .updt.updateid = updateid,
873 .updt.use_identifier = use_identifier,
874 .updt.use_timed = use_timed,
875 };
876
877 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100878}
879
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100880/*
881 * Build a peer protocol control class message.
882 * Returns the number of written bytes used to build the message if succeeded,
883 * 0 if not.
884 */
885static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
886{
887 if (size < sizeof p->control.head)
888 return 0;
889
890 msg[0] = p->control.head[0];
891 msg[1] = p->control.head[1];
892
893 return 2;
894}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100895
896/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100897 * Send a stick-table synchronization request message.
898 * Return 0 if the message could not be built modifying the appcxt 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_resync_reqmsg(struct appctx *appctx)
904{
905 struct peer_prep_params p = {
906 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
907 };
908
909 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
910}
911
912/*
913 * Send a stick-table synchronization confirmation message.
914 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
915 * Returns -1 if there was not enough room left to send the message,
916 * any other negative returned value must be considered as an error with an appctx st0
917 * returned value equal to PEER_SESS_ST_END.
918 */
919static inline int peer_send_resync_confirmsg(struct appctx *appctx)
920{
921 struct peer_prep_params p = {
922 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
923 };
924
925 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
926}
927
928/*
929 * Send a stick-table synchronization finished 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_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
936{
937 struct peer_prep_params p = {
938 .control.head = { PEER_MSG_CLASS_CONTROL, },
939 };
940
941 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
942 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
943
944 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
945}
946
947/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100948 * Send a heartbeat message.
949 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
950 * Returns -1 if there was not enough room left to send the message,
951 * any other negative returned value must be considered as an error with an appctx st0
952 * returned value equal to PEER_SESS_ST_END.
953 */
954static inline int peer_send_heartbeatmsg(struct appctx *appctx)
955{
956 struct peer_prep_params p = {
957 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
958 };
959
960 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
961}
962
963/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100964 * Build a peer protocol error class message.
965 * Returns the number of written bytes used to build the message if succeeded,
966 * 0 if not.
967 */
968static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
969{
970 if (size < sizeof p->error.head)
971 return 0;
972
973 msg[0] = p->error.head[0];
974 msg[1] = p->error.head[1];
975
976 return 2;
977}
978
979/*
980 * Send a "size limit reached" error message.
981 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
982 * Returns -1 if there was not enough room left to send the message,
983 * any other negative returned value must be considered as an error with an appctx st0
984 * returned value equal to PEER_SESS_ST_END.
985 */
986static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
987{
988 struct peer_prep_params p = {
989 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
990 };
991
992 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
993}
994
995/*
996 * Send a "peer protocol" error message.
997 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
998 * Returns -1 if there was not enough room left to send the message,
999 * any other negative returned value must be considered as an error with an appctx st0
1000 * returned value equal to PEER_SESS_ST_END.
1001 */
1002static inline int peer_send_error_protomsg(struct appctx *appctx)
1003{
1004 struct peer_prep_params p = {
1005 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1006 };
1007
1008 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1009}
1010
1011/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001012 * Function used to lookup for recent stick-table updates associated with
1013 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1014 */
1015static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1016{
1017 struct eb32_node *eb;
1018
1019 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1020 if (!eb) {
1021 eb = eb32_first(&st->table->updates);
1022 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
1023 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1024 return NULL;
1025 }
1026 }
1027
1028 if ((int)(eb->key - st->table->localupdate) > 0) {
1029 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1030 return NULL;
1031 }
1032
1033 return eb32_entry(eb, struct stksess, upd);
1034}
1035
1036/*
1037 * Function used to lookup for recent stick-table updates associated with
1038 * <st> shared stick-table during teach state 1 step.
1039 */
1040static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1041{
1042 struct eb32_node *eb;
1043
1044 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1045 if (!eb) {
1046 st->flags |= SHTABLE_F_TEACH_STAGE1;
1047 eb = eb32_first(&st->table->updates);
1048 if (eb)
1049 st->last_pushed = eb->key - 1;
1050 return NULL;
1051 }
1052
1053 return eb32_entry(eb, struct stksess, upd);
1054}
1055
1056/*
1057 * Function used to lookup for recent stick-table updates associated with
1058 * <st> shared stick-table during teach state 2 step.
1059 */
1060static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1061{
1062 struct eb32_node *eb;
1063
1064 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1065 if (!eb || eb->key > st->teaching_origin) {
1066 st->flags |= SHTABLE_F_TEACH_STAGE2;
1067 return NULL;
1068 }
1069
1070 return eb32_entry(eb, struct stksess, upd);
1071}
1072
1073/*
1074 * Generic function to emit update messages for <st> stick-table when a lesson must
1075 * be taught to the peer <p>.
1076 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1077 * this function, 0 if not.
1078 *
1079 * This function temporary unlock/lock <st> when it sends stick-table updates or
1080 * when decrementing its refcount in case of any error when it sends this updates.
1081 *
1082 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1083 * Returns -1 if there was not enough room left to send the message,
1084 * any other negative returned value must be considered as an error with an appcxt st0
1085 * returned value equal to PEER_SESS_ST_END.
1086 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1087 * unlocked if not already locked when entering this function.
1088 */
1089static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1090 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1091 struct shared_table *st, int locked)
1092{
1093 int ret, new_pushed, use_timed;
1094
1095 ret = 1;
1096 use_timed = 0;
1097 if (st != p->last_local_table) {
1098 ret = peer_send_switchmsg(st, appctx);
1099 if (ret <= 0)
1100 return ret;
1101
1102 p->last_local_table = st;
1103 }
1104
1105 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1106 use_timed = !(p->flags & PEER_F_DWNGRD);
1107
1108 /* We force new pushed to 1 to force identifier in update message */
1109 new_pushed = 1;
1110
1111 if (!locked)
1112 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1113
1114 while (1) {
1115 struct stksess *ts;
1116 unsigned updateid;
1117
1118 /* push local updates */
1119 ts = peer_stksess_lookup(st);
1120 if (!ts)
1121 break;
1122
1123 updateid = ts->upd.key;
1124 ts->ref_cnt++;
1125 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1126
1127 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1128 if (ret <= 0) {
1129 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1130 ts->ref_cnt--;
1131 if (!locked)
1132 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1133 return ret;
1134 }
1135
1136 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1137 ts->ref_cnt--;
1138 st->last_pushed = updateid;
1139
1140 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1141 (int)(st->last_pushed - st->table->commitupdate) > 0)
1142 st->table->commitupdate = st->last_pushed;
1143
1144 /* identifier may not needed in next update message */
1145 new_pushed = 0;
1146 }
1147
1148 out:
1149 if (!locked)
1150 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1151 return 1;
1152}
1153
1154/*
1155 * Function to emit update messages for <st> stick-table when a lesson must
1156 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1157 *
1158 * Note that <st> shared stick-table is locked when calling this function.
1159 *
1160 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1161 * Returns -1 if there was not enough room left to send the message,
1162 * any other negative returned value must be considered as an error with an appcxt st0
1163 * returned value equal to PEER_SESS_ST_END.
1164 */
1165static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1166 struct shared_table *st)
1167{
1168 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1169}
1170
1171/*
1172 * Function to emit update messages for <st> stick-table when a lesson must
1173 * be taught to the peer <p> during teach state 1 step.
1174 *
1175 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1176 * Returns -1 if there was not enough room left to send the message,
1177 * any other negative returned value must be considered as an error with an appcxt st0
1178 * returned value equal to PEER_SESS_ST_END.
1179 */
1180static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1181 struct shared_table *st)
1182{
1183 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1184}
1185
1186/*
1187 * Function to emit update messages for <st> stick-table when a lesson must
1188 * be taught to the peer <p> during teach state 1 step.
1189 *
1190 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1191 * Returns -1 if there was not enough room left to send the message,
1192 * any other negative returned value must be considered as an error with an appcxt st0
1193 * returned value equal to PEER_SESS_ST_END.
1194 */
1195static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1196 struct shared_table *st)
1197{
1198 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1199}
1200
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001201
1202/*
1203 * Function used to parse a stick-table update message after it has been received
1204 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1205 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1206 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1207 * was encountered.
1208 * <exp> must be set if the stick-table entry expires.
1209 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1210 * messages, in this case the stick-table udpate message is received with a stick-table
1211 * update ID.
1212 * <totl> is the length of the stick-table update message computed upon receipt.
1213 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001214static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1215 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001216{
1217 struct stream_interface *si = appctx->owner;
1218 struct shared_table *st = p->remote_table;
1219 struct stksess *ts, *newts;
1220 uint32_t update;
1221 int expire;
1222 unsigned int data_type;
1223 void *data_ptr;
1224
1225 /* Here we have data message */
1226 if (!st)
1227 goto ignore_msg;
1228
1229 expire = MS_TO_TICKS(st->table->expire);
1230
1231 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001232 if (msg_len < sizeof(update))
1233 goto malformed_exit;
1234
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001235 memcpy(&update, *msg_cur, sizeof(update));
1236 *msg_cur += sizeof(update);
1237 st->last_get = htonl(update);
1238 }
1239 else {
1240 st->last_get++;
1241 }
1242
1243 if (exp) {
1244 size_t expire_sz = sizeof expire;
1245
Willy Tarreau1e82a142019-01-29 11:08:06 +01001246 if (*msg_cur + expire_sz > msg_end)
1247 goto malformed_exit;
1248
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001249 memcpy(&expire, *msg_cur, expire_sz);
1250 *msg_cur += expire_sz;
1251 expire = ntohl(expire);
1252 }
1253
1254 newts = stksess_new(st->table, NULL);
1255 if (!newts)
1256 goto ignore_msg;
1257
1258 if (st->table->type == SMP_T_STR) {
1259 unsigned int to_read, to_store;
1260
1261 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001262 if (!*msg_cur)
1263 goto malformed_free_newts;
1264
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001265 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001266 if (*msg_cur + to_store > msg_end)
1267 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001268
1269 memcpy(newts->key.key, *msg_cur, to_store);
1270 newts->key.key[to_store] = 0;
1271 *msg_cur += to_read;
1272 }
1273 else if (st->table->type == SMP_T_SINT) {
1274 unsigned int netinteger;
1275
Willy Tarreau1e82a142019-01-29 11:08:06 +01001276 if (*msg_cur + sizeof(netinteger) > msg_end)
1277 goto malformed_free_newts;
1278
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001279 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1280 netinteger = ntohl(netinteger);
1281 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1282 *msg_cur += sizeof(netinteger);
1283 }
1284 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001285 if (*msg_cur + st->table->key_size > msg_end)
1286 goto malformed_free_newts;
1287
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001288 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1289 *msg_cur += st->table->key_size;
1290 }
1291
1292 /* lookup for existing entry */
1293 ts = stktable_set_entry(st->table, newts);
1294 if (ts != newts) {
1295 stksess_free(st->table, newts);
1296 newts = NULL;
1297 }
1298
1299 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1300
1301 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001302 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001303
1304 if (!((1 << data_type) & st->remote_data))
1305 continue;
1306
Willy Tarreau1e82a142019-01-29 11:08:06 +01001307 decoded_int = intdecode(msg_cur, msg_end);
1308 if (!*msg_cur)
1309 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001310
Willy Tarreau1e82a142019-01-29 11:08:06 +01001311 switch (stktable_data_types[data_type].std_type) {
1312 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001313 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1314 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001315 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001316 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001317
Willy Tarreau1e82a142019-01-29 11:08:06 +01001318 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001319 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1320 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001321 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001322 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001323
Willy Tarreau1e82a142019-01-29 11:08:06 +01001324 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001325 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1326 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001327 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001328 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001329
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001330 case STD_T_FRQP: {
1331 struct freq_ctr_period data;
1332
1333 /* First bit is reserved for the freq_ctr_period lock
1334 Note: here we're still protected by the stksess lock
1335 so we don't need to update the update the freq_ctr_period
1336 using its internal lock */
1337
Willy Tarreau1e82a142019-01-29 11:08:06 +01001338 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001339 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001340 if (!*msg_cur)
1341 goto malformed_unlock;
1342
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001343 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001344 if (!*msg_cur)
1345 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001346
1347 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1348 if (data_ptr)
1349 stktable_data_cast(data_ptr, std_t_frqp) = data;
1350 break;
1351 }
1352 }
1353 }
1354 /* Force new expiration */
1355 ts->expire = tick_add(now_ms, expire);
1356
1357 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1358 stktable_touch_remote(st->table, ts, 1);
1359 return 1;
1360
1361 ignore_msg:
1362 /* skip consumed message */
1363 co_skip(si_oc(si), totl);
1364 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001365
1366 malformed_unlock:
1367 /* malformed message */
1368 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1369 stktable_touch_remote(st->table, ts, 1);
1370 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1371 return 0;
1372
1373 malformed_free_newts:
1374 /* malformed message */
1375 stksess_free(st->table, newts);
1376 malformed_exit:
1377 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1378 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001379}
1380
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001381/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001382 * Function used to parse a stick-table update acknowledgement message after it
1383 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1384 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1385 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1386 * was encountered.
1387 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1388 */
1389static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1390 char **msg_cur, char *msg_end)
1391{
1392 /* ack message */
1393 uint32_t table_id ;
1394 uint32_t update;
1395 struct shared_table *st;
1396
1397 table_id = intdecode(msg_cur, msg_end);
1398 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1399 /* malformed message */
1400 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1401 return 0;
1402 }
1403
1404 memcpy(&update, *msg_cur, sizeof(update));
1405 update = ntohl(update);
1406
1407 for (st = p->tables; st; st = st->next) {
1408 if (st->local_id == table_id) {
1409 st->update = update;
1410 break;
1411 }
1412 }
1413
1414 return 1;
1415}
1416
1417/*
1418 * Function used to parse a stick-table switch message after it has been received
1419 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1420 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1421 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1422 * was encountered.
1423 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1424 */
1425static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1426 char **msg_cur, char *msg_end)
1427{
1428 struct shared_table *st;
1429 int table_id;
1430
1431 table_id = intdecode(msg_cur, msg_end);
1432 if (!*msg_cur) {
1433 /* malformed message */
1434 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1435 return 0;
1436 }
1437
1438 p->remote_table = NULL;
1439 for (st = p->tables; st; st = st->next) {
1440 if (st->remote_id == table_id) {
1441 p->remote_table = st;
1442 break;
1443 }
1444 }
1445
1446 return 1;
1447}
1448
1449/*
1450 * Function used to parse a stick-table definition message after it has been received
1451 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1452 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1453 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1454 * was encountered.
1455 * <totl> is the length of the stick-table update message computed upon receipt.
1456 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1457 */
1458static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1459 char **msg_cur, char *msg_end, int totl)
1460{
1461 struct stream_interface *si = appctx->owner;
1462 int table_id_len;
1463 struct shared_table *st;
1464 int table_type;
1465 int table_keylen;
1466 int table_id;
1467 uint64_t table_data;
1468
1469 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001470 if (!*msg_cur)
1471 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001472
1473 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001474 if (!*msg_cur)
1475 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001476
1477 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001478 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1479 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001480
1481 for (st = p->tables; st; st = st->next) {
1482 /* Reset IDs */
1483 if (st->remote_id == table_id)
1484 st->remote_id = 0;
1485
1486 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1487 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1488 p->remote_table = st;
1489 }
1490
1491 if (!p->remote_table)
1492 goto ignore_msg;
1493
1494 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001495 if (*msg_cur >= msg_end)
1496 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001497
1498 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001499 if (!*msg_cur)
1500 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001501
1502 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001503 if (!*msg_cur)
1504 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001505
1506 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001507 if (!*msg_cur)
1508 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001509
1510 if (p->remote_table->table->type != table_type
1511 || p->remote_table->table->key_size != table_keylen) {
1512 p->remote_table = NULL;
1513 goto ignore_msg;
1514 }
1515
1516 p->remote_table->remote_data = table_data;
1517 p->remote_table->remote_id = table_id;
1518 return 1;
1519
1520 ignore_msg:
1521 co_skip(si_oc(si), totl);
1522 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001523
1524 malformed_exit:
1525 /* malformed message */
1526 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1527 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001528}
1529
1530/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001531 * Receive a stick-table message.
1532 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1533 * -1 if there was an error updating the appctx state st0 accordingly.
1534 */
1535static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1536 uint32_t *msg_len, int *totl)
1537{
1538 int reql;
1539 struct stream_interface *si = appctx->owner;
1540
1541 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1542 if (reql <= 0) /* closed or EOL not found */
1543 goto incomplete;
1544
1545 *totl += reql;
1546
1547 if ((unsigned int)msg_head[1] < 128)
1548 return 1;
1549
1550 /* Read and Decode message length */
1551 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1552 if (reql <= 0) /* closed */
1553 goto incomplete;
1554
1555 *totl += reql;
1556
1557 if ((unsigned int)msg_head[2] < 240) {
1558 *msg_len = msg_head[2];
1559 }
1560 else {
1561 int i;
1562 char *cur;
1563 char *end;
1564
1565 for (i = 3 ; i < msg_head_sz ; i++) {
1566 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1567 if (reql <= 0) /* closed */
1568 goto incomplete;
1569
1570 *totl += reql;
1571
1572 if (!(msg_head[i] & 0x80))
1573 break;
1574 }
1575
1576 if (i == msg_head_sz) {
1577 /* malformed message */
1578 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1579 return -1;
1580 }
1581 end = msg_head + msg_head_sz;
1582 cur = &msg_head[2];
1583 *msg_len = intdecode(&cur, end);
1584 if (!cur) {
1585 /* malformed message */
1586 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1587 return -1;
1588 }
1589 }
1590
1591 /* Read message content */
1592 if (*msg_len) {
1593 if (*msg_len > trash.size) {
1594 /* Status code is not success, abort */
1595 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1596 return -1;
1597 }
1598
1599 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1600 if (reql <= 0) /* closed */
1601 goto incomplete;
1602 *totl += reql;
1603 }
1604
1605 return 1;
1606
1607 incomplete:
1608 if (reql < 0) {
1609 /* there was an error */
1610 appctx->st0 = PEER_SESS_ST_END;
1611 return -1;
1612 }
1613
1614 return 0;
1615}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001616
1617/*
1618 * Treat the awaited message with <msg_head> as header.*
1619 * Return 1 if succeeded, 0 if not.
1620 */
1621static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1622 char **msg_cur, char *msg_end, int msg_len, int totl)
1623{
1624 struct stream_interface *si = appctx->owner;
1625 struct stream *s = si_strm(si);
1626 struct peers *peers = strm_fe(s)->parent;
1627
1628 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1629 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1630 struct shared_table *st;
1631 /* Reset message: remote need resync */
1632
1633 /* prepare tables fot a global push */
1634 for (st = peer->tables; st; st = st->next) {
1635 st->teaching_origin = st->last_pushed = st->table->update;
1636 st->flags = 0;
1637 }
1638
1639 /* reset teaching flags to 0 */
1640 peer->flags &= PEER_TEACH_RESET;
1641
1642 /* flag to start to teach lesson */
1643 peer->flags |= PEER_F_TEACH_PROCESS;
1644 }
1645 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1646 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1647 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1648 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1649 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1650 }
1651 peer->confirm++;
1652 }
1653 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1654 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1655 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1656 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1657
1658 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001659 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001660 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1661 }
1662 peer->confirm++;
1663 }
1664 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1665 struct shared_table *st;
1666
1667 /* If stopping state */
1668 if (stopping) {
1669 /* Close session, push resync no more needed */
1670 peer->flags |= PEER_F_TEACH_COMPLETE;
1671 appctx->st0 = PEER_SESS_ST_END;
1672 return 0;
1673 }
1674 for (st = peer->tables; st; st = st->next) {
1675 st->update = st->last_pushed = st->teaching_origin;
1676 st->flags = 0;
1677 }
1678
1679 /* reset teaching flags to 0 */
1680 peer->flags &= PEER_TEACH_RESET;
1681 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001682 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001683 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001684 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001685 }
1686 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1687 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1688 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1689 return 0;
1690 }
1691 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1692 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1693 return 0;
1694 }
1695 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1696 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1697 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1698 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1699 int update, expire;
1700
1701 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1702 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1703 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1704 msg_cur, msg_end, msg_len, totl))
1705 return 0;
1706
1707 }
1708 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1709 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1710 return 0;
1711 }
1712 }
1713 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1714 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1715 return 0;
1716 }
1717
1718 return 1;
1719}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001720
1721
1722/*
1723 * Send any message to <peer> peer.
1724 * Returns 1 if succeeded, or -1 or 0 if failed.
1725 * -1 means an internal error occured, 0 is for a peer protocol error leading
1726 * to a peer state change (from the peer I/O handler point of view).
1727 */
1728static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1729{
1730 int repl;
1731 struct stream_interface *si = appctx->owner;
1732 struct stream *s = si_strm(si);
1733 struct peers *peers = strm_fe(s)->parent;
1734
1735 /* Need to request a resync */
1736 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1737 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1738 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1739
1740 repl = peer_send_resync_reqmsg(appctx);
1741 if (repl <= 0)
1742 return repl;
1743
1744 peers->flags |= PEERS_F_RESYNC_PROCESS;
1745 }
1746
1747 /* Nothing to read, now we start to write */
1748 if (peer->tables) {
1749 struct shared_table *st;
1750 struct shared_table *last_local_table;
1751
1752 last_local_table = peer->last_local_table;
1753 if (!last_local_table)
1754 last_local_table = peer->tables;
1755 st = last_local_table->next;
1756
1757 while (1) {
1758 if (!st)
1759 st = peer->tables;
1760
1761 /* It remains some updates to ack */
1762 if (st->last_get != st->last_acked) {
1763 repl = peer_send_ackmsg(st, appctx);
1764 if (repl <= 0)
1765 return repl;
1766
1767 st->last_acked = st->last_get;
1768 }
1769
1770 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1771 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1772 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1773 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1774
1775 repl = peer_send_teach_process_msgs(appctx, peer, st);
1776 if (repl <= 0) {
1777 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1778 return repl;
1779 }
1780 }
1781 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1782 }
1783 else {
1784 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1785 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1786 if (repl <= 0)
1787 return repl;
1788 }
1789
1790 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1791 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1792 if (repl <= 0)
1793 return repl;
1794 }
1795 }
1796
1797 if (st == last_local_table)
1798 break;
1799 st = st->next;
1800 }
1801 }
1802
1803 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1804 repl = peer_send_resync_finishedmsg(appctx, peer);
1805 if (repl <= 0)
1806 return repl;
1807
1808 /* flag finished message sent */
1809 peer->flags |= PEER_F_TEACH_FINISHED;
1810 }
1811
1812 /* Confirm finished or partial messages */
1813 while (peer->confirm) {
1814 repl = peer_send_resync_confirmsg(appctx);
1815 if (repl <= 0)
1816 return repl;
1817
1818 peer->confirm--;
1819 }
1820
1821 return 1;
1822}
1823
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001824/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001825 * Read and parse a first line of a "hello" peer protocol message.
1826 * Returns 0 if could not read a line, -1 if there was a read error or
1827 * the line is malformed, 1 if succeeded.
1828 */
1829static inline int peer_getline_version(struct appctx *appctx,
1830 unsigned int *maj_ver, unsigned int *min_ver)
1831{
1832 int reql;
1833
1834 reql = peer_getline(appctx);
1835 if (!reql)
1836 return 0;
1837
1838 if (reql < 0)
1839 return -1;
1840
1841 /* test protocol */
1842 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1843 appctx->st0 = PEER_SESS_ST_EXIT;
1844 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1845 return -1;
1846 }
1847 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1848 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1849 appctx->st0 = PEER_SESS_ST_EXIT;
1850 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1851 return -1;
1852 }
1853
1854 return 1;
1855}
1856
1857/*
1858 * Read and parse a second line of a "hello" peer protocol message.
1859 * Returns 0 if could not read a line, -1 if there was a read error or
1860 * the line is malformed, 1 if succeeded.
1861 */
1862static inline int peer_getline_host(struct appctx *appctx)
1863{
1864 int reql;
1865
1866 reql = peer_getline(appctx);
1867 if (!reql)
1868 return 0;
1869
1870 if (reql < 0)
1871 return -1;
1872
1873 /* test hostname match */
1874 if (strcmp(localpeer, trash.area) != 0) {
1875 appctx->st0 = PEER_SESS_ST_EXIT;
1876 appctx->st1 = PEER_SESS_SC_ERRHOST;
1877 return -1;
1878 }
1879
1880 return 1;
1881}
1882
1883/*
1884 * Read and parse a last line of a "hello" peer protocol message.
1885 * Returns 0 if could not read a character, -1 if there was a read error or
1886 * the line is malformed, 1 if succeeded.
1887 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1888 */
1889static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1890{
1891 char *p;
1892 int reql;
1893 struct peer *peer;
1894 struct stream_interface *si = appctx->owner;
1895 struct stream *s = si_strm(si);
1896 struct peers *peers = strm_fe(s)->parent;
1897
1898 reql = peer_getline(appctx);
1899 if (!reql)
1900 return 0;
1901
1902 if (reql < 0)
1903 return -1;
1904
1905 /* parse line "<peer name> <pid> <relative_pid>" */
1906 p = strchr(trash.area, ' ');
1907 if (!p) {
1908 appctx->st0 = PEER_SESS_ST_EXIT;
1909 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1910 return -1;
1911 }
1912 *p = 0;
1913
1914 /* lookup known peer */
1915 for (peer = peers->remote; peer; peer = peer->next) {
1916 if (strcmp(peer->id, trash.area) == 0)
1917 break;
1918 }
1919
1920 /* if unknown peer */
1921 if (!peer) {
1922 appctx->st0 = PEER_SESS_ST_EXIT;
1923 appctx->st1 = PEER_SESS_SC_ERRPEER;
1924 return -1;
1925 }
1926 *curpeer = peer;
1927
1928 return 1;
1929}
1930
1931/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001932 * Init <peer> peer after having accepted it at peer protocol level.
1933 */
1934static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1935{
1936 struct shared_table *st;
1937
1938 /* Register status code */
1939 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1940
1941 /* Awake main task */
1942 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1943
1944 /* Init confirm counter */
1945 peer->confirm = 0;
1946
1947 /* Init cursors */
1948 for (st = peer->tables; st ; st = st->next) {
1949 st->last_get = st->last_acked = 0;
1950 st->teaching_origin = st->last_pushed = st->update;
1951 }
1952
1953 /* reset teaching and learning flags to 0 */
1954 peer->flags &= PEER_TEACH_RESET;
1955 peer->flags &= PEER_LEARN_RESET;
1956
1957 /* if current peer is local */
1958 if (peer->local) {
1959 /* if current host need resyncfrom local and no process assined */
1960 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1961 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1962 /* assign local peer for a lesson, consider lesson already requested */
1963 peer->flags |= PEER_F_LEARN_ASSIGN;
1964 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1965 }
1966
1967 }
1968 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1969 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1970 /* assign peer for a lesson */
1971 peer->flags |= PEER_F_LEARN_ASSIGN;
1972 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1973 }
1974}
1975
1976/*
1977 * Init <peer> peer after having connected it at peer protocol level.
1978 */
1979static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1980{
1981 struct shared_table *st;
1982
1983 /* Init cursors */
1984 for (st = peer->tables; st ; st = st->next) {
1985 st->last_get = st->last_acked = 0;
1986 st->teaching_origin = st->last_pushed = st->update;
1987 }
1988
1989 /* Init confirm counter */
1990 peer->confirm = 0;
1991
1992 /* reset teaching and learning flags to 0 */
1993 peer->flags &= PEER_TEACH_RESET;
1994 peer->flags &= PEER_LEARN_RESET;
1995
1996 /* If current peer is local */
1997 if (peer->local) {
1998 /* flag to start to teach lesson */
1999 peer->flags |= PEER_F_TEACH_PROCESS;
2000 }
2001 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2002 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2003 /* If peer is remote and resync from remote is needed,
2004 and no peer currently assigned */
2005
2006 /* assign peer for a lesson */
2007 peer->flags |= PEER_F_LEARN_ASSIGN;
2008 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2009 }
2010}
2011
2012/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002013 * IO Handler to handle message exchance with a peer
2014 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002015static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002016{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002017 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002018 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002019 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002020 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002021 int reql = 0;
2022 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002023 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002024 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002025
Joseph Herlant82b2f542018-11-15 12:19:14 -08002026 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002027 if (si_ic(si)->buf.size == 0) {
2028 si_rx_room_blk(si);
2029 goto out;
2030 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002031
Emeric Brun2b920a12010-09-23 18:30:22 +02002032 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002033 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002034switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002035 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002036 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002037 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002038 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002039 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002040 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002041 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002042 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002043 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002044 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2045 if (reql <= 0) {
2046 if (!reql)
2047 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002048 goto switchstate;
2049 }
2050
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002051 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002052 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002053 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002054 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002055 reql = peer_getline_host(appctx);
2056 if (reql <= 0) {
2057 if (!reql)
2058 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002059 goto switchstate;
2060 }
2061
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002062 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002063 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002064 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002065 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002066 reql = peer_getline_last(appctx, &curpeer);
2067 if (reql <= 0) {
2068 if (!reql)
2069 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002070 goto switchstate;
2071 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002072
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002073 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002074 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002075 if (curpeer->local) {
2076 /* Local connection, reply a retry */
2077 appctx->st0 = PEER_SESS_ST_EXIT;
2078 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2079 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002080 }
Emeric Brun80527f52017-06-19 17:46:37 +02002081
2082 /* we're killing a connection, we must apply a random delay before
2083 * retrying otherwise the other end will do the same and we can loop
2084 * for a while.
2085 */
2086 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002087 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002088 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002089 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2090 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2091 curpeer->flags |= PEER_F_DWNGRD;
2092 }
2093 else {
2094 curpeer->flags &= ~PEER_F_DWNGRD;
2095 }
2096 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002097 curpeer->appctx = appctx;
2098 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002099 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002100 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002101 /* fall through */
2102 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002103 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002104 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002105 if (!curpeer) {
2106 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002107 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002108 if (curpeer->appctx != appctx) {
2109 appctx->st0 = PEER_SESS_ST_END;
2110 goto switchstate;
2111 }
2112 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002113
2114 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002115 if (repl <= 0) {
2116 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002117 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002118 goto switchstate;
2119 }
2120
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002121 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002122
Emeric Brun2b920a12010-09-23 18:30:22 +02002123 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002124 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002125 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002126 goto switchstate;
2127 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002128 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002129 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002130 if (!curpeer) {
2131 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002132 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002133 if (curpeer->appctx != appctx) {
2134 appctx->st0 = PEER_SESS_ST_END;
2135 goto switchstate;
2136 }
2137 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002138
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002139 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002140 if (repl <= 0) {
2141 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002142 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002143 goto switchstate;
2144 }
2145
2146 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002147 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002148 /* fall through */
2149 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002150 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002151 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002152 if (!curpeer) {
2153 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002154 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002155 if (curpeer->appctx != appctx) {
2156 appctx->st0 = PEER_SESS_ST_END;
2157 goto switchstate;
2158 }
2159 }
2160
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002161 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002162 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002163
Frédéric Lécaillece025572019-01-21 13:38:06 +01002164 reql = peer_getline(appctx);
2165 if (!reql)
2166 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002167
Frédéric Lécaillece025572019-01-21 13:38:06 +01002168 if (reql < 0)
2169 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002170
2171 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002173
2174 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002175 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002176
2177 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002178 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002179 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002180 }
2181 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002182 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2183 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002184 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002185 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002186 goto switchstate;
2187 }
Olivier Houcharded879892019-03-08 18:53:43 +01002188 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002189 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002190 /* fall through */
2191 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002192 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002193 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002194 char *msg_cur = trash.area;
2195 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002196 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002197 int totl = 0;
2198
Willy Tarreau2d372c22018-11-05 17:12:27 +01002199 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002200 if (!curpeer) {
2201 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002202 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002203 if (curpeer->appctx != appctx) {
2204 appctx->st0 = PEER_SESS_ST_END;
2205 goto switchstate;
2206 }
2207 }
2208
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002209 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2210 if (reql <= 0) {
2211 if (reql == -1)
2212 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002213 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002214 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002215
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002216 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002217 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002218 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002219
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002220 curpeer->flags |= PEER_F_ALIVE;
2221
Emeric Brun2b920a12010-09-23 18:30:22 +02002222 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002223 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002224 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002225 goto switchstate;
2226
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002227send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002228 if (curpeer->flags & PEER_F_HEARTBEAT) {
2229 curpeer->flags &= ~PEER_F_HEARTBEAT;
2230 repl = peer_send_heartbeatmsg(appctx);
2231 if (repl <= 0) {
2232 if (repl == -1)
2233 goto out;
2234 goto switchstate;
2235 }
2236 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002237 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002238 repl = peer_send_msgs(appctx, curpeer);
2239 if (repl <= 0) {
2240 if (repl == -1)
2241 goto out;
2242 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002243 }
2244
Emeric Brun2b920a12010-09-23 18:30:22 +02002245 /* noting more to do */
2246 goto out;
2247 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002248 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002249 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002250 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002251 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002252 if (peer_send_status_errormsg(appctx) == -1)
2253 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002254 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002255 goto switchstate;
2256 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002257 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002258 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002259 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002260 if (peer_send_error_size_limitmsg(appctx) == -1)
2261 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002262 appctx->st0 = PEER_SESS_ST_END;
2263 goto switchstate;
2264 }
2265 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002266 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002267 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002268 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002269 if (peer_send_error_protomsg(appctx) == -1)
2270 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002271 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002272 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002273 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002274 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002275 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002276 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002277 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002278 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002279 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002280 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002281 curpeer = NULL;
2282 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002283 si_shutw(si);
2284 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002285 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002286 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002287 }
2288 }
2289 }
2290out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002291 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002292
2293 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002294 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002295 return;
2296}
2297
Willy Tarreau30576452015-04-13 13:50:30 +02002298static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002299 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002300 .name = "<PEER>", /* used for logging */
2301 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002302 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002303};
Emeric Brun2b920a12010-09-23 18:30:22 +02002304
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002305
Emeric Brun2b920a12010-09-23 18:30:22 +02002306/*
2307 * Use this function to force a close of a peer session
2308 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002309static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002310{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002311 struct appctx *appctx = peer->appctx;
2312
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002313 /* Note that the peer sessions which have just been created
2314 * (->st0 == PEER_SESS_ST_CONNECT) must not
2315 * be shutdown, if not, the TCP session will never be closed
2316 * and stay in CLOSE_WAIT state after having been closed by
2317 * the remote side.
2318 */
2319 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 return;
2321
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002322 if (appctx->applet != &peer_applet)
2323 return;
2324
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002325 __peer_session_deinit(peer);
2326
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002327 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002328 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002329}
2330
Willy Tarreau91d96282015-03-13 15:47:26 +01002331/* Pre-configures a peers frontend to accept incoming connections */
2332void peers_setup_frontend(struct proxy *fe)
2333{
2334 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002335 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002336 fe->maxconn = 0;
2337 fe->conn_retries = CONN_RETRIES;
2338 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002339 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002340 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002341 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002342 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002343}
2344
Emeric Brun2b920a12010-09-23 18:30:22 +02002345/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002346 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002347 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002348static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002349{
Willy Tarreau04b92862017-09-15 11:01:04 +02002350 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002352 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002353 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002354 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002355 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002356
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002357 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002358 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002359 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002360 s = NULL;
2361
Emeric Brun1138fd02017-06-19 12:38:55 +02002362 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002363 if (!appctx)
2364 goto out_close;
2365
2366 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002367 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002368
Willy Tarreau04b92862017-09-15 11:01:04 +02002369 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002370 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002372 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002373 }
2374
Willy Tarreau87787ac2017-08-28 16:22:54 +02002375 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002376 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002377 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002378 }
2379
Willy Tarreau342bfb12015-04-05 01:35:34 +02002380 /* The tasks below are normally what is supposed to be done by
2381 * fe->accept().
2382 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002383 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002384
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002385 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002386 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002387 appctx_wakeup(appctx);
2388
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002389 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002390 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002391 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002392
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002393 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002394 * pre-initialized connection in si->conn.
2395 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002396 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002397 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002398
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002399 if (unlikely((cs = cs_new(conn)) == NULL))
2400 goto out_free_conn;
2401
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002402 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002403 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2404
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002405 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002406 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2407 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002408 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002409
Emeric Brun2b920a12010-09-23 18:30:22 +02002410 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002411 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002412
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002413 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002414
Emeric Brunb3971ab2015-05-12 18:49:09 +02002415 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002416 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002417 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002418 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002419
2420 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002421out_free_cs:
2422 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002423 out_free_conn:
2424 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002425 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002426 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002427 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002428 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002429 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002430 out_free_appctx:
2431 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002432 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002433 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002434}
2435
2436/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002437 * Task processing function to manage re-connect, peer session
2438 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002439 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002440static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002441{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002442 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002443 struct peer *ps;
2444 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002445
2446 task->expire = TICK_ETERNITY;
2447
Emeric Brunb3971ab2015-05-12 18:49:09 +02002448 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002449 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002450 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002451 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002452 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002453 return NULL;
2454 }
2455
Emeric Brun80527f52017-06-19 17:46:37 +02002456 /* Acquire lock for all peers of the section */
2457 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002458 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002459
Emeric Brun2b920a12010-09-23 18:30:22 +02002460 if (!stopping) {
2461 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002462
2463 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2464 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2465 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002466 /* Resync from local peer needed
2467 no peer was assigned for the lesson
2468 and no old local peer found
2469 or resync timeout expire */
2470
2471 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002472 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002473
2474 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002475 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002476 }
2477
2478 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002479 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002480 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002481 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002482 if (!ps->appctx) {
2483 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002484 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002485 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002486 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002487 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002488 tick_is_expired(ps->reconnect, now_ms))) {
2489 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002490 * or previous peer connection established with success
2491 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002492 * and reconnection timer is expired */
2493
2494 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002495 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002496 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002497 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002498 /* If previous session failed during connection
2499 * but reconnection timer is not expired */
2500
2501 /* reschedule task for reconnect */
2502 task->expire = tick_first(task->expire, ps->reconnect);
2503 }
2504 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002505 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002506 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002507 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002508 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2509 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002510 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2511 /* Resync from a remote is needed
2512 * and no peer was assigned for lesson
2513 * and current peer may be up2date */
2514
2515 /* assign peer for the lesson */
2516 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002517 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002518
Willy Tarreau9df94c22016-10-31 18:42:52 +01002519 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002520 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002521 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002522 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002523 int update_to_push = 0;
2524
Emeric Brunb3971ab2015-05-12 18:49:09 +02002525 /* Awake session if there is data to push */
2526 for (st = ps->tables; st ; st = st->next) {
2527 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002528 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002529 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002530 /* There is no need to send a heartbeat message
2531 * when some updates must be pushed. The remote
2532 * peer will consider <ps> peer as alive when it will
2533 * receive these updates.
2534 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002535 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002536 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002537 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002538 /* We are going to send updates, let's ensure we will
2539 * come back to send heartbeat messages or to reconnect.
2540 */
2541 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002542 appctx_wakeup(ps->appctx);
2543 break;
2544 }
2545 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002546 /* When there are updates to send we do not reconnect
2547 * and do not send heartbeat message either.
2548 */
2549 if (!update_to_push) {
2550 if (tick_is_expired(ps->reconnect, now_ms)) {
2551 if (ps->flags & PEER_F_ALIVE) {
2552 /* This peer was alive during a 'reconnect' period.
2553 * Flag it as not alive again for the next period.
2554 */
2555 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002556 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002557 }
2558 else {
2559 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002560 peer_session_forceshutdown(ps);
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002561 }
2562 }
2563 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2564 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2565 ps->flags |= PEER_F_HEARTBEAT;
2566 appctx_wakeup(ps->appctx);
2567 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002568 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002569 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002570 }
2571 /* else do nothing */
2572 } /* SUCCESSCODE */
2573 } /* !ps->peer->local */
2574 } /* for */
2575
2576 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002577 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2578 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2579 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002580 /* Resync from remote peer needed
2581 * no peer was assigned for the lesson
2582 * and resync timeout expire */
2583
2584 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002585 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002586 }
2587
Emeric Brunb3971ab2015-05-12 18:49:09 +02002588 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002589 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002590 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2591 if (!tick_is_expired(peers->resync_timeout, now_ms))
2592 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002593 }
2594 } /* !stopping */
2595 else {
2596 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002597 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002598 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002599 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002600 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002601 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002602 peers->flags |= PEERS_F_DONOTSTOP;
2603 ps = peers->local;
2604 for (st = ps->tables; st ; st = st->next)
2605 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002606 }
2607
2608 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002609 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002610 /* we're killing a connection, we must apply a random delay before
2611 * retrying otherwise the other end will do the same and we can loop
2612 * for a while.
2613 */
2614 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002615 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002616 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002617 }
2618 }
2619 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002620
Emeric Brunb3971ab2015-05-12 18:49:09 +02002621 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002622 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002623 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002624 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002625 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002626 peers->flags &= ~PEERS_F_DONOTSTOP;
2627 for (st = ps->tables; st ; st = st->next)
2628 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002629 }
2630 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002631 else if (!ps->appctx) {
2632 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002633 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002634 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2635 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2636 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002637 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002638 * or previous peer connection was successfully established
2639 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002640 * or during previous connect, peer replies a try again statuscode */
2641
2642 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002643 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002644 }
2645 else {
2646 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002647 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002648 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002649 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002650 peers->flags &= ~PEERS_F_DONOTSTOP;
2651 for (st = ps->tables; st ; st = st->next)
2652 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002653 }
2654 }
2655 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002656 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002657 /* current peer connection is active and established
2658 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002659 for (st = ps->tables; st ; st = st->next) {
2660 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002661 appctx_wakeup(ps->appctx);
2662 break;
2663 }
2664 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002665 }
2666 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002667
2668 /* Release lock for all peers of the section */
2669 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002670 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002671
Emeric Brun2b920a12010-09-23 18:30:22 +02002672 /* Wakeup for re-connect */
2673 return task;
2674}
2675
Emeric Brunb3971ab2015-05-12 18:49:09 +02002676
Emeric Brun2b920a12010-09-23 18:30:22 +02002677/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002678 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002679 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002680int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002681{
Emeric Brun2b920a12010-09-23 18:30:22 +02002682 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002683
Emeric Brun2b920a12010-09-23 18:30:22 +02002684 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002685 peers->peers_fe->maxconn += 3;
2686 }
2687
Emeric Brunc60def82017-09-27 14:59:38 +02002688 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002689 if (!peers->sync_task)
2690 return 0;
2691
Emeric Brunb3971ab2015-05-12 18:49:09 +02002692 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002693 peers->sync_task->context = (void *)peers;
2694 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2695 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002696 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002697}
2698
2699
2700
2701/*
2702 * Function used to register a table for sync on a group of peers
2703 *
2704 */
2705void peers_register_table(struct peers *peers, struct stktable *table)
2706{
2707 struct shared_table *st;
2708 struct peer * curpeer;
2709 int id = 0;
2710
2711 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002712 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002713 st->table = table;
2714 st->next = curpeer->tables;
2715 if (curpeer->tables)
2716 id = curpeer->tables->local_id;
2717 st->local_id = id + 1;
2718
2719 curpeer->tables = st;
2720 }
2721
2722 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002723}
2724
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002725/*
2726 * Parse the "show peers" command arguments.
2727 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
2728 * error message.
2729 */
2730static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
2731{
2732 appctx->ctx.cfgpeers.target = NULL;
2733
2734 if (*args[2]) {
2735 struct peers *p;
2736
2737 for (p = cfg_peers; p; p = p->next) {
2738 if (!strcmp(p->id, args[2])) {
2739 appctx->ctx.cfgpeers.target = p;
2740 break;
2741 }
2742 }
2743
2744 if (!p) {
2745 appctx->ctx.cli.severity = LOG_ERR;
2746 appctx->ctx.cli.msg = "No such peers\n";
2747 appctx->st0 = CLI_ST_PRINT;
2748 return 1;
2749 }
2750 }
2751
2752 return 0;
2753}
2754
2755/*
2756 * This function dumps the peer state information of <peers> "peers" section.
2757 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
2758 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
2759 */
2760static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
2761{
2762 struct tm tm;
2763
2764 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02002765 chunk_appendf(msg, "%p: [%02d/%s/%04d:%02d:%02d:%02d] id=%s state=%d flags=0x%x resync_timeout=%s task_calls=%u\n",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002766 peers,
2767 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2768 tm.tm_hour, tm.tm_min, tm.tm_sec,
2769 peers->id, peers->state, peers->flags,
2770 peers->resync_timeout ?
2771 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
2772 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02002773 TICKS_TO_MS(1000)) : "<NEVER>",
2774 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002775
2776 if (ci_putchk(si_ic(si), msg) == -1) {
2777 si_rx_room_blk(si);
2778 return 0;
2779 }
2780
2781 return 1;
2782}
2783
2784/*
2785 * This function dumps <peer> state information.
2786 * Returns 0 if the output buffer is full and needs to be called again, non-zero
2787 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
2788 */
2789static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
2790{
2791 struct connection *conn;
2792 char pn[INET6_ADDRSTRLEN];
2793 struct stream_interface *peer_si;
2794 struct stream *peer_s;
2795 struct appctx *appctx;
2796 struct shared_table *st;
2797
2798 addr_to_str(&peer->addr, pn, sizeof pn);
2799 chunk_appendf(msg, " %p: id=%s(%s) addr=%s:%d status=%s reconnect=%s confirm=%u\n",
2800 peer, peer->id,
2801 peer->local ? "local" : "remote",
2802 pn, get_host_port(&peer->addr),
2803 statuscode_str(peer->statuscode),
2804 peer->reconnect ?
2805 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
2806 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
2807 TICKS_TO_MS(1000)) : "<NEVER>",
2808 peer->confirm);
2809
2810 chunk_appendf(&trash, " flags=0x%x", peer->flags);
2811
2812 appctx = peer->appctx;
2813 if (!appctx)
2814 goto end;
2815
Emeric Brun0bbec0f2019-04-18 11:39:43 +02002816 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
2817 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002818
2819 peer_si = peer->appctx->owner;
2820 if (!peer_si)
2821 goto end;
2822
2823 peer_s = si_strm(peer_si);
2824 if (!peer_s)
2825 goto end;
2826
2827 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
2828
2829 conn = objt_conn(strm_orig(peer_s));
2830 if (conn)
2831 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
2832
2833 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2834 case AF_INET:
2835 case AF_INET6:
2836 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(&conn->addr.from));
2837 break;
2838 case AF_UNIX:
2839 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
2840 break;
2841 }
2842
2843 if (conn)
2844 conn_get_to_addr(conn);
2845
2846 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2847 case AF_INET:
2848 case AF_INET6:
2849 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(&conn->addr.to));
2850 break;
2851 case AF_UNIX:
2852 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
2853 break;
2854 }
2855
2856 if (peer->remote_table)
2857 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
2858 peer->remote_table,
2859 peer->remote_table->table->id,
2860 peer->remote_table->local_id,
2861 peer->remote_table->remote_id);
2862
2863 if (peer->last_local_table)
2864 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
2865 peer->last_local_table,
2866 peer->last_local_table->table->id,
2867 peer->last_local_table->local_id,
2868 peer->last_local_table->remote_id);
2869
2870 if (peer->tables) {
2871 chunk_appendf(&trash, "\n shared tables:");
2872 for (st = peer->tables; st; st = st->next) {
2873 struct stktable *t;
2874 t = st->table;
2875 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
2876 "flags=0x%x remote_data=0x%llx",
2877 st, st->local_id, st->remote_id,
2878 st->flags, (unsigned long long)st->remote_data);
2879 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
2880 " teaching_origin=%u update=%u",
2881 st->last_acked, st->last_pushed, st->last_get,
2882 st->teaching_origin, st->update);
2883 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
2884 " commitupdate=%u syncing=%u",
2885 t, t->id, t->update, t->localupdate, t->commitupdate, t->syncing);
2886 }
2887 }
2888
2889 end:
2890 chunk_appendf(&trash, "\n");
2891 if (ci_putchk(si_ic(si), msg) == -1) {
2892 si_rx_room_blk(si);
2893 return 0;
2894 }
2895
2896 return 1;
2897}
2898
2899/*
2900 * This function dumps all the peers of "peers" section.
2901 * Returns 0 if the output buffer is full and needs to be called
2902 * again, non-zero if not. It proceeds in an isolated thread, so
2903 * there is no thread safety issue here.
2904 */
2905static int cli_io_handler_show_peers(struct appctx *appctx)
2906{
2907 int show_all;
2908 int ret = 0, first_peers = 1;
2909 struct stream_interface *si = appctx->owner;
2910
2911 thread_isolate();
2912
2913 show_all = !appctx->ctx.cfgpeers.target;
2914
2915 chunk_reset(&trash);
2916
2917 while (appctx->st2 != STAT_ST_FIN) {
2918 switch (appctx->st2) {
2919 case STAT_ST_INIT:
2920 if (show_all)
2921 appctx->ctx.cfgpeers.peers = cfg_peers;
2922 else
2923 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
2924
2925 appctx->st2 = STAT_ST_LIST;
2926 /* fall through */
2927
2928 case STAT_ST_LIST:
2929 if (!appctx->ctx.cfgpeers.peers) {
2930 /* No more peers list. */
2931 appctx->st2 = STAT_ST_END;
2932 }
2933 else {
2934 if (!first_peers)
2935 chunk_appendf(&trash, "\n");
2936 else
2937 first_peers = 0;
2938 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
2939 goto out;
2940
2941 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
2942 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
2943 appctx->st2 = STAT_ST_INFO;
2944 }
2945 break;
2946
2947 case STAT_ST_INFO:
2948 if (!appctx->ctx.cfgpeers.peer) {
2949 /* End of peer list */
2950 if (show_all)
2951 appctx->st2 = STAT_ST_LIST;
2952 else
2953 appctx->st2 = STAT_ST_END;
2954 }
2955 else {
2956 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
2957 goto out;
2958
2959 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
2960 }
2961 break;
2962
2963 case STAT_ST_END:
2964 appctx->st2 = STAT_ST_FIN;
2965 break;
2966 }
2967 }
2968 ret = 1;
2969 out:
2970 thread_release();
2971 return ret;
2972}
2973
2974/*
2975 * CLI keywords.
2976 */
2977static struct cli_kw_list cli_kws = {{ }, {
2978 { { "show", "peers", NULL }, "show peers [peers section]: dump some information about all the peers or this peers section", cli_parse_show_peers, cli_io_handler_show_peers, },
2979 {},
2980}};
2981
2982/* Register cli keywords */
2983INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2984