From 7ff5e2746b3955e50c92ffe61bc7390e6aecbfda Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 15 Jan 2013 22:10:09 -0500 Subject: Support devices with multiple IPv4 addresses Add a get_ipv4_addresses() method to ethtool.etherinfo to support devices with multiple IPv4 addresses (rhbz#759150) Previously, get_etherinfo() made queries to NETLINK with NLQRY_ADDR, and callback_nl_address handled responses of family AF_INET (IPv4) by writing to fields within a struct etherinfo. If multiple AF_INET responses come back, each overwrote the last, and the last one won. This patch generalizes things by moving the relevant fields: char *ipv4_address; /**< Configured IPv4 address */ int ipv4_netmask; /**< Configured IPv4 netmask */ char *ipv4_broadcast; from (struct etherinfo) into a new Python class, currently named PyNetlinkIPv4Address. This object has a sane repr(): >>> ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses() [ethtool.NetlinkIPv4Address(address='192.168.1.10', netmask=24, broadcast='192.168.1.255')] and attributes: >>> print [iface.address for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] ['192.168.1.10'] >>> print [iface.netmask for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] [24] >>> print [iface.broadcast for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] ['192.168.1.255'] The (struct etherinfo) then gains a new field: PyObject *ipv4_addresses; /**< list of PyNetlinkIPv4Address instances */ which is created before starting the query, and populated by the callback as responses come in. All direct usage of the old fields (which assumed a single IPv4 address) are changed to use the last entry in the list (if any), to mimic the old behavior. dump_etherinfo() and _ethtool_etherinfo_str() are changed to loop over all of the IPv4 addresses when outputting, rather than just outputting one. Caveats: * the exact terminology is probably incorrect: I'm not a networking specialist * the relationship between each of devices, get_interfaces_info() results, and addresses seems both unclear and messy to me: how changable is the API? >>> ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses() [ethtool.NetlinkIPv4Address(address='192.168.1.10', netmask=24, broadcast='192.168.1.255')] It seems that an etherinfo object relates to a device: perhaps it should be named as such? But it may be too late to make this change. Notes: The _ethtool_etherinfo_members array within python-ethtool/etherinfo_obj.c was broken: it defined 4 attributes of type PyObject*, to be extracted from etherinfo_py->data, which is of a completed different type. If these PyMemberDef fields were ever used, Python would segfault. Thankfully _ethtool_etherinfo_getter() has handlers for these attributes, and gets called first. This is a modified version of the patch applied downstream in RHEL 6.4 within python-ethtool-0.6-3.el6: python-ethtool-0.6-add-get_ipv4_addresses-method.patch ported to take account of 508ffffbb3c48eeeb11eeab2bf971180fe4e1940 --- python-ethtool/etherinfo_struct.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'python-ethtool/etherinfo_struct.h') diff --git a/python-ethtool/etherinfo_struct.h b/python-ethtool/etherinfo_struct.h index f294637..bcb692d 100644 --- a/python-ethtool/etherinfo_struct.h +++ b/python-ethtool/etherinfo_struct.h @@ -13,6 +13,8 @@ * General Public License for more details. */ +#include + /** * @file etherinfo_struct.h * @author David Sommerseth @@ -33,12 +35,18 @@ struct etherinfo { char *device; /**< Device name */ int index; /**< NETLINK index reference */ char *hwaddress; /**< HW address / MAC address of device */ - char *ipv4_address; /**< Configured IPv4 address */ - int ipv4_netmask; /**< Configured IPv4 netmask */ - char *ipv4_broadcast; /**< Configured IPv4 broadcast address */ + PyObject *ipv4_addresses; /**< list of PyNetlinkIPv4Address instances */ struct ipv6address *ipv6_addresses; /**< Configured IPv6 addresses (as a pointer chain) */ }; +/* Python object containing data baked from a (struct rtnl_addr) */ +typedef struct PyNetlinkIPv4Address { + PyObject_HEAD + PyObject *ipv4_address; /**< string: Configured IPv4 address */ + int ipv4_netmask; /**< int: Configured IPv4 netmask */ + PyObject *ipv4_broadcast; /**< string: Configured IPv4 broadcast address */ +} PyNetlinkIPv4Address; +extern PyTypeObject ethtool_netlink_ipv4_address_Type; /** * Pointer chain with IPv6 addresses associated with a ethernet interface. Used @@ -91,4 +99,9 @@ typedef struct { */ #define RETURN_STRING(str) (str ? PyString_FromString(str) : (Py_INCREF(Py_None), Py_None)) +PyObject * +make_python_address_from_rtnl_addr(struct nl_object *obj, + struct rtnl_addr *addr); + + #endif -- cgit