]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: tunnel: introduce TUNNEL() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 6 Dec 2021 20:00:39 +0000 (05:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 7 Dec 2021 16:54:01 +0000 (01:54 +0900)
src/network/netdev/tunnel.c
src/network/netdev/tunnel.h

index 261c211c7dfd70679ad2868ec46b10f8ca76389b..9f2a93468c05904468824c97f59fe030f6711cf9 100644 (file)
@@ -177,13 +177,13 @@ static int netdev_ipip_sit_fill_message_create(NetDev *netdev, Link *link, sd_ne
         int r;
 
         assert(netdev);
+        assert(m);
 
         if (netdev->kind == NETDEV_KIND_IPIP)
                 t = IPIP(netdev);
         else
                 t = SIT(netdev);
 
-        assert(m);
         assert(t);
 
         if (link || t->assign_to_loopback) {
@@ -375,6 +375,7 @@ static int netdev_ip6gre_fill_message_create(NetDev *netdev, Link *link, sd_netl
         int r;
 
         assert(netdev);
+        assert(m);
 
         if (netdev->kind == NETDEV_KIND_IP6GRE)
                 t = IP6GRE(netdev);
@@ -382,7 +383,6 @@ static int netdev_ip6gre_fill_message_create(NetDev *netdev, Link *link, sd_netl
                 t = IP6GRETAP(netdev);
 
         assert(t);
-        assert(m);
 
         if (link || t->assign_to_loopback) {
                 r = sd_netlink_message_append_u32(m, IFLA_GRE_LINK, link ? link->ifindex : LOOPBACK_IFINDEX);
@@ -495,12 +495,15 @@ static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_netlink
 }
 
 static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
-        Tunnel *t = IP6TNL(netdev);
         uint8_t proto;
+        Tunnel *t;
         int r;
 
         assert(netdev);
         assert(m);
+
+        t = IP6TNL(netdev);
+
         assert(t);
 
         if (link || t->assign_to_loopback) {
@@ -564,45 +567,12 @@ static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_netl
 }
 
 static int netdev_tunnel_verify(NetDev *netdev, const char *filename) {
-        Tunnel *t = NULL;
+        Tunnel *t;
 
         assert(netdev);
         assert(filename);
 
-        switch (netdev->kind) {
-        case NETDEV_KIND_IPIP:
-                t = IPIP(netdev);
-                break;
-        case NETDEV_KIND_SIT:
-                t = SIT(netdev);
-                break;
-        case NETDEV_KIND_GRE:
-                t = GRE(netdev);
-                break;
-        case NETDEV_KIND_GRETAP:
-                t = GRETAP(netdev);
-                break;
-        case NETDEV_KIND_IP6GRE:
-                t = IP6GRE(netdev);
-                break;
-        case NETDEV_KIND_IP6GRETAP:
-                t = IP6GRETAP(netdev);
-                break;
-        case NETDEV_KIND_VTI:
-                t = VTI(netdev);
-                break;
-        case NETDEV_KIND_VTI6:
-                t = VTI6(netdev);
-                break;
-        case NETDEV_KIND_IP6TNL:
-                t = IP6TNL(netdev);
-                break;
-        case NETDEV_KIND_ERSPAN:
-                t = ERSPAN(netdev);
-                break;
-        default:
-                assert_not_reached();
-        }
+        t = TUNNEL(netdev);
 
         assert(t);
 
index 0f387ae68dc28a1a48923dacfe49c03a35170e81..b9e68759fb4ef64fe3c68133cc5f9c63c3d78e5d 100644 (file)
@@ -74,6 +74,36 @@ DEFINE_NETDEV_CAST(VTI, Tunnel);
 DEFINE_NETDEV_CAST(VTI6, Tunnel);
 DEFINE_NETDEV_CAST(IP6TNL, Tunnel);
 DEFINE_NETDEV_CAST(ERSPAN, Tunnel);
+
+static inline Tunnel* TUNNEL(NetDev *netdev) {
+        assert(netdev);
+
+        switch (netdev->kind) {
+        case NETDEV_KIND_IPIP:
+                return IPIP(netdev);
+        case NETDEV_KIND_SIT:
+                return SIT(netdev);
+        case NETDEV_KIND_GRE:
+                return GRE(netdev);
+        case NETDEV_KIND_GRETAP:
+                return GRETAP(netdev);
+        case NETDEV_KIND_IP6GRE:
+                return IP6GRE(netdev);
+        case NETDEV_KIND_IP6GRETAP:
+                return IP6GRETAP(netdev);
+        case NETDEV_KIND_VTI:
+                return VTI(netdev);
+        case NETDEV_KIND_VTI6:
+                return VTI6(netdev);
+        case NETDEV_KIND_IP6TNL:
+                return IP6TNL(netdev);
+        case NETDEV_KIND_ERSPAN:
+                return ERSPAN(netdev);
+        default:
+                return NULL;
+        }
+}
+
 extern const NetDevVTable ipip_vtable;
 extern const NetDevVTable sit_vtable;
 extern const NetDevVTable vti_vtable;