Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add "cilium bpf config list" #26105

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/cmdref/cilium_bpf.md

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

25 changes: 25 additions & 0 deletions Documentation/cmdref/cilium_bpf_config.md

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

33 changes: 33 additions & 0 deletions Documentation/cmdref/cilium_bpf_config_list.md

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

2 changes: 2 additions & 0 deletions bugtool/cmd/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func defaultCommands(confDir string, cmdDir string, k8sPods []string) []string {
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_call_policy", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_calls_overlay_2", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_capture_cache", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_runtime_config", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lxc", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_metrics", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_tunnel_map", bpffsMountpoint),
Expand Down Expand Up @@ -373,6 +374,7 @@ func copyCiliumInfoCommands(cmdDir string, k8sPods []string) []string {
"cilium endpoint list",
"cilium bpf auth list",
"cilium bpf bandwidth list",
"cilium bpf config list",
"cilium bpf tunnel list",
"cilium bpf lb list",
"cilium bpf lb list --revnat",
Expand Down
18 changes: 18 additions & 0 deletions cilium/cmd/bpf_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package cmd

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

// bpfConfigCmd represents the bpf command
var bpfConfigCmd = &cobra.Command{
Use: "config",
Short: "Manage runtime config",
}

func init() {
bpfCmd.AddCommand(bpfConfigCmd)
}
80 changes: 80 additions & 0 deletions cilium/cmd/bpf_config_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package cmd

import (
"errors"
"fmt"
"io/fs"
"os"
"text/tabwriter"

"github.com/spf13/cobra"

"github.com/cilium/cilium/pkg/command"
"github.com/cilium/cilium/pkg/common"
"github.com/cilium/cilium/pkg/maps/configmap"
)

type configEntry struct {
Key string
Value uint64
}

var bpfConfigListCmd = &cobra.Command{
Use: "list",
Short: "List all runtime config entries",
Long: "List all runtime config entries",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
common.RequireRootPrivilege("cilium bpf config list")

configMap, err := configmap.LoadMap()
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
fmt.Fprintln(os.Stderr, "Cannot find config bpf map")
return
}

Fatalf("Cannot load config bpf map: %s", err)
}

var entries []configEntry

for _, index := range []configmap.Index{
configmap.AgentLiveness,
configmap.UTimeOffset,
} {
value, err := configMap.Get(index)
if err != nil {
Fatalf("Cannot load value with index %q from config bpf map: %s", index, err)
}
entries = append(entries, configEntry{Key: index.String(), Value: value})
}

if command.OutputOption() {
if err := command.PrintOutput(entries); err != nil {
Fatalf("error getting output of map in JSON: %s\n", err)
}
return
}

printEntries(entries)
},
}

func printEntries(entries []configEntry) {
w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)

fmt.Fprintln(w, "KEY\tVALUE")
for _, e := range entries {
fmt.Fprintf(w, "%s\t%d\n", e.Key, e.Value)
}
w.Flush()
}

func init() {
bpfConfigCmd.AddCommand(bpfConfigListCmd)
command.AddOutputOption(bpfConfigListCmd)
}
48 changes: 43 additions & 5 deletions pkg/maps/configmap/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ const (
AgentLiveness
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylandreimerink @jrajahalme it looks like the "marker" entry UsedEntries is unused and lost its meaning. When L2 Announcement has been introduced, a new entry has been added to the end (whereas the intention of the marker entry was to be the last entry).

should we delete the entry and move AgentLiveness to second position?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving AgentLiveness is alright with me, as long as we do it before the feature freeze. Moving after that could pose upgrade issues if the map is not recreated. I also have no idea what UsedEntries was supposed to be used for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylandreimerink thanks - removed the constant (in agent & common.h)

)

// Value is the generic datapath runtime config value.
type Value uint64

// String pretty print the Index
func (k *Index) String() string {
return fmt.Sprintf("%d", uint32(*k))
func (r Index) String() string {
switch r {
case UTimeOffset:
return "UTimeOffset"
case AgentLiveness:
return "AgentLiveness"
default:
return "Unknown"
}
}

// Value is the generic datapath runtime config value.
type Value uint64

func (k *Index) New() bpf.MapKey { return new(Index) }

// String pretty print the config Value.
Expand All @@ -53,6 +60,8 @@ func (v *Value) New() bpf.MapValue { return new(Value) }
type Map interface {
// Update writes the given uint64 value to the bpf map at the given index.
Update(index Index, val uint64) error

Get(index Index) (uint64, error)
}

type configMap struct {
Expand All @@ -74,6 +83,21 @@ func newConfigMap() *configMap {
}
}

// LoadMap loads the pre-initialized config map for access.
// This should only be used from components which aren't capable of using hive - mainly the Cilium CLI.
// It needs to initialized beforehand via the Cilium Agent.
func LoadMap() (Map, error) {
var index Index
var value Value

m, err := bpf.OpenMap(bpf.MapPath(MapName), &index, &value)
if err != nil {
return nil, fmt.Errorf("failed to load bpf map: %w", err)
}

return &configMap{bpfMap: m}, nil
}

func (m *configMap) init() error {
if err := m.bpfMap.OpenOrCreate(); err != nil {
return fmt.Errorf("failed to init bpf map: %w", err)
Expand All @@ -90,6 +114,20 @@ func (m *configMap) close() error {
return nil
}

func (m *configMap) Get(index Index) (uint64, error) {
v, err := m.bpfMap.Lookup(&index)
if err != nil {
return 0, fmt.Errorf("failed to lookup entry: %w", err)
}

mapValue, ok := v.(*Value)
if !ok {
return 0, fmt.Errorf("wrong config map value: %w", err)
}

return uint64(*mapValue), nil
}

func (m *configMap) Update(index Index, val uint64) error {
value := Value(val)
return m.bpfMap.Update(&index, &value)
Expand Down