Initial commit
[ta/infra-ansible.git] / systemd / sriov.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 start()
18 {
19     local iface vf_count
20     local res=0
21
22     for iface in $SRIOV_INTERFACES
23     do
24         vf_count=$SRIOV_VF_COUNT
25         if [[ -z "$vf_count" ]]
26         then
27             if ! vf_count=$(</sys/class/net/$iface/device/sriov_totalvfs)
28             then
29                 echo "Failed to get supported VF count for the interface $iface" >&2
30                 res=1
31                 continue
32             fi
33         fi
34
35         echo "Creating $vf_count SR-IOV VFs for the interface $iface"
36         if ! echo $vf_count > /sys/class/net/$iface/device/sriov_numvfs
37         then
38             echo "Failed to create SR-IOV VFs for the interface $iface" >&2
39             res=1
40         fi
41     done
42
43     return $res
44 }
45
46 stop()
47 {
48     local iface
49     local res=0
50
51     for iface in $SRIOV_INTERFACES
52     do
53         echo "Removing SR-IOV VFs from the interface $iface"
54         if ! echo 0 > /sys/class/net/$iface/device/sriov_numvfs
55         then
56             echo "Failed to remove SR-IOV VFs from the interface $iface" >&2
57             res=1
58         fi
59     done
60
61     return $res
62 }
63
64 main()
65 {
66     local -r CONF_FILE="/etc/sriov/sriov.conf"
67     local -r ACTION=$1
68
69     if [[ $# -ne 1 || ! $ACTION =~ ^(start|stop)$ ]]
70     then
71         echo "usage: $0 [start|stop]" >&2
72         return 1
73     fi
74
75     [[ -r $CONF_FILE ]] && source $CONF_FILE
76
77     if [[ -n "$SRIOV_INTERFACES" ]]
78     then
79         $ACTION
80     else
81         echo "No SR-IOV interfaces to configure" >&2
82         return 1
83     fi
84 }
85
86 main "$@"