summaryrefslogtreecommitdiffstats
path: root/ethtool-cmd.py
blob: e828bce0f57f4aefb5e6ab54fbaa2e7cd49a139d (plain)
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
#! /usr/bin/python
# -*- python -*-
# -*- coding: utf-8 -*-
#   Copyright (C) 2008 Red Hat Inc.
#
#   This application 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; version 2.
#
#   This application 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.

import getopt, ethtool, sys

def usage():
	print '''Usage: ethtool [OPTIONS] [<interface>]
	-h|--help               Give this help list
	-i|--driver             Show driver information
	-k|--show-offload       Get protocol offload information
	-K|--offload            Set protocol offload
		[ tso on|off ]'''

tab = ""

def printtab(msg):
	print tab + msg

def show_offload(interface, args = None):
	try:
		sg = ethtool.get_sg(interface) and "on" or "off"
	except IOError:
		sg = "not supported"

	try:
		tso = ethtool.get_tso(interface) and "on" or "off"
	except IOError:
		tso = "not supported"

	try:
		ufo = ethtool.get_ufo(interface) and "on" or "off"
	except IOError:
		ufo = "not supported"

	try:
		gso = ethtool.get_gso(interface) and "on" or "off"
	except IOError:
		gso = "not supported"

	printtab("scatter-gather: %s" % sg)
	printtab("tcp segmentation offload: %s" % tso)
	printtab("udp fragmentation offload: %s" % ufo)
	printtab("generic segmentation offload: %s" % gso)

def set_offload(interface, args):
	cmd, value = [a.lower() for a in args]

	if cmd == "tso":
		value = value == "on" and 1 or 0
		try:
			ethtool.set_tso(interface, value)
		except:
			pass

def show_driver(interface, args = None):
	try:
		driver = ethtool.get_module(interface)
	except IOError:
		driver = "not implemented"

	try:
		bus = ethtool.get_businfo(interface)
	except IOError:
		bus = "not available"

	printtab("driver: %s" % driver)
	printtab("bus-info: %s" % bus)

def run_cmd(cmd, interface, args):
	global tab

	active_devices = ethtool.get_active_devices()
	if not interface:
		tab = "  "
		for interface in ethtool.get_devices():
			inactive = " (not active)"
			if interface in active_devices:
				inactive = ""
			print "%s%s:" % (interface, inactive)
			cmd(interface, args)
	else:
		cmd(interface, args)

def run_cmd_noargs(cmd, args):
	if args:
		run_cmd(cmd, args[0], None)
	else:
		run_cmd(cmd, None, None)

def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:],
					   "hikK",
					   ("help",
					    "driver",
					    "show-offload",
					    "offload"))
	except getopt.GetoptError, err:
		usage()
		print str(err)
		sys.exit(2)

	if not opts:
		usage()
		sys.exit(0)

	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			return
		elif o in ("-i", "--driver"):
			run_cmd_noargs(show_driver, args)
			break
		elif o in ("-k", "--show-offload"):
			run_cmd_noargs(show_offload, args)
			break
		elif o in ("-K", "--offload"):
			if len(args) == 2:
				interface = None
			elif len(args) == 3:
				interface = args[0]
				args = args[1:]
			else:
				usage()
				sys.exit(1)
			run_cmd(set_offload, interface, args)
			break

if __name__ == '__main__':
    main()