1 # Copyright (c) 2021 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 if [ -d "${pci_path}/driver" ]; then
102 echo ${ADDR} | sudo tee ${pci_path}/driver/unbind || {
103 die "Failed to unbind interface ${ADDR}!"
106 echo ${ADDR} | sudo tee ${drv_path}/bind || {
107 die "Failed to bind interface ${ADDR}!"
112 function clean_environment () {
114 # Cleanup environment by removing topology containers and shared volumes
115 # and binding interfaces back to original driver.
118 # - DCR_UUIDS - Docker Container UUIDs.
119 # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
120 # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
122 # - ADDR - PCI address of network interface.
123 # - DRIVER - Kernel driver.
127 # Kill docker containers.
128 docker rm --force "${DCR_UUIDS[@]}" || die "Cleanup containers failed!"
130 # Check if there are some leftover containers and remove all. Command will
131 # not fail in case there are no containers to remove.
132 docker rm --force $(docker ps -q --filter name=${DCR_UUIDS[dut1]}) || {
133 warn "Failed to remove hanged containers or nothing to remove!"
136 # Rebind interfaces back to kernel drivers.
137 for ADDR in ${TG_PCIDEVS[@]}; do
138 DRIVER="${TG_DRIVERS[0]}"
139 bind_interfaces_to_driver || die
141 for ADDR in ${DUT1_PCIDEVS[@]}; do
142 DRIVER="${DUT1_DRIVERS[0]}"
143 bind_interfaces_to_driver || die
148 function clean_environment_on_exit () {
150 # Cleanup environment by removing topology containers and binding
151 # interfaces back to original driver only if exit code is not 0.
152 # This function acts as workaround as 'set -eu' does not trigger ERR trap.
156 if [ $? -ne 0 ]; then
157 clean_environment || die
162 function deactivate_wrapper () {
164 # Acts as wrapper for deactivate docker topology.
167 # - ${@} - CSIT environment variables.
172 read_env_variables "${@}" || die
173 clean_environment || die
180 # Print the message to standard error end exit with error code specified
181 # by the second argument.
183 # Duplicate of common.sh function, as this file is also used standalone.
186 # - The default error message.
188 # - ${1} - The whole error message, be sure to quote. Optional
189 # - ${2} - the code to exit with, default: 1.
193 warn "${1:-Unspecified run-time error occurred!}"
198 function enter_mutex () {
200 # Enter mutual exclusion for protecting execution from starvation and
206 mutex_file="/tmp/mutex_file"
209 exec {lock_fd}>${mutex_file} || {
210 die "Mutex enter failed!"
212 flock --timeout "${mutex_timeout}" "${lock_fd}" || {
213 die "Calling flock() failed!"
215 # ----------------------
216 # Enter mutex succeeded.
217 warn "Mutex enter succeeded for PID $$."
221 function exit_mutex () {
223 # Exit mutual exclusion.
227 # ---------------------
228 # Remove mutex so we are not blocking others anymore.
229 flock -u "${lock_fd}" || {
230 die "Mutex destroy failed!"
232 warn "Mutex leave succeeded for PID $$."
236 function get_available_interfaces () {
238 # Find and get available Virtual functions.
241 # - ${1} - Nodeness, as set by common.sh get_test_code.
242 # - ${2} - Flavor, as set by common.sh get_test_code.
244 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
245 # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
246 # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
247 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
248 # - DUT1_VLANS - List of interface vlans to TG container.
249 # - DUT1_MODEL - List of interface models to TG container.
250 # - TG_NETDEVS - List of network devices allocated to TG container.
251 # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
252 # - TG_NETMACS - List of MAC addresses allocated to TG container.
253 # - TG_DRIVERS - List of interface drivers to TG container.
254 # - TG_VLANS - List of interface vlans to TG container.
255 # - TG_MODEL - List of interface models to TG container.
259 # Following code is specifying VFs ID based on nodeness and flavor.
260 # As there is great variability in hardware configuration outside LF,
261 # from bootstrap architecture 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
269 pci_id="0x154c\|0x1889"
270 tg_netdev=(ens1 enp134)
271 dut1_netdev=(ens5 enp175)
275 # Add Intel Corporation XL710/X710 Virtual Function to the
283 # Add Intel Corporation 82545EM Gigabit Ethernet Controller to the
286 tg_netdev=(enp0s8 enp0s9)
287 dut1_netdev=(enp0s16 enp0s17)
291 die "Unknown specification: ${case_text}!"
294 # TG side of connections.
301 # DUT1 side of connections.
309 # Find the first ${device_count} number of available TG Linux network
310 # VF device names. Only allowed VF PCI IDs are filtered.
311 for netdev in ${tg_netdev[@]}
314 for netdev_path in $(grep -l "${pci_id}" \
315 /sys/class/net/${netdev}*/device/device \
318 if [[ ${ports} -lt ${ports_per_nic} ]]; then
319 tg_netdev_name=$(dirname ${netdev_path})
320 tg_netdev_name=$(dirname ${tg_netdev_name})
321 TG_NETDEVS+=($(basename ${tg_netdev_name}))
327 ports_per_device=$((${ports_per_nic}*${#tg_netdev[@]}))
328 if [[ ${#TG_NETDEVS[@]} -eq ${ports_per_device} ]]; then
334 for netdev in "${TG_NETDEVS[@]}"; do
335 # Find the index of selected tg netdev among tg_netdevs
336 # e.g. enp8s5f7 is a vf of netdev enp8s5 with index 11
337 # and the corresponding dut1 netdev is enp133s13.
338 while [[ "${netdev}" != "${tg_netdev[$i]}"* ]]; do
341 # Rename tg netdev to dut1 netdev
342 # e.g. enp8s5f7 -> enp133s13f7
343 DUT1_NETDEVS+=(${netdev/${tg_netdev[$i]}/${dut1_netdev[$i]}})
344 # Don't need to reset i, all netdevs are sorted.
347 for NETDEV in "${TG_NETDEVS[@]}"; do
353 TG_PCIDEVS+=(${PCI_ADDR})
354 TG_NETMACS+=(${MAC_ADDR})
355 TG_DRIVERS+=(${KRN_DRIVER})
356 TG_VLANS+=(${VLAN_ID})
357 TG_MODELS+=(${MODEL})
359 for NETDEV in "${DUT1_NETDEVS[@]}"; do
365 DUT1_PCIDEVS+=(${PCI_ADDR})
366 DUT1_NETMACS+=(${MAC_ADDR})
367 DUT1_DRIVERS+=(${KRN_DRIVER})
368 DUT1_VLANS+=(${VLAN_ID})
369 DUT1_MODELS+=(${MODEL})
372 # We need at least two interfaces for TG/DUT1 for building topology.
373 if [ "${#TG_NETDEVS[@]}" -lt 2 ] || [ "${#DUT1_NETDEVS[@]}" -lt 2 ]; then
374 die "Not enough linux network interfaces found!"
379 function get_krn_driver () {
381 # Get kernel driver from linux network device name.
384 # - PCI_ADDR - PCI address of network device.
386 # - KRN_DRIVER - Kernel driver of network device.
390 pci_path="/sys/bus/pci/devices/${PCI_ADDR}"
391 KRN_DRIVER="$(basename $(readlink -f ${pci_path}/driver))" || {
392 die "Failed to get kernel driver of PCI interface!"
397 function get_mac_addr () {
399 # Get MAC address from linux network device name.
402 # - NETDEV - Linux network device name.
404 # - MAC_ADDR - MAC address of network device.
408 if [ -d /sys/class/net/${NETDEV}/device ]; then
409 MAC_ADDR="$(</sys/class/net/${NETDEV}/address)" || {
410 die "Failed to get MAC address of linux network interface!"
416 function get_csit_model () {
418 # Get CSIT model name from linux network device name.
421 # - NETDEV - Linux network device name.
423 # - MODEL - CSIT model name of network device.
427 if [ -d /sys/class/net/${NETDEV}/device ]; then
428 ID="$(</sys/class/net/${NETDEV}/device/device)" || {
429 die "Failed to get device id of linux network interface!"
445 function get_pci_addr () {
447 # Get PCI address in <domain>:<bus:<device>.<func> format from linux network
451 # - NETDEV - Linux network device name.
453 # - PCI_ADDR - PCI address of network device.
457 if [ -d /sys/class/net/${NETDEV}/device ]; then
458 PCI_ADDR=$(basename $(readlink /sys/class/net/${NETDEV}/device)) || {
459 die "Failed to get PCI address of linux network interface!"
462 if [ ! -d /sys/bus/pci/devices/${PCI_ADDR} ]; then
463 die "PCI device ${NETDEV} doesn't exist!"
468 function get_vlan_filter () {
470 # Get VLAN stripping filter from PF searched by mac adress.
473 # - MAC_ADDR - MAC address of VF.
475 # - VLAN_ID - VLAN ids.
479 # Sed regular expression pattern.
480 exp="s/^.*vlan ([[:digit:]]+).*$/\1/"
481 VLAN_ID=$(ip link | grep vlan | grep ${MAC_ADDR} | sed -re "${exp}") || true
482 VLAN_ID="${VLAN_ID:-0}"
486 function installed () {
488 # Check if the given utility is installed. Fail if not installed.
490 # Duplicate of common.sh function, as this file is also used standalone.
493 # - ${1} - Utility to check.
495 # - 0 - If command is installed.
496 # - 1 - If command is not installed.
504 function parse_env_variables () {
506 # Parse environment variables.
508 # Variables read, set or exported: Multiple,
509 # see the code for the current list.
513 IFS=@ read -a TG_NETMACS <<< "${CSIT_TG_INTERFACES_PORT_MAC}"
514 IFS=@ read -a TG_PCIDEVS <<< "${CSIT_TG_INTERFACES_PORT_PCI}"
515 IFS=@ read -a TG_DRIVERS <<< "${CSIT_TG_INTERFACES_PORT_DRV}"
516 IFS=@ read -a TG_VLANS <<< "${CSIT_TG_INTERFACES_PORT_VLAN}"
517 IFS=@ read -a TG_MODELS <<< "${CSIT_TG_INTERFACES_PORT_MODEL}"
518 IFS=@ read -a DUT1_NETMACS <<< "${CSIT_DUT1_INTERFACES_PORT_MAC}"
519 IFS=@ read -a DUT1_PCIDEVS <<< "${CSIT_DUT1_INTERFACES_PORT_PCI}"
520 IFS=@ read -a DUT1_DRIVERS <<< "${CSIT_DUT1_INTERFACES_PORT_DRV}"
521 IFS=@ read -a DUT1_VLANS <<< "${CSIT_DUT1_INTERFACES_PORT_VLAN}"
522 IFS=@ read -a DUT1_MODELS <<< "${CSIT_DUT1_INTERFACES_PORT_MODEL}"
524 for port in $(seq "${#TG_NETMACS[*]}"); do
525 CSIT_TG_INTERFACES+=$(cat << EOF
527 mac_address: "${TG_NETMACS[$((port-1))]}"
528 pci_address: "${TG_PCIDEVS[$((port-1))]}"
529 link: "link$((port-1))"
530 model: ${TG_MODELS[$((port-1))]}
531 driver: "${TG_DRIVERS[$((port-1))]}"
532 vlan: ${TG_VLANS[$((port-1))]}
535 CSIT_TG_INTERFACES+=$'\n'
537 for port in $(seq "${#DUT1_NETMACS[*]}"); do
538 CSIT_DUT1_INTERFACES+=$(cat << EOF
540 mac_address: "${DUT1_NETMACS[$((port-1))]}"
541 pci_address: "${DUT1_PCIDEVS[$((port-1))]}"
542 link: "link$((port-1))"
543 model: ${DUT1_MODELS[$((port-1))]}
544 driver: "${DUT1_DRIVERS[$((port-1))]}"
545 vlan: ${DUT1_VLANS[$((port-1))]}
548 CSIT_DUT1_INTERFACES+=$'\n'
553 function print_env_variables () {
555 # Get environment variables prefixed by CSIT_.
559 env | grep CSIT_ || true
563 function read_env_variables () {
565 # Read environment variables from parameters.
568 # - ${@} - Variables passed as an argument.
569 # Variables read, set or exported: Multiple,
570 # see the code for the current list.
574 for param in "$@"; do
577 declare -gA DCR_UUIDS
578 DCR_UUIDS+=([tg]="${CSIT_TG_UUID}")
579 DCR_UUIDS+=([dut1]="${CSIT_DUT1_UUID}")
581 IFS=@ read -a TG_NETMACS <<< "${CSIT_TG_INTERFACES_PORT_MAC}"
582 IFS=@ read -a TG_PCIDEVS <<< "${CSIT_TG_INTERFACES_PORT_PCI}"
583 IFS=@ read -a TG_DRIVERS <<< "${CSIT_TG_INTERFACES_PORT_DRV}"
584 IFS=@ read -a TG_VLANS <<< "${CSIT_TG_INTERFACES_PORT_VLAN}"
585 IFS=@ read -a TG_MODELS <<< "${CSIT_TG_INTERFACES_PORT_MODEL}"
586 IFS=@ read -a DUT1_NETMACS <<< "${CSIT_DUT1_INTERFACES_PORT_MAC}"
587 IFS=@ read -a DUT1_PCIDEVS <<< "${CSIT_DUT1_INTERFACES_PORT_PCI}"
588 IFS=@ read -a DUT1_DRIVERS <<< "${CSIT_DUT1_INTERFACES_PORT_DRV}"
589 IFS=@ read -a DUT1_VLANS <<< "${CSIT_DUT1_INTERFACES_PORT_VLAN}"
590 IFS=@ read -a DUT1_MODELS <<< "${CSIT_DUT1_INTERFACES_PORT_MODEL}"
594 function set_env_variables () {
596 # Set environment variables.
599 # - DCR_UUIDS - Docker Container UUIDs.
600 # - DCR_PORTS - Docker Container's SSH ports.
601 # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
602 # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
603 # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
604 # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
605 # - DUT1_VLANS - List of interface vlans to TG container.
606 # - DUT1_MODEL - List of interface models to TG container.
607 # - TG_NETDEVS - List of network devices allocated to TG container.
608 # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
609 # - TG_NETMACS - List of MAC addresses allocated to TG container.
610 # - TG_DRIVERS - List of interface drivers to TG container.
611 # - TG_VLANS - List of interface vlans to TG container.
612 # - TG_MODEL - List of interface models to TG container.
617 CSIT_TG_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
618 die "Reading hostname IP address failed!"
620 CSIT_TG_PORT="${DCR_PORTS[tg]#*:}"
621 CSIT_TG_UUID="${DCR_UUIDS[tg]}"
622 CSIT_TG_ARCH="$(uname -i)" || {
623 die "Reading machine architecture failed!"
625 CSIT_DUT1_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
626 die "Reading hostname IP address failed!"
628 CSIT_DUT1_PORT="${DCR_PORTS[dut1]#*:}"
629 CSIT_DUT1_UUID="${DCR_UUIDS[dut1]}"
630 CSIT_DUT1_ARCH="$(uname -i)" || {
631 die "Reading machine architecture failed!"
635 CSIT_TG_INTERFACES_PORT_MAC="${TG_NETMACS[*]}"
636 CSIT_TG_INTERFACES_PORT_PCI="${TG_PCIDEVS[*]}"
637 CSIT_TG_INTERFACES_PORT_DRV="${TG_DRIVERS[*]}"
638 CSIT_TG_INTERFACES_PORT_VLAN="${TG_VLANS[*]}"
639 CSIT_TG_INTERFACES_PORT_MODEL="${TG_MODELS[*]}"
640 CSIT_DUT1_INTERFACES_PORT_MAC="${DUT1_NETMACS[*]}"
641 CSIT_DUT1_INTERFACES_PORT_PCI="${DUT1_PCIDEVS[*]}"
642 CSIT_DUT1_INTERFACES_PORT_DRV="${DUT1_DRIVERS[*]}"
643 CSIT_DUT1_INTERFACES_PORT_VLAN="${DUT1_VLANS[*]}"
644 CSIT_DUT1_INTERFACES_PORT_MODEL="${DUT1_MODELS[*]}"
650 function start_topology_containers () {
652 # Starts csit-sut-dcr docker containers for TG/DUT1.
655 # - CSIT_DIR - Path to existing root of local CSIT git repository.
657 # - DCR_UUIDS - Docker Container UUIDs.
658 # - DCR_PORTS - Docker Container SSH TCP ports.
659 # - DCR_CPIDS - Docker Container PIDs (namespaces).
663 if ! installed docker; then
664 die "Docker not present. Please install before continue!"
667 # If the IMAGE is not already loaded then docker run will pull the IMAGE,
668 # and all image dependencies, before it starts the container.
670 # Run the container in the background and print the new container ID.
671 dcr_stc_params="--detach=true "
672 # Give extended privileges to this container. A "privileged" container is
673 # given access to all devices and able to run nested containers.
674 dcr_stc_params+="--privileged "
675 # Publish all exposed ports to random ports on the host interfaces.
676 dcr_stc_params+="--publish-all "
677 # Automatically remove the container when it exits.
678 dcr_stc_params+="--rm "
680 dcr_stc_params+="--shm-size 2G "
681 # Override access to PCI bus by attaching a filesystem mount to the
683 dcr_stc_params+="--mount type=tmpfs,destination=/sys/bus/pci/devices "
684 # Mount vfio to be able to bind to see bound interfaces. We cannot use
685 # --device=/dev/vfio as this does not see newly bound interfaces.
686 dcr_stc_params+="--volume /dev/vfio:/dev/vfio "
687 # Disable manipulation with hugepages by VPP.
688 dcr_stc_params+="--volume /dev/null:/etc/sysctl.d/80-vpp.conf "
689 # Mount docker.sock to be able to use docker deamon of the host.
690 dcr_stc_params+="--volume /var/run/docker.sock:/var/run/docker.sock "
691 # Mount /opt/boot/ where VM kernel and initrd are located.
692 dcr_stc_params+="--volume /opt/boot/:/opt/boot/ "
693 # Mount host hugepages for VMs.
694 dcr_stc_params+="--volume /dev/hugepages/:/dev/hugepages/ "
696 dcr_stc_params+="--sysctl net.ipv6.conf.all.disable_ipv6=1 "
697 dcr_stc_params+="--sysctl net.ipv6.conf.default.disable_ipv6=1 "
699 # Docker Container UUIDs.
700 declare -gA DCR_UUIDS
701 # Docker Container SSH TCP ports.
702 declare -gA DCR_PORTS
703 # Docker Container PIDs (namespaces).
704 declare -gA DCR_CPIDS
706 # Run TG and DUT1. As initial version we do support only 2-node.
707 params=(${dcr_stc_params} --name csit-tg-$(uuidgen) ${dcr_image})
708 DCR_UUIDS+=([tg]=$(docker run "${params[@]}")) || {
709 die "Failed to start TG docker container!"
711 params=(${dcr_stc_params} --name csit-dut1-$(uuidgen) ${dcr_image})
712 DCR_UUIDS+=([dut1]=$(docker run "${params[@]}")) || {
713 die "Failed to start DUT1 docker container!"
716 trap 'clean_environment_on_exit' EXIT || {
717 die "Trap attempt failed, please cleanup manually. Aborting!"
720 # Get Containers TCP ports.
721 params=(${DCR_UUIDS[tg]})
722 DCR_PORTS+=([tg]=$(docker port "${params[@]}")) || {
723 die "Failed to get port of TG docker container!"
725 params=(${DCR_UUIDS[dut1]})
726 DCR_PORTS+=([dut1]=$(docker port "${params[@]}")) || {
727 die "Failed to get port of DUT1 docker container!"
730 # Get Containers PIDs.
731 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[tg]})
732 DCR_CPIDS+=([tg]=$(docker inspect "${params[@]}")) || {
733 die "Failed to get PID of TG docker container!"
735 params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[dut1]})
736 DCR_CPIDS+=([dut1]=$(docker inspect "${params[@]}")) || {
737 die "Failed to get PID of DUT1 docker container!"
742 # Print the message to standard error.
744 # Duplicate of common.sh function, as this file is also used standalone.
747 # - ${@} - The text of the message.