]> granicus.if.org Git - libnl/commitdiff
Route cache support
authorThomas Graf <tgr@lsx.localdomain>
Mon, 5 May 2008 15:09:25 +0000 (17:09 +0200)
committerThomas Graf <tgr@lsx.localdomain>
Mon, 5 May 2008 15:09:25 +0000 (17:09 +0200)
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.

include/netlink/route/route.h
lib/route/route.c
src/nl-route-add.c
src/nl-route-delete.c
src/nl-route-list.c
src/utils.c
src/utils.h

index 53f47bea3200781d451a592bd28d25d10829a2d5..b7bf15528aded77eeb4ef6d2aa09a4e7ef16a44a 100644 (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 *);
index 9edaddcb34eae2249b80e7e2bdbb1827d48ce2be..260d3e282ae1d03972277b7896ba3a94fb2eef6f 100644 (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;
index 489574704c4ea7f7df233fb56b5c17ba5111c237..f411b30b46e2a767910e70803440346b6e9cdd27 100644 (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)
index e0e2c1c4dd294f59b0440ddd6f7653044d5e6daf..78403f6140a7967fb6adef56819df9da8a190927 100644 (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)
index 78222d2f0fce3c8c05a1254ec49262aa9afdfc4e..9999a021eb57e428d03116ed287daf37c3ca16f9 100644 (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);
index 9c3417248523f991a8bfd5b7cac53d395aeb93bd..8961219662eb6eaf3cecb39feb83ddcd04887eab 100644 (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());
index 1e0bcb46c771380b622f274bdcda0180529e3ee6..c4002061ab4170c748092df27d8c9e89c92e78d0 100644 (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);