Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactoring] - Externalize methods from Translator.ex #50

Open
Dwctor opened this issue Feb 26, 2024 · 0 comments
Open

[Refactoring] - Externalize methods from Translator.ex #50

Dwctor opened this issue Feb 26, 2024 · 0 comments
Labels
refactoring Improvements to code structure

Comments

@Dwctor
Copy link
Collaborator

Dwctor commented Feb 26, 2024

Honey Potion gives to the user access to libraries to help writing eBPF programs, such as Honey.Ethhdr and Honey.Bpf_helpers. These libraries contain methods that translate directly to C code instead of going through the translation, allowing more power and customizability.

However, as it stands, the implementation of these libraries is located inside lib/honey/translator.ex in the to_c method. This can be hard to find, customize and improve. Ideally we should move the implementation of these libraries and their methods outside the Translator.ex and into the lib/honey/c_libraries so that contribution becomes easier and so that advanced users may customize the implementations of the C counterpart to their own use cases.

As an example the implementation of the Honey.Ethhdr can be found in lib/honey/translator.ex in the format:

def to_c({{:., _, [Honey.Ethhdr, function]}, _, params}, _context) do
  case function do
    :init -> ...
    :const_udp -> ...
    :ip_protocol -> ...
    ...
  end
end

and it should be moved to lib/honey/c_libraries into a module that defines the functions themselves, such as:

defmodule Honey.Ethhdr do

  def init() do
    ...
  end

  def const_udp() do
    ...
  end

  def ip_protocol() do
    ...
  end

  ...

end

Elixir has interesting ways to delegate function calls dynamically during execution, and a solution that leverages that to use the atom passed as a variable into the to_c function as the function name called in the library would be ideal, as it wouldn't require further changes into the translator.ex file for each new library.

@Dwctor Dwctor added the refactoring Improvements to code structure label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Improvements to code structure
Projects
None yet
Development

No branches or pull requests

1 participant