chiark / gitweb /
rtnl: add preliminary support for containers
authorTom Gundersen <teg@jklm.no>
Thu, 21 Nov 2013 19:44:29 +0000 (20:44 +0100)
committerTom Gundersen <teg@jklm.no>
Tue, 26 Nov 2013 00:32:25 +0000 (01:32 +0100)
For now, we only support one container type IFLA_LINKINFO, and we
still lack support for parsing the containers again.

src/libsystemd-rtnl/rtnl-message.c
src/libsystemd-rtnl/test-rtnl.c
src/systemd/sd-rtnl.h

index 26da7f2db88c3b3db5cce254dce4afe2224b276d..800bf2354caa5583b047f8a31086d630fe824613 100644 (file)
@@ -36,6 +36,8 @@ struct sd_rtnl_message {
 
         struct nlmsghdr *hdr;
 
+        struct rtattr *current_container;
+
         struct rtattr *next_rta;
         size_t remaining_size;
 
@@ -131,7 +133,7 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
 
         assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK ||
                       nlmsg_type == RTM_SETLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
-        assert_return(index > 0, -EINVAL);
+        assert_return(nlmsg_type == RTM_NEWLINK || index > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
         r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
@@ -140,6 +142,8 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
 
         (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
         (*ret)->hdr->nlmsg_type = nlmsg_type;
+        if (nlmsg_type == RTM_NEWLINK)
+                (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE;
 
         ifi = NLMSG_DATA((*ret)->hdr);
 
@@ -203,6 +207,21 @@ int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
         return 0;
 }
 
+int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
+        struct ifinfomsg *ifi;
+
+        assert_return(m, -EINVAL);
+        assert_return(ifindex, -EINVAL);
+        assert_return(m->hdr->nlmsg_type == RTM_NEWLINK || m->hdr->nlmsg_type == RTM_DELLINK ||
+                      m->hdr->nlmsg_type == RTM_GETLINK || m->hdr->nlmsg_type == RTM_SETLINK, -EINVAL);
+
+        ifi = NLMSG_DATA(m->hdr);
+
+        *ifindex = ifi->ifi_index;
+
+        return 0;
+}
+
 /* If successful the updated message will be correctly aligned, if unsuccessful the old message is
    untouched */
 static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
@@ -211,11 +230,10 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         struct rtattr *rta;
         char *padding;
 
-        assert_return(m, -EINVAL);
-        assert_return(m->hdr, -EINVAL);
-        assert_return(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len, -EINVAL);
-        assert_return(data, -EINVAL);
-        assert_return(data_length > 0, -EINVAL);
+        assert(m);
+        assert(m->hdr);
+        assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
+        assert(!data || data_length > 0);
 
         /* get the size of the new rta attribute (with padding at the end) */
         rta_length = RTA_LENGTH(data_length);
@@ -234,15 +252,28 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         /* update message size */
         m->hdr->nlmsg_len = message_length;
 
+        /* we are inside a container, extend it */
+        if (m->current_container)
+                m->current_container->rta_len = (unsigned char *) m->hdr +
+                                                m->hdr->nlmsg_len -
+                                                (unsigned char *) m->current_container;
+
         /* fill in the attribute */
         rta->rta_type = type;
         rta->rta_len = rta_length;
-        /* we don't deal with the case where the user lies about the type and gives us
-         * too little data (so don't do that)
-         */
-        padding = mempcpy(RTA_DATA(rta), data, data_length);
-        /* make sure also the padding at the end of the message is initialized */
-        memset(padding, '\0', (unsigned char *) m->hdr + m->hdr->nlmsg_len - (unsigned char *) padding);
+        if (!data) {
+                /* this is a container, set pointer */
+                m->current_container = rta;
+        } else {
+                /* we don't deal with the case where the user lies about the type
+                 * and gives us too little data (so don't do that)
+                */
+                padding = mempcpy(RTA_DATA(rta), data, data_length);
+                /* make sure also the padding at the end of the message is initialized */
+                memset(padding, '\0', (unsigned char *) m->hdr +
+                                      m->hdr->nlmsg_len -
+                                      (unsigned char *) padding);
+        }
 
         return 0;
 }
@@ -257,6 +288,28 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
 
         sd_rtnl_message_get_type(m, &rtm_type);
 
+        if (m->current_container) {
+                switch (rtm_type) {
+                        case RTM_NEWLINK:
+                        case RTM_SETLINK:
+                        case RTM_GETLINK:
+                        case RTM_DELLINK:
+                                switch (m->current_container->rta_type) {
+                                        case IFLA_LINKINFO:
+                                                switch (type) {
+                                                        case IFLA_INFO_KIND:
+                                                                return add_rtattr(m, type, data, strlen(data) + 1);
+                                                        default:
+                                                                return -ENOTSUP;
+                                                }
+                                        default:
+                                                return -ENOTSUP;
+                                }
+                        default:
+                                return -ENOTSUP;
+                }
+        }
+
         switch (rtm_type) {
                 case RTM_NEWLINK:
                 case RTM_SETLINK:
@@ -329,13 +382,67 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
         }
 }
 
