]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | device_list() { | |
23 | local devices | |
24 | ||
25 | # Add all interfaces | |
26 | list_append devices $(devices_get_all) | |
27 | ||
28 | # Add all PHYs | |
29 | list_append devices $(phy_list) | |
30 | ||
31 | # Add all serial devices | |
32 | list_append devices $(serial_list) | |
33 | ||
34 | # Return a sorted result | |
35 | list_sort ${devices} | |
36 | } | |
37 | ||
38 | # Check if the device exists | |
39 | device_exists() { | |
40 | local device=${1} | |
41 | ||
42 | # If device name was not found, exit. | |
43 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
44 | ||
45 | # Check for a normal network device. | |
46 | [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK} | |
47 | ||
48 | # If the check above did not find a result, | |
49 | # we check for PHYs. | |
50 | phy_exists "${device}" && return ${EXIT_OK} | |
51 | ||
52 | # If the check above did not find a result, | |
53 | # we check for serial devices. | |
54 | serial_exists ${device} | |
55 | } | |
56 | ||
57 | device_matches_pattern() { | |
58 | local device="${1}" | |
59 | assert isset device | |
60 | ||
61 | local pattern="${2}" | |
62 | assert isset pattern | |
63 | ||
64 | pattern="^${pattern//N/[[:digit:]]+}$" | |
65 | ||
66 | [[ ${device} =~ ${pattern} ]] \ | |
67 | && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
68 | } | |
69 | ||
70 | device_delete() { | |
71 | local device=${1} | |
72 | assert isset device | |
73 | ||
74 | # Nothing to do, it device does not exist. | |
75 | device_exists ${device} || return ${EXIT_OK} | |
76 | ||
77 | # Delete the device. | |
78 | cmd_quiet ip link delete ${device} | |
79 | local ret=$? | |
80 | ||
81 | if [ ${ret} -ne ${EXIT_OK} ]; then | |
82 | log ERROR "device: Could not delete device '${device}': ${ret}" | |
83 | return ${EXIT_ERROR} | |
84 | fi | |
85 | ||
86 | return ${ret} | |
87 | } | |
88 | ||
89 | device_has_flag() { | |
90 | local device=${1} | |
91 | local flag=${2} | |
92 | ||
93 | local flags=$(__device_get_file ${device} flags) | |
94 | ||
95 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
96 | return ${EXIT_FALSE} | |
97 | else | |
98 | return ${EXIT_TRUE} | |
99 | fi | |
100 | } | |
101 | ||
102 | # Check if the device is up | |
103 | device_is_up() { | |
104 | local device=${1} | |
105 | ||
106 | device_exists ${device} || return ${EXIT_ERROR} | |
107 | ||
108 | device_has_flag ${device} 0x1 | |
109 | } | |
110 | ||
111 | device_ifindex_to_name() { | |
112 | local idx=${1} | |
113 | assert isset idx | |
114 | ||
115 | local device device_idx | |
116 | for device in ${SYS_CLASS_NET}/*; do | |
117 | device=$(basename ${device}) | |
118 | device_exists ${device} || continue | |
119 | ||
120 | device_idx=$(device_get_ifindex ${device}) | |
121 | ||
122 | if [ "${device_idx}" = "${idx}" ]; then | |
123 | print "${device}" | |
124 | return ${EXIT_OK} | |
125 | fi | |
126 | done | |
127 | ||
128 | return ${EXIT_ERROR} | |
129 | } | |
130 | ||
131 | device_get_ifindex() { | |
132 | local device=${1} | |
133 | assert isset device | |
134 | ||
135 | local path="${SYS_CLASS_NET}/${1}/ifindex" | |
136 | ||
137 | # Check if file can be read. | |
138 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
139 | ||
140 | print "$(<${path})" | |
141 | } | |
142 | ||
143 | # Check if the device is a batman-adv bridge | |
144 | device_is_batman_adv() { | |
145 | [ -d "${SYS_CLASS_NET}/${1}/mesh" ] | |
146 | } | |
147 | ||
148 | # Check if the device is a batman-adv slave port | |
149 | device_is_batman_adv_slave() { | |
150 | local device="${1}" | |
151 | ||
152 | if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then | |
153 | local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)" | |
154 | ||
155 | case "${status}" in | |
156 | "active") | |
157 | return ${EXIT_TRUE} | |
158 | ;; | |
159 | *) | |
160 | return ${EXIT_FALSE} | |
161 | ;; | |
162 | esac | |
163 | fi | |
164 | ||
165 | return ${EXIT_FALSE} | |
166 | } | |
167 | ||
168 | # Check if the device is a bonding device | |
169 | device_is_bonding() { | |
170 | [ -d "/sys/class/net/${1}/bonding" ] | |
171 | } | |
172 | ||
173 | # Check if the device bonded in a bonding device | |
174 | device_is_bonded() { | |
175 | local device=${1} | |
176 | ||
177 | [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] | |
178 | } | |
179 | ||
180 | # Check if the device is a bridge | |
181 | device_is_bridge() { | |
182 | [ -d "/sys/class/net/${1}/bridge" ] | |
183 | } | |
184 | ||
185 | device_is_bridge_attached() { | |
186 | local device=${1} | |
187 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] | |
188 | } | |
189 | ||
190 | device_is_wireless_monitor() { | |
191 | local device="${1}" | |
192 | assert isset device | |
193 | ||
194 | device_is_wireless "${device}" && \ | |
195 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}" | |
196 | } | |
197 | ||
198 | device_is_wireless_adhoc() { | |
199 | local device="${1}" | |
200 | assert isset device | |
201 | ||
202 | device_is_wireless "${device}" && \ | |
203 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}" | |
204 | } | |
205 | ||
206 | device_get_bridge() { | |
207 | local device=${1} | |
208 | assert isset device | |
209 | ||
210 | # Check if device is attached to a bridge. | |
211 | device_is_bridge_attached ${device} || return ${EXIT_ERROR} | |
212 | ||
213 | local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex" | |
214 | [ -r "${ifindex_path}" ] || return ${EXIT_ERROR} | |
215 | ||
216 | local ifindex=$(<${ifindex_path}) | |
217 | assert isset ifindex | |
218 | ||
219 | device_ifindex_to_name ${ifindex} | |
220 | } | |
221 | ||
222 | # Check if the device is a vlan device | |
223 | device_is_vlan() { | |
224 | local device=${1} | |
225 | assert isset device | |
226 | ||
227 | [ -e "${PROC_NET_VLAN}/${device}" ] | |
228 | } | |
229 | ||
230 | # Check if the device has vlan devices | |
231 | device_has_vlans() { | |
232 | local device=${1} | |
233 | assert isset device | |
234 | ||
235 | if device_is_vlan ${device}; then | |
236 | return ${EXIT_FALSE} | |
237 | fi | |
238 | ||
239 | local vlans=$(device_get_vlans ${device}) | |
240 | [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
241 | } | |
242 | ||
243 | device_get_vlans() { | |
244 | local device=${1} | |
245 | assert isset device | |
246 | ||
247 | # If no 8021q module has been loaded into the kernel, | |
248 | # we cannot do anything. | |
249 | [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} | |
250 | ||
251 | local dev spacer1 id spacer2 parent | |
252 | while read dev spacer1 id spacer2 parent; do | |
253 | [ "${parent}" = "${device}" ] || continue | |
254 | ||
255 | print "${dev}" | |
256 | done < ${PROC_NET_VLAN_CONFIG} | |
257 | } | |
258 | ||
259 | # Check if the device is a ppp device | |
260 | device_is_ppp() { | |
261 | local device=${1} | |
262 | ||
263 | local type=$(__device_get_file ${device} type) | |
264 | ||
265 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
266 | } | |
267 | ||
268 | # Check if the device is a pointopoint device. | |
269 | device_is_ptp() { | |
270 | local device=${1} | |
271 | ||
272 | device_has_flag ${device} 0x10 | |
273 | } | |
274 | ||
275 | # Check if the device is a loopback device | |
276 | device_is_loopback() { | |
277 | local device=${1} | |
278 | ||
279 | [ "${device}" = "lo" ] | |
280 | } | |
281 | ||
282 | # Check if the device is a dummy device | |
283 | # This is the worst possible check, but all I could come up with | |
284 | device_is_dummy() { | |
285 | local device="${1}" | |
286 | ||
287 | [[ ${device} =~ ^dummy[0-9]+$ ]] | |
288 | } | |
289 | ||
290 | device_is_ipsec() { | |
291 | local device="${1}" | |
292 | ||
293 | [[ ${device} =~ ^ipsec\- ]] | |
294 | } | |
295 | ||
296 | # Check if the device is a wireless device | |
297 | device_is_wireless() { | |
298 | local device=${1} | |
299 | ||
300 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
301 | } | |
302 | ||
303 | device_is_vti() { | |
304 | local device=${1} | |
305 | ||
306 | local type=$(__device_get_file ${device} type) | |
307 | ||
308 | [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
309 | } | |
310 | ||
311 | device_get_phy() { | |
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 | ||
322 | device_is_phy() { | |
323 | phy_exists $@ | |
324 | } | |
325 | ||
326 | device_is_serial() { | |
327 | serial_exists $@ | |
328 | } | |
329 | ||
330 | # Returns true if a device is a tun device | |
331 | device_is_tun() { | |
332 | local device="${1}" | |
333 | ||
334 | [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ] | |
335 | } | |
336 | ||
337 | # Check if the device is a physical network interface | |
338 | device_is_ethernet() { | |
339 | local device=${1} | |
340 | ||
341 | device_is_ethernet_compatible "${device}" || \ | |
342 | return ${EXIT_ERROR} | |
343 | ||
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 | ||
356 | device_is_vlan ${device} && \ | |
357 | return ${EXIT_ERROR} | |
358 | ||
359 | device_is_dummy ${device} && \ | |
360 | return ${EXIT_ERROR} | |
361 | ||
362 | device_is_tun ${device} && \ | |
363 | return ${EXIT_ERROR} | |
364 | ||
365 | return ${EXIT_OK} | |
366 | } | |
367 | ||
368 | # Get the device type | |
369 | device_get_type() { | |
370 | local device=${1} | |
371 | ||
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 | |
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_batman_adv ${device}; then | |
390 | echo "batman-adv" | |
391 | ||
392 | elif device_is_loopback ${device}; then | |
393 | echo "loopback" | |
394 | ||
395 | elif device_is_wireless_adhoc ${device}; then | |
396 | echo "wireless-adhoc" | |
397 | ||
398 | elif device_is_wireless ${device}; then | |
399 | echo "wireless" | |
400 | ||
401 | elif device_is_dummy ${device}; then | |
402 | echo "dummy" | |
403 | ||
404 | elif device_is_tun ${device}; then | |
405 | echo "tun" | |
406 | ||
407 | elif device_is_ethernet ${device}; then | |
408 | echo "ethernet" | |
409 | ||
410 | elif device_is_serial ${device}; then | |
411 | echo "serial" | |
412 | ||
413 | elif device_is_phy ${device}; then | |
414 | echo "phy" | |
415 | ||
416 | elif device_is_vti ${device}; then | |
417 | echo "vti" | |
418 | ||
419 | else | |
420 | echo "unknown" | |
421 | fi | |
422 | } | |
423 | ||
424 | device_is_ethernet_compatible() { | |
425 | local device="${1}" | |
426 | ||
427 | # /sys/class/net/*/type must equal 1 for ethernet compatible devices | |
428 | local type="$(__device_get_file "${device}" "type")" | |
429 | [[ "${type}" = "1" ]] | |
430 | } | |
431 | ||
432 | device_get_status() { | |
433 | local device=${1} | |
434 | assert isset device | |
435 | ||
436 | local status=${STATUS_DOWN} | |
437 | ||
438 | if device_is_up ${device}; then | |
439 | status=${STATUS_UP} | |
440 | ||
441 | if ! device_has_carrier ${device}; then | |
442 | status=${STATUS_NOCARRIER} | |
443 | fi | |
444 | fi | |
445 | ||
446 | echo "${status}" | |
447 | } | |
448 | ||
449 | device_get_address() { | |
450 | local device=${1} | |
451 | ||
452 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
453 | } | |
454 | ||
455 | device_set_address() { | |
456 | assert [ $# -eq 2 ] | |
457 | ||
458 | local device="${1}" | |
459 | local addr="${2}" | |
460 | ||
461 | if ! device_exists "${device}"; then | |
462 | error "Device '${device}' does not exist." | |
463 | return ${EXIT_ERROR} | |
464 | fi | |
465 | ||
466 | # Do nothing if the address has not changed | |
467 | local old_addr="$(device_get_address "${device}")" | |
468 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
469 | return ${EXIT_OK} | |
470 | fi | |
471 | ||
472 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
473 | ||
474 | local up | |
475 | if device_is_up "${device}"; then | |
476 | device_set_down "${device}" | |
477 | up=1 | |
478 | fi | |
479 | ||
480 | ip link set "${device}" address "${addr}" | |
481 | local ret=$? | |
482 | ||
483 | if [ "${up}" = "1" ]; then | |
484 | device_set_up "${device}" | |
485 | fi | |
486 | ||
487 | if [ "${ret}" != "0" ]; then | |
488 | error_log "Could not set address '${addr}' on device '${device}'" | |
489 | fi | |
490 | ||
491 | return ${ret} | |
492 | } | |
493 | ||
494 | device_get() { | |
495 | local device | |
496 | local devices | |
497 | ||
498 | for device in ${SYS_CLASS_NET}/*; do | |
499 | device=$(basename ${device}) | |
500 | ||
501 | # bonding_masters is no device | |
502 | [ "${device}" = "bonding_masters" ] && continue | |
503 | ||
504 | devices="${devices} ${device}" | |
505 | done | |
506 | ||
507 | echo ${devices} | |
508 | return ${EXIT_OK} | |
509 | } | |
510 | ||
511 | devices_get_all() { | |
512 | device_get | |
513 | } | |
514 | ||
515 | # Check if a device has a cable plugged in | |
516 | device_has_carrier() { | |
517 | local device=${1} | |
518 | assert isset device | |
519 | ||
520 | local carrier=$(__device_get_file ${device} carrier) | |
521 | [ "${carrier}" = "1" ] | |
522 | } | |
523 | ||
524 | device_is_promisc() { | |
525 | local device=${1} | |
526 | ||
527 | device_has_flag ${device} 0x200 | |
528 | } | |
529 | ||
530 | device_set_promisc() { | |
531 | local device=${1} | |
532 | local state=${2} | |
533 | ||
534 | assert device_exists ${device} | |
535 | assert isset state | |
536 | assert isoneof state on off | |
537 | ||
538 | ip link set ${device} promisc ${state} | |
539 | } | |
540 | ||
541 | # Check if the device is free | |
542 | device_is_free() { | |
543 | ! device_is_used $@ | |
544 | } | |
545 | ||
546 | # Check if the device is used | |
547 | device_is_used() { | |
548 | local device=${1} | |
549 | ||
550 | device_has_vlans ${device} && \ | |
551 | return ${EXIT_OK} | |
552 | device_is_bonded ${device} && \ | |
553 | return ${EXIT_OK} | |
554 | device_is_bridge_attached ${device} && \ | |
555 | return ${EXIT_OK} | |
556 | ||
557 | return ${EXIT_ERROR} | |
558 | } | |
559 | ||
560 | # Give the device a new name | |
561 | device_set_name() { | |
562 | local source=$1 | |
563 | local destination=${2} | |
564 | ||
565 | # Check if devices exists | |
566 | if ! device_exists ${source} || device_exists ${destination}; then | |
567 | return 4 | |
568 | fi | |
569 | ||
570 | local up | |
571 | if device_is_up ${source}; then | |
572 | ip link set ${source} down | |
573 | up=1 | |
574 | fi | |
575 | ||
576 | ip link set ${source} name ${destination} | |
577 | ||
578 | if [ "${up}" = "1" ]; then | |
579 | ip link set ${destination} up | |
580 | fi | |
581 | } | |
582 | ||
583 | # Set device up | |
584 | device_set_up() { | |
585 | assert [ $# -eq 1 ] | |
586 | ||
587 | local device=${1} | |
588 | ||
589 | # Do nothing if device is already up | |
590 | device_is_up ${device} && return ${EXIT_OK} | |
591 | ||
592 | log INFO "Bringing up ${device}" | |
593 | ||
594 | device_set_parent_up ${device} | |
595 | if ! cmd ip link set ${device} up; then | |
596 | return ${EXIT_ERROR} | |
597 | fi | |
598 | ||
599 | # Set SMP affinity | |
600 | if interrupt_use_smp_affinity; then | |
601 | device_auto_configure_smp_affinity ${device} | |
602 | fi | |
603 | ||
604 | return ${EXIT_OK} | |
605 | } | |
606 | ||
607 | device_set_parent_up() { | |
608 | local device=${1} | |
609 | local parent | |
610 | ||
611 | if device_is_vlan ${device}; then | |
612 | parent=$(vlan_get_parent ${device}) | |
613 | ||
614 | device_is_up ${parent} && return ${EXIT_OK} | |
615 | ||
616 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
617 | ||
618 | device_set_up ${parent} | |
619 | return $? | |
620 | fi | |
621 | ||
622 | return ${EXIT_OK} | |
623 | } | |
624 | ||
625 | # Set device down | |
626 | device_set_down() { | |
627 | assert [ $# -eq 1 ] | |
628 | ||
629 | local device=${1} | |
630 | local ret=${EXIT_OK} | |
631 | ||
632 | if device_is_up ${device}; then | |
633 | log INFO "Bringing down ${device}" | |
634 | ||
635 | cmd ip link set ${device} down | |
636 | ret=$? | |
637 | fi | |
638 | ||
639 | device_set_parent_down ${device} | |
640 | ||
641 | return ${ret} | |
642 | } | |
643 | ||
644 | device_set_parent_down() { | |
645 | local device=${1} | |
646 | local parent | |
647 | ||
648 | if device_is_vlan ${device}; then | |
649 | parent=$(vlan_get_parent ${device}) | |
650 | ||
651 | device_is_up ${parent} || return ${EXIT_OK} | |
652 | ||
653 | if device_is_free ${parent}; then | |
654 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
655 | ||
656 | device_set_down ${parent} | |
657 | fi | |
658 | fi | |
659 | ||
660 | return ${EXIT_OK} | |
661 | } | |
662 | ||
663 | device_get_mtu() { | |
664 | local device=${1} | |
665 | ||
666 | # Return an error if the device does not exist | |
667 | device_exists ${device} || return ${EXIT_ERROR} | |
668 | ||
669 | echo $(<${SYS_CLASS_NET}/${device}/mtu) | |
670 | } | |
671 | ||
672 | # Set mtu to a device | |
673 | device_set_mtu() { | |
674 | local device=${1} | |
675 | local mtu=${2} | |
676 | ||
677 | assert device_exists ${device} | |
678 | ||
679 | # Handle bridges differently | |
680 | if device_is_bridge ${device}; then | |
681 | local port | |
682 | for port in $(bridge_get_members ${device}); do | |
683 | device_set_mtu ${port} ${mtu} | |
684 | done | |
685 | fi | |
686 | ||
687 | log INFO "Setting MTU of ${device} to ${mtu}" | |
688 | ||
689 | local up | |
690 | if device_is_up ${device}; then | |
691 | device_set_down ${device} | |
692 | up=1 | |
693 | fi | |
694 | ||
695 | local ret=${EXIT_OK} | |
696 | if ! cmd ip link set ${device} mtu ${mtu}; then | |
697 | ret=${EXIT_ERROR} | |
698 | ||
699 | log ERROR "Could not set MTU ${mtu} on ${device}" | |
700 | fi | |
701 | ||
702 | if [ "${up}" = "1" ]; then | |
703 | device_set_up ${device} | |
704 | fi | |
705 | ||
706 | return ${ret} | |
707 | } | |
708 | ||
709 | device_adjust_mtu() { | |
710 | assert [ $# -eq 2 ] | |
711 | ||
712 | local device="${1}" | |
713 | local other_device="${2}" | |
714 | ||
715 | local mtu="$(device_get_mtu "${other_device}")" | |
716 | device_set_mtu "${device}" "${mtu}" | |
717 | } | |
718 | ||
719 | device_discover() { | |
720 | local device=${1} | |
721 | ||
722 | log INFO "Running discovery process on device '${device}'." | |
723 | ||
724 | local hook | |
725 | for hook in $(hook_zone_get_all); do | |
726 | hook_zone_exec ${hook} discover ${device} | |
727 | done | |
728 | } | |
729 | ||
730 | device_identify() { | |
731 | assert [ $# -ge 1 ] | |
732 | ||
733 | local device="${1}" | |
734 | ||
735 | # Flash for ten seconds by default | |
736 | local seconds="10" | |
737 | ||
738 | # Run in background? | |
739 | local background="false" | |
740 | ||
741 | local arg | |
742 | while read arg; do | |
743 | case "${arg}" in | |
744 | --background) | |
745 | background="true" | |
746 | ;; | |
747 | --seconds=*) | |
748 | seconds="$(cli_get_val "${arg}")" | |
749 | ;; | |
750 | esac | |
751 | done <<< "$(args $@)" | |
752 | ||
753 | assert isinteger seconds | |
754 | ||
755 | if ! device_exists "${device}"; then | |
756 | log ERROR "Cannot identify device ${device}: Does not exist" | |
757 | return ${EXIT_ERROR} | |
758 | fi | |
759 | ||
760 | if ! device_is_ethernet "${device}"; then | |
761 | log DEBUG "Cannot identify device ${device}: Not an ethernet device" | |
762 | return ${EXIT_NOT_SUPPORTED} | |
763 | fi | |
764 | ||
765 | log DEBUG "Identifying device ${device}" | |
766 | ||
767 | local command="ethtool --identify ${device} ${seconds}" | |
768 | local ret=0 | |
769 | ||
770 | if enabled background; then | |
771 | cmd_background "${command}" | |
772 | else | |
773 | cmd_quiet "${command}" | |
774 | ret=$? | |
775 | fi | |
776 | ||
777 | return ${ret} | |
778 | } | |
779 | ||
780 | device_has_ip() { | |
781 | local device=${1} | |
782 | local addr=${2} | |
783 | ||
784 | assert isset addr | |
785 | assert device_exists ${device} | |
786 | ||
787 | # IPv6 addresses must be fully imploded | |
788 | local protocol=$(ip_detect_protocol ${addr}) | |
789 | case "${protocol}" in | |
790 | ipv6) | |
791 | addr=$(ipv6_format "${addr}") | |
792 | ;; | |
793 | esac | |
794 | ||
795 | list_match ${addr} $(device_get_addresses ${device}) | |
796 | } | |
797 | ||
798 | device_get_addresses() { | |
799 | local device=${1} | |
800 | ||
801 | assert device_exists ${device} | |
802 | ||
803 | local prot | |
804 | local addr | |
805 | local line | |
806 | ip addr show ${device} | \ | |
807 | while read prot addr line; do | |
808 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
809 | done | |
810 | } | |
811 | ||
812 | __device_get_file() { | |
813 | local device=${1} | |
814 | local file=${2} | |
815 | ||
816 | fread "${SYS_CLASS_NET}/${device}/${file}" | |
817 | } | |
818 | ||
819 | __device_set_file() { | |
820 | assert [ $# -eq 3 ] | |
821 | ||
822 | local device="${1}" | |
823 | local file="${2}" | |
824 | local value="${3}" | |
825 | ||
826 | fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}" | |
827 | } | |
828 | ||
829 | device_get_rx_bytes() { | |
830 | local device=${1} | |
831 | ||
832 | __device_get_file ${device} statistics/rx_bytes | |
833 | } | |
834 | ||
835 | device_get_tx_bytes() { | |
836 | local device=${1} | |
837 | ||
838 | __device_get_file ${device} statistics/tx_bytes | |
839 | } | |
840 | ||
841 | device_get_rx_packets() { | |
842 | local device=${1} | |
843 | ||
844 | __device_get_file ${device} statistics/rx_packets | |
845 | } | |
846 | ||
847 | device_get_tx_packets() { | |
848 | local device=${1} | |
849 | ||
850 | __device_get_file ${device} statistics/tx_packets | |
851 | } | |
852 | ||
853 | device_get_rx_errors() { | |
854 | local device=${1} | |
855 | ||
856 | __device_get_file ${device} statistics/rx_errors | |
857 | } | |
858 | ||
859 | device_get_tx_errors() { | |
860 | local device=${1} | |
861 | ||
862 | __device_get_file ${device} statistics/tx_errors | |
863 | } | |
864 | ||
865 | device_get_speed() { | |
866 | local device=${1} | |
867 | ||
868 | local speed=$(__device_get_file ${device} speed) | |
869 | ||
870 | # Exit for no output (i.e. no link detected) | |
871 | isset speed || return ${EXIT_ERROR} | |
872 | ||
873 | # Don't return anything for negative values | |
874 | [ ${speed} -lt 0 ] && return ${EXIT_ERROR} | |
875 | ||
876 | print "${speed}" | |
877 | } | |
878 | ||
879 | device_get_duplex() { | |
880 | local device=${1} | |
881 | ||
882 | local duplex=$(__device_get_file ${device} duplex) | |
883 | ||
884 | case "${duplex}" in | |
885 | unknown) | |
886 | return ${EXIT_ERROR} | |
887 | ;; | |
888 | *) | |
889 | print "${duplex}" | |
890 | ;; | |
891 | esac | |
892 | } | |
893 | ||
894 | device_get_link_string() { | |
895 | local device="${1}" | |
896 | assert isset device | |
897 | ||
898 | local s | |
899 | ||
900 | local speed="$(device_get_speed "${device}")" | |
901 | if isset speed; then | |
902 | list_append s "${speed} MBit/s" | |
903 | fi | |
904 | ||
905 | local duplex="$(device_get_duplex "${device}")" | |
906 | if isset duplex; then | |
907 | list_append s "${duplex} duplex" | |
908 | fi | |
909 | ||
910 | print "${s}" | |
911 | } | |
912 | ||
913 | device_auto_configure_smp_affinity() { | |
914 | assert [ $# -eq 1 ] | |
915 | ||
916 | local device=${1} | |
917 | ||
918 | if lock_acquire "smp-affinity" 60; then | |
919 | device_set_smp_affinity ${device} auto | |
920 | ||
921 | lock_release "smp-affinity" | |
922 | fi | |
923 | } | |
924 | ||
925 | device_set_smp_affinity() { | |
926 | assert [ $# -eq 2 ] | |
927 | ||
928 | local device=${1} | |
929 | local mode=${2} | |
930 | ||
931 | # mode can be auto which will automatically try to find | |
932 | # the least busy processor, or an integer for the desired | |
933 | # processor that should handle this device | |
934 | ||
935 | local num_processors=$(system_get_processors) | |
936 | ||
937 | if [ "${mode}" = "auto" ]; then | |
938 | local processor=$(interrupt_choose_least_busy_processor) | |
939 | else | |
940 | assert isinteger mode | |
941 | local processor=${mode} | |
942 | ||
943 | if [ ${processor} -gt ${num_processors} ]; then | |
944 | log ERROR "Processor ${processor} does not exist" | |
945 | return ${EXIT_ERROR} | |
946 | fi | |
947 | fi | |
948 | ||
949 | local interrupts=$(interrupts_for_device ${device}) | |
950 | if ! isset interrupts; then | |
951 | log DEBUG "${device} has no interrupts. Not changing SMP affinity" | |
952 | return ${EXIT_OK} | |
953 | fi | |
954 | ||
955 | # Set SMP affinity | |
956 | local interrupt | |
957 | for interrupt in ${interrupts}; do | |
958 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
959 | done | |
960 | ||
961 | # Find all queues and assign them to the next processor | |
962 | local queue | |
963 | for queue in $(device_get_queues ${device}); do | |
964 | case "${queue}" in | |
965 | # Only handle receive queues | |
966 | rx-*) | |
967 | for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do | |
968 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
969 | done | |
970 | ||
971 | device_queue_set_smp_affinity ${device} ${queue} ${processor} | |
972 | ;; | |
973 | ||
974 | # Ignore the rest | |
975 | *) | |
976 | continue | |
977 | ;; | |
978 | esac | |
979 | ||
980 | # Get the next available processor if in auto mode | |
981 | [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor}) | |
982 | done | |
983 | ||
984 | return ${EXIT_OK} | |
985 | } | |
986 | ||
987 | device_get_queues() { | |
988 | assert [ $# -eq 1 ] | |
989 | ||
990 | local device=${1} | |
991 | ||
992 | local queue | |
993 | for queue in ${SYS_CLASS_NET}/${device}/queues/*; do | |
994 | [ -d "${queue}" ] || continue | |
995 | ||
996 | basename "${queue}" | |
997 | done | |
998 | } | |
999 | ||
1000 | device_supports_multiqueue() { | |
1001 | local device=${1} | |
1002 | ||
1003 | local num_queues=$(device_num_queues ${device}) | |
1004 | ||
1005 | if isset num_queues && [ ${num_queues} -gt 2 ]; then | |
1006 | return ${EXIT_TRUE} | |
1007 | fi | |
1008 | ||
1009 | return ${EXIT_FALSE} | |
1010 | } | |
1011 | ||
1012 | device_num_queues() { | |
1013 | local device=${1} | |
1014 | local type=${2} | |
1015 | ||
1016 | isset type && assert isoneof type rx tx | |
1017 | ||
1018 | local i=0 | |
1019 | ||
1020 | local q | |
1021 | for q in $(device_get_queues ${device}); do | |
1022 | case "${type},${q}" in | |
1023 | rx,rx-*) | |
1024 | (( i++ )) | |
1025 | ;; | |
1026 | tx,tx-*) | |
1027 | (( i++ )) | |
1028 | ;; | |
1029 | *,*) | |
1030 | (( i++ )) | |
1031 | ;; | |
1032 | esac | |
1033 | done | |
1034 | ||
1035 | print ${i} | |
1036 | } | |
1037 | ||
1038 | device_queue_get_smp_affinity() { | |
1039 | assert [ $# -eq 2 ] | |
1040 | ||
1041 | local device=${1} | |
1042 | local queue=${2} | |
1043 | ||
1044 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}" | |
1045 | ||
1046 | case "${queue}" in | |
1047 | rx-*) | |
1048 | path="${path}/rps_cpus" | |
1049 | ;; | |
1050 | tx-*) | |
1051 | path="${path}/xps_cpus" | |
1052 | ;; | |
1053 | esac | |
1054 | assert [ -r "${path}" ] | |
1055 | ||
1056 | __bitmap_to_processor_ids $(<${path}) | |
1057 | } | |
1058 | ||
1059 | device_queue_set_smp_affinity() { | |
1060 | assert [ $# -eq 3 ] | |
1061 | ||
1062 | local device=${1} | |
1063 | local queue=${2} | |
1064 | local processor=${3} | |
1065 | ||
1066 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus" | |
1067 | assert [ -w "${path}" ] | |
1068 | ||
1069 | log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}" | |
1070 | ||
1071 | __processor_id_to_bitmap ${processor} > ${path} | |
1072 | } |