From: Jiri Pirko jiri@mellanox.com
Use pyroute2 to get list of devlink ports.
Signed-off-by: Jiri Pirko jiri@mellanox.com --- v1->v2: - Fixed self._devices loop - Added devlink info update to handle_netlink_msgs --- lnst/Slave/DevlinkManager.py | 63 ++++++++++++++++++++++++++++++++++++++++++ lnst/Slave/InterfaceManager.py | 20 +++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 lnst/Slave/DevlinkManager.py
diff --git a/lnst/Slave/DevlinkManager.py b/lnst/Slave/DevlinkManager.py new file mode 100644 index 0000000..13662bc --- /dev/null +++ b/lnst/Slave/DevlinkManager.py @@ -0,0 +1,63 @@ +""" +This module defines the DevlinkManager class that contains a database of +the available devlink devices, handles netlink messages updating these devices. + +Copyright 2016 Mellanox Technologies. All rights reserved. +Licensed under the GNU General Public License, version 2 as +published by the Free Software Foundation; see COPYING for details. +""" + +__author__ = """ +jiri@mellanox.com (Jiri Pirko) +""" + +try: + from pyroute2 import DL + from pyroute2.netlink.exceptions import NetlinkError + + def dl_open(): + try: + return DL() + except NetlinkError: + return None + +except ImportError: + class DL(object): + def port_list(self): + return [] + + def close(self): + return + + def dl_open(): + return DL() + +class DevlinkManager(object): + def __init__(self): + self.rescan_ports() + + def rescan_ports(self): + self._ports = [] + + dl = dl_open() + if not dl: + return + + try: + for q in dl.port_list(): + dl_port = {} + dl_port["bus_name"] = q.get_attr('DEVLINK_ATTR_BUS_NAME') + dl_port["dev_name"] = q.get_attr('DEVLINK_ATTR_DEV_NAME') + dl_port["port_index"] = q.get_attr('DEVLINK_ATTR_PORT_INDEX') + dl_port["port_netdev_name"] = q.get_attr('DEVLINK_ATTR_PORT_NETDEV_NAME') + self._ports.append(dl_port) + except: + raise + finally: + dl.close() + + def get_port(self, ifname): + for dl_port in self._ports: + if dl_port["port_netdev_name"] == ifname: + return dl_port + return None diff --git a/lnst/Slave/InterfaceManager.py b/lnst/Slave/InterfaceManager.py index 5852254..4fb0919 100644 --- a/lnst/Slave/InterfaceManager.py +++ b/lnst/Slave/InterfaceManager.py @@ -21,6 +21,7 @@ from lnst.Common.NetUtils import normalize_hwaddr from lnst.Common.NetUtils import scan_netdevs from lnst.Common.ExecCmd import exec_cmd from lnst.Common.ConnectionHandler import recv_data +from lnst.Slave.DevlinkManager import DevlinkManager from pyroute2 import IPRSocket from pyroute2.netlink.rtnl import RTNLGRP_IPV4_IFADDR from pyroute2.netlink.rtnl import RTNLGRP_IPV6_IFADDR @@ -50,6 +51,8 @@ class InterfaceManager(object): self._nl_socket = IPRSocket() self._nl_socket.bind(groups=NL_GROUPS)
+ self._dl_manager = DevlinkManager() + self.rescan_devices()
self._server_handler = server_handler @@ -129,9 +132,19 @@ class InterfaceManager(object): self._server_handler.send_data_to_ctl(del_msg) del self._devices[i]
+ self._dl_manager.rescan_ports() + for device in self._devices.values(): + dl_port = self._dl_manager.get_port(device.get_name()) + device.set_devlink(dl_port) + def handle_netlink_msgs(self, msgs): for msg in msgs: self._handle_netlink_msg(msg) + + self._dl_manager.rescan_ports() + for device in self._devices.values(): + dl_port = self._dl_manager.get_port(device.get_name()) + device.set_devlink(dl_port)
def _handle_netlink_msg(self, msg): if msg['header']['type'] in [RTM_NEWLINK, RTM_NEWADDR, RTM_DELADDR]: @@ -354,9 +367,13 @@ class Device(object): self._peer = None self._mtu = None self._driver = None + self._devlink = None
self._if_manager = if_manager
+ def set_devlink(self, devlink_port_data): + self._devlink = devlink_port_data + def init_netlink(self, nl_msg): self._if_index = nl_msg['index'] self._ifi_type = nl_msg['ifi_type'] @@ -672,7 +689,8 @@ class Device(object): "netns": self._netns, "peer": self._peer.get_if_index() if self._peer else None, "mtu": self._mtu, - "driver": self._driver} + "driver": self._driver, + "devlink": self._devlink} return if_data
def set_speed(self, speed):
From: Jiri Pirko jiri@mellanox.com
Allow tasks to get devlink and devlink port name.
Signed-off-by: Jiri Pirko jiri@mellanox.com --- v1->v2: - no change --- lnst/Controller/Machine.py | 30 ++++++++++++++++++++++++++++++ lnst/Controller/Task.py | 12 ++++++++++++ 2 files changed, 42 insertions(+)
diff --git a/lnst/Controller/Machine.py b/lnst/Controller/Machine.py index 3e730bd..3725de6 100644 --- a/lnst/Controller/Machine.py +++ b/lnst/Controller/Machine.py @@ -606,6 +606,7 @@ class Interface(object): self._peer = None self._mtu = None self._driver = None + self._devlink = None
def get_id(self): return self._id @@ -779,6 +780,7 @@ class Interface(object): self.set_devname(if_data["name"]) self._mtu = if_data["mtu"] self._driver = if_data["driver"] + self._devlink = if_data["devlink"]
def get_config(self): config = {"id": self._id, @@ -921,6 +923,18 @@ class Interface(object): self.del_slave(self._machine.get_interface(if_id)) self._machine._rpc_call_x(self._netns, "slave_del", self._id, if_id)
+ def get_devlink_name(self): + if self._devlink: + return "%s/%s" % (self._devlink["bus_name"], + self._devlink["dev_name"]) + return None + + def get_devlink_port_name(self): + if self._devlink: + return "%s/%u" % (self.get_devlink_name(), + self._devlink["port_index"]) + return None + class StaticInterface(Interface): """ Static interface
@@ -1200,6 +1214,7 @@ class Device(object): self._peer = None self._mtu = None self._driver = None + self._devlink = None
self._machine = machine
@@ -1220,6 +1235,7 @@ class Device(object): self._peer = data["peer"] self._mtu = data["mtu"] self._driver = data["driver"] + self._devlink = data["driver"] return True
def slave_update(self): @@ -1298,3 +1314,17 @@ class Device(object): @pre_call_decorate def get_driver(self): return self._driver + + @pre_call_decorate + def get_devlink_name(self): + if self._devlink: + return "%s/%s" % (self._devlink["bus_name"], + self._devlink["dev_name"]) + return None + + @pre_call_decorate + def get_devlink_port_name(self): + if self._devlink: + return "%s/%u" % (self.get_devlink_name(), + self._devlink["port_index"]) + return None diff --git a/lnst/Controller/Task.py b/lnst/Controller/Task.py index 178d774..6f36e86 100644 --- a/lnst/Controller/Task.py +++ b/lnst/Controller/Task.py @@ -532,6 +532,12 @@ class DeviceAPI(object): def get_driver(self): return self._dev.get_driver()
+ def get_devlink_name(self): + return self._dev.get_devlink_name() + + def get_devlink_port_name(self): + return self._dev.get_devlink_port_name() + class InterfaceAPI(object): def __init__(self, interface, host): self._if = interface @@ -673,6 +679,12 @@ class InterfaceAPI(object): def slave_del(self, slave_id): return self._if.slave_del(slave_id)
+ def get_devlink_name(self): + return self._if.get_devlink_name() + + def get_devlink_port_name(self): + return self._if.get_devlink_port_name() + class ModuleAPI(object): """ An API class representing a module. """
On Fri, Jul 15, 2016 at 02:06:36PM +0200, Jiri Pirko wrote:
From: Jiri Pirko jiri@mellanox.com
Use pyroute2 to get list of devlink ports.
Signed-off-by: Jiri Pirko jiri@mellanox.com
v1->v2:
- Fixed self._devices loop
- Added devlink info update to handle_netlink_msgs
lnst/Slave/DevlinkManager.py | 63 ++++++++++++++++++++++++++++++++++++++++++ lnst/Slave/InterfaceManager.py | 20 +++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 lnst/Slave/DevlinkManager.py
diff --git a/lnst/Slave/DevlinkManager.py b/lnst/Slave/DevlinkManager.py new file mode 100644 index 0000000..13662bc --- /dev/null +++ b/lnst/Slave/DevlinkManager.py @@ -0,0 +1,63 @@ +""" +This module defines the DevlinkManager class that contains a database of +the available devlink devices, handles netlink messages updating these devices.
+Copyright 2016 Mellanox Technologies. All rights reserved. +Licensed under the GNU General Public License, version 2 as +published by the Free Software Foundation; see COPYING for details. +"""
+__author__ = """ +jiri@mellanox.com (Jiri Pirko) +"""
+try:
- from pyroute2 import DL
- from pyroute2.netlink.exceptions import NetlinkError
- def dl_open():
try:
return DL()
except NetlinkError:
return None
+except ImportError:
- class DL(object):
def port_list(self):
return []
def close(self):
return
- def dl_open():
return DL()
+class DevlinkManager(object):
- def __init__(self):
self.rescan_ports()
- def rescan_ports(self):
self._ports = []
dl = dl_open()
if not dl:
return
try:
for q in dl.port_list():
dl_port = {}
dl_port["bus_name"] = q.get_attr('DEVLINK_ATTR_BUS_NAME')
dl_port["dev_name"] = q.get_attr('DEVLINK_ATTR_DEV_NAME')
dl_port["port_index"] = q.get_attr('DEVLINK_ATTR_PORT_INDEX')
dl_port["port_netdev_name"] = q.get_attr('DEVLINK_ATTR_PORT_NETDEV_NAME')
self._ports.append(dl_port)
except:
raise
finally:
dl.close()
- def get_port(self, ifname):
for dl_port in self._ports:
if dl_port["port_netdev_name"] == ifname:
return dl_port
return None
diff --git a/lnst/Slave/InterfaceManager.py b/lnst/Slave/InterfaceManager.py index 5852254..4fb0919 100644 --- a/lnst/Slave/InterfaceManager.py +++ b/lnst/Slave/InterfaceManager.py @@ -21,6 +21,7 @@ from lnst.Common.NetUtils import normalize_hwaddr from lnst.Common.NetUtils import scan_netdevs from lnst.Common.ExecCmd import exec_cmd from lnst.Common.ConnectionHandler import recv_data +from lnst.Slave.DevlinkManager import DevlinkManager from pyroute2 import IPRSocket from pyroute2.netlink.rtnl import RTNLGRP_IPV4_IFADDR from pyroute2.netlink.rtnl import RTNLGRP_IPV6_IFADDR @@ -50,6 +51,8 @@ class InterfaceManager(object): self._nl_socket = IPRSocket() self._nl_socket.bind(groups=NL_GROUPS)
self._dl_manager = DevlinkManager()
self.rescan_devices() self._server_handler = server_handler
@@ -129,9 +132,19 @@ class InterfaceManager(object): self._server_handler.send_data_to_ctl(del_msg) del self._devices[i]
self._dl_manager.rescan_ports()
for device in self._devices.values():
dl_port = self._dl_manager.get_port(device.get_name())
device.set_devlink(dl_port)
- def handle_netlink_msgs(self, msgs): for msg in msgs: self._handle_netlink_msg(msg)
There's trailing whatespaces in the line above. But you can fix that when pushing.
self._dl_manager.rescan_ports()
for device in self._devices.values():
dl_port = self._dl_manager.get_port(device.get_name())
device.set_devlink(dl_port)
def _handle_netlink_msg(self, msg): if msg['header']['type'] in [RTM_NEWLINK, RTM_NEWADDR, RTM_DELADDR]:
@@ -354,9 +367,13 @@ class Device(object): self._peer = None self._mtu = None self._driver = None
self._devlink = None self._if_manager = if_manager
def set_devlink(self, devlink_port_data):
self._devlink = devlink_port_data
def init_netlink(self, nl_msg): self._if_index = nl_msg['index'] self._ifi_type = nl_msg['ifi_type']
@@ -672,7 +689,8 @@ class Device(object): "netns": self._netns, "peer": self._peer.get_if_index() if self._peer else None, "mtu": self._mtu,
"driver": self._driver}
"driver": self._driver,
"devlink": self._devlink} return if_data
def set_speed(self, speed):
-- 2.5.5 _______________________________________________ LNST-developers mailing list lnst-developers@lists.fedorahosted.org https://lists.fedorahosted.org/admin/lists/lnst-developers@lists.fedorahoste...
Everything else looks good to me. Ack for pushing.
Acked-by: Ondrej Lichtner olichtne@redhat.com
Tue, Jul 19, 2016 at 10:46:53AM CEST, olichtne@redhat.com wrote:
On Fri, Jul 15, 2016 at 02:06:36PM +0200, Jiri Pirko wrote:
From: Jiri Pirko jiri@mellanox.com
Use pyroute2 to get list of devlink ports.
Signed-off-by: Jiri Pirko jiri@mellanox.com
v1->v2:
- Fixed self._devices loop
- Added devlink info update to handle_netlink_msgs
lnst/Slave/DevlinkManager.py | 63 ++++++++++++++++++++++++++++++++++++++++++ lnst/Slave/InterfaceManager.py | 20 +++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 lnst/Slave/DevlinkManager.py
diff --git a/lnst/Slave/DevlinkManager.py b/lnst/Slave/DevlinkManager.py new file mode 100644 index 0000000..13662bc --- /dev/null +++ b/lnst/Slave/DevlinkManager.py @@ -0,0 +1,63 @@ +""" +This module defines the DevlinkManager class that contains a database of +the available devlink devices, handles netlink messages updating these devices.
+Copyright 2016 Mellanox Technologies. All rights reserved. +Licensed under the GNU General Public License, version 2 as +published by the Free Software Foundation; see COPYING for details. +"""
+__author__ = """ +jiri@mellanox.com (Jiri Pirko) +"""
+try:
- from pyroute2 import DL
- from pyroute2.netlink.exceptions import NetlinkError
- def dl_open():
try:
return DL()
except NetlinkError:
return None
+except ImportError:
- class DL(object):
def port_list(self):
return []
def close(self):
return
- def dl_open():
return DL()
+class DevlinkManager(object):
- def __init__(self):
self.rescan_ports()
- def rescan_ports(self):
self._ports = []
dl = dl_open()
if not dl:
return
try:
for q in dl.port_list():
dl_port = {}
dl_port["bus_name"] = q.get_attr('DEVLINK_ATTR_BUS_NAME')
dl_port["dev_name"] = q.get_attr('DEVLINK_ATTR_DEV_NAME')
dl_port["port_index"] = q.get_attr('DEVLINK_ATTR_PORT_INDEX')
dl_port["port_netdev_name"] = q.get_attr('DEVLINK_ATTR_PORT_NETDEV_NAME')
self._ports.append(dl_port)
except:
raise
finally:
dl.close()
- def get_port(self, ifname):
for dl_port in self._ports:
if dl_port["port_netdev_name"] == ifname:
return dl_port
return None
diff --git a/lnst/Slave/InterfaceManager.py b/lnst/Slave/InterfaceManager.py index 5852254..4fb0919 100644 --- a/lnst/Slave/InterfaceManager.py +++ b/lnst/Slave/InterfaceManager.py @@ -21,6 +21,7 @@ from lnst.Common.NetUtils import normalize_hwaddr from lnst.Common.NetUtils import scan_netdevs from lnst.Common.ExecCmd import exec_cmd from lnst.Common.ConnectionHandler import recv_data +from lnst.Slave.DevlinkManager import DevlinkManager from pyroute2 import IPRSocket from pyroute2.netlink.rtnl import RTNLGRP_IPV4_IFADDR from pyroute2.netlink.rtnl import RTNLGRP_IPV6_IFADDR @@ -50,6 +51,8 @@ class InterfaceManager(object): self._nl_socket = IPRSocket() self._nl_socket.bind(groups=NL_GROUPS)
self._dl_manager = DevlinkManager()
self.rescan_devices() self._server_handler = server_handler
@@ -129,9 +132,19 @@ class InterfaceManager(object): self._server_handler.send_data_to_ctl(del_msg) del self._devices[i]
self._dl_manager.rescan_ports()
for device in self._devices.values():
dl_port = self._dl_manager.get_port(device.get_name())
device.set_devlink(dl_port)
- def handle_netlink_msgs(self, msgs): for msg in msgs: self._handle_netlink_msg(msg)
There's trailing whatespaces in the line above. But you can fix that when pushing.
Will do. Thanks.
self._dl_manager.rescan_ports()
for device in self._devices.values():
dl_port = self._dl_manager.get_port(device.get_name())
device.set_devlink(dl_port)
def _handle_netlink_msg(self, msg): if msg['header']['type'] in [RTM_NEWLINK, RTM_NEWADDR, RTM_DELADDR]:
@@ -354,9 +367,13 @@ class Device(object): self._peer = None self._mtu = None self._driver = None
self._devlink = None self._if_manager = if_manager
def set_devlink(self, devlink_port_data):
self._devlink = devlink_port_data
def init_netlink(self, nl_msg): self._if_index = nl_msg['index'] self._ifi_type = nl_msg['ifi_type']
@@ -672,7 +689,8 @@ class Device(object): "netns": self._netns, "peer": self._peer.get_if_index() if self._peer else None, "mtu": self._mtu,
"driver": self._driver}
"driver": self._driver,
"devlink": self._devlink} return if_data
def set_speed(self, speed):
-- 2.5.5 _______________________________________________ LNST-developers mailing list lnst-developers@lists.fedorahosted.org https://lists.fedorahosted.org/admin/lists/lnst-developers@lists.fedorahoste...
Everything else looks good to me. Ack for pushing.
Acked-by: Ondrej Lichtner olichtne@redhat.com
lnst-developers@lists.fedorahosted.org