github.com/elfadel/cilium@v1.6.12/pkg/datapath/alignchecker/alignchecker.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 alignchecker 16 17 import ( 18 "reflect" 19 20 check "github.com/cilium/cilium/pkg/alignchecker" 21 "github.com/cilium/cilium/pkg/bpf" 22 "github.com/cilium/cilium/pkg/maps/configmap" 23 "github.com/cilium/cilium/pkg/maps/ctmap" 24 "github.com/cilium/cilium/pkg/maps/eppolicymap" 25 ipcachemap "github.com/cilium/cilium/pkg/maps/ipcache" 26 "github.com/cilium/cilium/pkg/maps/lbmap" 27 "github.com/cilium/cilium/pkg/maps/lxcmap" 28 "github.com/cilium/cilium/pkg/maps/metricsmap" 29 "github.com/cilium/cilium/pkg/maps/policymap" 30 "github.com/cilium/cilium/pkg/maps/sockmap" 31 "github.com/cilium/cilium/pkg/maps/tunnel" 32 ) 33 34 // CheckStructAlignments checks whether size and offsets of the C and Go 35 // structs for the datapath match. 36 // 37 // C struct size info is extracted from the given ELF object file debug section 38 // encoded in DWARF. 39 // 40 // To find a matching C struct field, a Go field has to be tagged with 41 // `align:"field_name_in_c_struct". In the case of unnamed union field, such 42 // union fields can be referred with special tags - `align:"$union0"`, 43 // `align:"$union1"`, etc. 44 func CheckStructAlignments(path string) error { 45 // Validate alignments of C and Go equivalent structs 46 toCheck := map[string][]reflect.Type{ 47 "ipv4_ct_tuple": {reflect.TypeOf(ctmap.CtKey4{}), reflect.TypeOf(ctmap.CtKey4Global{})}, 48 "ipv6_ct_tuple": {reflect.TypeOf(ctmap.CtKey6{}), reflect.TypeOf(ctmap.CtKey6Global{})}, 49 "ct_entry": {reflect.TypeOf(ctmap.CtEntry{})}, 50 "ipcache_key": {reflect.TypeOf(ipcachemap.Key{})}, 51 "remote_endpoint_info": {reflect.TypeOf(ipcachemap.RemoteEndpointInfo{})}, 52 "lb4_key_v2": {reflect.TypeOf(lbmap.Service4KeyV2{})}, 53 "lb4_service_v2": {reflect.TypeOf(lbmap.Service4ValueV2{})}, 54 "lb4_backend": {reflect.TypeOf(lbmap.Backend4Value{})}, 55 "lb6_key": {reflect.TypeOf(lbmap.Service6Key{})}, 56 "lb6_service": {reflect.TypeOf(lbmap.Service6Value{})}, 57 "lb6_key_v2": {reflect.TypeOf(lbmap.Service6KeyV2{})}, 58 "lb6_service_v2": {reflect.TypeOf(lbmap.Service6ValueV2{})}, 59 "lb6_backend": {reflect.TypeOf(lbmap.Backend6Value{})}, 60 "endpoint_info": {reflect.TypeOf(lxcmap.EndpointInfo{})}, 61 "metrics_key": {reflect.TypeOf(metricsmap.Key{})}, 62 "metrics_value": {reflect.TypeOf(metricsmap.Value{})}, 63 "policy_key": {reflect.TypeOf(policymap.PolicyKey{})}, 64 "policy_entry": {reflect.TypeOf(policymap.PolicyEntry{})}, 65 "sock_key": {reflect.TypeOf(sockmap.SockmapKey{})}, 66 "ep_config": {reflect.TypeOf(configmap.EndpointConfig{})}, 67 // TODO: alignchecker does not support nested structs yet. 68 // "ipv4_nat_entry": {reflect.TypeOf(nat.NatEntry4{})}, 69 // "ipv6_nat_entry": {reflect.TypeOf(nat.NatEntry6{})}, 70 "endpoint_key": { 71 reflect.TypeOf(bpf.EndpointKey{}), 72 reflect.TypeOf(eppolicymap.EndpointKey{}), 73 reflect.TypeOf(tunnel.TunnelEndpoint{}), 74 }, 75 } 76 return check.CheckStructAlignments(path, toCheck) 77 }