lib/Sys/Ebpf/Map.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Sys/Ebpf/Loader.pm ↑ Summary lib/Sys/Ebpf/Constants/BpfCmd.pm »
1 package Sys::Ebpf::Map;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use POSIX qw( unlink );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Sys::Ebpf::Constants::BpfCmd qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 BPF_MAP_CREATE
11 BPF_MAP_DELETE_ELEM
12 BPF_MAP_GET_NEXT_KEY
13 BPF_MAP_LOOKUP_ELEM
14 BPF_MAP_UPDATE_ELEM
15 BPF_OBJ_PIN
16 3 );
17 3 ▶use Sys::Ebpf::Constants::BpfMapType qw( BPF_MAP_TYPE_UNSPEC );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Sys::Ebpf::Constants::BpfMapUpdateFlags qw( BPF_ANY );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Sys::Ebpf::Constants::BpfMapCreateFlags ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 0 ▶use Sys::Ebpf::Syscall;
Subroutine BEGIN: called CC=1 SCAR=2.6
21  
22 sub new {
23 my ( $class, %args ) = @_;
24 my $self = {
25 map_name => $args{map_name},
26 map_fd => $args{map_fd},
27 map_type => $args{map_type},
28 key_size => $args{key_size},
29 value_size => $args{value_size},
30 max_entries => $args{max_entries},
31 map_flags => $args{map_flags},
32 key_schema => $args{key_schema},
33 value_schema => $args{value_schema},
34 };
35 bless $self, $class;
36 return $self;
37 }
38  
39 sub close {
40 my ($self) = @_;
41 if ( defined $self->{map_fd} && $self->{map_fd} > 0 ) {
42 my $res = POSIX::close( $self->{map_fd} );
43 if ( $res == -1 ) {
44 warn "Failed to close BPF map (fd: $self->{map_fd}): $!\n";
45 return 0;
46 }
47 $self->{map_fd} = undef;
48 return 1;
49 }
50 return 1;
51 }
52  
53 sub create {
54 my ( $class, $args ) = @_;
55  
56 my $defaults = {
57 map_name => "",
58 map_type => BPF_MAP_TYPE_UNSPEC(),
59 key_size => 0,
60 value_size => 0,
61 max_entries => 0,
62 map_flags => 0,
63 inner_map_fd => -1,
64 numa_node => 0,
65 map_ifindex => 0,
66 btf_fd => 0,
67 btf_key_type_id => 0,
68 btf_value_type_id => 0,
69 btf_vmlinux_value_type_id => 0,
70 key_schema => undef,
71 value_schema => undef,
72 };
73  
74 my $attrs = { %$defaults, %$args, };
75  
76 # key_size と value_size を自動計算
77 $attrs->{key_size} = _calculate_size( $attrs->{key_schema} )
78 if $attrs->{key_schema};
79 $attrs->{value_size} = _calculate_size( $attrs->{value_schema} )
80 if $attrs->{value_schema};
81  
82 # cf. https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h#L1459
83 my $attr = pack(
84 "L L L L L L L Z16 L L L L L",
85 $attrs->{map_type}, $attrs->{key_size},
86 $attrs->{value_size}, $attrs->{max_entries},
87 $attrs->{map_flags}, $attrs->{inner_map_fd},
88 $attrs->{numa_node}, $attrs->{map_name},
89 $attrs->{map_ifindex}, $attrs->{btf_fd},
90 $attrs->{btf_key_type_id}, $attrs->{btf_value_type_id},
91 $attrs->{btf_vmlinux_value_type_id}
92 );
93  
94 my $map_fd = syscall( Sys::Ebpf::Syscall::SYS_bpf(),
95 BPF_MAP_CREATE(), $attr, length($attr) );
96 if ( $map_fd < 0 ) {
97 my $errno = $!;
98 die "Failed to create BPF map: $errno\n";
99 }
100  
101 return $class->new(
102 map_name => $attrs->{map_name},
103 map_fd => $map_fd,
104 map_type => $attrs->{map_type},
105 key_size => $attrs->{key_size},
106 value_size => $attrs->{value_size},
107 max_entries => $attrs->{max_entries},
108 map_flags => $attrs->{map_flags},
109 key_schema => $attrs->{key_schema},
110 value_schema => $attrs->{value_schema},
111 );
112 }
113  
114 # cf. https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h#L1501
115 sub syscall_bpf_map_elem {
116 my ( $cmd, $map_fd, $key, $value, $flags ) = @_;
117  
118 my $attr = pack(
119 "L L Q Q Q",
120 $map_fd, # union bpf_attr::map_fd(32bit)
121 0, # padding (32bit)
122 unpack( "Q", pack( "P", $key ) ), # union bpf_attr::key(64bit)
123 defined($value)
124 ? unpack( "Q", pack( "P", $value ) )
125 : 0, # union bpf_attr::value(64bit), 0 if undef, delete operation case
126 $flags, # union bpf_attr::flags(64bit)
127 );
128 my $result
129 = syscall( Sys::Ebpf::Syscall::SYS_bpf(), $cmd, $attr,
130 length($attr) );
131 if ( $result < 0 ) {
132 my $errno = $!;
133 warn sprintf(
134 "syscall_bpf_map_elem failed with errno %d (%s). Command: %d, Map FD: %d, Flags: %d",
135 $errno, $!, $cmd, $map_fd, $flags );
136 }
137 return $result;
138 }
139  
140 sub raw_lookup {
141 my ( $self, $key, $flags ) = @_;
142 $flags //= 0;
143 my $value = "\0" x $self->{value_size};
144 my $res
145 = syscall_bpf_map_elem( BPF_MAP_LOOKUP_ELEM(), $self->{map_fd}, $key,
146 $value, $flags );
147 if ( $res < 0 ) {
148 my $errno = $!;
149 warn "syscall_bpf_map_elem failed with errno $errno: $!";
150 }
151  
152 return $res == 0 ? $value : undef;
153 }
154  
155 sub raw_update {
156 my ( $self, $key, $value, $flags ) = @_;
157 $flags //= BPF_ANY();
158 my $res
159 = syscall_bpf_map_elem( BPF_MAP_UPDATE_ELEM(), $self->{map_fd}, $key,
160 $value, $flags );
161 if ( $res < 0 ) {
162 my $errno = $!;
163 warn "Failed to update BPF map: $errno\n";
164 }
165 return $res;
166 }
167  
168 sub raw_delete {
169 my ( $self, $key, $flags ) = @_;
170 $flags //= BPF_ANY();
171 my $res
172 = syscall_bpf_map_elem( BPF_MAP_DELETE_ELEM(), $self->{map_fd}, $key,
173 undef, $flags );
174 if ( $res < 0 ) {
175 my $errno = $!;
176 warn "Failed to delete BPF map entry with key ",
177 unpack( "H*", $key ), ": $errno ($!)\n";
178 }
179 return $res;
180 }
181  
182 sub raw_get_next_key {
183 my ( $self, $key ) = @_;
184 my $next_key = "\0" x $self->{key_size};
185 my $res = syscall_bpf_map_elem( BPF_MAP_GET_NEXT_KEY(), $self->{map_fd},
186 $key, $next_key, 0 );
187 if ( $res < 0 ) {
188 my $errno = $!;
189 warn "Failed to get_next_key BPF map entry with key ",
190 unpack( "H*", $key ), ": $errno ($!)\n";
191 }
192 return $res == 0 ? $next_key : undef;
193 }
194  
195 sub update {
196 my ( $self, $key, $value, $flags ) = @_;
197 $flags //= BPF_ANY();
198  
199 # Serialize key and value
200 if ( !defined $self->{key_schema} || !defined $self->{value_schema} ) {
201 die "Key and value schema must be defined to use update method\n";
202 }
203 my $packed_key = $self->_serialize( $key, $self->{key_schema} );
204 my $packed_value = $self->_serialize( $value, $self->{value_schema} );
205  
206 return $self->raw_update( $packed_key, $packed_value, $flags );
207 }
208  
209 sub lookup {
210 my ( $self, $key ) = @_;
211 if ( !defined $key ) {
212 die "Key must be defined to use lookup method\n";
213 }
214 if ( !defined $self->{key_schema} || !defined $self->{value_schema} ) {
215 die "Key and value schema must be defined to use update method\n";
216 }
217 my $packed_key = $self->_serialize( $key, $self->{key_schema} );
218 my $packed_value = $self->raw_lookup($packed_key);
219 if ( !defined $packed_value ) {
220 return undef;
221 }
222 if ( $self->{value_schema} ) {
223 return $self->_deserialize( $packed_value, $self->{value_schema} );
224 }
225 return $packed_value;
226 }
227  
228 sub delete {
229 my ( $self, $key, $flags ) = @_;
230 $flags //= BPF_ANY();
231 if ( !defined $self->{key_schema} || !defined $self->{value_schema} ) {
232 die "Key and value schema must be defined to use update method\n";
233 }
234 my $packed_key = $self->_serialize( $key, $self->{key_schema} );
235 return $self->raw_delete( $packed_key, $flags );
236 }
237  
238 sub get_next_key {
239 my ( $self, $key ) = @_;
240 if ( !defined $self->{key_schema} || !defined $self->{value_schema} ) {
241 die "Key and value schema must be defined to use update method\n";
242 }
243 my $packed_key = $self->_serialize( $key, $self->{key_schema} );
244 my $next_packed_key = $self->raw_get_next_key($packed_key);
245 return undef unless defined $next_packed_key;
246 return $self->_deserialize( $next_packed_key, $self->{key_schema} );
247 }
248  
249 sub get_fd { $_[0]->{fd} }
250 sub get_name { $_[0]->{name} }
251 sub get_type { $_[0]->{type} }
252 sub get_key_size { $_[0]->{key_size} }
253 sub get_value_size { $_[0]->{value_size} }
254 sub get_max_entries { $_[0]->{max_entries} }
255 sub get_map_flags { $_[0]->{map_flags} }
256  
257 sub _calculate_size {
258 my ($schema) = @_;
259 my $size = 0;
260 for my $field (@$schema) {
261 my ( $name, $type ) = @$field;
262 $size += _get_type_size($type);
263 }
264 return $size;
265 }
266  
267 sub _serialize {
268 my ( $self, $data, $schema ) = @_;
269 my @packed;
270 for my $field (@$schema) {
271 my ( $name, $type ) = @$field;
272 my $value = $data->{$name};
273 my $packed_value = _pack_value( $type, $value );
274 push @packed, $packed_value;
275 }
276 return join( '', @packed );
277 }
278  
279 sub _deserialize {
280 my ( $self, $packed_data, $schema ) = @_;
281 my $offset = 0;
282 my $result = {};
283 for my $field (@$schema) {
284 my ( $name, $type ) = @$field;
285 my ( $value, $new_offset )
286 = _unpack_value( $type, $packed_data, $offset );
287 $result->{$name} = $value;
288 $offset = $new_offset;
289 }
290 return $result;
291 }
292  
293 sub _match_uint_or_uint_array {
294 my ($type) = @_;
295 return $type =~ /^uint(\d+)(?:\[(\d+)\])?$/ ? ( $1, $2 ) : ();
296 }
297  
298 sub _match_string {
299 my ($type) = @_;
300 return $type =~ /^string\[(\d+)\]$/ ? ( 8, $1 ) : ();
301 }
302  
303 sub _get_type_size {
304 my ($type) = @_;
305  
306 if ( my ( $bit_size, $array_size ) = _match_uint_or_uint_array($type) ) {
307 $array_size //= 1; # default to 1
308 if ( $bit_size =~ /^(8|16|32|64)$/ ) { # fixed-size integer
309 return ( $bit_size / 8 ) * $array_size;
310 }
311 }
312 elsif ( ( $bit_size, $array_size ) = _match_string($type) ) {
313 return ( $bit_size / 8 ) * $array_size;
314 }
315  
316 die "Unsupported type: $type";
317 }
318  
319 sub _pack_value {
320 my ( $type, $value ) = @_;
321  
322 if ( my ( $bit_size, $array_size ) = _match_uint_or_uint_array($type) ) {
323 $array_size //= 1; # default to 1
324 my $pack_char = _get_pack_char($bit_size);
325 return pack( "$pack_char$array_size", @$value )
326 if ref $value eq 'ARRAY';
327 return pack( $pack_char, $value );
328 }
329 elsif ( ( $bit_size, $array_size ) = _match_string($type) ) {
330 return pack( "A$array_size", $value )
331 if length($value) <= $array_size;
332 }
333  
334 die "Unsupported type: $type";
335 }
336  
337 sub _get_pack_char {
338 my ($bit_size) = @_;
339 return 'C' if $bit_size == 8;
340 return 'S' if $bit_size == 16;
341 return 'L' if $bit_size == 32;
342 return 'Q' if $bit_size == 64;
343 die "Unsupported bit size: $bit_size";
344 }
345  
346 sub _unpack_value {
347 my ( $type, $data, $offset ) = @_;
348  
349 if ( my ( $bit_size, $array_size ) = _match_uint_or_uint_array($type) ) {
350 $array_size //= 1;
351 my $pack_char = _get_pack_char($bit_size);
352 my $byte_size = ( $bit_size / 8 ) * $array_size;
353 my @values = unpack( "$pack_char$array_size",
354 substr( $data, $offset, $byte_size ) );
355  
356 # If array_size > 1, return array reference
357 if ( $array_size > 1 ) {
358 return ( \@values, $offset + $byte_size );
359 }
360  
361 # Otherwise, return the single value
362 return ( $values[0], $offset + $byte_size );
363 }
364 elsif ( ( $bit_size, $array_size ) = _match_string($type) ) {
365 my $value
366 = unpack( "A$array_size", substr( $data, $offset, $array_size ) );
367 my $len = length($value);
368 return ( $value, $offset + $len );
369 }
370  
371 die "Unsupported type: $type";
372 }
373  
374 sub unpack_at {
375 my ( $template, $data, $offset, $length ) = @_;
376 my $extracted = substr( $data, $offset, $length );
377 my @unpacked = unpack( $template, $extracted );
378  
379 #リストコンテキストで呼び出された場合(@unpacked の全要素が必要な場合)、全ての解凍された値を返します
380 # スカラーコンテキストで呼び出された場合(単一の値のみが必要な場合)、最初の解凍された値のみを返します
381 return ( wantarray ? @unpacked : $unpacked[0], $offset + $length );
382 }
383  
384 # TODO: enable file_flags
385 sub pin_bpf_map {
386 my ( $map_fd, $pin_path ) = @_;
387  
388 my $path_buf = pack( "Z*", $pin_path );
389 my $attr = pack(
390 "Q L L",
391 unpack( "Q", pack( "P", $path_buf ) ),
392 $map_fd,
393 0 # file_flags
394 );
395  
396 my $res = syscall( Sys::Ebpf::Syscall::SYS_bpf(),
397 BPF_OBJ_PIN, $attr, length($attr) );
398  
399 if ( $res < 0 ) {
400 my $errno = $!;
401 die "Failed to pin BPF map: $errno\n";
402 }
403  
404 return $res;
405 }
406  
407 # BPF_OBJ_PIN のunpinの実装
408 # return:
409 # 0: success
410 # -1: failed
411 sub unpin_bpf_map {
412 my ($pin_path) = @_;
413  
414 if ( unlink($pin_path) ) {
415 return 0;
416 }
417 return -1;
418 }
419  
420 1;
« lib/Sys/Ebpf/Loader.pm ↑ Summary lib/Sys/Ebpf/Constants/BpfCmd.pm »