]>
Commit | Line | Data |
---|---|---|
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 | ||
1c6a4e30 | 22 | device_list() { |
f90dd58c MT |
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 | ||
1848564d | 38 | # Check if the device exists |
1c6a4e30 | 39 | device_exists() { |
1848564d MT |
40 | local device=${1} |
41 | ||
42 | # If device name was not found, exit. | |
43 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
44 | ||
6c74a64c MT |
45 | # Check for a normal network device. |
46 | [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK} | |
47 | ||
f90dd58c MT |
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, | |
6c74a64c MT |
53 | # we check for serial devices. |
54 | serial_exists ${device} | |
1848564d MT |
55 | } |
56 | ||
1c6a4e30 | 57 | device_matches_pattern() { |
a23fdc0e MT |
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 | ||
1c6a4e30 | 70 | device_delete() { |
99be6026 MT |
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 | ||
1c6a4e30 | 89 | device_has_flag() { |
e369be1a MT |
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 | ||
1848564d | 102 | # Check if the device is up |
1c6a4e30 | 103 | device_is_up() { |
1848564d MT |
104 | local device=${1} |
105 | ||
106 | device_exists ${device} || return ${EXIT_ERROR} | |
107 | ||
e369be1a | 108 | device_has_flag ${device} 0x1 |
1848564d MT |
109 | } |
110 | ||
1c6a4e30 | 111 | device_ifindex_to_name() { |
99be6026 MT |
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 | ||
1c6a4e30 | 131 | device_get_ifindex() { |
99be6026 MT |
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 | ||
e6993835 | 143 | # Check if the device is a batman-adv bridge |
1c6a4e30 | 144 | device_is_batman_adv() { |
e6993835 MT |
145 | [ -d "${SYS_CLASS_NET}/${1}/mesh" ] |
146 | } | |
147 | ||
b8026986 | 148 | # Check if the device is a batman-adv slave port |
1c6a4e30 | 149 | device_is_batman_adv_slave() { |
7b192cf4 MT |
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} | |
e6993835 MT |
166 | } |
167 | ||
1848564d | 168 | # Check if the device is a bonding device |
1c6a4e30 | 169 | device_is_bonding() { |
1848564d MT |
170 | [ -d "/sys/class/net/${1}/bonding" ] |
171 | } | |
172 | ||
173 | # Check if the device bonded in a bonding device | |
1c6a4e30 | 174 | device_is_bonded() { |
711ffac1 | 175 | local device=${1} |
1848564d | 176 | |
0959482b | 177 | [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] |
1848564d MT |
178 | } |
179 | ||
180 | # Check if the device is a bridge | |
1c6a4e30 | 181 | device_is_bridge() { |
1848564d MT |
182 | [ -d "/sys/class/net/${1}/bridge" ] |
183 | } | |
184 | ||
1c6a4e30 | 185 | device_is_bridge_attached() { |
81ed640c | 186 | local device=${1} |
81ed640c MT |
187 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] |
188 | } | |
189 | ||
1c6a4e30 | 190 | device_is_wireless_monitor() { |
a23fdc0e MT |
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 | ||
1c6a4e30 | 198 | device_is_wireless_adhoc() { |
b8026986 MT |
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 | ||
1c6a4e30 | 206 | device_get_bridge() { |
99be6026 MT |
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 | ||
7951525a | 222 | # Check if the device is a vlan device |
1c6a4e30 | 223 | device_is_vlan() { |
1848564d | 224 | local device=${1} |
7951525a | 225 | assert isset device |
1848564d | 226 | |
7951525a | 227 | [ -e "${PROC_NET_VLAN}/${device}" ] |
1848564d MT |
228 | } |
229 | ||
7951525a | 230 | # Check if the device has vlan devices |
1c6a4e30 | 231 | device_has_vlans() { |
fb02e543 | 232 | local device=${1} |
7951525a | 233 | assert isset device |
fb02e543 | 234 | |
7951525a | 235 | if device_is_vlan ${device}; then |
ec63256a | 236 | return ${EXIT_FALSE} |
fb02e543 MT |
237 | fi |
238 | ||
7951525a MT |
239 | local vlans=$(device_get_vlans ${device}) |
240 | [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
ec63256a MT |
241 | } |
242 | ||
1c6a4e30 | 243 | device_get_vlans() { |
ec63256a | 244 | local device=${1} |
7951525a | 245 | assert isset device |
ec63256a | 246 | |
8357a7ff MT |
247 | # If no 8021q module has been loaded into the kernel, |
248 | # we cannot do anything. | |
7951525a | 249 | [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} |
8357a7ff | 250 | |
ec63256a MT |
251 | local dev spacer1 id spacer2 parent |
252 | while read dev spacer1 id spacer2 parent; do | |
7951525a MT |
253 | [ "${parent}" = "${device}" ] || continue |
254 | ||
255 | print "${dev}" | |
256 | done < ${PROC_NET_VLAN_CONFIG} | |
1848564d MT |
257 | } |
258 | ||
1848564d | 259 | # Check if the device is a ppp device |
1c6a4e30 | 260 | device_is_ppp() { |
1848564d MT |
261 | local device=${1} |
262 | ||
55b802cc | 263 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 264 | |
e369be1a MT |
265 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
266 | } | |
55b802cc | 267 | |
e369be1a | 268 | # Check if the device is a pointopoint device. |
1c6a4e30 | 269 | device_is_ptp() { |
e369be1a MT |
270 | local device=${1} |
271 | ||
272 | device_has_flag ${device} 0x10 | |
1848564d MT |
273 | } |
274 | ||
275 | # Check if the device is a loopback device | |
1c6a4e30 | 276 | device_is_loopback() { |
5bb2429a MT |
277 | local device=${1} |
278 | ||
1848564d MT |
279 | [ "${device}" = "lo" ] |
280 | } | |
281 | ||
0067696a MT |
282 | # Check if the device is a dummy device |
283 | # This is the worst possible check, but all I could come up with | |
1c6a4e30 | 284 | device_is_dummy() { |
0067696a MT |
285 | local device="${1}" |
286 | ||
287 | [[ ${device} =~ ^dummy[0-9]+$ ]] | |
288 | } | |
289 | ||
82fac748 MT |
290 | device_is_ipsec() { |
291 | local device="${1}" | |
292 | ||
293 | [[ ${device} =~ ^ipsec\- ]] | |
294 | } | |
295 | ||
a508c27e | 296 | # Check if the device is a wireless device |
1c6a4e30 | 297 | device_is_wireless() { |
a508c27e MT |
298 | local device=${1} |
299 | ||
300 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
301 | } | |
302 | ||
1a02da59 MT |
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 | ||
1c6a4e30 | 311 | device_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 | 322 | device_is_phy() { |
f90dd58c MT |
323 | phy_exists $@ |
324 | } | |
325 | ||
1c6a4e30 | 326 | device_is_serial() { |
6c74a64c MT |
327 | serial_exists $@ |
328 | } | |
329 | ||
2d6dab20 MT |
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 | ||
1848564d | 337 | # Check if the device is a physical network interface |
1c6a4e30 | 338 | device_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 | 369 | device_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 | ||
e6993835 MT |
389 | elif device_is_batman_adv ${device}; then |
390 | echo "batman-adv" | |
391 | ||
1848564d MT |
392 | elif device_is_loopback ${device}; then |
393 | echo "loopback" | |
394 | ||
b8026986 MT |
395 | elif device_is_wireless_adhoc ${device}; then |
396 | echo "wireless-adhoc" | |
397 | ||
a508c27e MT |
398 | elif device_is_wireless ${device}; then |
399 | echo "wireless" | |
400 | ||
0067696a MT |
401 | elif device_is_dummy ${device}; then |
402 | echo "dummy" | |
403 | ||
2d6dab20 MT |
404 | elif device_is_tun ${device}; then |
405 | echo "tun" | |
406 | ||
ec63256a MT |
407 | elif device_is_ethernet ${device}; then |
408 | echo "ethernet" | |
1848564d | 409 | |
6c74a64c MT |
410 | elif device_is_serial ${device}; then |
411 | echo "serial" | |
412 | ||
f90dd58c MT |
413 | elif device_is_phy ${device}; then |
414 | echo "phy" | |
415 | ||
1a02da59 MT |
416 | elif device_is_vti ${device}; then |
417 | echo "vti" | |
418 | ||
1848564d MT |
419 | else |
420 | echo "unknown" | |
421 | fi | |
422 | } | |
423 | ||
1c6a4e30 | 424 | device_is_ethernet_compatible() { |
a4f7ad26 MT |
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 | ||
1c6a4e30 | 432 | device_get_status() { |
711ffac1 | 433 | local device=${1} |
711ffac1 MT |
434 | assert isset device |
435 | ||
3cb2fc42 | 436 | local status=${STATUS_DOWN} |
711ffac1 | 437 | |
3cb2fc42 | 438 | if device_is_up ${device}; then |
711ffac1 | 439 | status=${STATUS_UP} |
711ffac1 | 440 | |
3cb2fc42 MT |
441 | if ! device_has_carrier ${device}; then |
442 | status=${STATUS_NOCARRIER} | |
443 | fi | |
444 | fi | |
711ffac1 MT |
445 | |
446 | echo "${status}" | |
447 | } | |
448 | ||
1c6a4e30 | 449 | device_get_address() { |
1848564d MT |
450 | local device=${1} |
451 | ||
452 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
453 | } | |
454 | ||
1c6a4e30 | 455 | device_set_address() { |
08c5b789 MT |
456 | assert [ $# -eq 2 ] |
457 | ||
458 | local device="${1}" | |
459 | local addr="${2}" | |
1b7a1578 | 460 | |
08c5b789 | 461 | if ! device_exists "${device}"; then |
1b7a1578 MT |
462 | error "Device '${device}' does not exist." |
463 | return ${EXIT_ERROR} | |
464 | fi | |
465 | ||
08c5b789 MT |
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}'" | |
1b7a1578 MT |
473 | |
474 | local up | |
08c5b789 MT |
475 | if device_is_up "${device}"; then |
476 | device_set_down "${device}" | |
1b7a1578 MT |
477 | up=1 |
478 | fi | |
479 | ||
08c5b789 | 480 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
481 | local ret=$? |
482 | ||
483 | if [ "${up}" = "1" ]; then | |
08c5b789 | 484 | device_set_up "${device}" |
1b7a1578 MT |
485 | fi |
486 | ||
487 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 488 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
489 | fi |
490 | ||
491 | return ${ret} | |
1848564d MT |
492 | } |
493 | ||
1c6a4e30 | 494 | device_get() { |
2ae0fb8d | 495 | local device |
711ffac1 MT |
496 | local devices |
497 | ||
2ae0fb8d MT |
498 | for device in ${SYS_CLASS_NET}/*; do |
499 | device=$(basename ${device}) | |
711ffac1 | 500 | |
2ae0fb8d MT |
501 | # bonding_masters is no device |
502 | [ "${device}" = "bonding_masters" ] && continue | |
503 | ||
504 | devices="${devices} ${device}" | |
505 | done | |
711ffac1 MT |
506 | |
507 | echo ${devices} | |
508 | return ${EXIT_OK} | |
509 | } | |
510 | ||
1c6a4e30 | 511 | devices_get_all() { |
711ffac1 | 512 | device_get |
1848564d MT |
513 | } |
514 | ||
515 | # Check if a device has a cable plugged in | |
1c6a4e30 | 516 | device_has_carrier() { |
5bb2429a MT |
517 | local device=${1} |
518 | assert isset device | |
519 | ||
ec63256a MT |
520 | local carrier=$(__device_get_file ${device} carrier) |
521 | [ "${carrier}" = "1" ] | |
1848564d MT |
522 | } |
523 | ||
1c6a4e30 | 524 | device_is_promisc() { |
1e4c26a4 MT |
525 | local device=${1} |
526 | ||
e369be1a | 527 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
528 | } |
529 | ||
1c6a4e30 | 530 | device_set_promisc() { |
cf6e4606 MT |
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 | ||
1848564d | 541 | # Check if the device is free |
1c6a4e30 | 542 | device_is_free() { |
81ed640c | 543 | ! device_is_used $@ |
1848564d MT |
544 | } |
545 | ||
546 | # Check if the device is used | |
1c6a4e30 | 547 | device_is_used() { |
5bb2429a | 548 | local device=${1} |
1848564d | 549 | |
7951525a | 550 | device_has_vlans ${device} && \ |
fb02e543 | 551 | return ${EXIT_OK} |
1848564d | 552 | device_is_bonded ${device} && \ |
fb02e543 | 553 | return ${EXIT_OK} |
81ed640c MT |
554 | device_is_bridge_attached ${device} && \ |
555 | return ${EXIT_OK} | |
1848564d | 556 | |
fb02e543 | 557 | return ${EXIT_ERROR} |
1848564d MT |
558 | } |
559 | ||
1b7a1578 | 560 | # Give the device a new name |
1c6a4e30 | 561 | device_set_name() { |
1848564d | 562 | local source=$1 |
1578dae9 | 563 | local destination=${2} |
1848564d MT |
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 | ||
0e523702 MT |
583 | device_set_master() { |
584 | local device="${1}" | |
585 | assert isset device | |
586 | ||
587 | local master="${2}" | |
588 | assert isset master | |
589 | ||
590 | if ! cmd ip link set "${device}" master "${master}"; then | |
591 | log ERROR "Could not set master ${master} for device ${device}" | |
592 | return ${EXIT_ERROR} | |
593 | fi | |
594 | ||
595 | return ${EXIT_OK} | |
596 | } | |
597 | ||
598 | device_remove_master() { | |
599 | local device="${1}" | |
600 | assert isset device | |
601 | ||
602 | if ! cmd ip link set "${device}" nomaster; then | |
603 | log ERROR "Could not remove master for device ${device}" | |
604 | return ${EXIT_ERROR} | |
605 | fi | |
606 | ||
607 | return ${EXIT_OK} | |
608 | } | |
609 | ||
1848564d | 610 | # Set device up |
1c6a4e30 | 611 | device_set_up() { |
45546be1 | 612 | assert [ $# -eq 1 ] |
1848564d | 613 | |
45546be1 | 614 | local device=${1} |
711ffac1 | 615 | |
1848564d MT |
616 | # Do nothing if device is already up |
617 | device_is_up ${device} && return ${EXIT_OK} | |
618 | ||
f18ee3d4 | 619 | log INFO "Bringing up ${device}" |
81ed640c | 620 | |
f18ee3d4 | 621 | device_set_parent_up ${device} |
45546be1 MT |
622 | if ! cmd ip link set ${device} up; then |
623 | return ${EXIT_ERROR} | |
624 | fi | |
de72bd91 MT |
625 | |
626 | # Set SMP affinity | |
627 | if interrupt_use_smp_affinity; then | |
505ead5d | 628 | device_auto_configure_smp_affinity ${device} |
de72bd91 MT |
629 | fi |
630 | ||
631 | return ${EXIT_OK} | |
1848564d MT |
632 | } |
633 | ||
1c6a4e30 | 634 | device_set_parent_up() { |
81ed640c MT |
635 | local device=${1} |
636 | local parent | |
637 | ||
7951525a MT |
638 | if device_is_vlan ${device}; then |
639 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
640 | |
641 | device_is_up ${parent} && return ${EXIT_OK} | |
642 | ||
643 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
644 | ||
645 | device_set_up ${parent} | |
646 | return $? | |
647 | fi | |
648 | ||
649 | return ${EXIT_OK} | |
650 | } | |
651 | ||
1848564d | 652 | # Set device down |
1c6a4e30 | 653 | device_set_down() { |
45546be1 | 654 | assert [ $# -eq 1 ] |
1848564d | 655 | |
45546be1 | 656 | local device=${1} |
81ed640c MT |
657 | local ret=${EXIT_OK} |
658 | ||
659 | if device_is_up ${device}; then | |
f18ee3d4 | 660 | log INFO "Bringing down ${device}" |
81ed640c | 661 | |
45546be1 | 662 | cmd ip link set ${device} down |
81ed640c MT |
663 | ret=$? |
664 | fi | |
665 | ||
666 | device_set_parent_down ${device} | |
1848564d | 667 | |
81ed640c MT |
668 | return ${ret} |
669 | } | |
670 | ||
1c6a4e30 | 671 | device_set_parent_down() { |
81ed640c MT |
672 | local device=${1} |
673 | local parent | |
674 | ||
7951525a MT |
675 | if device_is_vlan ${device}; then |
676 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
677 | |
678 | device_is_up ${parent} || return ${EXIT_OK} | |
679 | ||
680 | if device_is_free ${parent}; then | |
681 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
682 | ||
683 | device_set_down ${parent} | |
684 | fi | |
685 | fi | |
686 | ||
687 | return ${EXIT_OK} | |
1848564d MT |
688 | } |
689 | ||
1c6a4e30 | 690 | device_get_mtu() { |
1848564d MT |
691 | local device=${1} |
692 | ||
491e4f92 MT |
693 | # Return an error if the device does not exist |
694 | device_exists ${device} || return ${EXIT_ERROR} | |
1848564d | 695 | |
f3e6fe50 | 696 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
697 | } |
698 | ||
699 | # Set mtu to a device | |
1c6a4e30 | 700 | device_set_mtu() { |
1b7a1578 | 701 | local device=${1} |
1848564d MT |
702 | local mtu=${2} |
703 | ||
491e4f92 | 704 | assert device_exists ${device} |
1b7a1578 | 705 | |
3464bc89 AF |
706 | # Handle bridges differently |
707 | if device_is_bridge ${device}; then | |
708 | local port | |
709 | for port in $(bridge_get_members ${device}); do | |
710 | device_set_mtu ${port} ${mtu} | |
711 | done | |
712 | fi | |
713 | ||
491e4f92 | 714 | log INFO "Setting MTU of ${device} to ${mtu}" |
1b7a1578 | 715 | |
1848564d | 716 | local up |
1b7a1578 MT |
717 | if device_is_up ${device}; then |
718 | device_set_down ${device} | |
1848564d MT |
719 | up=1 |
720 | fi | |
721 | ||
491e4f92 MT |
722 | local ret=${EXIT_OK} |
723 | if ! cmd ip link set ${device} mtu ${mtu}; then | |
724 | ret=${EXIT_ERROR} | |
1848564d | 725 | |
491e4f92 | 726 | log ERROR "Could not set MTU ${mtu} on ${device}" |
1b7a1578 MT |
727 | fi |
728 | ||
491e4f92 MT |
729 | if [ "${up}" = "1" ]; then |
730 | device_set_up ${device} | |
1848564d MT |
731 | fi |
732 | ||
733 | return ${ret} | |
734 | } | |
735 | ||
1c6a4e30 | 736 | device_adjust_mtu() { |
3ee5ccb1 MT |
737 | assert [ $# -eq 2 ] |
738 | ||
739 | local device="${1}" | |
740 | local other_device="${2}" | |
741 | ||
742 | local mtu="$(device_get_mtu "${other_device}")" | |
743 | device_set_mtu "${device}" "${mtu}" | |
744 | } | |
745 | ||
1c6a4e30 | 746 | device_discover() { |
1848564d MT |
747 | local device=${1} |
748 | ||
1b7a1578 MT |
749 | log INFO "Running discovery process on device '${device}'." |
750 | ||
1848564d | 751 | local hook |
d61a01d4 MT |
752 | for hook in $(hook_zone_get_all); do |
753 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
754 | done |
755 | } | |
756 | ||
f5ee091e MT |
757 | device_identify() { |
758 | assert [ $# -ge 1 ] | |
759 | ||
760 | local device="${1}" | |
761 | ||
762 | # Flash for ten seconds by default | |
763 | local seconds="10" | |
764 | ||
765 | # Run in background? | |
766 | local background="false" | |
767 | ||
768 | local arg | |
769 | while read arg; do | |
770 | case "${arg}" in | |
771 | --background) | |
772 | background="true" | |
773 | ;; | |
774 | --seconds=*) | |
775 | seconds="$(cli_get_val "${arg}")" | |
776 | ;; | |
777 | esac | |
778 | done <<< "$(args $@)" | |
779 | ||
780 | assert isinteger seconds | |
781 | ||
782 | if ! device_exists "${device}"; then | |
783 | log ERROR "Cannot identify device ${device}: Does not exist" | |
784 | return ${EXIT_ERROR} | |
785 | fi | |
786 | ||
787 | if ! device_is_ethernet "${device}"; then | |
788 | log DEBUG "Cannot identify device ${device}: Not an ethernet device" | |
789 | return ${EXIT_NOT_SUPPORTED} | |
790 | fi | |
791 | ||
792 | log DEBUG "Identifying device ${device}" | |
793 | ||
794 | local command="ethtool --identify ${device} ${seconds}" | |
795 | local ret=0 | |
796 | ||
797 | if enabled background; then | |
798 | cmd_background "${command}" | |
799 | else | |
800 | cmd_quiet "${command}" | |
801 | ret=$? | |
802 | fi | |
803 | ||
804 | return ${ret} | |
805 | } | |
806 | ||
1c6a4e30 | 807 | device_has_ip() { |
1848564d MT |
808 | local device=${1} |
809 | local addr=${2} | |
810 | ||
38f61548 MT |
811 | assert isset addr |
812 | assert device_exists ${device} | |
813 | ||
814 | # IPv6 addresses must be fully imploded | |
815 | local protocol=$(ip_detect_protocol ${addr}) | |
816 | case "${protocol}" in | |
817 | ipv6) | |
13a6e69f | 818 | addr=$(ipv6_format "${addr}") |
38f61548 MT |
819 | ;; |
820 | esac | |
1848564d | 821 | |
8c9205b1 | 822 | list_match ${addr} $(device_get_addresses ${device}) |
1848564d | 823 | } |
4231f419 | 824 | |
1c6a4e30 | 825 | device_get_addresses() { |
4231f419 | 826 | local device=${1} |
4231f419 | 827 | |
38f61548 | 828 | assert device_exists ${device} |
4231f419 | 829 | |
38f61548 MT |
830 | local prot |
831 | local addr | |
832 | local line | |
833 | ip addr show ${device} | \ | |
834 | while read prot addr line; do | |
835 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
836 | done | |
4231f419 | 837 | } |
711ffac1 | 838 | |
1c6a4e30 | 839 | __device_get_file() { |
711ffac1 MT |
840 | local device=${1} |
841 | local file=${2} | |
842 | ||
750aae10 | 843 | fread "${SYS_CLASS_NET}/${device}/${file}" |
711ffac1 MT |
844 | } |
845 | ||
1c6a4e30 | 846 | __device_set_file() { |
2c083d57 MT |
847 | assert [ $# -eq 3 ] |
848 | ||
849 | local device="${1}" | |
850 | local file="${2}" | |
851 | local value="${3}" | |
852 | ||
644d3bb8 | 853 | fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}" |
2c083d57 MT |
854 | } |
855 | ||
1c6a4e30 | 856 | device_get_rx_bytes() { |
711ffac1 MT |
857 | local device=${1} |
858 | ||
859 | __device_get_file ${device} statistics/rx_bytes | |
860 | } | |
861 | ||
1c6a4e30 | 862 | device_get_tx_bytes() { |
711ffac1 MT |
863 | local device=${1} |
864 | ||
865 | __device_get_file ${device} statistics/tx_bytes | |
866 | } | |
867 | ||
1c6a4e30 | 868 | device_get_rx_packets() { |
711ffac1 MT |
869 | local device=${1} |
870 | ||
871 | __device_get_file ${device} statistics/rx_packets | |
872 | } | |
873 | ||
1c6a4e30 | 874 | device_get_tx_packets() { |
711ffac1 MT |
875 | local device=${1} |
876 | ||
877 | __device_get_file ${device} statistics/tx_packets | |
878 | } | |
879 | ||
1c6a4e30 | 880 | device_get_rx_errors() { |
711ffac1 MT |
881 | local device=${1} |
882 | ||
883 | __device_get_file ${device} statistics/rx_errors | |
884 | } | |
885 | ||
1c6a4e30 | 886 | device_get_tx_errors() { |
711ffac1 MT |
887 | local device=${1} |
888 | ||
889 | __device_get_file ${device} statistics/tx_errors | |
890 | } | |
ec63256a | 891 | |
1c6a4e30 | 892 | device_get_speed() { |
ec63256a MT |
893 | local device=${1} |
894 | ||
0d2d02da MT |
895 | local speed=$(__device_get_file ${device} speed) |
896 | ||
f887607c MT |
897 | # Exit for no output (i.e. no link detected) |
898 | isset speed || return ${EXIT_ERROR} | |
899 | ||
0d2d02da MT |
900 | # Don't return anything for negative values |
901 | [ ${speed} -lt 0 ] && return ${EXIT_ERROR} | |
902 | ||
903 | print "${speed}" | |
ec63256a MT |
904 | } |
905 | ||
1c6a4e30 | 906 | device_get_duplex() { |
ec63256a MT |
907 | local device=${1} |
908 | ||
cd0f7e51 MT |
909 | local duplex=$(__device_get_file ${device} duplex) |
910 | ||
911 | case "${duplex}" in | |
912 | unknown) | |
913 | return ${EXIT_ERROR} | |
914 | ;; | |
915 | *) | |
916 | print "${duplex}" | |
917 | ;; | |
918 | esac | |
ec63256a | 919 | } |
657540d8 | 920 | |
1c6a4e30 | 921 | device_get_link_string() { |
657540d8 MT |
922 | local device="${1}" |
923 | assert isset device | |
924 | ||
925 | local s | |
926 | ||
927 | local speed="$(device_get_speed "${device}")" | |
928 | if isset speed; then | |
929 | list_append s "${speed} MBit/s" | |
930 | fi | |
931 | ||
932 | local duplex="$(device_get_duplex "${device}")" | |
933 | if isset duplex; then | |
934 | list_append s "${duplex} duplex" | |
935 | fi | |
936 | ||
937 | print "${s}" | |
938 | } | |
de72bd91 MT |
939 | |
940 | device_auto_configure_smp_affinity() { | |
941 | assert [ $# -eq 1 ] | |
942 | ||
943 | local device=${1} | |
944 | ||
c73dc5dc | 945 | if lock_acquire "smp-affinity" 60; then |
505ead5d | 946 | device_set_smp_affinity ${device} auto |
de72bd91 MT |
947 | |
948 | lock_release "smp-affinity" | |
949 | fi | |
950 | } | |
951 | ||
952 | device_set_smp_affinity() { | |
953 | assert [ $# -eq 2 ] | |
954 | ||
955 | local device=${1} | |
956 | local mode=${2} | |
957 | ||
958 | # mode can be auto which will automatically try to find | |
959 | # the least busy processor, or an integer for the desired | |
960 | # processor that should handle this device | |
961 | ||
962 | local num_processors=$(system_get_processors) | |
963 | ||
964 | if [ "${mode}" = "auto" ]; then | |
965 | local processor=$(interrupt_choose_least_busy_processor) | |
966 | else | |
967 | assert isinteger mode | |
968 | local processor=${mode} | |
969 | ||
970 | if [ ${processor} -gt ${num_processors} ]; then | |
971 | log ERROR "Processor ${processor} does not exist" | |
972 | return ${EXIT_ERROR} | |
973 | fi | |
974 | fi | |
975 | ||
976 | local interrupts=$(interrupts_for_device ${device}) | |
977 | if ! isset interrupts; then | |
978 | log DEBUG "${device} has no interrupts. Not changing SMP affinity" | |
979 | return ${EXIT_OK} | |
980 | fi | |
981 | ||
982 | # Set SMP affinity | |
983 | local interrupt | |
984 | for interrupt in ${interrupts}; do | |
985 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
986 | done | |
987 | ||
988 | # Find all queues and assign them to the next processor | |
989 | local queue | |
990 | for queue in $(device_get_queues ${device}); do | |
991 | case "${queue}" in | |
992 | # Only handle receive queues | |
993 | rx-*) | |
994 | for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do | |
995 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
996 | done | |
997 | ||
998 | device_queue_set_smp_affinity ${device} ${queue} ${processor} | |
999 | ;; | |
1000 | ||
1001 | # Ignore the rest | |
1002 | *) | |
1003 | continue | |
1004 | ;; | |
1005 | esac | |
1006 | ||
1007 | # Get the next available processor if in auto mode | |
1008 | [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor}) | |
1009 | done | |
1010 | ||
1011 | return ${EXIT_OK} | |
1012 | } | |
1013 | ||
1014 | device_get_queues() { | |
1015 | assert [ $# -eq 1 ] | |
1016 | ||
1017 | local device=${1} | |
1018 | ||
1019 | local queue | |
1020 | for queue in ${SYS_CLASS_NET}/${device}/queues/*; do | |
89c73b14 MT |
1021 | [ -d "${queue}" ] || continue |
1022 | ||
de72bd91 MT |
1023 | basename "${queue}" |
1024 | done | |
1025 | } | |
1026 | ||
6529dfaa MT |
1027 | device_supports_multiqueue() { |
1028 | local device=${1} | |
1029 | ||
1030 | local num_queues=$(device_num_queues ${device}) | |
1031 | ||
1032 | if isset num_queues && [ ${num_queues} -gt 2 ]; then | |
1033 | return ${EXIT_TRUE} | |
1034 | fi | |
1035 | ||
1036 | return ${EXIT_FALSE} | |
1037 | } | |
1038 | ||
1039 | device_num_queues() { | |
1040 | local device=${1} | |
1041 | local type=${2} | |
1042 | ||
e396e74e | 1043 | isset type && assert isoneof type rx tx |
6529dfaa MT |
1044 | |
1045 | local i=0 | |
1046 | ||
1047 | local q | |
1048 | for q in $(device_get_queues ${device}); do | |
1049 | case "${type},${q}" in | |
1050 | rx,rx-*) | |
1051 | (( i++ )) | |
1052 | ;; | |
1053 | tx,tx-*) | |
1054 | (( i++ )) | |
1055 | ;; | |
1056 | *,*) | |
1057 | (( i++ )) | |
1058 | ;; | |
1059 | esac | |
1060 | done | |
1061 | ||
1062 | print ${i} | |
1063 | } | |
1064 | ||
1065 | device_queue_get_smp_affinity() { | |
1066 | assert [ $# -eq 2 ] | |
1067 | ||
1068 | local device=${1} | |
1069 | local queue=${2} | |
1070 | ||
e396e74e MT |
1071 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}" |
1072 | ||
1073 | case "${queue}" in | |
1074 | rx-*) | |
1075 | path="${path}/rps_cpus" | |
1076 | ;; | |
1077 | tx-*) | |
1078 | path="${path}/xps_cpus" | |
1079 | ;; | |
1080 | esac | |
6529dfaa MT |
1081 | assert [ -r "${path}" ] |
1082 | ||
1083 | __bitmap_to_processor_ids $(<${path}) | |
1084 | } | |
1085 | ||
de72bd91 MT |
1086 | device_queue_set_smp_affinity() { |
1087 | assert [ $# -eq 3 ] | |
1088 | ||
1089 | local device=${1} | |
1090 | local queue=${2} | |
1091 | local processor=${3} | |
1092 | ||
1093 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus" | |
1094 | assert [ -w "${path}" ] | |
1095 | ||
1096 | log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}" | |
1097 | ||
1098 | __processor_id_to_bitmap ${processor} > ${path} | |
1099 | } |