]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
ip vrf: Add name_is_vrf
authorDavid Ahern <dsa@cumulusnetworks.com>
Wed, 29 Jun 2016 18:26:57 +0000 (11:26 -0700)
committerStephen Hemminger <shemming@brocade.com>
Thu, 7 Jul 2016 04:28:31 +0000 (21:28 -0700)
Add name_is_vrf function to determine if given name corresponds to a
VRF device.

Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
ip/ip_common.h
ip/iplink_vrf.c

index e8da9e034b15d46cc382cec8b4515680d393b9c7..b747ea47122d3849e77da4d6c303a58c40b655da 100644 (file)
@@ -90,6 +90,8 @@ struct link_util *get_link_slave_kind(const char *slave_kind);
 
 void br_dump_bridge_id(const struct ifla_bridge_id *id, char *buf, size_t len);
 
+bool name_is_vrf(const char *name);
+
 #ifndef        INFINITY_LIFE_TIME
 #define     INFINITY_LIFE_TIME      0xFFFFFFFFU
 #endif
index e3c7b4652da52202875140695b2b93f392f89b59..166f566e6d77f90da3f0ee7659b8602bc8a539a9 100644 (file)
@@ -96,3 +96,53 @@ struct link_util vrf_slave_link_util = {
        .print_opt      = vrf_slave_print_opt,
        .slave          = true,
 };
+
+bool name_is_vrf(const char *name)
+{
+       struct {
+               struct nlmsghdr         n;
+               struct ifinfomsg        i;
+               char                    buf[1024];
+       } req = {
+               .n = {
+                       .nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+                       .nlmsg_flags = NLM_F_REQUEST,
+                       .nlmsg_type  = RTM_GETLINK,
+               },
+               .i = {
+                       .ifi_family  = preferred_family,
+               },
+       };
+       struct {
+               struct nlmsghdr n;
+               char buf[8192];
+       } answer;
+       struct rtattr *tb[IFLA_MAX+1];
+       struct rtattr *li[IFLA_INFO_MAX+1];
+       struct ifinfomsg *ifi;
+       int len;
+
+       addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
+
+       if (rtnl_talk(&rth, &req.n, &answer.n, sizeof(answer)) < 0)
+               return false;
+
+       ifi = NLMSG_DATA(&answer.n);
+       len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
+       if (len < 0) {
+               fprintf(stderr, "BUG: Invalid response to link query.\n");
+               return false;
+       }
+
+       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
+
+       if (!tb[IFLA_LINKINFO])
+               return false;
+
+       parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
+
+       if (!li[IFLA_INFO_KIND])
+               return false;
+
+       return strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf") == 0;
+}