squid限速001;

 

http://yp.oss.org.cn/software/show_resource.php?resource_id=934

在网上查了不少Squid中设置流量限制的文章,要么东抄西抄,语焉不详,要么翻译的不知所云,英文文档又讲的太理论,太繁琐。其实,这方便对我们一般网络管理员来说就那么几项设置有用,所以自己归纳总结了一些相关的知识和技巧。

Squid控制访问带宽的架构是这样的,首先,你可以定义一个或几个pool(池)来操控里面的流量,而你要为每个pool定义它们的class(类型),类型其实就是决定控制那些主机流量的范围,类型分为以下几种:

class定义:

class类型1表示只对pool设置相关总流量(这样比较好理解,不要加入什么bucket的概念)
class类型2表示既对pool设置总量控制,也为C类网段中的每个IP地址流量
class类型3表示既对pool设置总量控制,也为B类网段中的每个C类网段中的每个IP地址设置流量

最后就是对建立的pool设置它们的流量控制,整个过程是这样:

先在squid.acl.config文件中定义你允许哪些主机使用你的代理(这部分不属于带宽设置):

acl myteam src 172.16.23.108/32 172.16.23.53/32

acl mynet src 172.16.23.108/32 172.16.23.53/32

http_access allow myteam

http_access allow mynet

接下来进行带宽设置:

delay_pools 2  #假设你设置两个pools来分别管理两个不同的网段
 
delay_class 1 2    #设置第一个pools中的地址为C类网段中的每个IP地址流量
delay_access 1 allow myteam
delay_access 1 deny all
delay_parameters 1  64000/64000 32000/64000   #连接myteam总速度64000byte(64KB),每个ip可以3200的速度
 
delay_class 2 1  #将pool 2设为类型1,只控制总mynet的总流量
delay_access 2 allow mynet
delay_access 2 deny all
delay_parameters 2 32000/64000 #设置总流量速度为32000

注:   -1/-1表示流量无限制。 斜杆前后两个参数为最小流量与最大流量.

例如:delay_parameters 1 -1/-1  #表示你对pool 1不限速

每个delay_parameters的数值是由restore(byte/sec)/max(bytes)组成,restore是表示以bytes/sec的速度下载数据到bucket里,而max则表示bucket的容量bytes值.

备注2:SQUID FAQ中有提到,建议max至少要设为restore的两倍(It is recommended that the maximum is at least twice the restore value)

 

 

......

 

 

.....

 http://blog.chinaunix.net/uid-8474831-id-3503830.html

Squid自带限速功能delay_pool,但delay_pool的代码太复杂了。这次给大家介绍一种几十行代码的简单实现。

1. Squid下载速度的控制

要了解squid下载速度是怎样控制的,就必须先了解squid的数据下载流程。

下图是squid数据发送的大概流程,略去了一些细节和请求读取/处理。

可以看到,发送数据的流程中可能造成延迟的只有2个环节,comm_write等待客户端收数据,storeClientCopy等待原站或磁盘的数据。其他环节都是无阻塞的,不会造成延迟。

2. Squid下载限速的思路

不难理解,如果要限制一个链接的速度,就必须在下载过程的某个环节加入一些延时。这就面临3个问题

1)      在哪里加

2)      加多少

3)      怎么加

只要这3个问题解决了,那么限速的问题就迎刃而解了。

首先,在哪加?

一般来说,clientWriteComplete进入storeClientCopy之前比较合适。因为这个时候改free掉的内存buf已经在进入clientWriteComplete之前free掉了。如果在comm_write之前加延迟的话,刚刚从磁盘或原站取来的数据要在内存buf里多呆一段时间,会造成squid进程内存占用高。

第二,加多少?

不难计算,假如你限制每秒发送20k数据的话,你就计算一下,这一秒是否已经发够了20k数据?如果已经发够了20k,那么就等到下一秒的开始就可以了。例如现在是1秒200毫秒,就再等待800毫秒即可。

第三,怎么加?

当然不能用sleep加。这会使整个进程卡住的!

正确的加法是用squid的事件机制,eventAdd来加。

