-
Notifications
You must be signed in to change notification settings - Fork 11
/
configure_network_adapter.sh
410 lines (337 loc) · 11.6 KB
/
configure_network_adapter.sh
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/bin/ksh
#
# script to set the speed of a network adapter
#
# Author: Bernd Schemmer
#
# History:
#
# 1.0.9 31.03.2008
# - added support for e1000gx driver
#
# 1.0.8 04.01.2008
# - added support for new GLDv3 driver
# - added parameter createcmds
#
# 1.0.7 06.11.2007
# - added workaround for bug 6548250 for nxge driver
#
# 1.0.6 02.11.2007
# - added support for nxge adapters
#
# 1.0.5 15.12.2005
# - added suport for bge adapters
#
# 1.0.4 28.02.2005
# - change order of ndd commands
# - make code more flexible
#
# 1.0.3 10.02.2005
# - added check for executing user
#
# 1.0.2 17.11.2004
# - added support for adapters no 10 to 99
#
# 1.0.1 27.06.2004
# - minor bug changes
#
# 1.0.0, 01/10/2004
# - initial version
#
# Usage: ksh configure_network_adapter adapter adapter_speed
#
# Where: Adapter is the network adapter (e.g. hme0, ce0, etc)
#
# Adapter speed is the adapter speed:
#
# 100HD 100FD 1000HD 1000FD 10FD 10HD AUTO 10000 10G
#
# This is a ksh script!
#
NDD_KEYS=""
my_echo() {
if [ "${COMMAND}"x = "createcmds"x ] ; then
typeset THISMSG="# $*"
if [ "${OUTPUTFILE}"x != ""x ] ; then
echo "${THISMSG}" >>"${OUTPUTFILE}"
else
echo "${THISMSG}"
fi
else
echo "${THISMSG}"
fi
}
my_echo1() {
if [ "${COMMAND}"x = "createcmds"x ] ; then
typeset THISMSG="$*"
if [ "${OUTPUTFILE}"x != ""x ] ; then
echo "${THISMSG}" >>"${OUTPUTFILE}"
else
echo "${THISMSG}"
fi
fi
}
my_ndd() {
typeset THIS_CMD=$1
typeset THIS_ADAPTER=$2
typeset THIS_KEY=$3
typeset THIS_VAL=$4
# workaround for bug 6548250 for nxge driver
#
if [ "${NDD_KEYS}"x = ""x ] ; then
for i in $( ndd -get ${THIS_ADAPTER} \? | grep "read and write" | cut -f1 -d " " ) ; do
NDD_KEYS="${NDD_KEYS} ${i} "
done
fi
if [[ ${NDD_KEYS} == *\ ${THIS_KEY}\ * ]] ; then
if [ "${COMMAND}"x != "createcmds"x ] ; then
echo "Setting \"${THIS_KEY}\" \"${THIS_VAL}\" ..."
ndd -set ${THIS_ADAPTER} ${THIS_KEY} ${THIS_VAL}
else
if [ "${OUTPUTFILE}"x != ""x ] ; then
echo ndd -set ${THIS_ADAPTER} ${THIS_KEY} ${THIS_VAL} >>"${OUTPUTFILE}"
else
echo ndd -set ${THIS_ADAPTER} ${THIS_KEY} ${THIS_VAL}
fi
fi
fi
return $?
}
#
# usage: set_all_except_one [adv_xx] {0|1}
#
set_all_except_one() {
typeset NEW_MODE="$1"
typeset NEW_VALUE="$2"
[ "${NEW_VALUE}"x = ""x ] && NEW_VALUE=0
typeset CUR_KEY=""
for CUR_KEY in adv_10gfdx_cap adv_10gfdx_cap \
adv_100hdx_cap adv_100fdx_cap adv_100T4_cap \
adv_1000fdx_cap adv_1000hdx_cap \
adv_10fdx_cap adv_10hdx_cap
do
if [ "${NEW_MODE}"x != "${CUR_KEY}"x ] ; then
my_ndd -set ${ADAPTER_NAME_FOR_NDD} ${CUR_KEY} ${NEW_VALUE}
fi
done
}
toggle_autoneg() {
if [ $1 = 0 ] ; then
# toggle the adv_autoneg_cap and set it to 0
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 0
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 1
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 0
else
# toggle the adv_autoneg_cap and set it to 1
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 1
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 0
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 1
fi
}
SCRIPT_VERSION="v1.0.9"
ADAPTER=$1
ADAPTER_SPEED=$2
COMMAND="$3"
OUTPUTFILE="$4"
SCRIPT_NAME="${0##*/}"
case "${COMMAND}" in
"createcmds" )
: known command
;;
"" )
: no command
;;
* )
: invalid command
echo "${SCRIPT_NAME} - ERROR: Invalid command found \"${COMMAND}\" "
SHOW_USAGE=0
;;
esac
if [ "${ADAPTER}"x = ""x -o "${ADAPTER}"x = "-h"x -o "${SHOW_USAGE}"x = "0"x ] ; then
echo "${SCRIPT_NAME} ${SCRIPT_VERSION} - configure a network adapter"
echo ""
echo "Usage: ${SCRIPT_NAME} adapter adapter_speed [createcmds [outputfile]]"
echo ""
echo "Possible values for adapter_speed:"
echo ""
echo "Value means"
echo "-----------------------------------------------------------------"
echo "100fd, 100FD, 100FullDuplex - 100 Mbit FullDuplex"
echo "100hd, 100HD, 100HalfDuplex - 100 Mbit HalfDuplex"
echo "1000fd, 10000FD, 1000FullDuplex - 1000 Mbit FullDuplex"
echo "1000hd, 1000HD, 1000HalfDuplex - 1000 Mbit HalfDuplex"
echo "10fd, 10FD, 10HFullDuplex - 10 Mbit FullDuplex"
echo "10hd, 10HD, 10HalfDuplex - 10 Mbit HalfDuplex"
echo "10000, 10G - 10 Gigabit"
echo "auto, AUTO - use Autonegotiation"
echo ""
echo "createcmds : Save the commands in the file \"outpufile\" - do not change "
echo " the adapter configuration; default output file is STDOUT."
exit 99
fi
if [ "$( id | sed 's/uid=\([0-9]*\)(.*/\1/' )" != 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: You must be root to execute this script!"
exit 5
fi
if [ "${ADAPTER}"x != ""x -a "${ADAPTER_SPEED}"x != ""x ] ; then
# ADAPTER_NAME=${ADAPTER%%[0-9]*}
# ADAPTER_NUMBER=${ADAPTER##*[a-z]}
ADAPTER_NAME=${ADAPTER%[0-9]*}
ADAPTER_NUMBER=${ADAPTER##*[a-z]}
# echo \"${ADAPTER_NAME}\"
# echo \"${ADAPTER_NUMBER}\"
# echo "${ADAPTER_NAME} ${ADAPTER_NUMBER}"
INVALID_ADAPTER=1
[ "${ADAPTER_NUMBER}"x = ""x ] && INVALID_ADAPTER=0
[ "${ADAPTER_NAME}"x = ""x ] && INVALID_ADAPTER=0
[ "${ADAPTER_NUMBER}"x = "${ADAPTER}"x ] && INVALID_ADAPTER=0
[ "${ADAPTER_NAME}"x = "${ADAPTER}"x ] && INVALID_ADAPTER=0
if [ ${INVALID_ADAPTER} = 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: Invalid adapter parameter: \"${ADAPTER}\" "
exit 1
fi
# some adapter support a different syntax for ndd
#
case ${ADAPTER_NAME} in
bge )
ADAPTER_NAME_FOR_NDD="/dev/${ADAPTER_NAME}${ADAPTER_NUMBER}"
NEW_SYNTAX_FOR_NDD=0
;;
nxge )
ADAPTER_NAME_FOR_NDD="/dev/${ADAPTER_NAME}${ADAPTER_NUMBER}"
NEW_SYNTAX_FOR_NDD=0
;;
* )
ADAPTER_NAME_FOR_NDD="/dev/${ADAPTER_NAME}"
NEW_SYNTAX_FOR_NDD=1
;;
esac
if [ ${NEW_SYNTAX_FOR_NDD} = 1 ] ; then
ndd -set ${ADAPTER_NAME_FOR_NDD} instance ${ADAPTER_NUMBER} 2>/dev/null 1>/dev/null
if [ $? -ne 0 ] ; then
NND_SELECT_CMD=""
ndd -get ${ADAPTER_NAME_FOR_NDD}${ADAPTER_NUMBER} \? >/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
my_echo "Note: This looks like a new GLDv3 driver is used for this adapter"
ADAPTER_NAME_FOR_NDD="/dev/${ADAPTER_NAME}${ADAPTER_NUMBER}"
NEW_SYNTAX_FOR_NDD=0
BGE_SYNTAX=0
else
echo "${SCRIPT_NAME} - ERROR: Adapter \"${ADAPTER}\" not found!"
exit 2
fi
else
NDD_SELECT_CMD="ndd -set ${ADAPTER_NAME_FOR_NDD} instance ${ADAPTER_NUMBER}"
fi
fi
case ${ADAPTER_SPEED} in
"10000" | "10G" | "10g" )
my_echo "Setting the adapter \"${ADAPTER}\" to 10g"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_10gfdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_10gfdx_cap 1
set_all_except_one adv_10gfdx_cap 0
toggle_autoneg 0
;;
"100HD" | "100hd" | "100Hd" | "100HalfDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 100 Mbit/s HalfDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_100hdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_100hdx_cap 1
set_all_except_one adv_100hdx_cap 0
toggle_autoneg 0
;;
"100FD" | "100fd" | "100Fd" | "100FullDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 100 Mbit/s FullDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_100fdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_100fdx_cap 1
set_all_except_one adv_100fdx_cap 0
toggle_autoneg 0
;;
"1000FD" | "1000fd" | "1000Fd" | "1000FullDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 1000 Mbit/s FullDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_1000fdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_1000fdx_cap 1
set_all_except_one adv_10000fdx_cap 0
toggle_autoneg 0
;;
"1000HD" | "1000hd" | "1000Hd" | "1000HalfDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 1000 Mbit/s HalfDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_1000hdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_1000hdx_cap 1
set_all_except_one adv_1000hdx_cap 0
toggle_autoneg 0
;;
"10FD" | "10fd" | "10Fd" | "10FullDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 10 Mbit/s FullDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_10fdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_10fdx_cap 1
set_all_except_one adv_10fdx_cap 0
toggle_autoneg 0
;;
"10HD" | "10hd" | "10Hd" | "10HalfDuplex" )
my_echo "Setting the adapter \"${ADAPTER}\" to 10 Mbit/s HalfDuplex"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_10hdx_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_10hdx_cap 1
set_all_except_one adv_10ghdx_cap 0
toggle_autoneg 0
;;
"auto" | "AUTO" | "Auto" )
my_echo "Setting the adapter \"${ADAPTER}\" to AutoNegotiation"
my_echo1 "${NDD_SELECT_CMD}"
ndd -get ${ADAPTER_NAME_FOR_NDD} adv_autoneg_cap 1>/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "${SCRIPT_NAME} - ERROR: This mode is NOT supported by the adapter!"
exit 6
fi
my_ndd -set ${ADAPTER_NAME_FOR_NDD} adv_100T4_cap 0
set_all_except_one adv_100T4_cap 1
toggle_autoneg 1
;;
* )
echo "${SCRIPT_NAME} - ERROR: Invalid speed parameter: \"${ADAPTER_SPEED}\" "
exit 3
;;
esac
else
echo "${SCRIPT_NAME} - ERROR: Parameter missing!"
exit 4
fi
if [ "${COMMAND}"x = "createcmds"x -a "${OUTPUTFILE}"x != ""x ] ; then
echo "Outputfile \"${OUTPUTFILE}\" created."
fi
exit 0