]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: netlink: Add some wrapper macros to get rid of redundancy
authorShi Lei <shi_lei@massclouds.com>
Fri, 7 Sep 2018 07:17:25 +0000 (15:17 +0800)
committerErik Skultety <eskultet@redhat.com>
Wed, 12 Sep 2018 07:27:26 +0000 (09:27 +0200)
This patch adds wrapper macros around nla_nest_[start|end] and nla_put,
thus getting rid of some redundancy and making virNetlinkNewLink more
readable.

Signed-off-by: Shi Lei <shi_lei@massclouds.com>
Signed-off-by: Erik Skultety <eskultet@redhat.com>
src/util/virnetlink.c
src/util/virnetlink.h

index f0ff7e9db1f79d01444459ebb7868a4847626245..e473ccbe86e2ade051c3ddae21f0bb950cdf72ca 100644 (file)
@@ -537,41 +537,27 @@ virNetlinkNewLink(const char *ifname,
     if (nlmsg_append(nl_msg,  &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
         goto buffer_too_small;
 
-    if (ifname && nla_put(nl_msg, IFLA_IFNAME,
-                          (strlen(ifname) + 1), ifname) < 0)
-        goto buffer_too_small;
-
-    if (!(linkinfo = nla_nest_start(nl_msg, IFLA_LINKINFO)))
-        goto buffer_too_small;
+    NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname);
 
-    if (type && nla_put(nl_msg, IFLA_INFO_KIND, (strlen(type) + 1), type) < 0)
-        goto buffer_too_small;
+    NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO);
+    NETLINK_MSG_PUT(nl_msg, IFLA_INFO_KIND, (strlen(type) + 1), type);
 
     if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) &&
          extra_args &&
          extra_args->macvlan_mode &&
          *extra_args->macvlan_mode > 0) {
-        if (!(infodata = nla_nest_start(nl_msg, IFLA_INFO_DATA)))
-            goto buffer_too_small;
-
-        if (nla_put(nl_msg, IFLA_MACVLAN_MODE,
-                    sizeof(uint32_t), extra_args->macvlan_mode) < 0)
-            goto buffer_too_small;
-
-        nla_nest_end(nl_msg, infodata);
+        NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA);
+        NETLINK_MSG_PUT(nl_msg, IFLA_MACVLAN_MODE,
+                        sizeof(uint32_t), extra_args->macvlan_mode);
+        NETLINK_MSG_NEST_END(nl_msg, infodata);
     }
 
-    nla_nest_end(nl_msg, linkinfo);
+    NETLINK_MSG_NEST_END(nl_msg, linkinfo);
 
     if (extra_args) {
-        if (extra_args->ifindex &&
-            nla_put(nl_msg, IFLA_LINK,
-                    sizeof(uint32_t), extra_args->ifindex) < 0)
-            goto buffer_too_small;
-
-        if (extra_args->mac &&
-            nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, extra_args->mac) < 0)
-            goto buffer_too_small;
+        NETLINK_MSG_PUT(nl_msg, IFLA_LINK,
+                        sizeof(uint32_t), extra_args->ifindex);
+        NETLINK_MSG_PUT(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, extra_args->mac);
     }
 
     if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) < 0)
index 09bab08b1f4017daa2fef13b9a59e77b1a3071db..fd140f0d57c37049f7366c62e27c2e5a040e3f55 100644 (file)
@@ -48,6 +48,29 @@ struct nlmsghdr;
 
 # endif /* __linux__ */
 
+# define NETLINK_MSG_NEST_START(msg, container, attrtype) \
+do { \
+    container = nla_nest_start(msg, attrtype); \
+    if (!container) \
+        goto buffer_too_small; \
+} while(0)
+
+# define NETLINK_MSG_NEST_END(msg, container) \
+do { nla_nest_end(msg, container); } while(0)
+
+/*
+ * we need to use an intermediary pointer to @data as compilers may sometimes
+ * complain about @data not being a pointer type:
+ * error: the address of 'foo' will always evaluate as 'true' [-Werror=address]
+ */
+# define NETLINK_MSG_PUT(msg, attrtype, datalen, data) \
+do { \
+    const void *dataptr = data; \
+    if (dataptr && nla_put(msg, attrtype, datalen, dataptr) < 0) \
+        goto buffer_too_small; \
+} while(0)
+
+
 int virNetlinkStartup(void);
 void virNetlinkShutdown(void);