forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf_config_get.go
93 lines (78 loc) · 2.26 KB
/
bpf_config_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2018-2019 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 (
"fmt"
"os"
"strconv"
"github.com/cilium/cilium/common"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/command"
"github.com/cilium/cilium/pkg/maps/configmap"
"github.com/spf13/cobra"
)
// bpfConfigGetCmd represents the bpf_config_get command
var bpfConfigGetCmd = &cobra.Command{
Use: "get",
Short: "List contents of an endpoint config BPF map",
Run: func(cmd *cobra.Command, args []string) {
common.RequireRootPrivilege("cilium bpf config get")
requireEndpointID(cmd, args)
if true {
fmt.Printf("directly dumping endpoint config maps is not supported right now; See GH-6228 for more information\n")
return
}
listEndpointConfigMap(args)
},
}
func init() {
bpfConfigCmd.AddCommand(bpfConfigGetCmd)
command.AddJSONOutput(bpfConfigCmd)
}
func listEndpointConfigMap(args []string) {
lbl := args[0]
if lbl == "" {
Fatalf("Need ID or label\n")
}
id, err := strconv.Atoi(lbl)
if err != nil {
Fatalf("Invalid endpoint ID %q", lbl)
}
file := bpf.LocalMapPath(configmap.MapNamePrefix, uint16(id))
fd, err := bpf.ObjGet(file)
if err != nil {
Fatalf("%s\n", err)
}
defer bpf.ObjClose(fd)
m, _, err := configmap.OpenMapWithName(file)
if err != nil {
fmt.Printf("error opening map %s: %s\n", file, err)
os.Exit(1)
}
bpfConfigGet := make(map[string][]string)
if err := m.Map.Dump(bpfConfigGet); err != nil {
os.Exit(1)
}
if err != nil {
Fatalf("Error while opening bpf Map: %s\n", err)
}
fmt.Printf("bpfConfigGet: %s\n", bpfConfigGet)
if command.OutputJSON() {
if err := command.PrintOutput(bpfConfigCmd); err != nil {
os.Exit(1)
}
return
}
TablePrinter("KEY", "ENDPOINT CONFIGURATION", bpfConfigGet)
}