MCPcopy Index your code
hub / github.com/cilium/ebpf / createStructOpsMap

Method createStructOpsMap

elf_reader.go:1563–1605  ·  view source on GitHub ↗

createStructOpsMap() creates and registers a MapSpec for a struct_ops

(vsi btf.VarSecinfo, userData []byte, flags uint32)

Source from the content-addressed store, hash-verified

1561
1562// createStructOpsMap() creates and registers a MapSpec for a struct_ops
1563func (ec *elfCode) createStructOpsMap(vsi btf.VarSecinfo, userData []byte, flags uint32) (*btf.Struct, uint32, error) {
1564 varType, ok := btf.As[*btf.Var](vsi.Type)
1565 if !ok {
1566 return nil, 0, fmt.Errorf("vsi: expect var, got %T", vsi.Type)
1567 }
1568
1569 mapName := varType.Name
1570
1571 userSt, ok := btf.As[*btf.Struct](varType.Type)
1572 if !ok {
1573 return nil, 0, fmt.Errorf("var %s: expect struct, got %T", varType.Name, varType.Type)
1574 }
1575
1576 userSize := userSt.Size
1577 baseOff := vsi.Offset
1578 if uint64(baseOff)+uint64(userSize) > uint64(len(userData)) {
1579 return nil, 0, fmt.Errorf("%s exceeds section", mapName)
1580 }
1581
1582 // Register the MapSpec for this struct_ops instance if doesn't exist
1583 if _, exists := ec.maps[mapName]; exists {
1584 return nil, 0, fmt.Errorf("struct_ops map %s: already exists", mapName)
1585 }
1586
1587 ec.maps[mapName] = &MapSpec{
1588 Name: mapName,
1589 Type: StructOpsMap,
1590 Key: &btf.Int{Size: 4},
1591 KeySize: structOpsKeySize,
1592 ValueSize: userSize, // length of the user-struct type
1593 Value: userSt,
1594 Flags: flags,
1595 MaxEntries: 1,
1596 Contents: []MapKV{
1597 {
1598 Key: uint32(0),
1599 Value: append([]byte(nil), userData[baseOff:baseOff+userSize]...),
1600 },
1601 },
1602 }
1603
1604 return userSt, baseOff, nil
1605}
1606
1607type libbpfElfSectionDef struct {
1608 pattern string

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected