linux htb 源代码,LINUX TC:HTB相关源码
LINUX TC:HTB相關源碼 收藏
HTB(hierarchy token buffer)是linux tc(traffic control)模塊中的排隊隊列的一種。它的配置比CBQ要簡單.同時實現功能也很強大。下面,就來看看,HTB在linux中的源碼。
1、???????????? Qdisc_ops的注冊
先從module_init函數看起(遵從fudan_abc的建議)
static int __init htb_module_init(void)
{
return register_qdisc(&htb_qdisc_ops);
}
上面的code會調用register_qdisc函數,將htb_qdisc_ops注冊到系統中,那么htb_qdisc_ops包含了那些內容:
static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
.cl_ops??????? =???? &htb_class_ops,
.id????????? =???? "htb",
.priv_size?? =???? sizeof(struct htb_sched),
.enqueue???? =???? htb_enqueue,
.dequeue???? =???? htb_dequeue,
.peek??????????? =???? qdisc_peek_dequeued,
.drop??????????? =???? htb_drop,
.init????????????? =???? htb_init,
.reset?????????? =???? htb_reset,
.destroy????? =???? htb_destroy,
.dump????????? =???? htb_dump,
.owner???????? =???? THIS_MODULE,
};
可以看出,htb_qdisc_ops其實就是注冊了htb管理queue的函數,最重要的莫過于:enqueue 和dequeue函數,它們作用如同它們的名字一樣。那么到底將htb_qdisc_ops注冊到那了呢?
這就要看看register函數了
static struct Qdisc_ops *qdisc_base;
/*
*qdisc_base 就是系統維護所以qdisc所使用的變量,系統中的所有的qdisc都要
*注冊到這變量變量中
*在struct Qdisc_ops中,包含了成員(struct Qdisc_ops *)next
*也就是,所以的qdisc是以鏈表的形式存在的
*/
int register_qdisc(struct Qdisc_ops *qops)
{
struct Qdisc_ops *q, **qp;
int rc = -EEXIST;
write_lock(&qdisc_mod_lock);
/*
*首先,檢測這個qdisc是否已經注冊過了,這是通過比較id實現的,
*id的類型是char 數組:char???????????????? id[IFNAMSIZ];IFNAMESIZ=16
*htb的id=”htb”
*/
for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
if (!strcmp(qops->id, q->id))
goto out;
/*
*然后檢測ops中的enqueue、dequeue、peek函數,
*如果這些函數都沒有被初始化,將使用noop_qdisc_ops函數來初始化
*noop_qdisc_ops也是Qdisc_ops結構,
*它的作用就像是定義了Qdisc_ops的默認值
*/
if (qops->enqueue == NULL)
qops->enqueue = noop_qdisc_ops.enqueue;
if (qops->peek == NULL) {
if (qops->dequeue == NULL)
qops->peek = noop_qdisc_ops.peek;
else
goto out_einval;
}
if (qops->dequeue == NULL)
qops->dequeue = noop_qdisc_ops.dequeue;
/*
*然后檢測cl_ops成員。
*cl_ops是結構Qdisc_class_ops,
*它定義了用于管理掛載到這個qdisc下的所有class(或者qdisc)
*/
if (qops->cl_ops) {
const struct Qdisc_class_ops *cops = qops->cl_ops;
if (!(cops->get && cops->put && cops->walk && cops->leaf))
goto out_einval;
if (cops->tcf_chain && !(cops->bind_tcf && cops->unbind_tcf))
goto out_einval;
}
/*
*最后將新的qops插入到鏈表的尾部:*qp = qops;
*這樣就注冊完成了
*/
qops->next = NULL;
*qp = qops;
rc = 0;
out:
write_unlock(&qdisc_mod_lock);
return rc;
out_einval:
rc = -EINVAL;
goto out;
}
EXPORT_SYMBOL(register_qdisc);
Qdisc_class_ops是管理這個tree的,那么看看htb的cl_ops有哪些函數:
static const struct Qdisc_class_ops htb_class_ops = {
.graft?????????? =???? htb_graft,
.leaf???????????? =???? htb_leaf,
.qlen_notify????? =???? htb_qlen_notify,
.get????????????? =???? htb_get,
.put????????????? =???? htb_put,
.change????????????? =???? htb_change_class,
.delete???????? =???? htb_delete,
.walk?????????? =???? htb_walk,
.tcf_chain?? =???? htb_find_tcf,
.bind_tcf???? =???? htb_bind_filter,
.unbind_tcf =???? htb_unbind_filter,
.dump????????? =???? htb_dump_class,
.dump_stats????? =???? htb_dump_class_stats,
};
我們知道,tc qdisc命令添加qdisc到某個設備后,為了對數據包進行分類,需要使用tc filter 來添加fitler到某個qdisc, 當數據包來時,通過fitler來區分數據包,并轉發到不同的subqdisc 或者subclass。而綁定fitler都是通過函數:bind_tcf來實現的。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的linux htb 源代码,LINUX TC:HTB相关源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 差点令金融世界崩塌的数学公式
- 下一篇: New directions in au