chiark / gitweb /
sd-rtnl-message: store reference to the bus in the message
authorTom Gundersen <teg@jklm.no>
Mon, 17 Feb 2014 23:10:08 +0000 (00:10 +0100)
committerTom Gundersen <teg@jklm.no>
Tue, 18 Feb 2014 10:21:22 +0000 (11:21 +0100)
This mimics the sd-bus api, as we may need it in the future.

src/core/loopback-setup.c
src/libsystemd/sd-rtnl/rtnl-internal.h
src/libsystemd/sd-rtnl/rtnl-message.c
src/libsystemd/sd-rtnl/rtnl-util.c
src/libsystemd/sd-rtnl/test-rtnl.c
src/network/networkd-address.c
src/network/networkd-link.c
src/network/networkd-netdev.c
src/network/networkd-route.c
src/nspawn/nspawn.c
src/systemd/sd-rtnl.h

index 8a5b8121bc2404f1ca210aa4f1fd2831449153bb..e1f725cddb2b8dba16314b9cd7d07acba9b9c816 100644 (file)
@@ -50,7 +50,7 @@ static int add_addresses(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_ad
         _cleanup_rtnl_message_unref_ sd_rtnl_message *ipv4 = NULL, *ipv6 = NULL;
         int r;
 
-        r = sd_rtnl_message_new_addr(RTM_NEWADDR, if_loopback, AF_INET, &ipv4);
+        r = sd_rtnl_message_new_addr(rtnl, RTM_NEWADDR, if_loopback, AF_INET, &ipv4);
         if (r < 0)
                 return r;
 
@@ -79,7 +79,7 @@ static int add_addresses(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_ad
         if (!socket_ipv6_is_supported())
                 return 0;
 
-        r = sd_rtnl_message_new_addr(RTM_NEWADDR, if_loopback, AF_INET6, &ipv6);
+        r = sd_rtnl_message_new_addr(rtnl, RTM_NEWADDR, if_loopback, AF_INET6, &ipv6);
         if (r < 0)
                 return r;
 
@@ -112,7 +112,7 @@ static int start_interface(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_
         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, if_loopback, &req);
+        r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, if_loopback, &req);
         if (r < 0)
                 return r;
 
index 28c7b963fb7f14d9644034ebc95db98cf6eb90d2..0667be58380499112c3c34328089026f549e9056 100644 (file)
@@ -88,6 +88,8 @@ struct sd_rtnl {
 struct sd_rtnl_message {
         RefCount n_ref;
 
+        sd_rtnl *rtnl;
+
         struct nlmsghdr *hdr;
         size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
         unsigned n_containers; /* number of containers */
@@ -96,7 +98,7 @@ struct sd_rtnl_message {
         bool sealed:1;
 };
 
-int message_new(sd_rtnl_message **ret, size_t initial_size);
+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);
index a13bd48725212d057effac0eca6a3675110aab4e..eb4b445da7d55d5bdc3e3d281cf699c7a14808e9 100644 (file)
@@ -37,7 +37,7 @@
 #define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
 #define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
 
-int message_new(sd_rtnl_message **ret, size_t initial_size) {
+int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size) {
         sd_rtnl_message *m;
 
         assert_return(ret, -EINVAL);
@@ -58,6 +58,9 @@ int message_new(sd_rtnl_message **ret, size_t initial_size) {
         m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
         m->sealed = false;
 
+        if (rtnl)
+                m->rtnl = sd_rtnl_ref(rtnl);
+
         *ret = m;
 
         return 0;
@@ -81,7 +84,8 @@ int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char pr
         return 0;
 }
 
-int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
+int sd_rtnl_message_new_route(sd_rtnl *rtnl, uint16_t nlmsg_type,
+                              unsigned char rtm_family,
                               sd_rtnl_message **ret) {
         struct rtmsg *rtm;
         int r;
@@ -90,7 +94,7 @@ int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
         assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
         assert_return(ret, -EINVAL);
 
-        r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
+        r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct rtmsg)));
         if (r < 0)
                 return r;
 
@@ -142,7 +146,8 @@ int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
         return 0;
 }
 
-int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
+int sd_rtnl_message_new_link(sd_rtnl *rtnl, uint16_t nlmsg_type, int index,
+                             sd_rtnl_message **ret) {
         struct ifinfomsg *ifi;
         int r;
 
@@ -151,7 +156,7 @@ int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **r
                       nlmsg_type == RTM_SETLINK || index > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
-        r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
+        r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
         if (r < 0)
                 return r;
 
@@ -216,7 +221,8 @@ int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
         return 0;
 }
 
