github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/network/state/util.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 state
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/kubewharf/katalyst-core/pkg/config/agent/qrm"
    23  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    24  	"github.com/kubewharf/katalyst-core/pkg/util/machine"
    25  )
    26  
    27  // GenerateMachineState returns NICResourcesMap based on
    28  // machine info and reserved resources
    29  func GenerateMachineState(conf *qrm.QRMPluginsConfiguration, nics []machine.InterfaceInfo, reservation map[string]uint32) (NICMap, error) {
    30  	defaultMachineState := make(NICMap)
    31  	for _, iface := range nics {
    32  		reservedBandwidth := reservation[iface.Iface]
    33  		egressCapacity := uint32(float32(iface.Speed) * conf.EgressCapacityRate)
    34  		ingressCapacity := uint32(float32(iface.Speed) * conf.IngressCapacityRate)
    35  
    36  		generalLog := general.LoggerWithPrefix("GenerateMachineState", general.LoggingPKGFull)
    37  		generalLog.Infof("NIC %s's speed: %d, capacity: [%d/%d], reservation: %d", iface.Iface, iface.Speed, egressCapacity, ingressCapacity, reservedBandwidth)
    38  
    39  		// we do not differentiate egress reservation and ingress reservation for now. That is, they are supposed to be the same.
    40  		if reservedBandwidth > 0 && (reservedBandwidth >= egressCapacity || reservedBandwidth >= ingressCapacity) {
    41  			return nil, fmt.Errorf("invalid bandwidth reservation: %d on NIC: %s with capacity: [%d/%d]", reservedBandwidth, iface.Iface, egressCapacity, ingressCapacity)
    42  		}
    43  
    44  		allocatableEgress := egressCapacity - reservedBandwidth
    45  		allocatableIngress := ingressCapacity - reservedBandwidth
    46  
    47  		defaultMachineState[iface.Iface] = &NICState{
    48  			EgressState: BandwidthInfo{
    49  				Capacity:       egressCapacity,
    50  				SysReservation: 0,
    51  				Reservation:    reservedBandwidth,
    52  				Allocatable:    allocatableEgress,
    53  				Allocated:      0,
    54  				Free:           allocatableEgress,
    55  			},
    56  			IngressState: BandwidthInfo{
    57  				Capacity:       ingressCapacity,
    58  				SysReservation: 0,
    59  				Reservation:    reservedBandwidth,
    60  				Allocatable:    allocatableIngress,
    61  				Allocated:      0,
    62  				Free:           allocatableIngress,
    63  			},
    64  			PodEntries: make(PodEntries),
    65  		}
    66  	}
    67  
    68  	return defaultMachineState, nil
    69  }
    70  
    71  // GenerateMachineStateFromPodEntries returns NICMap for bandwidth based on
    72  // machine info and reserved resources along with existed pod entries
    73  func GenerateMachineStateFromPodEntries(conf *qrm.QRMPluginsConfiguration, nics []machine.InterfaceInfo,
    74  	podEntries PodEntries, reservation map[string]uint32,
    75  ) (NICMap, error) {
    76  	machineState, err := GenerateMachineState(conf, nics, reservation)
    77  	if err != nil {
    78  		return nil, fmt.Errorf("GenerateMachineState failed with error: %v", err)
    79  	}
    80  
    81  	for nicName, nicState := range machineState {
    82  		var allocatedEgressOnNIC, allocatedIngressOnNIC uint32 = 0, 0
    83  
    84  		for podUID, containerEntries := range podEntries {
    85  			for containerName, allocationInfo := range containerEntries {
    86  				if containerName != "" && allocationInfo != nil && allocationInfo.IfName == nicName {
    87  					allocatedEgressOnNIC += allocationInfo.Egress
    88  					allocatedIngressOnNIC += allocationInfo.Ingress
    89  
    90  					nicState.SetAllocationInfo(podUID, containerName, allocationInfo)
    91  				}
    92  			}
    93  		}
    94  
    95  		nicState.EgressState.Allocated = allocatedEgressOnNIC
    96  		nicState.IngressState.Allocated = allocatedIngressOnNIC
    97  
    98  		generalLog := general.LoggerWithPrefix("GenerateBandwidthStateFromPodEntries", general.LoggingPKGFull)
    99  		if nicState.EgressState.Allocatable < nicState.EgressState.Allocated || nicState.IngressState.Allocatable < nicState.EgressState.Allocated {
   100  			generalLog.Warningf("invalid allocated egress bandwidth: %d on NIC: %s"+
   101  				" with allocatable bandwidth size: %d, total egress capacity size: %d, reserved bandwidth size: %d",
   102  				nicState.EgressState.Allocated, nicName, nicState.EgressState.Allocatable, nicState.EgressState.Capacity, nicState.EgressState.Reservation)
   103  			nicState.EgressState.Allocatable = nicState.EgressState.Allocated
   104  		}
   105  		nicState.EgressState.Free = nicState.EgressState.Allocatable - nicState.EgressState.Allocated
   106  
   107  		if nicState.IngressState.Allocatable < nicState.IngressState.Allocated {
   108  			generalLog.Warningf("invalid allocated ingress bandwidth: %d on NIC: %s"+
   109  				" with allocatable bandwidth size: %d, total ingress capacity size: %d, reserved bandwidth size: %d",
   110  				nicState.IngressState.Allocated, nicName, nicState.IngressState.Allocatable, nicState.IngressState.Capacity, nicState.EgressState.Reservation)
   111  			nicState.IngressState.Allocatable = nicState.IngressState.Allocated
   112  		}
   113  		nicState.IngressState.Free = nicState.IngressState.Allocatable - nicState.IngressState.Allocated
   114  
   115  		machineState[nicName] = nicState
   116  	}
   117  
   118  	return machineState, nil
   119  }