blib/lib/App/Netdisco/SSHCollector/Platform/NXOS.pm untested

filter: Statementstmt 0.0% 0 / 266 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 53 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 557 CC 52 SCAR 33.8
CC
52
Cov
0%
_family_read
90.0
_listed_vrfs
42.0
macsuck
42.0
CRAP 29.4
1 package App::Netdisco::SSHCollector::Platform::NXOS;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::NXOS
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP, MAC table, and connected subnet data from Cisco NXOS devices.
10  
11 =cut
12  
13 use strict;
14 use warnings;
15  
16 use Dancer ':script';
17 use Moo;
18 use Expect;
19 use NetAddr::MAC qw/mac_as_ieee/;
20 use Regexp::Common 'net';
21 use JSON qw(decode_json);
22  
23 use App::Netdisco::Util::VRF qw/netdisco_vrf/;
24  
25 =head1 PUBLIC METHODS
26  
27 =over 4
28  
29 =item B<arpnip($host, $ssh)>
30  
31 Retrieve ARP entries from device. C<$host> is the hostname or IP address
32 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
33  
34 Returns a list of hashrefs in the format C<< { mac => MACADDR, ip => IPADDR } >>.
35 Entries from all VRFs are flattened into a single returned list.
36 Example:
37 C<< [ { ip => '192.0.2.10', mac => '0011.2233.4455' },
38 { ip => '2001:db8::1', mac => 'aabb.ccdd.eeff' } ] >>.
39  
40 =item B<arpnip_by_vrf($host, $ssh, $args)>
41  
42 As C<arpnip>, used when C<store_vrfs> is on. Reads C<show vrf | json>, then
43 sends the C<show ip arp vrf NAME | json> and C<show ipv6 neighbor vrf NAME |
44 json> commands of the listed VRFs chained with C<;>, at most 16 commands and
45 640 bytes per exec request, and names each entry's VRF from the list
46 (C<default> is C<''>). Each reply document is assigned by its own VRF name and
47 table shape, not its position. A VRF's family is read whole only when exactly
48 one document for it decodes and holds as many rows as its own count
49 (C<cnt-total> for ARP, C<count> for IPv6); a request that fails, or a document
50 that does not decode, leaves the VRF and families after it unread. VRF names
51 that differ only in case are all 0 and never queried, and make
52 C<vrf_list_complete> 0. Rows without a MAC (C<INCOMPLETE>) are skipped but
53 count as rows. Link-local neighbors are kept.
54  
55 Returns C<< { entries =E<gt> [ { ip, mac, vrf } ], vrf_read =E<gt> { VRF =E<gt> 0|1 },
56 vrf_list_complete =E<gt> 0|1, families =E<gt> [4, 6] } >>, or undef when
57 C<show vrf | json> fails or lists no VRF. A list row with no usable name, or a
58 list without C<default>, makes C<vrf_list_complete> 0; the global table is read
59 either way. A VRF named C<all> is 0 and is never queried. A VRF is 1 only when
60 both families were read whole.
61  
62 A subclass that overrides C<arpnip> must also override C<arpnip_by_vrf>, or
63 with C<store_vrfs> on this C<arpnip_by_vrf> is used instead of the override.
64  
65 =item B<macsuck($host, $ssh)>
66  
67 Retrieve MAC address table entries from device. C<$host> is the hostname or
68 IP address of the device. C<$ssh> is a Net::OpenSSH connection to the device.
69  
70 Returns a hashref keyed by VLAN and interface, where leaf keys are MAC
71 addresses, in the format
72 C<< { VLAN => { PORT => { MACADDR => COUNT } } } >>.
73 Example:
74 C<< { 10 => { 'Ethernet1/1' => { '00:11:22:33:44:55' => 1 } },
75 20 => { 'port-channel12' => { 'aa:bb:cc:dd:ee:ff' => 2 } } } >>.
76  
77 =item B<subnets($host, $ssh)>
78  
79 Retrieve directly connected IPv4 subnets from device routing tables. C<$host>
80 is the hostname or IP address of the device. C<$ssh> is a Net::OpenSSH
81 connection to the device.
82  
83 Returns a list of IPv4 CIDR strings in the format C<< X.X.X.X/NN >>.
84 Example:
85 C<< [ '10.1.1.0/24', '192.0.2.0/25' ] >>.
86  
87 =item B<subnets_by_vrf($host, $ssh, $args)>
88  
89 As C<subnets>, used when C<store_vrfs> is on, and reading IPv6 as well. Sends
90 C<show ip route direct vrf all | json> and C<show ipv6 route direct vrf all |
91 json>, each as one exec request (no pty), and names each prefix's VRF from
92 C<vrf-name-out> (the worker maps C<default> to C<''>). C<direct> lists only
93 connected and local-direct routes, so no default or learned route is returned,
94 but host routes (C</32>, C</128>) are: the worker filters them. VRF names, such
95 as C<egress-loadbalance-resolution->, are passed through as the device prints
96 them. Where NX-OS prints a hash for a one-row table, it is read as a list of
97 one.
98  
99 Returns C<< { VRF =E<gt> [ { prefix =E<gt> '10.1.1.0', len =E<gt> 24 }, ... ] } >>, or
100 undef when neither command returned usable JSON. A VRF with no connected routes
101 is absent. A command that fails contributes nothing and the other family's
102 prefixes are still returned. A failure is an SSH error (including a non-zero
103 exit, which a C<Syntax error> reply gives), a reply that does not decode, or a
104 reply that is not the expected table. Some images leave the exec channel open
105 after a complete reply; that costs the SSH inactivity timeout and that family.
106  
107 A subclass that overrides C<subnets> must also override C<subnets_by_vrf>, or
108 with C<store_vrfs> on this C<subnets_by_vrf> is used instead of the override.
109  
110 =back
111  
112 =cut
113  
114 my $if_name_map = {
115 Vl => "Vlan",
116 Lo => "Loopback",
117 Eth => "Ethernet",
118 Po => "port-channel",
119 };
120  
121 sub arpnip {
122 my ($self, $hostlabel, $ssh, $args) = @_;
123  
124 debug "$hostlabel $$ arpnip()";
125  
126 my ($pty, $pid) = $ssh->open2pty;
127 unless ($pty) {
128 warn "unable to run remote command [$hostlabel] " . $ssh->error;
129 return ();
130 }
131  
132 #$Expect::Debug = 1;
133 #$Expect::Exp_Internal = 1;
134  
135 my $expect = Expect->init($pty);
136 $expect->raw_pty(1);
137  
138 my ($pos, $error, $match, $before, $after);
139 my $prompt = qr/# +$/;
140 my $timeout = 10;
141  
142 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
143  
144 # we filter on the : in Age as the output header of the command may contain prompt chars, e.g.
145 # Flags: # - Adjacencies Throttled for Glean
146 $expect->send("show ip arp vrf all | inc ^[1-9] | no-more\n");
147 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
148  
149 my @arpentries;
150 my @data = split(/\R/, $before);
151  
152 #IP ARP Table for all contexts
153 #Total number of entries: 5
154 #Address Age MAC Address Interface
155 #192.168.228.1 00:00:43 0000.abcd.1111 mgmt0
156 #192.168.228.9 00:05:24 cccc.7777.1b1b mgmt0
157  
158 foreach (@data) {
159 my ($ip, $age, $mac, $iface) = split(/\s+/);
160  
161 if ($ip && $ip =~ m/(\d{1,3}\.){3}\d{1,3}/
162 && $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i) {
163 push(@arpentries, { ip => $ip, mac => $mac });
164 }
165 }
166  
167 $expect->send("show ipv6 neighbor vrf all | exclude Flags: | no-more\n");
168 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
169  
170 my @data6 = split(/\R/, $before);
171  
172 #IPv6 neighbors use this two-line format
173 #IPv6 Adjacency Table for all VRFs
174 #Total number of entries: 65
175 #Address Age MAC Address Pref Source Interface
176 #bff:a90:c405:120::3
177 # 00:01:46 5c71.0d42.df3f 50 icmpv6 Vlan376
178 #bff:a90:c405:120::52
179 # 3w0d 9440.c988.b6fd 50 icmpv6 Vlan376
180 # Occasionally it uses a one-line format
181 #2620:0:e50:1::b 1d01h 84b5.9ca0.bf39 50 icmpv6 Ethernet14/7
182 #2620:0:e50:1::f 1d01h cce1.7f96.1139 50 icmpv6 Ethernet14/7
183  
184 my $prevline;
185 foreach my $line (@data6) {
186 my ($addr, $age, $mac, $pref, $src, $iface) = split(/\s+/, $line);
187 # Check to see if everything is on one line
188 if ($addr and $addr =~ /$RE{net}{IPv6}/
189 and ($mac and $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i)) {
190 push(@arpentries, { ip => $addr, mac => $mac });
191 }
192 # It's on two lines
193 elsif ($mac and $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i
194 and $prevline =~ /$RE{net}{IPv6}/) {
195 push(@arpentries, { ip => $prevline, mac => $mac });
196 }
197
198 $prevline = $line;
199 }
200  
201 return @arpentries;
202 }
203  
204 my $VRF_NAME = qr/\A[A-Za-z0-9_.:-]+\z/;
205 my $COMMANDS_PER_REQUEST = 16;
206 # the live device ran a 676 byte command line; stay under what is proven
207 my $BYTES_PER_REQUEST = 640;
208  
209 # exec requests, as macsuck sends them: no pty and no echo to parse. Each
210 # exec costs a round trip, so the per-VRF commands share requests, chained
211 # with " ; ": NX-OS prints one JSON document per command, in order.
212 sub arpnip_by_vrf {
213 my ($self, $hostlabel, $ssh, $args) = @_;
214  
215 debug "$hostlabel $$ arpnip_by_vrf()";
216  
217 my ($vrfs, $list_whole, $ambiguous) = _listed_vrfs($hostlabel, $ssh);
218 unless (scalar @$vrfs) {
219 info "$hostlabel $$ show vrf | json did not return a VRF list, so VRF collection by CLI was skipped";
220 return undef;
221 }
222  
223 # "all" is refused because its reply names itself and would pass for
224 # every VRF at once
225 my @askable = grep { $_ =~ $VRF_NAME and lc $_ ne 'all' and not $ambiguous->{$_} } @$vrfs;
226 my %reads;
227 foreach my $chunk (_request_chunks(@askable)) {
228 %reads = (%reads, _read_chained($hostlabel, $ssh, $chunk));
229 }
230  
231 my (@entries, %vrf_read, %seen);
232 foreach my $vrf (@$vrfs) {
233 my $name = netdisco_vrf($vrf);
234 my $whole = 1;
235 foreach my $family (4, 6) {
236 my $read = $reads{$vrf}->{$family} || { whole => 0, entries => [] };
237 $whole = 0 unless $read->{whole};
238 foreach my $entry (@{ $read->{entries} }) {
239 next if $seen{ join '|', $name, @$entry{qw/ip mac/} }++;
240 push @entries, { %$entry, vrf => $name };
241 }
242 }
243 $vrf_read{$name} = $whole;
244 }
245  
246 return { entries => \@entries, vrf_read => \%vrf_read,
247 vrf_list_complete => $list_whole, families => [4, 6] };
248 }
249  
250 # ([names in the order NX-OS lists them], list whole, { names ambiguous by
251 # case }); no names when the list did not decode. A row that cannot be named,
252 # a list without "default", or names that differ only in case (NX-OS show
253 # commands ignore case, so neither could be read unambiguously) leave the
254 # list not whole.
255 sub _listed_vrfs {
256 my ($hostlabel, $ssh) = @_;
257 my $data = _json_reply($hostlabel, $ssh, 'show vrf | json') or return ([], 0, {});
258  
259 my (%seen, @names, %spellings);
260 my $whole = 1;
261 foreach my $row (_as_list(eval { $data->{TABLE_vrf}->{ROW_vrf} })) {
262 my $name = (ref {} eq ref $row ? $row->{vrf_name} : undef);
263 if (not defined $name or ref $name or not length $name) { $whole = 0; next }
264 next if $seen{$name}++;
265 push @names, $name;
266 push @{ $spellings{lc $name} }, $name;
267 }
268 my %ambiguous = map { ($_ => 1) } map { @$_ } grep { 1 < scalar @$_ } values %spellings;
269 $whole = 0 if scalar %ambiguous;
270 # the global table is always read; a list that hides it cannot vouch for it
271 if (@names and not grep { $_ eq 'default' } @names) {
272 push @names, 'default';
273 $whole = 0;
274 }
275 return (\@names, $whole, \%ambiguous);
276 }
277  
278 # lists of VRF names, each small enough for one chained request
279 sub _request_chunks {
280 my @names = @_;
281 my (@chunks, @current);
282 foreach my $name (@names) {
283 my $with = [ @current, $name ];
284 if (@current and (2 * @$with > $COMMANDS_PER_REQUEST
285 or length(_chain_command(@$with)) > $BYTES_PER_REQUEST)) {
286 push @chunks, [ @current ];
287 @current = ();
288 }
289 push @current, $name;
290 }
291 push @chunks, \@current if @current;
292 return @chunks;
293 }
294  
295 sub _chain_command {
296 return join ' ; ', map {
297 ("show ip arp vrf $_ | json | no-more", "show ipv6 neighbor vrf $_ | json | no-more")
298 } @_;
299 }
300  
301 # one exec request for the ARP and neighbor commands of these VRFs; returns
302 # (VRF => { 4 => read, 6 => read }). Each document is assigned by what it
303 # says (its VRF and its table shape), never by its position, so a missing or
304 # reordered document cannot shift another VRF's rows onto it. A (VRF, family)
305 # without exactly one document is unread.
306 sub _read_chained {
307 my ($hostlabel, $ssh, $vrfs) = @_;
308 my %asked = map { (lc $_ => $_) } @$vrfs;
309 my $command = _chain_command(@$vrfs);
310  
311 my @out = $ssh->capture($command);
312 if ($ssh->error) {
313 debug "$hostlabel $$ error in SSH command [$command] " . $ssh->error;
314 return ();
315 }
316  
317 my (%documents, %reads);
318 foreach my $data (_json_documents(join '', @out)) {
319 my ($vrf_row, $family) = _document_identity($data);
320 my $vrf = (defined $vrf_row ? $asked{ lc($vrf_row->{'vrf-name-out'} // '') } : undef);
321 unless (defined $vrf and $family) {
322 debug "$hostlabel $$ ignoring a document that answers no command asked";
323 next;
324 }
325 push @{ $documents{$vrf}->{$family} }, $vrf_row;
326 }
327 foreach my $vrf (keys %documents) {
328 foreach my $family (keys %{ $documents{$vrf} }) {
329 my $rows = $documents{$vrf}->{$family};
330 $reads{$vrf}->{$family} = _family_read($rows->[0], $family) if 1 == scalar @$rows;
331 }
332 }
333 return %reads;
334 }
335  
336 # the documents of a stream, up to the first one that does not decode
337 sub _json_documents {
338 my $text = shift;
339 my $parser = JSON->new;
340 $parser->incr_parse($text);
341 my @documents;
342 while (1) {
343 my $data = eval { $parser->incr_parse };
344 last unless defined $data;
345 push @documents, $data if ref {} eq ref $data;
346 }
347 return @documents;
348 }
349  
350 # (the ROW_vrf hash, 4 or 6), or nothing when the document is neither table
351 sub _document_identity {
352 my $data = shift;
353 my $vrf_row = _the_row($data->{TABLE_vrf}, 'ROW_vrf') or return ();
354 return ($vrf_row, 6) if exists $vrf_row->{TABLE_afi};
355 return ($vrf_row, 4) if exists $vrf_row->{'cnt-total'};
356 return ();
357 }
358  
359 # a reply that is a JSON object, or undef; empty output is the unknown-name form
360 sub _json_reply {
361 my ($hostlabel, $ssh, $command) = @_;
362 my @out = $ssh->capture($command);
363 if ($ssh->error) {
364 debug "$hostlabel $$ error in SSH command [$command] " . $ssh->error;
365 return undef;
366 }
367 my $data = eval { decode_json(join '', @out) };
368 return (ref {} eq ref $data ? $data : undef);
369 }
370  
371 # NX-OS gives a hash where a table has one row
372 sub _as_list {
373 my $rows = shift;
374 return (ref [] eq ref $rows ? @$rows : defined $rows ? ($rows) : ());
375 }
376  
377 # the one hash under TABLE->{ROW}, or undef
378 sub _the_row {
379 my ($table, $key) = @_;
380 my @rows = _as_list(ref {} eq ref $table ? $table->{$key} : undef);
381 return (1 == scalar @rows and ref {} eq ref $rows[0]) ? $rows[0] : undef;
382 }
383  
384 # { whole => 0|1, entries => [...] } from one document's ROW_vrf; whole needs
385 # as many rows as the document counts
386 sub _family_read {
387 my ($vrf_row, $family) = @_;
388 my $unread = { whole => 0, entries => [] };
389 my ($count_key, $ip_key) = (4 == $family) ? ('cnt-total', 'ip-addr-out') : ('count', 'ipv6-addr');
390  
391 my $holder = $vrf_row;
392 if (6 == $family) {
393 $holder = _the_row($vrf_row->{TABLE_afi}, 'ROW_afi') or return $unread;
394 return $unread unless defined $holder->{afi} and $holder->{afi} eq 'ipv6';
395 }
396 my $count = $holder->{$count_key};
397 return $unread unless defined $count and $count =~ m/\A\d+\z/;
398  
399 my @rows = grep { ref {} eq ref $_ }
400 _as_list(ref {} eq ref $holder->{TABLE_adj} ? $holder->{TABLE_adj}->{ROW_adj} : undef);
401 my @entries;
402 foreach my $row (@rows) {
403 my ($ip, $mac) = @$row{ $ip_key, 'mac' };
404 next unless defined $ip and defined $mac and $mac =~ m/\A(?:[0-9a-f]{4}\.){2}[0-9a-f]{4}\z/i;
405 push @entries, { ip => (6 == $family ? lc $ip : $ip), mac => mac_as_ieee($mac) };
406 }
407  
408 return { whole => ($count == scalar @rows ? 1 : 0), entries => \@entries };
409 }
410  
411 sub macsuck {
412 my ($self, $hostlabel, $ssh, $args) = @_;
413  
414 debug "$hostlabel $$ macsuck()";
415 # sent as an exec request: on stdin, NX-OS logs the session as a
416 # configuration from vty (#1783)
417 my @data = $ssh->capture("show mac address-table | no-more"); chomp @data;
418 if ($ssh->error) {
419 info "$hostlabel $$ error in SSH command " . $ssh->error;
420 return
421 }
422  
423 #hostname# show mac address-table
424 #Legend:
425 # * - primary entry, G - Gateway MAC, (R) - Routed MAC, O - Overlay MAC
426 # age - seconds since last seen,+ - primary entry using vPC Peer-Link,
427 # (T) - True, (F) - False, C - ControlPlane MAC, ~ - vsan,
428 # (NA)- Not Applicable
429 # VLAN MAC Address Type age Secure NTFY Ports
430 #---------+-----------------+--------+---------+------+----+------------------
431 #+ 234 d239.ea50.166a dynamic NA F F Po1122
432 #C 3 0050.5650.66d8 dynamic NA F F nve1(192.168.64.2)
433 #G - 0200.c0a8.4003 static - F F sup-eth1(R)
434 #G 1024 648f.3e48.aa4b static - F F vPC Peer-Link(R)
435 #* 1509 246e.9618.bc72 dynamic NA F F Eth1/12
436
437 my $re_mac_line = qr/^[CG\*\+]\s+([-0-9]+)\s+([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4})\s+\S+\s+([0-9]+|-|NA)\s+[FT]\s+[FT]\s+(([a-zA-Z]+)([0-9\/\.]*).*)$/i;
438 my $macentries = {};
439  
440 foreach my $line (@data) {
441 if ($line && $line =~ m/$re_mac_line/) {
442 my $port = exists $if_name_map->{$5} ? $if_name_map->{$5} . ($6 || '') : $4;
443 my $vlan = ($1 ? ($1 eq '-' ? 0 : $1) : 0);
444  
445 ++$macentries->{$vlan}->{$port}->{mac_as_ieee($2)};
446 }
447 }
448  
449 return $macentries;
450 }
451  
452 sub subnets {
453 my ($self, $hostlabel, $ssh, $args) = @_;
454  
455 debug "$hostlabel $$ subnets()";
456  
457 my ($pty, $pid) = $ssh->open2pty;
458 unless ($pty) {
459 warn "unable to run remote command [$hostlabel] " . $ssh->error;
460 return ();
461 }
462  
463 #$Expect::Debug = 1;
464 #$Expect::Exp_Internal = 1;
465  
466 my $expect = Expect->init($pty);
467 $expect->raw_pty(1);
468  
469 my ($pos, $error, $match, $before, $after);
470 my $prompt = qr/# +$/;
471 my $timeout = 10;
472  
473 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
474  
475 # IP Route Table for VRF "xyz"
476 #'*' denotes best ucast next-hop
477 #'**' denotes best mcast next-hop
478 #'[x/y]' denotes [preference/metric]
479 #'%<string>' in via output denotes VRF <string>
480 #
481 #0.0.0.0/0, ubest/mbest: 1/0 time
482 # *via 10.255.254.17, [1/0], 7w1d, static
483 #10.1.1.0/24, ubest/mbest: 1/0 time, attached
484 # *via 10.1.1.2, Vlan1234, [0/0], 1y13w, direct
485 #10.1.1.1/32, ubest/mbest: 1/0 time, attached
486 # *via 10.1.1.1, Vlan1234, [0/0], 1y13w, hsrp
487  
488 # include only lines with "attached" and exclude /32 subnets
489 $expect->send("show ip route vrf all | inc attached | exc /32 | no-more \n");
490 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
491  
492 my @subnets;
493 my @data = split(/\R/, $before);
494 foreach (@data) {
495 # subnet cidr is the first part before the comma and space
496 my ($cidr, $rest) = split(/,\s+/);
497 if ($cidr && $cidr =~ m/(\d{1,3}\.){3}\d{1,3}\/\d{1,2}/) {
498 push(@subnets, $cidr);
499 }
500 }
501 return @subnets;
502 }
503  
504 my @ROUTE_COMMANDS = (
505 { name => 'IPv4 route', command => 'show ip route direct vrf all | json' },
506 { name => 'IPv6 route', command => 'show ipv6 route direct vrf all | json' },
507 );
508  
509 sub subnets_by_vrf {
510 my ($self, $hostlabel, $ssh, $args) = @_;
511 debug "$hostlabel $$ subnets_by_vrf()";
512  
513 my (%subnets, $read_any);
514 foreach my $route (@ROUTE_COMMANDS) {
515 my @out = $ssh->capture($route->{command});
516 if ($ssh->error) {
517 info "$hostlabel $$ error running $route->{name} command: " . $ssh->error;
518 next;
519 }
520  
521 my $data = eval { decode_json(join '', @out) };
522 unless (ref {} eq ref $data and ref {} eq ref $data->{TABLE_vrf}) {
523 info "$hostlabel $$ failed to parse $route->{name} JSON";
524 next;
525 }
526  
527 $read_any = 1;
528 foreach my $vrf_row (_rows($data->{TABLE_vrf}, 'ROW_vrf')) {
529 my $name = $vrf_row->{'vrf-name-out'};
530 # an unnamable row must not be filed as a junk VRF or the global table
531 next if not defined $name or ref $name or not length $name;
532 my @prefixes = _row_prefixes($vrf_row);
533 push @{ $subnets{$name} }, @prefixes if scalar @prefixes;
534 }
535 }
536  
537 return $read_any ? \%subnets : undef;
538 }
539  
540 # the hash rows under TABLE->{ROW}, however many the device printed
541 sub _rows {
542 my ($table, $key) = @_;
543 return grep { ref {} eq ref $_ } _as_list(ref {} eq ref $table ? $table->{$key} : undef);
544 }
545  
546 # every connected prefix under one ROW_vrf, across its address families
547 sub _row_prefixes {
548 my $vrf_row = shift;
549 my @prefixes;
550 foreach my $addrf (_rows($vrf_row->{TABLE_addrf}, 'ROW_addrf')) {
551 foreach my $row (_rows($addrf->{TABLE_prefix}, 'ROW_prefix')) {
552 my $cidr = $row->{ipv6prefix} // $row->{ipprefix};
553 next unless defined $cidr and not ref $cidr;
554 my ($prefix, $len) = $cidr =~ m{^(.+)/(\d+)$} ? ($1, $2) : ($cidr, undef);
555 push @prefixes, { prefix => $prefix, len => $len };
556 }
557 }
558 return @prefixes;
559 }
560  
561 1;