Preamble


If you don't know NiceShaper, first of all, take a look at included config files, it's pretty usable template and really gives you solid start to see how build basic configuration.
Next come back and read this documentation to figure out how do it better, or what else you can do with this software.

Installation


To use NiceShaper you need Linux OS with working HTB and iptables.

Don't miss g++ and make programs, now go to be root, unpack package and type:

# make
# make install
now you have compiled program in /usr/local/bin directory.
If you haven't config dir yet:
9.# cp -R etc/niceshaper0.6 /etc
if yes, you need to be sure that there are no notation different between old and new version.


Configure


Configure files syntax.

- NiceShaper has 2 important files: /etc/niceshaper0.6/config.ns and /etc/niceshaper0.6/class.ns.
- include file /full/path inside them, attached next ones.
- configure uses "directives" and one or list of "values". eg:
rate 128kb/s
debug iptables iproute
- there can be situation that we have "directives" with many "parameters". eg:
stats unit kB/s file /var/www/stats/nsstats.txt
- each above situation can be write in two way, if parameters and values belongs to one directives they can be group or split to one/many lines.
- but beware! you can't split iface and class one, and each line must start with directive. eg.:
stats unit kB/s
stats file /var/www/stats/nsstats.txt mode 644
debug iptables
debug iproute
- proper bandwidth units are bit, bps, b/s, B, Bps and B/s where you can add 'k|m' if needed. default is b/s.
- '#' char is using for comment rest of line.
- '<#' comment '#>' to affect sector of file.
- ';' char is end of line equivalent, it give you way to minimalize length of config files by write a lot of directives in one line.
- remember, all examples are only examples and can be not optimal for you.

Main configure file, for example of asymmetric DSL 4/0.5Mbit.

- main configuration file /etc/niceshaper0.6/config.ns file, is divided on sections, one called global, and free number of sections for shape on appointed interfaces.
- for each section ( except global one ) there will be one process in system which will control attached classes.

<global>
run download upload
mark-on-ifaces eth0
stats unit kB/s
stats file /var/www/stats/nsstats.txt
stats owner root group root mode 644
log syslog true
log terminal true
log file none
</>

<download>
iface eth1 match dstip 192.168.0.0/24
iface eth2 match dstip 192.168.1.0/24
section speed 512kB/s
section shape 450kB/s
default low 10kB/s
default ceil 100kB/s
default htb prio 5
default htb scheduler sfq
default hold 30s
iptables hook POSTROUTING
reload 3s
</>

global section directives:

run - list of configured sections you want to run.
mark-on-ifaces - list of interfaces on which run fw filter in place of u32.
packets outs by this interfaces will be marked by iptables mark destination.
it's needed for control upload within network using private IP addresses and NATs outgoing packets, or to use advanced filters.
stats - statistics.
unit - bandwidth units for stats.
file {file|none} - automatic dump statistics to appointed file ( none for off ).
owner - set system owner for stats file ( default is root ).
group - set system group for stats file ( default is root ).
mode - set chmod for stats file ( default is 644 ).
classes {all|active|working|false} - define which classes should be displayed in stats (default: working).
all - display all defined classes.
active - display active classes only.
working - display classes with activity in time no longer, that 'hold' parameter define this.
false - do not display any classes.
sum {top|bottom|false} - how to display sum of section utilize.
log - how to log messages from program.
syslog {true|false} - log to system syslog, default true.
terminal {true|false} - log on Your terminal, default true, when initialization is finished properly it's going off (except debugs).
file {plik|none} - full path to disc file, default is none.
lang - messages language {en|pl}.

Your section directives:

iface interface match filter
- define work space for section, it's natural to place here your local network.
- define on which interface use HTB for this one, keep in mind that HTB can manage only outgoing packets on interface, so be sure you define here interface which packets going out not arrive.
- you can use niceshaper filters notation, or for this case simple:
iface eth1 dst network 192.168.0.0/24
and for manage upload:
iface eth0 src network 192.168.0.0/24
- iptables and iproute don't know interface aliases so put here only real interface part and it will work good.
- vlans uses notation like in linux OS - ethX.vid.
- for example:
eth1 - real eth1 interface or equivalent of eg. eth1:1, eth1:10 and so on aliases.
eth1.100 - on real interface eth1 vlan with vid 100.
section - parameters of section (in most cases bandwidth of your internet access).
speed - bandwidth of your internet access.
shape - value smaller about 10-20% than speed of your link.
- if it is too high you will see lags for interactive services.
- NiceShaper will reduce your users rates when summary load is higher than this value.
reload - how frequency renew HTB configuration.
4s is default, save and good working on each not so old machine.
smaller values can give better working but may make bigger load on slow processor.
this parameter must be between 0.1s to 600s with step not smaller than 0.1s.
mode - if you don't know iptables, you don't need to know, that this section will use:
download - POSTROUTING chain
upload - PREROUTING chain
remember this is only simple name alias for:
iptables - additional parameters for iptables.
hook - the same like mode but better to understand what will happend.
hook-insert - section main filters will be inserted into given chain.
hook-append - section main filters will be appended to given chain. ( this is identical like iptables-hook do ).
debug - echoes system commands before execute it.
iptables - echoes anything before send to iptables.
iproute - echoes anything before send to tc ( HTB ).
default - default parameters for each defined classes. All classes can override this default settings. It's not required, but gives You better reading.


