Commit f0223740 authored by Vitaly Lipatov's avatar Vitaly Lipatov

router: fix ip rule pref parsing (pref is $1 with colon, not a named field)

ip rule show format is "PREF:\tfrom ... lookup TABLE", not "... pref PREF". The awk was looking for a "pref"/"priority" field that doesn't exist, so old rules were never removed and new prefs were never applied. Also fix grep -c/-q "lookup TABLE" to use -w (word boundary) to prevent "lookup 2" from matching "lookup 200". Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 5e7e4541
......@@ -353,14 +353,15 @@ _fixup_rule_pref()
[ "$_saved_pref" = "$_pref" ] && return 0
# Pref changed — remove old rule(s) and add correct one
# ip rule show format: "PREF:\tfrom ... lookup TABLE" — pref is $1 with colon
local _old_prefs=$($_ipcmd -N rule show | awk -v t="$_table" \
'/lookup/ { for(i=1;i<=NF;i++) if($i=="lookup" && $(i+1)==t) { for(j=1;j<=NF;j++) if($j=="pref" || $j=="priority") print $(j+1) } }')
'/lookup/ { for(i=1;i<=NF;i++) if($i=="lookup" && $(i+1)==t) { p=$1; sub(/:$/,"",p); print p } }')
for _op in $_old_prefs ; do
[ "$_op" = "$_pref" ] && continue
vlog "$_tag$_label pref changed: removing old rule pref $_op"
$_ipcmd rule del lookup "$_table" pref "$_op" 2>/dev/null
done
if ! $_ipcmd -N rule show | grep -q "lookup $_table" ; then
if ! $_ipcmd -N rule show | grep -qw "lookup $_table" ; then
vlog "$_tag$_label pref changed: adding rule pref $_pref"
$_ipcmd rule add lookup "$_table" pref "$_pref" 2>/dev/null
fi
......@@ -381,7 +382,7 @@ check_list_changed()
read -r saved_hash < "$STATE_DIR/$_state/hash"
vlog "$_tag$_label hash: saved=$saved_hash current=$_current_hash"
if [ "$_current_hash" = "$saved_hash" ] ; then
local rule_count=$($_ipcmd -N rule show | grep -c "lookup $_table")
local rule_count=$($_ipcmd -N rule show | grep -cw "lookup $_table")
local route_count=$($_ipcmd route show table "$_table" 2>/dev/null | grep -c '^[^[:space:]]')
vlog "$_tag$_label hash match, checking state: rules=$rule_count routes=$route_count"
if [ "$rule_count" = "0" ] ; then
......@@ -413,7 +414,7 @@ check_list_changed()
# Detect routes/rules lost even when hash changed
if [ -z "$FORCE" ] && [ -z "$_need_reload" ] ; then
local cur_rules=$($_ipcmd -N rule show | grep -c "lookup $_table")
local cur_rules=$($_ipcmd -N rule show | grep -cw "lookup $_table")
local cur_routes=$($_ipcmd route show table "$_table" 2>/dev/null | grep -c '^[^[:space:]]')
if [ "$cur_rules" = "0" ] || [ "$cur_routes" = "0" ] ; then
_need_reload=1
......@@ -579,15 +580,16 @@ load_list_routes()
# Ensure ip rule exists with correct pref
# Remove rules for this table with wrong pref (leftover from old pref scheme)
# ip rule show format: "PREF:\tfrom ... lookup TABLE" — pref is $1 with colon
local _old_prefs=$($_ipcmd -N rule show | awk -v t="$_table" -v p="$_pref" \
'/lookup/ { for(i=1;i<=NF;i++) if($i=="lookup" && $(i+1)==t) { for(j=1;j<=NF;j++) if($j=="pref" || $j=="priority") { if($(j+1)!=p) print $(j+1) } } }')
'/lookup/ { for(i=1;i<=NF;i++) if($i=="lookup" && $(i+1)==t) { pr=$1; sub(/:$/,"",pr); if(pr!=p) print pr } }')
if [ -n "$_old_prefs" ] ; then
for _op in $_old_prefs ; do
vlog "$_tag$_label removing stale ip rule: lookup $_table pref $_op"
$_ipcmd rule del lookup "$_table" pref "$_op" 2>/dev/null
done
fi
if ! $_ipcmd -N rule show | grep -q "lookup $_table" ; then
if ! $_ipcmd -N rule show | grep -qw "lookup $_table" ; then
vlog "$_tag$_label adding ip rule: lookup $_table pref $_pref"
$_ipcmd rule add lookup "$_table" pref "$_pref" 2>/dev/null
fi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment