From c2563e4e6981ff1c7247582d7f1970cc733338b6 Mon Sep 17 00:00:00 2001 From: Lorenz Bauer Date: Tue, 7 Nov 2023 11:02:36 +0000 Subject: [PATCH] program: reuse kconfig package for LINUX_HAS_SYSCALL_WRAPPER Export a function from the kconfig package which allows writing an integer into a slice based on a btf.Int. Signed-off-by: Lorenz Bauer --- collection.go | 15 +++------------ internal/kconfig/kconfig.go | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/collection.go b/collection.go index 432eb3f61..a5532220f 100644 --- a/collection.go +++ b/collection.go @@ -630,24 +630,15 @@ func resolveKconfig(m *MapSpec) error { if !ok { return fmt.Errorf("variable %s must be an integer, got %s", n, v.Type) } - var value uint8 = 1 + var value uint64 = 1 if err := haveSyscallWrapper(); errors.Is(err, ErrNotSupported) { value = 0 } else if err != nil { return fmt.Errorf("unable to derive a value for LINUX_HAS_SYSCALL_WRAPPER: %w", err) } - switch integer.Size { - case 1: - data[vsi.Offset] = value - case 2: - internal.NativeEndian.PutUint16(data[vsi.Offset:], uint16(value)) - case 4: - internal.NativeEndian.PutUint32(data[vsi.Offset:], uint32(value)) - case 8: - internal.NativeEndian.PutUint64(data[vsi.Offset:], uint64(value)) - default: - return fmt.Errorf("variable %s must be a 8, 16, 32, or 64 bits integer, got %s", n, v.Type) + if err := kconfig.PutInteger(data[vsi.Offset:], integer, value); err != nil { + return fmt.Errorf("set LINUX_HAS_SYSCALL_WRAPPER: %w", err) } default: // Catch CONFIG_*. diff --git a/internal/kconfig/kconfig.go b/internal/kconfig/kconfig.go index d95e7eb0e..fa5308578 100644 --- a/internal/kconfig/kconfig.go +++ b/internal/kconfig/kconfig.go @@ -250,7 +250,20 @@ func putValueNumber(data []byte, typ btf.Type, value string) error { return fmt.Errorf("cannot parse value: %w", err) } - switch size { + return PutInteger(data, integer, n) +} + +// PutInteger writes n into data. +// +// integer determines how much is written into data and what the valid values +// are. +func PutInteger(data []byte, integer *btf.Int, n uint64) error { + // This function should match set_kcfg_value_num in libbpf. + if integer.Encoding == btf.Bool && n > 1 { + return fmt.Errorf("invalid boolean value: %d", n) + } + + switch integer.Size { case 1: data[0] = byte(n) case 2: @@ -260,7 +273,7 @@ func putValueNumber(data []byte, typ btf.Type, value string) error { case 8: internal.NativeEndian.PutUint64(data, uint64(n)) default: - return fmt.Errorf("size (%d) is not valid, expected: 1, 2, 4 or 8", size) + return fmt.Errorf("size (%d) is not valid, expected: 1, 2, 4 or 8", integer.Size) } return nil