-
Notifications
You must be signed in to change notification settings - Fork 564
/
dispatch.c
2489 lines (2119 loc) · 59.9 KB
/
dispatch.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
/*
* dispatcher module
*
* Copyright (C) 2010-2015 OpenSIPS Solutions
* Copyright (C) 2005-2010 Voice-System.ro
* Copyright (C) 2004-2006 FhG Fokus
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../ut.h"
#include "../../trim.h"
#include "../../dprint.h"
#include "../../action.h"
#include "../../route.h"
#include "../../dset.h"
#include "../../mem/shm_mem.h"
#include "../../parser/parse_uri.h"
#include "../../parser/parse_from.h"
#include "../../usr_avp.h"
#include "../../mi/mi.h"
#include "../../parser/digest/digest.h"
#include "../../resolve.h"
#include "../tm/tm_load.h"
#include "../../db/db.h"
#include "../../db/db_res.h"
#include "../../str.h"
#include "../../rw_locking.h"
#include "dispatch.h"
#include "ds_fixups.h"
#include "ds_bl.h"
#include "ds_clustering.h"
#define DS_TABLE_VERSION 8
/**
* in version 8, the "weight" column is given as a string, since it can contain
* both integer (the weight) or URL definitions (dynamically calculated weight)
*
* OpenSIPS retains backwards-compatibility with the former integer column flavor
*/
#define supported_ds_version(_ver) \
(DS_TABLE_VERSION == 8 ? (_ver == 8 || _ver == 7) : _ver == DS_TABLE_VERSION)
extern ds_partition_t *partitions;
extern struct socket_info *probing_sock;
extern event_id_t dispatch_evi_id;
extern ds_partition_t *default_partition;
struct tm_binds tmb;
struct fs_binds fs_api;
#define dst_is_active(_dst) \
(!((_dst).flags&(DS_INACTIVE_DST|DS_PROBING_DST)))
int init_ds_data(ds_partition_t *partition)
{
partition->data = (ds_data_t**)shm_malloc( sizeof(ds_data_t*) );
if (partition->data==NULL) {
LM_ERR("failed to allocate data holder in shm\n");
return -1;
}
*partition->data = NULL;
/* create & init lock */
if ((partition->lock = lock_init_rw()) == NULL) {
LM_CRIT("failed to init reader/writer lock\n");
return -1;
}
return 0;
}
/* destroy entire dispatching data */
static void ds_destroy_data_set( ds_data_t *d)
{
ds_set_p sp;
ds_set_p sp_curr;
ds_dest_p dest;
str ds_str = {MI_SSTR("dispatcher")};
/* free the list of sets */
sp = d->sets;
while(sp) {
sp_curr = sp;
sp = sp->next;
dest = sp_curr->dlist;
if (dest) {
do {
if(dest->uri.s!=NULL)
shm_free(dest->uri.s);
if(dest->param)
shm_free(dest->param);
if (dest->fs_sock)
fs_api.put_stats_evs(dest->fs_sock, &ds_str);
dest = dest->next;
}while(dest);
shm_free(sp_curr->dlist);
}
shm_free(sp_curr);
}
/* free the data holder */
shm_free(d);
}
/* destroy current dispatching data */
void ds_destroy_data(ds_partition_t *partition)
{
if (partition->data && *partition->data)
ds_destroy_data_set( *partition->data );
/* destroy rw lock */
if (partition->lock) {
lock_destroy_rw( partition->lock );
partition->lock = 0;
}
}
int add_dest2list(int id, str uri, struct socket_info *sock, str *comsock, int state,
int weight, int prio, str attrs, str description, ds_data_t *d_data)
{
ds_dest_p dp = NULL;
ds_set_p sp = NULL;
short new_set = 0;
ds_dest_p dp_it, dp_prev;
struct sip_uri puri;
str ds_str = {MI_SSTR("dispatcher")};
/* For DNS-Lookups */
struct proxy_l *proxy;
union sockaddr_union sau;
/* check uri */
if(parse_uri(uri.s, uri.len, &puri)!=0 || puri.host.len>254)
{
LM_ERR("bad uri [%.*s]\n", uri.len, uri.s);
goto err;
}
/* get dest set */
for( sp=d_data->sets ; sp ; sp=sp->next) {
if(sp->id == id)
break;
}
if(sp==NULL)
{
sp = (ds_set_p)shm_malloc(sizeof(ds_set_t));
if(sp==NULL)
{
LM_ERR("no more memory.\n");
return -1;
}
new_set = 1;
memset(sp, 0, sizeof(ds_set_t));
sp->id = id;
}
dp = (ds_dest_p)shm_malloc(sizeof(ds_dest_t));
if(dp==NULL)
{
LM_ERR("no more memory!\n");
goto err;
}
memset(dp, 0, sizeof(ds_dest_t));
/* store uri and attrs strings */
dp->uri.len = uri.len;
if (puri.user.len == 0 && puri.passwd.len == 0 && puri.headers.len == 0) {
/* The uri from db is good for ds_select_dst */
dp->uri.s = shm_malloc(uri.len + 1 + attrs.len + 1 + description.len + 1);
if(dp->uri.s==NULL){
LM_ERR("no more shm memory!\n");
goto err;
}
dp->dst_uri = dp->uri;
dp->attrs.s = dp->uri.s + dp->uri.len + 1;
dp->description.s = dp->uri.s + dp->uri.len + 1 + attrs.len + 1;
}
else {
dp->dst_uri.len = uri_typestrlen(puri.type) + 1 + puri.host.len
+ (puri.port.len ? puri.port.len + 1 : 0) + puri.params.len;
dp->uri.s = shm_malloc(uri.len+1 + dp->dst_uri.len + 1 + attrs.len+1
+ description.len + 1);
if(dp->uri.s==NULL){
LM_ERR("no more shm memory!\n");
goto err;
}
dp->description.s = dp->uri.s + dp->uri.len + 1 + dp->dst_uri.len + 1 + attrs.len + 1;
dp->attrs.s = dp->uri.s + dp->uri.len + 1 + dp->dst_uri.len + 1;
dp->dst_uri.s = dp->uri.s + dp->uri.len + 1;
char *p = uri_type2str(puri.type, dp->dst_uri.s);
*(p++) = ':';
memcpy(p, puri.host.s, puri.host.len);
p += puri.host.len;
if (puri.port.len) {
*(p++) = ':';
memcpy(p, puri.port.s, puri.port.len);
}
if (puri.params.len) {
memcpy(p, puri.params.s, puri.params.len);
p += puri.params.len;
}
dp->dst_uri.s[dp->dst_uri.len]='\0';
}
memcpy(dp->uri.s, uri.s, dp->uri.len);
if (attrs.len) {
memcpy(dp->attrs.s, attrs.s, attrs.len);
dp->attrs.s[attrs.len]='\0';
dp->attrs.len = attrs.len;
}
else dp->attrs.s = NULL;
if(description.len){
memcpy(dp->description.s, description.s, description.len);
dp->description.s[description.len]='\0';
dp->description.len = description.len;
}
/* copy state, weight & socket */
dp->sock = sock;
dp->weight = weight;
switch (state) {
case 0:
dp->flags = 0;
break;
case 1:
dp->flags = DS_INACTIVE_DST;
break;
case 2:
dp->flags = DS_PROBING_DST;
break;
default:
LM_CRIT("BUG: unknown state %d for destination %.*s\n",
state, uri.len, uri.s);
}
/* Do a DNS-Lookup for the Host-Name: */
proxy = mk_proxy( &puri.host, puri.port_no, puri.proto,
(puri.type==SIPS_URI_T));
if (proxy==NULL) {
LM_ERR("could not resolve %.*s, skipping it\n",
puri.host.len, puri.host.s);
goto err;
}
hostent2ip_addr( &dp->ips[0], &proxy->host, proxy->addr_idx);
dp->ports[0] = proxy->port;
dp->protos[0] = proxy->proto;
dp->ips_cnt = 1;
dp->priority = prio;
LM_DBG("first gw ip addr [%s]:%d\n",
ip_addr2a(&dp->ips[0]), dp->ports[0]);
/* get the next available IPs from DNS */
while (dp->ips_cnt<DS_MAX_IPS && (get_next_su( proxy, &sau, 0)==0) ) {
su2ip_addr( &dp->ips[dp->ips_cnt], &sau);
dp->ports[dp->ips_cnt] = proxy->port;
dp->protos[dp->ips_cnt] = proxy->proto;
LM_DBG("additional gw ip addr [%s]:%d, proto %d\n",
ip_addr2a(&dp->ips[dp->ips_cnt]),
dp->ports[dp->ips_cnt], dp->protos[dp->ips_cnt]);
/* one more IP found */
dp->ips_cnt++;
}
/* free al the helper structures */
free_proxy(proxy);
pkg_free(proxy);
if (fetch_freeswitch_stats) {
if (comsock->s && comsock->len > 0) {
dp->fs_sock = fs_api.get_stats_evs(comsock, &ds_str);
if (!dp->fs_sock) {
LM_ERR("failed to create FreeSWITCH stats socket!\n");
} else {
dp->weight = max_freeswitch_weight;
if (sp->redo_weights == 0) {
for (dp_it = sp->dlist; dp_it; dp_it = dp_it->next) {
if (dp_it->weight > max_freeswitch_weight) {
LM_WARN("(set %d) truncating static weight in "
"uri %.*s to 'max_freeswitch_weight'! (%d->%d)\n",
id, uri.len, uri.s, dp_it->weight, max_freeswitch_weight);
dp_it->weight = max_freeswitch_weight;
}
}
sp->redo_weights = 1;
}
}
} else if (sp->redo_weights && dp->weight > max_freeswitch_weight) {
LM_WARN("(set %d) truncating static weight in uri %.*s to"
"\"max_freeswitch_weight\"! (%d -> %d)\n", id,
uri.len, uri.s, weight, max_freeswitch_weight);
dp->weight = max_freeswitch_weight;
}
}
/*
* search the proper place based on priority
* put them in reverse order, since they will be reindexed
*/
for (dp_prev = NULL, dp_it = sp->dlist;
dp_it && dp_it->priority < prio;
dp_prev = dp_it, dp_it = dp_it->next);
if (!dp_prev) {
dp->next = sp->dlist;
sp->dlist = dp;
} else {
dp->next = dp_prev->next;
dp_prev->next = dp;
}
sp->nr++;
if (new_set) {
sp->next = d_data->sets;
d_data->sets = sp;
d_data->sets_no++;
}
LM_DBG("dest [%d/%d] <%.*s> <%.*s> successfully loaded\n", sp->id, sp->nr,
dp->uri.len, dp->uri.s, dp->dst_uri.len, dp->dst_uri.s);
return 0;
err:
/* free allocated memory */
if(dp!=NULL)
{
if(dp->uri.s!=NULL)
shm_free(dp->uri.s);
shm_free(dp);
}
if (sp != NULL && new_set)
shm_free(sp);
return -1;
}
/* iterates the whole set and calculates (1) the number of
active destinations and (2) the running and total weight
sum for the active destinations */
static inline void re_calculate_active_dsts(ds_set_p sp)
{
int j,i;
ds_dest_p dst;
int oldw;
/* pre-calculate the running weights for each destination */
for( j=0,i=-1,sp->active_nr=sp->nr ; j<sp->nr ; j++ ) {
dst = &sp->dlist[j];
if (dst->fs_sock && dst->fs_sock->stats.valid) {
lock_start_read(dst->fs_sock->stats_lk);
oldw = dst->weight;
dst->weight = round(max_freeswitch_weight *
(1 - dst->fs_sock->stats.sess /
(float)dst->fs_sock->stats.max_sess) *
(dst->fs_sock->stats.id_cpu / (float)100));
LM_DBG("weight update for %.*s: %d -> %d (%d %d %.3f)\n",
dst->uri.len, dst->uri.s, oldw, dst->weight,
dst->fs_sock->stats.sess, dst->fs_sock->stats.max_sess,
dst->fs_sock->stats.id_cpu);
lock_stop_read(dst->fs_sock->stats_lk);
}
/* running weight is the current weight plus the running weight of
* the previous element */
dst->running_weight = dst->weight
+ ((j==0) ? 0 : sp->dlist[j-1].running_weight);
/* now the running weight for the active destinations */
if ( dst_is_active(*dst)) {
dst->active_running_weight = dst->weight
+ ((i==-1) ? 0 : sp->dlist[i].active_running_weight);
i = j; /* last active destination */
} else {
dst->active_running_weight =
((i==-1) ? 0 : sp->dlist[i].active_running_weight);
sp->active_nr --;
}
LM_DBG("destination i=%d, j=%d, weight=%d, sum=%d, active_sum=%d\n",
i,j, dst->weight, dst->running_weight, dst->active_running_weight);
}
}
/* compact destinations from sets for fast access */
int reindex_dests( ds_data_t *d_data)
{
int j;
ds_set_p sp = NULL;
ds_dest_p dp = NULL, dp0= NULL;
for( sp=d_data->sets ; sp!= NULL ; sp=sp->next )
{
if (sp->nr == 0) {
dp0 = NULL;
continue;
}
dp0 = (ds_dest_p)shm_malloc(sp->nr*sizeof(ds_dest_t));
if(dp0==NULL)
{
LM_ERR("no more memory!\n");
goto err1;
}
memset(dp0, 0, sp->nr*sizeof(ds_dest_t));
/*copy from the old pointer to destination, and then free it*/
for(j=sp->nr-1; j>=0 && sp->dlist!= NULL; j--)
{
memcpy(&dp0[j], sp->dlist, sizeof(ds_dest_t));
if(j==sp->nr-1)
dp0[j].next = NULL;
else
dp0[j].next = &dp0[j+1];
dp = sp->dlist;
sp->dlist = dp->next;
shm_free(dp);
dp=NULL;
}
sp->dlist=dp0;
re_calculate_active_dsts(sp);
}
LM_DBG("found [%d] dest sets\n", d_data->sets_no);
return 0;
err1:
return -1;
}
/* variables used to generate the pvar name */
static int ds_has_pattern = 0;
static str ds_pattern_prefix = str_init("");
static str ds_pattern_suffix = str_init("");
void ds_pvar_parse_pattern(str pattern)
{
char *p, *end;
ds_pattern_prefix = pattern;
end = pattern.s + pattern.len - DS_PV_ALGO_MARKER_LEN + 1;
/* first try to see if we have the marker */
for (p = pattern.s; p < end &&
memcmp(p, DS_PV_ALGO_MARKER, DS_PV_ALGO_MARKER_LEN); p++);
/* if reached end - pattern not present => pure pvar */
if (p == end) {
LM_DBG("Pattern not found\n");
return;
}
ds_has_pattern = 1;
ds_pattern_prefix.len = p - pattern.s;
/* skip marker */
ds_pattern_suffix.s = p + DS_PV_ALGO_MARKER_LEN;
ds_pattern_suffix.len = pattern.s + pattern.len - ds_pattern_suffix.s;
}
ds_pvar_param_p ds_get_pvar_param(str uri)
{
str name;
int len = ds_pattern_prefix.len + uri.len + ds_pattern_suffix.len;
char buf[len]; /* XXX: check if this works for all compilers */
ds_pvar_param_p param;
if (ds_has_pattern) {
name.len = 0;
name.s = buf;
memcpy(buf, ds_pattern_prefix.s, ds_pattern_prefix.len);
name.len = ds_pattern_prefix.len;
memcpy(name.s + name.len, uri.s, uri.len);
name.len += uri.len;
memcpy(name.s + name.len, ds_pattern_suffix.s, ds_pattern_suffix.len);
name.len += ds_pattern_suffix.len;
}
param = shm_malloc(sizeof(ds_pvar_param_t));
if (!param) {
LM_ERR("no more shm memory\n");
return NULL;
}
if (!pv_parse_spec(ds_has_pattern ? &name : &ds_pattern_prefix,
¶m->pvar)) {
LM_ERR("cannot parse pattern spec\n");
shm_free(param);
return NULL;
}
return param;
}
int ds_pvar_algo(struct sip_msg *msg, ds_set_p set, ds_dest_p **sorted_set,
int ds_use_default)
{
pv_value_t val;
int i, j, k, end_idx, cnt;
ds_dest_p *sset;
ds_pvar_param_p param;
if (!set) {
LM_ERR("invalid set\n");
return -1;
}
sset = shm_realloc(*sorted_set, set->nr * sizeof(ds_dest_p));
if (!sset) {
LM_ERR("no more shm memory\n");
return -1;
}
*sorted_set = sset;
end_idx = set->nr - 1;
if (ds_use_default) {
sset[end_idx] = &set->dlist[end_idx];
end_idx--;
}
for (i = 0, cnt = 0; i < set->nr - (ds_use_default?1:0); i++) {
if ( !dst_is_active(set->dlist[i]) ) {
/* move to the end of the list */
sset[end_idx--] = &set->dlist[i];
continue;
}
/* if pvar not set - try to evaluate it */
if (set->dlist[i].param == NULL) {
param = ds_get_pvar_param(set->dlist[i].uri);
if (param == NULL) {
LM_ERR("cannot parse pvar for uri %.*s\n",
set->dlist[i].uri.len, set->dlist[i].uri.s);
continue;
}
set->dlist[i].param = (void *)param;
} else {
param = (ds_pvar_param_p)set->dlist[i].param;
}
if (pv_get_spec_value(msg, ¶m->pvar, &val) < 0) {
LM_ERR("cannot get spec value for spec %.*s\n",
set->dlist[i].uri.len, set->dlist[i].uri.s);
continue;
}
if (!(val.flags & PV_VAL_NULL)) {
if (!(val.flags & PV_VAL_INT)) {
/* last attempt to retrieve value */
if (!str2sint(&val.rs, ¶m->value)) {
LM_ERR("invalid pvar value type - not int\n");
continue;
}
} else {
param->value = val.ri;
}
} else {
param->value = 0;
}
/* search the proper position */
j = 0;
for (; j < cnt && ((ds_pvar_param_p)sset[j]->param)->value <= param->value; j++);
/* make space for the new entry */
for (k = cnt; k > j; k--)
sset[k] = sset[k - 1];
sset[j] = &set->dlist[i];
cnt++;
}
return cnt;
}
int ds_connect_db(ds_partition_t *partition)
{
if(!partition->db_url.s)
return -1;
if (*partition->db_handle)
{
LM_CRIT("BUG - db connection found already open\n");
return -1;
}
if ((*partition->db_handle = partition->dbf.init(&partition->db_url)) == 0)
return -1;
return 0;
}
void ds_disconnect_db(ds_partition_t *partition)
{
if(*partition->db_handle)
{
partition->dbf.close(*partition->db_handle);
*partition->db_handle = 0;
}
}
/*initialize and verify DB stuff*/
int init_ds_db(ds_partition_t *partition)
{
int _ds_table_version;
if(partition->table_name.s == 0){
LM_ERR("invalid database name\n");
return -1;
}
/* Find a database module */
if (db_bind_mod(&partition->db_url, &partition->dbf) < 0) {
LM_ERR("Unable to bind to a database driver\n");
return -1;
}
if(ds_connect_db(partition)!=0){
LM_ERR("unable to connect to the database\n");
return -1;
}
_ds_table_version = db_table_version(&partition->dbf,*partition->db_handle,
&partition->table_name);
if (_ds_table_version < 0) {
LM_ERR("failed to query table version\n");
return -1;
} else if (!supported_ds_version(_ds_table_version)) {
LM_ERR("invalid version for table '%.*s' (found %d, required %d)\n"
"(use opensips-cli to migrate to latest schema)\n",
partition->table_name.len, partition->table_name.s,
_ds_table_version, DS_TABLE_VERSION );
return -1;
}
return 0;
}
static void ds_inherit_state( ds_data_t *old_data , ds_data_t *new_data)
{
ds_set_p new_set, old_set;
ds_dest_p new_ds, old_ds;
int changed;
/* search the new sets through the old sets */
for ( new_set=new_data->sets ; new_set ; new_set=new_set->next ) {
for ( old_set=old_data->sets ; old_set ; old_set=old_set->next ) {
if (new_set->id==old_set->id)
break;
}
if (old_set==NULL) {
LM_DBG("new set id %d not found in old sets\n",new_set->id);
continue;
}
LM_DBG("set id %d found in old sets\n",new_set->id);
changed = 0;
/* sets are matching, try to match the destinations, one by one */
for ( new_ds=new_set->dlist ; new_ds ; new_ds=new_ds->next ) {
for ( old_ds=old_set->dlist ; old_ds ; old_ds=old_ds->next ) {
if (new_ds->uri.len==old_ds->uri.len &&
strncasecmp(new_ds->uri.s, old_ds->uri.s, old_ds->uri.len)==0 ) {
LM_DBG("DST <%.*s> found in old set, copying state\n",
new_ds->uri.len,new_ds->uri.s);
if (new_ds->flags != old_ds->flags) {
new_ds->flags = old_ds->flags;
changed = 1;
}
break;
}
}
if (old_ds==NULL)
LM_DBG("DST <%.*s> not found in old set\n",
new_ds->uri.len,new_ds->uri.s);
}
if (changed)
re_calculate_active_dsts(new_set);
}
}
void ds_flusher_routine(unsigned int ticks, void* param)
{
db_key_t key_cmp[2];
db_val_t val_cmp[2];
db_key_t key_set;
db_val_t val_set;
ds_set_p list;
int j;
ds_partition_t *partition;
for (partition = partitions; partition; partition = partition->next){
if (*partition->db_handle==NULL)
continue;
val_cmp[0].type = DB_INT;
val_cmp[0].nul = 0;
val_cmp[1].type = DB_STR;
val_cmp[1].nul = 0;
val_set.type = DB_INT;
val_set.nul = 0;
/* access ds data under reader's lock */
lock_start_read( partition->lock );
/* update the gateways */
if (partition->dbf.use_table(*partition->db_handle,
&partition->table_name) < 0) {
LM_ERR("cannot select table \"%.*s\"\n",
partition->table_name.len, partition->table_name.s);
lock_stop_read( partition->lock );
continue;
}
key_cmp[0] = &ds_set_id_col;
key_cmp[1] = &ds_dest_uri_col;
key_set = &ds_dest_state_col;
if (*partition->data) {
/* Iterate over the groups and the entries of each group */
for(list = (*partition->data)->sets; list!= NULL; list=list->next){
for(j=0; j<list->nr; j++) {
/* If the Flag of the entry is STATE_DIRTY -> flush do db*/
if ( (list->dlist[j].flags&DS_STATE_DIRTY_DST)==0 )
/* nothing to do for this destination */
continue;
/* populate the update */
val_cmp[0].val.int_val = list->id;
val_cmp[1].val.str_val = list->dlist[j].uri;
val_set.val.int_val =
(list->dlist[j].flags&DS_INACTIVE_DST) ? 1 :
((list->dlist[j].flags&DS_PROBING_DST)?2:0);
/* update the state of this destination */
LM_DBG("updating the state of destination <%.*s> to %d\n",
list->dlist[j].uri.len, list->dlist[j].uri.s,
val_set.val.int_val);
if (partition->dbf.update(*partition->db_handle,key_cmp,0,
val_cmp,&key_set,&val_set,2,1)<0 ) {
LM_ERR("DB update failed\n");
} else {
list->dlist[j].flags &= ~DS_STATE_DIRTY_DST;
}
}
}
}
lock_stop_read( partition->lock );
}
return;
}
/*load groups of destinations from DB*/
static ds_data_t* ds_load_data(ds_partition_t *partition, int use_state_col)
{
ds_data_t *d_data;
int i, id, nr_rows, cnt, nr_cols = 8;
int state;
int weight;
int prio;
struct socket_info *sock;
str uri;
str attrs, weight_st;
str host;
str description;
int port, proto;
db_res_t * res = NULL;
db_val_t * values;
db_row_t * rows;
db_key_t query_cols[8] = {&ds_set_id_col, &ds_dest_uri_col,
&ds_dest_sock_col, &ds_dest_weight_col, &ds_dest_attrs_col,
&ds_dest_prio_col, &ds_dest_description_col, &ds_dest_state_col};
if (!use_state_col)
nr_cols--;
if(*partition->db_handle == NULL){
LM_ERR("invalid DB handler\n");
return NULL;
}
if (partition->dbf.use_table(*partition->db_handle, &partition->table_name)
< 0) {
LM_ERR("error in use_table\n");
return NULL;
}
d_data = (ds_data_t*)shm_malloc( sizeof(ds_data_t) );
if (d_data==NULL) {
LM_ERR("failed to allocate new data structure in shm\n");
return NULL;
}
memset( d_data, 0, sizeof(ds_data_t));
/*select the whole table and all the columns*/
if(partition->dbf.query(*partition->db_handle,0,0,0,query_cols,0,nr_cols,
0,&res) < 0) {
LM_ERR("error while querying database\n");
goto error;
}
nr_rows = RES_ROW_N(res);
rows = RES_ROWS(res);
if(nr_rows == 0) {
LM_WARN("no dispatching data in the db -- empty destination set\n");
goto load_done;
}
cnt = 0;
for(i=0; i<nr_rows; i++) {
values = ROW_VALUES(rows+i);
/* id */
if (VAL_NULL(values)) {
LM_ERR("ds ID column cannot be NULL -> skipping\n");
continue;
}
id = VAL_INT(values);
/* uri */
get_str_from_dbval( "URI", values+1,
1/*not_null*/, 1/*not_empty*/, uri, error2);
/* sock */
get_str_from_dbval( "SOCKET", values+2,
0/*not_null*/, 0/*not_empty*/, attrs, error2);
if ( attrs.len ) {
if (parse_phostport( attrs.s, attrs.len, &host.s, &host.len,
&port, &proto)!=0){
LM_ERR("socket description <%.*s> is not valid -> ignoring\n",
attrs.len,attrs.s);
sock = NULL;
} else {
sock = grep_internal_sock_info( &host, port, proto);
if (sock == NULL) {
LM_ERR("socket <%.*s> is not local to opensips (we must "
"listen on it) -> ignoring it\n", attrs.len, attrs.s);
}
}
} else {
sock = NULL;
}
weight = 1;
/* weight */
if (values[3].type == DB_INT) {
weight = VAL_INT(values+3);
memset(&weight_st, 0, sizeof weight_st);
} else {
/* dynamic weight, given as a communication socket string */
get_str_from_dbval("WEIGHT", values+3,
0/*not_null*/, 0/*not_empty*/, weight_st, error2);
if (!is_fs_url(&weight_st)) {
str2int(&weight_st, (unsigned int *)&weight);
memset(&weight_st, 0, sizeof weight_st);
}
}
/* attrs */
get_str_from_dbval( "ATTRIBUTES", values+4,
0/*not_null*/, 0/*not_empty*/, attrs, error2);
/* priority */
if (VAL_NULL(values+5))
prio = 0;
else
prio = VAL_INT(values+5);
/* state */
if (!use_state_col || VAL_NULL(values+7))
/* active state */
state = 0;
else
state = VAL_INT(values+7);
get_str_from_dbval( "DESCRIPTION", values+6,
0/*not_null*/, 0/*not_empty*/, description, error2);
if (add_dest2list(id, uri, sock, &weight_st, state, weight, prio, attrs, description, d_data)
!= 0) {
LM_WARN("failed to add destination <%.*s> in group %d\n",
uri.len,uri.s,id);
continue;
} else {
cnt++;
}
}
if (cnt==0) {
LM_WARN("No record loaded from db, running on empty sets\n");
} else {
if(reindex_dests( d_data )!=0) {
LM_ERR("error on reindex\n");
goto error2;
}
}
load_done:
partition->dbf.free_result(*partition->db_handle, res);
return d_data;
error:
ds_destroy_data_set( d_data );
return NULL;
error2:
ds_destroy_data_set( d_data );
partition->dbf.free_result(*partition->db_handle, res);
return NULL;
}
int ds_reload_db(ds_partition_t *partition)
{
ds_data_t *old_data;
ds_data_t *new_data;
new_data = ds_load_data(partition, ds_persistent_state);
if (new_data==NULL) {
LM_ERR("failed to load the new data, dropping the reload\n");
return -1;
}
lock_start_write( partition->lock );
/* no more activ readers -> do the swapping */
old_data = *partition->data;
*partition->data = new_data;
lock_stop_write( partition->lock );
/* destroy old data */
if (old_data) {
/* copy the state of the destinations from the old set
* (for the matching ids) */
ds_inherit_state( old_data, new_data);
ds_destroy_data_set( old_data );
}
/* update the Black Lists with the new gateways */
populate_ds_bls( new_data->sets, partition->name);
return 0;
}
/**
*
*/