• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    C
  • License
    GNU General Publi...
  • Created almost 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Writing eBPF programs with Elixir!

🍯 Honey Potion - Writing eBPF with Elixir 🍯

Hex Version Hex Docs Total Download License Last Updated


logo

Description

Honey Potion is a framework that brings the powerful eBPF technology into Elixir. Users can write Elixir code that will be transformed into eBPF bytecodes. Many high-level features of Elixir are available and more will be added soon. In this alpha version, the framework translates the code to a subset of C that uses libbpf's features. Then it's possible to use clang to obtain the bytecodes and load it into the Kernel.

Dependencies

Honey Potion depends on a few BPF-related packages to run. Below are listed the packages with the ubuntu names. Other distros should have similar or equal names.

  • erlang and elixir - For the language used in Honey Potion
  • libbpf - Version 1.1 (See YouTube video on how to get it)
  • gcc-multilib - For C libraries (asm/types.h)
  • make - For Makefile compilation
  • llvm - For llc
  • clang - For clang

Note that clang, llc and bpftool can be compiled by the user, as long as they are in the $PATH.

YouTube

If you prefer the video format, Honey Potion has a YouTube playlist with guides on how to set up Honey Potion and how to create example programs.


YouTubeThumb

Installation

The package can be installed by adding honey to your list of dependencies in mix.exs:

def deps do
  [
    {:honey, git: "https://github.com/lac-dcc/honey-potion/", submodules: true}
  ]
end

Usage

When you use Honey in your module, it'll be translated to C the next time you compile the project. For example:

defmodule Minimal do
  use Honey, license: "Dual BSD/GPL"

  # ...
end

Will generate a few new sub-directories for you:

  • src : Where the C code is kept, Minimal.bpf.c for example
  • obj : Where compilation objects is be kept, Minimal.o for example
  • bin : Where the executable is kept, Minimal for example

To run your program, just go into the bin directory and run the executable with privileges.

If you wish to learn how to use Honey-Potion with a video click here.

Notice the license option: as eBPF demands, we need to specify a license to our program. Currently, Honey accepts one more option besides the license. The option clang_formater can take the path of the clang-formater executable, and it'll use it to beautify the C file generated.

Main function

A module that uses Honey must define a function main/1 that receives a ctx. The main function is the entry point of our eBPF program. For example:

defmodule Example.Minimal do
  use Honey, license: "Dual BSD/GPL"

  @sec "tracepoint/syscalls/sys_enter_kill"
  def main(ctx) do
    # ...
  end
end

Notice the @sec decorator: The main function must specify its program type, according to the available options in libbpf. The argument received, ctx, is a struct whose fields vary depending on the program type. In its Alpha version, only one type is allowed: tracepoint/syscalls/sys_enter_kill.

The main function must return an integer, otherwise an exception will be thrown at runtime (see Runtime Exceptions below).

Maps

Users can define maps using the macro defmap. For example, to create a map named my_map, you can:

defmap(:my_map,
     %{type: BPF_MAP_TYPE_ARRAY,
     max_entries: 10}
)

In the Alpha version, just the map type BPF_MAP_TYPE_ARRAY is available, but you only need to specify the number of entries and the map is ready to use.

Helper functions

eBPF and libbpf provides some helper functions, and so does Honey. In the Alpha version, there is a single module you can import:

import Honey.Bpf.Bpf_helpers

Referencing the usual #include <bpf/bpf_helpers>, this module allows you to call:

  • bpf_map_lookup_elem(map, key) ➜ Map access are easy, you can pass the name of the map declared with defmap and the key (currently, only integer), and the function will the return the value to you. In the Alpha version, if it is not possible to access that position of the map, a Runtime exception will be thrown.

  • bpf_map_update_elem(map, key, value) ➜ Update a position in a map. It receives the name of the map, the key (currently, only integers) and the value to be updated. The return is 0 on success, or a negative error in case of failure.

  • bpf_printk(params) ➜ Send a string to the debug pipe. In this Alpha version, params is an array. The first position must be a string containing up to three format specifiers %d. The number of next elements must be the same number of %d used. For example:

    bpf_printk(["I am printing the number %d, and also %d.", n1, n2])

    You can read the pipe with sudo cat /sys/kernel/debug/tracing/trace_pipe. In the Alpha version, only variables of type integers can be printed.

  • bpf_get_current_pid_tgid() ➜ Return the PID of the process that triggered the eBPF program.

Recursive functions

It is possible to define recursive functions and call them from main/1. For example, let's define a function that recursively sums two natural numbers:

def sum(a, b) do
  if b == 0 do
     a
  else
    sum(a + 1, b - 1)
  end
