github.com/elfadel/cilium@v1.6.12/pkg/datapath/loader/hash.go (about)

     1  // Copyright 2019 Authors of Cilium
     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 loader
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/cilium/cilium/pkg/crypto/sha1"
    21  	"github.com/cilium/cilium/pkg/datapath"
    22  )
    23  
    24  var (
    25  	// DatapathSHA is set during build to the SHA generated from bindata.sh
    26  	// which is hashed across all datapath template code, excluding the
    27  	// node, netdev, lxc and sockops header files (see daemon/Makefile).
    28  	DatapathSHA string
    29  )
    30  
    31  // datapathHash represents a unique enumeration of the datapath implementation.
    32  type datapathHash struct {
    33  	sha1.ResumableHash
    34  }
    35  
    36  // newDatapathHash creates a new datapath hash based on the contents of the datapath
    37  // template files under bpf/, generated by contrib/scripts/bindata.sh.
    38  func newDatapathHash() *datapathHash {
    39  	d := sha1.New()
    40  	io.WriteString(d, DatapathSHA)
    41  	return &datapathHash{
    42  		ResumableHash: d,
    43  	}
    44  }
    45  
    46  // hashDatapath returns a new datapath hash based on the specified datapath.
    47  //
    48  // The endpoint's static data is NOT included in this hash, for that perform:
    49  //	hash := hashDatapath(dp, nodeCfg, netdevCfg, ep)
    50  //	hashStr := hash.sumEndpoint(ep)
    51  func hashDatapath(dp datapath.Datapath, nodeCfg *datapath.LocalNodeConfiguration, netdevCfg datapath.DeviceConfiguration, epCfg datapath.EndpointConfiguration) *datapathHash {
    52  	d := newDatapathHash()
    53  
    54  	// Writes won't fail; it's an in-memory hash.
    55  	if nodeCfg != nil {
    56  		_ = dp.WriteNodeConfig(d, nodeCfg)
    57  	}
    58  	if netdevCfg != nil {
    59  		_ = dp.WriteNetdevConfig(d, netdevCfg)
    60  	}
    61  	if epCfg != nil {
    62  		_ = dp.WriteTemplateConfig(d, epCfg)
    63  	}
    64  
    65  	return d
    66  }
    67  
    68  // sumEndpoint returns the hash of the complete datapath for an endpoint.
    69  // It does not change the underlying hash state.
    70  func (d *datapathHash) sumEndpoint(dp datapath.Datapath, epCfg datapath.EndpointConfiguration, staticData bool) (string, error) {
    71  	result, err := d.Copy()
    72  	if err != nil {
    73  		return "", err
    74  	}
    75  	if staticData {
    76  		dp.WriteEndpointConfig(result, epCfg)
    77  	} else {
    78  		dp.WriteTemplateConfig(result, epCfg)
    79  	}
    80  	return result.String(), nil
    81  }