Skip to content

Commit a2e711b

Browse files
committed
Add tests to check correct execution of programs in cubes chain
- Test packet flowing beetwen two namespaces - Test packet flowing out of the network stack
1 parent 4c29de8 commit a2e711b

10 files changed

+271
-8
lines changed

src/services/pcn-helloworld/src/Helloworld_dp_egress.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
// WARNING: log messages from this program are used by programs_chain tests,
18+
// changing them may cause those tests to fail
19+
1720
static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
1821
pcn_log(ctx, LOG_DEBUG, "[EGRESS] Packet ongoing on port %d", md->in_port);
1922
return RX_OK;

src/services/pcn-helloworld/src/Helloworld_dp_ingress.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
// WARNING: log messages from this program are used by programs_chain tests,
18+
// changing them may cause those tests to fail
19+
1720
/*
1821
* This file contains the eBPF code that implements the service datapath.
1922
* Of course it is no required to have this into a separated file, however

src/services/pcn-transparent-helloworld/src/TransparentHelloworld_dp_egress.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18+
// WARNING: log messages from this program are used by programs_chain tests,
19+
// changing them may cause those tests to fail
20+
1821
#include <bcc/helpers.h>
1922

2023
/* TODO: move the definition to a file shared by control & data path*/
@@ -45,10 +48,10 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
4548
// what action should be performed in the packet?
4649
switch (*action) {
4750
case DROP:
48-
pcn_log(ctx, LOG_DEBUG, "Egress: dropping packet");
51+
pcn_log(ctx, LOG_DEBUG, "[EGRESS] dropping packet");
4952
return RX_DROP;
5053
case PASS:
51-
pcn_log(ctx, LOG_DEBUG, "Egress: passing packet");
54+
pcn_log(ctx, LOG_DEBUG, "[EGRESS] passing packet");
5255
return RX_OK;
5356
case SLOWPATH:
5457
#ifndef POLYCUBE_XDP
@@ -57,10 +60,10 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
5760
return RX_OK;
5861
}
5962
#endif
60-
pcn_log(ctx, LOG_DEBUG, "Egress: sending packet to slow path");
63+
pcn_log(ctx, LOG_DEBUG, "[EGRESS] sending packet to slow path");
6164
return pcn_pkt_controller(ctx, md, SLOWPATH_REASON);
6265
default:
63-
pcn_log(ctx, LOG_ERR, "Egress: bad action %d", *action);
66+
pcn_log(ctx, LOG_ERR, "[EGRESS] bad action %d", *action);
6467
return RX_DROP;
6568
}
6669

src/services/pcn-transparent-helloworld/src/TransparentHelloworld_dp_ingress.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18+
// WARNING: log messages from this program are used by programs_chain tests,
19+
// changing them may cause those tests to fail
20+
1821
#include <bcc/helpers.h>
1922

