cancel
Showing results for 
Search instead for 
Did you mean: 

Problem this snippet solves:

The BIG-IP local traffic management system includes a feature called rate shaping which allows you to enforce a throughput policy on incoming traffic. Throughput policies are useful for prioritizing and restricting bandwidth on selected traffic patterns.

Rate shaping can be useful for an e-commerce site that has preferred clients and would like to offer higher throughput for preferred customers, and lower throughput for other site traffic.

This example will discuss how to create and manage rate classes as well as how to assign them to virtual servers.

Code :

#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
# Software Development Kit for iControl"; you may not use this file except in
# compliance with the License. The License is included in the iControl
# Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2009 F5 Networks,
# Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
param (
  $g_bigip = $null,
  $g_uid = $null,
  $g_pwd = $null,
  $g_cmd = $null,
  $g_name = $null,
  $g_arg1 = $null,
  $g_arg2 = $null,
  $g_arg3 = $null,
  $g_arg4 = $null
);

Set-PSDebug -strict;

#-------------------------------------------------------------------------
# function Write-Usage
#-------------------------------------------------------------------------
function Write-Usage()
{
  Write-Host @"
Usage: Rateclass.ps1 host uid pwd [options]
  options
  -------
  list
     - Get a list of rate classess
  create name rate [UNIT_BPS|UNIT_KBPS|UNIT_MBPS]
     - Create a Rate class with the given max throughput rates.
  delete name
     - Delete the specified rate class.
  setbaserate name rate [UNIT_BPS|UNIT_KBPS|UNIT_MBPS]
     - Set the maximum throughput to allot to traffic for this rate class.
  setburstsize name size
     - Set the maximum number of bytes that traffic is allowed to burst beyond the base rate.
  setceilingrate name rate [UNIT_BPS|UNIT_KBPS|UNIT_MBPS]
     - Set how far beyond the base rate the traffic is allowed to flow when bursting.
  setdirection name [DIRECTION_ANY|DIRECTION_CLIENT|DIRECTION_SERVER]
     - Set the direction that the rate class is applied.
  setparent name parent
     - Set the parent rate class that this class is allowed to borrow from.
  setqueuetype name [QUEUE_NONE|QUEUE_STOCHASTIC_FAIR|QUEUE_PRIORITY_FIFO]
     - Set the type of queue used by the specified rate class.
  setvsrateclass name virtual
     - Set the rate class for the specified virtual server.
  removerateclass name virtual
     - Remove the rate class from the specified virtual server.
"@;
  exit;
}

#-------------------------------------------------------------------------
# Get-RateClassList
#-------------------------------------------------------------------------
function Get-RateClassList()
{
  param([string]$rc = $null );
  if ( $rc )
  {
    $rc_list = ,$rc;
  }
  else
  {
    $rc_list = (Get-F5.iControl).LocalLBRateClass.get_list();
  }
  $vs_list = (Get-F5.iControl).LocalLBVirtualServer.get_list();
  $vsrc_list = (Get-F5.iControl).LocalLBVirtualServer.get_rate_class($vs_list);
  
  Write-Host "Rate Classes:";
  if ( $rc_list )
  {
    $base_rates = (Get-F5.iControl).LocalLBRateClass.get_base_rate( $rc_list );
    $burst_sizes = (Get-F5.iControl).LocalLBRateClass.get_burst_size( $rc_list );
    $ceiling_rates = (Get-F5.iControl).LocalLBRateClass.get_ceiling_rate( $rc_list );
    $directions = (Get-F5.iControl).LocalLBRateClass.get_direction( $rc_list );
    $parents = (Get-F5.iControl).LocalLBRateClass.get_parent( $rc_list );
    $queue_types = (Get-F5.iControl).LocalLBRateClass.get_queue_type( $rc_list );
    for($i=0; $i -lt $rc_list.Length; $i++)
    {
      Write-Host @"
--------------------
Rate Class $($rc_list[$i])
  Base Rate    : $($base_rates[$i].rate) $($base_rates[$i].unit)
  Burst Size   : $($burst_sizes[$i])
  Ceiling Rate : $($ceiling_rates[$i].rate) $($ceiling_rates[$i].unit)
  Direction    : $($directions[$i])
  Parent       : $($parents[$i])
  Queue Type   : $($queue_types[$i])
"@

    Write-Host "  Virtuals     :";
      for($j=0; $j -lt $vsrc_list.Length; $j++)
      {
        if ( $vsrc_list[$j] -eq $rc_list[$i] )
    {
      Write-Host "                 $($vs_list[$j])"
    }
      }
    }
  }
}

