diff --git a/include/NetworkManager.h b/include/NetworkManager.h index 737ecc802f..670a6c9b92 100644 --- a/include/NetworkManager.h +++ b/include/NetworkManager.h @@ -54,6 +54,7 @@ #define NM_DBUS_INTERFACE_DEVICE_VLAN NM_DBUS_INTERFACE_DEVICE ".Vlan" #define NM_DBUS_INTERFACE_DEVICE_BRIDGE NM_DBUS_INTERFACE_DEVICE ".Bridge" #define NM_DBUS_INTERFACE_DEVICE_GENERIC NM_DBUS_INTERFACE_DEVICE ".Generic" +#define NM_DBUS_INTERFACE_DEVICE_VETH NM_DBUS_INTERFACE_DEVICE ".Veth" #define NM_DBUS_IFACE_SETTINGS "org.freedesktop.NetworkManager.Settings" diff --git a/introspection/Makefile.am b/introspection/Makefile.am index 82e9fba8d8..44804c95c4 100644 --- a/introspection/Makefile.am +++ b/introspection/Makefile.am @@ -16,6 +16,7 @@ EXTRA_DIST = \ nm-device-bridge.xml \ nm-device-vlan.xml \ nm-device-generic.xml \ + nm-device-veth.xml \ nm-device.xml \ nm-ip4-config.xml \ nm-ip6-config.xml \ diff --git a/introspection/nm-device-veth.xml b/introspection/nm-device-veth.xml new file mode 100644 index 0000000000..e06d0ceb7d --- /dev/null +++ b/introspection/nm-device-veth.xml @@ -0,0 +1,21 @@ + + + + + + + + The object path of the device's peer. + + + + + + + A dictionary mapping property names to variant boxed values + + + + + + diff --git a/src/Makefile.am b/src/Makefile.am index e90b46ce84..f937364f2a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -83,6 +83,8 @@ nm_sources = \ devices/nm-device-olpc-mesh.c \ devices/nm-device-olpc-mesh.h \ devices/nm-device-private.h \ + devices/nm-device-veth.c \ + devices/nm-device-veth.h \ devices/nm-device-vlan.c \ devices/nm-device-vlan.h \ devices/nm-device-wifi.c \ @@ -315,6 +317,7 @@ glue_sources = \ nm-device-infiniband-glue.h \ nm-device-modem-glue.h \ nm-device-olpc-mesh-glue.h \ + nm-device-veth-glue.h \ nm-device-vlan-glue.h \ nm-device-wifi-glue.h \ nm-dhcp4-config-glue.h \ diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index d88a4a3311..3fa34e52d3 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -240,7 +240,8 @@ constructor (GType type, priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); ifindex = nm_device_get_ifindex (self); - g_assert (nm_platform_link_get_type (ifindex) == NM_LINK_TYPE_ETHERNET); + g_assert ( nm_platform_link_get_type (ifindex) == NM_LINK_TYPE_ETHERNET + || nm_platform_link_get_type (ifindex) == NM_LINK_TYPE_VETH); nm_log_dbg (LOGD_HW | LOGD_ETHER, "(%s): kernel ifindex %d", nm_device_get_iface (NM_DEVICE (self)), diff --git a/src/devices/nm-device-veth.c b/src/devices/nm-device-veth.c new file mode 100644 index 0000000000..61935b562f --- /dev/null +++ b/src/devices/nm-device-veth.c @@ -0,0 +1,158 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2013 Red Hat, Inc. + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "nm-device-veth.h" +#include "nm-logging.h" +#include "nm-manager.h" +#include "nm-platform.h" +#include "nm-dbus-manager.h" + +#include "nm-device-veth-glue.h" + +G_DEFINE_TYPE (NMDeviceVeth, nm_device_veth, NM_TYPE_DEVICE_ETHERNET) + +#define NM_DEVICE_VETH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VETH, NMDeviceVethPrivate)) + +typedef struct { + NMDevice *peer; + gboolean ever_had_peer; +} NMDeviceVethPrivate; + +enum { + PROP_0, + PROP_PEER, + + LAST_PROP +}; + +/**************************************************************/ + +static void +set_peer (NMDeviceVeth *self, NMDevice *peer) +{ + NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self); + + if (!priv->peer) { + priv->ever_had_peer = TRUE; + priv->peer = peer; + g_object_add_weak_pointer (G_OBJECT (peer), (gpointer *) &priv->peer); + + g_object_notify (G_OBJECT (self), NM_DEVICE_VETH_PEER); + } +} + +static NMDevice * +get_peer (NMDeviceVeth *self) +{ + NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self); + NMDevice *device = NM_DEVICE (self), *peer = NULL; + NMPlatformVethProperties props; + + if (priv->ever_had_peer) + return priv->peer; + + if (!nm_platform_veth_get_properties (nm_device_get_ifindex (device), &props)) { + nm_log_warn (LOGD_HW, "(%s): could not read veth properties", + nm_device_get_iface (device)); + return NULL; + } + + peer = nm_manager_get_device_by_ifindex (nm_manager_get (), props.peer); + if (peer && NM_IS_DEVICE_VETH (peer)) { + set_peer (self, peer); + set_peer (NM_DEVICE_VETH (peer), device); + } + + return priv->peer; +} + + +/**************************************************************/ + +NMDevice * +nm_device_veth_new (const char *udi, + const char *iface, + const char *driver) +{ + g_return_val_if_fail (udi != NULL, NULL); + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VETH, + NM_DEVICE_UDI, udi, + NM_DEVICE_IFACE, iface, + NM_DEVICE_DRIVER, driver ? driver : "veth", + NM_DEVICE_TYPE_DESC, "Veth", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET, + NULL); +} + +static void +nm_device_veth_init (NMDeviceVeth *self) +{ +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMDeviceVeth *self = NM_DEVICE_VETH (object); + NMDevice *peer; + + switch (prop_id) { + case PROP_PEER: + peer = get_peer (self); + g_value_set_boxed (value, peer ? nm_device_get_path (peer) : "/"); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_device_veth_class_init (NMDeviceVethClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (NMDeviceVethPrivate)); + + object_class->get_property = get_property; + + /* properties */ + g_object_class_install_property + (object_class, PROP_PEER, + g_param_spec_boxed (NM_DEVICE_VETH_PEER, + "Peer", + "Peer device", + DBUS_TYPE_G_OBJECT_PATH, + G_PARAM_READABLE)); + + nm_dbus_manager_register_exported_type (nm_dbus_manager_get (), + G_TYPE_FROM_CLASS (klass), + &dbus_glib_nm_device_veth_object_info); +} diff --git a/src/devices/nm-device-veth.h b/src/devices/nm-device-veth.h new file mode 100644 index 0000000000..a1edba0224 --- /dev/null +++ b/src/devices/nm-device-veth.h @@ -0,0 +1,56 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2013 Red Hat, Inc. + */ + +#ifndef NM_DEVICE_VETH_H +#define NM_DEVICE_VETH_H + +#include + +#include "nm-device-ethernet.h" + +G_BEGIN_DECLS + +#define NM_TYPE_DEVICE_VETH (nm_device_veth_get_type ()) +#define NM_DEVICE_VETH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_VETH, NMDeviceVeth)) +#define NM_DEVICE_VETH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEVICE_VETH, NMDeviceVethClass)) +#define NM_IS_DEVICE_VETH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_VETH)) +#define NM_IS_DEVICE_VETH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE_VETH)) +#define NM_DEVICE_VETH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_VETH, NMDeviceVethClass)) + +#define NM_DEVICE_VETH_PEER "peer" + +typedef struct { + NMDeviceEthernet parent; +} NMDeviceVeth; + +typedef struct { + NMDeviceEthernetClass parent; + +} NMDeviceVethClass; + +GType nm_device_veth_get_type (void); + +NMDevice *nm_device_veth_new (const char *udi, + const char *iface, + const char *driver); + +G_END_DECLS + +#endif /* NM_DEVICE_VETH_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 29ad3092ca..72c94029a8 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -51,6 +51,7 @@ #include "nm-device-vlan.h" #include "nm-device-adsl.h" #include "nm-device-generic.h" +#include "nm-device-veth.h" #include "nm-system.h" #include "nm-setting-bluetooth.h" #include "nm-setting-connection.h" @@ -451,6 +452,21 @@ nm_manager_get_device_by_master (NMManager *manager, const char *master, const c return NULL; } +NMDevice * +nm_manager_get_device_by_ifindex (NMManager *manager, int ifindex) +{ + GSList *iter; + + for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) { + NMDevice *device = NM_DEVICE (iter->data); + + if (nm_device_get_ifindex (device) == ifindex) + return device; + } + + return NULL; +} + static gboolean manager_sleeping (NMManager *self) { @@ -2272,6 +2288,9 @@ udev_device_added_cb (NMUdevManager *udev_mgr, } else nm_log_err (LOGD_HW, "(%s): failed to get VLAN parent ifindex", iface); break; + case NM_LINK_TYPE_VETH: + device = nm_device_veth_new (sysfs_path, iface, driver); + break; default: device = nm_device_generic_new (sysfs_path, iface, driver); diff --git a/src/nm-manager.h b/src/nm-manager.h index f4b4909d9e..5aeeaab62a 100644 --- a/src/nm-manager.h +++ b/src/nm-manager.h @@ -104,8 +104,10 @@ const GSList *nm_manager_get_active_connections (NMManager *manager); GSList *nm_manager_get_devices (NMManager *manager); NMDevice *nm_manager_get_device_by_master (NMManager *manager, - const char *master, - const char *driver); + const char *master, + const char *driver); +NMDevice *nm_manager_get_device_by_ifindex (NMManager *manager, + int ifindex); NMActiveConnection *nm_manager_activate_connection (NMManager *manager, NMConnection *connection, diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index ca7cb846bc..4cef7bb53b 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -561,6 +561,12 @@ vlan_set_egress_map (NMPlatform *platform, int ifindex, int from, int to) return !!link_get (platform, ifindex); } +static gboolean +veth_get_properties (NMPlatform *platform, int ifindex, NMPlatformVethProperties *props) +{ + return FALSE; +} + /******************************************************************/ static GArray * @@ -1018,6 +1024,8 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass) platform_class->vlan_set_ingress_map = vlan_set_ingress_map; platform_class->vlan_set_egress_map = vlan_set_egress_map; + platform_class->veth_get_properties = veth_get_properties; + platform_class->ip4_address_get_all = ip4_address_get_all; platform_class->ip6_address_get_all = ip6_address_get_all; platform_class->ip4_address_add = ip4_address_add; diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 4f0ef6088b..1ce538595f 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -269,6 +269,8 @@ type_to_string (NMLinkType type) switch (type) { case NM_LINK_TYPE_DUMMY: return "dummy"; + case NM_LINK_TYPE_VETH: + return "veth"; case NM_LINK_TYPE_VLAN: return "vlan"; case NM_LINK_TYPE_BRIDGE: @@ -322,6 +324,8 @@ link_extract_type (struct rtnl_link *rtnllink, const char **out_name) return_type (NM_LINK_TYPE_INFINIBAND, "infiniband"); else if (!strcmp (type, "dummy")) return_type (NM_LINK_TYPE_DUMMY, "dummy"); + else if (!strcmp (type, "veth")) + return_type (NM_LINK_TYPE_VETH, "veth"); else if (!strcmp (type, "vlan")) return_type (NM_LINK_TYPE_VLAN, "vlan"); else if (!strcmp (type, "bridge")) @@ -1402,6 +1406,33 @@ slave_get_option (NMPlatform *platform, int slave, const char *option) return link_get_option (slave, slave_category (platform, slave), option); } +static gboolean +veth_get_properties (NMPlatform *platform, int ifindex, NMPlatformVethProperties *props) +{ + const char *ifname; + auto_g_free struct ethtool_stats *stats = NULL; + int peer_ifindex_stat; + + ifname = nm_platform_link_get_name (ifindex); + if (!ifname) + return FALSE; + + peer_ifindex_stat = ethtool_get_stringset_index (ifname, ETH_SS_STATS, "peer_ifindex"); + if (peer_ifindex_stat == -1) { + debug ("%s: peer_ifindex ethtool stat does not exist?", ifname); + return FALSE; + } + + stats = g_malloc0 (sizeof (*stats) + (peer_ifindex_stat + 1) * sizeof (guint64)); + stats->cmd = ETHTOOL_GSTATS; + stats->n_stats = peer_ifindex_stat + 1; + if (!ethtool_get (ifname, stats)) + return FALSE; + + props->peer = stats->data[peer_ifindex_stat]; + return TRUE; +} + /******************************************************************/ static int @@ -1884,6 +1915,8 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass) platform_class->vlan_set_ingress_map = vlan_set_ingress_map; platform_class->vlan_set_egress_map = vlan_set_egress_map; + platform_class->veth_get_properties = veth_get_properties; + platform_class->ip4_address_get_all = ip4_address_get_all; platform_class->ip6_address_get_all = ip6_address_get_all; platform_class->ip4_address_add = ip4_address_add; diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 823090d7db..30ff000ab9 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -901,6 +901,17 @@ nm_platform_vlan_set_egress_map (int ifindex, int from, int to) return klass->vlan_set_egress_map (platform, ifindex, from, to); } +gboolean +nm_platform_veth_get_properties (int ifindex, NMPlatformVethProperties *props) +{ + reset_error (); + + g_return_val_if_fail (ifindex > 0, FALSE); + g_return_val_if_fail (props != NULL, FALSE); + + return klass->veth_get_properties (platform, ifindex, props); +} + /******************************************************************/ GArray * diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index ff4fa778b4..270ab7891c 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -59,6 +59,7 @@ typedef enum { /* Virtual types */ NM_LINK_TYPE_DUMMY, NM_LINK_TYPE_LOOPBACK, + NM_LINK_TYPE_VETH, NM_LINK_TYPE_VLAN, /* Virtual types with slaves */ @@ -110,6 +111,10 @@ typedef struct { guint mss; } NMPlatformIP6Route; +typedef struct { + int peer; +} NMPlatformVethProperties; + /******************************************************************/ /* NMPlatform abstract class and its implementations provide a layer between @@ -191,6 +196,8 @@ typedef struct { gboolean (*vlan_set_ingress_map) (NMPlatform *, int ifindex, int from, int to); gboolean (*vlan_set_egress_map) (NMPlatform *, int ifindex, int from, int to); + gboolean (*veth_get_properties) (NMPlatform *, int ifindex, NMPlatformVethProperties *properties); + GArray * (*ip4_address_get_all) (NMPlatform *, int ifindex); GArray * (*ip6_address_get_all) (NMPlatform *, int ifindex); gboolean (*ip4_address_add) (NMPlatform *, int ifindex, in_addr_t address, int plen); @@ -298,6 +305,8 @@ gboolean nm_platform_vlan_get_info (int ifindex, int *parent, int *vlanid); gboolean nm_platform_vlan_set_ingress_map (int ifindex, int from, int to); gboolean nm_platform_vlan_set_egress_map (int ifindex, int from, int to); +gboolean nm_platform_veth_get_properties (int ifindex, NMPlatformVethProperties *properties); + GArray *nm_platform_ip4_address_get_all (int ifindex); GArray *nm_platform_ip6_address_get_all (int ifindex); gboolean nm_platform_ip4_address_add (int ifindex, in_addr_t address, int plen); diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c index c554d74600..a4bf4d43ec 100644 --- a/src/platform/tests/platform.c +++ b/src/platform/tests/platform.c @@ -342,6 +342,19 @@ do_vlan_set_egress_map (char **argv) return nm_platform_vlan_set_egress_map (ifindex, from, to); } +static gboolean +do_veth_get_properties (char **argv) +{ + int ifindex = parse_ifindex (*argv++); + NMPlatformVethProperties props; + + if (!nm_platform_veth_get_properties (ifindex, &props)) + return FALSE; + + printf ("peer: %d\n", props.peer); + return TRUE; +} + static gboolean do_ip4_address_get_all (char **argv) { @@ -626,6 +639,8 @@ static const command_t commands[] = { " " }, { "vlan-set-egress-map", "set vlan egress map", do_vlan_set_egress_map, 3, " " }, + { "veth-get-properties", "get veth properties", do_veth_get_properties, 1, + "" }, { "ip4-address-get-all", "print all IPv4 addresses", do_ip4_address_get_all, 1, "" }, { "ip6-address-get-all", "print all IPv6 addresses", do_ip6_address_get_all, 1, "" }, { "ip4-address-add", "add IPv4 address", do_ip4_address_add, 2, "
/" },