Linux内核中流量控制(5)
本文档的Copyleft归yfydz所有,使用GPL发布,可以自由拷贝,转载,转载时请保持文档的完整性,
严禁用于任何商业用途。
msn: yfydz_no1@hotmail.com
来源:http://yfydz.cublog.cn
5.5 SFQ(Stochastic Fairness Queueing discipline)SFQ算法是个比较简单的算法,速度也比较快,算法维护一定数量的数据包队列,入队是将数据包进行哈希后插入某队列,出队则是轮询方式出队列,另外可设置一定的随机因子,在计算哈希值时能碰撞少些,流控算法在net/sched/sch_sfq.c中定义,在实现中, 队列数量最大为128个,这是保证SFQ私有数据结构能小于4K,能在一个页面内分配。在使用用不建议作为网卡的根节点流控,而是最好作为分类流控方法如CBQ等的叶子节点。5.5.1 SFQ操作结构定义// TC使用的SFQ配置参数结构struct tc_sfq_qopt{// 定额 unsigned quantum; /* Bytes per round allocated to flow */// 扰动周期 int perturb_period; /* Period of hash perturbation */// 队列中的数据包数量限制 __u32 limit; /* Maximal packets in queue */ unsigned divisor; /* Hash divisor */// 最大队列数 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位数typedef unsigned char sfq_index;struct sfq_head{ sfq_index next; sfq_index prev;};// SFQ私有数据struct sfq_sched_data{/* Parameters */// 扰动间隔, 隔一定时间修改HASH扰动 值 int perturb_period;// unsigned quantum; /* Allotment per round: MUST BE >= MTU */// 流量限制值 int limit;/* Variables */// 扰动更新定时器 struct timer_list perturb_timer;// HASH扰动值 int perturbation;// 出队队列索引 sfq_index tail; /* Index of current slot in round */// 最大深度 sfq_index max_depth; /* Maximal depth */// HASH值对应的槽位索引表, 1024项, HASH值范围为0~1023 sfq_index ht[SFQ_HASH_DIVISOR]; /* Hash table */// 活动槽 sfq_index next[SFQ_DEPTH]; /* Active slots link */ short allot[SFQ_DEPTH]; /* Current allotment per slot */// 哈希值索引数组 unsigned short hash[SFQ_DEPTH]; /* Hash value indexed by slots */// 数据包队列, 128个队列 struct sk_buff_head qs[SFQ_DEPTH]; /* Slot queue */// 深度值, 256个成员 struct sfq_head dep[SFQ_DEPTH*2]; /* Linked list of slots, indexed bydepth */};SFQ数据结构比较怪异, 数据存储是数组, 但逻辑上又是双向链表, 访问时又使用数组索引。// SFQ流控操作结构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函数 .change = NULL, .dump = sfq_dump, .owner = THIS_MODULE,};5.5.2 SFQ一些基本操作// HASH函数static __inline__ unsigned sfq_fold_hash(struct sfq_sched_data *q, u32 h, u32 h1){// 哈希扰动值 int pert = q->perturbation; /* Have we any rotation primitives? If not, WHY? */// 计算哈希值, 最大值0x3ff=1023 h ^= (h1<<pert) ^ (h1>>(0x1F - pert)); h ^= h>>10; return h & 0x3FF;}// SFQ哈希函数static unsigned sfq_hash(struct sfq_sched_data *q, struct sk_buff *skb){ u32 h, h2;// skb->protocol是链路层中的协议值 switch (skb->protocol) {// IPV4 case __constant_htons(ETH_P_IP): { struct iphdr *iph = skb->nh.iph;// 哈希函数中用到了源地址,目的地址, 协议, 端口或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字节 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:// 其他协议就用路由参数, 链路层协议和sock指针 h = (u32)(unsigned long)skb->dst^skb->protocol; h2 = (u32)(unsigned long)skb->sk; }// 计算哈希值 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个队列尾索引,qlen是不会超过SFQ_DEPTH的 int d = q->qs[x].qlen + SFQ_DEPTH; p = d; n = q->dep[d].next;// x节点插入到队列s的最后, 但形成一个双向环型链表 q->dep[x].next = n; q->dep[x].prev = p; q->dep[p].next = q->dep[n].prev = x;}// 减少, 将X号索引点断开static inline void sfq_dec(struct sfq_sched_data *q, sfq_index x){ sfq_index p, n;// 断开x号索引 n = q->dep[x].next; p = q->dep[x].prev; q->dep[p].next = n; q->dep[n].prev = p;// n==p表示链表空了// 如果当前链表是最多元素链表, 相应最大链表长度减 if (n == p && q->max_depth == q->qs[x].qlen + 1) q->max_depth--; sfq_link(q, x);}// 增加, 增加X处索引点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 < d) q->max_depth = d; sfq_link(q, x);}5.5.3 初始化static int sfq_init(struct Qdisc *sch, struct rtattr *opt){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch); int i;// 初始化定时器 init_timer(&q->perturb_timer);// 定时器函数参数为流控结构 q->perturb_timer.data = (unsigned long)sch;// 定时器函数, 定时修改扰动值 q->perturb_timer.function = sfq_perturbation;// 初始化哈希表索引, 都为SFQ_DEPTH for (i=0; i<SFQ_HASH_DIVISOR; i++) q->ht[i] = SFQ_DEPTH; for (i=0; i<SFQ_DEPTH; i++) {// 初始化数据包队列头 skb_queue_head_init(&q->qs[i]);// 初始化dep的后SFQ_DEPTH个元素 q->dep[i+SFQ_DEPTH].next = i+SFQ_DEPTH; q->dep[i+SFQ_DEPTH].prev = i+SFQ_DEPTH; }// SFQ流控数据包总数限制 q->limit = SFQ_DEPTH; q->max_depth = 0; q->tail = SFQ_DEPTH;// 配置SFQ是允许不带任何参数的 if (opt == NULL) {// 缺省定额值为网卡MTU q->quantum = psched_mtu(sch->dev);// 不进行扰动更新 q->perturb_period = 0; } else {// 根据配置的参数设置SFQ参数 int err = sfq_change(sch, opt); if (err) return err; }// 初始化索引链表, 初始化dep的前128个元素 for (i=0; i<SFQ_DEPTH; i++) sfq_link(q, i); return 0;}// SFQ哈希扰动值修改, 这是定时器的定时函数static void sfq_perturbation(unsigned long arg){// 定时函数参数为流控结构 struct Qdisc *sch = (struct Qdisc*)arg;// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch);//生成随机扰动值 q->perturbation = net_random()&0x1F;// 扰动时间非0, 更新定时器, 现在是在时钟中断中, 定时器已经从定时链表中拆除了,// 所以要重新添加定时器 if (q->perturb_period) { q->perturb_timer.expires = jiffies + q->perturb_period; add_timer(&q->perturb_timer); }}// 设置SFQ参数, 只在初始化时调用, 以后将不再修改static int sfq_change(struct Qdisc *sch, struct rtattr *opt){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch);// SFQ配置参数 struct tc_sfq_qopt *ctl = RTA_DATA(opt);// 配置参数合法性检查 if (opt->rta_len < RTA_LENGTH(sizeof(*ctl))) return -EINVAL; sch_tree_lock(sch);// 设置定额, 该参数可选 q->quantum = ctl->quantum ? : psched_mtu(sch->dev);// 扰动周期, 该参数必须 q->perturb_period = ctl->perturb_period*HZ;// 数据包数量限制, 不超过SFQ_DEPTH if (ctl->limit) q->limit = min_t(u32, ctl->limit, SFQ_DEPTH);// 如果当前队列中的数据包数超过限制值, 丢包 while (sch->q.qlen >= q->limit-1) sfq_drop(sch);// 更新定时器 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 入队static intsfq_enqueue(struct sk_buff *skb, struct Qdisc* sch){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch);// 计算数据包的哈希值 unsigned hash = sfq_hash(q, skb); sfq_index x;// 该哈希值对应的队列号 x = q->ht[hash];// SFQ_DEPTH表示该队列还为空 if (x == SFQ_DEPTH) { q->ht[hash] = x = q->dep[SFQ_DEPTH].next; q->hash[x] = hash; }// 增加backlog sch->qstats.backlog += skb->len;// 将数据包添加到队列链表 __skb_queue_tail(&q->qs[x], skb);// x节点增加操作 sfq_inc(q, x);// 如果队列长度为1, 是新队列 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->tail为准备出队的那个队列索引 q->next[x] = q->next[q->tail]; q->next[q->tail] = x; q->tail = x; } }// 检查当前排队数据包数是否超过限制值 if (++sch->q.qlen < q->limit-1) { sch->bstats.bytes += skb->len; sch->bstats.packets++; return 0; }// 超限制情况,丢包 sfq_drop(sch); return NET_XMIT_CN;}5.5.5 重入队// 和入队操作几乎一样, 只是统计值处理有点变化而已static intsfq_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 < q->limit - 1) { sch->qstats.requeues++; return 0; } sch->qstats.drops++; sfq_drop(sch); return NET_XMIT_CN;} 5.5.6 出队static struct sk_buff *sfq_dequeue(struct Qdisc* sch){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch); struct sk_buff *skb; sfq_index a, old_a; /* No active slots */// 队列空 if (q->tail == SFQ_DEPTH) return NULL;// q->tail为要出队的队列索引号 a = old_a = q->next[q->tail]; /* Grab packet */// 数据包出队列 skb = __skb_dequeue(&q->qs[a]);// 减少该节点链接 sfq_dec(q, a);// 队列长度减 sch->q.qlen--; sch->qstats.backlog -= skb->len; /* Is the slot empty? */ if (q->qs[a].qlen == 0) {// 队列已经空了, 该队列号对应哈希值复位 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) <= 0) {// 如果该队列额度不够, 更新q->tail为该队列索引 q->tail = a;// 下一个活动槽位 a = q->next[a];// 增加额度值 q->allot[a] += q->quantum; } return skb;}5.5.7 复位static voidsfq_reset(struct Qdisc* sch){ struct sk_buff *skb;// 从SFQ队列中进行出队操作, 释放数据包, 直到队列空 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);// 只是进行删除定时器操作 del_timer(&q->perturb_timer);}5.5.9 丢包static unsigned int sfq_drop(struct Qdisc *sch){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch);// 最大队列深度 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) {// 对应的x号队列 sfq_index x = q->dep[d+SFQ_DEPTH].next; skb = q->qs[x].prev; len = skb->len;// skb数据包从队列断开, 释放数据包 __skb_unlink(skb, &q->qs[x]); kfree_skb(skb);// 减少X节点使用 sfq_dec(q, x);// 统计数更新 sch->q.qlen--; sch->qstats.drops++; sch->qstats.backlog -= len; return len; } if (d == 1) {// 每个队列长度都是1的情况, 都只有一个数据包 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */// 对应的队列索引 d = q->next[q->tail];// 更新下一槽位 q->next[q->tail] = q->next[d]; q->allot[q->next[d]] += q->quantum;// 从队列取数据包释放 skb = q->qs[d].prev; len = skb->len; __skb_unlink(skb, &q->qs[d]); kfree_skb(skb); sfq_dec(q, d);// 统计值更新 sch->q.qlen--; q->ht[q->hash[d]] = SFQ_DEPTH; sch->qstats.drops++; sch->qstats.backlog -= len; return len; } return 0;} 5.5.10 输出参数static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb){// SFQ私有数据 struct sfq_sched_data *q = qdisc_priv(sch); unsigned char *b = skb->tail;// 向TC输出的SFQ选项结构 struct tc_sfq_qopt opt;// 填写SFQ选项参数// 定额 opt.quantum = q->quantum;// 扰动周期 opt.perturb_period = q->perturb_period/HZ;// 队列包数限制 opt.limit = q->limit; opt.divisor = SFQ_HASH_DIVISOR; opt.flows = q->limit;// 打包到skb数据区 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); return skb->len;rtattr_failure: skb_trim(skb, b - skb->data); return -1;} ...... 待续 ......