diff --git a/radix/Makefile b/radix/Makefile new file mode 100644 index 0000000..9b33931 --- /dev/null +++ b/radix/Makefile @@ -0,0 +1,2 @@ +radix: radix.c radix.h + gcc radix.c -Wall -pedantic -std=c89 -O2 -o radix \ No newline at end of file diff --git a/radix/radix.c b/radix/radix.c index d51038d..9dd3881 100644 --- a/radix/radix.c +++ b/radix/radix.c @@ -24,7 +24,7 @@ static radix_node_t clone_node(radix_node_t n) { return ret; } -static void split_node(radix_node_t n, unsigned bit) { +static void split_node(radix_node_t n, unsigned bit) { /* sets cloned node as both childs */ radix_node_t tmp = clone_node(n); n->_data._mask = bit; n->_l = n->_r = tmp; @@ -32,9 +32,9 @@ static void split_node(radix_node_t n, unsigned bit) { } int destory_holder(struct radix_holder* t) { - destory_node(t->_root); + int tmp = destory_node(t->_root); free(t); - return 1; + return tmp; } static int destory_node(struct radix_node* n) { @@ -102,25 +102,23 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { } /* exact match */ - if (node->_data._mask == r->_mask && ibit == r->_mask) { - if (node->_data._rule != INVALID_RULE) - return 0; - /* make valid */ - node->_data._rule = r->_rule; - ++t->_size; - return 1; + if (node->_data._rule != INVALID_RULE) { + r->_rule = INVALID_RULE; + return 0; } - - - printf("!!!!!!!!!!!!!!\n"); - return 0; + /* make valid */ + node->_data._rule = r->_rule; + r->_mask = node->_data._mask; + r->_rule = node->_data._rule; + ++t->_size; + return 1; } int radix_search(radix_holder_t t, record_t r) { radix_node_t iter; int ibit; unsigned char b; - int m; + int m = 0; unsigned long mask[2] = {0x01ul << 63, 0x00}; if (!r) @@ -137,9 +135,8 @@ int radix_search(radix_holder_t t, record_t r) { b == GET_BIT(iter->_data._ip, mask)) continue; - if (ibit < iter->_data._mask) { /* bit mismatch */ + if (ibit < iter->_data._mask) { /* bit mismatch */ return r->_rule; - /* = ibit == iter->_data._mask ? iter->_data._rule : r->_rule; */ } /* mask ends with full match */ @@ -152,6 +149,7 @@ int radix_search(radix_holder_t t, record_t r) { if (!iter) return r->_rule; } + if (ibit == iter->_data._mask && iter->_data._rule != INVALID_RULE) { r->_rule = iter->_data._rule; m = iter->_data._mask; @@ -172,7 +170,7 @@ int radix_search(radix_holder_t t, record_t r) { #define RULES_FILE "../data/routing-data" #define TEST_FILE "../data/test-data" -#define TEST_ROWS 39222 /*39222*/ +#define TEST_ROWS 36764 #define READ_MODE "r" int parse_line(FILE* f, record_t r) { diff --git a/radix/radix.h b/radix/radix.h index 89e4529..46562ce 100644 --- a/radix/radix.h +++ b/radix/radix.h @@ -5,12 +5,12 @@ #include "stdlib.h" #include "string.h" -#define INVALID_RULE 0 +#define INVALID_RULE 0xffff struct routing_record { unsigned long _ip[2]; unsigned short _rule; - unsigned _mask:8; + unsigned char _mask; }; struct radix_node { diff --git a/tests/test-radix.py b/tests/test-radix.py index fdf5f43..c47bec9 100644 --- a/tests/test-radix.py +++ b/tests/test-radix.py @@ -10,7 +10,7 @@ def parse_rule(rule): def generate_random_test_cases(rules, n): with open('../data/test-data', 'w') as f: - for rule in rules: + for i, rule in enumerate(rules): rule_network = rule[0] rule_next_hop = rule[1] @@ -32,7 +32,8 @@ def generate_random_test_cases(rules, n): f.write(f"{test_network.supernet(new_prefix=closest_rule[0].prefixlen)} {closest_rule[1]}\n") # print(f"Generated test case: {test_network} {closest_rule[1]}") - print("Tests for rule", rule_network, "with next hop", rule_next_hop, "generated") + percentage_completed = (i + 1) / len(rules) * 100 + print(f"[{percentage_completed:.2f}%]Tests for rule {rule_network} {rule_next_hop} generated") def main(): rules = []