]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.device
Cleanup code that deletes ports/zones
[people/ms/network.git] / src / functions / functions.device
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
5b1fd814
MT
22declare -A DEVICE_LINK_SPEEDS=(
23 [10BaseT-Half]=0x1
24 [10BaseT-Full]=0x2
25 [100BaseT-Half]=0x4
26 [100BaseT-Full]=0x8
27 [1000BaseT-Half]=0x10
28 [1000BaseT-Full]=0x20
29 [10000BaseT-Full]=0x1000
30)
31
1c6a4e30 32device_list() {
f90dd58c 33 # Add all interfaces
91aeb1be
MT
34 local device
35 for device in $(list_directory ${SYS_CLASS_NET}); do
c1678cea
MT
36 if device_exists "${device}"; then
37 print "${device}"
38 fi
91aeb1be 39 done
f90dd58c 40
38d28e62
MT
41 # List all PHYs
42 phy_list
f90dd58c 43
38d28e62
MT
44 # List all serial devices
45 serial_list
f90dd58c
MT
46}
47
1848564d 48# Check if the device exists
1c6a4e30 49device_exists() {
1848564d
MT
50 local device=${1}
51
52 # If device name was not found, exit.
53 [ -n "${device}" ] || return ${EXIT_ERROR}
54
6c74a64c
MT
55 # Check for a normal network device.
56 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
57
f90dd58c
MT
58 # If the check above did not find a result,
59 # we check for PHYs.
60 phy_exists "${device}" && return ${EXIT_OK}
61
62 # If the check above did not find a result,
6c74a64c
MT
63 # we check for serial devices.
64 serial_exists ${device}
1848564d
MT
65}
66
1c6a4e30 67device_matches_pattern() {
a23fdc0e
MT
68 local device="${1}"
69 assert isset device
70
71 local pattern="${2}"
72 assert isset pattern
73
74 pattern="^${pattern//N/[[:digit:]]+}$"
75
76 [[ ${device} =~ ${pattern} ]] \
77 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
78}
79
1c6a4e30 80device_delete() {
99be6026
MT
81 local device=${1}
82 assert isset device
83
84 # Nothing to do, it device does not exist.
85 device_exists ${device} || return ${EXIT_OK}
86
cdcce259
MT
87 # Shut down device before we delete it
88 device_set_down "${device}"
89
99be6026
MT
90 # Delete the device.
91 cmd_quiet ip link delete ${device}
92 local ret=$?
93
94 if [ ${ret} -ne ${EXIT_OK} ]; then
95 log ERROR "device: Could not delete device '${device}': ${ret}"
96 return ${EXIT_ERROR}
97 fi
98
99 return ${ret}
100}
101
1c6a4e30 102device_has_flag() {
e369be1a
MT
103 local device=${1}
104 local flag=${2}
105
106 local flags=$(__device_get_file ${device} flags)
107
108 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
109 return ${EXIT_FALSE}
110 else
111 return ${EXIT_TRUE}
112 fi
113}
114
1848564d 115# Check if the device is up
1c6a4e30 116device_is_up() {
1848564d
MT
117 local device=${1}
118
119 device_exists ${device} || return ${EXIT_ERROR}
120
e369be1a 121 device_has_flag ${device} 0x1
1848564d
MT
122}
123
1c6a4e30 124device_ifindex_to_name() {
99be6026
MT
125 local idx=${1}
126 assert isset idx
127
128 local device device_idx
60b1f378 129 for device in $(list_directory "${SYS_CLASS_NET}"); do
99be6026
MT
130 device_idx=$(device_get_ifindex ${device})
131
132 if [ "${device_idx}" = "${idx}" ]; then
133 print "${device}"
134 return ${EXIT_OK}
135 fi
136 done
137
138 return ${EXIT_ERROR}
139}
140
1c6a4e30 141device_get_ifindex() {
99be6026
MT
142 local device=${1}
143 assert isset device
144
145 local path="${SYS_CLASS_NET}/${1}/ifindex"
146
147 # Check if file can be read.
148 [ -r "${path}" ] || return ${EXIT_ERROR}
149
150 print "$(<${path})"
151}
152
4ad5981f
MT
153device_get_driver() {
154 local device="${1}"
155 assert isset device
156
157 get_driver_from_path "${SYS_CLASS_NET}/${device}/device/driver/module"
158}
159
1848564d 160# Check if the device is a bonding device
1c6a4e30 161device_is_bonding() {
1848564d
MT
162 [ -d "/sys/class/net/${1}/bonding" ]
163}
164
165# Check if the device bonded in a bonding device
1c6a4e30 166device_is_bonded() {
711ffac1 167 local device=${1}
1848564d 168
0959482b 169 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
1848564d
MT
170}
171
172# Check if the device is a bridge
1c6a4e30 173device_is_bridge() {
1848564d
MT
174 [ -d "/sys/class/net/${1}/bridge" ]
175}
176
1c6a4e30 177device_is_bridge_attached() {
81ed640c 178 local device=${1}
81ed640c
MT
179 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
180}
181
1c6a4e30 182device_is_wireless_monitor() {
a23fdc0e
MT
183 local device="${1}"
184 assert isset device
185
186 device_is_wireless "${device}" && \
187 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
188}
189
1c6a4e30 190device_is_wireless_adhoc() {
b8026986
MT
191 local device="${1}"
192 assert isset device
193
194 device_is_wireless "${device}" && \
195 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
196}
197
1c6a4e30 198device_get_bridge() {
99be6026
MT
199 local device=${1}
200 assert isset device
201
202 # Check if device is attached to a bridge.
203 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
204
205 local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
206 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
207
208 local ifindex=$(<${ifindex_path})
209 assert isset ifindex
210
211 device_ifindex_to_name ${ifindex}
212}
213
7951525a 214# Check if the device is a vlan device
1c6a4e30 215device_is_vlan() {
1848564d 216 local device=${1}
7951525a 217 assert isset device
1848564d 218
7951525a 219 [ -e "${PROC_NET_VLAN}/${device}" ]
1848564d
MT
220}
221
7951525a 222# Check if the device has vlan devices
1c6a4e30 223device_has_vlans() {
fb02e543 224 local device=${1}
7951525a 225 assert isset device
fb02e543 226
7951525a 227 if device_is_vlan ${device}; then
ec63256a 228 return ${EXIT_FALSE}
fb02e543
MT
229 fi
230
7951525a
MT
231 local vlans=$(device_get_vlans ${device})
232 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
ec63256a
MT
233}
234
1c6a4e30 235device_get_vlans() {
ec63256a 236 local device=${1}
7951525a 237 assert isset device
ec63256a 238
8357a7ff
MT
239 # If no 8021q module has been loaded into the kernel,
240 # we cannot do anything.
7951525a 241 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
8357a7ff 242
ec63256a
MT
243 local dev spacer1 id spacer2 parent
244 while read dev spacer1 id spacer2 parent; do
7951525a
MT
245 [ "${parent}" = "${device}" ] || continue
246
247 print "${dev}"
248 done < ${PROC_NET_VLAN_CONFIG}
1848564d
MT
249}
250
1848564d 251# Check if the device is a ppp device
1c6a4e30 252device_is_ppp() {
1848564d
MT
253 local device=${1}
254
55b802cc 255 local type=$(__device_get_file ${device} type)
28f0b4ab 256
e369be1a
MT
257 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
258}
55b802cc 259
e369be1a 260# Check if the device is a pointopoint device.
1c6a4e30 261device_is_ptp() {
e369be1a
MT
262 local device=${1}
263
264 device_has_flag ${device} 0x10
1848564d
MT
265}
266
267# Check if the device is a loopback device
1c6a4e30 268device_is_loopback() {
5bb2429a
MT
269 local device=${1}
270
1848564d
MT
271 [ "${device}" = "lo" ]
272}
273
0067696a
MT
274# Check if the device is a dummy device
275# This is the worst possible check, but all I could come up with
1c6a4e30 276device_is_dummy() {
0067696a
MT
277 local device="${1}"
278
279 [[ ${device} =~ ^dummy[0-9]+$ ]]
280}
281
82fac748
MT
282device_is_ipsec() {
283 local device="${1}"
284
285 [[ ${device} =~ ^ipsec\- ]]
286}
287
a508c27e 288# Check if the device is a wireless device
1c6a4e30 289device_is_wireless() {
a508c27e
MT
290 local device=${1}
291
292 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
293}
294
1a02da59
MT
295device_is_vti() {
296 local device=${1}
297
298 local type=$(__device_get_file ${device} type)
299
300 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
301}
302
bcba5d27
JS
303device_is_vti6() {
304 local device=${1}
305
306 local type=$(__device_get_file ${device} type)
307
308 [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
309}
310
1c6a4e30 311device_get_phy() {
4733a336
MT
312 local device="${1}"
313
314 if device_is_wireless "${device}"; then
315 print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
316 return ${EXIT_OK}
317 fi
318
319 return ${EXIT_ERROR}
320}
321
1c6a4e30 322device_is_phy() {
2212045f 323 phy_exists "$@"
f90dd58c
MT
324}
325
1c6a4e30 326device_is_serial() {
2212045f 327 serial_exists "$@"
6c74a64c
MT
328}
329
2d6dab20
MT
330# Returns true if a device is a tun device
331device_is_tun() {
332 local device="${1}"
333
334 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
335}
336
1848564d 337# Check if the device is a physical network interface
1c6a4e30 338device_is_ethernet() {
1848564d
MT
339 local device=${1}
340
0067696a
MT
341 device_is_ethernet_compatible "${device}" || \
342 return ${EXIT_ERROR}
343
1848564d
MT
344 device_is_loopback ${device} && \
345 return ${EXIT_ERROR}
346
347 device_is_bonding ${device} && \
348 return ${EXIT_ERROR}
349
350 device_is_bridge ${device} && \
351 return ${EXIT_ERROR}
352
353 device_is_ppp ${device} && \
354 return ${EXIT_ERROR}
355
7951525a 356 device_is_vlan ${device} && \
1848564d
MT
357 return ${EXIT_ERROR}
358
0067696a 359 device_is_dummy ${device} && \
419b4cd0
MT
360 return ${EXIT_ERROR}
361
2d6dab20
MT
362 device_is_tun ${device} && \
363 return ${EXIT_ERROR}
364
1848564d
MT
365 return ${EXIT_OK}
366}
367
368# Get the device type
1c6a4e30 369device_get_type() {
5bb2429a 370 local device=${1}
1848564d 371
b9f27baf
MT
372 # If the device does not exist (happens on udev remove events),
373 # we do not bother to run all checks.
374 if ! device_exists "${device}"; then
375 echo "unknown"
376
377 elif device_is_vlan ${device}; then
1848564d
MT
378 echo "vlan"
379
380 elif device_is_bonding ${device}; then
381 echo "bonding"
382
383 elif device_is_bridge ${device}; then
384 echo "bridge"
385
386 elif device_is_ppp ${device}; then
387 echo "ppp"
388
389 elif device_is_loopback ${device}; then
390 echo "loopback"
391
b8026986
MT
392 elif device_is_wireless_adhoc ${device}; then
393 echo "wireless-adhoc"
394
a508c27e
MT
395 elif device_is_wireless ${device}; then
396 echo "wireless"
397
0067696a
MT
398 elif device_is_dummy ${device}; then
399 echo "dummy"
400
2d6dab20
MT
401 elif device_is_tun ${device}; then
402 echo "tun"
403
ec63256a
MT
404 elif device_is_ethernet ${device}; then
405 echo "ethernet"
1848564d 406
6c74a64c
MT
407 elif device_is_serial ${device}; then
408 echo "serial"
409
f90dd58c
MT
410 elif device_is_phy ${device}; then
411 echo "phy"
412
e173e612
JS
413 else
414 echo "$(device_tunnel_get_type "${device}")"
415 fi
416}
417
418# This function just checks the types a ip-tunnel device usually have
419# so when we know that the device is an ip-tunnel device we save time
420device_tunnel_get_type() {
421 local device=${1}
422
423 # If the device does not exist (happens on udev remove events),
424 # we do not bother to run all checks.
425 if ! device_exists "${device}"; then
426 echo "unknown"
427
1a02da59
MT
428 elif device_is_vti ${device}; then
429 echo "vti"
430
e173e612
JS
431 elif device_is_vti6 ${device}; then
432 echo "vti6"
433
1848564d
MT
434 else
435 echo "unknown"
436 fi
437}
438
1c6a4e30 439device_is_ethernet_compatible() {
a4f7ad26
MT
440 local device="${1}"
441
442 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
443 local type="$(__device_get_file "${device}" "type")"
444 [[ "${type}" = "1" ]]
445}
446
1c6a4e30 447device_get_status() {
711ffac1 448 local device=${1}
711ffac1
MT
449 assert isset device
450
3cb2fc42 451 local status=${STATUS_DOWN}
711ffac1 452
3cb2fc42 453 if device_is_up ${device}; then
711ffac1 454 status=${STATUS_UP}
711ffac1 455
3cb2fc42
MT
456 if ! device_has_carrier ${device}; then
457 status=${STATUS_NOCARRIER}
458 fi
459 fi
711ffac1
MT
460
461 echo "${status}"
462}
463
1c6a4e30 464device_get_address() {
1848564d
MT
465 local device=${1}
466
467 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
468}
469
1c6a4e30 470device_set_address() {
08c5b789
MT
471 assert [ $# -eq 2 ]
472
473 local device="${1}"
474 local addr="${2}"
1b7a1578 475
08c5b789 476 if ! device_exists "${device}"; then
1b7a1578
MT
477 error "Device '${device}' does not exist."
478 return ${EXIT_ERROR}
479 fi
480
08c5b789
MT
481 # Do nothing if the address has not changed
482 local old_addr="$(device_get_address "${device}")"
483 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
484 return ${EXIT_OK}
485 fi
486
487 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
1b7a1578
MT
488
489 local up
08c5b789
MT
490 if device_is_up "${device}"; then
491 device_set_down "${device}"
1b7a1578
MT
492 up=1
493 fi
494
08c5b789 495 ip link set "${device}" address "${addr}"
1b7a1578
MT
496 local ret=$?
497
498 if [ "${up}" = "1" ]; then
08c5b789 499 device_set_up "${device}"
1b7a1578
MT
500 fi
501
502 if [ "${ret}" != "0" ]; then
08c5b789 503 error_log "Could not set address '${addr}' on device '${device}'"
1b7a1578
MT
504 fi
505
506 return ${ret}
1848564d
MT
507}
508
1c6a4e30 509device_get() {
2ae0fb8d 510 local device
60b1f378 511 for device in $(list_directory "${SYS_CLASS_NET}"); do
2ae0fb8d
MT
512 # bonding_masters is no device
513 [ "${device}" = "bonding_masters" ] && continue
514
60b1f378 515 echo "${device}"
2ae0fb8d 516 done
711ffac1 517
711ffac1
MT
518 return ${EXIT_OK}
519}
520
1848564d 521# Check if a device has a cable plugged in
1c6a4e30 522device_has_carrier() {
5bb2429a
MT
523 local device=${1}
524 assert isset device
525
ec63256a
MT
526 local carrier=$(__device_get_file ${device} carrier)
527 [ "${carrier}" = "1" ]
1848564d
MT
528}
529
1c6a4e30 530device_is_promisc() {
1e4c26a4
MT
531 local device=${1}
532
e369be1a 533 device_has_flag ${device} 0x200
1e4c26a4
MT
534}
535
1c6a4e30 536device_set_promisc() {
cf6e4606
MT
537 local device=${1}
538 local state=${2}
539
540 assert device_exists ${device}
541 assert isset state
542 assert isoneof state on off
543
544 ip link set ${device} promisc ${state}
545}
546
1848564d 547# Check if the device is free
1c6a4e30 548device_is_free() {
2212045f 549 ! device_is_used "$@"
1848564d
MT
550}
551
552# Check if the device is used
1c6a4e30 553device_is_used() {
5bb2429a 554 local device=${1}
1848564d 555
7951525a 556 device_has_vlans ${device} && \
fb02e543 557 return ${EXIT_OK}
1848564d 558 device_is_bonded ${device} && \
fb02e543 559 return ${EXIT_OK}
81ed640c
MT
560 device_is_bridge_attached ${device} && \
561 return ${EXIT_OK}
1848564d 562
fb02e543 563 return ${EXIT_ERROR}
1848564d
MT
564}
565
1b7a1578 566# Give the device a new name
1c6a4e30 567device_set_name() {
1848564d 568 local source=$1
1578dae9 569 local destination=${2}
1848564d
MT
570
571 # Check if devices exists
572 if ! device_exists ${source} || device_exists ${destination}; then
573 return 4
574 fi
575
576 local up
577 if device_is_up ${source}; then
578 ip link set ${source} down
579 up=1
580 fi
581
582 ip link set ${source} name ${destination}
583
584 if [ "${up}" = "1" ]; then
585 ip link set ${destination} up
586 fi
587}
588
0e523702
MT
589device_set_master() {
590 local device="${1}"
591 assert isset device
592
593 local master="${2}"
594 assert isset master
595
596 if ! cmd ip link set "${device}" master "${master}"; then
597 log ERROR "Could not set master ${master} for device ${device}"
598 return ${EXIT_ERROR}
599 fi
600
601 return ${EXIT_OK}
602}
603
604device_remove_master() {
605 local device="${1}"
606 assert isset device
607
608 if ! cmd ip link set "${device}" nomaster; then
609 log ERROR "Could not remove master for device ${device}"
610 return ${EXIT_ERROR}
611 fi
612
613 return ${EXIT_OK}
614}
615
1848564d 616# Set device up
1c6a4e30 617device_set_up() {
45546be1 618 assert [ $# -eq 1 ]
1848564d 619
45546be1 620 local device=${1}
711ffac1 621
1848564d
MT
622 # Do nothing if device is already up
623 device_is_up ${device} && return ${EXIT_OK}
624
f18ee3d4 625 log INFO "Bringing up ${device}"
81ed640c 626
f18ee3d4 627 device_set_parent_up ${device}
45546be1
MT
628 if ! cmd ip link set ${device} up; then
629 return ${EXIT_ERROR}
630 fi
de72bd91
MT
631
632 # Set SMP affinity
633 if interrupt_use_smp_affinity; then
505ead5d 634 device_auto_configure_smp_affinity ${device}
de72bd91
MT
635 fi
636
637 return ${EXIT_OK}
1848564d
MT
638}
639
1c6a4e30 640device_set_parent_up() {
81ed640c
MT
641 local device=${1}
642 local parent
643
7951525a
MT
644 if device_is_vlan ${device}; then
645 parent=$(vlan_get_parent ${device})
81ed640c
MT
646
647 device_is_up ${parent} && return ${EXIT_OK}
648
649 log DEBUG "Setting up parent device '${parent}' of '${device}'"
650
651 device_set_up ${parent}
652 return $?
653 fi
654
655 return ${EXIT_OK}
656}
657
1848564d 658# Set device down
1c6a4e30 659device_set_down() {
45546be1 660 assert [ $# -eq 1 ]
1848564d 661
45546be1 662 local device=${1}
81ed640c
MT
663 local ret=${EXIT_OK}
664
665 if device_is_up ${device}; then
f18ee3d4 666 log INFO "Bringing down ${device}"
81ed640c 667
45546be1 668 cmd ip link set ${device} down
81ed640c
MT
669 ret=$?
670 fi
671
672 device_set_parent_down ${device}
1848564d 673
81ed640c
MT
674 return ${ret}
675}
676
1c6a4e30 677device_set_parent_down() {
81ed640c
MT
678 local device=${1}
679 local parent
680
7951525a
MT
681 if device_is_vlan ${device}; then
682 parent=$(vlan_get_parent ${device})
81ed640c
MT
683
684 device_is_up ${parent} || return ${EXIT_OK}
685
686 if device_is_free ${parent}; then
687 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
688
689 device_set_down ${parent}
690 fi
691 fi
692
693 return ${EXIT_OK}
1848564d
MT
694}
695
1c6a4e30 696device_get_mtu() {
1848564d
MT
697 local device=${1}
698
491e4f92
MT
699 # Return an error if the device does not exist
700 device_exists ${device} || return ${EXIT_ERROR}
1848564d 701
f3e6fe50 702 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
703}
704
705# Set mtu to a device
1c6a4e30 706device_set_mtu() {
1b7a1578 707 local device=${1}
1848564d
MT
708 local mtu=${2}
709
491e4f92 710 assert device_exists ${device}
1b7a1578 711
3464bc89
AF
712 # Handle bridges differently
713 if device_is_bridge ${device}; then
714 local port
715 for port in $(bridge_get_members ${device}); do
716 device_set_mtu ${port} ${mtu}
717 done
718 fi
719
491e4f92 720 log INFO "Setting MTU of ${device} to ${mtu}"
1b7a1578 721
1848564d 722 local up
1b7a1578
MT
723 if device_is_up ${device}; then
724 device_set_down ${device}
1848564d
MT
725 up=1
726 fi
727
491e4f92
MT
728 local ret=${EXIT_OK}
729 if ! cmd ip link set ${device} mtu ${mtu}; then
730 ret=${EXIT_ERROR}
1848564d 731
491e4f92 732 log ERROR "Could not set MTU ${mtu} on ${device}"
1b7a1578
MT
733 fi
734
491e4f92
MT
735 if [ "${up}" = "1" ]; then
736 device_set_up ${device}
1848564d
MT
737 fi
738
739 return ${ret}
740}
741
1c6a4e30 742device_adjust_mtu() {
3ee5ccb1
MT
743 assert [ $# -eq 2 ]
744
745 local device="${1}"
746 local other_device="${2}"
747
748 local mtu="$(device_get_mtu "${other_device}")"
749 device_set_mtu "${device}" "${mtu}"
750}
751
1c6a4e30 752device_discover() {
1848564d
MT
753 local device=${1}
754
1b7a1578
MT
755 log INFO "Running discovery process on device '${device}'."
756
1848564d 757 local hook
d61a01d4
MT
758 for hook in $(hook_zone_get_all); do
759 hook_zone_exec ${hook} discover ${device}
1848564d
MT
760 done
761}
762
f5ee091e
MT
763device_identify() {
764 assert [ $# -ge 1 ]
765
766 local device="${1}"
767
768 # Flash for ten seconds by default
769 local seconds="10"
770
771 # Run in background?
772 local background="false"
773
774 local arg
775 while read arg; do
776 case "${arg}" in
777 --background)
778 background="true"
779 ;;
780 --seconds=*)
781 seconds="$(cli_get_val "${arg}")"
782 ;;
783 esac
2212045f 784 done <<< "$(args "$@")"
f5ee091e
MT
785
786 assert isinteger seconds
787
788 if ! device_exists "${device}"; then
789 log ERROR "Cannot identify device ${device}: Does not exist"
790 return ${EXIT_ERROR}
791 fi
792
793 if ! device_is_ethernet "${device}"; then
794 log DEBUG "Cannot identify device ${device}: Not an ethernet device"
795 return ${EXIT_NOT_SUPPORTED}
796 fi
797
798 log DEBUG "Identifying device ${device}"
799
800 local command="ethtool --identify ${device} ${seconds}"
801 local ret=0
802
803 if enabled background; then
804 cmd_background "${command}"
805 else
806 cmd_quiet "${command}"
807 ret=$?
808 fi
809
810 return ${ret}
811}
812
1c6a4e30 813device_has_ip() {
1848564d
MT
814 local device=${1}
815 local addr=${2}
816
38f61548
MT
817 assert isset addr
818 assert device_exists ${device}
819
820 # IPv6 addresses must be fully imploded
821 local protocol=$(ip_detect_protocol ${addr})
822 case "${protocol}" in
823 ipv6)
13a6e69f 824 addr=$(ipv6_format "${addr}")
38f61548
MT
825 ;;
826 esac
1848564d 827
8c9205b1 828 list_match ${addr} $(device_get_addresses ${device})
1848564d 829}
4231f419 830
1c6a4e30 831device_get_addresses() {
4231f419 832 local device=${1}
4231f419 833
38f61548 834 assert device_exists ${device}
4231f419 835
38f61548
MT
836 local prot
837 local addr
838 local line
839 ip addr show ${device} | \
840 while read prot addr line; do
841 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
842 done
4231f419 843}
711ffac1 844
1c6a4e30 845__device_get_file() {
711ffac1
MT
846 local device=${1}
847 local file=${2}
848
750aae10 849 fread "${SYS_CLASS_NET}/${device}/${file}"
711ffac1
MT
850}
851
1c6a4e30 852__device_set_file() {
2c083d57
MT
853 assert [ $# -eq 3 ]
854
855 local device="${1}"
856 local file="${2}"
857 local value="${3}"
858
644d3bb8 859 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
2c083d57
MT
860}
861
1c6a4e30 862device_get_rx_bytes() {
711ffac1
MT
863 local device=${1}
864
865 __device_get_file ${device} statistics/rx_bytes
866}
867
1c6a4e30 868device_get_tx_bytes() {
711ffac1
MT
869 local device=${1}
870
871 __device_get_file ${device} statistics/tx_bytes
872}
873
1c6a4e30 874device_get_rx_packets() {
711ffac1
MT
875 local device=${1}
876
877 __device_get_file ${device} statistics/rx_packets
878}
879
1c6a4e30 880device_get_tx_packets() {
711ffac1
MT
881 local device=${1}
882
883 __device_get_file ${device} statistics/tx_packets
884}
885
1c6a4e30 886device_get_rx_errors() {
711ffac1
MT
887 local device=${1}
888
889 __device_get_file ${device} statistics/rx_errors
890}
891
1c6a4e30 892device_get_tx_errors() {
711ffac1
MT
893 local device=${1}
894
895 __device_get_file ${device} statistics/tx_errors
896}
ec63256a 897
5b1fd814
MT
898device_advertise_link_speeds() {
899 local device="${1}"
900 shift
901
902 assert isset device
903
904 # Advertised modes in hex
905 local advertise=0
906
907 local mode
908 for mode in $@; do
909 local m="${DEVICE_LINK_SPEEDS[${mode}]}"
910 if isset m; then
911 advertise="$(( advertise | m ))"
912 fi
913 done
914
915 # If nothing was selected, we reset and enable everything
916 if [ ${advertise} -eq 0 ]; then
917 advertise=0xffffff
918 fi
919
920 # Enable auto-negotiation
921 cmd_quiet ethtool --change "${device}" autoneg on
922
923 # Set advertised link speeds
924 if ! cmd_quiet ethtool --change "${device}" advertise "0x$(hex "${advertise}")"; then
925 log ERROR "Could not set link modes of ${device}: $@"
926 return ${EXIT_ERROR}
927 fi
928
929 log DEBUG "Set device link modes of ${device} to $@"
930 return ${EXIT_ERROR}
931}
932
1c6a4e30 933device_get_speed() {
ec63256a
MT
934 local device=${1}
935
0d2d02da
MT
936 local speed=$(__device_get_file ${device} speed)
937
f887607c
MT
938 # Exit for no output (i.e. no link detected)
939 isset speed || return ${EXIT_ERROR}
940
0d2d02da
MT
941 # Don't return anything for negative values
942 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
943
944 print "${speed}"
ec63256a
MT
945}
946
1c6a4e30 947device_get_duplex() {
ec63256a
MT
948 local device=${1}
949
cd0f7e51
MT
950 local duplex=$(__device_get_file ${device} duplex)
951
952 case "${duplex}" in
953 unknown)
954 return ${EXIT_ERROR}
955 ;;
956 *)
957 print "${duplex}"
958 ;;
959 esac
ec63256a 960}
657540d8 961
1c6a4e30 962device_get_link_string() {
657540d8
MT
963 local device="${1}"
964 assert isset device
965
966 local s
967
968 local speed="$(device_get_speed "${device}")"
969 if isset speed; then
970 list_append s "${speed} MBit/s"
971 fi
972
973 local duplex="$(device_get_duplex "${device}")"
974 if isset duplex; then
975 list_append s "${duplex} duplex"
976 fi
977
978 print "${s}"
979}
de72bd91
MT
980
981device_auto_configure_smp_affinity() {
982 assert [ $# -eq 1 ]
983
984 local device=${1}
985
c73dc5dc 986 if lock_acquire "smp-affinity" 60; then
505ead5d 987 device_set_smp_affinity ${device} auto
de72bd91
MT
988
989 lock_release "smp-affinity"
990 fi
991}
992
993device_set_smp_affinity() {
994 assert [ $# -eq 2 ]
995
996 local device=${1}
997 local mode=${2}
998
999 # mode can be auto which will automatically try to find
1000 # the least busy processor, or an integer for the desired
1001 # processor that should handle this device
1002
1003 local num_processors=$(system_get_processors)
1004
1005 if [ "${mode}" = "auto" ]; then
1006 local processor=$(interrupt_choose_least_busy_processor)
1007 else
1008 assert isinteger mode
1009 local processor=${mode}
1010
1011 if [ ${processor} -gt ${num_processors} ]; then
1012 log ERROR "Processor ${processor} does not exist"
1013 return ${EXIT_ERROR}
1014 fi
1015 fi
1016
1017 local interrupts=$(interrupts_for_device ${device})
1018 if ! isset interrupts; then
1019 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
1020 return ${EXIT_OK}
1021 fi
1022
1023 # Set SMP affinity
1024 local interrupt
1025 for interrupt in ${interrupts}; do
1026 interrupt_set_smp_affinity ${interrupt} ${processor}
1027 done
1028
1029 # Find all queues and assign them to the next processor
1030 local queue
1031 for queue in $(device_get_queues ${device}); do
1032 case "${queue}" in
1033 # Only handle receive queues
1034 rx-*)
1035 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1036 interrupt_set_smp_affinity ${interrupt} ${processor}
1037 done
1038
1039 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1040 ;;
1041
1042 # Ignore the rest
1043 *)
1044 continue
1045 ;;
1046 esac
1047
1048 # Get the next available processor if in auto mode
1049 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
1050 done
1051
1052 return ${EXIT_OK}
1053}
1054
1055device_get_queues() {
1056 assert [ $# -eq 1 ]
1057
1058 local device=${1}
1059
60b1f378 1060 list_directory "${SYS_CLASS_NET}/${device}/queues"
de72bd91
MT
1061}
1062
6529dfaa
MT
1063device_supports_multiqueue() {
1064 local device=${1}
1065
1066 local num_queues=$(device_num_queues ${device})
1067
1068 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1069 return ${EXIT_TRUE}
1070 fi
1071
1072 return ${EXIT_FALSE}
1073}
1074
1075device_num_queues() {
1076 local device=${1}
1077 local type=${2}
1078
e396e74e 1079 isset type && assert isoneof type rx tx
6529dfaa
MT
1080
1081 local i=0
1082
1083 local q
1084 for q in $(device_get_queues ${device}); do
1085 case "${type},${q}" in
1086 rx,rx-*)
1087 (( i++ ))
1088 ;;
1089 tx,tx-*)
1090 (( i++ ))
1091 ;;
1092 *,*)
1093 (( i++ ))
1094 ;;
1095 esac
1096 done
1097
1098 print ${i}
1099}
1100
1101device_queue_get_smp_affinity() {
1102 assert [ $# -eq 2 ]
1103
1104 local device=${1}
1105 local queue=${2}
1106
e396e74e
MT
1107 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1108
1109 case "${queue}" in
1110 rx-*)
1111 path="${path}/rps_cpus"
1112 ;;
1113 tx-*)
1114 path="${path}/xps_cpus"
1115 ;;
1116 esac
6529dfaa
MT
1117 assert [ -r "${path}" ]
1118
1119 __bitmap_to_processor_ids $(<${path})
1120}
1121
de72bd91
MT
1122device_queue_set_smp_affinity() {
1123 assert [ $# -eq 3 ]
1124
1125 local device=${1}
1126 local queue=${2}
1127 local processor=${3}
1128
1129 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1130 assert [ -w "${path}" ]
1131
1132 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1133
1134 __processor_id_to_bitmap ${processor} > ${path}
1135}
905b13b3
JS
1136
1137# Tries to find a device which has the given IP address assigned
1138device_get_by_assigned_ip_address() {
1139 local ip=${1}
1140
1141 assert isset ip
1142
1143 local device
1144
1145 # Read the first line of ip addr show to
1146 read -r device <<< $(ip addr show to "${ip}")
1147
1148 # If we did not found a device we return with ${EXIT_ERROR}
1149 if ! isset device; then
1150 return ${EXIT_ERROR}
1151 fi
1152
1153 # We get something like:
1154 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1155 # and we want upl0 so we take the second word and removing the :
1156 device=(${device})
1157 device=${device[1]}
1158 device=${device%:}
1159
1160 print "${device}"
1161 return ${EXIT_OK}
1162}
e8253eba
JS
1163
1164device_get_by_mac_address() {
1165 local mac=${1}
1166
1167 assert isset mac
1168
1169 local device
1170
1171 for device in $(device_list); do
1172 if [ "${mac}" = "$(device_get_address ${device})" ]; then
1173 print "${device}"
1174 return ${EXIT_OK}
1175 fi
1176 done
1177
1178 # We could not found a port to the given mac address so we return exit error
1179 return ${EXIT_ERROR}
1180}