xref: /openbmc/linux/net/sched/sch_gred.c (revision 339346d4)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * net/sched/sch_gred.c	Generic Random Early Detection queue.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:    J Hadi Salim (hadi@cyberus.ca) 1998-2002
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *             991129: -  Bug fix with grio mode
81da177e4SLinus Torvalds  *		       - a better sing. AvgQ mode with Grio(WRED)
937f2ad2bSZheng Yongjun  *		       - A finer grained VQ dequeue based on suggestion
101da177e4SLinus Torvalds  *		         from Ren Liu
111da177e4SLinus Torvalds  *		       - More error checks
121da177e4SLinus Torvalds  *
131e4dfaf9SThomas Graf  *  For all the glorious comments look at include/net/red.h
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
165a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/skbuff.h>
21890d8d23SJakub Kicinski #include <net/pkt_cls.h>
221da177e4SLinus Torvalds #include <net/pkt_sched.h>
2322b33429SThomas Graf #include <net/red.h>
241da177e4SLinus Torvalds 
25f62d6b93SThomas Graf #define GRED_DEF_PRIO (MAX_DPs / 2)
26716a1b40SThomas Graf #define GRED_VQ_MASK (MAX_DPs - 1)
27f62d6b93SThomas Graf 
2825fc1989SJakub Kicinski #define GRED_VQ_RED_FLAGS	(TC_RED_ECN | TC_RED_HARDDROP)
2925fc1989SJakub Kicinski 
301da177e4SLinus Torvalds struct gred_sched_data;
311da177e4SLinus Torvalds struct gred_sched;
321da177e4SLinus Torvalds 
33cc7ec456SEric Dumazet struct gred_sched_data {
341da177e4SLinus Torvalds 	u32		limit;		/* HARD maximal queue length	*/
35a73ed26bSEric Dumazet 	u32		DP;		/* the drop parameters */
3625fc1989SJakub Kicinski 	u32		red_flags;	/* virtualQ version of red_flags */
379f5cd0c8SJakub Kicinski 	u64		bytesin;	/* bytes seen on virtualQ so far*/
381da177e4SLinus Torvalds 	u32		packetsin;	/* packets seen on virtualQ so far*/
391da177e4SLinus Torvalds 	u32		backlog;	/* bytes on the virtualQ */
401da177e4SLinus Torvalds 	u8		prio;		/* the prio of this vq */
411da177e4SLinus Torvalds 
4222b33429SThomas Graf 	struct red_parms parms;
43eeca6688SEric Dumazet 	struct red_vars  vars;
4422b33429SThomas Graf 	struct red_stats stats;
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
47dea3f628SThomas Graf enum {
48dea3f628SThomas Graf 	GRED_WRED_MODE = 1,
49d6fd4e96SThomas Graf 	GRED_RIO_MODE,
50dea3f628SThomas Graf };
51dea3f628SThomas Graf 
52cc7ec456SEric Dumazet struct gred_sched {
531da177e4SLinus Torvalds 	struct gred_sched_data *tab[MAX_DPs];
54dea3f628SThomas Graf 	unsigned long	flags;
55b38c7eefSThomas Graf 	u32		red_flags;
561da177e4SLinus Torvalds 	u32 		DPs;
571da177e4SLinus Torvalds 	u32 		def;
58eeca6688SEric Dumazet 	struct red_vars wred_set;
59f25c0515SArnd Bergmann 	struct tc_gred_qopt_offload *opt;
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
gred_wred_mode(struct gred_sched * table)62dea3f628SThomas Graf static inline int gred_wred_mode(struct gred_sched *table)
63dea3f628SThomas Graf {
64dea3f628SThomas Graf 	return test_bit(GRED_WRED_MODE, &table->flags);
65dea3f628SThomas Graf }
66dea3f628SThomas Graf 
gred_enable_wred_mode(struct gred_sched * table)67dea3f628SThomas Graf static inline void gred_enable_wred_mode(struct gred_sched *table)
68dea3f628SThomas Graf {
69dea3f628SThomas Graf 	__set_bit(GRED_WRED_MODE, &table->flags);
70dea3f628SThomas Graf }
71dea3f628SThomas Graf 
gred_disable_wred_mode(struct gred_sched * table)72dea3f628SThomas Graf static inline void gred_disable_wred_mode(struct gred_sched *table)
73dea3f628SThomas Graf {
74dea3f628SThomas Graf 	__clear_bit(GRED_WRED_MODE, &table->flags);
75dea3f628SThomas Graf }
76dea3f628SThomas Graf 
gred_rio_mode(struct gred_sched * table)77d6fd4e96SThomas Graf static inline int gred_rio_mode(struct gred_sched *table)
78d6fd4e96SThomas Graf {
79d6fd4e96SThomas Graf 	return test_bit(GRED_RIO_MODE, &table->flags);
80d6fd4e96SThomas Graf }
81d6fd4e96SThomas Graf 
gred_enable_rio_mode(struct gred_sched * table)82d6fd4e96SThomas Graf static inline void gred_enable_rio_mode(struct gred_sched *table)
83d6fd4e96SThomas Graf {
84d6fd4e96SThomas Graf 	__set_bit(GRED_RIO_MODE, &table->flags);
85d6fd4e96SThomas Graf }
86d6fd4e96SThomas Graf 
gred_disable_rio_mode(struct gred_sched * table)87d6fd4e96SThomas Graf static inline void gred_disable_rio_mode(struct gred_sched *table)
88d6fd4e96SThomas Graf {
89d6fd4e96SThomas Graf 	__clear_bit(GRED_RIO_MODE, &table->flags);
90d6fd4e96SThomas Graf }
91d6fd4e96SThomas Graf 
gred_wred_mode_check(struct Qdisc * sch)92dea3f628SThomas Graf static inline int gred_wred_mode_check(struct Qdisc *sch)
93dea3f628SThomas Graf {
94dea3f628SThomas Graf 	struct gred_sched *table = qdisc_priv(sch);
95dea3f628SThomas Graf 	int i;
96dea3f628SThomas Graf 
97dea3f628SThomas Graf 	/* Really ugly O(n^2) but shouldn't be necessary too frequent. */
98dea3f628SThomas Graf 	for (i = 0; i < table->DPs; i++) {
99dea3f628SThomas Graf 		struct gred_sched_data *q = table->tab[i];
100dea3f628SThomas Graf 		int n;
101dea3f628SThomas Graf 
102dea3f628SThomas Graf 		if (q == NULL)
103dea3f628SThomas Graf 			continue;
104dea3f628SThomas Graf 
105c22e4640SDavid Ward 		for (n = i + 1; n < table->DPs; n++)
106c22e4640SDavid Ward 			if (table->tab[n] && table->tab[n]->prio == q->prio)
107dea3f628SThomas Graf 				return 1;
108dea3f628SThomas Graf 	}
109dea3f628SThomas Graf 
110dea3f628SThomas Graf 	return 0;
111dea3f628SThomas Graf }
112dea3f628SThomas Graf 
gred_backlog(struct gred_sched * table,struct gred_sched_data * q,struct Qdisc * sch)11322b33429SThomas Graf static inline unsigned int gred_backlog(struct gred_sched *table,
11422b33429SThomas Graf 					struct gred_sched_data *q,
11522b33429SThomas Graf 					struct Qdisc *sch)
11622b33429SThomas Graf {
11722b33429SThomas Graf 	if (gred_wred_mode(table))
11822b33429SThomas Graf 		return sch->qstats.backlog;
11922b33429SThomas Graf 	else
12022b33429SThomas Graf 		return q->backlog;
12122b33429SThomas Graf }
12222b33429SThomas Graf 
tc_index_to_dp(struct sk_buff * skb)123716a1b40SThomas Graf static inline u16 tc_index_to_dp(struct sk_buff *skb)
124716a1b40SThomas Graf {
125716a1b40SThomas Graf 	return skb->tc_index & GRED_VQ_MASK;
126716a1b40SThomas Graf }
127716a1b40SThomas Graf 
gred_load_wred_set(const struct gred_sched * table,struct gred_sched_data * q)128eeca6688SEric Dumazet static inline void gred_load_wred_set(const struct gred_sched *table,
1297051703bSThomas Graf 				      struct gred_sched_data *q)
1307051703bSThomas Graf {
131eeca6688SEric Dumazet 	q->vars.qavg = table->wred_set.qavg;
132eeca6688SEric Dumazet 	q->vars.qidlestart = table->wred_set.qidlestart;
1337051703bSThomas Graf }
1347051703bSThomas Graf 
gred_store_wred_set(struct gred_sched * table,struct gred_sched_data * q)1357051703bSThomas Graf static inline void gred_store_wred_set(struct gred_sched *table,
1367051703bSThomas Graf 				       struct gred_sched_data *q)
1377051703bSThomas Graf {
138eeca6688SEric Dumazet 	table->wred_set.qavg = q->vars.qavg;
139ba1bf474SDavid Ward 	table->wred_set.qidlestart = q->vars.qidlestart;
1407051703bSThomas Graf }
1417051703bSThomas Graf 
gred_use_ecn(struct gred_sched_data * q)14225fc1989SJakub Kicinski static int gred_use_ecn(struct gred_sched_data *q)
143b38c7eefSThomas Graf {
14425fc1989SJakub Kicinski 	return q->red_flags & TC_RED_ECN;
145b38c7eefSThomas Graf }
146b38c7eefSThomas Graf 
gred_use_harddrop(struct gred_sched_data * q)14725fc1989SJakub Kicinski static int gred_use_harddrop(struct gred_sched_data *q)
148bdc450a0SThomas Graf {
14925fc1989SJakub Kicinski 	return q->red_flags & TC_RED_HARDDROP;
150bdc450a0SThomas Graf }
151bdc450a0SThomas Graf 
gred_per_vq_red_flags_used(struct gred_sched * table)15272111015SJakub Kicinski static bool gred_per_vq_red_flags_used(struct gred_sched *table)
15372111015SJakub Kicinski {
15472111015SJakub Kicinski 	unsigned int i;
15572111015SJakub Kicinski 
15672111015SJakub Kicinski 	/* Local per-vq flags couldn't have been set unless global are 0 */
15772111015SJakub Kicinski 	if (table->red_flags)
15872111015SJakub Kicinski 		return false;
15972111015SJakub Kicinski 	for (i = 0; i < MAX_DPs; i++)
16072111015SJakub Kicinski 		if (table->tab[i] && table->tab[i]->red_flags)
16172111015SJakub Kicinski 			return true;
16272111015SJakub Kicinski 	return false;
16372111015SJakub Kicinski }
16472111015SJakub Kicinski 
gred_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)165520ac30fSEric Dumazet static int gred_enqueue(struct sk_buff *skb, struct Qdisc *sch,
166520ac30fSEric Dumazet 			struct sk_buff **to_free)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct gred_sched_data *q = NULL;
1691da177e4SLinus Torvalds 	struct gred_sched *t = qdisc_priv(sch);
17022b33429SThomas Graf 	unsigned long qavg = 0;
1714a591834SThomas Graf 	u16 dp = tc_index_to_dp(skb);
1721da177e4SLinus Torvalds 
173716a1b40SThomas Graf 	if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
17418e3fb84SThomas Graf 		dp = t->def;
17518e3fb84SThomas Graf 
176cc7ec456SEric Dumazet 		q = t->tab[dp];
177cc7ec456SEric Dumazet 		if (!q) {
17818e3fb84SThomas Graf 			/* Pass through packets not assigned to a DP
17918e3fb84SThomas Graf 			 * if no default DP has been configured. This
18018e3fb84SThomas Graf 			 * allows for DP flows to be left untouched.
18118e3fb84SThomas Graf 			 */
182a3eb95f8SDavid Ward 			if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <=
183a3eb95f8SDavid Ward 					sch->limit))
18418e3fb84SThomas Graf 				return qdisc_enqueue_tail(skb, sch);
18518e3fb84SThomas Graf 			else
1861da177e4SLinus Torvalds 				goto drop;
1871da177e4SLinus Torvalds 		}
18818e3fb84SThomas Graf 
189eeca6688SEric Dumazet 		/* fix tc_index? --could be controversial but needed for
1901da177e4SLinus Torvalds 		   requeueing */
19118e3fb84SThomas Graf 		skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
194e29fe837SDavid Ward 	/* sum up all the qaves of prios < ours to get the new qave */
195d6fd4e96SThomas Graf 	if (!gred_wred_mode(t) && gred_rio_mode(t)) {
1961e4dfaf9SThomas Graf 		int i;
1971da177e4SLinus Torvalds 
1981e4dfaf9SThomas Graf 		for (i = 0; i < t->DPs; i++) {
1991e4dfaf9SThomas Graf 			if (t->tab[i] && t->tab[i]->prio < q->prio &&
200eeca6688SEric Dumazet 			    !red_is_idling(&t->tab[i]->vars))
201eeca6688SEric Dumazet 				qavg += t->tab[i]->vars.qavg;
2021da177e4SLinus Torvalds 		}
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	q->packetsin++;
2070abf77e5SJussi Kivilinna 	q->bytesin += qdisc_pkt_len(skb);
2081da177e4SLinus Torvalds 
2091e4dfaf9SThomas Graf 	if (gred_wred_mode(t))
2107051703bSThomas Graf 		gred_load_wred_set(t, q);
2111da177e4SLinus Torvalds 
212eeca6688SEric Dumazet 	q->vars.qavg = red_calc_qavg(&q->parms,
213eeca6688SEric Dumazet 				     &q->vars,
214eeca6688SEric Dumazet 				     gred_backlog(t, q, sch));
2151da177e4SLinus Torvalds 
216eeca6688SEric Dumazet 	if (red_is_idling(&q->vars))
217eeca6688SEric Dumazet 		red_end_of_idle_period(&q->vars);
2181da177e4SLinus Torvalds 
219dea3f628SThomas Graf 	if (gred_wred_mode(t))
2207051703bSThomas Graf 		gred_store_wred_set(t, q);
2211da177e4SLinus Torvalds 
222eeca6688SEric Dumazet 	switch (red_action(&q->parms, &q->vars, q->vars.qavg + qavg)) {
22322b33429SThomas Graf 	case RED_DONT_MARK:
22422b33429SThomas Graf 		break;
22522b33429SThomas Graf 
22622b33429SThomas Graf 	case RED_PROB_MARK:
22725331d6cSJohn Fastabend 		qdisc_qstats_overlimit(sch);
22825fc1989SJakub Kicinski 		if (!gred_use_ecn(q) || !INET_ECN_set_ce(skb)) {
22922b33429SThomas Graf 			q->stats.prob_drop++;
230c3b553cdSThomas Graf 			goto congestion_drop;
231b38c7eefSThomas Graf 		}
232b38c7eefSThomas Graf 
233b38c7eefSThomas Graf 		q->stats.prob_mark++;
234b38c7eefSThomas Graf 		break;
23522b33429SThomas Graf 
23622b33429SThomas Graf 	case RED_HARD_MARK:
23725331d6cSJohn Fastabend 		qdisc_qstats_overlimit(sch);
23825fc1989SJakub Kicinski 		if (gred_use_harddrop(q) || !gred_use_ecn(q) ||
239bdc450a0SThomas Graf 		    !INET_ECN_set_ce(skb)) {
24022b33429SThomas Graf 			q->stats.forced_drop++;
241c3b553cdSThomas Graf 			goto congestion_drop;
24222b33429SThomas Graf 		}
243b38c7eefSThomas Graf 		q->stats.forced_mark++;
244b38c7eefSThomas Graf 		break;
245b38c7eefSThomas Graf 	}
24622b33429SThomas Graf 
247145a42b3SDavid Ward 	if (gred_backlog(t, q, sch) + qdisc_pkt_len(skb) <= q->limit) {
2480abf77e5SJussi Kivilinna 		q->backlog += qdisc_pkt_len(skb);
249edf7a7b1SThomas Graf 		return qdisc_enqueue_tail(skb, sch);
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 
25222b33429SThomas Graf 	q->stats.pdrop++;
2531da177e4SLinus Torvalds drop:
254520ac30fSEric Dumazet 	return qdisc_drop(skb, sch, to_free);
255c3b553cdSThomas Graf 
256c3b553cdSThomas Graf congestion_drop:
257520ac30fSEric Dumazet 	qdisc_drop(skb, sch, to_free);
258c3b553cdSThomas Graf 	return NET_XMIT_CN;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
gred_dequeue(struct Qdisc * sch)2611e4dfaf9SThomas Graf static struct sk_buff *gred_dequeue(struct Qdisc *sch)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	struct sk_buff *skb;
2641da177e4SLinus Torvalds 	struct gred_sched *t = qdisc_priv(sch);
2651da177e4SLinus Torvalds 
266edf7a7b1SThomas Graf 	skb = qdisc_dequeue_head(sch);
267edf7a7b1SThomas Graf 
2681da177e4SLinus Torvalds 	if (skb) {
2691e4dfaf9SThomas Graf 		struct gred_sched_data *q;
27018e3fb84SThomas Graf 		u16 dp = tc_index_to_dp(skb);
27118e3fb84SThomas Graf 
27218e3fb84SThomas Graf 		if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
273e87cc472SJoe Perches 			net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x after dequeue, screwing up backlog\n",
274e87cc472SJoe Perches 					     tc_index_to_dp(skb));
27518e3fb84SThomas Graf 		} else {
2760abf77e5SJussi Kivilinna 			q->backlog -= qdisc_pkt_len(skb);
27718e3fb84SThomas Graf 
278ba1bf474SDavid Ward 			if (gred_wred_mode(t)) {
279ba1bf474SDavid Ward 				if (!sch->qstats.backlog)
280ba1bf474SDavid Ward 					red_start_of_idle_period(&t->wred_set);
281ba1bf474SDavid Ward 			} else {
282ba1bf474SDavid Ward 				if (!q->backlog)
283eeca6688SEric Dumazet 					red_start_of_idle_period(&q->vars);
2841da177e4SLinus Torvalds 			}
285ba1bf474SDavid Ward 		}
28618e3fb84SThomas Graf 
2871da177e4SLinus Torvalds 		return skb;
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	return NULL;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
gred_reset(struct Qdisc * sch)2931da177e4SLinus Torvalds static void gred_reset(struct Qdisc *sch)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	int i;
2961da177e4SLinus Torvalds 	struct gred_sched *t = qdisc_priv(sch);
2971da177e4SLinus Torvalds 
298edf7a7b1SThomas Graf 	qdisc_reset_queue(sch);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	for (i = 0; i < t->DPs; i++) {
3011e4dfaf9SThomas Graf 		struct gred_sched_data *q = t->tab[i];
3021e4dfaf9SThomas Graf 
3031da177e4SLinus Torvalds 		if (!q)
3041da177e4SLinus Torvalds 			continue;
3051e4dfaf9SThomas Graf 
306eeca6688SEric Dumazet 		red_restart(&q->vars);
3071da177e4SLinus Torvalds 		q->backlog = 0;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
gred_offload(struct Qdisc * sch,enum tc_gred_command command)311890d8d23SJakub Kicinski static void gred_offload(struct Qdisc *sch, enum tc_gred_command command)
312890d8d23SJakub Kicinski {
313890d8d23SJakub Kicinski 	struct gred_sched *table = qdisc_priv(sch);
314890d8d23SJakub Kicinski 	struct net_device *dev = qdisc_dev(sch);
315f25c0515SArnd Bergmann 	struct tc_gred_qopt_offload *opt = table->opt;
316890d8d23SJakub Kicinski 
317890d8d23SJakub Kicinski 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
318890d8d23SJakub Kicinski 		return;
319890d8d23SJakub Kicinski 
320f25c0515SArnd Bergmann 	memset(opt, 0, sizeof(*opt));
321f25c0515SArnd Bergmann 	opt->command = command;
322f25c0515SArnd Bergmann 	opt->handle = sch->handle;
323f25c0515SArnd Bergmann 	opt->parent = sch->parent;
324f25c0515SArnd Bergmann 
325890d8d23SJakub Kicinski 	if (command == TC_GRED_REPLACE) {
326890d8d23SJakub Kicinski 		unsigned int i;
327890d8d23SJakub Kicinski 
328f25c0515SArnd Bergmann 		opt->set.grio_on = gred_rio_mode(table);
329f25c0515SArnd Bergmann 		opt->set.wred_on = gred_wred_mode(table);
330f25c0515SArnd Bergmann 		opt->set.dp_cnt = table->DPs;
331f25c0515SArnd Bergmann 		opt->set.dp_def = table->def;
332890d8d23SJakub Kicinski 
333890d8d23SJakub Kicinski 		for (i = 0; i < table->DPs; i++) {
334890d8d23SJakub Kicinski 			struct gred_sched_data *q = table->tab[i];
335890d8d23SJakub Kicinski 
336890d8d23SJakub Kicinski 			if (!q)
337890d8d23SJakub Kicinski 				continue;
338f25c0515SArnd Bergmann 			opt->set.tab[i].present = true;
339f25c0515SArnd Bergmann 			opt->set.tab[i].limit = q->limit;
340f25c0515SArnd Bergmann 			opt->set.tab[i].prio = q->prio;
341f25c0515SArnd Bergmann 			opt->set.tab[i].min = q->parms.qth_min >> q->parms.Wlog;
342f25c0515SArnd Bergmann 			opt->set.tab[i].max = q->parms.qth_max >> q->parms.Wlog;
343f25c0515SArnd Bergmann 			opt->set.tab[i].is_ecn = gred_use_ecn(q);
344f25c0515SArnd Bergmann 			opt->set.tab[i].is_harddrop = gred_use_harddrop(q);
345f25c0515SArnd Bergmann 			opt->set.tab[i].probability = q->parms.max_P;
346f25c0515SArnd Bergmann 			opt->set.tab[i].backlog = &q->backlog;
347890d8d23SJakub Kicinski 		}
348f25c0515SArnd Bergmann 		opt->set.qstats = &sch->qstats;
349890d8d23SJakub Kicinski 	}
350890d8d23SJakub Kicinski 
351f25c0515SArnd Bergmann 	dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_GRED, opt);
352890d8d23SJakub Kicinski }
353890d8d23SJakub Kicinski 
gred_offload_dump_stats(struct Qdisc * sch)354e49efd52SJakub Kicinski static int gred_offload_dump_stats(struct Qdisc *sch)
355e49efd52SJakub Kicinski {
356e49efd52SJakub Kicinski 	struct gred_sched *table = qdisc_priv(sch);
357e49efd52SJakub Kicinski 	struct tc_gred_qopt_offload *hw_stats;
358f56940daSAhmed S. Darwish 	u64 bytes = 0, packets = 0;
359e49efd52SJakub Kicinski 	unsigned int i;
360e49efd52SJakub Kicinski 	int ret;
361e49efd52SJakub Kicinski 
362e49efd52SJakub Kicinski 	hw_stats = kzalloc(sizeof(*hw_stats), GFP_KERNEL);
363e49efd52SJakub Kicinski 	if (!hw_stats)
364e49efd52SJakub Kicinski 		return -ENOMEM;
365e49efd52SJakub Kicinski 
366e49efd52SJakub Kicinski 	hw_stats->command = TC_GRED_STATS;
367e49efd52SJakub Kicinski 	hw_stats->handle = sch->handle;
368e49efd52SJakub Kicinski 	hw_stats->parent = sch->parent;
369e49efd52SJakub Kicinski 
37067c9e627SAhmed S. Darwish 	for (i = 0; i < MAX_DPs; i++) {
37150dc9a85SAhmed S. Darwish 		gnet_stats_basic_sync_init(&hw_stats->stats.bstats[i]);
372e49efd52SJakub Kicinski 		if (table->tab[i])
373e49efd52SJakub Kicinski 			hw_stats->stats.xstats[i] = &table->tab[i]->stats;
37467c9e627SAhmed S. Darwish 	}
375e49efd52SJakub Kicinski 
376e49efd52SJakub Kicinski 	ret = qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_GRED, hw_stats);
377e49efd52SJakub Kicinski 	/* Even if driver returns failure adjust the stats - in case offload
378e49efd52SJakub Kicinski 	 * ended but driver still wants to adjust the values.
379e49efd52SJakub Kicinski 	 */
380*339346d4SJakub Kicinski 	sch_tree_lock(sch);
381e49efd52SJakub Kicinski 	for (i = 0; i < MAX_DPs; i++) {
382e49efd52SJakub Kicinski 		if (!table->tab[i])
383e49efd52SJakub Kicinski 			continue;
38450dc9a85SAhmed S. Darwish 		table->tab[i]->packetsin += u64_stats_read(&hw_stats->stats.bstats[i].packets);
38550dc9a85SAhmed S. Darwish 		table->tab[i]->bytesin += u64_stats_read(&hw_stats->stats.bstats[i].bytes);
386e49efd52SJakub Kicinski 		table->tab[i]->backlog += hw_stats->stats.qstats[i].backlog;
387e49efd52SJakub Kicinski 
38850dc9a85SAhmed S. Darwish 		bytes += u64_stats_read(&hw_stats->stats.bstats[i].bytes);
38950dc9a85SAhmed S. Darwish 		packets += u64_stats_read(&hw_stats->stats.bstats[i].packets);
390e49efd52SJakub Kicinski 		sch->qstats.qlen += hw_stats->stats.qstats[i].qlen;
391e49efd52SJakub Kicinski 		sch->qstats.backlog += hw_stats->stats.qstats[i].backlog;
392e49efd52SJakub Kicinski 		sch->qstats.drops += hw_stats->stats.qstats[i].drops;
393e49efd52SJakub Kicinski 		sch->qstats.requeues += hw_stats->stats.qstats[i].requeues;
394e49efd52SJakub Kicinski 		sch->qstats.overlimits += hw_stats->stats.qstats[i].overlimits;
395e49efd52SJakub Kicinski 	}
396f56940daSAhmed S. Darwish 	_bstats_update(&sch->bstats, bytes, packets);
397*339346d4SJakub Kicinski 	sch_tree_unlock(sch);
398e49efd52SJakub Kicinski 
399e49efd52SJakub Kicinski 	kfree(hw_stats);
400e49efd52SJakub Kicinski 	return ret;
401e49efd52SJakub Kicinski }
402e49efd52SJakub Kicinski 
gred_destroy_vq(struct gred_sched_data * q)4036639607eSThomas Graf static inline void gred_destroy_vq(struct gred_sched_data *q)
4046639607eSThomas Graf {
4056639607eSThomas Graf 	kfree(q);
4066639607eSThomas Graf }
4076639607eSThomas Graf 
gred_change_table_def(struct Qdisc * sch,struct nlattr * dps,struct netlink_ext_ack * extack)4084777be08SJakub Kicinski static int gred_change_table_def(struct Qdisc *sch, struct nlattr *dps,
4094777be08SJakub Kicinski 				 struct netlink_ext_ack *extack)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	struct gred_sched *table = qdisc_priv(sch);
4121da177e4SLinus Torvalds 	struct tc_gred_sopt *sopt;
41325fc1989SJakub Kicinski 	bool red_flags_changed;
4146639607eSThomas Graf 	int i;
4151da177e4SLinus Torvalds 
416ac8ef4abSAlexander Aring 	if (!dps)
4171da177e4SLinus Torvalds 		return -EINVAL;
4181da177e4SLinus Torvalds 
4191e90474cSPatrick McHardy 	sopt = nla_data(dps);
4201da177e4SLinus Torvalds 
4214777be08SJakub Kicinski 	if (sopt->DPs > MAX_DPs) {
4224777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "number of virtual queues too high");
4231da177e4SLinus Torvalds 		return -EINVAL;
4244777be08SJakub Kicinski 	}
4254777be08SJakub Kicinski 	if (sopt->DPs == 0) {
4264777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
4274777be08SJakub Kicinski 				   "number of virtual queues can't be 0");
4284777be08SJakub Kicinski 		return -EINVAL;
4294777be08SJakub Kicinski 	}
4304777be08SJakub Kicinski 	if (sopt->def_DP >= sopt->DPs) {
4314777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "default virtual queue above virtual queue count");
4324777be08SJakub Kicinski 		return -EINVAL;
4334777be08SJakub Kicinski 	}
43472111015SJakub Kicinski 	if (sopt->flags && gred_per_vq_red_flags_used(table)) {
43572111015SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "can't set per-Qdisc RED flags when per-virtual queue flags are used");
43672111015SJakub Kicinski 		return -EINVAL;
43772111015SJakub Kicinski 	}
4381da177e4SLinus Torvalds 
4396639607eSThomas Graf 	sch_tree_lock(sch);
4401da177e4SLinus Torvalds 	table->DPs = sopt->DPs;
4411da177e4SLinus Torvalds 	table->def = sopt->def_DP;
44225fc1989SJakub Kicinski 	red_flags_changed = table->red_flags != sopt->flags;
443b38c7eefSThomas Graf 	table->red_flags = sopt->flags;
444dea3f628SThomas Graf 
4456639607eSThomas Graf 	/*
4466639607eSThomas Graf 	 * Every entry point to GRED is synchronized with the above code
4476639607eSThomas Graf 	 * and the DP is checked against DPs, i.e. shadowed VQs can no
4486639607eSThomas Graf 	 * longer be found so we can unlock right here.
4496639607eSThomas Graf 	 */
4506639607eSThomas Graf 	sch_tree_unlock(sch);
4516639607eSThomas Graf 
452dea3f628SThomas Graf 	if (sopt->grio) {
453d6fd4e96SThomas Graf 		gred_enable_rio_mode(table);
454dea3f628SThomas Graf 		gred_disable_wred_mode(table);
455dea3f628SThomas Graf 		if (gred_wred_mode_check(sch))
456dea3f628SThomas Graf 			gred_enable_wred_mode(table);
457dea3f628SThomas Graf 	} else {
458d6fd4e96SThomas Graf 		gred_disable_rio_mode(table);
459dea3f628SThomas Graf 		gred_disable_wred_mode(table);
460dea3f628SThomas Graf 	}
461dea3f628SThomas Graf 
46225fc1989SJakub Kicinski 	if (red_flags_changed)
46325fc1989SJakub Kicinski 		for (i = 0; i < table->DPs; i++)
46425fc1989SJakub Kicinski 			if (table->tab[i])
46525fc1989SJakub Kicinski 				table->tab[i]->red_flags =
46625fc1989SJakub Kicinski 					table->red_flags & GRED_VQ_RED_FLAGS;
46725fc1989SJakub Kicinski 
4686639607eSThomas Graf 	for (i = table->DPs; i < MAX_DPs; i++) {
4696639607eSThomas Graf 		if (table->tab[i]) {
470c17988a9SYang Yingliang 			pr_warn("GRED: Warning: Destroying shadowed VQ 0x%x\n",
471c17988a9SYang Yingliang 				i);
4726639607eSThomas Graf 			gred_destroy_vq(table->tab[i]);
4736639607eSThomas Graf 			table->tab[i] = NULL;
4746639607eSThomas Graf 		}
4756639607eSThomas Graf 	}
4766639607eSThomas Graf 
477890d8d23SJakub Kicinski 	gred_offload(sch, TC_GRED_REPLACE);
4781da177e4SLinus Torvalds 	return 0;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
gred_change_vq(struct Qdisc * sch,int dp,struct tc_gred_qopt * ctl,int prio,u8 * stab,u32 max_P,struct gred_sched_data ** prealloc,struct netlink_ext_ack * extack)481f62d6b93SThomas Graf static inline int gred_change_vq(struct Qdisc *sch, int dp,
482a73ed26bSEric Dumazet 				 struct tc_gred_qopt *ctl, int prio,
483869aa410SEric Dumazet 				 u8 *stab, u32 max_P,
4844777be08SJakub Kicinski 				 struct gred_sched_data **prealloc,
4854777be08SJakub Kicinski 				 struct netlink_ext_ack *extack)
4866639607eSThomas Graf {
4876639607eSThomas Graf 	struct gred_sched *table = qdisc_priv(sch);
488869aa410SEric Dumazet 	struct gred_sched_data *q = table->tab[dp];
4896639607eSThomas Graf 
490e323d865SEric Dumazet 	if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Scell_log, stab)) {
4914777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "invalid RED parameters");
4928afa10cbSNogah Frankel 		return -EINVAL;
4934777be08SJakub Kicinski 	}
4948afa10cbSNogah Frankel 
495869aa410SEric Dumazet 	if (!q) {
496869aa410SEric Dumazet 		table->tab[dp] = q = *prealloc;
497869aa410SEric Dumazet 		*prealloc = NULL;
498869aa410SEric Dumazet 		if (!q)
4991da177e4SLinus Torvalds 			return -ENOMEM;
50025fc1989SJakub Kicinski 		q->red_flags = table->red_flags & GRED_VQ_RED_FLAGS;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds 
503f62d6b93SThomas Graf 	q->DP = dp;
504f62d6b93SThomas Graf 	q->prio = prio;
505a3eb95f8SDavid Ward 	if (ctl->limit > sch->limit)
506a3eb95f8SDavid Ward 		q->limit = sch->limit;
507a3eb95f8SDavid Ward 	else
5081da177e4SLinus Torvalds 		q->limit = ctl->limit;
5091da177e4SLinus Torvalds 
51022b33429SThomas Graf 	if (q->backlog == 0)
511eeca6688SEric Dumazet 		red_end_of_idle_period(&q->vars);
51222b33429SThomas Graf 
51322b33429SThomas Graf 	red_set_parms(&q->parms,
51422b33429SThomas Graf 		      ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
515a73ed26bSEric Dumazet 		      ctl->Scell_log, stab, max_P);
516eeca6688SEric Dumazet 	red_set_vars(&q->vars);
517f62d6b93SThomas Graf 	return 0;
518f62d6b93SThomas Graf }
519f62d6b93SThomas Graf 
52072111015SJakub Kicinski static const struct nla_policy gred_vq_policy[TCA_GRED_VQ_MAX + 1] = {
52172111015SJakub Kicinski 	[TCA_GRED_VQ_DP]	= { .type = NLA_U32 },
52272111015SJakub Kicinski 	[TCA_GRED_VQ_FLAGS]	= { .type = NLA_U32 },
52372111015SJakub Kicinski };
52472111015SJakub Kicinski 
52572111015SJakub Kicinski static const struct nla_policy gred_vqe_policy[TCA_GRED_VQ_ENTRY_MAX + 1] = {
52672111015SJakub Kicinski 	[TCA_GRED_VQ_ENTRY]	= { .type = NLA_NESTED },
52772111015SJakub Kicinski };
52872111015SJakub Kicinski 
52927a3421eSPatrick McHardy static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
53027a3421eSPatrick McHardy 	[TCA_GRED_PARMS]	= { .len = sizeof(struct tc_gred_qopt) },
53127a3421eSPatrick McHardy 	[TCA_GRED_STAB]		= { .len = 256 },
53227a3421eSPatrick McHardy 	[TCA_GRED_DPS]		= { .len = sizeof(struct tc_gred_sopt) },
533a73ed26bSEric Dumazet 	[TCA_GRED_MAX_P]	= { .type = NLA_U32 },
534a3eb95f8SDavid Ward 	[TCA_GRED_LIMIT]	= { .type = NLA_U32 },
53572111015SJakub Kicinski 	[TCA_GRED_VQ_LIST]	= { .type = NLA_NESTED },
53627a3421eSPatrick McHardy };
53727a3421eSPatrick McHardy 
gred_vq_apply(struct gred_sched * table,const struct nlattr * entry)53872111015SJakub Kicinski static void gred_vq_apply(struct gred_sched *table, const struct nlattr *entry)
53972111015SJakub Kicinski {
54072111015SJakub Kicinski 	struct nlattr *tb[TCA_GRED_VQ_MAX + 1];
54172111015SJakub Kicinski 	u32 dp;
54272111015SJakub Kicinski 
5438cb08174SJohannes Berg 	nla_parse_nested_deprecated(tb, TCA_GRED_VQ_MAX, entry,
5448cb08174SJohannes Berg 				    gred_vq_policy, NULL);
54572111015SJakub Kicinski 
54672111015SJakub Kicinski 	dp = nla_get_u32(tb[TCA_GRED_VQ_DP]);
54772111015SJakub Kicinski 
54872111015SJakub Kicinski 	if (tb[TCA_GRED_VQ_FLAGS])
54972111015SJakub Kicinski 		table->tab[dp]->red_flags = nla_get_u32(tb[TCA_GRED_VQ_FLAGS]);
55072111015SJakub Kicinski }
55172111015SJakub Kicinski 
gred_vqs_apply(struct gred_sched * table,struct nlattr * vqs)55272111015SJakub Kicinski static void gred_vqs_apply(struct gred_sched *table, struct nlattr *vqs)
55372111015SJakub Kicinski {
55472111015SJakub Kicinski 	const struct nlattr *attr;
55572111015SJakub Kicinski 	int rem;
55672111015SJakub Kicinski 
55772111015SJakub Kicinski 	nla_for_each_nested(attr, vqs, rem) {
55872111015SJakub Kicinski 		switch (nla_type(attr)) {
55972111015SJakub Kicinski 		case TCA_GRED_VQ_ENTRY:
56072111015SJakub Kicinski 			gred_vq_apply(table, attr);
56172111015SJakub Kicinski 			break;
56272111015SJakub Kicinski 		}
56372111015SJakub Kicinski 	}
56472111015SJakub Kicinski }
56572111015SJakub Kicinski 
gred_vq_validate(struct gred_sched * table,u32 cdp,const struct nlattr * entry,struct netlink_ext_ack * extack)56672111015SJakub Kicinski static int gred_vq_validate(struct gred_sched *table, u32 cdp,
56772111015SJakub Kicinski 			    const struct nlattr *entry,
56872111015SJakub Kicinski 			    struct netlink_ext_ack *extack)
56972111015SJakub Kicinski {
57072111015SJakub Kicinski 	struct nlattr *tb[TCA_GRED_VQ_MAX + 1];
57172111015SJakub Kicinski 	int err;
57272111015SJakub Kicinski 	u32 dp;
57372111015SJakub Kicinski 
5748cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_GRED_VQ_MAX, entry,
5758cb08174SJohannes Berg 					  gred_vq_policy, extack);
57672111015SJakub Kicinski 	if (err < 0)
57772111015SJakub Kicinski 		return err;
57872111015SJakub Kicinski 
57972111015SJakub Kicinski 	if (!tb[TCA_GRED_VQ_DP]) {
58072111015SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Virtual queue with no index specified");
58172111015SJakub Kicinski 		return -EINVAL;
58272111015SJakub Kicinski 	}
58372111015SJakub Kicinski 	dp = nla_get_u32(tb[TCA_GRED_VQ_DP]);
58472111015SJakub Kicinski 	if (dp >= table->DPs) {
58572111015SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Virtual queue with index out of bounds");
58672111015SJakub Kicinski 		return -EINVAL;
58772111015SJakub Kicinski 	}
58872111015SJakub Kicinski 	if (dp != cdp && !table->tab[dp]) {
58972111015SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Virtual queue not yet instantiated");
59072111015SJakub Kicinski 		return -EINVAL;
59172111015SJakub Kicinski 	}
59272111015SJakub Kicinski 
59372111015SJakub Kicinski 	if (tb[TCA_GRED_VQ_FLAGS]) {
59472111015SJakub Kicinski 		u32 red_flags = nla_get_u32(tb[TCA_GRED_VQ_FLAGS]);
59572111015SJakub Kicinski 
59672111015SJakub Kicinski 		if (table->red_flags && table->red_flags != red_flags) {
59772111015SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "can't change per-virtual queue RED flags when per-Qdisc flags are used");
59872111015SJakub Kicinski 			return -EINVAL;
59972111015SJakub Kicinski 		}
60072111015SJakub Kicinski 		if (red_flags & ~GRED_VQ_RED_FLAGS) {
60172111015SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack,
60272111015SJakub Kicinski 					   "invalid RED flags specified");
60372111015SJakub Kicinski 			return -EINVAL;
60472111015SJakub Kicinski 		}
60572111015SJakub Kicinski 	}
60672111015SJakub Kicinski 
60772111015SJakub Kicinski 	return 0;
60872111015SJakub Kicinski }
60972111015SJakub Kicinski 
gred_vqs_validate(struct gred_sched * table,u32 cdp,struct nlattr * vqs,struct netlink_ext_ack * extack)61072111015SJakub Kicinski static int gred_vqs_validate(struct gred_sched *table, u32 cdp,
61172111015SJakub Kicinski 			     struct nlattr *vqs, struct netlink_ext_ack *extack)
61272111015SJakub Kicinski {
61372111015SJakub Kicinski 	const struct nlattr *attr;
61472111015SJakub Kicinski 	int rem, err;
61572111015SJakub Kicinski 
6168cb08174SJohannes Berg 	err = nla_validate_nested_deprecated(vqs, TCA_GRED_VQ_ENTRY_MAX,
61772111015SJakub Kicinski 					     gred_vqe_policy, extack);
61872111015SJakub Kicinski 	if (err < 0)
61972111015SJakub Kicinski 		return err;
62072111015SJakub Kicinski 
62172111015SJakub Kicinski 	nla_for_each_nested(attr, vqs, rem) {
62272111015SJakub Kicinski 		switch (nla_type(attr)) {
62372111015SJakub Kicinski 		case TCA_GRED_VQ_ENTRY:
62472111015SJakub Kicinski 			err = gred_vq_validate(table, cdp, attr, extack);
62572111015SJakub Kicinski 			if (err)
62672111015SJakub Kicinski 				return err;
62772111015SJakub Kicinski 			break;
62872111015SJakub Kicinski 		default:
62972111015SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "GRED_VQ_LIST can contain only entry attributes");
63072111015SJakub Kicinski 			return -EINVAL;
63172111015SJakub Kicinski 		}
63272111015SJakub Kicinski 	}
63372111015SJakub Kicinski 
63472111015SJakub Kicinski 	if (rem > 0) {
63572111015SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Trailing data after parsing virtual queue list");
63672111015SJakub Kicinski 		return -EINVAL;
63772111015SJakub Kicinski 	}
63872111015SJakub Kicinski 
63972111015SJakub Kicinski 	return 0;
64072111015SJakub Kicinski }
64172111015SJakub Kicinski 
gred_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)6422030721cSAlexander Aring static int gred_change(struct Qdisc *sch, struct nlattr *opt,
6432030721cSAlexander Aring 		       struct netlink_ext_ack *extack)
644f62d6b93SThomas Graf {
645f62d6b93SThomas Graf 	struct gred_sched *table = qdisc_priv(sch);
646f62d6b93SThomas Graf 	struct tc_gred_qopt *ctl;
6471e90474cSPatrick McHardy 	struct nlattr *tb[TCA_GRED_MAX + 1];
648cee63723SPatrick McHardy 	int err, prio = GRED_DEF_PRIO;
649f62d6b93SThomas Graf 	u8 *stab;
650a73ed26bSEric Dumazet 	u32 max_P;
651869aa410SEric Dumazet 	struct gred_sched_data *prealloc;
652f62d6b93SThomas Graf 
6538cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_GRED_MAX, opt, gred_policy,
6548cb08174SJohannes Berg 					  extack);
655cee63723SPatrick McHardy 	if (err < 0)
656cee63723SPatrick McHardy 		return err;
657cee63723SPatrick McHardy 
658a3eb95f8SDavid Ward 	if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL) {
659a3eb95f8SDavid Ward 		if (tb[TCA_GRED_LIMIT] != NULL)
660a3eb95f8SDavid Ward 			sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
6614777be08SJakub Kicinski 		return gred_change_table_def(sch, tb[TCA_GRED_DPS], extack);
662a3eb95f8SDavid Ward 	}
663f62d6b93SThomas Graf 
6641e90474cSPatrick McHardy 	if (tb[TCA_GRED_PARMS] == NULL ||
665a3eb95f8SDavid Ward 	    tb[TCA_GRED_STAB] == NULL ||
6664777be08SJakub Kicinski 	    tb[TCA_GRED_LIMIT] != NULL) {
6674777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "can't configure Qdisc and virtual queue at the same time");
668f62d6b93SThomas Graf 		return -EINVAL;
6694777be08SJakub Kicinski 	}
670f62d6b93SThomas Graf 
671a73ed26bSEric Dumazet 	max_P = tb[TCA_GRED_MAX_P] ? nla_get_u32(tb[TCA_GRED_MAX_P]) : 0;
672a73ed26bSEric Dumazet 
6731e90474cSPatrick McHardy 	ctl = nla_data(tb[TCA_GRED_PARMS]);
6741e90474cSPatrick McHardy 	stab = nla_data(tb[TCA_GRED_STAB]);
675f62d6b93SThomas Graf 
6764777be08SJakub Kicinski 	if (ctl->DP >= table->DPs) {
6774777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "virtual queue index above virtual queue count");
678255f4803SJakub Kicinski 		return -EINVAL;
6794777be08SJakub Kicinski 	}
680f62d6b93SThomas Graf 
68172111015SJakub Kicinski 	if (tb[TCA_GRED_VQ_LIST]) {
68272111015SJakub Kicinski 		err = gred_vqs_validate(table, ctl->DP, tb[TCA_GRED_VQ_LIST],
68372111015SJakub Kicinski 					extack);
68472111015SJakub Kicinski 		if (err)
68572111015SJakub Kicinski 			return err;
68672111015SJakub Kicinski 	}
68772111015SJakub Kicinski 
688f62d6b93SThomas Graf 	if (gred_rio_mode(table)) {
689f62d6b93SThomas Graf 		if (ctl->prio == 0) {
690f62d6b93SThomas Graf 			int def_prio = GRED_DEF_PRIO;
691f62d6b93SThomas Graf 
692f62d6b93SThomas Graf 			if (table->tab[table->def])
693f62d6b93SThomas Graf 				def_prio = table->tab[table->def]->prio;
694f62d6b93SThomas Graf 
695f62d6b93SThomas Graf 			printk(KERN_DEBUG "GRED: DP %u does not have a prio "
696f62d6b93SThomas Graf 			       "setting default to %d\n", ctl->DP, def_prio);
697f62d6b93SThomas Graf 
698f62d6b93SThomas Graf 			prio = def_prio;
699f62d6b93SThomas Graf 		} else
700f62d6b93SThomas Graf 			prio = ctl->prio;
701f62d6b93SThomas Graf 	}
702f62d6b93SThomas Graf 
703869aa410SEric Dumazet 	prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
704f62d6b93SThomas Graf 	sch_tree_lock(sch);
705f62d6b93SThomas Graf 
7064777be08SJakub Kicinski 	err = gred_change_vq(sch, ctl->DP, ctl, prio, stab, max_P, &prealloc,
7074777be08SJakub Kicinski 			     extack);
708f62d6b93SThomas Graf 	if (err < 0)
709255f4803SJakub Kicinski 		goto err_unlock_free;
710f62d6b93SThomas Graf 
71172111015SJakub Kicinski 	if (tb[TCA_GRED_VQ_LIST])
71272111015SJakub Kicinski 		gred_vqs_apply(table, tb[TCA_GRED_VQ_LIST]);
71372111015SJakub Kicinski 
714d6fd4e96SThomas Graf 	if (gred_rio_mode(table)) {
715dea3f628SThomas Graf 		gred_disable_wred_mode(table);
716dea3f628SThomas Graf 		if (gred_wred_mode_check(sch))
717dea3f628SThomas Graf 			gred_enable_wred_mode(table);
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
720f62d6b93SThomas Graf 	sch_tree_unlock(sch);
721869aa410SEric Dumazet 	kfree(prealloc);
722890d8d23SJakub Kicinski 
723890d8d23SJakub Kicinski 	gred_offload(sch, TC_GRED_REPLACE);
724255f4803SJakub Kicinski 	return 0;
725255f4803SJakub Kicinski 
726255f4803SJakub Kicinski err_unlock_free:
727255f4803SJakub Kicinski 	sch_tree_unlock(sch);
728255f4803SJakub Kicinski 	kfree(prealloc);
729f62d6b93SThomas Graf 	return err;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
gred_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)732e63d7dfdSAlexander Aring static int gred_init(struct Qdisc *sch, struct nlattr *opt,
733e63d7dfdSAlexander Aring 		     struct netlink_ext_ack *extack)
7341da177e4SLinus Torvalds {
735f25c0515SArnd Bergmann 	struct gred_sched *table = qdisc_priv(sch);
7361e90474cSPatrick McHardy 	struct nlattr *tb[TCA_GRED_MAX + 1];
737cee63723SPatrick McHardy 	int err;
7381da177e4SLinus Torvalds 
739ac8ef4abSAlexander Aring 	if (!opt)
7401da177e4SLinus Torvalds 		return -EINVAL;
7411da177e4SLinus Torvalds 
7428cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_GRED_MAX, opt, gred_policy,
7438cb08174SJohannes Berg 					  extack);
744cee63723SPatrick McHardy 	if (err < 0)
745cee63723SPatrick McHardy 		return err;
746cee63723SPatrick McHardy 
7474777be08SJakub Kicinski 	if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB]) {
7484777be08SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
7494777be08SJakub Kicinski 				   "virtual queue configuration can't be specified at initialization time");
7501da177e4SLinus Torvalds 		return -EINVAL;
7514777be08SJakub Kicinski 	}
7521da177e4SLinus Torvalds 
753a3eb95f8SDavid Ward 	if (tb[TCA_GRED_LIMIT])
754a3eb95f8SDavid Ward 		sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
755348e3435SPhil Sutter 	else
756348e3435SPhil Sutter 		sch->limit = qdisc_dev(sch)->tx_queue_len
757348e3435SPhil Sutter 		             * psched_mtu(qdisc_dev(sch));
758a3eb95f8SDavid Ward 
759f25c0515SArnd Bergmann 	if (qdisc_dev(sch)->netdev_ops->ndo_setup_tc) {
760f25c0515SArnd Bergmann 		table->opt = kzalloc(sizeof(*table->opt), GFP_KERNEL);
761f25c0515SArnd Bergmann 		if (!table->opt)
762f25c0515SArnd Bergmann 			return -ENOMEM;
763f25c0515SArnd Bergmann 	}
764f25c0515SArnd Bergmann 
7654777be08SJakub Kicinski 	return gred_change_table_def(sch, tb[TCA_GRED_DPS], extack);
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
gred_dump(struct Qdisc * sch,struct sk_buff * skb)7681da177e4SLinus Torvalds static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	struct gred_sched *table = qdisc_priv(sch);
77180e22e96SJakub Kicinski 	struct nlattr *parms, *vqs, *opts = NULL;
7721da177e4SLinus Torvalds 	int i;
773a73ed26bSEric Dumazet 	u32 max_p[MAX_DPs];
774e0636822SThomas Graf 	struct tc_gred_sopt sopt = {
775e0636822SThomas Graf 		.DPs	= table->DPs,
776e0636822SThomas Graf 		.def_DP	= table->def,
777e0636822SThomas Graf 		.grio	= gred_rio_mode(table),
778b38c7eefSThomas Graf 		.flags	= table->red_flags,
779e0636822SThomas Graf 	};
7801da177e4SLinus Torvalds 
781e49efd52SJakub Kicinski 	if (gred_offload_dump_stats(sch))
782e49efd52SJakub Kicinski 		goto nla_put_failure;
783e49efd52SJakub Kicinski 
784ae0be8deSMichal Kubecek 	opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
7851e90474cSPatrick McHardy 	if (opts == NULL)
7861e90474cSPatrick McHardy 		goto nla_put_failure;
7871b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_GRED_DPS, sizeof(sopt), &sopt))
7881b34ec43SDavid S. Miller 		goto nla_put_failure;
789a73ed26bSEric Dumazet 
790a73ed26bSEric Dumazet 	for (i = 0; i < MAX_DPs; i++) {
791a73ed26bSEric Dumazet 		struct gred_sched_data *q = table->tab[i];
792a73ed26bSEric Dumazet 
793a73ed26bSEric Dumazet 		max_p[i] = q ? q->parms.max_P : 0;
794a73ed26bSEric Dumazet 	}
7951b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_GRED_MAX_P, sizeof(max_p), max_p))
7961b34ec43SDavid S. Miller 		goto nla_put_failure;
797a73ed26bSEric Dumazet 
798a3eb95f8SDavid Ward 	if (nla_put_u32(skb, TCA_GRED_LIMIT, sch->limit))
799a3eb95f8SDavid Ward 		goto nla_put_failure;
800a3eb95f8SDavid Ward 
80180e22e96SJakub Kicinski 	/* Old style all-in-one dump of VQs */
802ae0be8deSMichal Kubecek 	parms = nla_nest_start_noflag(skb, TCA_GRED_PARMS);
8031e90474cSPatrick McHardy 	if (parms == NULL)
8041e90474cSPatrick McHardy 		goto nla_put_failure;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	for (i = 0; i < MAX_DPs; i++) {
80705f1cc01SThomas Graf 		struct gred_sched_data *q = table->tab[i];
80805f1cc01SThomas Graf 		struct tc_gred_qopt opt;
8091fe37b10SDavid Ward 		unsigned long qavg;
81005f1cc01SThomas Graf 
81105f1cc01SThomas Graf 		memset(&opt, 0, sizeof(opt));
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		if (!q) {
8141da177e4SLinus Torvalds 			/* hack -- fix at some point with proper message
8151da177e4SLinus Torvalds 			   This is how we indicate to tc that there is no VQ
8161da177e4SLinus Torvalds 			   at this DP */
8171da177e4SLinus Torvalds 
81805f1cc01SThomas Graf 			opt.DP = MAX_DPs + i;
81905f1cc01SThomas Graf 			goto append_opt;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 
82205f1cc01SThomas Graf 		opt.limit	= q->limit;
82305f1cc01SThomas Graf 		opt.DP		= q->DP;
824145a42b3SDavid Ward 		opt.backlog	= gred_backlog(table, q, sch);
82505f1cc01SThomas Graf 		opt.prio	= q->prio;
82622b33429SThomas Graf 		opt.qth_min	= q->parms.qth_min >> q->parms.Wlog;
82722b33429SThomas Graf 		opt.qth_max	= q->parms.qth_max >> q->parms.Wlog;
82822b33429SThomas Graf 		opt.Wlog	= q->parms.Wlog;
82922b33429SThomas Graf 		opt.Plog	= q->parms.Plog;
83022b33429SThomas Graf 		opt.Scell_log	= q->parms.Scell_log;
83122b33429SThomas Graf 		opt.early	= q->stats.prob_drop;
83222b33429SThomas Graf 		opt.forced	= q->stats.forced_drop;
83322b33429SThomas Graf 		opt.pdrop	= q->stats.pdrop;
83405f1cc01SThomas Graf 		opt.packets	= q->packetsin;
83505f1cc01SThomas Graf 		opt.bytesin	= q->bytesin;
83605f1cc01SThomas Graf 
837244b65dbSDavid Ward 		if (gred_wred_mode(table))
838244b65dbSDavid Ward 			gred_load_wred_set(table, q);
8391da177e4SLinus Torvalds 
8401fe37b10SDavid Ward 		qavg = red_calc_qavg(&q->parms, &q->vars,
8411fe37b10SDavid Ward 				     q->vars.qavg >> q->parms.Wlog);
8421fe37b10SDavid Ward 		opt.qave = qavg >> q->parms.Wlog;
8431da177e4SLinus Torvalds 
84405f1cc01SThomas Graf append_opt:
8451e90474cSPatrick McHardy 		if (nla_append(skb, sizeof(opt), &opt) < 0)
8461e90474cSPatrick McHardy 			goto nla_put_failure;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491e90474cSPatrick McHardy 	nla_nest_end(skb, parms);
8501da177e4SLinus Torvalds 
85180e22e96SJakub Kicinski 	/* Dump the VQs again, in more structured way */
852ae0be8deSMichal Kubecek 	vqs = nla_nest_start_noflag(skb, TCA_GRED_VQ_LIST);
85380e22e96SJakub Kicinski 	if (!vqs)
85480e22e96SJakub Kicinski 		goto nla_put_failure;
85580e22e96SJakub Kicinski 
85680e22e96SJakub Kicinski 	for (i = 0; i < MAX_DPs; i++) {
85780e22e96SJakub Kicinski 		struct gred_sched_data *q = table->tab[i];
85880e22e96SJakub Kicinski 		struct nlattr *vq;
85980e22e96SJakub Kicinski 
86080e22e96SJakub Kicinski 		if (!q)
86180e22e96SJakub Kicinski 			continue;
86280e22e96SJakub Kicinski 
863ae0be8deSMichal Kubecek 		vq = nla_nest_start_noflag(skb, TCA_GRED_VQ_ENTRY);
86480e22e96SJakub Kicinski 		if (!vq)
86580e22e96SJakub Kicinski 			goto nla_put_failure;
86680e22e96SJakub Kicinski 
86780e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_DP, q->DP))
86880e22e96SJakub Kicinski 			goto nla_put_failure;
86980e22e96SJakub Kicinski 
87072111015SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_FLAGS, q->red_flags))
87172111015SJakub Kicinski 			goto nla_put_failure;
87272111015SJakub Kicinski 
87380e22e96SJakub Kicinski 		/* Stats */
87480e22e96SJakub Kicinski 		if (nla_put_u64_64bit(skb, TCA_GRED_VQ_STAT_BYTES, q->bytesin,
87580e22e96SJakub Kicinski 				      TCA_GRED_VQ_PAD))
87680e22e96SJakub Kicinski 			goto nla_put_failure;
87780e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_PACKETS, q->packetsin))
87880e22e96SJakub Kicinski 			goto nla_put_failure;
87980e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_BACKLOG,
88080e22e96SJakub Kicinski 				gred_backlog(table, q, sch)))
88180e22e96SJakub Kicinski 			goto nla_put_failure;
88280e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_PROB_DROP,
88380e22e96SJakub Kicinski 				q->stats.prob_drop))
88480e22e96SJakub Kicinski 			goto nla_put_failure;
88580e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_PROB_MARK,
88680e22e96SJakub Kicinski 				q->stats.prob_mark))
88780e22e96SJakub Kicinski 			goto nla_put_failure;
88880e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_FORCED_DROP,
88980e22e96SJakub Kicinski 				q->stats.forced_drop))
89080e22e96SJakub Kicinski 			goto nla_put_failure;
89180e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_FORCED_MARK,
89280e22e96SJakub Kicinski 				q->stats.forced_mark))
89380e22e96SJakub Kicinski 			goto nla_put_failure;
89480e22e96SJakub Kicinski 		if (nla_put_u32(skb, TCA_GRED_VQ_STAT_PDROP, q->stats.pdrop))
89580e22e96SJakub Kicinski 			goto nla_put_failure;
89680e22e96SJakub Kicinski 
89780e22e96SJakub Kicinski 		nla_nest_end(skb, vq);
89880e22e96SJakub Kicinski 	}
89980e22e96SJakub Kicinski 	nla_nest_end(skb, vqs);
90080e22e96SJakub Kicinski 
9011e90474cSPatrick McHardy 	return nla_nest_end(skb, opts);
9021da177e4SLinus Torvalds 
9031e90474cSPatrick McHardy nla_put_failure:
904bc3ed28cSThomas Graf 	nla_nest_cancel(skb, opts);
905bc3ed28cSThomas Graf 	return -EMSGSIZE;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
gred_destroy(struct Qdisc * sch)9081da177e4SLinus Torvalds static void gred_destroy(struct Qdisc *sch)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	struct gred_sched *table = qdisc_priv(sch);
9111da177e4SLinus Torvalds 	int i;
9121da177e4SLinus Torvalds 
9134bf8594aSZhengchao Shao 	for (i = 0; i < table->DPs; i++)
9146639607eSThomas Graf 		gred_destroy_vq(table->tab[i]);
9154bf8594aSZhengchao Shao 
916890d8d23SJakub Kicinski 	gred_offload(sch, TC_GRED_DESTROY);
917f25c0515SArnd Bergmann 	kfree(table->opt);
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
92020fea08bSEric Dumazet static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
9211da177e4SLinus Torvalds 	.id		=	"gred",
9221da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct gred_sched),
9231da177e4SLinus Torvalds 	.enqueue	=	gred_enqueue,
9241da177e4SLinus Torvalds 	.dequeue	=	gred_dequeue,
9258e3af978SJarek Poplawski 	.peek		=	qdisc_peek_head,
9261da177e4SLinus Torvalds 	.init		=	gred_init,
9271da177e4SLinus Torvalds 	.reset		=	gred_reset,
9281da177e4SLinus Torvalds 	.destroy	=	gred_destroy,
9291da177e4SLinus Torvalds 	.change		=	gred_change,
9301da177e4SLinus Torvalds 	.dump		=	gred_dump,
9311da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
9321da177e4SLinus Torvalds };
9331da177e4SLinus Torvalds 
gred_module_init(void)9341da177e4SLinus Torvalds static int __init gred_module_init(void)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	return register_qdisc(&gred_qdisc_ops);
9371da177e4SLinus Torvalds }
9381e4dfaf9SThomas Graf 
gred_module_exit(void)9391da177e4SLinus Torvalds static void __exit gred_module_exit(void)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	unregister_qdisc(&gred_qdisc_ops);
9421da177e4SLinus Torvalds }
9431e4dfaf9SThomas Graf 
9441da177e4SLinus Torvalds module_init(gred_module_init)
9451da177e4SLinus Torvalds module_exit(gred_module_exit)
9461e4dfaf9SThomas Graf 
9471da177e4SLinus Torvalds MODULE_LICENSE("GPL");
948