Skip to content

Commit

Permalink
alignchecker: split alignment checks for monitor types into own package
Browse files Browse the repository at this point in the history
The datapath/alignchecker package also checks the monitor types for
proper alignment and thus needs to import the monitor package. These
types are not used in the agend but importing the package ends up
pulling in github.com/google/gopacket which adds quite a lot to the
binary size.

Thus, split out the alignment checks for the monitor types into its own
subpackage of package monitor, monitor/alignchecker and call the
corresponding CheckStructAlignments from tools/alignchecker/main.go as
well.

This reduces the binary size of cilium-agent by ~2MB.

Updates #10056

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser authored and borkmann committed Feb 19, 2020
1 parent 78fefa7 commit c6f3240
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
5 changes: 0 additions & 5 deletions pkg/datapath/alignchecker/alignchecker.go
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/cilium/cilium/pkg/maps/policymap"
"github.com/cilium/cilium/pkg/maps/sockmap"
"github.com/cilium/cilium/pkg/maps/tunnel"
"github.com/cilium/cilium/pkg/monitor"
)

// CheckStructAlignments checks whether size and offsets of the C and Go
Expand Down Expand Up @@ -69,10 +68,6 @@ func CheckStructAlignments(path string) error {
reflect.TypeOf(eppolicymap.EndpointKey{}),
reflect.TypeOf(tunnel.TunnelEndpoint{}),
},
"trace_notify": {reflect.TypeOf(monitor.TraceNotify{})},
"drop_notify": {reflect.TypeOf(monitor.DropNotify{})},
"debug_msg": {reflect.TypeOf(monitor.DebugMsg{})},
"debug_capture_msg": {reflect.TypeOf(monitor.DebugCapture{})},
}
return check.CheckStructAlignments(path, toCheck)
}
35 changes: 35 additions & 0 deletions pkg/monitor/alignchecker/alignchecker.go
@@ -0,0 +1,35 @@
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package alignchecker

import (
"reflect"

check "github.com/cilium/cilium/pkg/alignchecker"
"github.com/cilium/cilium/pkg/monitor"
)

// See pkg/datapath/alignchecker/alignchecker.go:CheckStructAlignments()
// comment.
func CheckStructAlignments(path string) error {
// Validate alignments of C and Go equivalent structs
toCheck := map[string][]reflect.Type{
"trace_notify": {reflect.TypeOf(monitor.TraceNotify{})},
"drop_notify": {reflect.TypeOf(monitor.DropNotify{})},
"debug_msg": {reflect.TypeOf(monitor.DebugMsg{})},
"debug_capture_msg": {reflect.TypeOf(monitor.DebugCapture{})},
}
return check.CheckStructAlignments(path, toCheck)
}
17 changes: 17 additions & 0 deletions pkg/monitor/alignchecker/doc.go
@@ -0,0 +1,17 @@
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package alignchecker is a thin wrapper around pkg/alignchecker to validate
// monitor object alignment.
package alignchecker
11 changes: 8 additions & 3 deletions tools/alignchecker/main.go
Expand Up @@ -18,7 +18,8 @@ import (
"fmt"
"os"

"github.com/cilium/cilium/pkg/datapath/alignchecker"
datapathchecker "github.com/cilium/cilium/pkg/datapath/alignchecker"
monitorchecker "github.com/cilium/cilium/pkg/monitor/alignchecker"
)

func main() {
Expand All @@ -32,8 +33,12 @@ func main() {
fmt.Fprintf(os.Stderr, "Cannot check alignment against %s: %s\n", bpfObjPath, err)
os.Exit(1)
}
if err := alignchecker.CheckStructAlignments(bpfObjPath); err != nil {
fmt.Fprintf(os.Stderr, "C and Go structs alignment check failed: %s\n", err)
if err := datapathchecker.CheckStructAlignments(bpfObjPath); err != nil {
fmt.Fprintf(os.Stderr, "C and Go structs alignment check in datapath failed: %s\n", err)
os.Exit(1)
}
if err := monitorchecker.CheckStructAlignments(bpfObjPath); err != nil {
fmt.Fprintf(os.Stderr, "C and Go structs alignment check in monitor failed: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "OK\n")
Expand Down

0 comments on commit c6f3240

Please sign in to comment.