亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1065 | 回復(fù): 0
打印 上一主題 下一主題

Linux內(nèi)核中流量控制(5) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-11-14 00:51 |只看該作者 |倒序?yàn)g覽
Linux內(nèi)核中流量控制(5)
本文檔的Copyleft歸yfydz所有,使用GPL發(fā)布,可以自由拷貝,轉(zhuǎn)載,轉(zhuǎn)載時(shí)請保持文檔的完整性,
嚴(yán)禁用于任何商業(yè)用途。
msn:
yfydz_no1@hotmail.com
來源:
http://yfydz.cublog.cn
5.5 SFQ(Stochastic Fairness Queueing discipline)
SFQ算法是個(gè)比較簡單的算法,速度也比較快,算法維護(hù)一定數(shù)量的數(shù)據(jù)包隊(duì)列,入隊(duì)是將數(shù)據(jù)包進(jìn)
行哈希后插入某隊(duì)列,出隊(duì)則是輪詢方式出隊(duì)列,另外可設(shè)置一定的隨機(jī)因子,在計(jì)算哈希值時(shí)能碰
撞少些,流控算法在net/sched/sch_sfq.c中定義,在實(shí)現(xiàn)中, 隊(duì)列數(shù)量最大為128個(gè),這是保證SFQ私
有數(shù)據(jù)結(jié)構(gòu)能小于4K,能在一個(gè)頁面內(nèi)分配。在使用用不建議作為網(wǎng)卡的根節(jié)點(diǎn)流控,而是最好作為
分類流控方法如CBQ等的葉子節(jié)點(diǎn)。
5.5.1 SFQ操作結(jié)構(gòu)定義
// TC使用的SFQ配置參數(shù)結(jié)構(gòu)
struct tc_sfq_qopt
{
// 定額
unsigned quantum; /* Bytes per round allocated to flow */
// 擾動(dòng)周期
int  perturb_period; /* Period of hash perturbation */
// 隊(duì)列中的數(shù)據(jù)包數(shù)量限制
__u32  limit;  /* Maximal packets in queue */
unsigned divisor; /* Hash divisor  */
// 最大隊(duì)列數(shù)
unsigned flows;  /* Maximal number of flows  */
};
#define SFQ_DEPTH  128
#define SFQ_HASH_DIVISOR 1024
/* This type should contain at least SFQ_DEPTH*2 values */
// SFQ索引值是無符合8位數(shù)
typedef unsigned char sfq_index;
struct sfq_head
{
sfq_index next;
sfq_index prev;
};
// SFQ私有數(shù)據(jù)
struct sfq_sched_data
{
/* Parameters */
// 擾動(dòng)間隔, 隔一定時(shí)間修改HASH擾動(dòng) 值
int  perturb_period;
//
unsigned quantum; /* Allotment per round: MUST BE >= MTU */
// 流量限制值
int  limit;
/* Variables */
// 擾動(dòng)更新定時(shí)器
struct timer_list perturb_timer;
// HASH擾動(dòng)值
int  perturbation;
// 出隊(duì)隊(duì)列索引
sfq_index tail;  /* Index of current slot in round */
// 最大深度
sfq_index max_depth; /* Maximal depth */
// HASH值對(duì)應(yīng)的槽位索引表, 1024項(xiàng), HASH值范圍為0~1023
sfq_index ht[SFQ_HASH_DIVISOR]; /* Hash table */
// 活動(dòng)槽
sfq_index next[SFQ_DEPTH]; /* Active slots link */
short  allot[SFQ_DEPTH]; /* Current allotment per slot */
// 哈希值索引數(shù)組
unsigned short hash[SFQ_DEPTH]; /* Hash value indexed by slots */
// 數(shù)據(jù)包隊(duì)列, 128個(gè)隊(duì)列
struct sk_buff_head qs[SFQ_DEPTH];  /* Slot queue */
// 深度值, 256個(gè)成員
struct sfq_head dep[SFQ_DEPTH*2]; /* Linked list of slots, indexed by
depth */
};
SFQ數(shù)據(jù)結(jié)構(gòu)比較怪異, 數(shù)據(jù)存儲(chǔ)是數(shù)組, 但邏輯上又是雙向鏈表, 訪問時(shí)又使用數(shù)組索引。
// SFQ流控操作結(jié)構(gòu)
static struct Qdisc_ops sfq_qdisc_ops = {
.next  = NULL,
.cl_ops  = NULL,
.id  = "sfq",
.priv_size = sizeof(struct sfq_sched_data),
.enqueue = sfq_enqueue,
.dequeue = sfq_dequeue,
.requeue = sfq_requeue,
.drop  = sfq_drop,
.init  = sfq_init,
.reset  = sfq_reset,
.destroy = sfq_destroy,
// 注意沒有change函數(shù)
.change  = NULL,
.dump  = sfq_dump,
.owner  = THIS_MODULE,
};
5.5.2 SFQ一些基本操作
// HASH函數(shù)
static __inline__ unsigned sfq_fold_hash(struct sfq_sched_data *q, u32 h, u32 h1)
{
// 哈希擾動(dòng)值
int pert = q->perturbation;
/* Have we any rotation primitives? If not, WHY? */
// 計(jì)算哈希值, 最大值0x3ff=1023
h ^= (h1>(0x1F - pert));
h ^= h>>10;
return h & 0x3FF;
}
// SFQ哈希函數(shù)
static unsigned sfq_hash(struct sfq_sched_data *q, struct sk_buff *skb)
{
u32 h, h2;
// skb->protocol是鏈路層中的協(xié)議值
switch (skb->protocol) {
// IPV4
case __constant_htons(ETH_P_IP):
{
  struct iphdr *iph = skb->nh.iph;
// 哈希函數(shù)中用到了源地址,目的地址, 協(xié)議, 端口或SPI值
  h = iph->daddr;
  h2 = iph->saddr^iph->protocol;
  if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
      (iph->protocol == IPPROTO_TCP ||
       iph->protocol == IPPROTO_UDP ||
       iph->protocol == IPPROTO_SCTP ||
       iph->protocol == IPPROTO_DCCP ||
       iph->protocol == IPPROTO_ESP))
   h2 ^= *(((u32*)iph) + iph->ihl);
  break;
}
// IPV6
case __constant_htons(ETH_P_IPV6):
{
  struct ipv6hdr *iph = skb->nh.ipv6h;
// 用地址的最后4字節(jié)
  h = iph->daddr.s6_addr32[3];
  h2 = iph->saddr.s6_addr32[3]^iph->nexthdr;
  if (iph->nexthdr == IPPROTO_TCP ||
      iph->nexthdr == IPPROTO_UDP ||
      iph->nexthdr == IPPROTO_SCTP ||
      iph->nexthdr == IPPROTO_DCCP ||
      iph->nexthdr == IPPROTO_ESP)
   h2 ^= *(u32*)&iph[1];
  break;
}
default:
// 其他協(xié)議就用路由參數(shù), 鏈路層協(xié)議和sock指針
  h = (u32)(unsigned long)skb->dst^skb->protocol;
  h2 = (u32)(unsigned long)skb->sk;
}
// 計(jì)算哈希值
return sfq_fold_hash(q, h, h2);
}
// 鏈接操作
static inline void sfq_link(struct sfq_sched_data *q, sfq_index x)
{
sfq_index p, n;
// 第X個(gè)隊(duì)列尾索引,qlen是不會(huì)超過SFQ_DEPTH的
int d = q->qs[x].qlen + SFQ_DEPTH;
p = d;
n = q->dep[d].next;
// x節(jié)點(diǎn)插入到隊(duì)列s的最后, 但形成一個(gè)雙向環(huán)型鏈表
q->dep[x].next = n;
q->dep[x].prev = p;
q->dep[p].next = q->dep[n].prev = x;
}
// 減少, 將X號(hào)索引點(diǎn)斷開
static inline void sfq_dec(struct sfq_sched_data *q, sfq_index x)
{
sfq_index p, n;
// 斷開x號(hào)索引
n = q->dep[x].next;
p = q->dep[x].prev;
q->dep[p].next = n;
q->dep[n].prev = p;
// n==p表示鏈表空了
// 如果當(dāng)前鏈表是最多元素鏈表, 相應(yīng)最大鏈表長度減
if (n == p && q->max_depth == q->qs[x].qlen + 1)
  q->max_depth--;
sfq_link(q, x);
}
// 增加, 增加X處索引點(diǎn)
static inline void sfq_inc(struct sfq_sched_data *q, sfq_index x)
{
sfq_index p, n;
int d;
n = q->dep[x].next;
p = q->dep[x].prev;
q->dep[p].next = n;
q->dep[n].prev = p;
d = q->qs[x].qlen;
if (q->max_depth max_depth = d;
sfq_link(q, x);
}
5.5.3 初始化
static int sfq_init(struct Qdisc *sch, struct rtattr *opt)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
int i;
// 初始化定時(shí)器
init_timer(&q->perturb_timer);
// 定時(shí)器函數(shù)參數(shù)為流控結(jié)構(gòu)
q->perturb_timer.data = (unsigned long)sch;
// 定時(shí)器函數(shù), 定時(shí)修改擾動(dòng)值
q->perturb_timer.function = sfq_perturbation;
// 初始化哈希表索引, 都為SFQ_DEPTH
for (i=0; iht = SFQ_DEPTH;
for (i=0; iqs);
// 初始化dep的后SFQ_DEPTH個(gè)元素
  q->dep[i+SFQ_DEPTH].next = i+SFQ_DEPTH;
  q->dep[i+SFQ_DEPTH].prev = i+SFQ_DEPTH;
}
// SFQ流控?cái)?shù)據(jù)包總數(shù)限制
q->limit = SFQ_DEPTH;
q->max_depth = 0;
q->tail = SFQ_DEPTH;
// 配置SFQ是允許不帶任何參數(shù)的
if (opt == NULL) {
// 缺省定額值為網(wǎng)卡MTU
  q->quantum = psched_mtu(sch->dev);
// 不進(jìn)行擾動(dòng)更新
  q->perturb_period = 0;
} else {
// 根據(jù)配置的參數(shù)設(shè)置SFQ參數(shù)
  int err = sfq_change(sch, opt);
  if (err)
   return err;
}
// 初始化索引鏈表, 初始化dep的前128個(gè)元素
for (i=0; i
// SFQ哈希擾動(dòng)值修改, 這是定時(shí)器的定時(shí)函數(shù)
static void sfq_perturbation(unsigned long arg)
{
// 定時(shí)函數(shù)參數(shù)為流控結(jié)構(gòu)
struct Qdisc *sch = (struct Qdisc*)arg;
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
//生成隨機(jī)擾動(dòng)值
q->perturbation = net_random()&0x1F;
// 擾動(dòng)時(shí)間非0, 更新定時(shí)器, 現(xiàn)在是在時(shí)鐘中斷中, 定時(shí)器已經(jīng)從定時(shí)鏈表中拆除了,
// 所以要重新添加定時(shí)器
if (q->perturb_period) {
  q->perturb_timer.expires = jiffies + q->perturb_period;
  add_timer(&q->perturb_timer);
}
}
// 設(shè)置SFQ參數(shù), 只在初始化時(shí)調(diào)用, 以后將不再修改
static int sfq_change(struct Qdisc *sch, struct rtattr *opt)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
// SFQ配置參數(shù)
struct tc_sfq_qopt *ctl = RTA_DATA(opt);
// 配置參數(shù)合法性檢查
if (opt->rta_len
sch_tree_lock(sch);
// 設(shè)置定額, 該參數(shù)可選
q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
// 擾動(dòng)周期, 該參數(shù)必須
q->perturb_period = ctl->perturb_period*HZ;
// 數(shù)據(jù)包數(shù)量限制, 不超過SFQ_DEPTH
if (ctl->limit)
  q->limit = min_t(u32, ctl->limit, SFQ_DEPTH);
// 如果當(dāng)前隊(duì)列中的數(shù)據(jù)包數(shù)超過限制值, 丟包
while (sch->q.qlen >= q->limit-1)
  sfq_drop(sch);
// 更新定時(shí)器
del_timer(&q->perturb_timer);
if (q->perturb_period) {
  q->perturb_timer.expires = jiffies + q->perturb_period;
  add_timer(&q->perturb_timer);
}
sch_tree_unlock(sch);
return 0;
}

5.5.4 入隊(duì)
static int
sfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
// 計(jì)算數(shù)據(jù)包的哈希值
unsigned hash = sfq_hash(q, skb);
sfq_index x;
// 該哈希值對(duì)應(yīng)的隊(duì)列號(hào)
x = q->ht[hash];
// SFQ_DEPTH表示該隊(duì)列還為空
if (x == SFQ_DEPTH) {
  q->ht[hash] = x = q->dep[SFQ_DEPTH].next;
  q->hash[x] = hash;
}
// 增加backlog
sch->qstats.backlog += skb->len;
// 將數(shù)據(jù)包添加到隊(duì)列鏈表
__skb_queue_tail(&q->qs[x], skb);
// x節(jié)點(diǎn)增加操作
sfq_inc(q, x);
// 如果隊(duì)列長度為1, 是新隊(duì)列
if (q->qs[x].qlen == 1) {  /* The flow is new */
  if (q->tail == SFQ_DEPTH) { /* It is the first flow */
// 這是第一個(gè)隊(duì)列的第一個(gè)包
   q->tail = x;
   q->next[x] = x;
   q->allot[x] = q->quantum;
  } else {
// q->tail為準(zhǔn)備出隊(duì)的那個(gè)隊(duì)列索引
   q->next[x] = q->next[q->tail];
   q->next[q->tail] = x;
   q->tail = x;
  }
}
// 檢查當(dāng)前排隊(duì)數(shù)據(jù)包數(shù)是否超過限制值
if (++sch->q.qlen limit-1) {
  sch->bstats.bytes += skb->len;
  sch->bstats.packets++;
  return 0;
}
// 超限制情況,丟包
sfq_drop(sch);
return NET_XMIT_CN;
}
5.5.5 重入隊(duì)
// 和入隊(duì)操作幾乎一樣, 只是統(tǒng)計(jì)值處理有點(diǎn)變化而已
static int
sfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
{
struct sfq_sched_data *q = qdisc_priv(sch);
unsigned hash = sfq_hash(q, skb);
sfq_index x;
x = q->ht[hash];
if (x == SFQ_DEPTH) {
  q->ht[hash] = x = q->dep[SFQ_DEPTH].next;
  q->hash[x] = hash;
}
sch->qstats.backlog += skb->len;
__skb_queue_head(&q->qs[x], skb);
sfq_inc(q, x);
if (q->qs[x].qlen == 1) {  /* The flow is new */
  if (q->tail == SFQ_DEPTH) { /* It is the first flow */
   q->tail = x;
   q->next[x] = x;
   q->allot[x] = q->quantum;
  } else {
   q->next[x] = q->next[q->tail];
   q->next[q->tail] = x;
   q->tail = x;
  }
}
if (++sch->q.qlen limit - 1) {
  sch->qstats.requeues++;
  return 0;
}
sch->qstats.drops++;
sfq_drop(sch);
return NET_XMIT_CN;
}

5.5.6 出隊(duì)
static struct sk_buff *
sfq_dequeue(struct Qdisc* sch)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
struct sk_buff *skb;
sfq_index a, old_a;
/* No active slots */
// 隊(duì)列空
if (q->tail == SFQ_DEPTH)
  return NULL;
// q->tail為要出隊(duì)的隊(duì)列索引號(hào)
a = old_a = q->next[q->tail];
/* Grab packet */
// 數(shù)據(jù)包出隊(duì)列
skb = __skb_dequeue(&q->qs[a]);
// 減少該節(jié)點(diǎn)鏈接
sfq_dec(q, a);
// 隊(duì)列長度減
sch->q.qlen--;
sch->qstats.backlog -= skb->len;
/* Is the slot empty? */
if (q->qs[a].qlen == 0) {
// 隊(duì)列已經(jīng)空了, 該隊(duì)列號(hào)對(duì)應(yīng)哈希值復(fù)位
  q->ht[q->hash[a]] = SFQ_DEPTH;
  a = q->next[a];
  if (a == old_a) {
   q->tail = SFQ_DEPTH;
   return skb;
  }
  q->next[q->tail] = a;
  q->allot[a] += q->quantum;
} else if ((q->allot[a] -= skb->len) tail為該隊(duì)列索引
  q->tail = a;
// 下一個(gè)活動(dòng)槽位
  a = q->next[a];
// 增加額度值
  q->allot[a] += q->quantum;
}
return skb;
}
5.5.7 復(fù)位
static void
sfq_reset(struct Qdisc* sch)
{
struct sk_buff *skb;
// 從SFQ隊(duì)列中進(jìn)行出隊(duì)操作, 釋放數(shù)據(jù)包, 直到隊(duì)列空
while ((skb = sfq_dequeue(sch)) != NULL)
  kfree_skb(skb);
}