3. 实现

1.      structs.h,clientHttpRequest结构里面加成员,用于记录上一次发送数据的时间,以及这一秒已经发送了多少数据。

 

点击(此处)折叠或打开

  1.     dlink_node active;
  2.     squid_off_t maxBodySize;
  3.     STHCB *header_callback; /* Temporarily here for storeClientCopyHeaders */
  4.     StoreEntry *header_entry; /* Temporarily here for storeClientCopyHeaders */
  5.     int is_modified;
  6.     //add by xiaosi start
  7.     struct timeval last_sent_time_tv;
  8.     size_t last_sent_bytes;
  9.     int limit_speed_started;
  10.     //add by xiaosi end


2.      client_side.c里面,clientWriteComplete函数的前面,加上我们自己实现的方法。

 

其中SPEED是限制到多少byte/s。limit_speed_event_handler是加了延迟之后的事件回调。不难看出其核心是重新调用storeClientCopy

 

点击(此处)折叠或打开

  1. //add by xiaosi start
  2. #define SPEED 20000
  3.  
  4. typedef struct
  5. {
  6.         clientHttpRequest *http;
  7.         squid_off_t seen_offset;
  8.         squid_off_t copy_offset;
  9.         size_t sz; 
  10. } limit_speed_event_arg_t;
  11.  
  12. CBDATA_TYPE(limit_speed_event_arg_t);
  13.  
  14. static void limit_speed_event_handler(void *data)
  15. {
  16.         limit_speed_event_arg_t *ea = data;
  17.         if(!cbdataValid(ea->http) || !cbdataValid(ea->http->sc))
  18.         { 
  19.                 cbdataUnlock(ea->http);
  20.                 cbdataUnlock(ea->http->sc);
  21.                 cbdataFree(ea);
  22.                 return;
  23.         } 
  24.  
  25.         storeClientCopy(ea->http->sc,
  26.                         ea->http->entry,
  27.                         ea->seen_offset,
  28.                         ea->copy_offset,
  29.                         ea->sz,
  30.                         memAllocate (MEM_STORE_CLIENT_BUF),
  31.                         clientSendMoreData,
  32.                         ea->http);
  33.         cbdataUnlock(ea->http);
  34.         cbdataUnlock(ea->http->sc);
  35.         cbdataFree(ea);
  36. }
  37. //add by xiaosi end

 

 

3.      核心操作在这里。在clientWriteComplete里,阻断storeClientCopy的调用。就加在最后一个else里面即可。

        其中最主要的操作就是,符合一定条件时,不调用storeClientCopy,而是eventAdd,过一会再继续。

 

点击(此处)折叠或打开

 

  1. } else {
  2.          /* More data will be coming from primary server; register with
  3.          * storage manager. */
  4.         //add by xiaosi start
  5.  
  6.         if(!http->limit_speed_started)
  7.         {
  8.                 http->last_sent_time_tv = current_time;
  9.                 http->last_sent_bytes = size;
  10.                 http->limit_speed_started = 1;
  11.         }
  12.  
  13.         int skip = 0;
  14.         if(http->last_sent_time_tv.tv_sec == current_time.tv_sec)
  15.         {
  16.                 if(http->last_sent_bytes + size > SPEED)
  17.                 {
  18.                         int usec = 1000000 - current_time.tv_usec;
  19.                         CBDATA_INIT_TYPE(limit_speed_event_arg_t);
  20.                         limit_speed_event_arg_t *ea = cbdataAlloc(limit_speed_event_arg_t);
  21.                         ea->http = http;
  22.                         ea->seen_offset = http->out.offset;
  23.                         ea->copy_offset = http->out.offset;
  24.                         ea->sz = STORE_CLIENT_BUF_SZ - http->last_sent_bytes - size + SPEED;
  25.                         cbdataLock(ea->http);
  26.                         cbdataLock(ea->http->sc);
  27.                         eventAdd("limit_speed_copyer", limit_speed_event_handler, ea, ((double)usec) / 1000000, 1);
  28.                         http->last_sent_time_tv = current_time;
  29.                         http->last_sent_bytes += size;
  30.                         skip = 1;
  31.                 }
  32.                 else
  33.                 {
  34.                         http->last_sent_bytes += size;
  35.                         http->last_sent_time_tv = current_time;
  36.                         skip = 0;
  37.                 }
  38.         }
  39.         else
  40.         {
  41.                 //in the diff second
  42.                 http->last_sent_bytes = 0;
  43.                 http->last_sent_time_tv = current_time;
  44.  
  45.                 skip = 0;
  46.         }
  47.  
  48.         if(!skip)
  49.         //add by xiaosi end
  50.         storeClientCopy(http->sc, entry,
  51.             http->out.offset,
  52.             http->out.offset,
  53.             STORE_CLIENT_BUF_SZ, memAllocate(MEM_STORE_CLIENT_BUF),
  54.             clientSendMoreData,
  55.             http);
  56.     }

 


