Commit 17a9a263 authored by Ad Schellevis's avatar Ad Schellevis

interfaces, refactor and rename get_interface_info() to remove most of the...

interfaces, refactor and rename get_interface_info() to remove most of the performance bottlenecks. for https://github.com/opnsense/core/issues/1662
parent de5ff59f
......@@ -4621,32 +4621,34 @@ function get_failover_interface($interface, $family = "all")
}
//returns interface information
function get_interface_info($ifdescr)
function get_interfaces_info()
{
global $config;
$result = array();
$all_intf_details = legacy_interfaces_details();
$ifup = legacy_interface_listget('up');
$all_intf_stats = legacy_interface_stats();
foreach (legacy_config_get_interfaces(array("virtual" => false)) as $ifdescr => $ifcnf) {
$ifinfo = array();
if (empty($config['interfaces'][$ifdescr])) {
return;
}
$ifinfo['if'] = get_real_interface($ifdescr);
if (!empty($all_intf_details[$ifinfo['if']])) {
$ifinfo = array_merge($ifinfo, $all_intf_details[$ifinfo['if']]);
}
$chkif = $ifinfo['if'];
$ifinfotmp = legacy_get_interface_addresses($chkif);
$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
$ifinfo['subnet'] = $ifinfotmp['subnet'];
$ifup = legacy_interface_listget('up');
$ifinfo['status'] = (is_array($ifup) && in_array($chkif, $ifup)) ? 'up' : 'down';
$ifinfo['status'] = (is_array($ifup) && in_array($ifinfo['if'], $ifup)) ? 'up' : 'down';
$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
if (!empty($ifinfo['ipv6'])) {
foreach ($ifinfo['ipv6'] as $ipv6addr) {
if (!empty($ipv6addr['link-local'])) {
$ifinfo['linklocal'] = $ipv6addr['ipaddr'];
} elseif (empty($ifinfo['ipaddrv6'])) {
$ifinfo['ipaddrv6'] = $ipv6addr['ipaddr'];
$ifinfo['subnetv6'] = $ipv6addr['subnetbits'];
}
}
}
$ifinfotmp = legacy_interface_stats($chkif);
$ifinfotmp = $all_intf_stats[$ifinfo['if']];
$ifinfo['inerrs'] = $ifinfotmp['input errors'];
$ifinfo['outerrs'] = $ifinfotmp['output errors'];
$ifinfo['collisions'] = $ifinfotmp['collisions'];
......@@ -4744,51 +4746,11 @@ function get_interface_info($ifdescr)
}
if ($ifinfo['status'] == "up") {
/* try to determine media with ifconfig */
unset($ifconfiginfo);
exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
$wifconfiginfo = array();
if (is_interface_wireless($ifdescr)) {
exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
array_shift($wifconfiginfo);
}
$matches = "";
foreach ($ifconfiginfo as $ici) {
/* don't list media/speed for wireless cards, as it always
displays 2 Mbps even though clients can connect at 11 Mbps */
if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
} elseif (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
} elseif (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
}
if (preg_match("/status: (.*)$/", $ici, $matches)) {
if ($matches[1] != 'active') {
$ifinfo['status'] = $matches[1];
}
if ($ifinfo['status'] == 'running') {
$ifinfo['status'] = 'up';
}
}
if (preg_match("/channel (\S*)/", $ici, $matches)) {
$ifinfo['channel'] = $matches[1];
}
if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
if ($matches[1][0] == '"') {
$ifinfo['ssid'] = substr($matches[1], 1, -1);
} else {
$ifinfo['ssid'] = $matches[1];
}
}
if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
$ifinfo['laggproto'] = $matches[1];
}
if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
$ifinfo['laggport'][] = $matches[1];
}
}
foreach ($wifconfiginfo as $ici) {
$elements = preg_split("/[ ]+/i", $ici);
if ($elements[0] != "") {
......@@ -4824,8 +4786,10 @@ function get_interface_info($ifdescr)
$ifinfo['bridgeint'] = $bridge;
}
}
$result[$ifdescr] = $ifinfo;
}
return $ifinfo;
return $result;
}
function convert_seconds_to_hms($sec)
......
......@@ -65,8 +65,9 @@ include("head.inc");
<?php
$mac_man = json_decode(configd_run("interface list macdb json"), true);
$pfctl_counters = json_decode(configd_run("filter list counters json"), true);
$ifsinfo = get_interfaces_info();
foreach (get_configured_interface_with_descr(false, true) as $ifdescr => $ifname):
$ifinfo = get_interface_info($ifdescr);
$ifinfo = $ifsinfo[$ifdescr];
$ifpfcounters = $pfctl_counters[$ifinfo['if']];
legacy_html_escape_form_data($ifinfo);
$ifdescr = htmlspecialchars($ifdescr);
......
......@@ -31,8 +31,9 @@
function interfaces_api()
{
$result = array();
$ifsinfo = get_interfaces_info();
foreach (get_configured_interface_with_descr() as $ifdescr => $ifname) {
$ifinfo = get_interface_info($ifdescr);
$ifinfo = $ifsinfo[$ifdescr];
$interfaceItem = array();
$interfaceItem['inpkts'] = $ifinfo['inpkts'];
$interfaceItem['outpkts'] = $ifinfo['outpkts'];
......
......@@ -68,8 +68,9 @@ require_once("interfaces.inc");
<table class="table table-striped table-condensed" data-plugin="interfaces" data-callback="interface_widget_update">
<tbody>
<?php
$ifsinfo = get_interfaces_info();
foreach (get_configured_interface_with_descr() as $ifdescr => $ifname):
$ifinfo = get_interface_info($ifdescr);
$ifinfo = $ifsinfo[$ifdescr];
$iswireless = is_interface_wireless($ifdescr);?>
<tr id="interface_widget_item_<?=$ifname;?>">
<td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment