18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
28 #include <linux/list.h>
29 #include <linux/hrtimer.h>
31 #include <linux/slab.h>
38 static int qdisc_notify(
struct net *
net,
struct sk_buff *oskb,
41 static int tclass_notify(
struct net *
net,
struct sk_buff *oskb,
146 for (qp = &qdisc_base; (q = *qp) !=
NULL; qp = &q->
next)
190 for (qp = &qdisc_base; (q = *qp) !=
NULL; qp = &q->
next)
222 static void qdisc_list_add(
struct Qdisc *q)
239 q = qdisc_match_from_root(dev->
qdisc, handle);
243 if (dev_ingress_queue(dev))
244 q = qdisc_match_from_root(
245 dev_ingress_queue(dev)->qdisc_sleeping,
259 cl = cops->
get(p, classid);
263 leaf = cops->
leaf(p, cl);
276 for (q = qdisc_base;
q; q = q->
next) {
278 if (!try_module_get(q->
owner))
294 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->
next) {
310 rtab->
next = qdisc_rtab_list;
311 qdisc_rtab_list = rtab;
321 if (!tab || --tab->
refcnt)
324 for (rtabp = &qdisc_rtab_list;
325 (rtab = *rtabp) !=
NULL;
326 rtabp = &rtab->
next) {
349 unsigned int tsize = 0;
353 err = nla_parse_nested(tb,
TCA_STAB_MAX, opt, stab_policy);
359 s = nla_data(tb[TCA_STAB_BASE]);
364 tab = nla_data(tb[TCA_STAB_DATA]);
365 tsize = nla_len(tb[TCA_STAB_DATA]) /
sizeof(
u16);
368 if (tsize != s->
tsize || (!tab && tsize > 0))
371 spin_lock(&qdisc_stab_lock);
376 if (tsize > 0 &&
memcmp(stab->
data, tab, tsize *
sizeof(
u16)))
379 spin_unlock(&qdisc_stab_lock);
383 spin_unlock(&qdisc_stab_lock);
394 spin_lock(&qdisc_stab_lock);
396 spin_unlock(&qdisc_stab_lock);
411 spin_lock(&qdisc_stab_lock);
418 spin_unlock(&qdisc_stab_lock);
426 nest = nla_nest_start(skb,
TCA_STAB);
428 goto nla_put_failure;
430 goto nla_put_failure;
431 nla_nest_end(skb, nest);
443 pkt_len = skb->
len + stab->
szopts.overhead;
447 slot = pkt_len + stab->
szopts.cell_align;
451 slot >>= stab->
szopts.cell_log;
455 pkt_len = stab->
data[stab->
szopts.tsize - 1] *
456 (slot / stab->
szopts.tsize) +
459 pkt_len <<= stab->
szopts.size_log;
470 pr_warn(
"%s: %s qdisc %X: is non-work-conserving?\n",
471 txt, qdisc->
ops->id, qdisc->
handle >> 16);
482 qdisc_unthrottled(wd->
qdisc);
501 &qdisc_root_sleeping(wd->
qdisc)->state))
504 qdisc_throttled(wd->
qdisc);
505 time = ktime_set(0, 0);
514 qdisc_unthrottled(wd->
qdisc);
518 static struct hlist_head *qdisc_class_hash_alloc(
unsigned int n)
530 for (
i = 0;
i <
n;
i++)
536 static void qdisc_class_hash_free(
struct hlist_head *h,
unsigned int n)
538 unsigned int size = n *
sizeof(
struct hlist_head);
551 unsigned int nsize, nmask, osize;
559 nhash = qdisc_class_hash_alloc(nsize);
563 ohash = clhash->
hash;
567 for (i = 0; i < osize; i++) {
569 h = qdisc_class_hash(cl->
classid, nmask);
570 hlist_add_head(&cl->
hnode, &nhash[h]);
573 clhash->
hash = nhash;
576 sch_tree_unlock(sch);
578 qdisc_class_hash_free(ohash, osize);
584 unsigned int size = 4;
586 clhash->
hash = qdisc_class_hash_alloc(size);
607 INIT_HLIST_NODE(&cl->
hnode);
609 hlist_add_head(&cl->
hnode, &clhash->
hash[h]);
617 hlist_del(&cl->
hnode);
650 while ((parentid = sch->
parent)) {
659 cops = sch->
ops->cl_ops;
661 cl = cops->
get(sch, parentid);
670 static void notify_and_destroy(
struct net *
net,
struct sk_buff *skb,
675 qdisc_notify(net, skb, n, clid, old,
new);
694 struct Qdisc *q = old;
695 struct net *net = dev_net(dev);
698 if (parent ==
NULL) {
699 unsigned int i, num_q, ingress;
707 if (!dev_ingress_queue(dev))
714 if (
new && new->ops->attach) {
715 new->ops->attach(
new);
719 for (i = 0; i < num_q; i++) {
720 struct netdev_queue *dev_queue = dev_ingress_queue(dev);
723 dev_queue = netdev_get_tx_queue(dev, i);
734 notify_and_destroy(net, skb, n, classid,
736 if (
new && !new->ops->attach)
740 notify_and_destroy(net, skb, n, classid, old,
new);
749 if (cops && cops->
graft) {
750 unsigned long cl = cops->
get(parent, classid);
752 err = cops->
graft(parent, cl,
new, &old);
753 cops->
put(parent, cl);
758 notify_and_destroy(net, skb, n, classid, old,
new);
773 static struct Qdisc *
784 ops = qdisc_lookup_ops(kind);
785 #ifdef CONFIG_MODULES
798 request_module(
"sch_%s", name);
800 ops = qdisc_lookup_ops(kind);
805 module_put(ops->
owner);
831 handle = qdisc_alloc_handle(dev);
843 stab = qdisc_get_stab(tca[TCA_STAB]);
860 root_lock = qdisc_root_sleeping_lock(sch);
862 root_lock = qdisc_lock(sch);
865 root_lock, tca[TCA_RATE]);
878 module_put(ops->
owner);
894 static int qdisc_change(
struct Qdisc *sch,
struct nlattr **tca)
899 if (tca[TCA_OPTIONS]) {
902 err = sch->
ops->change(sch, tca[TCA_OPTIONS]);
908 stab = qdisc_get_stab(tca[TCA_STAB]);
910 return PTR_ERR(stab);
923 qdisc_root_sleeping_lock(sch),
945 arg.w.stop =
arg.w.skip =
arg.w.count = 0;
946 arg.w.fn = check_loop_fn;
949 q->
ops->cl_ops->walk(q, &
arg.w);
960 leaf = cops->
leaf(q, cl);
962 if (leaf == arg->
p || arg->
depth > 7)
964 return check_loop(leaf, arg->
p, arg->
depth + 1);
973 static int tc_get_qdisc(
struct sk_buff *skb,
struct nlmsghdr *n,
void *arg)
975 struct net *net = sock_net(skb->
sk);
988 err = nlmsg_parse(n,
sizeof(*tcm), tca,
TCA_MAX,
NULL);
998 q = qdisc_leaf(p, clid);
999 }
else if (dev_ingress_queue(dev)) {
1000 q = dev_ingress_queue(dev)->qdisc_sleeping;
1024 err = qdisc_graft(dev, p, skb, n, clid,
NULL, q);
1028 qdisc_notify(net, skb, n, clid,
NULL, q);
1037 static int tc_modify_qdisc(
struct sk_buff *skb,
struct nlmsghdr *n,
void *arg)
1039 struct net *net = sock_net(skb->
sk);
1049 tcm = nlmsg_data(n);
1057 err = nlmsg_parse(n,
sizeof(*tcm), tca,
TCA_MAX,
NULL);
1067 q = qdisc_leaf(p, clid);
1069 q = dev_ingress_queue(dev)->qdisc_sleeping;
1087 goto create_n_graft;
1093 (p && check_loop(q, p, 0)))
1099 goto create_n_graft;
1125 goto create_n_graft;
1141 err = qdisc_change(q, tca);
1143 qdisc_notify(net, skb, n, clid,
NULL, q);
1150 if (dev_ingress_queue(dev))
1151 q = qdisc_create(dev, dev_ingress_queue(dev), p,
1159 if (p && p->
ops->cl_ops && p->
ops->cl_ops->select_queue)
1160 dev_queue = p->
ops->cl_ops->select_queue(p, tcm);
1164 dev_queue = netdev_get_tx_queue(dev, 0);
1166 q = qdisc_create(dev, dev_queue, p,
1177 err = qdisc_graft(dev, p, skb, n, clid, q,
NULL);
1187 static int tc_fill_qdisc(
struct sk_buff *skb,
struct Qdisc *q,
u32 clid,
1192 unsigned char *
b = skb_tail_pointer(skb);
1196 nlh = nlmsg_put(skb, portid, seq, event,
sizeof(*tcm), flags);
1198 goto out_nlmsg_trim;
1199 tcm = nlmsg_data(nlh);
1207 if (nla_put_string(skb, TCA_KIND, q->
ops->id))
1208 goto nla_put_failure;
1209 if (q->
ops->dump && q->
ops->dump(q, skb) < 0)
1210 goto nla_put_failure;
1214 if (stab && qdisc_dump_stab(skb, stab) < 0)
1215 goto nla_put_failure;
1218 qdisc_root_sleeping_lock(q), &
d) < 0)
1219 goto nla_put_failure;
1221 if (q->
ops->dump_stats && q->
ops->dump_stats(q, &
d) < 0)
1222 goto nla_put_failure;
1227 goto nla_put_failure;
1230 goto nla_put_failure;
1241 static bool tc_qdisc_dump_ignore(
struct Qdisc *q)
1246 static int qdisc_notify(
struct net *net,
struct sk_buff *oskb,
1257 if (old && !tc_qdisc_dump_ignore(old)) {
1258 if (tc_fill_qdisc(skb, old, clid, portid, n->
nlmsg_seq,
1262 if (
new && !tc_qdisc_dump_ignore(
new)) {
1263 if (tc_fill_qdisc(skb,
new, clid, portid, n->
nlmsg_seq,
1277 static int tc_dump_qdisc_root(
struct Qdisc *root,
struct sk_buff *skb,
1279 int *q_idx_p,
int s_q_idx)
1281 int ret = 0, q_idx = *q_idx_p;
1288 if (q_idx < s_q_idx) {
1291 if (!tc_qdisc_dump_ignore(q) &&
1298 if (q_idx < s_q_idx) {
1302 if (!tc_qdisc_dump_ignore(q) &&
1319 struct net *net = sock_net(skb->
sk);
1324 s_idx = cb->
args[0];
1325 s_q_idx = q_idx = cb->
args[1];
1338 if (tc_dump_qdisc_root(dev->
qdisc, skb, cb, &q_idx, s_q_idx) < 0)
1341 dev_queue = dev_ingress_queue(dev);
1344 &q_idx, s_q_idx) < 0)
1355 cb->
args[1] = q_idx;
1368 static int tc_ctl_tclass(
struct sk_buff *skb,
struct nlmsghdr *n,
void *arg)
1370 struct net *net = sock_net(skb->
sk);
1371 struct tcmsg *tcm = nlmsg_data(n);
1376 unsigned long cl = 0;
1377 unsigned long new_cl;
1387 err = nlmsg_parse(n,
sizeof(*tcm), tca,
TCA_MAX,
NULL);
1415 }
else if (qid == 0)
1416 qid = dev->
qdisc->handle;
1427 qid = dev->
qdisc->handle;
1436 cops = q->
ops->cl_ops;
1448 cl = cops->
get(q, clid);
1465 err = cops->
delete(q, cl);
1481 err = cops->
change(q, clid, portid, tca, &new_cl);
1493 static int tc_fill_tclass(
struct sk_buff *skb,
struct Qdisc *q,
1495 u32 portid,
u32 seq,
u16 flags,
int event)
1499 unsigned char *b = skb_tail_pointer(skb);
1503 nlh = nlmsg_put(skb, portid, seq, event,
sizeof(*tcm), flags);
1505 goto out_nlmsg_trim;
1506 tcm = nlmsg_data(nlh);
1514 if (nla_put_string(skb, TCA_KIND, q->
ops->id))
1515 goto nla_put_failure;
1516 if (cl_ops->
dump && cl_ops->
dump(q, cl, skb, tcm) < 0)
1517 goto nla_put_failure;
1520 qdisc_root_sleeping_lock(q), &
d) < 0)
1521 goto nla_put_failure;
1524 goto nla_put_failure;
1527 goto nla_put_failure;
1538 static int tclass_notify(
struct net *net,
struct sk_buff *oskb,
1540 unsigned long cl,
int event)
1549 if (tc_fill_tclass(skb, q, cl, portid, n->
nlmsg_seq, 0, event) < 0) {
1564 static int qdisc_class_dump(
struct Qdisc *q,
unsigned long cl,
struct qdisc_walker *arg)
1572 static int tc_dump_tclass_qdisc(
struct Qdisc *q,
struct sk_buff *skb,
1578 if (tc_qdisc_dump_ignore(q) ||
1579 *t_p < s_t || !q->ops->
cl_ops ||
1587 arg.w.fn = qdisc_class_dump;
1591 arg.w.skip = cb->
args[1];
1593 q->
ops->cl_ops->walk(q, &arg.w);
1594 cb->
args[1] = arg.w.count;
1601 static int tc_dump_tclass_root(
struct Qdisc *root,
struct sk_buff *skb,
1610 if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
1614 if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
1623 struct tcmsg *tcm = nlmsg_data(cb->
nlh);
1624 struct net *net = sock_net(skb->
sk);
1638 if (tc_dump_tclass_root(dev->
qdisc, skb, tcm, cb, &t, s_t) < 0)
1641 dev_queue = dev_ingress_queue(dev);
1664 for (; tp; tp = tp->
next) {
1671 #ifdef CONFIG_NET_CLS_ACT
1686 #ifdef CONFIG_NET_CLS_ACT
1692 #ifdef CONFIG_NET_CLS_ACT
1714 tp->
ops->destroy(tp);
1715 module_put(tp->
ops->owner);
1723 while ((tp = *fl) !=
NULL) {
1730 #ifdef CONFIG_PROC_FS
1731 static int psched_show(
struct seq_file *seq,
void *
v)
1751 .open = psched_open,
1757 static int __net_init psched_net_init(
struct net *net)
1768 static void __net_exit psched_net_exit(
struct net *net)
1773 static int __net_init psched_net_init(
struct net *net)
1778 static void __net_exit psched_net_exit(
struct net *net)
1784 .init = psched_net_init,
1785 .exit = psched_net_exit,
1788 static int __init pktsched_init(
void)
1795 "cannot initialize per netns operations\n");