Compare commits
4 Commits
f64503c1c6
...
51daa79d39
Author | SHA1 | Date |
---|---|---|
hladu357 | 51daa79d39 | |
hladu357 | 7cab4db54a | |
hladu357 | f278d1a799 | |
hladu357 | 75b8e2712b |
190
radix/radix.c
190
radix/radix.c
|
@ -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])
|
#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);
|
||||||
|
@ -11,9 +11,8 @@ radix_holder_t create_holder() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -38,7 +37,7 @@ int destory_holder(struct radix_holder* t) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int destory_node(struct radix_node* n) {
|
static int destory_node(struct radix_node* n) {
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
if (!n) return ret;
|
if (!n) return ret;
|
||||||
ret = destory_node(n->_l) + destory_node(n->_r);
|
ret = destory_node(n->_l) + destory_node(n->_r);
|
||||||
|
@ -50,10 +49,14 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
|
||||||
unsigned char b;
|
unsigned char b;
|
||||||
unsigned char ibit = 0;
|
unsigned char ibit = 0;
|
||||||
unsigned long mask[2] = {0x01ul << 63, 0x00};
|
unsigned long mask[2] = {0x01ul << 63, 0x00};
|
||||||
radix_node_t node = t->_root;
|
radix_node_t node;
|
||||||
radix_node_t* next = &t->_root;
|
radix_node_t* next;
|
||||||
|
|
||||||
r->_valid = INVALID_RULE;
|
if (!r || !t)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
node = t->_root;
|
||||||
|
next = &t->_root;
|
||||||
|
|
||||||
if (!node) {
|
if (!node) {
|
||||||
t->_root = create_node(r);
|
t->_root = create_node(r);
|
||||||
|
@ -93,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;
|
||||||
|
@ -101,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;
|
||||||
|
@ -116,30 +117,48 @@ int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int radix_search(radix_holder_t t, record_t r) {
|
int radix_search(radix_holder_t t, record_t r) {
|
||||||
|
radix_node_t iter;
|
||||||
|
int ibit;
|
||||||
|
unsigned char b;
|
||||||
|
int m;
|
||||||
|
unsigned long mask[2] = {0x01ul << 63, 0x00};
|
||||||
|
|
||||||
int ret = INVALID_RULE;
|
if (!r)
|
||||||
/*
|
|
||||||
radix_node_t iter = t->_root;
|
|
||||||
int ibit = 0;
|
|
||||||
if (!iter)
|
|
||||||
return INVALID_RULE;
|
return INVALID_RULE;
|
||||||
|
|
||||||
|
if (!t || !t->_root)
|
||||||
|
return r->_rule = INVALID_RULE;
|
||||||
|
|
||||||
for (;ibit < 128; ++ibit) {
|
iter = t->_root;
|
||||||
b = GET_BIT(key, ibit);
|
|
||||||
if (ibit < iter->_ip_end_bit &&
|
for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) {
|
||||||
b == GET_BIT(iter->_ip, ibit))
|
b = GET_BIT(r->_ip, mask);
|
||||||
|
if (ibit < iter->_data._mask && /* bit match */
|
||||||
|
b == GET_BIT(iter->_data._ip, mask))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (ibit == iter->_ip_end_bit) // set result if full match
|
if (ibit < iter->_data._mask) { /* bit mismatch */
|
||||||
ret = iter->_rule_valid ? iter->_rule : ret;
|
return r->_rule;
|
||||||
|
/* = ibit == iter->_data._mask ? iter->_data._rule : r->_rule; */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mask ends with full match */
|
||||||
|
if (iter->_data._rule != INVALID_RULE) {
|
||||||
|
r->_rule = iter->_data._rule;
|
||||||
|
m = iter->_data._mask;
|
||||||
|
}
|
||||||
|
|
||||||
iter = b ? iter->_l : iter->_r;
|
iter = b ? iter->_l : iter->_r;
|
||||||
if (!iter)
|
if (!iter)
|
||||||
return ret;
|
return r->_rule;
|
||||||
}
|
}
|
||||||
*/
|
if (ibit == iter->_data._mask && iter->_data._rule != INVALID_RULE) {
|
||||||
return ret;
|
r->_rule = iter->_data._rule;
|
||||||
|
m = iter->_data._mask;
|
||||||
|
}
|
||||||
|
r->_mask = m;
|
||||||
|
|
||||||
|
return r->_rule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -149,14 +168,15 @@ int radix_search(radix_holder_t t, record_t r) {
|
||||||
/*--------------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------------*/
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#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 0 /*66319*/
|
#define TEST_ROWS 39222 /*39222*/
|
||||||
#define READ_MODE "r"
|
#define READ_MODE "r"
|
||||||
|
|
||||||
int parse_line(FILE* f, record_t r) {
|
int parse_line(FILE* f, record_t r) {
|
||||||
int ikey, jkey;
|
int ikey, jkey, mask, rule;
|
||||||
unsigned long key[2] = {0,0};
|
unsigned long key[2] = {0,0};
|
||||||
struct in6_addr addr;
|
struct in6_addr addr;
|
||||||
char ipv6_cidr[50];
|
char ipv6_cidr[50];
|
||||||
|
@ -172,24 +192,26 @@ int parse_line(FILE* f, record_t r) {
|
||||||
separator = strchr(ipv6_cidr, '/');
|
separator = strchr(ipv6_cidr, '/');
|
||||||
if (!separator) {
|
if (!separator) {
|
||||||
printf("invalid CIDR notation\n");
|
printf("invalid CIDR notation\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*separator++ = '\0';
|
*separator++ = '\0';
|
||||||
r->_mask = atoi(separator);
|
mask = atoi(separator);
|
||||||
|
|
||||||
separator = strchr(separator, ' ');
|
separator = strchr(separator, ' ');
|
||||||
r->_rule = atoi(++separator);
|
rule = atoi(++separator);
|
||||||
|
|
||||||
if (r->_mask < 0 || r->_mask > 128) {
|
if (mask < 0 || mask > 128) {
|
||||||
printf("invalid prefix length\n");
|
printf("invalid prefix length\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
r->_mask = mask;
|
||||||
|
|
||||||
if (r->_rule < 0 || r->_rule > 65536) {
|
if (rule < 0 || rule > 65536) {
|
||||||
printf("invalid pop\n");
|
printf("invalid pop\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
r->_rule = rule;
|
||||||
|
|
||||||
if (inet_pton(AF_INET6, ipv6_cidr, &addr) != 1) {
|
if (inet_pton(AF_INET6, ipv6_cidr, &addr) != 1) {
|
||||||
printf("%s invalid address\n", ipv6_cidr);
|
printf("%s invalid address\n", ipv6_cidr);
|
||||||
|
@ -204,8 +226,7 @@ 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 !feof(f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_input(radix_holder_t t) {
|
int load_input(radix_holder_t t) {
|
||||||
|
@ -215,7 +236,7 @@ int load_input(radix_holder_t t) {
|
||||||
|
|
||||||
if (!(f = fopen(RULES_FILE, READ_MODE))) {
|
if (!(f = fopen(RULES_FILE, READ_MODE))) {
|
||||||
puts("cant open input file");
|
puts("cant open input file");
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (parse_line(f, &r)) {
|
while (parse_line(f, &r)) {
|
||||||
|
@ -228,67 +249,74 @@ int load_input(radix_holder_t t) {
|
||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_tests(unsigned long** subnets, int** rref) {
|
int load_tests(struct routing_record** ref) {
|
||||||
/*
|
|
||||||
unsigned long* ips;
|
|
||||||
int* ref;
|
|
||||||
int row;
|
|
||||||
FILE* f;
|
FILE* f;
|
||||||
struct routing_record r;
|
int cnt = 0;
|
||||||
|
struct routing_record* r = malloc(sizeof(struct routing_record) * TEST_ROWS);
|
||||||
|
|
||||||
if (!(f = fopen( TEST_FILE, READ_MODE ))) {
|
if (!(f = fopen(TEST_FILE, READ_MODE))) {
|
||||||
puts("cant open test file");
|
puts("cant open test file");
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ips = calloc( TEST_ROWS, sizeof(unsigned long) );
|
for (cnt = 0; cnt < TEST_ROWS; ++cnt) {
|
||||||
ref = calloc( TEST_ROWS, sizeof(int) );
|
if (!parse_line(f, &r[cnt])) {
|
||||||
|
printf("WARNING test parse failed %d\n", cnt);
|
||||||
for (row = 0; row < TEST_ROWS; ++ row) {
|
break;
|
||||||
parse_line(f, &r);
|
|
||||||
ips[row] = r._ip;
|
|
||||||
ref[row] = r._rule;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
*subnets = ips;
|
|
||||||
*rref = ref;
|
|
||||||
*/
|
|
||||||
return TEST_ROWS;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fire_tests(radix_holder_t t, unsigned long* subnets, int* ref, int num_tests ) {
|
|
||||||
/*
|
|
||||||
int row;
|
|
||||||
int wrong = 0;
|
|
||||||
struct routing_record r;
|
|
||||||
|
|
||||||
for (row = 0; row < TEST_ROWS; ++ row) {
|
|
||||||
r._ip = subnets[row];
|
|
||||||
r._ip = subnets[row+1];
|
|
||||||
radix_search(t, &r);
|
|
||||||
if ( !r._valid || r._rule != ref[row]) {
|
|
||||||
printf("ref: %d got: %d @ %d\n", ref[row], r._rule, row );
|
|
||||||
++wrong;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(subnets);
|
fclose(f);
|
||||||
free(ref);
|
*ref = r;
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fire_tests(radix_holder_t t, struct routing_record* r) {
|
||||||
|
int row;
|
||||||
|
int wrong = TEST_ROWS;
|
||||||
|
unsigned short ref_pop, ref_scope;
|
||||||
|
|
||||||
|
clock_t start, end;
|
||||||
|
double lookup_time;
|
||||||
|
|
||||||
|
start = clock();
|
||||||
|
|
||||||
|
for (row = 0; row < TEST_ROWS; ++row) {
|
||||||
|
ref_pop = r[row]._rule;
|
||||||
|
ref_scope = r[row]._mask;
|
||||||
|
|
||||||
|
r[row]._rule = INVALID_RULE;
|
||||||
|
radix_search(t, &r[row]);
|
||||||
|
if (ref_pop == r[row]._rule && ref_scope == r[row]._mask) {
|
||||||
|
--wrong;
|
||||||
|
} else {
|
||||||
|
if (ref_pop != r[row]._rule)
|
||||||
|
printf("pop: %d got: %d @ %d\n", ref_pop, r[row]._rule, row );
|
||||||
|
if (ref_scope != r[row]._mask)
|
||||||
|
printf("scp: %d got: %d @ %d\n", ref_scope, r[row]._mask, row );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end = clock();
|
||||||
|
lookup_time = ((double) (end - start)) / CLOCKS_PER_SEC * 1000;
|
||||||
|
|
||||||
|
printf("%d lookups in %f ms (%f microseconds per op)\n", TEST_ROWS, lookup_time, ((end - start)/(double)TEST_ROWS));
|
||||||
|
|
||||||
|
free(r);
|
||||||
|
|
||||||
return wrong;
|
return wrong;
|
||||||
*/
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
unsigned long* subnets;
|
struct routing_record* ref;
|
||||||
int* ref;
|
|
||||||
|
|
||||||
radix_holder_t t = create_holder();
|
radix_holder_t t = create_holder();
|
||||||
|
|
||||||
printf("Loaded %d rules\n", load_input(t) );
|
printf("Loaded %d rules\n", load_input(t) );
|
||||||
printf("Loaded %d tests\n", load_tests(&subnets, &ref) );
|
printf("Loaded %d tests\n", load_tests(&ref) );
|
||||||
printf("%d tests wrong\n", fire_tests(t, subnets, ref, TEST_ROWS) );
|
printf("%d tests wrong\n", fire_tests(t, ref) );
|
||||||
|
|
||||||
destory_holder(t);
|
destory_holder(t);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue