1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
16 # This library defines functions used by multiple entry scripts.
17 # Keep functions ordered alphabetically, please.
19 function activate_wrapper () {
20 # Acts as wrapper for activate docker topology.
23 # - ${1} - Node multiplicity of desired testbed.
24 # - ${2} - Node flavor string, usually describing the processor.
25 # - ${3} - CSIT-SUT-DCR image name and version.
30 get_available_interfaces "${1}" "${2}" || die
31 start_topology_containers "${3}" || die
32 bind_interfaces_to_containers || die
33 set_env_variables || die
34 print_env_variables || die
39 function bind_interfaces_to_containers () {
40 # Bind linux network interface to container and create symlink for PCI
41 # address in container.
44 # - DCR_UUIDS - Docker Container UUIDs.
45 # - DCR_CPIDS - Docker Container PIDs (namespaces).
46 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
47 # - PCI_ADDR - PCI address of network device.
48 # - TG_NETDEVS - List of network devices allocated to TG container.
50 # - NETDEV - Linux network interface.
54 for NETDEV in "${TG_NETDEVS[@]}"; do
56 link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
57 die "Reading symlink for PCI address failed!"
59 cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
61 sudo ip link set ${NETDEV} netns ${DCR_CPIDS[tg]} || {
62 die "Moving interface to ${DCR_CPIDS[tg]} namespace failed!"
64 docker exec "${DCR_UUIDS[tg]}" ${cmd} || {
65 die "Linking PCI address in container failed!"
68 for NETDEV in "${DUT1_NETDEVS[@]}"; do
70 link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
71 die "Reading symlink for PCI address failed!"
73 cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
75 sudo ip link set ${NETDEV} netns ${DCR_CPIDS[dut1]} || {
76 die "Moving interface to ${DCR_CPIDS[dut1]} namespace failed!"
78 docker exec "${DCR_UUIDS[dut1]}" ${cmd} || {
79 die "Linking PCI address in container failed!"
85 function bind_interfaces_to_driver () {
86 # Bind network interface specified by parameter to driver specified by
90 # - ADDR - PCI address of network interface.
91 # - DRIVER - Kernel driver.
93 pci_path="/sys/bus/pci/devices/${ADDR}"
94 drv_path="/sys/bus/pci/drivers/${DRIVER}"
95 vd="$(cat ${pci_path}/vendor ${pci_path}/device)" || {
96 die "Failed to retrieve interface details!"
99 echo ${vd} | sudo tee ${drv_path}/new_id
101 echo ${ADDR} | sudo tee ${pci_path}/driver/unbind || {
102 die "Failed to unbind interface ${ADDR}!"
104 echo ${ADDR} | sudo tee ${drv_path}/bind || {
105 die "Failed to bind interface ${ADDR}!"
110 function clean_environment () {
111 # Cleanup environment by removing topology containers and shared volumes
112 # and binding interfaces back to original driver.
115 # - DCR_UUIDS - Docker Container UUIDs.
116 # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
117 # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
119 # - ADDR - PCI address of network interface.
120 # - DRIVER - Kernel driver.
124 # Kill docker containers.
125 docker rm --force "${DCR_UUIDS[@]}" || die "Cleanup containers failed!"
127 # Remove DUT1 /tmp volume
128 docker volume rm "${DCR_VOLUMES[dut1]}" || {
129 die "Failed to remove DUT1 /tmp volume!"
132 # Rebind interfaces back to kernel drivers.
133 for ADDR in ${TG_PCIDEVS[@]}; do
134 DRIVER="${TG_DRIVERS[0]}"
135 bind_interfaces_to_driver || die
137 for ADDR in ${DUT1_PCIDEVS[@]}; do
138 DRIVER="${DUT1_DRIVERS[0]}"
139 bind_interfaces_to_driver || die
144 function clean_environment_on_exit () {
145 # Cleanup environment by removing topology containers and binding
146 # interfaces back to original driver only if exit code is not 0.
147 # This function acts as workaround as 'set -eu' does not trigger ERR trap.
149 if [ $? -ne 0 ]; then
150 clean_environment || die
155 function deactivate_wrapper () {
156 # Acts as wrapper for deactivate docker topology.
159 # - ${@} - CSIT environment variables.
164 read_env_variables "${@}" || die
165 clean_environment || die
171 # Print the message to standard error end exit with error code specified
172 # by the second argument.
175 # - The default error message.
177 # - ${1} - The whole error message, be sure to quote. Optional
178 # - ${2} - the code to exit with, default: 1.
182 warn "${1:-Unspecified run-time error occurred!}"
187 function enter_mutex () {
188 # Enter mutual exclusion for protecting execution from starvation and
194 mutex_file="/tmp/mutex_file"
197 exec {lock_fd}>${mutex_file} || {
198 die "Mutex enter failed!"
200 flock --timeout "${mutex_timeout}" "${lock_fd}" || {
201 die "Calling flock() failed!"
203 # ----------------------
204 # Enter mutex succeeded.
205 warn "Mutex enter succeeded for PID $$."
209 function exit_mutex () {
210 # Exit mutual exclusion.
214 # ---------------------
215 # Remove mutex so we are not blocking others anymore.
216 flock -u "${lock_fd}" || {
217 die "Mutex destroy failed!"
219 warn "Mutex leave succeeded for PID $$."
223 function get_available_interfaces () {
224 # Find and get available Virtual functions.
227 # - ${1} - Node flavor string, usually describing the processor and node
228 # multiplicity of desired testbed, separated by underscore.
230 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
231 # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
232 # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
233 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
234 # - TG_NETDEVS - List of network devices allocated to TG container.
235 # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
236 # - TG_NETMACS - List of MAC addresses allocated to TG container.
237 # - TG_DRIVERS - List of interface drivers to TG container.
241 # Following code is specifing VFs ID based on nodeness and flavor.
242 # As there is great variability in hardware configuration outside LF,
243 # from bootstrap architecure point of view these are considered as flavors.
244 # Anyone can override flavor for its own machine and add condition here.
245 # See http://pci-ids.ucw.cz/v2.2/pci.ids for more info.
246 case_text="${1}_${2}"
247 case "${case_text}" in
249 # Add Intel Corporation XL710/X710 Virtual Function to the
256 # Add Intel Corporation 82545EM Gigabit Ethernet Controller to the
259 tg_netdev=(eth1 eth2)
260 dut1_netdev=(eth3 eth4)
263 die "Unknown specification: ${case_text}!"
266 net_path="/sys/bus/pci/devices/*/net/*"
268 # TG side of connections.
273 # DUT1 side of connections.
279 # Following code is filtering available VFs represented by network device
280 # name. Only allowed VFs PCI IDs are used.
282 $(find ${net_path} -type d -name . -o -prune -exec basename '{}' ';');
284 if grep -q "${pci_id}" "/sys/class/net/${netdev}/device/device"; then
285 # We will filter to TG/DUT1 side of connection (this can be in
286 # future overriden by more advanced conditions for mapping).
287 for sub in ${tg_netdev[@]}; do
288 if [[ "${netdev#*$sub}" != "${netdev}" ]]; then
292 for sub in ${dut1_netdev[@]}; do
293 if [[ "${netdev#*$sub}" != "${netdev}" ]]; then
294 dut1_side+=(${netdev})
300 for netdev in "${tg_side[@]::2}"; do
301 TG_NETDEVS+=(${netdev})
303 for netdev in "${dut1_side[@]::2}"; do
304 DUT1_NETDEVS+=(${netdev})
307 for NETDEV in "${TG_NETDEVS[@]}"; do
311 TG_PCIDEVS+=(${PCI_ADDR})
312 TG_NETMACS+=(${MAC_ADDR})
313 TG_DRIVERS+=(${KRN_DRIVER})
315 for NETDEV in "${DUT1_NETDEVS[@]}"; do
319 DUT1_PCIDEVS+=(${PCI_ADDR})
320 DUT1_NETMACS+=(${MAC_ADDR})
321 DUT1_DRIVERS+=(${KRN_DRIVER})
324 # We need at least two interfaces for TG/DUT1 for building topology.
325 if [ "${#TG_NETDEVS[@]}" -ne 2 ] || [ "${#DUT1_NETDEVS[@]}" -ne 2 ]; then
326 die "Not enough linux network interfaces found!"
328 if [ "${#TG_PCIDEVS[@]}" -ne 2 ] || [ "${#DUT1_PCIDEVS[@]}" -ne 2 ]; then
329 die "Not enough pci interfaces found!"
334 function get_krn_driver () {
335 # Get kernel driver from linux network device name.
338 # - PCI_ADDR - PCI address of network device.
340 # - KRN_DRIVER - Kernel driver of network device.
344 pci_path="/sys/bus/pci/devices/${PCI_ADDR}"
345 KRN_DRIVER="$(basename $(readlink -f ${pci_path}/driver))" || {
346 die "Failed to get kernel driver of PCI interface!"
351 function get_mac_addr () {
352 # Get MAC address from linux network device name.
355 # - NETDEV - Linux network device name.
357 # - MAC_ADDR - MAC address of network device.
361 if [ -d /sys/class/net/${NETDEV}/device ]; then
362 MAC_ADDR="$(</sys/class/net/${NETDEV}/address)" || {
363 die "Failed to get MAC address of linux network interface!"
369 function get_pci_addr () {
370 # Get PCI address in <domain>:<bus:<device>.<func> format from linux network
374 # - NETDEV - Linux network device name.
376 # - PCI_ADDR - PCI address of network device.
380 if [ -d /sys/class/net/${NETDEV}/device ]; then
381 PCI_ADDR=$(basename $(readlink /sys/class/net/${NETDEV}/device)) || {
382 die "Failed to get PCI address of linux network interface!"
385 if [ ! -d /sys/bus/pci/devices/${PCI_ADDR} ]; then
386 die "PCI device ${NETDEV} doesn't exist!"
391 function installed () {
395 # Check if the given utility is installed. Fail if not installed.
398 # - ${1} - Utility to check.
400 # - 0 - If command is installed.
401 # - 1 - If command is not installed.
407 function print_env_variables () {
408 # Get environment variables prefixed by CSIT_.
416 function read_env_variables () {
417 # Read environment variables from parameters.
420 # - ${@} - Variables passed as an argument.
424 for param in "$@"; do
427 declare -gA DCR_UUIDS
428 declare -gA DCR_VOLUMES
429 DCR_UUIDS+=([tg]="${CSIT_TG_UUID}")
430 DCR_UUIDS+=([dut1]="${CSIT_DUT1_UUID}")
431 DCR_VOLUMES+=([dut1]="${CSIT_DUT1_VOL}")
432 TG_PCIDEVS=("${CSIT_TG_INTERFACES_PORT1_PCI}")
433 TG_DRIVERS=("${CSIT_TG_INTERFACES_PORT1_DRV}")
434 TG_PCIDEVS+=("${CSIT_TG_INTERFACES_PORT2_PCI}")
435 TG_DRIVERS+=("${CSIT_TG_INTERFACES_PORT2_DRV}")
436 DUT1_PCIDEVS=("${CSIT_DUT1_INTERFACES_PORT1_PCI}")
437 DUT1_DRIVERS=("${CSIT_DUT1_INTERFACES_PORT1_DRV}")
438 DUT1_PCIDEVS+=("${CSIT_DUT1_INTERFACES_PORT2_PCI}")
439 DUT1_DRIVERS+=("${CSIT_DUT1_INTERFACES_PORT2_DRV}")
443 function set_env_variables () {
444 # Set environment variables.
447 # - DCR_UUIDS - Docker Container UUIDs.
448 # - DCR_PORTS - Docker Container's SSH ports.
449 # - DUT1_NETMACS - List of network devices MAC addresses of DUT1 container.
450 # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
451 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
452 # - TG_NETMACS - List of network devices MAC addresses of TG container.
453 # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
454 # - TG_DRIVERS - List of interface drivers to TG container.
459 CSIT_TG_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
460 die "Reading hostname IP address failed!"
462 CSIT_TG_PORT="${DCR_PORTS[tg]#*:}"
463 CSIT_TG_UUID="${DCR_UUIDS[tg]}"
464 CSIT_TG_ARCH="$(uname -i)" || {
465 die "Reading machine architecture failed!"
467 CSIT_DUT1_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
468 die "Reading hostname IP address failed!"
470 CSIT_DUT1_PORT="${DCR_PORTS[dut1]#*:}"
471 CSIT_DUT1_UUID="${DCR_UUIDS[dut1]}"
472 CSIT_DUT1_ARCH="$(uname -i)" || {
473 die "Reading machine architecture failed!"
475 CSIT_DUT1_VOL="${DCR_VOLUMES[dut1]}"
476 CSIT_TG_INTERFACES_PORT1_MAC="${TG_NETMACS[0]}"
477 CSIT_TG_INTERFACES_PORT1_PCI="${TG_PCIDEVS[0]}"
478 CSIT_TG_INTERFACES_PORT1_DRV="${TG_DRIVERS[0]}"
479 CSIT_TG_INTERFACES_PORT2_MAC="${TG_NETMACS[1]}"
480 CSIT_TG_INTERFACES_PORT2_PCI="${TG_PCIDEVS[1]}"
481 CSIT_TG_INTERFACES_PORT2_DRV="${TG_DRIVERS[1]}"
482 CSIT_DUT1_INTERFACES_PORT1_MAC="${DUT1_NETMACS[0]}"
483 CSIT_DUT1_INTERFACES_PORT1_PCI="${DUT1_PCIDEVS[0]}"
484 CSIT_DUT1_INTERFACES_PORT1_DRV="${DUT1_DRIVERS[0]}"
485 CSIT_DUT1_INTERFACES_PORT2_MAC="${DUT1_NETMACS[1]}"
486 CSIT_DUT1_INTERFACES_PORT2_PCI="${DUT1_PCIDEVS[1]}"
487 CSIT_DUT1_INTERFACES_PORT2_DRV="${DUT1_DRIVERS[1]}"
492 function start_topology_containers () {
493 # Starts csit-sut-dcr docker containers for TG/DUT1.
496 # - CSIT_DIR - Path to existing root of local CSIT git repository.
498 # - DCR_UUIDS - Docker Container UUIDs.
499 # - DCR_PORTS - Docker Container SSH TCP ports.
500 # - DCR_CPIDS - Docker Container PIDs (namespaces).
504 if ! installed docker; then
505 die "Docker not present. Please install before continue!"
508 # If the IMAGE is not already loaded then docker run will pull the IMAGE,
509 # and all image dependencies, before it starts the container.
511 # Run the container in the background and print the new container ID.
512 dcr_stc_params="--detach=true "
513 # Give extended privileges to this container. A "privileged" container is
514 # given access to all devices and able to run nested containers.
515 dcr_stc_params+="--privileged "
516 # Publish all exposed ports to random ports on the host interfaces.
517 dcr_stc_params+="--publish-all "
518 # Automatically remove the container when it exits.
519 dcr_stc_params+="--rm "
521 dcr_stc_params+="--shm-size 512M "
522 # Override access to PCI bus by attaching a filesystem mount to the
524 dcr_stc_params+="--mount type=tmpfs,destination=/sys/bus/pci/devices "
525 # Mount vfio to be able to bind to see binded interfaces. We cannot use
526 # --device=/dev/vfio as this does not see newly binded interfaces.
527 dcr_stc_params+="--volume /dev/vfio:/dev/vfio "
528 # Mount nested_vm image to be able to run VM tests.
529 dcr_stc_params+="--volume /var/lib/vm/vhost-nested.img:/var/lib/vm/vhost-nested.img "
530 # Mount docker.sock to be able to use docker deamon of the host.
531 dcr_stc_params+="--volume /var/run/docker.sock:/var/run/docker.sock "
533 # Docker Container UUIDs.
534 declare -gA DCR_UUIDS
535 # Docker Container SSH TCP ports.
536 declare -gA DCR_PORTS
537 # Docker Container PIDs (namespaces).
538 declare -gA DCR_CPIDS
539 # Docker Container volumes with no relationship to the host.
540 declare -gA DCR_VOLUMES
542 # Create DUT1 /tmp volume to be able to install VPP in "nested" container.
543 params=(--name DUT1_VOL_$(uuidgen))
544 DCR_VOLUMES+=([dut1]="$(docker volume create "${params[@]}")") || {
545 die "Failed to create DUT1 /tmp volume!"
548 # Mount DUT1_VOL as /tmp directory on DUT1 container
549 dcr_stc_params_dut1="--volume ${DCR_VOLUMES[dut1]}:/tmp "
551 # Run TG and DUT1. As initial version we do support only 2-node.
552 params=(${dcr_stc_params} --name csit-tg-$(uuidgen) ${dcr_image})
553 DCR_UUIDS+=([tg]="$(docker run "${params[@]}")") || {
554 die "Failed to start TG docker container!"
556 params=(${dcr_stc_params} ${dcr_stc_params_dut1}
557 --name csit-dut1-$(uuidgen) ${dcr_image})
558 DCR_UUIDS+=([dut1]="$(docker run "${params[@]}")") || {
559 die "Failed to start DUT1 docker container!"
562 trap 'clean_environment_on_exit' EXIT || {
563 die "Trap attempt failed, please cleanup manually. Aborting!"
566 # Get Containers TCP ports.
567 params=(${DCR_UUIDS[tg]})
568 DCR_PORTS+=([tg]="$(docker port "${params[@]}")") || {
569 die "Failed to get port of TG docker container!"
571 params=(${DCR_UUIDS[dut1]})
572 DCR_PORTS+=([dut1]="$(docker port "${params[@]}")") || {
573 die "Failed to get port of DUT1 docker container!"
576 # Get Containers PIDs.
577 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[tg]})
578 DCR_CPIDS+=([tg]="$(docker inspect "${params[@]}")") || {
579 die "Failed to get PID of TG docker container!"
581 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[dut1]})
582 DCR_CPIDS+=([dut1]="$(docker inspect "${params[@]}")") || {
583 die "Failed to get PID of DUT1 docker container!"
588 # Print the message to standard error.
591 # - ${@} - The text of the message.