xref: /linux/tools/testing/selftests/net/netns-name.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source lib.sh
5set -o pipefail
6
7DEV=dummy-dev0
8DEV2=dummy-dev1
9ALT_NAME=some-alt-name
10
11RET_CODE=0
12
13cleanup() {
14    cleanup_ns $NS $test_ns
15}
16
17trap cleanup EXIT
18
19fail() {
20    echo "ERROR: ${1:-unexpected return code} (ret: $_)" >&2
21    RET_CODE=1
22}
23
24setup_ns NS test_ns
25
26#
27# Test basic move without a rename
28#
29ip -netns $NS link add name $DEV type dummy || fail
30ip -netns $NS link set dev $DEV netns $test_ns ||
31    fail "Can't perform a netns move"
32ip -netns $test_ns link show dev $DEV >> /dev/null || fail "Device not found after move"
33ip -netns $test_ns link del $DEV || fail
34
35#
36# Test move with a conflict
37#
38ip -netns $test_ns link add name $DEV type dummy
39ip -netns $NS link add name $DEV type dummy || fail
40ip -netns $NS link set dev $DEV netns $test_ns 2> /dev/null &&
41    fail "Performed a netns move with a name conflict"
42ip -netns $test_ns link show dev $DEV >> /dev/null || fail "Device not found after move"
43ip -netns $NS link del $DEV || fail
44ip -netns $test_ns link del $DEV || fail
45
46#
47# Test move with a conflict and rename
48#
49ip -netns $test_ns link add name $DEV type dummy
50ip -netns $NS link add name $DEV type dummy || fail
51ip -netns $NS link set dev $DEV netns $test_ns name $DEV2 ||
52    fail "Can't perform a netns move with rename"
53ip -netns $test_ns link del $DEV2 || fail
54ip -netns $test_ns link del $DEV || fail
55
56#
57# Test dup alt-name with netns move
58#
59ip -netns $test_ns link add name $DEV type dummy || fail
60ip -netns $test_ns link property add dev $DEV altname $ALT_NAME || fail
61ip -netns $NS link add name $DEV2 type dummy || fail
62ip -netns $NS link property add dev $DEV2 altname $ALT_NAME || fail
63
64ip -netns $NS link set dev $DEV2 netns $test_ns 2> /dev/null &&
65    fail "Moved with alt-name dup"
66
67ip -netns $test_ns link del $DEV || fail
68ip -netns $NS link del $DEV2 || fail
69
70#
71# Test creating alt-name in one net-ns and using in another
72#
73ip -netns $NS link add name $DEV type dummy || fail
74ip -netns $NS link property add dev $DEV altname $ALT_NAME || fail
75ip -netns $NS link set dev $DEV netns $test_ns || fail
76ip -netns $test_ns link show dev $ALT_NAME >> /dev/null || fail "Can't find alt-name after move"
77ip -netns $NS link show dev $ALT_NAME 2> /dev/null &&
78    fail "Can still find alt-name after move"
79ip -netns $test_ns link del $DEV || fail
80
81echo -ne "$(basename $0) \t\t\t\t"
82if [ $RET_CODE -eq 0 ]; then
83    echo "[  OK  ]"
84else
85    echo "[ FAIL ]"
86fi
87exit $RET_CODE
88