Controlling Traffic Shaping Programatically (throttling traffic dynamically)

  • Hey, I will open with the quick and dirty version, then elaborate farther:
    I need to find either:
    A) The pf-altq config files that pfsense alters, and alter them/apply changes from a script running on a cron job (to edit the upper limit on the fly)
    B) Find the code in pfsense that updates the upper limits on a queue, and call that code from my own to affect realtime changes to the upper limit on a queue.

    Full Explanation of what I am doing:

    I am currently working on a modification to our pfsense installation (which once finished up, I will happily release back into the community) Basically what it does is monitor bandwidth consumption in realtime, and meters it by the 95th percentile (using a cron job). The code is configured to know what our monthly 95th percentile billing rate is at our ISP (so if you pay for 5MB at the 95th percentile on a 100MB fibre connection, it's goal is to maintain that 5mb barrier without allowing you to be billed overage, while at the same time allowing maximum bandwidth on the network.)

    It does this by dynamically adjusting the upper limit of the master queue on my WAN, so that all traffic is hard throttled at a certain amount. In normal operation it runs that wide open, but it monitors and does the math to know that if I am X% through the month, and I have a 95th of Ymbps, then I need to reduce that for Z samples in order to bring it below the threshold of my billing rate. But it also does the math to know what my current "risk level" is based on where I am in the month (basically it becomes more and more strict about it's throttling as I approach the end of the month, because I have less buffer time to recover from a sudden spike).

    Anyway, the math is in it's infancy right now, but it will effectively throttle the rate and control so we can't get overage. Right now my challenge is to make it ACTUALLY throttle the bandwidth (which means the script that I am running from cron, needs to be able to change the upper limit on certain queues on the fly). I can't seem to find where pfsense stores this config file, or where in the code it actually affects a change to the upper limit (I don't know why I am having so much trouble tracing it in the code… The XML form code is throwing me off a fair bit for one).

    So if anyone can help I would greatly appreciate it. I want to get this actually functioning, so that I can return to smoothing out the algorithms so that I get an effective and efficient solution working, then finally I want to develop a Web UI for it from within pfsense, so that it can be a package that can be released to the community. (I figure alot of tier1 bandwidth providers meter on the 95th so it is very useful to be able to control these rates. From a billing standpoint it can save thousands of dollars per month for someone using it). Also if anyone else is interested in helping with development, I would be thankful for the help (Right now I mostly need help with affecting change to upper limits on a queue, as well as I will need alot of help plugging the UI into pfsense).


  • If you use HFSC just this would do:

    foreach ($config['shaper']['queue'] as $queue) {
    if ($queue['name'] == $name_of_queue_to_change)
    $queue[$name_of_queue]['upperlimit'] = "on";
    $queue[$name_of_queue]['upperlimit3'] = [bandwidth to cap to];


    It should be called from PHP script.

  • What do I need to include in my script to keep it stand alone? (i'm assuming the write_config() function is in a library somewhere, but that's what I was having trouble finding. I assumed that was linked to affecting changes based on the config, but I couldn't find where that function exists.)

    So what libs do I need to include in my php file in order to first of all initially populate the $config array, and secondly have access to the write_config() function?


  • Nevermind I finally found the includes. Thanks a ton for your help! That got me on the right track.

    Once I get this working in a stable way I will release what I have and hopefully someone can give me a hand plugging it into the GUI in a sane fashion. I'm sure others may find this functionality useful.

  • Just was I was after!

    Just looking a provisioning a connection that is billed partly this way. An intelligent algorithm for this should GREATLY increase the value for money on these links. How much of the algorithm have you implemented? I will start thinking about it.

  • There are accounting programs in the freebsd tree that do the accounting and the data read by them can be used to reassign the queues.

    For example: does all o fthis already. It is coupled with ipfw(4) though.

    It can use netgraph and you can use that coupled with

    ng_pf module written by me and you can collect statistics from PF.

    With that you can build a utility which modifies the ruleset or creates rules on an anchor to take propper actions.

    Or the propper solution convince people to open a bounty with enough backing for me to make it interact with PF+ALTQ dynamically.

    Good day.

Log in to reply