]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/ll_map.c
rdma: Place PD parsing print routine into separate function
[mirror_iproute2.git] / lib / ll_map.c
index 571d11e1cd6b5ff9b2d993b5feb0997a8000b2c6..2d7b65dcb8f7e3a21b726199eee4cc3719669524 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -30,7 +29,7 @@ struct ll_cache {
        unsigned        flags;
        unsigned        index;
        unsigned short  type;
-       char            name[IFNAMSIZ];
+       char            name[];
 };
 
 #define IDXMAP_SIZE    1024
@@ -78,8 +77,7 @@ static struct ll_cache *ll_get_by_name(const char *name)
        return NULL;
 }
 
-int ll_remember_index(const struct sockaddr_nl *who,
-                     struct nlmsghdr *n, void *arg)
+int ll_remember_index(struct nlmsghdr *n, void *arg)
 {
        unsigned int h;
        const char *ifname;
@@ -90,7 +88,7 @@ int ll_remember_index(const struct sockaddr_nl *who,
        if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
                return 0;
 
-       if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
+       if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
                return -1;
 
        im = ll_get_by_index(ifi->ifi_index);
@@ -120,7 +118,7 @@ int ll_remember_index(const struct sockaddr_nl *who,
                return 0;
        }
 
-       im = malloc(sizeof(*im));
+       im = malloc(sizeof(*im) + strlen(ifname) + 1);
        if (im == NULL)
                return 0;
        im->index = ifi->ifi_index;
@@ -137,8 +135,68 @@ int ll_remember_index(const struct sockaddr_nl *who,
        return 0;
 }
 
-const char *ll_idx_n2a(unsigned idx, char *buf)
+const char *ll_idx_n2a(unsigned int idx)
 {
+       static char buf[IFNAMSIZ];
+
+       snprintf(buf, sizeof(buf), "if%u", idx);
+       return buf;
+}
+
+static unsigned int ll_idx_a2n(const char *name)
+{
+       unsigned int idx;
+
+       if (sscanf(name, "if%u", &idx) != 1)
+               return 0;
+       return idx;
+}
+
+static int ll_link_get(const char *name, int index)
+{
+       struct {
+               struct nlmsghdr         n;
+               struct ifinfomsg        ifm;
+               char                    buf[1024];
+       } req = {
+               .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+               .n.nlmsg_flags = NLM_F_REQUEST,
+               .n.nlmsg_type = RTM_GETLINK,
+               .ifm.ifi_index = index,
+       };
+       __u32 filt_mask = RTEXT_FILTER_VF | RTEXT_FILTER_SKIP_STATS;
+       struct rtnl_handle rth = {};
+       struct nlmsghdr *answer;
+       int rc = 0;
+
+       if (rtnl_open(&rth, 0) < 0)
+               return 0;
+
+       addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask);
+       if (name)
+               addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name,
+                         strlen(name) + 1);
+
+       if (rtnl_talk(&rth, &req.n, &answer) < 0)
+               goto out;
+
+       /* add entry to cache */
+       rc  = ll_remember_index(answer, NULL);
+       if (!rc) {
+               struct ifinfomsg *ifm = NLMSG_DATA(answer);
+
+               rc = ifm->ifi_index;
+       }
+
+       free(answer);
+out:
+       rtnl_close(&rth);
+       return rc;
+}
+
+const char *ll_index_to_name(unsigned int idx)
+{
+       static char buf[IFNAMSIZ];
        const struct ll_cache *im;
 
        if (idx == 0)
@@ -148,19 +206,18 @@ const char *ll_idx_n2a(unsigned idx, char *buf)
        if (im)
                return im->name;
 
+       if (ll_link_get(NULL, idx) == idx) {
+               im = ll_get_by_index(idx);
+               if (im)
+                       return im->name;
+       }
+
        if (if_indextoname(idx, buf) == NULL)
-               snprintf(buf, IFNAMSIZ, "if%d", idx);
+               snprintf(buf, IFNAMSIZ, "if%u", idx);
 
        return buf;
 }
 
-const char *ll_index_to_name(unsigned idx)
-{
-       static char nbuf[IFNAMSIZ];
-
-       return ll_idx_n2a(idx, nbuf);
-}
-
 int ll_index_to_type(unsigned idx)
 {
        const struct ll_cache *im;
@@ -195,12 +252,28 @@ unsigned ll_name_to_index(const char *name)
        if (im)
                return im->index;
 
-       idx = if_nametoindex(name);
+       idx = ll_link_get(name, 0);
        if (idx == 0)
-               sscanf(name, "if%u", &idx);
+               idx = if_nametoindex(name);
+       if (idx == 0)
+               idx = ll_idx_a2n(name);
        return idx;
 }
 
+void ll_drop_by_index(unsigned index)
+{
+       struct ll_cache *im;
+
+       im = ll_get_by_index(index);
+       if (!im)
+               return;
+
+       hlist_del(&im->idx_hash);
+       hlist_del(&im->name_hash);
+
+       free(im);
+}
+
 void ll_init_map(struct rtnl_handle *rth)
 {
        static int initialized;
@@ -208,7 +281,7 @@ void ll_init_map(struct rtnl_handle *rth)
        if (initialized)
                return;
 
-       if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
+       if (rtnl_linkdump_req(rth, AF_UNSPEC) < 0) {
                perror("Cannot send dump request");
                exit(1);
        }