Narzędzia użytkownika

Narzędzia witryny


freebsd:ipfw:tables

IPFW tables

Czasem się zdarza, że trzeba wypełnić tablicę dużą ilością obiektów, np. lista blokowanych IP. W takim wypadku klasyczne podejście by wypełniać tablicę w pętli w bash-u mija się z celem, to trwa to zbyt długo. Dla przykładu lista zawierająca 105k adresów, która zostanie wczytana przy pomocy skryptu:

while read IP_ADDR; do
  ipfw -q table IPSUM add $IP_ADDR
done < /etc/ipsum_ip.txt

Wykona się w czasie:

# ipfw table IPSUM info
--- table(IPSUM), set(0) ---
 kindex: 2, type: addr
 references: 0, valtype: legacy
 algorithm: addr:radix
 items: 0, size: 296
 
# time ./ipsum_bash.sh 
69.451u 209.959s 4:46.19 97.6%	398+188k 0+0io 0pf+0w
 
# ipfw table IPSUM info
--- table(IPSUM), set(0) ---
 kindex: 2, type: addr
 references: 0, valtype: legacy
 algorithm: addr:radix
 items: 105675, size: 12681296

Czas ten można znacząco skrócić, gdy zamiast listy adresów IP (jeden na linię), stworzymy listę poleceń do IPFW jak poniżej:

table IPSUM add xxx.xxx.xx.xx
table IPSUM add xxx.xxx.xxx.x
table IPSUM add xxx.xxx.xx.xxx
table IPSUM add xx.xxx.xxx.x
...

Wtedy ładujemy ją poleceniem:

ipfw /path/lista.txt

Porównanie czasu ładowania:

# ipfw table IPSUM info
--- table(IPSUM), set(0) ---
 kindex: 2, type: addr
 references: 0, valtype: legacy
 algorithm: addr:radix
 items: 0, size: 296
 
# time ./ipsum_ipfw.sh 
0.307u 0.485s 0:00.81 96.2%	156+176k 0+0io 0pf+0w
 
# ipfw table IPSUM info
--- table(IPSUM), set(0) ---
 kindex: 2, type: addr
 references: 0, valtype: legacy
 algorithm: addr:radix
 items: 105675, size: 12681296

Przyspieszenie tej operacji to ponad 400x :)

freebsd/ipfw/tables.txt · ostatnio zmienione: 2022/06/15 17:19 przez mky