Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface IMap<K, V>

Generic interface to manipulate an eBPF map of any type. API-compatible with JavaScript Map.

In an eBPF map, keys and values are binary strings of a fixed length, defined at map creation (see MapDef). RawMap returns these directly as Buffer with no conversion, and ConvMap performs the conversion specified at construction time.

Operation semantics

The exact semantics of the operations (whether they're atomic or not, supported flags, etc.) depend on the chosen map type.

If your kernel doesn't implement a map operation, it will generally throw EINVAL. If it's implemented, but the map type doesn't have it, it will generally throw ENOTSUPP.

Convenience methods

Some methods, such as clear, are implemented using one or more map operations. These are marked as convenience methods in their documentation.

Notably, iterating methods such as entries, consumeEntries, values and also clear are all convenience methods, implemented using a combination of keys and another operation.

Batched operations

These are special map operations that process many entries. setBatch and deleteBatch are mostly equivalent to a repeated set or delete on each of the entries. getBatch iterates through the entries of the map and is mostly equivalent to entries.

The difference is that all this is performed in the kernel through a single syscall, so they perform better than normal operations or iterating methods (especially when syscall overhead is a problem).

However they're much more recent and, like other operations, may not be available on your map type or kernel version.

Type parameters

  • K

  • V

Hierarchy

  • IMap

Implemented by

Index

Batched operations Methods

  • deleteBatch(keys: K[]): void
  • Perform delete operation on the passed entries.

    Since Linux 5.6.

    Unlike in delete, an entry isn't found, ENOENT will be thrown and no more entries will be processed.

    Note that if an error is thrown, part of the entries could already have been processed. The thrown error includes a count field that, if not undefined, corresponds to the amount of processed entries.

    Parameters

    • keys: K[]

      Entry keys to delete

    Returns void

  • getBatch(batchSize: number, flags?: number): IterableIterator<[K, V][]>
  • Iterate through the map entries.

    This works like entries but the iteration is performed in the kernel, returning many items at once. The interator yields each batch produced by the kernel, until an error is found or there are no more entries.

    batchSize specifies the requested size, but batches may be smaller. If the kernel returns a partial batch together with an error, the partial batch will be yielded before throwing the error. If the map is empty, nothing is yielded.

    Since Linux 5.6.

    Parameters

    • batchSize: number

      Amount of entries to request per batch, must be non-zero

    • Optional flags: number

      Operation flags, see MapLookupFlags

    Returns IterableIterator<[K, V][]>

  • setBatch(entries: [K, V][], flags?: number): IMap<K, V>
  • Perform set operation on the passed entries.

    Since Linux 5.6.

    Note that if an error is thrown, part of the entries could already have been processed. The thrown error includes a count field that, if not undefined, corresponds to the amount of processed entries.

    Parameters

    • entries: [K, V][]

      Entries to set

    • Optional flags: number

      Operation flags, see MapUpdateFlags

    Returns IMap<K, V>

Convenience Methods

  • [iterator](): IterableIterator<[K, V]>
  • clear(start?: K): void
  • Convenience function. Non-atomically iterates over the map's entries, deleting them.

    Note: For kernels older than 4.12, a start key must be passed. See keys.

    This is a wrapper around keys and delete.

    Parameters

    • Optional start: K

    Returns void

  • consumeEntries(start?: K): IterableIterator<[K, V]>
  • Convenience function. Non-atomically iterates through the map's entries, deleting them while iterating.

    This is a wrapper around keys and getDelete.

    Parameters

    • Optional start: K

    Returns IterableIterator<[K, V]>

  • entries(start?: K): IterableIterator<[K, V]>
  • Convenience function. Non-atomically iterates through the map's entries. Gets the next key before yielding the current one, making it suitable for deleting entries while iterating.

    Note: For kernels older than 4.12, a start key must be passed. See keys.

    This is a wrapper around keys and get.

    Parameters

    • Optional start: K

    Returns IterableIterator<[K, V]>

  • has(key: K): boolean
  • Convenience function. Tests if the map has an entry.

    Parameters

    • key: K

      Entry key

    Returns boolean

  • values(start?: K): IterableIterator<V>
  • Convenience function. Non-atomically iterates through the map's values.

    Note: For kernels older than 4.12, a start key must be passed. See keys.

    This is a wrapper around keys and get.

    Parameters

    • Optional start: K

    Returns IterableIterator<V>

Operations Methods

  • delete(key: K): boolean
  • Delete a single map entry.

    Returns true if an entry was found and deleted, false otherwise.

    Parameters

    • key: K

      Entry key

    Returns boolean

  • freeze(): void
  • Freezes the map, making it non-modifiable from userspace. The map stays writeable from BPF side.

    Since Linux 5.2.

    Returns void

  • get(key: K, flags?: number): undefined | V
  • Fetch the value for a single key.

    Parameters

    • key: K

      Entry key

    • Optional flags: number

      Operation flags (since Linux 5.1), see MapLookupFlags

    Returns undefined | V

    Entry value, or undefined if no such entry exists

  • getDelete(key: K): undefined | V
  • Atomically deletes an entry and returns its former value, or undefined if no entry was found. This operation is generally implemented only for stack / queue types.

    Since Linux 4.20. Most map types probably won't implement this operation.

    Parameters

    • key: K

      Entry key

    Returns undefined | V

  • keys(start?: K): Generator<K, undefined, unknown>
  • Non-atomically iterates through the map's keys, starting by the key immediately following the passed one. If no key isn't passed or it doesn't exist, iteration starts by the first key in the map.

    Note: Not passing a key is only supported on kernels 4.12 and above.

    Because this calls BPF_MAP_GET_NEXT_KEY repeatedly, if the map's keys are deleted while it's being iterated (by this or another program), iteration could restart to the beginning. However, this method fetches the next key before yielding the current one, making it safe to delete the current key (and any past ones).

    Keep in mind that the order of keys depends on the type of map, and isn't necessarily guaranteed to be consistent.

    Parameters

    • Optional start: K

      Start key (if passed and found, iteration will yield keys after this one, i.e. it's not included in the result)

    Returns Generator<K, undefined, unknown>

  • set(key: K, value: V, flags?: number): IMap<K, V>
  • Add or override a single map entry.

    Parameters

    • key: K

      Entry key

    • value: V

      Entry value

    • Optional flags: number

      Operation flags (since Linux 3.19), see MapUpdateFlags

    Returns IMap<K, V>

Generated using TypeDoc