基本的なトラフィック制御の実現

以下のテキストは、執筆時当時の情報を元に書いたものであり、 現在の情勢にそぐわないことを含む場合があるので注意されたい。 また、テキストは最終提出原稿で校正を経る前のものなので、実際にUNIXUSER 本誌に記載されたものとは異なる。誤字脱字等そのままである。

致命的な誤り以外は加筆修正等は行なわないので情報の鮮度に気をつけつつ 利用して欲しい。

目次


===========================================
Part2 
===========================================

Part1
 ipfw+dummynet(FreeBSD)  iproute2+tc(Linux)
 altq(NetBSD) 











	* 
	  ()
	* 
	  ()




	* 1Mbps
	*  SSH(22), HTTP(80), SMTP(25)
	  ()
	* SMTP1/4
	* 
	  HTTPSSH
	* SSH





Part1


	* SMTP Traffic Shaping 256Kbps
	* 768KbpsHTTPSSH 3:7 
	  


Part1
Weighted Fair Queue  Traffic Shaping 
 

---[ ]------------------------------------------------------------
%%% 

       
         (1Mbps)
   +------------------------------------------------------+
   |                                                      |
   |  +----256Kbps-----------+                            |
   |  |                      |                            |
   |  |     [SMTP]	     |                            |
   |  | [SMTP]        [SMTP] |                            |
   |  |         [SMTP]	     |                            |
   |  +----------------------+                            |
   |  +------------------- 768Kbps --------------------+  |
   |  |			        		       |  |
   |  | +---------+  +-------------------------------+ |  |
   |  | | [HTTP]  |  |   [SSH]                       | |  |
   |  | |         |  |                 [SSH]         | |  |
   |  | | [HTTP]  |  |             [SSH]             | |  |
   |  | |                              [SSH]         | |  |
   |  | | [HTTP]  |  |        [SSH]                  | |  |
   |  | |         |  |                    [SSH]      | |  |
   |  | |         |  |    [SSH]                      | |  |
   |  | +---------+  +-------------------------------+ |  |
   |  |       3    :    7                              |  |
   |  +------------------------------------------------+  |
   +------------------------------------------------------+

----------------------------------------------------------------------


FreeBSDLinux


	1.  Traffic Shaping 
	2. 3:7
	3. 12()





256Kbps
256Kbps
256Kbps



PRIQ




ipfw(FreeBSD)


ipfw
ipfw


 
---[ ]------------------------------------------------------------
sysctl net.inet.ip.fw.one_pass 

----------------------------------------------------------------------

	ipfw pipe 512 config bw 512K
	ipfw add pipe 512 tcp from me 22 to any

SSH512Kbps


	ipfw add reset tcp from me 22 to 10.10.10.1




pipequeue

ipfwdummynet(4)2


	* pipe	-  
	  	  
	* queue	- WF2Q+
	  	  pipequeue
		  pipequeue
		  

 pipe  queue 



  pipequeue

	ipfw pipe  config 
	ipfw queue  config 

  165535
   

---[ ]------------------------------------------------------------

 ----------------------------------------------------------
  pipequeue
 ----------------------------------------------------------
  buckets 
	
  mask 
	pipequeuesrc/dst
  	(
  	)pipe/queue
	  dst-ip 
	  src-ip 
	  dst-port 
	  src-port 
	  proto 
	  all
	
  	pipe/queuepipe/queue
  	
  	
  noerror
	
  plr 
	(01)()
  queue ( | KB)
	queue
  red | gred  w_q/min_th/max_th/max_p
	RED(Random Early Detection)GRED(Glant RED)
 ----------------------------------------------------------
  pipe
 ----------------------------------------------------------
  bw 
	 bit/s  Byte/s 
	 K  M 
  delay 
	
 ----------------------------------------------------------
  queue
 ----------------------------------------------------------
  pipe 
	
  weight 
	(1100)
  	
 ----------------------------------------------------------

----------------------------------------------------------------------

  256Kbpspipepipe
  
  256

	ipfw pipe 256 config bw 256K

  256
   ipfw  add 

	ipfw add [] pipe  

  
   SSH
  256Kbpspipe
  

---[ ]------------------------------------------------------------
  : 2

						IP
   	server		10.1.1.1
   		client		10.1.1.100
----------------------------------------------------------------------



	server# ipfw add pipe 256 tcp from me 22 to 10.1.1.100
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   client(10.1.1.100)sshserver
   

	client# ssh -o Compression=no server cat /bin/csh | dd of=/dev/null
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	1273+1 records in
	1273+1 records out
	651840 bytes transferred in 21.670 secs (30080 bytes/sec)

  SSHCPU
  
  
  SSH
  
  ftp 