4.      还有一点小细节,在clientHttpRequest创建出来的时候,初始化一下limit_speed_started为0。涉及到的函数有parseHttpRequestAbort和parseHttpRequest。只要加在cbdataAlloc(clientHttpRequest)的后面即可

 

 

点击(此处)折叠或打开

  1. //xiaosi add start
  2. http->limit_speed_started = 0;
  3. //xiaosi add end

 

 

 

好了。编译并安装,是不是下载成功限制到20k了呢?

另外,回源的速度是由客户端的速度决定的。客户端限定到20k之后,看一看回源速度,是不是也变成20k了呢?

 

这个例子是比较简单的,限制的速度是写死在代码里的,另外,也不支持只限速符合某些acl的请求。

速度的配置,acl的配置,这些实现相对简单,有squid开发经验的读者可以自行搞定。

有问题,想法或对我的算法的改进,欢迎留言哦!

 

 

......

http://studyboy.cn/?p=417

接触delay pool 是从昨天才开始的,一直很头疼怎么对squid用户做限速,

tc的限速太霸道,做不好就会影响整个服务器的对外速度

 

先看看转载的这个delay pool的例子

 

This seems to be a FAQ, and it shows up as requests pretty frequently, so I thought I’d post a very short howto. This isn’t really a mod, since it can be easily done on a stock SmoothWall.

 

Since fixes 6 or 7, Squid (the web proxy included with SmoothWall) his included support for delay pools, which is a tremendously useful way to keep your heavy users’ web downloading in check, without penalizing ordinary users (ie, casual browsers).

The basic concept is that each client on the network has his own pool or “bucket” of bandwidth that drains as he browses the web. The bucket is constantly being refilled at a rate you specify. A brief example:

  • You choose to give each user a 2 megabyte bucket and 4 K/s refill rate
  • A casual browser visits a web page with 300 K of text and photos. He downloads those files at your connection’s maximum speed in a few seconds. Now his bucket has ~1700 K left. Over the next few minutes as he reads the page, his bucket is refilled at 4 K/s. By the time he clicks a link and loads another page, his bucket has topped out at 2000KB.
  • A heavy user starts a 10 MB download. He gets the first 2000KB at your connection’s maximum speed, and then his bucket is empty. His download isn’t stopped or cancelled though – his bucket is also refilling at 4 K/s, except he uses that as soon as it appears. The net effect is that he gets the first 2 MB at your network’s top speed, and the other 8 MB at 4 K/s.

The real advantage in using delay pools is that you don’t have to regulate what people can and can’t download. Anyone can download any file of any size – but the abusive clients who leave their computers downloading something 24/7 will be automatically throttled, while casual users will never be affected.

Delay Pools is not QOS. It does not prioritize traffic by type. It’s nothing more than selective HTTP throttling for heavy users.

I typically have 100+ users sharing a 1 mbps internet connection, and it’s made a world of difference. A very simple config is as follows (the file to edit is /var/smoothwall/proxy/acl):

Code:
acl users1 src 192.168.1.2-192.168.1.254/32
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.1/255.255.255.255

 

