Skip to content

Commit

Permalink
program: reuse kconfig package for LINUX_HAS_SYSCALL_WRAPPER
Browse files Browse the repository at this point in the history
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 <lmb@isovalent.com>
  • Loading branch information
lmb committed Nov 7, 2023
1 parent c44a8d2 commit c2563e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
15 changes: 3 additions & 12 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_*.
Expand Down
17 changes: 15 additions & 2 deletions internal/kconfig/kconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit c2563e4

Please sign in to comment.