chiark / gitweb /
sd-rtnl:introduce table-based lookup and typesafe read() functions
authorSusant Sahani <susant@redhat.com>
Wed, 5 Mar 2014 15:13:25 +0000 (20:43 +0530)
committerTom Gundersen <teg@jklm.no>
Thu, 6 Mar 2014 18:30:09 +0000 (19:30 +0100)
This patch introduces new netlink attribute parsing logic
which is table based lookup and sd_rtnl_message_read_*
methods for reading attributes. By doing this user does not
have to loop for the attribute values . Only providing the
attribute type it gets the attribute values which is optimized
and sd_rtnl_message_read_* methods are simplified.

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

index 0667be5..0c17d94 100644 (file)
@@ -94,7 +94,8 @@ struct sd_rtnl_message {
         size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
         unsigned n_containers; /* number of containers */
         size_t next_rta_offset; /* offset from hdr to next rta */
-
+        size_t *rta_offset_tb;
+        unsigned short rta_tb_size;
         bool sealed:1;
 };
 
@@ -103,6 +104,14 @@ int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size);
 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
 int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret);
 
+int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data);
+int rtnl_message_parse(sd_rtnl_message *m,
+                       size_t **rta_offset_tb,
+                       unsigned short *rta_tb_size,
+                       int max,
+                       struct rtattr *rta,
+                       unsigned int rt_len);
+
 /* Make sure callbacks don't destroy the rtnl connection */
 #define RTNL_DONT_DESTROY(rtnl) \
         _cleanup_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl)
index 41a6e4e..dd3a8fb 100644 (file)
@@ -280,6 +280,7 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
         if (m && REFCNT_DEC(m->n_ref) <= 0) {
                 sd_rtnl_unref(m->rtnl);
                 free(m->hdr);
+                free(m->rta_offset_tb);
                 free(m);
         }
 
@@ -800,6 +801,81 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
         return 1;
 }
 
+int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data) {
+        assert_return(m, -EINVAL);
+        assert_return(m->sealed, -EPERM);
+        assert_return(data, -EINVAL);
+        assert_return(m->rta_offset_tb, -EINVAL);
+        assert_return(type < m->rta_tb_size, -EINVAL);
+
+        if(!m->rta_offset_tb[type])
+                return -ENODATA;
+
+        *data = RTA_DATA((struct rtattr *)((uint8_t *) m->hdr + m->rta_offset_tb[type]));
+
+        return 0;
+}
+
+int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **data) {
+        int r;
+        void *attr_data;
+
+        assert_return(data, -EINVAL);
+
+        r = rtnl_message_read_internal(m, type, &attr_data);
+        if(r < 0)
+                return r;
+
+        *data = (char *) attr_data;
+
+        return 0;
+}
+
+int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data) {
+        int r;
+        void *attr_data;
+
+        assert_return(data, -EINVAL);
+
+        r = rtnl_message_read_internal(m, type, &attr_data);
+        if(r < 0)
+                return r;
+
+        *data = *(uint8_t *) attr_data;
+
+        return 0;
+}
+
+int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data) {
+        int r;
+        void *attr_data;
+
+        assert_return(data, -EINVAL);
+
+        r = rtnl_message_read_internal(m, type, &attr_data);
+        if(r < 0)
+                return r;
+
+        *data = *(uint16_t *) attr_data;
+
+        return 0;
+}
+
+int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data) {
+        int r;
+        void *attr_data;
+
+        assert_return(data, -EINVAL);
+
+        r = rtnl_message_read_internal(m, type, &attr_data);
+        if(r < 0)
+                return r;
+
+        *data = *(uint32_t *) attr_data;
+
+        return 0;
+}
+
 int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
         assert_return(m, -EINVAL);
         assert_return(m->sealed, -EINVAL);
@@ -872,6 +948,33 @@ static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
         return 0;
 }
 
+int rtnl_message_parse(sd_rtnl_message *m,
+                       size_t **rta_offset_tb,
+                       unsigned short *rta_tb_size,
+                       int max,
+                       struct rtattr *rta,
+                       unsigned int rt_len) {
+        int type;
+        size_t *tb;
+
+        tb = (size_t *) new0(size_t *, max);
+        if(!tb)
+                return -ENOMEM;
+
+        *rta_tb_size = max;
+
+        for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
+                type = rta->rta_type;
+
+                if (type < max && !tb[type])
+                        tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr;
+        }
+
+        *rta_offset_tb = tb;
+
+        return 0;
+}
+
 /* returns the number of bytes sent, or a negative error code */
 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
         union {
@@ -961,6 +1064,14 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
 
                                         ifi = NLMSG_DATA(m->hdr);
                                         UPDATE_RTA(m, IFLA_RTA(ifi));
+
+                                        r = rtnl_message_parse(m,
+                                                               &m->rta_offset_tb,
+                                                               &m->rta_tb_size,
+                                                               IFLA_MAX,
+                                                               IFLA_RTA(ifi),
+                                                               IFLA_PAYLOAD(m->hdr));
+
                                 }
                                 break;
                         case RTM_NEWADDR:
