blob: 7ccd0c5b61b0a6bda2405323876dbbf9d555fd21 [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
Frédéric Lécaille39143342019-05-24 14:32:27 +0200170/* The maximum length of an encoded data length. */
171#define PEER_MSG_ENC_LENGTH_MAXLEN 5
172
173#define PEER_MSG_HEADER_LEN 2
174
Emeric Brun2b920a12010-09-23 18:30:22 +0200175/**********************************/
176/* Peer Session IO handler states */
177/**********************************/
178
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100179enum {
180 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
181 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
182 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
183 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100184 /* after this point, data were possibly exchanged */
185 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
186 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
187 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
188 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
189 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200190 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
191 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100192 PEER_SESS_ST_END, /* Killed session */
193};
Emeric Brun2b920a12010-09-23 18:30:22 +0200194
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100195/***************************************************/
196/* Peer Session status code - part of the protocol */
197/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200198
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100199#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
200#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200201
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100202#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200203
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100204#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200205
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100206#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
207#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
208#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
209#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200210
211#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200212#define PEER_MAJOR_VER 2
213#define PEER_MINOR_VER 1
214#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200215
Willy Tarreau6254a922019-01-29 17:45:23 +0100216static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200217struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200218static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200219
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200220static const char *statuscode_str(int statuscode)
221{
222 switch (statuscode) {
223 case PEER_SESS_SC_CONNECTCODE:
224 return "CONN";
225 case PEER_SESS_SC_CONNECTEDCODE:
226 return "HSHK";
227 case PEER_SESS_SC_SUCCESSCODE:
228 return "ESTA";
229 case PEER_SESS_SC_TRYAGAIN:
230 return "RETR";
231 case PEER_SESS_SC_ERRPROTO:
232 return "PROT";
233 case PEER_SESS_SC_ERRVERSION:
234 return "VERS";
235 case PEER_SESS_SC_ERRHOST:
236 return "NAME";
237 case PEER_SESS_SC_ERRPEER:
238 return "UNKN";
239 default:
240 return "NONE";
241 }
242}
243
Emeric Brun18928af2017-03-29 16:32:53 +0200244/* This function encode an uint64 to 'dynamic' length format.
245 The encoded value is written at address *str, and the
246 caller must assure that size after *str is large enought.
247 At return, the *str is set at the next Byte after then
248 encoded integer. The function returns then length of the
249 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200250int intencode(uint64_t i, char **str) {
251 int idx = 0;
252 unsigned char *msg;
253
Emeric Brunb3971ab2015-05-12 18:49:09 +0200254 msg = (unsigned char *)*str;
255 if (i < 240) {
256 msg[0] = (unsigned char)i;
257 *str = (char *)&msg[idx+1];
258 return (idx+1);
259 }
260
261 msg[idx] =(unsigned char)i | 240;
262 i = (i - 240) >> 4;
263 while (i >= 128) {
264 msg[++idx] = (unsigned char)i | 128;
265 i = (i - 128) >> 7;
266 }
267 msg[++idx] = (unsigned char)i;
268 *str = (char *)&msg[idx+1];
269 return (idx+1);
270}
271
272
273/* This function returns the decoded integer or 0
274 if decode failed
275 *str point on the beginning of the integer to decode
276 at the end of decoding *str point on the end of the
277 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200278uint64_t intdecode(char **str, char *end)
279{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200280 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200281 uint64_t i;
282 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200283
284 if (!*str)
285 return 0;
286
287 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200288 if (msg >= (unsigned char *)end)
289 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200290
Emeric Brun18928af2017-03-29 16:32:53 +0200291 i = *(msg++);
292 if (i >= 240) {
293 shift = 4;
294 do {
295 if (msg >= (unsigned char *)end)
296 goto fail;
297 i += (uint64_t)*msg << shift;
298 shift += 7;
299 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200300 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100301 *str = (char *)msg;
302 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200303
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100304 fail:
305 *str = NULL;
306 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200307}
Emeric Brun2b920a12010-09-23 18:30:22 +0200308
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100309/*
310 * Build a "hello" peer protocol message.
311 * Return the number of written bytes written to build this messages if succeeded,
312 * 0 if not.
313 */
314static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
315{
316 int min_ver, ret;
317 struct peer *peer;
318
319 peer = p->hello.peer;
320 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
321 /* Prepare headers */
322 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
323 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
324 if (ret >= size)
325 return 0;
326
327 return ret;
328}
329
330/*
331 * Build a "handshake succeeded" status message.
332 * Return the number of written bytes written to build this messages if succeeded,
333 * 0 if not.
334 */
335static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
336{
337 int ret;
338
339 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
340 if (ret >= size)
341 return 0;
342
343 return ret;
344}
345
346/*
347 * Build an error status message.
348 * Return the number of written bytes written to build this messages if succeeded,
349 * 0 if not.
350 */
351static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
352{
353 int ret;
354 unsigned int st1;
355
356 st1 = p->error_status.st1;
357 ret = snprintf(msg, size, "%d\n", st1);
358 if (ret >= size)
359 return 0;
360
361 return ret;
362}
363
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200364/* Set the stick-table UPDATE message type byte at <msg_type> address,
365 * depending on <use_identifier> and <use_timed> boolean parameters.
366 * Always successful.
367 */
368static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
369{
370 if (use_timed) {
371 if (use_identifier)
372 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
373 else
374 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
375 }
376 else {
377 if (use_identifier)
378 *msg_type = PEER_MSG_STKT_UPDATE;
379 else
380 *msg_type = PEER_MSG_STKT_INCUPDATE;
381 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200382}
Emeric Brun2b920a12010-09-23 18:30:22 +0200383/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200384 * This prepare the data update message on the stick session <ts>, <st> is the considered
385 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800386 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200387 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
388 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200389 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100390static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200391{
392 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200393 unsigned short datalen;
394 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200395 unsigned int data_type;
396 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100397 struct stksess *ts;
398 struct shared_table *st;
399 unsigned int updateid;
400 int use_identifier;
401 int use_timed;
402
403 ts = p->updt.stksess;
404 st = p->updt.shared_table;
405 updateid = p->updt.updateid;
406 use_identifier = p->updt.use_identifier;
407 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200408
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200409 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200410
Emeric Brun2b920a12010-09-23 18:30:22 +0200411 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200412
413 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200414 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200415 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200416 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200417
418 /* encode update identifier if needed */
419 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200420 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200421 memcpy(cursor, &netinteger, sizeof(netinteger));
422 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200423 }
424
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200425 if (use_timed) {
426 netinteger = htonl(tick_remain(now_ms, ts->expire));
427 memcpy(cursor, &netinteger, sizeof(netinteger));
428 cursor += sizeof(netinteger);
429 }
430
Emeric Brunb3971ab2015-05-12 18:49:09 +0200431 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200432 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200433 int stlen = strlen((char *)ts->key.key);
434
Emeric Brunb3971ab2015-05-12 18:49:09 +0200435 intencode(stlen, &cursor);
436 memcpy(cursor, ts->key.key, stlen);
437 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200438 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200439 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200440 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200441 memcpy(cursor, &netinteger, sizeof(netinteger));
442 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200443 }
444 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 memcpy(cursor, ts->key.key, st->table->key_size);
446 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200447 }
448
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200451 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200452
Emeric Brun94900952015-06-11 18:25:54 +0200453 data_ptr = stktable_data_ptr(st->table, ts, data_type);
454 if (data_ptr) {
455 switch (stktable_data_types[data_type].std_type) {
456 case STD_T_SINT: {
457 int data;
458
459 data = stktable_data_cast(data_ptr, std_t_sint);
460 intencode(data, &cursor);
461 break;
462 }
463 case STD_T_UINT: {
464 unsigned int data;
465
466 data = stktable_data_cast(data_ptr, std_t_uint);
467 intencode(data, &cursor);
468 break;
469 }
470 case STD_T_ULL: {
471 unsigned long long data;
472
473 data = stktable_data_cast(data_ptr, std_t_ull);
474 intencode(data, &cursor);
475 break;
476 }
477 case STD_T_FRQP: {
478 struct freq_ctr_period *frqp;
479
480 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
481 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
482 intencode(frqp->curr_ctr, &cursor);
483 intencode(frqp->prev_ctr, &cursor);
484 break;
485 }
486 }
487 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200488 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100489 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200490
491 /* Compute datalen */
492 datalen = (cursor - datamsg);
493
494 /* prepare message header */
495 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200496 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200497 cursor = &msg[2];
498 intencode(datalen, &cursor);
499
500 /* move data after header */
501 memmove(cursor, datamsg, datalen);
502
503 /* return header size + data_len */
504 return (cursor - msg) + datalen;
505}
506
507/*
508 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800509 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200510 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
511 * check size)
512 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100513static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200514{
515 int len;
516 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200517 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200518 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200520 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100521 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100523 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200524 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200525
526 /* Encode data */
527
528 /* encode local id */
529 intencode(st->local_id, &cursor);
530
531 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100532 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200533 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100534 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200535 cursor += len;
536
537 /* encode table type */
538
539 intencode(st->table->type, &cursor);
540
541 /* encode table key size */
542 intencode(st->table->key_size, &cursor);
543
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200544 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200546 /* encode available known data types in table */
547 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
548 if (st->table->data_ofs[data_type]) {
549 switch (stktable_data_types[data_type].std_type) {
550 case STD_T_SINT:
551 case STD_T_UINT:
552 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200553 data |= 1 << data_type;
554 break;
Emeric Brun94900952015-06-11 18:25:54 +0200555 case STD_T_FRQP:
556 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200557 intencode(data_type, &chunkq);
558 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200559 break;
560 }
561 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200562 }
563 intencode(data, &cursor);
564
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200565 /* Encode stick-table entries duration. */
566 intencode(st->table->expire, &cursor);
567
568 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 chunk->data = chunkq - chunkp;
570 memcpy(cursor, chunk->area, chunk->data);
571 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200572 }
573
Emeric Brunb3971ab2015-05-12 18:49:09 +0200574 /* Compute datalen */
575 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200576
Emeric Brunb3971ab2015-05-12 18:49:09 +0200577 /* prepare message header */
578 msg[0] = PEER_MSG_CLASS_STICKTABLE;
579 msg[1] = PEER_MSG_STKT_DEFINE;
580 cursor = &msg[2];
581 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200582
Emeric Brunb3971ab2015-05-12 18:49:09 +0200583 /* move data after header */
584 memmove(cursor, datamsg, datalen);
585
586 /* return header size + data_len */
587 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200588}
589
Emeric Brunb3971ab2015-05-12 18:49:09 +0200590/*
591 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
592 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800593 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200594 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
595 * check size)
596 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100597static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200598{
599 unsigned short datalen;
600 char *cursor, *datamsg;
601 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100602 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200603
Frédéric Lécaille39143342019-05-24 14:32:27 +0200604 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200605
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100606 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200607 intencode(st->remote_id, &cursor);
608 netinteger = htonl(st->last_get);
609 memcpy(cursor, &netinteger, sizeof(netinteger));
610 cursor += sizeof(netinteger);
611
612 /* Compute datalen */
613 datalen = (cursor - datamsg);
614
615 /* prepare message header */
616 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200617 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200618 cursor = &msg[2];
619 intencode(datalen, &cursor);
620
621 /* move data after header */
622 memmove(cursor, datamsg, datalen);
623
624 /* return header size + data_len */
625 return (cursor - msg) + datalen;
626}
Emeric Brun2b920a12010-09-23 18:30:22 +0200627
628/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200629 * Function to deinit connected peer
630 */
631void __peer_session_deinit(struct peer *peer)
632{
633 struct stream_interface *si;
634 struct stream *s;
635 struct peers *peers;
636
637 if (!peer->appctx)
638 return;
639
640 si = peer->appctx->owner;
641 if (!si)
642 return;
643
644 s = si_strm(si);
645 if (!s)
646 return;
647
648 peers = strm_fe(s)->parent;
649 if (!peers)
650 return;
651
652 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
653 HA_ATOMIC_SUB(&connected_peers, 1);
654
655 HA_ATOMIC_SUB(&active_peers, 1);
656
657 /* Re-init current table pointers to force announcement on re-connect */
658 peer->remote_table = peer->last_local_table = NULL;
659 peer->appctx = NULL;
660 if (peer->flags & PEER_F_LEARN_ASSIGN) {
661 /* unassign current peer for learning */
662 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
663 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
664
665 /* reschedule a resync */
666 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
667 }
668 /* reset teaching and learning flags to 0 */
669 peer->flags &= PEER_TEACH_RESET;
670 peer->flags &= PEER_LEARN_RESET;
671 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
672}
673
674/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 * Callback to release a session with a peer
676 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200677static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200678{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200679 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200680
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100681 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100682 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200683 return;
684
685 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200686 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100687 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200688 if (peer->appctx == appctx)
689 __peer_session_deinit(peer);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100690 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200691 }
692}
693
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200694/* Retrieve the major and minor versions of peers protocol
695 * announced by a remote peer. <str> is a null-terminated
696 * string with the following format: "<maj_ver>.<min_ver>".
697 */
698static int peer_get_version(const char *str,
699 unsigned int *maj_ver, unsigned int *min_ver)
700{
701 unsigned int majv, minv;
702 const char *pos, *saved;
703 const char *end;
704
705 saved = pos = str;
706 end = str + strlen(str);
707
708 majv = read_uint(&pos, end);
709 if (saved == pos || *pos++ != '.')
710 return -1;
711
712 saved = pos;
713 minv = read_uint(&pos, end);
714 if (saved == pos || pos != end)
715 return -1;
716
717 *maj_ver = majv;
718 *min_ver = minv;
719
720 return 0;
721}
Emeric Brun2b920a12010-09-23 18:30:22 +0200722
723/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100724 * Parse a line terminated by an optional '\r' character, followed by a mandatory
725 * '\n' character.
726 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
727 * a line could not be read because the communication channel is closed.
728 */
729static inline int peer_getline(struct appctx *appctx)
730{
731 int n;
732 struct stream_interface *si = appctx->owner;
733
734 n = co_getline(si_oc(si), trash.area, trash.size);
735 if (!n)
736 return 0;
737
738 if (n < 0 || trash.area[n - 1] != '\n') {
739 appctx->st0 = PEER_SESS_ST_END;
740 return -1;
741 }
742
743 if (n > 1 && (trash.area[n - 2] == '\r'))
744 trash.area[n - 2] = 0;
745 else
746 trash.area[n - 1] = 0;
747
748 co_skip(si_oc(si), n);
749
750 return n;
751}
752
753/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100754 * Send a message after having called <peer_prepare_msg> to build it.
755 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
756 * Returns -1 if there was not enough room left to send the message,
757 * any other negative returned value must be considered as an error with an appcxt st0
758 * returned value equal to PEER_SESS_ST_END.
759 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100760static inline int peer_send_msg(struct appctx *appctx,
761 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
762 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100763{
764 int ret, msglen;
765 struct stream_interface *si = appctx->owner;
766
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100767 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100768 if (!msglen) {
769 /* internal error: message does not fit in trash */
770 appctx->st0 = PEER_SESS_ST_END;
771 return 0;
772 }
773
774 /* message to buffer */
775 ret = ci_putblk(si_ic(si), trash.area, msglen);
776 if (ret <= 0) {
777 if (ret == -1) {
778 /* No more write possible */
779 si_rx_room_blk(si);
780 return -1;
781 }
782 appctx->st0 = PEER_SESS_ST_END;
783 }
784
785 return ret;
786}
787
788/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100789 * Send a hello message.
790 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
791 * Returns -1 if there was not enough room left to send the message,
792 * any other negative returned value must be considered as an error with an appcxt st0
793 * returned value equal to PEER_SESS_ST_END.
794 */
795static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
796{
797 struct peer_prep_params p = {
798 .hello.peer = peer,
799 };
800
801 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
802}
803
804/*
805 * Send a success peer handshake status message.
806 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
807 * Returns -1 if there was not enough room left to send the message,
808 * any other negative returned value must be considered as an error with an appcxt st0
809 * returned value equal to PEER_SESS_ST_END.
810 */
811static inline int peer_send_status_successmsg(struct appctx *appctx)
812{
813 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
814}
815
816/*
817 * Send a peer handshake status error message.
818 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
819 * Returns -1 if there was not enough room left to send the message,
820 * any other negative returned value must be considered as an error with an appcxt st0
821 * returned value equal to PEER_SESS_ST_END.
822 */
823static inline int peer_send_status_errormsg(struct appctx *appctx)
824{
825 struct peer_prep_params p = {
826 .error_status.st1 = appctx->st1,
827 };
828
829 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
830}
831
832/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100833 * Send a stick-table switch message.
834 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
835 * Returns -1 if there was not enough room left to send the message,
836 * any other negative returned value must be considered as an error with an appcxt st0
837 * returned value equal to PEER_SESS_ST_END.
838 */
839static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
840{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100841 struct peer_prep_params p = {
842 .swtch.shared_table = st,
843 };
844
845 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100846}
847
848/*
849 * Send a stick-table update acknowledgement message.
850 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
851 * Returns -1 if there was not enough room left to send the message,
852 * any other negative returned value must be considered as an error with an appcxt st0
853 * returned value equal to PEER_SESS_ST_END.
854 */
855static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
856{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100857 struct peer_prep_params p = {
858 .ack.shared_table = st,
859 };
860
861 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100862}
863
864/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100865 * Send a stick-table update message.
866 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
867 * Returns -1 if there was not enough room left to send the message,
868 * any other negative returned value must be considered as an error with an appcxt st0
869 * returned value equal to PEER_SESS_ST_END.
870 */
871static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
872 unsigned int updateid, int use_identifier, int use_timed)
873{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100874 struct peer_prep_params p = {
875 .updt.stksess = ts,
876 .updt.shared_table = st,
877 .updt.updateid = updateid,
878 .updt.use_identifier = use_identifier,
879 .updt.use_timed = use_timed,
880 };
881
882 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100883}
884
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100885/*
886 * Build a peer protocol control class message.
887 * Returns the number of written bytes used to build the message if succeeded,
888 * 0 if not.
889 */
890static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
891{
892 if (size < sizeof p->control.head)
893 return 0;
894
895 msg[0] = p->control.head[0];
896 msg[1] = p->control.head[1];
897
898 return 2;
899}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100900
901/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100902 * Send a stick-table synchronization request message.
903 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
904 * Returns -1 if there was not enough room left to send the message,
905 * any other negative returned value must be considered as an error with an appctx st0
906 * returned value equal to PEER_SESS_ST_END.
907 */
908static inline int peer_send_resync_reqmsg(struct appctx *appctx)
909{
910 struct peer_prep_params p = {
911 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
912 };
913
914 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
915}
916
917/*
918 * Send a stick-table synchronization confirmation message.
919 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
920 * Returns -1 if there was not enough room left to send the message,
921 * any other negative returned value must be considered as an error with an appctx st0
922 * returned value equal to PEER_SESS_ST_END.
923 */
924static inline int peer_send_resync_confirmsg(struct appctx *appctx)
925{
926 struct peer_prep_params p = {
927 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
928 };
929
930 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
931}
932
933/*
934 * Send a stick-table synchronization finished message.
935 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
936 * Returns -1 if there was not enough room left to send the message,
937 * any other negative returned value must be considered as an error with an appctx st0
938 * returned value equal to PEER_SESS_ST_END.
939 */
940static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
941{
942 struct peer_prep_params p = {
943 .control.head = { PEER_MSG_CLASS_CONTROL, },
944 };
945
946 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
947 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
948
949 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
950}
951
952/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100953 * Send a heartbeat message.
954 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
955 * Returns -1 if there was not enough room left to send the message,
956 * any other negative returned value must be considered as an error with an appctx st0
957 * returned value equal to PEER_SESS_ST_END.
958 */
959static inline int peer_send_heartbeatmsg(struct appctx *appctx)
960{
961 struct peer_prep_params p = {
962 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
963 };
964
965 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
966}
967
968/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100969 * Build a peer protocol error class message.
970 * Returns the number of written bytes used to build the message if succeeded,
971 * 0 if not.
972 */
973static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
974{
975 if (size < sizeof p->error.head)
976 return 0;
977
978 msg[0] = p->error.head[0];
979 msg[1] = p->error.head[1];
980
981 return 2;
982}
983
984/*
985 * Send a "size limit reached" error message.
986 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
987 * Returns -1 if there was not enough room left to send the message,
988 * any other negative returned value must be considered as an error with an appctx st0
989 * returned value equal to PEER_SESS_ST_END.
990 */
991static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
992{
993 struct peer_prep_params p = {
994 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
995 };
996
997 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
998}
999
1000/*
1001 * Send a "peer protocol" error message.
1002 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1003 * Returns -1 if there was not enough room left to send the message,
1004 * any other negative returned value must be considered as an error with an appctx st0
1005 * returned value equal to PEER_SESS_ST_END.
1006 */
1007static inline int peer_send_error_protomsg(struct appctx *appctx)
1008{
1009 struct peer_prep_params p = {
1010 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1011 };
1012
1013 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1014}
1015
1016/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001017 * Function used to lookup for recent stick-table updates associated with
1018 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1019 */
1020static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1021{
1022 struct eb32_node *eb;
1023
1024 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1025 if (!eb) {
1026 eb = eb32_first(&st->table->updates);
1027 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
1028 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1029 return NULL;
1030 }
1031 }
1032
1033 if ((int)(eb->key - st->table->localupdate) > 0) {
1034 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1035 return NULL;
1036 }
1037
1038 return eb32_entry(eb, struct stksess, upd);
1039}
1040
1041/*
1042 * Function used to lookup for recent stick-table updates associated with
1043 * <st> shared stick-table during teach state 1 step.
1044 */
1045static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1046{
1047 struct eb32_node *eb;
1048
1049 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1050 if (!eb) {
1051 st->flags |= SHTABLE_F_TEACH_STAGE1;
1052 eb = eb32_first(&st->table->updates);
1053 if (eb)
1054 st->last_pushed = eb->key - 1;
1055 return NULL;
1056 }
1057
1058 return eb32_entry(eb, struct stksess, upd);
1059}
1060
1061/*
1062 * Function used to lookup for recent stick-table updates associated with
1063 * <st> shared stick-table during teach state 2 step.
1064 */
1065static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1066{
1067 struct eb32_node *eb;
1068
1069 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1070 if (!eb || eb->key > st->teaching_origin) {
1071 st->flags |= SHTABLE_F_TEACH_STAGE2;
1072 return NULL;
1073 }
1074
1075 return eb32_entry(eb, struct stksess, upd);
1076}
1077
1078/*
1079 * Generic function to emit update messages for <st> stick-table when a lesson must
1080 * be taught to the peer <p>.
1081 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1082 * this function, 0 if not.
1083 *
1084 * This function temporary unlock/lock <st> when it sends stick-table updates or
1085 * when decrementing its refcount in case of any error when it sends this updates.
1086 *
1087 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1088 * Returns -1 if there was not enough room left to send the message,
1089 * any other negative returned value must be considered as an error with an appcxt st0
1090 * returned value equal to PEER_SESS_ST_END.
1091 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1092 * unlocked if not already locked when entering this function.
1093 */
1094static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1095 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1096 struct shared_table *st, int locked)
1097{
1098 int ret, new_pushed, use_timed;
1099
1100 ret = 1;
1101 use_timed = 0;
1102 if (st != p->last_local_table) {
1103 ret = peer_send_switchmsg(st, appctx);
1104 if (ret <= 0)
1105 return ret;
1106
1107 p->last_local_table = st;
1108 }
1109
1110 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1111 use_timed = !(p->flags & PEER_F_DWNGRD);
1112
1113 /* We force new pushed to 1 to force identifier in update message */
1114 new_pushed = 1;
1115
1116 if (!locked)
1117 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1118
1119 while (1) {
1120 struct stksess *ts;
1121 unsigned updateid;
1122
1123 /* push local updates */
1124 ts = peer_stksess_lookup(st);
1125 if (!ts)
1126 break;
1127
1128 updateid = ts->upd.key;
1129 ts->ref_cnt++;
1130 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1131
1132 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1133 if (ret <= 0) {
1134 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1135 ts->ref_cnt--;
1136 if (!locked)
1137 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1138 return ret;
1139 }
1140
1141 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1142 ts->ref_cnt--;
1143 st->last_pushed = updateid;
1144
1145 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1146 (int)(st->last_pushed - st->table->commitupdate) > 0)
1147 st->table->commitupdate = st->last_pushed;
1148
1149 /* identifier may not needed in next update message */
1150 new_pushed = 0;
1151 }
1152
1153 out:
1154 if (!locked)
1155 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1156 return 1;
1157}
1158
1159/*
1160 * Function to emit update messages for <st> stick-table when a lesson must
1161 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1162 *
1163 * Note that <st> shared stick-table is locked when calling this function.
1164 *
1165 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1166 * Returns -1 if there was not enough room left to send the message,
1167 * any other negative returned value must be considered as an error with an appcxt st0
1168 * returned value equal to PEER_SESS_ST_END.
1169 */
1170static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1171 struct shared_table *st)
1172{
1173 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1174}
1175
1176/*
1177 * Function to emit update messages for <st> stick-table when a lesson must
1178 * be taught to the peer <p> during teach state 1 step.
1179 *
1180 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1181 * Returns -1 if there was not enough room left to send the message,
1182 * any other negative returned value must be considered as an error with an appcxt st0
1183 * returned value equal to PEER_SESS_ST_END.
1184 */
1185static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1186 struct shared_table *st)
1187{
1188 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1189}
1190
1191/*
1192 * Function to emit update messages for <st> stick-table when a lesson must
1193 * be taught to the peer <p> during teach state 1 step.
1194 *
1195 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1196 * Returns -1 if there was not enough room left to send the message,
1197 * any other negative returned value must be considered as an error with an appcxt st0
1198 * returned value equal to PEER_SESS_ST_END.
1199 */
1200static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1201 struct shared_table *st)
1202{
1203 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1204}
1205
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001206
1207/*
1208 * Function used to parse a stick-table update message after it has been received
1209 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1210 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1211 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1212 * was encountered.
1213 * <exp> must be set if the stick-table entry expires.
1214 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1215 * messages, in this case the stick-table udpate message is received with a stick-table
1216 * update ID.
1217 * <totl> is the length of the stick-table update message computed upon receipt.
1218 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001219static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1220 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001221{
1222 struct stream_interface *si = appctx->owner;
1223 struct shared_table *st = p->remote_table;
1224 struct stksess *ts, *newts;
1225 uint32_t update;
1226 int expire;
1227 unsigned int data_type;
1228 void *data_ptr;
1229
1230 /* Here we have data message */
1231 if (!st)
1232 goto ignore_msg;
1233
1234 expire = MS_TO_TICKS(st->table->expire);
1235
1236 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001237 if (msg_len < sizeof(update))
1238 goto malformed_exit;
1239
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001240 memcpy(&update, *msg_cur, sizeof(update));
1241 *msg_cur += sizeof(update);
1242 st->last_get = htonl(update);
1243 }
1244 else {
1245 st->last_get++;
1246 }
1247
1248 if (exp) {
1249 size_t expire_sz = sizeof expire;
1250
Willy Tarreau1e82a142019-01-29 11:08:06 +01001251 if (*msg_cur + expire_sz > msg_end)
1252 goto malformed_exit;
1253
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001254 memcpy(&expire, *msg_cur, expire_sz);
1255 *msg_cur += expire_sz;
1256 expire = ntohl(expire);
1257 }
1258
1259 newts = stksess_new(st->table, NULL);
1260 if (!newts)
1261 goto ignore_msg;
1262
1263 if (st->table->type == SMP_T_STR) {
1264 unsigned int to_read, to_store;
1265
1266 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001267 if (!*msg_cur)
1268 goto malformed_free_newts;
1269
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001270 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001271 if (*msg_cur + to_store > msg_end)
1272 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001273
1274 memcpy(newts->key.key, *msg_cur, to_store);
1275 newts->key.key[to_store] = 0;
1276 *msg_cur += to_read;
1277 }
1278 else if (st->table->type == SMP_T_SINT) {
1279 unsigned int netinteger;
1280
Willy Tarreau1e82a142019-01-29 11:08:06 +01001281 if (*msg_cur + sizeof(netinteger) > msg_end)
1282 goto malformed_free_newts;
1283
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001284 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1285 netinteger = ntohl(netinteger);
1286 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1287 *msg_cur += sizeof(netinteger);
1288 }
1289 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001290 if (*msg_cur + st->table->key_size > msg_end)
1291 goto malformed_free_newts;
1292
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001293 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1294 *msg_cur += st->table->key_size;
1295 }
1296
1297 /* lookup for existing entry */
1298 ts = stktable_set_entry(st->table, newts);
1299 if (ts != newts) {
1300 stksess_free(st->table, newts);
1301 newts = NULL;
1302 }
1303
1304 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1305
1306 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001307 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001308
1309 if (!((1 << data_type) & st->remote_data))
1310 continue;
1311
Willy Tarreau1e82a142019-01-29 11:08:06 +01001312 decoded_int = intdecode(msg_cur, msg_end);
1313 if (!*msg_cur)
1314 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001315
Willy Tarreau1e82a142019-01-29 11:08:06 +01001316 switch (stktable_data_types[data_type].std_type) {
1317 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001318 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1319 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001320 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001321 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001322
Willy Tarreau1e82a142019-01-29 11:08:06 +01001323 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001324 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1325 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001326 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001327 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001328
Willy Tarreau1e82a142019-01-29 11:08:06 +01001329 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001330 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1331 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001332 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001333 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001334
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001335 case STD_T_FRQP: {
1336 struct freq_ctr_period data;
1337
1338 /* First bit is reserved for the freq_ctr_period lock
1339 Note: here we're still protected by the stksess lock
1340 so we don't need to update the update the freq_ctr_period
1341 using its internal lock */
1342
Willy Tarreau1e82a142019-01-29 11:08:06 +01001343 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001344 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001345 if (!*msg_cur)
1346 goto malformed_unlock;
1347
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001348 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001349 if (!*msg_cur)
1350 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001351
1352 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1353 if (data_ptr)
1354 stktable_data_cast(data_ptr, std_t_frqp) = data;
1355 break;
1356 }
1357 }
1358 }
1359 /* Force new expiration */
1360 ts->expire = tick_add(now_ms, expire);
1361
1362 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1363 stktable_touch_remote(st->table, ts, 1);
1364 return 1;
1365
1366 ignore_msg:
1367 /* skip consumed message */
1368 co_skip(si_oc(si), totl);
1369 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001370
1371 malformed_unlock:
1372 /* malformed message */
1373 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1374 stktable_touch_remote(st->table, ts, 1);
1375 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1376 return 0;
1377
1378 malformed_free_newts:
1379 /* malformed message */
1380 stksess_free(st->table, newts);
1381 malformed_exit:
1382 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1383 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001384}
1385
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001386/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001387 * Function used to parse a stick-table update acknowledgement message after it
1388 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1389 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1390 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1391 * was encountered.
1392 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1393 */
1394static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1395 char **msg_cur, char *msg_end)
1396{
1397 /* ack message */
1398 uint32_t table_id ;
1399 uint32_t update;
1400 struct shared_table *st;
1401
1402 table_id = intdecode(msg_cur, msg_end);
1403 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1404 /* malformed message */
1405 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1406 return 0;
1407 }
1408
1409 memcpy(&update, *msg_cur, sizeof(update));
1410 update = ntohl(update);
1411
1412 for (st = p->tables; st; st = st->next) {
1413 if (st->local_id == table_id) {
1414 st->update = update;
1415 break;
1416 }
1417 }
1418
1419 return 1;
1420}
1421
1422/*
1423 * Function used to parse a stick-table switch message after it has been received
1424 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1425 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1426 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1427 * was encountered.
1428 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1429 */
1430static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1431 char **msg_cur, char *msg_end)
1432{
1433 struct shared_table *st;
1434 int table_id;
1435
1436 table_id = intdecode(msg_cur, msg_end);
1437 if (!*msg_cur) {
1438 /* malformed message */
1439 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1440 return 0;
1441 }
1442
1443 p->remote_table = NULL;
1444 for (st = p->tables; st; st = st->next) {
1445 if (st->remote_id == table_id) {
1446 p->remote_table = st;
1447 break;
1448 }
1449 }
1450
1451 return 1;
1452}
1453
1454/*
1455 * Function used to parse a stick-table definition message after it has been received
1456 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1457 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1458 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1459 * was encountered.
1460 * <totl> is the length of the stick-table update message computed upon receipt.
1461 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1462 */
1463static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1464 char **msg_cur, char *msg_end, int totl)
1465{
1466 struct stream_interface *si = appctx->owner;
1467 int table_id_len;
1468 struct shared_table *st;
1469 int table_type;
1470 int table_keylen;
1471 int table_id;
1472 uint64_t table_data;
1473
1474 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001475 if (!*msg_cur)
1476 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001477
1478 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001479 if (!*msg_cur)
1480 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001481
1482 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001483 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1484 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001485
1486 for (st = p->tables; st; st = st->next) {
1487 /* Reset IDs */
1488 if (st->remote_id == table_id)
1489 st->remote_id = 0;
1490
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001491 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1492 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001493 p->remote_table = st;
1494 }
1495
1496 if (!p->remote_table)
1497 goto ignore_msg;
1498
1499 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001500 if (*msg_cur >= msg_end)
1501 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001502
1503 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001504 if (!*msg_cur)
1505 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001506
1507 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001508 if (!*msg_cur)
1509 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001510
1511 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001512 if (!*msg_cur)
1513 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001514
1515 if (p->remote_table->table->type != table_type
1516 || p->remote_table->table->key_size != table_keylen) {
1517 p->remote_table = NULL;
1518 goto ignore_msg;
1519 }
1520
1521 p->remote_table->remote_data = table_data;
1522 p->remote_table->remote_id = table_id;
1523 return 1;
1524
1525 ignore_msg:
1526 co_skip(si_oc(si), totl);
1527 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001528
1529 malformed_exit:
1530 /* malformed message */
1531 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1532 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001533}
1534
1535/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001536 * Receive a stick-table message.
1537 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1538 * -1 if there was an error updating the appctx state st0 accordingly.
1539 */
1540static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1541 uint32_t *msg_len, int *totl)
1542{
1543 int reql;
1544 struct stream_interface *si = appctx->owner;
1545
1546 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1547 if (reql <= 0) /* closed or EOL not found */
1548 goto incomplete;
1549
1550 *totl += reql;
1551
1552 if ((unsigned int)msg_head[1] < 128)
1553 return 1;
1554
1555 /* Read and Decode message length */
1556 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1557 if (reql <= 0) /* closed */
1558 goto incomplete;
1559
1560 *totl += reql;
1561
1562 if ((unsigned int)msg_head[2] < 240) {
1563 *msg_len = msg_head[2];
1564 }
1565 else {
1566 int i;
1567 char *cur;
1568 char *end;
1569
1570 for (i = 3 ; i < msg_head_sz ; i++) {
1571 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1572 if (reql <= 0) /* closed */
1573 goto incomplete;
1574
1575 *totl += reql;
1576
1577 if (!(msg_head[i] & 0x80))
1578 break;
1579 }
1580
1581 if (i == msg_head_sz) {
1582 /* malformed message */
1583 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1584 return -1;
1585 }
1586 end = msg_head + msg_head_sz;
1587 cur = &msg_head[2];
1588 *msg_len = intdecode(&cur, end);
1589 if (!cur) {
1590 /* malformed message */
1591 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1592 return -1;
1593 }
1594 }
1595
1596 /* Read message content */
1597 if (*msg_len) {
1598 if (*msg_len > trash.size) {
1599 /* Status code is not success, abort */
1600 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1601 return -1;
1602 }
1603
1604 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1605 if (reql <= 0) /* closed */
1606 goto incomplete;
1607 *totl += reql;
1608 }
1609
1610 return 1;
1611
1612 incomplete:
1613 if (reql < 0) {
1614 /* there was an error */
1615 appctx->st0 = PEER_SESS_ST_END;
1616 return -1;
1617 }
1618
1619 return 0;
1620}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001621
1622/*
1623 * Treat the awaited message with <msg_head> as header.*
1624 * Return 1 if succeeded, 0 if not.
1625 */
1626static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1627 char **msg_cur, char *msg_end, int msg_len, int totl)
1628{
1629 struct stream_interface *si = appctx->owner;
1630 struct stream *s = si_strm(si);
1631 struct peers *peers = strm_fe(s)->parent;
1632
1633 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1634 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1635 struct shared_table *st;
1636 /* Reset message: remote need resync */
1637
1638 /* prepare tables fot a global push */
1639 for (st = peer->tables; st; st = st->next) {
1640 st->teaching_origin = st->last_pushed = st->table->update;
1641 st->flags = 0;
1642 }
1643
1644 /* reset teaching flags to 0 */
1645 peer->flags &= PEER_TEACH_RESET;
1646
1647 /* flag to start to teach lesson */
1648 peer->flags |= PEER_F_TEACH_PROCESS;
1649 }
1650 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1651 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1652 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1653 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1654 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1655 }
1656 peer->confirm++;
1657 }
1658 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1659 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1660 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1661 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1662
1663 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001664 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001665 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1666 }
1667 peer->confirm++;
1668 }
1669 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1670 struct shared_table *st;
1671
1672 /* If stopping state */
1673 if (stopping) {
1674 /* Close session, push resync no more needed */
1675 peer->flags |= PEER_F_TEACH_COMPLETE;
1676 appctx->st0 = PEER_SESS_ST_END;
1677 return 0;
1678 }
1679 for (st = peer->tables; st; st = st->next) {
1680 st->update = st->last_pushed = st->teaching_origin;
1681 st->flags = 0;
1682 }
1683
1684 /* reset teaching flags to 0 */
1685 peer->flags &= PEER_TEACH_RESET;
1686 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001687 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001688 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001689 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001690 }
1691 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1692 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1693 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1694 return 0;
1695 }
1696 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1697 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1698 return 0;
1699 }
1700 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1701 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1702 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1703 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1704 int update, expire;
1705
1706 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1707 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1708 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1709 msg_cur, msg_end, msg_len, totl))
1710 return 0;
1711
1712 }
1713 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1714 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1715 return 0;
1716 }
1717 }
1718 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1719 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1720 return 0;
1721 }
1722
1723 return 1;
1724}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001725
1726
1727/*
1728 * Send any message to <peer> peer.
1729 * Returns 1 if succeeded, or -1 or 0 if failed.
1730 * -1 means an internal error occured, 0 is for a peer protocol error leading
1731 * to a peer state change (from the peer I/O handler point of view).
1732 */
1733static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1734{
1735 int repl;
1736 struct stream_interface *si = appctx->owner;
1737 struct stream *s = si_strm(si);
1738 struct peers *peers = strm_fe(s)->parent;
1739
1740 /* Need to request a resync */
1741 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1742 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1743 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1744
1745 repl = peer_send_resync_reqmsg(appctx);
1746 if (repl <= 0)
1747 return repl;
1748
1749 peers->flags |= PEERS_F_RESYNC_PROCESS;
1750 }
1751
1752 /* Nothing to read, now we start to write */
1753 if (peer->tables) {
1754 struct shared_table *st;
1755 struct shared_table *last_local_table;
1756
1757 last_local_table = peer->last_local_table;
1758 if (!last_local_table)
1759 last_local_table = peer->tables;
1760 st = last_local_table->next;
1761
1762 while (1) {
1763 if (!st)
1764 st = peer->tables;
1765
1766 /* It remains some updates to ack */
1767 if (st->last_get != st->last_acked) {
1768 repl = peer_send_ackmsg(st, appctx);
1769 if (repl <= 0)
1770 return repl;
1771
1772 st->last_acked = st->last_get;
1773 }
1774
1775 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1776 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1777 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1778 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1779
1780 repl = peer_send_teach_process_msgs(appctx, peer, st);
1781 if (repl <= 0) {
1782 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1783 return repl;
1784 }
1785 }
1786 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1787 }
1788 else {
1789 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1790 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1791 if (repl <= 0)
1792 return repl;
1793 }
1794
1795 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1796 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1797 if (repl <= 0)
1798 return repl;
1799 }
1800 }
1801
1802 if (st == last_local_table)
1803 break;
1804 st = st->next;
1805 }
1806 }
1807
1808 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1809 repl = peer_send_resync_finishedmsg(appctx, peer);
1810 if (repl <= 0)
1811 return repl;
1812
1813 /* flag finished message sent */
1814 peer->flags |= PEER_F_TEACH_FINISHED;
1815 }
1816
1817 /* Confirm finished or partial messages */
1818 while (peer->confirm) {
1819 repl = peer_send_resync_confirmsg(appctx);
1820 if (repl <= 0)
1821 return repl;
1822
1823 peer->confirm--;
1824 }
1825
1826 return 1;
1827}
1828
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001829/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001830 * Read and parse a first line of a "hello" peer protocol message.
1831 * Returns 0 if could not read a line, -1 if there was a read error or
1832 * the line is malformed, 1 if succeeded.
1833 */
1834static inline int peer_getline_version(struct appctx *appctx,
1835 unsigned int *maj_ver, unsigned int *min_ver)
1836{
1837 int reql;
1838
1839 reql = peer_getline(appctx);
1840 if (!reql)
1841 return 0;
1842
1843 if (reql < 0)
1844 return -1;
1845
1846 /* test protocol */
1847 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1848 appctx->st0 = PEER_SESS_ST_EXIT;
1849 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1850 return -1;
1851 }
1852 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1853 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1854 appctx->st0 = PEER_SESS_ST_EXIT;
1855 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1856 return -1;
1857 }
1858
1859 return 1;
1860}
1861
1862/*
1863 * Read and parse a second line of a "hello" peer protocol message.
1864 * Returns 0 if could not read a line, -1 if there was a read error or
1865 * the line is malformed, 1 if succeeded.
1866 */
1867static inline int peer_getline_host(struct appctx *appctx)
1868{
1869 int reql;
1870
1871 reql = peer_getline(appctx);
1872 if (!reql)
1873 return 0;
1874
1875 if (reql < 0)
1876 return -1;
1877
1878 /* test hostname match */
1879 if (strcmp(localpeer, trash.area) != 0) {
1880 appctx->st0 = PEER_SESS_ST_EXIT;
1881 appctx->st1 = PEER_SESS_SC_ERRHOST;
1882 return -1;
1883 }
1884
1885 return 1;
1886}
1887
1888/*
1889 * Read and parse a last line of a "hello" peer protocol message.
1890 * Returns 0 if could not read a character, -1 if there was a read error or
1891 * the line is malformed, 1 if succeeded.
1892 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1893 */
1894static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1895{
1896 char *p;
1897 int reql;
1898 struct peer *peer;
1899 struct stream_interface *si = appctx->owner;
1900 struct stream *s = si_strm(si);
1901 struct peers *peers = strm_fe(s)->parent;
1902
1903 reql = peer_getline(appctx);
1904 if (!reql)
1905 return 0;
1906
1907 if (reql < 0)
1908 return -1;
1909
1910 /* parse line "<peer name> <pid> <relative_pid>" */
1911 p = strchr(trash.area, ' ');
1912 if (!p) {
1913 appctx->st0 = PEER_SESS_ST_EXIT;
1914 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1915 return -1;
1916 }
1917 *p = 0;
1918
1919 /* lookup known peer */
1920 for (peer = peers->remote; peer; peer = peer->next) {
1921 if (strcmp(peer->id, trash.area) == 0)
1922 break;
1923 }
1924
1925 /* if unknown peer */
1926 if (!peer) {
1927 appctx->st0 = PEER_SESS_ST_EXIT;
1928 appctx->st1 = PEER_SESS_SC_ERRPEER;
1929 return -1;
1930 }
1931 *curpeer = peer;
1932
1933 return 1;
1934}
1935
1936/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001937 * Init <peer> peer after having accepted it at peer protocol level.
1938 */
1939static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1940{
1941 struct shared_table *st;
1942
1943 /* Register status code */
1944 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1945
1946 /* Awake main task */
1947 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1948
1949 /* Init confirm counter */
1950 peer->confirm = 0;
1951
1952 /* Init cursors */
1953 for (st = peer->tables; st ; st = st->next) {
1954 st->last_get = st->last_acked = 0;
1955 st->teaching_origin = st->last_pushed = st->update;
1956 }
1957
1958 /* reset teaching and learning flags to 0 */
1959 peer->flags &= PEER_TEACH_RESET;
1960 peer->flags &= PEER_LEARN_RESET;
1961
1962 /* if current peer is local */
1963 if (peer->local) {
1964 /* if current host need resyncfrom local and no process assined */
1965 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1966 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1967 /* assign local peer for a lesson, consider lesson already requested */
1968 peer->flags |= PEER_F_LEARN_ASSIGN;
1969 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1970 }
1971
1972 }
1973 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1974 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1975 /* assign peer for a lesson */
1976 peer->flags |= PEER_F_LEARN_ASSIGN;
1977 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1978 }
1979}
1980
1981/*
1982 * Init <peer> peer after having connected it at peer protocol level.
1983 */
1984static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1985{
1986 struct shared_table *st;
1987
1988 /* Init cursors */
1989 for (st = peer->tables; st ; st = st->next) {
1990 st->last_get = st->last_acked = 0;
1991 st->teaching_origin = st->last_pushed = st->update;
1992 }
1993
1994 /* Init confirm counter */
1995 peer->confirm = 0;
1996
1997 /* reset teaching and learning flags to 0 */
1998 peer->flags &= PEER_TEACH_RESET;
1999 peer->flags &= PEER_LEARN_RESET;
2000
2001 /* If current peer is local */
2002 if (peer->local) {
2003 /* flag to start to teach lesson */
2004 peer->flags |= PEER_F_TEACH_PROCESS;
2005 }
2006 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2007 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2008 /* If peer is remote and resync from remote is needed,
2009 and no peer currently assigned */
2010
2011 /* assign peer for a lesson */
2012 peer->flags |= PEER_F_LEARN_ASSIGN;
2013 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2014 }
2015}
2016
2017/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002018 * IO Handler to handle message exchance with a peer
2019 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002020static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002021{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002022 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002023 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002024 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002025 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 int reql = 0;
2027 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002028 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002029 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002030
Joseph Herlant82b2f542018-11-15 12:19:14 -08002031 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002032 if (si_ic(si)->buf.size == 0) {
2033 si_rx_room_blk(si);
2034 goto out;
2035 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002036
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002038 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002039switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002040 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002041 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002042 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002043 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002044 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002045 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002046 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002047 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002048 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002049 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2050 if (reql <= 0) {
2051 if (!reql)
2052 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002053 goto switchstate;
2054 }
2055
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002056 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002057 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002058 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002059 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002060 reql = peer_getline_host(appctx);
2061 if (reql <= 0) {
2062 if (!reql)
2063 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002064 goto switchstate;
2065 }
2066
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002067 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002068 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002069 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002070 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002071 reql = peer_getline_last(appctx, &curpeer);
2072 if (reql <= 0) {
2073 if (!reql)
2074 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002075 goto switchstate;
2076 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002077
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002078 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002079 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002080 if (curpeer->local) {
2081 /* Local connection, reply a retry */
2082 appctx->st0 = PEER_SESS_ST_EXIT;
2083 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2084 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002085 }
Emeric Brun80527f52017-06-19 17:46:37 +02002086
2087 /* we're killing a connection, we must apply a random delay before
2088 * retrying otherwise the other end will do the same and we can loop
2089 * for a while.
2090 */
2091 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002092 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002093 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002094 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2095 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2096 curpeer->flags |= PEER_F_DWNGRD;
2097 }
2098 else {
2099 curpeer->flags &= ~PEER_F_DWNGRD;
2100 }
2101 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002102 curpeer->appctx = appctx;
2103 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002104 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002105 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 /* fall through */
2107 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002108 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002109 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002110 if (!curpeer) {
2111 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002112 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002113 if (curpeer->appctx != appctx) {
2114 appctx->st0 = PEER_SESS_ST_END;
2115 goto switchstate;
2116 }
2117 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002118
2119 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002120 if (repl <= 0) {
2121 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002122 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002123 goto switchstate;
2124 }
2125
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002126 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002127
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002129 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002130 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002131 goto switchstate;
2132 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002133 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002134 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002135 if (!curpeer) {
2136 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002137 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002138 if (curpeer->appctx != appctx) {
2139 appctx->st0 = PEER_SESS_ST_END;
2140 goto switchstate;
2141 }
2142 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002143
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002144 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002145 if (repl <= 0) {
2146 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002147 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002148 goto switchstate;
2149 }
2150
2151 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002152 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002153 /* fall through */
2154 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002155 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002156 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002157 if (!curpeer) {
2158 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002159 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002160 if (curpeer->appctx != appctx) {
2161 appctx->st0 = PEER_SESS_ST_END;
2162 goto switchstate;
2163 }
2164 }
2165
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002166 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002167 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002168
Frédéric Lécaillece025572019-01-21 13:38:06 +01002169 reql = peer_getline(appctx);
2170 if (!reql)
2171 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002172
Frédéric Lécaillece025572019-01-21 13:38:06 +01002173 if (reql < 0)
2174 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002175
2176 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002177 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002178
2179 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002180 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002181
2182 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002183 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002184 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002185 }
2186 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002187 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2188 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002189 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002190 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002191 goto switchstate;
2192 }
Olivier Houcharded879892019-03-08 18:53:43 +01002193 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002194 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002195 /* fall through */
2196 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002197 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002198 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002199 char *msg_cur = trash.area;
2200 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002201 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002202 int totl = 0;
2203
Willy Tarreau2d372c22018-11-05 17:12:27 +01002204 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002205 if (!curpeer) {
2206 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002207 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002208 if (curpeer->appctx != appctx) {
2209 appctx->st0 = PEER_SESS_ST_END;
2210 goto switchstate;
2211 }
2212 }
2213
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002214 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2215 if (reql <= 0) {
2216 if (reql == -1)
2217 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002218 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002219 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002220
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002221 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002222 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002223 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002224
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002225 curpeer->flags |= PEER_F_ALIVE;
2226
Emeric Brun2b920a12010-09-23 18:30:22 +02002227 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002228 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002229 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002230 goto switchstate;
2231
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002232send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002233 if (curpeer->flags & PEER_F_HEARTBEAT) {
2234 curpeer->flags &= ~PEER_F_HEARTBEAT;
2235 repl = peer_send_heartbeatmsg(appctx);
2236 if (repl <= 0) {
2237 if (repl == -1)
2238 goto out;
2239 goto switchstate;
2240 }
2241 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002242 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002243 repl = peer_send_msgs(appctx, curpeer);
2244 if (repl <= 0) {
2245 if (repl == -1)
2246 goto out;
2247 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002248 }
2249
Emeric Brun2b920a12010-09-23 18:30:22 +02002250 /* noting more to do */
2251 goto out;
2252 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002253 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002254 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002255 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002256 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002257 if (peer_send_status_errormsg(appctx) == -1)
2258 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002259 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002260 goto switchstate;
2261 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002262 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002263 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002264 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002265 if (peer_send_error_size_limitmsg(appctx) == -1)
2266 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002267 appctx->st0 = PEER_SESS_ST_END;
2268 goto switchstate;
2269 }
2270 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002271 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002272 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002273 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002274 if (peer_send_error_protomsg(appctx) == -1)
2275 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002276 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002277 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002278 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002279 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002280 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002281 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002282 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002283 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002284 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002285 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002286 curpeer = NULL;
2287 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002288 si_shutw(si);
2289 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002290 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002291 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002292 }
2293 }
2294 }
2295out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002296 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002297
2298 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002299 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002300 return;
2301}
2302
Willy Tarreau30576452015-04-13 13:50:30 +02002303static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002304 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002305 .name = "<PEER>", /* used for logging */
2306 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002307 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002308};
Emeric Brun2b920a12010-09-23 18:30:22 +02002309
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002310
Emeric Brun2b920a12010-09-23 18:30:22 +02002311/*
2312 * Use this function to force a close of a peer session
2313 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002314static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002315{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002316 struct appctx *appctx = peer->appctx;
2317
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002318 /* Note that the peer sessions which have just been created
2319 * (->st0 == PEER_SESS_ST_CONNECT) must not
2320 * be shutdown, if not, the TCP session will never be closed
2321 * and stay in CLOSE_WAIT state after having been closed by
2322 * the remote side.
2323 */
2324 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 return;
2326
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002327 if (appctx->applet != &peer_applet)
2328 return;
2329
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002330 __peer_session_deinit(peer);
2331
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002332 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002333 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002334}
2335
Willy Tarreau91d96282015-03-13 15:47:26 +01002336/* Pre-configures a peers frontend to accept incoming connections */
2337void peers_setup_frontend(struct proxy *fe)
2338{
2339 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002340 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002341 fe->maxconn = 0;
2342 fe->conn_retries = CONN_RETRIES;
2343 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002344 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002345 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002346 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002347 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002348}
2349
Emeric Brun2b920a12010-09-23 18:30:22 +02002350/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002351 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002352 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002353static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002354{
Willy Tarreau04b92862017-09-15 11:01:04 +02002355 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002357 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002358 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002359 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002360 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002361
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002362 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002363 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002364 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002365 s = NULL;
2366
Emeric Brun1138fd02017-06-19 12:38:55 +02002367 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002368 if (!appctx)
2369 goto out_close;
2370
2371 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002372 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002373
Willy Tarreau04b92862017-09-15 11:01:04 +02002374 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002375 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002376 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002377 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002378 }
2379
Willy Tarreau87787ac2017-08-28 16:22:54 +02002380 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002381 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002382 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002383 }
2384
Willy Tarreau342bfb12015-04-05 01:35:34 +02002385 /* The tasks below are normally what is supposed to be done by
2386 * fe->accept().
2387 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002388 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002389
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002390 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002391 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002392 appctx_wakeup(appctx);
2393
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002394 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002395 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002396 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002397
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002398 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002399 * pre-initialized connection in si->conn.
2400 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002401 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002402 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002403
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002404 if (unlikely((cs = cs_new(conn)) == NULL))
2405 goto out_free_conn;
2406
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002407 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002408 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2409
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002410 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002411 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2412 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002413 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002414
Emeric Brun2b920a12010-09-23 18:30:22 +02002415 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002416 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002417
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002418 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002419
Emeric Brunb3971ab2015-05-12 18:49:09 +02002420 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002421 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002422 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002423 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002424
2425 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002426out_free_cs:
2427 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002428 out_free_conn:
2429 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002430 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002431 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002432 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002433 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002434 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002435 out_free_appctx:
2436 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002437 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002438 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002439}
2440
2441/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002442 * Task processing function to manage re-connect, peer session
2443 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002444 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002445static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002446{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002447 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002448 struct peer *ps;
2449 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002450
2451 task->expire = TICK_ETERNITY;
2452
Emeric Brunb3971ab2015-05-12 18:49:09 +02002453 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002454 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002455 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002456 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002457 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002458 return NULL;
2459 }
2460
Emeric Brun80527f52017-06-19 17:46:37 +02002461 /* Acquire lock for all peers of the section */
2462 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002463 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002464
Emeric Brun2b920a12010-09-23 18:30:22 +02002465 if (!stopping) {
2466 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002467
2468 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2469 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2470 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002471 /* Resync from local peer needed
2472 no peer was assigned for the lesson
2473 and no old local peer found
2474 or resync timeout expire */
2475
2476 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002477 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002478
2479 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002480 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002481 }
2482
2483 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002484 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002485 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002486 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002487 if (!ps->appctx) {
2488 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002489 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002490 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002491 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002492 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002493 tick_is_expired(ps->reconnect, now_ms))) {
2494 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002495 * or previous peer connection established with success
2496 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002497 * and reconnection timer is expired */
2498
2499 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002500 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002501 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002502 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002503 /* If previous session failed during connection
2504 * but reconnection timer is not expired */
2505
2506 /* reschedule task for reconnect */
2507 task->expire = tick_first(task->expire, ps->reconnect);
2508 }
2509 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002510 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002511 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002512 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002513 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2514 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002515 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2516 /* Resync from a remote is needed
2517 * and no peer was assigned for lesson
2518 * and current peer may be up2date */
2519
2520 /* assign peer for the lesson */
2521 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002522 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002523
Willy Tarreau9df94c22016-10-31 18:42:52 +01002524 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002525 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002526 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002527 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002528 int update_to_push = 0;
2529
Emeric Brunb3971ab2015-05-12 18:49:09 +02002530 /* Awake session if there is data to push */
2531 for (st = ps->tables; st ; st = st->next) {
2532 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002533 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002534 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002535 /* There is no need to send a heartbeat message
2536 * when some updates must be pushed. The remote
2537 * peer will consider <ps> peer as alive when it will
2538 * receive these updates.
2539 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002540 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002541 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002542 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002543 /* We are going to send updates, let's ensure we will
2544 * come back to send heartbeat messages or to reconnect.
2545 */
2546 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002547 appctx_wakeup(ps->appctx);
2548 break;
2549 }
2550 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002551 /* When there are updates to send we do not reconnect
2552 * and do not send heartbeat message either.
2553 */
2554 if (!update_to_push) {
2555 if (tick_is_expired(ps->reconnect, now_ms)) {
2556 if (ps->flags & PEER_F_ALIVE) {
2557 /* This peer was alive during a 'reconnect' period.
2558 * Flag it as not alive again for the next period.
2559 */
2560 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002561 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002562 }
2563 else {
2564 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002565 peer_session_forceshutdown(ps);
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002566 }
2567 }
2568 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2569 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2570 ps->flags |= PEER_F_HEARTBEAT;
2571 appctx_wakeup(ps->appctx);
2572 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002573 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002574 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002575 }
2576 /* else do nothing */
2577 } /* SUCCESSCODE */
2578 } /* !ps->peer->local */
2579 } /* for */
2580
2581 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002582 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2583 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2584 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002585 /* Resync from remote peer needed
2586 * no peer was assigned for the lesson
2587 * and resync timeout expire */
2588
2589 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002590 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002591 }
2592
Emeric Brunb3971ab2015-05-12 18:49:09 +02002593 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002594 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002595 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2596 if (!tick_is_expired(peers->resync_timeout, now_ms))
2597 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002598 }
2599 } /* !stopping */
2600 else {
2601 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002602 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002603 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002604 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002605 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002606 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002607 peers->flags |= PEERS_F_DONOTSTOP;
2608 ps = peers->local;
2609 for (st = ps->tables; st ; st = st->next)
2610 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002611 }
2612
2613 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002614 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002615 /* we're killing a connection, we must apply a random delay before
2616 * retrying otherwise the other end will do the same and we can loop
2617 * for a while.
2618 */
2619 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002620 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002621 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002622 }
2623 }
2624 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002625
Emeric Brunb3971ab2015-05-12 18:49:09 +02002626 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002627 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002628 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002629 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002630 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002631 peers->flags &= ~PEERS_F_DONOTSTOP;
2632 for (st = ps->tables; st ; st = st->next)
2633 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002634 }
2635 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002636 else if (!ps->appctx) {
2637 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002638 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002639 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2640 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2641 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002642 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002643 * or previous peer connection was successfully established
2644 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002645 * or during previous connect, peer replies a try again statuscode */
2646
2647 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002648 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002649 }
2650 else {
2651 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002652 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002653 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002654 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002655 peers->flags &= ~PEERS_F_DONOTSTOP;
2656 for (st = ps->tables; st ; st = st->next)
2657 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002658 }
2659 }
2660 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002661 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002662 /* current peer connection is active and established
2663 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002664 for (st = ps->tables; st ; st = st->next) {
2665 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002666 appctx_wakeup(ps->appctx);
2667 break;
2668 }
2669 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002670 }
2671 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002672
2673 /* Release lock for all peers of the section */
2674 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002675 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002676
Emeric Brun2b920a12010-09-23 18:30:22 +02002677 /* Wakeup for re-connect */
2678 return task;
2679}
2680
Emeric Brunb3971ab2015-05-12 18:49:09 +02002681
Emeric Brun2b920a12010-09-23 18:30:22 +02002682/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002683 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002684 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002685int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002686{
Emeric Brun2b920a12010-09-23 18:30:22 +02002687 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002688
Emeric Brun2b920a12010-09-23 18:30:22 +02002689 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002690 peers->peers_fe->maxconn += 3;
2691 }
2692
Emeric Brunc60def82017-09-27 14:59:38 +02002693 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002694 if (!peers->sync_task)
2695 return 0;
2696
Emeric Brunb3971ab2015-05-12 18:49:09 +02002697 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002698 peers->sync_task->context = (void *)peers;
2699 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2700 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002701 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002702}
2703
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002704/*
2705 * Allocate a cache a dictionary entries used upon transmission.
2706 */
2707struct dcache_tx *new_dcache_tx(size_t max_entries)
2708{
2709 struct dcache_tx *d;
2710
2711 d = malloc(sizeof *d);
2712 if (!d)
2713 return NULL;
2714
2715 d->lru_key = 0;
2716 d->keys = EB_ROOT_UNIQUE;
2717 d->values = EB_ROOT_UNIQUE;
2718 d->max_entries = max_entries;
2719
2720 return d;
2721}
2722
2723/*
2724 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
2725 * as maximum of entries.
2726 * Return the dictionay cache if succeeded, NULL if not.
2727 * Must be deallocated calling free_dcache().
2728 */
2729struct dcache *new_dcache(size_t max_entries)
2730{
2731 struct dcache_tx *dc_tx;
2732 struct dcache *dc;
2733 struct dcache_rx *dc_rx;
2734
2735 dc = calloc(1, sizeof *dc);
2736 dc_tx = new_dcache_tx(max_entries);
2737 dc_rx = calloc(max_entries, sizeof *dc_rx);
2738 if (!dc || !dc_tx || !dc_rx)
2739 goto err;
2740
2741 dc->tx = dc_tx;
2742 dc->rx = dc_rx;
2743 dc->max_entries = max_entries;
2744
2745 return dc;
2746
2747 err:
2748 free(dc);
2749 free(dc_tx);
2750 free(dc_rx);
2751 return NULL;
2752}
2753
2754/*
2755 * Deallocate a cache of dictionary entries.
2756 */
2757void free_dcache(struct dcache *dc)
2758{
2759 free(dc->tx); dc->tx = NULL;
2760 free(dc->rx); dc->rx = NULL;
2761 free(dc);
2762}
2763
2764/*
2765 * Look for the dictionary entry with <k> as key in <d> cache of dictionary entries
2766 * used upon transmission.
2767 * Return the entry if found, NULL if not.
2768 */
2769struct dcache_tx_entry *dcache_tx_lookup_key(struct dcache_tx *d, unsigned int k)
2770{
2771 struct dcache_tx_entry *de;
2772 struct eb32_node *node;
2773
2774 de = NULL;
2775 node = eb32_lookup(&d->keys, k);
2776 if (node)
2777 de = container_of(node, struct dcache_tx_entry, key);
2778
2779 return de;
2780}
2781
2782/*
2783 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
2784 * entries used upon transmission.
2785 * Return the entry if found, NULL if not.
2786 */
2787struct dcache_tx_entry *dcache_tx_lookup_value(struct dcache_tx *d,
2788 struct dcache_tx_entry *i)
2789{
2790 struct dcache_tx_entry *de;
2791 struct ebpt_node *node;
2792
2793 de = NULL;
2794 node = ebpt_lookup(&d->values, i->value.key);
2795 if (node)
2796 de = container_of(node, struct dcache_tx_entry, value);
2797
2798 return de;
2799}
2800
2801/*
2802 * Release the memory allocated for the cached dictionary entry <dce>.
2803 */
2804
2805static inline void free_dcache_tx_entry(struct dcache_tx_entry *dce)
2806{
2807 dce->value.key = NULL;
2808 dce->key.key = 0;
2809 free(dce);
2810}
2811
2812/*
2813 * Allocate a new dictionary entry with <s> as string value which is strdup()'ed.
2814 * Returns the new allocated entry if succeeded, NULL if not.
2815 */
2816struct dcache_tx_entry *new_dcache_tx_entry(unsigned int k, struct dict_entry *de)
2817{
2818 struct dcache_tx_entry *dce;
2819
2820 dce = calloc(1, sizeof *dce);
2821 if (!dce)
2822 return NULL;
2823
2824 dce->value.key = de;
2825 dce->key.key = k;
2826
2827 return dce;
2828}
2829
2830/*
2831 * Flush <dc> cache.
2832 * Always succeeds.
2833 */
2834static inline void flush_dcache(struct peer *peer)
2835{
2836 int i;
2837 struct dcache *dc = peer_stkt_dict_cache(peer, st);
2838
2839 if (!eb_is_empty(&dc->tx->keys)) {
2840 struct eb32_node *node, *next;
2841
2842 for (node = eb32_first(&dc->tx->keys); node; node = next) {
2843 next = eb32_next(node);
2844 eb32_delete(node);
2845 }
2846 }
2847
2848 if (!eb_is_empty(&dc->tx->values)) {
2849 struct ebpt_node *node, *next;
2850
2851 for (node = ebpt_first(&dc->tx->values); node; node = next) {
2852 struct dcache_tx_entry *de;
2853
2854 next = ebpt_next(node);
2855 ebpt_delete(node);
2856 de = container_of(node, struct dcache_tx_entry, value);
2857 free_dcache_tx_entry(de);
2858 }
2859 }
2860
2861 for (i = 0; i < dc->max_entries; i++) {
2862 dc->rx[i].id = 0;
2863 dc->rx[i].de = NULL;
2864 }
2865}
2866
2867/*
2868 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
2869 * with information provided by <i> dictionary cache entry (especially the value
2870 * to be inserted if not already). Return <i> if already present in the cache
2871 * or something different of <i> if not already in the cache, or NULL if something
2872 * failed.
2873 */
2874struct dcache_tx_entry *dcache_tx_insert(struct dcache *dc,
2875 struct dcache_tx_entry *i)
2876{
2877 struct dcache_tx *dc_tx;
2878 struct dcache_tx_entry *o;
2879
2880 dc_tx = dc->tx;
2881 o = dcache_tx_lookup_value(dc_tx, i);
2882 if (o) {
2883 /* Copy the key. */
2884 i->key.key = o->key.key;
2885 return i;
2886 }
2887
2888 o = dcache_tx_lookup_key(dc_tx, dc_tx->lru_key);
2889 if (o) {
2890 o->value.key = i->value.key;
2891 return o;
2892 }
2893
2894 o = new_dcache_tx_entry(dc_tx->lru_key, i->value.key);
2895 if (!o)
2896 return NULL;
2897
2898 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc_tx->max_entries - 1);
2899 eb32_insert(&dc_tx->keys, &o->key);
2900 ebpt_insert(&dc_tx->values, &o->value);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002901
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002902 return o;
2903}
Emeric Brunb3971ab2015-05-12 18:49:09 +02002904
2905/*
2906 * Function used to register a table for sync on a group of peers
2907 *
2908 */
2909void peers_register_table(struct peers *peers, struct stktable *table)
2910{
2911 struct shared_table *st;
2912 struct peer * curpeer;
2913 int id = 0;
2914
2915 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002916 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002917 st->table = table;
2918 st->next = curpeer->tables;
2919 if (curpeer->tables)
2920 id = curpeer->tables->local_id;
2921 st->local_id = id + 1;
2922
2923 curpeer->tables = st;
2924 }
2925
2926 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002927}
2928
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002929/*
2930 * Parse the "show peers" command arguments.
2931 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
2932 * error message.
2933 */
2934static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
2935{
2936 appctx->ctx.cfgpeers.target = NULL;
2937
2938 if (*args[2]) {
2939 struct peers *p;
2940
2941 for (p = cfg_peers; p; p = p->next) {
2942 if (!strcmp(p->id, args[2])) {
2943 appctx->ctx.cfgpeers.target = p;
2944 break;
2945 }
2946 }
2947
2948 if (!p) {
2949 appctx->ctx.cli.severity = LOG_ERR;
2950 appctx->ctx.cli.msg = "No such peers\n";
2951 appctx->st0 = CLI_ST_PRINT;
2952 return 1;
2953 }
2954 }
2955
2956 return 0;
2957}
2958
2959/*
2960 * This function dumps the peer state information of <peers> "peers" section.
2961 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
2962 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
2963 */
2964static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
2965{
2966 struct tm tm;
2967
2968 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02002969 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 +02002970 peers,
2971 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2972 tm.tm_hour, tm.tm_min, tm.tm_sec,
2973 peers->id, peers->state, peers->flags,
2974 peers->resync_timeout ?
2975 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
2976 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02002977 TICKS_TO_MS(1000)) : "<NEVER>",
2978 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02002979
2980 if (ci_putchk(si_ic(si), msg) == -1) {
2981 si_rx_room_blk(si);
2982 return 0;
2983 }
2984
2985 return 1;
2986}
2987
2988/*
2989 * This function dumps <peer> state information.
2990 * Returns 0 if the output buffer is full and needs to be called again, non-zero
2991 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
2992 */
2993static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
2994{
2995 struct connection *conn;
2996 char pn[INET6_ADDRSTRLEN];
2997 struct stream_interface *peer_si;
2998 struct stream *peer_s;
2999 struct appctx *appctx;
3000 struct shared_table *st;
3001
3002 addr_to_str(&peer->addr, pn, sizeof pn);
3003 chunk_appendf(msg, " %p: id=%s(%s) addr=%s:%d status=%s reconnect=%s confirm=%u\n",
3004 peer, peer->id,
3005 peer->local ? "local" : "remote",
3006 pn, get_host_port(&peer->addr),
3007 statuscode_str(peer->statuscode),
3008 peer->reconnect ?
3009 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3010 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3011 TICKS_TO_MS(1000)) : "<NEVER>",
3012 peer->confirm);
3013
3014 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3015
3016 appctx = peer->appctx;
3017 if (!appctx)
3018 goto end;
3019
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003020 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3021 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003022
3023 peer_si = peer->appctx->owner;
3024 if (!peer_si)
3025 goto end;
3026
3027 peer_s = si_strm(peer_si);
3028 if (!peer_s)
3029 goto end;
3030
3031 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3032
3033 conn = objt_conn(strm_orig(peer_s));
3034 if (conn)
3035 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3036
3037 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3038 case AF_INET:
3039 case AF_INET6:
3040 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(&conn->addr.from));
3041 break;
3042 case AF_UNIX:
3043 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3044 break;
3045 }
3046
3047 if (conn)
3048 conn_get_to_addr(conn);
3049
3050 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3051 case AF_INET:
3052 case AF_INET6:
3053 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(&conn->addr.to));
3054 break;
3055 case AF_UNIX:
3056 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3057 break;
3058 }
3059
3060 if (peer->remote_table)
3061 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3062 peer->remote_table,
3063 peer->remote_table->table->id,
3064 peer->remote_table->local_id,
3065 peer->remote_table->remote_id);
3066
3067 if (peer->last_local_table)
3068 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3069 peer->last_local_table,
3070 peer->last_local_table->table->id,
3071 peer->last_local_table->local_id,
3072 peer->last_local_table->remote_id);
3073
3074 if (peer->tables) {
3075 chunk_appendf(&trash, "\n shared tables:");
3076 for (st = peer->tables; st; st = st->next) {
3077 struct stktable *t;
3078 t = st->table;
3079 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3080 "flags=0x%x remote_data=0x%llx",
3081 st, st->local_id, st->remote_id,
3082 st->flags, (unsigned long long)st->remote_data);
3083 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3084 " teaching_origin=%u update=%u",
3085 st->last_acked, st->last_pushed, st->last_get,
3086 st->teaching_origin, st->update);
3087 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
3088 " commitupdate=%u syncing=%u",
3089 t, t->id, t->update, t->localupdate, t->commitupdate, t->syncing);
3090 }
3091 }
3092
3093 end:
3094 chunk_appendf(&trash, "\n");
3095 if (ci_putchk(si_ic(si), msg) == -1) {
3096 si_rx_room_blk(si);
3097 return 0;
3098 }
3099
3100 return 1;
3101}
3102
3103/*
3104 * This function dumps all the peers of "peers" section.
3105 * Returns 0 if the output buffer is full and needs to be called
3106 * again, non-zero if not. It proceeds in an isolated thread, so
3107 * there is no thread safety issue here.
3108 */
3109static int cli_io_handler_show_peers(struct appctx *appctx)
3110{
3111 int show_all;
3112 int ret = 0, first_peers = 1;
3113 struct stream_interface *si = appctx->owner;
3114
3115 thread_isolate();
3116
3117 show_all = !appctx->ctx.cfgpeers.target;
3118
3119 chunk_reset(&trash);
3120
3121 while (appctx->st2 != STAT_ST_FIN) {
3122 switch (appctx->st2) {
3123 case STAT_ST_INIT:
3124 if (show_all)
3125 appctx->ctx.cfgpeers.peers = cfg_peers;
3126 else
3127 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3128
3129 appctx->st2 = STAT_ST_LIST;
3130 /* fall through */
3131
3132 case STAT_ST_LIST:
3133 if (!appctx->ctx.cfgpeers.peers) {
3134 /* No more peers list. */
3135 appctx->st2 = STAT_ST_END;
3136 }
3137 else {
3138 if (!first_peers)
3139 chunk_appendf(&trash, "\n");
3140 else
3141 first_peers = 0;
3142 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3143 goto out;
3144
3145 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3146 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3147 appctx->st2 = STAT_ST_INFO;
3148 }
3149 break;
3150
3151 case STAT_ST_INFO:
3152 if (!appctx->ctx.cfgpeers.peer) {
3153 /* End of peer list */
3154 if (show_all)
3155 appctx->st2 = STAT_ST_LIST;
3156 else
3157 appctx->st2 = STAT_ST_END;
3158 }
3159 else {
3160 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3161 goto out;
3162
3163 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3164 }
3165 break;
3166
3167 case STAT_ST_END:
3168 appctx->st2 = STAT_ST_FIN;
3169 break;
3170 }
3171 }
3172 ret = 1;
3173 out:
3174 thread_release();
3175 return ret;
3176}
3177
3178/*
3179 * CLI keywords.
3180 */
3181static struct cli_kw_list cli_kws = {{ }, {
3182 { { "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, },
3183 {},
3184}};
3185
3186/* Register cli keywords */
3187INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3188