summaryrefslogtreecommitdiff
path: root/src/maat_ip.c
blob: 440c77b6de2adc67f15ddfaf2f55248d1cc75f07 (plain)
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
/*
**********************************************************************************************
*	File: maat_ip.c
*   Description: 
*	Authors: Liu WenTan <[email protected]>
*	Date:    2022-10-31
*   Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
***********************************************************************************************
*/

#include <stdint.h>
#include <assert.h>

#include "log/log.h"
#include "maat_utils.h"
#include "maat_ex_data.h"
#include "ip_matcher.h"
#include "interval_matcher.h"
#include "maat_ip.h"
#include "maat_rule.h"
#include "maat_compile.h"
#include "alignment.h"
#include "maat_garbage_collection.h"

#define MODULE_IP	 module_name_str("maat.ip")

struct ip_schema {
    int item_id_column;
    int group_id_column;
    int addr_type_column;
    int addr_format_column;
    int ip1_column;
    int ip2_column;
    int port_format_column;
    int port1_column;
    int port2_column;
    int protocol_column;
    int table_id; //ugly
    struct table_manager *ref_tbl_mgr;
};

struct ipv4_item_rule {
    uint32_t min_ip;   /* 源地址下界;0表示忽略本字段 */
    uint32_t max_ip;   /* 源地址上界;0表示固定IP=min_addr */
};

struct ipv6_item_rule {
    uint32_t min_ip[4]; /* 源地址下界;全0表示忽略本字段 */
    uint32_t max_ip[4]; /* 源地址上界;全0表示固定IP=min_addr */
};

struct ip_item {
    long long item_id;
    long long group_id;
    int addr_type;
    union {
        struct ipv4_item_rule ipv4;
        struct ipv6_item_rule ipv6;
    };
    enum ip_format ip_format;
    enum port_format port_format;
    uint16_t min_port;
    uint16_t max_port;
    int proto;
    void *user_data;
    void (*user_data_free)(void *data);
};

struct ip_runtime {
	struct ip_matcher *ip_matcher;
    struct interval_matcher *intval_matcher;
    struct rcu_hash_table *item_hash;

    long long version;
    long long rule_num;
    long long ipv6_rule_num;
    size_t n_worker_thread;
    struct log_handle *logger;
    struct maat_garbage_bin *ref_garbage_bin;
    
    long long update_err_cnt;
    long long *scan_cnt;
    long long *scan_cpu_time;
    long long *hit_cnt;
};

void *ip_schema_new(cJSON *json, struct table_manager *tbl_mgr, 
                    const char *table_name, struct log_handle *logger)
{
    struct ip_schema *ip_schema = ALLOC(struct ip_schema, 1);

    cJSON *custom_item = NULL;
    cJSON *item = cJSON_GetObjectItem(json, "table_id");
    if (item != NULL && item->type == cJSON_Number) {
        ip_schema->table_id = item->valueint;
    }

    item = cJSON_GetObjectItem(json, "custom");
    if (NULL == item || item->type != cJSON_Object) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table %s has no custom column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "item_id");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->item_id_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no item_id column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "group_id");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->group_id_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no group_id column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "addr_type");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->addr_type_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no add_type column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "addr_format");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->addr_format_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no addr_format column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "ip1");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->ip1_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no ip1 column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "ip2");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->ip2_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no ip2 column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "port_format");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->port_format_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no port_format column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "port1");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->port1_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no port1 column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "port2");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->port2_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no port2 column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    custom_item = cJSON_GetObjectItem(item, "protocol");
    if (custom_item != NULL && custom_item->type == cJSON_Number) {
        ip_schema->protocol_column = custom_item->valueint;
    } else {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] table %s has no protocol column", 
                  __FUNCTION__, __LINE__, table_name);
        goto error;
    }

    ip_schema->ref_tbl_mgr = tbl_mgr;
    return ip_schema;
error:
    FREE(ip_schema);
    return NULL;
}

void ip_schema_free(void *ip_schema)
{
    FREE(ip_schema);
}

