github.com/fafucoder/cilium@v1.6.11/cilium/cmd/bpf_ct_flush.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/maps/ctmap"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  // bpfCtFlushCmd represents the bpf_ct_flush command
    29  var bpfCtFlushCmd = &cobra.Command{
    30  	Use:    "flush ( <endpoint identifier> | global )",
    31  	Short:  "Flush all connection tracking entries",
    32  	PreRun: requireEndpointIDorGlobal,
    33  	Run: func(cmd *cobra.Command, args []string) {
    34  		common.RequireRootPrivilege("cilium bpf ct flush")
    35  		flushCt(args[0])
    36  	},
    37  }
    38  
    39  func init() {
    40  	bpfCtCmd.AddCommand(bpfCtFlushCmd)
    41  }
    42  
    43  type dummyEndpoint struct {
    44  	ID int
    45  }
    46  
    47  func (d dummyEndpoint) GetID() uint64 {
    48  	return uint64(d.ID)
    49  }
    50  
    51  func flushCt(eID string) {
    52  	var maps []*ctmap.Map
    53  	if eID == "global" {
    54  		maps = ctmap.GlobalMaps(true, true)
    55  	} else {
    56  		id, _ := strconv.Atoi(eID)
    57  		maps = ctmap.LocalMaps(&dummyEndpoint{ID: id}, true, true)
    58  	}
    59  	for _, m := range maps {
    60  		path, err := m.Path()
    61  		if err == nil {
    62  			err = m.Open()
    63  		}
    64  		if err != nil {
    65  			if os.IsNotExist(err) {
    66  				msg := "Unable to open %s: %s."
    67  				if eID != "global" {
    68  					msg = "Unable to open %s: %s: please try using \"cilium bpf ct flush global\"."
    69  				}
    70  				fmt.Fprintf(os.Stderr, msg+" Skipping.\n", path, err)
    71  				continue
    72  			}
    73  			Fatalf("Unable to open %s: %s", path, err)
    74  		}
    75  		defer m.Close()
    76  		entries := m.Flush()
    77  		fmt.Printf("Flushed %d entries from %s\n", entries, path)
    78  	}
    79  }