feat(device): Reconfigure
[csit.git] / resources / libraries / bash / function / device.sh
1 # Copyright (c) 2024 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 set -exuo pipefail
15
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.
19
20 function activate_wrapper () {
21
22     # Acts as wrapper for activate docker topology.
23     #
24     # Variables read:
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.
28
29     set -exuo pipefail
30
31     enter_mutex || die
32     get_available_interfaces "${1}" "${2}" || die
33     bind_dut_interfaces_to_vpp_driver || die
34     start_topology_containers "${3}" || die
35     bind_interfaces_to_containers || die
36     set_env_variables || die
37     print_env_variables || die
38     exit_mutex || die
39 }
40
41
42 function bind_dut_interfaces_to_vpp_driver () {
43
44     # Bind DUT network interfaces to the driver that vpp will use
45     #
46     # Variables read:
47     # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
48     # Variables set:
49     # - NETDEV - Linux network interface.
50     # - DRIVER - Kernel driver to bind the interface to.
51     # - KRN_DRIVER - The original kernel driver of the network interface.
52
53     for NETDEV in "${DUT1_NETDEVS[@]}"; do
54         get_pci_addr || die
55         get_krn_driver || die
56         if [[ ${KRN_DRIVER} == "iavf" ]]; then
57             DRIVER="vfio-pci"
58             ADDR=${PCI_ADDR}
59             bind_interfaces_to_driver || die
60         fi
61     done
62 }
63
64
65 function bind_interfaces_to_containers () {
66
67     # Bind linux network interface to container and create symlink for PCI
68     # address in container.
69     #
70     # Variables read:
71     # - DCR_UUIDS - Docker Container UUIDs.
72     # - DCR_CPIDS - Docker Container PIDs (namespaces).
73     # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
74     # - PCI_ADDR - PCI address of network device.
75     # - TG_NETDEVS - List of network devices allocated to TG container.
76     # Variables set:
77     # - NETDEV - Linux network interface.
78     # - KRN_DRIVER - Kernel driver of network device.
79
80     set -exuo pipefail
81
82     for PCI_ADDR in "${TG_PCIDEVS[@]}"; do
83         get_netdev_name || die
84         link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
85             die "Reading symlink for PCI address failed!"
86         }
87         cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
88
89         docker exec "${DCR_UUIDS[tg]}" ${cmd} || {
90             die "Linking PCI address in container failed!"
91         }
92
93         sudo ip link set ${NETDEV} netns ${DCR_CPIDS[tg]} || {
94             die "Moving interface to ${DCR_CPIDS[tg]} namespace failed!"
95         }
96     done
97     for PCI_ADDR in "${DUT1_PCIDEVS[@]}"; do
98         link_target=$(readlink -f /sys/bus/pci/devices/"${PCI_ADDR}") || {
99             die "Reading symlink for PCI address failed!"
100         }
101         cmd="ln -s ${link_target} /sys/bus/pci/devices/${PCI_ADDR}"
102
103         docker exec "${DCR_UUIDS[dut1]}" ${cmd} || {
104             die "Linking PCI address in container failed!"
105         }
106
107         get_krn_driver
108         if [[ ${KRN_DRIVER} != "vfio-pci" ]]; then
109             get_netdev_name || die
110             sudo ip link set ${NETDEV} netns ${DCR_CPIDS[dut1]} || {
111                 die "Moving interface to ${DCR_CPIDS[dut1]} namespace failed!"
112             }
113         fi
114     done
115 }
116
117
118 function bind_interfaces_to_driver () {
119
120     # Bind network interface specified by parameter to driver specified by
121     # parameter.
122     #
123     # Variables read:
124     # - ADDR - PCI address of network interface.
125     # - DRIVER - Kernel driver.
126
127     set -exuo pipefail
128
129     pci_path="/sys/bus/pci/devices/${ADDR}"
130     drv_path="/sys/bus/pci/drivers/${DRIVER}"
131     if [ -d "${pci_path}/driver" ]; then
132         echo ${ADDR} | sudo tee ${pci_path}/driver/unbind > /dev/null || {
133             die "Failed to unbind interface ${ADDR}!"
134         }
135     fi
136
137     echo ${DRIVER} | sudo tee /sys/bus/pci/devices/${ADDR}/driver_override \
138         > /dev/null || {
139         die "Failed to override driver to ${DRIVER} for ${ADDR}!"
140     }
141
142     echo ${ADDR} | sudo tee ${drv_path}/bind > /dev/null || {
143         die "Failed to bind interface ${ADDR}!"
144     }
145
146     echo | sudo tee /sys/bus/pci/devices/${ADDR}/driver_override > /dev/null \
147         || die "Failed to reset driver override for ${ADDR}!"
148 }
149
150
151 function clean_environment () {
152
153     # Cleanup environment by removing topology containers and shared volumes
154     # and binding interfaces back to original driver.
155     #
156     # Variables read:
157     # - DCR_UUIDS - Docker Container UUIDs.
158     # - DUT1_PCIDEVS - List of PCI addresses of devices of DUT1 container.
159     # - TG_PCIDEVS - List of PCI addresses of devices of TG container.
160     # Variables set:
161     # - ADDR - PCI address of network interface.
162     # - DRIVER - Kernel driver.
163
164     set -exuo pipefail
165
166     # Kill docker containers.
167     docker rm --force "${DCR_UUIDS[@]}" || die "Cleanup containers failed!"
168
169     # Check if there are some leftover containers and remove all. Command will
170     # not fail in case there are no containers to remove.
171     docker rm --force $(docker ps -q --filter name=${DCR_UUIDS[dut1]}) || {
172         warn "Failed to remove hanged containers or nothing to remove!"
173     }
174
175     # Rebind interfaces back to kernel drivers.
176     i=0
177     for ADDR in ${TG_PCIDEVS[@]}; do
178         DRIVER="${TG_DRIVERS[${i}]}"
179         bind_interfaces_to_driver || die
180         ((i++))
181     done
182     i=0
183     for ADDR in ${DUT1_PCIDEVS[@]}; do
184         DRIVER="${DUT1_DRIVERS[${i}]}"
185         bind_interfaces_to_driver || die
186         ((i++))
187     done
188 }
189
190
191 function clean_environment_on_exit () {
192
193     # Cleanup environment by removing topology containers and binding
194     # interfaces back to original driver only if exit code is not 0.
195     # This function acts as workaround as 'set -eu' does not trigger ERR trap.
196
197     set -exuo pipefail
198
199     if [ $? -ne 0 ]; then
200         clean_environment || die
201     fi
202 }
203
204
205 function deactivate_wrapper () {
206
207     # Acts as wrapper for deactivate docker topology.
208     #
209     # Variables read:
210     # - ${@} - CSIT environment variables.
211
212     set -exuo pipefail
213
214     enter_mutex || die
215     read_env_variables "${@}" || die
216     clean_environment || die
217     exit_mutex || die
218 }
219
220
221 function die () {
222
223     # Print the message to standard error end exit with error code specified
224     # by the second argument.
225     #
226     # Duplicate of common.sh function, as this file is also used standalone.
227     #
228     # Hardcoded values:
229     # - The default error message.
230     # Arguments:
231     # - ${1} - The whole error message, be sure to quote. Optional
232     # - ${2} - the code to exit with, default: 1.
233
234     set -x
235     set +eu
236     warn "${1:-Unspecified run-time error occurred!}"
237     exit "${2:-1}"
238 }
239
240
241 function enter_mutex () {
242
243     # Enter mutual exclusion for protecting execution from starvation and
244     # deadlock.
245
246     set -exuo pipefail
247
248     mutex_timeout=3600
249     mutex_file="/tmp/mutex_file"
250
251     # Create mutex.
252     exec {lock_fd}>${mutex_file} || {
253         die "Mutex enter failed!"
254     }
255     flock --timeout "${mutex_timeout}" "${lock_fd}" || {
256         die "Calling flock() failed!"
257     }
258     # ----------------------
259     # Enter mutex succeeded.
260     warn "Mutex enter succeeded for PID $$."
261 }
262
263
264 function exit_mutex () {
265
266     # Exit mutual exclusion.
267
268     set -exuo pipefail
269
270     # ---------------------
271     # Remove mutex so we are not blocking others anymore.
272     flock -u "${lock_fd}" || {
273         die "Mutex destroy failed!"
274     }
275     warn "Mutex leave succeeded for PID $$."
276 }
277
278
279 function get_available_interfaces () {
280
281     # Find and get available Virtual functions.
282     #
283     # Arguments:
284     # - ${1} - Nodeness, as set by common.sh get_test_code.
285     # - ${2} - Flavor, as set by common.sh get_test_code.
286     # Variables set:
287     # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
288     # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
289     # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
290     # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
291     # - DUT1_VLANS - List of interface vlans to TG container.
292     # - DUT1_MODEL - List of interface models to TG container.
293     # - TG_NETDEVS - List of network devices allocated to TG container.
294     # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
295     # - TG_NETMACS - List of MAC addresses allocated to TG container.
296     # - TG_DRIVERS - List of interface drivers to TG container.
297     # - TG_VLANS - List of interface vlans to TG container.
298     # - TG_MODEL - List of interface models to TG container.
299
300     set -exuo pipefail
301
302     # Following code is specifying VFs ID based on nodeness and flavor.
303     # As there is great variability in hardware configuration outside LF,
304     # from bootstrap architecture point of view these are considered as flavors.
305     # Anyone can override flavor for its own machine and add condition here.
306     # See http://pci-ids.ucw.cz/v2.2/pci.ids for more info.
307     case_text="${1}_${2}"
308     case "${case_text}" in
309         "1n_skx")
310             # Add Intel Corporation XL710/X710 Virtual Function to the
311             # whitelist.
312             # Add Intel Corporation E810 Virtual Function to the
313             # whitelist.
314             pci_id="0x154c\|0x1889"
315             tg_netdev=(ens1 enp134)
316             dut1_netdev=(ens5 enp175)
317             ports_per_nic=2
318             ;;
319        "1n_alt")
320             # Add Intel Corporation XL710/X710 Virtual Function to the
321             # whitelist.
322             # Add MT2892 Family [ConnectX-6 Dx] Virtual Function to the
323             # whitelist.
324             pci_id="0x154c\|0x101e"
325             tg_netdev=(enp1s0f0 enp1s0f1 enP1p1s0f0)
326             dut1_netdev=(enP3p2s0f0 enP3p2s0f1 enP1p1s0f1)
327             ports_per_nic=2
328             ;;
329         "1n_spr")
330             # Add Intel Corporation XL710/X710 Virtual Function to the
331             # whitelist.
332             # Add Intel Corporation E810 Virtual Function to the
333             # whitelist.
334             pci_id="0x154c\|0x1889"
335             tg_netdev=(enp42s0 ens5)
336             dut1_netdev=(enp61s0 ens7)
337             ports_per_nic=2
338             ;;
339        "1n_vbox")
340             # Add Intel Corporation 82545EM Gigabit Ethernet Controller to the
341             # whitelist.
342             pci_id="0x100f"
343             tg_netdev=(enp0s8 enp0s9)
344             dut1_netdev=(enp0s16 enp0s17)
345             ports_per_nic=1
346             ;;
347         *)
348             die "Unknown specification: ${case_text}!"
349     esac
350
351     # TG side of connections.
352     TG_NETDEVS=()
353     TG_PCIDEVS=()
354     TG_NETMACS=()
355     TG_DRIVERS=()
356     TG_VLANS=()
357     TG_MODEL=()
358     # DUT1 side of connections.
359     DUT1_NETDEVS=()
360     DUT1_PCIDEVS=()
361     DUT1_NETMACS=()
362     DUT1_DRIVERS=()
363     DUT1_VLANS=()
364     DUT1_MODEL=()
365
366     # Find the first ${device_count} number of available TG Linux network
367     # VF device names. Only allowed VF PCI IDs are filtered.
368     for netdev in ${tg_netdev[@]}
369     do
370         ports=0
371         for netdev_path in $(grep -l "${pci_id}" \
372                              /sys/class/net/${netdev}*/device/device \
373                              2> /dev/null)
374         do
375             if [[ ${ports} -lt ${ports_per_nic} ]]; then
376                 tg_netdev_name=$(dirname ${netdev_path})
377                 tg_netdev_name=$(dirname ${tg_netdev_name})
378                 TG_NETDEVS+=($(basename ${tg_netdev_name}))
379                 ((ports++))
380             else
381                 break
382             fi
383         done
384         ports_per_device=$((${ports_per_nic}*${#tg_netdev[@]}))
385         if [[ ${#TG_NETDEVS[@]} -eq ${ports_per_device} ]]; then
386             break
387         fi
388     done
389
390     i=0
391     for netdev in "${TG_NETDEVS[@]}"; do
392         # Find the index of selected tg netdev among tg_netdevs
393         # e.g. enp8s5f7 is a vf of netdev enp8s5 with index 11
394         # and the corresponding dut1 netdev is enp133s13.
395         while [[ "${netdev}" != "${tg_netdev[$i]}"* ]]; do
396             ((i++))
397         done
398         # Rename tg netdev to dut1 netdev
399         # e.g. enp8s5f7 -> enp133s13f7
400         DUT1_NETDEVS+=(${netdev/${tg_netdev[$i]}/${dut1_netdev[$i]}})
401         # Don't need to reset i, all netdevs are sorted.
402     done
403
404     for NETDEV in "${TG_NETDEVS[@]}"; do
405         get_pci_addr
406         get_mac_addr
407         get_krn_driver
408         get_vlan_filter
409         get_csit_model
410         TG_PCIDEVS+=(${PCI_ADDR})
411         TG_NETMACS+=(${MAC_ADDR})
412         TG_DRIVERS+=(${KRN_DRIVER})
413         TG_VLANS+=(${VLAN_ID})
414         TG_MODELS+=(${MODEL})
415     done
416     for NETDEV in "${DUT1_NETDEVS[@]}"; do
417         get_pci_addr
418         get_mac_addr
419         get_krn_driver
420         get_vlan_filter
421         get_csit_model
422         DUT1_PCIDEVS+=(${PCI_ADDR})
423         DUT1_NETMACS+=(${MAC_ADDR})
424         DUT1_DRIVERS+=(${KRN_DRIVER})
425         DUT1_VLANS+=(${VLAN_ID})
426         DUT1_MODELS+=(${MODEL})
427     done
428
429     # We need at least two interfaces for TG/DUT1 for building topology.
430     if [ "${#TG_NETDEVS[@]}" -lt 2 ] || [ "${#DUT1_NETDEVS[@]}" -lt 2 ]; then
431         die "Not enough linux network interfaces found!"
432     fi
433 }
434
435
436 function get_krn_driver () {
437
438     # Get kernel driver from linux network device name.
439     #
440     # Variables read:
441     # - PCI_ADDR - PCI address of network device.
442     # Variables set:
443     # - KRN_DRIVER - Kernel driver of network device.
444
445     set -exuo pipefail
446
447     pci_path="/sys/bus/pci/devices/${PCI_ADDR}"
448     KRN_DRIVER="$(basename $(readlink -f ${pci_path}/driver))" || {
449         die "Failed to get kernel driver of PCI interface!"
450     }
451 }
452
453
454 function get_mac_addr () {
455
456     # Get MAC address from linux network device name.
457     #
458     # Variables read:
459     # - NETDEV - Linux network device name.
460     # Variables set:
461     # - MAC_ADDR - MAC address of network device.
462
463     set -exuo pipefail
464
465     if [ -d /sys/class/net/${NETDEV}/device ]; then
466         MAC_ADDR="$(</sys/class/net/${NETDEV}/address)" || {
467             die "Failed to get MAC address of linux network interface!"
468         }
469     fi
470 }
471
472
473 function get_netdev_name () {
474
475     # Get Linux network device name.
476     #
477     # Variables read:
478     # - PCI_ADDR - PCI address of the device.
479     # Variables set:
480     # - NETDEV - Linux network device name.
481
482     set -exuo pipefail
483
484     if [ -d /sys/bus/pci/devices/${PCI_ADDR}/net ]; then
485         NETDEV="$(basename /sys/bus/pci/devices/${PCI_ADDR}/net/*)" || {
486             die "Failed to get Linux interface name of ${PCI_ADDR}"
487         }
488     fi
489 }
490
491
492 function get_csit_model () {
493
494     # Get CSIT model name from linux network device name.
495     #
496     # Variables read:
497     # - NETDEV - Linux network device name.
498     # Variables set:
499     # - MODEL - CSIT model name of network device.
500
501     set -exuo pipefail
502
503     if [ -d /sys/class/net/${NETDEV}/device ]; then
504         ID="$(</sys/class/net/${NETDEV}/device/device)" || {
505             die "Failed to get device id of linux network interface!"
506         }
507         case "${ID}" in
508             "0x1592"|"0x1889")
509                 MODEL="Intel-E810CQ"
510                 ;;
511             "0x1572"|"0x154c")
512                 MODEL="Intel-X710"
513                 ;;
514             "0x101e")
515                 MODEL="Mellanox-CX6DX"
516                 ;;
517             *)
518                 MODEL="virtual"
519         esac
520     fi
521 }
522
523
524 function get_pci_addr () {
525
526     # Get PCI address in <domain>:<bus:<device>.<func> format from linux network
527     # device name.
528     #
529     # Variables read:
530     # - NETDEV - Linux network device name.
531     # Variables set:
532     # - PCI_ADDR - PCI address of network device.
533
534     set -exuo pipefail
535
536     if [ -d /sys/class/net/${NETDEV}/device ]; then
537         PCI_ADDR=$(basename $(readlink /sys/class/net/${NETDEV}/device)) || {
538             die "Failed to get PCI address of linux network interface!"
539         }
540         if [ ! -d /sys/bus/pci/devices/${PCI_ADDR} ]; then
541             die "PCI device ${PCI_ADDR} doesn't exist!"
542         fi
543     else
544         die "Can't get device info of interface ${NETDEV}!"
545     fi
546 }
547
548
549 function get_vfio_group () {
550
551     # Get the VFIO group of a pci device.
552     #
553     # Variables read:
554     # - PCI_ADDR - PCI address of a device.
555     # Variables set:
556     # - VFIO_GROUP - The VFIO group of the PCI device.
557
558     if [[ -d /sys/bus/pci/devices/${PCI_ADDR}/iommu_group ]]; then
559         VFIO_GROUP="$(basename\
560             $(readlink /sys/bus/pci/devices/${PCI_ADDR}/iommu_group)\
561         )" || {
562             die "PCI device ${PCI_ADDR} does not have an iommu group!"
563         }
564     fi
565 }
566
567 function get_vlan_filter () {
568
569     # Get VLAN stripping filter from PF searched by mac adress.
570     #
571     # Variables read:
572     # - MAC_ADDR - MAC address of VF.
573     # Variables set:
574     # - VLAN_ID - VLAN ids.
575
576     set -exuo pipefail
577
578     # Sed regular expression pattern.
579     exp="s/^.*vlan ([[:digit:]]+).*$/\1/"
580     VLAN_ID=$(ip link | grep vlan | grep ${MAC_ADDR} | sed -re "${exp}") || true
581     VLAN_ID="${VLAN_ID:-0}"
582 }
583
584
585 function installed () {
586
587     # Check if the given utility is installed. Fail if not installed.
588     #
589     # Duplicate of common.sh function, as this file is also used standalone.
590     #
591     # Arguments:
592     # - ${1} - Utility to check.
593     # Returns:
594     # - 0 - If command is installed.
595     # - 1 - If command is not installed.
596
597     set -exuo pipefail
598
599     command -v "${1}"
600 }
601
602
603 function parse_env_variables () {
604
605     # Parse environment variables.
606     #
607     # Variables read, set or exported: Multiple,
608     # see the code for the current list.
609
610     set -exuo pipefail
611
612     IFS=@ read -a TG_NETMACS <<< "${CSIT_TG_INTERFACES_PORT_MAC}"
613     IFS=@ read -a TG_PCIDEVS <<< "${CSIT_TG_INTERFACES_PORT_PCI}"
614     IFS=@ read -a TG_DRIVERS <<< "${CSIT_TG_INTERFACES_PORT_DRV}"
615     IFS=@ read -a TG_VLANS <<< "${CSIT_TG_INTERFACES_PORT_VLAN}"
616     IFS=@ read -a TG_MODELS <<< "${CSIT_TG_INTERFACES_PORT_MODEL}"
617     IFS=@ read -a DUT1_NETMACS <<< "${CSIT_DUT1_INTERFACES_PORT_MAC}"
618     IFS=@ read -a DUT1_PCIDEVS <<< "${CSIT_DUT1_INTERFACES_PORT_PCI}"
619     IFS=@ read -a DUT1_DRIVERS <<< "${CSIT_DUT1_INTERFACES_PORT_DRV}"
620     IFS=@ read -a DUT1_VLANS <<< "${CSIT_DUT1_INTERFACES_PORT_VLAN}"
621     IFS=@ read -a DUT1_MODELS <<< "${CSIT_DUT1_INTERFACES_PORT_MODEL}"
622
623     for port in $(seq "${#TG_NETMACS[*]}"); do
624         CSIT_TG_INTERFACES+=$(cat << EOF
625         port$((port-1)):
626             mac_address: "${TG_NETMACS[$((port-1))]}"
627             pci_address: "${TG_PCIDEVS[$((port-1))]}"
628             link: "link$((port-1))"
629             model: ${TG_MODELS[$((port-1))]}
630             driver: "${TG_DRIVERS[$((port-1))]}"
631             vlan: ${TG_VLANS[$((port-1))]}
632 EOF
633     )
634         CSIT_TG_INTERFACES+=$'\n'
635     done
636     for port in $(seq "${#DUT1_NETMACS[*]}"); do
637         CSIT_DUT1_INTERFACES+=$(cat << EOF
638         port$((port-1)):
639             mac_address: "${DUT1_NETMACS[$((port-1))]}"
640             pci_address: "${DUT1_PCIDEVS[$((port-1))]}"
641             link: "link$((port-1))"
642             model: ${DUT1_MODELS[$((port-1))]}
643             driver: "${DUT1_DRIVERS[$((port-1))]}"
644             vlan: ${DUT1_VLANS[$((port-1))]}
645 EOF
646     )
647         CSIT_DUT1_INTERFACES+=$'\n'
648     done
649 }
650
651
652 function print_env_variables () {
653
654     # Get environment variables prefixed by CSIT_.
655
656     set -exuo pipefail
657
658     env | grep CSIT_ || true
659 }
660
661
662 function read_env_variables () {
663
664     # Read environment variables from parameters.
665     #
666     # Arguments:
667     # - ${@} - Variables passed as an argument.
668     # Variables read, set or exported: Multiple,
669     # see the code for the current list.
670
671     set -exuo pipefail
672
673     for param in "$@"; do
674         export "${param}"
675     done
676     declare -gA DCR_UUIDS
677     DCR_UUIDS+=([tg]="${CSIT_TG_UUID}")
678     DCR_UUIDS+=([dut1]="${CSIT_DUT1_UUID}")
679
680     IFS=@ read -a TG_NETMACS <<< "${CSIT_TG_INTERFACES_PORT_MAC}"
681     IFS=@ read -a TG_PCIDEVS <<< "${CSIT_TG_INTERFACES_PORT_PCI}"
682     IFS=@ read -a TG_DRIVERS <<< "${CSIT_TG_INTERFACES_PORT_DRV}"
683     IFS=@ read -a TG_VLANS <<< "${CSIT_TG_INTERFACES_PORT_VLAN}"
684     IFS=@ read -a TG_MODELS <<< "${CSIT_TG_INTERFACES_PORT_MODEL}"
685     IFS=@ read -a DUT1_NETMACS <<< "${CSIT_DUT1_INTERFACES_PORT_MAC}"
686     IFS=@ read -a DUT1_PCIDEVS <<< "${CSIT_DUT1_INTERFACES_PORT_PCI}"
687     IFS=@ read -a DUT1_DRIVERS <<< "${CSIT_DUT1_INTERFACES_PORT_DRV}"
688     IFS=@ read -a DUT1_VLANS <<< "${CSIT_DUT1_INTERFACES_PORT_VLAN}"
689     IFS=@ read -a DUT1_MODELS <<< "${CSIT_DUT1_INTERFACES_PORT_MODEL}"
690 }
691
692
693 function set_env_variables () {
694
695     # Set environment variables.
696     #
697     # Variables read:
698     # - DCR_UUIDS - Docker Container UUIDs.
699     # - DCR_PORTS - Docker Container's SSH ports.
700     # - DUT1_NETDEVS - List of network devices allocated to DUT1 container.
701     # - DUT1_PCIDEVS - List of PCI addresses allocated to DUT1 container.
702     # - DUT1_NETMACS - List of MAC addresses allocated to DUT1 container.
703     # - DUT1_DRIVERS - List of interface drivers to DUT1 container.
704     # - DUT1_VLANS - List of interface vlans to TG container.
705     # - DUT1_MODEL - List of interface models to TG container.
706     # - TG_NETDEVS - List of network devices allocated to TG container.
707     # - TG_PCIDEVS - List of PCI addresses allocated to TG container.
708     # - TG_NETMACS - List of MAC addresses allocated to TG container.
709     # - TG_DRIVERS - List of interface drivers to TG container.
710     # - TG_VLANS - List of interface vlans to TG container.
711     # - TG_MODEL - List of interface models to TG container.
712
713     set -exuo pipefail
714
715     set -a
716     CSIT_TG_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
717         die "Reading hostname IP address failed!"
718     }
719     CSIT_TG_PORT="${DCR_PORTS[tg]##*:}"
720     CSIT_TG_UUID="${DCR_UUIDS[tg]}"
721     CSIT_TG_ARCH="$(uname -i)" || {
722         die "Reading machine architecture failed!"
723     }
724     CSIT_DUT1_HOST="$(hostname --all-ip-addresses | awk '{print $1}')" || {
725         die "Reading hostname IP address failed!"
726     }
727     CSIT_DUT1_PORT="${DCR_PORTS[dut1]##*:}"
728     CSIT_DUT1_UUID="${DCR_UUIDS[dut1]}"
729     CSIT_DUT1_ARCH="$(uname -i)" || {
730         die "Reading machine architecture failed!"
731     }
732     OIFS="$IFS" IFS=@
733     set -a
734     CSIT_TG_INTERFACES_PORT_MAC="${TG_NETMACS[*]}"
735     CSIT_TG_INTERFACES_PORT_PCI="${TG_PCIDEVS[*]}"
736     CSIT_TG_INTERFACES_PORT_DRV="${TG_DRIVERS[*]}"
737     CSIT_TG_INTERFACES_PORT_VLAN="${TG_VLANS[*]}"
738     CSIT_TG_INTERFACES_PORT_MODEL="${TG_MODELS[*]}"
739     CSIT_DUT1_INTERFACES_PORT_MAC="${DUT1_NETMACS[*]}"
740     CSIT_DUT1_INTERFACES_PORT_PCI="${DUT1_PCIDEVS[*]}"
741     CSIT_DUT1_INTERFACES_PORT_DRV="${DUT1_DRIVERS[*]}"
742     CSIT_DUT1_INTERFACES_PORT_VLAN="${DUT1_VLANS[*]}"
743     CSIT_DUT1_INTERFACES_PORT_MODEL="${DUT1_MODELS[*]}"
744     set +a
745     IFS="$OIFS"
746 }
747
748
749 function start_topology_containers () {
750
751     # Starts csit-sut-dcr docker containers for TG/DUT1.
752     #
753     # Variables read:
754     # - CSIT_DIR - Path to existing root of local CSIT git repository.
755     # Variables set:
756     # - DCR_UUIDS - Docker Container UUIDs.
757     # - DCR_PORTS - Docker Container SSH TCP ports.
758     # - DCR_CPIDS - Docker Container PIDs (namespaces).
759
760     set -exuo pipefail
761
762     if ! installed docker; then
763         die "Docker not present. Please install before continue!"
764     fi
765
766     # If the IMAGE is not already loaded then docker run will pull the IMAGE,
767     # and all image dependencies, before it starts the container.
768     dcr_image="${1}"
769     # Run the container in the background and print the new container ID.
770     dcr_stc_params="--detach=true "
771     # Give extended privileges to this container. A "privileged" container is
772     # given access to all devices and able to run nested containers.
773     dcr_stc_params+="--privileged "
774     # Publish all exposed ports to random ports on the host interfaces.
775     dcr_stc_params+="--publish-all "
776     # Automatically remove the container when it exits.
777     dcr_stc_params+="--rm "
778     # Size of /dev/shm.
779     dcr_stc_params+="--shm-size 2G "
780     # Override access to PCI bus by attaching a filesystem mount to the
781     # container.
782     dcr_stc_params+="--mount type=tmpfs,destination=/sys/bus/pci/devices "
783     # Mount vfio devices to be able to use VFs inside the container.
784     vfio_bound="false"
785     for PCI_ADDR in ${DUT1_PCIDEVS[@]}; do
786         get_krn_driver
787         if [[ ${KRN_DRIVER} == "vfio-pci" ]]; then
788             get_vfio_group
789             dcr_stc_params+="--device /dev/vfio/${VFIO_GROUP} "
790             vfio_bound="true"
791         fi
792     done
793     if ! ${vfio_bound}; then
794         dcr_stc_params+="--volume /dev/vfio:/dev/vfio "
795     fi
796     # Disable manipulation with hugepages by VPP.
797     dcr_stc_params+="--volume /dev/null:/etc/sysctl.d/80-vpp.conf "
798     # Mount docker.sock to be able to use docker deamon of the host.
799     dcr_stc_params+="--volume /var/run/docker.sock:/var/run/docker.sock "
800     # Mount /opt/boot/ where VM kernel and initrd are located.
801     dcr_stc_params+="--volume /opt/boot/:/opt/boot/ "
802     # Mount host hugepages for VMs.
803     dcr_stc_params+="--volume /dev/hugepages/:/dev/hugepages/ "
804     # Disable IPv6.
805     dcr_stc_params+="--sysctl net.ipv6.conf.all.disable_ipv6=1 "
806     dcr_stc_params+="--sysctl net.ipv6.conf.default.disable_ipv6=1 "
807
808     # Docker Container UUIDs.
809     declare -gA DCR_UUIDS
810     # Docker Container SSH TCP ports.
811     declare -gA DCR_PORTS
812     # Docker Container PIDs (namespaces).
813     declare -gA DCR_CPIDS
814
815     # Run TG and DUT1. As initial version we do support only 2-node.
816     params=(${dcr_stc_params} --name csit-tg-$(uuidgen) ${dcr_image})
817     DCR_UUIDS+=([tg]=$(docker run "${params[@]}")) || {
818         die "Failed to start TG docker container!"
819     }
820     params=(${dcr_stc_params} --name csit-dut1-$(uuidgen) ${dcr_image})
821     DCR_UUIDS+=([dut1]=$(docker run "${params[@]}")) || {
822         die "Failed to start DUT1 docker container!"
823     }
824
825     trap 'clean_environment_on_exit' EXIT || {
826         die "Trap attempt failed, please cleanup manually. Aborting!"
827     }
828
829     # Get Containers TCP ports.
830     params=(${DCR_UUIDS[tg]})
831     DCR_PORTS+=([tg]=$(docker port "${params[@]}")) || {
832         die "Failed to get port of TG docker container!"
833     }
834     params=(${DCR_UUIDS[dut1]})
835     DCR_PORTS+=([dut1]=$(docker port "${params[@]}")) || {
836         die "Failed to get port of DUT1 docker container!"
837     }
838
839     # Get Containers PIDs.
840     params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[tg]})
841     DCR_CPIDS+=([tg]=$(docker inspect "${params[@]}")) || {
842         die "Failed to get PID of TG docker container!"
843     }
844     params=(--format="{{ .State.Pid }}" ${DCR_UUIDS[dut1]})
845     DCR_CPIDS+=([dut1]=$(docker inspect "${params[@]}")) || {
846         die "Failed to get PID of DUT1 docker container!"
847     }
848 }
849
850 function warn () {
851     # Print the message to standard error.
852     #
853     # Duplicate of common.sh function, as this file is also used standalone.
854     #
855     # Arguments:
856     # - ${@} - The text of the message.
857
858     set -exuo pipefail
859
860     echo "$@" >&2
861 }