Mike Dillinger reported issues with insertion of entries into sets supporting intervals that were denied because of false conflicts with elements that were already expired. Partial failures would occur to, leading to the generation of new intervals the user didn't specify, as only the opening or the closing elements wouldn't be inserted. The reproducer provided by Mike looks like this: #!/bin/bash nft list set ip filter blacklist4-ip-1m for ((i=1;i<=10;i++)); do nft add element filter blacklist4-ip-1m {$i.$i.$i.$i} sleep 1 done nft list set ip filter blacklist4-ip-1m which, run in a loop at different intervals, show the different kind of failures. Extend the existing test case for overlapping and non-overlapping intervals to systematically cover sets with a configured timeout. Signed-off-by: Stefano Brivio <sbrivio@xxxxxxxxxx> --- .../testcases/sets/0044interval_overlap_0 | 81 ++++++++++++++----- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/tests/shell/testcases/sets/0044interval_overlap_0 b/tests/shell/testcases/sets/0044interval_overlap_0 index fad92ddcf356..16f661a00116 100755 --- a/tests/shell/testcases/sets/0044interval_overlap_0 +++ b/tests/shell/testcases/sets/0044interval_overlap_0 @@ -7,6 +7,13 @@ # existing one # - for concatenated ranges, the new element is less specific than any existing # overlapping element, as elements are evaluated in order of insertion +# +# Then, repeat the test with a set configured for 1s timeout, checking that: +# - we can insert all the elements as described above +# - once the timeout has expired, we can insert all the elements again, and old +# elements are not present +# - before the timeout expires again, we can re-add elements that are not +# expected to fail, but old elements might be present # Accept Interval List intervals_simple=" @@ -39,28 +46,62 @@ intervals_concat=" y 15-20 . 49-61 0-2 . 0-3, 10-20 . 30-40, 15-20 . 50-60, 3-9 . 4-29, 15-20 . 49-61 " -$NFT add table t -$NFT add set t s '{ type inet_service ; flags interval ; }' -$NFT add set t c '{ type inet_service . inet_service ; flags interval ; }' +match_elements() { + skip=0 + n=0 + out= + for a in $($NFT list set t ${1})}; do + [ ${n} -eq 0 ] && [ "${a}" = "elements" ] && n=1 + [ ${n} -eq 1 ] && [ "${a}" = "=" ] && n=2 + [ ${n} -eq 2 ] && [ "${a}" = "{" ] && n=3 && continue + [ ${n} -lt 3 ] && continue + + [ "${a}" = "}" ] && break + + [ ${skip} -eq 1 ] && skip=0 && out="${out}," && continue + [ "${a}" = "expires" ] && skip=1 && continue + + [ -n "${out}" ] && out="${out} ${a}" || out="${a}" + done + [ "${out%,}" = "${2}" ] +} -IFS=' +add_elements() { + set="s" + IFS=' ' -set="s" -for t in ${intervals_simple} switch ${intervals_concat}; do - [ "${t}" = "switch" ] && set="c" && continue - [ -z "${pass}" ] && pass="${t}" && continue - [ -z "${interval}" ] && interval="${t}" && continue + for t in ${intervals_simple} switch ${intervals_concat}; do + unset IFS + [ "${t}" = "switch" ] && set="c" && continue + [ -z "${pass}" ] && pass="${t}" && continue + [ -z "${interval}" ] && interval="${t}" && continue - if [ "${pass}" = "y" ]; then - $NFT add element t ${set} "{ ${interval} }" - else - ! $NFT add element t ${set} "{ ${interval} }" 2>/dev/null - fi - $NFT list set t ${set} | tr -d '\n\t' | tr -s ' ' | \ - grep -q "elements = { ${t} }" + if [ "${pass}" = "y" ]; then + $NFT add element t ${set} "{ ${interval} }" + else + ! $NFT add element t ${set} "{ ${interval} }" 2>/dev/null + fi - pass= - interval= -done + [ "${1}" != "nomatch" ] && match_elements "${set}" "${t}" -unset IFS + pass= + interval= + IFS=' +' + done + unset IFS +} + +$NFT add table t +$NFT add set t s '{ type inet_service ; flags interval ; }' +$NFT add set t c '{ type inet_service . inet_service ; flags interval ; }' +add_elements + +$NFT flush ruleset +$NFT add table t +$NFT add set t s '{ type inet_service ; flags interval,timeout; timeout 1s; gc-interval 1s; }' +$NFT add set t c '{ type inet_service . inet_service ; flags interval,timeout ; timeout 1s; gc-interval 1s; }' +add_elements +sleep 1 +add_elements +add_elements nomatch -- 2.26.2