1*4882a593Smuzhiyun #ifndef __NET_SCHED_CODEL_H
2*4882a593Smuzhiyun #define __NET_SCHED_CODEL_H
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun * Codel - The Controlled-Delay Active Queue Management algorithm
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
8*4882a593Smuzhiyun * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
9*4882a593Smuzhiyun * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
10*4882a593Smuzhiyun * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
13*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
14*4882a593Smuzhiyun * are met:
15*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
16*4882a593Smuzhiyun * notice, this list of conditions, and the following disclaimer,
17*4882a593Smuzhiyun * without modification.
18*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
19*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
20*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
21*4882a593Smuzhiyun * 3. The names of the authors may not be used to endorse or promote products
22*4882a593Smuzhiyun * derived from this software without specific prior written permission.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Alternatively, provided that this notice is retained in full, this
25*4882a593Smuzhiyun * software may be distributed under the terms of the GNU General
26*4882a593Smuzhiyun * Public License ("GPL") version 2, in which case the provisions of the
27*4882a593Smuzhiyun * GPL apply INSTEAD OF those given above.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40*4882a593Smuzhiyun * DAMAGE.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #include <linux/types.h>
45*4882a593Smuzhiyun #include <linux/ktime.h>
46*4882a593Smuzhiyun #include <linux/skbuff.h>
47*4882a593Smuzhiyun #include <net/pkt_sched.h>
48*4882a593Smuzhiyun #include <net/inet_ecn.h>
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Controlling Queue Delay (CoDel) algorithm
51*4882a593Smuzhiyun * =========================================
52*4882a593Smuzhiyun * Source : Kathleen Nichols and Van Jacobson
53*4882a593Smuzhiyun * http://queue.acm.org/detail.cfm?id=2209336
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * Implemented on linux by Dave Taht and Eric Dumazet
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* CoDel uses a 1024 nsec clock, encoded in u32
60*4882a593Smuzhiyun * This gives a range of 2199 seconds, because of signed compares
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun typedef u32 codel_time_t;
63*4882a593Smuzhiyun typedef s32 codel_tdiff_t;
64*4882a593Smuzhiyun #define CODEL_SHIFT 10
65*4882a593Smuzhiyun #define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
66*4882a593Smuzhiyun
codel_get_time(void)67*4882a593Smuzhiyun static inline codel_time_t codel_get_time(void)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun u64 ns = ktime_get_ns();
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return ns >> CODEL_SHIFT;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
75*4882a593Smuzhiyun * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
76*4882a593Smuzhiyun * codel_time_after(a,b) returns true if the time a is after time b.
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun #define codel_time_after(a, b) \
79*4882a593Smuzhiyun (typecheck(codel_time_t, a) && \
80*4882a593Smuzhiyun typecheck(codel_time_t, b) && \
81*4882a593Smuzhiyun ((s32)((a) - (b)) > 0))
82*4882a593Smuzhiyun #define codel_time_before(a, b) codel_time_after(b, a)
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #define codel_time_after_eq(a, b) \
85*4882a593Smuzhiyun (typecheck(codel_time_t, a) && \
86*4882a593Smuzhiyun typecheck(codel_time_t, b) && \
87*4882a593Smuzhiyun ((s32)((a) - (b)) >= 0))
88*4882a593Smuzhiyun #define codel_time_before_eq(a, b) codel_time_after_eq(b, a)
89*4882a593Smuzhiyun
codel_time_to_us(codel_time_t val)90*4882a593Smuzhiyun static inline u32 codel_time_to_us(codel_time_t val)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun u64 valns = ((u64)val << CODEL_SHIFT);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun do_div(valns, NSEC_PER_USEC);
95*4882a593Smuzhiyun return (u32)valns;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * struct codel_params - contains codel parameters
100*4882a593Smuzhiyun * @target: target queue size (in time units)
101*4882a593Smuzhiyun * @ce_threshold: threshold for marking packets with ECN CE
102*4882a593Smuzhiyun * @interval: width of moving time window
103*4882a593Smuzhiyun * @mtu: device mtu, or minimal queue backlog in bytes.
104*4882a593Smuzhiyun * @ecn: is Explicit Congestion Notification enabled
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun struct codel_params {
107*4882a593Smuzhiyun codel_time_t target;
108*4882a593Smuzhiyun codel_time_t ce_threshold;
109*4882a593Smuzhiyun codel_time_t interval;
110*4882a593Smuzhiyun u32 mtu;
111*4882a593Smuzhiyun bool ecn;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun * struct codel_vars - contains codel variables
116*4882a593Smuzhiyun * @count: how many drops we've done since the last time we
117*4882a593Smuzhiyun * entered dropping state
118*4882a593Smuzhiyun * @lastcount: count at entry to dropping state
119*4882a593Smuzhiyun * @dropping: set to true if in dropping state
120*4882a593Smuzhiyun * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
121*4882a593Smuzhiyun * @first_above_time: when we went (or will go) continuously above target
122*4882a593Smuzhiyun * for interval
123*4882a593Smuzhiyun * @drop_next: time to drop next packet, or when we dropped last
124*4882a593Smuzhiyun * @ldelay: sojourn time of last dequeued packet
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun struct codel_vars {
127*4882a593Smuzhiyun u32 count;
128*4882a593Smuzhiyun u32 lastcount;
129*4882a593Smuzhiyun bool dropping;
130*4882a593Smuzhiyun u16 rec_inv_sqrt;
131*4882a593Smuzhiyun codel_time_t first_above_time;
132*4882a593Smuzhiyun codel_time_t drop_next;
133*4882a593Smuzhiyun codel_time_t ldelay;
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
137*4882a593Smuzhiyun /* needed shift to get a Q0.32 number from rec_inv_sqrt */
138*4882a593Smuzhiyun #define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * struct codel_stats - contains codel shared variables and stats
142*4882a593Smuzhiyun * @maxpacket: largest packet we've seen so far
143*4882a593Smuzhiyun * @drop_count: temp count of dropped packets in dequeue()
144*4882a593Smuzhiyun * @drop_len: bytes of dropped packets in dequeue()
145*4882a593Smuzhiyun * ecn_mark: number of packets we ECN marked instead of dropping
146*4882a593Smuzhiyun * ce_mark: number of packets CE marked because sojourn time was above ce_threshold
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun struct codel_stats {
149*4882a593Smuzhiyun u32 maxpacket;
150*4882a593Smuzhiyun u32 drop_count;
151*4882a593Smuzhiyun u32 drop_len;
152*4882a593Smuzhiyun u32 ecn_mark;
153*4882a593Smuzhiyun u32 ce_mark;
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun #define CODEL_DISABLED_THRESHOLD INT_MAX
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun typedef u32 (*codel_skb_len_t)(const struct sk_buff *skb);
159*4882a593Smuzhiyun typedef codel_time_t (*codel_skb_time_t)(const struct sk_buff *skb);
160*4882a593Smuzhiyun typedef void (*codel_skb_drop_t)(struct sk_buff *skb, void *ctx);
161*4882a593Smuzhiyun typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
162*4882a593Smuzhiyun void *ctx);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #endif
165