github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/mitigate/mock.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mitigate
    16  
    17  import "strings"
    18  
    19  // MockCPU represents data from CPUs that will be mitigated.
    20  type MockCPU struct {
    21  	Name           string
    22  	VendorID       string
    23  	Family         int64
    24  	Model          int64
    25  	ModelName      string
    26  	Bugs           string
    27  	PhysicalCores  int64
    28  	Cores          int64
    29  	ThreadsPerCore int64
    30  }
    31  
    32  // CascadeLake2 is a two core Intel CascadeLake machine.
    33  var CascadeLake2 = MockCPU{
    34  	Name:           "CascadeLake",
    35  	VendorID:       "GenuineIntel",
    36  	Family:         6,
    37  	Model:          85,
    38  	ModelName:      "Intel(R) Xeon(R) CPU",
    39  	Bugs:           "spectre_v1 spectre_v2 spec_store_bypass mds swapgs taa",
    40  	PhysicalCores:  1,
    41  	Cores:          1,
    42  	ThreadsPerCore: 2,
    43  }
    44  
    45  // CascadeLake4 is a four core Intel CascadeLake machine.
    46  var CascadeLake4 = MockCPU{
    47  	Name:           "CascadeLake",
    48  	VendorID:       "GenuineIntel",
    49  	Family:         6,
    50  	Model:          85,
    51  	ModelName:      "Intel(R) Xeon(R) CPU",
    52  	Bugs:           "spectre_v1 spectre_v2 spec_store_bypass mds swapgs taa",
    53  	PhysicalCores:  1,
    54  	Cores:          2,
    55  	ThreadsPerCore: 2,
    56  }
    57  
    58  // Haswell2 is a two core Intel Haswell machine.
    59  var Haswell2 = MockCPU{
    60  	Name:           "Haswell",
    61  	VendorID:       "GenuineIntel",
    62  	Family:         6,
    63  	Model:          63,
    64  	ModelName:      "Intel(R) Xeon(R) CPU",
    65  	Bugs:           "cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs",
    66  	PhysicalCores:  1,
    67  	Cores:          1,
    68  	ThreadsPerCore: 2,
    69  }
    70  
    71  // Haswell2core is a 2 core Intel Haswell machine with no hyperthread pairs.
    72  var Haswell2core = MockCPU{
    73  	Name:           "Haswell2Physical",
    74  	VendorID:       "GenuineIntel",
    75  	Family:         6,
    76  	Model:          63,
    77  	ModelName:      "Intel(R) Xeon(R) CPU",
    78  	Bugs:           "cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs",
    79  	PhysicalCores:  2,
    80  	Cores:          1,
    81  	ThreadsPerCore: 1,
    82  }
    83  
    84  // AMD2 is an two core AMD machine.
    85  var AMD2 = MockCPU{
    86  	Name:           "AMD",
    87  	VendorID:       "AuthenticAMD",
    88  	Family:         23,
    89  	Model:          49,
    90  	ModelName:      "AMD EPYC 7B12",
    91  	Bugs:           "sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass",
    92  	PhysicalCores:  1,
    93  	Cores:          1,
    94  	ThreadsPerCore: 2,
    95  }
    96  
    97  // AMD8 is an eight core AMD machine.
    98  var AMD8 = MockCPU{
    99  	Name:           "AMD",
   100  	VendorID:       "AuthenticAMD",
   101  	Family:         23,
   102  	Model:          49,
   103  	ModelName:      "AMD EPYC 7B12",
   104  	Bugs:           "sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass",
   105  	PhysicalCores:  4,
   106  	Cores:          1,
   107  	ThreadsPerCore: 2,
   108  }
   109  
   110  // Empty is an empty CPU set.
   111  var Empty = MockCPU{
   112  	Name: "Empty",
   113  }
   114  
   115  // MakeCPUSet makes a cpuSet from a MockCPU.
   116  func (tc MockCPU) MakeCPUSet() CPUSet {
   117  	bugs := make(map[string]struct{})
   118  	for _, bug := range strings.Split(tc.Bugs, " ") {
   119  		bugs[bug] = struct{}{}
   120  	}
   121  	var cpus CPUSet = []*CPU{}
   122  	for i := int64(0); i < tc.PhysicalCores; i++ {
   123  		for j := int64(0); j < tc.Cores; j++ {
   124  			for k := int64(0); k < tc.ThreadsPerCore; k++ {
   125  				processorNum := (i*tc.Cores+j)*tc.ThreadsPerCore + k
   126  				cpu := &CPU{
   127  					processorNumber: processorNum,
   128  					vendorID:        tc.VendorID,
   129  					cpuFamily:       tc.Family,
   130  					model:           tc.Model,
   131  					physicalID:      i,
   132  					coreID:          j,
   133  					bugs:            bugs,
   134  				}
   135  				cpus = append(cpus, cpu)
   136  			}
   137  		}
   138  	}
   139  	return cpus
   140  }
   141  
   142  // NumCPUs returns the number of CPUs for this CPU.
   143  func (tc MockCPU) NumCPUs() int {
   144  	return int(tc.PhysicalCores * tc.Cores * tc.ThreadsPerCore)
   145  }