#-------------------------------------------------------------------------
# Create-RateClass
#-------------------------------------------------------------------------
function Create-RateClass()
{
  param([string]$name = $null, [int]$rate = 0, [string]$unit = "UNIT_BPS");
  if ( $name -and $rate )
  {
    $rateunit = New-Object -TypeName iControl.LocalLBRateClassRateUnit;
    $rateunit.rate = $rate;
    $rateunit.unit = $unit;
    (Get-F5.iControl).LocalLBRateClass.create( (,$name), (,$rateunit) );
    Write-Host "Rate Class $name created...";
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Delete-RateClass
#-------------------------------------------------------------------------
function Delete-RateClass()
{
  param([string]$name = $null);
  if ( $name )
  {
    (Get-F5.iControl).LocalLBRateClass.delete_rate_class( (,$name) );
    Write-Host "Rate Class $name deleted...";
    Get-RateClassList;
  }
}

#-------------------------------------------------------------------------
# Set-RateClassBaseRate
#-------------------------------------------------------------------------
function Set-RateClassBaseRate()
{
  param([string]$name = $null, [int]$rate = 0, [string]$unit = "UNIT_BPS");
  if ( $name -and $rate )
  {
    $rateunit = New-Object -TypeName iControl.LocalLBRateClassRateUnit;
    $rateunit.rate = $rate;
    $rateunit.unit = $unit;
    (Get-F5.iControl).LocalLBRateClass.set_base_rate( (,$name), (,$rateunit) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-RateClassBurstSize
#-------------------------------------------------------------------------
function Set-RateClassBurstSize()
{
  param([string]$name = $null, [int]$size = 0);
  if ( $name -and $size )
  {
    (Get-F5.iControl).LocalLBRateClass.set_burst_size( (,$name), (,$size) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-RateClassCeilingRate
#-------------------------------------------------------------------------
function Set-RateClassCeilingRate()
{
  param([string]$name = $null, [int]$rate = 0, [string]$unit = "UNIT_BPS");
  if ( $name -and $rate )
  {
    $rateunit = New-Object -TypeName iControl.LocalLBRateClassRateUnit;
    $rateunit.rate = $rate;
    $rateunit.unit = $unit;
    (Get-F5.iControl).LocalLBRateClass.set_ceiling_rate( (,$name), (,$rateunit) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-RateClassDirection
#-------------------------------------------------------------------------
function Set-RateClassDirection()
{
  param([string]$name = $null, [string]$direction = "DIRECTION_ANY");
  if ( $name )
  {
    (Get-F5.iControl).LocalLBRateClass.set_direction( (,$name), (,$direction) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-RateclassParent
#-------------------------------------------------------------------------
function Set-RateClassParent()
{
  param([string]$name = $null, [string]$parent = "");
  if ( $name )
  {
    (Get-F5.iControl).LocalLBRateClass.set_parent( (,$name), (,$parent) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-RateClassQueueType
#-------------------------------------------------------------------------
function Set-RateClassQueueType()
{
  param([string]$name = $null, [string]$type = "QUEUE_STOCHASTIC_FAIR");
  if ( $name )
  {
    (Get-F5.iControl).LocalLBRateClass.set_queue_type( (,$name), (,$type) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Set-VirtualServerRateClass
#-------------------------------------------------------------------------
function Set-VirtualServerRateClass()
{
  param([string]$name = $null, [string]$vs = $null);
  if ( $name )
  {
    (Get-F5.iControl).LocalLBVirtualServer.set_rate_class( (,$vs), (,$name) );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Remove-VirtualServerRateClass
#-------------------------------------------------------------------------
function Remove-VirtualServerRateClass()
{
  param([string]$name = $null, [string]$vs = $null);
  if ( $name )
  {
    (Get-F5.iControl).LocalLBVirtualServer.set_rate_class( (,$vs), (,"") );
    Get-RateClassList $name;
  }
}

#-------------------------------------------------------------------------
# Do-Initialize
#-------------------------------------------------------------------------
function Do-Initialize()
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
  
  return $success;
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) )
{
  Write-Usage;
}

if ( Do-Initialize )
{
  switch ($g_cmd)
  {
    "" {
      Get-RateClassList;
    }
  "list" {
      Get-RateClassList $g_name;
    }
    "create" {
      Create-RateClass $g_name $g_arg1 $g_arg2;
    }
    "delete" {
      Delete-RateClass $g_name;
    }
    "setbaserate" {
      Set-RateClassBaseRate $g_name $g_arg1 $g_arg2;
    }
    "setburstsize" {
      Set-RateclassBurstSize $g_name $g_arg1;
    }
    "setceilingrate" {
      Set-RateClassCeilingRate $g_name $g_arg1 $g_arg2;
    }
    "setdirection" {
      Set-RateClassDirection $g_name $g_arg1;
    }
    "setparent" {
      Set-RateClassParent $g_name $g_arg1;
    }
    "setqueuetype" {
      Set-RateClassQueueType $g_name $g_arg1;
    }
    "setvsrateclass" {
      Set-VirtualServerRateClass $g_name $g_arg1;
    }
    "removevsrateclass" {
      Remove-VirtualServerRateClass $g_name $g_arg1;
    }
    default {
      Write-Usage;
    }
    
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}
Version history
Last update:
‎09-Mar-2015 12:07
Updated by:
Contributors