Skip to content

Commit

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

use strict;
use warnings;
use utf8;

use Exporter 'import';

# cf. https://github.com/torvalds/linux/blob/master/tools/include/uapi/linux/netlink.h
my %constants = (
'NETLINK_ROUTE' => 0,
'NETLINK_UNUSED' => 1,
'NETLINK_USERSOCK' => 2,
'NETLINK_FIREWALL' => 3,
'NETLINK_SOCK_DIAG' => 4,
'NETLINK_NFLOG' => 5,
'NETLINK_XFRM' => 6,
'NETLINK_SELINUX' => 7,
'NETLINK_ISCSI' => 8,
'NETLINK_AUDIT' => 9,
'NETLINK_FIB_LOOKUP' => 10,
'NETLINK_CONNECTOR' => 11,
'NETLINK_NETFILTER' => 12,
'NETLINK_IP6_FW' => 13,
'NETLINK_DNRTMSG' => 14,
'NETLINK_KOBJECT_UEVENT' => 15,
'NETLINK_GENERIC' => 16,
'NETLINK_SCSITRANSPORT' => 18,
'NETLINK_ECRYPTFS' => 19,
'NETLINK_RDMA' => 20,
'NETLINK_CRYPTO' => 21,
'NETLINK_SMC' => 22,

'NLMSG_NOOP' => 1,
'NLMSG_ERROR' => 2,
'NLMSG_DONE' => 3,
'NLMSG_OVERRUN' => 4,

# /* Flags values */
'NLM_F_REQUEST' => 0x01,
'NLM_F_MULTI' => 0x02,
'NLM_F_ACK' => 0x04,
'NLM_F_ECHO' => 0x08,
'NLM_F_DUMP_INTR' => 0x10,
'NLM_F_DUMP_FILTERED' => 0x20,

# /* Modifiers to GET request */
'NLM_F_ROOT' => 0x100,
'NLM_F_MATCH' => 0x200,
'NLM_F_ATOMIC' => 0x400,

# 'NLM_F_DUMP' => ( NLM_F_ROOT | NLM_F_MATCH ),

# /* Modifiers to NEW request */
'NLM_F_REPLACE' => 0x100,
'NLM_F_EXCL' => 0x200,
'NLM_F_CREATE' => 0x400,
'NLM_F_APPEND' => 0x800,

# /* Modifiers to DELETE request */
'NLM_F_NONREC' => 0x100,

# /* Flags for ACK message */
'NLM_F_CAPPED' => 0x100,
'NLM_F_ACK_TLVS' => 0x200,

# /* Attribute types */
'NLA_F_NESTED' => ( 1 << 15 ),
'NLA_F_NET_BYTEORDER' => ( 1 << 14 ),
'NLMSG_HDRLEN' => 16,
'NLA_HDRLEN' => 4,

# Socket options
'NETLINK_ADD_MEMBERSHIP' => 1,
'NETLINK_DROP_MEMBERSHIP' => 2,
'NETLINK_PKTINFO' => 3,
'NETLINK_BROADCAST_ERROR' => 4,
'NETLINK_NO_ENOBUFS' => 5,
'NETLINK_RX_RING' => 6,
'NETLINK_TX_RING' => 7,
'NETLINK_LISTEN_ALL_NSID' => 8,
'NETLINK_CAP_ACK' => 10,
'NETLINK_EXT_ACK' => 11,
'NETLINK_GET_STRICT_CHK' => 12,
);

# 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;
165 changes: 165 additions & 0 deletions lib/Sys/Ebpf/Link/Netlink/Constants/Socket.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package Sys::Ebpf::Link::Netlink::Constants::Socket;

use strict;
use warnings;
use utf8;

use Exporter 'import';

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

# /* Supported address families. */
'AF_UNSPEC' => 0,
'AF_UNIX' => 1,
'AF_INET' => 2,
'AF_AX25' => 3,
'AF_IPX' => 4,
'AF_APPLETALK' => 5,
'AF_NETROM' => 6,
'AF_BRIDGE' => 7,
'AF_ATMPVC' => 8,
'AF_X25' => 9,
'AF_INET6' => 10,
'AF_ROSE' => 11,
'AF_DECnet' => 12,
'AF_NETBEUI' => 13,
'AF_SECURITY' => 14,
'AF_KEY' => 15,
'AF_NETLINK' => 16,
'AF_ROUTE' => 16,
'AF_PACKET' => 17,
'AF_ASH' => 18,
'AF_ECONET' => 19,
'AF_ATMSVC' => 20,
'AF_RDS' => 21,
'AF_SNA' => 22,
'AF_IRDA' => 23,
'AF_PPPOX' => 24,
'AF_WANPIPE' => 25,
'AF_LLC' => 26,
'AF_IB' => 27,
'AF_MPLS' => 28,
'AF_CAN' => 29,
'AF_TIPC' => 30,
'AF_BLUETOOTH' => 31,
'AF_IUCV' => 32,
'AF_RXRPC' => 33,
'AF_ISDN' => 34,
'AF_PHONET' => 35,
'AF_IEEE802154' => 36,
'AF_CAIF' => 37,
'AF_ALG' => 38,
'AF_NFC' => 39,
'AF_VSOCK' => 40,
'AF_KCM' => 41,
'AF_QIPCRTR' => 42,
'AF_SMC' => 43,
'AF_XDP' => 44,
'AF_MCTP' => 45,

# /* Protocol families, same as address families. */
'PF_UNSPEC' => 0,
'PF_UNIX' => 1,
'PF_LOCAL' => 1,
'PF_INET' => 2,
'PF_AX25' => 3,
'PF_IPX' => 4,
'PF_APPLETALK' => 5,
'PF_NETROM' => 6,
'PF_BRIDGE' => 7,
'PF_ATMPVC' => 8,
'PF_X25' => 9,
'PF_INET6' => 10,
'PF_ROSE' => 11,
'PF_DECnet' => 12,
'PF_NETBEUI' => 13,
'PF_SECURITY' => 14,
'PF_KEY' => 15,
'PF_NETLINK' => 16,
'PF_ROUTE' => 16,
'PF_PACKET' => 17,
'PF_ASH' => 18,
'PF_ECONET' => 19,
'PF_ATMSVC' => 20,
'PF_RDS' => 21,
'PF_SNA' => 22,
'PF_IRDA' => 23,
'PF_PPPOX' => 24,
'PF_WANPIPE' => 25,
'PF_LLC' => 26,
'PF_IB' => 27,
'PF_MPLS' => 28,
'PF_CAN' => 29,
'PF_TIPC' => 30,
'PF_BLUETOOTH' => 31,
'PF_IUCV' => 32,
'PF_RXRPC' => 33,
'PF_ISDN' => 34,
'PF_PHONET' => 35,
'PF_IEEE802154' => 36,
'PF_CAIF' => 37,
'PF_ALG' => 38,
'PF_NFC' => 39,
'PF_VSOCK' => 40,
'PF_KCM' => 41,
'PF_QIPCRTR' => 42,
'PF_SMC' => 43,
'PF_XDP' => 44,
'PF_MCTP' => 45,

# /* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */
'SOL_IP' => 0,
'SOL_SOCKET' => 1,
'SOL_TCP' => 6,
'SOL_UDP' => 17,
'SOL_IPV6' => 41,
'SOL_ICMPV6' => 58,
'SOL_SCTP' => 132,
'SOL_UDPLITE' => 136,
'SOL_RAW' => 255,
'SOL_IPX' => 256,
'SOL_AX25' => 257,
'SOL_ATALK' => 258,
'SOL_NETROM' => 259,
'SOL_ROSE' => 260,
'SOL_DECNET' => 261,
'SOL_X25' => 262,
'SOL_PACKET' => 263,
'SOL_ATM' => 264,
'SOL_AAL' => 265,
'SOL_IRDA' => 266,
'SOL_NETBEUI' => 267,
'SOL_LLC' => 268,
'SOL_DCCP' => 269,
'SOL_NETLINK' => 270,
'SOL_TIPC' => 271,
'SOL_RXRPC' => 272,
'SOL_PPPOL2TP' => 273,
'SOL_BLUETOOTH' => 274,
'SOL_PNPIPE' => 275,
'SOL_RDS' => 276,
'SOL_IUCV' => 277,
'SOL_CAIF' => 278,
'SOL_ALG' => 279,
'SOL_NFC' => 280,
'SOL_KCM' => 281,
'SOL_TLS' => 282,
'SOL_XDP' => 283,
'SOL_MPTCP' => 284,
'SOL_MCTP' => 285,
'SOL_SMC' => 286,
'SOL_VSOCK' => 287,
);

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

our @EXPORT_OK
= qw(pack_sockaddr_nl pack_nlattr pack_nlmsghdr pack_ifinfomsg);

use constant {
NETLINK_ROUTE => 0,
NLMSG_HDRLEN => 16,
NLA_HDRLEN => 4,
NLM_F_REQUEST => 0x0001,
NLM_F_ACK => 0x0004,
PF_NETLINK => 16,
AF_NETLINK => 16,
SOL_NETLINK => 270,
NETLINK_EXT_ACK => 11,
};
use Sys::Ebpf::Link::Netlink::Constants::Netlink qw(
NETLINK_ROUTE
NLMSG_HDRLEN
NLA_HDRLEN
NLM_F_REQUEST
NLM_F_ACK
NETLINK_EXT_ACK
);
use Sys::Ebpf::Link::Netlink::Constants::Socket qw(
PF_NETLINK
AF_NETLINK
SOL_NETLINK
);

our @EXPORT_OK = qw(
pack_sockaddr_nl
pack_nlattr
pack_nlmsghdr
pack_ifinfomsg
);

sub new {
my ( $class, %args ) = @_;
Expand Down
24 changes: 14 additions & 10 deletions lib/Sys/Ebpf/Link/Netlink/Xdp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package Sys::Ebpf::Link::Netlink::Xdp;
use strict;
use warnings;
use IO::Interface::Simple ();
use Sys::Ebpf::Link::Netlink::Socket;
use Sys::Ebpf::Link::Netlink::Socket
qw(pack_nlattr pack_nlmsghdr pack_ifinfomsg);
use Sys::Ebpf::Link::Netlink::Constants::Iflink qw(
IFLA_XDP
IFLA_XDP_FD
Expand All @@ -13,20 +14,23 @@ use Sys::Ebpf::Link::Netlink::Constants::Iflink qw(
XDP_FLAGS_SKB_MODE
XDP_FLAGS_DRV_MODE
);
use Sys::Ebpf::Link::Netlink::Constants::Netlink qw(
NLA_F_NESTED
NLM_F_REQUEST
NLM_F_ACK
NLMSG_HDRLEN
NLA_HDRLEN
NETLINK_ROUTE
NLMSG_ERROR
);

use Socket qw( AF_UNSPEC );
use Errno ();

# 定数の定義
use constant {
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,
RTM_SETLINK => 19,
IFF_UP => 1 << 0,
};

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

0 comments on commit 6ab3e9a

Please sign in to comment.