acl SSL_ports port 445 443 441 563
acl Safe_ports port 80                  # http
acl Safe_ports port 81                  # smoothwall http
acl Safe_ports port 21                  # ftp 
acl Safe_ports port 445 443 441 563     # https, snews
acl Safe_ports port 70                  # gopher
acl Safe_ports port 210                 # wais  
acl Safe_ports port 1025-65535          # unregistered ports
acl Safe_ports port 280                 # http-mgmt
acl Safe_ports port 488                 # gss-http 
acl Safe_ports port 591                 # filemaker
acl Safe_ports port 777                 # multiling http
acl CONNECT method CONNECT

http_access allow localhost
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow users1
http_access deny all

################################################################################
# delay_pools config
################################################################################

# my delay_pools acl, as defined above, is:
# acl users1 src 192.168.1.2-192.168.1.254/32

# define one class 2 pool
delay_pools 1
delay_class 1 2

# users1 follows the rules of pool 1
delay_access 1 allow users1
delay_access 1 deny all

# Everyone in users1 has access to the full bandwidth until
# his 2 megabyte bucket is empty, then it refills at 4 kbyte/sec
# 1 kbyte = 1024, 1 mb = 1048576
# the two commented out lines are:
# – 8 MB bucket, 16 K/s refill
# – 4 MB bucket, 8 K/s refill

#delay_parameters 1 -1/-1 16384/8388608
#delay_parameters 1 -1/-1 8192/4194304
delay_parameters 1 -1/-1 4096/2097152

# everyone’s bucket starts out full
delay_initial_bucket_level 100

(After editing /var/smoothwall/proxy/acl you need to restart Squid via the SmoothWall GUI – this will generate a new squid.conf file.)

You can get much more complicated than that; there are other types of pools, you can play with Squid acls to have different policies for different clients at different times of the day or for different file types.

The above simply imposes a 2 MB bucket with 4 K/s refill on all users in 192.168.1.2 – 192.168.1.254.

If you’re having problems with network congestion, or you’re always going over your ISP’s bandwidth quota, try using delay pools to keep web downloading in check. Experiment with different bucket sizes and refill rates. In general, a large bucket size is desirable to prevent casual users from ever being throttled – the heavy downloaders are going to be stuck at the refill rate within minutes anyway.

Of course, delay pools are useless if your abusive clients aren’t sending traffic through the proxy – so it’s usually wise to have Squid enabled in transparent mode so they have no choice.

Hope this is helpful to someone.

 
 
解释下,
#定义一个延迟池,如果要定义两个,就2
delay_pools 1
#定义1号延迟池属于第二种延迟池分类
#延迟池分类分成3种,1:单机,2:一个C段。3:一个B段
delay_class 1 2
#定义哪些用户要归于这个定义的延迟池
delay_access 1 allow users1
delay_access 1 deny all
 
# Everyone in users1 has access to the full bandwidth until
# his 2 megabyte bucket is empty, then it refills at 4 kbyte/sec
# 1 kbyte = 1024, 1 mb = 1048576
# the two commented out lines are:
# – 8 MB bucket, 16 K/s refill
# – 4 MB bucket, 8 K/s refill

 

#delay_parameters 1 -1/-1 16384/8388608
#delay_parameters 1 -1/-1 8192/4194304

#啥意思呢,就是这个池子里面的用户传输的流量超过2M,那么速度就会被打回4k/s,这样可以让那些下载小文件的用户能尽快下完,下载大文件的用户在下载了2M之后以慢速下载不影响别人
delay_parameters 1 -1/-1 4096/2097152
#定义所有人100%都符合这个规定
# everyone’s bucket starts out full
delay_initial_bucket_level 100

 

 

==========

http://blog.csdn.net/dolameng/article/details/2413125

视频点播业务是近年发展势头最好的互联网业务。本文以从技术角度解析视频点播服务关键技术

视频内容是如何在互联网进行分发的

视频网站如youtube,优酷网,土豆网,新浪视频等视频分享网站通多CDN技术进行视频内容分发。CDN翻译成汉语就是“内容分发网络”。编辑,网友制作或上传视频到CDN网络,CDN网络将这些视频分发到分布于全国各地IDC机房中的点播服务上。用户则就近访问最近的点播服务进行视频体验。组成CDN网络的关键软件有“Squid”  “Apache”    等基于HTTP协议的服务端软件 和域名服务器软件如 bind,域名服务器软件负责根据用户的ip地址将用户的http请求引导到离用户最近的IDC机房中的点播服务器。

CDN软件分析

流行的点播服务主要基于HTTP协议,mms协议,rmtp协议,其中http协议没有控制消息,所以此协议不支持对点播流媒体的控制操作,如进度拖动,play,pause,stop等。

squid apche,这两种服务器支持基于http的点播服务,时下最流行,大部分视频分享网站都采用类似这样的方案。squid主要功能是反向代理的功能。一般作为点播前端服务器使用。什么是反向代理---说白了就是,当用户访问squid服务软件时,如果squid没有此视频文件,则它会根据配置文件里设置的参数,到上行的数据中心去抓取文件,之后再返回给用户进行观看。举例说,当西安的用户访问优库网时,优酷网发现访问来自西安的用户,则将此访问引导至优酷网在西安布置的squid服务器,当squid发现自己没有用户要的视频时,则它根据配置文件里设置的参数,到北京的数据中心的服务器抓取文件到自己本地。之后再返回视频文件给用户。当下次再有用户访问相同的视频时,本地已经存在了,就直接返回视频给用户了。

media service,flash server。这两种服务器支持控制协议,其中media service支持mms,rtsp,flash server支持rtmp。这两种协议服务的点播业务,当用户访问时,用户可以拖动滚动条,和进行暂停,终止等控制操作。56网的视频可以拖动进行观看,我觉得应该使用的是rtmp协议。新浪早期的视频服务都是mms协议的视频点播,也支持拖动。

 

点播技术方案和优略势

squid + apache 方案,次方案的优势:

软件很成熟稳定。

视频分发采用拉模式。并且squid可以组群,所以分发简单,相对高效

技术开放。由于squid,apache都是linux上流行的软件,并且源码开放,所以用户可以进行二次开发

软件成本低、linux不需要授权费,squid,apache都免费,软件成本基本为0

劣势

squid是一个古董软件,设计的目标是代理功能,那个时候还没有视频分享概念。所以做cdn服务有些强人所难。很多公司搭建cdn网络时都针对squid进行改造和二次开发工作

squid代理的单位是文件。当视频文件比较大时,如几百兆大小,反向代理抓取效率低。大家都有经验,传几百兆大小的文件需要花很长时间,并且大量占用带宽和cpu。

squid与系统的结合。作为服务器软件,支持并发量是一个很重要的参数,如果一台服务器支持并发用户量大,则可以为公司节省大量成本。影响并发量数量有以下一些因素:

带宽:我们都知道服务器网卡通常是千兆网卡。如果视频编码率为400K码流,1000M除以400K,理论跑满网卡可以支持2500个连接。但实际情况一般能跑到500M,支持并发1000个连接就很不错了。

内存:当并发连接数量很多时,服务器使用的内存往往出现瓶颈。想想,如果一个连接需要使用1M内存进行数据传输,那么1000个并发1G内存就没了。这就是事实。所以ngix这个软件在这方面比apache太有优势了。

cpu:网络io在很多系统上消耗致命的cpu。原因是有些平台没有提供高效的io模型方案。网络io分阻塞和非阻塞模式,异步和同步模式。

最差的方式是阻塞同步模式。但这个模式实际是最常用的。这就是bsd socket的接口,read,write,connect,bind,listen,打多数软件为了编程方便,让调用线程阻塞调用这些系统调用。当并发量大的时候,尤其是服务器软件,这种编程方式绝对不能接受。

非阻塞同步模式:这个模式在服务器编程中最流行,系统调用在unix和linux是 select, poll,windows也支持select,但提供了相对性能更好的waitforobject系统调用。但这些调用的基础都是查询句柄状态,而且是在用户态下,当并发量大时,这个轮询也会耗去大量的cpu。linux2.6之前的服务器,只能采用这个方案,而且是最优方案。但这对于网络服务大并发的要求此方案不可接受。此方案有些二次开发针对write,read系统调用次数进行改造,替换成writev,readv这种基于iovector的调用替换,在io时减少系统调用的次数。但个人认为效果有限。

