Skip to content

Commit

Permalink
issue #88 , made the changes to display the entire "BIB" database, an…
Browse files Browse the repository at this point in the history
…d fix the previous commit due to an error on user application, that didn't display the entire Session Database.
  • Loading branch information
dhfelix committed May 23, 2014
1 parent 547979e commit 5b3febf
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/nat64/comm/config_proto.h
Expand Up @@ -203,7 +203,8 @@ struct request_bib {
l4_protocol l4_proto;
union {
struct {
/* Nothing needed here. */
bool iterate;
struct ipv4_tuple_address ipv4;
} display;
struct {
/* Nothing needed here. */
Expand Down
2 changes: 2 additions & 0 deletions include/nat64/mod/bib_db.h
Expand Up @@ -200,6 +200,8 @@ int bibdb_remove(struct bib_entry *entry, bool lock);
* @param arg something you want to send func for every entry.
*/
int bibdb_for_each(l4_protocol l4_proto, int (*func)(struct bib_entry *, void *), void *arg);
int bibdb_iterate_by_ipv4(l4_protocol l4_proto, struct ipv4_tuple_address *ipv4,
bool iterate, int (*func)(struct bib_entry *, void *), void *arg);
/**
* Sets in the value pointed by "result" the number of entries in the table whose protocol is
* "l4_proto".
Expand Down
55 changes: 55 additions & 0 deletions mod/bib_db.c
Expand Up @@ -478,6 +478,61 @@ int bibdb_for_each(l4_protocol l4_proto, int (*func)(struct bib_entry *, void *)
return error;
}

static struct rb_node *find_best_node(struct bib_table *table, struct ipv4_tuple_address *ipv4, bool iterate)
{
struct rb_node **node, *parent;
struct bib_entry *bib;
int error;
int gap;

if (!iterate) {
return rb_first(&table->tree4);
}

if (!ipv4) {
return NULL;
}

error = rbtree_find_node(ipv4, &table->tree4, compare_full4, struct bib_entry,
tree4_hook, parent, node);
if (*node) {
return rb_next(*node);
}
bib = rb_entry(parent, struct bib_entry, tree4_hook);
gap = compare_full4(bib, ipv4);
if (gap < 0)
return parent;

return rb_next(parent);
}

/** TODO: look for an appropiate name for this function*/
/**
* Iterate through the session table, starts next ipv4_tuple_address
*/
int bibdb_iterate_by_ipv4(l4_protocol l4_proto, struct ipv4_tuple_address *ipv4,
bool iterate, int (*func)(struct bib_entry *, void *), void *arg)
{
struct bib_table *table;
struct rb_node *node;
int error;

error = get_bibdb_table(l4_proto, &table);
if (error)
return error;

spin_lock_bh(&table->lock);
for (node = find_best_node(table, ipv4, iterate); node; node = rb_next(node)) {
error = func(rb_entry(node, struct bib_entry, tree4_hook), arg);
if (error)
goto spin_exit;
}

spin_exit:
spin_unlock_bh(&table->lock);
return error;
}

int bibdb_count(l4_protocol proto, u64 *result)
{
struct bib_table *table;
Expand Down
9 changes: 7 additions & 2 deletions mod/config.c
Expand Up @@ -250,8 +250,13 @@ static int handle_bib_config(struct nlmsghdr *nl_hdr, struct request_hdr *nat64_
}

stream_init(stream, nl_socket, nl_hdr);
error = bibdb_for_each(request->l4_proto, bib_entry_to_userspace, stream);
stream_close(stream);
error = bibdb_iterate_by_ipv4(request->l4_proto, &request->display.ipv4,
request->display.iterate, bib_entry_to_userspace, stream);
if (error > 0) {
error = stream_close_continue(stream);
} else {
error = stream_close(stream);
}

kfree(stream);
return error;
Expand Down
8 changes: 5 additions & 3 deletions mod/session_db.c
Expand Up @@ -677,6 +677,11 @@ static struct rb_node *find_best_node(struct session_table *table, struct ipv4_t
if (!iterate) {
return rb_first(&table->tree4);
}

if (!ipv4) {
return NULL;
}

error = rbtree_find_node(ipv4, &table->tree4, compare_local4, struct session_entry,
tree4_hook, parent, node);
if (*node) {
Expand All @@ -686,8 +691,6 @@ static struct rb_node *find_best_node(struct session_table *table, struct ipv4_t
gap = compare_local4(session, ipv4);
if (gap < 0)
return parent;
else if (gap > 0)
return rb_next(parent);

return rb_next(parent);
}
Expand All @@ -702,7 +705,6 @@ int sessiondb_iterate_by_ipv4(l4_protocol l4_proto, struct ipv4_tuple_address *i
struct session_table *table;
struct rb_node *node;
int error;
int i = 1;

error = get_session_table(l4_proto, &table);
if (error)
Expand Down
18 changes: 17 additions & 1 deletion usr/src/bib.c
Expand Up @@ -13,6 +13,7 @@
struct display_params {
bool numeric_hostname;
int row_count;
struct request_bib *req_payload;
};

static int bib_display_response(struct nl_msg *msg, void *arg)
Expand All @@ -35,6 +36,14 @@ static int bib_display_response(struct nl_msg *msg, void *arg)
}

params->row_count += entry_count;

if (hdr->nlmsg_flags == NLM_F_MULTI) {
params->req_payload->display.iterate = true;
params->req_payload->display.ipv4.address = *(&entries[entry_count - 1].ipv4.address);
params->req_payload->display.ipv4.l4_id = *(&entries[entry_count - 1].ipv4.l4_id);
} else {
params->req_payload->display.iterate = false;
}
return 0;
}

Expand All @@ -52,11 +61,18 @@ static bool display_single_table(char *table_name, l4_protocol l4_proto, bool nu
hdr->mode = MODE_BIB;
hdr->operation = OP_DISPLAY;
payload->l4_proto = l4_proto;
payload->display.iterate = false;

params.numeric_hostname = numeric_hostname;
params.row_count = 0;
params.req_payload = payload;

do {
error = netlink_request(request, hdr->length, bib_display_response, &params);
if (error)
break;
} while (params.req_payload->display.iterate);

error = netlink_request(request, hdr->length, bib_display_response, &params);
if (!error) {
if (params.row_count > 0)
printf(" (Fetched %u entries.)\n", params.row_count);
Expand Down
18 changes: 7 additions & 11 deletions usr/src/session.c
Expand Up @@ -14,7 +14,7 @@
struct display_params {
bool numeric_hostname;
int row_count;
struct request_session req_payload;
struct request_session *req_payload;
};

static int session_display_response(struct nl_msg *msg, void *arg)
Expand Down Expand Up @@ -54,11 +54,11 @@ static int session_display_response(struct nl_msg *msg, void *arg)
params->row_count += entry_count;

if (hdr->nlmsg_flags == NLM_F_MULTI) {
params->req_payload.iterate = true;
params->req_payload.ipv4.address = *(&entries[entry_count - 1].ipv4.local.address);
params->req_payload.ipv4.l4_id = *(&entries[entry_count - 1].ipv4.local.l4_id);
params->req_payload->iterate = true;
params->req_payload->ipv4.address = *(&entries[entry_count - 1].ipv4.local.address);
params->req_payload->ipv4.l4_id = *(&entries[entry_count - 1].ipv4.local.l4_id);
} else {
params->req_payload.iterate = false;
params->req_payload->iterate = false;
}

return 0;
Expand All @@ -83,17 +83,13 @@ static bool display_single_table(char *table_name, u_int8_t l4_proto, bool numer

params.numeric_hostname = numeric_hostname;
params.row_count = 0;
params.req_payload = payload;

do {
error = netlink_request(request, hdr->length, session_display_response, &params);
if (error)
break;
if (payload->iterate) {
payload->iterate = params.req_payload.iterate;
payload->ipv4.address = params.req_payload.ipv4.address;
payload->ipv4.l4_id = params.req_payload.ipv4.l4_id;
}
} while (payload->iterate);
} while (params.req_payload->iterate);

if (!error) {
if (params.row_count > 0)
Expand Down

0 comments on commit 5b3febf

Please sign in to comment.