Fixed some typos
[osm/openvim.git] / scripts / host-add-develop.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 #Get configuration of a host for using it as a compute node
25
26 function usage(){
27
28 echo -e "\nUsage: $0 --user-name <user> --ip=<X.X.X.X> --cores=<core_number> --huge-pages-1G=<huga_pages_number> --nb-10GB-interfaces==<interface_number>"
29 echo -e "Generate a develop host yaml to be used for openvim host-add\n"
30 echo -e " --user-name -u <user>* Server OAM Ip"
31 echo -e " --ip -i <ip>* Server hostname"
32 echo -e " --cores -c <cores> Numa Cores available must be an odd number and bigger or equal to 4."
33 echo -e " --huge-pages-1G -hp <huge_pages_number> Must be an odd number and bigger or equal to 16. 4GiB of memory will be reserved for the host OS, the rest will be used by VM."
34 echo -e " --nb-10GB-interfaces -ni <nb-10GB-interfaces> Dataplane interfaces must be an odd number and bigger or equal to 4."
35 echo -e " * Mandatory arguments"
36 echo -e "\n"
37 echo -e "The output will be a server descriptor with two numas and resources (memory, cores and interfaces) equally distributed between them."
38 echo -e "Each interface (physical funtion) will have defined 8 SR-IOV (virtual functions)."
39
40 exit 1
41 }
42
43 function get_hash_value() { echo `eval echo $\{\`echo $USER[$IP]\`\}`; }
44
45 function get_mac(){
46 seed=$1
47 b1=$((seed%16)); seed=$((seed/16))
48 b2=$((seed%16)); seed=$((seed/16))
49 b3=$((seed%16)); seed=$((seed/16))
50 b4=$((seed%16)); seed=$((seed/16))
51 b5=$((seed%16)); seed=$((seed/16))
52 mac=`printf "%02X:%02X:%02X:%02X:%02X:%02X" 2 $b5 $b4 $b3 $b2 $b1`
53 echo $mac
54 }
55
56 function _parse_opts(){
57
58 #help argument
59 if [ -n "$option_help" ];
60 then
61 usage
62 fi
63
64 #User argument
65 [ -z "$option_user_name" ] && echo -e "ERROR: User argument is mandatory, --user-name=<user>\n" && usage
66 USER_NAME=${option_user_name}
67
68 [ -z "$option_ip" ] && echo -e "ERROR: OAM IP argument is mandatory, --ip=<X.X.X.X>\n" && usage
69 IP=${option_ip}
70
71 #TODO to be checl diference between real cores an numa cores
72 #cores argument
73 REAL_CORES=$(grep -c "^processor" "/proc/cpuinfo")
74 if [ -z "$option_cores" ] ; then
75 CORES=REAL_CORES
76 else
77 CORES=${option_cores}
78 fi
79
80 #Ensure the core user input is big enough
81 ([ $((CORES%2)) -ne 0 ]) && echo -e "ERROR: Wrong number of cores\n" && usage
82
83 MEMORY=$(($(grep MemTotal /proc/meminfo | awk '{print $2}') /1024/1024))
84 if [ -z "$option_huge_pages_1G" ] ; then
85 HUGE_PAGES_MEMORY=0
86 else
87 HUGE_PAGES_MEMORY=${option_huge_pages_1G}
88 fi
89 #Ensure the memory user input is big enough
90 #([ $((MEMORY%2)) -ne 0 ] || [ $MEMORY -lt 16 ] ) && echo -e "ERROR: Wrong number of memory\n" && usage
91
92 #nb_10GB_interfaces argument
93 if [ -z "$nb_10GB_interfaces" ] ; then
94 INTERFACES=8
95 else
96 INTERFACES=${nb_10GB_interfaces}
97 fi
98 ([ $((INTERFACES%2)) -ne 0 ] || [ $INTERFACES -lt 4 ] ) && echo -e "ERROR: Wrong number of interfaces\n" && usage
99
100 # Parameter by default
101 NUMAS=1
102 ([ $((NUMAS%2)) -ne 0 ]) && NUMAS=1
103 }
104
105 function _generate_compute_develope_yaml(){
106
107 _yaml_init
108
109 FEATURES_LIST="lps,dioc,hwsv,tlbps,ht,lps,64b,iommu"
110
111 #Generate a cpu topology for 4 numas with hyperthreading
112 #in this developing/fake server all cores can be used
113 #TODO check if this calculation is correct
114 echo2file "#This file was created by $0"
115 echo2file "#for adding this compute node to openvim"
116 echo2file "#copy this file to openvim controller and run"
117 echo2file "#openvim host-add <this>"
118 echo2file
119 echo2file "host:"
120 echo2file " name: $HOST_NAME"
121 echo2file " user: $USER_NAME"
122 echo2file " ip_name: $IP"
123 echo2file "host-data:"
124 echo2file " name: $HOST_NAME"
125 echo2file " user: $USER_NAME"
126 echo2file " ip_name: $IP"
127 echo2file " ranking: 100"
128 echo2file " description: $HOST_NAME"
129 echo2file " features: $FEATURES_LIST"
130 echo2file " numas:"
131
132 numa=0
133 last_iface=0
134 iface_counter=0
135
136 while [ $numa -lt $NUMAS ]
137 do
138
139 HUGE_PAGES=$((HUGE_PAGES_MEMORY/2-2))
140 ([ ${HUGE_PAGES} -lt -1 ]) && HUGE_PAGES=0
141
142 echo2file " - numa_socket: $numa"
143 #MEMORY
144 echo2file " hugepages: $((HUGE_PAGES))"
145 echo2file " memory: $((MEMORY/$NUMAS))"
146 #CORES
147 echo2file " cores:"
148
149 for((n_core=0;n_core<$REAL_CORES;n_core++))
150 do
151 THREAD_ID=$(($n_core+1))
152 CORE_ID=$(($((${n_core}+${numa}))/2))
153 echo2file " - core_id: ${CORE_ID}"
154 echo2file " thread_id: ${THREAD_ID}"
155 [ $CORE_ID -eq 0 ] && echo2file " status: noteligible"
156
157 thread_counter=$((thread_counter+1))
158 done
159 # GENERATE INTERFACES INFORMATION AND PRINT IT
160 seed=$RANDOM
161 echo2file " interfaces:"
162 for ((iface=0;iface<$INTERFACES;iface+=2))
163 do
164 name="iface$iface_counter"
165 bus=$((iface+last_iface))
166 bus=$((iface))
167 pci=`printf "0000:%02X:00.0" $bus`
168 mac=`get_mac $seed`
169 seed=$((seed+1))
170
171 echo2file " - source_name: $name"
172 echo2file " Mbps: 10000"
173 echo2file " pci: \"$pci\""
174 echo2file " mac: \"$mac\""
175 echo2file " sriovs:"
176
177 PCI_COUNTER=0
178 for((nb_sriov=0;nb_sriov<8;nb_sriov++))
179 do
180 #PCI_COUNTER=$((PCI_COUNTER+2))
181 #echo2file "nb_sriov "$nb_sriov
182 #echo2file "PCI_COUNTER "$PCI_COUNTER
183
184 pci=`printf "0000:%02X:10.%i" $bus $nb_sriov`
185 mac=`get_mac $seed`
186 seed=$((seed+1))
187 echo2file " - source_name: eth$nb_sriov"
188 echo2file " mac: \"$mac\""
189 echo2file " pci: \"$pci\""
190 done
191 iface_counter=$((iface_counter+1))
192 done
193 last_iface=$(((numa+1)*127/NUMAS+5)) #made-up formula for more realistic pci numbers
194 numa=$((numa+1))
195 done
196 }
197
198 function _yaml_init(){
199 echo -n > host-develope.yaml
200 }
201
202 function echo2file(){
203 echo "${1}"
204 echo "${1}" >> host-develope.yaml
205 }
206
207 function _get_opts()
208 {
209 options="$1"
210 shift
211
212 get_argument=""
213 #reset variables
214 params=""
215 for option_group in $options
216 do
217 _name=${option_group%%:*}
218 _name=${_name%=}
219 _name=${_name//-/_}
220 eval option_${_name}='""'
221 done
222
223 while [[ $# -gt 0 ]]
224 do
225 argument="$1"
226 shift
227 if [[ -n $get_argument ]]
228 then
229 [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument" >&2 && return 1
230 eval ${get_argument}='"$argument"'
231 #echo option $get_argument with argument
232 get_argument=""
233 continue
234 fi
235
236
237 #short options
238 if [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
239 then
240 index=0
241 while index=$((index+1)) && [[ $index -lt ${#argument} ]]
242 do
243 option=${argument:$index:1}
244 bad_option=y
245 for option_group in $options
246 do
247 _name=""
248 for o in $(echo $option_group | tr ":=" " ")
249 do
250 [[ -z "$_name" ]] && _name=${o//-/_}
251 #echo option $option versus $o
252 if [[ "$option" == "${o}" ]]
253 then
254 eval option_${_name}='${option_'${_name}'}-'
255 bad_option=n
256 if [[ ${option_group:${#option_group}-1} != "=" ]]
257 then
258 continue
259 fi
260 if [[ ${#argument} -gt $((index+1)) ]]
261 then
262 eval option_${_name}='"${argument:$((index+1))}"'
263 index=${#argument}
264 else
265 get_argument=option_${_name}
266 #echo next should be argument $argument
267 fi
268
269 break
270 fi
271 done
272 done
273 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && return 1
274 done
275 elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
276 then
277 option=${argument:2}
278 option_argument=${option#*=}
279 option_name=${option%%=*}
280 [[ "$option_name" == "$option" ]] && option_argument=""
281 bad_option=y
282 for option_group in $options
283 do
284 _name=""
285 for o in $(echo $option_group | tr ":=" " ")
286 do
287 [[ -z "$_name" ]] && _name=${o//-/_}
288 #echo option $option versus $o
289 if [[ "$option_name" == "${o}" ]]
290 then
291 bad_option=n
292 if [[ ${option_group:${#option_group}-1} != "=" ]]
293 then #not an argument
294 [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && return 1
295 eval option_${_name}='"${option_'${_name}'}-"'
296 elif [[ -n "${option_argument}" ]]
297 then
298 eval option_${_name}='"${option_argument}"'
299 else
300 get_argument=option_${_name}
301 #echo next should be argument $argument
302 fi
303 break
304 fi
305 done
306 done
307 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && return 1
308 elif [[ ${argument:0:2} == "--" ]]
309 then
310 option__="$*"
311 bad_option=y
312 for o in $options
313 do
314 if [[ "$o" == "--" ]]
315 then
316 bad_option=n
317 option__=" $*"
318 break
319 fi
320 done
321 [[ $bad_option == y ]] && echo "invalid argument '--'? Type -h for help" >&2 && return 1
322 break
323 else
324 params="$params ${argument}"
325 fi
326
327 done
328
329 [[ -n "$get_argument" ]] && echo "option '-$option' requires an argument" >&2 && return 1
330 return 0
331 }
332
333 #process options
334 DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
335 DIRNAME=$(dirname $DIRNAME)
336
337 #source ${DIRNAME}/get-options.sh "help:h user:u= ip:i= cores:c= huge-pages-1G:hp= nb-10GB-interfaces:ni=" $*
338 _get_opts "help:h user-name:u= ip:i= cores:c= huge-pages-1G:hp= nb-10GB-interfaces:ni=" $* || exit 1
339 _parse_opts
340
341 #check root privileges
342 [ "${USER}" != "root" ] && echo "Needed root privileges" >&2 && exit 2
343 #check root privileges and non a root user behind
344
345
346 HOST_NAME=`cat /etc/hostname`
347
348 _generate_compute_develope_yaml
349