github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/kernelsupport/map.go (about) 1 package kernelsupport 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // MapSupport is a flagset that describes which map types are supported 9 type MapSupport uint64 10 11 // TODO add comments 12 const ( 13 // KFeatMapHash means the kernel supports Hash maps 14 KFeatMapHash MapSupport = 1 << iota 15 KFeatMapArray 16 KFeatMapTailCall 17 KFeatMapPerfEvent 18 KFeatMapPerCPUHash 19 KFeatMapPerCPUArray 20 KFeatMapStackTrace 21 KFeatMapCGroupArray 22 KFeatMapLRUHash 23 KFeatMapLRUPerCPUHash 24 KFeatMapLPMTrie 25 // KFeatMapLPMTrieNextKey indicates that the MapGetNextKey command is for the LPM trie map type is implemented 26 // by the kernel. 27 KFeatMapLPMTrieNextKey 28 KFeatMapArrayOfMaps 29 KFeatMapHashOfMaps 30 KFeatMapNetdevArray 31 KFeatMapSocketArray 32 KFeatMapCPU 33 KFeatMapAFXDP 34 KFeatMapSocketHash 35 KFeatMapCGroupStorage 36 KFeatMapReuseportSocketArray 37 KFeatMapPerCPUCGroupStorage 38 KFeatMapQueue 39 KFeatMapStack 40 KFeatMapSocketLocalStorage 41 KFeatMapNetdevHash 42 KFeatMapStructOps 43 KFeatMapRingBuffer 44 KFeatMapINodeStorage 45 KFeatMapTaskStorage 46 // KFeatMapPerCPUArrayBatchOps means batch operations are supported on Per CPU array maps 47 KFeatMapPerCPUArrayBatchOps 48 // KFeatMapLPMTrieBatchOps means batch operations are supported on LPM trie's 49 KFeatMapLPMTrieBatchOps 50 // KFeatMapDynamicInnerMap means that inner-maps of map-in-map types can have dynamic size 51 KFeatMapDynamicInnerMap 52 // An end marker for enumeration, not an actual feature flag 53 kFeatMapMax //nolint:revive // leading k is used to stay consistent with exported vars 54 ) 55 56 // Has returns true if 'ms' has all the specified flags 57 func (ms MapSupport) Has(flags MapSupport) bool { 58 return ms&flags == flags 59 } 60 61 var mapSupportToString = map[MapSupport]string{ 62 KFeatMapHash: "Hash", 63 KFeatMapArray: "Array", 64 KFeatMapTailCall: "Tail call", 65 KFeatMapPerfEvent: "Perf event", 66 KFeatMapPerCPUHash: "Per CPU hash", 67 KFeatMapPerCPUArray: "Per CPU array", 68 KFeatMapStackTrace: "Stack trace", 69 KFeatMapCGroupArray: "CGroup array", 70 KFeatMapLRUHash: "LRU hash", 71 KFeatMapLRUPerCPUHash: "LRU per CPU hash", 72 KFeatMapLPMTrie: "LPM trie", 73 KFeatMapLPMTrieNextKey: "LPM trie next key", 74 KFeatMapArrayOfMaps: "Array of maps", 75 KFeatMapHashOfMaps: "Hash of maps", 76 KFeatMapNetdevArray: "Netdev array", 77 KFeatMapSocketArray: "Socket array", 78 KFeatMapCPU: "CPU", 79 KFeatMapAFXDP: "AF_XDP", 80 KFeatMapSocketHash: "Socket hash", 81 KFeatMapCGroupStorage: "CGroup storage", 82 KFeatMapReuseportSocketArray: "Reuseport socket array", 83 KFeatMapPerCPUCGroupStorage: "Per CPU cgroup storage", 84 KFeatMapQueue: "Queue", 85 KFeatMapStack: "Stack", 86 KFeatMapSocketLocalStorage: "Socket local storage", 87 KFeatMapNetdevHash: "Netdev hash", 88 KFeatMapStructOps: "Struct ops", 89 KFeatMapRingBuffer: "Ring buffer", 90 KFeatMapINodeStorage: "INode storage", 91 KFeatMapTaskStorage: "Task storage", 92 KFeatMapPerCPUArrayBatchOps: "Per CPU array batch operations", 93 KFeatMapLPMTrieBatchOps: "LPM Trie batch operations", 94 } 95 96 func (ms MapSupport) String() string { 97 var maps []string 98 for i := MapSupport(1); i < kFeatMapMax; i = i << 1 { 99 // If this flag is set 100 if ms&i > 0 { 101 mapStr := mapSupportToString[i] 102 if mapStr == "" { 103 mapStr = fmt.Sprintf("missing map str(%d)", i) 104 } 105 maps = append(maps, mapStr) 106 } 107 } 108 109 if len(maps) == 0 { 110 return "No support" 111 } 112 113 if len(maps) == 1 { 114 return maps[0] 115 } 116 117 return strings.Join(maps, ", ") 118 }