---[ ]------------------------------------------------------------




----------------------------------------------------------------------

(30080 bytes/sec)Kbps

	% echo '30080*8/1024' | bc -l
	  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
	235.00000000000000000000

  235Kbps256Kbps

queue

HTTPSSH 3:7 
pipe

	server# ipfw sh | grep 'pipe 256'
		~~~~~~~
	00100     1858      869804 pipe 256 tcp from me 22 to 10.1.1.100
	(100)
	server# ipfw del 100
		~~~~~~~~~~~~

256Kbpspipequeue3:7
256pipe

	server# ipfw queue 1 config weight 30 pipe 256
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	server# ipfw queue 2 config weight 70 pipe 256
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HTTPSSHqueue


	server# ipfw add queue 1 tcp from me 80 to 10.1.1.100
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	server# ipfw add queue 2 tcp from me 22 to 10.1.1.100
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(10.1.1.100)HTTPSSH


	(SSH)
	server# ssh -o Compression=no server cat /bin/csh|dd of=/dev/null
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	1273+1 records in
	1273+1 records out
	651840 bytes transferred in 21.605 secs (30170 bytes/sec)
         235.7Kbps

	(HTTP)
	server# ssh -o Compression=no server cat /bin/csh|dd of=/dev/null
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	1273+1 records in
	1273+1 records out
	651840 bytes transferred in 31.079 secs (20973 bytes/sec)
         163.9Kbps

70%30%HTTP




ipfwSMTP/HTTP/SSH

 


	server# ipfw sh |grep queue
		~~~~~~~~~~~~~~~~~~~
	00100     2591     3224774 queue 1 tcp from me 80 to 10.1.1.100
	00200     1409     2031788 queue 2 tcp from me 22 to 10.1.1.100
	(100, 200)
	server# ipfw del 100
		~~~~~~~~~~~~
	server# ipfw del 200
		~~~~~~~~~~~~

 

	* SMTP256Kbpspipe
	* 768Kbpspipe3:7queue2


 

---[ ]------------------------------------------------------------
#!/bin/sh
ipfw pipe 256 config bw 256K
ipfw pipe 768 config bw 768K
ipfw queue 1 config weight 30 pipe 768
ipfw queue 2 config weight 70 pipe 768
ipfw add pipe 256 tcp from me to any 25 via ppp0
ipfw add queue 1 tcp from me 80 to any via ppp0
ipfw add queue 2 tcp from me 22 to any via ppp0
----------------------------------------------------------------------


via ppp0 ppp0
WAN




iproute2+tc


iproute2+tc 
HTB(Hierarchical Token Bucket) 
CBQiproute2+tc



ipfwWF2Q+HTB
HTBTraffice Shaping  Token Bucket 




 Linux kernel 2.4.28 
http://www.kernel.org/pub/linux/kernel/v2.4/linux-2.4.28.tar.bz2
 /usr/src QoS 

---[ ]------------------------------------------------------------
 bison, ncurses-devel 
apt-get 
apt-get update
apt-get install kernel-source
apt-get install bison
apt-get install ncurses-devel

----------------------------------------------------------------------

HTB  U32 
 /usr/src/linux  make menuconfig 

	Networking options  --->
	  QoS and/or fair queueing  --->

 
---[ ]------------------------------------------------------------
 image menuconfig.png
 %%
 %%

 Linux Kernel v2.4.28 Configuration
 
QoS  and/or fair queueing 
  Arrow keys navigate the menu.   selects submenus --->.           
  Highlighted letters are hotkeys.  Pressing  includes,  excludes,  
   modularizes features.  Press  to exit,  for Help.      
  Legend: [*] built-in  [ ] excluded   module  < > module capable      
    
               [*] QoS and/or fair queueing                           
               < >   CBQ packet scheduler                             
               <*>   HTB packet scheduler                             
               < >   CSZ packet scheduler                             
               < >   H-FSC packet scheduler                           
               < >   The simplest PRIO pseudoscheduler                
               < >   RED queue                                        
               <*>   SFQ queue                                        
               < >   TEQL queue                                       
               < >   TBF queue                                        
               < >   GRED queue                                       
               < >   Network emulator                                 
               < >   Diffserv field marker                            
               [ ]   QoS support                                      
               [*]   Packet classifier API                            
               < >     TC index classifier                            
               < >     Routing table based classifier                 
               < >     Firewall based classifier                      
               <*>     U32 classifier                                 
    


 %%%

 [*] QoS and/or fair queueing
   <*>   CBQ packet scheduler
   <*>   HTB packet scheduler
   < >   H-FSC packet scheduler
   < >   The simplest PRIO pseudoscheduler
   < >   RED queue
   <*>   SFQ queue
   < >   TEQL queue
   <*>   TBF queue
   < >   GRED queue
   < >   Network emulator
   < >   Diffserv field marker
   [ ]   QoS support
   [*]   Packet classifier API
   < >     TC index classifier
   < >     Routing table based classifier
   < >     Firewall based classifier
   <*>     U32 classifier
