日期:2014-05-16  浏览次数:20833 次

Linux内核中流量控制(9)
本文档的Copyleft归yfydz所有,使用GPL发布,可以自由拷贝,转载,转载时请保持文档的完整性,严禁用于任何商业用途。
msn: yfydz_no1@hotmail.com
来源:http://yfydz.cublog.cn

5.9 ingress

ingress流控方法是针对输入数据进行流控处理的,在net/sched/sch_ingress.c中定义, 使用时要求在内核配置中定义CONFIG_NET_CLS_ACT或CONFIG_NETFILTER,不过从代码实现看起来好象没什么意义。

5.9.1 ingress操作结构定义

// ingress私有数据结构
struct ingress_qdisc_data {
// 内部流控
 struct Qdisc  *q;
// 过滤规则
 struct tcf_proto *filter_list;
};

// ingress流控操作结构
static struct Qdisc_ops ingress_qdisc_ops = {
 .next  = NULL,
 .cl_ops  = &ingress_class_ops,
 .id  = "ingress",
 .priv_size = sizeof(struct ingress_qdisc_data),
 .enqueue = ingress_enqueue,
 .dequeue = ingress_dequeue,
 .requeue = ingress_requeue,
 .drop  = ingress_drop,
 .init  = ingress_init,
 .reset  = ingress_reset,
 .destroy = ingress_destroy,
// 无change函数
 .change  = NULL,
 .dump  = ingress_dump,
 .owner  = THIS_MODULE,
};
 
// ingress类别操作结构
static struct Qdisc_class_ops ingress_class_ops = {
 .graft  = ingress_graft,
 .leaf  = ingress_leaf,
 .get  = ingress_get,
 .put  = ingress_put,
 .change  = ingress_change,
 .delete  = NULL,
 .walk  = ingress_walk,
 .tcf_chain = ingress_find_tcf,
 .bind_tcf = ingress_bind_filter,
 .unbind_tcf = ingress_put,
 .dump  = NULL,
};

5.9.1 ingress的netfilter节点

// 没定义NET_CLS_ACT, 而定义了NETFILTER的情况下才有效, 在netfilter节点中进行
// 数据包是否接收的判断
// 但如果定义了NET_CLS_ACT后, 该段代码无效
#ifndef CONFIG_NET_CLS_ACT
#ifdef CONFIG_NETFILTER
/* after ipt_filter */
static struct nf_hook_ops ing_ops = {
// 钩子函数
 .hook           = ing_hook,
 .owner  = THIS_MODULE,
 .pf             = PF_INET,
// 挂接在PREROUTING点
 .hooknum        = NF_IP_PRE_ROUTING,
// 优先权NF_IP_PRI_FILTER + 1, 不过在PREROUTING点缺省好象没定义NF_IP_PRI_FILTER
// 级别的节点
 .priority       = NF_IP_PRI_FILTER + 1,
};
static struct nf_hook_ops ing6_ops = {
 .hook           = ing_hook,
 .owner  = THIS_MODULE,
 .pf             = PF_INET6,
 .hooknum        = NF_IP6_PRE_ROUTING,
 .priority       = NF_IP6_PRI_FILTER + 1,
};

static unsigned int
ing_hook(unsigned int hook, struct sk_buff **pskb,
                             const struct net_device *indev,
                             const struct net_device *outdev,
                      int (*okfn)(struct sk_buff *))
{
 
 struct Qdisc *q;
 struct sk_buff *skb = *pskb;
        struct net_device *dev = skb->dev;
// 缺省动作, 接受
 int fwres=NF_ACCEPT;
 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
  skb->sk ? "(owned)" : "(unowned)",
  skb->dev ? (*pskb)->dev->name : "(no dev)",
  skb->len);
/*
revisit later: Use a private since lock dev->queue_lock is also
used on the egress (might slow things for an iota)
*/
// 如果设备里定义了接收流控
 if (dev->qdisc_ingress) {
  spin_lock(&dev->queue_lock);
  if ((q = dev->qdisc_ingress) != NULL)
// 调用接收流控的入队函数, 返回值是对数据包的判断结果
   fwres = q->enqueue(skb, q);
  spin_unlock(&dev->queue_lock);
        }
// 如果没定义, 就直接返回缺省动作: 通过 
 return fwres;
}
#endif
#endif

5.9.3 初始化

static int ingress_init(struct Qdisc *sch,struct rtattr *opt)
{
 struct ingress_qdisc_data *p = PRIV(sch);
/* Make sure either netfilter or preferably CLS_ACT is
* compiled in */
// 输入接收流控必须在内核选项中定义分类动作NET_CLS_ACT或NETFILTER
#ifndef CONFIG_NET_CLS_ACT
#ifndef CONFIG_NETFILTER
// 两者都没定义的话就初始化失败
 printk("You MUST compile classifier actions into the kernel\n");
 return -EINVAL;
#else
// 使用NET_CLS_ACT优于使用NETFILTER
 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
#endif
#endif
                                                                               
#ifndef CONFIG_NET_CLS_ACT
#ifdef CONFIG_NETFILTER
// 没定义NET_CLS_ACT, 而定义了NETFILTER的情况
// 登记输入流控的netfilter钩子节点函数
 if (!nf_registered) {
// 登记IPV4钩子节点
  if (nf_register_hook(&ing_ops) < 0) {
   printk("ingress qdisc registration error \n");
   return -EINVAL;
  }
// 非0表示已经登记了
  nf_registered++;

// 登记IPV4钩子节点
  if (nf_register_hook(&ing6_ops) < 0) {
   printk("IPv6 ingress qdisc registration error, " \
       "disabling IPv6 support.\n");
  } else