Skip to content

Commit 13ec0f5

Browse files
committed
pethtool: Add python ethtool equivalent to test features
It should be as close to the ethtool usage model as possible, but things like doing the same command on all available interfaces when no interface is specified is implemented. Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 401e728 commit 13ec0f5

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

ethtool-cmd.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#! /usr/bin/python
2+
# -*- python -*-
3+
# -*- coding: utf-8 -*-
4+
# Copyright (C) 2008 Red Hat Inc.
5+
#
6+
# This application is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU General Public License
8+
# as published by the Free Software Foundation; version 2.
9+
#
10+
# This application is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# General Public License for more details.
14+
15+
import getopt, ethtool, sys
16+
17+
def usage():
18+
print '''Usage: ethtool [OPTIONS] [<interface>]
19+
-h|--help Give this help list
20+
-i|--driver Show driver information
21+
-k|--show-offload Get protocol offload information
22+
-K|--offload Set protocol offload
23+
[ tso on|off ]'''
24+
25+
tab = ""
26+
27+
def printtab(msg):
28+
print tab + msg
29+
30+
def show_offload(interface, args = None):
31+
try:
32+
sg = ethtool.get_sg(interface) and "on" or "off"
33+
except IOError:
34+
sg = "not supported"
35+
36+
try:
37+
tso = ethtool.get_tso(interface) and "on" or "off"
38+
except IOError:
39+
tso = "not supported"
40+
41+
try:
42+
ufo = ethtool.get_ufo(interface) and "on" or "off"
43+
except IOError:
44+
ufo = "not supported"
45+
46+
try:
47+
gso = ethtool.get_gso(interface) and "on" or "off"
48+
except IOError:
49+
gso = "not supported"
50+
51+
printtab("scatter-gather: %s" % sg)
52+
printtab("tcp segmentation offload: %s" % tso)
53+
printtab("udp fragmentation offload: %s" % ufo)
54+
printtab("generic segmentation offload: %s" % gso)
55+
56+
def set_offload(interface, args):
57+
cmd, value = [a.lower() for a in args]
58+
59+
if cmd == "tso":
60+
value = value == "on" and 1 or 0
61+
try:
62+
ethtool.set_tso(interface, value)
63+
except:
64+
pass
65+
66+
def show_driver(interface, args = None):
67+
try:
68+
driver = ethtool.get_module(interface)
69+
except IOError:
70+
driver = "not implemented"
71+
72+
try:
73+
bus = ethtool.get_businfo(interface)
74+
except IOError:
75+
bus = "not available"
76+
77+
printtab("driver: %s" % driver)
78+
printtab("bus-info: %s" % bus)
79+
80+
def run_cmd(cmd, interface, args):
81+
global tab
82+
if not interface:
83+
tab = " "
84+
for interface in ethtool.get_devices():
85+
print "%s:" % interface
86+
cmd(interface, args)
87+
else:
88+
cmd(interface, args)
89+
90+
def run_cmd_noargs(cmd, args):
91+
if args:
92+
run_cmd(cmd, args[0], None)
93+
else:
94+
run_cmd(cmd, None, None)
95+
96+
def main():
97+
try:
98+
opts, args = getopt.getopt(sys.argv[1:],
99+
"hikK",
100+
("help",
101+
"driver",
102+
"show-offload",
103+
"offload"))
104+
except getopt.GetoptError, err:
105+
usage()
106+
print str(err)
107+
sys.exit(2)
108+
109+
if not opts:
110+
usage()
111+
sys.exit(0)
112+
113+
for o, a in opts:
114+
if o in ("-h", "--help"):
115+
usage()
116+
return
117+
elif o in ("-i", "--driver"):
118+
run_cmd_noargs(show_driver, args)
119+
break
120+
elif o in ("-k", "--show-offload"):
121+
run_cmd_noargs(show_offload, args)
122+
break
123+
elif o in ("-K", "--offload"):
124+
if len(args) == 2:
125+
interface = None
126+
elif len(args) == 3:
127+
interface = args[0]
128+
args = args[1:]
129+
else:
130+
usage()
131+
sys.exit(1)
132+
run_cmd(set_offload, interface, args)
133+
break
134+
135+
if __name__ == '__main__':
136+
main()

rpm/SPECS/python-ethtool.spec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ ethtool python bindings
1919
%install
2020
rm -rf %{buildroot}
2121
make DESTDIR=%{buildroot} install
22+
mkdir -p %{buildroot}%{_sbindir}
23+
cp -f ethtool-cmd.py %{buildroot}%{_sbindir}/pethtool
2224

2325
%clean
2426
rm -rf %{buildroot}
2527

2628
%files
2729
%defattr(-,root,root)
2830
%doc COPYING
31+
%{_sbindir}/pethtool
2932
%{python_sitearch}/ethtool.so
3033

3134
%changelog

0 commit comments

Comments
 (0)