最高效的方案非阻塞异步模式:支持此模式的的操作系统有solaris ,windows,linux 2.6以上版本,freebsd

solaris 和windows在aio基础上实现,是真正的内核态异步io。linux的epoll调用只是异步通知句柄状态的改变,但io的读写还是用户负责,所以算不上真正的异步io。freebsd的kqueue也类似。但这个方案是现有方案的中的最佳方案了。其中本人更看好windows的iocp,因为windows的iocp中的系统调用不仅是真正的异步io,而且还支持overlapped读写,可以说支持多线程并发读写同一个io句柄。windows平台应该是网络服务最优前途的方案。太牛了。solaris的aiowait系统调用不支持多线程。

 

cpu对io的影响好像用掉的笔墨太多,主要是这个对并发量影响巨大。

带宽限速:我们都知道,用户带宽越来越大,如果我们不限制用户下载速度,后果很严重。比如:如果用户使10M带宽,如果不限速,100个用户就把服务器带宽吃掉了。实际情况中,50个这样的,就不行了。所以服务器一定要限制用户下载速度。如果视频的码流是400K,最好限制用户500K,这样既不影响用户流畅的观看,又节省了服务器带宽。这样一台服务器并发就可以支持更多的用户了。

本地缓存:反向代理软件squid的主要的优势就是进行本地缓存。但如果缓存被大量的用户很少访问的文件占用,服务器磁盘空间毕竟有限,这时候会加剧对反向代理的次数。降低代理效果,所以对缓存要进行算法调整和设计。

cdn对p2p技术的应用。如果反向代理软件可以组成p2p网络,进行网络数据块交换,存储。这样会极大的减少数据中心访问次数,加速数据交换效率。squid因为是基于文件的代理,如果进行交换也是基于文件的交换,当问及大小巨大时,效果很差。

 

写的很多了。media service方案和flash server方案下次再写吧。睡觉了。。。

最后推荐我写的cdn软件 http://code.google.com/p/vodserver  这个软件配合 apache或iis可以轻松的搭建高效的cdn网络。下次再说它的好。。。。

 

 

===============================

http://striven.wicp.net/squid%E7%9A%84%E5%9B%9E%E6%BA%90%E9%99%90%E9%80%9F/

http://meisw.51099.com/show-624-1.html

squid限速

编译加-enable-delay-pools选项

添加以下行:
    delay_pools 1     
   delay_class 1 1
   delay_access 1 allow all
   delay_parameters 1 50000/50000 # 限制网速在50K以内
    delay_initial_bucket_level 50

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

今天测试了一下squid使用delay_pools来对源网站的访问进行限速.非常好用,如下

使用squid建二个源网站

 

1
2
3
4
5
6
acl php-oa url_regex -i ^http://.*php-oa.com/.*
acl sudo-u url_regex -i ^http://.*sudo-u.com/.*
http_access allow mysite
http_access allow sudo-u
http_access deny all
icp_access allow all

下面开始正文,我们要用到squid中的delay_pools,delay_pools里可以定义多个容器(多个源定义多个),而这个容器就是我们要控制的带宽,当容器到达所设定的容量时,这个容器的所有者就无法超过我们所设定的带宽限制.

 

开始设置squid的delay_pools

 

01
02
03
04
05
06
07
08
09
10
11
delay_pools 2  #设置二个pools来对二个源进行控制
  
delay_class 1 2    #设置第一个pools中的地址为C类网段中的每个IP地址流量
delay_access 1 allow php-oa
delay_access 1 deny all
delay_parameters 1  64000/64000 64000/32000   #连接php-oa总速度64000,每个ip可以3200的速度
  
delay_class 2 1
delay_access 2 allow sudo-u
delay_access 2 deny all
delay_parameters 2 32000/16000 #客户端下载sudo-u.com这个网站的总速度为1600,但squid连接源网站速度为32000

 

class定义:

