From: Thomas Graf Date: Mon, 5 May 2008 15:09:25 +0000 (+0200) Subject: Route cache support X-Git-Tag: libnl2_0~110 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85808860b6174aecc901c34c90968911ad013280;p=libnl 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. --- diff --git a/include/netlink/route/route.h b/include/netlink/route/route.h index 53f47be..b7bf155 100644 --- a/include/netlink/route/route.h +++ b/include/netlink/route/route.h @@ -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 *); diff --git a/lib/route/route.c b/lib/route/route.c index 9edaddc..260d3e2 100644 --- a/lib/route/route.c +++ b/lib/route/route.c @@ -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; diff --git a/src/nl-route-add.c b/src/nl-route-add.c index 4895747..f411b30 100644 --- a/src/nl-route-add.c +++ b/src/nl-route-add.c @@ -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) diff --git a/src/nl-route-delete.c b/src/nl-route-delete.c index e0e2c1c..78403f6 100644 --- a/src/nl-route-delete.c +++ b/src/nl-route-delete.c @@ -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) diff --git a/src/nl-route-list.c b/src/nl-route-list.c index 78222d2..9999a02 100644 --- a/src/nl-route-list.c +++ b/src/nl-route-list.c @@ -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, ¶ms, 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); diff --git a/src/utils.c b/src/utils.c index 9c34172..8961219 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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()); diff --git a/src/utils.h b/src/utils.h index 1e0bcb4..c400206 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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);