forked from kimchi-project/kimchi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetinfo.py
266 lines (193 loc) · 7.12 KB
/
netinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Project Kimchi
#
# Copyright IBM, Corp. 2013-2015
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
import ethtool
import glob
import os
from distutils.spawn import find_executable
from wok.utils import run_command
from wok.utils import wok_log
NET_PATH = '/sys/class/net'
NIC_PATH = '/sys/class/net/*/device'
BRIDGE_PATH = '/sys/class/net/*/bridge'
BONDING_PATH = '/sys/class/net/*/bonding'
WLAN_PATH = '/sys/class/net/*/wireless'
NET_BRPORT = '/sys/class/net/%s/brport'
NET_MASTER = '/sys/class/net/%s/master'
NET_STATE = '/sys/class/net/%s/carrier'
PROC_NET_VLAN = '/proc/net/vlan/'
BONDING_SLAVES = '/sys/class/net/%s/bonding/slaves'
BRIDGE_PORTS = '/sys/class/net/%s/brif'
def wlans():
return [b.split('/')[-2] for b in glob.glob(WLAN_PATH)]
def is_wlan(iface):
return iface in wlans()
# FIXME if we do not want to list usb nic
def nics():
return list(set([b.split('/')[-2] for b in glob.glob(NIC_PATH)]) -
set(wlans()))
def is_nic(iface):
return iface in nics()
def bondings():
return [b.split('/')[-2] for b in glob.glob(BONDING_PATH)]
def is_bonding(iface):
return iface in bondings()
def vlans():
return list(set([b.split('/')[-1]
for b in glob.glob(NET_PATH + '/*')]) &
set([b.split('/')[-1]
for b in glob.glob(PROC_NET_VLAN + '*')]))
def is_vlan(iface):
return iface in vlans()
def bridges():
return list(set([b.split('/')[-2] for b in glob.glob(BRIDGE_PATH)] +
ovs_bridges()))
def is_bridge(iface):
return iface in bridges()
# In some distributions, like Fedora, the files bridge and brif are not created
# under /sys/class/net/<ovsbridge> for OVS bridges. These specific functions
# allows one to differentiate OVS bridges from other types of bridges.
def ovs_bridges():
ovs_cmd = find_executable("ovs-vsctl")
# openvswitch not installed: there is no OVS bridge configured
if ovs_cmd is None:
return []
out, error, rc = run_command([ovs_cmd, 'list-br'])
if rc != 0:
wok_log.info("Error listing OVS bridges")
return []
return [x.strip() for x in out.rstrip('\n').split('\n') if x.strip()]
def is_ovs_bridge(iface):
return iface in ovs_bridges()
def ovs_bridge_ports(ovsbr):
ovs_cmd = find_executable("ovs-vsctl")
# openvswitch not installed: there is no OVS bridge configured
if ovs_cmd is None:
return []
out, error, rc = run_command([ovs_cmd, 'list-ports', ovsbr])
if rc != 0:
wok_log.info("Error listing OVS bridge ports for %s" % str(ovsbr))
return []
return [x.strip() for x in out.rstrip('\n').split('\n') if x.strip()]
def all_interfaces():
return [d.rsplit("/", 1)[-1] for d in glob.glob(NET_PATH + '/*')]
def slaves(bonding):
with open(BONDING_SLAVES % bonding) as bonding_file:
res = bonding_file.readline().split()
return res
def ports(bridge):
if bridge in ovs_bridges():
return ovs_bridge_ports(bridge)
return os.listdir(BRIDGE_PORTS % bridge)
def is_brport(nic):
ovs_brports = []
for ovsbr in ovs_bridges():
ovs_brports += ovs_bridge_ports(ovsbr)
return os.path.exists(NET_BRPORT % nic) or nic in ovs_brports
def is_bondlave(nic):
return os.path.exists(NET_MASTER % nic)
def operstate(dev):
link_status = link_detected(dev)
return "down" if link_status == "n/a" else "up"
def link_detected(dev):
# try to read interface carrier (link) status
try:
with open(NET_STATE % dev) as dev_file:
carrier = dev_file.readline().strip()
# when IOError is raised, interface is down
except IOError:
return "n/a"
# if value is 1, interface up with cable connected
# 0 corresponds to interface up with cable disconnected
return "yes" if carrier == '1' else "no"
def get_vlan_device(vlan):
""" Return the device of the given VLAN. """
dev = None
if os.path.exists(PROC_NET_VLAN + vlan):
with open(PROC_NET_VLAN + vlan) as vlan_file:
for line in vlan_file:
if "Device:" in line:
dummy, dev = line.split()
break
return dev
def get_bridge_port_device(bridge):
"""Return the nics list that belongs to bridge."""
# br --- v --- bond --- nic1
if bridge not in bridges():
raise ValueError('unknown bridge %s' % bridge)
nics = []
for port in ports(bridge):
if port in vlans():
device = get_vlan_device(port)
if device in bondings():
nics.extend(slaves(device))
else:
nics.append(device)
if port in bondings():
nics.extend(slaves(port))
else:
nics.append(port)
return nics
def aggregated_bridges():
return [bridge for bridge in bridges() if
(set(get_bridge_port_device(bridge)) & set(nics()))]
def bare_nics():
"The nic is not a port of a bridge or a slave of bond."
return [nic for nic in nics() if not (is_brport(nic) or is_bondlave(nic))]
def is_bare_nic(iface):
return iface in bare_nics()
# The nic will not be exposed when it is a port of a bridge or
# a slave of bond.
# The bridge will not be exposed when all it's port are tap.
def all_favored_interfaces():
return list(set(aggregated_bridges() + bare_nics() + bondings()) -
set(ovs_bridges()))
def get_interface_type(iface):
# FIXME if we want to get more device type
# just support nic, bridge, bondings and vlan, for we just
# want to expose this 4 kinds of interface
try:
if is_nic(iface):
return "nic"
if is_bonding(iface):
return "bonding"
if is_bridge(iface):
return "bridge"
if is_vlan(iface):
return "vlan"
return 'unknown'
except IOError:
return 'unknown'
def get_interface_info(iface):
if iface not in ethtool.get_devices():
raise ValueError('unknown interface: %s' % iface)
ipaddr = ''
netmask = ''
try:
ipaddr = ethtool.get_ipaddr(iface)
netmask = ethtool.get_netmask(iface)
except IOError:
pass
iface_link_detected = link_detected(iface)
iface_status = 'active' if iface_link_detected != "n/a" else "inactive"
return {'name': iface,
'type': get_interface_type(iface),
'status': iface_status,
'link_detected': iface_link_detected,
'ipaddr': ipaddr,
'netmask': netmask}