| 1 | # SNMP::Info::Layer2::TPLink | ||
| 2 | # | ||
| 3 | # Copyright (c) 2025 The Netdisco Development Team | ||
| 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::Layer2::TPLink; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use Exporter; | ||
| 35 | use SNMP::Info::Layer2; | ||
| 36 | use SNMP::Info::EtherLike; | ||
| 37 | |||
| 38 | @SNMP::Info::Layer2::TPLink::ISA = qw/ | ||
| 39 | SNMP::Info::EtherLike SNMP::Info::Layer2 Exporter/; | ||
| 40 | @SNMP::Info::Layer2::TPLink::EXPORT_OK = qw//; | ||
| 41 | |||
| 42 | our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE); | ||
| 43 | |||
| 44 | $VERSION = '3.985003'; | ||
| 45 | |||
| 46 | %MIBS = ( | ||
| 47 | %SNMP::Info::Layer2::MIBS, | ||
| 48 | %SNMP::Info::EtherLike::MIBS, | ||
| 49 | # Ensure we can reference TP-Link system/product objects | ||
| 50 | 'TPLINK-SYSINFO-MIB' => 'tpSysInfoDescription', | ||
| 51 | 'TPLINK-MIB' => 'tplinkProducts', | ||
| 52 | 'TPLINK-LLDP-MIB' => 'tplinkLldpMIBObjects', | ||
| 53 | 'TPLINK-LLDPINFO-MIB' => 'lldpInfo', | ||
| 54 | 'TPLINK-DOT1Q-VLAN-MIB' => 'tplinkDot1qVlanMIBObjects', | ||
| 55 | 'TPLINK-PORTCONFIG-MIB' => 'tpPortConfigTable', | ||
| 56 | 'TPLINK-SPANNING-TREE-MIB' => 'tplinkSpanningTreeMIBObjects', | ||
| 57 | 'TPLINK-L2BRIDGE-MIB' => 'tplinkl2BridgeMIBObjects', | ||
| 58 | 'TPLINK-POWER-OVER-ETHERNET-MIB' => 'tplinkPowerOverEthernetMIB', | ||
| 59 | ); | ||
| 60 | |||
| 61 | %GLOBALS = ( | ||
| 62 | %SNMP::Info::Layer2::GLOBALS, | ||
| 63 | %SNMP::Info::EtherLike::GLOBALS, | ||
| 64 | 'tp_sysinfo_descr' => 'tpSysInfoDescription', | ||
| 65 | 'tp_sysinfo_hostname' => 'tpSysInfoHostName', | ||
| 66 | 'tp_sysinfo_hwver' => 'tpSysInfoHwVersion', | ||
| 67 | 'tp_sysinfo_swver' => 'tpSysInfoSwVersion', | ||
| 68 | 'tp_sysinfo_mac' => 'tpSysInfoMacAddr', | ||
| 69 | |||
| 70 | # netdisco/netdisco-mibs#281: tpSysInfoSerialNum (netdisco-mibs names .8 | ||
| 71 | # tpSysInfoUpTime). | ||
| 72 | 'tp_sysinfo_serial' => '.1.3.6.1.4.1.11863.6.1.1.8.0', | ||
| 73 | |||
| 74 | # netdisco/netdisco-mibs#281: powerSupplyUnitInternalPower and | ||
| 75 | # powerSupplyUnitExternalPower (netdisco-mibs lacks | ||
| 76 | # TPLINK-POWERSUPPLYUNIT-MIB). | ||
| 77 | 'ps1_status' => '.1.3.6.1.4.1.11863.6.88.1.1.3.0', | ||
| 78 | 'ps2_status' => '.1.3.6.1.4.1.11863.6.88.1.1.4.0', | ||
| 79 | |||
| 80 | # Spanning Tree globals from TP-Link private MIB | ||
| 81 | 'stp_ver' => 'TPLINK-SPANNING-TREE-MIB::tpStpMode', | ||
| 82 | 'stp_time' => 'TPLINK-SPANNING-TREE-MIB::tpStpLastTopologyChangeTime', | ||
| 83 | 'stp_root' => 'TPLINK-SPANNING-TREE-MIB::tpStpCISTRoot', | ||
| 84 | 'stp_root_port' => 'TPLINK-SPANNING-TREE-MIB::tpStpRootPort', | ||
| 85 | 'stp_priority' => 'TPLINK-SPANNING-TREE-MIB::tpStpCistPriority', | ||
| 86 | 'v_index' => 'TPLINK-DOT1Q-VLAN-MIB::dot1qVlanId', | ||
| 87 | 'v_name' => 'TPLINK-DOT1Q-VLAN-MIB::dot1qVlanDescription', | ||
| 88 | 'tp_power_limit' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpSystemPowerLimit.0', | ||
| 89 | 'tp_power_consumption' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpSystemPowerConsumption.0', | ||
| 90 | 'tp_power_remain' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpSystemPowerRemain.0', | ||
| 91 | ); | ||
| 92 | |||
| 93 | %FUNCS = ( | ||
| 94 | %SNMP::Info::Layer2::FUNCS, | ||
| 95 | %SNMP::Info::EtherLike::FUNCS, | ||
| 96 | |||
| 97 | # netdisco/netdisco-mibs#281: TP-Link 2024 vlanPortPvid. netdisco-mibs | ||
| 98 | # names column 2 vlanPortType with enums, so labels arrive and | ||
| 99 | # munge_tp_pvid maps them back. | ||
| 100 | 'tp_vlan_port_pvid' => '.1.3.6.1.4.1.11863.6.14.1.1.1.1.2', | ||
| 101 | 'tp_vlan_tagged' => 'TPLINK-DOT1Q-VLAN-MIB::vlanTagPortMemberAdd', | ||
| 102 | 'tp_vlan_untagged' => 'TPLINK-DOT1Q-VLAN-MIB::vlanUntagPortMemberAdd', | ||
| 103 | # TP-Link port config (duplex/speed/etc) | ||
| 104 | 'i_duplex_admin' => 'TPLINK-PORTCONFIG-MIB::tpPortConfigDuplex', | ||
| 105 | 'i_speed_admin' => 'TPLINK-PORTCONFIG-MIB::tpPortConfigSpeed', | ||
| 106 | 'tp_port_config_descr' => 'TPLINK-PORTCONFIG-MIB::tpPortConfigDescription', | ||
| 107 | 'tp_lldp_rem_id' => 'TPLINK-LLDPINFO-MIB::lldpNeighborChassisId', | ||
| 108 | 'tp_lldp_rem_pid' => 'TPLINK-LLDPINFO-MIB::lldpNeighborPortIdDescr', | ||
| 109 | 'tp_lldp_rem_desc' => 'TPLINK-LLDPINFO-MIB::lldpNeighborPortDescr', | ||
| 110 | 'tp_lldp_rem_sysname' => 'TPLINK-LLDPINFO-MIB::lldpNeighborDeviceName', | ||
| 111 | 'tp_lldp_rem_sysdesc' => 'TPLINK-LLDPINFO-MIB::lldpNeighborDeviceDescr', | ||
| 112 | 'tp_lldp_rem_cap_spt' => 'TPLINK-LLDPINFO-MIB::lldpNeighborCapAvailable', | ||
| 113 | 'tp_lldp_rman' => 'TPLINK-LLDPINFO-MIB::lldpNeighborManageIpAddr', | ||
| 114 | 'tp_lldp_oper_mau' => 'TPLINK-LLDPINFO-MIB::lldpLocalOperMau', | ||
| 115 | # TP-Link dynamic MAC forwarding table | ||
| 116 | 'tpl2BridgeManageDynMac' => 'TPLINK-L2BRIDGE-MIB::tpl2BridgeManageDynMac', | ||
| 117 | 'tpl2BridgeManageDynVlanId' => 'TPLINK-L2BRIDGE-MIB::tpl2BridgeManageDynVlanId', | ||
| 118 | 'tpl2BridgeManageDynPort' => 'TPLINK-L2BRIDGE-MIB::tpl2BridgeManageDynPort', | ||
| 119 | # Map TP-Link STP per-port table fields into the Bridge expected names | ||
| 120 | 'stp_i_root' => 'TPLINK-SPANNING-TREE-MIB::tpStpCISTRoot', | ||
| 121 | 'stp_i_time' => 'TPLINK-SPANNING-TREE-MIB::tpStpLastTopologyChangeTime', | ||
| 122 | 'stp_i_root_port' => 'TPLINK-SPANNING-TREE-MIB::tpStpRootPort', | ||
| 123 | 'stp_i_priority' => 'TPLINK-SPANNING-TREE-MIB::tpStpCistPriority', | ||
| 124 | # Per-port STP mapped keys (index by ifIndex) | ||
| 125 | 'stp_p_id' => 'TPLINK-SPANNING-TREE-MIB::tpStpPortNumber', | ||
| 126 | 'stp_p_priority' => 'TPLINK-SPANNING-TREE-MIB::tpStpPortPriority', | ||
| 127 | 'stp_p_state' => 'TPLINK-SPANNING-TREE-MIB::tpStpPortStatus', | ||
| 128 | 'stp_p_cost' => 'TPLINK-SPANNING-TREE-MIB::tpStpPortInPathCost', | ||
| 129 | 'stp_p_role' => 'TPLINK-SPANNING-TREE-MIB::tpStpPortRole', | ||
| 130 | 'is_edgeport_admin' => 'TPLINK-SPANNING-TREE-MIB::tpStpEdgePortStatus', | ||
| 131 | 'is_edgeport_oper' => 'TPLINK-SPANNING-TREE-MIB::tpStpEdgePortStatus', | ||
| 132 | # PoE | ||
| 133 | 'tp_peth_port_admin' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpPoePortStatus', | ||
| 134 | 'tp_peth_port_status' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpPoePowerStatus', | ||
| 135 | 'tp_peth_port_class' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpPoeClass', | ||
| 136 | 'tp_peth_port_power' => 'TPLINK-POWER-OVER-ETHERNET-MIB::tpPoePower', | ||
| 137 | ); | ||
| 138 | |||
| 139 | %MUNGE = ( | ||
| 140 | %SNMP::Info::Layer2::MUNGE, | ||
| 141 | %SNMP::Info::EtherLike::MUNGE, | ||
| 142 | 'tp_vlan_port_pvid' => \&munge_tp_pvid, | ||
| 143 | 'tp_power_limit' => \&munge_power, | ||
| 144 | 'tp_power_consumption' => \&munge_power, | ||
| 145 | 'tp_power_remain' => \&munge_power, | ||
| 146 | ); | ||
| 147 | |||
| 148 | sub vendor { | ||
| 149 | return 'TP-Link'; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub os { | ||
| 153 | return 'tplink'; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub model { | ||
| 157 | my $tp = shift; | ||
| 158 | |||
| 159 | # tpSysInfoHwVersion is the short model, e.g. "T1600G-28TS 3.0"; | ||
| 160 | # tpSysInfoDescription and sysDescr are long product descriptions | ||
| 161 | foreach my $model ( $tp->tp_sysinfo_hwver(), $tp->tp_sysinfo_descr(), | ||
| 162 | $tp->description() ) | ||
| 163 | { | ||
| 164 | next unless defined $model; | ||
| 165 | $model =~ s/^\s+|\s+$//g; | ||
| 166 | return $model if $model ne ''; | ||
| 167 | } | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub os_ver { | ||
| 172 | my $tp = shift; | ||
| 173 | |||
| 174 | # Prefer TP-Link specific SW version | ||
| 175 | my $sw = $tp->tp_sysinfo_swver(); | ||
| 176 | return $sw if defined $sw and $sw ne ''; | ||
| 177 | |||
| 178 | # Last resort, take from sysDescr | ||
| 179 | my $desc = $tp->description() || ''; | ||
| 180 | if ( $desc =~ /([\d]+(?:\.[\d]+)+)/ ) { | ||
| 181 | return $1; | ||
| 182 | } | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub serial { | ||
| 187 | my $tp = shift; | ||
| 188 | |||
| 189 | my $serial = $tp->tp_sysinfo_serial(); | ||
| 190 | return $serial if defined $serial and $serial !~ /^\s*$/; | ||
| 191 | |||
| 192 | return; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub mac { | ||
| 196 | my $tp = shift; | ||
| 197 | |||
| 198 | my $mac = $tp->b_mac(); | ||
| 199 | return $mac if defined $mac and $mac ne ''; | ||
| 200 | |||
| 201 | $mac = $tp->tp_sysinfo_mac(); | ||
| 202 | if ( defined $mac and $mac ne '' ) { | ||
| 203 | $mac =~ s/-/:/g; | ||
| 204 | return uc $mac; | ||
| 205 | } | ||
| 206 | |||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub i_name { | ||
| 211 | my $tp = shift; | ||
| 212 | my $partial = shift; | ||
| 213 | |||
| 214 | my $names = $tp->orig_i_name($partial) || {}; | ||
| 215 | my $aliases = $tp->i_alias($partial) || {}; | ||
| 216 | $aliases = $tp->tp_port_config_descr($partial) || {} | ||
| 217 | unless keys %$aliases; | ||
| 218 | |||
| 219 | my %out; | ||
| 220 | foreach my $iid ( keys %$names ) { | ||
| 221 | my $alias = $aliases->{$iid}; | ||
| 222 | $out{$iid} | ||
| 223 | = ( defined $alias and $alias !~ /^\s*$/ ) | ||
| 224 | ? $alias | ||
| 225 | : $names->{$iid}; | ||
| 226 | } | ||
| 227 | |||
| 228 | return \%out; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub i_duplex { | ||
| 232 | my $tp = shift; | ||
| 233 | my $partial = shift; | ||
| 234 | |||
| 235 | # EtherLike is present on most JetStream firmware; dot3StatsIndex equals | ||
| 236 | # ifIndex and no walk publishes it, so el_duplex is keyed directly. | ||
| 237 | my $el_duplex = $tp->el_duplex($partial) || {}; | ||
| 238 | my %el_out; | ||
| 239 | foreach my $iid ( keys %$el_duplex ) { | ||
| 240 | my $duplex = $el_duplex->{$iid}; | ||
| 241 | next unless defined $duplex; | ||
| 242 | $el_out{$iid} = 'full' if $duplex =~ /full/i; | ||
| 243 | $el_out{$iid} = 'half' if $duplex =~ /half/i; | ||
| 244 | } | ||
| 245 | return \%el_out if keys %el_out; | ||
| 246 | |||
| 247 | my $mau = $tp->tp_lldp_oper_mau($partial) || {}; | ||
| 248 | my %out; | ||
| 249 | foreach my $key ( keys %$mau ) { | ||
| 250 | if ( defined $mau->{$key} | ||
| 251 | and $mau->{$key} =~ /speed\(\S+\)\/duplex\((full|half)\)/i ) | ||
| 252 | { | ||
| 253 | $out{$key} = lc $1; | ||
| 254 | } | ||
| 255 | else { | ||
| 256 | $out{$key} = 'unknown'; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | return \%out; | ||
| 261 | } | ||
| 262 | |||
| 263 | # PoE methods | ||
| 264 | sub munge_power { | ||
| 265 | my $power = shift; | ||
| 266 | |||
| 267 | return $power ? $power / 10 : 0; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _tp_port_map { | ||
| 271 | my $tp = shift; | ||
| 272 | |||
| 273 | my $descriptions = $tp->i_description() || {}; | ||
| 274 | my %map; | ||
| 275 | foreach my $iid ( keys %$descriptions ) { | ||
| 276 | my $description = $descriptions->{$iid}; | ||
| 277 | next unless defined $description; | ||
| 278 | if ( $description =~ m{(\d+/\d+/\d+)} ) { | ||
| 279 | $map{$1} = $iid; | ||
| 280 | } | ||
| 281 | elsif ( $description =~ /^port-channel\s*(\d+)$/i ) { | ||
| 282 | $map{"LAG$1"} = $iid; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | return \%map; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _tp_peth_by_port { | ||
| 289 | my $tp = shift; | ||
| 290 | my $column = shift || {}; | ||
| 291 | |||
| 292 | my $port_map = $tp->_tp_port_map(); | ||
| 293 | my %out; | ||
| 294 | foreach my $port ( keys %$column ) { | ||
| 295 | next unless exists $port_map->{"1/0/$port"}; | ||
| 296 | $out{"1.$port"} = $column->{$port}; | ||
| 297 | } | ||
| 298 | return \%out; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub peth_power_status { | ||
| 302 | my $tp = shift; | ||
| 303 | |||
| 304 | my $watts = $tp->tp_power_limit(); | ||
| 305 | return defined $watts ? { 1 => 'on' } : {}; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub peth_power_watts { | ||
| 309 | my $tp = shift; | ||
| 310 | |||
| 311 | my $watts = $tp->tp_power_limit(); | ||
| 312 | return defined $watts ? { 1 => $watts } : {}; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub peth_port_ifindex { | ||
| 316 | my $tp = shift; | ||
| 317 | |||
| 318 | my $port_map = $tp->_tp_port_map(); | ||
| 319 | my $admin = $tp->tp_peth_port_admin() || {}; | ||
| 320 | my %out; | ||
| 321 | foreach my $port ( keys %$admin ) { | ||
| 322 | my $iid = $port_map->{"1/0/$port"}; | ||
| 323 | $out{"1.$port"} = $iid if defined $iid; | ||
| 324 | } | ||
| 325 | return \%out; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub peth_port_admin { | ||
| 329 | my $tp = shift; | ||
| 330 | |||
| 331 | my $admin = $tp->_tp_peth_by_port( $tp->tp_peth_port_admin() ); | ||
| 332 | foreach my $entry ( keys %$admin ) { | ||
| 333 | my $state = $admin->{$entry}; | ||
| 334 | $admin->{$entry} | ||
| 335 | = ( defined $state and $state eq 'enable' ) ? 'true' : 'false'; | ||
| 336 | } | ||
| 337 | return $admin; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub peth_port_status { | ||
| 341 | my $tp = shift; | ||
| 342 | |||
| 343 | my %state_name = ( | ||
| 344 | 'on' => 'deliveringPower', | ||
| 345 | 'off' => 'searching', | ||
| 346 | 'turning-on' => 'searching', | ||
| 347 | 'overload' => 'fault', | ||
| 348 | 'short' => 'fault', | ||
| 349 | 'voltage-high' => 'fault', | ||
| 350 | 'voltage-low' => 'fault', | ||
| 351 | 'hardware-fault' => 'fault', | ||
| 352 | 'overtemperature' => 'fault', | ||
| 353 | ); | ||
| 354 | my $admin = $tp->_tp_peth_by_port( $tp->tp_peth_port_admin() ); | ||
| 355 | my $status = $tp->_tp_peth_by_port( $tp->tp_peth_port_status() ); | ||
| 356 | foreach my $entry ( keys %$status ) { | ||
| 357 | my $state = $status->{$entry}; | ||
| 358 | my $set = $admin->{$entry}; | ||
| 359 | $status->{$entry} | ||
| 360 | = ( defined $set and $set eq 'disable' ) ? 'disabled' | ||
| 361 | : defined $state ? ( $state_name{$state} || 'otherFault' ) | ||
| 362 | : 'otherFault'; | ||
| 363 | } | ||
| 364 | return $status; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub peth_port_class { | ||
| 368 | my $tp = shift; | ||
| 369 | |||
| 370 | return $tp->_tp_peth_by_port( $tp->tp_peth_port_class() ); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub peth_port_power { | ||
| 374 | my $tp = shift; | ||
| 375 | |||
| 376 | my $power = $tp->_tp_peth_by_port( $tp->tp_peth_port_power() ); | ||
| 377 | foreach my $entry ( keys %$power ) { | ||
| 378 | $power->{$entry} *= 100 if defined $power->{$entry}; | ||
| 379 | } | ||
| 380 | return $power; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub munge_tp_pvid { | ||
| 384 | my $value = shift; | ||
| 385 | |||
| 386 | my %by_label = ( access => 0, trunk => 1, general => 2 ); | ||
| 387 | return $by_label{$value} if defined $value and exists $by_label{$value}; | ||
| 388 | return $value; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _tp_vlan_ports { | ||
| 392 | my $tp = shift; | ||
| 393 | my $lists = shift || {}; | ||
| 394 | |||
| 395 | my $port_map = $tp->_tp_port_map(); | ||
| 396 | my %seen; | ||
| 397 | foreach my $vlan ( keys %$lists ) { | ||
| 398 | foreach my $port ( _tp_expand_port_list( $lists->{$vlan} ) ) { | ||
| 399 | my $iid = $port_map->{$port}; | ||
| 400 | $seen{$iid}{$vlan} = 1 if defined $iid; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | my %out; | ||
| 405 | foreach my $iid ( keys %seen ) { | ||
| 406 | $out{$iid} = [ sort { $a <=> $b } keys %{ $seen{$iid} } ]; | ||
| 407 | } | ||
| 408 | return \%out; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub _tp_expand_port_list { | ||
| 412 | my $list = shift; | ||
| 413 | |||
| 414 | return () unless defined $list; | ||
| 415 | my @ports; | ||
| 416 | foreach my $token ( split /,/, $list ) { | ||
| 417 | $token =~ s/^\s+|\s+$//g; | ||
| 418 | if ( $token =~ m{^(\d+/\d+/)(\d+)-(\d+)$} ) { | ||
| 419 | push @ports, map {"$1$_"} $2 .. $3; | ||
| 420 | } | ||
| 421 | elsif ( $token =~ /^LAG(\d+)-(\d+)$/ ) { | ||
| 422 | push @ports, map {"LAG$_"} $1 .. $2; | ||
| 423 | } | ||
| 424 | elsif ( $token =~ m{^(?:\d+/\d+/\d+|LAG\d+)$} ) { | ||
| 425 | push @ports, $token; | ||
| 426 | } | ||
| 427 | } | ||
| 428 | return @ports; | ||
| 429 | } | ||
| 430 | |||
| 431 | sub _tp_vlan_membership { | ||
| 432 | my $tp = shift; | ||
| 433 | my $partial = shift; | ||
| 434 | my @columns = @_; | ||
| 435 | |||
| 436 | my %lists; | ||
| 437 | foreach my $column (@columns) { | ||
| 438 | my $rows = $tp->$column() || {}; | ||
| 439 | foreach my $vlan ( keys %$rows ) { | ||
| 440 | next unless defined $rows->{$vlan} and $rows->{$vlan} ne ''; | ||
| 441 | $lists{$vlan} .= ( exists $lists{$vlan} ? ',' : '' ) | ||
| 442 | . $rows->{$vlan}; | ||
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | my $members = $tp->_tp_vlan_ports( \%lists ); | ||
| 447 | return $members unless defined $partial; | ||
| 448 | return { map { $_ => $members->{$_} } | ||
| 449 | grep { $_ eq $partial } keys %$members }; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub i_vlan_membership { | ||
| 453 | my $tp = shift; | ||
| 454 | my $partial = shift; | ||
| 455 | |||
| 456 | return $tp->_tp_vlan_membership( $partial, 'tp_vlan_tagged', | ||
| 457 | 'tp_vlan_untagged' ); | ||
| 458 | } | ||
| 459 | |||
| 460 | sub i_vlan_membership_untagged { | ||
| 461 | my $tp = shift; | ||
| 462 | my $partial = shift; | ||
| 463 | |||
| 464 | return $tp->_tp_vlan_membership( $partial, 'tp_vlan_untagged' ); | ||
| 465 | } | ||
| 466 | |||
| 467 | sub i_vlan { | ||
| 468 | my $tp = shift; | ||
| 469 | my $partial = shift; | ||
| 470 | |||
| 471 | my $pvids = $tp->tp_vlan_port_pvid($partial) || {}; | ||
| 472 | return { %$pvids } if keys %$pvids; | ||
| 473 | |||
| 474 | my $untagged = $tp->i_vlan_membership_untagged($partial) || {}; | ||
| 475 | my %out; | ||
| 476 | foreach my $iid ( keys %$untagged ) { | ||
| 477 | $out{$iid} = $untagged->{$iid}[0] if @{ $untagged->{$iid} } == 1; | ||
| 478 | } | ||
| 479 | return \%out; | ||
| 480 | } | ||
| 481 | |||
| 482 | # TP-Link switches may publish neighbors only in the TP-Link table. | ||
| 483 | sub hasLLDP { | ||
| 484 | my $tp = shift; | ||
| 485 | |||
| 486 | return 1 if $tp->SUPER::hasLLDP(); | ||
| 487 | my $tp_rem_id = $tp->tp_lldp_rem_id() || {}; | ||
| 488 | return 1 if scalar keys %$tp_rem_id; | ||
| 489 | return; | ||
| 490 | } | ||
| 491 | |||
| 492 | # Decided once per device so neighbor keys never mix. Some models publish | ||
| 493 | # LLDP-MIB remote rows, which then take precedence. | ||
| 494 | # Memoized because an empty walk is not cached; clear_cache resets it. | ||
| 495 | sub _tp_lldp_standard { | ||
| 496 | my $tp = shift; | ||
| 497 | |||
| 498 | return $tp->{_tp_lldp_standard} if exists $tp->{_tp_lldp_standard}; | ||
| 499 | my $rem_id = $tp->lldp_rem_id() || {}; | ||
| 500 | return $tp->{_tp_lldp_standard} = scalar keys %$rem_id ? 1 : 0; | ||
| 501 | } | ||
| 502 | |||
| 503 | sub _tp_lldp_neighbors { | ||
| 504 | my ( $tp, $partial ) = @_; | ||
| 505 | return $tp->tp_lldp_rem_id($partial) || {}; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub _tp_lldp_first { | ||
| 509 | my ( $tp, $partial, @columns ) = @_; | ||
| 510 | |||
| 511 | my @tables = map { $tp->$_($partial) || {} } @columns; | ||
| 512 | my %out; | ||
| 513 | foreach my $key ( keys %{ $tp->_tp_lldp_neighbors($partial) } ) { | ||
| 514 | my ($value) = grep { defined $_ and $_ ne '' } | ||
| 515 | map { $_->{$key} } @tables; | ||
| 516 | $out{$key} = $value if defined $value; | ||
| 517 | } | ||
| 518 | return \%out; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub _tp_lldp_addr { | ||
| 522 | my ( $tp, $partial, $want_v6 ) = @_; | ||
| 523 | |||
| 524 | my $rman = $tp->tp_lldp_rman($partial) || {}; | ||
| 525 | my %out; | ||
| 526 | foreach my $key ( keys %{ $tp->_tp_lldp_neighbors($partial) } ) { | ||
| 527 | my $addr = $rman->{$key}; | ||
| 528 | next unless defined $addr; | ||
| 529 | my $v4 = $addr =~ /^(?:::)?(\d{1,3}(?:\.\d{1,3}){3})$/ ? $1 : undef; | ||
| 530 | if ($want_v6) { | ||
| 531 | $out{$key} = $addr | ||
| 532 | if !defined $v4 and $addr =~ /:/ and $addr ne '::'; | ||
| 533 | } | ||
| 534 | elsif ( defined $v4 ) { | ||
| 535 | $out{$key} = $v4; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | return \%out; | ||
| 539 | } | ||
| 540 | |||
| 541 | sub _tp_lldp_std_if { | ||
| 542 | my ( $tp, $partial ) = @_; | ||
| 543 | |||
| 544 | my $inherited = $tp->SUPER::lldp_if($partial) || {}; | ||
| 545 | my $port_id = $tp->lldp_lport_id() || {}; | ||
| 546 | my %ifindex_of = reverse %{ $tp->i_description() || {} }; | ||
| 547 | |||
| 548 | # lldpLocPortDesc holds the port alias when one is set, and aliases | ||
| 549 | # repeat across ports, so the exact ifDescr in lldpLocPortId decides. | ||
| 550 | my %lldp_if = %$inherited; | ||
| 551 | foreach my $key ( keys %lldp_if ) { | ||
| 552 | my $local_port = ( split /\./, $key )[1]; | ||
| 553 | my $descr = defined $local_port ? $port_id->{$local_port} : undef; | ||
| 554 | $lldp_if{$key} = $ifindex_of{$descr} | ||
| 555 | if defined $descr and exists $ifindex_of{$descr}; | ||
| 556 | } | ||
| 557 | return \%lldp_if; | ||
| 558 | } | ||
| 559 | |||
| 560 | sub lldp_if { | ||
| 561 | my ( $tp, $partial ) = @_; | ||
| 562 | return $tp->_tp_lldp_std_if($partial) if $tp->_tp_lldp_standard; | ||
| 563 | |||
| 564 | my %lldp_if; | ||
| 565 | foreach my $key ( keys %{ $tp->_tp_lldp_neighbors($partial) } ) { | ||
| 566 | my ($ifindex) = split /\./, $key; | ||
| 567 | $lldp_if{$key} = $ifindex; | ||
| 568 | } | ||
| 569 | return \%lldp_if; | ||
| 570 | } | ||
| 571 | |||
| 572 | # lldpRemTable index by localPort.remIndex, newest lldpRemTimeMark kept | ||
| 573 | sub _tp_lldp_rem_keys { | ||
| 574 | my $tp = shift; | ||
| 575 | |||
| 576 | my %full_key; | ||
| 577 | foreach my $key ( keys %{ $tp->lldp_rem_id() || {} } ) { | ||
| 578 | my ( $time_mark, $short ) = split /\./, $key, 2; | ||
| 579 | next unless defined $short; | ||
| 580 | my $kept = $full_key{$short}; | ||
| 581 | next if defined $kept and ( split /\./, $kept )[0] >= $time_mark; | ||
| 582 | $full_key{$short} = $key; | ||
| 583 | } | ||
| 584 | return \%full_key; | ||
| 585 | } | ||
| 586 | |||
| 587 | # Some Omada firmware indexes lldpRemManAddrTable without lldpRemTimeMark | ||
| 588 | # (localPort.remIndex.subtype.len.addr). Such rows take the lldpRemTable | ||
| 589 | # index so management addresses line up with c_if and c_port. | ||
| 590 | sub lldp_rman_addr { | ||
| 591 | my ( $tp, $partial ) = @_; | ||
| 592 | |||
| 593 | my $rman = $tp->SUPER::lldp_rman_addr($partial); | ||
| 594 | return $rman unless $tp->_tp_lldp_standard and ref $rman eq 'HASH'; | ||
| 595 | |||
| 596 | my $rem_keys = $tp->_tp_lldp_rem_keys(); | ||
| 597 | my %is_rem_key = map { $_ => 1 } values %$rem_keys; | ||
| 598 | my %out; | ||
| 599 | foreach my $key ( keys %$rman ) { | ||
| 600 | my @parts = split /\./, $key; | ||
| 601 | if ( $is_rem_key{ join '.', @parts[ 0 .. 2 ] } ) { | ||
| 602 | $out{$key} = $rman->{$key}; | ||
| 603 | next; | ||
| 604 | } | ||
| 605 | |||
| 606 | my $addr_len = $parts[3]; | ||
| 607 | next unless defined $addr_len and @parts == 4 + $addr_len; | ||
| 608 | my $full = $rem_keys->{ join '.', @parts[ 0, 1 ] }; | ||
| 609 | next unless defined $full; | ||
| 610 | $out{ join '.', $full, @parts[ 2 .. $#parts ] } = $rman->{$key}; | ||
| 611 | } | ||
| 612 | return \%out; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub lldp_ip { | ||
| 616 | my ( $tp, $partial ) = @_; | ||
| 617 | return $tp->SUPER::lldp_ip($partial) if $tp->_tp_lldp_standard; | ||
| 618 | return $tp->_tp_lldp_addr( $partial, 0 ); | ||
| 619 | } | ||
| 620 | |||
| 621 | sub lldp_ipv6 { | ||
| 622 | my ( $tp, $partial ) = @_; | ||
| 623 | return $tp->SUPER::lldp_ipv6($partial) if $tp->_tp_lldp_standard; | ||
| 624 | return $tp->_tp_lldp_addr( $partial, 1 ); | ||
| 625 | } | ||
| 626 | |||
| 627 | sub lldp_addrs { | ||
| 628 | my ( $tp, $partial ) = @_; | ||
| 629 | return $tp->SUPER::lldp_addrs($partial) if $tp->_tp_lldp_standard; | ||
| 630 | return $tp->_lldp_addrs_from_families($partial); | ||
| 631 | } | ||
| 632 | |||
| 633 | sub lldp_port { | ||
| 634 | my ( $tp, $partial ) = @_; | ||
| 635 | return $tp->SUPER::lldp_port($partial) if $tp->_tp_lldp_standard; | ||
| 636 | return $tp->_tp_lldp_first( $partial, 'tp_lldp_rem_pid', | ||
| 637 | 'tp_lldp_rem_desc' ); | ||
| 638 | } | ||
| 639 | |||
| 640 | sub lldp_id { | ||
| 641 | my ( $tp, $partial ) = @_; | ||
| 642 | return $tp->SUPER::lldp_id($partial) if $tp->_tp_lldp_standard; | ||
| 643 | return $tp->_tp_lldp_first( $partial, 'tp_lldp_rem_id' ); | ||
| 644 | } | ||
| 645 | |||
| 646 | sub lldp_platform { | ||
| 647 | my ( $tp, $partial ) = @_; | ||
| 648 | return $tp->SUPER::lldp_platform($partial) if $tp->_tp_lldp_standard; | ||
| 649 | return $tp->_tp_lldp_first( $partial, 'tp_lldp_rem_sysdesc', | ||
| 650 | 'tp_lldp_rem_sysname' ); | ||
| 651 | } | ||
| 652 | |||
| 653 | my %TP_LLDP_CAP = ( | ||
| 654 | 'Other' => 'other', | ||
| 655 | 'Repeater' => 'repeater', | ||
| 656 | 'Bridge' => 'bridge', | ||
| 657 | 'WLAN Access Point' => 'wlanAccessPoint', | ||
| 658 | 'Router' => 'router', | ||
| 659 | 'Telephone' => 'telephone', | ||
| 660 | 'DOCSIS Cable Device' => 'docsisCableDevice', | ||
| 661 | 'Station Only' => 'stationOnly', | ||
| 662 | ); | ||
| 663 | my $TP_LLDP_CAP_NAMES = join '|', map {quotemeta} | ||
| 664 | sort { length $b <=> length $a } keys %TP_LLDP_CAP; | ||
| 665 | |||
| 666 | sub lldp_cap { | ||
| 667 | my ( $tp, $partial ) = @_; | ||
| 668 | return $tp->SUPER::lldp_cap($partial) if $tp->_tp_lldp_standard; | ||
| 669 | |||
| 670 | my $text = $tp->_tp_lldp_first( $partial, 'tp_lldp_rem_cap_spt' ); | ||
| 671 | my %lldp_cap; | ||
| 672 | foreach my $key ( keys %$text ) { | ||
| 673 | my @caps = map { $TP_LLDP_CAP{$_} } | ||
| 674 | $text->{$key} =~ /(?:^|\s)($TP_LLDP_CAP_NAMES)(?=\s|$)/g; | ||
| 675 | $lldp_cap{$key} = \@caps if @caps; | ||
| 676 | } | ||
| 677 | return \%lldp_cap; | ||
| 678 | } | ||
| 679 | |||
| 680 | sub _tp_bridge_port_ifindex { | ||
| 681 | my ( $tp, $number, $port_map ) = @_; | ||
| 682 | |||
| 683 | my @matches = grep { m{^\d+/\d+/\Q$number\E$} } keys %$port_map; | ||
| 684 | return unless @matches == 1; | ||
| 685 | return $port_map->{ $matches[0] }; | ||
| 686 | } | ||
| 687 | |||
| 688 | # Build the Q-BRIDGE style forwarding table (dot1qTpFdbPort) from | ||
| 689 | # tpl2BridgeManageDynPort when the standard column is absent. Its index is | ||
| 690 | # mac.vlan; the result is keyed vlan.mac like dot1qTpFdbEntry. | ||
| 691 | sub qb_fw_port { | ||
| 692 | my $tp = shift; | ||
| 693 | my $partial = shift; | ||
| 694 | |||
| 695 | my $super = $tp->SUPER::qb_fw_port($partial) || {}; | ||
| 696 | return $super if ( ref {} eq ref $super and scalar keys %$super ); | ||
| 697 | |||
| 698 | my $dyn_port = $tp->tpl2BridgeManageDynPort($partial) || {}; | ||
| 699 | my $port_map = $tp->_tp_port_map(); | ||
| 700 | my $bp_index = $tp->bp_index() || {}; | ||
| 701 | |||
| 702 | my %out; | ||
| 703 | foreach my $key ( keys %$dyn_port ) { | ||
| 704 | my $pval = $dyn_port->{$key}; | ||
| 705 | next unless defined $pval and $pval ne '' and $pval ne '0'; | ||
| 706 | |||
| 707 | my @parts = split /\./, $key; | ||
| 708 | next unless @parts >= 2; | ||
| 709 | my $vlan = pop @parts; | ||
| 710 | |||
| 711 | my $ifindex; | ||
| 712 | if ( $pval =~ m{^\d+/\d+/\d+$} ) { | ||
| 713 | $ifindex = $port_map->{$pval}; | ||
| 714 | } | ||
| 715 | elsif ( $pval =~ /^\d+$/ ) { | ||
| 716 | $ifindex = $tp->_tp_bridge_port_ifindex( $pval, $port_map ); | ||
| 717 | $ifindex = $bp_index->{$pval} | ||
| 718 | if !defined $ifindex and exists $bp_index->{$pval}; | ||
| 719 | } | ||
| 720 | |||
| 721 | $out{ join( '.', $vlan, @parts ) } | ||
| 722 | = defined $ifindex ? $ifindex : $pval; | ||
| 723 | } | ||
| 724 | |||
| 725 | return \%out; | ||
| 726 | } | ||
| 727 | |||
| 728 | # dot1qTpFdbPort carries front-panel port numbers, but Netdisco looks | ||
| 729 | # fw_port values up in bp_index(), which is keyed by ifIndex. | ||
| 730 | sub fw_port { | ||
| 731 | my $tp = shift; | ||
| 732 | my $partial = shift; | ||
| 733 | |||
| 734 | my $fw = $tp->SUPER::fw_port($partial) || {}; | ||
| 735 | |||
| 736 | my $interfaces = $tp->interfaces() || {}; | ||
| 737 | my $bp_index = $tp->bp_index() || {}; | ||
| 738 | my $port_map = $tp->_tp_port_map(); | ||
| 739 | |||
| 740 | my %out; | ||
| 741 | foreach my $idx ( keys %$fw ) { | ||
| 742 | my $portval = $fw->{$idx}; | ||
| 743 | next unless defined $portval and $portval ne ''; | ||
| 744 | |||
| 745 | my $ifindex; | ||
| 746 | if ( $portval =~ /^\d+$/ ) { | ||
| 747 | next if $portval == 0; | ||
| 748 | $ifindex = $tp->_tp_bridge_port_ifindex( $portval, $port_map ); | ||
| 749 | if ( !defined $ifindex and exists $interfaces->{$portval} ) { | ||
| 750 | $ifindex = $portval; | ||
| 751 | } | ||
| 752 | if ( !defined $ifindex and exists $bp_index->{$portval} ) { | ||
| 753 | $ifindex = $bp_index->{$portval}; | ||
| 754 | } | ||
| 755 | } | ||
| 756 | else { | ||
| 757 | $ifindex = $port_map->{$portval}; | ||
| 758 | } | ||
| 759 | |||
| 760 | $out{$idx} = defined $ifindex ? $ifindex : $portval; | ||
| 761 | } | ||
| 762 | |||
| 763 | return \%out; | ||
| 764 | } | ||
| 765 | |||
| 766 | 1; | ||
| 767 | __END__ |