MCPcopy
hub / github.com/cilium/ebpf / newMapWithOptions

Function newMapWithOptions

map.go:394–470  ·  view source on GitHub ↗
(spec *MapSpec, opts MapOptions, c *btf.Cache)

Source from the content-addressed store, hash-verified

392}
393
394func newMapWithOptions(spec *MapSpec, opts MapOptions, c *btf.Cache) (_ *Map, err error) {
395 closeOnError := func(c io.Closer) {
396 if err != nil {
397 c.Close()
398 }
399 }
400
401 switch spec.Pinning {
402 case PinByName:
403 if spec.Name == "" {
404 return nil, fmt.Errorf("pin by name: missing Name")
405 }
406
407 if opts.PinPath == "" {
408 return nil, fmt.Errorf("pin by name: missing MapOptions.PinPath")
409 }
410
411 path := filepath.Join(opts.PinPath, spec.Name)
412 m, err := LoadPinnedMap(path, &opts.LoadPinOptions)
413 if errors.Is(err, unix.ENOENT) {
414 break
415 }
416 if err != nil {
417 return nil, fmt.Errorf("load pinned map: %w", err)
418 }
419 defer closeOnError(m)
420
421 if err := spec.Compatible(m); err != nil {
422 return nil, fmt.Errorf("use pinned map %s: %w", spec.Name, err)
423 }
424
425 return m, nil
426
427 case PinNone:
428 // Nothing to do here
429
430 default:
431 return nil, fmt.Errorf("pin type %d: %w", int(spec.Pinning), ErrNotSupported)
432 }
433
434 var innerFd *sys.FD
435 if spec.Type.canStoreMap() {
436 if spec.InnerMap == nil {
437 return nil, fmt.Errorf("%s requires InnerMap", spec.Type)
438 }
439
440 if spec.InnerMap.Pinning != PinNone {
441 return nil, errors.New("inner maps cannot be pinned")
442 }
443
444 template, err := spec.InnerMap.createMap(nil, c)
445 if err != nil {
446 return nil, fmt.Errorf("inner map: %w", err)
447 }
448 defer template.Close()
449
450 // Intentionally skip populating and freezing (finalizing)
451 // the inner map template since it will be removed shortly.

Callers 2

NewMapWithOptionsFunction · 0.85
loadMapMethod · 0.85

Calls 7

LoadPinnedMapFunction · 0.85
canStoreMapMethod · 0.80
createMapMethod · 0.80
CloseMethod · 0.65
PinMethod · 0.65
IsMethod · 0.45
CompatibleMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…