rule valid flag removed

This commit is contained in:
hladu357 2024-06-09 17:36:35 +02:00
parent 75b8e2712b
commit f278d1a799
2 changed files with 17 additions and 22 deletions

View File

@ -13,7 +13,6 @@ radix_holder_t create_holder() {
static radix_node_t create_node(record_t record) { static radix_node_t create_node(record_t record) {
radix_node_t ret = malloc(sizeof(struct radix_node)); radix_node_t ret = malloc(sizeof(struct radix_node));
record->_valid = VALID_RULE;
memcpy(&ret->_data, record, sizeof(struct routing_record)); memcpy(&ret->_data, record, sizeof(struct routing_record));
ret->_l = ret->_r = NULL; ret->_l = ret->_r = NULL;
return ret; return ret;
@ -29,7 +28,7 @@ static void split_node(radix_node_t n, unsigned bit) {
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;
n->_data._valid = INVALID_RULE; n->_data._rule = INVALID_RULE;
} }
int destory_holder(struct radix_holder* t) { int destory_holder(struct radix_holder* t) {
@ -59,8 +58,6 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
node = t->_root; node = t->_root;
next = &t->_root; next = &t->_root;
r->_valid = INVALID_RULE;
if (!node) { if (!node) {
t->_root = create_node(r); t->_root = create_node(r);
return t->_size = 1; return t->_size = 1;
@ -99,7 +96,6 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
next = b ? &node->_r : &node->_l; next = b ? &node->_r : &node->_l;
*next = NULL; *next = NULL;
r->_valid = VALID_RULE;
memcpy(&node->_data, r, sizeof(struct routing_record)); memcpy(&node->_data, r, sizeof(struct routing_record));
++t->_size; ++t->_size;
return 1; return 1;
@ -107,10 +103,9 @@ 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._mask == r->_mask && ibit == r->_mask) {
if (node->_data._valid) if (node->_data._rule != INVALID_RULE)
return 0; return 0;
/* make valid */ /* make valid */
node->_data._valid = VALID_RULE;
node->_data._rule = r->_rule; node->_data._rule = r->_rule;
++t->_size; ++t->_size;
return 1; return 1;
@ -134,21 +129,25 @@ int radix_search(radix_holder_t t, record_t r) {
return r->_rule = INVALID_RULE; return r->_rule = INVALID_RULE;
iter = t->_root; iter = t->_root;
ret = r->_rule = iter->_data._valid ? iter->_data._rule : INVALID_RULE;
for (ibit = 0;ibit < 128; ++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 < iter->_data._mask && if (ibit < iter->_data._mask && /* bit match */
b == GET_BIT(iter->_data._ip, mask)) b == GET_BIT(iter->_data._ip, mask))
continue; continue;
if (ibit >= iter->_data._mask) /* match upto node mask */ if (ibit < iter->_data._mask) { /* bit mismatch */
ret = r->_rule = iter->_data._valid ? iter->_data._rule : ret; return r->_rule = INVALID_RULE;
}
/* mask ends with full match */
ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE;
iter = b ? iter->_l : iter->_r; iter = b ? iter->_l : iter->_r;
if (!iter) if (!iter)
return ret; return ret;
} }
ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE;
return ret; return ret;
} }
@ -216,7 +215,6 @@ int parse_line(FILE* f, record_t r) {
r->_ip[0] = key[0]; r->_ip[0] = key[0];
r->_ip[1] = key[1]; r->_ip[1] = key[1];
r->_valid = VALID_RULE;
return 1; return 1;
} }
@ -267,7 +265,7 @@ int fire_tests(radix_holder_t t, struct routing_record* r) {
for (row = 0; row < TEST_ROWS; ++row, ++r) { for (row = 0; row < TEST_ROWS; ++row, ++r) {
ref_pop = r->_rule; ref_pop = r->_rule;
radix_search(t, r); radix_search(t, r);
if ( !r->_valid || ref_pop != r->_rule) { if ( ref_pop != r->_rule) {
printf("ref: %d got: %d @ %d\n", ref_pop, r->_rule, row ); printf("ref: %d got: %d @ %d\n", ref_pop, r->_rule, row );
++wrong; ++wrong;
} }

View File

@ -5,15 +5,12 @@
#include "stdlib.h" #include "stdlib.h"
#include "string.h" #include "string.h"
#define VALID_RULE 1
#define INVALID_RULE 0 #define INVALID_RULE 0
#define RULE_NOT_FOUND -1
struct routing_record { struct routing_record {
unsigned long _ip[2]; unsigned long _ip[2];
unsigned short _rule; unsigned short _rule;
unsigned _mask:7; unsigned _mask:8;
unsigned _valid:1;
}; };
struct radix_node { struct radix_node {
@ -32,12 +29,12 @@ typedef struct radix_holder* radix_holder_t;
typedef struct routing_record* record_t; typedef struct routing_record* record_t;
radix_holder_t create_holder(); radix_holder_t create_holder();
radix_node_t create_node(record_t record); static radix_node_t create_node(record_t record);
static radix_node_t clone_node(radix_node_t n); static radix_node_t clone_node(radix_node_t n);
static void split_node(struct radix_node* n, unsigned bit); static void split_node(struct radix_node* n, unsigned bit);
int destory_holder(struct radix_holder*); int destory_holder(struct radix_holder*);
int destory_node(struct radix_node*); static int destory_node(struct radix_node*);
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);