Compare commits
3 Commits
e224d1fb98
...
f442ff7790
Author | SHA1 | Date |
---|---|---|
hladu357 | f442ff7790 | |
hladu357 | a94f373c4b | |
hladu357 | 071e384920 |
230
radix/radix.c
230
radix/radix.c
|
@ -1,32 +1,35 @@
|
||||||
#include "radix.h"
|
#include "radix.h"
|
||||||
|
|
||||||
#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1u << (15 - bit % 16)) ) != 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])
|
||||||
|
|
||||||
radix_node_t create_node(ipv6_t key, unsigned end_bit, unsigned rule) {
|
int radix_insert(radix_holder_t, record_t);
|
||||||
radix_node_t ret = calloc(1, sizeof(struct radix_node));
|
int radix_search(radix_holder_t, record_t);
|
||||||
ret->_ip = key;
|
|
||||||
ret->_ip_end_bit = end_bit;
|
radix_holder_t create_holder() {
|
||||||
ret->_rule_valid = VALID_RULE;
|
radix_holder_t ret = calloc(1, sizeof(struct radix_holder));
|
||||||
ret->_rule = rule;
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
radix_node_t create_node(record_t record) {
|
||||||
|
radix_node_t ret = malloc(sizeof(struct radix_node));
|
||||||
|
record->_valid = VALID_RULE;
|
||||||
|
memcpy(&ret->_data, record, sizeof(struct routing_record));
|
||||||
|
ret->_l = ret->_r = NULL;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static radix_node_t clone_node(radix_node_t n) {
|
static radix_node_t clone_node(radix_node_t n) {
|
||||||
radix_node_t ret = calloc(1, sizeof(struct radix_node));
|
radix_node_t ret = malloc(sizeof(struct radix_node));
|
||||||
memcpy(ret, n, sizeof(struct radix_node));
|
memcpy(ret, n, sizeof(struct radix_node));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void split_node(radix_node_t n, unsigned bit) {
|
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->_ip_end_bit = bit;
|
n->_data._mask = bit;
|
||||||
n->_l = n->_r = tmp;
|
n->_l = n->_r = tmp;
|
||||||
n->_rule_valid = INVALID_RULE;
|
n->_data._valid = INVALID_RULE;
|
||||||
}
|
|
||||||
|
|
||||||
radix_holder_t create_holder() {
|
|
||||||
radix_holder_t ret = calloc(1, sizeof(struct radix_holder));
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int destory_holder(struct radix_holder* t) {
|
int destory_holder(struct radix_holder* t) {
|
||||||
|
@ -43,24 +46,28 @@ int destory_node(struct radix_node* n) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int radix_insert(radix_holder_t t, ipv6_t key, int val, int mask) {
|
int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
|
||||||
unsigned char b;
|
unsigned char b;
|
||||||
int ibit;
|
unsigned char ibit = 0;
|
||||||
|
unsigned long mask[2] = {0x01ul << 63, 0x00};
|
||||||
radix_node_t node = t->_root;
|
radix_node_t node = t->_root;
|
||||||
radix_node_t* next = &t->_root;
|
radix_node_t* next = &t->_root;
|
||||||
|
|
||||||
if (!t->_root) {
|
r->_valid = INVALID_RULE;
|
||||||
t->_root = create_node(key, mask, val);
|
|
||||||
|
if (!node) {
|
||||||
|
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 < mask; ++ibit) {
|
for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) {
|
||||||
b = GET_BIT(key, ibit);
|
b = GET_BIT(r->_ip, mask);
|
||||||
if (ibit >= node->_ip_end_bit) { /* node key ends */
|
if (ibit == node->_data._mask) { /* node ends */
|
||||||
next = b ? &node->_l : &node->_r;
|
next = b ? &node->_l : &node->_r;
|
||||||
|
|
||||||
if (!*next) {
|
if (!*next) { /* add child */
|
||||||
*next = create_node(key, mask, val);
|
*next = create_node(r);
|
||||||
|
|
||||||
++t->_size;
|
++t->_size;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -69,48 +76,49 @@ int radix_insert(radix_holder_t t, ipv6_t key, int val, int mask) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (b == GET_BIT(node->_ip, ibit)) /* bit match */
|
if (b != GET_BIT(node->_data._ip, mask)) {/* bit differs -> split node */
|
||||||
continue;
|
split_node(node, ibit);
|
||||||
|
next = b ? &node->_l : &node->_r; /* new node */
|
||||||
|
*next = create_node(r);
|
||||||
|
|
||||||
next = b ? &node->_l : &node->_r;
|
++t->_size;
|
||||||
if (*next && ibit == mask - 1) { /* last bit mismatch */
|
return 1;
|
||||||
node = *next;
|
}
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
split_node(node, ibit); /* split node */
|
/* insert before */
|
||||||
*next = create_node(key, mask, val);
|
if (node->_data._mask > r->_mask) {
|
||||||
|
split_node(node, ibit);
|
||||||
|
b = GET_BIT(node->_data._ip, mask);
|
||||||
|
next = b ? &node->_r : &node->_l;
|
||||||
|
*next = NULL;
|
||||||
|
|
||||||
|
r->_valid = VALID_RULE;
|
||||||
|
memcpy(&node->_data, r, sizeof(struct routing_record));
|
||||||
++t->_size;
|
++t->_size;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* exact match */
|
/* exact match */
|
||||||
if (node->_rule_valid && node->_ip_end_bit == mask) {
|
if (node->_data._mask == r->_mask && ibit == r->_mask) {
|
||||||
printf("WARNING: conflicting rule %x:%x:%x:%x:%x:%x:%x:%x",
|
if (node->_data._valid)
|
||||||
key.s[0], key.s[1], key.s[2], key.s[3],
|
|
||||||
key.s[4], key.s[5], key.s[6], key.s[7]);
|
|
||||||
|
|
||||||
printf("/%d %d\n", mask, val);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
/* make valid */
|
||||||
|
node->_data._valid = VALID_RULE;
|
||||||
/* match with lower mask */
|
node->_data._rule = r->_rule;
|
||||||
if (node->_rule_valid) {
|
++t->_size;
|
||||||
split_node(node, ibit);
|
|
||||||
|
|
||||||
b = GET_BIT(node->_ip, ibit + 1);
|
|
||||||
next = b ? &node->_r : &node->_l;
|
|
||||||
*next = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->_rule_valid = VALID_RULE;
|
|
||||||
node->_rule = val;
|
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("!!!!!!!!!!!!!!\n");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int radix_search(radix_holder_t t, ipv6_t key) {
|
int radix_search(radix_holder_t t, record_t r) {
|
||||||
unsigned char b;
|
|
||||||
int ret = INVALID_RULE;
|
int ret = INVALID_RULE;
|
||||||
|
/*
|
||||||
radix_node_t iter = t->_root;
|
radix_node_t iter = t->_root;
|
||||||
int ibit = 0;
|
int ibit = 0;
|
||||||
if (!iter)
|
if (!iter)
|
||||||
|
@ -123,13 +131,14 @@ int radix_search(radix_holder_t t, ipv6_t key) {
|
||||||
b == GET_BIT(iter->_ip, ibit))
|
b == GET_BIT(iter->_ip, ibit))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (ibit == iter->_ip_end_bit) /* set result if full match */
|
if (ibit == iter->_ip_end_bit) // set result if full match
|
||||||
ret = iter->_rule_valid ? iter->_rule : ret;
|
ret = iter->_rule_valid ? iter->_rule : ret;
|
||||||
|
|
||||||
iter = b ? iter->_l : iter->_r;
|
iter = b ? iter->_l : iter->_r;
|
||||||
if (!iter)
|
if (!iter)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,30 +148,68 @@ int radix_search(radix_holder_t t, ipv6_t key) {
|
||||||
/* TESTING */
|
/* TESTING */
|
||||||
/*--------------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------------*/
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <arpa/inet.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 5527
|
#define TEST_ROWS 0 /*66319*/
|
||||||
#define READ_MODE "r"
|
#define READ_MODE "r"
|
||||||
|
|
||||||
int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */
|
int parse_line(FILE* f, record_t r) {
|
||||||
memset(key, 0, sizeof(ipv6_t));
|
int ikey, jkey;
|
||||||
|
unsigned long key[2] = {0,0};
|
||||||
|
struct in6_addr addr;
|
||||||
|
char ipv6_cidr[50];
|
||||||
|
char *separator;
|
||||||
|
|
||||||
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
|
if (fgets(ipv6_cidr, sizeof(ipv6_cidr), f) == NULL) {
|
||||||
&key->s[0], &key->s[1], &key->s[2], &key->s[3],
|
if (!feof(f))
|
||||||
&key->s[4], &key->s[5], &key->s[6], &key->s[7]);
|
printf("error reading\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (feof(f)) return 0; /* ends with newline */
|
ipv6_cidr[strcspn(ipv6_cidr, "\n")] = 0;
|
||||||
while (fgetc(f) != '/');
|
separator = strchr(ipv6_cidr, '/');
|
||||||
|
if (!separator) {
|
||||||
|
printf("invalid CIDR notation\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
fscanf(f, "%d %d", mask, val);
|
*separator++ = '\0';
|
||||||
|
r->_mask = atoi(separator);
|
||||||
|
|
||||||
|
separator = strchr(separator, ' ');
|
||||||
|
r->_rule = atoi(++separator);
|
||||||
|
|
||||||
|
if (r->_mask < 0 || r->_mask > 128) {
|
||||||
|
printf("invalid prefix length\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r->_rule < 0 || r->_rule > 65536) {
|
||||||
|
printf("invalid pop\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inet_pton(AF_INET6, ipv6_cidr, &addr) != 1) {
|
||||||
|
printf("%s invalid address\n", ipv6_cidr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ikey = 0; ikey < 2; ++ikey) {
|
||||||
|
for (jkey = 0; jkey < 8; ++jkey) {
|
||||||
|
key[ikey] = (key[ikey] << 8) | addr.__in6_u.__u6_addr8[jkey + (ikey * 8)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r->_ip[0] = key[0];
|
||||||
|
r->_ip[1] = key[1];
|
||||||
|
r->_valid = VALID_RULE;
|
||||||
return !feof(f);
|
return !feof(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_input(radix_holder_t t) {
|
int load_input(radix_holder_t t) {
|
||||||
int val;
|
struct routing_record r;
|
||||||
int mask;
|
|
||||||
ipv6_t key;
|
|
||||||
FILE* f;
|
FILE* f;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
|
||||||
|
@ -171,68 +218,77 @@ int load_input(radix_holder_t t) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (parse_line(&key, &val, &mask, f)) {
|
while (parse_line(f, &r)) {
|
||||||
cnt += radix_insert(t, key, val, mask);
|
if (radix_insert(t, &r))
|
||||||
|
++cnt;
|
||||||
|
else
|
||||||
|
printf("WARNING: conflicting insert\n");
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_tests(ipv6_t** rips, int** rref) {
|
int load_tests(unsigned long** subnets, int** rref) {
|
||||||
ipv6_t* ips;
|
/*
|
||||||
|
unsigned long* ips;
|
||||||
int* ref;
|
int* ref;
|
||||||
int row;
|
int row;
|
||||||
FILE* f;
|
FILE* f;
|
||||||
|
struct routing_record r;
|
||||||
|
|
||||||
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ips = calloc( TEST_ROWS, sizeof(ipv6_t) );
|
ips = calloc( TEST_ROWS, sizeof(unsigned long) );
|
||||||
ref = calloc( TEST_ROWS, sizeof(int) );
|
ref = calloc( TEST_ROWS, sizeof(int) );
|
||||||
|
|
||||||
for (row = 0; row < TEST_ROWS; ++ row) {
|
for (row = 0; row < TEST_ROWS; ++ row) {
|
||||||
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
|
parse_line(f, &r);
|
||||||
&ips[row].s[0], &ips[row].s[1], &ips[row].s[2], &ips[row].s[3],
|
ips[row] = r._ip;
|
||||||
&ips[row].s[4], &ips[row].s[5], &ips[row].s[6], &ips[row].s[7]);
|
ref[row] = r._rule;
|
||||||
|
|
||||||
|
|
||||||
while (fgetc(f) != ' ');
|
|
||||||
fscanf(f, "%d", &(ref[row]));
|
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
*rips = ips;
|
*subnets = ips;
|
||||||
*rref = ref;
|
*rref = ref;
|
||||||
|
*/
|
||||||
return TEST_ROWS;
|
return TEST_ROWS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fire_tests(radix_holder_t t, ipv6_t* ips, int* ref, int num_tests ) {
|
int fire_tests(radix_holder_t t, unsigned long* subnets, int* ref, int num_tests ) {
|
||||||
|
/*
|
||||||
int row;
|
int row;
|
||||||
int wrong = 0;
|
int wrong = 0;
|
||||||
|
struct routing_record r;
|
||||||
|
|
||||||
for (row = 0; row < TEST_ROWS; ++ row) {
|
for (row = 0; row < TEST_ROWS; ++ row) {
|
||||||
if (radix_search(t, ips[row]) != ref[row]) {
|
r._ip = subnets[row];
|
||||||
printf("ref: %d got: %d @ %d\n", ref[row], radix_search(t, ips[row]), 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;
|
++wrong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(ips);
|
free(subnets);
|
||||||
free(ref);
|
free(ref);
|
||||||
return wrong;
|
return wrong;
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
ipv6_t* ips;
|
unsigned long* subnets;
|
||||||
int* 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(&ips, &ref) );
|
printf("Loaded %d tests\n", load_tests(&subnets, &ref) );
|
||||||
printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) );
|
printf("%d tests wrong\n", fire_tests(t, subnets, ref, TEST_ROWS) );
|
||||||
|
|
||||||
destory_holder(t);
|
destory_holder(t);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -9,19 +9,17 @@
|
||||||
#define INVALID_RULE 0
|
#define INVALID_RULE 0
|
||||||
#define RULE_NOT_FOUND -1
|
#define RULE_NOT_FOUND -1
|
||||||
|
|
||||||
union ipv6 {
|
struct routing_record {
|
||||||
unsigned short s[8];
|
unsigned long long _ip;
|
||||||
unsigned int i[4];
|
unsigned short _rule;
|
||||||
unsigned long l[2];
|
unsigned _ip_end_bit:7;
|
||||||
|
unsigned _rule_valid:1;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct radix_node {
|
struct radix_node {
|
||||||
struct radix_node* _l;
|
struct radix_node* _l;
|
||||||
struct radix_node* _r;
|
struct radix_node* _r;
|
||||||
union ipv6 _ip;
|
struct routing_record _data;
|
||||||
unsigned short _rule;
|
|
||||||
unsigned _ip_end_bit:7;
|
|
||||||
unsigned _rule_valid:1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct radix_holder {
|
struct radix_holder {
|
||||||
|
@ -31,18 +29,17 @@ struct radix_holder {
|
||||||
|
|
||||||
typedef struct radix_node* radix_node_t;
|
typedef struct radix_node* radix_node_t;
|
||||||
typedef struct radix_holder* radix_holder_t;
|
typedef struct radix_holder* radix_holder_t;
|
||||||
typedef union ipv6 ipv6_t;
|
typedef struct routing_record* record_t;
|
||||||
typedef int rule_t;
|
|
||||||
|
|
||||||
struct radix_holder* create_holder();
|
struct radix_holder* create_holder();
|
||||||
struct radix_node* create_node(ipv6_t key, unsigned end_bit, unsigned rule);
|
struct radix_node* create_node(record_t record);
|
||||||
static struct radix_node* clone_node(struct radix_node* n);
|
static struct radix_node* 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*);
|
int destory_node(struct radix_node*);
|
||||||
|
|
||||||
int radix_insert(struct radix_holder*, ipv6_t key, int val, int mask);
|
int radix_insert(radix_holder_t, record_t);
|
||||||
rule_t radix_search(struct radix_holder*, ipv6_t key);
|
int radix_search(radix_holder_t, record_t);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -14,6 +14,7 @@ def generate_random_test_cases(rules, n):
|
||||||
rule_network = rule[0]
|
rule_network = rule[0]
|
||||||
rule_next_hop = rule[1]
|
rule_next_hop = rule[1]
|
||||||
|
|
||||||
|
network = rule_network
|
||||||
for _ in range(n):
|
for _ in range(n):
|
||||||
next_hop = rule_next_hop
|
next_hop = rule_next_hop
|
||||||
address = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
|
address = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
|
||||||
|
@ -23,9 +24,10 @@ def generate_random_test_cases(rules, n):
|
||||||
continue
|
continue
|
||||||
oth_network = oth_rule[0]
|
oth_network = oth_rule[0]
|
||||||
oth_next_hop = oth_rule[1]
|
oth_next_hop = oth_rule[1]
|
||||||
if address in oth_network and oth_network.subnet_of( rule_network ):
|
if address in oth_network and oth_network.subnet_of( network ):
|
||||||
print(f"address {address} is closer to {oth_network} {rule_next_hop} than {rule_network} {oth_next_hop}")
|
print(f"address {address} is closer to {oth_network} {rule_next_hop} than {rule_network} {oth_next_hop}")
|
||||||
next_hop = oth_next_hop
|
next_hop = oth_next_hop
|
||||||
|
network = oth_network
|
||||||
f.write(f"{address} {next_hop}\n")
|
f.write(f"{address} {next_hop}\n")
|
||||||
print(f"Generated test case: {address} {next_hop}")
|
print(f"Generated test case: {address} {next_hop}")
|
||||||
print("tests for rule ", rule_network, " with next hop ", rule_next_hop, " generated")
|
print("tests for rule ", rule_network, " with next hop ", rule_next_hop, " generated")
|
||||||
|
|
Loading…
Reference in New Issue