Skip to content

Commit

Permalink
refactor: modify xdp attach code
Browse files Browse the repository at this point in the history
  • Loading branch information
takehaya committed Sep 29, 2024
1 parent 6ab3e9a commit cefdb38
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 165 deletions.
96 changes: 96 additions & 0 deletions lib/Sys/Ebpf/Link/Netlink/Constants/Rtnetlink.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package Sys::Ebpf::Link::Netlink::Constants::Rtnetlink;

use strict;
use warnings;
use utf8;

use Exporter 'import';

# cf. https://github.com/torvalds/linux/blob/master/include/uapi/linux/rtnetlink.h
my %constants = (

# /* Routing table identifiers. */
# /* Types of messages */
'RTM_BASE' => 16,
'RTM_NEWLINK' => 16,
'RTM_DELLINK' => 17,
'RTM_GETLINK' => 18,
'RTM_SETLINK' => 19,
'RTM_NEWADDR' => 20,
'RTM_DELADDR' => 21,
'RTM_GETADDR' => 22,
'RTM_NEWROUTE' => 24,
'RTM_DELROUTE' => 25,
'RTM_GETROUTE' => 26,
'RTM_NEWNEIGH' => 28,
'RTM_DELNEIGH' => 29,
'RTM_GETNEIGH' => 30,
'RTM_NEWRULE' => 32,
'RTM_DELRULE' => 33,
'RTM_GETRULE' => 34,
'RTM_NEWQDISC' => 36,
'RTM_DELQDISC' => 37,
'RTM_GETQDISC' => 38,
'RTM_NEWTCLASS' => 40,
'RTM_DELTCLASS' => 41,
'RTM_GETTCLASS' => 42,
'RTM_NEWTFILTER' => 44,
'RTM_DELTFILTER' => 45,
'RTM_GETTFILTER' => 46,
'RTM_NEWACTION' => 48,
'RTM_DELACTION' => 49,
'RTM_GETACTION' => 50,
'RTM_NEWPREFIX' => 52,
'RTM_GETMULTICAST' => 58,
'RTM_GETANYCAST' => 62,
'RTM_NEWNEIGHTBL' => 64,
'RTM_GETNEIGHTBL' => 66,
'RTM_SETNEIGHTBL' => 67,
'RTM_NEWNDUSEROPT' => 68,
'RTM_NEWADDRLABEL' => 72,
'RTM_DELADDRLABEL' => 73,
'RTM_GETADDRLABEL' => 74,
'RTM_GETDCB' => 78,
'RTM_SETDCB' => 79,
'RTM_NEWNETCONF' => 80,
'RTM_GETNETCONF' => 82,
'RTM_NEWMDB' => 84,
'RTM_DELMDB' => 85,
'RTM_GETMDB' => 86,
'RTM_NEWNSID' => 88,
'RTM_DELNSID' => 89,
'RTM_GETNSID' => 90,
'RTM_NEWSTATS' => 92,
'RTM_GETSTATS' => 94,
'RTM_NEWCACHEREPORT' => 96,
'RTM_NEWCHAIN' => 100,
'RTM_DELCHAIN' => 101,
'RTM_GETCHAIN' => 102,
'RTM_NEWNEXTHOP' => 104,
'RTM_DELNEXTHOP' => 105,
'RTM_GETNEXTHOP' => 106,
'RTM_NEWLINKPROP' => 108,
'RTM_DELLINKPROP' => 109,
'RTM_GETLINKPROP' => 110,
'RTM_NEWVLAN' => 112,
'RTM_DELVLAN' => 113,
'RTM_GETVLAN' => 114,
'RTM_NEWNEXTHOPBUCKET' => 116,
'RTM_DELNEXTHOPBUCKET' => 117,
'RTM_GETNEXTHOPBUCKET' => 118,
'RTM_NEWTUNNEL' => 120,
'RTM_DELTUNNEL' => 121,
'RTM_GETTUNNEL' => 122,
);

# Export all constants
our @EXPORT_OK = keys %constants;
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

# Define constants as subroutines
for my $name (@EXPORT_OK) {
no strict 'refs';
*{$name} = sub () { $constants{$name} };
}

1;
26 changes: 24 additions & 2 deletions lib/Sys/Ebpf/Link/Netlink/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ sub new {
setsockopt( $self->{sock}, SOL_NETLINK, NETLINK_EXT_ACK, $one )
or warn "Failed to set NETLINK_EXT_ACK: $!";

# ソケットをバインド
my $sockaddr_nl = pack_sockaddr_nl($$); # プロセスのPIDにバインド
# ソケットをバインド(procss pid bind)
my $sockaddr_nl = pack_sockaddr_nl($$);
bind( $self->{sock}, $sockaddr_nl )
or die "Failed to bind Netlink socket: $!";

Expand Down Expand Up @@ -81,6 +81,28 @@ sub close {
close( $self->{sock} ) if $self->{sock};
}

# helper functions

sub get_error_message {
my ($response) = @_;
my $offset = NLMSG_HDRLEN + 4; # After error code
my $len = length($response);

if ( $len > $offset ) {
my $attr_data = substr( $response, $offset );
while ( length($attr_data) >= NLA_HDRLEN ) {
my ( $nla_len, $nla_type ) = unpack( 'S S', $attr_data );
my $payload
= substr( $attr_data, NLA_HDRLEN, $nla_len - NLA_HDRLEN );
if ( $nla_type == 1 ) { # NLMSGERR_ATTR_MSG
return unpack( 'Z*', $payload );
}
$attr_data = substr( $attr_data, ( $nla_len + 3 ) & ~3 );
}
}
return "";
}

# cf. https://github.com/torvalds/linux/blob/3efc57369a0ce8f76bf0804f7e673982384e4ac9/include/uapi/linux/netlink.h#L37
# typedef unsigned short __kernel_sa_family_t;
# struct sockaddr_nl {
Expand Down
Loading

0 comments on commit cefdb38

Please sign in to comment.