forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstick_table.c
5091 lines (4264 loc) · 162 KB
/
stick_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Stick tables management functions.
*
* Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
* Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <string.h>
#include <errno.h>
#include <import/ebmbtree.h>
#include <import/ebsttree.h>
#include <import/ebistree.h>
#include <haproxy/api.h>
#include <haproxy/arg.h>
#include <haproxy/cfgparse.h>
#include <haproxy/cli.h>
#include <haproxy/dict.h>
#include <haproxy/errors.h>
#include <haproxy/global.h>
#include <haproxy/http_rules.h>
#include <haproxy/list.h>
#include <haproxy/log.h>
#include <haproxy/net_helper.h>
#include <haproxy/peers.h>
#include <haproxy/pool.h>
#include <haproxy/proto_tcp.h>
#include <haproxy/proxy.h>
#include <haproxy/sample.h>
#include <haproxy/stats-t.h>
#include <haproxy/stick_table.h>
#include <haproxy/stream.h>
#include <haproxy/stream_interface.h>
#include <haproxy/task.h>
#include <haproxy/tcp_rules.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
/* structure used to return a table key built from a sample */
static THREAD_LOCAL struct stktable_key static_table_key;
static int (*smp_fetch_src)(const struct arg *, struct sample *, const char *, void *);
struct stktable *stktables_list;
struct eb_root stktable_by_name = EB_ROOT;
#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
/* This function inserts stktable <t> into the tree of known stick-table.
* The stick-table ID is used as the storing key so it must already have
* been initialized.
*/
void stktable_store_name(struct stktable *t)
{
t->name.key = t->id;
ebis_insert(&stktable_by_name, &t->name);
}
struct stktable *stktable_find_by_name(const char *name)
{
struct ebpt_node *node;
struct stktable *t;
node = ebis_lookup(&stktable_by_name, name);
if (node) {
t = container_of(node, struct stktable, name);
if (strcmp(t->id, name) == 0)
return t;
}
return NULL;
}
/*
* Free an allocated sticky session <ts>, and decrease sticky sessions counter
* in table <t>.
*/
void __stksess_free(struct stktable *t, struct stksess *ts)
{
t->current--;
pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
}
/*
* Free an allocated sticky session <ts>, and decrease sticky sessions counter
* in table <t>.
* This function locks the table
*/
void stksess_free(struct stktable *t, struct stksess *ts)
{
void *data;
data = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_KEY);
if (data) {
dict_entry_unref(&server_key_dict, stktable_data_cast(data, std_t_dict));
stktable_data_cast(data, std_t_dict) = NULL;
}
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
__stksess_free(t, ts);
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
}
/*
* Kill an stksess (only if its ref_cnt is zero).
*/
int __stksess_kill(struct stktable *t, struct stksess *ts)
{
if (ts->ref_cnt)
return 0;
eb32_delete(&ts->exp);
eb32_delete(&ts->upd);
ebmb_delete(&ts->key);
__stksess_free(t, ts);
return 1;
}
/*
* Decrease the refcount if decrefcnt is not 0.
* and try to kill the stksess
* This function locks the table
*/
int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcnt)
{
int ret;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
if (decrefcnt)
ts->ref_cnt--;
ret = __stksess_kill(t, ts);
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return ret;
}
/*
* Initialize or update the key in the sticky session <ts> present in table <t>
* from the value present in <key>.
*/
void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
{
if (t->type != SMP_T_STR)
memcpy(ts->key.key, key->key, t->key_size);
else {
memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
}
}
/*
* Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
* is returned.
*/
static struct stksess *__stksess_init(struct stktable *t, struct stksess * ts)
{
memset((void *)ts - t->data_size, 0, t->data_size);
ts->ref_cnt = 0;
ts->key.node.leaf_p = NULL;
ts->exp.node.leaf_p = NULL;
ts->upd.node.leaf_p = NULL;
ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
HA_RWLOCK_INIT(&ts->lock);
return ts;
}
/*
* Trash oldest <to_batch> sticky sessions from table <t>
* Returns number of trashed sticky sessions. It may actually trash less
* than expected if finding these requires too long a search time (e.g.
* most of them have ts->ref_cnt>0).
*/
int __stktable_trash_oldest(struct stktable *t, int to_batch)
{
struct stksess *ts;
struct eb32_node *eb;
int max_search = to_batch * 2; // no more than 50% misses
int batched = 0;
int looped = 0;
eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
while (batched < to_batch) {
if (unlikely(!eb)) {
/* we might have reached the end of the tree, typically because
* <now_ms> is in the first half and we're first scanning the last
* half. Let's loop back to the beginning of the tree now if we
* have not yet visited it.
*/
if (looped)
break;
looped = 1;
eb = eb32_first(&t->exps);
if (likely(!eb))
break;
}
if (--max_search < 0)
break;
/* timer looks expired, detach it from the queue */
ts = eb32_entry(eb, struct stksess, exp);
eb = eb32_next(eb);
/* don't delete an entry which is currently referenced */
if (ts->ref_cnt)
continue;
eb32_delete(&ts->exp);
if (ts->expire != ts->exp.key) {
if (!tick_isset(ts->expire))
continue;
ts->exp.key = ts->expire;
eb32_insert(&t->exps, &ts->exp);
if (!eb || eb->key > ts->exp.key)
eb = &ts->exp;
continue;
}
/* session expired, trash it */
ebmb_delete(&ts->key);
eb32_delete(&ts->upd);
__stksess_free(t, ts);
batched++;
}
return batched;
}
/*
* Trash oldest <to_batch> sticky sessions from table <t>
* Returns number of trashed sticky sessions.
* This function locks the table
*/
int stktable_trash_oldest(struct stktable *t, int to_batch)
{
int ret;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
ret = __stktable_trash_oldest(t, to_batch);
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return ret;
}
/*
* Allocate and initialise a new sticky session.
* The new sticky session is returned or NULL in case of lack of memory.
* Sticky sessions should only be allocated this way, and must be freed using
* stksess_free(). Table <t>'s sticky session counter is increased. If <key>
* is not NULL, it is assigned to the new session.
*/
struct stksess *__stksess_new(struct stktable *t, struct stktable_key *key)
{
struct stksess *ts;
if (unlikely(t->current == t->size)) {
if ( t->nopurge )
return NULL;
if (!__stktable_trash_oldest(t, (t->size >> 8) + 1))
return NULL;
}
ts = pool_alloc(t->pool);
if (ts) {
t->current++;
ts = (void *)ts + round_ptr_size(t->data_size);
__stksess_init(t, ts);
if (key)
stksess_setkey(t, ts, key);
}
return ts;
}
/*
* Allocate and initialise a new sticky session.
* The new sticky session is returned or NULL in case of lack of memory.
* Sticky sessions should only be allocated this way, and must be freed using
* stksess_free(). Table <t>'s sticky session counter is increased. If <key>
* is not NULL, it is assigned to the new session.
* This function locks the table
*/
struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
{
struct stksess *ts;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
ts = __stksess_new(t, key);
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return ts;
}
/*
* Looks in table <t> for a sticky session matching key <key>.
* Returns pointer on requested sticky session or NULL if none was found.
*/
struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
{
struct ebmb_node *eb;
if (t->type == SMP_T_STR)
eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
else
eb = ebmb_lookup(&t->keys, key->key, t->key_size);
if (unlikely(!eb)) {
/* no session found */
return NULL;
}
return ebmb_entry(eb, struct stksess, key);
}
/*
* Looks in table <t> for a sticky session matching key <key>.
* Returns pointer on requested sticky session or NULL if none was found.
* The refcount of the found entry is increased and this function
* is protected using the table lock
*/
struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
{
struct stksess *ts;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
ts = __stktable_lookup_key(t, key);
if (ts)
ts->ref_cnt++;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return ts;
}
/*
* Looks in table <t> for a sticky session with same key as <ts>.
* Returns pointer on requested sticky session or NULL if none was found.
*/
struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
{
struct ebmb_node *eb;
if (t->type == SMP_T_STR)
eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
else
eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
if (unlikely(!eb))
return NULL;
return ebmb_entry(eb, struct stksess, key);
}
/*
* Looks in table <t> for a sticky session with same key as <ts>.
* Returns pointer on requested sticky session or NULL if none was found.
* The refcount of the found entry is increased and this function
* is protected using the table lock
*/
struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
{
struct stksess *lts;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
lts = __stktable_lookup(t, ts);
if (lts)
lts->ref_cnt++;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return lts;
}
/* Update the expiration timer for <ts> but do not touch its expiration node.
* The table's expiration timer is updated if set.
* The node will be also inserted into the update tree if needed, at a position
* depending if the update is a local or coming from a remote node
*/
void __stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire)
{
struct eb32_node * eb;
ts->expire = expire;
if (t->expire) {
t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
task_queue(t->exp_task);
}
/* If sync is enabled */
if (t->sync_task) {
if (local) {
/* If this entry is not in the tree
or not scheduled for at least one peer */
if (!ts->upd.node.leaf_p
|| (int)(t->commitupdate - ts->upd.key) >= 0
|| (int)(ts->upd.key - t->localupdate) >= 0) {
ts->upd.key = ++t->update;
t->localupdate = t->update;
eb32_delete(&ts->upd);
eb = eb32_insert(&t->updates, &ts->upd);
if (eb != &ts->upd) {
eb32_delete(eb);
eb32_insert(&t->updates, &ts->upd);
}
}
task_wakeup(t->sync_task, TASK_WOKEN_MSG);
}
else {
/* If this entry is not in the tree */
if (!ts->upd.node.leaf_p) {
ts->upd.key= (++t->update)+(2147483648U);
eb = eb32_insert(&t->updates, &ts->upd);
if (eb != &ts->upd) {
eb32_delete(eb);
eb32_insert(&t->updates, &ts->upd);
}
}
}
}
}
/* Update the expiration timer for <ts> but do not touch its expiration node.
* The table's expiration timer is updated using the date of expiration coming from
* <t> stick-table configuration.
* The node will be also inserted into the update tree if needed, at a position
* considering the update is coming from a remote node
*/
void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
{
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
__stktable_touch_with_exp(t, ts, 0, ts->expire);
if (decrefcnt)
ts->ref_cnt--;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
}
/* Update the expiration timer for <ts> but do not touch its expiration node.
* The table's expiration timer is updated using the date of expiration coming from
* <t> stick-table configuration.
* The node will be also inserted into the update tree if needed, at a position
* considering the update was made locally
*/
void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
{
int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
__stktable_touch_with_exp(t, ts, 1, expire);
if (decrefcnt)
ts->ref_cnt--;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
}
/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL */
static void stktable_release(struct stktable *t, struct stksess *ts)
{
if (!ts)
return;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
ts->ref_cnt--;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
}
/* Insert new sticky session <ts> in the table. It is assumed that it does not
* yet exist (the caller must check this). The table's timeout is updated if it
* is set. <ts> is returned.
*/
void __stktable_store(struct stktable *t, struct stksess *ts)
{
ebmb_insert(&t->keys, &ts->key, t->key_size);
ts->exp.key = ts->expire;
eb32_insert(&t->exps, &ts->exp);
if (t->expire) {
t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
task_queue(t->exp_task);
}
}
/* Returns a valid or initialized stksess for the specified stktable_key in the
* specified table, or NULL if the key was NULL, or if no entry was found nor
* could be created. The entry's expiration is updated.
*/
struct stksess *__stktable_get_entry(struct stktable *table, struct stktable_key *key)
{
struct stksess *ts;
if (!key)
return NULL;
ts = __stktable_lookup_key(table, key);
if (ts == NULL) {
/* entry does not exist, initialize a new one */
ts = __stksess_new(table, key);
if (!ts)
return NULL;
__stktable_store(table, ts);
}
return ts;
}
/* Returns a valid or initialized stksess for the specified stktable_key in the
* specified table, or NULL if the key was NULL, or if no entry was found nor
* could be created. The entry's expiration is updated.
* This function locks the table, and the refcount of the entry is increased.
*/
struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
{
struct stksess *ts;
HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
ts = __stktable_get_entry(table, key);
if (ts)
ts->ref_cnt++;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
return ts;
}
/* Lookup for an entry with the same key and store the submitted
* stksess if not found.
*/
struct stksess *__stktable_set_entry(struct stktable *table, struct stksess *nts)
{
struct stksess *ts;
ts = __stktable_lookup(table, nts);
if (ts == NULL) {
ts = nts;
__stktable_store(table, ts);
}
return ts;
}
/* Lookup for an entry with the same key and store the submitted
* stksess if not found.
* This function locks the table, and the refcount of the entry is increased.
*/
struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
{
struct stksess *ts;
HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
ts = __stktable_set_entry(table, nts);
ts->ref_cnt++;
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
return ts;
}
/*
* Trash expired sticky sessions from table <t>. The next expiration date is
* returned.
*/
static int stktable_trash_expired(struct stktable *t)
{
struct stksess *ts;
struct eb32_node *eb;
int looped = 0;
HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
while (1) {
if (unlikely(!eb)) {
/* we might have reached the end of the tree, typically because
* <now_ms> is in the first half and we're first scanning the last
* half. Let's loop back to the beginning of the tree now if we
* have not yet visited it.
*/
if (looped)
break;
looped = 1;
eb = eb32_first(&t->exps);
if (likely(!eb))
break;
}
if (likely(tick_is_lt(now_ms, eb->key))) {
/* timer not expired yet, revisit it later */
t->exp_next = eb->key;
goto out_unlock;
}
/* timer looks expired, detach it from the queue */
ts = eb32_entry(eb, struct stksess, exp);
eb = eb32_next(eb);
/* don't delete an entry which is currently referenced */
if (ts->ref_cnt)
continue;
eb32_delete(&ts->exp);
if (!tick_is_expired(ts->expire, now_ms)) {
if (!tick_isset(ts->expire))
continue;
ts->exp.key = ts->expire;
eb32_insert(&t->exps, &ts->exp);
if (!eb || eb->key > ts->exp.key)
eb = &ts->exp;
continue;
}
/* session expired, trash it */
ebmb_delete(&ts->key);
eb32_delete(&ts->upd);
__stksess_free(t, ts);
}
/* We have found no task to expire in any tree */
t->exp_next = TICK_ETERNITY;
out_unlock:
HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
return t->exp_next;
}
/*
* Task processing function to trash expired sticky sessions. A pointer to the
* task itself is returned since it never dies.
*/
struct task *process_table_expire(struct task *task, void *context, unsigned int state)
{
struct stktable *t = context;
task->expire = stktable_trash_expired(t);
return task;
}
/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
int stktable_init(struct stktable *t)
{
int peers_retval = 0;
if (t->size) {
t->keys = EB_ROOT_UNIQUE;
memset(&t->exps, 0, sizeof(t->exps));
t->updates = EB_ROOT_UNIQUE;
HA_SPIN_INIT(&t->lock);
t->pool = create_pool("sticktables", sizeof(struct stksess) + round_ptr_size(t->data_size) + t->key_size, MEM_F_SHARED);
t->exp_next = TICK_ETERNITY;
if ( t->expire ) {
t->exp_task = task_new(MAX_THREADS_MASK);
if (!t->exp_task)
return 0;
t->exp_task->process = process_table_expire;
t->exp_task->context = (void *)t;
}
if (t->peers.p && t->peers.p->peers_fe && !t->peers.p->peers_fe->disabled) {
peers_retval = peers_register_table(t->peers.p, t);
}
return (t->pool != NULL) && !peers_retval;
}
return 1;
}
/*
* Configuration keywords of known table types
*/
struct stktable_type stktable_types[SMP_TYPES] = {
[SMP_T_SINT] = { "integer", 0, 4 },
[SMP_T_IPV4] = { "ip", 0, 4 },
[SMP_T_IPV6] = { "ipv6", 0, 16 },
[SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
[SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
};
/*
* Parse table type configuration.
* Returns 0 on successful parsing, else 1.
* <myidx> is set at next configuration <args> index.
*/
int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
{
for (*type = 0; *type < SMP_TYPES; (*type)++) {
if (!stktable_types[*type].kw)
continue;
if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
continue;
*key_size = stktable_types[*type].default_size;
(*myidx)++;
if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
if (strcmp("len", args[*myidx]) == 0) {
(*myidx)++;
*key_size = atol(args[*myidx]);
if (!*key_size)
break;
if (*type == SMP_T_STR) {
/* null terminated string needs +1 for '\0'. */
(*key_size)++;
}
(*myidx)++;
}
}
return 0;
}
return 1;
}
/* reserve some space for data type <type>, there is 2 optionnals
* argument at <sa> and <sa2> to configure this data type and
* they can be NULL if unused for a given type.
* Returns PE_NONE (0) if OK or an error code among :
* - PE_ENUM_OOR if <type> does not exist
* - PE_EXIST if <type> is already registered
* - PE_ARG_NOT_USE if <sa>/<sa2> was provided but not expected
* - PE_ARG_MISSING if <sa>/<sa2> was expected but not provided
* - PE_ARG_VALUE_OOR if type is an array and <sa> it out of array size range.
*/
int stktable_alloc_data_type(struct stktable *t, int type, const char *sa, const char *sa2)
{
if (type >= STKTABLE_DATA_TYPES)
return PE_ENUM_OOR;
if (t->data_ofs[type])
/* already allocated */
return PE_EXIST;
t->data_nbelem[type] = 1;
if (stktable_data_types[type].is_array) {
/* arrays take their element count on first argument */
if (!sa)
return PE_ARG_MISSING;
t->data_nbelem[type] = atoi(sa);
if (!t->data_nbelem[type] || (t->data_nbelem[type] > STKTABLE_MAX_DT_ARRAY_SIZE))
return PE_ARG_VALUE_OOR;
sa = sa2;
}
switch (stktable_data_types[type].arg_type) {
case ARG_T_NONE:
if (sa)
return PE_ARG_NOT_USED;
break;
case ARG_T_INT:
if (!sa)
return PE_ARG_MISSING;
t->data_arg[type].i = atoi(sa);
break;
case ARG_T_DELAY:
if (!sa)
return PE_ARG_MISSING;
sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
if (sa)
return PE_ARG_INVC; /* invalid char */
break;
}
t->data_size += t->data_nbelem[type] * stktable_type_size(stktable_data_types[type].std_type);
t->data_ofs[type] = -t->data_size;
return PE_NONE;
}
/*
* Parse a line with <linenum> as number in <file> configuration file to configure
* the stick-table with <t> as address and <id> as ID.
* <peers> provides the "peers" section pointer only if this function is called
* from a "peers" section.
* <nid> is the stick-table name which is sent over the network. It must be equal
* to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
* "peers" section name followed by a '/' character if parsed from a "peers" section.
* This is the responsibility of the caller to check this.
* Return an error status with ERR_* flags set if required, 0 if no error was encountered.
*/
int parse_stick_table(const char *file, int linenum, char **args,
struct stktable *t, char *id, char *nid, struct peers *peers)
{
int err_code = 0;
int idx = 1;
unsigned int val;
if (!id || !*id) {
ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_ABORT;
goto out;
}
/* Store the "peers" section if this function is called from a "peers" section. */
if (peers) {
t->peers.p = peers;
idx++;
}
t->id = id;
t->nid = nid;
t->type = (unsigned int)-1;
t->conf.file = file;
t->conf.line = linenum;
while (*args[idx]) {
const char *err;
if (strcmp(args[idx], "size") == 0) {
idx++;
if (!*(args[idx])) {
ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
file, linenum, args[0], args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
if ((err = parse_size_err(args[idx], &t->size))) {
ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
file, linenum, args[0], *err, args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
idx++;
}
/* This argument does not exit in "peers" section. */
else if (!peers && strcmp(args[idx], "peers") == 0) {
idx++;
if (!*(args[idx])) {
ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
file, linenum, args[0], args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
t->peers.name = strdup(args[idx++]);
}
else if (strcmp(args[idx], "expire") == 0) {
idx++;
if (!*(args[idx])) {
ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
file, linenum, args[0], args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
if (err == PARSE_TIME_OVER) {
ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
file, linenum, args[0], args[idx], args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
else if (err == PARSE_TIME_UNDER) {
ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
file, linenum, args[0], args[idx], args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
else if (err) {
ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
file, linenum, args[0], *err, args[idx-1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
t->expire = val;
idx++;
}
else if (strcmp(args[idx], "nopurge") == 0) {
t->nopurge = 1;
idx++;
}
else if (strcmp(args[idx], "type") == 0) {
idx++;
if (stktable_parse_type(args, &idx, &t->type, &t->key_size) != 0) {
ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n",
file, linenum, args[0], args[idx]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
/* idx already points to next arg */
}
else if (strcmp(args[idx], "store") == 0) {
int type, err;
char *cw, *nw, *sa, *sa2;
idx++;
nw = args[idx];
while (*nw) {
/* the "store" keyword supports a comma-separated list */
cw = nw;
sa = NULL; /* store arg */
sa2 = NULL;
while (*nw && *nw != ',') {
if (*nw == '(') {
*nw = 0;
sa = ++nw;
while (*nw != ')') {
if (!*nw) {
ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
file, linenum, args[0], cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
if (*nw == ',') {
*nw = '\0';
sa2 = nw + 1;
}
nw++;
}
*nw = '\0';
}
nw++;
}
if (*nw)
*nw++ = '\0';
type = stktable_get_data_type(cw);
if (type < 0) {
ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
file, linenum, args[0], cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
err = stktable_alloc_data_type(t, type, sa, sa2);
switch (err) {
case PE_NONE: break;
case PE_EXIST:
ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
file, linenum, args[0], cw);
err_code |= ERR_WARN;
break;
case PE_ARG_MISSING:
ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
file, linenum, args[0], cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
case PE_ARG_NOT_USED:
ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
file, linenum, args[0], cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
case PE_ARG_VALUE_OOR:
ha_alert("parsing [%s:%d] : %s: array size is out of allowed range (1-%d) for store option '%s'.\n",
file, linenum, args[0], STKTABLE_MAX_DT_ARRAY_SIZE, cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
default:
ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
file, linenum, args[0], cw);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
}
idx++;
if (t->data_ofs[STKTABLE_DT_GPT] && t->data_ofs[STKTABLE_DT_GPT0]) {
ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpt' and 'gpt0' in a same table is not permitted as 'gpt' overrides 'gpt0'.\n",
file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
else if (t->data_ofs[STKTABLE_DT_GPC] && (t->data_ofs[STKTABLE_DT_GPC0] || t->data_ofs[STKTABLE_DT_GPC1])) {
ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc' and 'gpc[0/1]' in a same table is not permitted as 'gpc' overrides 'gpc[0/1]'.\n",
file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
else if (t->data_ofs[STKTABLE_DT_GPC_RATE] && (t->data_ofs[STKTABLE_DT_GPC0_RATE] || t->data_ofs[STKTABLE_DT_GPC1_RATE])) {
ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc_rate' and 'gpc[0/1]_rate' in a same table is not permitted as 'gpc_rate' overrides 'gpc[0/1]_rate'.\n",
file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
}
else if (strcmp(args[idx], "srvkey") == 0) {
char *keytype;
idx++;
keytype = args[idx];
if (strcmp(keytype, "name") == 0) {
t->server_key_type = STKTABLE_SRV_NAME;
}
else if (strcmp(keytype, "addr") == 0) {
t->server_key_type = STKTABLE_SRV_ADDR;
}
else {
ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
file, linenum, args[0], keytype);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
idx++;
}
else {
ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
file, linenum, args[0], args[idx]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
}
if (!t->size) {
ha_alert("parsing [%s:%d] : %s: missing size.\n",
file, linenum, args[0]);