blob: 76e94c15e0e8b990802020a13477d1e7003bc87c [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>
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +020039#include <proto/dict.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020040#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010041#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020042#include <proto/log.h>
43#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020044#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020045#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020047#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020048#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010049#include <proto/signal.h>
50#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020052#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020053
54
55/*******************************/
56/* Current peer learning state */
57/*******************************/
58
59/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020060/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020061/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010062#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
63#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
64#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
65#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
66#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
67 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020068
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010069#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70#define PEERS_RESYNC_FROMLOCAL 0x00000000
71#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
72#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020073
74/***********************************/
75/* Current shared table sync state */
76/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010077#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
78#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020079
80/******************************/
81/* Remote peer teaching state */
82/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010083#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
84#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
85#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
86#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
87#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
88#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
89#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
90#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 +020091
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010092#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
93#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020094
Frédéric Lécaille54bff832019-03-26 10:25:20 +010095#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
96#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010097#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
98
Emeric Brunb3971ab2015-05-12 18:49:09 +020099/*****************************/
100/* Sync message class */
101/*****************************/
102enum {
103 PEER_MSG_CLASS_CONTROL = 0,
104 PEER_MSG_CLASS_ERROR,
105 PEER_MSG_CLASS_STICKTABLE = 10,
106 PEER_MSG_CLASS_RESERVED = 255,
107};
108
109/*****************************/
110/* control message types */
111/*****************************/
112enum {
113 PEER_MSG_CTRL_RESYNCREQ = 0,
114 PEER_MSG_CTRL_RESYNCFINISHED,
115 PEER_MSG_CTRL_RESYNCPARTIAL,
116 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100117 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200118};
119
120/*****************************/
121/* error message types */
122/*****************************/
123enum {
124 PEER_MSG_ERR_PROTOCOL = 0,
125 PEER_MSG_ERR_SIZELIMIT,
126};
127
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100128/*
129 * Parameters used by functions to build peer protocol messages. */
130struct peer_prep_params {
131 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100132 struct peer *peer;
133 } hello;
134 struct {
135 unsigned int st1;
136 } error_status;
137 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100138 struct stksess *stksess;
139 struct shared_table *shared_table;
140 unsigned int updateid;
141 int use_identifier;
142 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200143 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100144 } updt;
145 struct {
146 struct shared_table *shared_table;
147 } swtch;
148 struct {
149 struct shared_table *shared_table;
150 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100151 struct {
152 unsigned char head[2];
153 } control;
154 struct {
155 unsigned char head[2];
156 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100157};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200158
159/*******************************/
160/* stick table sync mesg types */
161/* Note: ids >= 128 contains */
162/* id message cotains data */
163/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200164#define PEER_MSG_STKT_UPDATE 0x80
165#define PEER_MSG_STKT_INCUPDATE 0x81
166#define PEER_MSG_STKT_DEFINE 0x82
167#define PEER_MSG_STKT_SWITCH 0x83
168#define PEER_MSG_STKT_ACK 0x84
169#define PEER_MSG_STKT_UPDATE_TIMED 0x85
170#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200171
Frédéric Lécaille39143342019-05-24 14:32:27 +0200172/* The maximum length of an encoded data length. */
173#define PEER_MSG_ENC_LENGTH_MAXLEN 5
174
175#define PEER_MSG_HEADER_LEN 2
176
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200177#define PEER_STKT_CACHE_MAX_ENTRIES 128
178
Emeric Brun2b920a12010-09-23 18:30:22 +0200179/**********************************/
180/* Peer Session IO handler states */
181/**********************************/
182
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100183enum {
184 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
185 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
186 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
187 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100188 /* after this point, data were possibly exchanged */
189 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
190 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
191 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
192 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
193 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200194 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
195 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100196 PEER_SESS_ST_END, /* Killed session */
197};
Emeric Brun2b920a12010-09-23 18:30:22 +0200198
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100199/***************************************************/
200/* Peer Session status code - part of the protocol */
201/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200202
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100203#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
204#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200205
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100206#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200207
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100208#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200209
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100210#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
211#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
212#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
213#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200214
215#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200216#define PEER_MAJOR_VER 2
217#define PEER_MINOR_VER 1
218#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200219
Willy Tarreau6254a922019-01-29 17:45:23 +0100220static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200221struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200222static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200223
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200224struct dcache_tx_entry *dcache_tx_insert(struct dcache *dc,
225 struct dcache_tx_entry *i);
226static inline void flush_dcache(struct peer *peer);
227
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200228static const char *statuscode_str(int statuscode)
229{
230 switch (statuscode) {
231 case PEER_SESS_SC_CONNECTCODE:
232 return "CONN";
233 case PEER_SESS_SC_CONNECTEDCODE:
234 return "HSHK";
235 case PEER_SESS_SC_SUCCESSCODE:
236 return "ESTA";
237 case PEER_SESS_SC_TRYAGAIN:
238 return "RETR";
239 case PEER_SESS_SC_ERRPROTO:
240 return "PROT";
241 case PEER_SESS_SC_ERRVERSION:
242 return "VERS";
243 case PEER_SESS_SC_ERRHOST:
244 return "NAME";
245 case PEER_SESS_SC_ERRPEER:
246 return "UNKN";
247 default:
248 return "NONE";
249 }
250}
251
Emeric Brun18928af2017-03-29 16:32:53 +0200252/* This function encode an uint64 to 'dynamic' length format.
253 The encoded value is written at address *str, and the
254 caller must assure that size after *str is large enought.
255 At return, the *str is set at the next Byte after then
256 encoded integer. The function returns then length of the
257 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200258int intencode(uint64_t i, char **str) {
259 int idx = 0;
260 unsigned char *msg;
261
Emeric Brunb3971ab2015-05-12 18:49:09 +0200262 msg = (unsigned char *)*str;
263 if (i < 240) {
264 msg[0] = (unsigned char)i;
265 *str = (char *)&msg[idx+1];
266 return (idx+1);
267 }
268
269 msg[idx] =(unsigned char)i | 240;
270 i = (i - 240) >> 4;
271 while (i >= 128) {
272 msg[++idx] = (unsigned char)i | 128;
273 i = (i - 128) >> 7;
274 }
275 msg[++idx] = (unsigned char)i;
276 *str = (char *)&msg[idx+1];
277 return (idx+1);
278}
279
280
281/* This function returns the decoded integer or 0
282 if decode failed
283 *str point on the beginning of the integer to decode
284 at the end of decoding *str point on the end of the
285 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200286uint64_t intdecode(char **str, char *end)
287{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200288 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200289 uint64_t i;
290 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200291
292 if (!*str)
293 return 0;
294
295 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200296 if (msg >= (unsigned char *)end)
297 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200298
Emeric Brun18928af2017-03-29 16:32:53 +0200299 i = *(msg++);
300 if (i >= 240) {
301 shift = 4;
302 do {
303 if (msg >= (unsigned char *)end)
304 goto fail;
305 i += (uint64_t)*msg << shift;
306 shift += 7;
307 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200308 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100309 *str = (char *)msg;
310 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200311
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100312 fail:
313 *str = NULL;
314 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315}
Emeric Brun2b920a12010-09-23 18:30:22 +0200316
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100317/*
318 * Build a "hello" peer protocol message.
319 * Return the number of written bytes written to build this messages if succeeded,
320 * 0 if not.
321 */
322static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
323{
324 int min_ver, ret;
325 struct peer *peer;
326
327 peer = p->hello.peer;
328 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
329 /* Prepare headers */
330 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
331 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
332 if (ret >= size)
333 return 0;
334
335 return ret;
336}
337
338/*
339 * Build a "handshake succeeded" status message.
340 * Return the number of written bytes written to build this messages if succeeded,
341 * 0 if not.
342 */
343static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
344{
345 int ret;
346
347 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
348 if (ret >= size)
349 return 0;
350
351 return ret;
352}
353
354/*
355 * Build an error status message.
356 * Return the number of written bytes written to build this messages if succeeded,
357 * 0 if not.
358 */
359static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
360{
361 int ret;
362 unsigned int st1;
363
364 st1 = p->error_status.st1;
365 ret = snprintf(msg, size, "%d\n", st1);
366 if (ret >= size)
367 return 0;
368
369 return ret;
370}
371
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200372/* Set the stick-table UPDATE message type byte at <msg_type> address,
373 * depending on <use_identifier> and <use_timed> boolean parameters.
374 * Always successful.
375 */
376static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
377{
378 if (use_timed) {
379 if (use_identifier)
380 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
381 else
382 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
383 }
384 else {
385 if (use_identifier)
386 *msg_type = PEER_MSG_STKT_UPDATE;
387 else
388 *msg_type = PEER_MSG_STKT_INCUPDATE;
389 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200390}
Emeric Brun2b920a12010-09-23 18:30:22 +0200391/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200392 * This prepare the data update message on the stick session <ts>, <st> is the considered
393 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800394 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200395 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
396 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200397 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100398static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200399{
400 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200401 unsigned short datalen;
402 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200403 unsigned int data_type;
404 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100405 struct stksess *ts;
406 struct shared_table *st;
407 unsigned int updateid;
408 int use_identifier;
409 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200410 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100411
412 ts = p->updt.stksess;
413 st = p->updt.shared_table;
414 updateid = p->updt.updateid;
415 use_identifier = p->updt.use_identifier;
416 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200417 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200418
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200419 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200420
Emeric Brun2b920a12010-09-23 18:30:22 +0200421 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200422
423 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200424 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200425 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200426 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200427
428 /* encode update identifier if needed */
429 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200430 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200431 memcpy(cursor, &netinteger, sizeof(netinteger));
432 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200433 }
434
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200435 if (use_timed) {
436 netinteger = htonl(tick_remain(now_ms, ts->expire));
437 memcpy(cursor, &netinteger, sizeof(netinteger));
438 cursor += sizeof(netinteger);
439 }
440
Emeric Brunb3971ab2015-05-12 18:49:09 +0200441 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200442 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200443 int stlen = strlen((char *)ts->key.key);
444
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 intencode(stlen, &cursor);
446 memcpy(cursor, ts->key.key, stlen);
447 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200448 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200449 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200450 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200451 memcpy(cursor, &netinteger, sizeof(netinteger));
452 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200453 }
454 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200455 memcpy(cursor, ts->key.key, st->table->key_size);
456 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200457 }
458
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100459 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200460 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200461 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462
Emeric Brun94900952015-06-11 18:25:54 +0200463 data_ptr = stktable_data_ptr(st->table, ts, data_type);
464 if (data_ptr) {
465 switch (stktable_data_types[data_type].std_type) {
466 case STD_T_SINT: {
467 int data;
468
469 data = stktable_data_cast(data_ptr, std_t_sint);
470 intencode(data, &cursor);
471 break;
472 }
473 case STD_T_UINT: {
474 unsigned int data;
475
476 data = stktable_data_cast(data_ptr, std_t_uint);
477 intencode(data, &cursor);
478 break;
479 }
480 case STD_T_ULL: {
481 unsigned long long data;
482
483 data = stktable_data_cast(data_ptr, std_t_ull);
484 intencode(data, &cursor);
485 break;
486 }
487 case STD_T_FRQP: {
488 struct freq_ctr_period *frqp;
489
490 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
491 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
492 intencode(frqp->curr_ctr, &cursor);
493 intencode(frqp->prev_ctr, &cursor);
494 break;
495 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200496 case STD_T_DICT: {
497 struct dict_entry *de;
498 struct dcache_tx_entry *cached_de;
499 struct dcache_tx_entry cde = {0};
500 char *beg, *end;
501 size_t value_len, data_len;
502 struct dcache *dc;
503
504 de = stktable_data_cast(data_ptr, std_t_dict);
505 if (!de)
506 break;
507
508 dc = peer->dcache;
509 cde.value.key = de;
510 cached_de = dcache_tx_insert(dc, &cde);
511 if (!cached_de)
512 break;
513
514 /* Leave enough room to encode the remaining data length. */
515 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
516 /* Encode the dictionary entry key */
517 intencode(cached_de->key.key + 1, &end);
518 if (cached_de != &cde) {
519 /* Encode the length of the dictionary entry data */
520 value_len = strlen(de->value.key);
521 intencode(value_len, &end);
522 /* Copy the data */
523 memcpy(end, de->value.key, value_len);
524 end += value_len;
525 }
526 /* Encode the length of the data */
527 data_len = end - beg;
528 intencode(data_len, &cursor);
529 memmove(cursor, beg, data_len);
530 cursor += data_len;
531 break;
532 }
Emeric Brun94900952015-06-11 18:25:54 +0200533 }
534 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200535 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100536 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200537
538 /* Compute datalen */
539 datalen = (cursor - datamsg);
540
541 /* prepare message header */
542 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200543 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200544 cursor = &msg[2];
545 intencode(datalen, &cursor);
546
547 /* move data after header */
548 memmove(cursor, datamsg, datalen);
549
550 /* return header size + data_len */
551 return (cursor - msg) + datalen;
552}
553
554/*
555 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800556 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200557 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
558 * check size)
559 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100560static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200561{
562 int len;
563 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200564 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200565 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200566 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200567 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100568 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200569
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100570 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200571 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572
573 /* Encode data */
574
575 /* encode local id */
576 intencode(st->local_id, &cursor);
577
578 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100579 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200580 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100581 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200582 cursor += len;
583
584 /* encode table type */
585
586 intencode(st->table->type, &cursor);
587
588 /* encode table key size */
589 intencode(st->table->key_size, &cursor);
590
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200591 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200593 /* encode available known data types in table */
594 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
595 if (st->table->data_ofs[data_type]) {
596 switch (stktable_data_types[data_type].std_type) {
597 case STD_T_SINT:
598 case STD_T_UINT:
599 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200600 case STD_T_DICT:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200601 data |= 1 << data_type;
602 break;
Emeric Brun94900952015-06-11 18:25:54 +0200603 case STD_T_FRQP:
604 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200605 intencode(data_type, &chunkq);
606 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200607 break;
608 }
609 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200610 }
611 intencode(data, &cursor);
612
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200613 /* Encode stick-table entries duration. */
614 intencode(st->table->expire, &cursor);
615
616 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 chunk->data = chunkq - chunkp;
618 memcpy(cursor, chunk->area, chunk->data);
619 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200620 }
621
Emeric Brunb3971ab2015-05-12 18:49:09 +0200622 /* Compute datalen */
623 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200624
Emeric Brunb3971ab2015-05-12 18:49:09 +0200625 /* prepare message header */
626 msg[0] = PEER_MSG_CLASS_STICKTABLE;
627 msg[1] = PEER_MSG_STKT_DEFINE;
628 cursor = &msg[2];
629 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200630
Emeric Brunb3971ab2015-05-12 18:49:09 +0200631 /* move data after header */
632 memmove(cursor, datamsg, datalen);
633
634 /* return header size + data_len */
635 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200636}
637
Emeric Brunb3971ab2015-05-12 18:49:09 +0200638/*
639 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
640 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800641 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200642 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
643 * check size)
644 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100645static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200646{
647 unsigned short datalen;
648 char *cursor, *datamsg;
649 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100650 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200651
Frédéric Lécaille39143342019-05-24 14:32:27 +0200652 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200653
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100654 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200655 intencode(st->remote_id, &cursor);
656 netinteger = htonl(st->last_get);
657 memcpy(cursor, &netinteger, sizeof(netinteger));
658 cursor += sizeof(netinteger);
659
660 /* Compute datalen */
661 datalen = (cursor - datamsg);
662
663 /* prepare message header */
664 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200665 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200666 cursor = &msg[2];
667 intencode(datalen, &cursor);
668
669 /* move data after header */
670 memmove(cursor, datamsg, datalen);
671
672 /* return header size + data_len */
673 return (cursor - msg) + datalen;
674}
Emeric Brun2b920a12010-09-23 18:30:22 +0200675
676/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200677 * Function to deinit connected peer
678 */
679void __peer_session_deinit(struct peer *peer)
680{
681 struct stream_interface *si;
682 struct stream *s;
683 struct peers *peers;
684
685 if (!peer->appctx)
686 return;
687
688 si = peer->appctx->owner;
689 if (!si)
690 return;
691
692 s = si_strm(si);
693 if (!s)
694 return;
695
696 peers = strm_fe(s)->parent;
697 if (!peers)
698 return;
699
700 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
701 HA_ATOMIC_SUB(&connected_peers, 1);
702
703 HA_ATOMIC_SUB(&active_peers, 1);
704
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200705 flush_dcache(peer);
706
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200707 /* Re-init current table pointers to force announcement on re-connect */
708 peer->remote_table = peer->last_local_table = NULL;
709 peer->appctx = NULL;
710 if (peer->flags & PEER_F_LEARN_ASSIGN) {
711 /* unassign current peer for learning */
712 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
713 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
714
715 /* reschedule a resync */
716 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
717 }
718 /* reset teaching and learning flags to 0 */
719 peer->flags &= PEER_TEACH_RESET;
720 peer->flags &= PEER_LEARN_RESET;
721 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
722}
723
724/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200725 * Callback to release a session with a peer
726 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200727static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200728{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200729 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200730
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100731 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100732 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200733 return;
734
735 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200736 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100737 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200738 if (peer->appctx == appctx)
739 __peer_session_deinit(peer);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100740 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200741 }
742}
743
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200744/* Retrieve the major and minor versions of peers protocol
745 * announced by a remote peer. <str> is a null-terminated
746 * string with the following format: "<maj_ver>.<min_ver>".
747 */
748static int peer_get_version(const char *str,
749 unsigned int *maj_ver, unsigned int *min_ver)
750{
751 unsigned int majv, minv;
752 const char *pos, *saved;
753 const char *end;
754
755 saved = pos = str;
756 end = str + strlen(str);
757
758 majv = read_uint(&pos, end);
759 if (saved == pos || *pos++ != '.')
760 return -1;
761
762 saved = pos;
763 minv = read_uint(&pos, end);
764 if (saved == pos || pos != end)
765 return -1;
766
767 *maj_ver = majv;
768 *min_ver = minv;
769
770 return 0;
771}
Emeric Brun2b920a12010-09-23 18:30:22 +0200772
773/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100774 * Parse a line terminated by an optional '\r' character, followed by a mandatory
775 * '\n' character.
776 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
777 * a line could not be read because the communication channel is closed.
778 */
779static inline int peer_getline(struct appctx *appctx)
780{
781 int n;
782 struct stream_interface *si = appctx->owner;
783
784 n = co_getline(si_oc(si), trash.area, trash.size);
785 if (!n)
786 return 0;
787
788 if (n < 0 || trash.area[n - 1] != '\n') {
789 appctx->st0 = PEER_SESS_ST_END;
790 return -1;
791 }
792
793 if (n > 1 && (trash.area[n - 2] == '\r'))
794 trash.area[n - 2] = 0;
795 else
796 trash.area[n - 1] = 0;
797
798 co_skip(si_oc(si), n);
799
800 return n;
801}
802
803/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100804 * Send a message after having called <peer_prepare_msg> to build it.
805 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
806 * Returns -1 if there was not enough room left to send the message,
807 * any other negative returned value must be considered as an error with an appcxt st0
808 * returned value equal to PEER_SESS_ST_END.
809 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100810static inline int peer_send_msg(struct appctx *appctx,
811 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
812 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100813{
814 int ret, msglen;
815 struct stream_interface *si = appctx->owner;
816
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100817 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100818 if (!msglen) {
819 /* internal error: message does not fit in trash */
820 appctx->st0 = PEER_SESS_ST_END;
821 return 0;
822 }
823
824 /* message to buffer */
825 ret = ci_putblk(si_ic(si), trash.area, msglen);
826 if (ret <= 0) {
827 if (ret == -1) {
828 /* No more write possible */
829 si_rx_room_blk(si);
830 return -1;
831 }
832 appctx->st0 = PEER_SESS_ST_END;
833 }
834
835 return ret;
836}
837
838/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100839 * Send a hello message.
840 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
841 * Returns -1 if there was not enough room left to send the message,
842 * any other negative returned value must be considered as an error with an appcxt st0
843 * returned value equal to PEER_SESS_ST_END.
844 */
845static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
846{
847 struct peer_prep_params p = {
848 .hello.peer = peer,
849 };
850
851 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
852}
853
854/*
855 * Send a success peer handshake status message.
856 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
857 * Returns -1 if there was not enough room left to send the message,
858 * any other negative returned value must be considered as an error with an appcxt st0
859 * returned value equal to PEER_SESS_ST_END.
860 */
861static inline int peer_send_status_successmsg(struct appctx *appctx)
862{
863 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
864}
865
866/*
867 * Send a peer handshake status error message.
868 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
869 * Returns -1 if there was not enough room left to send the message,
870 * any other negative returned value must be considered as an error with an appcxt st0
871 * returned value equal to PEER_SESS_ST_END.
872 */
873static inline int peer_send_status_errormsg(struct appctx *appctx)
874{
875 struct peer_prep_params p = {
876 .error_status.st1 = appctx->st1,
877 };
878
879 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
880}
881
882/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100883 * Send a stick-table switch message.
884 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
885 * Returns -1 if there was not enough room left to send the message,
886 * any other negative returned value must be considered as an error with an appcxt st0
887 * returned value equal to PEER_SESS_ST_END.
888 */
889static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
890{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100891 struct peer_prep_params p = {
892 .swtch.shared_table = st,
893 };
894
895 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100896}
897
898/*
899 * Send a stick-table update acknowledgement message.
900 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
901 * Returns -1 if there was not enough room left to send the message,
902 * any other negative returned value must be considered as an error with an appcxt st0
903 * returned value equal to PEER_SESS_ST_END.
904 */
905static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
906{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100907 struct peer_prep_params p = {
908 .ack.shared_table = st,
909 };
910
911 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100912}
913
914/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100915 * Send a stick-table update message.
916 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
917 * Returns -1 if there was not enough room left to send the message,
918 * any other negative returned value must be considered as an error with an appcxt st0
919 * returned value equal to PEER_SESS_ST_END.
920 */
921static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
922 unsigned int updateid, int use_identifier, int use_timed)
923{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100924 struct peer_prep_params p = {
925 .updt.stksess = ts,
926 .updt.shared_table = st,
927 .updt.updateid = updateid,
928 .updt.use_identifier = use_identifier,
929 .updt.use_timed = use_timed,
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200930 .updt.peer = appctx->ctx.peers.ptr,
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100931 };
932
933 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100934}
935
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100936/*
937 * Build a peer protocol control class message.
938 * Returns the number of written bytes used to build the message if succeeded,
939 * 0 if not.
940 */
941static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
942{
943 if (size < sizeof p->control.head)
944 return 0;
945
946 msg[0] = p->control.head[0];
947 msg[1] = p->control.head[1];
948
949 return 2;
950}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100951
952/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100953 * Send a stick-table synchronization request message.
954 * Return 0 if the message could not be built modifying the appcxt 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_resync_reqmsg(struct appctx *appctx)
960{
961 struct peer_prep_params p = {
962 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
963 };
964
965 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
966}
967
968/*
969 * Send a stick-table synchronization confirmation message.
970 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
971 * Returns -1 if there was not enough room left to send the message,
972 * any other negative returned value must be considered as an error with an appctx st0
973 * returned value equal to PEER_SESS_ST_END.
974 */
975static inline int peer_send_resync_confirmsg(struct appctx *appctx)
976{
977 struct peer_prep_params p = {
978 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
979 };
980
981 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
982}
983
984/*
985 * Send a stick-table synchronization finished 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_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
992{
993 struct peer_prep_params p = {
994 .control.head = { PEER_MSG_CLASS_CONTROL, },
995 };
996
997 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
998 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
999
1000 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1001}
1002
1003/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001004 * Send a heartbeat message.
1005 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1006 * Returns -1 if there was not enough room left to send the message,
1007 * any other negative returned value must be considered as an error with an appctx st0
1008 * returned value equal to PEER_SESS_ST_END.
1009 */
1010static inline int peer_send_heartbeatmsg(struct appctx *appctx)
1011{
1012 struct peer_prep_params p = {
1013 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1014 };
1015
1016 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1017}
1018
1019/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001020 * Build a peer protocol error class message.
1021 * Returns the number of written bytes used to build the message if succeeded,
1022 * 0 if not.
1023 */
1024static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1025{
1026 if (size < sizeof p->error.head)
1027 return 0;
1028
1029 msg[0] = p->error.head[0];
1030 msg[1] = p->error.head[1];
1031
1032 return 2;
1033}
1034
1035/*
1036 * Send a "size limit reached" error message.
1037 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1038 * Returns -1 if there was not enough room left to send the message,
1039 * any other negative returned value must be considered as an error with an appctx st0
1040 * returned value equal to PEER_SESS_ST_END.
1041 */
1042static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1043{
1044 struct peer_prep_params p = {
1045 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1046 };
1047
1048 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1049}
1050
1051/*
1052 * Send a "peer protocol" error message.
1053 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1054 * Returns -1 if there was not enough room left to send the message,
1055 * any other negative returned value must be considered as an error with an appctx st0
1056 * returned value equal to PEER_SESS_ST_END.
1057 */
1058static inline int peer_send_error_protomsg(struct appctx *appctx)
1059{
1060 struct peer_prep_params p = {
1061 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1062 };
1063
1064 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1065}
1066
1067/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001068 * Function used to lookup for recent stick-table updates associated with
1069 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1070 */
1071static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1072{
1073 struct eb32_node *eb;
1074
1075 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1076 if (!eb) {
1077 eb = eb32_first(&st->table->updates);
1078 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
1079 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1080 return NULL;
1081 }
1082 }
1083
1084 if ((int)(eb->key - st->table->localupdate) > 0) {
1085 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1086 return NULL;
1087 }
1088
1089 return eb32_entry(eb, struct stksess, upd);
1090}
1091
1092/*
1093 * Function used to lookup for recent stick-table updates associated with
1094 * <st> shared stick-table during teach state 1 step.
1095 */
1096static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1097{
1098 struct eb32_node *eb;
1099
1100 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1101 if (!eb) {
1102 st->flags |= SHTABLE_F_TEACH_STAGE1;
1103 eb = eb32_first(&st->table->updates);
1104 if (eb)
1105 st->last_pushed = eb->key - 1;
1106 return NULL;
1107 }
1108
1109 return eb32_entry(eb, struct stksess, upd);
1110}
1111
1112/*
1113 * Function used to lookup for recent stick-table updates associated with
1114 * <st> shared stick-table during teach state 2 step.
1115 */
1116static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1117{
1118 struct eb32_node *eb;
1119
1120 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1121 if (!eb || eb->key > st->teaching_origin) {
1122 st->flags |= SHTABLE_F_TEACH_STAGE2;
1123 return NULL;
1124 }
1125
1126 return eb32_entry(eb, struct stksess, upd);
1127}
1128
1129/*
1130 * Generic function to emit update messages for <st> stick-table when a lesson must
1131 * be taught to the peer <p>.
1132 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1133 * this function, 0 if not.
1134 *
1135 * This function temporary unlock/lock <st> when it sends stick-table updates or
1136 * when decrementing its refcount in case of any error when it sends this updates.
1137 *
1138 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1139 * Returns -1 if there was not enough room left to send the message,
1140 * any other negative returned value must be considered as an error with an appcxt st0
1141 * returned value equal to PEER_SESS_ST_END.
1142 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1143 * unlocked if not already locked when entering this function.
1144 */
1145static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1146 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1147 struct shared_table *st, int locked)
1148{
1149 int ret, new_pushed, use_timed;
1150
1151 ret = 1;
1152 use_timed = 0;
1153 if (st != p->last_local_table) {
1154 ret = peer_send_switchmsg(st, appctx);
1155 if (ret <= 0)
1156 return ret;
1157
1158 p->last_local_table = st;
1159 }
1160
1161 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1162 use_timed = !(p->flags & PEER_F_DWNGRD);
1163
1164 /* We force new pushed to 1 to force identifier in update message */
1165 new_pushed = 1;
1166
1167 if (!locked)
1168 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1169
1170 while (1) {
1171 struct stksess *ts;
1172 unsigned updateid;
1173
1174 /* push local updates */
1175 ts = peer_stksess_lookup(st);
1176 if (!ts)
1177 break;
1178
1179 updateid = ts->upd.key;
1180 ts->ref_cnt++;
1181 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1182
1183 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1184 if (ret <= 0) {
1185 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1186 ts->ref_cnt--;
1187 if (!locked)
1188 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1189 return ret;
1190 }
1191
1192 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1193 ts->ref_cnt--;
1194 st->last_pushed = updateid;
1195
1196 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1197 (int)(st->last_pushed - st->table->commitupdate) > 0)
1198 st->table->commitupdate = st->last_pushed;
1199
1200 /* identifier may not needed in next update message */
1201 new_pushed = 0;
1202 }
1203
1204 out:
1205 if (!locked)
1206 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1207 return 1;
1208}
1209
1210/*
1211 * Function to emit update messages for <st> stick-table when a lesson must
1212 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1213 *
1214 * Note that <st> shared stick-table is locked when calling this function.
1215 *
1216 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1217 * Returns -1 if there was not enough room left to send the message,
1218 * any other negative returned value must be considered as an error with an appcxt st0
1219 * returned value equal to PEER_SESS_ST_END.
1220 */
1221static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1222 struct shared_table *st)
1223{
1224 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1225}
1226
1227/*
1228 * Function to emit update messages for <st> stick-table when a lesson must
1229 * be taught to the peer <p> during teach state 1 step.
1230 *
1231 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1232 * Returns -1 if there was not enough room left to send the message,
1233 * any other negative returned value must be considered as an error with an appcxt st0
1234 * returned value equal to PEER_SESS_ST_END.
1235 */
1236static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1237 struct shared_table *st)
1238{
1239 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1240}
1241
1242/*
1243 * Function to emit update messages for <st> stick-table when a lesson must
1244 * be taught to the peer <p> during teach state 1 step.
1245 *
1246 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1247 * Returns -1 if there was not enough room left to send the message,
1248 * any other negative returned value must be considered as an error with an appcxt st0
1249 * returned value equal to PEER_SESS_ST_END.
1250 */
1251static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1252 struct shared_table *st)
1253{
1254 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1255}
1256
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001257
1258/*
1259 * Function used to parse a stick-table update message after it has been received
1260 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1261 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1262 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1263 * was encountered.
1264 * <exp> must be set if the stick-table entry expires.
1265 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1266 * messages, in this case the stick-table udpate message is received with a stick-table
1267 * update ID.
1268 * <totl> is the length of the stick-table update message computed upon receipt.
1269 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001270static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1271 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001272{
1273 struct stream_interface *si = appctx->owner;
1274 struct shared_table *st = p->remote_table;
1275 struct stksess *ts, *newts;
1276 uint32_t update;
1277 int expire;
1278 unsigned int data_type;
1279 void *data_ptr;
1280
1281 /* Here we have data message */
1282 if (!st)
1283 goto ignore_msg;
1284
1285 expire = MS_TO_TICKS(st->table->expire);
1286
1287 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001288 if (msg_len < sizeof(update))
1289 goto malformed_exit;
1290
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001291 memcpy(&update, *msg_cur, sizeof(update));
1292 *msg_cur += sizeof(update);
1293 st->last_get = htonl(update);
1294 }
1295 else {
1296 st->last_get++;
1297 }
1298
1299 if (exp) {
1300 size_t expire_sz = sizeof expire;
1301
Willy Tarreau1e82a142019-01-29 11:08:06 +01001302 if (*msg_cur + expire_sz > msg_end)
1303 goto malformed_exit;
1304
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001305 memcpy(&expire, *msg_cur, expire_sz);
1306 *msg_cur += expire_sz;
1307 expire = ntohl(expire);
1308 }
1309
1310 newts = stksess_new(st->table, NULL);
1311 if (!newts)
1312 goto ignore_msg;
1313
1314 if (st->table->type == SMP_T_STR) {
1315 unsigned int to_read, to_store;
1316
1317 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001318 if (!*msg_cur)
1319 goto malformed_free_newts;
1320
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001321 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001322 if (*msg_cur + to_store > msg_end)
1323 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001324
1325 memcpy(newts->key.key, *msg_cur, to_store);
1326 newts->key.key[to_store] = 0;
1327 *msg_cur += to_read;
1328 }
1329 else if (st->table->type == SMP_T_SINT) {
1330 unsigned int netinteger;
1331
Willy Tarreau1e82a142019-01-29 11:08:06 +01001332 if (*msg_cur + sizeof(netinteger) > msg_end)
1333 goto malformed_free_newts;
1334
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001335 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1336 netinteger = ntohl(netinteger);
1337 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1338 *msg_cur += sizeof(netinteger);
1339 }
1340 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001341 if (*msg_cur + st->table->key_size > msg_end)
1342 goto malformed_free_newts;
1343
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001344 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1345 *msg_cur += st->table->key_size;
1346 }
1347
1348 /* lookup for existing entry */
1349 ts = stktable_set_entry(st->table, newts);
1350 if (ts != newts) {
1351 stksess_free(st->table, newts);
1352 newts = NULL;
1353 }
1354
1355 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1356
1357 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001358 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001359
1360 if (!((1 << data_type) & st->remote_data))
1361 continue;
1362
Willy Tarreau1e82a142019-01-29 11:08:06 +01001363 decoded_int = intdecode(msg_cur, msg_end);
1364 if (!*msg_cur)
1365 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001366
Willy Tarreau1e82a142019-01-29 11:08:06 +01001367 switch (stktable_data_types[data_type].std_type) {
1368 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001369 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1370 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001371 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001372 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001373
Willy Tarreau1e82a142019-01-29 11:08:06 +01001374 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001375 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1376 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001377 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001378 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001379
Willy Tarreau1e82a142019-01-29 11:08:06 +01001380 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001381 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1382 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001383 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001384 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001385
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001386 case STD_T_FRQP: {
1387 struct freq_ctr_period data;
1388
1389 /* First bit is reserved for the freq_ctr_period lock
1390 Note: here we're still protected by the stksess lock
1391 so we don't need to update the update the freq_ctr_period
1392 using its internal lock */
1393
Willy Tarreau1e82a142019-01-29 11:08:06 +01001394 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001395 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001396 if (!*msg_cur)
1397 goto malformed_unlock;
1398
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001399 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001400 if (!*msg_cur)
1401 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001402
1403 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1404 if (data_ptr)
1405 stktable_data_cast(data_ptr, std_t_frqp) = data;
1406 break;
1407 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001408 case STD_T_DICT: {
1409 struct buffer *chunk;
1410 size_t data_len, value_len;
1411 unsigned int id;
1412 struct dict_entry *de;
1413 struct dcache *dc;
1414
1415 data_len = decoded_int;
1416 if (*msg_cur + data_len > msg_end)
1417 goto malformed_unlock;
1418
1419 id = intdecode(msg_cur, msg_end);
1420 if (!*msg_cur || !id)
1421 goto malformed_unlock;
1422
1423 dc = p->dcache;
1424 if (*msg_cur == msg_end) {
1425 /* Dictionary entry key without value. */
1426 if (id > dc->max_entries)
1427 break;
1428 /* IDs sent over the network are numbered from 1. */
1429 de = dc->rx[id - 1].de;
1430 }
1431 else {
1432 chunk = get_trash_chunk();
1433 value_len = intdecode(msg_cur, msg_end);
1434 if (!*msg_cur || *msg_cur + value_len > msg_end ||
1435 unlikely(value_len + 1 >= chunk->size))
1436 goto malformed_unlock;
1437
1438 chunk_memcpy(chunk, *msg_cur, value_len);
1439 chunk->area[chunk->data] = '\0';
1440 de = dict_insert(&server_name_dict, chunk->area);
1441 dc->rx[id - 1].de = de;
1442 }
1443 if (de) {
1444 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1445 if (data_ptr)
1446 stktable_data_cast(data_ptr, std_t_dict) = de;
1447 }
1448 break;
1449 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001450 }
1451 }
1452 /* Force new expiration */
1453 ts->expire = tick_add(now_ms, expire);
1454
1455 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1456 stktable_touch_remote(st->table, ts, 1);
1457 return 1;
1458
1459 ignore_msg:
1460 /* skip consumed message */
1461 co_skip(si_oc(si), totl);
1462 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001463
1464 malformed_unlock:
1465 /* malformed message */
1466 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1467 stktable_touch_remote(st->table, ts, 1);
1468 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1469 return 0;
1470
1471 malformed_free_newts:
1472 /* malformed message */
1473 stksess_free(st->table, newts);
1474 malformed_exit:
1475 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1476 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001477}
1478
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001479/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001480 * Function used to parse a stick-table update acknowledgement message after it
1481 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1482 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1483 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1484 * was encountered.
1485 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1486 */
1487static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1488 char **msg_cur, char *msg_end)
1489{
1490 /* ack message */
1491 uint32_t table_id ;
1492 uint32_t update;
1493 struct shared_table *st;
1494
1495 table_id = intdecode(msg_cur, msg_end);
1496 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1497 /* malformed message */
1498 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1499 return 0;
1500 }
1501
1502 memcpy(&update, *msg_cur, sizeof(update));
1503 update = ntohl(update);
1504
1505 for (st = p->tables; st; st = st->next) {
1506 if (st->local_id == table_id) {
1507 st->update = update;
1508 break;
1509 }
1510 }
1511
1512 return 1;
1513}
1514
1515/*
1516 * Function used to parse a stick-table switch message after it has been received
1517 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1518 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1519 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1520 * was encountered.
1521 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1522 */
1523static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1524 char **msg_cur, char *msg_end)
1525{
1526 struct shared_table *st;
1527 int table_id;
1528
1529 table_id = intdecode(msg_cur, msg_end);
1530 if (!*msg_cur) {
1531 /* malformed message */
1532 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1533 return 0;
1534 }
1535
1536 p->remote_table = NULL;
1537 for (st = p->tables; st; st = st->next) {
1538 if (st->remote_id == table_id) {
1539 p->remote_table = st;
1540 break;
1541 }
1542 }
1543
1544 return 1;
1545}
1546
1547/*
1548 * Function used to parse a stick-table definition message after it has been received
1549 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1550 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1551 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1552 * was encountered.
1553 * <totl> is the length of the stick-table update message computed upon receipt.
1554 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1555 */
1556static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1557 char **msg_cur, char *msg_end, int totl)
1558{
1559 struct stream_interface *si = appctx->owner;
1560 int table_id_len;
1561 struct shared_table *st;
1562 int table_type;
1563 int table_keylen;
1564 int table_id;
1565 uint64_t table_data;
1566
1567 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001568 if (!*msg_cur)
1569 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001570
1571 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001572 if (!*msg_cur)
1573 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001574
1575 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001576 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1577 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001578
1579 for (st = p->tables; st; st = st->next) {
1580 /* Reset IDs */
1581 if (st->remote_id == table_id)
1582 st->remote_id = 0;
1583
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001584 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1585 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001586 p->remote_table = st;
1587 }
1588
1589 if (!p->remote_table)
1590 goto ignore_msg;
1591
1592 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001593 if (*msg_cur >= msg_end)
1594 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001595
1596 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001597 if (!*msg_cur)
1598 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001599
1600 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001601 if (!*msg_cur)
1602 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001603
1604 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001605 if (!*msg_cur)
1606 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001607
1608 if (p->remote_table->table->type != table_type
1609 || p->remote_table->table->key_size != table_keylen) {
1610 p->remote_table = NULL;
1611 goto ignore_msg;
1612 }
1613
1614 p->remote_table->remote_data = table_data;
1615 p->remote_table->remote_id = table_id;
1616 return 1;
1617
1618 ignore_msg:
1619 co_skip(si_oc(si), totl);
1620 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001621
1622 malformed_exit:
1623 /* malformed message */
1624 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1625 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001626}
1627
1628/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001629 * Receive a stick-table message.
1630 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1631 * -1 if there was an error updating the appctx state st0 accordingly.
1632 */
1633static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1634 uint32_t *msg_len, int *totl)
1635{
1636 int reql;
1637 struct stream_interface *si = appctx->owner;
1638
1639 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1640 if (reql <= 0) /* closed or EOL not found */
1641 goto incomplete;
1642
1643 *totl += reql;
1644
1645 if ((unsigned int)msg_head[1] < 128)
1646 return 1;
1647
1648 /* Read and Decode message length */
1649 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1650 if (reql <= 0) /* closed */
1651 goto incomplete;
1652
1653 *totl += reql;
1654
1655 if ((unsigned int)msg_head[2] < 240) {
1656 *msg_len = msg_head[2];
1657 }
1658 else {
1659 int i;
1660 char *cur;
1661 char *end;
1662
1663 for (i = 3 ; i < msg_head_sz ; i++) {
1664 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1665 if (reql <= 0) /* closed */
1666 goto incomplete;
1667
1668 *totl += reql;
1669
1670 if (!(msg_head[i] & 0x80))
1671 break;
1672 }
1673
1674 if (i == msg_head_sz) {
1675 /* malformed message */
1676 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1677 return -1;
1678 }
1679 end = msg_head + msg_head_sz;
1680 cur = &msg_head[2];
1681 *msg_len = intdecode(&cur, end);
1682 if (!cur) {
1683 /* malformed message */
1684 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1685 return -1;
1686 }
1687 }
1688
1689 /* Read message content */
1690 if (*msg_len) {
1691 if (*msg_len > trash.size) {
1692 /* Status code is not success, abort */
1693 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1694 return -1;
1695 }
1696
1697 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1698 if (reql <= 0) /* closed */
1699 goto incomplete;
1700 *totl += reql;
1701 }
1702
1703 return 1;
1704
1705 incomplete:
1706 if (reql < 0) {
1707 /* there was an error */
1708 appctx->st0 = PEER_SESS_ST_END;
1709 return -1;
1710 }
1711
1712 return 0;
1713}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001714
1715/*
1716 * Treat the awaited message with <msg_head> as header.*
1717 * Return 1 if succeeded, 0 if not.
1718 */
1719static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1720 char **msg_cur, char *msg_end, int msg_len, int totl)
1721{
1722 struct stream_interface *si = appctx->owner;
1723 struct stream *s = si_strm(si);
1724 struct peers *peers = strm_fe(s)->parent;
1725
1726 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1727 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1728 struct shared_table *st;
1729 /* Reset message: remote need resync */
1730
1731 /* prepare tables fot a global push */
1732 for (st = peer->tables; st; st = st->next) {
1733 st->teaching_origin = st->last_pushed = st->table->update;
1734 st->flags = 0;
1735 }
1736
1737 /* reset teaching flags to 0 */
1738 peer->flags &= PEER_TEACH_RESET;
1739
1740 /* flag to start to teach lesson */
1741 peer->flags |= PEER_F_TEACH_PROCESS;
1742 }
1743 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1744 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1745 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1746 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1747 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1748 }
1749 peer->confirm++;
1750 }
1751 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1752 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1753 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1754 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1755
1756 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001757 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001758 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1759 }
1760 peer->confirm++;
1761 }
1762 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1763 struct shared_table *st;
1764
1765 /* If stopping state */
1766 if (stopping) {
1767 /* Close session, push resync no more needed */
1768 peer->flags |= PEER_F_TEACH_COMPLETE;
1769 appctx->st0 = PEER_SESS_ST_END;
1770 return 0;
1771 }
1772 for (st = peer->tables; st; st = st->next) {
1773 st->update = st->last_pushed = st->teaching_origin;
1774 st->flags = 0;
1775 }
1776
1777 /* reset teaching flags to 0 */
1778 peer->flags &= PEER_TEACH_RESET;
1779 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001780 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001781 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001782 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001783 }
1784 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1785 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1786 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1787 return 0;
1788 }
1789 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1790 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1791 return 0;
1792 }
1793 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1794 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1795 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1796 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1797 int update, expire;
1798
1799 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1800 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1801 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1802 msg_cur, msg_end, msg_len, totl))
1803 return 0;
1804
1805 }
1806 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1807 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1808 return 0;
1809 }
1810 }
1811 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1812 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1813 return 0;
1814 }
1815
1816 return 1;
1817}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001818
1819
1820/*
1821 * Send any message to <peer> peer.
1822 * Returns 1 if succeeded, or -1 or 0 if failed.
1823 * -1 means an internal error occured, 0 is for a peer protocol error leading
1824 * to a peer state change (from the peer I/O handler point of view).
1825 */
1826static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1827{
1828 int repl;
1829 struct stream_interface *si = appctx->owner;
1830 struct stream *s = si_strm(si);
1831 struct peers *peers = strm_fe(s)->parent;
1832
1833 /* Need to request a resync */
1834 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1835 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1836 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1837
1838 repl = peer_send_resync_reqmsg(appctx);
1839 if (repl <= 0)
1840 return repl;
1841
1842 peers->flags |= PEERS_F_RESYNC_PROCESS;
1843 }
1844
1845 /* Nothing to read, now we start to write */
1846 if (peer->tables) {
1847 struct shared_table *st;
1848 struct shared_table *last_local_table;
1849
1850 last_local_table = peer->last_local_table;
1851 if (!last_local_table)
1852 last_local_table = peer->tables;
1853 st = last_local_table->next;
1854
1855 while (1) {
1856 if (!st)
1857 st = peer->tables;
1858
1859 /* It remains some updates to ack */
1860 if (st->last_get != st->last_acked) {
1861 repl = peer_send_ackmsg(st, appctx);
1862 if (repl <= 0)
1863 return repl;
1864
1865 st->last_acked = st->last_get;
1866 }
1867
1868 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1869 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1870 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1871 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1872
1873 repl = peer_send_teach_process_msgs(appctx, peer, st);
1874 if (repl <= 0) {
1875 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1876 return repl;
1877 }
1878 }
1879 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1880 }
1881 else {
1882 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1883 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1884 if (repl <= 0)
1885 return repl;
1886 }
1887
1888 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1889 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1890 if (repl <= 0)
1891 return repl;
1892 }
1893 }
1894
1895 if (st == last_local_table)
1896 break;
1897 st = st->next;
1898 }
1899 }
1900
1901 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1902 repl = peer_send_resync_finishedmsg(appctx, peer);
1903 if (repl <= 0)
1904 return repl;
1905
1906 /* flag finished message sent */
1907 peer->flags |= PEER_F_TEACH_FINISHED;
1908 }
1909
1910 /* Confirm finished or partial messages */
1911 while (peer->confirm) {
1912 repl = peer_send_resync_confirmsg(appctx);
1913 if (repl <= 0)
1914 return repl;
1915
1916 peer->confirm--;
1917 }
1918
1919 return 1;
1920}
1921
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001922/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001923 * Read and parse a first line of a "hello" peer protocol message.
1924 * Returns 0 if could not read a line, -1 if there was a read error or
1925 * the line is malformed, 1 if succeeded.
1926 */
1927static inline int peer_getline_version(struct appctx *appctx,
1928 unsigned int *maj_ver, unsigned int *min_ver)
1929{
1930 int reql;
1931
1932 reql = peer_getline(appctx);
1933 if (!reql)
1934 return 0;
1935
1936 if (reql < 0)
1937 return -1;
1938
1939 /* test protocol */
1940 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1941 appctx->st0 = PEER_SESS_ST_EXIT;
1942 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1943 return -1;
1944 }
1945 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1946 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1947 appctx->st0 = PEER_SESS_ST_EXIT;
1948 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1949 return -1;
1950 }
1951
1952 return 1;
1953}
1954
1955/*
1956 * Read and parse a second line of a "hello" peer protocol message.
1957 * Returns 0 if could not read a line, -1 if there was a read error or
1958 * the line is malformed, 1 if succeeded.
1959 */
1960static inline int peer_getline_host(struct appctx *appctx)
1961{
1962 int reql;
1963
1964 reql = peer_getline(appctx);
1965 if (!reql)
1966 return 0;
1967
1968 if (reql < 0)
1969 return -1;
1970
1971 /* test hostname match */
1972 if (strcmp(localpeer, trash.area) != 0) {
1973 appctx->st0 = PEER_SESS_ST_EXIT;
1974 appctx->st1 = PEER_SESS_SC_ERRHOST;
1975 return -1;
1976 }
1977
1978 return 1;
1979}
1980
1981/*
1982 * Read and parse a last line of a "hello" peer protocol message.
1983 * Returns 0 if could not read a character, -1 if there was a read error or
1984 * the line is malformed, 1 if succeeded.
1985 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1986 */
1987static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1988{
1989 char *p;
1990 int reql;
1991 struct peer *peer;
1992 struct stream_interface *si = appctx->owner;
1993 struct stream *s = si_strm(si);
1994 struct peers *peers = strm_fe(s)->parent;
1995
1996 reql = peer_getline(appctx);
1997 if (!reql)
1998 return 0;
1999
2000 if (reql < 0)
2001 return -1;
2002
2003 /* parse line "<peer name> <pid> <relative_pid>" */
2004 p = strchr(trash.area, ' ');
2005 if (!p) {
2006 appctx->st0 = PEER_SESS_ST_EXIT;
2007 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2008 return -1;
2009 }
2010 *p = 0;
2011
2012 /* lookup known peer */
2013 for (peer = peers->remote; peer; peer = peer->next) {
2014 if (strcmp(peer->id, trash.area) == 0)
2015 break;
2016 }
2017
2018 /* if unknown peer */
2019 if (!peer) {
2020 appctx->st0 = PEER_SESS_ST_EXIT;
2021 appctx->st1 = PEER_SESS_SC_ERRPEER;
2022 return -1;
2023 }
2024 *curpeer = peer;
2025
2026 return 1;
2027}
2028
2029/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002030 * Init <peer> peer after having accepted it at peer protocol level.
2031 */
2032static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2033{
2034 struct shared_table *st;
2035
2036 /* Register status code */
2037 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
2038
2039 /* Awake main task */
2040 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2041
2042 /* Init confirm counter */
2043 peer->confirm = 0;
2044
2045 /* Init cursors */
2046 for (st = peer->tables; st ; st = st->next) {
2047 st->last_get = st->last_acked = 0;
2048 st->teaching_origin = st->last_pushed = st->update;
2049 }
2050
2051 /* reset teaching and learning flags to 0 */
2052 peer->flags &= PEER_TEACH_RESET;
2053 peer->flags &= PEER_LEARN_RESET;
2054
2055 /* if current peer is local */
2056 if (peer->local) {
2057 /* if current host need resyncfrom local and no process assined */
2058 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2059 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2060 /* assign local peer for a lesson, consider lesson already requested */
2061 peer->flags |= PEER_F_LEARN_ASSIGN;
2062 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2063 }
2064
2065 }
2066 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2067 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2068 /* assign peer for a lesson */
2069 peer->flags |= PEER_F_LEARN_ASSIGN;
2070 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2071 }
2072}
2073
2074/*
2075 * Init <peer> peer after having connected it at peer protocol level.
2076 */
2077static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2078{
2079 struct shared_table *st;
2080
2081 /* Init cursors */
2082 for (st = peer->tables; st ; st = st->next) {
2083 st->last_get = st->last_acked = 0;
2084 st->teaching_origin = st->last_pushed = st->update;
2085 }
2086
2087 /* Init confirm counter */
2088 peer->confirm = 0;
2089
2090 /* reset teaching and learning flags to 0 */
2091 peer->flags &= PEER_TEACH_RESET;
2092 peer->flags &= PEER_LEARN_RESET;
2093
2094 /* If current peer is local */
2095 if (peer->local) {
2096 /* flag to start to teach lesson */
2097 peer->flags |= PEER_F_TEACH_PROCESS;
2098 }
2099 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2100 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2101 /* If peer is remote and resync from remote is needed,
2102 and no peer currently assigned */
2103
2104 /* assign peer for a lesson */
2105 peer->flags |= PEER_F_LEARN_ASSIGN;
2106 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2107 }
2108}
2109
2110/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002111 * IO Handler to handle message exchance with a peer
2112 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002113static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002114{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002115 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002116 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002117 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002118 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002119 int reql = 0;
2120 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002121 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002122 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002123
Joseph Herlant82b2f542018-11-15 12:19:14 -08002124 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002125 if (si_ic(si)->buf.size == 0) {
2126 si_rx_room_blk(si);
2127 goto out;
2128 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002129
Emeric Brun2b920a12010-09-23 18:30:22 +02002130 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002131 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002132switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002133 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002134 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002135 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002136 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002137 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002138 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002139 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002140 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002141 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002142 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2143 if (reql <= 0) {
2144 if (!reql)
2145 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002146 goto switchstate;
2147 }
2148
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002149 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002150 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002151 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002152 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002153 reql = peer_getline_host(appctx);
2154 if (reql <= 0) {
2155 if (!reql)
2156 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002157 goto switchstate;
2158 }
2159
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002160 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002161 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002162 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002163 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002164 reql = peer_getline_last(appctx, &curpeer);
2165 if (reql <= 0) {
2166 if (!reql)
2167 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002168 goto switchstate;
2169 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002170
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002171 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002172 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002173 if (curpeer->local) {
2174 /* Local connection, reply a retry */
2175 appctx->st0 = PEER_SESS_ST_EXIT;
2176 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2177 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002178 }
Emeric Brun80527f52017-06-19 17:46:37 +02002179
2180 /* we're killing a connection, we must apply a random delay before
2181 * retrying otherwise the other end will do the same and we can loop
2182 * for a while.
2183 */
2184 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002185 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002186 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002187 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2188 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2189 curpeer->flags |= PEER_F_DWNGRD;
2190 }
2191 else {
2192 curpeer->flags &= ~PEER_F_DWNGRD;
2193 }
2194 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002195 curpeer->appctx = appctx;
2196 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002197 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002198 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002199 /* fall through */
2200 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002201 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002202 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002203 if (!curpeer) {
2204 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002205 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002206 if (curpeer->appctx != appctx) {
2207 appctx->st0 = PEER_SESS_ST_END;
2208 goto switchstate;
2209 }
2210 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002211
2212 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002213 if (repl <= 0) {
2214 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002215 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002216 goto switchstate;
2217 }
2218
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002219 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002220
Emeric Brun2b920a12010-09-23 18:30:22 +02002221 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002222 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002223 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002224 goto switchstate;
2225 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002226 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002227 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002228 if (!curpeer) {
2229 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002230 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002231 if (curpeer->appctx != appctx) {
2232 appctx->st0 = PEER_SESS_ST_END;
2233 goto switchstate;
2234 }
2235 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002236
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002237 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002238 if (repl <= 0) {
2239 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002240 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002241 goto switchstate;
2242 }
2243
2244 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002245 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002246 /* fall through */
2247 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002248 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002249 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002250 if (!curpeer) {
2251 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002252 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002253 if (curpeer->appctx != appctx) {
2254 appctx->st0 = PEER_SESS_ST_END;
2255 goto switchstate;
2256 }
2257 }
2258
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002259 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002260 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002261
Frédéric Lécaillece025572019-01-21 13:38:06 +01002262 reql = peer_getline(appctx);
2263 if (!reql)
2264 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002265
Frédéric Lécaillece025572019-01-21 13:38:06 +01002266 if (reql < 0)
2267 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002268
2269 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002270 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002271
2272 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002273 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002274
2275 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002276 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002277 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002278 }
2279 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002280 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2281 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002282 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002283 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002284 goto switchstate;
2285 }
Olivier Houcharded879892019-03-08 18:53:43 +01002286 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002287 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002288 /* fall through */
2289 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002290 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002291 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 char *msg_cur = trash.area;
2293 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002294 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002295 int totl = 0;
2296
Willy Tarreau2d372c22018-11-05 17:12:27 +01002297 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002298 if (!curpeer) {
2299 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002300 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002301 if (curpeer->appctx != appctx) {
2302 appctx->st0 = PEER_SESS_ST_END;
2303 goto switchstate;
2304 }
2305 }
2306
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002307 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2308 if (reql <= 0) {
2309 if (reql == -1)
2310 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002311 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002312 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002313
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002314 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002315 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002316 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002317
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002318 curpeer->flags |= PEER_F_ALIVE;
2319
Emeric Brun2b920a12010-09-23 18:30:22 +02002320 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002321 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002322 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002323 goto switchstate;
2324
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002325send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002326 if (curpeer->flags & PEER_F_HEARTBEAT) {
2327 curpeer->flags &= ~PEER_F_HEARTBEAT;
2328 repl = peer_send_heartbeatmsg(appctx);
2329 if (repl <= 0) {
2330 if (repl == -1)
2331 goto out;
2332 goto switchstate;
2333 }
2334 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002335 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002336 repl = peer_send_msgs(appctx, curpeer);
2337 if (repl <= 0) {
2338 if (repl == -1)
2339 goto out;
2340 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002341 }
2342
Emeric Brun2b920a12010-09-23 18:30:22 +02002343 /* noting more to do */
2344 goto out;
2345 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002346 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002347 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002348 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002349 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002350 if (peer_send_status_errormsg(appctx) == -1)
2351 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002352 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002353 goto switchstate;
2354 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002355 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002356 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002357 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002358 if (peer_send_error_size_limitmsg(appctx) == -1)
2359 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002360 appctx->st0 = PEER_SESS_ST_END;
2361 goto switchstate;
2362 }
2363 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002364 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002365 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002366 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002367 if (peer_send_error_protomsg(appctx) == -1)
2368 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002369 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002370 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002371 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002372 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002373 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002374 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002375 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002376 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002377 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002378 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002379 curpeer = NULL;
2380 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002381 si_shutw(si);
2382 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002383 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002384 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002385 }
2386 }
2387 }
2388out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002389 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002390
2391 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002392 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002393 return;
2394}
2395
Willy Tarreau30576452015-04-13 13:50:30 +02002396static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002397 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002398 .name = "<PEER>", /* used for logging */
2399 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002400 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002401};
Emeric Brun2b920a12010-09-23 18:30:22 +02002402
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002403
Emeric Brun2b920a12010-09-23 18:30:22 +02002404/*
2405 * Use this function to force a close of a peer session
2406 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002407static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002408{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002409 struct appctx *appctx = peer->appctx;
2410
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002411 /* Note that the peer sessions which have just been created
2412 * (->st0 == PEER_SESS_ST_CONNECT) must not
2413 * be shutdown, if not, the TCP session will never be closed
2414 * and stay in CLOSE_WAIT state after having been closed by
2415 * the remote side.
2416 */
2417 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002418 return;
2419
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002420 if (appctx->applet != &peer_applet)
2421 return;
2422
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002423 __peer_session_deinit(peer);
2424
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002425 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002426 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002427}
2428
Willy Tarreau91d96282015-03-13 15:47:26 +01002429/* Pre-configures a peers frontend to accept incoming connections */
2430void peers_setup_frontend(struct proxy *fe)
2431{
2432 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002433 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002434 fe->maxconn = 0;
2435 fe->conn_retries = CONN_RETRIES;
2436 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002437 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002438 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002439 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002440 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002441}
2442
Emeric Brun2b920a12010-09-23 18:30:22 +02002443/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002444 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002445 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002446static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002447{
Willy Tarreau04b92862017-09-15 11:01:04 +02002448 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002449 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002450 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002451 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002452 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002453 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002454
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002455 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002456 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002457 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002458 s = NULL;
2459
Emeric Brun1138fd02017-06-19 12:38:55 +02002460 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002461 if (!appctx)
2462 goto out_close;
2463
2464 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002465 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002466
Willy Tarreau04b92862017-09-15 11:01:04 +02002467 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002468 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002469 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002470 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002471 }
2472
Willy Tarreau87787ac2017-08-28 16:22:54 +02002473 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002474 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002475 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002476 }
2477
Willy Tarreau342bfb12015-04-05 01:35:34 +02002478 /* The tasks below are normally what is supposed to be done by
2479 * fe->accept().
2480 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002481 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002482
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002483 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002484 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002485 appctx_wakeup(appctx);
2486
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002487 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002488 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002489 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002490
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002491 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002492 * pre-initialized connection in si->conn.
2493 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002494 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002495 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002496
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002497 if (unlikely((cs = cs_new(conn)) == NULL))
2498 goto out_free_conn;
2499
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002500 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002501 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2502
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002503 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002504 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2505 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002506 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002507
Emeric Brun2b920a12010-09-23 18:30:22 +02002508 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002509 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002510
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002511 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002512
Emeric Brunb3971ab2015-05-12 18:49:09 +02002513 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002514 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002515 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002516 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002517
2518 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002519out_free_cs:
2520 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002521 out_free_conn:
2522 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002523 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002524 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002525 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002526 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002527 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002528 out_free_appctx:
2529 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002530 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002531 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002532}
2533
2534/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002535 * Task processing function to manage re-connect, peer session
2536 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002537 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002538static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002539{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002540 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002541 struct peer *ps;
2542 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002543
2544 task->expire = TICK_ETERNITY;
2545
Emeric Brunb3971ab2015-05-12 18:49:09 +02002546 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002547 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002548 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002549 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002550 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002551 return NULL;
2552 }
2553
Emeric Brun80527f52017-06-19 17:46:37 +02002554 /* Acquire lock for all peers of the section */
2555 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002556 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002557
Emeric Brun2b920a12010-09-23 18:30:22 +02002558 if (!stopping) {
2559 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002560
2561 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2562 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2563 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002564 /* Resync from local peer needed
2565 no peer was assigned for the lesson
2566 and no old local peer found
2567 or resync timeout expire */
2568
2569 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002570 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002571
2572 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002573 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002574 }
2575
2576 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002577 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002578 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002579 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002580 if (!ps->appctx) {
2581 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002582 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002583 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002584 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002585 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002586 tick_is_expired(ps->reconnect, now_ms))) {
2587 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002588 * or previous peer connection established with success
2589 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002590 * and reconnection timer is expired */
2591
2592 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002593 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002594 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002595 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002596 /* If previous session failed during connection
2597 * but reconnection timer is not expired */
2598
2599 /* reschedule task for reconnect */
2600 task->expire = tick_first(task->expire, ps->reconnect);
2601 }
2602 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002603 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002604 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002605 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002606 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2607 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002608 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2609 /* Resync from a remote is needed
2610 * and no peer was assigned for lesson
2611 * and current peer may be up2date */
2612
2613 /* assign peer for the lesson */
2614 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002615 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002616
Willy Tarreau9df94c22016-10-31 18:42:52 +01002617 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002618 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002619 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002620 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002621 int update_to_push = 0;
2622
Emeric Brunb3971ab2015-05-12 18:49:09 +02002623 /* Awake session if there is data to push */
2624 for (st = ps->tables; st ; st = st->next) {
2625 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002626 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002627 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002628 /* There is no need to send a heartbeat message
2629 * when some updates must be pushed. The remote
2630 * peer will consider <ps> peer as alive when it will
2631 * receive these updates.
2632 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002633 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002634 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002635 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002636 /* We are going to send updates, let's ensure we will
2637 * come back to send heartbeat messages or to reconnect.
2638 */
2639 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002640 appctx_wakeup(ps->appctx);
2641 break;
2642 }
2643 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002644 /* When there are updates to send we do not reconnect
2645 * and do not send heartbeat message either.
2646 */
2647 if (!update_to_push) {
2648 if (tick_is_expired(ps->reconnect, now_ms)) {
2649 if (ps->flags & PEER_F_ALIVE) {
2650 /* This peer was alive during a 'reconnect' period.
2651 * Flag it as not alive again for the next period.
2652 */
2653 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002654 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002655 }
2656 else {
2657 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002658 peer_session_forceshutdown(ps);
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002659 }
2660 }
2661 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2662 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2663 ps->flags |= PEER_F_HEARTBEAT;
2664 appctx_wakeup(ps->appctx);
2665 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002666 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002667 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002668 }
2669 /* else do nothing */
2670 } /* SUCCESSCODE */
2671 } /* !ps->peer->local */
2672 } /* for */
2673
2674 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002675 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2676 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2677 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002678 /* Resync from remote peer needed
2679 * no peer was assigned for the lesson
2680 * and resync timeout expire */
2681
2682 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002683 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002684 }
2685
Emeric Brunb3971ab2015-05-12 18:49:09 +02002686 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002687 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002688 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2689 if (!tick_is_expired(peers->resync_timeout, now_ms))
2690 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002691 }
2692 } /* !stopping */
2693 else {
2694 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002695 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002696 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002697 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002698 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002699 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002700 peers->flags |= PEERS_F_DONOTSTOP;
2701 ps = peers->local;
2702 for (st = ps->tables; st ; st = st->next)
2703 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002704 }
2705
2706 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002707 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002708 /* we're killing a connection, we must apply a random delay before
2709 * retrying otherwise the other end will do the same and we can loop
2710 * for a while.
2711 */
2712 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002713 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002714 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002715 }
2716 }
2717 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002718
Emeric Brunb3971ab2015-05-12 18:49:09 +02002719 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002720 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002721 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002722 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002723 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002724 peers->flags &= ~PEERS_F_DONOTSTOP;
2725 for (st = ps->tables; st ; st = st->next)
2726 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002727 }
2728 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002729 else if (!ps->appctx) {
2730 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002731 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002732 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2733 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2734 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002735 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002736 * or previous peer connection was successfully established
2737 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002738 * or during previous connect, peer replies a try again statuscode */
2739
2740 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002741 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002742 }
2743 else {
2744 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002745 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002746 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002747 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002748 peers->flags &= ~PEERS_F_DONOTSTOP;
2749 for (st = ps->tables; st ; st = st->next)
2750 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002751 }
2752 }
2753 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002754 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002755 /* current peer connection is active and established
2756 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002757 for (st = ps->tables; st ; st = st->next) {
2758 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002759 appctx_wakeup(ps->appctx);
2760 break;
2761 }
2762 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002763 }
2764 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002765
2766 /* Release lock for all peers of the section */
2767 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002768 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002769
Emeric Brun2b920a12010-09-23 18:30:22 +02002770 /* Wakeup for re-connect */
2771 return task;
2772}
2773
Emeric Brunb3971ab2015-05-12 18:49:09 +02002774
Emeric Brun2b920a12010-09-23 18:30:22 +02002775/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002776 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002777 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002778int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002779{
Emeric Brun2b920a12010-09-23 18:30:22 +02002780 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002781
Emeric Brun2b920a12010-09-23 18:30:22 +02002782 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002783 peers->peers_fe->maxconn += 3;
2784 }
2785
Emeric Brunc60def82017-09-27 14:59:38 +02002786 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002787 if (!peers->sync_task)
2788 return 0;
2789
Emeric Brunb3971ab2015-05-12 18:49:09 +02002790 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002791 peers->sync_task->context = (void *)peers;
2792 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2793 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002794 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002795}
2796
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002797/*
2798 * Allocate a cache a dictionary entries used upon transmission.
2799 */
2800struct dcache_tx *new_dcache_tx(size_t max_entries)
2801{
2802 struct dcache_tx *d;
2803
2804 d = malloc(sizeof *d);
2805 if (!d)
2806 return NULL;
2807
2808 d->lru_key = 0;
2809 d->keys = EB_ROOT_UNIQUE;
2810 d->values = EB_ROOT_UNIQUE;
2811 d->max_entries = max_entries;
2812
2813 return d;
2814}
2815
2816/*
2817 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
2818 * as maximum of entries.
2819 * Return the dictionay cache if succeeded, NULL if not.
2820 * Must be deallocated calling free_dcache().
2821 */
2822struct dcache *new_dcache(size_t max_entries)
2823{
2824 struct dcache_tx *dc_tx;
2825 struct dcache *dc;
2826 struct dcache_rx *dc_rx;
2827
2828 dc = calloc(1, sizeof *dc);
2829 dc_tx = new_dcache_tx(max_entries);
2830 dc_rx = calloc(max_entries, sizeof *dc_rx);
2831 if (!dc || !dc_tx || !dc_rx)
2832 goto err;
2833
2834 dc->tx = dc_tx;
2835 dc->rx = dc_rx;
2836 dc->max_entries = max_entries;
2837
2838 return dc;
2839
2840 err:
2841 free(dc);
2842 free(dc_tx);
2843 free(dc_rx);
2844 return NULL;
2845}
2846
2847/*
2848 * Deallocate a cache of dictionary entries.
2849 */
2850void free_dcache(struct dcache *dc)
2851{
2852 free(dc->tx); dc->tx = NULL;
2853 free(dc->rx); dc->rx = NULL;
2854 free(dc);
2855}
2856
2857/*
2858 * Look for the dictionary entry with <k> as key in <d> cache of dictionary entries
2859 * used upon transmission.
2860 * Return the entry if found, NULL if not.
2861 */
2862struct dcache_tx_entry *dcache_tx_lookup_key(struct dcache_tx *d, unsigned int k)
2863{
2864 struct dcache_tx_entry *de;
2865 struct eb32_node *node;
2866
2867 de = NULL;
2868 node = eb32_lookup(&d->keys, k);
2869 if (node)
2870 de = container_of(node, struct dcache_tx_entry, key);
2871
2872 return de;
2873}
2874
2875/*
2876 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
2877 * entries used upon transmission.
2878 * Return the entry if found, NULL if not.
2879 */
2880struct dcache_tx_entry *dcache_tx_lookup_value(struct dcache_tx *d,
2881 struct dcache_tx_entry *i)
2882{
2883 struct dcache_tx_entry *de;
2884 struct ebpt_node *node;
2885
2886 de = NULL;
2887 node = ebpt_lookup(&d->values, i->value.key);
2888 if (node)
2889 de = container_of(node, struct dcache_tx_entry, value);
2890
2891 return de;
2892}
2893
2894/*
2895 * Release the memory allocated for the cached dictionary entry <dce>.
2896 */
2897
2898static inline void free_dcache_tx_entry(struct dcache_tx_entry *dce)
2899{
2900 dce->value.key = NULL;
2901 dce->key.key = 0;
2902 free(dce);
2903}
2904
2905/*
2906 * Allocate a new dictionary entry with <s> as string value which is strdup()'ed.
2907 * Returns the new allocated entry if succeeded, NULL if not.
2908 */
2909struct dcache_tx_entry *new_dcache_tx_entry(unsigned int k, struct dict_entry *de)
2910{
2911 struct dcache_tx_entry *dce;
2912
2913 dce = calloc(1, sizeof *dce);
2914 if (!dce)
2915 return NULL;
2916
2917 dce->value.key = de;
2918 dce->key.key = k;
2919
2920 return dce;
2921}
2922
2923/*
2924 * Flush <dc> cache.
2925 * Always succeeds.
2926 */
2927static inline void flush_dcache(struct peer *peer)
2928{
2929 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002930 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002931
2932 if (!eb_is_empty(&dc->tx->keys)) {
2933 struct eb32_node *node, *next;
2934
2935 for (node = eb32_first(&dc->tx->keys); node; node = next) {
2936 next = eb32_next(node);
2937 eb32_delete(node);
2938 }
2939 }
2940
2941 if (!eb_is_empty(&dc->tx->values)) {
2942 struct ebpt_node *node, *next;
2943
2944 for (node = ebpt_first(&dc->tx->values); node; node = next) {
2945 struct dcache_tx_entry *de;
2946
2947 next = ebpt_next(node);
2948 ebpt_delete(node);
2949 de = container_of(node, struct dcache_tx_entry, value);
2950 free_dcache_tx_entry(de);
2951 }
2952 }
2953
2954 for (i = 0; i < dc->max_entries; i++) {
2955 dc->rx[i].id = 0;
2956 dc->rx[i].de = NULL;
2957 }
2958}
2959
2960/*
2961 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
2962 * with information provided by <i> dictionary cache entry (especially the value
2963 * to be inserted if not already). Return <i> if already present in the cache
2964 * or something different of <i> if not already in the cache, or NULL if something
2965 * failed.
2966 */
2967struct dcache_tx_entry *dcache_tx_insert(struct dcache *dc,
2968 struct dcache_tx_entry *i)
2969{
2970 struct dcache_tx *dc_tx;
2971 struct dcache_tx_entry *o;
2972
2973 dc_tx = dc->tx;
2974 o = dcache_tx_lookup_value(dc_tx, i);
2975 if (o) {
2976 /* Copy the key. */
2977 i->key.key = o->key.key;
2978 return i;
2979 }
2980
2981 o = dcache_tx_lookup_key(dc_tx, dc_tx->lru_key);
2982 if (o) {
2983 o->value.key = i->value.key;
2984 return o;
2985 }
2986
2987 o = new_dcache_tx_entry(dc_tx->lru_key, i->value.key);
2988 if (!o)
2989 return NULL;
2990
2991 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc_tx->max_entries - 1);
2992 eb32_insert(&dc_tx->keys, &o->key);
2993 ebpt_insert(&dc_tx->values, &o->value);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002994
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002995 return o;
2996}
Emeric Brunb3971ab2015-05-12 18:49:09 +02002997
2998/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002999 * Allocate a dictionary cache for each peer of <peers> section.
3000 * Return 1 if succeeded, 0 if not.
3001 */
3002int peers_alloc_dcache(struct peers *peers)
3003{
3004 struct peer *p;
3005
3006 for (p = peers->remote; p; p = p->next) {
3007 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3008 if (!p->dcache)
3009 return 0;
3010 }
3011
3012 return 1;
3013}
3014
3015/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003016 * Function used to register a table for sync on a group of peers
3017 *
3018 */
3019void peers_register_table(struct peers *peers, struct stktable *table)
3020{
3021 struct shared_table *st;
3022 struct peer * curpeer;
3023 int id = 0;
3024
3025 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003026 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003027 st->table = table;
3028 st->next = curpeer->tables;
3029 if (curpeer->tables)
3030 id = curpeer->tables->local_id;
3031 st->local_id = id + 1;
3032
3033 curpeer->tables = st;
3034 }
3035
3036 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02003037}
3038
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003039/*
3040 * Parse the "show peers" command arguments.
3041 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3042 * error message.
3043 */
3044static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3045{
3046 appctx->ctx.cfgpeers.target = NULL;
3047
3048 if (*args[2]) {
3049 struct peers *p;
3050
3051 for (p = cfg_peers; p; p = p->next) {
3052 if (!strcmp(p->id, args[2])) {
3053 appctx->ctx.cfgpeers.target = p;
3054 break;
3055 }
3056 }
3057
3058 if (!p) {
3059 appctx->ctx.cli.severity = LOG_ERR;
3060 appctx->ctx.cli.msg = "No such peers\n";
3061 appctx->st0 = CLI_ST_PRINT;
3062 return 1;
3063 }
3064 }
3065
3066 return 0;
3067}
3068
3069/*
3070 * This function dumps the peer state information of <peers> "peers" section.
3071 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3072 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3073 */
3074static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3075{
3076 struct tm tm;
3077
3078 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003079 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 +02003080 peers,
3081 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3082 tm.tm_hour, tm.tm_min, tm.tm_sec,
3083 peers->id, peers->state, peers->flags,
3084 peers->resync_timeout ?
3085 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3086 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003087 TICKS_TO_MS(1000)) : "<NEVER>",
3088 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003089
3090 if (ci_putchk(si_ic(si), msg) == -1) {
3091 si_rx_room_blk(si);
3092 return 0;
3093 }
3094
3095 return 1;
3096}
3097
3098/*
3099 * This function dumps <peer> state information.
3100 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3101 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3102 */
3103static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3104{
3105 struct connection *conn;
3106 char pn[INET6_ADDRSTRLEN];
3107 struct stream_interface *peer_si;
3108 struct stream *peer_s;
3109 struct appctx *appctx;
3110 struct shared_table *st;
3111
3112 addr_to_str(&peer->addr, pn, sizeof pn);
3113 chunk_appendf(msg, " %p: id=%s(%s) addr=%s:%d status=%s reconnect=%s confirm=%u\n",
3114 peer, peer->id,
3115 peer->local ? "local" : "remote",
3116 pn, get_host_port(&peer->addr),
3117 statuscode_str(peer->statuscode),
3118 peer->reconnect ?
3119 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3120 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3121 TICKS_TO_MS(1000)) : "<NEVER>",
3122 peer->confirm);
3123
3124 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3125
3126 appctx = peer->appctx;
3127 if (!appctx)
3128 goto end;
3129
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003130 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3131 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003132
3133 peer_si = peer->appctx->owner;
3134 if (!peer_si)
3135 goto end;
3136
3137 peer_s = si_strm(peer_si);
3138 if (!peer_s)
3139 goto end;
3140
3141 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3142
3143 conn = objt_conn(strm_orig(peer_s));
3144 if (conn)
3145 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3146
3147 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3148 case AF_INET:
3149 case AF_INET6:
3150 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(&conn->addr.from));
3151 break;
3152 case AF_UNIX:
3153 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3154 break;
3155 }
3156
3157 if (conn)
3158 conn_get_to_addr(conn);
3159
3160 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3161 case AF_INET:
3162 case AF_INET6:
3163 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(&conn->addr.to));
3164 break;
3165 case AF_UNIX:
3166 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3167 break;
3168 }
3169
3170 if (peer->remote_table)
3171 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3172 peer->remote_table,
3173 peer->remote_table->table->id,
3174 peer->remote_table->local_id,
3175 peer->remote_table->remote_id);
3176
3177 if (peer->last_local_table)
3178 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3179 peer->last_local_table,
3180 peer->last_local_table->table->id,
3181 peer->last_local_table->local_id,
3182 peer->last_local_table->remote_id);
3183
3184 if (peer->tables) {
3185 chunk_appendf(&trash, "\n shared tables:");
3186 for (st = peer->tables; st; st = st->next) {
3187 struct stktable *t;
3188 t = st->table;
3189 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3190 "flags=0x%x remote_data=0x%llx",
3191 st, st->local_id, st->remote_id,
3192 st->flags, (unsigned long long)st->remote_data);
3193 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3194 " teaching_origin=%u update=%u",
3195 st->last_acked, st->last_pushed, st->last_get,
3196 st->teaching_origin, st->update);
3197 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
3198 " commitupdate=%u syncing=%u",
3199 t, t->id, t->update, t->localupdate, t->commitupdate, t->syncing);
3200 }
3201 }
3202
3203 end:
3204 chunk_appendf(&trash, "\n");
3205 if (ci_putchk(si_ic(si), msg) == -1) {
3206 si_rx_room_blk(si);
3207 return 0;
3208 }
3209
3210 return 1;
3211}
3212
3213/*
3214 * This function dumps all the peers of "peers" section.
3215 * Returns 0 if the output buffer is full and needs to be called
3216 * again, non-zero if not. It proceeds in an isolated thread, so
3217 * there is no thread safety issue here.
3218 */
3219static int cli_io_handler_show_peers(struct appctx *appctx)
3220{
3221 int show_all;
3222 int ret = 0, first_peers = 1;
3223 struct stream_interface *si = appctx->owner;
3224
3225 thread_isolate();
3226
3227 show_all = !appctx->ctx.cfgpeers.target;
3228
3229 chunk_reset(&trash);
3230
3231 while (appctx->st2 != STAT_ST_FIN) {
3232 switch (appctx->st2) {
3233 case STAT_ST_INIT:
3234 if (show_all)
3235 appctx->ctx.cfgpeers.peers = cfg_peers;
3236 else
3237 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3238
3239 appctx->st2 = STAT_ST_LIST;
3240 /* fall through */
3241
3242 case STAT_ST_LIST:
3243 if (!appctx->ctx.cfgpeers.peers) {
3244 /* No more peers list. */
3245 appctx->st2 = STAT_ST_END;
3246 }
3247 else {
3248 if (!first_peers)
3249 chunk_appendf(&trash, "\n");
3250 else
3251 first_peers = 0;
3252 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3253 goto out;
3254
3255 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3256 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3257 appctx->st2 = STAT_ST_INFO;
3258 }
3259 break;
3260
3261 case STAT_ST_INFO:
3262 if (!appctx->ctx.cfgpeers.peer) {
3263 /* End of peer list */
3264 if (show_all)
3265 appctx->st2 = STAT_ST_LIST;
3266 else
3267 appctx->st2 = STAT_ST_END;
3268 }
3269 else {
3270 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3271 goto out;
3272
3273 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3274 }
3275 break;
3276
3277 case STAT_ST_END:
3278 appctx->st2 = STAT_ST_FIN;
3279 break;
3280 }
3281 }
3282 ret = 1;
3283 out:
3284 thread_release();
3285 return ret;
3286}
3287
3288/*
3289 * CLI keywords.
3290 */
3291static struct cli_kw_list cli_kws = {{ }, {
3292 { { "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, },
3293 {},
3294}};
3295
3296/* Register cli keywords */
3297INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3298