brintos

brintos / linux-shallow public Read only

0
0
Text · 5.1 KiB · 92eb880 Raw
214 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1}5#6# Create a veth pair and set IPs/routes on both. Then ping to establish7# an entry in the ARP/ND table. Depending on the test set sysctl option to8# 1 or 0. Set remote veth down which will cause local veth to go into a no9# carrier state. Depending on the test check the ARP/ND table:10#11# {arp,ndisc}_evict_nocarrier=1 should contain no ARP/ND after no carrier12# {arp,ndisc}_evict_nocarrer=0 should still contain the single ARP/ND entry13#14 15source lib.sh16 17readonly V4_ADDR0=10.0.10.118readonly V4_ADDR1=10.0.10.219readonly V6_ADDR0=2001:db8:91::120readonly V6_ADDR1=2001:db8:91::221nsid=10022ret=023 24cleanup_v6()25{26    cleanup_ns ${me} ${peer}27 28    sysctl -w net.ipv6.conf.veth1.ndisc_evict_nocarrier=1 >/dev/null 2>&129    sysctl -w net.ipv6.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&130}31 32setup_v6() {33    setup_ns me peer34 35    IP="ip -netns ${me}"36 37    $IP li add veth1 type veth peer name veth238    $IP li set veth1 up39    $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad40    $IP li set veth2 netns ${peer} up41    ip -netns ${peer} -6 addr add $V6_ADDR1/64 dev veth2 nodad42 43    ip netns exec ${me} sysctl -w $1 >/dev/null 2>&144 45    # Establish an ND cache entry46    ip netns exec ${me} ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&147    # Should have the veth1 entry in ND table48    ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&149    if [ $? -ne 0 ]; then50        cleanup_v651        echo "failed"52        exit 153    fi54 55    # Set veth2 down, which will put veth1 in NOCARRIER state56    ip netns exec ${peer} ip link set veth2 down57}58 59setup_v4() {60    setup_ns PEER_NS61    ip link add name veth0 type veth peer name veth162    ip link set dev veth0 up63    ip link set dev veth1 netns "${PEER_NS}"64    ip netns exec "${PEER_NS}" ip link set dev veth1 up65    ip addr add $V4_ADDR0/24 dev veth066    ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth167    ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth168    ip route add default via $V4_ADDR0 dev veth069 70    sysctl -w "$1" >/dev/null 2>&171 72    # Establish an ARP cache entry73    ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&174    # Should have the veth1 entry in ARP table75    ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&176    if [ $? -ne 0 ]; then77        cleanup_v478        echo "failed"79        exit 180    fi81 82    # Set veth1 down, which will put veth0 in NOCARRIER state83    ip netns exec "${PEER_NS}" ip link set veth1 down84}85 86cleanup_v4() {87    ip neigh flush dev veth088    ip link del veth089    cleanup_ns $PEER_NS90 91    sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&192    sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&193}94 95# Run test when arp_evict_nocarrier = 1 (default).96run_arp_evict_nocarrier_enabled() {97    echo "run arp_evict_nocarrier=1 test"98    setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1"99 100    # ARP table should be empty101    ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1102 103    if [ $? -eq 0 ];then104        echo "failed"105        ret=1106    else107        echo "ok"108    fi109 110    cleanup_v4111}112 113# Run test when arp_evict_nocarrier = 0114run_arp_evict_nocarrier_disabled() {115    echo "run arp_evict_nocarrier=0 test"116    setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0"117 118    # ARP table should still contain the entry119    ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1120 121    if [ $? -eq 0 ];then122        echo "ok"123    else124        echo "failed"125        ret=1126    fi127 128    cleanup_v4129}130 131run_arp_evict_nocarrier_disabled_all() {132    echo "run all.arp_evict_nocarrier=0 test"133    setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0"134 135    # ARP table should still contain the entry136    ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1137 138    if [ $? -eq 0 ];then139        echo "ok"140    else141        echo "failed"142    fi143 144    cleanup_v4145}146 147run_ndisc_evict_nocarrier_enabled() {148    echo "run ndisc_evict_nocarrier=1 test"149 150    setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1"151 152    ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1153 154    if [ $? -eq 0 ];then155        echo "failed"156        ret=1157    else158        echo "ok"159    fi160 161    cleanup_v6162}163 164run_ndisc_evict_nocarrier_disabled() {165    echo "run ndisc_evict_nocarrier=0 test"166 167    setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0"168 169    ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1170 171    if [ $? -eq 0 ];then172        echo "ok"173    else174        echo "failed"175        ret=1176    fi177 178    cleanup_v6179}180 181run_ndisc_evict_nocarrier_disabled_all() {182    echo "run all.ndisc_evict_nocarrier=0 test"183 184    setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0"185 186    ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1187 188    if [ $? -eq 0 ];then189        echo "ok"190    else191        echo "failed"192        ret=1193    fi194 195    cleanup_v6196}197 198run_all_tests() {199    run_arp_evict_nocarrier_enabled200    run_arp_evict_nocarrier_disabled201    run_arp_evict_nocarrier_disabled_all202    run_ndisc_evict_nocarrier_enabled203    run_ndisc_evict_nocarrier_disabled204    run_ndisc_evict_nocarrier_disabled_all205}206 207if [ "$(id -u)" -ne 0 ];then208	echo "SKIP: Need root privileges"209	exit $ksft_skip;210fi211 212run_all_tests213exit $ret214