| 1 | # SNMP::Info::Layer3::SONiC - SNMP interface to SONiC switches | ||
| 2 | # | ||
| 3 | # Copyright (c) 2026 by SNMP::Info Developers. | ||
| 4 | # | ||
| 5 | # Redistribution and use in source and binary forms, with or without | ||
| 6 | # modification, are permitted provided that the following conditions are met: | ||
| 7 | # | ||
| 8 | # * Redistributions of source code must retain the above copyright notice, | ||
| 9 | # this list of conditions and the following disclaimer. | ||
| 10 | # * Redistributions in binary form must reproduce the above copyright | ||
| 11 | # notice, this list of conditions and the following disclaimer in the | ||
| 12 | # documentation and/or other materials provided with the distribution. | ||
| 13 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 14 | # names of its contributors may be used to endorse or promote products | ||
| 15 | # derived from this software without specific prior written permission. | ||
| 16 | # | ||
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | |||
| 29 | package SNMP::Info::Layer3::SONiC; | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | use Exporter; | ||
| 34 | use SNMP::Info::Layer3::NetSNMP; | ||
| 35 | |||
| 36 | @SNMP::Info::Layer3::SONiC::ISA = qw/SNMP::Info::Layer3::NetSNMP Exporter/; | ||
| 37 | @SNMP::Info::Layer3::SONiC::EXPORT_OK = qw//; | ||
| 38 | |||
| 39 | our ( $VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE ); | ||
| 40 | |||
| 41 | $VERSION = '3.985003'; | ||
| 42 | |||
| 43 | %MIBS = ( %SNMP::Info::Layer3::NetSNMP::MIBS, ); | ||
| 44 | |||
| 45 | %GLOBALS = ( | ||
| 46 | %SNMP::Info::Layer3::NetSNMP::GLOBALS, | ||
| 47 | |||
| 48 | # LLDP-MIB lldpLocChassisIdSubtype and lldpLocChassisId, as community | ||
| 49 | # builds serve them: without the .0 instance | ||
| 50 | 'sonic_loc_id_type' => '.1.0.8802.1.1.2.1.3.1', | ||
| 51 | 'sonic_loc_id_os' => '.1.0.8802.1.1.2.1.3.2', | ||
| 52 | ); | ||
| 53 | |||
| 54 | %FUNCS = ( %SNMP::Info::Layer3::NetSNMP::FUNCS, ); | ||
| 55 | |||
| 56 | %MUNGE = ( %SNMP::Info::Layer3::NetSNMP::MUNGE, ); | ||
| 57 | |||
| 58 | # HwSku names start with the hardware maker; Force10 and DellEMC are Dell's. | ||
| 59 | my %VENDOR_OF_SKU_PREFIX = ( | ||
| 60 | accton => 'accton', | ||
| 61 | dell => 'dell', | ||
| 62 | dellemc => 'dell', | ||
| 63 | force10 => 'dell', | ||
| 64 | ); | ||
| 65 | |||
| 66 | my $MAC_RE = qr/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/; | ||
| 67 | |||
| 68 | # sysServices is fixed at 72 (layers 4 and 7) in the SNMP container's | ||
| 69 | # snmpd.conf, whatever the switch does. | ||
| 70 | sub layers { | ||
| 71 | my $sonic = shift; | ||
| 72 | |||
| 73 | my $layers = $sonic->SUPER::layers() || '00000000'; | ||
| 74 | substr( $layers, 5, 2, '11' ); | ||
| 75 | return $layers; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub vendor { | ||
| 79 | my $sonic = shift; | ||
| 80 | |||
| 81 | my $sku = $sonic->model() || ''; | ||
| 82 | if ( $sku =~ /^([A-Za-z0-9]+)-/ ) { | ||
| 83 | my $vendor = $VENDOR_OF_SKU_PREFIX{ lc $1 }; | ||
| 84 | return $vendor if $vendor; | ||
| 85 | } | ||
| 86 | return 'sonic'; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub os { return 'sonic' } | ||
| 90 | |||
| 91 | sub os_ver { | ||
| 92 | my $sonic = shift; | ||
| 93 | |||
| 94 | my $descr = $sonic->description() || ''; | ||
| 95 | return $1 if $descr =~ /^"?SONiC Software Version:\s+SONiC\.([^\s"]+)/; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub model { | ||
| 100 | my $sonic = shift; | ||
| 101 | |||
| 102 | my $descr = $sonic->description() || ''; | ||
| 103 | return $1 if $descr =~ /\s-\sHwSku:\s+([^\s"]+)/; | ||
| 104 | return $sonic->SUPER::model(); | ||
| 105 | } | ||
| 106 | |||
| 107 | # Layer3::NetSNMP returns no serial for a generic Linux host; a switch | ||
| 108 | # serves its chassis serial in ENTITY-MIB. | ||
| 109 | sub serial { | ||
| 110 | my $sonic = shift; | ||
| 111 | return $sonic->SNMP::Info::Layer3::serial(); | ||
| 112 | } | ||
| 113 | |||
| 114 | # The ifIndex column holds the row's index minus one, so key each | ||
| 115 | # interface by its row instead. | ||
| 116 | sub i_index { | ||
| 117 | my $sonic = shift; | ||
| 118 | my $partial = shift; | ||
| 119 | |||
| 120 | my $i_descr = $sonic->i_description($partial) || {}; | ||
| 121 | return { map { $_ => $_ } keys %$i_descr }; | ||
| 122 | } | ||
| 123 | |||
| 124 | # Community builds serve no dot1dBasePortTable, and dot1qTpFdbPort holds | ||
| 125 | # an ifIndex there, so each interface is its own bridge port. | ||
| 126 | sub bp_index { | ||
| 127 | my $sonic = shift; | ||
| 128 | |||
| 129 | my $bp_index = $sonic->SUPER::bp_index(); | ||
| 130 | return $bp_index if ref {} eq ref $bp_index and scalar keys %$bp_index; | ||
| 131 | |||
| 132 | my $i_index = $sonic->i_index() || {}; | ||
| 133 | return { map { $_ => $_ } keys %$i_index }; | ||
| 134 | } | ||
| 135 | |||
| 136 | # dot1qTpFdbPort holds an ifIndex, not a bridge port, even where | ||
| 137 | # dot1dBasePortTable numbers the bridge ports from 0. | ||
| 138 | sub fw_port { | ||
| 139 | my $sonic = shift; | ||
| 140 | |||
| 141 | my $qb_fw_port = $sonic->qb_fw_port() || {}; | ||
| 142 | my %port_of_ifindex = reverse %{ $sonic->bp_index() || {} }; | ||
| 143 | |||
| 144 | my %fw_port; | ||
| 145 | foreach my $idx ( keys %$qb_fw_port ) { | ||
| 146 | my $port = $port_of_ifindex{ $qb_fw_port->{$idx} }; | ||
| 147 | next unless defined $port; | ||
| 148 | $fw_port{$idx} = $port; | ||
| 149 | } | ||
| 150 | return \%fw_port; | ||
| 151 | } | ||
| 152 | |||
| 153 | # ipNetToMediaTable serves only its PhysAddress column, and | ||
| 154 | # ipNetToPhysicalTable keys the same neighbors by another ifIndex, so the | ||
| 155 | # address comes from the index of each at_paddr row. | ||
| 156 | sub at_netaddr { | ||
| 157 | my $sonic = shift; | ||
| 158 | my $partial = shift; | ||
| 159 | |||
| 160 | my $at_paddr = $sonic->at_paddr($partial) || {}; | ||
| 161 | |||
| 162 | my %at_netaddr; | ||
| 163 | foreach my $idx ( keys %$at_paddr ) { | ||
| 164 | next unless $idx =~ /\.(\d+\.\d+\.\d+\.\d+)$/; | ||
| 165 | $at_netaddr{$idx} = $1; | ||
| 166 | } | ||
| 167 | return \%at_netaddr; | ||
| 168 | } | ||
| 169 | |||
| 170 | # Neighbors identify the switch by its LLDP chassis ID. | ||
| 171 | sub mac { | ||
| 172 | my $sonic = shift; | ||
| 173 | |||
| 174 | my $type = $sonic->lldp_loc_id_type() // ''; | ||
| 175 | my $id = $sonic->lldp_loc_id(); | ||
| 176 | return $id if $type =~ /mac/ and defined $id and $id =~ $MAC_RE; | ||
| 177 | return $sonic->SUPER::mac(); | ||
| 178 | } | ||
| 179 | |||
| 180 | # Community builds serve the LLDP local scalars without their .0 instance. | ||
| 181 | sub lldp_loc_id_type { | ||
| 182 | my $sonic = shift; | ||
| 183 | return $sonic->orig_lldp_loc_id_type() // $sonic->sonic_loc_id_type(); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub lldp_loc_id_os { | ||
| 187 | my $sonic = shift; | ||
| 188 | return $sonic->orig_lldp_loc_id_os() // $sonic->sonic_loc_id_os(); | ||
| 189 | } | ||
| 190 | |||
| 191 | # The agent can serve lldpRemManAddrTable under a later time mark than | ||
| 192 | # lldpRemTable, so the two share no key. The local port and remote index | ||
| 193 | # identify the neighbor; where two time marks give the same neighbor, the | ||
| 194 | # later row is kept. | ||
| 195 | sub _lldp_rem { | ||
| 196 | my ( $sonic, $method, $partial ) = @_; | ||
| 197 | |||
| 198 | my $rows = $sonic->SUPER::_lldp_rem( $method, $partial ); | ||
| 199 | my @keys | ||
| 200 | = sort { _lldp_time_mark($a) <=> _lldp_time_mark($b) } keys %$rows; | ||
| 201 | |||
| 202 | my %rows; | ||
| 203 | foreach my $key (@keys) { | ||
| 204 | next unless $key =~ /^\d+\.(\d+(?:\.\d+)*)$/; | ||
| 205 | $rows{"0.$1"} = $rows->{$key}; | ||
| 206 | } | ||
| 207 | return \%rows; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _lldp_time_mark { | ||
| 211 | my $key = shift; | ||
| 212 | |||
| 213 | return $key =~ /^(\d+)\./ ? $1 : 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | # Enterprise SONiC 4.4 serves dot1qPvid as 0 on most ports, including ports | ||
| 217 | # learning addresses in a VLAN, so 0 is read as unknown. | ||
| 218 | sub i_vlan { | ||
| 219 | my $sonic = shift; | ||
| 220 | my $partial = shift; | ||
| 221 | |||
| 222 | my $i_vlan = $sonic->SUPER::i_vlan($partial) || {}; | ||
| 223 | return { | ||
| 224 | map { $_ => $i_vlan->{$_} } | ||
| 225 | grep { $i_vlan->{$_} } keys %$i_vlan | ||
| 226 | }; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub i_untagged { goto &i_vlan } | ||
| 230 | |||
| 231 | # Enterprise SONiC 4.4 serves the VLAN port lists as text ("ff ff f8 80 | ||
| 232 | # 00"), and they put ports untagged in several VLANs at once, so they are | ||
| 233 | # not read. Community builds serve no VLAN tables. | ||
| 234 | sub i_vlan_membership { return {} } | ||
| 235 | sub i_vlan_membership_untagged { return {} } | ||
| 236 | |||
| 237 | 1; | ||
| 238 | __END__ |