blob: 8ffc0cb59ae45770bca723d5f1e0d4c5ef708f94 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020027
28#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010029#include <types/listener.h>
30#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020031#include <types/peers.h>
32
33#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020034#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020036#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010037#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020038#include <proto/log.h>
39#include <proto/hdr_idx.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020040#include <proto/proto_tcp.h>
41#include <proto/proto_http.h>
42#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020043#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020044#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010045#include <proto/signal.h>
46#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020047#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049
50
51/*******************************/
52/* Current peer learning state */
53/*******************************/
54
55/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020056/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020057/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
59#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
60#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
61#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
62#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020063 to push data to new process */
64
Emeric Brunb3971ab2015-05-12 18:49:09 +020065#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
66#define PEERS_RESYNC_FROMLOCAL 0x00000000
67#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
68#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
69
70/***********************************/
71/* Current shared table sync state */
72/***********************************/
73#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
74#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020075
76/******************************/
77/* Remote peer teaching state */
78/******************************/
79#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020080#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
81#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
82#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
83#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020084#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020085
Emeric Brunb3971ab2015-05-12 18:49:09 +020086#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020087#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
88
Emeric Brunb3971ab2015-05-12 18:49:09 +020089/*****************************/
90/* Sync message class */
91/*****************************/
92enum {
93 PEER_MSG_CLASS_CONTROL = 0,
94 PEER_MSG_CLASS_ERROR,
95 PEER_MSG_CLASS_STICKTABLE = 10,
96 PEER_MSG_CLASS_RESERVED = 255,
97};
98
99/*****************************/
100/* control message types */
101/*****************************/
102enum {
103 PEER_MSG_CTRL_RESYNCREQ = 0,
104 PEER_MSG_CTRL_RESYNCFINISHED,
105 PEER_MSG_CTRL_RESYNCPARTIAL,
106 PEER_MSG_CTRL_RESYNCCONFIRM,
107};
108
109/*****************************/
110/* error message types */
111/*****************************/
112enum {
113 PEER_MSG_ERR_PROTOCOL = 0,
114 PEER_MSG_ERR_SIZELIMIT,
115};
116
117
118/*******************************/
119/* stick table sync mesg types */
120/* Note: ids >= 128 contains */
121/* id message cotains data */
122/*******************************/
123enum {
124 PEER_MSG_STKT_UPDATE = 128,
125 PEER_MSG_STKT_INCUPDATE,
126 PEER_MSG_STKT_DEFINE,
127 PEER_MSG_STKT_SWITCH,
128 PEER_MSG_STKT_ACK,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200129 PEER_MSG_STKT_UPDATE_TIMED,
130 PEER_MSG_STKT_INCUPDATE_TIMED,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200131};
Emeric Brun2b920a12010-09-23 18:30:22 +0200132
133/**********************************/
134/* Peer Session IO handler states */
135/**********************************/
136
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100137enum {
138 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
139 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
140 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
141 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100142 /* after this point, data were possibly exchanged */
143 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
144 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
145 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
146 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
147 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200148 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
149 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100150 PEER_SESS_ST_END, /* Killed session */
151};
Emeric Brun2b920a12010-09-23 18:30:22 +0200152
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100153/***************************************************/
154/* Peer Session status code - part of the protocol */
155/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200156
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100157#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
158#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200159
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100160#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200161
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100162#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200163
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100164#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
165#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
166#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
167#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200168
169#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200170#define PEER_MAJOR_VER 2
171#define PEER_MINOR_VER 1
172#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200173
174struct peers *peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100175static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200176
Emeric Brunb3971ab2015-05-12 18:49:09 +0200177int intencode(uint64_t i, char **str) {
178 int idx = 0;
179 unsigned char *msg;
180
181 if (!*str)
182 return 0;
183
184 msg = (unsigned char *)*str;
185 if (i < 240) {
186 msg[0] = (unsigned char)i;
187 *str = (char *)&msg[idx+1];
188 return (idx+1);
189 }
190
191 msg[idx] =(unsigned char)i | 240;
192 i = (i - 240) >> 4;
193 while (i >= 128) {
194 msg[++idx] = (unsigned char)i | 128;
195 i = (i - 128) >> 7;
196 }
197 msg[++idx] = (unsigned char)i;
198 *str = (char *)&msg[idx+1];
199 return (idx+1);
200}
201
202
203/* This function returns the decoded integer or 0
204 if decode failed
205 *str point on the beginning of the integer to decode
206 at the end of decoding *str point on the end of the
207 encoded integer or to null if end is reached */
208uint64_t intdecode(char **str, char *end) {
209 uint64_t i;
210 int idx = 0;
211 unsigned char *msg;
212
213 if (!*str)
214 return 0;
215
216 msg = (unsigned char *)*str;
217 if (msg >= (unsigned char *)end) {
218 *str = NULL;
219 return 0;
220 }
221
222 if (msg[idx] < 240) {
223 *str = (char *)&msg[idx+1];
224 return msg[idx];
225 }
226 i = msg[idx];
227 do {
228 idx++;
229 if (msg >= (unsigned char *)end) {
230 *str = NULL;
231 return 0;
232 }
233 i += (uint64_t)msg[idx] << (4 + 7*(idx-1));
234 }
Frédéric Lécaille22fc3202016-07-19 14:04:36 +0200235 while (msg[idx] >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200236 *str = (char *)&msg[idx+1];
237 return i;
238}
Emeric Brun2b920a12010-09-23 18:30:22 +0200239
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200240/* Set the stick-table UPDATE message type byte at <msg_type> address,
241 * depending on <use_identifier> and <use_timed> boolean parameters.
242 * Always successful.
243 */
244static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
245{
246 if (use_timed) {
247 if (use_identifier)
248 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
249 else
250 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
251 }
252 else {
253 if (use_identifier)
254 *msg_type = PEER_MSG_STKT_UPDATE;
255 else
256 *msg_type = PEER_MSG_STKT_INCUPDATE;
257 }
258
259}
Emeric Brun2b920a12010-09-23 18:30:22 +0200260/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200261 * This prepare the data update message on the stick session <ts>, <st> is the considered
262 * stick table.
263 * <msg> is a buffer of <size> to recieve data message content
264 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
265 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200266 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200267static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200268{
269 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200270 unsigned short datalen;
271 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200272 unsigned int data_type;
273 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274
275 cursor = datamsg = msg + 1 + 5;
276
Emeric Brun2b920a12010-09-23 18:30:22 +0200277 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 /* check if we need to send the update identifer */
Emeric Bruna6a09982015-09-22 15:34:19 +0200280 if (!st->last_pushed || ts->upd.key < st->last_pushed || ((ts->upd.key - st->last_pushed) != 1)) {
281 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200282 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200283
284 /* encode update identifier if needed */
285 if (use_identifier) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 netinteger = htonl(ts->upd.key);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287 memcpy(cursor, &netinteger, sizeof(netinteger));
288 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200289 }
290
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200291 if (use_timed) {
292 netinteger = htonl(tick_remain(now_ms, ts->expire));
293 memcpy(cursor, &netinteger, sizeof(netinteger));
294 cursor += sizeof(netinteger);
295 }
296
Emeric Brunb3971ab2015-05-12 18:49:09 +0200297 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200298 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200299 int stlen = strlen((char *)ts->key.key);
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 intencode(stlen, &cursor);
302 memcpy(cursor, ts->key.key, stlen);
303 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200304 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200305 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200306 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200307 memcpy(cursor, &netinteger, sizeof(netinteger));
308 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200309 }
310 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, ts->key.key, st->table->key_size);
312 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200316 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200317
Emeric Brun94900952015-06-11 18:25:54 +0200318 data_ptr = stktable_data_ptr(st->table, ts, data_type);
319 if (data_ptr) {
320 switch (stktable_data_types[data_type].std_type) {
321 case STD_T_SINT: {
322 int data;
323
324 data = stktable_data_cast(data_ptr, std_t_sint);
325 intencode(data, &cursor);
326 break;
327 }
328 case STD_T_UINT: {
329 unsigned int data;
330
331 data = stktable_data_cast(data_ptr, std_t_uint);
332 intencode(data, &cursor);
333 break;
334 }
335 case STD_T_ULL: {
336 unsigned long long data;
337
338 data = stktable_data_cast(data_ptr, std_t_ull);
339 intencode(data, &cursor);
340 break;
341 }
342 case STD_T_FRQP: {
343 struct freq_ctr_period *frqp;
344
345 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
346 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
347 intencode(frqp->curr_ctr, &cursor);
348 intencode(frqp->prev_ctr, &cursor);
349 break;
350 }
351 }
352 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200353 }
354
355 /* Compute datalen */
356 datalen = (cursor - datamsg);
357
358 /* prepare message header */
359 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200360 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200361 cursor = &msg[2];
362 intencode(datalen, &cursor);
363
364 /* move data after header */
365 memmove(cursor, datamsg, datalen);
366
367 /* return header size + data_len */
368 return (cursor - msg) + datalen;
369}
370
371/*
372 * This prepare the switch table message to targeted share table <st>.
373 * <msg> is a buffer of <size> to recieve data message content
374 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
375 * check size)
376 */
377static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
378{
379 int len;
380 unsigned short datalen;
381 char *cursor, *datamsg;
382 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200383 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200384
385 cursor = datamsg = msg + 2 + 5;
386
387 /* Encode data */
388
389 /* encode local id */
390 intencode(st->local_id, &cursor);
391
392 /* encode table name */
393 len = strlen(st->table->id);
394 intencode(len, &cursor);
395 memcpy(cursor, st->table->id, len);
396 cursor += len;
397
398 /* encode table type */
399
400 intencode(st->table->type, &cursor);
401
402 /* encode table key size */
403 intencode(st->table->key_size, &cursor);
404
Emeric Brun94900952015-06-11 18:25:54 +0200405 /* encode available known data types in table */
406 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
407 if (st->table->data_ofs[data_type]) {
408 switch (stktable_data_types[data_type].std_type) {
409 case STD_T_SINT:
410 case STD_T_UINT:
411 case STD_T_ULL:
412 case STD_T_FRQP:
413 data |= 1 << data_type;
414 break;
415 }
416 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200417 }
418 intencode(data, &cursor);
419
420 /* Compute datalen */
421 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200422
Emeric Brunb3971ab2015-05-12 18:49:09 +0200423 /* prepare message header */
424 msg[0] = PEER_MSG_CLASS_STICKTABLE;
425 msg[1] = PEER_MSG_STKT_DEFINE;
426 cursor = &msg[2];
427 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200428
Emeric Brunb3971ab2015-05-12 18:49:09 +0200429 /* move data after header */
430 memmove(cursor, datamsg, datalen);
431
432 /* return header size + data_len */
433 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200434}
435
Emeric Brunb3971ab2015-05-12 18:49:09 +0200436/*
437 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
438 * stick table.
439 * <msg> is a buffer of <size> to recieve data message content
440 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
441 * check size)
442 */
443static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
444{
445 unsigned short datalen;
446 char *cursor, *datamsg;
447 uint32_t netinteger;
448
Emeric Brunb058f1c2015-09-22 15:50:18 +0200449 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450
451 intencode(st->remote_id, &cursor);
452 netinteger = htonl(st->last_get);
453 memcpy(cursor, &netinteger, sizeof(netinteger));
454 cursor += sizeof(netinteger);
455
456 /* Compute datalen */
457 datalen = (cursor - datamsg);
458
459 /* prepare message header */
460 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200461 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 cursor = &msg[2];
463 intencode(datalen, &cursor);
464
465 /* move data after header */
466 memmove(cursor, datamsg, datalen);
467
468 /* return header size + data_len */
469 return (cursor - msg) + datalen;
470}
Emeric Brun2b920a12010-09-23 18:30:22 +0200471
472/*
473 * Callback to release a session with a peer
474 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200475static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200476{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200477 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200478 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200479 struct peer *peer = appctx->ctx.peers.ptr;
480 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200481
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100482 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100483 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200484 return;
485
486 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487 if (peer) {
Willy Tarreau9df94c22016-10-31 18:42:52 +0100488 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200489 /* Re-init current table pointers to force announcement on re-connect */
490 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491 peer->appctx = NULL;
492 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200493 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200494 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
495 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200496
497 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200499 }
500 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200501 peer->flags &= PEER_TEACH_RESET;
502 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200505 }
506}
507
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200508/* Retrieve the major and minor versions of peers protocol
509 * announced by a remote peer. <str> is a null-terminated
510 * string with the following format: "<maj_ver>.<min_ver>".
511 */
512static int peer_get_version(const char *str,
513 unsigned int *maj_ver, unsigned int *min_ver)
514{
515 unsigned int majv, minv;
516 const char *pos, *saved;
517 const char *end;
518
519 saved = pos = str;
520 end = str + strlen(str);
521
522 majv = read_uint(&pos, end);
523 if (saved == pos || *pos++ != '.')
524 return -1;
525
526 saved = pos;
527 minv = read_uint(&pos, end);
528 if (saved == pos || pos != end)
529 return -1;
530
531 *maj_ver = majv;
532 *min_ver = minv;
533
534 return 0;
535}
Emeric Brun2b920a12010-09-23 18:30:22 +0200536
537/*
538 * IO Handler to handle message exchance with a peer
539 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200540static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200541{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200542 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200543 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200544 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200545 int reql = 0;
546 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200547 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
548 unsigned int maj_ver, min_ver;
Emeric Brun2b920a12010-09-23 18:30:22 +0200549
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100550 /* Check if the input buffer is avalaible. */
551 if (si_ic(si)->buf->size == 0)
552 goto full;
553
Emeric Brun2b920a12010-09-23 18:30:22 +0200554 while (1) {
555switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200556 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100557 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100558 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100559 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100560 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200561 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100562 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100563 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200564 if (reql <= 0) { /* closed or EOL not found */
565 if (reql == 0)
566 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100567 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200568 goto switchstate;
569 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100570 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100571 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200572 goto switchstate;
573 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100574 else if (reql > 1 && (trash.str[reql-2] == '\r'))
575 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200576 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100577 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200578
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100579 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200580
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200581 /* test protocol */
582 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) {
583 appctx->st0 = PEER_SESS_ST_EXIT;
584 appctx->st1 = PEER_SESS_SC_ERRPROTO;
585 goto switchstate;
586 }
587 if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 ||
588 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100589 appctx->st0 = PEER_SESS_ST_EXIT;
590 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200591 goto switchstate;
592 }
593
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100594 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200595 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100596 case PEER_SESS_ST_GETHOST:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100597 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200598 if (reql <= 0) { /* closed or EOL not found */
599 if (reql == 0)
600 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100601 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602 goto switchstate;
603 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100605 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200606 goto switchstate;
607 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100608 else if (reql > 1 && (trash.str[reql-2] == '\r'))
609 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200610 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100611 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200612
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100613 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200614
615 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100616 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100617 appctx->st0 = PEER_SESS_ST_EXIT;
618 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200619 goto switchstate;
620 }
621
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100622 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200623 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100624 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200625 struct peer *curpeer;
626 char *p;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100627 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 if (reql <= 0) { /* closed or EOL not found */
629 if (reql == 0)
630 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100631 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200632 goto switchstate;
633 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100634 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200635 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100636 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200637 goto switchstate;
638 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100639 else if (reql > 1 && (trash.str[reql-2] == '\r'))
640 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200641 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100642 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200643
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100644 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200645
Emeric Brunb3971ab2015-05-12 18:49:09 +0200646 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100647 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200648 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100649 appctx->st0 = PEER_SESS_ST_EXIT;
650 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200651 goto switchstate;
652 }
653 *p = 0;
654
655 /* lookup known peer */
656 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100657 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200658 break;
659 }
660
661 /* if unknown peer */
662 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100663 appctx->st0 = PEER_SESS_ST_EXIT;
664 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200665 goto switchstate;
666 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200667
Willy Tarreau9df94c22016-10-31 18:42:52 +0100668 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200669 if (curpeer->local) {
670 /* Local connection, reply a retry */
671 appctx->st0 = PEER_SESS_ST_EXIT;
672 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
673 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200674 }
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100675 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200676 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200677 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
678 if (min_ver == PEER_DWNGRD_MINOR_VER) {
679 curpeer->flags |= PEER_F_DWNGRD;
680 }
681 else {
682 curpeer->flags &= ~PEER_F_DWNGRD;
683 }
684 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200685 curpeer->appctx = appctx;
686 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100687 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200688 /* fall through */
689 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100690 case PEER_SESS_ST_SENDSUCCESS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200691 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200692 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200693
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100694 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100695 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200696 if (repl <= 0) {
697 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100698 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100699 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200700 goto switchstate;
701 }
702
703 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200704 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200705
706 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200707 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200708
709 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200710 curpeer->confirm = 0;
711
712 /* Init cursors */
713 for (st = curpeer->tables; st ; st = st->next) {
714 st->last_get = st->last_acked = 0;
715 st->teaching_origin = st->last_pushed = st->update;
716 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200717
718 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200719 curpeer->flags &= PEER_TEACH_RESET;
720 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200721
722 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200723 if (curpeer->local) {
724 /* if current host need resyncfrom local and no process assined */
725 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
726 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
727 /* assign local peer for a lesson, consider lesson already requested */
728 curpeer->flags |= PEER_F_LEARN_ASSIGN;
729 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
730 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200731
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732 }
733 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
734 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
735 /* assign peer for a lesson */
736 curpeer->flags |= PEER_F_LEARN_ASSIGN;
737 peers->flags |= PEERS_F_RESYNC_ASSIGN;
738 }
739
740
Emeric Brun2b920a12010-09-23 18:30:22 +0200741 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100742 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200743 goto switchstate;
744 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100745 case PEER_SESS_ST_CONNECT: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200746 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200747
748 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100749 repl = snprintf(trash.str, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200750 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
751 PEER_MAJOR_VER,
752 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200753 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200754 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100755 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200756 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200757
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100758 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100759 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200760 goto switchstate;
761 }
762
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100763 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200764 if (repl <= 0) {
765 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100766 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100767 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200768 goto switchstate;
769 }
770
771 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100772 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200773 /* fall through */
774 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100775 case PEER_SESS_ST_GETSTATUS: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200776 struct peer *curpeer = appctx->ctx.peers.ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200777 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200778
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100779 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200780 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200781
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100782 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200783 if (reql <= 0) { /* closed or EOL not found */
784 if (reql == 0)
785 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100786 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200787 goto switchstate;
788 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100789 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200790 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100791 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200792 goto switchstate;
793 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100794 else if (reql > 1 && (trash.str[reql-2] == '\r'))
795 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200796 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100797 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200798
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100799 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200800
801 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200802 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200803
804 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200805 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200806
807 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200808 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200809 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200810 for (st = curpeer->tables; st ; st = st->next) {
811 st->last_get = st->last_acked = 0;
812 st->teaching_origin = st->last_pushed = st->update;
813 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200814
815 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200816 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200817
Emeric Brunb3971ab2015-05-12 18:49:09 +0200818 /* reset teaching and learning flags to 0 */
819 curpeer->flags &= PEER_TEACH_RESET;
820 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200821
Emeric Brunb3971ab2015-05-12 18:49:09 +0200822 /* If current peer is local */
823 if (curpeer->local) {
824 /* flag to start to teach lesson */
825 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200826
Emeric Brunb3971ab2015-05-12 18:49:09 +0200827 }
828 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
829 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
830 /* If peer is remote and resync from remote is needed,
831 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200832
Emeric Brunb3971ab2015-05-12 18:49:09 +0200833 /* assign peer for a lesson */
834 curpeer->flags |= PEER_F_LEARN_ASSIGN;
835 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200836 }
837
838 }
839 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200840 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
841 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200842 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100843 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200844 goto switchstate;
845 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100846 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200847 /* fall through */
848 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100849 case PEER_SESS_ST_WAITMSG: {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200850 struct peer *curpeer = appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200851 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200852 uint32_t msg_len = 0;
853 char *msg_cur = trash.str;
854 char *msg_end = trash.str;
855 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200856 int totl = 0;
857
Emeric Brunb3971ab2015-05-12 18:49:09 +0200858 reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200859 if (reql <= 0) /* closed or EOL not found */
860 goto incomplete;
861
Emeric Brun2b920a12010-09-23 18:30:22 +0200862 totl += reql;
863
Emeric Brunb3971ab2015-05-12 18:49:09 +0200864 if (msg_head[1] >= 128) {
865 /* Read and Decode message length */
866 reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
867 if (reql <= 0) /* closed */
868 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200869
Emeric Brunb3971ab2015-05-12 18:49:09 +0200870 totl += reql;
871
872 if (msg_head[2] < 240) {
873 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200874 }
875 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200876 int i;
877 char *cur;
878 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200879
Emeric Brunb3971ab2015-05-12 18:49:09 +0200880 for (i = 3 ; i < sizeof(msg_head) ; i++) {
881 reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
882 if (reql <= 0) /* closed */
883 goto incomplete;
884
885 totl += reql;
886
887 if (!(msg_head[i] & 0x80))
888 break;
889 }
890
891 if (i == sizeof(msg_head)) {
892 /* malformed message */
893 appctx->st0 = PEER_SESS_ST_ERRPROTO;
894 goto switchstate;
895
896 }
897 end = (char *)msg_head + sizeof(msg_head);
898 cur = (char *)&msg_head[2];
899 msg_len = intdecode(&cur, end);
900 if (!cur) {
901 /* malformed message */
902 appctx->st0 = PEER_SESS_ST_ERRPROTO;
903 goto switchstate;
904 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200905 }
906
Willy Tarreau86a446e2013-11-25 23:02:37 +0100907
Emeric Brunb3971ab2015-05-12 18:49:09 +0200908 /* Read message content */
909 if (msg_len) {
910 if (msg_len > trash.size) {
911 /* Status code is not success, abort */
912 appctx->st0 = PEER_SESS_ST_ERRSIZE;
913 goto switchstate;
914 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200915
Emeric Brunb3971ab2015-05-12 18:49:09 +0200916 reql = bo_getblk(si_oc(si), trash.str, msg_len, totl);
917 if (reql <= 0) /* closed */
918 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200919 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100920
Emeric Brunb3971ab2015-05-12 18:49:09 +0200921 msg_end += msg_len;
922 }
923 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100924
Emeric Brunb3971ab2015-05-12 18:49:09 +0200925 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
926 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
927 struct shared_table *st;
928 /* Reset message: remote need resync */
929
930 /* prepare tables fot a global push */
931 for (st = curpeer->tables; st; st = st->next) {
932 st->teaching_origin = st->last_pushed = st->table->update;
933 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100934 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200935
Emeric Brunb3971ab2015-05-12 18:49:09 +0200936 /* reset teaching flags to 0 */
937 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200938
Emeric Brunb3971ab2015-05-12 18:49:09 +0200939 /* flag to start to teach lesson */
940 curpeer->flags |= PEER_F_TEACH_PROCESS;
941
942
943 }
944 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
945
946 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
947 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
948 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
949 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100950 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200951 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200952 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200953 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
954
955 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
956 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
957 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
958
959 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
960 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
961 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100962 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200963 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200964 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200965 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +0200966 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200967
968 /* If stopping state */
969 if (stopping) {
970 /* Close session, push resync no more needed */
971 curpeer->flags |= PEER_F_TEACH_COMPLETE;
972 appctx->st0 = PEER_SESS_ST_END;
973 goto switchstate;
974 }
Emeric Brun597b26e2016-08-12 11:23:31 +0200975 for (st = curpeer->tables; st; st = st->next) {
976 st->update = st->last_pushed = st->teaching_origin;
977 st->flags = 0;
978 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200979
980 /* reset teaching flags to 0 */
981 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200982 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200983 }
984 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
985 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
986 int table_id_len;
987 struct shared_table *st;
988 int table_type;
989 int table_keylen;
990 int table_id;
991 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +0200992
Emeric Brunb3971ab2015-05-12 18:49:09 +0200993 table_id = intdecode(&msg_cur, msg_end);
994 if (!msg_cur) {
995 /* malformed message */
996 appctx->st0 = PEER_SESS_ST_ERRPROTO;
997 goto switchstate;
998 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200999
Emeric Brunb3971ab2015-05-12 18:49:09 +02001000 table_id_len = intdecode(&msg_cur, msg_end);
1001 if (!msg_cur) {
1002 /* malformed message */
1003 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1004 goto switchstate;
1005 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001006
Emeric Brunb3971ab2015-05-12 18:49:09 +02001007 curpeer->remote_table = NULL;
1008 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1009 /* malformed message */
1010 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1011 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001012 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001013
Emeric Brunb3971ab2015-05-12 18:49:09 +02001014 for (st = curpeer->tables; st; st = st->next) {
1015 /* Reset IDs */
1016 if (st->remote_id == table_id)
1017 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001018
Emeric Brunb3971ab2015-05-12 18:49:09 +02001019 if (!curpeer->remote_table
1020 && (table_id_len == strlen(st->table->id))
1021 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1022 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001023 }
1024 }
1025
Emeric Brunb3971ab2015-05-12 18:49:09 +02001026 if (!curpeer->remote_table) {
1027 goto ignore_msg;
1028 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001029
Emeric Brunb3971ab2015-05-12 18:49:09 +02001030 msg_cur += table_id_len;
1031 if (msg_cur >= msg_end) {
1032 /* malformed message */
1033 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1034 goto switchstate;
1035 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001036
Emeric Brunb3971ab2015-05-12 18:49:09 +02001037 table_type = intdecode(&msg_cur, msg_end);
1038 if (!msg_cur) {
1039 /* malformed message */
1040 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1041 goto switchstate;
1042 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001043
Emeric Brunb3971ab2015-05-12 18:49:09 +02001044 table_keylen = intdecode(&msg_cur, msg_end);
1045 if (!msg_cur) {
1046 /* malformed message */
1047 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1048 goto switchstate;
1049 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001050
Emeric Brunb3971ab2015-05-12 18:49:09 +02001051 table_data = intdecode(&msg_cur, msg_end);
1052 if (!msg_cur) {
1053 /* malformed message */
1054 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1055 goto switchstate;
1056 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001057
Emeric Brunb3971ab2015-05-12 18:49:09 +02001058 if (curpeer->remote_table->table->type != table_type
1059 || curpeer->remote_table->table->key_size != table_keylen) {
1060 curpeer->remote_table = NULL;
1061 goto ignore_msg;
1062 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001063
Emeric Brunb3971ab2015-05-12 18:49:09 +02001064 curpeer->remote_table->remote_data = table_data;
1065 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001066 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001067 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1068 struct shared_table *st;
1069 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001070
Emeric Brunb3971ab2015-05-12 18:49:09 +02001071 table_id = intdecode(&msg_cur, msg_end);
1072 if (!msg_cur) {
1073 /* malformed message */
1074 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1075 goto switchstate;
1076 }
1077 curpeer->remote_table = NULL;
1078 for (st = curpeer->tables; st; st = st->next) {
1079 if (st->remote_id == table_id) {
1080 curpeer->remote_table = st;
1081 break;
1082 }
1083 }
1084
Emeric Brun2b920a12010-09-23 18:30:22 +02001085 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001086 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001087 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1088 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1089 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001090 struct shared_table *st = curpeer->remote_table;
1091 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001092 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001093 unsigned int data_type;
1094 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001095
Emeric Brunb3971ab2015-05-12 18:49:09 +02001096 /* Here we have data message */
1097 if (!st)
1098 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001099
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001100 expire = MS_TO_TICKS(st->table->expire);
1101
1102 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1103 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001104 if (msg_len < sizeof(update)) {
1105 /* malformed message */
1106 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1107 goto switchstate;
1108 }
1109 memcpy(&update, msg_cur, sizeof(update));
1110 msg_cur += sizeof(update);
1111 st->last_get = htonl(update);
1112 }
1113 else {
1114 st->last_get++;
1115 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001116
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001117 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1118 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1119 size_t expire_sz = sizeof expire;
1120
1121 if (msg_cur + expire_sz > msg_end) {
1122 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1123 goto switchstate;
1124 }
1125 memcpy(&expire, msg_cur, expire_sz);
1126 msg_cur += expire_sz;
1127 expire = ntohl(expire);
1128 }
1129
Emeric Brunb3971ab2015-05-12 18:49:09 +02001130 newts = stksess_new(st->table, NULL);
1131 if (!newts)
1132 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001133
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001134 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001135 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001136
Emeric Brunb3971ab2015-05-12 18:49:09 +02001137 to_read = intdecode(&msg_cur, msg_end);
1138 if (!msg_cur) {
1139 /* malformed message */
1140 stksess_free(st->table, newts);
1141 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1142 goto switchstate;
1143 }
1144 to_store = MIN(to_read, st->table->key_size - 1);
1145 if (msg_cur + to_store > msg_end) {
1146 /* malformed message */
1147 stksess_free(st->table, newts);
1148 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1149 goto switchstate;
1150 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001151
Emeric Brunb3971ab2015-05-12 18:49:09 +02001152 memcpy(newts->key.key, msg_cur, to_store);
1153 newts->key.key[to_store] = 0;
1154 msg_cur += to_read;
1155 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001156 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001157 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001158
Emeric Brunb3971ab2015-05-12 18:49:09 +02001159 if (msg_cur + sizeof(netinteger) > msg_end) {
1160 /* malformed message */
1161 stksess_free(st->table, newts);
1162 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1163 goto switchstate;
1164 }
1165 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1166 netinteger = ntohl(netinteger);
1167 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1168 msg_cur += sizeof(netinteger);
1169 }
1170 else {
1171 if (msg_cur + st->table->key_size > msg_end) {
1172 /* malformed message */
1173 stksess_free(st->table, newts);
1174 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1175 goto switchstate;
1176 }
1177 memcpy(newts->key.key, msg_cur, st->table->key_size);
1178 msg_cur += st->table->key_size;
1179 }
1180
1181 /* lookup for existing entry */
1182 ts = stktable_lookup(st->table, newts);
1183 if (ts) {
1184 /* the entry already exist, we can free ours */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001185 stktable_touch_with_exp(st->table, ts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001186 stksess_free(st->table, newts);
1187 newts = NULL;
1188 }
1189 else {
1190 struct eb32_node *eb;
1191
1192 /* create new entry */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001193 ts = stktable_store_with_exp(st->table, newts, 0, tick_add(now_ms, expire));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001194 newts = NULL; /* don't reuse it */
1195
Emeric Brun234fc3c2015-12-16 15:16:46 +01001196 ts->upd.key= (++st->table->update)+(2147483648U);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001197 eb = eb32_insert(&st->table->updates, &ts->upd);
1198 if (eb != &ts->upd) {
1199 eb32_delete(eb);
1200 eb32_insert(&st->table->updates, &ts->upd);
1201 }
1202 }
1203
Emeric Brun94900952015-06-11 18:25:54 +02001204 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001205
Emeric Brun94900952015-06-11 18:25:54 +02001206 if ((1 << data_type) & st->remote_data) {
1207 switch (stktable_data_types[data_type].std_type) {
1208 case STD_T_SINT: {
1209 int data;
1210
1211 data = intdecode(&msg_cur, msg_end);
1212 if (!msg_cur) {
1213 /* malformed message */
1214 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1215 goto switchstate;
1216 }
1217
1218 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1219 if (data_ptr)
1220 stktable_data_cast(data_ptr, std_t_sint) = data;
1221 break;
1222 }
1223 case STD_T_UINT: {
1224 unsigned int data;
1225
1226 data = intdecode(&msg_cur, msg_end);
1227 if (!msg_cur) {
1228 /* malformed message */
1229 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1230 goto switchstate;
1231 }
1232
1233 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1234 if (data_ptr)
1235 stktable_data_cast(data_ptr, std_t_uint) = data;
1236 break;
1237 }
1238 case STD_T_ULL: {
1239 unsigned long long data;
1240
1241 data = intdecode(&msg_cur, msg_end);
1242 if (!msg_cur) {
1243 /* malformed message */
1244 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1245 goto switchstate;
1246 }
1247
1248 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1249 if (data_ptr)
1250 stktable_data_cast(data_ptr, std_t_ull) = data;
1251 break;
1252 }
1253 case STD_T_FRQP: {
1254 struct freq_ctr_period data;
1255
Willy Tarreau3bb46172016-03-25 18:17:47 +01001256 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end));
Emeric Brun94900952015-06-11 18:25:54 +02001257 if (!msg_cur) {
1258 /* malformed message */
1259 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1260 goto switchstate;
1261 }
1262 data.curr_ctr = intdecode(&msg_cur, msg_end);
1263 if (!msg_cur) {
1264 /* malformed message */
1265 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1266 goto switchstate;
1267 }
1268 data.prev_ctr = intdecode(&msg_cur, msg_end);
1269 if (!msg_cur) {
1270 /* malformed message */
1271 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1272 goto switchstate;
1273 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001274
Emeric Brun94900952015-06-11 18:25:54 +02001275 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1276 if (data_ptr)
1277 stktable_data_cast(data_ptr, std_t_frqp) = data;
1278 break;
1279 }
1280 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001281 }
1282 }
1283 }
1284 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1285 /* ack message */
1286 uint32_t table_id ;
1287 uint32_t update;
1288 struct shared_table *st;
1289
1290 table_id = intdecode(&msg_cur, msg_end);
1291 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1292 /* malformed message */
1293 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1294 goto switchstate;
1295 }
1296 memcpy(&update, msg_cur, sizeof(update));
1297 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001298
Emeric Brunb3971ab2015-05-12 18:49:09 +02001299 for (st = curpeer->tables; st; st = st->next) {
1300 if (st->local_id == table_id) {
1301 st->update = update;
1302 break;
1303 }
1304 }
1305 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001306 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001307 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1308 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001309 goto switchstate;
1310 }
1311
Emeric Brunb3971ab2015-05-12 18:49:09 +02001312ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001313 /* skip consumed message */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001314 bo_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001315 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001316 goto switchstate;
1317
Emeric Brun2b920a12010-09-23 18:30:22 +02001318incomplete:
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001319 /* we get here when a bo_getblk() returns <= 0 in reql */
1320
Willy Tarreau72d6c162013-04-11 16:14:13 +02001321 if (reql < 0) {
1322 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001323 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001324 goto switchstate;
1325 }
1326
Emeric Brun2b920a12010-09-23 18:30:22 +02001327
Emeric Brun2b920a12010-09-23 18:30:22 +02001328
Emeric Brunb3971ab2015-05-12 18:49:09 +02001329
Emeric Brun2b920a12010-09-23 18:30:22 +02001330 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001331 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1332 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1333 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1334 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001335
Emeric Brunb3971ab2015-05-12 18:49:09 +02001336 /* Current peer was elected to request a resync */
1337 msg[0] = PEER_MSG_CLASS_CONTROL;
1338 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001339
Emeric Brunb3971ab2015-05-12 18:49:09 +02001340 /* message to buffer */
1341 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1342 if (repl <= 0) {
1343 /* no more write possible */
1344 if (repl == -1)
1345 goto full;
1346 appctx->st0 = PEER_SESS_ST_END;
1347 goto switchstate;
1348 }
1349 peers->flags |= PEERS_F_RESYNC_PROCESS;
1350 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001351
Emeric Brunb3971ab2015-05-12 18:49:09 +02001352 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001353
Emeric Brunb3971ab2015-05-12 18:49:09 +02001354 if (curpeer->tables) {
1355 struct shared_table *st;
1356 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001357
Emeric Brunb3971ab2015-05-12 18:49:09 +02001358 last_local_table = curpeer->last_local_table;
1359 if (!last_local_table)
1360 last_local_table = curpeer->tables;
1361 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001362
Emeric Brunb3971ab2015-05-12 18:49:09 +02001363 while (1) {
1364 if (!st)
1365 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001366
Emeric Brunb3971ab2015-05-12 18:49:09 +02001367 /* It remains some updates to ack */
1368 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001369 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001370
Emeric Brunb3971ab2015-05-12 18:49:09 +02001371 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1372 if (!msglen) {
1373 /* internal error: message does not fit in trash */
1374 appctx->st0 = PEER_SESS_ST_END;
1375 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001376 }
1377
Emeric Brunb3971ab2015-05-12 18:49:09 +02001378 /* message to buffer */
1379 repl = bi_putblk(si_ic(si), trash.str, msglen);
1380 if (repl <= 0) {
1381 /* no more write possible */
1382 if (repl == -1) {
1383 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001384 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001385 appctx->st0 = PEER_SESS_ST_END;
1386 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001387 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001388 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001389 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001390
Emeric Brunb3971ab2015-05-12 18:49:09 +02001391 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1392 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1393 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1394 struct eb32_node *eb;
1395 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001396
Emeric Brunb3971ab2015-05-12 18:49:09 +02001397 if (st != curpeer->last_local_table) {
1398 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001399
Emeric Brunb3971ab2015-05-12 18:49:09 +02001400 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1401 if (!msglen) {
1402 /* internal error: message does not fit in trash */
1403 appctx->st0 = PEER_SESS_ST_END;
1404 goto switchstate;
1405 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001406
Emeric Brunb3971ab2015-05-12 18:49:09 +02001407 /* message to buffer */
1408 repl = bi_putblk(si_ic(si), trash.str, msglen);
1409 if (repl <= 0) {
1410 /* no more write possible */
1411 if (repl == -1) {
1412 goto full;
1413 }
1414 appctx->st0 = PEER_SESS_ST_END;
1415 goto switchstate;
1416 }
1417 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001418 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001419
1420 /* We force new pushed to 1 to force identifier in update message */
1421 new_pushed = 1;
1422 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1423 while (1) {
1424 uint32_t msglen;
1425 struct stksess *ts;
1426
1427 /* push local updates */
1428 if (!eb) {
1429 eb = eb32_first(&st->table->updates);
1430 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001431 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001432 break;
1433 }
1434 }
1435
1436 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001437 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001438 break;
1439 }
1440
1441 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001442 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001443 if (!msglen) {
1444 /* internal error: message does not fit in trash */
1445 appctx->st0 = PEER_SESS_ST_END;
1446 goto switchstate;
1447 }
1448
1449 /* message to buffer */
1450 repl = bi_putblk(si_ic(si), trash.str, msglen);
1451 if (repl <= 0) {
1452 /* no more write possible */
1453 if (repl == -1) {
1454 goto full;
1455 }
1456 appctx->st0 = PEER_SESS_ST_END;
1457 goto switchstate;
1458 }
1459 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001460 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1461 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001462 /* identifier may not needed in next update message */
1463 new_pushed = 0;
1464
1465 eb = eb32_next(eb);
1466 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001467 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001468 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001469 else {
1470 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1471 struct eb32_node *eb;
1472 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001473
Emeric Brunb3971ab2015-05-12 18:49:09 +02001474 if (st != curpeer->last_local_table) {
1475 int msglen;
1476
1477 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1478 if (!msglen) {
1479 /* internal error: message does not fit in trash */
1480 appctx->st0 = PEER_SESS_ST_END;
1481 goto switchstate;
1482 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001483
Emeric Brunb3971ab2015-05-12 18:49:09 +02001484 /* message to buffer */
1485 repl = bi_putblk(si_ic(si), trash.str, msglen);
1486 if (repl <= 0) {
1487 /* no more write possible */
1488 if (repl == -1) {
1489 goto full;
1490 }
1491 appctx->st0 = PEER_SESS_ST_END;
1492 goto switchstate;
1493 }
1494 curpeer->last_local_table = st;
1495 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001496
Emeric Brunb3971ab2015-05-12 18:49:09 +02001497 /* We force new pushed to 1 to force identifier in update message */
1498 new_pushed = 1;
1499 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1500 while (1) {
1501 uint32_t msglen;
1502 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001503 int use_timed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001504
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 /* push local updates */
1506 if (!eb) {
1507 st->flags |= SHTABLE_F_TEACH_STAGE1;
1508 eb = eb32_first(&st->table->updates);
1509 if (eb)
1510 st->last_pushed = eb->key - 1;
1511 break;
1512 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001513
Emeric Brunb3971ab2015-05-12 18:49:09 +02001514 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001515 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1516 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001517 if (!msglen) {
1518 /* internal error: message does not fit in trash */
1519 appctx->st0 = PEER_SESS_ST_END;
1520 goto switchstate;
1521 }
1522
1523 /* message to buffer */
1524 repl = bi_putblk(si_ic(si), trash.str, msglen);
1525 if (repl <= 0) {
1526 /* no more write possible */
1527 if (repl == -1) {
1528 goto full;
1529 }
1530 appctx->st0 = PEER_SESS_ST_END;
1531 goto switchstate;
1532 }
1533 st->last_pushed = ts->upd.key;
1534 /* identifier may not needed in next update message */
1535 new_pushed = 0;
1536
1537 eb = eb32_next(eb);
1538 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001539 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001540
Emeric Brunb3971ab2015-05-12 18:49:09 +02001541 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1542 struct eb32_node *eb;
1543 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001544
Emeric Brunb3971ab2015-05-12 18:49:09 +02001545 if (st != curpeer->last_local_table) {
1546 int msglen;
1547
1548 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1549 if (!msglen) {
1550 /* internal error: message does not fit in trash */
1551 appctx->st0 = PEER_SESS_ST_END;
1552 goto switchstate;
1553 }
1554
1555 /* message to buffer */
1556 repl = bi_putblk(si_ic(si), trash.str, msglen);
1557 if (repl <= 0) {
1558 /* no more write possible */
1559 if (repl == -1) {
1560 goto full;
1561 }
1562 appctx->st0 = PEER_SESS_ST_END;
1563 goto switchstate;
1564 }
1565 curpeer->last_local_table = st;
1566 }
1567
1568 /* We force new pushed to 1 to force identifier in update message */
1569 new_pushed = 1;
1570 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1571 while (1) {
1572 uint32_t msglen;
1573 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001574 int use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001575
1576 /* push local updates */
1577 if (!eb || eb->key > st->teaching_origin) {
1578 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001579 break;
1580 }
1581
1582 ts = eb32_entry(eb, struct stksess, upd);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001583 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
1584 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001585 if (!msglen) {
1586 /* internal error: message does not fit in trash */
1587 appctx->st0 = PEER_SESS_ST_END;
1588 goto switchstate;
1589 }
1590
1591 /* message to buffer */
1592 repl = bi_putblk(si_ic(si), trash.str, msglen);
1593 if (repl <= 0) {
1594 /* no more write possible */
1595 if (repl == -1) {
1596 goto full;
1597 }
1598 appctx->st0 = PEER_SESS_ST_END;
1599 goto switchstate;
1600 }
1601 st->last_pushed = ts->upd.key;
1602 /* identifier may not needed in next update message */
1603 new_pushed = 0;
1604
1605 eb = eb32_next(eb);
1606 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001607 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001608 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001609
1610 if (st == last_local_table)
1611 break;
1612 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001613 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001614 }
1615
1616
1617 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1618 unsigned char msg[2];
1619
1620 /* Current peer was elected to request a resync */
1621 msg[0] = PEER_MSG_CLASS_CONTROL;
1622 msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1623 /* process final lesson message */
1624 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1625 if (repl <= 0) {
1626 /* no more write possible */
1627 if (repl == -1)
1628 goto full;
1629 appctx->st0 = PEER_SESS_ST_END;
1630 goto switchstate;
1631 }
1632 /* flag finished message sent */
1633 curpeer->flags |= PEER_F_TEACH_FINISHED;
1634 }
1635
Emeric Brun597b26e2016-08-12 11:23:31 +02001636 /* Confirm finished or partial messages */
1637 while (curpeer->confirm) {
1638 unsigned char msg[2];
1639
1640 /* There is a confirm messages to send */
1641 msg[0] = PEER_MSG_CLASS_CONTROL;
1642 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1643
1644 /* message to buffer */
1645 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1646 if (repl <= 0) {
1647 /* no more write possible */
1648 if (repl == -1)
1649 goto full;
1650 appctx->st0 = PEER_SESS_ST_END;
1651 goto switchstate;
1652 }
1653 curpeer->confirm--;
1654 }
1655
Emeric Brun2b920a12010-09-23 18:30:22 +02001656 /* noting more to do */
1657 goto out;
1658 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001659 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001660 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001661 if (bi_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001662 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001663 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001664 goto switchstate;
1665 case PEER_SESS_ST_ERRSIZE: {
1666 unsigned char msg[2];
1667
1668 msg[0] = PEER_MSG_CLASS_ERROR;
1669 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1670
1671 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1672 goto full;
1673 appctx->st0 = PEER_SESS_ST_END;
1674 goto switchstate;
1675 }
1676 case PEER_SESS_ST_ERRPROTO: {
1677 unsigned char msg[2];
1678
1679 msg[0] = PEER_MSG_CLASS_ERROR;
1680 msg[1] = PEER_MSG_ERR_PROTOCOL;
1681
1682 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1683 goto full;
1684 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001685 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001686 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001687 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001688 si_shutw(si);
1689 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001690 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001691 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001692 }
1693 }
1694 }
1695out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001696 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001697 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001698full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001699 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001700 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001701}
1702
Willy Tarreau30576452015-04-13 13:50:30 +02001703static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001704 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001705 .name = "<PEER>", /* used for logging */
1706 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001707 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001708};
Emeric Brun2b920a12010-09-23 18:30:22 +02001709
1710/*
1711 * Use this function to force a close of a peer session
1712 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001713static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001714{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001715 struct peer *ps;
1716
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001717 if (!appctx)
1718 return;
1719
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001720 if (appctx->applet != &peer_applet)
1721 return;
1722
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001723 ps = appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001724 /* we're killing a connection, we must apply a random delay before
1725 * retrying otherwise the other end will do the same and we can loop
1726 * for a while.
1727 */
1728 if (ps)
1729 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1730
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001731 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001732 appctx->ctx.peers.ptr = NULL;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001733 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001734}
1735
Willy Tarreau91d96282015-03-13 15:47:26 +01001736/* Pre-configures a peers frontend to accept incoming connections */
1737void peers_setup_frontend(struct proxy *fe)
1738{
1739 fe->last_change = now.tv_sec;
1740 fe->cap = PR_CAP_FE;
1741 fe->maxconn = 0;
1742 fe->conn_retries = CONN_RETRIES;
1743 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001744 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001745 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001746 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001747 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001748}
1749
Emeric Brun2b920a12010-09-23 18:30:22 +02001750/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001751 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001752 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001753static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001754{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001755 struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe);
Willy Tarreauc95bad52016-12-22 00:13:31 +01001756 struct proxy *p = l->bind_conf->frontend; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001757 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001758 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001759 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001760 struct task *t;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001761 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001762
Emeric Brunb3971ab2015-05-12 18:49:09 +02001763 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1764 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001765 s = NULL;
1766
1767 appctx = appctx_new(&peer_applet);
1768 if (!appctx)
1769 goto out_close;
1770
1771 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001772 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001773
Willy Tarreau4099a7c2015-04-05 00:39:55 +02001774 sess = session_new(p, l, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001775 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001776 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001777 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001778 }
1779
Willy Tarreau8baf9062015-04-05 00:46:36 +02001780 if ((t = task_new()) == NULL) {
Willy Tarreau15b5e142015-04-04 14:38:25 +02001781 Alert("out of memory in peer_session_create().\n");
1782 goto out_free_sess;
1783 }
Willy Tarreau8baf9062015-04-05 00:46:36 +02001784 t->nice = l->nice;
1785
Willy Tarreau73b65ac2015-04-08 18:26:29 +02001786 if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001787 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau8baf9062015-04-05 00:46:36 +02001788 goto out_free_task;
1789 }
1790
Willy Tarreau342bfb12015-04-05 01:35:34 +02001791 /* The tasks below are normally what is supposed to be done by
1792 * fe->accept().
1793 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001794 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001795
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001796 /* applet is waiting for data */
1797 si_applet_cant_get(&s->si[0]);
1798 appctx_wakeup(appctx);
1799
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001800 /* initiate an outgoing connection */
1801 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001802
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001803 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001804 * pre-initialized connection in si->conn.
1805 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001806 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001807 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001808
1809 conn_prepare(conn, peer->proto, peer->xprt);
1810 si_attach_conn(&s->si[1], conn);
1811
1812 conn->target = s->target = &s->be->obj_type;
1813 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001814 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001815 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001816
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001817 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001818
Emeric Brun2b920a12010-09-23 18:30:22 +02001819 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1820 p->feconn++;/* beconn will be increased later */
1821 jobs++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001822 if (!(s->sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001823 actconn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001824 totalconn++;
1825
Emeric Brunb3971ab2015-05-12 18:49:09 +02001826 peer->appctx = appctx;
Willy Tarreau9df94c22016-10-31 18:42:52 +01001827 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001828
1829 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001830 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001831 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001832 pool_free2(pool2_stream, s);
Willy Tarreau8baf9062015-04-05 00:46:36 +02001833 out_free_task:
1834 task_free(t);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001835 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001836 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001837 out_free_appctx:
1838 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001839 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01001840 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001841}
1842
1843/*
1844 * Task processing function to manage re-connect and peer session
1845 * tasks wakeup on local update.
1846 */
Simon Horman96553772011-06-08 09:18:51 +09001847static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001848{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001849 struct peers *peers = task->context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001850 struct peer *ps;
1851 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001852
1853 task->expire = TICK_ETERNITY;
1854
Emeric Brunb3971ab2015-05-12 18:49:09 +02001855 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001856 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001857 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001858 task_delete(peers->sync_task);
1859 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02001860 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001861 return NULL;
1862 }
1863
Emeric Brun2b920a12010-09-23 18:30:22 +02001864 if (!stopping) {
1865 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001866
1867 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1868 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1869 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001870 /* Resync from local peer needed
1871 no peer was assigned for the lesson
1872 and no old local peer found
1873 or resync timeout expire */
1874
1875 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001876 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001877
1878 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001879 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001880 }
1881
1882 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001883 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001884 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001885 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001886 if (!ps->appctx) {
1887 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001888 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001889 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001890 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001891 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001892 tick_is_expired(ps->reconnect, now_ms))) {
1893 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001894 * or previous peer connection established with success
1895 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02001896 * and reconnection timer is expired */
1897
1898 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001899 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001900 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001901 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001902 /* If previous session failed during connection
1903 * but reconnection timer is not expired */
1904
1905 /* reschedule task for reconnect */
1906 task->expire = tick_first(task->expire, ps->reconnect);
1907 }
1908 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001909 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001910 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001911 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001912 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1913 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001914 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1915 /* Resync from a remote is needed
1916 * and no peer was assigned for lesson
1917 * and current peer may be up2date */
1918
1919 /* assign peer for the lesson */
1920 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001921 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001922
Willy Tarreau9df94c22016-10-31 18:42:52 +01001923 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001924 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001925 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001926 else {
1927 /* Awake session if there is data to push */
1928 for (st = ps->tables; st ; st = st->next) {
1929 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001930 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001931 appctx_wakeup(ps->appctx);
1932 break;
1933 }
1934 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001935 }
1936 /* else do nothing */
1937 } /* SUCCESSCODE */
1938 } /* !ps->peer->local */
1939 } /* for */
1940
1941 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001942 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1943 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1944 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001945 /* Resync from remote peer needed
1946 * no peer was assigned for the lesson
1947 * and resync timeout expire */
1948
1949 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001950 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001951 }
1952
Emeric Brunb3971ab2015-05-12 18:49:09 +02001953 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001954 /* Resync not finished*/
1955 /* reschedule task to resync timeout, to ended resync if needed */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001956 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001957 }
1958 } /* !stopping */
1959 else {
1960 /* soft stop case */
1961 if (task->state & TASK_WOKEN_SIGNAL) {
1962 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001963 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001964 /* add DO NOT STOP flag if not present */
1965 jobs++;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001966 peers->flags |= PEERS_F_DONOTSTOP;
1967 ps = peers->local;
1968 for (st = ps->tables; st ; st = st->next)
1969 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001970 }
1971
1972 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001973 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01001974 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001975 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02001976 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001977 }
1978 }
1979 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001980
Emeric Brunb3971ab2015-05-12 18:49:09 +02001981 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001982 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001983 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001984 /* resync of new process was complete, current process can die now */
1985 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001986 peers->flags &= ~PEERS_F_DONOTSTOP;
1987 for (st = ps->tables; st ; st = st->next)
1988 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001989 }
1990 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01001991 else if (!ps->appctx) {
1992 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02001993 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001994 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
1995 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
1996 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001997 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01001998 * or previous peer connection was successfully established
1999 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002000 * or during previous connect, peer replies a try again statuscode */
2001
2002 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002003 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002004 }
2005 else {
2006 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002007 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002008 /* unable to resync new process, current process can die now */
2009 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002010 peers->flags &= ~PEERS_F_DONOTSTOP;
2011 for (st = ps->tables; st ; st = st->next)
2012 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002013 }
2014 }
2015 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002016 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002017 /* current peer connection is active and established
2018 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002019 for (st = ps->tables; st ; st = st->next) {
2020 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002021 appctx_wakeup(ps->appctx);
2022 break;
2023 }
2024 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002025 }
2026 } /* stopping */
2027 /* Wakeup for re-connect */
2028 return task;
2029}
2030
Emeric Brunb3971ab2015-05-12 18:49:09 +02002031
Emeric Brun2b920a12010-09-23 18:30:22 +02002032/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002033 *
2034 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002035void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002036{
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002038 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002039
Emeric Brun2b920a12010-09-23 18:30:22 +02002040 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002041 peers->peers_fe->maxconn += 3;
2042 }
2043
Willy Tarreau4348fad2012-09-20 16:48:07 +02002044 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2045 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002046 peers->sync_task = task_new();
2047 peers->sync_task->process = process_peer_sync;
2048 peers->sync_task->expire = TICK_ETERNITY;
2049 peers->sync_task->context = (void *)peers;
2050 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2051 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
2052}
2053
2054
2055
2056/*
2057 * Function used to register a table for sync on a group of peers
2058 *
2059 */
2060void peers_register_table(struct peers *peers, struct stktable *table)
2061{
2062 struct shared_table *st;
2063 struct peer * curpeer;
2064 int id = 0;
2065
2066 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002067 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 st->table = table;
2069 st->next = curpeer->tables;
2070 if (curpeer->tables)
2071 id = curpeer->tables->local_id;
2072 st->local_id = id + 1;
2073
2074 curpeer->tables = st;
2075 }
2076
2077 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002078}
2079