chiark / gitweb /
rtnl: simplify link_new()
authorTom Gundersen <teg@jklm.no>
Fri, 6 Dec 2013 17:16:16 +0000 (18:16 +0100)
committerTom Gundersen <teg@jklm.no>
Mon, 16 Dec 2013 16:28:18 +0000 (17:28 +0100)
Drop most of the arguments and instead introduce link_set_{flags,type}.

src/core/loopback-setup.c
src/libsystemd-rtnl/rtnl-message.c
src/libsystemd-rtnl/rtnl-util.c
src/libsystemd-rtnl/test-rtnl.c
src/network/networkd-bridge.c
src/network/networkd-link.c
src/systemd/sd-rtnl.h

index 5172a6984b83fda3a654599b0287a8ff9cc91b6b..7bb20ecabe0395c59f1795c98b4b43ac2817ff4d 100644 (file)
@@ -89,7 +89,11 @@ static int start_interface(sd_rtnl *rtnl, int if_loopback, uint32_t ipv4_address
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, if_loopback, 0, IFF_UP, &req);
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, if_loopback, &req);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_message_link_set_flags(req, IFF_UP);
         if (r < 0)
                 return r;
 
index 870fdcdf2e4b0f2174c1344dcfcd5fce1d4bfdbd..8a3aa63baf836984571a2d8e867e57200dc5347d 100644 (file)
@@ -160,7 +160,27 @@ int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
         return 0;
 }
 
-int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned type, unsigned flags, sd_rtnl_message **ret) {
+int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags) {
+        struct ifinfomsg *ifi;
+
+        ifi = NLMSG_DATA(m->hdr);
+
+        ifi->ifi_flags = flags;
+
+        return 0;
+}
+
+int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
+        struct ifinfomsg *ifi;
+
+        ifi = NLMSG_DATA(m->hdr);
+
+        ifi->ifi_type = type;
+
+        return 0;
+}
+
+int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
         struct ifinfomsg *ifi;
         int r;
 
@@ -181,8 +201,6 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned type, unsi
 
         ifi->ifi_family = AF_UNSPEC;
         ifi->ifi_index = index;
-        ifi->ifi_type = type;
-        ifi->ifi_flags = flags;
         ifi->ifi_change = 0xffffffff;
 
         return 0;
index 4e7661bb7775a1185d8c7eb31e108f4793bc83d4..264b72ec3765ff69bd3bb0edf52a5e7bc5b9c888 100644 (file)
@@ -34,7 +34,7 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         assert(ifindex > 0);
         assert(name);
 
-        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, 0, 0, &message);
+        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
         if (r < 0)
                 return r;
 
@@ -61,7 +61,7 @@ int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
         if (!alias && !mac && mtu == 0)
                 return 0;
 
-        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, 0, 0, &message);
+        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
         if (r < 0)
                 return r;
 
index 6c34a8537f6b7b40437989723a03b5888a608566..409a0fa7b25e9dc318ca07547fea5cca36792896 100644 (file)
@@ -37,7 +37,7 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         void *data;
 
         /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &message) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &message) >= 0);
         assert(sd_rtnl_message_append(message, IFLA_IFNAME, name) >= 0);
         assert(sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
         assert(sd_rtnl_message_append(message, IFLA_MTU, &mtu) >= 0);
@@ -142,7 +142,7 @@ static void test_event_loop(int ifindex) {
         assert(ifname);
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &m) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m) >= 0);
 
         assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
 
@@ -176,7 +176,7 @@ static void test_async(int ifindex) {
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
 
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &m) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m) >= 0);
 
         assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
 
@@ -191,8 +191,8 @@ static void test_pipe(int ifindex) {
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
 
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &m1) >= 0);
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &m2) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m1) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m2) >= 0);
 
         counter ++;
         assert(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
@@ -211,7 +211,7 @@ static void test_container(void) {
         uint16_t type;
         void *data;
 
-        assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &m) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, &m) >= 0);
 
         assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
         assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL);
@@ -275,7 +275,7 @@ int main(void) {
 
         test_link_configure(rtnl, if_loopback);
 
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, &m) >= 0);
         assert(m);
 
         assert(sd_rtnl_message_get_type(m, &type) >= 0);
@@ -294,7 +294,7 @@ int main(void) {
         assert((m = sd_rtnl_message_unref(m)) == NULL);
         assert((r = sd_rtnl_message_unref(r)) == NULL);
 
-        assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
+        assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, &m) >= 0);
         assert(m);
 
         assert(sd_rtnl_message_append(m, IFLA_MTU, &mtu) >= 0);
index 1704f65ee679c89caa6528fec39b652bde13a1d6..b764b7d5939d6040e05a2a1a77c64a9a245ac6e8 100644 (file)
@@ -84,7 +84,7 @@ static int bridge_join_ready(Bridge *bridge, Link* link, sd_rtnl_message_handler
         assert(link);
         assert(callback);
 
-        r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, 0, 0, &req);
+        r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_SETLINK message: %s",
                           strerror(-r));
@@ -155,7 +155,7 @@ static int bridge_create(Bridge *bridge) {
         assert(bridge->manager);
         assert(bridge->manager->rtnl);
 
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &req);
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, 0, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_NEWLINK message: %s",
                           strerror(-r));
index 8c7b0fc1eeb8564008ddb782323fb0110c3e7e56..16255f9227aa7aab0abded0de5f67b1a5bceeff0 100644 (file)
@@ -264,7 +264,7 @@ static int link_get(Link *link) {
         assert(link->manager);
         assert(link->manager->rtnl);
 
-        r = sd_rtnl_message_link_new(RTM_GETLINK, link->ifindex, 0, 0, &req);
+        r = sd_rtnl_message_link_new(RTM_GETLINK, link->ifindex, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_GETLINK message");
                 return r;
@@ -301,12 +301,18 @@ static int link_up(Link *link) {
         assert(link->manager);
         assert(link->manager->rtnl);
 
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, link->ifindex, 0, IFF_UP, &req);
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, link->ifindex, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_NEWLINK message");
                 return r;
         }
 
+        r = sd_rtnl_message_link_set_flags(req, IFF_UP);
+        if (r < 0) {
+                log_error("Could not set link flags");
+                return r;
+        }
+
         r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL);
         if (r < 0) {
                 log_error("Could not send rtnetlink message: %s", strerror(-r));
index 0a63b28a19e264499064bd9a1632e3ec8b536eaa..066d778ce1672243856e85a1b59bf37c7b3c6f33 100644 (file)
@@ -64,8 +64,7 @@ int sd_rtnl_attach_event(sd_rtnl *nl, sd_event *e, int priority);
 int sd_rtnl_detach_event(sd_rtnl *nl);
 
 /* messages */
-int sd_rtnl_message_link_new(uint16_t msg_type, int index, unsigned int type,
-                             unsigned int flags, sd_rtnl_message **ret);
+int sd_rtnl_message_link_new(uint16_t msg_type, int index, sd_rtnl_message **ret);
 int sd_rtnl_message_addr_new(uint16_t msg_type, int index, unsigned char family,
                              unsigned char prefixlen, unsigned char flags,
                              unsigned char scope, sd_rtnl_message **ret);
@@ -80,6 +79,8 @@ 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_set_flags(sd_rtnl_message *m, unsigned flags);
+int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type);
 int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex);
 int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags);