-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/bpf: move maps memlock retrieval from bugtool to bpf
These operations might eventually be taken care of by cilium/ebpf directly and we can remove this. I moved this to the bpf package as it made little sense to import pkg/bugtool to just perform those operations on maps. Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
- Loading branch information
Showing
3 changed files
with
119 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package bpf | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cilium/ebpf" | ||
) | ||
|
||
type ExtendedMapInfo struct { | ||
ebpf.MapInfo | ||
Memlock int | ||
} | ||
|
||
func ExtendedInfoFromMapID(id int) (ExtendedMapInfo, error) { | ||
m, err := ebpf.NewMapFromID(ebpf.MapID(id)) | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed creating a map FD from ID: %w", err) | ||
} | ||
defer m.Close() | ||
|
||
xInfo, err := ExtendedInfoFromMap(m) | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed to retrieve extended info from map %v: %w", m, err) | ||
} | ||
|
||
return xInfo, nil | ||
} | ||
|
||
func ExtendedInfoFromMap(m *ebpf.Map) (ExtendedMapInfo, error) { | ||
info, err := m.Info() | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed to retrieve map info: %w", err) | ||
} | ||
|
||
memlock, err := ParseMemlockFromFDInfo(m.FD()) | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed to parse memlock from fd (%d) info: %w", m.FD(), err) | ||
} | ||
return ExtendedMapInfo{ | ||
MapInfo: *info, | ||
Memlock: memlock, | ||
}, nil | ||
} | ||
|
||
func MemlockInfoFromMapID(id int) (ExtendedMapInfo, error) { | ||
m, err := ebpf.NewMapFromID(ebpf.MapID(id)) | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed creating a map FD from ID: %w", err) | ||
} | ||
defer m.Close() | ||
memlock, err := ParseMemlockFromFDInfo(m.FD()) | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed parsing fdinfo for memlock: %w", err) | ||
} | ||
info, err := m.Info() | ||
if err != nil { | ||
return ExtendedMapInfo{}, fmt.Errorf("failed retrieving info from map: %w", err) | ||
} | ||
|
||
return ExtendedMapInfo{ | ||
MapInfo: *info, | ||
Memlock: memlock, | ||
}, nil | ||
} | ||
|
||
func ParseMemlockFromFDInfo(fd int) (int, error) { | ||
path := fmt.Sprintf("/proc/self/fdinfo/%d", fd) | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to open file %q: %w", path, err) | ||
} | ||
defer file.Close() | ||
return parseMemlockFromFDInfoReader(file) | ||
} | ||
|
||
func parseMemlockFromFDInfoReader(r io.Reader) (int, error) { | ||
scanner := bufio.NewScanner(r) | ||
for scanner.Scan() { | ||
fields := strings.Fields(scanner.Text()) | ||
if len(fields) > 1 && fields[0] == "memlock:" { | ||
memlock, err := strconv.Atoi(fields[1]) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed converting memlock to int: %w", err) | ||
} | ||
return memlock, nil | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return 0, fmt.Errorf("failed to scan: %w", err) | ||
} | ||
return 0, fmt.Errorf("didn't find memlock field") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters