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