Classes in NiceShaper


- file /etc/niceshaper0.6/class.ns include classes, which are equivalent for HTB classes.
- class is build with head and free number of parameters.
- all packets will be testing for match filters of each classes and pass to first equal.
- normally you will use standard type of class but, there are 3 special classes types with common features, they do not include measure rate to rest and do not use dynamic rate shaping:

do-not-shape - this class give tool to not shape localhost traffic e.g. localhost file server. still this class will compute rate and show in stats.
virtual - like before but on top of that packets match this class will be only compute for stats, and continue working with next classes.
wrapper - next similar but there you can set maximum rate for this class, it's good if you have slow local network like 2.4ghz WLAN, and do not want to have overload.

class definition

one simple class:

class download eth1 Mario
match dstip 192.168.0.77

or in one line notation ( in future i will use only first way for easier understand ):

class download eth1 Mariusz ; match dstip 192.168.0.77

class is starting with header and ending with next class header or end of file.

class section interface classname
section - section to which class will be attached.
interface - interface on which HTB for this class will be working.
classname - name of class for stats.

Each class need one parameter which is filter:

match test

- filter define which packets should go to class.
- class can have unlimited number of filters.

class parameters

low - minimal rate ( default 0 ).
ceil - maximal rate ( default is equal section shape ).
rate - static rate ( low = ceil = rate ).
strict - value in percent { 0% - 100% }, specify how You don't like long time transfers from each clients (default: 70).
With low values gives restrictive for these ones, higher values in situation of overload bandwidth will cut a lot of another classes.
hold - time of no active is needed to remove class from HTB ( default 30s ).
iptables - additional parameters for iptables.
target - {accept|return|drop} target in iptables chain, for packets passed to class.
accept - packet for class with this option, stops traversing parent mangle table chain (look at iptables hook directive).
return - packets for class with this option will be returning to PREROUTING/POSTROUTING chain, as RETURN destination is set instead of ACCEPT.
drop - packet is droped, it's simple util to block clients, especially when class file is created automaticaly.
htb - additional parameters for HTB.
prio - priority for htb class, value between 0 - 7 , where 0 is highest ( default is 5 ).
scheduler - qdisc type for HTB class {none|sfq|esfq}.
sfq - sfq scheduler configuration if using.
perturb - set sfq perturb parameter ( default 10 ).
esfq - esfq scheduler configuration if using.
perturb - set esfq perturb parameter ( default 10 ).
hash - esfq hash {classic|src|dst} ( default classic )

There is no need to write this line for each class if their are identical, use default instead, and overwrite in selected classes if needed.

simple filters

proto - protocol ( tcp, udp or icmp ), default all.
srcip - source ip address or network.
dstip - destination ip address or network.
srcport|sport - source port ( it needs proto too ).
dstport|dport - destination port ( it needs proto too ).
from localhost - it's need to proper work filters points packets going from localhost.
to localhost - it's need to proper work filters points packets going to localhost.

srcip and dstip can point one ip address or network with mask.
mask can be not continuous ( eg. 255.255.128.255 ) but in this case use packet marking.

for example:

all packets going to 192.168.0.77:
match srcip 192.168.0.77
pop3 from interia.pl going to network 192.168.0.0/29:
match srcip 217.74.65.69 srcport 110 dstip 192.168.0.0/29 proto tcp

filters working only with packets marking on interface

Iptables netfilter can use many more matching criteria with their modules, but U32 filter can't.
Here are filters you can use but need mark-on-ifaces for proper work.

not-srcip - all except of given source ip address or network.
not-dstip - all except of given destination ip address or network.
not-srcport|not-sport - all except of given source port ( it needs proto too ).
not-dstport|not-dport - all except of given destination port ( it needs proto too ).
length - length of packet in bytes, ie. 500, :500( from 0 to 500), 500: ( 500 and above ), 128:500 ( from 128 to 500 ).
state - state of packet {new|established|related|invalid|untracked }.
tos - Type of Service field in the IP header.
ttl - time to live field in the IP header.
ttl-lower - TTL less than the given value.
ttl-greater - TTL greater than the given value.
mark - packets with given mark value.


Working with IMQ


IMQ interfaces can be use equal like their physical brothers;)
But remember to redirect traffic of each classes on IMQs by imq-redirect in each class uses imq or for whole classes in section thanks to default directive:
default imq-redirect

example config file with imq:

<download>
iface eth1 match dstip 192.168.0.0/24
iface eth2 match dstip 192.168.1.0/24
iface imq5 match dstip 10.10.5.0/24
default imq-redirect
...
</download>

and class:

class download imq5 ip5_55
match dstip 10.10.5.55