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