github.com/fafucoder/cilium@v1.6.11/cilium/cmd/bpf_config_get.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"strconv"
    21  
    22  	"github.com/cilium/cilium/common"
    23  	"github.com/cilium/cilium/pkg/bpf"
    24  	"github.com/cilium/cilium/pkg/command"
    25  	"github.com/cilium/cilium/pkg/maps/configmap"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  // bpfConfigGetCmd represents the bpf_config_get command
    31  var bpfConfigGetCmd = &cobra.Command{
    32  	Use:   "get",
    33  	Short: "List contents of an endpoint config BPF map",
    34  	Run: func(cmd *cobra.Command, args []string) {
    35  		common.RequireRootPrivilege("cilium bpf config get")
    36  		requireEndpointID(cmd, args)
    37  
    38  		if true {
    39  			fmt.Printf("directly dumping endpoint config maps is not supported right now; See GH-6228 for more information\n")
    40  			return
    41  		}
    42  		listEndpointConfigMap(args)
    43  	},
    44  }
    45  
    46  func init() {
    47  	bpfConfigCmd.AddCommand(bpfConfigGetCmd)
    48  	command.AddJSONOutput(bpfConfigCmd)
    49  }
    50  
    51  func listEndpointConfigMap(args []string) {
    52  	lbl := args[0]
    53  
    54  	if lbl == "" {
    55  		Fatalf("Need ID or label\n")
    56  	}
    57  
    58  	id, err := strconv.Atoi(lbl)
    59  	if err != nil {
    60  		Fatalf("Invalid endpoint ID %q", lbl)
    61  	}
    62  	file := bpf.LocalMapPath(configmap.MapNamePrefix, uint16(id))
    63  
    64  	fd, err := bpf.ObjGet(file)
    65  	if err != nil {
    66  		Fatalf("%s\n", err)
    67  	}
    68  	defer bpf.ObjClose(fd)
    69  
    70  	m, _, err := configmap.OpenMapWithName(file)
    71  	if err != nil {
    72  		fmt.Printf("error opening map %s: %s\n", file, err)
    73  		os.Exit(1)
    74  	}
    75  
    76  	bpfConfigGet := make(map[string][]string)
    77  	if err := m.Map.Dump(bpfConfigGet); err != nil {
    78  		os.Exit(1)
    79  	}
    80  	if err != nil {
    81  		Fatalf("Error while opening bpf Map: %s\n", err)
    82  	}
    83  
    84  	fmt.Printf("bpfConfigGet: %s\n", bpfConfigGet)
    85  	if command.OutputJSON() {
    86  		if err := command.PrintOutput(bpfConfigCmd); err != nil {
    87  			os.Exit(1)
    88  		}
    89  		return
    90  	}
    91  
    92  	TablePrinter("KEY", "ENDPOINT CONFIGURATION", bpfConfigGet)
    93  }