final touch ;)

This commit is contained in:
hladu357 2024-06-10 11:00:30 +02:00
parent 86db3f9801
commit 0de3b8c933
4 changed files with 25 additions and 26 deletions

2
radix/Makefile Normal file
View File

@ -0,0 +1,2 @@
radix: radix.c radix.h
gcc radix.c -Wall -pedantic -std=c89 -O2 -o radix

View File

@ -1,7 +1,7 @@
#include "radix.h" #include "radix.h"
#define GET_BIT(key, mask) ( ((key[0] & mask[0]) | (key[1] & mask[1])) != 0 ) #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_insert(radix_holder_t, record_t);
int radix_search(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; 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); radix_node_t tmp = clone_node(n);
n->_data._mask = bit; n->_data._mask = bit;
n->_l = n->_r = tmp; 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) { int destory_holder(struct radix_holder* t) {
destory_node(t->_root); int tmp = destory_node(t->_root);
free(t); free(t);
return 1; return tmp;
} }
static int destory_node(struct radix_node* n) { 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); t->_root = create_node(r);
return t->_size = 1; return t->_size = 1;
} }
/*todo zkontrolovat mezni hodnoty, masku 128 a tak.. */
for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) { for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) {
b = GET_BIT(r->_ip, mask); b = GET_BIT(r->_ip, mask);
if (ibit == node->_data._mask) { /* node ends */ 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 */ if (!*next) { /* add child */
*next = create_node(r); *next = create_node(r);
++t->_size; ++t->_size;
return 1; 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 */ if (b != GET_BIT(node->_data._ip, mask)) {/* bit differs -> split node */
split_node(node, ibit); split_node(node, ibit);
next = b ? &node->_l : &node->_r; /* new node */ next = b ? &node->_l : &node->_r;
*next = create_node(r); *next = create_node(r);
++t->_size; ++t->_size;
@ -102,25 +101,21 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
} }
/* exact match */ /* exact match */
if (node->_data._mask == r->_mask && ibit == r->_mask) { if (node->_data._rule != INVALID_RULE) {
if (node->_data._rule != INVALID_RULE) r->_rule = INVALID_RULE;
return 0; return 0;
}
/* make valid */ /* make valid */
node->_data._rule = r->_rule; node->_data._rule = r->_rule;
++t->_size; ++t->_size;
return 1; return 1;
}
printf("!!!!!!!!!!!!!!\n");
return 0;
} }
int radix_search(radix_holder_t t, record_t r) { int radix_search(radix_holder_t t, record_t r) {
radix_node_t iter; radix_node_t iter;
int ibit; int ibit;
unsigned char b; unsigned char b;
int m; int m = 0;
unsigned long mask[2] = {0x01ul << 63, 0x00}; unsigned long mask[2] = {0x01ul << 63, 0x00};
if (!r) if (!r)
@ -139,7 +134,6 @@ int radix_search(radix_holder_t t, record_t r) {
if (ibit < iter->_data._mask) { /* bit mismatch */ if (ibit < iter->_data._mask) { /* bit mismatch */
return r->_rule; return r->_rule;
/* = ibit == iter->_data._mask ? iter->_data._rule : r->_rule; */
} }
/* mask ends with full match */ /* mask ends with full match */
@ -152,6 +146,8 @@ int radix_search(radix_holder_t t, record_t r) {
if (!iter) if (!iter)
return r->_rule; return r->_rule;
} }
/* full match */
if (ibit == iter->_data._mask && iter->_data._rule != INVALID_RULE) { if (ibit == iter->_data._mask && iter->_data._rule != INVALID_RULE) {
r->_rule = iter->_data._rule; r->_rule = iter->_data._rule;
m = iter->_data._mask; 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 RULES_FILE "../data/routing-data"
#define TEST_FILE "../data/test-data" #define TEST_FILE "../data/test-data"
#define TEST_ROWS 39222 /*39222*/ #define TEST_ROWS 20
#define READ_MODE "r" #define READ_MODE "r"
int parse_line(FILE* f, record_t r) { int parse_line(FILE* f, record_t r) {

View File

@ -5,12 +5,12 @@
#include "stdlib.h" #include "stdlib.h"
#include "string.h" #include "string.h"
#define INVALID_RULE 0 #define INVALID_RULE 0xffff
struct routing_record { struct routing_record {
unsigned long _ip[2]; unsigned long _ip[2];
unsigned short _rule; unsigned short _rule;
unsigned _mask:8; unsigned char _mask;
}; };
struct radix_node { struct radix_node {

View File

@ -10,7 +10,7 @@ def parse_rule(rule):
def generate_random_test_cases(rules, n): def generate_random_test_cases(rules, n):
with open('../data/test-data', 'w') as f: with open('../data/test-data', 'w') as f:
for rule in rules: for i, rule in enumerate(rules):
rule_network = rule[0] rule_network = rule[0]
rule_next_hop = rule[1] 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") 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(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(): def main():
rules = [] rules = []