- 論壇徽章:
- 0
|
Linux內(nèi)核中流量控制(6)
本文檔的Copyleft歸yfydz所有,使用GPL發(fā)布,可以自由拷貝,轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)保持文檔的完整性,嚴(yán)禁用于任何商業(yè)用途。
msn:
yfydz_no1@hotmail.com
來(lái)源:
http://yfydz.cublog.cn
5.6 DSMARK(Differentiated Services field marker)
DSMARK算法實(shí)際不能算特別流控算法, 本身只是一個(gè)pfifo流控, 所做的多余功能就是能設(shè)置數(shù)據(jù)包的tos/ds域,
其實(shí)意義不大, 該功能在網(wǎng)絡(luò)層就能作(netfilter/TOS目標(biāo))。使用時(shí)除了用tc qdisc命令設(shè)置外, 還要用tc
class/filter命令對(duì)數(shù)據(jù)進(jìn)行分類, 以設(shè)置合適的tos/ds值。
5.6.1 DSMARK操作結(jié)構(gòu)定義
#define NO_DEFAULT_INDEX (1
// DSMARK私有數(shù)據(jù)
struct dsmark_qdisc_data {
// 內(nèi)部流控節(jié)點(diǎn)
struct Qdisc *q;
// 分類規(guī)則表, 通過(guò)tc class命令定義
struct tcf_proto *filter_list;
// value/mask數(shù)組, 用于設(shè)置數(shù)據(jù)包IP頭中的tos/ds域
u8 *mask; /* "owns" the array */
u8 *value;
// 索引
u16 indices;
// 缺省索引
u32 default_index; /* index range is 0...0xffff */
// 是否設(shè)置TC索引值標(biāo)志
int set_tc_index;
};
// DSMARK流控操作結(jié)構(gòu)
static struct Qdisc_ops dsmark_qdisc_ops = {
.next = NULL,
.cl_ops = &dsmark_class_ops,
.id = "dsmark",
.priv_size = sizeof(struct dsmark_qdisc_data),
.enqueue = dsmark_enqueue,
.dequeue = dsmark_dequeue,
.requeue = dsmark_requeue,
.drop = dsmark_drop,
.init = dsmark_init,
.reset = dsmark_reset,
.destroy = dsmark_destroy,
// 沒(méi)有change函數(shù)
.change = NULL,
.dump = dsmark_dump,
.owner = THIS_MODULE,
};
// DSMARK類別操作結(jié)構(gòu)
static struct Qdisc_class_ops dsmark_class_ops = {
.graft = dsmark_graft,
.leaf = dsmark_leaf,
.get = dsmark_get,
.put = dsmark_put,
.change = dsmark_change,
.delete = dsmark_delete,
.walk = dsmark_walk,
.tcf_chain = dsmark_find_tcf,
.bind_tcf = dsmark_bind_filter,
.unbind_tcf = dsmark_put,
.dump = dsmark_dump_class,
};
5.6.2 初始化
static int dsmark_init(struct Qdisc *sch, struct rtattr *opt)
{
// DSMARK私有數(shù)據(jù)
struct dsmark_qdisc_data *p = PRIV(sch);
// 保存相關(guān)屬性
struct rtattr *tb[TCA_DSMARK_MAX];
int err = -EINVAL;
u32 default_index = NO_DEFAULT_INDEX;
u16 indices;
u8 *mask;
DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
// 選項(xiàng)參數(shù)解析, 至少要提供一個(gè)indices參數(shù)
if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt)
// 獲取indices值并檢查是否合法, indices必須是2的整次冪
indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]);
if (!indices || !dsmark_valid_indices(indices))
goto errout;
// 如果有缺省索引參數(shù), 設(shè)置之
if (tb[TCA_DSMARK_DEFAULT_INDEX-1])
default_index = RTA_GET_U16(tb[TCA_DSMARK_DEFAULT_INDEX-1]);
// 分配value和mask空間, 都是indices大小
mask = kmalloc(indices * 2, GFP_KERNEL);
if (mask == NULL) {
err = -ENOMEM;
goto errout;
}
// 前一半空間都初始化0xff作為mask
p->mask = mask;
memset(p->mask, 0xff, indices);
// 后一半空間都初始化0作為value
p->value = p->mask + indices;
memset(p->value, 0, indices);
// 設(shè)置DSMARK參數(shù)
p->indices = indices;
p->default_index = default_index;
p->set_tc_index = RTA_GET_FLAG(tb[TCA_DSMARK_SET_TC_INDEX-1]);
// 內(nèi)部流控節(jié)點(diǎn)是一個(gè)pfifo類型的qdisc
p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
if (p->q == NULL)
p->q = &noop_qdisc;
DPRINTK("dsmark_init: qdisc %p\n", p->q);
err = 0;
errout:
rtattr_failure:
return err;
}
5.6.3 入隊(duì)
// 入隊(duì)操作主要是生成數(shù)據(jù)包的tc_index值
static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch)
{
struct dsmark_qdisc_data *p = PRIV(sch);
int err;
D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
// 如果有set_tc_index標(biāo)志
if (p->set_tc_index) {
/* FIXME: Safe with non-linear skbs? --RR */
// 提取IPv4頭的TOS字段或IPv4的ds字段作為數(shù)據(jù)包的tc_index
switch (skb->protocol) {
case __constant_htons(ETH_P_IP):
skb->tc_index = ipv4_get_dsfield(skb->nh.iph)
& ~INET_ECN_MASK;
break;
case __constant_htons(ETH_P_IPV6):
skb->tc_index = ipv6_get_dsfield(skb->nh.ipv6h)
& ~INET_ECN_MASK;
break;
default:
skb->tc_index = 0;
break;
};
}
// 如果該數(shù)據(jù)包是由此qdisc處理
if (TC_H_MAJ(skb->priority) == sch->handle)
// 數(shù)據(jù)包的tc_index設(shè)置為skb->priority的低16位
skb->tc_index = TC_H_MIN(skb->priority);
else {
// 否則調(diào)用分類規(guī)則對(duì)數(shù)據(jù)包進(jìn)行分類
struct tcf_result res;
int result = tc_classify(skb, p->filter_list, &res);
D2PRINTK("result %d class 0x%04x\n", result, res.classid);
// 分類結(jié)果
switch (result) {
#ifdef CONFIG_NET_CLS_POLICE
// 丟包
case TC_POLICE_SHOT:
kfree_skb(skb);
sch->qstats.drops++;
return NET_XMIT_POLICED;
#if 0
case TC_POLICE_RECLASSIFY:
/* FIXME: what to do here ??? */
#endif
#endif
// 獲取了新的分類值
case TC_POLICE_OK:
skb->tc_index = TC_H_MIN(res.classid);
break;
// 使用缺省分類值, 如果沒(méi)有設(shè)置set_tc_index標(biāo)志, 也沒(méi)有定義缺省索引值
// 則tc_index值不改變
case TC_POLICE_UNSPEC:
/* fall through */
default:
if (p->default_index != NO_DEFAULT_INDEX)
skb->tc_index = p->default_index;
break;
};
}
// 進(jìn)行pfifo的入隊(duì)操作
err = p->q->enqueue(skb,p->q);
if (err != NET_XMIT_SUCCESS) {
sch->qstats.drops++;
return err;
}
sch->bstats.bytes += skb->len;
sch->bstats.packets++;
sch->q.qlen++;
return NET_XMIT_SUCCESS;
}
5.6.4 重入隊(duì)
static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch)
{
struct dsmark_qdisc_data *p = PRIV(sch);
int err;
D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
// 就是調(diào)用pfifo的重入隊(duì)操作
err = p->q->ops->requeue(skb, p->q);
if (err != NET_XMIT_SUCCESS) {
sch->qstats.drops++;
return err;
}
sch->q.qlen++;
sch->qstats.requeues++;
return NET_XMIT_SUCCESS;
}
5.6.5 出隊(duì)
static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
{
// DSMARK私有數(shù)據(jù)
struct dsmark_qdisc_data *p = PRIV(sch);
struct sk_buff *skb;
u32 index;
D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])\n", sch, p);
// 調(diào)用pfifo的出隊(duì)操作
skb = p->q->ops->dequeue(p->q);
if (skb == NULL)
return NULL;
// 隊(duì)列長(zhǎng)度減
sch->q.qlen--;
// 獲取數(shù)據(jù)包索引值, 不超過(guò)(indices-1)
index = skb->tc_index & (p->indices - 1);
D2PRINTK("index %d->%d\n", skb->tc_index, index);
// 以下修改TPv4數(shù)據(jù)包的TOS值或IPv6的ds值
// 具體修改的值是由索引值對(duì)應(yīng)的value/mask數(shù)組中的值確定的
switch (skb->protocol) {
case __constant_htons(ETH_P_IP):
ipv4_change_dsfield(skb->nh.iph, p->mask[index],
p->value[index]);
break;
case __constant_htons(ETH_P_IPV6):
ipv6_change_dsfield(skb->nh.ipv6h, p->mask[index],
p->value[index]);
break;
default:
/*
* Only complain if a change was actually attempted.
* This way, we can send non-IP traffic through dsmark
* and don't need yet another qdisc as a bypass.
*/
if (p->mask[index] != 0xff || p->value[index])
printk(KERN_WARNING "dsmark_dequeue: "
"unsupported protocol %d\n",
htons(skb->protocol));
break;
};
// 數(shù)據(jù)包返回
return skb;
}
5.6.6 復(fù)位
static void dsmark_reset(struct Qdisc *sch)
{
struct dsmark_qdisc_data *p = PRIV(sch);
DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
// 就是調(diào)用pfifo的復(fù)位函數(shù)
qdisc_reset(p->q);
sch->q.qlen = 0;
}
5.6.7 釋放
static void dsmark_destroy(struct Qdisc *sch)
{
struct dsmark_qdisc_data *p = PRIV(sch);
struct tcf_proto *tp;
DPRINTK("dsmark_destroy(sch %p,[qdisc %p])\n", sch, p);
// 釋放分類規(guī)則
while (p->filter_list) {
tp = p->filter_list;
p->filter_list = tp->next;
tcf_destroy(tp);
}
// 釋放pfifo
qdisc_destroy(p->q);
// 釋放mask和value, 因?yàn)閙ask就是動(dòng)態(tài)空間頭, 所以mask和value一起釋放了
kfree(p->mask);
}
5.6.8 丟包
static unsigned int dsmark_drop(struct Qdisc *sch)
{
struct dsmark_qdisc_data *p = PRIV(sch);
unsigned int len;
DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
if (p->q->ops->drop == NULL)
return 0;
// 就是調(diào)用pfifo的drop函數(shù)
len = p->q->ops->drop(p->q);
if (len)
sch->q.qlen--;
return len;
}
5.6.9 輸出參數(shù)
static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
{
// DSMARK私有數(shù)據(jù)
struct dsmark_qdisc_data *p = PRIV(sch);
struct rtattr *opts = NULL;
// 輸出indices
opts = RTA_NEST(skb, TCA_OPTIONS);
RTA_PUT_U16(skb, TCA_DSMARK_INDICES, p->indices);
// 輸出缺省索引值
if (p->default_index != NO_DEFAULT_INDEX)
RTA_PUT_U16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index);
// 輸出set_tc_index標(biāo)志
if (p->set_tc_index)
RTA_PUT_FLAG(skb, TCA_DSMARK_SET_TC_INDEX);
return RTA_NEST_END(skb, opts);
rtattr_failure:
return RTA_NEST_CANCEL(skb, opts);
}
5.6.10 DSMARK類別操作
static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
struct Qdisc *new, struct Qdisc **old)
{
struct dsmark_qdisc_data *p = PRIV(sch);
DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",
sch, p, new, old);
if (new == NULL) {
new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
if (new == NULL)
new = &noop_qdisc;
}
sch_tree_lock(sch);
*old = xchg(&p->q, new);
qdisc_reset(*old);
sch->q.qlen = 0;
sch_tree_unlock(sch);
return 0;
}
// 返回葉子節(jié)點(diǎn)的qdisc
static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
{
return PRIV(sch)->q;
}
// 獲取某類別ID
static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
{
DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)\n",
sch, PRIV(sch), classid);
// 就是classid的低16位加1
return TC_H_MIN(classid) + 1;
}
// 綁定過(guò)濾器
static unsigned long dsmark_bind_filter(struct Qdisc *sch,
unsigned long parent, u32 classid)
{
return dsmark_get(sch, classid);
}
static void dsmark_put(struct Qdisc *sch, unsigned long cl)
{
}
// 修改dsmark參數(shù): value/mask
static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
struct rtattr **tca, unsigned long *arg)
{
// DSMARK私有數(shù)據(jù)
struct dsmark_qdisc_data *p = PRIV(sch);
// 輸入?yún)?shù)
struct rtattr *opt = tca[TCA_OPTIONS-1];
// 輸出參數(shù)數(shù)組
struct rtattr *tb[TCA_DSMARK_MAX];
int err = -EINVAL;
u8 mask = 0;
DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
"arg 0x%lx\n", sch, p, classid, parent, *arg);
// 檢查索引值是否合法
if (!dsmark_valid_index(p, *arg)) {
err = -ENOENT;
goto rtattr_failure;
}
// 解析opt輸出到tb
if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt))
goto rtattr_failure;
// 解析mask
if (tb[TCA_DSMARK_MASK-1])
mask = RTA_GET_U8(tb[TCA_DSMARK_MASK-1]);
// 解析value并保存到指定索引的數(shù)組項(xiàng)
if (tb[TCA_DSMARK_VALUE-1])
p->value[*arg-1] = RTA_GET_U8(tb[TCA_DSMARK_VALUE-1]);
// 保存mask到指定索引的數(shù)組項(xiàng), mask為什么要解析和設(shè)置分開(kāi)呢?
if (tb[TCA_DSMARK_MASK-1])
p->mask[*arg-1] = mask;
err = 0;
rtattr_failure:
return err;
}
// 清除索引值對(duì)應(yīng)數(shù)組項(xiàng)
static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
{
struct dsmark_qdisc_data *p = PRIV(sch);
// 檢查索引值是否合法
if (!dsmark_valid_index(p, arg))
return -EINVAL;
// 將value和mask恢復(fù)缺省值, 0/0xff
p->mask[arg-1] = 0xff;
p->value[arg-1] = 0;
return 0;
}
// 遍歷
static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker)
{
struct dsmark_qdisc_data *p = PRIV(sch);
int i;
DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
if (walker->stop)
return;
// 遍歷數(shù)組
for (i = 0; i indices; i++) {
// mask=0xff和value=0的索引項(xiàng)沒(méi)用
if (p->mask == 0xff && !p->value)
goto ignore;
if (walker->count >= walker->skip) {
// 調(diào)用相關(guān)處理函數(shù)進(jìn)行處理
if (walker->fn(sch, i+1, walker) stop = 1;
break;
}
}
ignore:
walker->count++;
}
}
// 返回過(guò)濾規(guī)則
static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl)
{
return &PRIV(sch)->filter_list;
}
// 類別輸出, 輸出某索引值對(duì)應(yīng)的value和mask
static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
struct sk_buff *skb, struct tcmsg *tcm)
{
// 私有數(shù)據(jù)
struct dsmark_qdisc_data *p = PRIV(sch);
struct rtattr *opts = NULL;
DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n", sch, p, cl);
// 檢查索引值是否合法, 必須大于0, 不超過(guò)indices
if (!dsmark_valid_index(p, cl))
return -EINVAL;
// qdisc基本參數(shù)
tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl-1);
tcm->tcm_info = p->q->handle;
// 該索引位置對(duì)應(yīng)的mask和value
opts = RTA_NEST(skb, TCA_OPTIONS);
RTA_PUT_U8(skb,TCA_DSMARK_MASK, p->mask[cl-1]);
RTA_PUT_U8(skb,TCA_DSMARK_VALUE, p->value[cl-1]);
return RTA_NEST_END(skb, opts);
rtattr_failure:
return RTA_NEST_CANCEL(skb, opts);
}
...... 待續(xù) ......
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/33048/showart_2094268.html |
|