-int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char family,
+int sd_rtnl_message_new_addr(sd_rtnl *rtnl, uint16_t nlmsg_type, int index,
+                             unsigned char family,
                              sd_rtnl_message **ret) {
         struct ifaddrmsg *ifa;
         int r;
@@ -226,7 +232,7 @@ int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char famil
         assert_return(family == AF_INET || family == AF_INET6, -EINVAL);
         assert_return(ret, -EINVAL);
 
-        r = message_new(ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
+        r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
         if (r < 0)
                 return r;
 
@@ -258,6 +264,7 @@ sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m) {
 
 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);
         }
@@ -897,7 +904,7 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
         if (r < 0)
                 return r;
 
-        r = message_new(&m, need);
+        r = message_new(nl, &m, need);
         if (r < 0)
                 return r;
 
index 6545ad0c5b4085b80af32b2e0e86c0fadf6026e7..ec1a92e01f9f091a65b2034bedf9991ca28f1dac 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_new_link(RTM_SETLINK, ifindex, &message);
+        r = sd_rtnl_message_new_link(rtnl, 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_new_link(RTM_SETLINK, ifindex, &message);
+        r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, ifindex, &message);
         if (r < 0)
                 return r;
 
@@ -105,7 +105,7 @@ int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message
 
         assert(error <= 0);
 
