github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/kernelsupport/attach.go (about)

     1  package kernelsupport
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // AttachSupport is a flagset that describes which attach types are supported
     9  type AttachSupport uint64
    10  
    11  // TODO add comments
    12  const (
    13  	// 3.19 Ingress and Egress
    14  	KFeatAttachINetIngressEgress AttachSupport = 1 << iota
    15  	// 4.10
    16  	KFeatAttachInetSocketCreate
    17  	// 4.13
    18  	KFeatAttachSocketOps
    19  	// 4.14 stream parser and stream verdict
    20  	KFeatAttachStreamParserVerdict
    21  	// 4.15
    22  	KFeatAttachCGroupDevice
    23  	// 4.17
    24  	KFeatAttachSKMsgVerdict
    25  	// 4.17 inet4 + inet6
    26  	KFeatAttachCGroupInetBind
    27  	// 4.17 inet4 + inet6
    28  	KFeatAttachCGroupInetConnect
    29  	// 4.17 inet4 + inet6
    30  	KFeatAttachCGroupInetPostBind
    31  	// 4.18 udp4 + udp6
    32  	KFeatAttachCGroupUDPSendMsg
    33  	// 4.18
    34  	KFeatAttachLIRCMode2
    35  	// 4.20
    36  	KFeatAttachFlowDissector
    37  	// 5.2
    38  	KFeatAttachCGroupSysctl
    39  	// 5.2 udp4 + udp6
    40  	KFeatAttachCGroupUDPRecvMsg
    41  	// 5.3 CGroupGetSocket + CGroupSetSocket
    42  	KFeatAttachCGroupGetSetSocket
    43  	// 5.5
    44  	KFeatAttachTraceRawTP
    45  	// 5.5
    46  	KFeatAttachTraceFentry
    47  	// 5.5
    48  	KFeatAttachTraceFExit
    49  	// 5.7
    50  	KFeatAttachModifyReturn
    51  	// 5.7
    52  	KFeatAttachLSMMAC
    53  	// 5.8
    54  	KFeatAttachTraceIter
    55  	// 5.8 inet4 + inet6
    56  	KFeatAttachCGroupINetGetPeerName
    57  	// 5.8 inet4 + inet6
    58  	KFeatAttachCGroupINetGetSocketName
    59  	// 5.8
    60  	KFeatAttachXDPDevMap
    61  	// 5.9
    62  	KFeatAttachCGroupInetSocketRelease
    63  	// 5.9
    64  	KFeatAttachXDPCPUMap
    65  	// 5.9
    66  	KFeatAttachSKLookup
    67  	// 5.9
    68  	KFeatAttachXDP
    69  
    70  	// An end marker for enumeration, not an actual feature flag
    71  	kFeatAttachMax //nolint:revive // leading k is used to stay consistent with exported vars
    72  )
    73  
    74  // Has returns true if 'as' has all the specified flags
    75  func (as AttachSupport) Has(flags AttachSupport) bool {
    76  	return as&flags == flags
    77  }
    78  
    79  var attachSupportToString = map[AttachSupport]string{
    80  	KFeatAttachINetIngressEgress:       "INet ingress/egress",
    81  	KFeatAttachInetSocketCreate:        "INet socket create",
    82  	KFeatAttachSocketOps:               "Socket operations",
    83  	KFeatAttachStreamParserVerdict:     "Stream parser/verdict",
    84  	KFeatAttachCGroupDevice:            "CGroup device",
    85  	KFeatAttachSKMsgVerdict:            "SK message verdict",
    86  	KFeatAttachCGroupInetBind:          "CGroup inet bind",
    87  	KFeatAttachCGroupInetConnect:       "CGroup inet connect",
    88  	KFeatAttachCGroupInetPostBind:      "CGroup inet post bind",
    89  	KFeatAttachCGroupUDPSendMsg:        "CGroup UDP send message",
    90  	KFeatAttachLIRCMode2:               "LIRC mode2",
    91  	KFeatAttachFlowDissector:           "Flow dissector",
    92  	KFeatAttachCGroupSysctl:            "CGroup sysctl",
    93  	KFeatAttachCGroupUDPRecvMsg:        "CGroup UDP receive message",
    94  	KFeatAttachCGroupGetSetSocket:      "CGroup get/set socket",
    95  	KFeatAttachTraceRawTP:              "Trace raw TP",
    96  	KFeatAttachTraceFentry:             "Trace fentry",
    97  	KFeatAttachTraceFExit:              "Trace fexit",
    98  	KFeatAttachModifyReturn:            "Modify return",
    99  	KFeatAttachLSMMAC:                  "LSM MAC",
   100  	KFeatAttachTraceIter:               "Trace iterator",
   101  	KFeatAttachCGroupINetGetPeerName:   "CGroup inet get peer name",
   102  	KFeatAttachCGroupINetGetSocketName: "CGroup inet get socket name",
   103  	KFeatAttachXDPDevMap:               "XDP device map",
   104  	KFeatAttachCGroupInetSocketRelease: "CGroup inet socket release",
   105  	KFeatAttachXDPCPUMap:               "XDP CPU map",
   106  	KFeatAttachSKLookup:                "SK lookup",
   107  	KFeatAttachXDP:                     "XDP",
   108  }
   109  
   110  func (as AttachSupport) String() string {
   111  	var attachTypes []string
   112  	for i := AttachSupport(1); i < kFeatAttachMax; i = i << 1 {
   113  		// If this flag is set
   114  		if as&i > 0 {
   115  			attachStr := attachSupportToString[i]
   116  			if attachStr == "" {
   117  				attachStr = fmt.Sprintf("missing attach str(%d)", i)
   118  			}
   119  			attachTypes = append(attachTypes, attachStr)
   120  		}
   121  	}
   122  
   123  	if len(attachTypes) == 0 {
   124  		return "No support"
   125  	}
   126  
   127  	if len(attachTypes) == 1 {
   128  		return attachTypes[0]
   129  	}
   130  
   131  	return strings.Join(attachTypes, ", ")
   132  }