Rename nbs/nbr port names to nbsp/nbrp.
[cascardo/ovs.git] / tests / system-common-macros.at
1 # DEL_NAMESPACES(ns [, ns ... ])
2 #
3 # Delete namespaces from the running OS
4 m4_define([DEL_NAMESPACES],
5    [m4_foreach([ns], [$@],
6                [ip netns del ns
7 ])
8    ]
9 )
10
11 # ADD_NAMESPACES(ns [, ns ... ])
12 #
13 # Add new namespaces, if ns exists, the old one
14 # will be remove before new ones are installed.
15 m4_define([ADD_NAMESPACES],
16    [m4_foreach([ns], [$@],
17                [DEL_NAMESPACES(ns)
18                 AT_CHECK([ip netns add ns || return 77])
19                 on_exit 'DEL_NAMESPACES(ns)'
20                 ip netns exec ns sysctl -w net.netfilter.nf_conntrack_helper=0
21                ])
22    ]
23 )
24
25 # NS_EXEC([namespace], [command])
26 #
27 # Execute 'command' in 'namespace'
28 m4_define([NS_EXEC],
29     [ip netns exec $1 sh << NS_EXEC_HEREDOC
30 $2
31 NS_EXEC_HEREDOC])
32
33 # NS_CHECK_EXEC([namespace], [command], other_params...)
34 #
35 # Wrapper for AT_CHECK that executes 'command' inside 'namespace'.
36 # 'other_params' as passed as they are to AT_CHECK.
37 m4_define([NS_CHECK_EXEC],
38     [ AT_CHECK([NS_EXEC([$1], [$2])], m4_shift(m4_shift($@))) ]
39 )
40
41 # ADD_BR([name], [vsctl-args])
42 #
43 # Expands into the proper ovs-vsctl commands to create a bridge with the
44 # appropriate type, and allows additional arguments to be passed.
45 m4_define([ADD_BR], [ovs-vsctl _ADD_BR([$1]) -- $2])
46
47 # ADD_INT([port], [namespace], [ovs-br], [ip_addr])
48 #
49 # Add an internal port to 'ovs-br', then shift it into 'namespace' and
50 # configure it with 'ip_addr' (specified in CIDR notation).
51 m4_define([ADD_INT],
52     [ AT_CHECK([ovs-vsctl add-port $3 $1 -- set int $1 type=internal])
53       AT_CHECK([ip link set $1 netns $2])
54       NS_CHECK_EXEC([$2], [ip addr add $4 dev $1])
55       NS_CHECK_EXEC([$2], [ip link set dev $1 up])
56     ]
57 )
58
59 # ADD_VETH([port], [namespace], [ovs-br], [ip_addr] [mac_addr [gateway]])
60 #
61 # Add a pair of veth ports. 'port' will be added to name space 'namespace',
62 # and "ovs-'port'" will be added to ovs bridge 'ovs-br'.
63 #
64 # The 'port' in 'namespace' will be brought up with static IP address
65 # with 'ip_addr' in CIDR notation.
66 #
67 # Optionally, one can specify the 'mac_addr' for 'port' and the default
68 # 'gateway'.
69 #
70 # The existing 'port' or 'ovs-port' will be removed before new ones are added.
71 #
72 m4_define([ADD_VETH],
73     [ AT_CHECK([ip link add $1 type veth peer name ovs-$1 || return 77])
74       CONFIGURE_VETH_OFFLOADS([$1])
75       AT_CHECK([ip link set $1 netns $2])
76       AT_CHECK([ip link set dev ovs-$1 up])
77       AT_CHECK([ovs-vsctl add-port $3 ovs-$1 -- \
78                 set interface ovs-$1 external-ids:iface-id="$1"])
79       NS_CHECK_EXEC([$2], [ip addr add $4 dev $1])
80       NS_CHECK_EXEC([$2], [ip link set dev $1 up])
81       if test -n "$5"; then
82         NS_CHECK_EXEC([$2], [ip link set dev $1 address $5])
83       fi
84       if test -n "$6"; then
85         NS_CHECK_EXEC([$2], [ip route add default via $6])
86       fi
87       on_exit 'ip link del ovs-$1'
88     ]
89 )
90
91 # ADD_VLAN([port], [namespace], [vlan-id], [ip-addr])
92 #
93 # Add a VLAN device named 'port' within 'namespace'. It will be configured
94 # with the ID 'vlan-id' and the address 'ip-addr'.
95 m4_define([ADD_VLAN],
96     [ NS_CHECK_EXEC([$2], [ip link add link $1 name $1.$3 type vlan id $3])
97       NS_CHECK_EXEC([$2], [ip link set dev $1.$3 up])
98       NS_CHECK_EXEC([$2], [ip addr add dev $1.$3 $4])
99     ]
100 )
101
102 # ADD_OVS_TUNNEL([type], [bridge], [port], [remote-addr], [overlay-addr])
103 #
104 # Add an ovs-based tunnel device in the root namespace, with name 'port' and
105 # type 'type'. The tunnel device will be configured as point-to-point with the
106 # 'remote-addr' as the underlay address of the remote tunnel endpoint.
107 #
108 # 'port will be configured with the address 'overlay-addr'.
109 #
110 m4_define([ADD_OVS_TUNNEL],
111    [AT_CHECK([ovs-vsctl add-port $2 $3 -- \
112               set int $3 type=$1 options:remote_ip=$4])
113     AT_CHECK([ip addr add dev $2 $5])
114     AT_CHECK([ip link set dev $2 up])
115     AT_CHECK([ip link set dev $2 mtu 1450])
116     on_exit 'ip addr del dev $2 $5'
117    ]
118 )
119
120 # ADD_NATIVE_TUNNEL([type], [port], [namespace], [remote-addr], [overlay-addr],
121 #                   [link-args])
122 #
123 # Add a native tunnel device within 'namespace', with name 'port' and type
124 # 'type'. The tunnel device will be configured as point-to-point with the
125 # 'remote-addr' as the underlay address of the remote tunnel endpoint (as
126 # viewed from the perspective of that namespace).
127 #
128 # 'port' will be configured with the address 'overlay-addr'. 'link-args' is
129 # made available so that additional arguments can be passed to "ip link",
130 # for instance to configure the vxlan destination port.
131 #
132 m4_define([ADD_NATIVE_TUNNEL],
133    [NS_CHECK_EXEC([$3], [ip link add dev $2 type $1 remote $4 $6])
134     NS_CHECK_EXEC([$3], [ip addr add dev $2 $5])
135     NS_CHECK_EXEC([$3], [ip link set dev $2 up])
136     NS_CHECK_EXEC([$3], [ip link set dev $2 mtu 1450])
137    ]
138 )
139
140 # FORMAT_PING([])
141 #
142 # Strip variant pieces from ping output so the output can be reliably compared.
143 #
144 m4_define([FORMAT_PING], [grep "transmitted" | sed 's/time.*ms$/time 0ms/'])
145
146 # FORMAT_CT([ip-addr])
147 #
148 # Strip content from the piped input which would differ from test to test
149 # and limit the output to the rows containing 'ip-addr'.
150 #
151 m4_define([FORMAT_CT],
152     [[grep "dst=$1" | sed -e 's/port=[0-9]*/port=<cleared>/g' -e 's/id=[0-9]*/id=<cleared>/g' -e 's/state=[0-9_A-Z]*/state=<cleared>/g' | sort | uniq]])
153
154 # NETNS_DAEMONIZE([namespace], [command], [pidfile])
155 #
156 # Run 'command' as a background process within 'namespace' and record its pid
157 # to 'pidfile' to allow cleanup on exit.
158 #
159 m4_define([NETNS_DAEMONIZE],
160    [ip netns exec $1 $2 & echo $! > $3
161      echo "kill \`cat $3\`" >> cleanup
162    ]
163 )
164
165 # OVS_CHECK_VXLAN()
166 #
167 # Do basic check for vxlan functionality, skip the test if it's not there.
168 m4_define([OVS_CHECK_VXLAN],
169     [AT_SKIP_IF([! ip link add foo type vxlan help 2>&1 | grep dstport >/dev/null])])
170
171 # OVS_CHECK_GRE()
172 m4_define([OVS_CHECK_GRE],
173     [AT_SKIP_IF([! ip link add foo type gretap help 2>&1 | grep gre >/dev/null])])
174
175 # OVS_CHECK_GENEVE()
176 m4_define([OVS_CHECK_GENEVE],
177     [AT_SKIP_IF([! ip link add foo type geneve help 2>&1 | grep geneve >/dev/null])])