-
Notifications
You must be signed in to change notification settings - Fork 770
add StructOpsMap support #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shun159
wants to merge
34
commits into
cilium:main
Choose a base branch
from
shun159:feature/struct-ops-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−14
Open
add StructOpsMap support #1845
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d4526fd
struct_ops: add structOpsMeta to carry BTF hints for StructOpsMap cre…
shun159 142a1aa
struct_ops: drop unused members for now
shun159 94d1686
struct_ops: Add struct_ops loader
shun159 daaf51d
struct_ops: implement preLoad()
shun159 da8c896
map: fix log message
shun159 93366b1
struct_ops: implement onProgramLoaded() and postLoad()
shun159 d0f70d2
testing: remove struct_ops dummy helpers
shun159 9bec0f0
struct_ops: fix to remove unused struct members
shun159 5334dab
struct_ops: move some struct_ops logics to collection
shun159 b6bcad6
struct_ops: moved postLoad() logic into populateDeferredMaps()
shun159 6f726b3
struct_ops: move initKernStructOps() to NewCollectionWithOptions()
shun159 ba767e9
testing: fix to skip sturct_ops test when bpf_testmod_ops is not loaded
shun159 23b95ca
struct_ops: add code comments
shun159 9736468
struct_ops: switch to AttachTo for program binding
shun159 4aa4d4b
remove debug message
shun159 ee3da8d
remove unused function
shun159 2528247
struct_ops: refactor kern_vdata population
shun159 877aac6
struct_ops: remove unused struct type
shun159 ad68628
struct_ops: use value type as MapSpec.Value and drop unused helpers
shun159 e8839fb
struct_ops: use value type as MapSpec.Value and drop unused helpers (2)
shun159 4adb3ac
struct_ops: remove necessary function and struct type
shun159 3040131
struct_ops: remove an unecessary struct field
shun159 fbc36d3
struct_ops: remove ad-hoc helpers and use findTargetInKernel
shun159 c264d20
struct_ops: make anonymous an unused variable
shun159 bc72363
struct_ops: we don't use MapSpec.Contents in struct_ops
shun159 c1aca89
struct_ops: remove AttachTo guard
shun159 f5f64b2
struct_ops: fix to use cached BTF for find struct_ops struct
shun159 3837f4b
prog: fix to ignore AttachTo string which is not matched with the
shun159 1c82f84
struct_ops: remove an unused helper
shun159 4d8b42c
collection: remove an unnecessary initializer
shun159 7989861
struct_ops: simplify inner struct handling, and some tweaks
shun159 e7994d6
struct_ops: rename populateFuncPtr to populateStructOpsProgram
shun159 0d78137
sturct_ops: fix to reuse BTF handle resolved by MarshalMapKV
shun159 cfeae69
struct_ops: add requireTestmodOps
shun159 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -334,7 +334,7 @@ func NewMap(spec *MapSpec) (*Map, error) { | |
// | ||
// May return an error wrapping ErrMapIncompatible. | ||
func NewMapWithOptions(spec *MapSpec, opts MapOptions) (*Map, error) { | ||
m, err := newMapWithOptions(spec, opts) | ||
m, err := newMapWithOptions(spec, opts, btf.NewCache()) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating map: %w", err) | ||
} | ||
|
@@ -347,7 +347,7 @@ func NewMapWithOptions(spec *MapSpec, opts MapOptions) (*Map, error) { | |
return m, nil | ||
} | ||
|
||
func newMapWithOptions(spec *MapSpec, opts MapOptions) (_ *Map, err error) { | ||
func newMapWithOptions(spec *MapSpec, opts MapOptions, c *btf.Cache) (_ *Map, err error) { | ||
closeOnError := func(c io.Closer) { | ||
if err != nil { | ||
c.Close() | ||
|
@@ -397,7 +397,7 @@ func newMapWithOptions(spec *MapSpec, opts MapOptions) (_ *Map, err error) { | |
return nil, errors.New("inner maps cannot be pinned") | ||
} | ||
|
||
template, err := spec.InnerMap.createMap(nil) | ||
template, err := spec.InnerMap.createMap(nil, c) | ||
if err != nil { | ||
return nil, fmt.Errorf("inner map: %w", err) | ||
} | ||
|
@@ -409,7 +409,7 @@ func newMapWithOptions(spec *MapSpec, opts MapOptions) (_ *Map, err error) { | |
innerFd = template.fd | ||
} | ||
|
||
m, err := spec.createMap(innerFd) | ||
m, err := spec.createMap(innerFd, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -504,7 +504,7 @@ func (m *Map) memorySize() (int, error) { | |
|
||
// createMap validates the spec's properties and creates the map in the kernel | ||
// using the given opts. It does not populate or freeze the map. | ||
func (spec *MapSpec) createMap(inner *sys.FD) (_ *Map, err error) { | ||
func (spec *MapSpec) createMap(inner *sys.FD, c *btf.Cache) (_ *Map, err error) { | ||
closeOnError := func(closer io.Closer) { | ||
if err != nil { | ||
closer.Close() | ||
|
@@ -551,14 +551,59 @@ func (spec *MapSpec) createMap(inner *sys.FD) (_ *Map, err error) { | |
if err != nil && !errors.Is(err, btf.ErrNotSupported) { | ||
return nil, fmt.Errorf("load BTF: %w", err) | ||
} | ||
defer handle.Close() | ||
|
||
if handle != nil { | ||
defer handle.Close() | ||
if spec.Type == StructOpsMap { | ||
if spec.Value == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
return nil, fmt.Errorf("struct_ops map: missing value type information") | ||
} | ||
|
||
valueType, ok := btf.As[*btf.Struct](spec.Value) | ||
if !ok { | ||
return nil, fmt.Errorf("value must be Struct type") | ||
} | ||
|
||
if spec.KeySize != 4 { | ||
return nil, fmt.Errorf("struct_ops: KeySize must be 4") | ||
} | ||
|
||
// struct_ops: resolve value type ("bpf_struct_ops_<name>") and | ||
// record kernel-specific BTF IDs / FDs needed for map creation. | ||
target := btf.Type((*btf.Struct)(nil)) | ||
s, module, err := findTargetInKernel(valueType.Name, &target, c) | ||
if err != nil { | ||
return nil, fmt.Errorf("lookup value type %q: %w", valueType.Name, err) | ||
} | ||
defer module.Close() | ||
|
||
vType := target.(*btf.Struct) | ||
|
||
// Use BTF k/v during map creation. | ||
btfValueTypeId, err := s.TypeID(vType) | ||
if err != nil { | ||
return nil, fmt.Errorf("lookup type_id: %w", err) | ||
} | ||
|
||
attr.ValueSize = spec.ValueSize | ||
attr.BtfVmlinuxValueTypeId = btfValueTypeId | ||
|
||
if handle == nil { | ||
return nil, fmt.Errorf("struct_ops: BTF handle is not resolved") | ||
} | ||
attr.BtfFd = uint32(handle.FD()) | ||
attr.BtfKeyTypeId = keyTypeID | ||
attr.BtfValueTypeId = valueTypeID | ||
|
||
if module != nil { | ||
// BPF_F_VTYPE_BTF_OBJ_FD is required if the type comes from a module | ||
attr.MapFlags |= sys.BPF_F_VTYPE_BTF_OBJ_FD | ||
// set FD for the kernel module | ||
attr.ValueTypeBtfObjFd = int32(module.FD()) | ||
} | ||
} else { | ||
if handle != nil { | ||
// Use BTF k/v during map creation. | ||
attr.BtfFd = uint32(handle.FD()) | ||
attr.BtfKeyTypeId = keyTypeID | ||
attr.BtfValueTypeId = valueTypeID | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.