How to match C struct conditional field with golang struct field

Viewed 87

I ran into issue with https://github.com/cilium/cilium/pull/17370#discussion_r731170618, where in order to save memory space, the suggestion is to make a conditional field like below:

 struct remote_endpoint_info {
        __u32           sec_label;
        __u32           tunnel_endpoint;
#ifdef ENABLE_VTEP
        mac_t           vtep_mac;
#endif
        __u8            key;
 };

but it cause misalignment issue with golang struct below:

// RemoteEndpointInfo implements the bpf.MapValue interface. It contains the
// security identity of a remote endpoint.
// +k8s:deepcopy-gen=true
   +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
type RemoteEndpointInfo struct {
        SecurityIdentity uint32        `align:"sec_label"`
        TunnelEndpoint   types.IPv4    `align:"tunnel_endpoint"`
        VtepMAC          mac.Uint64MAC `align:"vtep_mac"`
        Key              uint8         `align:"key"`
}

I wonder if I could do


 struct remote_endpoint_info {
        __u32           sec_label;
        __u32           tunnel_endpoint;
        mac_t           vtep_mac __maybe_unused;
        __u8            key;
 };

I got no misalignment error report, but does it really save memory space like the conditional ifdef ENABLE_VTEP

0 Answers
Related