class类型1为单个IP地址流量
class类型2为C类网段中的每个IP地址流量
class类型3为B类网段中的每个C类网段中的每个IP地址流量

delay_parameters语法:

类型1只有一个总带宽流量实际也就是这个IP地址的流量
delay_parameters pool total
例:delay_parameters 1 64000/64000

类型2有两个带宽流量参数,第一个为整个C类型网段流量,第二个为每个IP流量
delay_parameters pool tatal per-host
例:delay_parameters 1 -1/-1 64000/64000

类型3有三个带宽流量参数,第一个为整个B类网总流量,第二个为每个B类网段中的C类网段总流量,第三个为了B类网段中每个C类网段中的每个IP流量
delay_parameters pool total network per-host
例:delay_parameters 1 -1/-1 -1/-1 64000/64000

注:  -1/-1表示流量无限制.每个delay_parameters的数值是由"回源站的速度/客户最大下载速度"组成
  另外,对HIT的文件没有作用

 

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

UserDefine
acl vip src 192.168.24.0/255.255.255.0
acl normal_users src 192.168.11.232 192.168.11.211 192.168.11.231
acl vip_sc200 src 192.168.23.211 192.168.23.112
acl banneduser src  192.168.9.100
acl vip_me src 192.168.9.30
acl test src 192.168.100.250
acl all src 0.0.0.0/0
 
# define download files extension
acl disable_extension urlpath_regex -i \.mp3$ \.avi$ \.rmvb$ \.rm$ \.ra$ \.ram$ \.mpe$ \.smi$ \.mpeg$ \.wmv$ \.wma$ \.3gp$ \.td$ \.t
 
# define disable site dstdomain files.
 
# define disable ip for domain file
 
#Port Define
http_port 88
cache_mgr 5111
visible_hostname ifc-proxy
 
 
#HttpAccess
http_access allow  vip
http_access allow vip_me
http_access deny disable_extension
 
#MaxConn Define
acl num_conn maxconn 5
acl num_conn2 maxconn 20
 
#HttpAccess MaxConn Define
http_access deny normal_users num_conn
http_access deny test num_conn
http_access deny vip_sc200 num_conn2
 
#SpeedControl
delay_pools 4
delay_class 1 3
delay_class 2 3
delay_class 3 2
delay_class 4 3
delay_access 1 allow vip_sc200
delay_access 2 allow normal_users
delay_access 3 allow test
delay_access 4 allow all
delay_parameters 1 128000/128000 -1/-1 32000/64000
delay_parameters 2 64000/64000 -1/-1 8000/64000
delay_parameters 3 64000/64000 8000/64000
delay_parameters 4 1/1 1/1 1/1
前面一长段可以不看,看最后的一段,下面是注释
delay_pools 4 //表示有4个限制池,或者说对4种不同的目标限制
delay_class 1 3
delay_class 2 3
delay_class 3 2
delay_class 4 3 //上面的4行分别声明了四个池的类型,池1,2,4用于B类网段控制,池3用于C类网段控制,另外如果声明类型为class n 1表示控制单个ip地址
delay_access 1 allow vip_sc200
delay_access 2 allow normal_users
delay_access 3 allow test
delay_access 4 allow all //这几行分别对一开始定义的四个用户组进行限速,分别应用1,2,3,4号限制池
delay_parameters 1 128000/128000 -1/-1 32000/64000
delay_parameters 2 64000/64000 -1/-1 8000/64000
delay_parameters 3 64000/64000 8000/64000
delay_parameters 4 1/1 1/1 1/1
这四行分别声明了1,2,3,4号限制池的速度限制大小,比如第一个,我们前面声明的是B类网段,所以他的格式大概就是
delay_parameters 限制池号 本池最大速度 网段最大速度 单个ip最大速度
至于限制C类段的就少个网段最大速度,格式为
delay_parameters 限制池号 本池最大速度 单个ip最大速度

可能说的不太清楚,如果看不懂的可以参考下面的内容
 

关于设定SQUID带宽限制和流量整形,刻利用squid.conf种的delay_pools字段来完成.

