From 0de3b8c933e1a6c14b2d22c27351daf187a92340 Mon Sep 17 00:00:00 2001 From: hladu357 Date: Mon, 10 Jun 2024 11:00:30 +0200 Subject: [PATCH] final touch ;) --- radix/Makefile | 2 ++ radix/radix.c | 40 ++++++++++++++++++---------------------- radix/radix.h | 4 ++-- tests/test-radix.py | 5 +++-- 4 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 radix/Makefile 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..2e7b295 100644 --- a/radix/radix.c +++ b/radix/radix.c @@ -1,7 +1,7 @@ #include "radix.h" #define GET_BIT(key, mask) ( ((key[0] & mask[0]) | (key[1] & mask[1])) != 0 ) -#define SHIFT_MASK(mask) mask[0] >>= 1, mask[1] = (mask[1] >> 1) | (!mask[0] * (1ul << 63)) +#define SHIFT_MASK(mask) ( mask[0] >>= 1, mask[1] = (mask[1] >> 1) | (!mask[0] * (1ul << 63)) ) int radix_insert(radix_holder_t, record_t); int radix_search(radix_holder_t, record_t); @@ -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) { @@ -62,7 +62,7 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { t->_root = create_node(r); return t->_size = 1; } - /*todo zkontrolovat mezni hodnoty, masku 128 a tak.. */ + for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) { b = GET_BIT(r->_ip, mask); if (ibit == node->_data._mask) { /* node ends */ @@ -70,7 +70,6 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { if (!*next) { /* add child */ *next = create_node(r); - ++t->_size; return 1; } @@ -81,7 +80,7 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) { if (b != GET_BIT(node->_data._ip, mask)) {/* bit differs -> split node */ split_node(node, ibit); - next = b ? &node->_l : &node->_r; /* new node */ + next = b ? &node->_l : &node->_r; *next = create_node(r); ++t->_size; @@ -102,25 +101,21 @@ 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; + ++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 +132,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 +146,8 @@ int radix_search(radix_holder_t t, record_t r) { if (!iter) return r->_rule; } + + /* full match */ if (ibit == iter->_data._mask && iter->_data._rule != INVALID_RULE) { r->_rule = iter->_data._rule; m = iter->_data._mask; @@ -172,7 +168,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 20 #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 = []