| 1 | |||
| 2 | use v5.20; | ||
| 3 | use warnings; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | |||
| 6 | package Protocol::Sys::Virt::Devel v1.0.1; | ||
| 7 | |||
| 8 | use parent 'Exporter'; | ||
| 9 | our @EXPORT_OK = qw( extract_all ); | ||
| 10 | |||
| 11 | use File::Spec; | ||
| 12 | use List::Util qw(pairfirst); | ||
| 13 | use XDR::Parse; | ||
| 14 | |||
| 15 | |||
| 16 | my @modules = ( | ||
| 17 | { | ||
| 18 | name => 'transport', | ||
| 19 | definitions => 'src/rpc/virnetprotocol.x', | ||
| 20 | }, | ||
| 21 | { | ||
| 22 | name => 'keepalive', | ||
| 23 | definitions => 'src/rpc/virkeepaliveprotocol.x', | ||
| 24 | }, | ||
| 25 | { | ||
| 26 | name => 'remote', | ||
| 27 | definitions => 'src/remote/remote_protocol.x', | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | my %headers = ( | ||
| 32 | 'include/libvirt/libvirt-common.h.in' => { | ||
| 33 | module => [ | ||
| 34 | qr// => 'Client', | ||
| 35 | ], | ||
| 36 | }, | ||
| 37 | 'include/libvirt/libvirt-host.h' => { | ||
| 38 | module => [ | ||
| 39 | qr// => 'Client', | ||
| 40 | ], | ||
| 41 | }, | ||
| 42 | 'include/libvirt/libvirt-domain.h' => { | ||
| 43 | module => [ | ||
| 44 | qr/^VIR_DOMAIN_(DEFINE_|EVENT_ID_)/ => 'Client', | ||
| 45 | qr/^VIR_(DOMAIN_|KEYCODE_|PERF_|VCPU_|MEMORY_|MIGRATE_|DUMP_)/ => 'Domain', | ||
| 46 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 47 | ], | ||
| 48 | }, | ||
| 49 | 'include/libvirt/libvirt-domain-checkpoint.h' => { | ||
| 50 | module => [ | ||
| 51 | qr/^VIR_DOMAIN_CHECKPOINT_CREATE/ => 'Domain', | ||
| 52 | qr/^VIR_DOMAIN_CHECKPOINT_/ => 'DomainCheckpoint', | ||
| 53 | ], | ||
| 54 | }, | ||
| 55 | 'include/libvirt/libvirt-domain-snapshot.h' => { | ||
| 56 | module => [ | ||
| 57 | qr/^VIR_DOMAIN_SNAPSHOT_CREATE_/ => 'Domain', | ||
| 58 | qr/^VIR_DOMAIN_SNAPSHOT_/ => 'DomainSnapshot', | ||
| 59 | ], | ||
| 60 | }, | ||
| 61 | 'include/libvirt/libvirt-interface.h' => { | ||
| 62 | module => [ | ||
| 63 | qr/^VIR_INTERFACE_DEFINE_/ => 'Client', | ||
| 64 | qr/^VIR_INTERFACE_/ => 'Interface', | ||
| 65 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 66 | ], | ||
| 67 | }, | ||
| 68 | 'include/libvirt/libvirt-network.h' => { | ||
| 69 | module => [ | ||
| 70 | qr/^VIR_NETWORK_(DEFINE_|CREATE_|EVENT_ID_)/ => 'Client', | ||
| 71 | qr/^VIR_NETWORK_PORT_CREATE_/ => 'Network', | ||
| 72 | qr/^VIR_NETWORK_PORT_/ => 'NetworkPort', | ||
| 73 | qr/^VIR_(NETWORK_|IP_)/ => 'Network', | ||
| 74 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 75 | ], | ||
| 76 | }, | ||
| 77 | 'include/libvirt/libvirt-nodedev.h' => { | ||
| 78 | module => [ | ||
| 79 | qr/^VIR_NODE_DEVICE_(DEFINE_|CREATE_|EVENT_ID_)/ => 'Client', | ||
| 80 | qr/^VIR_NODE_DEVICE_/ => 'NodeDevice', | ||
| 81 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 82 | ], | ||
| 83 | }, | ||
| 84 | 'include/libvirt/libvirt-nwfilter.h' => { | ||
| 85 | module => [ | ||
| 86 | qr/^VIR_NWFILTER_DEFINE_/ => 'Client', | ||
| 87 | qr/^VIR_NWFILTER_BINDING_CREATE_/ => 'Client', | ||
| 88 | qr/^VIR_NWFILTER_BINDING_/ => 'NwFilterBinding', # no occurrances in libvirt 10.3.0 | ||
| 89 | qr/^VIR_NWFILTER_/ => 'NwFilter', # no occurrances in libvirt 10.3.0 | ||
| 90 | ], | ||
| 91 | }, | ||
| 92 | 'src/libvirt_internal.h' => { | ||
| 93 | module => [ | ||
| 94 | qr/^VIR_DRV_FEATURE_/ => 'Remote', | ||
| 95 | ], | ||
| 96 | }, | ||
| 97 | 'include/libvirt/libvirt-secret.h' => { | ||
| 98 | module => [ | ||
| 99 | qr/^VIR_SECRET_(DEFINE_|CREATE_|EVENT_ID_)/ => 'Client', | ||
| 100 | qr/^VIR_SECRET_USAGE_/ => 'Client', | ||
| 101 | qr/^VIR_SECRET_/ => 'Secret', | ||
| 102 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 103 | ], | ||
| 104 | }, | ||
| 105 | 'include/libvirt/libvirt-storage.h' => { | ||
| 106 | module => [ | ||
| 107 | qr/^VIR_STORAGE_POOL_(CREATE_|DEFINE_|EVENT_ID_)/ => 'Client', | ||
| 108 | qr/^VIR_STORAGE_VOL_CREATE_/ => 'Client', | ||
| 109 | qr/^VIR_STORAGE_POOL_/ => 'StoragePool', | ||
| 110 | qr/^VIR_STORAGE_VOL_/ => 'StorageVol', | ||
| 111 | qr/^VIR_STORAGE_XML/ => 'StoragePool', | ||
| 112 | qr/^VIR_CONNECT_/ => 'Client', | ||
| 113 | ], | ||
| 114 | }, | ||
| 115 | 'include/libvirt/libvirt-stream.h' => { | ||
| 116 | module => [ | ||
| 117 | qr/^VIR_STREAM_/ => 'Stream', | ||
| 118 | ], | ||
| 119 | }, | ||
| 120 | ); | ||
| 121 | |||
| 122 | my $parser = XDR::Parse->new; | ||
| 123 | |||
| 124 | sub _find_toplevel_definition { | ||
| 125 | my ($ast, $name) = @_; | ||
| 126 | for my $def (@{ $ast } ) { | ||
| 127 | next if $def->{def} eq 'passthrough'; | ||
| 128 | next if $def->{def} eq 'preprocessor'; | ||
| 129 | |||
| 130 | return $def | ||
| 131 | if $def->{name}->{content} eq $name; | ||
| 132 | } | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _ast($filename) { | ||
| 137 | open my $fh, '<:encoding(UTF-8)', $filename | ||
| 138 | or die "Error opening '$filename': $!"; | ||
| 139 | |||
| 140 | return $parser->parse( $fh ); | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | my %prefix_strip_h = ( | ||
| 146 | Client => qr/^VIR_(CONNECT_|NODE_(?!DEVICE_))?/, | ||
| 147 | Domain => qr/^VIR_(DOMAIN_)?/, | ||
| 148 | DomainCheckpoint => qr/^VIR_(DOMAIN_CHECKPOINT_)?/, | ||
| 149 | DomainSnapshot => qr/^VIR_(DOMAIN_SNAPSHOT_)?/, | ||
| 150 | Interface => qr/^VIR_(INTERFACE_)?/, | ||
| 151 | Network => qr/^VIR_(NETWORK_)?/, | ||
| 152 | NetworkPort => qr/^VIR_(NETWORK_PORT_)?/, | ||
| 153 | NwFilter => qr/^VIR_(NWFILTER_)?/, | ||
| 154 | NwFilterBinding => qr/^VIR_(NWFILTER_BINDING_)?/, | ||
| 155 | NodeDevice => qr/^VIR_(NODE_DEVICE_)?/, | ||
| 156 | Remote => qr/^VIR_/, | ||
| 157 | Secret => qr/^VIR_(SECRET_)?/, | ||
| 158 | StoragePool => qr/^VIR_(STORAGE_(POOL_)?)?/, | ||
| 159 | StorageVol => qr/^VIR_(STORAGE_(VOL_)?)?/, | ||
| 160 | Stream => qr/^VIR_STREAM_/, | ||
| 161 | ); | ||
| 162 | |||
| 163 | sub _strip_number_suffix { | ||
| 164 | my $v = ($_[0] =~ s/([0-9]+)(?:u|l|ul|ll|ull)(\s|$)/$1$2/igr); | ||
| 165 | $v; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _trim { | ||
| 169 | my $v = ($_[0] =~ s/^\s*//r); | ||
| 170 | $v =~ s/\s*$//; | ||
| 171 | $v; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _header_constant_extractor($libvirt, $header) { | ||
| 175 | my @syms; | ||
| 176 | |||
| 177 | open my $fh, '<:encoding(UTF-8)', File::Spec->catfile($libvirt, $header) | ||
| 178 | or die "Error opening '$header': $!"; | ||
| 179 | |||
| 180 | my $in_enum = 0; | ||
| 181 | while (my $line = <$fh>) { | ||
| 182 | if (not $in_enum | ||
| 183 | and $line =~ m/^\s*typedef\s+enum\s*{/) { | ||
| 184 | $in_enum = 1; | ||
| 185 | next; | ||
| 186 | } | ||
| 187 | $line =~ s{/\*([^*]|\*[^/])*\*/}{}g | ||
| 188 | if $in_enum; | ||
| 189 | if ($in_enum | ||
| 190 | and $line =~ m/^\s*}/) { | ||
| 191 | $in_enum = 0; | ||
| 192 | next; | ||
| 193 | } | ||
| 194 | if ($in_enum | ||
| 195 | and $line =~ m{\s*(VIR_[A-Z0-9_]+)\s*=\s*((?:[^,/\n]|/(?!\*))+)}) { | ||
| 196 | my ($orig, $val) = ($1, $2); | ||
| 197 | $val = _strip_number_suffix(_trim($val)); | ||
| 198 | my (undef, $module) = pairfirst {; | ||
| 199 | $orig =~ m/$a/ | ||
| 200 | } @{ $headers{$header}->{module} }; | ||
| 201 | die "No module for constant $orig in $header" | ||
| 202 | unless $module; | ||
| 203 | my $sym = ($orig =~ s/$prefix_strip_h{$module}//r); | ||
| 204 | push @syms, { | ||
| 205 | sym => $sym, | ||
| 206 | value => $val, | ||
| 207 | orig => $orig, | ||
| 208 | mod => $module | ||
| 209 | }; | ||
| 210 | } | ||
| 211 | if ($line =~ m{#\s*define (VIR_[A-Z0-9_]+)\s+((?:[^\n/]|/(?!\*))+)}) { | ||
| 212 | my ($orig, $val) = ($1, $2); | ||
| 213 | $val = _strip_number_suffix(_trim($val)); | ||
| 214 | next if $orig =~ m/^(VIR_DEPRECATED|VIR_EXPORT_VAR)$/; # for C programs, but not for us... | ||
| 215 | next if $val =~ m/^\@.*\@$/; # defined by autoconf macro... can't include | ||
| 216 | my (undef, $module) = pairfirst {; | ||
| 217 | $orig =~ m/$a/ | ||
| 218 | } @{ $headers{$header}->{module} }; | ||
| 219 | die "No module for constant $orig in $header" | ||
| 220 | unless $module; | ||
| 221 | my $sym = ($orig =~ s/$prefix_strip_h{$module}//r); | ||
| 222 | push @syms, { sym => $sym, value => $val, orig => $orig, mod => $module }; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | return @syms; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _estimate_entrypoint($proc_name) { | ||
| 230 | $proc_name = (lc($proc_name) =~ s/remote_proc_/vir_/ir); | ||
| 231 | return $proc_name =~ s/_//igr; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub _header_entrypoint_extractor($libvirt, $ast) { | ||
| 235 | # find toplevel definition 'remote_procedure' and extract the enum members | ||
| 236 | my $procs = _find_toplevel_definition( $ast, 'remote_procedure' ); | ||
| 237 | my %ep_map = ( | ||
| 238 | map { | ||
| 239 | _estimate_entrypoint($_->{name}->{content}) => $_->{name}->{content} | ||
| 240 | } @{ $procs->{definition}->{type}->{declaration}->{elements} } ); | ||
| 241 | |||
| 242 | my $entrypoints = join('|', keys %ep_map); | ||
| 243 | my $ep_regex = qr/(?i)\b($entrypoints)\b/; | ||
| 244 | |||
| 245 | my %actual; | ||
| 246 | for my $header (sort keys %headers) { | ||
| 247 | open my $fh, '<:encoding(UTF-8)', File::Spec->catfile($libvirt, $header) | ||
| 248 | or die "Error opening '$header': $!"; | ||
| 249 | |||
| 250 | my $in_comment = 0; | ||
| 251 | my $lineno = 0; | ||
| 252 | while (my $line = <$fh>) { | ||
| 253 | $lineno++; | ||
| 254 | if ($in_comment) { | ||
| 255 | # strip out everything up to the closing '*/' | ||
| 256 | if ($line =~ s|.*?\*/||) { | ||
| 257 | # stipped; continue regular processing | ||
| 258 | $in_comment = 0; | ||
| 259 | } | ||
| 260 | else { | ||
| 261 | # the entire line is part of the comment | ||
| 262 | # don't process it any further | ||
| 263 | next; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | $line =~ s|#.*$||; # remove trailing comments | ||
| 267 | $line =~ s|/\*.*?\*/||g; # remove inline comments | ||
| 268 | if ($line =~ s|/\*.*$||) { | ||
| 269 | $in_comment = 1; | ||
| 270 | } | ||
| 271 | if ($line =~ m/$ep_regex/) { | ||
| 272 | my $proc = $ep_map{lc($1)}; | ||
| 273 | $actual{$proc} = { | ||
| 274 | name => $1, | ||
| 275 | header => $header, | ||
| 276 | lineno => $lineno, | ||
| 277 | }; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | return \%actual; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub extract_all($libvirt) { | ||
| 285 | my @h_syms; | ||
| 286 | for my $header (sort keys %headers) { | ||
| 287 | push @h_syms, | ||
| 288 | _header_constant_extractor( $libvirt, $header ); | ||
| 289 | } | ||
| 290 | my %h_sym_values; | ||
| 291 | for my $h_sym (@h_syms) { | ||
| 292 | $h_sym_values{$h_sym->{orig}} = $h_sym->{value}; | ||
| 293 | } | ||
| 294 | for my $key (keys %h_sym_values) { | ||
| 295 | if (exists $h_sym_values{$h_sym_values{$key}}) { | ||
| 296 | $h_sym_values{$key} = $h_sym_values{$h_sym_values{$key}}; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | for my $val (values %h_sym_values) { | ||
| 300 | die "Unresolved value: $val" | ||
| 301 | if $val =~ m/^[A-Z_]+$/; | ||
| 302 | } | ||
| 303 | for my $h_sym (@h_syms) { | ||
| 304 | $h_sym->{resolved} = $h_sym_values{$h_sym->{orig}}; | ||
| 305 | } | ||
| 306 | my $ast = { | ||
| 307 | map { | ||
| 308 | $_->{name} => | ||
| 309 | _ast(File::Spec->catfile($libvirt, $_->{definitions})) | ||
| 310 | } @modules | ||
| 311 | }; | ||
| 312 | my $ep_map = _header_entrypoint_extractor($libvirt, $ast->{remote}); | ||
| 313 | |||
| 314 | return { | ||
| 315 | ast => $ast, | ||
| 316 | header_syms => \@h_syms, | ||
| 317 | proc_entrypoints => $ep_map, | ||
| 318 | xdr_parse_version => $XDR::Parse::VERSION, | ||
| 319 | }; | ||
| 320 | } | ||
| 321 | |||
| 322 | 1; | ||
| 323 | |||
| 324 | |||
| 325 | __END__ |