Route cache support

This changesets adds the possibility to fill a nl_cache with
the contents of the route cache. It also adds the possibility
to limit route caches to certain address families.
This commit is contained in:
Thomas Graf 2008-05-05 17:09:25 +02:00
parent 861901c55b
commit 85808860b6
7 changed files with 34 additions and 12 deletions

View file

@ -23,6 +23,9 @@
extern "C" {
#endif
/* flags */
#define ROUTE_CACHE_CONTENT 1
struct rtnl_route;
struct rtnl_rtcacheinfo
@ -42,7 +45,8 @@ extern struct nl_object_ops route_obj_ops;
/* General */
extern struct rtnl_route * rtnl_route_alloc(void);
extern void rtnl_route_put(struct rtnl_route *);
extern struct nl_cache * rtnl_route_alloc_cache(struct nl_handle *);
extern struct nl_cache * rtnl_route_alloc_cache(struct nl_handle *,
int, int);
extern void rtnl_route_get(struct rtnl_route *);
extern void rtnl_route_put(struct rtnl_route *);

View file

@ -48,7 +48,14 @@ errout:
static int route_request_update(struct nl_cache *c, struct nl_handle *h)
{
return nl_rtgen_request(h, RTM_GETROUTE, AF_UNSPEC, NLM_F_DUMP);
struct rtmsg rhdr = {
.rtm_family = c->c_iarg1,
};
if (c->c_iarg2 & ROUTE_CACHE_CONTENT)
rhdr.rtm_flags |= RTM_F_CLONED;
return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr, sizeof(rhdr));
}
/**
@ -59,6 +66,8 @@ static int route_request_update(struct nl_cache *c, struct nl_handle *h)
/**
* Build a route cache holding all routes currently configured in the kernel
* @arg handle netlink handle
* @arg family Address family of routes to cover or AF_UNSPEC
* @arg flags Flags
*
* Allocates a new cache, initializes it properly and updates it to
* contain all routes currently configured in the kernel.
@ -67,7 +76,8 @@ static int route_request_update(struct nl_cache *c, struct nl_handle *h)
* cache after using it.
* @return The cache or NULL if an error has occured.
*/
struct nl_cache *rtnl_route_alloc_cache(struct nl_handle *handle)
struct nl_cache *rtnl_route_alloc_cache(struct nl_handle *handle,
int family, int flags)
{
struct nl_cache *cache;
@ -75,6 +85,9 @@ struct nl_cache *rtnl_route_alloc_cache(struct nl_handle *handle)
if (!cache)
return NULL;
cache->c_iarg1 = family;
cache->c_iarg2 = flags;
if (handle && nl_cache_refill(handle, cache) < 0) {
free(cache);
return NULL;

View file

@ -66,7 +66,7 @@ int main(int argc, char *argv[])
nlh = nltool_alloc_handle();
nltool_connect(nlh, NETLINK_ROUTE);
link_cache = nltool_alloc_link_cache(nlh);
route_cache = nltool_alloc_route_cache(nlh);
route_cache = nltool_alloc_route_cache(nlh, 0);
route = rtnl_route_alloc();
if (!route)

View file

@ -103,7 +103,7 @@ int main(int argc, char *argv[])
nlh = nltool_alloc_handle();
nltool_connect(nlh, NETLINK_ROUTE);
link_cache = nltool_alloc_link_cache(nlh);
route_cache = nltool_alloc_route_cache(nlh);
route_cache = nltool_alloc_route_cache(nlh, 0);
route = rtnl_route_alloc();
if (!route)

View file

@ -23,6 +23,7 @@ static void print_usage(void)
"Usage: nl-route-list [OPTION]... [ROUTE]\n"
"\n"
"Options\n"
" -c, --cache List the contents of the route cache\n"
" -f, --format=TYPE Output format { brief | details | stats }\n"
" -h, --help Show this help\n"
" -v, --version Show versioning information\n"
@ -59,12 +60,11 @@ int main(int argc, char *argv[])
.dp_fd = stdout,
.dp_type = NL_DUMP_BRIEF
};
int err = 1;
int err = 1, print_cache = 0;
nlh = nltool_alloc_handle();
nltool_connect(nlh, NETLINK_ROUTE);
link_cache = nltool_alloc_link_cache(nlh);
route_cache = nltool_alloc_route_cache(nlh);
route = rtnl_route_alloc();
if (!route)
@ -84,6 +84,7 @@ int main(int argc, char *argv[])
ARG_TYPE,
};
static struct option long_opts[] = {
{ "cache", 0, 0, 'c' },
{ "format", 1, 0, 'f' },
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
@ -102,11 +103,12 @@ int main(int argc, char *argv[])
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "f:hvd:n:t:", long_opts, &optidx);
c = getopt_long(argc, argv, "cf:hvd:n:t:", long_opts, &optidx);
if (c == -1)
break;
switch (c) {
case 'c': print_cache = 1; break;
case 'f': params.dp_type = nltool_parse_dumptype(optarg); break;
case 'h': print_usage(); break;
case 'v': print_version(); break;
@ -125,13 +127,16 @@ int main(int argc, char *argv[])
}
}
route_cache = nltool_alloc_route_cache(nlh,
print_cache ? ROUTE_CACHE_CONTENT : 0);
nl_cache_dump_filter(route_cache, &params, OBJ_CAST(route));
err = 0;
rtnl_route_put(route);
errout:
nl_cache_free(route_cache);
errout:
nl_cache_free(link_cache);
nl_close(nlh);
nl_handle_destroy(nlh);

View file

@ -141,11 +141,11 @@ struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh)
return cache;
}
struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh, int flags)
{
struct nl_cache *cache;
cache = rtnl_route_alloc_cache(nlh);
cache = rtnl_route_alloc_cache(nlh, AF_UNSPEC, flags);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve route cache: %s\n",
nl_geterror());

View file

@ -56,7 +56,7 @@ extern struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh, int);
extern struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh);
extern struct nl_cache *nltool_alloc_genl_family_cache(struct nl_handle *nlh);