delay pools里的bucket就像是一个容器,而这个容器就是squid要控制带宽用的,当容器到达所设定的容量时,这个容器的所有者就无法超过我们所设定的带宽限制,所有的bucket则称之为unified bucket.

Class分为三种:
(1)Class 1:包含一个unified bucket,而这个bucket是给这个class里所定义的host使用.
(2)Class 2:包含一个unified bucket和255个buckets,每一个bucket分配给8bit网络的使用者(255 hosts)使用IPv4 class C).
(3)Class 3:包含255个buckets,每一个bucket分配给16bit网络的使用者(65535 hosts)使用(IPv4 class B).

(1)Class 1:contains a single unified bucket which is used for all requests from hosts subject to the pool
(2)Class 2:contains one unified bucket and 255 buckets, one for each host on an 8-bit network (IPv4 class C)
(3)Class 3:contains 255 buckets for the subnets in a 16-bit network, and individual buckets for every host on these networks (IPv4 class B)

推测:如果ACL只定义一个class C字段,要限制每个host的单一带宽,可以使用Class 2来做;但如果ACL有定义好几个class C字段,使用Class 3可再对各个class C字段做个别的总带宽限制
delay_parameters语法:

class 1 delay pool;
delay_parameters pool total
class 2 delay pool;
delay_parameters pool tatal per-host
class 3 delay pool;
delay_parameters pool total network per-host

每个delay_parameters的数值是由restore(byte/sec)/max(bytes)组成,restore是表示以bytes/sec的速度下载object到bucket里,而max则表示bucket的bytes值.
备注1:如果要设定为unilit speed的话,将数值设定为-1即可

备注2:SQUID FAQ中有提到,建议max至少要设为restore的两倍(It is recommended that the maximum is at least twice the restore value)

[设定文档格式说明]
acl all src 0.0.0.0/0.0.0.0
acl lan src 192.168.1.0/255.255.255.0 # 定义 ACL
delay_pools n # 总共有几个 delay_pools
delay_class n1 1 # 第 n1 个 delay_pool 的种类是 Class 1
delay_class n2 3 # 第 n2 个 delay_pool 的种类是 Class 3
delay_class n3 2 # 第 n3 个 delay_pool 的种类是 Class 2
delay_access n1 allow lan
delay_access n1 deny all # 定义 delay_pool n1 的 access rule
delay_parameters n1 64000/64000 # 定义 delay_pool n1 的速度限制,依 class 的不同有不同的定义方式 (请参照上面的说明)

[范例说明]
1. 限制限制带宽为 512 Kbps
acl all src 0.0.0.0/0.0.0.0 # might already be defined
delay_pools 1
delay_class 1 1
delay_access 1 allow all
delay_parameters 1 64000/64000 # 512 kbits == 64 kbytes per second

2. 限制限制单一的带宽为 128 Kbps
acl only128kusers src 192.168.1.0/255.255.192.0
acl all src 0.0.0.0/0.0.0.0
delay_pools 1
delay_class 1 3
delay_access 1 allow only128kusers
delay_access 1 deny all
delay_parameters 1 64000/64000 -1/-1 16000/64000

3. 对某些特定的网站设置不通的带宽限制 (自己尝试一下,如果有错误请自行修改)
acl lan_use src 192.168.1.0/255.255.255.0 # 设置 LAN 使用者的 ACL
acl kkbox dstdomain .kkbox.com.tw # 设置特定域名的 ACL
delay_pools 2 # 设置两个 delay_pools
delay_class 1 1 # 第一个是 Class 1 的,用來限制总带宽
delay_class 2 2 # 第二个是 Class 2 的,用来限制单一的带宽
delay_access 1 allow kkbox
delay_access 1 deny all
delay_access 2 allow lan_use
delay_access 2 deny all
delay_parameters 1 64000/64000 # 不限制指定域名的单一带宽,但对总带宽速作限制
delay_parameters 2 64000/64000 10000/50000 # 限制 LAN 的所有使用者单一带宽,并对总的带宽作以限制

 

posted @ 2014-10-28 09:16  陳聽溪  阅读(1035)  评论(0编辑  收藏  举报