-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-stat.c
2111 lines (1831 loc) · 54.2 KB
/
module-stat.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
#define MODULE_LOG_PREFIX "stat"
#include "globals.h"
#ifdef WITH_LB
#include "cscrypt/md5.h"
#include "module-cacheex.h"
#include "module-cccam.h"
#include "oscam-array.h"
#include "oscam-cache.h"
#include "oscam-conf-chk.h"
#include "oscam-chk.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-files.h"
#include "oscam-lock.h"
#include "oscam-string.h"
#include "oscam-time.h"
#define UNDEF_AVG_TIME 99999 // NOT set here 0 or small value! Could cause there reader get selected
#define MAX_ECM_SEND_CACHE 16
#define LB_NONE 0
#define LB_FASTEST_READER_FIRST 1
#define LB_OLDEST_READER_FIRST 2
#define LB_LOWEST_USAGELEVEL 3
#define DEFAULT_LOCK_TIMEOUT 1000000
extern CS_MUTEX_LOCK ecmcache_lock;
extern struct ecm_request_t *ecmcwcache;
static int32_t stat_load_save;
static struct timeb last_housekeeping;
void init_stat(void)
{
stat_load_save = -100;
//checking config
if(cfg.lb_nbest_readers < 2)
{ cfg.lb_nbest_readers = DEFAULT_NBEST; }
if(cfg.lb_nfb_readers < 2)
{ cfg.lb_nfb_readers = DEFAULT_NFB; }
if(cfg.lb_min_ecmcount < 2)
{ cfg.lb_min_ecmcount = DEFAULT_MIN_ECM_COUNT; }
if(cfg.lb_max_ecmcount < 3)
{ cfg.lb_max_ecmcount = DEFAULT_MAX_ECM_COUNT; }
if(cfg.lb_reopen_seconds < 10)
{ cfg.lb_reopen_seconds = DEFAULT_REOPEN_SECONDS; }
if(cfg.lb_retrylimit < 0)
{ cfg.lb_retrylimit = DEFAULT_RETRYLIMIT; }
if(cfg.lb_stat_cleanup <= 0)
{ cfg.lb_stat_cleanup = DEFAULT_LB_STAT_CLEANUP; }
}
#define LINESIZE 1024
static uint32_t get_prid(uint16_t caid, uint32_t prid)
{
int32_t i;
for(i = 0; i < cfg.lb_noproviderforcaid.ctnum; i++)
{
CAIDTAB_DATA *d = &cfg.lb_noproviderforcaid.ctdata[i];
uint16_t tcaid = d->caid;
if(!tcaid) { break; }
if((tcaid == caid) || (tcaid < 0x0100 && (caid >> 8) == tcaid))
{
prid = 0;
break;
}
}
return prid;
}
static void get_stat_query(ECM_REQUEST *er, STAT_QUERY *q)
{
memset(q, 0, sizeof(STAT_QUERY));
q->caid = er->caid;
q->prid = get_prid(er->caid, er->prid);
q->srvid = er->srvid;
q->chid = er->chid;
q->ecmlen = er->ecmlen;
}
void load_stat_from_file(void)
{
stat_load_save = 0;
char buf[256];
char *line;
char *fname;
FILE *file;
if(!cfg.lb_savepath)
{
get_tmp_dir_filename(buf, sizeof(buf), "stat");
fname = buf;
}
else
{ fname = cfg.lb_savepath; }
file = fopen(fname, "r");
if(!file)
{
cs_log_dbg(D_LB, "loadbalancer: could not open %s for reading (errno=%d %s)", fname, errno, strerror(errno));
return;
}
if(!cs_malloc(&line, LINESIZE))
{
fclose(file);
return;
}
cs_log_dbg(D_LB, "loadbalancer: load statistics from %s", fname);
struct timeb ts, te;
cs_ftime(&ts);
struct s_reader *rdr = NULL;
READER_STAT *s;
int32_t i = 1;
int32_t valid = 0;
int32_t count = 0;
int32_t type = 0;
char *ptr, *saveptr1 = NULL;
char *split[12];
while(fgets(line, LINESIZE, file))
{
if(!line[0] || line[0] == '#' || line[0] == ';')
{ continue; }
if(!cs_malloc(&s, sizeof(READER_STAT)))
{ continue; }
//get type by evaluating first line:
if(type == 0)
{
if(strstr(line, " rc ")) { type = 2; }
else { type = 1; }
}
if(type == 1) // New format - faster parsing:
{
for(i = 0, ptr = strtok_r(line, ",", &saveptr1); ptr && i < 12 ; ptr = strtok_r(NULL, ",", &saveptr1), i++)
{ split[i] = ptr; }
valid = (i == 11);
if(valid)
{
cs_strncpy(buf, split[0], sizeof(buf));
s->rc = atoi(split[1]);
s->caid = a2i(split[2], 4);
s->prid = a2i(split[3], 6);
s->srvid = a2i(split[4], 4);
s->chid = a2i(split[5], 4);
s->time_avg = atoi(split[6]);
s->ecm_count = atoi(split[7]);
s->last_received.time = atol(split[8]);
s->fail_factor = atoi(split[9]);
s->ecmlen = a2i(split[10], 2);
}
}
else // Old format - keep for compatibility:
{
i = sscanf(line, "%255s rc %04d caid %04hX prid %06X srvid %04hX time avg %d ms ecms %d last %ld fail %d len %02hX\n",
buf, &s->rc, &s->caid, &s->prid, &s->srvid,
&s->time_avg, &s->ecm_count, &s->last_received.time, &s->fail_factor, &s->ecmlen);
valid = i > 5;
}
if(valid && s->ecmlen > 0)
{
if(rdr == NULL || strcmp(buf, rdr->label) != 0)
{
LL_ITER itr = ll_iter_create(configured_readers);
while((rdr = ll_iter_next(&itr)))
{
if(strcmp(rdr->label, buf) == 0)
{
break;
}
}
}
if(rdr != NULL && strcmp(buf, rdr->label) == 0)
{
if(!rdr->lb_stat)
{
rdr->lb_stat = ll_create("lb_stat");
cs_lock_create(__func__, &rdr->lb_stat_lock, rdr->label, DEFAULT_LOCK_TIMEOUT);
}
ll_append(rdr->lb_stat, s);
count++;
}
else
{
cs_log("loadbalancer: statistics could not be loaded for %s", buf);
NULLFREE(s);
}
}
else
{
cs_log_dbg(D_LB, "loadbalancer: statistics ERROR: %s rc=%d i=%d", buf, s->rc, i);
NULLFREE(s);
}
}
fclose(file);
NULLFREE(line);
cs_ftime(&te);
#ifdef WITH_DEBUG
int64_t load_time = comp_timeb(&te, &ts);
cs_log_dbg(D_LB, "loadbalancer: statistics loaded %d records in %"PRId64" ms", count, load_time);
#endif
}
void lb_destroy_stats(struct s_reader *rdr)
{
if(!rdr->lb_stat)
return;
cs_lock_destroy(__func__, &rdr->lb_stat_lock);
ll_destroy_data(&rdr->lb_stat);
}
/**
* get statistic values for reader ridx and caid/prid/srvid/ecmlen
**/
static READER_STAT *get_stat_lock(struct s_reader *rdr, STAT_QUERY *q, int8_t lock)
{
if(!rdr->lb_stat)
{
rdr->lb_stat = ll_create("lb_stat");
cs_lock_create(__func__, &rdr->lb_stat_lock, rdr->label, DEFAULT_LOCK_TIMEOUT);
}
if(lock) { cs_readlock(__func__, &rdr->lb_stat_lock); }
LL_ITER it = ll_iter_create(rdr->lb_stat);
READER_STAT *s;
int32_t i = 0;
while((s = ll_iter_next(&it)))
{
i++;
if(s->caid == q->caid && s->prid == q->prid && s->srvid == q->srvid && s->chid == q->chid)
{
if(s->ecmlen == q->ecmlen)
{ break; }
if(!s->ecmlen)
{
s->ecmlen = q->ecmlen;
break;
}
if(!q->ecmlen) // Query without ecmlen from dvbapi
{ break; }
}
}
if(lock) { cs_readunlock(__func__, &rdr->lb_stat_lock); }
// Move stat to list start for faster access:
if (i > 10 && s && !rdr->lb_stat_busy) {
if (lock) cs_writelock(__func__, &rdr->lb_stat_lock);
ll_iter_move_first(&it);
if (lock) cs_writeunlock(__func__, &rdr->lb_stat_lock);
}
return s;
}
/**
* get statistic values for reader ridx and caid/prid/srvid/ecmlen
**/
static READER_STAT *get_stat(struct s_reader *rdr, STAT_QUERY *q)
{
return get_stat_lock(rdr, q, 1);
}
/**
* Calculates average time
*/
static void calc_stat(READER_STAT *s)
{
int32_t i, c = 0, t = 0;
for(i = 0; i < LB_MAX_STAT_TIME; i++)
{
if(s->time_stat[i] > 0)
{
t += (int32_t)s->time_stat[i];
c++;
}
}
if(!c)
{ s->time_avg = UNDEF_AVG_TIME; }
else
{ s->time_avg = t / c; }
}
/**
* Saves statistik to /tmp/.oscam/stat.n where n is reader-index
*/
static void save_stat_to_file_thread(void)
{
stat_load_save = 0;
char buf[256];
set_thread_name(__func__);
char *fname;
if(!cfg.lb_savepath)
{
get_tmp_dir_filename(buf, sizeof(buf), "stat");
fname = buf;
}
else
{ fname = cfg.lb_savepath; }
FILE *file = fopen(fname, "w");
if(!file)
{
cs_log("can't write to file %s", fname);
return;
}
struct timeb ts, te;
cs_ftime(&ts);
int32_t cleanup_timeout = (cfg.lb_stat_cleanup * 60 * 60 * 1000);
int32_t count = 0;
struct s_reader *rdr;
LL_ITER itr = ll_iter_create(configured_readers);
while((rdr = ll_iter_next(&itr)))
{
if(rdr->lb_stat)
{
rdr->lb_stat_busy = 1;
cs_writelock(__func__, &rdr->lb_stat_lock);
LL_ITER it = ll_iter_create(rdr->lb_stat);
READER_STAT *s;
while((s = ll_iter_next(&it)))
{
int64_t gone = comp_timeb(&ts, &s->last_received);
if(gone > cleanup_timeout || !s->ecmlen) // cleanup old stats
{
ll_iter_remove_data(&it);
continue;
}
//Old version, too slow to parse:
//fprintf(file, "%s rc %d caid %04hX prid %06X srvid %04hX time avg %d ms ecms %d last %ld fail %d len %02hX\n",
// rdr->label, s->rc, s->caid, s->prid,
// s->srvid, s->time_avg, s->ecm_count, s->last_received, s->fail_factor, s->ecmlen);
//New version:
fprintf(file, "%s,%d,%04hX,%06X,%04hX,%04hX,%d,%d,%ld,%d,%02hX\n",
rdr->label, s->rc, s->caid, s->prid,
s->srvid, (uint16_t)s->chid, s->time_avg, s->ecm_count, s->last_received.time, s->fail_factor, s->ecmlen);
count++;
//if(count % 500 == 0) { // Saving stats is using too much cpu and causes high file load. so we need a break
// cs_readunlock(__func__, &rdr->lb_stat_lock);
// cs_sleepms(100);
// cs_readlock(__func__, &rdr->lb_stat_lock);
//}
}
cs_writeunlock(__func__, &rdr->lb_stat_lock);
rdr->lb_stat_busy = 0;
}
}
fclose(file);
cs_ftime(&te);
int64_t load_time = comp_timeb(&te, &ts);
cs_log("loadbalancer: statistic saved %d records to %s in %"PRId64" ms", count, fname, load_time);
}
void save_stat_to_file(int32_t thread)
{
stat_load_save = 0;
if(thread)
{ start_thread("save lb stats", (void *)&save_stat_to_file_thread, NULL, NULL, 1, 1); }
else
{ save_stat_to_file_thread(); }
}
/**
* fail_factor is multiplied to the reopen_time. This function increases the fail_factor
**/
static void inc_fail(READER_STAT *s)
{
if(s->fail_factor <= 0)
{ s->fail_factor = 1; }
else
{ s->fail_factor++; } // inc by one at the time
}
static READER_STAT *get_add_stat(struct s_reader *rdr, STAT_QUERY *q)
{
if (rdr->lb_stat_busy)
return NULL;
if(!rdr->lb_stat)
{
rdr->lb_stat = ll_create("lb_stat");
cs_lock_create(__func__, &rdr->lb_stat_lock, rdr->label, DEFAULT_LOCK_TIMEOUT);
}
cs_writelock(__func__, &rdr->lb_stat_lock);
READER_STAT *s = get_stat_lock(rdr, q, 0);
if(!s)
{
if(cs_malloc(&s, sizeof(READER_STAT)))
{
s->caid = q->caid;
s->prid = q->prid;
s->srvid = q->srvid;
s->chid = q->chid;
s->ecmlen = q->ecmlen;
s->time_avg = UNDEF_AVG_TIME; // dummy placeholder
s->rc = E_FOUND; // set to found--> do not change!
cs_ftime(&s->last_received);
s->fail_factor = 0;
s->ecm_count = 0;
ll_prepend(rdr->lb_stat, s);
}
}
cs_writeunlock(__func__, &rdr->lb_stat_lock);
return s;
}
static void housekeeping_stat(int32_t force);
void readerinfofix_get_stat_query(ECM_REQUEST *er, STAT_QUERY *q)
{
get_stat_query(er, q);
}
void readerinfofix_inc_fail(READER_STAT *s)
{
inc_fail(s);
}
READER_STAT *readerinfofix_get_add_stat(struct s_reader *rdr, STAT_QUERY *q)
{
return get_add_stat(rdr, q);
}
static int32_t get_reopen_seconds(READER_STAT *s)
{
int32_t max = (INT_MAX / cfg.lb_reopen_seconds);
if(max > 9999) { max = 9999; }
if(s->fail_factor > max)
{ s->fail_factor = max; }
if(!s->fail_factor)
{ return cfg.lb_reopen_seconds; }
return s->fail_factor * cfg.lb_reopen_seconds;
}
/**
* Adds caid/prid/srvid/ecmlen to stat-list for reader ridx with time/rc
*/
static void add_stat(struct s_reader *rdr, ECM_REQUEST *er, int32_t ecm_time, int32_t rc, uint8_t rcEx)
{
//inc ecm_count if found, drop to 0 if not found:
// rc codes:
// 0 = found +
// 1 = cache1 #
// 2 = cache2 #
// 3 = cacheex #
// 4 = not found -
// 5 = timeout -
// 6 = sleeping #
// 7 = fake -
// 8 = invalid -
// 9 = corrupt #
// 10= no card #
// 11= expdate #
// 12= disabled #
// 13= stopped #
// 100= unhandled #
// + = adds statistic values
// # = ignored because of duplicate values, temporary failures or softblocks
// - = causes loadbalancer to block this reader for this caid/prov/sid
if(!rdr || !er || !cfg.lb_mode || !er->ecmlen || !er->client || rdr->lb_stat_busy)
{ return; }
struct s_client *cl = rdr->client;
if(!check_client(cl))
{ return; }
// IGNORE stats for fallback reader with lb_force_fallback parameter
if(chk_is_fixed_fallback(rdr, er) && rdr->lb_force_fallback)
{ return; }
// IGNORE fails for ratelimit check
if(rc == E_NOTFOUND && rcEx == E2_RATELIMIT)
{
#ifdef WITH_DEBUG
if((D_LB & cs_dblevel))
{
char buf[ECM_FMT_LEN];
format_ecm(er, buf, ECM_FMT_LEN);
cs_log_dbg(D_LB, "loadbalancer: NOT adding stat (blocking) for reader %s because fails ratelimit checks!", rdr->label);
}
#endif
return;
}
// IGNORE fails when reader has positive services defined in new lb_whitelist_services parameter! See ticket #3310,#3311
if(rc >= E_NOTFOUND && has_lb_srvid(cl, er))
{
#ifdef WITH_DEBUG
if((D_LB & cs_dblevel))
{
char buf[ECM_FMT_LEN];
format_ecm(er, buf, ECM_FMT_LEN);
cs_log_dbg(D_LB, "loadbalancer: NOT adding stat (blocking) for reader %s because has positive srvid: rc %d %s time %d ms",
rdr->label, rc, buf, ecm_time);
}
#endif
return;
}
// IGNORE fails for sleep CMD08
if(rc == E_NOTFOUND && rdr->client->stopped==2)
{
#ifdef WITH_DEBUG
if((D_LB & cs_dblevel))
{
char buf[ECM_FMT_LEN];
format_ecm(er, buf, ECM_FMT_LEN);
cs_log_dbg(D_LB, "loadbalancer: NOT adding stat (no block) for reader %s because CMD08 sleep command!", rdr->label);
}
#endif
return;
}
// IGNORE timeouts on local readers (they could be busy handling an emm or entitlement refresh)
if(rc == E_TIMEOUT && !is_network_reader(rdr))
{
#ifdef WITH_DEBUG
if((D_LB & cs_dblevel))
{
cs_log_dbg(D_LB, "loadbalancer: NOT adding stat (no block) for reader %s because timeout on local reader", rdr->label);
}
#endif
return;
}
// IGNORE unhandled ecmresponses
if(rc == E_UNHANDLED)
{
#ifdef WITH_DEBUG
if((D_LB & cs_dblevel))
{
cs_log_dbg(D_LB, "loadbalancer: NOT adding stat (no block) for reader %s because unhandled reponse", rdr->label);
}
#endif
return;
}
// ignore too old ecms
if((uint32_t)ecm_time >= 3 * cfg.ctimeout)
{ return; }
if((uint32_t)ecm_time >= cfg.ctimeout)
{ rc = E_TIMEOUT;}
STAT_QUERY q;
get_stat_query(er, &q);
READER_STAT *s;
s = get_add_stat(rdr, &q);
if (!s) return;
struct timeb now;
cs_ftime(&now);
cs_ftime(&s->last_received);
if(rc == E_FOUND) // found
{
s->rc = E_FOUND;
s->ecm_count++;
s->fail_factor = 0;
// FASTEST READER:
s->time_idx++;
if(s->time_idx >= LB_MAX_STAT_TIME)
{ s->time_idx = 0; }
s->time_stat[s->time_idx] = ecm_time;
calc_stat(s);
// OLDEST READER now set by get best reader!
// USAGELEVEL:
/* Assign a value to rdr->lb_usagelevel_ecmcount,
because no determined value was assigned before. */
if(rdr->lb_usagelevel_ecmcount < 0)
{ rdr->lb_usagelevel_ecmcount = 0; }
rdr->lb_usagelevel_ecmcount++; /* ecm is found so counter should increase */
if((rdr->lb_usagelevel_ecmcount % cfg.lb_min_ecmcount) == 0) //update every MIN_ECM_COUNT usagelevel:
{
int64_t t = comp_timeb(&now, &rdr->lb_usagelevel_time) / 1000;
rdr->lb_usagelevel = cfg.lb_min_ecmcount * 1000 / (t < 1 ? 1 : t);
/* Reset of usagelevel time and counter */
rdr->lb_usagelevel_time = now;
rdr->lb_usagelevel_ecmcount = 0;
}
}
else if(rc == E_NOTFOUND || rc == E_TIMEOUT || rc == E_FAKE) // not found / timeout /fake
{
inc_fail(s);
s->rc = rc;
}
else if(rc == E_INVALID) // invalid
{
s->rc = rc;
}
else
{
#ifdef WITH_DEBUG
if(rc >= E_FOUND && (D_LB & cs_dblevel))
{
char buf[ECM_FMT_LEN];
format_ecm(er, buf, ECM_FMT_LEN);
cs_log_dbg(D_LB, "loadbalancer: not handled stat for reader %s: rc %d %s time %d ms",
rdr->label, rc, buf, ecm_time);
}
#endif
return;
}
housekeeping_stat(0);
#ifdef WITH_DEBUG
if(D_LB & cs_dblevel)
{
char buf[ECM_FMT_LEN];
format_ecm(er, buf, ECM_FMT_LEN);
cs_log_dbg(D_LB, "loadbalancer: adding stat for reader %s: rc %d %s time %d ms fail %d",
rdr->label, rc, buf, ecm_time, s->fail_factor);
}
#endif
if(cfg.lb_save)
{
stat_load_save++;
if(stat_load_save > cfg.lb_save)
{ save_stat_to_file(1); }
}
}
int32_t clean_stat_by_rc(struct s_reader *rdr, int8_t rc, int8_t inverse)
{
int32_t count = 0;
if(rdr && rdr->lb_stat)
{
if (rdr->lb_stat_busy) return 0;
rdr->lb_stat_busy = 1;
cs_writelock(__func__, &rdr->lb_stat_lock);
READER_STAT *s;
LL_ITER itr = ll_iter_create(rdr->lb_stat);
while((s = ll_iter_next(&itr)))
{
if((!inverse && s->rc == rc) || (inverse && s->rc != rc))
{
ll_iter_remove_data(&itr);
count++;
}
}
cs_writeunlock(__func__, &rdr->lb_stat_lock);
rdr->lb_stat_busy = 0;
}
return count;
}
int32_t clean_all_stats_by_rc(int8_t rc, int8_t inverse)
{
int32_t count = 0;
LL_ITER itr = ll_iter_create(configured_readers);
struct s_reader *rdr;
while((rdr = ll_iter_next(&itr)))
{
count += clean_stat_by_rc(rdr, rc, inverse);
}
save_stat_to_file(0);
return count;
}
int32_t clean_stat_by_id(struct s_reader *rdr, uint16_t caid, uint32_t prid, uint16_t srvid, uint16_t chid, uint16_t ecmlen)
{
int32_t count = 0;
if(rdr && rdr->lb_stat)
{
if (rdr->lb_stat_busy) return 0;
rdr->lb_stat_busy = 1;
cs_writelock(__func__, &rdr->lb_stat_lock);
READER_STAT *s;
LL_ITER itr = ll_iter_create(rdr->lb_stat);
while((s = ll_iter_next(&itr)))
{
if(s->caid == caid &&
s->prid == prid &&
s->srvid == srvid &&
s->chid == chid &&
s->ecmlen == ecmlen)
{
ll_iter_remove_data(&itr);
count++;
break; // because the entry should unique we can left here
}
}
cs_writeunlock(__func__, &rdr->lb_stat_lock);
rdr->lb_stat_busy = 0;
}
return count;
}
/*
static int32_t has_ident(FTAB *ftab, ECM_REQUEST *er) {
if (!ftab || !ftab->filts)
return 0;
int32_t j, k;
for (j = 0; j < ftab->nfilts; j++) {
if (ftab->filts[j].caid) {
if (ftab->filts[j].caid==er->caid) { //caid matches!
int32_t nprids = ftab->filts[j].nprids;
if (!nprids) // No Provider ->Ok
return 1;
for (k = 0; k < nprids; k++) {
uint32_t prid = ftab->filts[j].prids[k];
if (prid == er->prid) { //Provider matches
return 1;
}
}
}
}
}
return 0; //No match!
}*/
static int32_t get_retrylimit(ECM_REQUEST *er)
{
return caidvaluetab_get_value(&cfg.lb_retrylimittab, er->caid, cfg.lb_retrylimit);
}
static int32_t get_nfb_readers(ECM_REQUEST *er)
{
int32_t nfb_readers = er->client->account->lb_nfb_readers == -1 ? cfg.lb_nfb_readers : er->client->account->lb_nfb_readers;
if(nfb_readers <= 0) { nfb_readers = 1; }
return nfb_readers;
}
static int32_t get_nbest_readers(ECM_REQUEST *er)
{
int32_t nbest_readers = er->client->account->lb_nbest_readers == -1 ? cfg.lb_nbest_readers : er->client->account->lb_nbest_readers;
CAIDVALUETAB *nbest_readers_tab = er->client->account->lb_nbest_readers_tab.cvnum == 0 ? &cfg.lb_nbest_readers_tab : &er->client->account->lb_nbest_readers_tab;
if(nbest_readers <= 0) { nbest_readers = 1; }
return caidvaluetab_get_value(nbest_readers_tab, er->caid, nbest_readers);
}
static void convert_to_beta_int(ECM_REQUEST *er, uint16_t caid_to)
{
uint8_t md5tmp[MD5_DIGEST_LENGTH];
convert_to_beta(er->client, er, caid_to);
// update ecmd5 for store ECM in cache
memcpy(er->ecmd5, MD5(er->ecm + 13, er->ecmlen - 13, md5tmp), CS_ECMSTORESIZE);
cacheex_update_hash(er);
er->btun = 2; // marked as auto-betatunnel converted. Also for fixing recursive lock in get_cw
}
static void convert_to_nagra_int(ECM_REQUEST *er, uint16_t caid_to)
{
uint8_t md5tmp[MD5_DIGEST_LENGTH];
convert_to_nagra(er->client, er, caid_to);
// update ecmd5 for store ECM in cache
memcpy(er->ecmd5, MD5(er->ecm + 3, er->ecmlen - 3, md5tmp), CS_ECMSTORESIZE);
cacheex_update_hash(er);
er->btun = 2; // marked as auto-betatunnel converted. Also for fixing recursive lock in get_cw
}
static int32_t lb_valid_btun(ECM_REQUEST *er, uint16_t caidto)
{
STAT_QUERY q;
READER_STAT *s;
struct s_reader *rdr;
get_stat_query(er, &q);
q.caid = caidto;
cs_readlock(__func__, &readerlist_lock);
for(rdr = first_active_reader; rdr ; rdr = rdr->next)
{
if(rdr->lb_stat && rdr->client)
{
s = get_stat(rdr, &q);
if(s && s->rc == E_FOUND)
{
cs_readunlock(__func__, &readerlist_lock);
return 1;
}
}
}
cs_readunlock(__func__, &readerlist_lock);
return 0;
}
static uint16_t __lb_get_betatunnel_caid_to(uint16_t caid)
{
int32_t lbbm = cfg.lb_auto_betatunnel_mode;
if(lbbm <= 3)
{
if(caid == 0x1801) { return 0x1722; }
if(caid == 0x1833) { return 0x1702; }
if(caid == 0x1834) { return 0x1722; }
if(caid == 0x1835) { return 0x1722; }
}
if(lbbm >= 1)
{
if(caid == 0x1702) { return 0x1833; }
}
if(lbbm == 1 || lbbm == 4)
{
if(caid == 0x1722) { return 0x1801; }
}
else if(lbbm == 2 || lbbm == 5)
{
if(caid == 0x1722) { return 0x1834; }
}
else if(lbbm == 3 || lbbm == 6)
{
if(caid == 0x1722) { return 0x1835; }
}
return 0;
}
uint16_t lb_get_betatunnel_caid_to(ECM_REQUEST *er)
{
if(!cfg.lb_auto_betatunnel)
return 0;
uint16_t caidto = __lb_get_betatunnel_caid_to(er->caid);
if(lb_valid_btun(er, caidto))
return caidto;
return 0;
}
void check_lb_auto_betatunnel_mode(ECM_REQUEST *er)
{
int32_t lbbm = cfg.lb_auto_betatunnel_mode;
if(lbbm == 1 || lbbm == 4)
{
er->caid = 0x1801;
}
else if(lbbm == 2 || lbbm == 5)
{
er->caid = 0x1834;
}
else if(lbbm == 3 || lbbm == 6)
{
er->caid = 0x1835;
}
// no other way to autodetect 1801, 1834 or 1835
}
uint16_t get_rdr_caid(struct s_reader *rdr)
{
if(is_network_reader(rdr) || rdr->typ == R_EMU)
{
return 0; // reader caid is not real caid
}
else
{
return rdr->caid;
}
}
static void reset_ecmcount_reader(READER_STAT *s, struct s_reader *rdr)
{
cs_readlock(__func__, &rdr->lb_stat_lock);
if(rdr->lb_stat && rdr->client)
{
if(s)
{
s->ecm_count = 0;
}
}
cs_readunlock(__func__, &rdr->lb_stat_lock);
}
static void reset_avgtime_reader(READER_STAT *s, struct s_reader *rdr)
{
cs_readlock(__func__, &rdr->lb_stat_lock);
if(rdr->lb_stat && rdr->client)
{
if(!s) { return; }
int32_t i;
for(i = 0; i < LB_MAX_STAT_TIME; i++)
{
if(s->time_stat[i] > 0) { s->time_stat[i] = 0; }
}
s->time_avg = UNDEF_AVG_TIME;
}
cs_readunlock(__func__, &rdr->lb_stat_lock);
}
/* force_reopen=1 -> force opening of block readers
* force_reopen=0 -> no force opening of block readers, use reopen_seconds
*/
static void try_open_blocked_readers(ECM_REQUEST *er, STAT_QUERY *q, int32_t *max_reopen, int32_t *force_reopen)
{
struct s_ecm_answer *ea;
READER_STAT *s;
struct s_reader *rdr;
for(ea = er->matching_rdr; ea; ea = ea->next)
{
if((ea->status & READER_FALLBACK) || (ea->status & READER_ACTIVE)) { continue; }
rdr = ea->reader;
s = get_stat(rdr, q);
if(!s) { continue; }
if(!cfg.lb_reopen_invalid && s->rc == E_INVALID){
cs_log_dbg(D_LB, "loadbalancer: reader %s blocked because INVALID sent! It will be blocked until stats cleaned!", rdr->label);
continue;
}
// if force_reopen we must active the "valid" reader
if(s->rc != E_FOUND && (*force_reopen) && cfg.lb_force_reopen_always)
{
cs_log_dbg(D_LB, "loadbalancer: force opening reader %s and reset fail_factor! --> ACTIVE", rdr->label);
ea->status |= READER_ACTIVE;
s->fail_factor = 0;
continue;
}
//active readers reach get_reopen_seconds(s)
struct timeb now;
cs_ftime(&now);
int64_t gone = comp_timeb(&now, &s->last_received);
int32_t reopenseconds = get_reopen_seconds(s);
if(s->rc != E_FOUND && gone > reopenseconds*1000 )
{
if(*max_reopen)
{
cs_log_dbg(D_LB, "loadbalancer: reader %s reaches %d seconds for reopening (fail_factor %d) --> ACTIVE", rdr->label, reopenseconds, s->fail_factor);
ea->status |= READER_ACTIVE;
(*max_reopen)--;
}
else
{
cs_log_dbg(D_LB, "loadbalancer: reader %s reaches %d seconds for reopening (fail_factor %d), but max_reopen reached!", rdr->label, reopenseconds, s->fail_factor);
}
continue;
}
if(s->rc != E_FOUND) // for debug output
{
cs_log_dbg(D_LB, "loadbalancer: reader %s blocked for %d seconds (fail_factor %d), retrying in %d seconds", rdr->label, get_reopen_seconds(s), s->fail_factor, (uint) (reopenseconds - (gone/1000)));
continue;
}
if(s->rc == E_FOUND) // for debug output
{ cs_log_dbg(D_LB, "loadbalancer: reader %s \"e_found\" but not selected for lbvalue check", rdr->label); }
}
}