cilium/cilium · error

adding key type: %w

Error message

adding key type: %w

What it means

addMapKV registers a map's BTF key and value types with a btf.Builder used by dpgen's -maps/-types commands. This error wraps a failure while adding the key type (spec.Key) to the builder — e.g. the BTF type could not be resolved or encoded. Callers runMaps and runTypes further wrap it with the map name.

Source

Thrown at tools/dpgen/util.go:211

	}

	if a.ValueSize != b.ValueSize {
		return fmt.Errorf("value size mismatch: %d != %d", a.ValueSize, b.ValueSize)
	}

	if a.MaxEntries != b.MaxEntries {
		return fmt.Errorf("max entries mismatch: %d != %d", a.MaxEntries, b.MaxEntries)
	}

	return nil
}

// addMapKV adds the BTF types of the map's key and value to the given BTF
// builder.
func addMapKV(bb *btf.Builder, added *set.Set[string], spec *ebpf.MapSpec) error {
	if spec.Key != nil {
		if err := addType(bb, added, spec.Key); err != nil {
			return fmt.Errorf("adding key type: %w", err)
		}
	}
	if spec.Value != nil {
		if err := addType(bb, added, spec.Value); err != nil {
			return fmt.Errorf("adding value type: %w", err)
		}
	}
	return nil
}

// addVariableType adds the BTF type of the variable to the given BTF builder.
func addVariableType(bb *btf.Builder, added *set.Set[string], v *ebpf.VariableSpec) error {
	if v.Type == nil || v.Type.Type == nil {
		return fmt.Errorf("no BTF type information")
	}

	return addType(bb, added, v.Type.Type)
}

View on GitHub (pinned to ac7b90affa)

Solutions

  1. Recompile the BPF objects with BTF enabled (clang -g / -target bpf with BTF, not stripped) and rerun dpgen.
  2. Inspect the wrapped inner error (strip this frame via errors.Unwrap) to see which BTF construct failed; adjust the C type or upgrade cilium/ebpf.
  3. Check for clang/LLVM version incompatibilities and build with the toolchain version cilium requires.
  4. Verify the object with `bpftool btf dump file <obj>` to confirm key BTF is present and valid.

Example fix

// before: compiling without BTF
clang -target bpf -c prog.c -o prog.o
// after
clang -target bpf -g -c prog.c -o prog.o
Defensive patterns

Strategy: validation

Validate before calling

f, err := os.Open(objPath)
if err != nil { return err }
rd, err := elf.NewFile(f)
if err != nil { return err }
if rd.Section(".BTF") == nil {
    return fmt.Errorf("%s has no .BTF section; rebuild with clang -g", objPath)
}

Try / catch

if err := runTypes(cmd, args); err != nil {
    if strings.Contains(err.Error(), "adding key type:") {
        // verify object BTF with bpftool btf dump and rebuild with -g
    }
}

Prevention

When it happens

Trigger: Running `dpgen -types` or `dpgen -maps` over ELF objects whose map has a Key BTF type that the builder cannot add (missing/corrupt BTF in the object, unsupported type constructs, deduplication conflicts).

Common situations: Objects compiled without BTF or with stripped BTF sections; clang versions producing BTF constructs the cilium/ebpf library can't parse; duplicate type names across objects clashing under the deduplicating builder.

Related errors


AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31). Data as JSON: /api/errors/d4ce8884833d2901. Report an issue: GitHub.