diff options
author | Thomas Haller <thaller@redhat.com> | 2019-12-31 02:38:06 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-01-09 10:42:31 +0100 |
commit | 4a743aba651a2234c502d7d468413494e343d6e2 (patch) | |
tree | b760187f86b978705b3fe21147667905fe633346 | |
parent | 6e8653eb5b45f284db8d35776b3ae35d7523aca8 (diff) | |
download | NetworkManager-4a743aba651a2234c502d7d468413494e343d6e2.tar.gz |
platform: extend nm_platform_link_add() to accept type specific extra parameter
This will be used to unify all link-add implementation.
-rw-r--r-- | src/platform/nm-fake-platform.c | 26 | ||||
-rw-r--r-- | src/platform/nm-linux-platform.c | 27 | ||||
-rw-r--r-- | src/platform/nm-platform.c | 41 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 4 |
4 files changed, 63 insertions, 35 deletions
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 9fa2791fec..6271ee997c 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -276,11 +276,11 @@ link_add_pre (NMPlatform *platform, static int link_add (NMPlatform *platform, - const char *name, NMLinkType type, - const char *veth_peer, + const char *name, const void *address, size_t address_len, + gconstpointer extra_data, const NMPlatformLink **out_link) { NMFakePlatformLink *device; @@ -291,14 +291,20 @@ link_add (NMPlatform *platform, nm_auto_nmpobj const NMPObject *obj_new_veth = NULL; NMPCacheOpsType cache_op; NMPCacheOpsType cache_op_veth = NMP_CACHE_OPS_UNCHANGED; + const char *veth_peer = NULL; device = link_add_pre (platform, name, type, address, address_len); - if (veth_peer) { - g_assert (type == NM_LINK_TYPE_VETH); + switch (type) { + case NM_LINK_TYPE_VETH: + veth_peer = extra_data; + g_assert (veth_peer); device_veth = link_add_pre (platform, veth_peer, type, NULL, 0); - } else - g_assert (type != NM_LINK_TYPE_VETH); + break; + default: + g_assert (!extra_data); + break; + } link_add_prepare (platform, device, (NMPObject *) device->obj); cache_op = nmp_cache_update_netlink (nm_platform_get_cache (platform), @@ -1355,12 +1361,12 @@ nm_fake_platform_setup (void) nm_platform_setup (platform); /* add loopback interface */ - link_add (platform, "lo", NM_LINK_TYPE_LOOPBACK, NULL, NULL, 0, NULL); + link_add (platform, NM_LINK_TYPE_LOOPBACK, "lo", NULL, 0, NULL, NULL); /* add some ethernets */ - link_add (platform, "eth0", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); - link_add (platform, "eth1", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); - link_add (platform, "eth2", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); + link_add (platform, NM_LINK_TYPE_ETHERNET, "eth0", NULL, 0, NULL, NULL); + link_add (platform, NM_LINK_TYPE_ETHERNET, "eth1", NULL, 0, NULL, NULL); + link_add (platform, NM_LINK_TYPE_ETHERNET, "eth2", NULL, 0, NULL, NULL); } static void diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index f7a4504100..586f1dc556 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -3689,13 +3689,13 @@ nla_put_failure: static gboolean _nl_msg_new_link_set_linkinfo (struct nl_msg *msg, NMLinkType link_type, - const char *veth_peer) + gconstpointer extra_data) { struct nlattr *info; + struct nlattr *data = NULL; const char *kind; nm_assert (msg); - nm_assert (!!veth_peer == (link_type == NM_LINK_TYPE_VETH)); kind = nm_link_type_to_rtnl_type_string (link_type); if (!kind) @@ -3706,9 +3706,13 @@ _nl_msg_new_link_set_linkinfo (struct nl_msg *msg, NLA_PUT_STRING (msg, IFLA_INFO_KIND, kind); - if (veth_peer) { + switch (link_type) { + case NM_LINK_TYPE_VETH: { + const char *veth_peer = extra_data; const struct ifinfomsg ifi = { }; - struct nlattr *data, *info_peer; + struct nlattr *info_peer; + + nm_assert (veth_peer); if (!(data = nla_nest_start (msg, IFLA_INFO_DATA))) goto nla_put_failure; @@ -3718,9 +3722,16 @@ _nl_msg_new_link_set_linkinfo (struct nl_msg *msg, goto nla_put_failure; NLA_PUT_STRING (msg, IFLA_IFNAME, veth_peer); nla_nest_end (msg, info_peer); - nla_nest_end (msg, data); + break; + } + default: + nm_assert (!extra_data); + break; } + if (data) + nla_nest_end (msg, data); + nla_nest_end (msg, info); return TRUE; @@ -6491,11 +6502,11 @@ out: static int link_add (NMPlatform *platform, - const char *name, NMLinkType type, - const char *veth_peer, + const char *name, const void *address, size_t address_len, + gconstpointer extra_data, const NMPlatformLink **out_link) { nm_auto_nlmsg struct nl_msg *nlmsg = NULL; @@ -6522,7 +6533,7 @@ link_add (NMPlatform *platform, if (address && address_len) NLA_PUT (nlmsg, IFLA_ADDRESS, address_len, address); - if (!_nl_msg_new_link_set_linkinfo (nlmsg, type, veth_peer)) + if (!_nl_msg_new_link_set_linkinfo (nlmsg, type, extra_data)) return -NME_UNSPEC; return do_add_link_with_lookup (platform, type, name, nlmsg, out_link); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 09793824fc..833ed63904 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1150,11 +1150,11 @@ _link_add_check_existing (NMPlatform *self, const char *name, NMLinkType type, c /** * nm_platform_link_add: * @self: platform instance - * @name: Interface name * @type: Interface type - * @veth_peer: For veths, the peer name + * @name: Interface name * @address: (allow-none): set the mac address of the link * @address_len: the length of the @address + * @extra_data: depending on @type, additional data. * @out_link: on success, the link object * * Add a software interface. If the interface already exists and is of type @@ -1169,22 +1169,22 @@ _link_add_check_existing (NMPlatform *self, const char *name, NMLinkType type, c */ static int nm_platform_link_add (NMPlatform *self, - const char *name, NMLinkType type, - const char *veth_peer, + const char *name, const void *address, size_t address_len, + gconstpointer extra_data, const NMPlatformLink **out_link) { int r; char addr_buf[NM_UTILS_HWADDR_LEN_MAX * 3]; + char buf[512]; _CHECK_SELF (self, klass, -NME_BUG); g_return_val_if_fail (name, -NME_BUG); g_return_val_if_fail ((address != NULL) ^ (address_len == 0) , -NME_BUG); g_return_val_if_fail (address_len <= NM_UTILS_HWADDR_LEN_MAX, -NME_BUG); - g_return_val_if_fail ((!!veth_peer) == (type == NM_LINK_TYPE_VETH), -NME_BUG); r = _link_add_check_existing (self, name, type, out_link); if (r < 0) @@ -1194,16 +1194,27 @@ nm_platform_link_add (NMPlatform *self, "%s " /* type */ "\"%s\"" /* name */ "%s%s" /* address */ - "%s%s" /* veth peer */ + "%s" /* extra_data */ "", nm_link_type_to_string (type), name, address ? ", address: " : "", address ? nm_utils_hwaddr_ntoa_buf (address, address_len, FALSE, addr_buf, sizeof (addr_buf)) : "", - veth_peer ? ", veth-peer: " : "", - veth_peer ?: ""); + ({ + const char *str = ""; + + switch (type) { + case NM_LINK_TYPE_VETH: + str = nm_sprintf_buf (buf, ", veth-peer \"%s\"", (const char *) extra_data); + break; + default: + break; + } + + str; + })); - return klass->link_add (self, name, type, veth_peer, address, address_len, out_link); + return klass->link_add (self, type, name, address, address_len, extra_data, out_link); } int @@ -1212,7 +1223,7 @@ nm_platform_link_veth_add (NMPlatform *self, const char *peer, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_VETH, peer, NULL, 0, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_VETH, name, NULL, 0, peer, out_link); } /** @@ -1228,7 +1239,7 @@ nm_platform_link_dummy_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_DUMMY, NULL, NULL, 0, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_DUMMY, name, NULL, 0, NULL, out_link); } /** @@ -2256,7 +2267,7 @@ nm_platform_link_wireguard_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_WIREGUARD, NULL, NULL, 0, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_WIREGUARD, name, NULL, 0, NULL, out_link); } int @@ -2334,7 +2345,7 @@ nm_platform_link_bridge_add (NMPlatform *self, size_t address_len, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_BRIDGE, NULL, address, address_len, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_BRIDGE, name, address, address_len, NULL, out_link); } /** @@ -2350,7 +2361,7 @@ nm_platform_link_bond_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_BOND, NULL, NULL, 0, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_BOND, name, NULL, 0, NULL, out_link); } /** @@ -2366,7 +2377,7 @@ nm_platform_link_team_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_TEAM, NULL, NULL, 0, out_link); + return nm_platform_link_add (self, NM_LINK_TYPE_TEAM, name, NULL, 0, NULL, out_link); } /** diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index a31564ed3f..7850aa9563 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -965,11 +965,11 @@ typedef struct { void (*process_events) (NMPlatform *self); int (*link_add) (NMPlatform *self, - const char *name, NMLinkType type, - const char *veth_peer, + const char *name, const void *address, size_t address_len, + gconstpointer extra_data, const NMPlatformLink **out_link); gboolean (*link_delete) (NMPlatform *self, int ifindex); gboolean (*link_refresh) (NMPlatform *self, int ifindex); |