Repository navigation
Expand file tree
/
Copy pathlinux_ethernet_optimization.sh
More file actions
executable file
·1445 lines (1225 loc) · 34.8 KB
/
Copy pathlinux_ethernet_optimization.sh
File metadata and controls
executable file
·1445 lines (1225 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# -*- mode: sh-mode; -*-
# vim: filetype=sh
#set -euo pipefail
#IFS=$'\n\t'
shopt -s extglob
# ========================
# Critical design decisions (DO NOT change behavior)
# 1) RFS disabled by default: production safety (hash collision causes ksoftirq 100%)
# 2) mlx5 queue count / 2: mlx5e exposes both normal and XSK queues together
# 3) systemd detection: INVOCATION_ID / SYSTEMD_EXEC_PID / LAUNCHED_BY_SYSTEMD
# 4) is_sourced behavior: script can be sourced as API
# 5) >64 CPU mask calculation: bash cannot handle big integers, needs fallback
# 6) 10-second delay: bonding scenario prevents both ports resetting simultaneously
# ========================
# dependencies: ethtool, lspci, bash version >=4, grep, GNU sed column
# optional: calc, bc, or python3 (needed only for systems with >64 CPUs)
check_script_requirements() {
for command in ethtool lspci grep sed column; do
if [ -z "$(command -v "${command}")" ]; then
echo "ERROR: you need ${command} command to run the script" >&2
return 1
fi
done
if ! sed --version >/dev/null 2>&1; then
echo "ERROR: you need GNU version of sed to run the script" >&2
return 1
fi
if [ -z "$BASH_VERSION" ]; then
echo "ERROR: this script requires bash shell" >&2
return 1
fi
local bash_major_version="${BASH_VERSION%%.*}"
if [ "${bash_major_version}" -lt 4 ]; then
echo "ERROR: this script requires bash version 4 or higher (current: ${BASH_VERSION})" >&2
return 1
fi
return 0
}
check_root() {
test ${EUID} -eq 0
}
# check if the script is running by systemd or not
run_by_systemd() {
# systemd v232 added $INVOCATION_ID
# systemd v248 added $SYSTEMD_EXEC_PID
# man 5 systemd.exec for more information
# NOTE: there's no easy way to determine for systemd version before v232
# if you use systemd version before v232
# you have to setup environment in the systemd service file
# Environment=LAUNCHED_BY_SYSTEMD=1
# systemd detection must be preserved to avoid misdetection in boot scenario
[ -n "$INVOCATION_ID" ] || [ -n "$SYSTEMD_EXEC_PID" ] || [ -n "$LAUNCHED_BY_SYSTEMD" ]
}
# if the script is sourced by other scripts, or executed directly
# NOTE: this function doesn't support zsh
# This detection method relies on bash-specific behavior of $0
is_sourced() {
case ${0##*/} in
dash | -dash | bash | -bash | ksh | -ksh | sh | -sh)
return 0
;;
esac
return 1 # NOT sourced.
}
add_to_run_queue() {
run_queue+=("$@")
}
confirm_and_run() {
local REPLY
local DO_ACTION
if [ "${dry_run}" = "true" ]; then
DO_ACTION=false
elif [ "${assume_yes}" = "true" ]; then
DO_ACTION=true
else
read -p "Perform Optimization[yn]: " -n 1 -r </dev/tty
echo
if [[ "$REPLY" =~ ^[Yy] ]]; then
DO_ACTION=true
else
DO_ACTION=false
fi
fi
if [ "${DO_ACTION}" = "true" ]; then
if "$@"; then
:
# echo "Finished Optimization"
else
:
#echo "Failed Performing Optimization"
fi
else
:
#echo -e "Ignored"
fi
}
get_number_of_cpus() {
grep -c processor /proc/cpuinfo
}
get_running_rx_queues_of_device() {
local ETH=$1
find /sys/class/net/"${ETH}"/queues/ -type d -name "rx-*"
}
get_running_tx_queues_of_device() {
local ETH=$1
find /sys/class/net/"${ETH}"/queues/ -type d -name "tx-*"
}
_sysfs_read() {
local path=$1
cat "${path}"
}
_sysfs_write() {
local path=$1
local value=$2
echo "${value}" >"${path}"
}
set_rps_mask_for_queue() {
local QUEUE=$1
# the cpu mask to be set
local MASK=$2
_sysfs_write "${QUEUE}/rps_cpus" "${MASK}"
}
set_xps_mask_for_queue() {
local QUEUE=$1
# the cpu mask to be set
local MASK=$2
_sysfs_write "${QUEUE}/xps_cpus" "${MASK}"
}
get_rps_mask_from_queue() {
local QUEUE=$1
_sysfs_read "${QUEUE}/rps_cpus"
}
get_xps_mask_from_queue() {
local QUEUE=$1
_sysfs_read "${QUEUE}/xps_cpus"
}
set_rfs_for_queue() {
local QUEUE=$1
# the cpu mask to be set
local CNT=$2
_sysfs_write "${QUEUE}/rps_flow_cnt" "${CNT}"
}
get_rfs_from_queue() {
local QUEUE=$1
_sysfs_read "${QUEUE}/rps_flow_cnt"
}
get_rfs_flow_entries() {
_sysfs_read /proc/sys/net/core/rps_sock_flow_entries
}
set_rfs_flow_entries() {
local number=$1
_sysfs_write /proc/sys/net/core/rps_sock_flow_entries "${number}"
}
# query ethernet queres seen in the kernel
get_ethernet_kernel_queues_number() {
local ETH=$1
# INFO: one of RX, TX (case insensitive due to ${2,,})
local INFO=${2,,}
# mlx5e exposes two groups of queues: normal + XSK (AF_XDP)
# Must divide by 2 for actual queue count in production
nm_queues="$(find /sys/class/net/"${ETH}"/queues -type d -name "${INFO}-*" | wc -l)"
if [[ "$(get_driver_of_ethernet_card "${ETH}")" == "mlx5"* ]]; then
echo "$((nm_queues / 2))"
else
echo "${nm_queues}"
fi
}
# query ethernet queue info
# including hardware capabilities and current settings
get_ethernet_hardware_queues_number() {
# ethernet card names, such as eno1
local ETH=$1
# what info to query, one of RX TX or Combined
local INFO=$2
# max or current
local TYPE=$3
case "${INFO}" in
"RX" | "TX" | "Other" | "Combined") ;;
*)
echo "Unrecognized INFO ${INFO}" >&2
return 1
;;
esac
_ethtool_extract_value "${ETH}" "${INFO}" "${TYPE}" "-l"
}
# set ethernet hardware queue number
# this may reset the ethernet card
set_ethernet_hardware_queues_number() {
# ethernet card names, such as eno1
local ETH=$1
# what info to query, one of RX or TX
local INFO=$2
local VALUE=$3
case "${INFO}" in
"RX" | "TX" | "Other" | "Combined")
# ,, is used to convert uppercase to lowercase
# this feature requires at least bash version 4
ethtool -L "${ETH}" "${INFO,,}" "${VALUE}"
return $?
;;
*)
echo "Unrecognized INFO ${INFO}" >&2
return 1
;;
esac
}
# query ethernet ringbuffer info
# including hardware capabilities and current settings
get_ethernet_hardware_ringbuffer() {
# ethernet card names, such as eno1
local ETH=$1
# what info to query, one of RX or TX
local INFO=$2
# max or current
local TYPE=$3
case "${INFO}" in
"RX" | "TX") ;;
*)
echo "Unrecognized INFO ${INFO}" >&2
return 1
;;
esac
_ethtool_extract_value "${ETH}" "${INFO}" "${TYPE}" "-g"
}
# Parse ethtool output and extract specific value
# ASSUMPTIONS:
# - ethtool output contains exact strings "Pre-set maximums:" and "Current hardware settings:"
# - INFO field format: "<INFO>: <value>"
# KNOWN FAILURES:
# - Different NIC firmware may change output format
# - Older ethtool versions may not support -l/-g flags
# FALLBACK: Returns empty string if parsing fails
_ethtool_extract_value() {
local ETH=$1
local INFO=$2
local TYPE=$3
local MODE=$4
local current_string="Current hardware settings:"
local max_string="Pre-set maximums:"
local started=false
local start_string
local stop_string
case "${TYPE}" in
"max")
start_string="${max_string}"
stop_string="${current_string}"
;;
"current")
start_string="${current_string}"
stop_string="end_of_the_file_not_exist"
;;
*)
echo "Unrecognized TYPE ${TYPE}" >&2
return 1
;;
esac
while read -r line; do
if [ "$line" = "${start_string}" ]; then
started=true
elif [ "$line" = "${stop_string}" ]; then
return
elif [ "${started}" = "true" ] && [[ $line =~ ^${INFO}:[[:space:]]* ]]; then
echo -n "$line" | grep "${INFO}" | cut -d : -f 2 | xargs
# There should be only one
return
fi
done < <(ethtool "${MODE}" "${ETH}")
}
# set ethernet ringbuffer info
# this may reset the ethernet card
set_ethernet_hardware_ringbuffer() {
# ethernet card names, such as eno1
local ETH=$1
# what info to query, one of RX or TX
local INFO=$2
local VALUE=$3
case "${INFO}" in
"RX" | "TX")
# ,, is used to convert uppercase to lowercase
# this feature requires at least bash version 4
ethtool -G "${ETH}" "${INFO,,}" "${VALUE}"
return $?
;;
*)
echo "Unrecognized INFO ${INFO}" >&2
return 1
;;
esac
}
# bignum_calc - Arbitrary precision calculator with multiple backend fallback
#
# USAGE: bignum_calc <mode> <expression>
#
# PARAMETERS:
# mode Output format: "hex", "bin", "oct", "default" (decimal)
# expression Math expression (supports: +, -, *, /, ^, parentheses)
#
# RETURNS:
# Calculation result in specified format
#
# BACKENDS (fallback order):
# 1. bash (CPU ≤ 64 cores only)
# 2. bc (recommended for > 64 cores)
# 3. python3 (fallback)
# 4. calc (last resort)
#
# EXAMPLE:
# bignum_calc hex "2 ^ 128 - 1"
# # Output: ffffffffffffffffffffffffffffffff
bignum_calc() {
# mode: bin for base2, oct for base8, default for base10, hex for base 16
local mode="${1:-default}"
shift
local expr="$*"
local result
local nr_cpu
nr_cpu="$(get_number_of_cpus)"
if [ "${nr_cpu}" -le 64 ]; then
result="$(_calc_with_bash "${mode}" "${expr}")"
echo "${result}"
return 0
fi
if command -v bc >/dev/null 2>&1; then
result="$(_calc_with_bc "${mode}" "${expr}")"
echo "${result}"
return 0
fi
if command -v python3 >/dev/null 2>&1; then
result="$(_calc_with_python "${mode}" "${expr}")"
echo "${result}"
return 0
fi
if command -v calc >/dev/null 2>&1; then
result="$(_calc_with_calc "${mode}" "${expr}")"
echo "${result}"
return 0
fi
echo "ERROR: no suitable calculator found (need bc, python3, or calc for >64 CPUs)" >&2
return 1
}
_calc_with_bash() {
local mode=$1
local expr=$2
local value
local tmp
local power
if [[ "${expr}" =~ ^2[[:space:]]*\^[[:space:]]*([0-9]+)[[:space:]]*-[[:space:]]*1$ ]]; then
power="${BASH_REMATCH[1]}"
if [ "${mode}" = "hex" ] && [ "${power}" -eq 64 ]; then
echo ffffffffffffffff
return 0
fi
fi
value=$((${expr//^/**}))
case "${mode}" in
bin)
if [ "${value}" -eq 0 ]; then
echo 0
return 0
fi
while [ "${value}" -gt 0 ]; do
tmp="$((value & 1))${tmp}"
value=$((value >> 1))
done
echo "${tmp}"
;;
oct)
printf "%o" "${value}"
;;
default)
echo "${value}"
;;
hex)
printf "%x" "${value}"
;;
esac
}
_calc_with_bc() {
local mode=$1
local expr=$2
local result
case "${mode}" in
bin)
result="$(echo "ibase=10; obase=2; ${expr}" | bc)"
;;
oct)
result="$(echo "ibase=10; obase=8; ${expr}" | bc)"
;;
default)
result="$(echo "scale=0; ${expr}" | bc)"
;;
hex)
result="$(echo "ibase=10; obase=16; ${expr}" | bc)"
;;
esac
echo "${result,,}"
}
_calc_with_python() {
local mode=$1
local expr=$2
local result
# SECURITY NOTE: Expression comes from internal script logic only
# If future modifications allow external input, add validation here
result="$(
python3 - <<PY
import sys
# Expression sanitized by script logic - only arithmetic operators used
expr = "${expr}".replace('^', '**')
value = eval(expr, {"__builtins__": {}}, {})
mode = "${mode}"
if mode == "hex":
print(hex(value)[2:])
elif mode == "bin":
print(bin(value)[2:])
elif mode == "oct":
print(oct(value)[2:])
else:
print(value)
PY
)"
echo "${result,,}"
}
_calc_with_calc() {
local mode=$1
local expr=$2
local result
result="$(calc -m0 -e -d "a=config('display',0); a=config('mode','${mode}'); ${expr}" | xargs)"
case "${mode}" in
bin)
if [[ "${result}" == 0b* ]]; then
result="${result#0b}"
fi
;;
oct)
if [[ "${result}" == 0* ]] && [[ "${result}" != "0" ]]; then
result="${result#0}"
fi
;;
hex)
if [[ "${result}" == 0x* ]]; then
result="${result#0x}"
fi
;;
default) ;;
esac
echo "${result,,}"
}
format_cpumask() {
local mask="$1"
# Insert comma every 8 hex chars per cpuset specification
# GNU sed specific (not POSIX compatible, not BSD sed compatible)
echo "${mask}" | sed ':a;s/\B.\{8\}\>/,&/;ta'
}
# generate a cpuset CPU mask that uses all cpu
# man 7 cpuset, go for FORMAT section for detailed format description
generate_cpus_mask_for_all_cpu() {
local NR_CPU
NR_CPU="$(get_number_of_cpus)"
format_cpumask "$(bignum_calc hex "2 ^ ${NR_CPU} - 1")"
}
# generate_cpus_mask - Generate CPU mask for round-robin queue distribution
#
# USAGE: generate_cpus_mask <number> <total>
#
# PARAMETERS:
# number Starting queue number (0-based)
# total Total number of queues
#
# ALGORITHM:
# Distributes queues across CPUs in round-robin fashion.
# For queue N, assigns to CPU (N mod NR_CPU), then (N+total mod NR_CPU), etc.
#
# RETURNS:
# Formatted CPU mask (comma-separated 8-hex-digit groups per cpuset spec)
#
# EXAMPLE:
# generate_cpus_mask 0 4 # Queue 0 of 4 total
# # On 8-CPU system: assigns CPUs 0,4 → mask: 00000011
#
# man 7 cpuset, go to FORMAT section for detailed format description
generate_cpus_mask() {
local number=$1
local total=$2
local NR_CPU
NR_CPU=$(get_number_of_cpus)
# Round-robin CPU mask generation based on queue number
mask=0
while [ "${number}" -lt "${NR_CPU}" ]; do
temp_mask="$(generate_cpus_mask_for_one_cpu "$((number % NR_CPU))" false)"
mask="$(bignum_calc default "${mask} | ${temp_mask}")"
number="$((number + total))"
done
format_cpumask "$(bignum_calc hex "${mask}")"
}
# generate a cpuset CPU mask that uses only one CPU
generate_cpus_mask_for_one_cpu() {
# CPU number
local CPU=$1
local format=$2
if "${format}"; then
format_cpumask "$(bignum_calc hex "2 ^ ${CPU}")"
else
bignum_calc default "2 ^ ${CPU}"
fi
}
# return irq list of the ethernet card
get_irq_of_ethernet_card() {
local ETH_NAME=$1
# NOTE:
# The irq name registered to kernel is driver specific and varies from driver to driver
# This makes it pretty hard to track which irq belongs to the card by query /proc/irq/*/<irq name>
# The method used here seems to be driver irrelevant, but not 100% sure
# Needs more testing for it
# the legacy irq file in device/irq is usually not used, and not exist in /proc/irq/<number>
# we have to check if the irq actually exists
# IRQ list from legacy irq + MSI IRQ directory
while read -r irq; do
if [ -d "/proc/irq/${irq}" ]; then
echo "${irq}"
fi
done < <(
cat "/sys/class/net/${ETH_NAME}/device/irq" 2>/dev/null
# Iterate over MSI IRQ directory (avoid ls parsing anti-pattern)
for irq_file in "/sys/class/net/${ETH_NAME}/device/msi_irqs/"*; do
[ -e "${irq_file}" ] || continue
basename "${irq_file}"
done 2>/dev/null
)
}
# return the queue number of an irq
get_queue_from_irq() {
local IRQ=$1
# The irq name registeded to kernel is driver specific.
# but usually, it will have quene numbers at the end if its a multi-queued device, such as xxx-<queue> or xxx@<queue>
# an exception is, some mlx card may have the following format: mlx5_comp<queue>@pci:<pci id>
# It may not have any queue number if its a single-queled device, so the function may return an empty string
# IRQ name format is driver-specific, mlx5 requires special parsing
if find "/proc/irq/${IRQ}/" -type d | grep -qE 'mlx.*@pci:'; then
find "/proc/irq/${IRQ}/" -type d | sed -e 's|^.*/||' | grep -E '^mlx.*@pci:.*$' | sed -e 's/@pci:.*$//' | grep -Eo '[0-9]+$'
else
find "/proc/irq/${IRQ}/" -type d | grep -E '[^a-z0-9][0-9]+$' | grep -Eo '[0-9]+$'
fi
}
set_irq_smp_affinity() {
local IRQ=$1
local MASK=$2
_sysfs_write "/proc/irq/${IRQ}/smp_affinity" "${MASK}"
# shellcheck disable=SC2320
return $?
}
get_irq_smp_affinity() {
local IRQ=$1
_sysfs_read "/proc/irq/${IRQ}/smp_affinity"
}
mask_is_equal() {
local MASK1=$1
local MASK2=$2
# remove the leading zeros and commas before compare
test "${MASK1##*([0,])}" == "${MASK2##*([0,])}"
}
get_driver_of_ethernet_card() {
# ethernet card names, such as eno1
local ETH_NAME=$1
basename "$(readlink "/sys/class/net/${ETH_NAME}/device/driver")"
}
get_ethernet_hardware_info() {
# ethernet card names, such as eno1
local ETH_NAME=$1
local PCIADDR
PCIADDR="$(basename "$(readlink "/sys/class/net/${ETH_NAME}/device")")"
if [ -z "${PCIADDR}" ]; then
return 1
fi
lspci -s "${PCIADDR}" -vmm
}
get_vendor_of_ethernet_card() {
# ethernet card names, such as eno1
local ETH_NAME=$1
get_ethernet_hardware_info "${ETH_NAME}" | grep -E '^Vendor:' | cut -d : -f 2 | xargs
}
get_name_of_ethernet_card() {
# ethernet card names, such as eno1
local ETH_NAME=$1
get_ethernet_hardware_info "${ETH_NAME}" | grep -E '^Device:' | cut -d : -f 2 | xargs
}
get_physical_ethernet_card_list() {
local ETH_NAME
local DRIVER_PATH
local DRIVER_NAME
# we remove all the devices in /sys/class/net that links to virtual device
# and also exclude virtio and other virtualization drivers
while read -r ETH_NAME; do
DRIVER_NAME="$(get_driver_of_ethernet_card "${ETH_NAME}")"
case "${DRIVER_NAME}" in
virtio_net | veth | vmxnet3 | xen-netfront | hv_netvsc)
continue
;;
esac
echo "${ETH_NAME}"
done < <(find /sys/class/net -mindepth 1 -maxdepth 1 -lname '*/virtual/*' -prune -o -type l -printf '%f\n')
}
_match_exact_in_list() {
local target=$1
shift
local item
for item in "$@"; do
if [[ "${target}" == "${item}" ]]; then
return 0
fi
done
return 1
}
_match_substr_ci_in_list() {
local target=$1
shift
local item
local target_lc
target_lc="${target,,}"
for item in "$@"; do
if [[ "${target_lc}" =~ .*"${item,,}".* ]]; then
return 0
fi
done
return 1
}
get_filtered_ethernet_card_list() {
local ETH_NAME
local devname
local drivername
local vendorname
while read -r ETH_NAME; do
devname=""
drivername=""
vendorname=""
if _match_exact_in_list "${ETH_NAME}" "${osname_exclude_list[@]}"; then
continue
fi
if [ "${#device_exclude_list[@]}" -gt 0 ]; then
devname="$(get_name_of_ethernet_card "${ETH_NAME}")"
if _match_substr_ci_in_list "${devname}" "${device_exclude_list[@]}"; then
continue
fi
fi
if [ "${#driver_exclude_list[@]}" -gt 0 ]; then
drivername="$(get_driver_of_ethernet_card "${ETH_NAME}")"
if _match_exact_in_list "${drivername}" "${driver_exclude_list[@]}"; then
continue
fi
fi
if [ "${#vendor_exclude_list[@]}" -gt 0 ]; then
vendorname="$(get_vendor_of_ethernet_card "${ETH_NAME}")"
if _match_substr_ci_in_list "${vendorname}" "${vendor_exclude_list[@]}"; then
continue
fi
fi
if _match_exact_in_list "${ETH_NAME}" "${osname_include_list[@]}"; then
echo "${ETH_NAME}"
continue
fi
if [ "${#device_include_list[@]}" -gt 0 ]; then
if [ -z "${devname}" ]; then
devname="$(get_name_of_ethernet_card "${ETH_NAME}")"
fi
if _match_substr_ci_in_list "${devname}" "${device_include_list[@]}"; then
echo "${ETH_NAME}"
continue
fi
fi
if [ "${#driver_include_list[@]}" -gt 0 ]; then
if [ -z "${drivername}" ]; then
drivername="$(get_driver_of_ethernet_card "${ETH_NAME}")"
fi
if _match_exact_in_list "${drivername}" "${driver_include_list[@]}"; then
echo "${ETH_NAME}"
continue
fi
fi
if [ "${#vendor_include_list[@]}" -gt 0 ]; then
if [ -z "${vendorname}" ]; then
vendorname="$(get_vendor_of_ethernet_card "${ETH_NAME}")"
fi
if _match_substr_ci_in_list "${vendorname}" "${vendor_include_list[@]}"; then
echo "${ETH_NAME}"
continue
fi
fi
if [ "${#osname_include_list[@]}" -eq 0 ] &&
[ "${#device_include_list[@]}" -eq 0 ] &&
[ "${#driver_include_list[@]}" -eq 0 ] &&
[ "${#vendor_include_list[@]}" -eq 0 ]; then
# there's no include list, anything not excluded will be shown
echo "${ETH_NAME}"
fi
# in the find we remove all the devices in /sys/class/net that links to virtual device
done < <(get_physical_ethernet_card_list)
}
# Validate if a string is a positive integer
_is_positive_integer() {
local value="$1"
[[ "${value}" =~ ^[0-9]+$ ]] && [ "${value}" -gt 0 ]
}
# Unified RPS/XPS optimization logic
# mode: "check" (return 0/1), "apply" (output + modify)
# direction: "rx" for RPS, "tx" for XPS
#
# LIMITATION: This function does NOT consider NUMA topology.
# On multi-socket NUMA systems, this may distribute packets to CPUs on remote NUMA nodes,
# causing cross-node memory access with 2-3× latency penalty.
# Optimal: RPS/XPS should only use CPUs local to the NIC's NUMA node.
# Check NIC's NUMA node: cat /sys/class/net/<dev>/device/numa_node
# Get local CPUs: cat /sys/devices/system/node/node<N>/cpulist
_optimize_packet_steering() {
local eth_name=$1
local mode=$2
local direction=$3
local label
local get_mask_func
local set_mask_func
local get_queues_func
if [ "${direction}" == "rx" ]; then
label="RPS"
get_mask_func="get_rps_mask_from_queue"
set_mask_func="set_rps_mask_for_queue"
get_queues_func="get_running_rx_queues_of_device"
else
label="XPS"
get_mask_func="get_xps_mask_from_queue"
set_mask_func="set_xps_mask_for_queue"
get_queues_func="get_running_tx_queues_of_device"
fi
local NR_CPU NR_QUEUE
NR_CPU=$(get_number_of_cpus)
NR_QUEUE=$(get_ethernet_kernel_queues_number "${eth_name}" "${direction}")
local needs_change=false
while read -r queue; do
local queue_name queue_number current_mask best_mask
queue_name="$(basename "${queue}")"
queue_number="$(echo "${queue_name}" | grep -Eo '[0-9]+$')"
current_mask="$("${get_mask_func}" "${queue}")"
if [ "$NR_QUEUE" -lt "$NR_CPU" ]; then
best_mask="$(generate_cpus_mask "${queue_number}" "${NR_QUEUE}")"
else
best_mask="0"
fi
if ! mask_is_equal "${best_mask}" "${current_mask}"; then
needs_change=true
if [ "${mode}" = "apply" ]; then
echo -e "${eth_name} \t ${label} \t\t ${queue_name} \t\t ${current_mask} \t ${best_mask}"
"${set_mask_func}" "${queue}" "${best_mask}"
fi
fi
done < <("${get_queues_func}" "${eth_name}")
if [ "${mode}" = "check" ] && [ "${needs_change}" = "true" ]; then
return 1
fi
return 0
}
check_ethernet_rps() {
_optimize_packet_steering "$1" "check" "rx"
}
set_ethernet_rps_to_optimum() {
_optimize_packet_steering "$1" "apply" "rx"
}
check_ethernet_xps() {
_optimize_packet_steering "$1" "check" "tx"
}
set_ethernet_xps_to_optimum() {
_optimize_packet_steering "$1" "apply" "tx"
}
# RFS optimization logic (always disabled for safety)
# RFS disabled by default: production hash collision may cause ksoftirq 100%
_optimize_rfs() {
local eth_name=$1
local mode=$2
local needs_change=false
local best_rfs="0"
while read -r queue; do
local queue_name current_rfs
queue_name="$(basename "${queue}")"
current_rfs="$(get_rfs_from_queue "${queue}")"
if [ "${current_rfs}" != "${best_rfs}" ]; then
needs_change=true
if [ "${mode}" = "apply" ]; then
echo -e "${eth_name} \t RFS \t\t ${queue_name} \t\t ${current_rfs} \t ${best_rfs}"
set_rfs_for_queue "${queue}" "${best_rfs}"
fi
fi
done < <(get_running_rx_queues_of_device "${eth_name}")
local current_rfs_flow
current_rfs_flow="$(get_rfs_flow_entries)"
if [ "${current_rfs_flow}" -ne 0 ]; then
needs_change=true
if [ "${mode}" = "apply" ]; then
echo -e "N/A \t RFS-FLOW \t -- \t\t ${current_rfs_flow} \t 0"
set_rfs_flow_entries 0
fi
fi
if [ "${mode}" = "check" ] && [ "${needs_change}" = "true" ]; then
return 1
fi
return 0
}
check_ethernet_rfs() {
_optimize_rfs "$1" "check"
}
set_ethernet_rfs_to_optimum() {
_optimize_rfs "$1" "apply"
}
_optimize_ringbuffer() {
local eth_name=$1
local mode=$2
local needs_change=false
for INFO in "RX" "TX"; do
local current_ringbuffer max_ringbuffer
current_ringbuffer="$(get_ethernet_hardware_ringbuffer "${eth_name}" "${INFO}" current)"
max_ringbuffer="$(get_ethernet_hardware_ringbuffer "${eth_name}" "${INFO}" max)"
if [ -z "${current_ringbuffer}" ] || [ -z "${max_ringbuffer}" ]; then
continue
fi
if _is_positive_integer "${current_ringbuffer}" && _is_positive_integer "${max_ringbuffer}"; then
if [ "${max_ringbuffer}" -gt "${current_ringbuffer}" ]; then
needs_change=true
if [ "${mode}" = "apply" ]; then
echo -e "${eth_name} \t RINGBUFFER \t ${INFO} \t\t ${current_ringbuffer} \t ${max_ringbuffer}"
set_ethernet_hardware_ringbuffer "${eth_name}" "${INFO}" "${max_ringbuffer}"
fi
fi
fi
done
if [ "${mode}" = "check" ] && [ "${needs_change}" = "true" ]; then
return 1
fi
return 0
}
check_ethernet_ringbuffer() {
_optimize_ringbuffer "$1" "check"
}