npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bpf

v1.3.1

Published

Bindings for eBPF (libbpf)

Downloads

12

Readme

bpf

Node.js bindings to libbpf. BPF is a Virtual Machine (and associated bytecode definition) embedded in the Linux kernel. This module allows you to:

  • Assemble & compile BPF programs

  • Use the kernel's ebpf interface:

    • Load programs into the kernel
    • Attach them to events
    • Create & manipulate eBPF maps
    • Other: perf events, check feature availability, pin objects to BPFFS, query program bytecode, BTF data, etc.

Status

Not enough functionality for standalone use yet.

There's a generic API to create (createMap) and manipulate (IMap) eBPF maps of any type, including map-in-map support, but only some map types are currently tested. There's no BTF support for now.

It has a raw version (which operates with binary Buffers directly), and a high-level version which uses a conversion supplied by the user.

Apart from the generic API, there's a few other sub-APIs for specific map types, such as IArrayMap for ARRAY maps. These also have raw and high-level versions.

Usage

There's prebuilds for x86, x64, arm32v7 and arm64v8, so you don't need anything in those cases.

For other archs, libbpf and its dependencies are included so you only need a compiler:

sudo apt install build-essential

Then, install this module:

npm install bpf

libbpf is kernel agnostic, but not all of the exposed features may be supported by the running kernel, even if they are present in the API and typings. This extends to everything (functions, flags, map types, ...). For some notable cases, we make a mention in the docs, but for a full reference of features and their required kernel versions, go here. If a feature isn't supported by your kernel and you try to use it, you'll likely get an EINVAL error.

Examples

Raw map operations

const bpf = require('bpf')

// Create a HASH map, with 4-byte keys and values
const ref = bpf.createMap({
  type: bpf.MapType.HASH,
  keySize: 4, valueSize: 4,
  maxEntries: 10
})

// Wrap it in `RawMap` to operate with it
const map = new bpf.RawMap(ref)

// Add some entries
const key1 = Buffer.of(0,0,0,1), key2 = Buffer.of(0,0,9,2)
map.set(key1, Buffer.from('abcd'))
map.set(key2, Buffer.from('w00t'))

// Print entries (warning: HASH has no order)
for (const [k, v] of map)
  console.log('entry', k.toString('hex'), '=', v.toString('ascii'))
// entry 00000902 = w00t
// entry 00000001 = abcd

map.get(key2) // -> <Buffer 77 30 30 74>
map.delete(key2) // -> true
map.get(key2) // -> undefined

Using conversions

// We'll have NUL-terminated strings as keys, and uint32 values
const ref = bpf.createMap({
  type: bpf.MapType.HASH,
  keySize: 16, valueSize: 4,
  maxEntries: 7
})
// bpf already provides a conversion for uint32, for convenience
// So we only need to write one for the keys
const stringConversion = {
  parse: (buf) => {
    const size = buf.indexOf(0)
    return buf.slice(0, size === -1 ? buf.length : size).toString()
  },
  format: (buf, x) => {
    buf.fill(0)
    buf.write(x)
  }
}
const map = new bpf.ConvMap(ref, stringConversion, bpf.u32type)

map.set('a cat', 1)
map.set('foo', 3458)
map.set('test', 5)

[...map] // -> [ ['foo', 3458], ['test', 5], ['a cat', 1] ]

Array maps

const doubleConversion = {
  parse: (buf) => buf.readDoubleLE(),
  format: (buf, x) => buf.writeDoubleLE(x),
}

const ref = bpf.createMap({
  type: bpf.MapType.ARRAY,
  keySize: 4, // always 4
  valueSize: 8,
  maxEntries: 5 // array length
})
const array = new bpf.ConvArrayMap(ref, doubleConversion)

// Or equivalently...
const array2 = bpf.createArrayMap(5, 8, doubleConversion)

// Values are initialized to zero
[...array] // -> [ 0, 0, 0, 0, 0 ]

array.set(3, 106.5)
[...array] // -> [ 0, 0, 0, 106.5, 0 ]

Using your own FD

const fd = getMapFDFromSomewhereElse()

// By default it duplicates the FD, leaving the original FD unaffected
const ref = bpf.createMapRef(fd)
ref.fd === fd  // -> false

// Use the `transfer` option if you want bpf to take its ownership
const ref2 = bpf.createMapRef(fd, { transfer: true })
ref2.fd === fd  // -> true

const map = new RawMap(ref)
// ...

Map-in-map

// our values will be ARRAY maps
const innerParams = {
  type: bpf.MapType.ARRAY,
  keySize: 4, valueSize: 4,
  maxEntries: 5
}
const createInnerMap = () =>
  new bpf.ConvArrayMap(bpf.createMap(innerParams), bpf.u32type)

// create an ARRAY_OF_MAPS, passing the inner parameters
const ref = bpf.createMap({
  type: bpf.MapType.ARRAY_OF_MAPS,
  keySize: 4, valueSize: 4,
  maxEntries: 7,
  innerMap: innerParams
})
const map = new bpf.ConvMap(ref, bpf.u32type, bpf.u32type)

// the map is initially empty
[...map] // -> []

// to set an entry, pass the FD of a suitable map
const value1 = createInnerMap()
map.set(2, value1.ref.fd)

// when getting, the *ID* will be returned instead
map.get(2) === value1.ref.id  // -> true