end

To avoid infinite recursion and satisfy the eBPF verifier, we require you to inform a constant number that will be used to limit the maximum depth of recursion at runtime. This is done through the macro fuel. Its syntax is: fuel max_number, function_call(...)

Let's see an example inside main:

@sec "tracepoint/syscalls/sys_enter_kill"
def main(ctx) do
  x = 100
  y = fuel 10, sum(x, 5)
  bpf_printk(["The value of y is %d", y])
end

We provide a constant amount of 10 units fuel in the first call sum. Each time sum calls itself, it burns one unit of fuel. If, at some point, sum tries to calls itself again with no fuel remaining, a Runtime Exception will be thrown and the program will halt.

Runtime Exceptions

Exceptions are a natural part of dynamically-typed languages such as Elixir. To allow many of the high-level constructs of Elixir, we simulate the notion of Runtime Exceptions when translating programs to eBPF. In this Alpha version, when a Runtime Exception is thrown, the program will print the exception message to the debug pipe, and return with 0.

Current limitations

This framework is still Alpha, and we have lots of features to add, improve and correct. Amongst the current known limitations are:

  • We cannot destructure elements while doing pattern matching. Because of that, the matching operator = is working like a traditional assignment operator with only a simple variable in the left-hand side. For the same reason, case and if-else blocks are not supported, unless they are totally optimized out at compile time.
  • Only a small number of operators are available, such as +, -, *, / and ==.
  • We do not support function guards nor default arguments.
  • We do not support mutual recursive functions.
  • We do not support user-defined structs.
  • To run the executable currently, executable has to be in bin folder and object in obj folder.

There are more, and we are actively working to improve it.

Contributing

Contributions are very welcome! If you are interested in collaborating, let's stay in touch so our work doesn't overlap. Feedback and suggestions are also very much appreciated! You can file a GitHub issue or contact us at [email protected].

Copyright & License

Copyright (C) 2022 Compilers Laboratory - Federal University of Minas Gerais (UFMG), Brazil

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

More Repositories

1

llvm-course

Code examples used for the LAC LLVM course
C++
120
star
2

wyvern

Automatic conversion of call by value into call by need in the LLVM IR.
C++
55
star
3

sepe

Synthesis of Optimized Hash Functions
C++
49
star
4

lif

A tool to eliminate timing-based side channels
LLVM
47
star
5

jotai-benchmarks

Collection of executable benchmarks
C
41
star
6

chimera

A tool for synthesizing Verilog programs
Verilog
33
star
7

yali

A framework to analyze a space formed by the combination of program encodings, obfuscation passes and stochastic classification models.
LLVM
30
star
8

merlin

Program instrumentation and cost function generation
C++
28
star
9

lushu

System to recognize infinite languages and react to string events
Jupyter Notebook
26
star
10

Nisse

Ball Larus edge profiling with loop optimization
C++
22
star
11

Angha

Angha is a framework for constructing compilable synthetic C benchmarks out of publicly available C repositories.
Python
21
star
12

Daedalus

Daedalus is an LLVM pass that compresses code by identifying and extracting recurrent program slices.
C++
19
star
13

Benchmarks

LLVM test suite benchmarks (260 benchmarks - 36 test suites)
C
18
star
14

hamsa

A tool to extract complexities and costs from programs and kernels
C++
17
star
15

hapi

A domain specific language to define access policies.
Kotlin
14
star
16

elixir-pbbs

Rewrite the Problem-Based Benchmark Suite in Elixir
Elixir
14
star
17

DropletSearch

Space search exploration in the compiler optimization space
Python
12
star
18

hydra

A benchmark game for guessing the hottest point of a program
C
10
star
19

Zhe

Discovery and Treatment of String Events
Kotlin
9
star
20

BenchGen

Automatic Generation of Benchmarks to Stress-Test Computing Systems.
C
8
star
21

CMeasure

Power measurement tool designed to extract power data from the National Instruments DAQ USB-6009 Device
C++
7
star
22

koroghlu

Project to build a docker for artifacts.
Python
5
star
23

JINN-C

JINN-C Adaptive Compiler for Heterogeneous Single-ISA architectures
Scala
5
star
24

bennu

template_autotvm
Python
4
star
25

hapi-visualizer

Web interface to visualize policies descriptions
CSS
3
star
26

Zhe-Lang

Kotlin
3
star
27

templates

Templates for presentations
TeX
3
star
28

page.bcmp

HTML
3
star
29

benchMetrics

3
star
30

DCC888

Python
2
star
31

StaticFeatureExtractor

A Tool to Extract Code Features from JVM Compatible Languages.
Java
1
star