/
ratelimit_helper.c
1019 lines (877 loc) · 23.3 KB
/
ratelimit_helper.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
/*
* Copyright (C) 2011 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2011-09-26 created (razvancrainea)
*/
#include "../../sr_module.h"
#include "../../ut.h"
#include "../../locking.h"
#include "../../mod_fix.h"
#include "../../timer.h"
#include "../../socket_info.h"
#include "../../resolve.h"
#include "../../bin_interface.h"
#include "../../cachedb/cachedb.h"
#include "../../cachedb/cachedb_cap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ratelimit.h"
/* parameters */
int rl_expire_time = RL_DEFAULT_EXPIRE;
int rl_hash_size = RL_HASHSIZE;
str rl_default_algo_s = str_init("TAILDROP");
static rl_algo_t rl_default_algo = PIPE_ALGO_NOP;
/* other functions */
static rl_algo_t get_rl_algo(str);
/* big hash table */
rl_big_htable rl_htable;
static int rl_dests_nr;
static rl_repl_dst_t *rl_dests;
/* feedback algorithm */
static int *rl_feedback_limit;
static cachedb_funcs cdbf;
static cachedb_con *cdbc = 0;
int rl_buffer_th = RL_BUF_THRESHOLD;
/* returnes the idex of the pipe in our hash */
#define RL_GET_INDEX(_n) core_hash(&(_n), NULL, rl_htable.size);
/* gets the lock associated with the hash index */
#define RL_GET_LOCK(_l) \
lock_set_get(rl_htable.locks, ((_l) % rl_htable.locks_no))
/* releases the lock associated with the hash index */
#define RL_RELEASE_LOCK(_l) \
lock_set_release(rl_htable.locks, ((_l) % rl_htable.locks_no))
/* retrieves the structure associated with the index and key */
#define RL_GET_PIPE(_i, _k) \
(rl_pipe_t **)map_get(rl_htable.maps[(_i)], _k)
#define RL_FIND_PIPE(_i, _k) \
(rl_pipe_t **)map_find(rl_htable.maps[(_i)], _k)
/* returns true if the pipe should use cachedb interface */
#define RL_USE_CDB(_p) \
(cdbc && (_p)->algo!=PIPE_ALGO_NETWORK && (_p)->algo!=PIPE_ALGO_FEEDBACK)
static str rl_name_buffer = {0, 0};
static inline int rl_set_name(str * name)
{
if (name->len + db_prefix.len > rl_name_buffer.len) {
rl_name_buffer.len = name->len + db_prefix.len;
rl_name_buffer.s = pkg_realloc(rl_name_buffer.s,
rl_name_buffer.len);
if (!rl_name_buffer.s) {
LM_ERR("cannot realloc buffer\n");
rl_name_buffer.len = 0;
return -1;
}
}
memcpy(rl_name_buffer.s + db_prefix.len, name->s, name->len);
rl_name_buffer.len = name->len + db_prefix.len;
return 0;
}
/* NOTE: assumes that the pipe has been locked. If fails, releases the lock */
static int rl_change_counter(str *name, rl_pipe_t *pipe, int c)
{
int new_counter;
int ret;
if (rl_set_name(name) < 0)
return -1;
if (pipe->my_counter + c < 0) {
LM_DBG("Counter going negative\n");
return 1;
}
if (c) {
if (c < 0)
ret = cdbf.sub(cdbc, &rl_name_buffer, -c, rl_expire_time, &new_counter);
else
ret = cdbf.add(cdbc, &rl_name_buffer, c, rl_expire_time, &new_counter);
} else {
if (pipe->my_counter) {
ret = cdbf.sub(cdbc, &rl_name_buffer, pipe->my_counter, rl_expire_time,
&new_counter);
} else {
ret = cdbf.get_counter(cdbc, &rl_name_buffer, &new_counter);
}
}
if (ret < 0) {
LM_ERR("cannot change counter for pipe %.*s with %d\n",
name->len, name->s, c);
return -1;
}
pipe->my_counter = c ? pipe->my_counter + c : 0;
pipe->counter = new_counter;
LM_DBG("changed with %d; my_counter: %d; counter: %d\n",
c, pipe->my_counter, new_counter);
return 0;
}
/* NOTE: assumes that the pipe has been locked */
static int rl_get_counter(str *name, rl_pipe_t * pipe)
{
int new_counter;
if (rl_set_name(name) < 0)
return -1;
if (cdbf.get_counter(cdbc, &rl_name_buffer, &new_counter) < 0) {
LM_ERR("cannot retrieve key\n");
return -1;
}
pipe->counter = new_counter;
return 0;
}
int init_cachedb(str * db_url)
{
if (cachedb_bind_mod(db_url, &cdbf) < 0) {
LM_ERR("cannot bind functions for db_url %.*s\n",
db_url->len, db_url->s);
return -1;
}
if (!CACHEDB_CAPABILITY(&cdbf,
CACHEDB_CAP_GET|CACHEDB_CAP_ADD|CACHEDB_CAP_SUB)) {
LM_ERR("not enough capabilities\n");
return -1;
}
cdbc = cdbf.init(db_url);
if (!cdbc) {
LM_ERR("cannot connect to db_url %.*s\n", db_url->len, db_url->s);
return -1;
}
/* guessing that the name is not larger than 32 */
rl_name_buffer.len = db_prefix.len + 32;
rl_name_buffer.s = pkg_malloc(rl_name_buffer.len);
if (!rl_name_buffer.s) {
LM_ERR("no more pkg memory\n");
rl_name_buffer.len = 0;
return -1;
}
/* copy prefix - this is constant*/
memcpy(rl_name_buffer.s, db_prefix.s, db_prefix.len);
return 0;
}
void destroy_cachedb(void)
{
if (cdbc)
cdbf.destroy(cdbc);
if (rl_name_buffer.s)
pkg_free(rl_name_buffer.s);
}
int init_rl_table(unsigned int size)
{
unsigned int i;
rl_htable.maps = shm_malloc(sizeof(map_t) * size);
if (!rl_htable.maps) {
LM_ERR("no more shm memory\n");
return -1;
}
memset(rl_htable.maps, 0, sizeof(map_t) * size);
for (i = 0; i < size; i++) {
rl_htable.maps[i] = map_create(AVLMAP_SHARED);
if (!rl_htable.maps[i]) {
LM_ERR("cannot create map index %d\n", i);
goto error;
}
rl_htable.size++;
}
if (!rl_default_algo_s.s) {
LM_ERR("Default algorithm was not specified\n");
return -1;
}
/* resolve the default algorithm */
rl_default_algo = get_rl_algo(rl_default_algo_s);
if (rl_default_algo < 0) {
LM_ERR("unknown algoritm <%.*s>\n", rl_default_algo_s.len,
rl_default_algo_s.s);
return -1;
}
LM_DBG("default algorithm is %.*s [ %d ]\n",
rl_default_algo_s.len, rl_default_algo_s.s, rl_default_algo);
/* if at least 25% of the size locks can't be allocated
* we return an error */
for ( i = size; i > size / 4; i--) {
rl_htable.locks = lock_set_alloc(i);
if (!rl_htable.locks)
continue;
if (!lock_set_init(rl_htable.locks)) {
lock_set_dealloc(rl_htable.locks);
rl_htable.locks = 0;
continue;
}
break;
}
if (!rl_htable.locks) {
LM_ERR("unable to allocted at least %d locks for the hash table\n",
size/4);
goto error;
}
rl_htable.locks_no = i;
LM_DBG("%d locks allocated for %d hashsize\n",
rl_htable.locks_no, rl_htable.size);
return 0;
error:
mod_destroy();
return -1;
}
/* the map between names and algorithms */
struct {
str name;
rl_algo_t algo;
} rl_algo_names[] = {
{ str_init("NOP"), PIPE_ALGO_NOP},
{ str_init("RED"), PIPE_ALGO_RED},
{ str_init("TAILDROP"), PIPE_ALGO_TAILDROP},
{ str_init("FEEDBACK"), PIPE_ALGO_FEEDBACK},
{ str_init("NETWORK"), PIPE_ALGO_NETWORK},
{ { 0, 0 }, 0},
};
static rl_algo_t get_rl_algo(str name)
{
int i;
if (!name.s || !name.len)
return -1;
for ( i = 0 ; rl_algo_names[i].name.s ; i++) {
if (rl_algo_names[i].name.len == name.len &&
strncasecmp(rl_algo_names[i].name.s, name.s, name.len) == 0)
return rl_algo_names[i].algo;
}
return -1;
}
static str * get_rl_algo_name(rl_algo_t algo)
{
int i;
for (i = 0; rl_algo_names[i].name.s ; i++)
if (rl_algo_names[i].algo == algo)
return &rl_algo_names[i].name;
return NULL;
}
int w_rl_check_2(struct sip_msg *_m, char *_n, char *_l)
{
return w_rl_check_3(_m, _n, _l, NULL);
}
int w_rl_check_3(struct sip_msg *_m, char *_n, char *_l, char *_a)
{
str name;
int limit = 0, ret = 1, should_update = 0;
str algorithm;
unsigned int hash_idx;
rl_pipe_t **pipe;
rl_algo_t algo = -1;
/* retrieve and check parameters */
if (!_n || !_l) {
LM_ERR("invalid parameters\n");
goto end;
}
if (fixup_get_svalue(_m, (gparam_p)_n, &name) < 0) {
LM_ERR("cannot retrieve identifier\n");
goto end;
}
if (fixup_get_ivalue(_m, (gparam_p)_l, &limit) < 0) {
LM_ERR("cannot retrieve limit\n");
goto end;
}
algorithm.s = 0;
if (!_a || fixup_get_svalue(_m, (gparam_p)_a, &algorithm) < 0 ||
(algo = get_rl_algo(algorithm)) < 0) {
algo = PIPE_ALGO_NOP;
}
/* get limit for FEEDBACK algorithm */
if (algo == PIPE_ALGO_FEEDBACK) {
lock_get(rl_lock);
if (*rl_feedback_limit) {
if (*rl_feedback_limit != limit) {
LM_WARN("FEEDBACK limit should be the same for all pipes, but"
" new limit %d differs - setting to %d\n",
limit, *rl_feedback_limit);
limit = *rl_feedback_limit;
}
} else {
if (limit <= 0 || limit >= 100) {
LM_ERR("invalid limit for FEEDBACK algorithm "
"(must be between 0 and 100)\n");
lock_release(rl_lock);
goto end;
}
*rl_feedback_limit = limit;
pid_setpoint_limit(limit);
}
lock_release(rl_lock);
}
hash_idx = RL_GET_INDEX(name);
RL_GET_LOCK(hash_idx);
/* try to get the value */
pipe = RL_GET_PIPE(hash_idx, name);
if (!pipe) {
LM_ERR("cannot get the index\n");
goto release;
}
if (!*pipe) {
/* allocate new pipe */
*pipe = shm_malloc(sizeof(rl_pipe_t) +
rl_dests_nr * sizeof(rl_repl_counter_t));
if (!*pipe) {
LM_ERR("no more shm memory\n");
goto release;
}
memset(*pipe, 0, sizeof(rl_pipe_t) + rl_dests_nr * sizeof(rl_repl_counter_t));
(*pipe)->dsts = (rl_repl_counter_t *)((*pipe) + 1);
LM_DBG("Pipe %.*s doens't exist, but was created %p\n",
name.len, name.s, *pipe);
if (algo == PIPE_ALGO_NETWORK)
should_update = 1;
(*pipe)->algo = (algo == PIPE_ALGO_NOP) ? rl_default_algo : algo;
} else {
LM_DBG("Pipe %.*s found: %p - last used %lu\n",
name.len, name.s, *pipe, (*pipe)->last_used);
if (algo != PIPE_ALGO_NOP && (*pipe)->algo != algo) {
LM_WARN("algorithm %d different from the initial one %d for pipe "
"%.*s", algo, (*pipe)->algo, name.len, name.s);
}
}
/* set/update the limit */
(*pipe)->limit = limit;
/* set the last used time */
(*pipe)->last_used = time(0);
if (RL_USE_CDB(*pipe)) {
/* release the counter for a while */
if (rl_change_counter(&name, *pipe, 1) < 0) {
LM_ERR("cannot increase counter\n");
goto end;
}
} else {
(*pipe)->counter++;
}
ret = rl_pipe_check(*pipe);
LM_DBG("Pipe %.*s counter:%d load:%d limit:%d should %sbe blocked (%p)\n",
name.len, name.s, (*pipe)->counter, (*pipe)->load,
(*pipe)->limit, ret == 1? "NOT " : "", *pipe);
release:
RL_RELEASE_LOCK(hash_idx);
if (should_update) {
lock_get(rl_lock);
(*rl_network_count)++;
lock_release(rl_lock);
}
end:
return ret;
}
/* timer housekeeping, invoked each timer interval to reset counters */
void rl_timer(unsigned int ticks, void *param)
{
unsigned int i = 0;
map_iterator_t it, del;
rl_pipe_t **pipe;
str *key;
void *value;
unsigned long now = time(0);
/* get CPU load */
if (get_cpuload() < 0) {
LM_ERR("cannot update CPU load\n");
i = 1;
}
lock_get(rl_lock);
/* if CPU was successfully loaded */
if (!i)
do_update_load();
/* update network if needed */
if (*rl_network_count)
*rl_network_load = get_total_bytes_waiting(PROTO_NONE);
lock_release(rl_lock);
/* iterate through each map */
for (i = 0; i < rl_htable.size; i++) {
RL_GET_LOCK(i);
/* iterate through all the entries */
if (map_first(rl_htable.maps[i], &it) < 0) {
LM_ERR("map doesn't exist\n");
goto next_map;
}
for (; iterator_is_valid(&it);) {
pipe = (rl_pipe_t **)iterator_val(&it);
if (!pipe || !*pipe) {
LM_ERR("[BUG] bogus map[%d] state\n", i);
goto next_pipe;
}
key = iterator_key(&it);
if (!key) {
LM_ERR("cannot retrieve pipe key\n");
goto next_pipe;
}
/* check to see if it is expired */
if ((*pipe)->last_used + rl_expire_time < now) {
/* this pipe is engaged in a transaction */
del = it;
if (iterator_next(&it) < 0)
LM_DBG("cannot find next iterator\n");
if ((*pipe)->algo == PIPE_ALGO_NETWORK) {
lock_get(rl_lock);
(*rl_network_count)--;
lock_release(rl_lock);
}
LM_DBG("Deleting ratelimit pipe key \"%.*s\"\n",
key->len, key->s);
value = iterator_delete(&del);
/* free resources */
if (value)
shm_free(value);
continue;
} else {
/* leave the lock if a cachedb query should be done*/
if (RL_USE_CDB(*pipe)) {
if (rl_get_counter(key, *pipe) < 0) {
LM_ERR("cannot get pipe counter\n");
goto next_pipe;
}
}
switch ((*pipe)->algo) {
case PIPE_ALGO_NETWORK:
/* handle network algo */
(*pipe)->load =
(*rl_network_load > (*pipe)->limit) ? -1 : 1;
break;
case PIPE_ALGO_RED:
if ((*pipe)->limit && rl_timer_interval)
(*pipe)->load = (*pipe)->counter /
((*pipe)->limit * rl_timer_interval);
break;
default:
break;
}
(*pipe)->last_counter = rl_get_all_counters(*pipe);
if (RL_USE_CDB(*pipe)) {
if (rl_change_counter(key, *pipe, 0) < 0) {
LM_ERR("cannot reset counter\n");
}
} else {
(*pipe)->counter = 0;
}
}
next_pipe:
if (iterator_next(&it) < 0)
break;
}
next_map:
RL_RELEASE_LOCK(i);
}
}
struct rl_param_t {
int counter;
struct mi_node * node;
struct mi_root * root;
};
static int rl_map_print(void *param, str key, void *value)
{
struct mi_attr* attr;
char* p;
int len;
struct rl_param_t * rl_param = (struct rl_param_t *)param;
struct mi_node * rpl;
rl_pipe_t *pipe = (rl_pipe_t *)value;
struct mi_node * node;
str *alg;
if (!pipe) {
LM_ERR("invalid pipe value\n");
return -1;
}
if (!rl_param || !rl_param->node || !rl_param->root) {
LM_ERR("no reply node\n");
return -1;
}
rpl = rl_param->node;
if (!key.len || !key.s) {
LM_ERR("no key found\n");
return -1;
}
/* skip if no algo */
if (pipe->algo == PIPE_ALGO_NOP)
return 0;
if (!(node = add_mi_node_child(rpl, 0, "PIPE", 4, 0, 0)))
return -1;
if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "id", 2, key.s, key.len)))
return -1;
if (!(alg = get_rl_algo_name(pipe->algo))) {
LM_ERR("[BUG] unknown algorithm %d\n", pipe->algo);
return -1;
}
if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "algorithm", 9,
alg->s, alg->len)))
return -1;
p = int2str((unsigned long)(pipe->limit), &len);
if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "limit", 5, p, len)))
return -1;
p = int2str((unsigned long)(pipe->last_counter), &len);
if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "counter", 7, p, len)))
return -1;
if ((++rl_param->counter % 50) == 0) {
LM_DBG("flush mi tree - number %d\n", rl_param->counter);
flush_mi_tree(rl_param->root);
}
return 0;
}
int rl_stats(struct mi_root *rpl_tree, str * value)
{
rl_pipe_t **pipe;
struct rl_param_t param;
int i;
memset(¶m, 0, sizeof(struct rl_param_t));
param.node = &rpl_tree->node;
param.root = rpl_tree;
if (value && value->s && value->len) {
i = RL_GET_INDEX(*value);
RL_GET_LOCK(i);
pipe = RL_FIND_PIPE(i, *value);
if (!pipe || !*pipe) {
LM_DBG("pipe %.*s not found\n", value->len, value->s);
goto error;
}
if (rl_map_print(¶m, *value, *pipe)) {
LM_ERR("cannot print value for key %.*s\n",
value->len, value->s);
goto error;
}
RL_RELEASE_LOCK(i);
} else {
/* iterate through each map */
for (i = 0; i < rl_htable.size; i++) {
RL_GET_LOCK(i);
if (map_for_each(rl_htable.maps[i], rl_map_print, ¶m)) {
LM_ERR("cannot print values\n");
goto error;
}
RL_RELEASE_LOCK(i);
}
}
return 0;
error:
RL_RELEASE_LOCK(i);
return -1;
}
int rl_bin_status(struct mi_root *rpl_tree)
{
int index = 0;
struct mi_node *node;
char* p;
int len;
for (index = 0; index < rl_dests_nr; index++) {
if (!(node = add_mi_node_child(&rpl_tree->node, 0, "Instance", 8,
rl_dests[index].dst.s, rl_dests[index].dst.len))) {
LM_ERR("cannot add a new instance\n");
return -1;
}
if (*rl_dests[index].last_msg) {
p = int2str((unsigned long)(*rl_dests[index].last_msg), &len);
} else {
p = "never";
len = 5;
}
if (!add_mi_attr(node, MI_DUP_VALUE, "timestamp", 9, p, len)) {
LM_ERR("cannot add last update\n");
return -1;
}
}
return 0;
}
int w_rl_set_count(str key, int val)
{
unsigned int hash_idx;
int ret = -1;
rl_pipe_t **pipe;
hash_idx = RL_GET_INDEX(key);
RL_GET_LOCK(hash_idx);
/* try to get the value */
pipe = RL_FIND_PIPE(hash_idx, key);
if (!pipe || !*pipe) {
LM_DBG("cannot find any pipe named %.*s\n", key.len, key.s);
goto release;
}
if (RL_USE_CDB(*pipe)) {
if (rl_change_counter(&key, *pipe, val) < 0) {
LM_ERR("cannot decrease counter\n");
goto release;
}
} else {
if (val && (val + (*pipe)->counter >= 0)) {
(*pipe)->counter += val;
} else {
(*pipe)->counter = 0;
}
}
LM_DBG("new counter for key %.*s is %d\n",
key.len, key.s, (*pipe)->counter);
ret = 0;
release:
RL_RELEASE_LOCK(hash_idx);
return ret;
}
static inline int w_rl_change_counter(struct sip_msg *_m, char *_n, int dec)
{
str name;
if (!_n || fixup_get_svalue(_m, (gparam_p)_n, &name) < 0) {
LM_ERR("cannot retrieve identifier\n");
return -1;
}
if (w_rl_set_count(name, dec)) {
LM_ERR("cannot find any pipe named %.*s\n", name.len, name.s);
return -1;
}
return 1;
}
int w_rl_dec(struct sip_msg *_m, char *_n)
{
return w_rl_change_counter(_m, _n, -1);
}
int w_rl_reset(struct sip_msg *_m, char *_n)
{
return w_rl_change_counter(_m, _n, 0);
}
void rl_rcv_bin(int packet_type, struct receive_info *ri)
{
rl_algo_t algo;
int limit;
int counter;
str name;
int index;
char *ip;
unsigned short port;
rl_pipe_t **pipe;
unsigned int hash_idx;
time_t now;
if (packet_type != RL_PIPE_COUNTER)
return;
/* match the server */
for (index = 0; index < rl_dests_nr; index++) {
if (su_cmp(&ri->src_su, &rl_dests[index].to))
break;
}
if (index == rl_dests_nr) {
get_su_info(&ri->src_su.s, ip, port);
LM_WARN("received bin packet from unknown source: %s:%hu\n",
ip, port);
return;
}
now = time(0);
*rl_dests[index].last_msg = now;
for (;;) {
if (bin_pop_str(&name) == 1)
break; /* pop'ed all pipes */
if (bin_pop_int(&algo) < 0) {
LM_ERR("cannot pop pipe's algorithm\n");
return;
}
if (bin_pop_int(&limit) < 0) {
LM_ERR("cannot pop pipe's limit\n");
return;
}
if (bin_pop_int(&counter) < 0) {
LM_ERR("cannot pop pipe's counter\n");
return;
}
hash_idx = RL_GET_INDEX(name);
RL_GET_LOCK(hash_idx);
/* try to get the value */
pipe = RL_GET_PIPE(hash_idx, name);
if (!pipe) {
LM_ERR("cannot get the index\n");
goto release;
}
if (!*pipe) {
/* if the pipe does not exist, alocate it in case we need it later */
*pipe = shm_malloc(sizeof(rl_pipe_t) +
rl_dests_nr * sizeof(rl_repl_counter_t));
if (!*pipe) {
LM_ERR("no more shm memory\n");
goto release;
}
memset(*pipe, 0, sizeof(rl_pipe_t) +
rl_dests_nr * sizeof(rl_repl_counter_t));
(*pipe)->dsts = (rl_repl_counter_t *)((*pipe) + 1);
LM_DBG("Pipe %.*s doesn't exist, but was created %p\n",
name.len, name.s, *pipe);
(*pipe)->algo = algo;
(*pipe)->limit = limit;
} else {
LM_DBG("Pipe %.*s found: %p - last used %lu\n",
name.len, name.s, *pipe, (*pipe)->last_used);
if ((*pipe)->algo != algo)
LM_WARN("algorithm %d different from the initial one %d for "
"pipe %.*s", algo, (*pipe)->algo, name.len, name.s);
if ((*pipe)->limit != limit)
LM_WARN("limit %d different from the initial one %d for "
"pipe %.*s", limit, (*pipe)->limit, name.len, name.s);
}
/* set the last used time */
(*pipe)->last_used = time(0);
/* set the destination's counter */
(*pipe)->dsts[index].counter = counter;
(*pipe)->dsts[index].update = now;
RL_RELEASE_LOCK(hash_idx);
}
return;
release:
RL_RELEASE_LOCK(hash_idx);
}
int rl_repl_init(void)
{
int index;
if (rl_buffer_th > (BUF_SIZE * 0.9)) {
LM_WARN("Buffer size too big %d - pipe information might get lost",
rl_buffer_th);
return -1;
}
if (rl_dests_nr &&
bin_register_cb("ratelimit", rl_rcv_bin) < 0) {
LM_ERR("Cannot register binary packet callback!\n");
return -1;
}
/* alocate the last_message counter in shared memory */
for (index = 0; index < rl_dests_nr; index++) {
rl_dests[index].last_msg = shm_malloc(sizeof(time_t));
if (!rl_dests[index].last_msg) {
LM_ERR("OOM shm\n");
return -1;
}
}
return 0;
}
int rl_add_repl_dst(modparam_t type, void *val)
{
char *host;
int hlen, port;
int proto;
struct hostent *he;
str st;
rl_dests = pkg_realloc(rl_dests, (rl_dests_nr + 1) * sizeof(rl_repl_dst_t));
if (!rl_dests) {
LM_ERR("oom\n");
return -1;
}
if (parse_phostport(val, strlen(val), &host, &hlen, &port, &proto) < 0) {
LM_ERR("Bad replication destination IP!\n");
return -1;
}
if (proto == PROTO_NONE)
proto = PROTO_UDP;
st.s = host;
st.len = hlen;
he = sip_resolvehost(&st, (unsigned short *)&port,
(unsigned short *)&proto, 0, 0);
if (!he) {
LM_ERR("Cannot resolve host: %.*s\n", hlen, host);
return -1;
}
if (!port) {
LM_ERR("no port specified for host %.*s\n", hlen, host);
return -1;
}
rl_dests[rl_dests_nr].id = rl_dests_nr;
rl_dests[rl_dests_nr].dst.s = (char *)val;
rl_dests[rl_dests_nr].dst.len = strlen(rl_dests[rl_dests_nr].dst.s);
hostent2su(&rl_dests[rl_dests_nr].to, he, 0, port);
LM_DBG("Added destination <%.*s>\n",
rl_dests[rl_dests_nr].dst.len, rl_dests[rl_dests_nr].dst.s);
/* init done */
rl_dests_nr++;
return 1;
}
static inline void rl_replicate(void)
{
unsigned i;
for (i = 0; i < rl_dests_nr; i++)
bin_send(&rl_dests[i].to);
}
void rl_timer_repl(utime_t ticks, void *param)
{
static str module_name = str_init("ratelimit");
unsigned int i = 0;
map_iterator_t it;
rl_pipe_t **pipe;
str *key;
int nr = 0;
int ret;
if (bin_init(&module_name, RL_PIPE_COUNTER) < 0) {
LM_ERR("cannot initiate bin buffer\n");
return;
}
/* iterate through each map */
for (i = 0; i < rl_htable.size; i++) {
RL_GET_LOCK(i);
/* iterate through all the entries */
if (map_first(rl_htable.maps[i], &it) < 0) {
LM_ERR("map doesn't exist\n");
goto next_map;
}
for (; iterator_is_valid(&it);) {
pipe = (rl_pipe_t **)iterator_val(&it);
if (!pipe || !*pipe) {
LM_ERR("[BUG] bogus map[%d] state\n", i);
goto next_pipe;
}
/* ignore cachedb replicated stuff */
if (RL_USE_CDB(*pipe))
goto next_pipe;
key = iterator_key(&it);
if (!key) {
LM_ERR("cannot retrieve pipe key\n");
goto next_pipe;
}
if (bin_push_str(key) < 0)
goto error;
if (bin_push_int((*pipe)->algo) < 0)
goto error;
if (bin_push_int((*pipe)->limit) < 0)
goto error;
if ((ret = bin_push_int((*pipe)->counter)) < 0)
goto error;
nr++;
if (ret > rl_buffer_th) {
/* send the buffer */
if (nr)
rl_replicate();
if (bin_init(&module_name, RL_PIPE_COUNTER) < 0) {
LM_ERR("cannot initiate bin buffer\n");
RL_RELEASE_LOCK(i);
return;
}
nr = 0;
}
next_pipe:
if (iterator_next(&it) < 0)
break;
}
next_map:
RL_RELEASE_LOCK(i);
}
/* if there is anything else to send, do it now */
if (nr)
rl_replicate();
return;
error:
LM_ERR("cannot add pipe info in buffer\n");