function upd8021QPort($instance = 'desired', $object_id, $port_name, $port) { global $tablemap_8021q; if (!array_key_exists($instance, $tablemap_8021q)) { throw new InvalidArgException('instance', $instance); } // Replace current port configuration with the provided one. If the new // native VLAN ID doesn't belong to the allowed list, don't issue // INSERT query, which would always trigger an FK exception. // This function indicates an error, but doesn't revert it, so it is // assummed, that the calling function performs necessary transaction wrapping. // A record on a port with none VLANs allowed makes no sense regardless of port mode. if ($port['mode'] != 'trunk' and !count($port['allowed'])) { return 0; } usePreparedUpdateBlade($tablemap_8021q[$instance]['pvm'], array('vlan_mode' => $port['mode']), array('object_id' => $object_id, 'port_name' => $port_name)); usePreparedDeleteBlade($tablemap_8021q[$instance]['pav'], array('object_id' => $object_id, 'port_name' => $port_name)); // The goal is to INSERT as many rows as there are values in 'allowed' list // without wrapping each row with own INSERT (otherwise the SQL connection // instantly becomes the bottleneck). foreach (listToRanges($port['allowed']) as $range) { usePreparedExecuteBlade('INSERT INTO ' . $tablemap_8021q[$instance]['pav'] . ' (object_id, port_name, vlan_id) ' . 'SELECT ?, ?, vlan_id FROM VLANValidID WHERE vlan_id BETWEEN ? AND ?', array($object_id, $port_name, $range['from'], $range['to'])); } if ($port['native'] and in_array($port['native'], $port['allowed'])) { usePreparedInsertBlade($tablemap_8021q[$instance]['pnv'], array('object_id' => $object_id, 'port_name' => $port_name, 'vlan_id' => $port['native'])); } return 1; }
function buildVLANFilter($role, $string) { // set base switch ($role) { case 'access': // 1-4094 $min = VLAN_MIN_ID; $max = VLAN_MAX_ID; break; case 'trunk': // 2-4094 // 2-4094 case 'uplink': case 'downlink': case 'anymode': $min = VLAN_MIN_ID + 1; $max = VLAN_MAX_ID; break; default: // none return array(); } if ($string == '') { // fast track return array(array('from' => $min, 'to' => $max)); } // transform $vlanidlist = array(); foreach (iosParseVLANString($string) as $vlan_id) { if ($min <= $vlan_id and $vlan_id <= $max) { $vlanidlist[] = $vlan_id; } } return listToRanges($vlanidlist); }
function render8021QReport() { if (!count($domains = getVLANDomainOptions())) { echo '<center><h3>(no VLAN configuration exists)</h3></center>'; return; } $vlanstats = array(); for ($i = VLAN_MIN_ID; $i <= VLAN_MAX_ID; $i++) { $vlanstats[$i] = array(); } $header = '<tr><th> </th>'; foreach ($domains as $domain_id => $domain_name) { foreach (getDomainVLANList($domain_id) as $vlan_id => $vlan_info) { $vlanstats[$vlan_id][$domain_id] = $vlan_info; } $header .= '<th>' . mkA($domain_name, 'vlandomain', $domain_id) . '</th>'; } $header .= '</tr>'; $output = $available = array(); for ($i = VLAN_MIN_ID; $i <= VLAN_MAX_ID; $i++) { if (!count($vlanstats[$i])) { $available[] = $i; } else { $output[$i] = FALSE; } } foreach (listToRanges($available) as $span) { if ($span['to'] - $span['from'] < 4) { for ($i = $span['from']; $i <= $span['to']; $i++) { $output[$i] = FALSE; } } else { $output[$span['from']] = TRUE; $output[$span['to']] = FALSE; } } ksort($output, SORT_NUMERIC); $header_delay = 0; startPortlet('VLAN existence per domain'); echo '<table border=1 cellspacing=0 cellpadding=5 align=center class=rackspace>'; foreach ($output as $vlan_id => $tbc) { if (--$header_delay <= 0) { echo $header; $header_delay = 25; } echo '<tr class="state_' . (count($vlanstats[$vlan_id]) ? 'T' : 'F'); echo '"><th class=tdright>' . $vlan_id . '</th>'; foreach (array_keys($domains) as $domain_id) { echo '<td class=tdcenter>'; if (array_key_exists($domain_id, $vlanstats[$vlan_id])) { echo mkA('∃', 'vlan', "{$domain_id}-{$vlan_id}"); } else { echo ' '; } echo '</td>'; } echo '</tr>'; if ($tbc) { echo '<tr class="state_A"><th>...</th><td colspan=' . count($domains) . '> </td></tr>'; } } echo '</table>'; finishPortlet(); }
function ros11TranslatePushQueue($dummy_object_id, $queue, $dummy_vlan_names) { $ret = ''; foreach ($queue as $cmd) { switch ($cmd['opcode']) { case 'begin configuration': $ret .= "configure terminal\n"; break; case 'end configuration': $ret .= "end\n"; break; case 'save configuration': $ret .= "copy running-config startup-config\nY\n"; break; case 'create VLAN': $ret .= "vlan database\nvlan {$cmd['arg1']}\nexit\n"; break; case 'destroy VLAN': if (isset($vlan_names[$cmd['arg1']])) { $ret .= "vlan database\nno vlan {$cmd['arg1']}\nexit\n"; } break; case 'set access': $ret .= "interface {$cmd['arg1']}\nswitchport access vlan {$cmd['arg2']}\nexit\n"; break; case 'unset access': $ret .= "interface {$cmd['arg1']}\nno switchport access vlan\nexit\n"; break; case 'set mode': $ret .= "interface {$cmd['arg1']}\n"; $ret .= "switchport mode {$cmd['arg2']}\n"; if ($cmd['arg2'] == 'trunk') { $ret .= "no switchport trunk native vlan\nswitchport trunk allowed vlan remove all\n"; } $ret .= "exit\n"; break; case 'add allowed': case 'rem allowed': $ret .= "interface {$cmd['port']}\n"; # default VLAN special case $ordinary = array(); foreach ($cmd['vlans'] as $vid) { if ($vid == VLAN_DFL_ID) { $ret .= $cmd['opcode'] == 'add allowed' ? "no switchport forbidden default-vlan\nswitchport default-vlan tagged\n" : "switchport forbidden default-vlan\nno switchport default-vlan tagged\n"; } else { $ordinary[] = $vid; } } foreach (listToRanges($ordinary) as $range) { $ret .= 'switchport trunk allowed vlan ' . ($cmd['opcode'] == 'add allowed' ? 'add ' : 'remove ') . ($range['from'] == $range['to'] ? $range['to'] : "{$range['from']}-{$range['to']}") . "\n"; } $ret .= "exit\n"; break; case 'set native': $ret .= "interface {$cmd['arg1']}\n"; # default VLAN special case if ($cmd['arg2'] == VLAN_DFL_ID) { $ret .= "no switchport default-vlan tagged\n"; } else { $ret .= "switchport trunk native vlan {$cmd['arg2']}\n"; } $ret .= "exit\n"; break; case 'unset native': $ret .= "interface {$cmd['arg1']}\n"; # default VLAN special case if ($cmd['arg2'] == VLAN_DFL_ID) { $ret .= "switchport default-vlan tagged\n"; } else { # Although a native VLAN is always one of the allowed VLANs in ROS (as seen in the # output of "show interfaces switchport"), the config text doesn't display the # native VLAN in the list of allowed VLANs. Respectively, setting the current # native VLAN as allowed leaves it allowed, but not native any more. $ret .= "switchport trunk allowed vlan add {$cmd['arg2']}\n"; } $ret .= "exit\n"; break; case 'getlldpstatus': $ret .= "show lldp neighbors detail\n"; break; case 'getportstatus': $ret .= "show interfaces status\n"; break; case 'getmaclist': $ret .= "show mac address-table dynamic\n"; break; case 'getportmaclist': $ret .= "show mac address-table dynamic interface {$cmd['arg1']}\n"; break; case 'cite': $ret .= $cmd['arg1']; break; case 'getallconf': case 'get8021q': $ret .= "show running-config\n"; break; default: throw new InvalidArgException('opcode', $cmd['opcode']); } } return $ret; }