github.com/cilium/cilium@v1.16.2/pkg/datapath/alignchecker/alignchecker.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package alignchecker
     5  
     6  import (
     7  	check "github.com/cilium/cilium/pkg/alignchecker"
     8  	"github.com/cilium/cilium/pkg/bpf"
     9  	"github.com/cilium/cilium/pkg/maps/authmap"
    10  	"github.com/cilium/cilium/pkg/maps/bwmap"
    11  	"github.com/cilium/cilium/pkg/maps/ctmap"
    12  	"github.com/cilium/cilium/pkg/maps/egressmap"
    13  	"github.com/cilium/cilium/pkg/maps/eventsmap"
    14  	"github.com/cilium/cilium/pkg/maps/fragmap"
    15  	ipcachemap "github.com/cilium/cilium/pkg/maps/ipcache"
    16  	"github.com/cilium/cilium/pkg/maps/lbmap"
    17  	"github.com/cilium/cilium/pkg/maps/lxcmap"
    18  	"github.com/cilium/cilium/pkg/maps/metricsmap"
    19  	"github.com/cilium/cilium/pkg/maps/neighborsmap"
    20  	"github.com/cilium/cilium/pkg/maps/policymap"
    21  	"github.com/cilium/cilium/pkg/maps/recorder"
    22  	"github.com/cilium/cilium/pkg/maps/signalmap"
    23  	"github.com/cilium/cilium/pkg/maps/srv6map"
    24  	"github.com/cilium/cilium/pkg/maps/tunnel"
    25  	"github.com/cilium/cilium/pkg/maps/vtep"
    26  )
    27  
    28  var (
    29  	toCheck      = map[string][]any{}
    30  	toCheckSizes = map[string][]any{}
    31  )
    32  
    33  func registerToCheck(c map[string][]any) {
    34  	for typ, str := range c {
    35  		toCheck[typ] = append(toCheck[typ], str...)
    36  	}
    37  }
    38  
    39  func registerToCheckSizes(c map[string][]any) {
    40  	for size, str := range c {
    41  		toCheckSizes[size] = append(toCheckSizes[size], str...)
    42  	}
    43  }
    44  
    45  // CheckStructAlignments checks whether size and offsets of the C and Go
    46  // structs for the datapath match.
    47  //
    48  // C struct layout is extracted from the given ELF object file's BTF info.
    49  //
    50  // To find a matching C struct field, a Go field has to be tagged with
    51  // `align:"field_name_in_c_struct". In the case of unnamed union field, such
    52  // union fields can be referred with special tags - `align:"$union0"`,
    53  // `align:"$union1"`, etc.
    54  func CheckStructAlignments(path string) error {
    55  	// Validate alignments of C and Go equivalent structs
    56  	if err := check.CheckStructAlignments(path, toCheck, true); err != nil {
    57  		return err
    58  	}
    59  	return check.CheckStructAlignments(path, toCheckSizes, false)
    60  }
    61  
    62  func init() {
    63  	registerToCheck(map[string][]any{
    64  		"ipv4_ct_tuple":        {ctmap.CtKey4{}, ctmap.CtKey4Global{}},
    65  		"ipv6_ct_tuple":        {ctmap.CtKey6{}, ctmap.CtKey6Global{}},
    66  		"ct_entry":             {ctmap.CtEntry{}},
    67  		"ipcache_key":          {ipcachemap.Key{}},
    68  		"remote_endpoint_info": {ipcachemap.RemoteEndpointInfo{}},
    69  		"lb4_key":              {lbmap.Service4Key{}},
    70  		"lb4_service":          {lbmap.Service4Value{}},
    71  		"lb4_backend":          {lbmap.Backend4ValueV3{}},
    72  		"lb6_key":              {lbmap.Service6Key{}},
    73  		"lb6_service":          {lbmap.Service6Value{}},
    74  		"lb6_backend":          {lbmap.Backend6ValueV3{}},
    75  		"endpoint_info":        {lxcmap.EndpointInfo{}},
    76  		"metrics_key":          {metricsmap.Key{}},
    77  		"metrics_value":        {metricsmap.Value{}},
    78  		"policy_key":           {policymap.PolicyKey{}},
    79  		"policy_entry":         {policymap.PolicyEntry{}},
    80  		"ipv4_revnat_tuple":    {lbmap.SockRevNat4Key{}},
    81  		"ipv4_revnat_entry":    {lbmap.SockRevNat4Value{}},
    82  		"ipv6_revnat_tuple":    {lbmap.SockRevNat6Key{}},
    83  		"ipv6_revnat_entry":    {lbmap.SockRevNat6Value{}},
    84  		"v6addr": {
    85  			neighborsmap.Key6{},
    86  			srv6map.PolicyValue{},
    87  			srv6map.SIDKey{},
    88  		},
    89  		"macaddr":           {neighborsmap.Value{}},
    90  		"ipv4_frag_id":      {fragmap.FragmentKey{}},
    91  		"ipv4_frag_l4ports": {fragmap.FragmentValue{}},
    92  		"capture4_wcard":    {recorder.CaptureWcard4{}},
    93  		"capture6_wcard":    {recorder.CaptureWcard6{}},
    94  		"capture_rule":      {recorder.CaptureRule4{}},
    95  		// TODO: alignchecker does not support nested structs yet.
    96  		// "capture_rule":      {recorder.CaptureRule6{}},
    97  		// "ipv4_nat_entry":    {nat.NatEntry4{}},
    98  		// "ipv6_nat_entry":    {nat.NatEntry6{}},
    99  		"endpoint_key":           {bpf.EndpointKey{}},
   100  		"lb4_affinity_key":       {lbmap.Affinity4Key{}},
   101  		"lb6_affinity_key":       {lbmap.Affinity6Key{}},
   102  		"lb_affinity_match":      {lbmap.AffinityMatchKey{}},
   103  		"lb_affinity_val":        {lbmap.AffinityValue{}},
   104  		"lb4_src_range_key":      {lbmap.SourceRangeKey4{}},
   105  		"lb6_src_range_key":      {lbmap.SourceRangeKey6{}},
   106  		"edt_id":                 {bwmap.EdtId{}},
   107  		"edt_info":               {bwmap.EdtInfo{}},
   108  		"egress_gw_policy_key":   {egressmap.EgressPolicyKey4{}},
   109  		"egress_gw_policy_entry": {egressmap.EgressPolicyVal4{}},
   110  		"srv6_vrf_key4":          {srv6map.VRFKey4{}},
   111  		"srv6_vrf_key6":          {srv6map.VRFKey6{}},
   112  		"srv6_policy_key4":       {srv6map.PolicyKey4{}},
   113  		"srv6_policy_key6":       {srv6map.PolicyKey6{}},
   114  		"tunnel_key":             {tunnel.TunnelKey{}},
   115  		"tunnel_value":           {tunnel.TunnelValue{}},
   116  		"vtep_key":               {vtep.Key{}},
   117  		"vtep_value":             {vtep.VtepEndpointInfo{}},
   118  		"auth_key":               {authmap.AuthKey{}},
   119  		"auth_info":              {authmap.AuthInfo{}},
   120  		"skip_lb4_key":           {lbmap.SkipLB4Key{}},
   121  		"skip_lb6_key":           {lbmap.SkipLB6Key{}},
   122  	})
   123  
   124  	registerToCheckSizes(map[string][]any{
   125  		"__u16": {
   126  			lbmap.Backend4Key{},
   127  			lbmap.Backend6Key{},
   128  			lbmap.RevNat4Key{},
   129  			lbmap.RevNat6Key{},
   130  		},
   131  		"__u32": {
   132  			lbmap.Backend4KeyV3{},
   133  			lbmap.Backend6KeyV3{},
   134  			signalmap.Key{},
   135  			signalmap.Value{},
   136  			eventsmap.Key{},
   137  			eventsmap.Value{},
   138  			policymap.CallKey{},
   139  			policymap.CallValue{},
   140  			srv6map.VRFValue{},
   141  			srv6map.SIDValue{},
   142  		},
   143  		"__be32": {neighborsmap.Key4{}},
   144  	})
   145  }