github.com/fafucoder/cilium@v1.6.11/cilium/cmd/bpf_ct_list.go (about) 1 // Copyright 2017-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 "strconv" 21 22 "github.com/cilium/cilium/common" 23 "github.com/cilium/cilium/pkg/command" 24 "github.com/cilium/cilium/pkg/maps/ctmap" 25 26 "github.com/spf13/cobra" 27 ) 28 29 // bpfCtListCmd represents the bpf_ct_list command 30 var bpfCtListCmd = &cobra.Command{ 31 Use: "list ( <endpoint identifier> | global )", 32 Aliases: []string{"ls"}, 33 Short: "List connection tracking entries", 34 PreRun: requireEndpointIDorGlobal, 35 Run: func(cmd *cobra.Command, args []string) { 36 common.RequireRootPrivilege("cilium bpf ct list") 37 dumpCt(args[0]) 38 }, 39 } 40 41 func init() { 42 bpfCtCmd.AddCommand(bpfCtListCmd) 43 command.AddJSONOutput(bpfCtListCmd) 44 } 45 46 func dumpCt(eID string) { 47 var maps []*ctmap.Map 48 if eID == "global" { 49 maps = ctmap.GlobalMaps(true, true) 50 } else { 51 id, _ := strconv.Atoi(eID) 52 maps = ctmap.LocalMaps(&dummyEndpoint{ID: id}, true, true) 53 } 54 55 for _, m := range maps { 56 path, err := m.Path() 57 if err == nil { 58 err = m.Open() 59 } 60 if err != nil { 61 if os.IsNotExist(err) { 62 msg := "Unable to open %s: %s." 63 if eID != "global" { 64 msg = "Unable to open %s: %s: please try using \"cilium bpf ct list global\"." 65 } 66 fmt.Fprintf(os.Stderr, msg+" Skipping.\n", path, err) 67 continue 68 } 69 Fatalf("Unable to open %s: %s", path, err) 70 } 71 defer m.Close() 72 if command.OutputJSON() { 73 if err := command.PrintOutput(m); err != nil { 74 os.Exit(1) 75 } 76 } else { 77 out, err := m.DumpEntries() 78 if err != nil { 79 Fatalf("Error while dumping BPF Map: %s", err) 80 } 81 fmt.Println(out) 82 } 83 } 84 }