github.com/fafucoder/cilium@v1.6.11/pkg/monitor/datapath_drop.go (about)

     1  // Copyright 2016-2017 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 monitor
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  
    21  	"github.com/cilium/cilium/pkg/monitor/api"
    22  )
    23  
    24  const (
    25  	// DropNotifyLen is the amount of packet data provided in a drop notification
    26  	DropNotifyLen = 32
    27  )
    28  
    29  // DropNotify is the message format of a drop notification in the BPF ring buffer
    30  type DropNotify struct {
    31  	Type     uint8
    32  	SubType  uint8
    33  	Source   uint16
    34  	Hash     uint32
    35  	OrigLen  uint32
    36  	CapLen   uint32
    37  	SrcLabel uint32
    38  	DstLabel uint32
    39  	DstID    uint32
    40  	Unused   uint32
    41  	// data
    42  }
    43  
    44  // DumpInfo prints a summary of the drop messages.
    45  func (n *DropNotify) DumpInfo(data []byte) {
    46  	fmt.Printf("xx drop (%s) flow %#x to endpoint %d, identity %d->%d: %s\n",
    47  		api.DropReason(n.SubType), n.Hash, n.DstID, n.SrcLabel, n.DstLabel,
    48  		GetConnectionSummary(data[DropNotifyLen:]))
    49  }
    50  
    51  // DumpVerbose prints the drop notification in human readable form
    52  func (n *DropNotify) DumpVerbose(dissect bool, data []byte, prefix string) {
    53  	fmt.Printf("%s MARK %#x FROM %d DROP: %d bytes, reason %s",
    54  		prefix, n.Hash, n.Source, n.OrigLen, api.DropReason(n.SubType))
    55  
    56  	if n.SrcLabel != 0 || n.DstLabel != 0 {
    57  		fmt.Printf(", identity %d->%d", n.SrcLabel, n.DstLabel)
    58  	}
    59  
    60  	if n.DstID != 0 {
    61  		fmt.Printf(", to endpoint %d\n", n.DstID)
    62  	} else {
    63  		fmt.Printf("\n")
    64  	}
    65  
    66  	if n.CapLen > 0 && len(data) > DropNotifyLen {
    67  		Dissect(dissect, data[DropNotifyLen:])
    68  	}
    69  }
    70  
    71  func (n *DropNotify) getJSON(data []byte, cpuPrefix string) (string, error) {
    72  
    73  	v := DropNotifyToVerbose(n)
    74  	v.CPUPrefix = cpuPrefix
    75  	if n.CapLen > 0 && len(data) > DropNotifyLen {
    76  		v.Summary = GetDissectSummary(data[DropNotifyLen:])
    77  	}
    78  
    79  	ret, err := json.Marshal(v)
    80  	return string(ret), err
    81  }
    82  
    83  // DumpJSON prints notification in json format
    84  func (n *DropNotify) DumpJSON(data []byte, cpuPrefix string) {
    85  	resp, err := n.getJSON(data, cpuPrefix)
    86  	if err == nil {
    87  		fmt.Println(resp)
    88  	}
    89  }
    90  
    91  // DropNotifyVerbose represents a json notification printed by monitor
    92  type DropNotifyVerbose struct {
    93  	CPUPrefix string `json:"cpu,omitempty"`
    94  	Type      string `json:"type,omitempty"`
    95  	Mark      string `json:"mark,omitempty"`
    96  	Reason    string `json:"reason,omitempty"`
    97  
    98  	Source   uint16 `json:"source"`
    99  	Bytes    uint32 `json:"bytes"`
   100  	SrcLabel uint32 `json:"srcLabel"`
   101  	DstLabel uint32 `json:"dstLabel"`
   102  	DstID    uint32 `json:"dstID"`
   103  
   104  	Summary *DissectSummary `json:"summary,omitempty"`
   105  }
   106  
   107  //DropNotifyToVerbose creates verbose notification from DropNotify
   108  func DropNotifyToVerbose(n *DropNotify) DropNotifyVerbose {
   109  	return DropNotifyVerbose{
   110  		Type:     "drop",
   111  		Mark:     fmt.Sprintf("%#x", n.Hash),
   112  		Reason:   api.DropReason(n.SubType),
   113  		Source:   n.Source,
   114  		Bytes:    n.OrigLen,
   115  		SrcLabel: n.SrcLabel,
   116  		DstLabel: n.DstLabel,
   117  		DstID:    n.DstID,
   118  	}
   119  }