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