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 # Deliberately not depending on common.sh to allow standalone usage.
18 # Keep functions ordered alphabetically, please.
20 function activate_wrapper () {
22 # Acts as wrapper for activate docker topology.
25 # - ${1} - Node multiplicity of desired testbed.
26 # - ${2} - Node flavor string, usually describing the processor.
27 # - ${3} - CSIT-SUT-DCR image name and version.
32 get_available_interfaces "${1}" "${2}" || die
33 start_topology_containers "${3}" || die
34 bind_interfaces_to_containers || die
35 set_env_variables || die
36 print_env_variables || die
41 function bind_interfaces_to_containers () {
43 # Bind linux network interface to container and create symlink for PCI
44 # address in container.
47 # - DCR_UUIDS - Docker Container UUIDs.
48 # - DCR_CPIDS - Docker Container PIDs (namespaces).
49 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
50 # - PCI_ADDR - PCI address of network device.
51 # - TG_NETDEVS - List of network devices allocated to TG container.
53 # - NETDEV - Linux network interface.
57 for NETDEV in "${TG_NETDEVS[@]}"; do
59 link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
60 die "Reading symlink for PCI address failed!"
62 cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
64 sudo ip link set ${NETDEV} netns ${DCR_CPIDS[tg]} || {
65 die "Moving interface to ${DCR_CPIDS[tg]} namespace failed!"
67 docker exec "${DCR_UUIDS[tg]}" ${cmd} || {
68 die "Linking PCI address in container failed!"
71 for NETDEV in "${DUT1_NETDEVS[@]}"; do
73 link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
74 die "Reading symlink for PCI address failed!"
76 cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
78 sudo ip link set ${NETDEV} netns ${DCR_CPIDS[dut1]} || {
79 die "Moving interface to ${DCR_CPIDS[dut1]} namespace failed!"
81 docker exec "${DCR_UUIDS[dut1]}" ${cmd} || {
82 die "Linking PCI address in container failed!"
88 function bind_interfaces_to_driver () {
90 # Bind network interface specified by parameter to driver specified by
94 # - ADDR - PCI address of network interface.
95 # - DRIVER - Kernel driver.
99 pci_path="/sys/bus/pci/devices/${ADDR}"
100 drv_path="/sys/bus/pci/drivers/${DRIVER}"
101 vd=$(cat ${pci_path}/vendor ${pci_path}/device) || {
102 die "Failed to retrieve interface details!"
105 echo ${vd} | sudo tee ${drv_path}/new_id
107 echo ${ADDR} | sudo tee ${pci_path}/driver/unbind || {
108 die "Failed to unbind interface ${ADDR}!"
110 echo ${ADDR} | sudo tee ${drv_path}/bind || {
111 die "Failed to bind interface ${ADDR}!"
116 function clean_environment () {
118 # Cleanup environment by removing topology containers and shared volumes
119 # and binding interfaces back to original driver.
122 # - DCR_UUIDS - Docker Container UUIDs.
123 # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
124 # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
126 # - ADDR - PCI address of network interface.
127 # - DRIVER - Kernel driver.
131 # Kill docker containers.
132 docker rm --force "${DCR_UUIDS[@]}" || die "Cleanup containers failed!"
134 # Check if there are some leftover containers and remove all. Command will
135 # not fail in case there are no containers to remove.
136 docker rm --force $(docker ps -q --filter name=${DCR_UUIDS[dut1]}) || {
137 warn "Failed to remove hanged containers or nothing to remove!"
140 # Rebind interfaces back to kernel drivers.
141 for ADDR in ${TG_PCIDEVS[@]}; do
142 DRIVER="${TG_DRIVERS[0]}"
143 bind_interfaces_to_driver || die
145 for ADDR in ${DUT1_PCIDEVS[@]}; do
146 DRIVER="${DUT1_DRIVERS[0]}"
147 bind_interfaces_to_driver || die
152 function clean_environment_on_exit () {
154 # Cleanup environment by removing topology containers and binding
155 # interfaces back to original driver only if exit code is not 0.
156 # This function acts as workaround as 'set -eu' does not trigger ERR trap.
160 if [ $? -ne 0 ]; then
161 clean_environment || die
166 function deactivate_wrapper () {
168 # Acts as wrapper for deactivate docker topology.
171 # - ${@} - CSIT environment variables.
176 read_env_variables "${@}" || die
177 clean_environment || die
184 # Print the message to standard error end exit with error code specified
185 # by the second argument.
187 # Duplicate of common.sh function, as this file is also used standalone.
190 # - The default error message.
192 # - ${1} - The whole error message, be sure to quote. Optional
193 # - ${2} - the code to exit with, default: 1.
197 warn "${1:-Unspecified run-time error occurred!}"
202 function enter_mutex () {
204 # Enter mutual exclusion for protecting execution from starvation and
210 mutex_file="/tmp/mutex_file"
213 exec {lock_fd}>${mutex_file} || {
214 die "Mutex enter failed!"
216 flock --timeout "${mutex_timeout}" "${lock_fd}" || {
217 die "Calling flock() failed!"
219 # ----------------------
220 # Enter mutex succeeded.
221 warn "Mutex enter succeeded for PID $$."
225 function exit_mutex () {
227 # Exit mutual exclusion.
231 # ---------------------
232 # Remove mutex so we are not blocking others anymore.
233 flock -u "${lock_fd}" || {
234 die "Mutex destroy failed!"
236 warn "Mutex leave succeeded for PID $$."
240 function get_available_interfaces () {
242 # Find and get available Virtual functions.
245 # - ${1} - Nodeness, as set by common.sh get_test_code.
246 # - ${2} - Flavor, as set by common.sh get_test_code.
248 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
249 # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
250 # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
251 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
252 # - TG_NETDEVS - List of network devices allocated to TG container.
253 # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
254 # - TG_NETMACS - List of MAC addresses allocated to TG container.
255 # - TG_DRIVERS - List of interface drivers to TG container.
259 # Following code is specifing VFs ID based on nodeness and flavor.
260 # As there is great variability in hardware configuration outside LF,
261 # from bootstrap architecure point of view these are considered as flavors.
262 # Anyone can override flavor for its own machine and add condition here.
263 # See http://pci-ids.ucw.cz/v2.2/pci.ids for more info.
264 case_text="${1}_${2}"
265 case "${case_text}" in
267 # Add Intel Corporation XL710/X710 Virtual Function to the
274 # Add Intel Corporation XL710/X710 Virtual Function to the
277 tg_netdev=(enp5s2 enp5s3 enp5s4 enp5s5
278 enp5s6 enp5s7 enp5s8 enp5s9)
279 tg_netdev+=(enp8s2 enp8s3 enp8s4 enp8s5
280 enp8s6 enp8s7 enp8s8 enp8s9)
281 tg_netdev+=(enp8s10 enp8s11 enp8s12 enp8s13
282 enp8s14 enp8s15 enp8s16 enp8s17)
283 dut1_netdev=(enp133s2 enp133s3 enp133s4 enp133s5
284 enp133s6 enp133s7 enp133s8 enp133s9)
285 dut1_netdev+=(enp133s10 enp133s11 enp133s12 enp133s13
286 enp133s14 enp133s15 enp133s16 enp133s17)
287 dut1_netdev+=(enp5s10 enp5s11 enp5s12 enp5s13
288 enp5s14 enp5s15 enp5s16 enp5s17)
291 # Add Intel Corporation 82545EM Gigabit Ethernet Controller to the
294 tg_netdev=(enpTGa enpTGb)
295 dut1_netdev=(enpSUTa enpSUTb)
298 die "Unknown specification: ${case_text}!"
303 # TG side of connections.
309 # DUT1 side of connections.
316 # Find the first ${device_count} number of available TG Linux network
317 # VF device names. Only allowed VF PCI IDs are filtered.
318 for netdev in ${tg_netdev[@]}
320 for netdev_path in $(grep -l "${pci_id}" \
321 /sys/class/net/${netdev}*/device/device \
324 if [[ ${#TG_NETDEVS[@]} -lt ${device_count} ]]; then
325 tg_netdev_name=$(dirname ${netdev_path})
326 tg_netdev_name=$(dirname ${tg_netdev_name})
327 TG_NETDEVS+=($(basename ${tg_netdev_name}))
332 if [[ ${#TG_NETDEVS[@]} -eq ${device_count} ]]; then
338 for netdev in "${TG_NETDEVS[@]}"; do
339 # Find the index of selected tg netdev among tg_netdevs
340 # e.g. enp8s5f7 is a vf of netdev enp8s5 with index 11
341 # and the corresponding dut1 netdev is enp133s13.
342 while [[ "${netdev}" != "${tg_netdev[$i]}"* ]]; do
345 # Rename tg netdev to dut1 netdev
346 # e.g. enp8s5f7 -> enp133s13f7
347 DUT1_NETDEVS+=(${netdev/${tg_netdev[$i]}/${dut1_netdev[$i]}})
348 # Don't need to reset i, all netdevs are sorted.
351 for NETDEV in "${TG_NETDEVS[@]}"; do
356 TG_PCIDEVS+=(${PCI_ADDR})
357 TG_NETMACS+=(${MAC_ADDR})
358 TG_DRIVERS+=(${KRN_DRIVER})
359 TG_VLANS+=(${VLAN_ID})
361 for NETDEV in "${DUT1_NETDEVS[@]}"; do
366 DUT1_PCIDEVS+=(${PCI_ADDR})
367 DUT1_NETMACS+=(${MAC_ADDR})
368 DUT1_DRIVERS+=(${KRN_DRIVER})
369 DUT1_VLANS+=(${VLAN_ID})
372 # We need at least two interfaces for TG/DUT1 for building topology.
373 if [ "${#TG_NETDEVS[@]}" -ne 2 ] || [ "${#DUT1_NETDEVS[@]}" -ne 2 ]; then
374 die "Not enough linux network interfaces found!"
376 if [ "${#TG_PCIDEVS[@]}" -ne 2 ] || [ "${#DUT1_PCIDEVS[@]}" -ne 2 ]; then
377 die "Not enough pci interfaces found!"
382 function get_krn_driver () {
384 # Get kernel driver from linux network device name.
387 # - PCI_ADDR - PCI address of network device.
389 # - KRN_DRIVER - Kernel driver of network device.
393 pci_path="/sys/bus/pci/devices/${PCI_ADDR}"
394 KRN_DRIVER="$(basename $(readlink -f ${pci_path}/driver))" || {
395 die "Failed to get kernel driver of PCI interface!"
400 function get_mac_addr () {
402 # Get MAC address from linux network device name.
405 # - NETDEV - Linux network device name.
407 # - MAC_ADDR - MAC address of network device.
411 if [ -d /sys/class/net/${NETDEV}/device ]; then
412 MAC_ADDR="$(</sys/class/net/${NETDEV}/address)" || {
413 die "Failed to get MAC address of linux network interface!"
419 function get_pci_addr () {
421 # Get PCI address in <domain>:<bus:<device>.<func> format from linux network
425 # - NETDEV - Linux network device name.
427 # - PCI_ADDR - PCI address of network device.
431 if [ -d /sys/class/net/${NETDEV}/device ]; then
432 PCI_ADDR=$(basename $(readlink /sys/class/net/${NETDEV}/device)) || {
433 die "Failed to get PCI address of linux network interface!"
436 if [ ! -d /sys/bus/pci/devices/${PCI_ADDR} ]; then
437 die "PCI device ${NETDEV} doesn't exist!"
442 function get_vlan_filter () {
444 # Get VLAN stripping filter from PF searched by mac adress.
447 # - MAC_ADDR - MAC address of VF.
449 # - VLAN_ID - VLAN ids.
453 # Sed regular expression pattern.
454 exp="s/^.*vlan ([[:digit:]]+).*$/\1/"
455 VLAN_ID=$(ip link | grep vlan | grep ${MAC_ADDR} | sed -re "${exp}") || true
456 VLAN_ID="${VLAN_ID:-0}"
460 function installed () {
462 # Check if the given utility is installed. Fail if not installed.
464 # Duplicate of common.sh function, as this file is also used standalone.
467 # - ${1} - Utility to check.
469 # - 0 - If command is installed.
470 # - 1 - If command is not installed.
478 function print_env_variables () {
480 # Get environment variables prefixed by CSIT_.
484 env | grep CSIT_ || true
488 function read_env_variables () {
490 # Read environment variables from parameters.
493 # - ${@} - Variables passed as an argument.
494 # Variables read, set or exported: Multiple,
495 # see the code for the current list.
496 # TODO: Do we need to list them and their meanings?
500 for param in "$@"; do
503 declare -gA DCR_UUIDS
504 DCR_UUIDS+=([tg]="${CSIT_TG_UUID}")
505 DCR_UUIDS+=([dut1]="${CSIT_DUT1_UUID}")
506 TG_PCIDEVS=("${CSIT_TG_INTERFACES_PORT1_PCI}")
507 TG_DRIVERS=("${CSIT_TG_INTERFACES_PORT1_DRV}")
508 TG_VLANS+=("${CSIT_TG_INTERFACES_PORT1_VLAN}")
509 TG_PCIDEVS+=("${CSIT_TG_INTERFACES_PORT2_PCI}")
510 TG_DRIVERS+=("${CSIT_TG_INTERFACES_PORT2_DRV}")
511 TG_VLANS+=("${CSIT_TG_INTERFACES_PORT2_VLAN}")
512 DUT1_PCIDEVS=("${CSIT_DUT1_INTERFACES_PORT1_PCI}")
513 DUT1_DRIVERS=("${CSIT_DUT1_INTERFACES_PORT1_DRV}")
514 DUT1_VLANS+=("${CSIT_DUT1_INTERFACES_PORT1_VLAN}")
515 DUT1_PCIDEVS+=("${CSIT_DUT1_INTERFACES_PORT2_PCI}")
516 DUT1_DRIVERS+=("${CSIT_DUT1_INTERFACES_PORT2_DRV}")
517 DUT1_VLANS+=("${CSIT_DUT1_INTERFACES_PORT2_VLAN}")
521 function set_env_variables () {
523 # Set environment variables.
526 # - DCR_UUIDS - Docker Container UUIDs.
527 # - DCR_PORTS - Docker Container's SSH ports.
528 # - DUT1_NETMACS - List of network devices MAC addresses of DUT1 container.
529 # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
530 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
531 # - TG_NETMACS - List of network devices MAC addresses of TG container.
532 # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
533 # - TG_DRIVERS - List of interface drivers to TG container.
534 # Variables set: TODO.
539 CSIT_TG_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
540 die "Reading hostname IP address failed!"
542 CSIT_TG_PORT="${DCR_PORTS[tg]#*:}"
543 CSIT_TG_UUID="${DCR_UUIDS[tg]}"
544 CSIT_TG_ARCH="$(uname -i)" || {
545 die "Reading machine architecture failed!"
547 CSIT_DUT1_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
548 die "Reading hostname IP address failed!"
550 CSIT_DUT1_PORT="${DCR_PORTS[dut1]#*:}"
551 CSIT_DUT1_UUID="${DCR_UUIDS[dut1]}"
552 CSIT_DUT1_ARCH="$(uname -i)" || {
553 die "Reading machine architecture failed!"
555 CSIT_TG_INTERFACES_PORT1_MAC="${TG_NETMACS[0]}"
556 CSIT_TG_INTERFACES_PORT1_PCI="${TG_PCIDEVS[0]}"
557 CSIT_TG_INTERFACES_PORT1_DRV="${TG_DRIVERS[0]}"
558 CSIT_TG_INTERFACES_PORT1_VLAN="${TG_VLANS[0]}"
559 CSIT_TG_INTERFACES_PORT2_MAC="${TG_NETMACS[1]}"
560 CSIT_TG_INTERFACES_PORT2_PCI="${TG_PCIDEVS[1]}"
561 CSIT_TG_INTERFACES_PORT2_DRV="${TG_DRIVERS[1]}"
562 CSIT_TG_INTERFACES_PORT2_VLAN="${TG_VLANS[1]}"
563 CSIT_DUT1_INTERFACES_PORT1_MAC="${DUT1_NETMACS[0]}"
564 CSIT_DUT1_INTERFACES_PORT1_PCI="${DUT1_PCIDEVS[0]}"
565 CSIT_DUT1_INTERFACES_PORT1_DRV="${DUT1_DRIVERS[0]}"
566 CSIT_DUT1_INTERFACES_PORT1_VLAN="${DUT1_VLANS[0]}"
567 CSIT_DUT1_INTERFACES_PORT2_MAC="${DUT1_NETMACS[1]}"
568 CSIT_DUT1_INTERFACES_PORT2_PCI="${DUT1_PCIDEVS[1]}"
569 CSIT_DUT1_INTERFACES_PORT2_DRV="${DUT1_DRIVERS[1]}"
570 CSIT_DUT1_INTERFACES_PORT2_VLAN="${DUT1_VLANS[1]}"
575 function start_topology_containers () {
577 # Starts csit-sut-dcr docker containers for TG/DUT1.
580 # - CSIT_DIR - Path to existing root of local CSIT git repository.
582 # - DCR_UUIDS - Docker Container UUIDs.
583 # - DCR_PORTS - Docker Container SSH TCP ports.
584 # - DCR_CPIDS - Docker Container PIDs (namespaces).
588 if ! installed docker; then
589 die "Docker not present. Please install before continue!"
592 # If the IMAGE is not already loaded then docker run will pull the IMAGE,
593 # and all image dependencies, before it starts the container.
595 # Run the container in the background and print the new container ID.
596 dcr_stc_params="--detach=true "
597 # Give extended privileges to this container. A "privileged" container is
598 # given access to all devices and able to run nested containers.
599 dcr_stc_params+="--privileged "
600 # Publish all exposed ports to random ports on the host interfaces.
601 dcr_stc_params+="--publish-all "
602 # Automatically remove the container when it exits.
603 dcr_stc_params+="--rm "
605 dcr_stc_params+="--shm-size 512M "
606 # Override access to PCI bus by attaching a filesystem mount to the
608 dcr_stc_params+="--mount type=tmpfs,destination=/sys/bus/pci/devices "
609 # Mount vfio to be able to bind to see binded interfaces. We cannot use
610 # --device=/dev/vfio as this does not see newly binded interfaces.
611 dcr_stc_params+="--volume /dev/vfio:/dev/vfio "
612 # Mount docker.sock to be able to use docker deamon of the host.
613 dcr_stc_params+="--volume /var/run/docker.sock:/var/run/docker.sock "
614 # Mount /opt/boot/ where VM kernel and initrd are located.
615 dcr_stc_params+="--volume /opt/boot/:/opt/boot/ "
616 # Mount host hugepages for VMs.
617 dcr_stc_params+="--volume /dev/hugepages/:/dev/hugepages/ "
619 # Docker Container UUIDs.
620 declare -gA DCR_UUIDS
621 # Docker Container SSH TCP ports.
622 declare -gA DCR_PORTS
623 # Docker Container PIDs (namespaces).
624 declare -gA DCR_CPIDS
626 # Run TG and DUT1. As initial version we do support only 2-node.
627 params=(${dcr_stc_params} --name csit-tg-$(uuidgen) ${dcr_image})
628 DCR_UUIDS+=([tg]=$(docker run "${params[@]}")) || {
629 die "Failed to start TG docker container!"
631 params=(${dcr_stc_params} --name csit-dut1-$(uuidgen) ${dcr_image})
632 DCR_UUIDS+=([dut1]=$(docker run "${params[@]}")) || {
633 die "Failed to start DUT1 docker container!"
636 trap 'clean_environment_on_exit' EXIT || {
637 die "Trap attempt failed, please cleanup manually. Aborting!"
640 # Get Containers TCP ports.
641 params=(${DCR_UUIDS[tg]})
642 DCR_PORTS+=([tg]=$(docker port "${params[@]}")) || {
643 die "Failed to get port of TG docker container!"
645 params=(${DCR_UUIDS[dut1]})
646 DCR_PORTS+=([dut1]=$(docker port "${params[@]}")) || {
647 die "Failed to get port of DUT1 docker container!"
650 # Get Containers PIDs.
651 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[tg]})
652 DCR_CPIDS+=([tg]=$(docker inspect "${params[@]}")) || {
653 die "Failed to get PID of TG docker container!"
655 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[dut1]})
656 DCR_CPIDS+=([dut1]=$(docker inspect "${params[@]}")) || {
657 die "Failed to get PID of DUT1 docker container!"
662 # Print the message to standard error.
664 # Duplicate of common.sh function, as this file is also used standalone.
667 # - ${@} - The text of the message.