Skip to content

Commit

Permalink
chore: refactor and change module name
Browse files Browse the repository at this point in the history
  • Loading branch information
takehaya committed Sep 13, 2024
1 parent 8dd8389 commit e0703a5
Show file tree
Hide file tree
Showing 35 changed files with 169 additions and 197 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# sys::ebpf
# Sys::Ebpf

ebpf - It's pure-perl ebpf loader

# SYNOPSIS

use sys::ebpf;
use Sys::Ebpf;

# DESCRIPTION

Expand Down
24 changes: 12 additions & 12 deletions lib/sys/ebpf.pm → lib/Sys/Ebpf.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sys::ebpf;
package Sys::Ebpf;

use strict;
use warnings;
Expand All @@ -16,14 +16,14 @@ ebpf - Pure-Perl interface for eBPF (extended Berkeley Packet Filter)
=head1 SYNOPSIS
use sys::ebpf::;
use Sys::Ebpf::;
# Create a new eBPF loader
my $loader = sys::ebpf::loader->new();
my $loader = Sys::Ebpf::loader->new();
# Load a BPF map
my $map_fd = $loader->load_bpf_map({
map_type => sys::ebpf::constants::bpf_map_type::BPF_MAP_TYPE_ARRAY,
map_type => Sys::Ebpf::Constants::bpf_map_type::BPF_MAP_TYPE_ARRAY,
key_size => 4,
value_size => 8,
max_entries => 1,
Expand All @@ -46,13 +46,13 @@ This module includes several submodules:
=over 6
=item * C<sys::ebpf::loader> - For loading eBPF programs and maps
=item * C<Sys::Ebpf::loader> - For loading eBPF programs and maps
=item * C<sys::ebpf::asm> - eBPF assembly helpers
=item * C<Sys::Ebpf::asm> - eBPF assembly helpers
=item * C<sys::ebpf::reader> - For reading ELF files
=item * C<Sys::Ebpf::reader> - For reading ELF files
=item * C<sys::ebpf::elf::parser> - For parsing ELF files
=item * C<Sys::Ebpf::elf::parser> - For parsing ELF files
=back
Expand All @@ -65,13 +65,13 @@ Refer to the documentation of individual submodules for specific functions and u
=over 4
=item * L<sys::ebpf::loader>
=item * L<Sys::Ebpf::loader>
=item * L<sys::ebpf::asm>
=item * L<Sys::Ebpf::asm>
=item * C<sys::ebpf::reader> - For reading ELF files
=item * C<Sys::Ebpf::reader> - For reading ELF files
=item * C<sys::ebpf::elf::parser> - For parsing ELF files
=item * C<Sys::Ebpf::elf::parser> - For parsing ELF files
=back
Expand Down
8 changes: 3 additions & 5 deletions lib/sys/ebpf/asm.pm → lib/Sys/Ebpf/Asm.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package sys::ebpf::asm;
package Sys::Ebpf::Asm;

use strict;
use warnings;
use utf8;

our $VERSION = $sys::ebpf::VERSION;

# cf. https://www.kernel.org/doc/html/v6.11-rc7/bpf/standardization/instruction-set.html
# cf. https://github.com/iovisor/bpf-docs/blob/master/eBPF.md
# BPF instruction classes
Expand Down Expand Up @@ -177,8 +175,8 @@ sub get_imm { $_[0]->{imm} }
sub deserialize_128bit_instruction {
my ($binary) = @_;

my $high = sys::ebpf::asm->deserialize( substr( $binary, 0, 8 ) );
my $low = sys::ebpf::asm->deserialize( substr( $binary, 8, 8 ) );
my $high = Sys::Ebpf::asm->deserialize( substr( $binary, 0, 8 ) );
my $low = Sys::Ebpf::asm->deserialize( substr( $binary, 8, 8 ) );

return ( $high, $low );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package sys::ebpf::constants::bpf_attach_type;
package Sys::Ebpf::Constants::BpfAttachType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

# 定数を配列で定義
my @constants = (
'BPF_CGROUP_INET_INGRESS', 0,
'BPF_CGROUP_INET_EGRESS', 1,
Expand Down Expand Up @@ -69,7 +66,6 @@ my @constants = (
'__MAX_BPF_ATTACH_TYPE', 56,
);

# 定数を定義し、エクスポート用配列に追加
our @EXPORT_OK = keys %constants;
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::constants::bpf_cmd;
package Sys::Ebpf::Constants::BpfCmd;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my %constants = (
'BPF_MAP_CREATE', 0,
'BPF_MAP_LOOKUP_ELEM', 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::constants::bpf_map_create_flags;
package Sys::Ebpf::Constants::BpfMapCreateFlags;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my %constants = (
'BPF_F_NO_PREALLOC' => 1 << 0,
'BPF_F_NO_COMMON_LRU' => 1 << 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::constants::bpf_map_type;
package Sys::Ebpf::Constants::BpfMapType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my %constants = (
'BPF_MAP_TYPE_UNSPEC', 0,
'BPF_MAP_TYPE_HASH', 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::constants::bpf_map_update_flags;
package Sys::Ebpf::Constants::BpfMapUpdateFlags;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my %constants = (
'BPF_ANY' => 0, # create new element or update existing
'BPF_NOEXIST' => 1, # create new element if it didn't exist
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::constants::bpf_prog_type;
package Sys::Ebpf::Constants::BpfProgType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my %constants = (
'BPF_PROG_TYPE_UNSPEC', 0,
'BPF_PROG_TYPE_SOCKET_FILTER', 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::elf::machine_type;
package Sys::Ebpf::Elf::MachineType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my @constants = (
'EM_NONE', 0, 'EM_M32', 1,
'EM_SPARC', 2, 'EM_386', 3,
Expand Down
10 changes: 5 additions & 5 deletions lib/sys/ebpf/elf/parser.pm → lib/Sys/Ebpf/Elf/Parser.pm
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package sys::ebpf::elf::parser;
package Sys::Ebpf::Elf::Parser;

use strict;
use warnings;
use utf8;

our $VERSION = $sys::ebpf::VERSION;
our $VERSION = $Sys::Ebpf::VERSION;

use sys::ebpf::elf::section_type qw( SHT_REL SHT_RELA );
use sys::ebpf::elf::machine_type qw( EM_BPF );
use Sys::Ebpf::Elf::SectionType qw( SHT_REL SHT_RELA );
use Sys::Ebpf::Elf::MachineType qw( EM_BPF );

# elf64形式はこの通り
# cf. https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
Expand Down Expand Up @@ -80,7 +80,7 @@ sub parse_elf {
$elf->{e_type} = $e_type;
$elf->{e_machine} = $e_machine;
$elf->{e_machine_name}
= sys::ebpf::elf::machine_type->get_machine_name($e_machine);
= Sys::Ebpf::elf::MachineType->get_machine_name($e_machine);
$elf->{e_version} = $e_version;
$elf->{e_entry} = $e_entry;
$elf->{e_phoff} = $e_phoff;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sys::ebpf::elf::section_flag_type;
package Sys::Ebpf::Elf::SectionFlagType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;
our $VERSION = $Sys::Ebpf::VERSION;

my @constants = (
'SHF_WRITE', 1 << 0, 'SHF_ALLOC', 1 << 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::elf::section_type;
package Sys::Ebpf::Elf::SectionType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

my @constants = (
'SHT_NULL', 0, 'SHT_PROGBITS', 1,
'SHT_SYMTAB', 2, 'SHT_STRTAB', 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::elf::symbol_bind;
package Sys::Ebpf::Elf::SymbolBind;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

# Symbol Bindings (upper 4 bits of st_info)
my @constants = (
'STB_LOCAL', 0, 'STB_GLOBAL', 1, 'STB_WEAK', 2, 'STB_NUM', 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sys::ebpf::elf::symbol_type;
package Sys::Ebpf::Elf::SymbolType;

use strict;
use warnings;
use utf8;

use Exporter 'import';

our $VERSION = $sys::ebpf::VERSION;

# Symbol Types (lower 4 bits of st_info)
my @constants = (
'STT_NOTYPE', 0, 'STT_OBJECT', 1, 'STT_FUNC', 2,
Expand Down
34 changes: 16 additions & 18 deletions lib/sys/ebpf/loader.pm → lib/Sys/Ebpf/Loader.pm
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package sys::ebpf::loader;
package Sys::Ebpf::Loader;

use strict;
use warnings;
use utf8;

use sys::ebpf::asm;
use sys::ebpf::reader ();
use sys::ebpf::map;
our $VERSION = $sys::ebpf::VERSION;
use Sys::Ebpf::Asm;
use Sys::Ebpf::Reader ();
use Sys::Ebpf::Map;

use Data::Dumper ();

use sys::ebpf::elf::section_type qw( SHT_PROGBITS );
use sys::ebpf::constants::bpf_cmd qw( BPF_PROG_LOAD );
use sys::ebpf::constants::bpf_prog_type qw( BPF_PROG_TYPE_KPROBE );
use sys::ebpf::elf::section_type qw( SHT_PROGBITS );
use sys::ebpf::elf::symbol_type qw(STT_OBJECT);
use Errno qw( EACCES EPERM );
use sys::ebpf::syscall;
use Sys::Ebpf::Elf::SectionType qw( SHT_PROGBITS );
use Sys::Ebpf::Constants::BpfCmd qw( BPF_PROG_LOAD );
use Sys::Ebpf::Constants::BpfProgType qw( BPF_PROG_TYPE_KPROBE );
use Sys::Ebpf::Elf::SymbolType qw(STT_OBJECT);
use Errno qw( EACCES EPERM );
use Sys::Ebpf::Syscall;

sub new {
my ( $class, $file ) = @_;
Expand All @@ -30,7 +28,7 @@ sub load_elf {
my ($self) = @_;
my $file = $self->{file};

my $reader = sys::ebpf::reader->new($file);
my $reader = Sys::Ebpf::Reader->new($file);
$self->{reader} = $reader;

my $bpfelf = $reader->parse_ebpf();
Expand Down Expand Up @@ -123,7 +121,7 @@ sub extract_bpf_map_attributes {
sub find_section {
my ( $self, $section_name ) = @_;
my $bpfelf = $self->{bpfelf};
return sys::ebpf::elf::parser::find_section( $bpfelf->{sections},
return Sys::Ebpf::Elf::Parser::find_section( $bpfelf->{sections},
$section_name );
}

Expand Down Expand Up @@ -199,7 +197,7 @@ sub load_bpf_program {
);

# syscallの実行
my $fd = syscall( sys::ebpf::syscall::SYS_bpf(),
my $fd = syscall( Sys::Ebpf::Syscall::SYS_bpf(),
BPF_PROG_LOAD, $attr, length($attr) );

if ( $fd < 0 ) {
Expand Down Expand Up @@ -271,7 +269,7 @@ sub apply_map_relocations {
. "\n"; # デバッグ出力

my ( $high, $low )
= sys::ebpf::asm::deserialize_128bit_instruction($bpf_insn);
= Sys::Ebpf::Asm::deserialize_128bit_instruction($bpf_insn);

# 即値 (64ビット) にマップFDを設定
$high->set_imm($map_fd);
Expand All @@ -282,7 +280,7 @@ sub apply_map_relocations {

# 修正後の命令をパックして、元の場所に書き戻す
my $new_bpf_insn
= sys::ebpf::asm::serialize_128bit_instruction( $high, $low );
= Sys::Ebpf::Asm::serialize_128bit_instruction( $high, $low );
substr( $self->{reader}->{raw_elf_data},
$r_offset, 16, $new_bpf_insn );

Expand Down Expand Up @@ -315,7 +313,7 @@ sub load_bpf {
# map_attr_refの各キー(マップ名)に対して処理を実行
for my $map (@$maps) {
my $map_name = $map->{map_name};
my $map_instance = sys::ebpf::map->create($map);
my $map_instance = Sys::Ebpf::Map->create($map);
my $map_fd = $map_instance->{map_fd};
if ( $map_fd < 0 ) {
die "Failed to load BPF map: $map_name (FD: $map_fd})\n";
Expand Down
Loading

0 comments on commit e0703a5

Please sign in to comment.