@@ -973,6 +1084,13 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
 
                                         ifa = NLMSG_DATA(m->hdr);
                                         UPDATE_RTA(m, IFA_RTA(ifa));
+
+                                        r = rtnl_message_parse(m,
+                                                               &m->rta_offset_tb,
+                                                               &m->rta_tb_size,
+                                                               IFA_MAX,
+                                                               IFA_RTA(ifa),
+                                                               IFA_PAYLOAD(m->hdr));
                                 }
                                 break;
                         case RTM_NEWROUTE:
@@ -985,6 +1103,13 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
 
                                         rtm = NLMSG_DATA(m->hdr);
                                         UPDATE_RTA(m, RTM_RTA(rtm));
+
+                                        r = rtnl_message_parse(m,
+                                                               &m->rta_offset_tb,
+                                                               &m->rta_tb_size,
+                                                               RTA_MAX,
+                                                               RTM_RTA(rtm),
+                                                               RTM_PAYLOAD(m->hdr));
                                 }
                                 break;
                         case NLMSG_NOOP:
index 1016bb6..dc21042 100644 (file)
@@ -28,6 +28,7 @@
 #include "rtnl-util.h"
 #include "event-util.h"
 #include "missing.h"
+#include "rtnl-internal.h"
 
 static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *message;
@@ -63,7 +64,10 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
         unsigned int mtu = 1500;
         unsigned int *mtu_reply;
         void *data;
+        char *str_data;
         uint16_t type;
+        uint8_t u8_data;
+        uint32_t u32_data;
 
         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
         assert_se(m);
@@ -123,9 +127,21 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
                 }
         }
 
+        assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
+
+        assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
+        assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
+        assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
+
+        assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
+        assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
+        assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
+        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
+        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
+
         assert_se(sd_rtnl_flush(rtnl) >= 0);
         assert_se((m = sd_rtnl_message_unref(m)) == NULL);
-
+        assert_se((r = sd_rtnl_message_unref(r)) == NULL);
 }
 
 static void test_route(void) {
@@ -134,7 +150,9 @@ static void test_route(void) {
         uint32_t index = 2;
         uint16_t type;
         void *data;
+        uint32_t u32_data;
         int r;
+        struct rtmsg *rtm;
 
         r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET);
         if (r < 0) {
@@ -165,6 +183,19 @@ static void test_route(void) {
         assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
         assert_se(type == RTA_OIF);
         assert_se(*(uint32_t *) data == index);
+
+        rtm = NLMSG_DATA(req->hdr);
+        r = rtnl_message_parse(req,
+                               &req->rta_offset_tb,
+                               &req->rta_tb_size,
+                               RTA_MAX,
+                               RTM_RTA(rtm),
+                               RTM_PAYLOAD(req->hdr));
+
+        assert_se(sd_rtnl_message_read_u32(req, RTA_GATEWAY, &u32_data) == 0);
+        assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) == 0);
+
+        assert_se((req = sd_rtnl_message_unref(req)) == NULL);
 }
 
 static void test_multiple(void) {
@@ -226,6 +257,8 @@ static void test_event_loop(int ifindex) {
         assert_se(sd_event_run(event, 0) >= 0);
 
         assert_se(sd_rtnl_detach_event(rtnl) >= 0);
+
+        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
 }
 
 static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
@@ -255,6 +288,8 @@ static void test_async(int ifindex) {
 
         assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
         assert_se(sd_rtnl_process(rtnl, &r) >= 0);
+
+        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
 }
 
 static void test_pipe(int ifindex) {
@@ -277,12 +312,17 @@ static void test_pipe(int ifindex) {
                 assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
                 assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
         }
+
+        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
 }
 
 static void test_container(void) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
         uint16_t type;
+        uint32_t u32_data;
         void *data;
+        int r;
+        struct ifinfomsg *ifi;
 
         assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
 
@@ -320,6 +360,19 @@ static void test_container(void) {
         assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
         assert_se(sd_rtnl_message_exit_container(m) >= 0);
 */
+
+        ifi = NLMSG_DATA(m->hdr);
+        r = rtnl_message_parse(m,
+                               &m->rta_offset_tb,
+                               &m->rta_tb_size,
+                               IFLA_MAX,
+                               IFLA_RTA(ifi),
+                               IFLA_PAYLOAD(m->hdr));
+        if(r < 0)
+                return;
+
+        assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0);
+
         assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
 }
 
@@ -334,6 +387,8 @@ static void test_match(void) {
         assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
         assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
         assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
+
+        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
 }
 
 int main(void) {
index d56c14e..886a66e 100644 (file)
@@ -103,6 +103,10 @@ 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);
+int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **data);
+int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data);
+int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data);
+int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data);
 int sd_rtnl_message_exit_container(sd_rtnl_message *m);
 
 int sd_rtnl_message_rewind(sd_rtnl_message *m);