struct ip_item *ip_item_new(const char *line, struct ip_schema *ip_schema,
                            struct log_handle *logger)
{
    size_t column_offset = 0;
    size_t column_len = 0;
    char addr_format[16] = {0};
    char port_format[16] = {0};
    char ip1_str[40] = {0};
    char ip2_str[40] = {0};
    struct ip_item *ip_item = ALLOC(struct ip_item, 1);

    int ret = get_column_pos(line, ip_schema->item_id_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip plus table(table_id:%d) line:%s has no item_id",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->item_id = atoll(line + column_offset);

    ret = get_column_pos(line, ip_schema->group_id_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP,
                  "[%s:%d] ip plus table(table_id:%d) line:%s has no group_id",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->group_id = atoll(line + column_offset);

    ret = get_column_pos(line, ip_schema->addr_type_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip plus table(table_id:%d) line:%s has no addr_type",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->addr_type = atoi(line + column_offset);

    if (ip_item->addr_type != IPv4 && ip_item->addr_type != IPv6) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has invalid addr type:%d",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line, ip_item->addr_type);
        goto error;
    }

    ret = get_column_pos(line, ip_schema->addr_format_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP,
                  "[%s:%d] ip table(table_id:%d) line:%s has no addr_format",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    memcpy(addr_format, (line + column_offset), column_len);
    if (IP_FORMAT_UNKNOWN == ip_format_str2int(addr_format)) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has invalid saddr_format, should be single/range/CIDR",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }

    ret = get_column_pos(line, ip_schema->ip1_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has no ip1",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    memcpy(ip1_str, (line + column_offset), column_len);

    ret = get_column_pos(line, ip_schema->ip2_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has no ip2",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    memcpy(ip2_str, (line + column_offset), column_len);

    if (IPv4 == ip_item->addr_type) {
        ret = ip_format2range(ip_item->addr_type, ip_format_str2int(addr_format), ip1_str, ip2_str, 
                              &ip_item->ipv4.min_ip, &ip_item->ipv4.max_ip);
        if (ret < 0) {
            log_error(logger, MODULE_IP, 
                      "[%s:%d] ip table(table_id:%d) line:%s ip_format2range(ip4) failed",
                      __FUNCTION__, __LINE__, ip_schema->table_id, line);
            goto error;
        }
    } else {
        //ipv6
        ret = ip_format2range(ip_item->addr_type, ip_format_str2int(addr_format), ip1_str, ip2_str, 
                              ip_item->ipv6.min_ip, ip_item->ipv6.max_ip);
        if (ret < 0) {
            log_error(logger, MODULE_IP, 
                      "[%s:%d] ip table(table_id:%d) line:%s ip_format2range(ip6) failed",
                      __FUNCTION__, __LINE__, ip_schema->table_id, line);
            goto error;
        }
    }

    ret = get_column_pos(line, ip_schema->port_format_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP,
                  "[%s:%d] ip table(table_id:%d) line:%s has no port_format",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    
    memcpy(port_format, (line + column_offset), column_len);
    if (PORT_FORMAT_UNKNOWN == port_format_str2int(port_format)) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has invalid port_format, should be single/range",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->port_format = port_format_str2int(port_format);

    ret = get_column_pos(line, ip_schema->port1_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has no port1",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->min_port = atoi(line + column_offset);

    ret = get_column_pos(line, ip_schema->port2_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has no port2",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->max_port = atoi(line + column_offset);

    ret = get_column_pos(line, ip_schema->protocol_column, &column_offset, &column_len);
    if (ret < 0) {
        log_error(logger, MODULE_IP, 
                  "[%s:%d] ip table(table_id:%d) line:%s has no protocol",
                  __FUNCTION__, __LINE__, ip_schema->table_id, line);
        goto error;
    }
    ip_item->proto = atoi(line + column_offset);

    return ip_item;
error:
    FREE(ip_item);
    return NULL;
}

void ip_item_free(struct ip_item *item)
{
    if (NULL == item) {
        return;
    }

    if (item->user_data != NULL && item->user_data_free != NULL) {
        item->user_data_free(item->user_data);
        item->user_data = NULL;
    }

    FREE(item);
}

void ip_item_free_cb(void *user_ctx, void *data)
{
    struct ip_item *item = (struct ip_item *)data;
    ip_item_free(item);
}

void *ip_runtime_new(void *ip_schema, size_t max_thread_num,
                     struct maat_garbage_bin *garbage_bin, 
                     struct log_handle *logger)
{
    if (NULL == ip_schema) {
        return NULL;
    }

    struct ip_runtime *ip_rt = ALLOC(struct ip_runtime, 1);

    ip_rt->item_hash = rcu_hash_new(ip_item_free_cb, NULL);
    ip_rt->n_worker_thread = max_thread_num;
    ip_rt->ref_garbage_bin = garbage_bin;
    ip_rt->logger = logger;

    ip_rt->hit_cnt = alignment_int64_array_alloc(max_thread_num);
    ip_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
    ip_rt->scan_cpu_time = alignment_int64_array_alloc(max_thread_num);

    return ip_rt;
}

void ip_runtime_free(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    if (ip_rt->ip_matcher != NULL) {
        ip_matcher_free(ip_rt->ip_matcher);
        ip_rt->ip_matcher = NULL;
    }

    if (ip_rt->intval_matcher != NULL) {
        interval_matcher_free(ip_rt->intval_matcher);
        ip_rt->intval_matcher = NULL;
    }

    if (ip_rt->item_hash != NULL) {
        rcu_hash_free(ip_rt->item_hash);
        ip_rt->item_hash = NULL;
    }

    if (ip_rt->hit_cnt != NULL) {
        alignment_int64_array_free(ip_rt->hit_cnt);
        ip_rt->hit_cnt = NULL;
    }

    if (ip_rt->scan_cnt != NULL) {
        alignment_int64_array_free(ip_rt->scan_cnt);
        ip_rt->scan_cnt = NULL;
    }

    if (ip_rt->scan_cpu_time != NULL) {
        alignment_int64_array_free(ip_rt->scan_cpu_time);
        ip_rt->scan_cpu_time = NULL;
    }

    FREE(ip_rt);
}

void ip_item_to_ip_rule(struct ip_item *item, struct ip_rule *rule)
{
    if (IPv4 == item->addr_type) {
        rule->type = IPv4;
        rule->ipv4_rule.start_ip = item->ipv4.min_ip;
        rule->ipv4_rule.end_ip = item->ipv4.max_ip;
    } else {
        rule->type = IPv6;
        memcpy(rule->ipv6_rule.start_ip, item->ipv6.min_ip, 
               sizeof(item->ipv6.min_ip));
        memcpy(rule->ipv6_rule.end_ip, item->ipv6.max_ip, 
               sizeof(item->ipv6.max_ip));
    }
    rule->rule_id = item->item_id;
}

void ip_item_to_port_rule(struct ip_item *item, struct interval_rule *rule)
{
    rule->start = item->min_port;
    rule->end = item->max_port;
    rule->result.rule_id = item->item_id;
}

int ip_runtime_update_row(struct ip_runtime *ip_rt, char *key, size_t key_len, 
                          struct ip_item *item, int is_valid)
{
    int ret = -1;
    
    if (0 == is_valid) {
        // delete
        rcu_hash_del(ip_rt->item_hash, key, key_len);
    } else {
        // add
        ret = rcu_hash_add(ip_rt->item_hash, key, key_len, (void *)item);
        if (ret < 0) {
            log_error(ip_rt->logger, MODULE_IP,
                      "[%s:%d] ip item(item_id:%lld) add to ip runtime htable failed", 
                      __FUNCTION__, __LINE__, item->item_id);
            return -1;
        }
    }

    return 0;
}

int ip_runtime_update(void *ip_runtime, void *ip_schema, 
                      const char *table_name, const char *line, 
                      int valid_column)
{
    if (NULL == ip_runtime || NULL == ip_schema || NULL == line) {
        return -1;
    }

    struct ip_schema *schema = (struct ip_schema *)ip_schema;
    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;

    long long item_id = get_column_value(line, schema->item_id_column);
    if (item_id < 0) {
        ip_rt->update_err_cnt++;
        return -1;
    }

    int is_valid = get_column_value(line, valid_column);
    if (is_valid < 0) {
        ip_rt->update_err_cnt++;
        return -1;
    }
    
    struct ip_item *ip_item = NULL;
    if (1 == is_valid) {
        //add
        ip_item = ip_item_new(line, schema, ip_rt->logger);
        if (NULL == ip_item) {
            ip_rt->update_err_cnt++;
            return -1;
        }

        struct maat_item_inner *inner_item = maat_item_inner_new(ip_item->item_id,
                                                                 ip_item->group_id,
                                                                 0);
        ip_item->user_data = inner_item;
        ip_item->user_data_free = maat_item_inner_free;
    }
    
    int ret = ip_runtime_update_row(ip_rt, (char *)&item_id, sizeof(long long),
                                    ip_item, is_valid);
    if (ret < 0) {
        if (ip_item != NULL) {
            ip_item_free(ip_item);
            ip_item = NULL;
        }
        ip_rt->update_err_cnt++;
        return -1;
    }

    return 0;
}

void garbage_ip_matcher_free(void *ip_matcher, void *arg)
{
    struct ip_matcher *matcher = (struct ip_matcher *)ip_matcher;
    ip_matcher_free(matcher);
}

int ip_runtime_commit(void *ip_runtime, const char *table_name, long long maat_rt_version)
{
    if (NULL == ip_runtime) {
        return -1;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;

    int updating_flag = rcu_hash_is_updating(ip_rt->item_hash);
    if (0 == updating_flag) {
        return 0;
    }

    ip_rt->ipv6_rule_num = 0;

    struct ip_rule *rules = NULL;
    struct interval_rule *intval_rules = NULL;
    void **ex_data_array = NULL;

    size_t rule_cnt = rcu_updating_hash_list(ip_rt->item_hash, &ex_data_array);
    if (rule_cnt > 0) {
        rules = ALLOC(struct ip_rule, rule_cnt);
        intval_rules = ALLOC(struct interval_rule, rule_cnt);
        for (size_t i = 0; i < rule_cnt; i++) {
            struct ip_item *item = (struct ip_item *)ex_data_array[i];
            if (item->addr_type == IPv6) {
                ip_rt->ipv6_rule_num++;
            }

            ip_item_to_ip_rule(item, &rules[i]);
            ip_item_to_port_rule(item, &intval_rules[i]);
        }
    }

    int ret = 0;
    size_t mem_used = 0;
    struct ip_matcher *new_ip_matcher = NULL;
    struct ip_matcher *old_ip_matcher = NULL;

    struct interval_matcher *new_intval_matcher = NULL;
    struct interval_matcher *old_intval_matcher = NULL;

    if (rule_cnt > 0) {
        new_ip_matcher = ip_matcher_new(rules, rule_cnt, &mem_used);
        if (NULL == new_ip_matcher) {
            log_error(ip_rt->logger, MODULE_IP,
                      "[%s:%d] table[%s] rebuild ip_matcher engine failed when update %zu ip rules",
                      __FUNCTION__, __LINE__, table_name, rule_cnt);
            ret = -1;
        }

        new_intval_matcher = interval_matcher_new(intval_rules, rule_cnt);
        if (NULL == new_intval_matcher) {
            log_error(ip_rt->logger, MODULE_IP,
                      "[%s:%d] table[%s] rebuild interval_matcher engine failed when update %zu ip rules",
                      __FUNCTION__, __LINE__, table_name, rule_cnt);
            ret = -1;
        }
    }

    old_ip_matcher = ip_rt->ip_matcher;
    ip_rt->ip_matcher = new_ip_matcher;
    rcu_hash_commit(ip_rt->item_hash);

    if (old_ip_matcher != NULL) {
        maat_garbage_bagging(ip_rt->ref_garbage_bin, old_ip_matcher, NULL,
                             garbage_ip_matcher_free);
    }

    old_intval_matcher = ip_rt->intval_matcher;
    ip_rt->intval_matcher = new_intval_matcher;
    if (old_intval_matcher != NULL) {
        maat_garbage_bagging(ip_rt->ref_garbage_bin, old_intval_matcher, NULL,
                             garbage_interval_matcher_free);
    }

    ip_rt->rule_num = rule_cnt;
    ip_rt->version = maat_rt_version;
    
    log_info(ip_rt->logger, MODULE_IP,
             "table[%s] commit %zu ip rules and rebuild ip_matcher completed, version:%lld", 
             table_name, rule_cnt, ip_rt->version);

    if (rules != NULL) {
        FREE(rules);
    }
    
    if (intval_rules != NULL) {
        FREE(intval_rules);
    }

    if (ex_data_array != NULL) {
        FREE(ex_data_array);
    }

    return ret;
}

long long ip_runtime_rule_count(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return 0;
    }
    
    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    return ip_rt->rule_num;
}

long long ip_runtime_ipv6_rule_count(void *ip_runtime)
{
     if (NULL == ip_runtime) {
        return 0;
    }
    
    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    return ip_rt->ipv6_rule_num;
}

int validate_port(struct ip_item *item, uint16_t port, int proto)
{
    uint16_t host_port = ntohs(port);

    if (item->min_port > host_port || item->max_port < host_port) {
        return -1;
    }
    
    if (item->proto != -1 && item->proto != proto) {
        return -1;
    }

    return 0;
}

int validate_proto(struct ip_item *item, int proto)
{
    if (item->proto != -1 && item->proto != proto) {
        return -1;
    }

    return 0;
}

int ip_runtime_scan(struct ip_runtime *ip_rt, int thread_id, int ip_type, 
                    uint8_t *ip_addr, uint16_t port, int proto, int vtable_id, 
                    struct maat_state *state)
{
    if (0 == ip_rt->rule_num) {
        //empty ip table
        return 0;
    }
    
    struct scan_result ip_results[MAX_SCANNER_HIT_ITEM_NUM];

    /* if ip_addr = "0.0.0.0" means any ip */
    int any_ip_flag = 0;

    struct ip_data scan_data;
    if (ip_type == IPv4) {
        scan_data.type = IPv4;
        scan_data.ipv4 = ntohl(*(uint32_t *)ip_addr);
        if (0 == scan_data.ipv4) {
            any_ip_flag = 1;
        }
    } else {
        scan_data.type = IPv6;
        for (int i = 0; i < 4; i++) {
            scan_data.ipv6[i] = *((uint32_t *)ip_addr + i);
        }
        ipv6_ntoh(scan_data.ipv6);
        if (0 == scan_data.ipv6[0] && 0 == scan_data.ipv6[1] &&
            0 == scan_data.ipv6[2] && 0 == scan_data.ipv6[3]) {
            any_ip_flag = 1;
        }
    }

    int ret = 0;
    size_t real_hit_item_cnt = 0;
    struct maat_item hit_maat_items[MAX_SCANNER_HIT_ITEM_NUM];

    // any ip, so scan port+proto
    if (1 == any_ip_flag) {
        struct interval_result port_results[MAX_SCANNER_HIT_ITEM_NUM];
        uint16_t host_port = ntohs(port);
        int n_hit_port_item = interval_matcher_match(ip_rt->intval_matcher, host_port, 
                                                     port_results, MAX_SCANNER_HIT_ITEM_NUM);
        if (n_hit_port_item <= 0) {
            return n_hit_port_item;
        }

        if (n_hit_port_item > MAX_SCANNER_HIT_ITEM_NUM) {
            n_hit_port_item = MAX_SCANNER_HIT_ITEM_NUM;
        }

        for (int i = 0; i < n_hit_port_item; i++) {
            long long item_id = port_results[i].rule_id;
            struct ip_item *ip_item = (struct ip_item *)rcu_hash_find(ip_rt->item_hash,
                                                                      (char *)&item_id,
                                                                      sizeof(long long));
            if (!ip_item) {
                // item config has been deleted
                continue;
            }

            ret = validate_proto(ip_item, proto);
            if (ret < 0) {
                continue;
            }

            hit_maat_items[real_hit_item_cnt].item_id = port_results[i].rule_id;
            hit_maat_items[real_hit_item_cnt].group_id = ip_item->group_id;
            real_hit_item_cnt++;
        }
    } else {
        int n_hit_ip_item = ip_matcher_match(ip_rt->ip_matcher, &scan_data,
                                             ip_results, MAX_SCANNER_HIT_ITEM_NUM);
        if (n_hit_ip_item <= 0) {
            return n_hit_ip_item;
        }

        if (n_hit_ip_item > MAX_SCANNER_HIT_ITEM_NUM) {
            n_hit_ip_item = MAX_SCANNER_HIT_ITEM_NUM;
        }

        for (size_t i = 0; i < n_hit_ip_item; i++) {
            long long item_id = ip_results[i].rule_id;
            struct ip_item *ip_item = (struct ip_item *)rcu_hash_find(ip_rt->item_hash,
                                                                      (char *)&item_id,
                                                                      sizeof(long long));
            if (!ip_item) {
                // item config has been deleted
                continue;
            }

            ret = validate_port(ip_item, port, proto);
            if (ret < 0) {
                continue;
            }

            hit_maat_items[real_hit_item_cnt].item_id = ip_results[i].rule_id;
            hit_maat_items[real_hit_item_cnt].group_id = ip_item->group_id;
            real_hit_item_cnt++;
        }
    }

    maat_compile_state_update(vtable_id, hit_maat_items, real_hit_item_cnt, state);

    return real_hit_item_cnt;
}

void ip_runtime_hit_inc(struct ip_runtime *ip_rt, int thread_id)
{
    if (NULL == ip_rt || thread_id < 0) {
        return;
    }

    alignment_int64_array_add(ip_rt->hit_cnt, thread_id, 1);
}

void ip_runtime_perf_stat(struct ip_runtime *ip_rt, struct timespec *start,
                          struct timespec *end, int thread_id)
{
    if (NULL == ip_rt || thread_id < 0) {
        return;
    }

    alignment_int64_array_add(ip_rt->scan_cnt, thread_id, 1);
    
    if (start != NULL && end != NULL) {
        long long consume_time = (end->tv_sec - start->tv_sec) * 1000000000 + end->tv_nsec - start->tv_nsec;
        alignment_int64_array_add(ip_rt->scan_cpu_time, thread_id, consume_time);
    }
}

long long ip_runtime_scan_count(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return 0;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    long long sum = alignment_int64_array_sum(ip_rt->scan_cnt, ip_rt->n_worker_thread);
    alignment_int64_array_reset(ip_rt->scan_cnt, ip_rt->n_worker_thread);

    return sum;
}

long long ip_runtime_scan_cpu_time(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return 0;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    long long sum = alignment_int64_array_sum(ip_rt->scan_cpu_time, 
                                              ip_rt->n_worker_thread);
    alignment_int64_array_reset(ip_rt->scan_cpu_time, ip_rt->n_worker_thread);

    return sum;
}

long long ip_runtime_hit_count(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return 0;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    long long sum = alignment_int64_array_sum(ip_rt->hit_cnt, ip_rt->n_worker_thread);
    alignment_int64_array_reset(ip_rt->hit_cnt, ip_rt->n_worker_thread);

    return sum;
}

long long ip_runtime_update_err_count(void *ip_runtime)
{
    if (NULL == ip_runtime) {
        return 0;
    }

    struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
    return ip_rt->update_err_cnt;
}