Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1}
0005 #
0006 # Create a veth pair and set IPs/routes on both. Then ping to establish
0007 # an entry in the ARP/ND table. Depending on the test set sysctl option to
0008 # 1 or 0. Set remote veth down which will cause local veth to go into a no
0009 # carrier state. Depending on the test check the ARP/ND table:
0010 #
0011 # {arp,ndisc}_evict_nocarrier=1 should contain no ARP/ND after no carrier
0012 # {arp,ndisc}_evict_nocarrer=0 should still contain the single ARP/ND entry
0013 #
0014 
0015 readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
0016 readonly V4_ADDR0=10.0.10.1
0017 readonly V4_ADDR1=10.0.10.2
0018 readonly V6_ADDR0=2001:db8:91::1
0019 readonly V6_ADDR1=2001:db8:91::2
0020 nsid=100
0021 
0022 cleanup_v6()
0023 {
0024     ip netns del me
0025     ip netns del peer
0026 
0027     sysctl -w net.ipv4.conf.veth0.ndisc_evict_nocarrier=1 >/dev/null 2>&1
0028     sysctl -w net.ipv4.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&1
0029 }
0030 
0031 create_ns()
0032 {
0033     local n=${1}
0034 
0035     ip netns del ${n} 2>/dev/null
0036 
0037     ip netns add ${n}
0038     ip netns set ${n} $((nsid++))
0039     ip -netns ${n} link set lo up
0040 }
0041 
0042 
0043 setup_v6() {
0044     create_ns me
0045     create_ns peer
0046 
0047     IP="ip -netns me"
0048 
0049     $IP li add veth1 type veth peer name veth2
0050     $IP li set veth1 up
0051     $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad
0052     $IP li set veth2 netns peer up
0053     ip -netns peer -6 addr add $V6_ADDR1/64 dev veth2 nodad
0054 
0055     ip netns exec me sysctl -w $1 >/dev/null 2>&1
0056 
0057     # Establish an ND cache entry
0058     ip netns exec me ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&1
0059     # Should have the veth1 entry in ND table
0060     ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
0061     if [ $? -ne 0 ]; then
0062         cleanup_v6
0063         echo "failed"
0064         exit
0065     fi
0066 
0067     # Set veth2 down, which will put veth1 in NOCARRIER state
0068     ip netns exec peer ip link set veth2 down
0069 }
0070 
0071 setup_v4() {
0072     ip netns add "${PEER_NS}"
0073     ip link add name veth0 type veth peer name veth1
0074     ip link set dev veth0 up
0075     ip link set dev veth1 netns "${PEER_NS}"
0076     ip netns exec "${PEER_NS}" ip link set dev veth1 up
0077     ip addr add $V4_ADDR0/24 dev veth0
0078     ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth1
0079     ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth1
0080     ip route add default via $V4_ADDR0 dev veth0
0081 
0082     sysctl -w "$1" >/dev/null 2>&1
0083 
0084     # Establish an ARP cache entry
0085     ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&1
0086     # Should have the veth1 entry in ARP table
0087     ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
0088     if [ $? -ne 0 ]; then
0089         cleanup_v4
0090         echo "failed"
0091         exit
0092     fi
0093 
0094     # Set veth1 down, which will put veth0 in NOCARRIER state
0095     ip netns exec "${PEER_NS}" ip link set veth1 down
0096 }
0097 
0098 cleanup_v4() {
0099     ip neigh flush dev veth0
0100     ip link del veth0
0101     local -r ns="$(ip netns list|grep $PEER_NS)"
0102     [ -n "$ns" ] && ip netns del $ns 2>/dev/null
0103 
0104     sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&1
0105     sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&1
0106 }
0107 
0108 # Run test when arp_evict_nocarrier = 1 (default).
0109 run_arp_evict_nocarrier_enabled() {
0110     echo "run arp_evict_nocarrier=1 test"
0111     setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1"
0112 
0113     # ARP table should be empty
0114     ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
0115 
0116     if [ $? -eq 0 ];then
0117         echo "failed"
0118     else
0119         echo "ok"
0120     fi
0121 
0122     cleanup_v4
0123 }
0124 
0125 # Run test when arp_evict_nocarrier = 0
0126 run_arp_evict_nocarrier_disabled() {
0127     echo "run arp_evict_nocarrier=0 test"
0128     setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0"
0129 
0130     # ARP table should still contain the entry
0131     ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
0132 
0133     if [ $? -eq 0 ];then
0134         echo "ok"
0135     else
0136         echo "failed"
0137     fi
0138 
0139     cleanup_v4
0140 }
0141 
0142 run_arp_evict_nocarrier_disabled_all() {
0143     echo "run all.arp_evict_nocarrier=0 test"
0144     setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0"
0145 
0146     # ARP table should still contain the entry
0147     ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
0148 
0149     if [ $? -eq 0 ];then
0150         echo "ok"
0151     else
0152         echo "failed"
0153     fi
0154 
0155     cleanup_v4
0156 }
0157 
0158 run_ndisc_evict_nocarrier_enabled() {
0159     echo "run ndisc_evict_nocarrier=1 test"
0160 
0161     setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1"
0162 
0163     ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
0164 
0165     if [ $? -eq 0 ];then
0166         echo "failed"
0167     else
0168         echo "ok"
0169     fi
0170 
0171     cleanup_v6
0172 }
0173 
0174 run_ndisc_evict_nocarrier_disabled() {
0175     echo "run ndisc_evict_nocarrier=0 test"
0176 
0177     setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0"
0178 
0179     ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
0180 
0181     if [ $? -eq 0 ];then
0182         echo "ok"
0183     else
0184         echo "failed"
0185     fi
0186 
0187     cleanup_v6
0188 }
0189 
0190 run_ndisc_evict_nocarrier_disabled_all() {
0191     echo "run all.ndisc_evict_nocarrier=0 test"
0192 
0193     setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0"
0194 
0195     ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
0196 
0197     if [ $? -eq 0 ];then
0198         echo "ok"
0199     else
0200         echo "failed"
0201     fi
0202 
0203     cleanup_v6
0204 }
0205 
0206 run_all_tests() {
0207     run_arp_evict_nocarrier_enabled
0208     run_arp_evict_nocarrier_disabled
0209     run_arp_evict_nocarrier_disabled_all
0210     run_ndisc_evict_nocarrier_enabled
0211     run_ndisc_evict_nocarrier_disabled
0212     run_ndisc_evict_nocarrier_disabled_all
0213 }
0214 
0215 if [ "$(id -u)" -ne 0 ];then
0216         echo "SKIP: Need root privileges"
0217         exit $ksft_skip;
0218 fi
0219 
0220 run_all_tests