Ignore:
Timestamp:
May 1, 2016, 2:04:11 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Improved method for new network topology queue tree preparation. Not yet functional. Also RouterOS limits number of usable levels for queue tree to 7.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/NetworkConfigRouterOS/Generators/Queue.php

    r829 r831  
    44{
    55  var $UsedNames;
     6  var $Devices;
     7  var $QueueItems;
    68
    79  function CheckName($Name)
     
    1315  function Run()
    1416  {
    15     //print_r($this->BuildTree($this->System->Config['MainRouter']['DeviceId'], 140 * 1000 * 1000));
    16     //die();
    17 
    1817    $PathQueue = array('queue', 'tree');
    1918
     
    5352    {
    5453      $Service = $DbResult->fetch_array();
    55       $FreeInetSpeed = $Service['InternetSpeedMax'] * 1000;
     54      $FreeInetSpeed = $Service['InternetSpeedMax'];
    5655    } else $FreeInetSpeed = 0;
    5756
     
    6261    $ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global');
    6362
    64     // Divide rules by subnet number
    65     $DbResult = $this->Database->query('SELECT `Id`, `Name`, `AddressRange`, `Mask` FROM `NetworkSubnet`');
    6663    // Slow free internet
    6764    $PacketMark = GetMarkByComment('free-out');
     
    7168
    7269    // Process users
    73     $DbResult = $this->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` WHERE `Member`.`Blocked`=0');
     70    $DbResult = $this->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` '.
     71      'LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` WHERE `Member`.`Blocked`=0');
    7472    while($Member = $DbResult->fetch_assoc())
    7573    {
     
    8482        echo('Služba '.$Service['Name'].': ');
    8583        $MemberName = RouterOSIdent($Member['Name'].'-'.$Member['Id'].'-'.$ServiceIndex);
    86         $SpeedIn = round($Service['InternetSpeedMin'] / $InDivider) * 1000;
    87         $SpeedOut = round($Service['InternetSpeedMin'] / $OutDivider) * 1000;
    88         $UserMaxSpeedIn = round($Service['InternetSpeedMax'] / $InDivider) * 1000;
    89         $UserMaxSpeedOut = round($Service['InternetSpeedMax'] / $OutDivider) * 1000;
     84        $SpeedIn = round($Service['InternetSpeedMin'] / $InDivider);
     85        $SpeedOut = round($Service['InternetSpeedMin'] / $OutDivider);
     86        $UserMaxSpeedIn = round($Service['InternetSpeedMax'] / $InDivider);
     87        $UserMaxSpeedOut = round($Service['InternetSpeedMax'] / $OutDivider);
    9088        $ItemsQueue[] = array('name' => $MemberName.'-out', 'limit-at' => $SpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => 'main-out');
    9189        $ItemsQueue[] = array('name' => $MemberName.'-in', 'limit-at' => $SpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => 'main-in');
     
    117115            $Name = RouterOSIdent($Name);
    118116            echo($Name.', ');
    119             $IPParts = explode('.', $Interface['LocalIP']);
    120             $Subnet = $IPParts[2];
    121117            $PacketMark = GetMarkByComment($Name.'-out');
    122118            $ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
     
    133129          $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']);
    134130          echo($Subnet['Name'].', ');
    135           $IPParts = explode('.', $Subnet['AddressRange']);
    136           $SubnetNumber = $IPParts[2];
    137131          $PacketMark = GetMarkByComment($Subnet['Name'].'-out');
    138132          $ItemsQueue[] = array('name' => $Subnet['Name'].'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
     
    151145  }
    152146
     147  function UpdateMinSpeed($DeviceId)
     148  {
     149    $MinSpeed = 0;
     150    foreach($this->Devices[$DeviceId]['Childs'] as $DeviceChild)
     151    {
     152      $this->UpdateMinSpeed($DeviceChild);
     153      $MinSpeed += $this->Devices[$DeviceChild]['MinSpeed'];
     154    }
     155    $this->Devices[$DeviceId]['MinSpeed'] = $MinSpeed;
     156    if($this->Devices[$DeviceId]['DeviceCount'] > 0)
     157      $this->Devices[$DeviceId]['MinSpeed'] += round($this->Devices[$DeviceId]['InternetSpeedMin'] / $this->Devices[$DeviceId]['DeviceCount']);
     158  }
     159
    153160  // Calculate maximum real speed available for each network device Start with main router and continue with adjecement nodes.
    154161  function BuildTree($RootDeviceId, $BaseSpeed)
    155162  {
    156163    // Load network devices
    157     $Devices = array();
    158     $DbResult = $this->Database->query('SELECT `Name`,`Id` FROM `NetworkDevice`');
     164    $this->Devices = array();
     165    $DbResult = $this->Database->query('SELECT `NetworkDevice`.`Name`,`NetworkDevice`.`Id`, '.
     166      '`Service`.`InternetSpeedMin`, `Service`.`InternetSpeedMax`, '.
     167      '(SELECT COUNT(*) FROM `NetworkDevice` AS `T` WHERE `T`.`Service` = `NetworkDevice`.`Service`) AS `DeviceCount` FROM `NetworkDevice` '.
     168      'LEFT JOIN `ServiceCustomerRel` ON `ServiceCustomerRel`.`Id`=`NetworkDevice`.`Service` '.
     169      'LEFT JOIN `Service` ON `Service`.`Id` = `ServiceCustomerRel`.`Service`');
    159170    while($Device = $DbResult->fetch_assoc())
    160171    {
     
    162173      $Device['Calculated'] = false;
    163174      $Device['MaxSpeed'] = 0;
    164       $Devices[$Device['Id']] = $Device;
     175      $Device['MinSpeed'] = 0;
     176      $Device['Childs'] = array();
     177      $Device['Parent'] = 0;
     178      $Device['QueueName'] = '';
     179      $this->Devices[$Device['Id']] = $Device;
    165180    }
    166181
     
    172187      $Interface['Links'] = array();
    173188      $Interfaces[$Interface['Id']] = $Interface;
    174       $Devices[$Interface['Device']]['Interfaces'][] = $Interface['Id'];
     189      $this->Devices[$Interface['Device']]['Interfaces'][] = $Interface['Id'];
    175190    }
    176191
     
    187202    }
    188203
     204    // Calculate maximum speed for network devices
    189205    $DevicesToCheck = array($RootDeviceId);
    190     $Devices[$RootDeviceId]['MaxSpeed'] = $BaseSpeed;
    191     $Devices[$RootDeviceId]['Calculated'] = true;
     206    $this->Devices[$RootDeviceId]['MaxSpeed'] = $BaseSpeed;
     207    $this->Devices[$RootDeviceId]['Calculated'] = true;
    192208
    193209    while(count($DevicesToCheck) > 0)
    194210    {
    195       echo('Pass'."\n");
     211      //echo('Pass'."\n");
    196212      $NewDevicesToCheck = array();
    197213      foreach($DevicesToCheck as $DeviceId)
    198214      {
    199         echo($Devices[$DeviceId]['Name'].': ');
    200         foreach($Devices[$DeviceId]['Interfaces'] as $InterfaceId)
     215        //echo($this->Devices[$DeviceId]['Name'].': ');
     216        foreach($this->Devices[$DeviceId]['Interfaces'] as $InterfaceId)
    201217        {
    202218          foreach($Interfaces[$InterfaceId]['Links'] as $LinkId)
     
    207223
    208224            $Device2Id = $Interfaces[$Interface2Id]['Device'];
    209             if($Devices[$Device2Id]['Calculated'] == false)
     225            if($this->Devices[$Device2Id]['Calculated'] == false)
    210226            {
    211               $Devices[$Device2Id]['Calculated'] = true;
    212               $NewMaxSpeed = $Devices[$DeviceId]['MaxSpeed'];
     227              $this->Devices[$Device2Id]['Calculated'] = true;
     228              $NewMaxSpeed = $this->Devices[$DeviceId]['MaxSpeed'];
    213229              if($NewMaxSpeed > $Link['MaxRealSpeed'])
    214230                $NewMaxSpeed = $Link['MaxRealSpeed'];
    215               echo($Devices[$Device2Id]['Name'].' '.$Device2Id.', ');
    216               $Devices[$Device2Id]['MaxSpeed'] = $NewMaxSpeed;
    217               echo($NewMaxSpeed.", ".count($NewDevicesToCheck).' ');
     231              //echo($this->Devices[$Device2Id]['Name'].' '.$Device2Id.', ');
     232              $this->Devices[$Device2Id]['MaxSpeed'] = $NewMaxSpeed;
     233              // Set nodes tree relation
     234              $this->Devices[$Device2Id]['Parent'] = $DeviceId;
     235              $this->Devices[$DeviceId]['Childs'][] = $Device2Id;
     236              //echo($NewMaxSpeed.", ".count($NewDevicesToCheck).' ');
    218237              $NewDevicesToCheck[] = $Device2Id;
    219238            }
    220239          }
    221240        }
    222         echo("\n");
     241        //echo("\n");
    223242      }
    224243      $DevicesToCheck = $NewDevicesToCheck;
    225244    }
    226     //print_r($Devices);
     245
     246    // Calculate maximum speed for network devices
     247    $this->UpdateMinSpeed($RootDeviceId);
     248
     249    //print_r($this->Devices);
    227250
    228251    echo('Not linked network devices: ');
    229     foreach($Devices as $Device)
     252    foreach($this->Devices as $Device)
    230253    {
    231254      if($Device['MaxSpeed'] == 0) echo($Device['Name'].', ');
    232255    }
    233256    echo("\n");
    234 
    235     $Tree = array();
    236     return($Tree);
     257  }
     258
     259  function BuildQueueItems($DeviceId)
     260  {
     261    $Device = $this->Devices[$DeviceId];
     262
     263    // Device
     264    if($Device['Parent'] != 0)
     265      $ParentQueueName = $this->Devices[$Device['Parent']]['QueueName'];
     266      else $ParentQueueName = 'main';
     267    $Name = $Device['Name'];
     268    $Name = RouterOSIdent($Name);
     269    $DeviceName = $Name;
     270    $this->Devices[$DeviceId]['QueueName'] = $Name;
     271    echo($Name.', ');
     272    $PacketMark = GetMarkByComment($Name.'-out');
     273    $this->ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $Device['MinSpeed'],
     274      'max-limit' => $Device['MaxSpeed'], 'parent' => $ParentQueueName.'-out',
     275      'packet-mark' => $PacketMark);
     276    $PacketMark = GetMarkByComment($Name.'-in');
     277    $this->ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => $Device['MinSpeed'],
     278      'max-limit' => $Device['MaxSpeed'], 'parent' => $ParentQueueName.'-in',
     279      'packet-mark' => $PacketMark);
     280    $this->CheckName($Name.'-out');
     281    $this->CheckName($Name.'-in');
     282
     283    // Interfaces
     284    $DbResult3 = $this->Database->select('NetworkInterface', '*', '`Device` = '.$DeviceId.' AND `LocalIP` != ""');
     285    $IntCount = $DbResult3->num_rows;
     286    while($Interface = $DbResult3->fetch_assoc())
     287    {
     288      $Name = $Device['Name'];
     289      if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
     290        else $Name .= '-';
     291      $Name = RouterOSIdent($Name);
     292      echo($Name.', ');
     293      $PacketMark = GetMarkByComment($Name.'-out');
     294      $this->ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => round($Device['MinSpeed'] / $IntCount),
     295        'max-limit' => $Device['MaxSpeed'], 'parent' => $DeviceName.'-out',
     296        'packet-mark' => $PacketMark);
     297      $PacketMark = GetMarkByComment($Name.'-in');
     298      $this->ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => round($Device['MinSpeed'] / $IntCount),
     299        'max-limit' => $Device['MaxSpeed'], 'parent' => $DeviceName.'-in',
     300        'packet-mark' => $PacketMark);
     301      $this->CheckName($Name.'-out');
     302      $this->CheckName($Name.'-in');
     303    }
     304
     305    // Process childs
     306    foreach($Device['Childs'] as $DeviceChild)
     307    {
     308      $this->BuildQueueItems($DeviceChild);
     309    }
     310  }
     311
     312  function RunTopology()
     313  {
     314    $PathQueue = array('queue', 'tree');
     315
     316    $Routerboard = new Routerboard();
     317    $Routerboard->UserName = $this->System->Config['MainRouter']['UserName'];
     318    $Routerboard->Timeout = $this->System->Config['MainRouter']['ConnectTimeout'];
     319    $Routerboard->HostName = $this->System->Config['MainRouter']['HostName'];
     320    $Routerboard->Debug = true;
     321
     322    $this->UsedNames = array();
     323
     324    $Finance = &$this->System->Modules['Finance'];
     325    $Finance->LoadMonthParameters(0);
     326
     327    $InDivider = 1;
     328    $OutDivider = 1;
     329    $TotalMaxSpeedIn = round($Finance->RealMaxSpeed / $InDivider) * 1000;
     330    $TotalMaxSpeedOut = round($Finance->RealMaxSpeed / $OutDivider) * 1000;
     331    $UsersMaxSpeedIn = round($Finance->MaxSpeed / $InDivider) * 1000;
     332    $UsersMaxSpeedOut = round($Finance->MaxSpeed / $OutDivider) * 1000;
     333    $VoipMaxSpeedIn = $TotalMaxSpeedIn - 136;
     334    $VoipMaxSpeedOut = $TotalMaxSpeedOut - 136;
     335    $VoipSpeedIn = 100; //$SpeedReserve;
     336    $VoipSpeedOut = 100; //$SpeedReserve;
     337    $OutInterface = 'eth1';
     338    $InInterface = 'ifb0';
     339    $InetInterface = $this->System->Config['MainRouter']['InetInterface'];
     340
     341    $DbResult = $this->Database->select('Service', '*', '(`ChangeAction` IS NULL) AND (`Id`='.TARIFF_FREE.')');
     342    if($DbResult->num_rows == 1)
     343    {
     344      $Service = $DbResult->fetch_array();
     345      $FreeInetSpeed = $Service['InternetSpeedMax'];
     346    } else $FreeInetSpeed = 0;
     347
     348    $this->ItemsQueue = array();
     349
     350    // Root of tree and main limit
     351    $this->ItemsQueue[] = array('name' => 'main-out', 'limit-at' => $UsersMaxSpeedIn, 'max-limit' => $UsersMaxSpeedIn, 'parent' => 'global');
     352    $this->ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global');
     353
     354    // Slow free internet
     355    $PacketMark = GetMarkByComment('free-out');
     356    $this->ItemsQueue[] = array('name' => 'free-out', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-out', 'packet-mark' => $PacketMark);
     357    $PacketMark = GetMarkByComment('free-in');
     358    $this->ItemsQueue[] = array('name' => 'free-in', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-in', 'packet-mark' => $PacketMark);
     359
     360    $this->BuildTree($this->System->Config['MainRouter']['DeviceId'], 140 * 1000 * 1000);
     361
     362    $this->BuildQueueItems($this->System->Config['MainRouter']['DeviceId']);
     363
     364    //print_r($this->ItemsQueue);
     365    $Routerboard->ListUpdate($PathQueue, array('name', 'limit-at', 'max-limit',
     366      'parent', 'packet-mark'), $this->ItemsQueue, array(), true);
    237367  }
    238368}
Note: See TracChangeset for help on using the changeset viewer.