Documentation
¶
Overview ¶
Package ouroboros provides a Go SDK for managing multiple eBPF programs.
This package allows you to programmatically interact with Ouroboros projects, manage eBPF programs, and perform operations like building, loading, and attaching programs without using the CLI.
Example usage:
import "github.com/PacketStream-LLC/ouroboros/pkg/ouroboros"
// Load an existing project
o, err := ouroboros.New()
if err != nil {
log.Fatal(err)
}
// Add a new program
prog := ouroboros.Program{
Name: "myprogram",
}
if err := o.AddProgram(prog); err != nil {
log.Fatal(err)
}
// Save configuration
if err := o.SaveConfig(); err != nil {
log.Fatal(err)
}
Index ¶
- func CalculateMapMemlock(mapInfo *MapInfo) uint64
- func MapTypeToString(mapType ebpf.MapType) string
- type AttachMode
- type AttachOptions
- type AttachedProgram
- type BuildOptions
- type Config
- type LoadOptions
- type LoadedProgram
- type MapAnalysis
- type MapInfo
- type Ouroboros
- func (o *Ouroboros) AddNewProgram(progName string, mainCTemplate string) error
- func (o *Ouroboros) AddProgram(prog Program) error
- func (o *Ouroboros) AnalyzeMaps() (*MapAnalysis, error)
- func (o *Ouroboros) AttachMainProgram(ifaceName string, opts *AttachOptions) (*AttachedProgram, error)
- func (o *Ouroboros) AttachToInterface(loaded *LoadedProgram, ifaceName string, opts *AttachOptions) (*AttachedProgram, error)
- func (o *Ouroboros) BuildAll(opts *BuildOptions) map[string]error
- func (o *Ouroboros) BuildAllOrFail(opts *BuildOptions) error
- func (o *Ouroboros) BuildProgram(progName string, opts *BuildOptions) error
- func (o *Ouroboros) CleanBuildArtifacts() error
- func (o *Ouroboros) CleanProgram(progName string) error
- func (o *Ouroboros) CompileToLLVMIR(progName string, opts *BuildOptions) error
- func (o *Ouroboros) Config() *Config
- func (o *Ouroboros) CreateProgramFromTemplate(progName string, tmpl string) error
- func (o *Ouroboros) DetachByName(ifaceName string) error
- func (o *Ouroboros) DetachFromInterface(attached *AttachedProgram) error
- func (o *Ouroboros) EnsureGlobalDirectory() error
- func (o *Ouroboros) EnsureProgramDirectory(progName string) error
- func (o *Ouroboros) EnsureTargetDirectory() error
- func (o *Ouroboros) GenerateGitignore() error
- func (o *Ouroboros) GenerateMakefile() error
- func (o *Ouroboros) GenerateProgramsHeader() error
- func (o *Ouroboros) GetAttachedProgramID(ifaceName string) (uint32, error)
- func (o *Ouroboros) GetBpfBaseDir() string
- func (o *Ouroboros) GetCompileArgs() []string
- func (o *Ouroboros) GetLoadedProgram(progName string) (*ebpf.Program, error)
- func (o *Ouroboros) GetMainProgram() *Program
- func (o *Ouroboros) GetMap(mapName string) (*MapInfo, error)
- func (o *Ouroboros) GetMapOptions() ebpf.MapOptions
- func (o *Ouroboros) GetMapsByProgram(progName string) (map[string]*MapInfo, error)
- func (o *Ouroboros) GetNextProgramID() int
- func (o *Ouroboros) GetOuroborosGlobalDir() (string, error)
- func (o *Ouroboros) GetProgram(name string) *Program
- func (o *Ouroboros) GetProgramByID(id int) *Program
- func (o *Ouroboros) GetProgramLLPath(progName string) string
- func (o *Ouroboros) GetProgramMainFile(progName string) string
- func (o *Ouroboros) GetProgramMap() string
- func (o *Ouroboros) GetProgramObjectPath(progName string) string
- func (o *Ouroboros) GetProgramPath(progName string) string
- func (o *Ouroboros) GetProgramSize(progName string) int64
- func (o *Ouroboros) GetProgramsByMap(mapName string) ([]string, error)
- func (o *Ouroboros) GetProjectRoot() (string, error)
- func (o *Ouroboros) GetSrcDir() (string, error)
- func (o *Ouroboros) GetTargetDir() (string, error)
- func (o *Ouroboros) IsAttached(ifaceName string) (bool, error)
- func (o *Ouroboros) IsMapShared(mapName string) (bool, error)
- func (o *Ouroboros) IsProgramBuilt(progName string) bool
- func (o *Ouroboros) IsProgramLoaded(progName string) bool
- func (o *Ouroboros) ListInterfaces() ([]string, error)
- func (o *Ouroboros) ListMaps() (map[string]*MapInfo, error)
- func (o *Ouroboros) ListPrograms() []Program
- func (o *Ouroboros) LoadAllPrograms(opts *LoadOptions) (map[string]*LoadedProgram, map[string]error)
- func (o *Ouroboros) LoadPinnedMap(mapName string) (*ebpf.Map, error)
- func (o *Ouroboros) LoadProgram(progName string, opts *LoadOptions) (*LoadedProgram, error)
- func (o *Ouroboros) LoadProgramMap(recreate bool) (*ebpf.Map, error)
- func (o *Ouroboros) PinProgram(loaded *LoadedProgram) error
- func (o *Ouroboros) RebuildProgram(progName string, opts *BuildOptions) error
- func (o *Ouroboros) ReloadConfig() error
- func (o *Ouroboros) RemoveProgram(name string) error
- func (o *Ouroboros) SaveConfig() error
- func (o *Ouroboros) SetCompileArgs(args []string)
- func (o *Ouroboros) UnloadAllPrograms() map[string]error
- func (o *Ouroboros) UnloadProgram(loaded *LoadedProgram) error
- func (o *Ouroboros) UnpinProgram(progName string) error
- func (o *Ouroboros) UpdateProgramMap(progMap *ebpf.Map, loaded *LoadedProgram) error
- type Program
- type ProgramMetadata
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateMapMemlock ¶
CalculateMapMemlock estimates the memory locked by a map. This is an approximation of kernel memory usage.
func MapTypeToString ¶
MapTypeToString converts a map type to its string representation.
Types ¶
type AttachMode ¶
type AttachMode int
AttachMode specifies how to attach an XDP program.
const ( // AttachModeGeneric uses generic XDP (slower but works everywhere) AttachModeGeneric AttachMode = iota // AttachModeNative uses native XDP (faster, driver support required) AttachModeNative // AttachModeOffload uses hardware offload (fastest, hardware support required) AttachModeOffload )
type AttachOptions ¶
type AttachOptions struct {
// Mode specifies the XDP attach mode
Mode AttachMode
// Replace allows replacing an existing attached program
Replace bool
}
AttachOptions contains options for attaching programs.
type AttachedProgram ¶
type AttachedProgram struct {
Program *LoadedProgram
Interface string
Link link.Link
}
AttachedProgram represents a program attached to an interface.
type BuildOptions ¶
type BuildOptions struct {
// Stdout is where to write build output (optional, defaults to discard)
Stdout io.Writer
// Stderr is where to write build errors (optional, defaults to discard)
Stderr io.Writer
// AdditionalArgs are additional arguments to pass to clang
AdditionalArgs []string
}
BuildOptions contains options for building eBPF programs.
type Config ¶
type Config struct {
Programs []Program `json:"programs"`
CompileArgs []string `json:"compile_args"`
ProgramMap string `json:"program_map,omitempty"`
ProgramPrefix string `json:"program_prefix,omitempty"`
BpfBaseDir string `json:"bpf_base_dir,omitempty"`
}
Config represents the Ouroboros project configuration.
type LoadOptions ¶
type LoadOptions struct {
// PinPath is the directory where programs and maps should be pinned
PinPath string
// ReplaceMaps allows replacing existing pinned maps
ReplaceMaps bool
// RecreateProgramMap automatically recreates program maps if incompatible
RecreateProgramMap bool
}
LoadOptions contains options for loading eBPF programs.
type LoadedProgram ¶
LoadedProgram represents a program that has been loaded into the kernel.
type MapAnalysis ¶
type MapAnalysis struct {
TotalMaps int
UniqueMaps []string
MapsByType map[ebpf.MapType][]string
Dependencies map[string][]string // map name -> programs using it
}
MapAnalysis contains analysis results for maps and their relationships.
type MapInfo ¶
type MapInfo struct {
Name string
Type ebpf.MapType
KeySize uint32
ValueSize uint32
MaxEntries uint32
Programs []string // Programs that use this map
ID uint32 // Kernel ID if loaded
Pinned bool // Whether the map is pinned
}
MapInfo represents an eBPF map with its metadata and relationships.
type Ouroboros ¶
type Ouroboros struct {
// contains filtered or unexported fields
}
Ouroboros represents the main SDK instance for managing eBPF programs. It encapsulates the project configuration and provides methods for program management, building, loading, and other operations.
func InitializeProject ¶
InitializeProject creates a new Ouroboros project structure with the given main program name. It creates the necessary directories (src, target, src/_ouroboros) and a default configuration.
func InitializeProjectWithMap ¶
InitializeProjectWithMap creates a new Ouroboros project structure with custom program map name. It creates the necessary directories (src, target, src/_ouroboros) and a default configuration. If programMapName is empty, no custom program map name will be set.
func New ¶
New creates a new Ouroboros SDK instance by loading the configuration from the current directory. Returns an error if the configuration file doesn't exist or is invalid.
func NewWithConfig ¶
NewWithConfig creates a new Ouroboros SDK instance with the provided configuration. This is useful for creating projects programmatically or for testing.
func (*Ouroboros) AddNewProgram ¶
AddNewProgram is a high-level operation that adds a program with all necessary setup. It creates the program directory, generates the main.c file from the template, assigns an ID, and saves the configuration.
func (*Ouroboros) AddProgram ¶
AddProgram adds a new program to the configuration. If the program ID is 0, it will be automatically assigned. Returns an error if a program with the same name already exists.
func (*Ouroboros) AnalyzeMaps ¶
func (o *Ouroboros) AnalyzeMaps() (*MapAnalysis, error)
AnalyzeMaps performs comprehensive analysis of all maps.
func (*Ouroboros) AttachMainProgram ¶
func (o *Ouroboros) AttachMainProgram(ifaceName string, opts *AttachOptions) (*AttachedProgram, error)
AttachMainProgram is a convenience method to attach the main program to an interface.
func (*Ouroboros) AttachToInterface ¶
func (o *Ouroboros) AttachToInterface(loaded *LoadedProgram, ifaceName string, opts *AttachOptions) (*AttachedProgram, error)
AttachToInterface attaches a loaded XDP program to a network interface. The program must be loaded before calling this method.
func (*Ouroboros) BuildAll ¶
func (o *Ouroboros) BuildAll(opts *BuildOptions) map[string]error
BuildAll compiles all eBPF programs in the project. Returns a map of program names to errors (nil if successful).
func (*Ouroboros) BuildAllOrFail ¶
func (o *Ouroboros) BuildAllOrFail(opts *BuildOptions) error
BuildAllOrFail compiles all eBPF programs and returns an error if any fail.
func (*Ouroboros) BuildProgram ¶
func (o *Ouroboros) BuildProgram(progName string, opts *BuildOptions) error
BuildProgram compiles a single eBPF program. It runs clang with the appropriate flags to compile the program's main.c into an eBPF object file in the target directory.
func (*Ouroboros) CleanBuildArtifacts ¶
CleanBuildArtifacts removes all build artifacts including the target directory and the global ouroboros directory.
func (*Ouroboros) CleanProgram ¶
CleanProgram removes the compiled object for a specific program.
func (*Ouroboros) CompileToLLVMIR ¶
func (o *Ouroboros) CompileToLLVMIR(progName string, opts *BuildOptions) error
CompileToLLVMIR compiles a program to LLVM IR (.ll file) instead of object code. This is useful for analysis and optimization.
func (*Ouroboros) CreateProgramFromTemplate ¶
CreateProgramFromTemplate creates a new program directory and main.c from template. The template should use {{.ProgramName}} for the program name placeholder.
func (*Ouroboros) DetachByName ¶
DetachByName detaches a program from an interface by name. This is useful when you don't have the AttachedProgram handle. This method loads the pinned link and uses it to detach.
func (*Ouroboros) DetachFromInterface ¶
func (o *Ouroboros) DetachFromInterface(attached *AttachedProgram) error
DetachFromInterface detaches a program from an interface.
func (*Ouroboros) EnsureGlobalDirectory ¶
EnsureGlobalDirectory creates the global ouroboros directory if it doesn't exist.
func (*Ouroboros) EnsureProgramDirectory ¶
EnsureProgramDirectory creates a program's source directory if it doesn't exist.
func (*Ouroboros) EnsureTargetDirectory ¶
EnsureTargetDirectory creates the target directory if it doesn't exist.
func (*Ouroboros) GenerateGitignore ¶
GenerateGitignore creates a .gitignore file for the project.
func (*Ouroboros) GenerateMakefile ¶
GenerateMakefile generates a basic Makefile for building the project.
func (*Ouroboros) GenerateProgramsHeader ¶
GenerateProgramsHeader generates the programs.h header file in src/_ouroboros/. This header contains #define constants for each program's ID, making it easy to reference programs in C code (especially for tail calls).
func (*Ouroboros) GetAttachedProgramID ¶
GetAttachedProgramID returns the ID of the program attached to an interface. Returns 0 if no program is attached. Note: This requires additional kernel support and is currently not fully implemented.
func (*Ouroboros) GetBpfBaseDir ¶
GetBpfBaseDir returns the BPF filesystem base directory.
func (*Ouroboros) GetCompileArgs ¶
GetCompileArgs returns the compile arguments from configuration.
func (*Ouroboros) GetLoadedProgram ¶
GetLoadedProgram retrieves a handle to a loaded (pinned) program.
func (*Ouroboros) GetMainProgram ¶
GetMainProgram returns the main program, or nil if no main program is set.
func (*Ouroboros) GetMapOptions ¶
func (o *Ouroboros) GetMapOptions() ebpf.MapOptions
GetMapOptions returns eBPF map options with the configured pin path.
func (*Ouroboros) GetMapsByProgram ¶
GetMapsByProgram returns all maps used by a specific program.
func (*Ouroboros) GetNextProgramID ¶
GetNextProgramID returns the next available program ID.
func (*Ouroboros) GetOuroborosGlobalDir ¶
GetOuroborosGlobalDir returns the absolute path to the _ouroboros directory.
func (*Ouroboros) GetProgram ¶
GetProgram finds a program by name. Returns nil if not found.
func (*Ouroboros) GetProgramByID ¶
GetProgramByID finds a program by ID. Returns nil if not found.
func (*Ouroboros) GetProgramLLPath ¶
GetProgramLLPath returns the path to a program's LLVM IR file.
func (*Ouroboros) GetProgramMainFile ¶
GetProgramMainFile returns the path to a program's main.c file.
func (*Ouroboros) GetProgramMap ¶
GetProgramMap returns the configured program map name.
func (*Ouroboros) GetProgramObjectPath ¶
GetProgramObjectPath returns the path to a compiled program object file.
func (*Ouroboros) GetProgramPath ¶
GetProgramPath returns the filesystem path for a program's source directory.
func (*Ouroboros) GetProgramSize ¶
GetProgramSize returns the size of a compiled program object in bytes. Returns 0 if the program hasn't been built.
func (*Ouroboros) GetProgramsByMap ¶
GetProgramsByMap returns all programs that use a specific map.
func (*Ouroboros) GetProjectRoot ¶
GetProjectRoot returns the project root directory (where ouroboros.json is located).
func (*Ouroboros) GetTargetDir ¶
GetTargetDir returns the absolute path to the target directory.
func (*Ouroboros) IsAttached ¶
IsAttached checks if any XDP program is attached to an interface. This checks for a pinned link in the BPF filesystem.
func (*Ouroboros) IsMapShared ¶
IsMapShared returns true if a map is used by multiple programs.
func (*Ouroboros) IsProgramBuilt ¶
IsProgramBuilt checks if a program has been built (object file exists).
func (*Ouroboros) IsProgramLoaded ¶
IsProgramLoaded checks if a program is currently loaded (pinned) in the kernel.
func (*Ouroboros) ListInterfaces ¶
ListInterfaces returns all available network interfaces.
func (*Ouroboros) ListMaps ¶
ListMaps discovers all maps from compiled programs. It analyzes the object files to extract map specifications.
func (*Ouroboros) ListPrograms ¶
ListPrograms returns all programs in the configuration.
func (*Ouroboros) LoadAllPrograms ¶
func (o *Ouroboros) LoadAllPrograms(opts *LoadOptions) (map[string]*LoadedProgram, map[string]error)
LoadAllPrograms loads all programs in the project into the kernel. Returns a map of program names to loaded programs or errors.
func (*Ouroboros) LoadPinnedMap ¶
LoadPinnedMap loads a pinned map by name and returns the handle.
func (*Ouroboros) LoadProgram ¶
func (o *Ouroboros) LoadProgram(progName string, opts *LoadOptions) (*LoadedProgram, error)
LoadProgram loads a single eBPF program into the kernel. It loads the compiled object file, pins it to the BPF filesystem, and returns a handle to the loaded program.
func (*Ouroboros) LoadProgramMap ¶
LoadProgramMap loads or creates the program array map used for tail calls.
func (*Ouroboros) PinProgram ¶
func (o *Ouroboros) PinProgram(loaded *LoadedProgram) error
PinProgram pins a loaded program to the BPF filesystem. This allows the program to persist after the loading process exits.
func (*Ouroboros) RebuildProgram ¶
func (o *Ouroboros) RebuildProgram(progName string, opts *BuildOptions) error
RebuildProgram removes old build artifacts and rebuilds a program.
func (*Ouroboros) ReloadConfig ¶
ReloadConfig reloads the configuration from disk.
func (*Ouroboros) RemoveProgram ¶
RemoveProgram removes a program from the configuration by name.
func (*Ouroboros) SaveConfig ¶
SaveConfig writes the current configuration to disk.
func (*Ouroboros) SetCompileArgs ¶
SetCompileArgs sets the compile arguments.
func (*Ouroboros) UnloadAllPrograms ¶
UnloadAllPrograms unloads all loaded programs.
func (*Ouroboros) UnloadProgram ¶
func (o *Ouroboros) UnloadProgram(loaded *LoadedProgram) error
UnloadProgram unloads a program from the kernel and closes its resources.
func (*Ouroboros) UnpinProgram ¶
UnpinProgram unpins a program from the BPF filesystem.
func (*Ouroboros) UpdateProgramMap ¶
func (o *Ouroboros) UpdateProgramMap(progMap *ebpf.Map, loaded *LoadedProgram) error
UpdateProgramMap updates the program array map with a loaded program. This is used for tail call support.
type Program ¶
type Program struct {
Name string `json:"name"`
ID int `json:"id"`
IsMain bool `json:"is_main,omitempty"`
Metadata *ProgramMetadata `json:"metadata,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
}
Program represents an eBPF program in the project.
type ProgramMetadata ¶
type ProgramMetadata struct {
HiddenOnFlow bool `json:"hidden_on_flow,omitempty"`
}
ProgramMetadata contains additional program metadata.