2023
enum {
@@ -41,16 +44,16 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
4144
// what action should be performed in the packet?
4245
switch (*action) {
4346
case DROP:
44-
pcn_log(ctx, LOG_DEBUG, "Ingress: dropping packet");
47+
pcn_log(ctx, LOG_DEBUG, "[INGRESS] dropping packet");
4548
return RX_DROP;
4649
case PASS:
47-
pcn_log(ctx, LOG_DEBUG, "Ingress: passing packet");
50+
pcn_log(ctx, LOG_DEBUG, "[INGRESS] passing packet");
4851
return RX_OK;
4952
case SLOWPATH:
50-
pcn_log(ctx, LOG_DEBUG, "Ingress: sending packet to slow path");
53+
pcn_log(ctx, LOG_DEBUG, "[INGRESS] sending packet to slow path");
5154
return pcn_pkt_controller(ctx, md, SLOWPATH_REASON);
5255
default:
53-
pcn_log(ctx, LOG_ERR, "Ingress: bad action %d", *action);
56+
pcn_log(ctx, LOG_ERR, "[INGRESS] bad action %d", *action);
5457
return RX_DROP;
5558
}
5659

tests/helpers.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ function create_veth {
3232
done
3333
}
3434

35+
function create_veth_no_ipv6 {
36+
for i in `seq 1 $1`;
37+
do
38+
sudo ip netns del ns${i} || true
39+
sudo ip netns add ns${i}
40+
sudo ip link add veth${i}_ type veth peer name veth${i}
41+
sudo ip link set veth${i}_ netns ns${i}
42+
sudo sysctl -w net.ipv6.conf.veth${i}.disable_ipv6=1
43+
sudo ip netns exec ns${i} sysctl -w net.ipv6.conf.veth${i}_.disable_ipv6=1
44+
sudo ip netns exec ns${i} ip link set dev veth${i}_ up
45+
sudo ip link set dev veth${i} up
46+
sudo ip netns exec ns${i} ifconfig veth${i}_ 10.0.0.${i}/24
47+
done
48+
}
49+
3550
function create_link {
3651
for i in `seq 1 $1`;
3752
do

tests/programs_chain_common.bash

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#! /bin/bash
2+
3+
# WARNING: this tests rely on log messages of helloworld and
4+
# transparenthelloworld services, changing them may cause this tests to
5+
# fail
6+
7+
source "${BASH_SOURCE%/*}/helpers.bash"
8+
9+
LOG_SIZE=50
10+
11+
# Expected sequences of log messages for a packet flowing from ns1 to ns2 and
12+
# from ns2 to ns1.
13+
# Sequences are in reversed order.
14+
seq_1to2=("[hw2] [EGRESS]"
15+
"[hw2] Forwarding"
16+
"[hw2] [INGRESS]"
17+
"[th3] [EGRESS]"
18+
"[hw1] Forwarding"
19+
"[hw1] [INGRESS]"
20+
"[th2] [INGRESS]"
21+
"[th1] [INGRESS]"
22+
)
23+
seq_2to1=("[th1] [EGRESS]"
24+
"[hw1] [EGRESS]"
25+
"[th2] [EGRESS]"
26+
"[hw1] Forwarding"
27+
"[hw1] [INGRESS]"
28+
"[th3] [INGRESS]"
29+
"[hw2] Forwarding"
30+
"[hw2] [INGRESS]"
31+
)
32+
33+
# Expected sequence of log messages for a packet flowing out of the network
34+
# stack.
35+
# Sequence is in reversed order.
36+
seq_stack_egress=("[th1] [EGRESS]"
37+
"[hw1] [EGRESS]")
38+
39+
function cleanup {
40+
set +e
41+
42+
polycubectl hw1 del
43+
polycubectl hw2 del
44+
polycubectl th1 del
45+
polycubectl th2 del
46+
polycubectl th3 del
47+
48+
delete_veth 2
49+
50+
echo "FAIL"
51+
}
52+
53+
# Retrieves the last LOG_SIZE lines of polycubed log, output in string $1
54+
function get_log {
55+
local -n output=$1
56+
57+
# Check if polycubed is running into a container
58+
if [ -z "$container" ]; then
59+
# Retrieve log from tmp file
60+
output=$(tail -n $LOG_SIZE tmp)
61+
else
62+
# Retrieve the log from the stdout of the container
63+
output=$(docker logs --tail $LOG_SIZE $container)
64+
fi
65+
}
66+
67+
# Reverses the log $1 and extracts only inportant parts, output in array $2
68+
function process_log {
69+
local -n output=$2
70+
output=()
71+
IFS_BACKUP=$IFS
72+
while IFS= read -r line; do
73+
IFS=" "
74+
read -ra arr <<< "$line"
75+
log_line="${arr[3]} ${arr[5]}"
76+
output=("$log_line" "${output[@]}")
77+
done <<< "$1"
78+
IFS=$IFS_BACKUP
79+
}
80+
81+
# Looks for sequence $1 in array $2, returns 0 if found, 1 otherwise
82+
function find_sequence {
83+
local -n seq=$1
84+
local -n arr=$2
85+
local ret=1
86+
local i=0
87+
for line in "${arr[@]}"; do
88+
if [ "$line" = "${seq[i]}" ]; then
89+
((i=i+1))
90+
if [ $i -eq ${#seq[@]} ]; then
91+
ret=0
92+
break
93+
fi
94+
95+
elif [ $i -gt 0 ]; then
96+
break
97+
fi
98+
done
99+
100+
return $ret
101+
}
102+
103+
104+
# Tests whether ingress and egress programs in a chain of cubes are executed
105+
# correcly
106+
# Cubes chain:
107+
# +-----+ +-----+-----+-----+ +-----+
108+
# veth1-| th1 |--------| th2 | hw1 | th2 |--------| hw2 |-------- veth2
109+
# +-----+ +-----+-----+-----+ +-----+
110+
# th = TransparentHelloworld
111+
# hw = HelloWorld
112+
# $1: xdp_skb | tc
113+
function test_programs_chain {
114+
trap cleanup EXIT
115+
116+
create_veth_no_ipv6 2
117+
118+
set -e
119+
set -x
120+
121+
polycubectl helloworld add hw1 loglevel=trace type=$1
122+
polycubectl hw1 set action=forward
123+
polycubectl hw1 ports add to_veth1 peer=veth1
124+
polycubectl hw1 ports add to_hw2
125+
126+
polycubectl helloworld add hw2 loglevel=trace type=$1
127+
polycubectl hw2 set action=forward
128+
polycubectl hw2 ports add to_hw1
129+
polycubectl hw2 ports add to_veth2 peer=veth2
130+
131+
polycubectl connect hw1:to_hw2 hw2:to_hw1
132+
133+
polycubectl transparenthelloworld add th1 loglevel=trace type=$1
134+
polycubectl attach th1 veth1
135+
136+
polycubectl transparenthelloworld add th2 loglevel=trace type=$1
137+
polycubectl attach th2 hw1:to_veth1
138+
139+
polycubectl transparenthelloworld add th3 loglevel=trace type=$1
140+
polycubectl attach th3 hw1:to_hw2
141+
142+
set +x
143+
144+
sudo ip netns exec ns1 ping 10.0.0.2 -c 1 1>/dev/null 2>&1
145+
146+
get_log log
147+
148+
process_log "$log" plog
149+
150+
find_sequence seq_1to2 plog
151+
find_sequence seq_2to1 plog
152+
153+
# Cleanup
154+
set -x
155+
polycubectl hw1 del
156+
polycubectl hw2 del
157+
polycubectl th1 del
158+
polycubectl th2 del
159+
polycubectl th3 del
160+
set +x
161+
delete_veth 2
162+
trap - EXIT
163+
164+
echo "SUCCESS"
165+
}
166+
167+
# Tests whether egress programs are executed correcly with packets flowing out
168+
# of the network stack of the host.
169+
# Cubes chain:
170+
# +-----+ +-----+-----+
171+
# veth1-| th1 |--------| th2 | hw1 |
172+
# +-----+ +-----+-----+
173+
# th = TransparentHelloworld
174+
# hw = HelloWorld
175+
# $1: xdp_skb | tc
176+
function test_stack_programs_chain {
177+
trap cleanup EXIT
178+
179+
create_veth_no_ipv6 1
180+
181+
set -e
182+
set -x
183+
184+
sudo ip addr add 10.0.0.2/24 dev veth1
185+
186+
polycubectl helloworld add hw1 loglevel=trace type=$1
187+
polycubectl hw1 ports add to_veth1 peer=veth1
188+
189+
polycubectl transparenthelloworld add th1 loglevel=trace type=$1
190+
polycubectl attach th1 veth1
191+
192+
polycubectl transparenthelloworld add th2 loglevel=trace type=$1
193+
polycubectl attach th2 hw1:to_veth1
194+
195+
set +x
196+
197+
# This command will fail since the echo reply is dropped by hw1 cube
198+
ping 10.0.0.1 -c 1 1>/dev/null 2>&1 || true
199+
200+
get_log log
201+
202+
process_log "$log" plog
203+
204+
find_sequence seq_stack_egress plog
205+
206+
# Cleanup
207+
set -x
208+
polycubectl hw1 del
209+
polycubectl th1 del
210+
polycubectl th2 del
211+
set +x
212+
delete_veth 1
213+
trap - EXIT
214+
215+
echo "SUCCESS"
216+
}

tests/test_programs_chain_tc.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
source "${BASH_SOURCE%/*}/programs_chain_common.bash"
4+
5+
test_programs_chain "tc"

tests/test_programs_chain_xdp.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
source "${BASH_SOURCE%/*}/programs_chain_common.bash"
4+
5+
test_programs_chain "xdp_skb"

tests/test_stack_programs_chain_tc.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
source "${BASH_SOURCE%/*}/programs_chain_common.bash"
4+
5+
test_stack_programs_chain "tc"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
source "${BASH_SOURCE%/*}/programs_chain_common.bash"
4+
5+
test_stack_programs_chain "xdp_skb"

0 commit comments

Comments
 (0)