]>
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 | ||
bcba5d27 JS |
289 | device_is_vti6() { |
290 | local device=${1} | |
291 | ||
292 | local type=$(__device_get_file ${device} type) | |
293 | ||
294 | [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
295 | } | |
296 | ||
1c6a4e30 | 297 | device_get_phy() { |
4733a336 MT |
298 | local device="${1}" |
299 | ||
300 | if device_is_wireless "${device}"; then | |
301 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
302 | return ${EXIT_OK} | |
303 | fi | |
304 | ||
305 | return ${EXIT_ERROR} | |
306 | } | |
307 | ||
1c6a4e30 | 308 | device_is_phy() { |
2212045f | 309 | phy_exists "$@" |
f90dd58c MT |
310 | } |
311 | ||
1c6a4e30 | 312 | device_is_serial() { |
2212045f | 313 | serial_exists "$@" |
6c74a64c MT |
314 | } |
315 | ||
2d6dab20 MT |
316 | # Returns true if a device is a tun device |
317 | device_is_tun() { | |
318 | local device="${1}" | |
319 | ||
320 | [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ] | |
321 | } | |
322 | ||
1848564d | 323 | # Check if the device is a physical network interface |
1c6a4e30 | 324 | device_is_ethernet() { |
1848564d MT |
325 | local device=${1} |
326 | ||
0067696a MT |
327 | device_is_ethernet_compatible "${device}" || \ |
328 | return ${EXIT_ERROR} | |
329 | ||
1848564d MT |
330 | device_is_loopback ${device} && \ |
331 | return ${EXIT_ERROR} | |
332 | ||
333 | device_is_bonding ${device} && \ | |
334 | return ${EXIT_ERROR} | |
335 | ||
336 | device_is_bridge ${device} && \ | |
337 | return ${EXIT_ERROR} | |
338 | ||
339 | device_is_ppp ${device} && \ | |
340 | return ${EXIT_ERROR} | |
341 | ||
7951525a | 342 | device_is_vlan ${device} && \ |
1848564d MT |
343 | return ${EXIT_ERROR} |
344 | ||
0067696a | 345 | device_is_dummy ${device} && \ |
419b4cd0 MT |
346 | return ${EXIT_ERROR} |
347 | ||
2d6dab20 MT |
348 | device_is_tun ${device} && \ |
349 | return ${EXIT_ERROR} | |
350 | ||
1848564d MT |
351 | return ${EXIT_OK} |
352 | } | |
353 | ||
354 | # Get the device type | |
1c6a4e30 | 355 | device_get_type() { |
5bb2429a | 356 | local device=${1} |
1848564d | 357 | |
b9f27baf MT |
358 | # If the device does not exist (happens on udev remove events), |
359 | # we do not bother to run all checks. | |
360 | if ! device_exists "${device}"; then | |
361 | echo "unknown" | |
362 | ||
363 | elif device_is_vlan ${device}; then | |
1848564d MT |
364 | echo "vlan" |
365 | ||
366 | elif device_is_bonding ${device}; then | |
367 | echo "bonding" | |
368 | ||
369 | elif device_is_bridge ${device}; then | |
370 | echo "bridge" | |
371 | ||
372 | elif device_is_ppp ${device}; then | |
373 | echo "ppp" | |
374 | ||
375 | elif device_is_loopback ${device}; then | |
376 | echo "loopback" | |
377 | ||
b8026986 MT |
378 | elif device_is_wireless_adhoc ${device}; then |
379 | echo "wireless-adhoc" | |
380 | ||
a508c27e MT |
381 | elif device_is_wireless ${device}; then |
382 | echo "wireless" | |
383 | ||
0067696a MT |
384 | elif device_is_dummy ${device}; then |
385 | echo "dummy" | |
386 | ||
2d6dab20 MT |
387 | elif device_is_tun ${device}; then |
388 | echo "tun" | |
389 | ||
ec63256a MT |
390 | elif device_is_ethernet ${device}; then |
391 | echo "ethernet" | |
1848564d | 392 | |
6c74a64c MT |
393 | elif device_is_serial ${device}; then |
394 | echo "serial" | |
395 | ||
f90dd58c MT |
396 | elif device_is_phy ${device}; then |
397 | echo "phy" | |
398 | ||
e173e612 JS |
399 | else |
400 | echo "$(device_tunnel_get_type "${device}")" | |
401 | fi | |
402 | } | |
403 | ||
404 | # This function just checks the types a ip-tunnel device usually have | |
405 | # so when we know that the device is an ip-tunnel device we save time | |
406 | device_tunnel_get_type() { | |
407 | local device=${1} | |
408 | ||
409 | # If the device does not exist (happens on udev remove events), | |
410 | # we do not bother to run all checks. | |
411 | if ! device_exists "${device}"; then | |
412 | echo "unknown" | |
413 | ||
1a02da59 MT |
414 | elif device_is_vti ${device}; then |
415 | echo "vti" | |
416 | ||
e173e612 JS |
417 | elif device_is_vti6 ${device}; then |
418 | echo "vti6" | |
419 | ||
1848564d MT |
420 | else |
421 | echo "unknown" | |
422 | fi | |
423 | } | |
424 | ||
1c6a4e30 | 425 | device_is_ethernet_compatible() { |
a4f7ad26 MT |
426 | local device="${1}" |
427 | ||
428 | # /sys/class/net/*/type must equal 1 for ethernet compatible devices | |
429 | local type="$(__device_get_file "${device}" "type")" | |
430 | [[ "${type}" = "1" ]] | |
431 | } | |
432 | ||
1c6a4e30 | 433 | device_get_status() { |
711ffac1 | 434 | local device=${1} |
711ffac1 MT |
435 | assert isset device |
436 | ||
3cb2fc42 | 437 | local status=${STATUS_DOWN} |
711ffac1 | 438 | |
3cb2fc42 | 439 | if device_is_up ${device}; then |
711ffac1 | 440 | status=${STATUS_UP} |
711ffac1 | 441 | |
3cb2fc42 MT |
442 | if ! device_has_carrier ${device}; then |
443 | status=${STATUS_NOCARRIER} | |
444 | fi | |
445 | fi | |
711ffac1 MT |
446 | |
447 | echo "${status}" | |
448 | } | |
449 | ||
1c6a4e30 | 450 | device_get_address() { |
1848564d MT |
451 | local device=${1} |
452 | ||
453 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
454 | } | |
455 | ||
1c6a4e30 | 456 | device_set_address() { |
08c5b789 MT |
457 | assert [ $# -eq 2 ] |
458 | ||
459 | local device="${1}" | |
460 | local addr="${2}" | |
1b7a1578 | 461 | |
08c5b789 | 462 | if ! device_exists "${device}"; then |
1b7a1578 MT |
463 | error "Device '${device}' does not exist." |
464 | return ${EXIT_ERROR} | |
465 | fi | |
466 | ||
08c5b789 MT |
467 | # Do nothing if the address has not changed |
468 | local old_addr="$(device_get_address "${device}")" | |
469 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
470 | return ${EXIT_OK} | |
471 | fi | |
472 | ||
473 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
474 | |
475 | local up | |
08c5b789 MT |
476 | if device_is_up "${device}"; then |
477 | device_set_down "${device}" | |
1b7a1578 MT |
478 | up=1 |
479 | fi | |
480 | ||
08c5b789 | 481 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
482 | local ret=$? |
483 | ||
484 | if [ "${up}" = "1" ]; then | |
08c5b789 | 485 | device_set_up "${device}" |
1b7a1578 MT |
486 | fi |
487 | ||
488 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 489 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
490 | fi |
491 | ||
492 | return ${ret} | |
1848564d MT |
493 | } |
494 | ||
1c6a4e30 | 495 | device_get() { |
2ae0fb8d | 496 | local device |
60b1f378 | 497 | for device in $(list_directory "${SYS_CLASS_NET}"); do |
2ae0fb8d MT |
498 | # bonding_masters is no device |
499 | [ "${device}" = "bonding_masters" ] && continue | |
500 | ||
60b1f378 | 501 | echo "${device}" |
2ae0fb8d | 502 | done |
711ffac1 | 503 | |
711ffac1 MT |
504 | return ${EXIT_OK} |
505 | } | |
506 | ||
1848564d | 507 | # Check if a device has a cable plugged in |
1c6a4e30 | 508 | device_has_carrier() { |
5bb2429a MT |
509 | local device=${1} |
510 | assert isset device | |
511 | ||
ec63256a MT |
512 | local carrier=$(__device_get_file ${device} carrier) |
513 | [ "${carrier}" = "1" ] | |
1848564d MT |
514 | } |
515 | ||
1c6a4e30 | 516 | device_is_promisc() { |
1e4c26a4 MT |
517 | local device=${1} |
518 | ||
e369be1a | 519 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
520 | } |
521 | ||
1c6a4e30 | 522 | device_set_promisc() { |
cf6e4606 MT |
523 | local device=${1} |
524 | local state=${2} | |
525 | ||
526 | assert device_exists ${device} | |
527 | assert isset state | |
528 | assert isoneof state on off | |
529 | ||
530 | ip link set ${device} promisc ${state} | |
531 | } | |
532 | ||
1848564d | 533 | # Check if the device is free |
1c6a4e30 | 534 | device_is_free() { |
2212045f | 535 | ! device_is_used "$@" |
1848564d MT |
536 | } |
537 | ||
538 | # Check if the device is used | |
1c6a4e30 | 539 | device_is_used() { |
5bb2429a | 540 | local device=${1} |
1848564d | 541 | |
7951525a | 542 | device_has_vlans ${device} && \ |
fb02e543 | 543 | return ${EXIT_OK} |
1848564d | 544 | device_is_bonded ${device} && \ |
fb02e543 | 545 | return ${EXIT_OK} |
81ed640c MT |
546 | device_is_bridge_attached ${device} && \ |
547 | return ${EXIT_OK} | |
1848564d | 548 | |
fb02e543 | 549 | return ${EXIT_ERROR} |
1848564d MT |
550 | } |
551 | ||
1b7a1578 | 552 | # Give the device a new name |
1c6a4e30 | 553 | device_set_name() { |
1848564d | 554 | local source=$1 |
1578dae9 | 555 | local destination=${2} |
1848564d MT |
556 | |
557 | # Check if devices exists | |
558 | if ! device_exists ${source} || device_exists ${destination}; then | |
559 | return 4 | |
560 | fi | |
561 | ||
562 | local up | |
563 | if device_is_up ${source}; then | |
564 | ip link set ${source} down | |
565 | up=1 | |
566 | fi | |
567 | ||
568 | ip link set ${source} name ${destination} | |
569 | ||
570 | if [ "${up}" = "1" ]; then | |
571 | ip link set ${destination} up | |
572 | fi | |
573 | } | |
574 | ||
0e523702 MT |
575 | device_set_master() { |
576 | local device="${1}" | |
577 | assert isset device | |
578 | ||
579 | local master="${2}" | |
580 | assert isset master | |
581 | ||
582 | if ! cmd ip link set "${device}" master "${master}"; then | |
583 | log ERROR "Could not set master ${master} for device ${device}" | |
584 | return ${EXIT_ERROR} | |
585 | fi | |
586 | ||
587 | return ${EXIT_OK} | |
588 | } | |
589 | ||
590 | device_remove_master() { | |
591 | local device="${1}" | |
592 | assert isset device | |
593 | ||
594 | if ! cmd ip link set "${device}" nomaster; then | |
595 | log ERROR "Could not remove master for device ${device}" | |
596 | return ${EXIT_ERROR} | |
597 | fi | |
598 | ||
599 | return ${EXIT_OK} | |
600 | } | |
601 | ||
1848564d | 602 | # Set device up |
1c6a4e30 | 603 | device_set_up() { |
45546be1 | 604 | assert [ $# -eq 1 ] |
1848564d | 605 | |
45546be1 | 606 | local device=${1} |
711ffac1 | 607 | |
1848564d MT |
608 | # Do nothing if device is already up |
609 | device_is_up ${device} && return ${EXIT_OK} | |
610 | ||
f18ee3d4 | 611 | log INFO "Bringing up ${device}" |
81ed640c | 612 | |
f18ee3d4 | 613 | device_set_parent_up ${device} |
45546be1 MT |
614 | if ! cmd ip link set ${device} up; then |
615 | return ${EXIT_ERROR} | |
616 | fi | |
de72bd91 MT |
617 | |
618 | # Set SMP affinity | |
619 | if interrupt_use_smp_affinity; then | |
505ead5d | 620 | device_auto_configure_smp_affinity ${device} |
de72bd91 MT |
621 | fi |
622 | ||
623 | return ${EXIT_OK} | |
1848564d MT |
624 | } |
625 | ||
1c6a4e30 | 626 | device_set_parent_up() { |
81ed640c MT |
627 | local device=${1} |
628 | local parent | |
629 | ||
7951525a MT |
630 | if device_is_vlan ${device}; then |
631 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
632 | |
633 | device_is_up ${parent} && return ${EXIT_OK} | |
634 | ||
635 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
636 | ||
637 | device_set_up ${parent} | |
638 | return $? | |
639 | fi | |
640 | ||
641 | return ${EXIT_OK} | |
642 | } | |
643 | ||
1848564d | 644 | # Set device down |
1c6a4e30 | 645 | device_set_down() { |
45546be1 | 646 | assert [ $# -eq 1 ] |
1848564d | 647 | |
45546be1 | 648 | local device=${1} |
81ed640c MT |
649 | local ret=${EXIT_OK} |
650 | ||
651 | if device_is_up ${device}; then | |
f18ee3d4 | 652 | log INFO "Bringing down ${device}" |
81ed640c | 653 | |
45546be1 | 654 | cmd ip link set ${device} down |
81ed640c MT |
655 | ret=$? |
656 | fi | |
657 | ||
658 | device_set_parent_down ${device} | |
1848564d | 659 | |
81ed640c MT |
660 | return ${ret} |
661 | } | |
662 | ||
1c6a4e30 | 663 | device_set_parent_down() { |
81ed640c MT |
664 | local device=${1} |
665 | local parent | |
666 | ||
7951525a MT |
667 | if device_is_vlan ${device}; then |
668 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
669 | |
670 | device_is_up ${parent} || return ${EXIT_OK} | |
671 | ||
672 | if device_is_free ${parent}; then | |
673 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
674 | ||
675 | device_set_down ${parent} | |
676 | fi | |
677 | fi | |
678 | ||
679 | return ${EXIT_OK} | |
1848564d MT |
680 | } |
681 | ||
1c6a4e30 | 682 | device_get_mtu() { |
1848564d MT |
683 | local device=${1} |
684 | ||
491e4f92 MT |
685 | # Return an error if the device does not exist |
686 | device_exists ${device} || return ${EXIT_ERROR} | |
1848564d | 687 | |
f3e6fe50 | 688 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
689 | } |
690 | ||
691 | # Set mtu to a device | |
1c6a4e30 | 692 | device_set_mtu() { |
1b7a1578 | 693 | local device=${1} |
1848564d MT |
694 | local mtu=${2} |
695 | ||
491e4f92 | 696 | assert device_exists ${device} |
1b7a1578 | 697 | |
3464bc89 AF |
698 | # Handle bridges differently |
699 | if device_is_bridge ${device}; then | |
700 | local port | |
701 | for port in $(bridge_get_members ${device}); do | |
702 | device_set_mtu ${port} ${mtu} | |
703 | done | |
704 | fi | |
705 | ||
491e4f92 | 706 | log INFO "Setting MTU of ${device} to ${mtu}" |
1b7a1578 | 707 | |
1848564d | 708 | local up |
1b7a1578 MT |
709 | if device_is_up ${device}; then |
710 | device_set_down ${device} | |
1848564d MT |
711 | up=1 |
712 | fi | |
713 | ||
491e4f92 MT |
714 | local ret=${EXIT_OK} |
715 | if ! cmd ip link set ${device} mtu ${mtu}; then | |
716 | ret=${EXIT_ERROR} | |
1848564d | 717 | |
491e4f92 | 718 | log ERROR "Could not set MTU ${mtu} on ${device}" |
1b7a1578 MT |
719 | fi |
720 | ||
491e4f92 MT |
721 | if [ "${up}" = "1" ]; then |
722 | device_set_up ${device} | |
1848564d MT |
723 | fi |
724 | ||
725 | return ${ret} | |
726 | } | |
727 | ||
1c6a4e30 | 728 | device_adjust_mtu() { |
3ee5ccb1 MT |
729 | assert [ $# -eq 2 ] |
730 | ||
731 | local device="${1}" | |
732 | local other_device="${2}" | |
733 | ||
734 | local mtu="$(device_get_mtu "${other_device}")" | |
735 | device_set_mtu "${device}" "${mtu}" | |
736 | } | |
737 | ||
1c6a4e30 | 738 | device_discover() { |
1848564d MT |
739 | local device=${1} |
740 | ||
1b7a1578 MT |
741 | log INFO "Running discovery process on device '${device}'." |
742 | ||
1848564d | 743 | local hook |
d61a01d4 MT |
744 | for hook in $(hook_zone_get_all); do |
745 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
746 | done |
747 | } | |
748 | ||
f5ee091e MT |
749 | device_identify() { |
750 | assert [ $# -ge 1 ] | |
751 | ||
752 | local device="${1}" | |
753 | ||
754 | # Flash for ten seconds by default | |
755 | local seconds="10" | |
756 | ||
757 | # Run in background? | |
758 | local background="false" | |
759 | ||
760 | local arg | |
761 | while read arg; do | |
762 | case "${arg}" in | |
763 | --background) | |
764 | background="true" | |
765 | ;; | |
766 | --seconds=*) | |
767 | seconds="$(cli_get_val "${arg}")" | |
768 | ;; | |
769 | esac | |
2212045f | 770 | done <<< "$(args "$@")" |
f5ee091e MT |
771 | |
772 | assert isinteger seconds | |
773 | ||
774 | if ! device_exists "${device}"; then | |
775 | log ERROR "Cannot identify device ${device}: Does not exist" | |
776 | return ${EXIT_ERROR} | |
777 | fi | |
778 | ||
779 | if ! device_is_ethernet "${device}"; then | |
780 | log DEBUG "Cannot identify device ${device}: Not an ethernet device" | |
781 | return ${EXIT_NOT_SUPPORTED} | |
782 | fi | |
783 | ||
784 | log DEBUG "Identifying device ${device}" | |
785 | ||
786 | local command="ethtool --identify ${device} ${seconds}" | |
787 | local ret=0 | |
788 | ||
789 | if enabled background; then | |
790 | cmd_background "${command}" | |
791 | else | |
792 | cmd_quiet "${command}" | |
793 | ret=$? | |
794 | fi | |
795 | ||
796 | return ${ret} | |
797 | } | |
798 | ||
1c6a4e30 | 799 | device_has_ip() { |
1848564d MT |
800 | local device=${1} |
801 | local addr=${2} | |
802 | ||
38f61548 MT |
803 | assert isset addr |
804 | assert device_exists ${device} | |
805 | ||
806 | # IPv6 addresses must be fully imploded | |
807 | local protocol=$(ip_detect_protocol ${addr}) | |
808 | case "${protocol}" in | |
809 | ipv6) | |
13a6e69f | 810 | addr=$(ipv6_format "${addr}") |
38f61548 MT |
811 | ;; |
812 | esac | |
1848564d | 813 | |
8c9205b1 | 814 | list_match ${addr} $(device_get_addresses ${device}) |
1848564d | 815 | } |
4231f419 | 816 | |
1c6a4e30 | 817 | device_get_addresses() { |
4231f419 | 818 | local device=${1} |
4231f419 | 819 | |
38f61548 | 820 | assert device_exists ${device} |
4231f419 | 821 | |
38f61548 MT |
822 | local prot |
823 | local addr | |
824 | local line | |
825 | ip addr show ${device} | \ | |
826 | while read prot addr line; do | |
827 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
828 | done | |
4231f419 | 829 | } |
711ffac1 | 830 | |
1c6a4e30 | 831 | __device_get_file() { |
711ffac1 MT |
832 | local device=${1} |
833 | local file=${2} | |
834 | ||
750aae10 | 835 | fread "${SYS_CLASS_NET}/${device}/${file}" |
711ffac1 MT |
836 | } |
837 | ||
1c6a4e30 | 838 | __device_set_file() { |
2c083d57 MT |
839 | assert [ $# -eq 3 ] |
840 | ||
841 | local device="${1}" | |
842 | local file="${2}" | |
843 | local value="${3}" | |
844 | ||
644d3bb8 | 845 | fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}" |
2c083d57 MT |
846 | } |
847 | ||
1c6a4e30 | 848 | device_get_rx_bytes() { |
711ffac1 MT |
849 | local device=${1} |
850 | ||
851 | __device_get_file ${device} statistics/rx_bytes | |
852 | } | |
853 | ||
1c6a4e30 | 854 | device_get_tx_bytes() { |
711ffac1 MT |
855 | local device=${1} |
856 | ||
857 | __device_get_file ${device} statistics/tx_bytes | |
858 | } | |
859 | ||
1c6a4e30 | 860 | device_get_rx_packets() { |
711ffac1 MT |
861 | local device=${1} |
862 | ||
863 | __device_get_file ${device} statistics/rx_packets | |
864 | } | |
865 | ||
1c6a4e30 | 866 | device_get_tx_packets() { |
711ffac1 MT |
867 | local device=${1} |
868 | ||
869 | __device_get_file ${device} statistics/tx_packets | |
870 | } | |
871 | ||
1c6a4e30 | 872 | device_get_rx_errors() { |
711ffac1 MT |
873 | local device=${1} |
874 | ||
875 | __device_get_file ${device} statistics/rx_errors | |
876 | } | |
877 | ||
1c6a4e30 | 878 | device_get_tx_errors() { |
711ffac1 MT |
879 | local device=${1} |
880 | ||
881 | __device_get_file ${device} statistics/tx_errors | |
882 | } | |
ec63256a | 883 | |
1c6a4e30 | 884 | device_get_speed() { |
ec63256a MT |
885 | local device=${1} |
886 | ||
0d2d02da MT |
887 | local speed=$(__device_get_file ${device} speed) |
888 | ||
f887607c MT |
889 | # Exit for no output (i.e. no link detected) |
890 | isset speed || return ${EXIT_ERROR} | |
891 | ||
0d2d02da MT |
892 | # Don't return anything for negative values |
893 | [ ${speed} -lt 0 ] && return ${EXIT_ERROR} | |
894 | ||
895 | print "${speed}" | |
ec63256a MT |
896 | } |
897 | ||
3a0e1dda MT |
898 | device_set_speed() { |
899 | local device="${1}" | |
900 | assert isset device | |
901 | ||
902 | local speed="${2}" | |
903 | assert isinteger speed | |
904 | ||
905 | if ! cmd_quiet ethtool --change "${device}" speed "${speed}"; then | |
906 | log ERROR "Could not set speed of ${device} to ${speed} MBit/s" | |
907 | return ${EXIT_ERROR} | |
908 | fi | |
909 | ||
910 | log DEBUG "Set speed of ${device} to ${speed} MBit/s" | |
911 | ||
912 | return ${EXIT_OK} | |
913 | } | |
914 | ||
1c6a4e30 | 915 | device_get_duplex() { |
ec63256a MT |
916 | local device=${1} |
917 | ||
cd0f7e51 MT |
918 | local duplex=$(__device_get_file ${device} duplex) |
919 | ||
920 | case "${duplex}" in | |
921 | unknown) | |
922 | return ${EXIT_ERROR} | |
923 | ;; | |
924 | *) | |
925 | print "${duplex}" | |
926 | ;; | |
927 | esac | |
ec63256a | 928 | } |
657540d8 | 929 | |
178aabc0 MT |
930 | device_set_duplex() { |
931 | local device="${1}" | |
932 | assert isset device | |
933 | ||
934 | local duplex="${2}" | |
935 | assert isset duplex | |
936 | ||
937 | if ! cmd_quiet ethtool --change "${device}" duplex "${duplex}"; then | |
938 | log ERROR "Could not set ${device} to ${duplex}-duplex mode" | |
939 | return ${EXIT_ERROR} | |
940 | fi | |
941 | ||
942 | log DEBUG "Set ${device} to ${duplex}-duplex mode" | |
943 | return ${EXIT_OK} | |
944 | } | |
945 | ||
1c6a4e30 | 946 | device_get_link_string() { |
657540d8 MT |
947 | local device="${1}" |
948 | assert isset device | |
949 | ||
950 | local s | |
951 | ||
952 | local speed="$(device_get_speed "${device}")" | |
953 | if isset speed; then | |
954 | list_append s "${speed} MBit/s" | |
955 | fi | |
956 | ||
957 | local duplex="$(device_get_duplex "${device}")" | |
958 | if isset duplex; then | |
959 | list_append s "${duplex} duplex" | |
960 | fi | |
961 | ||
962 | print "${s}" | |
963 | } | |
de72bd91 MT |
964 | |
965 | device_auto_configure_smp_affinity() { | |
966 | assert [ $# -eq 1 ] | |
967 | ||
968 | local device=${1} | |
969 | ||
c73dc5dc | 970 | if lock_acquire "smp-affinity" 60; then |
505ead5d | 971 | device_set_smp_affinity ${device} auto |
de72bd91 MT |
972 | |
973 | lock_release "smp-affinity" | |
974 | fi | |
975 | } | |
976 | ||
977 | device_set_smp_affinity() { | |
978 | assert [ $# -eq 2 ] | |
979 | ||
980 | local device=${1} | |
981 | local mode=${2} | |
982 | ||
983 | # mode can be auto which will automatically try to find | |
984 | # the least busy processor, or an integer for the desired | |
985 | # processor that should handle this device | |
986 | ||
987 | local num_processors=$(system_get_processors) | |
988 | ||
989 | if [ "${mode}" = "auto" ]; then | |
990 | local processor=$(interrupt_choose_least_busy_processor) | |
991 | else | |
992 | assert isinteger mode | |
993 | local processor=${mode} | |
994 | ||
995 | if [ ${processor} -gt ${num_processors} ]; then | |
996 | log ERROR "Processor ${processor} does not exist" | |
997 | return ${EXIT_ERROR} | |
998 | fi | |
999 | fi | |
1000 | ||
1001 | local interrupts=$(interrupts_for_device ${device}) | |
1002 | if ! isset interrupts; then | |
1003 | log DEBUG "${device} has no interrupts. Not changing SMP affinity" | |
1004 | return ${EXIT_OK} | |
1005 | fi | |
1006 | ||
1007 | # Set SMP affinity | |
1008 | local interrupt | |
1009 | for interrupt in ${interrupts}; do | |
1010 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
1011 | done | |
1012 | ||
1013 | # Find all queues and assign them to the next processor | |
1014 | local queue | |
1015 | for queue in $(device_get_queues ${device}); do | |
1016 | case "${queue}" in | |
1017 | # Only handle receive queues | |
1018 | rx-*) | |
1019 | for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do | |
1020 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
1021 | done | |
1022 | ||
1023 | device_queue_set_smp_affinity ${device} ${queue} ${processor} | |
1024 | ;; | |
1025 | ||
1026 | # Ignore the rest | |
1027 | *) | |
1028 | continue | |
1029 | ;; | |
1030 | esac | |
1031 | ||
1032 | # Get the next available processor if in auto mode | |
1033 | [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor}) | |
1034 | done | |
1035 | ||
1036 | return ${EXIT_OK} | |
1037 | } | |
1038 | ||
1039 | device_get_queues() { | |
1040 | assert [ $# -eq 1 ] | |
1041 | ||
1042 | local device=${1} | |
1043 | ||
60b1f378 | 1044 | list_directory "${SYS_CLASS_NET}/${device}/queues" |
de72bd91 MT |
1045 | } |
1046 | ||
6529dfaa MT |
1047 | device_supports_multiqueue() { |
1048 | local device=${1} | |
1049 | ||
1050 | local num_queues=$(device_num_queues ${device}) | |
1051 | ||
1052 | if isset num_queues && [ ${num_queues} -gt 2 ]; then | |
1053 | return ${EXIT_TRUE} | |
1054 | fi | |
1055 | ||
1056 | return ${EXIT_FALSE} | |
1057 | } | |
1058 | ||
1059 | device_num_queues() { | |
1060 | local device=${1} | |
1061 | local type=${2} | |
1062 | ||
e396e74e | 1063 | isset type && assert isoneof type rx tx |
6529dfaa MT |
1064 | |
1065 | local i=0 | |
1066 | ||
1067 | local q | |
1068 | for q in $(device_get_queues ${device}); do | |
1069 | case "${type},${q}" in | |
1070 | rx,rx-*) | |
1071 | (( i++ )) | |
1072 | ;; | |
1073 | tx,tx-*) | |
1074 | (( i++ )) | |
1075 | ;; | |
1076 | *,*) | |
1077 | (( i++ )) | |
1078 | ;; | |
1079 | esac | |
1080 | done | |
1081 | ||
1082 | print ${i} | |
1083 | } | |
1084 | ||
1085 | device_queue_get_smp_affinity() { | |
1086 | assert [ $# -eq 2 ] | |
1087 | ||
1088 | local device=${1} | |
1089 | local queue=${2} | |
1090 | ||
e396e74e MT |
1091 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}" |
1092 | ||
1093 | case "${queue}" in | |
1094 | rx-*) | |
1095 | path="${path}/rps_cpus" | |
1096 | ;; | |
1097 | tx-*) | |
1098 | path="${path}/xps_cpus" | |
1099 | ;; | |
1100 | esac | |
6529dfaa MT |
1101 | assert [ -r "${path}" ] |
1102 | ||
1103 | __bitmap_to_processor_ids $(<${path}) | |
1104 | } | |
1105 | ||
de72bd91 MT |
1106 | device_queue_set_smp_affinity() { |
1107 | assert [ $# -eq 3 ] | |
1108 | ||
1109 | local device=${1} | |
1110 | local queue=${2} | |
1111 | local processor=${3} | |
1112 | ||
1113 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus" | |
1114 | assert [ -w "${path}" ] | |
1115 | ||
1116 | log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}" | |
1117 | ||
1118 | __processor_id_to_bitmap ${processor} > ${path} | |
1119 | } | |
905b13b3 JS |
1120 | |
1121 | # Tries to find a device which has the given IP address assigned | |
1122 | device_get_by_assigned_ip_address() { | |
1123 | local ip=${1} | |
1124 | ||
1125 | assert isset ip | |
1126 | ||
1127 | local device | |
1128 | ||
1129 | # Read the first line of ip addr show to | |
1130 | read -r device <<< $(ip addr show to "${ip}") | |
1131 | ||
1132 | # If we did not found a device we return with ${EXIT_ERROR} | |
1133 | if ! isset device; then | |
1134 | return ${EXIT_ERROR} | |
1135 | fi | |
1136 | ||
1137 | # We get something like: | |
1138 | # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 | |
1139 | # and we want upl0 so we take the second word and removing the : | |
1140 | device=(${device}) | |
1141 | device=${device[1]} | |
1142 | device=${device%:} | |
1143 | ||
1144 | print "${device}" | |
1145 | return ${EXIT_OK} | |
1146 | } | |
e8253eba JS |
1147 | |
1148 | device_get_by_mac_address() { | |
1149 | local mac=${1} | |
1150 | ||
1151 | assert isset mac | |
1152 | ||
1153 | local device | |
1154 | ||
1155 | for device in $(device_list); do | |
1156 | if [ "${mac}" = "$(device_get_address ${device})" ]; then | |
1157 | print "${device}" | |
1158 | return ${EXIT_OK} | |
1159 | fi | |
1160 | done | |
1161 | ||
1162 | # We could not found a port to the given mac address so we return exit error | |
1163 | return ${EXIT_ERROR} | |
1164 | } |