----------------------------------------------------------------------


&

	(LILO)
	# make dep
	# make bzImage
	# make bzlilo
	( /etc/lilo.conf )

HTBtc

tc(/sbin/tc)HTB

http://luxik.cdi.cz/~devik/qos/htb/
HTB3

http://luxik.cdi.cz/~devik/qos/htb/v3/htb3.6-020525.tgz
 tc  /sbin 
(tc)

qdiscclass

qdiscclass
iproute2

qdisc(queue discipline)
(class)
qdiscqdisc
qdisc
 


qdisc
 CBQ  HTB(Hierarchical Token Bucket) CBQPart1
iproute2CBQ
HTB 
class
HTB

---[ ]------------------------------------------------------------
iproute2 qdisc  class handle
----------------------------------------------------------------------

handle

qdisc
handleclass
qdischandlehandle

	:

 qdisc  qdisc 
0qdiscclass
qdisc0
class

	:

 :0 
qdisc

root qdisc handle  1: 
handletc
handle


HTBqdiscclass

HTB

ipfwSSH256Kbps
 eth0 
 root qdisc (1:) HTBqdisc

	# tc qdisc add dev eth0 root handle 1: htb

htb qdisc 256Kbps htb class

	# tc class add dev eth0 parent 1: classid 1:1 htb rate 256kbit

 1:0 qdisc htb class  1:1 
handlerate class

classSSH
  10.1.1.100 SSH htb class 1:1


	# tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 \
	   match ip sport 22 0xffff \
	   match ip dst 10.1.1.100  flowid 1:1



HTBSMTP/HTTP/SSH

 

 qdisc class  qdisc 
class 

---[ ]------------------------------------------------------------
%%% 

       
         (1Mbps)
   +---------------- htb qdisc 1: ------------------------+
   |                                                      |
   |  +-htb class 1:10 256K--+                            |
   |  |                      |                            |
   |  |     [SMTP]	     |                            |
   |  |                      |                            |
   |  |                	     |                            |
   |  +----------------------+                            |
   |  +------------ htb class 1:20 768Kbps ------------+  |
   |  |			        		       |  |
   |  | +----- htb class 1:80 230Kbps -------+         |  |
   |  | |                                    |         |  |
   |  | |       [HTTP]                       |   3     |  |
   |  | |                                    |         |  |
   |  | +--------------------------------  --+         |  |
   |  |			        		 :     |  |
   |  | +----- htb class 1:22 538Kbps ---  --+         |  |
   |  | |                                    |         |  |
   |  | |                                    |         |  |
   |  | |                                    |   7     |  |
   |  | |        [SSH]                       |         |  |
   |  | |                                    |         |  |
   |  | |                                    |         |  |
   |  | |                                    |         |  |
   |  | +------------------------------------+         |  |
   |  |                                                |  |
   |  +------------------------------------------------+  |
   +------------------------------------------------------+
----------------------------------------------------------------------



----------------------------------------------------------------------------
#!/bin/sh
# 
tc qdisc del dev eth0 root 2> /dev/null

# root qdisc
tc qdisc add dev eth0 root handle 1: htb

# SMTPclass
tc class add dev eth0 parent 1: classid 1:10 htb rate 256Kbit

# HTTP+SSHclass
tc class add dev eth0 parent 1: classid 1:20 htb rate 768Kbit

# 1:20HTTPSSHclass
tc class add dev eth0 parent 1:20 classid 1:80 htb rate 230Kbit ceil 768Kbit
tc class add dev eth0 parent 1:20 classid 1:22 htb rate 538Kbit ceil 768Kbit

# 
u32() {
  tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 "$@"
}
# SMTP, HTTP, SSH 1:10, 1:80, 1:22 
u32 match ip dport 25 0xffff flowid 1:10
u32 match ip sport 80 0xffff flowid 1:80
u32 match ip sport 22 0xffff flowid 1:22
----------------------------------------------------------------------------

HTBclass rate class
ceil
1:80  1:22 1:20ceil1:20rate


%%%
%%%       
%%% ipfwtc
%%% ipfw pipe/queue 
%%% tc
%%% tc
%%% 
%%% 
%%% iproute2tc
%%% 


ALTQCBQ(NetBSD)


