Skip to content

Commit

Permalink
bpf: Migrate cilium-migrate-map From C to Go
Browse files Browse the repository at this point in the history
Currently initializing bpf programs and migrating maps
is done with a bash script. This creates a Go version of
the "cilium-migrate-map" program and refactors it to be
useful by a, similarly, migrated init program. Specifically,
giving feedback as to whether any maps even need migrating
(often they don't) thus allowing the init program to
skip the expensive loading and processing of ELF files.

Additionally, a Go version of the C command line program
is also added to the cilium container cli program to be used,
temporarily, by the init.sh program to ensure that the logic
of the new Go migrate program is sound.

Signed-off-by: Nate Sweet <nathanjsweet@pm.me>
  • Loading branch information
nathanjsweet committed Aug 11, 2021
1 parent fc6ef4d commit 2e5dcd4
Show file tree
Hide file tree
Showing 40 changed files with 2,113 additions and 468 deletions.
8 changes: 4 additions & 4 deletions bpf/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ function bpf_load()
bpf_compile $IN $OUT obj "$OPTS"
tc qdisc replace dev $DEV clsact || true
[ -z "$(tc filter show dev $DEV $WHERE | grep -v 'pref 1 bpf chain 0 $\|pref 1 bpf chain 0 handle 0x1')" ] || tc filter del dev $DEV $WHERE
cilium-map-migrate -s $OUT
cilium map migrate -s $OUT
set +e
tc filter replace dev $DEV $WHERE prio 1 handle 1 bpf da obj $OUT sec $SEC
RETCODE=$?
set -e
cilium-map-migrate -e $OUT -r $RETCODE
cilium map migrate -e $OUT -r $RETCODE
return $RETCODE
}

Expand All @@ -286,12 +286,12 @@ function bpf_load_cgroups()
TMP_FILE="$BPFMNT/tc/globals/cilium_cgroups_$WHERE"
rm -f $TMP_FILE

cilium-map-migrate -s $OUT
cilium map migrate -s $OUT
set +e
tc exec bpf pin $TMP_FILE obj $OUT type $PROG_TYPE attach_type $WHERE sec $WHERE
RETCODE=$?
set -e
cilium-map-migrate -e $OUT -r $RETCODE
cilium map migrate -e $OUT -r $RETCODE

if [ "$RETCODE" -eq "0" ]; then
set +e
Expand Down
29 changes: 29 additions & 0 deletions cilium/cmd/bpf_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 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 cmd

import (
"github.com/spf13/cobra"
)

// bpfMigrateCmd represents the map command
var bpfMapCmd = &cobra.Command{
Use: "map",
Short: "BPF map commands",
}

func init() {
bpfCmd.AddCommand(bpfMapCmd)
}
73 changes: 73 additions & 0 deletions cilium/cmd/bpf_map_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2021 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 cmd

import (
"log/syslog"

"github.com/spf13/cobra"

"github.com/cilium/cilium/pkg/bpf/migrate"
)

// bpfMigrateCmd represents the migrate command
var (
bpfMigrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate map",
Run: func(cmd *cobra.Command, args []string) {
if len(s) > 0 && len(e) > 0 {
log.Fatalf("s (%q) and e (%q) cannot be both set", s, e)
}
var (
pathName string
finalize bool
)
if len(s) > 0 {
pathName = s
} else if len(e) > 0 {
pathName = e
finalize = true
} else {
log.Fatalf("either s or e must be a valid filepath")
}
sysLogger, err := syslog.New(syslog.LOG_WARNING, "cilium-map-migrate")
if err != nil {
log.Printf("Could not open syslog: %v", err)
sysLogger = nil
} else {
defer sysLogger.Close()
}
if !finalize {
_, err = migrate.Start(pathName, sysLogger)
} else {
err = migrate.Finish(pathName, r, sysLogger)
}
if err != nil {
log.Fatalf("error migrating state for %q: %v", pathName, err)
}
},
}
s string
e string
r int
)

func init() {
bpfMapCmd.AddCommand(bpfMigrateCmd)
bpfMigrateCmd.Flags().StringVarP(&s, "start", "s", "", "start file")
bpfMigrateCmd.Flags().StringVarP(&e, "end", "e", "", "end file")
bpfMigrateCmd.Flags().IntVarP(&r, "return", "r", 0, "return code")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/cilium/customvet v0.0.0-20201209211516-9852765c1ac4
github.com/cilium/deepequal-gen v0.0.0-20200406125435-ad6a9003139e
github.com/cilium/ebpf v0.5.1-0.20210421150058-a4ee356536f3
github.com/cilium/ebpf v0.6.3-0.20210804143410-d3bc4435231e
github.com/cilium/ipam v0.0.0-20201106170308-4184bc4bf9d6
github.com/cilium/proxy v0.0.0-20210511221533-82a70d56bf32
github.com/cncf/udpa/go v0.0.0-20201211205326-cc1b757b3edd // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e5dcd4

Please sign in to comment.