github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/pkg/sysinfo/sysinfo.go (about)

     1  package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
     2  
     3  import "github.com/docker/docker/pkg/parsers"
     4  
     5  // Opt for New().
     6  type Opt func(info *SysInfo)
     7  
     8  // SysInfo stores information about which features a kernel supports.
     9  // TODO Windows: Factor out platform specific capabilities.
    10  type SysInfo struct {
    11  	// Whether the kernel supports AppArmor or not
    12  	AppArmor bool
    13  	// Whether the kernel supports Seccomp or not
    14  	Seccomp bool
    15  
    16  	cgroupMemInfo
    17  	cgroupCPUInfo
    18  	cgroupBlkioInfo
    19  	cgroupCpusetInfo
    20  	cgroupPids
    21  
    22  	// Whether the kernel supports cgroup namespaces or not
    23  	CgroupNamespaces bool
    24  
    25  	// Whether IPv4 forwarding is supported or not, if this was disabled, networking will not work
    26  	IPv4ForwardingDisabled bool
    27  
    28  	// Whether bridge-nf-call-iptables is supported or not
    29  	BridgeNFCallIPTablesDisabled bool
    30  
    31  	// Whether bridge-nf-call-ip6tables is supported or not
    32  	BridgeNFCallIP6TablesDisabled bool
    33  
    34  	// Whether the cgroup has the mountpoint of "devices" or not
    35  	CgroupDevicesEnabled bool
    36  
    37  	// Whether the cgroup is in unified mode (v2).
    38  	CgroupUnified bool
    39  
    40  	// Warnings contains a slice of warnings that occurred  while collecting
    41  	// system information. These warnings are intended to be informational
    42  	// messages for the user, and can either be logged or returned to the
    43  	// client; they are not intended to be parsed / used for other purposes,
    44  	// and do not have a fixed format.
    45  	Warnings []string
    46  
    47  	// cgMounts is the list of cgroup v1 mount paths, indexed by subsystem, to
    48  	// inspect availability of subsystems.
    49  	cgMounts map[string]string
    50  
    51  	// cg2GroupPath is the cgroup v2 group path to inspect availability of the controllers.
    52  	cg2GroupPath string
    53  
    54  	// cg2Controllers is an index of available cgroup v2 controllers.
    55  	cg2Controllers map[string]struct{}
    56  }
    57  
    58  type cgroupMemInfo struct {
    59  	// Whether memory limit is supported or not
    60  	MemoryLimit bool
    61  
    62  	// Whether swap limit is supported or not
    63  	SwapLimit bool
    64  
    65  	// Whether soft limit is supported or not
    66  	MemoryReservation bool
    67  
    68  	// Whether OOM killer disable is supported or not
    69  	OomKillDisable bool
    70  
    71  	// Whether memory swappiness is supported or not
    72  	MemorySwappiness bool
    73  
    74  	// Whether kernel memory limit is supported or not
    75  	KernelMemory bool
    76  
    77  	// Whether kernel memory TCP limit is supported or not
    78  	KernelMemoryTCP bool
    79  }
    80  
    81  type cgroupCPUInfo struct {
    82  	// Whether CPU shares is supported or not
    83  	CPUShares bool
    84  
    85  	// Whether CPU CFS (Completely Fair Scheduler) is supported
    86  	CPUCfs bool
    87  
    88  	// Whether CPU real-time scheduler is supported
    89  	CPURealtime bool
    90  }
    91  
    92  type cgroupBlkioInfo struct {
    93  	// Whether Block IO weight is supported or not
    94  	BlkioWeight bool
    95  
    96  	// Whether Block IO weight_device is supported or not
    97  	BlkioWeightDevice bool
    98  
    99  	// Whether Block IO read limit in bytes per second is supported or not
   100  	BlkioReadBpsDevice bool
   101  
   102  	// Whether Block IO write limit in bytes per second is supported or not
   103  	BlkioWriteBpsDevice bool
   104  
   105  	// Whether Block IO read limit in IO per second is supported or not
   106  	BlkioReadIOpsDevice bool
   107  
   108  	// Whether Block IO write limit in IO per second is supported or not
   109  	BlkioWriteIOpsDevice bool
   110  }
   111  
   112  type cgroupCpusetInfo struct {
   113  	// Whether Cpuset is supported or not
   114  	Cpuset bool
   115  
   116  	// Available Cpuset's cpus
   117  	Cpus string
   118  
   119  	// Available Cpuset's memory nodes
   120  	Mems string
   121  }
   122  
   123  type cgroupPids struct {
   124  	// Whether Pids Limit is supported or not
   125  	PidsLimit bool
   126  }
   127  
   128  // IsCpusetCpusAvailable returns `true` if the provided string set is contained
   129  // in cgroup's cpuset.cpus set, `false` otherwise.
   130  // If error is not nil a parsing error occurred.
   131  func (c cgroupCpusetInfo) IsCpusetCpusAvailable(provided string) (bool, error) {
   132  	return isCpusetListAvailable(provided, c.Cpus)
   133  }
   134  
   135  // IsCpusetMemsAvailable returns `true` if the provided string set is contained
   136  // in cgroup's cpuset.mems set, `false` otherwise.
   137  // If error is not nil a parsing error occurred.
   138  func (c cgroupCpusetInfo) IsCpusetMemsAvailable(provided string) (bool, error) {
   139  	return isCpusetListAvailable(provided, c.Mems)
   140  }
   141  
   142  func isCpusetListAvailable(provided, available string) (bool, error) {
   143  	parsedAvailable, err := parsers.ParseUintList(available)
   144  	if err != nil {
   145  		return false, err
   146  	}
   147  	// 8192 is the normal maximum number of CPUs in Linux, so accept numbers up to this
   148  	// or more if we actually have more CPUs.
   149  	max := 8192
   150  	for m := range parsedAvailable {
   151  		if m > max {
   152  			max = m
   153  		}
   154  	}
   155  	parsedProvided, err := parsers.ParseUintListMaximum(provided, max)
   156  	if err != nil {
   157  		return false, err
   158  	}
   159  	for k := range parsedProvided {
   160  		if !parsedAvailable[k] {
   161  			return false, nil
   162  		}
   163  	}
   164  	return true, nil
   165  }