-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_qos.sh
executable file
·221 lines (168 loc) · 4.8 KB
/
setup_qos.sh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(C) 2021-2022 Intel Corporation
# Authors:
# Hector Blanco Alcaine
#
# ./setup_qos.sh --period <PERIOD> --bytes <BYTES> --offset <OFFSET> --interface <IFACE> --address <ADDRESS> --vid <VID> --pcp <PCP> -- <COMMAND> <ARGS>
#
# Set up an interface as a talker with the provided parameters except
# the destination MAC
function usage () {
echo "Usage:"
echo "$0 --period <PERIOD> --bytes <BYTES> --offset <OFFSET> --interface <IFACE> --address <ADDRESS> --vid <VID> --pcp <PCP> -- <COMMAND> <ARGS>"
echo "Example:"
echo "$0 --period 2000000 --bytes 1522 --offset 250000 --interface eth0 --address AB:CD:EF:FE:DC:BA --vid 3 --pcp 6 -- ping -4 -w 1 8.8.8.8"
}
function check_root () {
if [ `id --user` -ne 0 ]; then
echo "This script must be run as root! Aborting."
exit 1
fi
}
function parse_args () {
# Check the number of arguments supplied
if [ $# -lt 15 ]; then
echo "Wrong number of arguments"
usage
exit 1
fi
# getopt will set name if there are options
ARGS=`getopt --longoptions "period:,bytes:,offset:,interface:,address:,vid:,pcp:" --options "c:b:o:i:a:v:p:" -- "$@"`
# Handle wrong parameter names
# The case when parameters are missing is handled after the while below
if [[ $? -ne 0 ]]; then
usage
exit 1
fi
eval set -- "${ARGS}"
# Assign parameter values to variables
while [ $# -ge 1 ]; do
case "$1" in
--)
shift
break
;;
-c|--period)
PERIOD="$2"
shift
;;
-b|--bytes)
BYTES="$2"
shift
;;
-o|--offset)
OFFSET="$2"
shift
;;
-i|--interface)
IFACE="$2"
shift
;;
-a|--address)
ADDRESS="$2"
shift
;;
-v|--vid)
VID="$2"
shift
;;
-p|--pcp)
PCP="$2"
shift
;;
\?)
echo "Invalid option $OPTARG"
usage
exit 1
;;
esac
shift
done
COMMAND="$1"
shift
ARGS="$*"
}
function check_args () {
VLAN_INTERFACE="${IFACE}.${VID}"
OUTPUT=`ip link show ${VLAN_INTERFACE} > /dev/null 2>&1`
if [ $? -eq 0 ]; then
echo "VLAN interface ${VLAN_INTERFACE} already exists! Aborting."
echo "Delete VLAN interface with: ip link del ${VLAN_INTERFACE}"
exit 1
fi
OUTPUT=`which ${COMMAND}`
if [ $? -ne 0 ] && [ ! -x ${COMMAND} ]; then
echo "Command ${COMMAND} not found! Aborting."
exit 1
fi
}
function setup_talker () {
# Call python script to do everything and return vlan iface and soprio
cat >/tmp/call_detd.py << EOF
import sys
import traceback
from detd import StreamConfiguration
from detd import TrafficSpecification
from detd import Interface
from detd import Configuration
from detd import ServiceProxy
interface_name = "$IFACE"
interval = $PERIOD # ns
size = $BYTES # Bytes
txoffset = $OFFSET # ns
addr = "$ADDRESS"
vid = $VID
pcp = $PCP
stream = StreamConfiguration(addr, vid, pcp, txoffset)
traffic = TrafficSpecification(interval, size)
interface = Interface(interface_name)
config = Configuration(interface, stream, traffic)
# Both Server() and ServiceProxy() point to the same UDS by default
proxy = ServiceProxy()
try:
vlan_iface, soprio = proxy.add_talker(config)
except:
traceback.print_exc()
sys.exit(1)
print("{},{}".format(vlan_iface, soprio))
sys.exit(0)
EOF
OUTPUT=`python3 /tmp/call_detd.py`
if [ $? -ne 0 ]; then
echo "Talker setup failed! Aborting."
exit 1
fi
VIFACE=`echo $OUTPUT | cut -f1 -d','`
SOPRIO=`echo $OUTPUT | cut -f2 -d','`
# rm /tmp/detd.py
}
function run_command () {
# Setup net_prio:rt control group
if [[ ! -d /sys/fs/cgroup/net_prio ]]; then
echo "The directory /sys/fs/cgroup/net_prio does not exist"
echo "Creating directory /sys/fs/cgroup/net_prio"
mkdir /sys/fs/cgroup/net_prio
fi
OUTPUT=`mountpoint /sys/fs/cgroup/net_prio`
if [ $? -ne 0 ]; then
echo "Mounting net_prio cgroup at /sys/fs/cgroup/net_prio"
mount -t cgroup -o net_prio none /sys/fs/cgroup/net_prio
fi
mkdir -p /sys/fs/cgroup/net_prio/rt
# Set socket priority SOPRIO for traffic
# - Originating from processes belonging to rtnet_prio cgroup
# - Outgoing on VIFACE
# E.g.:
# echo "eth0.3 7" > /sys/fs/cgroup/net_prio/rt/net_prio.ifpriomap
echo "${VIFACE} ${SOPRIO}" > /sys/fs/cgroup/net_prio/rt/net_prio.ifpriomap
# Setup cgroups for the application
# Run command adding it to the net_prio:rt cgroup
cgexec -g net_prio:rt ${COMMAND} ${ARGS}
}
check_root
parse_args "$@"
check_args
setup_talker
run_command
exit 0