Skip to content

Commit

Permalink
refactor: modify constant isolated iflink
Browse files Browse the repository at this point in the history
  • Loading branch information
takehaya committed Sep 29, 2024
1 parent b456118 commit 0cd8e93
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 27 deletions.
50 changes: 50 additions & 0 deletions lib/Sys/Ebpf/Link/Netlink/Constants/Iflink.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Sys::Ebpf::Link::Netlink::Constants::Iflink;

use strict;
use warnings;
use utf8;

use Exporter 'import';

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

# IFLA_AF_SPEC section
'IFLA_UNSPEC' => 0,
'IFLA_XDP' => 43,

# /* XDP section */
'XDP_FLAGS_UPDATE_IF_NOEXIST' => 1 << 0,
'XDP_FLAGS_SKB_MODE' => 1 << 1,
'XDP_FLAGS_DRV_MODE' => 1 << 2,
'XDP_FLAGS_HW_MODE' => 1 << 3,
'XDP_FLAGS_REPLACE' => 1 << 4,
'XDP_FLAGS_MODES' => ( 1 << 1 | 1 << 2 | 1 << 3 ),
'XDP_FLAGS_MASK' => ( 1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 ),
'XDP_ATTACHED_NONE' => 0,
'XDP_ATTACHED_DRV' => 1,
'XDP_ATTACHED_SKB' => 2,
'XDP_ATTACHED_HW' => 3,
'XDP_ATTACHED_MULTI' => 4,
'IFLA_XDP_UNSPEC' => 0,
'IFLA_XDP_FD' => 1,
'IFLA_XDP_ATTACHED' => 2,
'IFLA_XDP_FLAGS' => 3,
'IFLA_XDP_PROG_ID' => 4,
'IFLA_XDP_DRV_PROG_ID' => 5,
'IFLA_XDP_SKB_PROG_ID' => 6,
'IFLA_XDP_HW_PROG_ID' => 7,
'IFLA_XDP_EXPECTED_FD' => 8,
);

# 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;
31 changes: 20 additions & 11 deletions lib/Sys/Ebpf/Link/Netlink/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use Socket qw( SOCK_RAW );
use Errno ();
use Exporter 'import';

# エクスポートする関数をリストに追加
our @EXPORT_OK
= qw(pack_sockaddr_nl pack_nlattr pack_nlmsghdr pack_ifinfomsg);

Expand Down Expand Up @@ -49,9 +48,8 @@ sub send_message {
my ( $self, $message ) = @_;

# カーネル(pid 0)にメッセージを送信
my $sockaddr_nl = pack_sockaddr_nl(0); # pid=0(カーネル)

my $bytes_sent = send( $self->{sock}, $message, 0, $sockaddr_nl );
my $sockaddr_nl = pack_sockaddr_nl(0);
my $bytes_sent = send( $self->{sock}, $message, 0, $sockaddr_nl );
unless ($bytes_sent) {
die "Failed to send Netlink message: $!";
}
Expand All @@ -77,15 +75,19 @@ sub close {
close( $self->{sock} ) if $self->{sock};
}

# ヘルパー関数

# sockaddr_nlをパック
# cf. https://github.com/torvalds/linux/blob/3efc57369a0ce8f76bf0804f7e673982384e4ac9/include/uapi/linux/netlink.h#L37
# typedef unsigned short __kernel_sa_family_t;
# struct sockaddr_nl {
# __kernel_sa_family_t nl_family; /* AF_NETLINK */
# unsigned short nl_pad; /* zero */
# __u32 nl_pid; /* port ID */
# __u32 nl_groups; /* multicast groups mask */
# };
sub pack_sockaddr_nl {
my ($pid) = @_;
return pack( 'S x2 L L', AF_NETLINK, $pid, 0 );
}

# Netlink属性をパック
# cf. https://github.com/torvalds/linux/blob/3efc57369a0ce8f76bf0804f7e673982384e4ac9/include/uapi/linux/netlink.h#L229
# /*
# * <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)-->
Expand All @@ -105,12 +107,11 @@ sub pack_nlattr {
my $nla_padded_len = ( $nla_len + 3 ) & ~3; # 4バイト境界にアライン
my $padding = "\0" x ( $nla_padded_len - $nla_len ); # パディング
return
pack( 'S< S<', $nla_padded_len, $type )
pack( 'S S', $nla_padded_len, $type )
. $payload
. $padding; # パディング後の長さでパック
}

# Netlinkメッセージヘッダーをパック
# cf. https://github.com/torvalds/linux/blob/3efc57369a0ce8f76bf0804f7e673982384e4ac9/include/uapi/linux/netlink.h#L52
# /**
# * struct nlmsghdr - fixed format metadata header of Netlink messages
Expand All @@ -132,7 +133,15 @@ sub pack_nlmsghdr {
return pack( 'L S S L L', $len, $type, $flags, $seq, $pid );
}

# ifinfomsgをパック
# cf. https://github.com/torvalds/linux/blob/3efc57369a0ce8f76bf0804f7e673982384e4ac9/include/uapi/linux/rtnetlink.h#L561
# struct ifinfomsg {
# unsigned char ifi_family;
# unsigned char __ifi_pad;
# unsigned short ifi_type; /* ARPHRD_* */
# int ifi_index; /* Link index */
# unsigned ifi_flags; /* IFF_* flags */
# unsigned ifi_change; /* IFF_* change mask */
# };
sub pack_ifinfomsg {
my ( $family, $type, $index, $flags, $change ) = @_;
return pack( 'C C S I I I', $family, 0, $type, $index, $flags, $change );
Expand Down
34 changes: 18 additions & 16 deletions lib/Sys/Ebpf/Link/Netlink/Xdp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ use strict;
use warnings;
use IO::Interface::Simple ();
use Sys::Ebpf::Link::Netlink::Socket;
use Sys::Ebpf::Link::Netlink::Constants::Iflink qw(
IFLA_XDP
IFLA_XDP_FD
IFLA_XDP_ATTACHED
IFLA_XDP_FLAGS
XDP_FLAGS_UPDATE_IF_NOEXIST
XDP_FLAGS_SKB_MODE
XDP_FLAGS_DRV_MODE
);
use Socket qw( AF_UNSPEC );
use Errno ();

# 定数の定義
use constant {
RTM_SETLINK => 19,
IFLA_XDP => 43,
IFLA_XDP_FD => 1,
IFLA_XDP_ATTACHED => 2,
IFLA_XDP_FLAGS => 3,
NLA_F_NESTED => 0x8000,
XDP_FLAGS_UPDATE_IF_NOEXIST => 1 << 0,
XDP_FLAGS_SKB_MODE => 1 << 1,
XDP_FLAGS_DRV_MODE => 1 << 2,
NLM_F_REQUEST => 0x0001,
NLM_F_ACK => 0x0004,
NLMSG_HDRLEN => 16,
NLA_HDRLEN => 4,
NETLINK_ROUTE => 0, # NETLINK_ROUTEの定義
NLMSG_ERROR => 2, # NLMSG_ERRORの定義
IFF_UP => 1 << 0,
RTM_SETLINK => 19,
NLA_F_NESTED => 0x8000,
NLM_F_REQUEST => 0x0001,
NLM_F_ACK => 0x0004,
NLMSG_HDRLEN => 16,
NLA_HDRLEN => 4,
NETLINK_ROUTE => 0, # NETLINK_ROUTEの定義
NLMSG_ERROR => 2, # NLMSG_ERRORの定義
IFF_UP => 1 << 0,
};

# ヘルパー関数をインポート
Expand Down

0 comments on commit 0cd8e93

Please sign in to comment.