blob: 19b8824121579dff10c249bd63ef6c2903a5cbc7 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002 * Stick table 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>
26
27#include <types/global.h>
28#include <types/peers.h>
29
30#include <proto/acl.h>
31#include <proto/buffers.h>
32#include <proto/fd.h>
33#include <proto/log.h>
34#include <proto/hdr_idx.h>
35#include <proto/pattern.h>
36#include <proto/protocols.h>
37#include <proto/proto_tcp.h>
38#include <proto/proto_http.h>
39#include <proto/proxy.h>
40#include <proto/session.h>
41#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
44#include <proto/stick_table.h>
45#include <proto/signal.h>
46
47
48/*******************************/
49/* Current peer learning state */
50/*******************************/
51
52/******************************/
53/* Current table resync state */
54/******************************/
55#define SHTABLE_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
56#define SHTABLE_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
57#define SHTABLE_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
58#define SHTABLE_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
59#define SHTABLE_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
60 to push data to new process */
61
62#define SHTABLE_RESYNC_STATEMASK (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE)
63#define SHTABLE_RESYNC_FROMLOCAL 0x00000000
64#define SHTABLE_RESYNC_FROMREMOTE SHTABLE_F_RESYNC_LOCAL
65#define SHTABLE_RESYNC_FINISHED (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE)
66
67/******************************/
68/* Remote peer teaching state */
69/******************************/
70#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
71#define PEER_F_TEACH_STAGE1 0x00000002 /* Teach state 1 complete */
72#define PEER_F_TEACH_STAGE2 0x00000004 /* Teach stage 2 complete */
73#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
74#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
75#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
76#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
77
78#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_STAGE1|PEER_F_TEACH_STAGE2|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
79#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
80
81
82/**********************************/
83/* Peer Session IO handler states */
84/**********************************/
85
86#define PEER_SESSION_ACCEPT 1000 /* Initial state for session create by an accept */
87#define PEER_SESSION_GETVERSION 1001 /* Validate supported protocol version*/
88#define PEER_SESSION_GETHOST 1002 /* Validate host ID correspond to local host id */
89#define PEER_SESSION_GETPEER 1003 /* Validate peer ID correspond to a known remote peer id */
90#define PEER_SESSION_GETTABLE 1004 /* Search into registered table for a table with same id and
91 validate type and size */
92#define PEER_SESSION_SENDSUCCESS 1005 /* Send ret code 200 (success) and wait for message */
93/* next state is WAITMSG */
94
95#define PEER_SESSION_CONNECT 2000 /* Initial state for session create on a connect,
96 push presentation into buffer */
97#define PEER_SESSION_GETSTATUS 2001 /* Wait for the welcome message */
98#define PEER_SESSION_WAITMSG 2002 /* Wait for datamessages*/
99/* loop on WAITMSG */
100
101#define PEER_SESSION_EXIT 10000 /* Exit with status code */
102#define PEER_SESSION_END 10001 /* Killed session */
103/* session ended */
104
105
106/**********************************/
107/* Peer Session status code */
108/**********************************/
109
110#define PEER_SESSION_CONNECTCODE 100 /* connect in progress */
111#define PEER_SESSION_CONNECTEDCODE 110 /* tcp connect success */
112
113#define PEER_SESSION_SUCCESSCODE 200 /* accept or connect successful */
114
115#define PEER_SESSION_TRYAGAIN 300 /* try again later */
116
117#define PEER_SESSION_ERRPROTO 501 /* error protocol */
118#define PEER_SESSION_ERRVERSION 502 /* unknown protocol version */
119#define PEER_SESSION_ERRHOST 503 /* bad host name */
120#define PEER_SESSION_ERRPEER 504 /* unknown peer */
121#define PEER_SESSION_ERRTYPE 505 /* table key type mismatch */
122#define PEER_SESSION_ERRSIZE 506 /* table key size mismatch */
123#define PEER_SESSION_ERRTABLE 507 /* unknown table */
124
125#define PEER_SESSION_PROTO_NAME "HAProxyS"
126
127struct peers *peers = NULL;
128void peer_session_forceshutdown(struct session * session);
129
130
131/*
132 * This prepare the data update message of the stick session <ts>, <ps> is the the peer session
133 * where the data going to be pushed, <msg> is a buffer of <size> to recieve data message content
134 */
135int peer_prepare_datamsg(struct stksess *ts, struct peer_session *ps, char *msg, size_t size)
136{
137 uint32_t netinteger;
138 int len;
139 /* construct message */
140 if (ps->lastpush && ts->upd.key > ps->lastpush && (ts->upd.key - ps->lastpush) <= 127) {
141 msg[0] = 0x80 + ts->upd.key - ps->lastpush;
142 len = sizeof(char);
143 }
144 else {
145 msg[0] = 'D';
146 netinteger = htonl(ts->upd.key);
147 memcpy(&msg[sizeof(char)], &netinteger, sizeof(netinteger));
148 len = sizeof(char) + sizeof(netinteger);
149 }
150
151 if (ps->table->table->type == STKTABLE_TYPE_STRING) {
152 int stlen = strlen((char *)ts->key.key);
153
154 netinteger = htonl(strlen((char *)ts->key.key));
155 memcpy(&msg[len], &netinteger, sizeof(netinteger));
156 memcpy(&msg[len+sizeof(netinteger)], ts->key.key, stlen);
157 len += sizeof(netinteger) + stlen;
158
159 }
160 else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) {
161 netinteger = htonl(*((uint32_t *)ts->key.key));
162 memcpy(&msg[len], &netinteger, sizeof(netinteger));
163 len += sizeof(netinteger);
164 }
165 else {
166 memcpy(&msg[len], ts->key.key, ps->table->table->key_size);
167 len += ps->table->table->key_size;
168 }
169
170 if (stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID))
171 netinteger = htonl(stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id));
172 else
173 netinteger = 0;
174
175 memcpy(&msg[len], &netinteger , sizeof(netinteger));
176 len += sizeof(netinteger);
177
178 return len;
179}
180
181
182/*
183 * Callback to release a session with a peer
184 */
185void peer_session_release(struct stream_interface *si)
186{
187 struct task *t= (struct task *)si->owner;
188 struct session *s = (struct session *)t->context;
189 struct peer_session *ps = (struct peer_session *)si->private;
190
191 /* si->private is not a peer session */
192 if (si->st0 < PEER_SESSION_SENDSUCCESS)
193 return;
194
195 /* peer session identified */
196 if (ps) {
197 if (ps->session == s) {
198 ps->session = NULL;
199 if (ps->flags & PEER_F_LEARN_ASSIGN) {
200 /* unassign current peer for learning */
201 ps->flags &= ~(PEER_F_LEARN_ASSIGN);
202 ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS);
203
204 /* reschedule a resync */
205 ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
206 }
207 /* reset teaching and learning flags to 0 */
208 ps->flags &= PEER_TEACH_RESET;
209 ps->flags &= PEER_LEARN_RESET;
210 }
211 task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG);
212 }
213}
214
215
216/*
217 * IO Handler to handle message exchance with a peer
218 */
219void peer_io_handler(struct stream_interface *si)
220{
221 struct task *t= (struct task *)si->owner;
222 struct session *s = (struct session *)t->context;
223 struct peers *curpeers = (struct peers *)s->fe->parent;
224 int reql = 0;
225 int repl = 0;
226
227 while (1) {
228switchstate:
229 switch(si->st0) {
230 case PEER_SESSION_ACCEPT:
231 si->private = NULL;
232 si->st0 = PEER_SESSION_GETVERSION;
233 /* fall through */
234 case PEER_SESSION_GETVERSION:
235 reql = buffer_get_line(si->ob, trash, sizeof(trash));
236 if (reql <= 0) { /* closed or EOL not found */
237 if (reql == 0)
238 goto out;
239 si->st0 = PEER_SESSION_END;
240 goto switchstate;
241 }
242 if (trash[reql-1] != '\n') {
243 si->st0 = PEER_SESSION_END;
244 goto switchstate;
245 }
246 else if (reql > 1 && (trash[reql-2] == '\r'))
247 trash[reql-2] = 0;
248 else
249 trash[reql-1] = 0;
250
251 buffer_skip(si->ob, reql);
252
253 /* test version */
254 if (strcmp(PEER_SESSION_PROTO_NAME " 1.0", trash) != 0) {
255 si->st0 = PEER_SESSION_EXIT;
256 si->st1 = PEER_SESSION_ERRVERSION;
257 /* test protocol */
258 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash, strlen(PEER_SESSION_PROTO_NAME)+1) != 0)
259 si->st1 = PEER_SESSION_ERRPROTO;
260 goto switchstate;
261 }
262
263 si->st0 = PEER_SESSION_GETHOST;
264 /* fall through */
265 case PEER_SESSION_GETHOST:
266 reql = buffer_get_line(si->ob, trash, sizeof(trash));
267 if (reql <= 0) { /* closed or EOL not found */
268 if (reql == 0)
269 goto out;
270 si->st0 = PEER_SESSION_END;
271 goto switchstate;
272 }
273 if (trash[reql-1] != '\n') {
274 si->st0 = PEER_SESSION_END;
275 goto switchstate;
276 }
277 else if (reql > 1 && (trash[reql-2] == '\r'))
278 trash[reql-2] = 0;
279 else
280 trash[reql-1] = 0;
281
282 buffer_skip(si->ob, reql);
283
284 /* test hostname match */
285 if (strcmp(localpeer, trash) != 0) {
286 si->st0 = PEER_SESSION_EXIT;
287 si->st1 = PEER_SESSION_ERRHOST;
288 goto switchstate;
289 }
290
291 si->st0 = PEER_SESSION_GETPEER;
292 /* fall through */
293 case PEER_SESSION_GETPEER: {
294 struct peer *curpeer;
295 char *p;
296 reql = buffer_get_line(si->ob, trash, sizeof(trash));
297 if (reql <= 0) { /* closed or EOL not found */
298 if (reql == 0)
299 goto out;
300 si->st0 = PEER_SESSION_END;
301 goto switchstate;
302 }
303 if (trash[reql-1] != '\n') {
304 /* Incomplete line, we quit */
305 si->st0 = PEER_SESSION_END;
306 goto switchstate;
307 }
308 else if (reql > 1 && (trash[reql-2] == '\r'))
309 trash[reql-2] = 0;
310 else
311 trash[reql-1] = 0;
312
313 buffer_skip(si->ob, reql);
314
315 /* parse line "<peer name> <pid>" */
316 p = strchr(trash, ' ');
317 if (!p) {
318 si->st0 = PEER_SESSION_EXIT;
319 si->st1 = PEER_SESSION_ERRPROTO;
320 goto switchstate;
321 }
322 *p = 0;
323
324 /* lookup known peer */
325 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
326 if (strcmp(curpeer->id, trash) == 0)
327 break;
328 }
329
330 /* if unknown peer */
331 if (!curpeer) {
332 si->st0 = PEER_SESSION_EXIT;
333 si->st1 = PEER_SESSION_ERRPEER;
334 goto switchstate;
335 }
336
337 si->private = curpeer;
338 si->st0 = PEER_SESSION_GETTABLE;
339 /* fall through */
340 }
341 case PEER_SESSION_GETTABLE: {
342 struct peer *curpeer = (struct peer *)si->private;
343 struct shared_table *st;
344 struct peer_session *ps = NULL;
345 unsigned long key_type;
346 size_t key_size;
347 char *p;
348
349 reql = buffer_get_line(si->ob, trash, sizeof(trash));
350 if (reql <= 0) { /* closed or EOL not found */
351 if (reql == 0)
352 goto out;
353 si->private = NULL;
354 si->st0 = PEER_SESSION_END;
355 goto switchstate;
356 }
357 /* Re init si->private to null, to handle correctly a release case */
358 si->private = NULL;
359
360 if (trash[reql-1] != '\n') {
361 /* Incomplete line, we quit */
362 si->st0 = PEER_SESSION_END;
363 goto switchstate;
364 }
365 else if (reql > 1 && (trash[reql-2] == '\r'))
366 trash[reql-2] = 0;
367 else
368 trash[reql-1] = 0;
369
370 buffer_skip(si->ob, reql);
371
372 /* Parse line "<table name> <type> <size>" */
373 p = strchr(trash, ' ');
374 if (!p) {
375 si->st0 = PEER_SESSION_EXIT;
376 si->st1 = PEER_SESSION_ERRPROTO;
377 goto switchstate;
378 }
379 *p = 0;
380 key_type = (unsigned long)atol(p+1);
381
382 p = strchr(p+1, ' ');
383 if (!p) {
384 si->private = NULL;
385 si->st0 = PEER_SESSION_EXIT;
386 si->st1 = PEER_SESSION_ERRPROTO;
387 goto switchstate;
388 }
389
390 key_size = (size_t)atoi(p);
391 for (st = curpeers->tables; st; st = st->next) {
392 /* If table name matches */
393 if (strcmp(st->table->id, trash) == 0) {
394 /* If key size mismatches */
395 if (key_size != st->table->key_size) {
396 si->st0 = PEER_SESSION_EXIT;
397 si->st1 = PEER_SESSION_ERRSIZE;
398 goto switchstate;
399 }
400
401 /* If key type mismatches */
402 if (key_type != st->table->type) {
403 si->st0 = PEER_SESSION_EXIT;
404 si->st1 = PEER_SESSION_ERRTYPE;
405 goto switchstate;
406 }
407
408 /* lookup peer session of current peer */
409 for (ps = st->sessions; ps; ps = ps->next) {
410 if (ps->peer == curpeer) {
411 /* If session already active, replaced by new one */
412 if (ps->session && ps->session != s) {
413 if (ps->peer->local) {
414 /* Local connection, reply a retry */
415 si->st0 = PEER_SESSION_EXIT;
416 si->st1 = PEER_SESSION_TRYAGAIN;
417 goto switchstate;
418 }
419 peer_session_forceshutdown(ps->session);
420 }
421 ps->session = s;
422 break;
423 }
424 }
425 break;
426 }
427 }
428
429 /* If table not found */
430 if (!st){
431 si->st0 = PEER_SESSION_EXIT;
432 si->st1 = PEER_SESSION_ERRTABLE;
433 goto switchstate;
434 }
435
436 /* If no peer session for current peer */
437 if (!ps) {
438 si->st0 = PEER_SESSION_EXIT;
439 si->st1 = PEER_SESSION_ERRPEER;
440 goto switchstate;
441 }
442
443 si->private = ps;
444 si->st0 = PEER_SESSION_SENDSUCCESS;
445 /* fall through */
446 }
447 case PEER_SESSION_SENDSUCCESS:{
448 struct peer_session *ps = (struct peer_session *)si->private;
449
450 repl = snprintf(trash, sizeof(trash), "%d\n", PEER_SESSION_SUCCESSCODE);
451 repl = buffer_put_block(si->ib, trash, repl);
452 if (repl <= 0) {
453 if (repl == -1)
454 goto out;
455 si->st0 = PEER_SESSION_END;
456 goto switchstate;
457 }
458
459 /* Register status code */
460 ps->statuscode = PEER_SESSION_SUCCESSCODE;
461
462 /* Awake main task */
463 task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG);
464
465 /* Init cursors */
466 ps->teaching_origin =ps->lastpush = ps->lastack = ps->pushack = 0;
467 ps->pushed = ps->update;
468
469 /* Init confirm counter */
470 ps->confirm = 0;
471
472 /* reset teaching and learning flags to 0 */
473 ps->flags &= PEER_TEACH_RESET;
474 ps->flags &= PEER_LEARN_RESET;
475
476 /* if current peer is local */
477 if (ps->peer->local) {
478 /* if table need resyncfrom local and no process assined */
479 if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL &&
480 !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) {
481 /* assign local peer for a lesson, consider lesson already requested */
482 ps->flags |= PEER_F_LEARN_ASSIGN;
483 ps->table->flags |= (SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS);
484 }
485
486 }
487 else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE &&
488 !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) {
489 /* assign peer for a lesson */
490 ps->flags |= PEER_F_LEARN_ASSIGN;
491 ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN;
492 }
493 /* switch to waiting message state */
494 si->st0 = PEER_SESSION_WAITMSG;
495 goto switchstate;
496 }
497 case PEER_SESSION_CONNECT: {
498 struct peer_session *ps = (struct peer_session *)si->private;
499
500 /* Send headers */
501 repl = snprintf(trash, sizeof(trash),
502 PEER_SESSION_PROTO_NAME " 1.0\n%s\n%s %d\n%s %lu %d\n",
503 ps->peer->id,
504 localpeer,
505 getpid(),
506 ps->table->table->id,
507 ps->table->table->type,
Willy Tarreaubd55e312010-11-11 10:55:09 +0100508 (int)ps->table->table->key_size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200509
510 if (repl >= sizeof(trash)) {
511 si->st0 = PEER_SESSION_END;
512 goto switchstate;
513 }
514
515 repl = buffer_put_block(si->ib, trash, repl);
516 if (repl <= 0) {
517 if (repl == -1)
518 goto out;
519 si->st0 = PEER_SESSION_END;
520 goto switchstate;
521 }
522
523 /* switch to the waiting statuscode state */
524 si->st0 = PEER_SESSION_GETSTATUS;
525 /* fall through */
526 }
527 case PEER_SESSION_GETSTATUS: {
528 struct peer_session *ps = (struct peer_session *)si->private;
529
530 if (si->ib->flags & BF_WRITE_PARTIAL)
531 ps->statuscode = PEER_SESSION_CONNECTEDCODE;
532
533 reql = buffer_get_line(si->ob, trash, sizeof(trash));
534 if (reql <= 0) { /* closed or EOL not found */
535 if (reql == 0)
536 goto out;
537 si->st0 = PEER_SESSION_END;
538 goto switchstate;
539 }
540 if (trash[reql-1] != '\n') {
541 /* Incomplete line, we quit */
542 si->st0 = PEER_SESSION_END;
543 goto switchstate;
544 }
545 else if (reql > 1 && (trash[reql-2] == '\r'))
546 trash[reql-2] = 0;
547 else
548 trash[reql-1] = 0;
549
550 buffer_skip(si->ob, reql);
551
552 /* Register status code */
553 ps->statuscode = atoi(trash);
554
555 /* Awake main task */
556 task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG);
557
558 /* If status code is success */
559 if (ps->statuscode == PEER_SESSION_SUCCESSCODE) {
560 /* Init cursors */
561 ps->teaching_origin = ps->lastpush = ps->lastack = ps->pushack = 0;
562 ps->pushed = ps->update;
563
564 /* Init confirm counter */
565 ps->confirm = 0;
566
567 /* reset teaching and learning flags to 0 */
568 ps->flags &= PEER_TEACH_RESET;
569 ps->flags &= PEER_LEARN_RESET;
570
571 /* If current peer is local */
572 if (ps->peer->local) {
573 /* Init cursors to push a resync */
574 ps->teaching_origin = ps->pushed = ps->table->table->update;
575 /* flag to start to teach lesson */
576 ps->flags |= PEER_F_TEACH_PROCESS;
577
578 }
579 else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE &&
580 !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) {
581 /* If peer is remote and resync from remote is needed,
582 and no peer currently assigned */
583
584 /* assign peer for a lesson */
585 ps->flags |= PEER_F_LEARN_ASSIGN;
586 ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN;
587 }
588
589 }
590 else {
591 /* Status code is not success, abort */
592 si->st0 = PEER_SESSION_END;
593 goto switchstate;
594 }
595 si->st0 = PEER_SESSION_WAITMSG;
596 /* fall through */
597 }
598 case PEER_SESSION_WAITMSG: {
599 struct peer_session *ps = (struct peer_session *)si->private;
600 char c;
601 int totl = 0;
602
603 reql = buffer_get_block(si->ob, (char *)&c, sizeof(c), totl);
604 if (reql <= 0) { /* closed or EOL not found */
605 if (reql == 0) {
606 /* nothing to read */
607 goto incomplete;
608 }
609 si->st0 = PEER_SESSION_END;
610 goto switchstate;
611 }
612 totl += reql;
613
614 if ((c & 0x80) || (c == 'D')) {
615 /* Here we have data message */
616 unsigned int pushack;
617 struct stksess *ts;
618 struct stksess *newts;
619 struct stktable_key stkey;
620 int srvid;
621 uint32_t netinteger;
622
623 /* Compute update remote version */
624 if (c & 0x80) {
625 pushack = ps->pushack + (unsigned int)(c & 0x7F);
626 }
627 else {
628 reql = buffer_get_block(si->ob, (char *)&netinteger, sizeof(netinteger), totl);
629 if (reql <= 0) { /* closed or EOL not found */
630 if (reql == 0) {
631 goto incomplete;
632 }
633 si->st0 = PEER_SESSION_END;
634 goto switchstate;
635 }
636 totl += reql;
637 pushack = ntohl(netinteger);
638 }
639
640 /* read key */
641 if (ps->table->table->type == STKTABLE_TYPE_STRING) {
642 /* type string */
643 stkey.key = stkey.data.buf;
644
645 reql = buffer_get_block(si->ob, (char *)&netinteger, sizeof(netinteger), totl);
646 if (reql <= 0) { /* closed or EOL not found */
647 if (reql == 0) {
648 goto incomplete;
649 }
650 si->st0 = PEER_SESSION_END;
651 goto switchstate;
652 }
653 totl += reql;
654 stkey.key_len = ntohl(netinteger);
655
656 reql = buffer_get_block(si->ob, stkey.key, stkey.key_len, totl);
657 if (reql <= 0) { /* closed or EOL not found */
658 if (reql == 0) {
659 goto incomplete;
660 }
661 si->st0 = PEER_SESSION_END;
662 goto switchstate;
663 }
664 totl += reql;
665 }
666 else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) {
667 /* type integer */
668 stkey.key_len = (size_t)-1;
669 stkey.key = &stkey.data.integer;
670
671 reql = buffer_get_block(si->ob, (char *)&netinteger, sizeof(netinteger), totl);
672 if (reql <= 0) { /* closed or EOL not found */
673 if (reql == 0) {
674 goto incomplete;
675 }
676 si->st0 = PEER_SESSION_END;
677 goto switchstate;
678 }
679 totl += reql;
680 stkey.data.integer = ntohl(netinteger);
681 }
682 else {
683 /* type ip */
684 stkey.key_len = (size_t)-1;
685 stkey.key = stkey.data.buf;
686
687 reql = buffer_get_block(si->ob, (char *)&stkey.data.buf, ps->table->table->key_size, totl);
688 if (reql <= 0) { /* closed or EOL not found */
689 if (reql == 0) {
690 goto incomplete;
691 }
692 si->st0 = PEER_SESSION_END;
693 goto switchstate;
694 }
695 totl += reql;
696
697 }
698
699 /* read server id */
700 reql = buffer_get_block(si->ob, (char *)&netinteger, sizeof(netinteger), totl);
701 if (reql <= 0) { /* closed or EOL not found */
702 if (reql == 0) {
703 goto incomplete;
704 }
705 si->st0 = PEER_SESSION_END;
706 goto switchstate;
707 }
708 totl += reql;
709 srvid = ntohl(netinteger);
710
711 /* update entry */
712 newts = stksess_new(ps->table->table, &stkey);
713 if (newts) {
714 /* lookup for existing entry */
715 ts = stktable_lookup(ps->table->table, newts);
716 if (ts) {
717 /* the entry already exist, we can free ours */
718 stktable_touch(ps->table->table, ts, 0);
719 stksess_free(ps->table->table, newts);
720 }
721 else {
722 struct eb32_node *eb;
723
724 /* create new entry */
725 ts = stktable_store(ps->table->table, newts, 0);
726 ts->upd.key= (++ps->table->table->update)+(2^31);
727 eb = eb32_insert(&ps->table->table->updates, &ts->upd);
728 if (eb != &ts->upd) {
729 eb32_delete(eb);
730 eb32_insert(&ps->table->table->updates, &ts->upd);
731 }
732 }
733
734 /* update entry */
735 if (srvid && stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID))
736 stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid;
737 ps->pushack = pushack;
738 }
739
740 }
741 else if (c == 'R') {
742 /* Reset message: remote need resync */
743
744 /* reinit counters for a resync */
745 ps->lastpush = 0;
746 ps->teaching_origin = ps->pushed = ps->table->table->update;
747
748 /* reset teaching flags to 0 */
749 ps->flags &= PEER_TEACH_RESET;
750
751 /* flag to start to teach lesson */
752 ps->flags |= PEER_F_TEACH_PROCESS;
753 }
754 else if (c == 'F') {
755 /* Finish message, all known updates have been pushed by remote */
756 /* and remote is up to date */
757
758 /* If resync is in progress with remote peer */
759 if (ps->flags & PEER_F_LEARN_ASSIGN) {
760
761 /* unassign current peer for learning */
762 ps->flags &= ~PEER_F_LEARN_ASSIGN;
763 ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS);
764
765 /* Consider table is now up2date, resync resync no more needed from local neither remote */
766 ps->table->flags |= (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE);
767 }
768 /* Increase confirm counter to launch a confirm message */
769 ps->confirm++;
770 }
771 else if (c == 'c') {
772 /* confirm message, remote peer is now up to date with us */
773
774 /* If stopping state */
775 if (stopping) {
776 /* Close session, push resync no more needed */
777 ps->flags |= PEER_F_TEACH_COMPLETE;
778 si->st0 = PEER_SESSION_END;
779 goto switchstate;
780 }
781
782 /* reset teaching flags to 0 */
783 ps->flags &= PEER_TEACH_RESET;
784 }
785 else if (c == 'C') {
786 /* Continue message, all known updates have been pushed by remote */
787 /* but remote is not up to date */
788
789 /* If resync is in progress with current peer */
790 if (ps->flags & PEER_F_LEARN_ASSIGN) {
791
792 /* unassign current peer */
793 ps->flags &= ~PEER_F_LEARN_ASSIGN;
794 ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS);
795
796 /* flag current peer is not up 2 date to try from an other */
797 ps->flags |= PEER_F_LEARN_NOTUP2DATE;
798
799 /* reschedule a resync */
800 ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
801 task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG);
802 }
803 ps->confirm++;
804 }
805 else if (c == 'A') {
806 /* ack message */
807 uint32_t netinteger;
808
809 reql = buffer_get_block(si->ob, (char *)&netinteger, sizeof(netinteger), totl);
810 if (reql <= 0) { /* closed or EOL not found */
811 if (reql == 0) {
812 goto incomplete;
813 }
814 si->st0 = PEER_SESSION_END;
815 goto switchstate;
816 }
817 totl += reql;
818
819 /* Consider remote is up to date with "acked" version */
820 ps->update = ntohl(netinteger);
821 }
822 else {
823 /* Unknown message */
824 si->st0 = PEER_SESSION_END;
825 goto switchstate;
826 }
827
828 /* skip consumed message */
829 buffer_skip(si->ob, totl);
830
831 /* loop on that state to peek next message */
832 continue;
833incomplete:
834 /* Nothing to read, now we start to write */
835
836 /* Confirm finished or partial messages */
837 while (ps->confirm) {
838 /* There is a confirm messages to send */
839 repl = buffer_put_char(si->ib, 'c');
840 if (repl <= 0) {
841 /* no more write possible */
842 if (repl == -1)
843 goto out;
844 si->st0 = PEER_SESSION_END;
845 goto switchstate;
846 }
847 ps->confirm--;
848 }
849
850 /* Need to request a resync */
851 if ((ps->flags & PEER_F_LEARN_ASSIGN) &&
852 (ps->table->flags & SHTABLE_F_RESYNC_ASSIGN) &&
853 !(ps->table->flags & SHTABLE_F_RESYNC_PROCESS)) {
854 /* Current peer was elected to request a resync */
855
856 repl = buffer_put_char(si->ib, 'R');
857 if (repl <= 0) {
858 /* no more write possible */
859 if (repl == -1)
860 goto out;
861 si->st0 = PEER_SESSION_END;
862 goto switchstate;
863 }
864 ps->table->flags |= SHTABLE_F_RESYNC_PROCESS;
865 }
866
867 /* It remains some updates to ack */
868 if (ps->pushack != ps->lastack) {
869 uint32_t netinteger;
870
871 trash[0] = 'A';
872 netinteger = htonl(ps->pushack);
873 memcpy(&trash[1], &netinteger, sizeof(netinteger));
874
875 repl = buffer_put_block(si->ib, trash, 1+sizeof(netinteger));
876 if (repl <= 0) {
877 /* no more write possible */
878 if (repl == -1)
879 goto out;
880 si->st0 = PEER_SESSION_END;
881 goto switchstate;
882 }
883 ps->lastack = ps->pushack;
884 }
885
886 if (ps->flags & PEER_F_TEACH_PROCESS) {
887 /* current peer was requested for a lesson */
888
889 if (!(ps->flags & PEER_F_TEACH_STAGE1)) {
890 /* lesson stage 1 not complete */
891 struct eb32_node *eb;
892
893 eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1);
894 while (1) {
895 int msglen;
896 struct stksess *ts;
897
898 if (!eb) {
899 /* flag lesson stage1 complete */
900 ps->flags |= PEER_F_TEACH_STAGE1;
901 eb = eb32_first(&ps->table->table->updates);
902 if (eb)
903 ps->pushed = eb->key - 1;
904 break;
905 }
906
907 ts = eb32_entry(eb, struct stksess, upd);
908 msglen = peer_prepare_datamsg(ts, ps, trash, sizeof(trash));
909 if (msglen) {
910 /* message to buffer */
911 repl = buffer_put_block(si->ib, trash, msglen);
912 if (repl <= 0) {
913 /* no more write possible */
914 if (repl == -1)
915 goto out;
916 si->st0 = PEER_SESSION_END;
917 goto switchstate;
918 }
919 ps->lastpush = ps->pushed = ts->upd.key;
920 }
921 eb = eb32_next(eb);
922 }
923 } /* !TEACH_STAGE1 */
924
925 if (!(ps->flags & PEER_F_TEACH_STAGE2)) {
926 /* lesson stage 2 not complete */
927 struct eb32_node *eb;
928
929 eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1);
930 while (1) {
931 int msglen;
932 struct stksess *ts;
933
934 if (!eb || eb->key > ps->teaching_origin) {
935 /* flag lesson stage1 complete */
936 ps->flags |= PEER_F_TEACH_STAGE2;
937 ps->pushed = ps->teaching_origin;
938 break;
939 }
940
941 ts = eb32_entry(eb, struct stksess, upd);
942 msglen = peer_prepare_datamsg(ts, ps, trash, sizeof(trash));
943 if (msglen) {
944 /* message to buffer */
945 repl = buffer_put_block(si->ib, trash, msglen);
946 if (repl <= 0) {
947 /* no more write possible */
948 if (repl == -1)
949 goto out;
950 si->st0 = PEER_SESSION_END;
951 goto switchstate;
952 }
953 ps->lastpush = ps->pushed = ts->upd.key;
954 }
955 eb = eb32_next(eb);
956 }
957 } /* !TEACH_STAGE2 */
958
959 if (!(ps->flags & PEER_F_TEACH_FINISHED)) {
960 /* process final lesson message */
961 repl = buffer_put_char(si->ib, ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FINISHED) ? 'F' : 'C');
962 if (repl <= 0) {
963 /* no more write possible */
964 if (repl == -1)
965 goto out;
966 si->st0 = PEER_SESSION_END;
967 goto switchstate;
968 }
969
970 /* flag finished message sent */
971 ps->flags |= PEER_F_TEACH_FINISHED;
972 } /* !TEACH_FINISHED */
973 } /* TEACH_PROCESS */
974
975 if (!(ps->flags & PEER_F_LEARN_ASSIGN) &&
976 (int)(ps->pushed - ps->table->table->localupdate) < 0) {
977 /* Push local updates, only if no learning in progress (to avoid ping-pong effects) */
978 struct eb32_node *eb;
979
980 eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1);
981 while (1) {
982 int msglen;
983 struct stksess *ts;
984
985 /* push local updates */
986 if (!eb) {
987 eb = eb32_first(&ps->table->table->updates);
988 if (!eb || ((int)(eb->key - ps->pushed) <= 0)) {
989 ps->pushed = ps->table->table->localupdate;
990 break;
991 }
992 }
993
994 if ((int)(eb->key - ps->table->table->localupdate) > 0) {
995 ps->pushed = ps->table->table->localupdate;
996 break;
997 }
998
999 ts = eb32_entry(eb, struct stksess, upd);
1000 msglen = peer_prepare_datamsg(ts, ps, trash, sizeof(trash));
1001 if (msglen) {
1002 /* message to buffer */
1003 repl = buffer_put_block(si->ib, trash, msglen);
1004 if (repl <= 0) {
1005 /* no more write possible */
1006 if (repl == -1)
1007 goto out;
1008 si->st0 = PEER_SESSION_END;
1009 goto switchstate;
1010 }
1011 ps->lastpush = ps->pushed = ts->upd.key;
1012 }
1013 eb = eb32_next(eb);
1014 }
1015 } /* ! LEARN_ASSIGN */
1016 /* noting more to do */
1017 goto out;
1018 }
1019 case PEER_SESSION_EXIT:
1020 repl = snprintf(trash, sizeof(trash), "%d\n", si->st1);
1021
1022 if (buffer_put_block(si->ib, trash, repl) == -1)
1023 goto out;
1024 si->st0 = PEER_SESSION_END;
1025 /* fall through */
1026 case PEER_SESSION_END: {
1027 si->shutw(si);
1028 si->shutr(si);
1029 si->ib->flags |= BF_READ_NULL;
1030 goto quit;
1031 }
1032 }
1033 }
1034out:
1035 si->update(si);
1036 si->ob->flags |= BF_READ_DONTWAIT;
1037 /* we don't want to expire timeouts while we're processing requests */
1038 si->ib->rex = TICK_ETERNITY;
1039 si->ob->wex = TICK_ETERNITY;
1040quit:
1041 return;
1042}
1043
1044
1045/*
1046 * Use this function to force a close of a peer session
1047 */
1048void peer_session_forceshutdown(struct session * session)
1049{
1050 struct stream_interface *oldsi;
1051
1052 if (session->si[0].iohandler == peer_io_handler) {
1053 oldsi = &session->si[0];
1054 }
1055 else {
1056 oldsi = &session->si[1];
1057 }
1058
1059 /* call release to reinit resync states if needed */
1060 peer_session_release(oldsi);
1061 oldsi->st0 = PEER_SESSION_END;
1062 oldsi->private = NULL;
1063 task_wakeup(session->task, TASK_WOKEN_MSG);
1064}
1065
1066/*
1067 * this function is called on a read event from a listen socket, corresponding
1068 * to an accept. It tries to accept as many connections as possible.
Willy Tarreaubd55e312010-11-11 10:55:09 +01001069 * It returns a positive value upon success, 0 if the connection needs to be
1070 * closed and ignored, or a negative value upon critical failure.
Emeric Brun2b920a12010-09-23 18:30:22 +02001071 */
1072int peer_accept(struct session *s)
1073{
1074 /* we have a dedicated I/O handler for the stats */
1075 stream_int_register_handler(&s->si[1], peer_io_handler);
1076 s->si[1].release = peer_session_release;
1077 s->si[1].private = s;
1078 s->si[1].st0 = PEER_SESSION_ACCEPT;
1079
1080 tv_zero(&s->logs.tv_request);
1081 s->logs.t_queue = 0;
1082 s->logs.t_connect = 0;
1083 s->logs.t_data = 0;
1084 s->logs.t_close = 0;
1085 s->logs.bytes_in = s->logs.bytes_out = 0;
1086 s->logs.prx_queue_size = 0;/* we get the number of pending conns before us */
1087 s->logs.srv_queue_size = 0; /* we will get this number soon */
1088
1089 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
1090
1091 if (s->listener->timeout) {
1092 s->req->rto = *s->listener->timeout;
1093 s->rep->wto = *s->listener->timeout;
1094 }
1095 return 1;
1096}
1097
1098/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001099 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001100 */
1101struct session *peer_session_create(struct peer *peer, struct peer_session *ps)
1102{
1103 struct listener *l = ((struct proxy *)peer->peers->peers_fe)->listen;
1104 struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */
1105 struct session *s;
1106 struct http_txn *txn;
1107 struct task *t;
1108
1109 if ((s = pool_alloc2(pool2_session)) == NULL) { /* disable this proxy for a while */
1110 Alert("out of memory in event_accept().\n");
1111 p->state = PR_STIDLE;
1112 goto out_close;
1113 }
1114
1115 LIST_ADDQ(&sessions, &s->list);
1116 LIST_INIT(&s->back_refs);
1117
1118 s->flags = SN_ASSIGNED|SN_ADDR_SET;
1119 s->term_trace = 0;
1120
1121 /* if this session comes from a known monitoring system, we want to ignore
1122 * it as soon as possible, which means closing it immediately for TCP.
1123 */
1124 if ((t = task_new()) == NULL) { /* disable this proxy for a while */
1125 Alert("out of memory in event_accept().\n");
1126 p->state = PR_STIDLE;
1127 goto out_free_session;
1128 }
1129
1130 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1131 ps->statuscode = PEER_SESSION_CONNECTCODE;
1132
1133 t->process = l->handler;
1134 t->context = s;
1135 t->nice = l->nice;
1136
1137 memcpy(&s->srv_addr, &peer->addr, sizeof(s->srv_addr));
1138 s->task = t;
1139 s->listener = l;
1140
1141 /* Note: initially, the session's backend points to the frontend.
1142 * This changes later when switching rules are executed or
1143 * when the default backend is assigned.
1144 */
1145 s->be = s->fe = p;
1146
1147 s->req = s->rep = NULL; /* will be allocated later */
1148
1149 s->si[0].fd = -1;
1150 s->si[0].owner = t;
1151 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
1152 s->si[0].err_type = SI_ET_NONE;
1153 s->si[0].err_loc = NULL;
1154 s->si[0].exp = TICK_ETERNITY;
1155 s->si[0].flags = SI_FL_NONE;
1156 if (s->fe->options2 & PR_O2_INDEPSTR)
1157 s->si[0].flags |= SI_FL_INDEP_STR;
1158 s->si[0].private = (void *)ps;
1159 s->si[0].st0 = PEER_SESSION_CONNECT;
1160
1161 stream_int_register_handler(&s->si[0], peer_io_handler);
1162 s->si[0].release = peer_session_release;
1163
1164 s->si[1].fd = -1; /* just to help with debugging */
1165 s->si[1].owner = t;
1166 s->si[1].state = s->si[1].prev_state = SI_ST_ASS;
1167 s->si[1].conn_retries = p->conn_retries;
1168 s->si[1].err_type = SI_ET_NONE;
1169 s->si[1].err_loc = NULL;
1170 s->si[1].connect = tcpv4_connect_server;
1171 s->si[1].exp = TICK_ETERNITY;
1172 s->si[1].flags = SI_FL_NONE;
1173 if (s->be->options2 & PR_O2_INDEPSTR)
1174 s->si[1].flags |= SI_FL_INDEP_STR;
1175
1176 stream_sock_prepare_interface(&s->si[1]);
1177 s->si[1].release = NULL;
1178
1179 s->srv = s->prev_srv = s->srv_conn = NULL;
1180 s->pend_pos = NULL;
1181
1182 /* init store persistence */
1183 s->store_count = 0;
1184 s->stkctr1_entry = NULL;
1185 s->stkctr2_entry = NULL;
1186
1187 /* FIXME: the logs are horribly complicated now, because they are
1188 * defined in <p>, <p>, and later <be> and <be>.
1189 */
1190
1191 s->logs.logwait = 0;
1192 s->do_log = NULL;
1193
1194 /* default error reporting function, may be changed by analysers */
1195 s->srv_error = default_srv_error;
1196
1197 s->data_source = DATA_SRC_NONE;
1198
1199 s->uniq_id = 0;
1200
1201 txn = &s->txn;
1202 /* Those variables will be checked and freed if non-NULL in
1203 * session.c:session_free(). It is important that they are
1204 * properly initialized.
1205 */
1206 txn->sessid = NULL;
1207 txn->srv_cookie = NULL;
1208 txn->cli_cookie = NULL;
1209 txn->uri = NULL;
1210 txn->req.cap = NULL;
1211 txn->rsp.cap = NULL;
1212 txn->hdr_idx.v = NULL;
1213 txn->hdr_idx.size = txn->hdr_idx.used = 0;
1214
1215 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
1216 goto out_fail_req; /* no memory */
1217
1218 s->req->size = global.tune.bufsize;
1219 buffer_init(s->req);
1220 s->req->prod = &s->si[0];
1221 s->req->cons = &s->si[1];
1222 s->si[0].ib = s->si[1].ob = s->req;
1223
1224 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
1225
1226 /* activate default analysers enabled for this listener */
1227 s->req->analysers = l->analysers;
1228
1229 /* note: this should not happen anymore since there's always at least the switching rules */
1230 if (!s->req->analysers) {
1231 buffer_auto_connect(s->req);/* don't wait to establish connection */
1232 buffer_auto_close(s->req);/* let the producer forward close requests */
1233 }
1234
1235 s->req->rto = s->fe->timeout.client;
1236 s->req->wto = s->be->timeout.server;
1237
1238 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
1239 goto out_fail_rep; /* no memory */
1240
1241 s->rep->size = global.tune.bufsize;
1242 buffer_init(s->rep);
1243 s->rep->prod = &s->si[1];
1244 s->rep->cons = &s->si[0];
1245 s->si[0].ob = s->si[1].ib = s->rep;
1246
1247 s->rep->rto = s->be->timeout.server;
1248 s->rep->wto = s->fe->timeout.client;
1249
1250 s->req->rex = TICK_ETERNITY;
1251 s->req->wex = TICK_ETERNITY;
1252 s->req->analyse_exp = TICK_ETERNITY;
1253 s->rep->rex = TICK_ETERNITY;
1254 s->rep->wex = TICK_ETERNITY;
1255 s->rep->analyse_exp = TICK_ETERNITY;
1256 t->expire = TICK_ETERNITY;
1257
1258 s->rep->flags |= BF_READ_DONTWAIT;
1259 /* it is important not to call the wakeup function directly but to
1260 * pass through task_wakeup(), because this one knows how to apply
1261 * priorities to tasks.
1262 */
1263 task_wakeup(t, TASK_WOKEN_INIT);
1264
1265 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1266 p->feconn++;/* beconn will be increased later */
1267 jobs++;
1268 actconn++;
1269 totalconn++;
1270
1271 return s;
1272
1273 /* Error unrolling */
1274 out_fail_rep:
1275 pool_free2(pool2_buffer, s->req);
1276 out_fail_req:
1277 task_free(t);
1278 out_free_session:
1279 LIST_DEL(&s->list);
1280 pool_free2(pool2_session, s);
1281 out_close:
1282 return s;
1283}
1284
1285/*
1286 * Task processing function to manage re-connect and peer session
1287 * tasks wakeup on local update.
1288 */
1289struct task *process_peer_sync(struct task * task)
1290{
1291 struct shared_table *st = (struct shared_table *)task->context;
1292 struct peer_session *ps;
1293
1294 task->expire = TICK_ETERNITY;
1295
1296 if (!stopping) {
1297 /* Normal case (not soft stop)*/
1298 if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL) &&
1299 (!nb_oldpids || tick_is_expired(st->resync_timeout, now_ms)) &&
1300 !(st->flags & SHTABLE_F_RESYNC_ASSIGN)) {
1301 /* Resync from local peer needed
1302 no peer was assigned for the lesson
1303 and no old local peer found
1304 or resync timeout expire */
1305
1306 /* flag no more resync from local, to try resync from remotes */
1307 st->flags |= SHTABLE_F_RESYNC_LOCAL;
1308
1309 /* reschedule a resync */
1310 st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1311 }
1312
1313 /* For each session */
1314 for (ps = st->sessions; ps; ps = ps->next) {
1315 /* For each remote peers */
1316 if (!ps->peer->local) {
1317 if (!ps->session) {
1318 /* no active session */
1319 if (ps->statuscode == 0 ||
1320 ps->statuscode == PEER_SESSION_SUCCESSCODE ||
1321 ((ps->statuscode == PEER_SESSION_CONNECTCODE ||
1322 ps->statuscode == PEER_SESSION_CONNECTEDCODE) &&
1323 tick_is_expired(ps->reconnect, now_ms))) {
1324 /* connection never tried
1325 * or previous session established with success
1326 * or previous session failed during connection
1327 * and reconnection timer is expired */
1328
1329 /* retry a connect */
1330 ps->session = peer_session_create(ps->peer, ps);
1331 }
1332 else if (ps->statuscode == PEER_SESSION_CONNECTCODE ||
1333 ps->statuscode == PEER_SESSION_CONNECTEDCODE) {
1334 /* If previous session failed during connection
1335 * but reconnection timer is not expired */
1336
1337 /* reschedule task for reconnect */
1338 task->expire = tick_first(task->expire, ps->reconnect);
1339 }
1340 /* else do nothing */
1341 } /* !ps->session */
1342 else if (ps->statuscode == PEER_SESSION_SUCCESSCODE) {
1343 /* current session is active and established */
1344 if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) &&
1345 !(st->flags & SHTABLE_F_RESYNC_ASSIGN) &&
1346 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1347 /* Resync from a remote is needed
1348 * and no peer was assigned for lesson
1349 * and current peer may be up2date */
1350
1351 /* assign peer for the lesson */
1352 ps->flags |= PEER_F_LEARN_ASSIGN;
1353 st->flags |= SHTABLE_F_RESYNC_ASSIGN;
1354
1355 /* awake peer session task to handle a request of resync */
1356 task_wakeup(ps->session->task, TASK_WOKEN_MSG);
1357 }
1358 else if ((int)(ps->pushed - ps->table->table->localupdate) < 0) {
1359 /* awake peer session task to push local updates */
1360 task_wakeup(ps->session->task, TASK_WOKEN_MSG);
1361 }
1362 /* else do nothing */
1363 } /* SUCCESSCODE */
1364 } /* !ps->peer->local */
1365 } /* for */
1366
1367 /* Resync from remotes expired: consider resync is finished */
1368 if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) &&
1369 !(st->flags & SHTABLE_F_RESYNC_ASSIGN) &&
1370 tick_is_expired(st->resync_timeout, now_ms)) {
1371 /* Resync from remote peer needed
1372 * no peer was assigned for the lesson
1373 * and resync timeout expire */
1374
1375 /* flag no more resync from remote, consider resync is finished */
1376 st->flags |= SHTABLE_F_RESYNC_REMOTE;
1377 }
1378
1379 if ((st->flags & SHTABLE_RESYNC_STATEMASK) != SHTABLE_RESYNC_FINISHED) {
1380 /* Resync not finished*/
1381 /* reschedule task to resync timeout, to ended resync if needed */
1382 task->expire = tick_first(task->expire, st->resync_timeout);
1383 }
1384 } /* !stopping */
1385 else {
1386 /* soft stop case */
1387 if (task->state & TASK_WOKEN_SIGNAL) {
1388 /* We've just recieved the signal */
1389 if (!(st->flags & SHTABLE_F_DONOTSTOP)) {
1390 /* add DO NOT STOP flag if not present */
1391 jobs++;
1392 st->flags |= SHTABLE_F_DONOTSTOP;
1393 }
1394
1395 /* disconnect all connected peers */
1396 for (ps = st->sessions; ps; ps = ps->next) {
1397 if (ps->session) {
1398 peer_session_forceshutdown(ps->session);
1399 ps->session = NULL;
1400 }
1401 }
1402 }
1403 ps = st->local_session;
1404
1405 if (ps->flags & PEER_F_TEACH_COMPLETE) {
1406 if (st->flags & SHTABLE_F_DONOTSTOP) {
1407 /* resync of new process was complete, current process can die now */
1408 jobs--;
1409 st->flags &= ~SHTABLE_F_DONOTSTOP;
1410 }
1411 }
1412 else if (!ps->session) {
1413 /* If session is not active */
1414 if (ps->statuscode == 0 ||
1415 ps->statuscode == PEER_SESSION_SUCCESSCODE ||
1416 ps->statuscode == PEER_SESSION_CONNECTEDCODE ||
1417 ps->statuscode == PEER_SESSION_TRYAGAIN) {
1418 /* connection never tried
1419 * or previous session was successfully established
1420 * or previous session tcp connect success but init state incomplete
1421 * or during previous connect, peer replies a try again statuscode */
1422
1423 /* connect to the peer */
1424 ps->session = peer_session_create(ps->peer, ps);
1425 }
1426 else {
1427 /* Other error cases */
1428 if (st->flags & SHTABLE_F_DONOTSTOP) {
1429 /* unable to resync new process, current process can die now */
1430 jobs--;
1431 st->flags &= ~SHTABLE_F_DONOTSTOP;
1432 }
1433 }
1434 }
1435 else if (ps->statuscode == PEER_SESSION_SUCCESSCODE &&
1436 (int)(ps->pushed - ps->table->table->localupdate) < 0) {
1437 /* current session active and established
1438 awake session to push remaining local updates */
1439 task_wakeup(ps->session->task, TASK_WOKEN_MSG);
1440 }
1441 } /* stopping */
1442 /* Wakeup for re-connect */
1443 return task;
1444}
1445
1446/*
1447 * Function used to register a table for sync on a group of peers
1448 *
1449 */
1450void peers_register_table(struct peers *peers, struct stktable *table)
1451{
1452 struct shared_table *st;
1453 struct peer * curpeer;
1454 struct peer_session *ps;
1455
1456 st = (struct shared_table *)calloc(1,sizeof(struct shared_table));
1457 st->table = table;
1458 st->next = peers->tables;
1459 st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1460 peers->tables = st;
1461
1462 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
1463 ps = (struct peer_session *)calloc(1,sizeof(struct peer_session));
1464 ps->table = st;
1465 ps->peer = curpeer;
1466 if (curpeer->local)
1467 st->local_session = ps;
1468 ps->next = st->sessions;
1469 ps->reconnect = now_ms;
1470 st->sessions = ps;
1471 peers->peers_fe->maxconn += 3;
1472 }
1473
1474 peers->peers_fe->listen->maxconn = peers->peers_fe->maxconn;
1475 st->sync_task = task_new();
1476 st->sync_task->process = process_peer_sync;
1477 st->sync_task->expire = TICK_ETERNITY;
1478 st->sync_task->context = (void *)st;
1479 table->sync_task =st->sync_task;
1480 signal_register_task(0, table->sync_task, 0);
1481 task_wakeup(st->sync_task, TASK_WOKEN_INIT);
1482}
1483