11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * net/sched/sch_prio.c Simple 3-band priority "scheduler". 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 51da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 61da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 71da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 101da177e4SLinus Torvalds * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>: 111da177e4SLinus Torvalds * Init -- EINVAL when opt undefined 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/module.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/types.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/string.h> 191da177e4SLinus Torvalds #include <linux/errno.h> 201da177e4SLinus Torvalds #include <linux/skbuff.h> 21dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 221da177e4SLinus Torvalds #include <net/pkt_sched.h> 23cf1facdaSJiri Pirko #include <net/pkt_cls.h> 241da177e4SLinus Torvalds 25cc7ec456SEric Dumazet struct prio_sched_data { 261da177e4SLinus Torvalds int bands; 2725d8c0d5SJohn Fastabend struct tcf_proto __rcu *filter_list; 286529eabaSJiri Pirko struct tcf_block *block; 291da177e4SLinus Torvalds u8 prio2band[TC_PRIO_MAX+1]; 301da177e4SLinus Torvalds struct Qdisc *queues[TCQ_PRIO_BANDS]; 311da177e4SLinus Torvalds }; 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static struct Qdisc * 351da177e4SLinus Torvalds prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) 361da177e4SLinus Torvalds { 371da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 381da177e4SLinus Torvalds u32 band = skb->priority; 391da177e4SLinus Torvalds struct tcf_result res; 4025d8c0d5SJohn Fastabend struct tcf_proto *fl; 41bdba91ecSPatrick McHardy int err; 421da177e4SLinus Torvalds 43c27f339aSJarek Poplawski *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; 441da177e4SLinus Torvalds if (TC_H_MAJ(skb->priority) != sch->handle) { 4525d8c0d5SJohn Fastabend fl = rcu_dereference_bh(q->filter_list); 4687d83093SJiri Pirko err = tcf_classify(skb, fl, &res, false); 471da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 48dbaaa07aSLucas Nussbaum switch (err) { 491da177e4SLinus Torvalds case TC_ACT_STOLEN: 501da177e4SLinus Torvalds case TC_ACT_QUEUED: 51e25ea21fSJiri Pirko case TC_ACT_TRAP: 52378a2f09SJarek Poplawski *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; 53*f3ae608eSGustavo A. R. Silva /* fall through */ 541da177e4SLinus Torvalds case TC_ACT_SHOT: 551da177e4SLinus Torvalds return NULL; 563ff50b79SStephen Hemminger } 571da177e4SLinus Torvalds #endif 5825d8c0d5SJohn Fastabend if (!fl || err < 0) { 591da177e4SLinus Torvalds if (TC_H_MAJ(band)) 601da177e4SLinus Torvalds band = 0; 611d8ae3fdSDavid S. Miller return q->queues[q->prio2band[band & TC_PRIO_MAX]]; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds band = res.classid; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds band = TC_H_MIN(band) - 1; 663e5c2d3bSJamal Hadi Salim if (band >= q->bands) 671d8ae3fdSDavid S. Miller return q->queues[q->prio2band[0]]; 681d8ae3fdSDavid S. Miller 691da177e4SLinus Torvalds return q->queues[band]; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds static int 73520ac30fSEric Dumazet prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds struct Qdisc *qdisc; 761da177e4SLinus Torvalds int ret; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds qdisc = prio_classify(skb, sch, &ret); 791da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 801da177e4SLinus Torvalds if (qdisc == NULL) { 8129f1df6cSJamal Hadi Salim 82c27f339aSJarek Poplawski if (ret & __NET_XMIT_BYPASS) 8325331d6cSJohn Fastabend qdisc_qstats_drop(sch); 8439ad1297SGao Feng __qdisc_drop(skb, to_free); 851da177e4SLinus Torvalds return ret; 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds #endif 881da177e4SLinus Torvalds 89520ac30fSEric Dumazet ret = qdisc_enqueue(skb, qdisc, to_free); 905f86173bSJussi Kivilinna if (ret == NET_XMIT_SUCCESS) { 916529d75aSWANG Cong qdisc_qstats_backlog_inc(sch, skb); 921da177e4SLinus Torvalds sch->q.qlen++; 931da177e4SLinus Torvalds return NET_XMIT_SUCCESS; 941da177e4SLinus Torvalds } 95378a2f09SJarek Poplawski if (net_xmit_drop_count(ret)) 9625331d6cSJohn Fastabend qdisc_qstats_drop(sch); 971da177e4SLinus Torvalds return ret; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 10048a8f519SPatrick McHardy static struct sk_buff *prio_peek(struct Qdisc *sch) 10148a8f519SPatrick McHardy { 10248a8f519SPatrick McHardy struct prio_sched_data *q = qdisc_priv(sch); 10348a8f519SPatrick McHardy int prio; 10448a8f519SPatrick McHardy 10548a8f519SPatrick McHardy for (prio = 0; prio < q->bands; prio++) { 10648a8f519SPatrick McHardy struct Qdisc *qdisc = q->queues[prio]; 10748a8f519SPatrick McHardy struct sk_buff *skb = qdisc->ops->peek(qdisc); 10848a8f519SPatrick McHardy if (skb) 10948a8f519SPatrick McHardy return skb; 11048a8f519SPatrick McHardy } 11148a8f519SPatrick McHardy return NULL; 11248a8f519SPatrick McHardy } 1131da177e4SLinus Torvalds 1141d8ae3fdSDavid S. Miller static struct sk_buff *prio_dequeue(struct Qdisc *sch) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1171da177e4SLinus Torvalds int prio; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) { 1201d8ae3fdSDavid S. Miller struct Qdisc *qdisc = q->queues[prio]; 1213557619fSFlorian Westphal struct sk_buff *skb = qdisc_dequeue_peeked(qdisc); 1221da177e4SLinus Torvalds if (skb) { 1239190b3b3SEric Dumazet qdisc_bstats_update(sch, skb); 1246529d75aSWANG Cong qdisc_qstats_backlog_dec(sch, skb); 1251da177e4SLinus Torvalds sch->q.qlen--; 1261da177e4SLinus Torvalds return skb; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds return NULL; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static void 1341da177e4SLinus Torvalds prio_reset(struct Qdisc *sch) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds int prio; 1371da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) 1401da177e4SLinus Torvalds qdisc_reset(q->queues[prio]); 1416529d75aSWANG Cong sch->qstats.backlog = 0; 1421da177e4SLinus Torvalds sch->q.qlen = 0; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds static void 1461da177e4SLinus Torvalds prio_destroy(struct Qdisc *sch) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds int prio; 1491da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1501da177e4SLinus Torvalds 1516529eabaSJiri Pirko tcf_block_put(q->block); 1521da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) 1531da177e4SLinus Torvalds qdisc_destroy(q->queues[prio]); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561e90474cSPatrick McHardy static int prio_tune(struct Qdisc *sch, struct nlattr *opt) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1593d7c8257SEric Dumazet struct Qdisc *queues[TCQ_PRIO_BANDS]; 1603d7c8257SEric Dumazet int oldbands = q->bands, i; 161d62733c8SPeter P Waskiewicz Jr struct tc_prio_qopt *qopt; 1621da177e4SLinus Torvalds 1631d8ae3fdSDavid S. Miller if (nla_len(opt) < sizeof(*qopt)) 164d62733c8SPeter P Waskiewicz Jr return -EINVAL; 1651d8ae3fdSDavid S. Miller qopt = nla_data(opt); 166d62733c8SPeter P Waskiewicz Jr 1671d8ae3fdSDavid S. Miller if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) 1681da177e4SLinus Torvalds return -EINVAL; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds for (i = 0; i <= TC_PRIO_MAX; i++) { 1711d8ae3fdSDavid S. Miller if (qopt->priomap[i] >= qopt->bands) 1721da177e4SLinus Torvalds return -EINVAL; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1753d7c8257SEric Dumazet /* Before commit, make sure we can allocate all new qdiscs */ 1763d7c8257SEric Dumazet for (i = oldbands; i < qopt->bands; i++) { 1773d7c8257SEric Dumazet queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 1783d7c8257SEric Dumazet TC_H_MAKE(sch->handle, i + 1)); 1793d7c8257SEric Dumazet if (!queues[i]) { 1803d7c8257SEric Dumazet while (i > oldbands) 1813d7c8257SEric Dumazet qdisc_destroy(queues[--i]); 1823d7c8257SEric Dumazet return -ENOMEM; 1833d7c8257SEric Dumazet } 1843d7c8257SEric Dumazet } 1853d7c8257SEric Dumazet 1861da177e4SLinus Torvalds sch_tree_lock(sch); 1871d8ae3fdSDavid S. Miller q->bands = qopt->bands; 1881da177e4SLinus Torvalds memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); 1891da177e4SLinus Torvalds 1903d7c8257SEric Dumazet for (i = q->bands; i < oldbands; i++) { 191b94c8afcSPatrick McHardy struct Qdisc *child = q->queues[i]; 1923d7c8257SEric Dumazet 1933d7c8257SEric Dumazet qdisc_tree_reduce_backlog(child, child->q.qlen, 1943d7c8257SEric Dumazet child->qstats.backlog); 1951da177e4SLinus Torvalds qdisc_destroy(child); 1961da177e4SLinus Torvalds } 1973d7c8257SEric Dumazet 19849b49971SJiri Kosina for (i = oldbands; i < q->bands; i++) { 1993d7c8257SEric Dumazet q->queues[i] = queues[i]; 20049b49971SJiri Kosina if (q->queues[i] != &noop_qdisc) 20149b49971SJiri Kosina qdisc_hash_add(q->queues[i], true); 20249b49971SJiri Kosina } 2033d7c8257SEric Dumazet 2041da177e4SLinus Torvalds sch_tree_unlock(sch); 2051da177e4SLinus Torvalds return 0; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081e90474cSPatrick McHardy static int prio_init(struct Qdisc *sch, struct nlattr *opt) 2091da177e4SLinus Torvalds { 2106529eabaSJiri Pirko struct prio_sched_data *q = qdisc_priv(sch); 2116529eabaSJiri Pirko int err; 2126529eabaSJiri Pirko 2133d7c8257SEric Dumazet if (!opt) 2141da177e4SLinus Torvalds return -EINVAL; 2151da177e4SLinus Torvalds 21669d78ef2SJiri Pirko err = tcf_block_get(&q->block, &q->filter_list, sch); 2176529eabaSJiri Pirko if (err) 2186529eabaSJiri Pirko return err; 2196529eabaSJiri Pirko 2203d7c8257SEric Dumazet return prio_tune(sch, opt); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 22627a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 2271da177e4SLinus Torvalds struct tc_prio_qopt opt; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds opt.bands = q->bands; 2301da177e4SLinus Torvalds memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1); 231d62733c8SPeter P Waskiewicz Jr 2321b34ec43SDavid S. Miller if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 2331b34ec43SDavid S. Miller goto nla_put_failure; 234d62733c8SPeter P Waskiewicz Jr 2351da177e4SLinus Torvalds return skb->len; 2361da177e4SLinus Torvalds 2371e90474cSPatrick McHardy nla_put_failure: 238dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 2391da177e4SLinus Torvalds return -1; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 2431da177e4SLinus Torvalds struct Qdisc **old) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2461da177e4SLinus Torvalds unsigned long band = arg - 1; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (new == NULL) 2491da177e4SLinus Torvalds new = &noop_qdisc; 2501da177e4SLinus Torvalds 25186a7996cSWANG Cong *old = qdisc_replace(sch, new, &q->queues[band]); 2521da177e4SLinus Torvalds return 0; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static struct Qdisc * 2561da177e4SLinus Torvalds prio_leaf(struct Qdisc *sch, unsigned long arg) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2591da177e4SLinus Torvalds unsigned long band = arg - 1; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds return q->queues[band]; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264143976ceSWANG Cong static unsigned long prio_find(struct Qdisc *sch, u32 classid) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2671da177e4SLinus Torvalds unsigned long band = TC_H_MIN(classid); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds if (band - 1 >= q->bands) 2701da177e4SLinus Torvalds return 0; 2711da177e4SLinus Torvalds return band; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) 2751da177e4SLinus Torvalds { 276143976ceSWANG Cong return prio_find(sch, classid); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds 280143976ceSWANG Cong static void prio_unbind(struct Qdisc *q, unsigned long cl) 2811da177e4SLinus Torvalds { 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, 2851da177e4SLinus Torvalds struct tcmsg *tcm) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds tcm->tcm_handle |= TC_H_MIN(cl); 2901da177e4SLinus Torvalds tcm->tcm_info = q->queues[cl-1]->handle; 2911da177e4SLinus Torvalds return 0; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2942cf6c36cSJarek Poplawski static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 2952cf6c36cSJarek Poplawski struct gnet_dump *d) 2962cf6c36cSJarek Poplawski { 2972cf6c36cSJarek Poplawski struct prio_sched_data *q = qdisc_priv(sch); 2982cf6c36cSJarek Poplawski struct Qdisc *cl_q; 2992cf6c36cSJarek Poplawski 3002cf6c36cSJarek Poplawski cl_q = q->queues[cl - 1]; 301edb09eb1SEric Dumazet if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), 302edb09eb1SEric Dumazet d, NULL, &cl_q->bstats) < 0 || 303b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0) 3042cf6c36cSJarek Poplawski return -1; 3052cf6c36cSJarek Poplawski 3062cf6c36cSJarek Poplawski return 0; 3072cf6c36cSJarek Poplawski } 3082cf6c36cSJarek Poplawski 3091da177e4SLinus Torvalds static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 3121da177e4SLinus Torvalds int prio; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (arg->stop) 3151da177e4SLinus Torvalds return; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) { 3181da177e4SLinus Torvalds if (arg->count < arg->skip) { 3191da177e4SLinus Torvalds arg->count++; 3201da177e4SLinus Torvalds continue; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds if (arg->fn(sch, prio + 1, arg) < 0) { 3231da177e4SLinus Torvalds arg->stop = 1; 3241da177e4SLinus Torvalds break; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds arg->count++; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3306529eabaSJiri Pirko static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds if (cl) 3351da177e4SLinus Torvalds return NULL; 3366529eabaSJiri Pirko return q->block; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 33920fea08bSEric Dumazet static const struct Qdisc_class_ops prio_class_ops = { 3401da177e4SLinus Torvalds .graft = prio_graft, 3411da177e4SLinus Torvalds .leaf = prio_leaf, 342143976ceSWANG Cong .find = prio_find, 3431da177e4SLinus Torvalds .walk = prio_walk, 3446529eabaSJiri Pirko .tcf_block = prio_tcf_block, 3451da177e4SLinus Torvalds .bind_tcf = prio_bind, 346143976ceSWANG Cong .unbind_tcf = prio_unbind, 3471da177e4SLinus Torvalds .dump = prio_dump_class, 3482cf6c36cSJarek Poplawski .dump_stats = prio_dump_class_stats, 3491da177e4SLinus Torvalds }; 3501da177e4SLinus Torvalds 35120fea08bSEric Dumazet static struct Qdisc_ops prio_qdisc_ops __read_mostly = { 3521da177e4SLinus Torvalds .next = NULL, 3531da177e4SLinus Torvalds .cl_ops = &prio_class_ops, 3541da177e4SLinus Torvalds .id = "prio", 3551da177e4SLinus Torvalds .priv_size = sizeof(struct prio_sched_data), 3561da177e4SLinus Torvalds .enqueue = prio_enqueue, 3571da177e4SLinus Torvalds .dequeue = prio_dequeue, 35848a8f519SPatrick McHardy .peek = prio_peek, 3591da177e4SLinus Torvalds .init = prio_init, 3601da177e4SLinus Torvalds .reset = prio_reset, 3611da177e4SLinus Torvalds .destroy = prio_destroy, 3621da177e4SLinus Torvalds .change = prio_tune, 3631da177e4SLinus Torvalds .dump = prio_dump, 3641da177e4SLinus Torvalds .owner = THIS_MODULE, 3651da177e4SLinus Torvalds }; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds static int __init prio_module_init(void) 3681da177e4SLinus Torvalds { 3691d8ae3fdSDavid S. Miller return register_qdisc(&prio_qdisc_ops); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds static void __exit prio_module_exit(void) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds unregister_qdisc(&prio_qdisc_ops); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds module_init(prio_module_init) 3781da177e4SLinus Torvalds module_exit(prio_module_exit) 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 381