|
| 1 | +/* |
| 2 | +Copyright 2020 The OpenEBS Authors |
| 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 | +package iolimit |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/openebs/lib-csi/pkg/common/errors" |
| 21 | + "github.com/openebs/lib-csi/pkg/common/helpers" |
| 22 | + "io/ioutil" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + "syscall" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + baseCgroupPath = "/sys/fs/cgroup" |
| 30 | +) |
| 31 | + |
| 32 | +// SetIOLimits sets iops, bps limits for a pod with uid podUid for accessing a device named deviceName |
| 33 | +// provided that the underlying cgroup used for pod namespacing is cgroup2 (cgroup v2) |
| 34 | +func SetIOLimits(request *Request) error { |
| 35 | + if !helpers.DirExists(baseCgroupPath) { |
| 36 | + return errors.New(baseCgroupPath + " does not exist") |
| 37 | + } |
| 38 | + if err := checkCgroupV2(); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + validRequest, err := validate(request) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + err = setIOLimits(validRequest) |
| 46 | + return err |
| 47 | +} |
| 48 | + |
| 49 | +func validate(request *Request) (*ValidRequest, error) { |
| 50 | + if !helpers.IsValidUUID(request.PodUid) { |
| 51 | + return nil, errors.New("Expected PodUid in UUID format, Got " + request.PodUid) |
| 52 | + } |
| 53 | + podCGPath, err := getPodCGroupPath(request.PodUid, request.ContainerRuntime) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + ioMaxFile := podCGPath + "/io.max" |
| 58 | + if !helpers.FileExists(ioMaxFile) { |
| 59 | + return nil, errors.New("io.max file is not present in pod CGroup") |
| 60 | + } |
| 61 | + deviceNumber, err := getDeviceNumber(request.DeviceName) |
| 62 | + if err != nil { |
| 63 | + return nil, errors.New("Device Major:Minor numbers could not be obtained") |
| 64 | + } |
| 65 | + return &ValidRequest{ |
| 66 | + FilePath: ioMaxFile, |
| 67 | + DeviceNumber: deviceNumber, |
| 68 | + IOMax: request.IOLimit, |
| 69 | + }, nil |
| 70 | +} |
| 71 | + |
| 72 | +func getPodCGroupPath(podUid string, cruntime string) (string, error) { |
| 73 | + switch cruntime { |
| 74 | + case "containerd": |
| 75 | + path, err := getContainerdCGPath(podUid) |
| 76 | + if err != nil { |
| 77 | + return "", err |
| 78 | + } |
| 79 | + return path, nil |
| 80 | + default: |
| 81 | + return "", errors.New(cruntime + " runtime support is not present") |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +func checkCgroupV2() error { |
| 87 | + if !helpers.FileExists(baseCgroupPath + "/cgroup.controllers") { |
| 88 | + return errors.New("CGroupV2 not enabled") |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func getContainerdPodCGSuffix(podUid string) string { |
| 94 | + return "pod" + strings.ReplaceAll(podUid, "-", "_") |
| 95 | +} |
| 96 | + |
| 97 | +func getContainerdCGPath(podUid string) (string, error) { |
| 98 | + kubepodsCGPath := baseCgroupPath + "/kubepods.slice" |
| 99 | + podSuffix := getContainerdPodCGSuffix(podUid) |
| 100 | + podCGPath := kubepodsCGPath + "/kubepods-besteffort.slice/kubepods-besteffort-" + podSuffix + ".slice" |
| 101 | + if helpers.DirExists(podCGPath) { |
| 102 | + return podCGPath, nil |
| 103 | + } |
| 104 | + podCGPath = kubepodsCGPath + "/kubepods-burstable.slice/kubepods-burstable-" + podSuffix + ".slice" |
| 105 | + if helpers.DirExists(podCGPath) { |
| 106 | + return podCGPath, nil |
| 107 | + } |
| 108 | + return "", errors.New("CGroup Path not found for pod with Uid: " + podUid) |
| 109 | +} |
| 110 | + |
| 111 | +func getDeviceNumber(deviceName string) (*DeviceNumber, error) { |
| 112 | + stat := syscall.Stat_t{} |
| 113 | + if err := syscall.Stat(deviceName, &stat); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + return &DeviceNumber{ |
| 117 | + Major: uint64(stat.Rdev/256), |
| 118 | + Minor: uint64(stat.Rdev%256), |
| 119 | + }, nil |
| 120 | +} |
| 121 | + |
| 122 | +func getIOLimitsStr(deviceNumber *DeviceNumber, ioMax *IOMax) string { |
| 123 | + line := strconv.FormatUint(deviceNumber.Major, 10) + ":" + strconv.FormatUint(deviceNumber.Minor, 10) |
| 124 | + if ioMax.Riops != 0 { |
| 125 | + line += " riops=" + strconv.FormatUint(ioMax.Riops, 10) |
| 126 | + } |
| 127 | + if ioMax.Wiops != 0 { |
| 128 | + line += " wiops=" + strconv.FormatUint(ioMax.Wiops, 10) |
| 129 | + } |
| 130 | + if ioMax.Rbps != 0 { |
| 131 | + line += " rbps=" + strconv.FormatUint(ioMax.Rbps, 10) |
| 132 | + } |
| 133 | + if ioMax.Wbps != 0 { |
| 134 | + line += " wbps=" + strconv.FormatUint(ioMax.Wbps, 10) |
| 135 | + } |
| 136 | + return line |
| 137 | +} |
| 138 | + |
| 139 | +func setIOLimits(request *ValidRequest) error { |
| 140 | + line := getIOLimitsStr(request.DeviceNumber, request.IOMax) |
| 141 | + err := ioutil.WriteFile(request.FilePath, []byte(line), 0700) |
| 142 | + return err |
| 143 | +} |
0 commit comments