| 1 | # SNMP::Info::RapidCityVRF | ||
| 2 | # | ||
| 3 | # Copyright (c) 2026 The SNMP::Info Developers | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # * Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # * Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 15 | # names of its contributors may be used to endorse or promote products | ||
| 16 | # derived from this software without specific prior written permission. | ||
| 17 | # | ||
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 22 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 28 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 29 | |||
| 30 | package SNMP::Info::RapidCityVRF; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use Exporter; | ||
| 35 | use SNMP::Info; | ||
| 36 | use SNMP::Info::VRF; | ||
| 37 | |||
| 38 | @SNMP::Info::RapidCityVRF::ISA = qw/SNMP::Info Exporter/; | ||
| 39 | @SNMP::Info::RapidCityVRF::EXPORT_OK = qw//; | ||
| 40 | |||
| 41 | our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE); | ||
| 42 | |||
| 43 | $VERSION = '3.985003'; | ||
| 44 | |||
| 45 | %MIBS = ( 'RAPID-CITY' => 'rapidCity' ); | ||
| 46 | |||
| 47 | %GLOBALS = (); | ||
| 48 | |||
| 49 | %FUNCS = ( | ||
| 50 | # RAPID-CITY::rcVrfConfigTable | ||
| 51 | 'vrf_rc_name' => 'rcVrfName', | ||
| 52 | 'vrf_rc_context' => 'rcVrfContextName', | ||
| 53 | # RAPID-CITY::rcVrfIpVpnTable | ||
| 54 | 'vrf_rc_isid' => 'rcVrfIpVpnIsidNumber', | ||
| 55 | # RAPID-CITY::rcVlanTable. RapidCity.pm reads rcVlanIfIndex and | ||
| 56 | # rcIpAdEntAddr too, as rc_vlan_if and rc_ip_addr; this mixin keeps its | ||
| 57 | # own names so it stands alone without RapidCity in @ISA. | ||
| 58 | 'vrf_rc_vlan_if' => 'rcVlanIfIndex', | ||
| 59 | 'vrf_rc_vlan_vrf' => 'rcVlanVrfId', | ||
| 60 | # RAPID-CITY::rcIpAddrTable | ||
| 61 | 'vrf_rc_ip_vrf' => 'rcIpAdEntVrfId', | ||
| 62 | 'vrf_rc_ip_addr' => 'rcIpAdEntAddr', | ||
| 63 | 'vrf_rc_ip_mask' => 'rcIpAdEntNetMask', | ||
| 64 | ); | ||
| 65 | |||
| 66 | %MUNGE = (); | ||
| 67 | |||
| 68 | # Each VOSS VRF is the context its rcVrfContextName names. | ||
| 69 | sub _context_kinds { | ||
| 70 | my ( $vrf, $feature ) = @_; | ||
| 71 | |||
| 72 | return unless ( $feature // '' ) eq 'vrf'; | ||
| 73 | return ['vrf']; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _snmp_context { | ||
| 77 | my ( $vrf, $kind, $id ) = @_; | ||
| 78 | |||
| 79 | return unless ( $kind // '' ) eq 'vrf'; | ||
| 80 | return if !defined $id or $id eq q() or $id eq 'default'; | ||
| 81 | return $vrf->_voss_snmp_context($id); | ||
| 82 | } | ||
| 83 | |||
| 84 | # VOSS reads a v2c community of the form community::vrfid in that VRF's | ||
| 85 | # context. vrfid 0, GlobalRouter, is the default context. | ||
| 86 | sub _voss_snmp_context { | ||
| 87 | my ( $vrf, $id ) = @_; | ||
| 88 | |||
| 89 | my $names = $vrf->vrf_rc_name() || {}; | ||
| 90 | my $contexts = $vrf->vrf_rc_context() || {}; | ||
| 91 | my ($vrfid) = grep { $_ ne '0' and ( $names->{$_} // q() ) eq $id } | ||
| 92 | sort keys %$names; | ||
| 93 | return unless defined $vrfid; | ||
| 94 | my $context = SNMP::Info::VRF::_vrf_text( $contexts->{$vrfid} ); | ||
| 95 | return unless defined $context; | ||
| 96 | return { | ||
| 97 | kind => 'vrf', | ||
| 98 | id => $id, | ||
| 99 | v2c => "%s::$vrfid", | ||
| 100 | v3 => $context | ||
| 101 | }; | ||
| 102 | } | ||
| 103 | |||
| 104 | # VOSS serves a community with an @ suffix from the default context, so the | ||
| 105 | # suffix never names the session's context. | ||
| 106 | sub _vrf_community_suffix_is_context { return 0; } | ||
| 107 | |||
| 108 | # The context a VOSS VRF context's own rcVrfConfigTable row names. A VRF | ||
| 109 | # context serves only its own row; the default context serves every row, | ||
| 110 | # vrfid 0 (GlobalRouter, context '') among them. '' is the default context. | ||
| 111 | # Undef over SNMPv3 (its Context decides) or when the table is not served. | ||
| 112 | # A VRF context whose one row names no context reads as "vrfid N", a context | ||
| 113 | # no instance has, so the readers refuse it instead of reading it as the | ||
| 114 | # default. | ||
| 115 | sub _vrf_community_context { | ||
| 116 | my $vrf = shift; | ||
| 117 | |||
| 118 | my $version = ( $vrf->session() || {} )->{Version}; | ||
| 119 | return if defined $version and $version eq '3'; | ||
| 120 | |||
| 121 | my $contexts = $vrf->vrf_rc_context(); | ||
| 122 | return unless $contexts and %$contexts; | ||
| 123 | my @vrfids = sort keys %$contexts; | ||
| 124 | return q() if @vrfids > 1 or grep { $_ eq '0' } @vrfids; | ||
| 125 | my $context = SNMP::Info::VRF::_vrf_text( $contexts->{ $vrfids[0] } ); | ||
| 126 | return $context if defined $context and length $context; | ||
| 127 | return "vrfid $vrfids[0]"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Each VOSS VRF context serves only its own VLANs, addresses, routes and | ||
| 131 | # neighbors; the default context maps every VRF's VLANs. | ||
| 132 | sub _vrf_context_filters { return 1; } | ||
| 133 | sub _vrf_context_ports_from_routes { return 1; } | ||
| 134 | sub _vrf_subnets_from_routes { return 1; } | ||
| 135 | sub _vrf_default_context_members { return 1; } | ||
| 136 | |||
| 137 | # rcVrfConfigTable is indexed by vrfid. vrfid 0, GlobalRouter, is the | ||
| 138 | # instance default, which any row lists; the I-SID of an IP VPN is its | ||
| 139 | # layer 3 I-SID. | ||
| 140 | sub _vrf_vendor_instances { | ||
| 141 | my $vrf = shift; | ||
| 142 | |||
| 143 | my $names = $vrf->vrf_rc_name() || {}; | ||
| 144 | my $isids = $vrf->vrf_rc_isid() || {}; | ||
| 145 | my %instances; | ||
| 146 | foreach my $vrfid ( keys %$names ) { | ||
| 147 | my $name = SNMP::Info::VRF::_vrf_text( $names->{$vrfid} ); | ||
| 148 | next if !defined $name or $vrfid eq '0' or $name eq 'default'; | ||
| 149 | |||
| 150 | my $record = SNMP::Info::VRF::_vrf_record( $name, 'vrf', 'device' ); | ||
| 151 | my $isid = $isids->{$vrfid}; | ||
| 152 | $record->{l3_isid} = $isid + 0 if defined $isid and $isid > 0; | ||
| 153 | $record->{sources}{list} = 'RAPID-CITY'; | ||
| 154 | $record->{sources}{l3_isid} = 'RAPID-CITY' | ||
| 155 | if defined $record->{l3_isid}; | ||
| 156 | $instances{$name} = $record; | ||
| 157 | } | ||
| 158 | $vrf->_vrf_list_default( \%instances, 'RAPID-CITY' ) if %$names; | ||
| 159 | return \%instances; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _vrf_vendor_list_methods { | ||
| 163 | return qw/vrf_rc_name vrf_rc_context vrf_rc_isid vrf_rc_vlan_if | ||
| 164 | vrf_rc_vlan_vrf vrf_rc_ip_vrf/; | ||
| 165 | } | ||
| 166 | |||
| 167 | # { ifIndex => name } of the VRFs other than default | ||
| 168 | sub _vrf_vendor_membership { | ||
| 169 | my $vrf = shift; | ||
| 170 | |||
| 171 | my $ports = $vrf->_voss_ports(); | ||
| 172 | return { | ||
| 173 | map { $_ => $ports->{$_} } | ||
| 174 | grep { $ports->{$_} ne 'default' } keys %$ports | ||
| 175 | }; | ||
| 176 | } | ||
| 177 | |||
| 178 | # { ifIndex => name } from rcVlanTable, each VLAN's interface in the VRF | ||
| 179 | # rcVlanVrfId gives it, and from rcIpAddrTable, indexed ifIndex then | ||
| 180 | # address, for an interface with no VLAN. vrfid 0 is default. A read of | ||
| 181 | # $name records the walks. | ||
| 182 | sub _voss_ports { | ||
| 183 | my ( $vrf, $name ) = @_; | ||
| 184 | |||
| 185 | my $names = $vrf->_voss_column( 'vrf_rc_name', $name ); | ||
| 186 | my $vlan_ifs = $vrf->_voss_column( 'vrf_rc_vlan_if', $name ); | ||
| 187 | my $vlan_ids = $vrf->_voss_column( 'vrf_rc_vlan_vrf', $name ); | ||
| 188 | my $ip_ids = $vrf->_voss_column( 'vrf_rc_ip_vrf', $name ); | ||
| 189 | |||
| 190 | my %ports; | ||
| 191 | foreach my $iid ( keys %$ip_ids ) { | ||
| 192 | my ($ifindex) = $iid =~ /^(\d+)\./ or next; | ||
| 193 | my $vrf_name = _voss_vrf_name( $names, $ip_ids->{$iid} ); | ||
| 194 | $ports{$ifindex} = $vrf_name if defined $vrf_name; | ||
| 195 | } | ||
| 196 | foreach my $vlan ( keys %$vlan_ids ) { | ||
| 197 | my $vrf_name = _voss_vrf_name( $names, $vlan_ids->{$vlan} ); | ||
| 198 | $ports{ $vlan_ifs->{$vlan} } = $vrf_name | ||
| 199 | if defined $vrf_name and defined $vlan_ifs->{$vlan}; | ||
| 200 | } | ||
| 201 | return \%ports; | ||
| 202 | } | ||
| 203 | |||
| 204 | # A walk that counts toward the read of $name, or a plain one without | ||
| 205 | sub _voss_column { | ||
| 206 | my ( $vrf, $method, $name ) = @_; | ||
| 207 | |||
| 208 | return $vrf->_vrf_column( $method, $name ) if defined $name; | ||
| 209 | return $vrf->$method() || {}; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub _voss_vrf_name { | ||
| 213 | my ( $names, $vrfid ) = @_; | ||
| 214 | |||
| 215 | return unless defined $vrfid; | ||
| 216 | return 'default' if $vrfid eq '0'; | ||
| 217 | return SNMP::Info::VRF::_vrf_text( $names->{$vrfid} ); | ||
| 218 | } | ||
| 219 | |||
| 220 | # VOSS: the ports this context's rcVlanTable and rcIpAddrTable give the VRF, | ||
| 221 | # the interfaces of its connected routes, and those of the ipAddrTable | ||
| 222 | # addresses a connected route names as its next hop. A route learned over | ||
| 223 | # the fabric sits on the fabric VLAN's interface, which is no port of the | ||
| 224 | # VRF. | ||
| 225 | sub _vrf_route_ports { | ||
| 226 | my ( $vrf, $name ) = @_; | ||
| 227 | |||
| 228 | my $mapped = $vrf->_voss_ports($name); | ||
| 229 | my %ports = map { $_ => $name } grep { $mapped->{$_} eq $name } | ||
| 230 | keys %$mapped; | ||
| 231 | my $ifs = $vrf->_vrf_column( 'vrf_cidr_if', $name ); | ||
| 232 | my $protos = $vrf->_vrf_column( 'vrf_cidr_proto', $name ); | ||
| 233 | foreach my $iid ( keys %$protos ) { | ||
| 234 | next unless SNMP::Info::VRF::_vrf_connected_proto( $protos->{$iid} ); | ||
| 235 | my $ifindex = SNMP::Info::VRF::_vrf_route_ifindex( $ifs->{$iid} ); | ||
| 236 | $ports{$ifindex} = $name if defined $ifindex; | ||
| 237 | } | ||
| 238 | $ports{$_} = $name for $vrf->_voss_next_hop_ports($name); | ||
| 239 | return \%ports; | ||
| 240 | } | ||
| 241 | |||
| 242 | # The default context's ipAddrTable places the management CLIP's address | ||
| 243 | # on the CLIP's ifIndex, while its local host route sits on the management | ||
| 244 | # VLAN's interface: the route's next hop is the address, so the CLIP is a | ||
| 245 | # port of the context. Other VRFs' addresses there have no such route. | ||
| 246 | sub _voss_next_hop_ports { | ||
| 247 | my ( $vrf, $name ) = @_; | ||
| 248 | |||
| 249 | my %hops = map { join( q{.}, @{ $_->{hop} } ) => 1 } | ||
| 250 | grep { $_->{family} eq 'ipv4' } | ||
| 251 | @{ $vrf->_voss_connected_routes($name) }; | ||
| 252 | my $rows = $vrf->_vrf_ipadent_rows($name); | ||
| 253 | return map { $_->{ifindex} } | ||
| 254 | grep { !SNMP::Info::VRF::_vrf_unspecified( $_->{ip} ) } | ||
| 255 | grep { $hops{ $_->{ip} } } values %$rows; | ||
| 256 | } | ||
| 257 | |||
| 258 | # VOSS indexes inetCidrRouteTable with no length octet before either | ||
| 259 | # address: type, destination, prefix length, policy 0, next hop type, next | ||
| 260 | # hop. Gives { family, dest, len, hop }, the addresses as octet lists, or | ||
| 261 | # nothing for another shape. | ||
| 262 | my %VOSS_INET = ( 1 => [ ipv4 => 4 ], 2 => [ ipv6 => 16 ] ); | ||
| 263 | |||
| 264 | sub _voss_route_index { | ||
| 265 | my $iid = shift; | ||
| 266 | |||
| 267 | return unless defined $iid and $iid =~ /^\d+(?:\.\d+)*$/; | ||
| 268 | my ( $type, @arcs ) = split /\./, $iid; | ||
| 269 | my ( $family, $bytes ) = @{ $VOSS_INET{$type} || [] } or return; | ||
| 270 | return if @arcs < $bytes + 3; | ||
| 271 | my @dest = splice @arcs, 0, $bytes; | ||
| 272 | my ( $length, $policy, $hop_type ) = splice @arcs, 0, 3; | ||
| 273 | my ( undef, $hop_bytes ) = @{ $VOSS_INET{$hop_type} || [] } or return; | ||
| 274 | return if $policy ne '0' or @arcs != $hop_bytes or $length > $bytes * 8; | ||
| 275 | return if grep { $_ > 255 } @dest, @arcs; | ||
| 276 | return { | ||
| 277 | family => $family, | ||
| 278 | dest => \@dest, | ||
| 279 | len => $length + 0, | ||
| 280 | hop => \@arcs | ||
| 281 | }; | ||
| 282 | } | ||
| 283 | |||
| 284 | # The connected routes of this context, each with its interface | ||
| 285 | sub _voss_connected_routes { | ||
| 286 | my ( $vrf, $name ) = @_; | ||
| 287 | |||
| 288 | my $protos = $vrf->_vrf_column( 'vrf_cidr_proto', $name ); | ||
| 289 | my $ifs = $vrf->_vrf_column( 'vrf_cidr_if', $name ); | ||
| 290 | my @routes; | ||
| 291 | foreach my $iid ( sort keys %$protos ) { | ||
| 292 | next unless SNMP::Info::VRF::_vrf_connected_proto( $protos->{$iid} ); | ||
| 293 | my $route = _voss_route_index($iid) or next; | ||
| 294 | $route->{ifindex} | ||
| 295 | = SNMP::Info::VRF::_vrf_route_ifindex( $ifs->{$iid} ); | ||
| 296 | push @routes, $route; | ||
| 297 | } | ||
| 298 | return \@routes; | ||
| 299 | } | ||
| 300 | |||
| 301 | # VOSS: the connected routes, parsed from the index VOSS serves; a host | ||
| 302 | # route is no subnet | ||
| 303 | sub _vrf_context_route_subnets { | ||
| 304 | my ( $vrf, $name ) = @_; | ||
| 305 | |||
| 306 | my @subnets; | ||
| 307 | foreach my $route ( @{ $vrf->_voss_connected_routes($name) } ) { | ||
| 308 | next if $route->{len} >= 8 * @{ $route->{dest} }; | ||
| 309 | my $prefix = SNMP::Info::VRF::_vrf_network( $route->{family}, | ||
| 310 | $route->{dest}, $route->{len} ) | ||
| 311 | or next; | ||
| 312 | push @subnets, { | ||
| 313 | prefix => $prefix, | ||
| 314 | len => $route->{len}, | ||
| 315 | family => $route->{family}, | ||
| 316 | ifindex => $route->{ifindex}, | ||
| 317 | }; | ||
| 318 | } | ||
| 319 | push @subnets, @{ $vrf->_voss_standard_route_subnets($name) }; | ||
| 320 | return \@subnets; | ||
| 321 | } | ||
| 322 | |||
| 323 | # Connected routes in the standard index shape, which the VOSS parser | ||
| 324 | # rejects, read by the standard parser | ||
| 325 | sub _voss_standard_route_subnets { | ||
| 326 | my ( $vrf, $name ) = @_; | ||
| 327 | |||
| 328 | my $protos = $vrf->_vrf_column( 'vrf_cidr_proto', $name ); | ||
| 329 | my $ifs = $vrf->_vrf_column( 'vrf_cidr_if', $name ); | ||
| 330 | my @subnets; | ||
| 331 | foreach my $iid ( sort keys %$protos ) { | ||
| 332 | next unless SNMP::Info::VRF::_vrf_connected_proto( $protos->{$iid} ); | ||
| 333 | next if _voss_route_index($iid) or $iid !~ /^\d+(?:\.\d+)*$/; | ||
| 334 | my $subnet = SNMP::Info::VRF::_vrf_route_subnet( split /\./, $iid ) | ||
| 335 | or next; | ||
| 336 | $subnet->{ifindex} | ||
| 337 | = SNMP::Info::VRF::_vrf_route_ifindex( $ifs->{$iid} ); | ||
| 338 | push @subnets, $subnet; | ||
| 339 | } | ||
| 340 | return \@subnets; | ||
| 341 | } | ||
| 342 | |||
| 343 | # VOSS: a VRF context serves no IP-MIB address table. Its IPv4 addresses | ||
| 344 | # are the rcIpAddrTable rows whose rcIpAdEntVrfId is the VRF's; in the | ||
| 345 | # default context those of vrfid 0 add to ipAddrTable. IPv6 addresses are | ||
| 346 | # in IPV6-MIB ipv6AddrTable. | ||
| 347 | sub _vrf_vendor_address_rows { | ||
| 348 | my ( $vrf, $name ) = @_; | ||
| 349 | |||
| 350 | return { | ||
| 351 | %{ $vrf->_voss_rc_ip_addresses($name) }, | ||
| 352 | %{ $vrf->_vrf_ipv6_mib_address_rows($name) }, | ||
| 353 | }; | ||
| 354 | } | ||
| 355 | |||
| 356 | # rcIpAddrTable is indexed by ifIndex then address, so an address two VRFs | ||
| 357 | # share on their own VLANs is two rows | ||
| 358 | sub _voss_rc_ip_addresses { | ||
| 359 | my ( $vrf, $name ) = @_; | ||
| 360 | |||
| 361 | my $names = $vrf->_vrf_column( 'vrf_rc_name', $name ); | ||
| 362 | my $vrfs = $vrf->_vrf_column( 'vrf_rc_ip_vrf', $name ); | ||
| 363 | my $ips = $vrf->_vrf_column( 'vrf_rc_ip_addr', $name ); | ||
| 364 | my $masks = $vrf->_vrf_column( 'vrf_rc_ip_mask', $name ); | ||
| 365 | my %rows; | ||
| 366 | foreach my $iid ( keys %$ips ) { | ||
| 367 | my ($ifindex) = $iid =~ /^(\d+)\./ or next; | ||
| 368 | my $vrf_name = _voss_vrf_name( $names, $vrfs->{$iid} ); | ||
| 369 | next unless defined $vrf_name and $vrf_name eq $name; | ||
| 370 | $rows{"$ifindex.$ips->{$iid}"} = { | ||
| 371 | ifindex => $ifindex, | ||
| 372 | ip => $ips->{$iid}, | ||
| 373 | prefix_len => | ||
| 374 | scalar SNMP::Info::VRF::_vrf_mask_length( $masks->{$iid} ), | ||
| 375 | family => 'ipv4', | ||
| 376 | }; | ||
| 377 | } | ||
| 378 | return \%rows; | ||
| 379 | } | ||
| 380 | |||
| 381 | 1; | ||
| 382 | |||
| 383 | __END__ |