-static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
+        uint16_t rtm_type;
+
         assert_return(m, -EINVAL);
-        assert_return(data, -EINVAL);
+        assert_return(!m->current_container, -EINVAL);
+
+        sd_rtnl_message_get_type(m, &rtm_type);
+
+        switch (rtm_type) {
+                case RTM_NEWLINK:
+                case RTM_SETLINK:
+                case RTM_GETLINK:
+                case RTM_DELLINK:
+                        if (type == IFLA_LINKINFO)
+                                return add_rtattr(m, type, NULL, 0);
+                        else
+                                return -ENOTSUP;
+                default:
+                        return -ENOTSUP;
+        }
+
+        return 0;
+}
+
+int sd_rtnl_message_close_container(sd_rtnl_message *m) {
+        assert_return(m, -EINVAL);
+        assert_return(m->current_container, -EINVAL);
+
+        m->current_container = NULL;
+
+        return 0;
+}
+
+static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+        uint16_t rtm_type;
+        int r;
+
+        assert(m);
+        assert(m->next_rta);
+        assert(type);
+        assert(data);
 
         if (!RTA_OK(m->next_rta, m->remaining_size))
                 return 0;
 
+        /* make sure we don't try to read a container
+         * TODO: add support for entering containers for reading */
+        r = sd_rtnl_message_get_type(m, &rtm_type);
+        if (r < 0)
+                return r;
+
+        switch (rtm_type) {
+                case RTM_NEWLINK:
+                case RTM_GETLINK:
+                case RTM_SETLINK:
+                case RTM_DELLINK:
+                        if (m->next_rta->rta_type == IFLA_LINKINFO) {
+                                return -EINVAL;
+                        }
+        }
+
         *data = RTA_DATA(m->next_rta);
         *type = m->next_rta->rta_type;
 
@@ -346,11 +453,14 @@ static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
 
 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
         uint16_t rtm_type;
+        int r;
 
         assert_return(m, -EINVAL);
         assert_return(data, -EINVAL);
 
-        sd_rtnl_message_get_type(m, &rtm_type);
+        r = sd_rtnl_message_get_type(m, &rtm_type);
+        if (r < 0)
+                return r;
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
index 75a210d3fbbca9d816e3dbd6ac79708b3c88b498..4b62c02361ee78b601286b4e6657fb17c87f6975 100644 (file)
@@ -206,6 +206,30 @@ static void test_pipe(int ifindex) {
         }
 }
 
+static void test_container(void) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+        uint16_t type;
+        void *data;
+
+        assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &m) >= 0);
+
+        assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
+        assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL);
+        assert(sd_rtnl_message_append(m, IFLA_INFO_KIND, "kind") >= 0);
+        assert(sd_rtnl_message_close_container(m) >= 0);
+        assert(sd_rtnl_message_close_container(m) == -EINVAL);
+
+        assert(sd_rtnl_message_read(m, &type, &data) == -EINVAL);
+
+/* TODO: add support for entering containers
+        assert(sd_rtnl_message_read(m, &type, &data) > 0);
+        assert(type == IFLA_INFO_KIND);
+        assert(streq("kind", (char *) data));
+
+        assert(sd_rtnl_message_read(m, &type, &data) == 0);
+*/
+}
+
 int main(void) {
         sd_rtnl *rtnl;
         sd_rtnl_message *m;
@@ -220,6 +244,8 @@ int main(void) {
 
         test_route();
 
+        test_container();
+
         assert(sd_rtnl_open(0, &rtnl) >= 0);
         assert(rtnl);
 
index e5fa785a99434694e2bb67cfbf4a17a0358ea15a..6db5d8a248a4561e11f5ba6ada0e76f311f631a3 100644 (file)
@@ -76,7 +76,13 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m);
 
 int sd_rtnl_message_get_errno(sd_rtnl_message *m);
 int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type);
+
+int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex);
+
 int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *data);
+int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type);
+int sd_rtnl_message_close_container(sd_rtnl_message *m);
+
 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data);
 
 _SD_END_DECLARATIONS;