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

     1  // Copyright 2018 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  
    21  	"github.com/cilium/cilium/common"
    22  	"github.com/cilium/cilium/pkg/command"
    23  	"github.com/cilium/cilium/pkg/maps/ipcache"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  const (
    29  	ipAddrTitle          = "IP PREFIX/ADDRESS"
    30  	identityTitle        = "IDENTITY"
    31  	ipCacheListUsage     = "List endpoint IPs (local and remote) and their corresponding security identities.\n" + kernelVersionWarning
    32  	kernelVersionWarning = `
    33  Note that for Linux kernel versions between 4.11 and 4.15 inclusive, the native
    34  LPM map type used for implementing the IPCache does not provide the ability to
    35  walk / dump the entries, so on these kernel versions this tool will never
    36  return any entries, even if entries exist in the map. You may instead run:
    37      cilium map get cilium_ipcache
    38  `
    39  )
    40  
    41  var bpfIPCacheListCmd = &cobra.Command{
    42  	Use:     "list",
    43  	Aliases: []string{"ls"},
    44  	Short:   "List endpoint IPs (local and remote) and their corresponding security identities",
    45  	Long:    ipCacheListUsage,
    46  	Run: func(cmd *cobra.Command, args []string) {
    47  		common.RequireRootPrivilege("cilium bpf ipcache list")
    48  
    49  		bpfIPCacheList := make(map[string][]string)
    50  		if err := ipcache.IPCache.Dump(bpfIPCacheList); err != nil {
    51  			fmt.Fprintf(os.Stderr, "error dumping contents of map: %s\n", err)
    52  			os.Exit(1)
    53  		}
    54  
    55  		if command.OutputJSON() {
    56  			if err := command.PrintOutput(bpfIPCacheList); err != nil {
    57  				fmt.Fprintf(os.Stderr, "error getting output of map in JSON: %s\n", err)
    58  				os.Exit(1)
    59  			}
    60  			return
    61  		}
    62  
    63  		if len(bpfIPCacheList) == 0 {
    64  			fmt.Fprintf(os.Stderr, "No entries found.\n%v\n", kernelVersionWarning)
    65  		} else {
    66  			TablePrinter(ipAddrTitle, identityTitle, bpfIPCacheList)
    67  		}
    68  	},
    69  }
    70  
    71  func init() {
    72  	bpfIPCacheCmd.AddCommand(bpfIPCacheListCmd)
    73  	command.AddJSONOutput(bpfIPCacheListCmd)
    74  }