CBQALTQPF+ALTQ
IPfilterALTQ
ALTQ
IPfilter 
ALTQ

NetBSD/i386 ALTQ


ALTQ

NetBSDALTQALTQ
 /usr/src/sys 
ALTQ




  /usr/src/sys/arch/i386/conf GENERIC
  

	# cd /usr/src/sys/arch/i386/conf
	# cp GENERIC ALTQ
	# vi ALTQ

  ALTQ3

---[ 3]---------------------------------------------------------
options         ALTQ
options         ALTQ_CBQ
options         ALTQ_WFQ
options         ALTQ_FIFOQ
options         ALTQ_RED
options         ALTQ_RIO
options         ALTQ_HFSC
options         ALTQ_CDNR
options         ALTQ_PRIQ
pseudo-device   altq
----------------------------------------------------------------------

   ALTQ_CBQ 
  config

	# config ALTQ



  1(altqattach)
  /usr/src/sys/altq/altq_conf.c altqattach 
   4
---[ 4]---------------------------------------------------------

#ifdef __FreeBSD__
static void altq_drvinit __P((void *));
#else
void    altqattach __P((int));
#endif

   


#ifdef __FreeBSD__
static void altq_drvinit __P((void *));
#elif defined(__NetBSD__)
void    altqattach __P((int));
void 
altqattach(int unused) 
{ /* XXX Dummy function */ 
} 
#else
void    altqattach __P((int));
#endif
----------------------------------------------------------------------

  
  /usr/src/sys/conf/files 

	defpseudo altq:         ifnet



  
  

	# cd /usr/src/sys/arch/i386/compile/ALTQ
	# make depend
	# make
	# make install

  ALTQ

ALTQ

ALTQaltqd(8)altqd
 /etc/altq.conf 
altq.conf(5)

altq.conf 3

	* interface

	  
	  bandwidth
	  

	   interface IF bandwidth BPS 

	* class

	  (1)

	  class  IF 
		  

	* filter

	  
	  (1)

	  filter IF  dest dest
			src src 

 /etc/altq.conf  altqd 
 -f 
altq.conf 

ALTQ_CBQ

ALTQ_CBQ
CBQ
Part18ALTQ_CBQ


	
------------------------------------------------------------
# pppoe0  1Mbps CBQ
interface pppoe0 bandwidth 1M cbq

# root. NULL
# pbandwidth 
class cbq pppoe0 root_class NULL priority 0 pbandwidth 100

#  default 
# 
# borrow
# 
class cbq pppoe0 default_class root_class borrow pbandwidth 100 default
------------------------------------------------------------

CBQ

256Kbpsdefault_class 
25%b256_class

------------------------------------------------------------
class cbq pppoe0 b256_class default_class pbandwidth 25
------------------------------------------------------------

FreeBSD/LinuxSSH256Kbps
 b256_class SSH

------------------------------------------------------------
filter pppoe0 b256_class 0 0 0 22 6
------------------------------------------------------------

 altq.conf  altqd 
SSH256Kbps


ALTQ_CBQSMTP/HTTP/SSH

Linux 5ALTQ_CBQ
ALTQ_CBQctl_class 
2%
 altq.conf  4


 4 ---------------------------------------------------------------
# 
interface pppoe0 bandwidth 1M cbq

# root. 4rootNULL
class cbq pppoe0 root_class NULL priority 0 pbandwidth 100

# 
class cbq pppoe0 default_class root_class borrow pbandwidth 100 default

# default_classsmtp_class, misc_class 
# 28%, 70% smtp_class  borrow 
# 28%ALTQ2%
# 2%
class cbq pppoe0 smtp_class default_class pbandwidth 28
        filter pppoe0 smtp_class 0 0 0 25 6
class cbq pppoe0 misc_class default_class borrow pbandwidth 70

# filter dest dest src src protocol
# protocol /etc/protocolsTCP6
# 0
class cbq pppoe0 http_class misc_class borrow pbandwidth 21
        filter pppoe0 http_class 0 0 0 80 6
class cbq pppoe0 ssh_class misc_class borrow pbandwidth 49
        filter pppoe0 smtp_class 0 0 0 22 6
----------------------------------------------------------------------------








Apache

IP




iproute2tc
OS


OS

Part3

---[ ]------------------------------------------------------------
----------------------------------------------------------------------
---[ ]------------------------------------------------------------
----------------------------------------------------------------------


yuuji@gentei.org
Fingerprint16 = FF F9 FF CC E0 FE 5C F7 19 97 28 24 EC 5D 39 BA
HIROSE Yuuji - ASTROLOGY / BIKE / EPO / GUEST BOOK / YaTeX [Tweet]