-        r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
+        r = message_new(NULL, ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
         if (r < 0)
                 return r;
 
index 21e12f65e3fe7c46c03e0f85a6df751668b4b493..7bbc4486f2440c50f929b1ed89bfab1022dc917a 100644 (file)
@@ -36,7 +36,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_new_link(RTM_GETLINK, ifindex, &message) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &message) >= 0);
         assert(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
         assert(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
         assert(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
@@ -65,7 +65,7 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
         void *data;
         uint16_t type;
 
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
         assert(m);
 
         /* u8 test cases  */
@@ -136,7 +136,7 @@ static void test_route(void) {
         void *data;
         int r;
 
-        r = sd_rtnl_message_new_route(RTM_NEWROUTE, AF_INET, &req);
+        r = sd_rtnl_message_new_route(NULL, RTM_NEWROUTE, AF_INET, &req);
         if (r < 0) {
                 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
                 return;
@@ -215,7 +215,7 @@ static void test_event_loop(int ifindex) {
         assert(ifname);
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
 
         assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
 
@@ -249,7 +249,7 @@ static void test_async(int ifindex) {
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
 
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
 
         assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
 
@@ -264,8 +264,8 @@ static void test_pipe(int ifindex) {
 
         assert(sd_rtnl_open(0, &rtnl) >= 0);
 
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m1) >= 0);
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m2) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m1) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m2) >= 0);
 
         counter ++;
         assert(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
@@ -284,7 +284,7 @@ static void test_container(void) {
         uint16_t type;
         void *data;
 
-        assert(sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m) >= 0);
+        assert(sd_rtnl_message_new_link(NULL, RTM_NEWLINK, 0, &m) >= 0);
 
         assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
         assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP);
@@ -364,7 +364,7 @@ int main(void) {
 
         test_link_configure(rtnl, if_loopback);
 
-        assert(sd_rtnl_message_new_link(RTM_GETLINK, if_loopback, &m) >= 0);
+        assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, if_loopback, &m) >= 0);
         assert(m);
 
         assert(sd_rtnl_message_get_type(m, &type) >= 0);
index edae62c802cc6b5b64aecc96d40950bc7d7698a7..560605408f30553996171a79eec3a7d6d4ac7c78 100644 (file)
@@ -105,7 +105,8 @@ int address_drop(Address *address, Link *link,
         assert(link->manager);
         assert(link->manager->rtnl);
 
-        r = sd_rtnl_message_new_addr(RTM_DELADDR, link->ifindex, address->family, &req);
+        r = sd_rtnl_message_new_addr(link->manager->rtnl, RTM_DELADDR,
+                                     link->ifindex, address->family, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_DELADDR message: %s",
                           strerror(-r));
@@ -149,8 +150,8 @@ int address_configure(Address *address, Link *link,
         assert(link->manager);
         assert(link->manager->rtnl);
 
-        r = sd_rtnl_message_new_addr(RTM_NEWADDR, link->ifindex,
-                        address->family, &req);
+        r = sd_rtnl_message_new_addr(link->manager->rtnl, RTM_NEWADDR,
+                                     link->ifindex, address->family, &req);
         if (r < 0) {
                 log_error("Could not allocate RTM_NEWADDR message: %s",
                           strerror(-r));
index 9aca1b647786971ade07a01b80ad1b5770f0da73..ec283d5b7b3e9e5c0172c576c747e93ba793e64d 100644 (file)
@@ -435,7 +435,8 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
 
         log_debug_link(link, "setting MTU: %" PRIu32, mtu);
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_SETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_SETLINK message");
                 return r;
@@ -794,7 +795,8 @@ static int link_up(Link *link) {
 
         log_debug_link(link, "bringing link up");
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_SETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_SETLINK message");
                 return r;
@@ -961,7 +963,8 @@ static int link_get(Link *link) {
 
         log_debug_link(link, "requesting link status");
 
-        r = sd_rtnl_message_new_link(RTM_GETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_GETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_GETLINK message");
                 return r;
index 30eb77d4bcb3c6fe7a5c842b7ca6ffd81f1a22a2..01ea8f68bc7aeecfbd2edfca650718ae0eee38f7 100644 (file)
@@ -89,10 +89,13 @@ static int netdev_enslave_ready(NetDev *netdev, Link* link, sd_rtnl_message_hand
 
         assert(netdev);
         assert(netdev->state == NETDEV_STATE_READY);
+        assert(netdev->manager);
+        assert(netdev->manager->rtnl);
         assert(link);
         assert(callback);
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(netdev->manager->rtnl, RTM_SETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_netdev(netdev,
                                  "Could not allocate RTM_SETLINK message: %s",
@@ -163,12 +166,13 @@ static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t c
         int r;
 
         assert(netdev);
-        assert(!(netdev->kind == NETDEV_KIND_VLAN) || (link && callback && netdev->vlanid <= VLANID_MAX));
+        assert(!(netdev->kind == NETDEV_KIND_VLAN) ||
+               (link && callback && netdev->vlanid <= VLANID_MAX));
         assert(netdev->name);
         assert(netdev->manager);
         assert(netdev->manager->rtnl);
 
-        r = sd_rtnl_message_new_link(RTM_NEWLINK, 0, &req);
+        r = sd_rtnl_message_new_link(netdev->manager->rtnl, RTM_NEWLINK, 0, &req);
         if (r < 0) {
                 log_error_netdev(netdev,
                                  "Could not allocate RTM_NEWLINK message: %s",
index 098539ed19c8a147f15481689fa2a958edf6c5ea..5896c4a4acd36a17a1202ac1d5e3a5f103dde277 100644 (file)
@@ -105,7 +105,8 @@ int route_configure(Route *route, Link *link,
         assert(link->ifindex > 0);
         assert(route->family == AF_INET || route->family == AF_INET6);
 
-        r = sd_rtnl_message_new_route(RTM_NEWROUTE, route->family, &req);
+        r = sd_rtnl_message_new_route(link->manager->rtnl, RTM_NEWROUTE,
+                                      route->family, &req);
         if (r < 0) {
                 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
                 return r;
index c003bd4ccc8468bb87142116563e5c4ff4696cce..089af078860bce50546cfb4ff5ac72893261bb3d 100644 (file)
@@ -1303,7 +1303,7 @@ static int setup_veth(pid_t pid, char iface_name[]) {
                 return r;
         }
 
-        r = sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m);
+        r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, 0, &m);
         if (r < 0) {
                 log_error("Failed to allocate netlink message: %s", strerror(-r));
                 return r;
@@ -1404,7 +1404,7 @@ static int setup_bridge(const char veth_name[]) {
                 return r;
         }
 
-        r = sd_rtnl_message_new_link(RTM_SETLINK, 0, &m);
+        r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, 0, &m);
         if (r < 0) {
                 log_error("Failed to allocate netlink message: %s", strerror(-r));
                 return r;
@@ -1479,7 +1479,7 @@ static int move_network_interfaces(pid_t pid) {
                         return -EBUSY;
                 }
 
-                r = sd_rtnl_message_new_link(RTM_NEWLINK, ifi, &m);
+                r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, ifi, &m);
                 if (r < 0) {
                         log_error("Failed to allocate netlink message: %s", strerror(-r));
                         return r;
index 321f42af06da0f14dbe773fee2a56c9bb0d0731e..502183e9e8187e3fd32f65fdb44912a9065ce77b 100644 (file)
@@ -67,11 +67,12 @@ 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_new_link(uint16_t msg_type, int index, sd_rtnl_message **ret);
-int sd_rtnl_message_new_addr(uint16_t msg_type, int index, unsigned char family,
+int sd_rtnl_message_new_link(sd_rtnl *rtnl, uint16_t msg_type, int index,
                              sd_rtnl_message **ret);
-int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
-                              sd_rtnl_message **ret);
+int sd_rtnl_message_new_addr(sd_rtnl *rtnl, uint16_t msg_type, int index,
+                             unsigned char family, sd_rtnl_message **ret);
+int sd_rtnl_message_new_route(sd_rtnl *rtnl, uint16_t nlmsg_type,
+                              unsigned char rtm_family, sd_rtnl_message **ret);
 
 sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m);
 sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m);