5.5.8 釋放
static void sfq_destroy(struct Qdisc *sch)
{
struct sfq_sched_data *q = qdisc_priv(sch);
// 只是進(jìn)行刪除定時(shí)器操作
del_timer(&q->perturb_timer);
}
5.5.9 丟包
static unsigned int sfq_drop(struct Qdisc *sch)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
// 最大隊(duì)列深度
sfq_index d = q->max_depth;
struct sk_buff *skb;
unsigned int len;
/* Queue is full! Find the longest slot and
    drop a packet from it */
if (d > 1) {
// 對(duì)應(yīng)的x號(hào)隊(duì)列
  sfq_index x = q->dep[d+SFQ_DEPTH].next;
  skb = q->qs[x].prev;
  len = skb->len;
// skb數(shù)據(jù)包從隊(duì)列斷開, 釋放數(shù)據(jù)包
  __skb_unlink(skb, &q->qs[x]);
  kfree_skb(skb);
// 減少X節(jié)點(diǎn)使用
  sfq_dec(q, x);
// 統(tǒng)計(jì)數(shù)更新
  sch->q.qlen--;
  sch->qstats.drops++;
  sch->qstats.backlog -= len;
  return len;
}
if (d == 1) {
// 每個(gè)隊(duì)列長度都是1的情況, 都只有一個(gè)數(shù)據(jù)包
  /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
// 對(duì)應(yīng)的隊(duì)列索引
  d = q->next[q->tail];
// 更新下一槽位
  q->next[q->tail] = q->next[d];
  q->allot[q->next[d]] += q->quantum;
// 從隊(duì)列取數(shù)據(jù)包釋放
  skb = q->qs[d].prev;
  len = skb->len;
  __skb_unlink(skb, &q->qs[d]);
  kfree_skb(skb);
  sfq_dec(q, d);
// 統(tǒng)計(jì)值更新
  sch->q.qlen--;
  q->ht[q->hash[d]] = SFQ_DEPTH;
  sch->qstats.drops++;
  sch->qstats.backlog -= len;
  return len;
}
return 0;
}

5.5.10 輸出參數(shù)
static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb)
{
// SFQ私有數(shù)據(jù)
struct sfq_sched_data *q = qdisc_priv(sch);
unsigned char  *b = skb->tail;
// 向TC輸出的SFQ選項(xiàng)結(jié)構(gòu)
struct tc_sfq_qopt opt;
// 填寫SFQ選項(xiàng)參數(shù)
// 定額
opt.quantum = q->quantum;
// 擾動(dòng)周期
opt.perturb_period = q->perturb_period/HZ;
// 隊(duì)列包數(shù)限制
opt.limit = q->limit;
opt.divisor = SFQ_HASH_DIVISOR;
opt.flows = q->limit;
// 打包到skb數(shù)據(jù)區(qū)
RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
return skb->len;
rtattr_failure:
skb_trim(skb, b - skb->data);
return -1;
}

...... 待續(xù) ......
               
               
               

本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u/33048/showart_2094267.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP