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; 28*6529eabaSJiri 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: 51378a2f09SJarek Poplawski *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; 521da177e4SLinus Torvalds case TC_ACT_SHOT: 531da177e4SLinus Torvalds return NULL; 543ff50b79SStephen Hemminger } 551da177e4SLinus Torvalds #endif 5625d8c0d5SJohn Fastabend if (!fl || err < 0) { 571da177e4SLinus Torvalds if (TC_H_MAJ(band)) 581da177e4SLinus Torvalds band = 0; 591d8ae3fdSDavid S. Miller return q->queues[q->prio2band[band & TC_PRIO_MAX]]; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds band = res.classid; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds band = TC_H_MIN(band) - 1; 643e5c2d3bSJamal Hadi Salim if (band >= q->bands) 651d8ae3fdSDavid S. Miller return q->queues[q->prio2band[0]]; 661d8ae3fdSDavid S. Miller 671da177e4SLinus Torvalds return q->queues[band]; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds static int 71520ac30fSEric Dumazet prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct Qdisc *qdisc; 741da177e4SLinus Torvalds int ret; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds qdisc = prio_classify(skb, sch, &ret); 771da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 781da177e4SLinus Torvalds if (qdisc == NULL) { 7929f1df6cSJamal Hadi Salim 80c27f339aSJarek Poplawski if (ret & __NET_XMIT_BYPASS) 8125331d6cSJohn Fastabend qdisc_qstats_drop(sch); 821da177e4SLinus Torvalds kfree_skb(skb); 831da177e4SLinus Torvalds return ret; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds #endif 861da177e4SLinus Torvalds 87520ac30fSEric Dumazet ret = qdisc_enqueue(skb, qdisc, to_free); 885f86173bSJussi Kivilinna if (ret == NET_XMIT_SUCCESS) { 896529d75aSWANG Cong qdisc_qstats_backlog_inc(sch, skb); 901da177e4SLinus Torvalds sch->q.qlen++; 911da177e4SLinus Torvalds return NET_XMIT_SUCCESS; 921da177e4SLinus Torvalds } 93378a2f09SJarek Poplawski if (net_xmit_drop_count(ret)) 9425331d6cSJohn Fastabend qdisc_qstats_drop(sch); 951da177e4SLinus Torvalds return ret; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 9848a8f519SPatrick McHardy static struct sk_buff *prio_peek(struct Qdisc *sch) 9948a8f519SPatrick McHardy { 10048a8f519SPatrick McHardy struct prio_sched_data *q = qdisc_priv(sch); 10148a8f519SPatrick McHardy int prio; 10248a8f519SPatrick McHardy 10348a8f519SPatrick McHardy for (prio = 0; prio < q->bands; prio++) { 10448a8f519SPatrick McHardy struct Qdisc *qdisc = q->queues[prio]; 10548a8f519SPatrick McHardy struct sk_buff *skb = qdisc->ops->peek(qdisc); 10648a8f519SPatrick McHardy if (skb) 10748a8f519SPatrick McHardy return skb; 10848a8f519SPatrick McHardy } 10948a8f519SPatrick McHardy return NULL; 11048a8f519SPatrick McHardy } 1111da177e4SLinus Torvalds 1121d8ae3fdSDavid S. Miller static struct sk_buff *prio_dequeue(struct Qdisc *sch) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1151da177e4SLinus Torvalds int prio; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) { 1181d8ae3fdSDavid S. Miller struct Qdisc *qdisc = q->queues[prio]; 1193557619fSFlorian Westphal struct sk_buff *skb = qdisc_dequeue_peeked(qdisc); 1201da177e4SLinus Torvalds if (skb) { 1219190b3b3SEric Dumazet qdisc_bstats_update(sch, skb); 1226529d75aSWANG Cong qdisc_qstats_backlog_dec(sch, skb); 1231da177e4SLinus Torvalds sch->q.qlen--; 1241da177e4SLinus Torvalds return skb; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds return NULL; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static void 1321da177e4SLinus Torvalds prio_reset(struct Qdisc *sch) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds int prio; 1351da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) 1381da177e4SLinus Torvalds qdisc_reset(q->queues[prio]); 1396529d75aSWANG Cong sch->qstats.backlog = 0; 1401da177e4SLinus Torvalds sch->q.qlen = 0; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static void 1441da177e4SLinus Torvalds prio_destroy(struct Qdisc *sch) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds int prio; 1471da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1481da177e4SLinus Torvalds 149*6529eabaSJiri Pirko tcf_block_put(q->block); 1501da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) 1511da177e4SLinus Torvalds qdisc_destroy(q->queues[prio]); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541e90474cSPatrick McHardy static int prio_tune(struct Qdisc *sch, struct nlattr *opt) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 1573d7c8257SEric Dumazet struct Qdisc *queues[TCQ_PRIO_BANDS]; 1583d7c8257SEric Dumazet int oldbands = q->bands, i; 159d62733c8SPeter P Waskiewicz Jr struct tc_prio_qopt *qopt; 1601da177e4SLinus Torvalds 1611d8ae3fdSDavid S. Miller if (nla_len(opt) < sizeof(*qopt)) 162d62733c8SPeter P Waskiewicz Jr return -EINVAL; 1631d8ae3fdSDavid S. Miller qopt = nla_data(opt); 164d62733c8SPeter P Waskiewicz Jr 1651d8ae3fdSDavid S. Miller if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) 1661da177e4SLinus Torvalds return -EINVAL; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds for (i = 0; i <= TC_PRIO_MAX; i++) { 1691d8ae3fdSDavid S. Miller if (qopt->priomap[i] >= qopt->bands) 1701da177e4SLinus Torvalds return -EINVAL; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1733d7c8257SEric Dumazet /* Before commit, make sure we can allocate all new qdiscs */ 1743d7c8257SEric Dumazet for (i = oldbands; i < qopt->bands; i++) { 1753d7c8257SEric Dumazet queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 1763d7c8257SEric Dumazet TC_H_MAKE(sch->handle, i + 1)); 1773d7c8257SEric Dumazet if (!queues[i]) { 1783d7c8257SEric Dumazet while (i > oldbands) 1793d7c8257SEric Dumazet qdisc_destroy(queues[--i]); 1803d7c8257SEric Dumazet return -ENOMEM; 1813d7c8257SEric Dumazet } 1823d7c8257SEric Dumazet } 1833d7c8257SEric Dumazet 1841da177e4SLinus Torvalds sch_tree_lock(sch); 1851d8ae3fdSDavid S. Miller q->bands = qopt->bands; 1861da177e4SLinus Torvalds memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); 1871da177e4SLinus Torvalds 1883d7c8257SEric Dumazet for (i = q->bands; i < oldbands; i++) { 189b94c8afcSPatrick McHardy struct Qdisc *child = q->queues[i]; 1903d7c8257SEric Dumazet 1913d7c8257SEric Dumazet qdisc_tree_reduce_backlog(child, child->q.qlen, 1923d7c8257SEric Dumazet child->qstats.backlog); 1931da177e4SLinus Torvalds qdisc_destroy(child); 1941da177e4SLinus Torvalds } 1953d7c8257SEric Dumazet 19649b49971SJiri Kosina for (i = oldbands; i < q->bands; i++) { 1973d7c8257SEric Dumazet q->queues[i] = queues[i]; 19849b49971SJiri Kosina if (q->queues[i] != &noop_qdisc) 19949b49971SJiri Kosina qdisc_hash_add(q->queues[i], true); 20049b49971SJiri Kosina } 2013d7c8257SEric Dumazet 2021da177e4SLinus Torvalds sch_tree_unlock(sch); 2031da177e4SLinus Torvalds return 0; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061e90474cSPatrick McHardy static int prio_init(struct Qdisc *sch, struct nlattr *opt) 2071da177e4SLinus Torvalds { 208*6529eabaSJiri Pirko struct prio_sched_data *q = qdisc_priv(sch); 209*6529eabaSJiri Pirko int err; 210*6529eabaSJiri Pirko 2113d7c8257SEric Dumazet if (!opt) 2121da177e4SLinus Torvalds return -EINVAL; 2131da177e4SLinus Torvalds 214*6529eabaSJiri Pirko err = tcf_block_get(&q->block, &q->filter_list); 215*6529eabaSJiri Pirko if (err) 216*6529eabaSJiri Pirko return err; 217*6529eabaSJiri Pirko 2183d7c8257SEric Dumazet return prio_tune(sch, opt); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 22427a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 2251da177e4SLinus Torvalds struct tc_prio_qopt opt; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds opt.bands = q->bands; 2281da177e4SLinus Torvalds memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1); 229d62733c8SPeter P Waskiewicz Jr 2301b34ec43SDavid S. Miller if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 2311b34ec43SDavid S. Miller goto nla_put_failure; 232d62733c8SPeter P Waskiewicz Jr 2331da177e4SLinus Torvalds return skb->len; 2341da177e4SLinus Torvalds 2351e90474cSPatrick McHardy nla_put_failure: 236dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 2371da177e4SLinus Torvalds return -1; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 2411da177e4SLinus Torvalds struct Qdisc **old) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2441da177e4SLinus Torvalds unsigned long band = arg - 1; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (new == NULL) 2471da177e4SLinus Torvalds new = &noop_qdisc; 2481da177e4SLinus Torvalds 24986a7996cSWANG Cong *old = qdisc_replace(sch, new, &q->queues[band]); 2501da177e4SLinus Torvalds return 0; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static struct Qdisc * 2541da177e4SLinus Torvalds prio_leaf(struct Qdisc *sch, unsigned long arg) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2571da177e4SLinus Torvalds unsigned long band = arg - 1; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds return q->queues[band]; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static unsigned long prio_get(struct Qdisc *sch, u32 classid) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2651da177e4SLinus Torvalds unsigned long band = TC_H_MIN(classid); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (band - 1 >= q->bands) 2681da177e4SLinus Torvalds return 0; 2691da177e4SLinus Torvalds return band; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds return prio_get(sch, classid); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds static void prio_put(struct Qdisc *q, unsigned long cl) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, 2831da177e4SLinus Torvalds struct tcmsg *tcm) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds tcm->tcm_handle |= TC_H_MIN(cl); 2881da177e4SLinus Torvalds tcm->tcm_info = q->queues[cl-1]->handle; 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2922cf6c36cSJarek Poplawski static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 2932cf6c36cSJarek Poplawski struct gnet_dump *d) 2942cf6c36cSJarek Poplawski { 2952cf6c36cSJarek Poplawski struct prio_sched_data *q = qdisc_priv(sch); 2962cf6c36cSJarek Poplawski struct Qdisc *cl_q; 2972cf6c36cSJarek Poplawski 2982cf6c36cSJarek Poplawski cl_q = q->queues[cl - 1]; 299edb09eb1SEric Dumazet if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), 300edb09eb1SEric Dumazet d, NULL, &cl_q->bstats) < 0 || 301b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0) 3022cf6c36cSJarek Poplawski return -1; 3032cf6c36cSJarek Poplawski 3042cf6c36cSJarek Poplawski return 0; 3052cf6c36cSJarek Poplawski } 3062cf6c36cSJarek Poplawski 3071da177e4SLinus Torvalds static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 3101da177e4SLinus Torvalds int prio; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds if (arg->stop) 3131da177e4SLinus Torvalds return; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds for (prio = 0; prio < q->bands; prio++) { 3161da177e4SLinus Torvalds if (arg->count < arg->skip) { 3171da177e4SLinus Torvalds arg->count++; 3181da177e4SLinus Torvalds continue; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds if (arg->fn(sch, prio + 1, arg) < 0) { 3211da177e4SLinus Torvalds arg->stop = 1; 3221da177e4SLinus Torvalds break; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds arg->count++; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 328*6529eabaSJiri Pirko static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds struct prio_sched_data *q = qdisc_priv(sch); 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds if (cl) 3331da177e4SLinus Torvalds return NULL; 334*6529eabaSJiri Pirko return q->block; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 33720fea08bSEric Dumazet static const struct Qdisc_class_ops prio_class_ops = { 3381da177e4SLinus Torvalds .graft = prio_graft, 3391da177e4SLinus Torvalds .leaf = prio_leaf, 3401da177e4SLinus Torvalds .get = prio_get, 3411da177e4SLinus Torvalds .put = prio_put, 3421da177e4SLinus Torvalds .walk = prio_walk, 343*6529eabaSJiri Pirko .tcf_block = prio_tcf_block, 3441da177e4SLinus Torvalds .bind_tcf = prio_bind, 3451da177e4SLinus Torvalds .unbind_tcf = prio_put, 3461da177e4SLinus Torvalds .dump = prio_dump_class, 3472cf6c36cSJarek Poplawski .dump_stats = prio_dump_class_stats, 3481da177e4SLinus Torvalds }; 3491da177e4SLinus Torvalds 35020fea08bSEric Dumazet static struct Qdisc_ops prio_qdisc_ops __read_mostly = { 3511da177e4SLinus Torvalds .next = NULL, 3521da177e4SLinus Torvalds .cl_ops = &prio_class_ops, 3531da177e4SLinus Torvalds .id = "prio", 3541da177e4SLinus Torvalds .priv_size = sizeof(struct prio_sched_data), 3551da177e4SLinus Torvalds .enqueue = prio_enqueue, 3561da177e4SLinus Torvalds .dequeue = prio_dequeue, 35748a8f519SPatrick McHardy .peek = prio_peek, 3581da177e4SLinus Torvalds .init = prio_init, 3591da177e4SLinus Torvalds .reset = prio_reset, 3601da177e4SLinus Torvalds .destroy = prio_destroy, 3611da177e4SLinus Torvalds .change = prio_tune, 3621da177e4SLinus Torvalds .dump = prio_dump, 3631da177e4SLinus Torvalds .owner = THIS_MODULE, 3641da177e4SLinus Torvalds }; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds static int __init prio_module_init(void) 3671da177e4SLinus Torvalds { 3681d8ae3fdSDavid S. Miller return register_qdisc(&prio_qdisc_ops); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds static void __exit prio_module_exit(void) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds unregister_qdisc(&prio_qdisc_ops); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds module_init(prio_module_init) 3771da177e4SLinus Torvalds module_exit(prio_module_exit) 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 380