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