blob: 1925c65e617b9eddae6e199f660a72232e750235 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __NET_PKT_SCHED_H
2#define __NET_PKT_SCHED_H
3
Patrick McHardy538e43a2006-01-08 22:12:03 -08004#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <net/sch_generic.h>
6
7struct qdisc_walker
8{
9 int stop;
10 int skip;
11 int count;
12 int (*fn)(struct Qdisc *, unsigned long cl, struct qdisc_walker *);
13};
14
15extern rwlock_t qdisc_tree_lock;
16
Thomas Graf3d54b822005-07-05 14:15:09 -070017#define QDISC_ALIGNTO 32
18#define QDISC_ALIGN(len) (((len) + QDISC_ALIGNTO-1) & ~(QDISC_ALIGNTO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20static inline void *qdisc_priv(struct Qdisc *q)
21{
Thomas Graf3d54b822005-07-05 14:15:09 -070022 return (char *) q + QDISC_ALIGN(sizeof(struct Qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
25/*
26 Timer resolution MUST BE < 10% of min_schedulable_packet_size/bandwidth
27
28 Normal IP packet size ~ 512byte, hence:
29
30 0.5Kbyte/1Mbyte/sec = 0.5msec, so that we need 50usec timer for
31 10Mbit ethernet.
32
33 10msec resolution -> <50Kbit/sec.
34
35 The result: [34]86 is not good choice for QoS router :-(
36
37 The things are not so bad, because we may use artifical
38 clock evaluated by integration of network data flow
39 in the most critical places.
40
41 Note: we do not use fastgettimeofday.
42 The reason is that, when it is not the same thing as
43 gettimeofday, it returns invalid timestamp, which is
44 not updated, when net_bh is active.
45 */
46
47/* General note about internal clock.
48
49 Any clock source returns time intervals, measured in units
50 close to 1usec. With source CONFIG_NET_SCH_CLK_GETTIMEOFDAY it is precisely
51 microseconds, otherwise something close but different chosen to minimize
52 arithmetic cost. Ratio usec/internal untis in form nominator/denominator
53 may be read from /proc/net/psched.
54 */
55
56
57#ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
58
59typedef struct timeval psched_time_t;
60typedef long psched_tdiff_t;
61
62#define PSCHED_GET_TIME(stamp) do_gettimeofday(&(stamp))
Patrick McHardy538e43a2006-01-08 22:12:03 -080063#define PSCHED_US2JIFFIE(usecs) usecs_to_jiffies(usecs)
64#define PSCHED_JIFFIE2US(delay) jiffies_to_usecs(delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#else /* !CONFIG_NET_SCH_CLK_GETTIMEOFDAY */
67
68typedef u64 psched_time_t;
69typedef long psched_tdiff_t;
70
71#ifdef CONFIG_NET_SCH_CLK_JIFFIES
72
73#if HZ < 96
74#define PSCHED_JSCALE 14
75#elif HZ >= 96 && HZ < 192
76#define PSCHED_JSCALE 13
77#elif HZ >= 192 && HZ < 384
78#define PSCHED_JSCALE 12
79#elif HZ >= 384 && HZ < 768
80#define PSCHED_JSCALE 11
81#elif HZ >= 768
82#define PSCHED_JSCALE 10
83#endif
84
85#define PSCHED_GET_TIME(stamp) ((stamp) = (get_jiffies_64()<<PSCHED_JSCALE))
86#define PSCHED_US2JIFFIE(delay) (((delay)+(1<<PSCHED_JSCALE)-1)>>PSCHED_JSCALE)
87#define PSCHED_JIFFIE2US(delay) ((delay)<<PSCHED_JSCALE)
88
89#endif /* CONFIG_NET_SCH_CLK_JIFFIES */
90#ifdef CONFIG_NET_SCH_CLK_CPU
91#include <asm/timex.h>
92
93extern psched_tdiff_t psched_clock_per_hz;
94extern int psched_clock_scale;
95extern psched_time_t psched_time_base;
96extern cycles_t psched_time_mark;
97
98#define PSCHED_GET_TIME(stamp) \
99do { \
100 cycles_t cur = get_cycles(); \
101 if (sizeof(cycles_t) == sizeof(u32)) { \
102 if (cur <= psched_time_mark) \
103 psched_time_base += 0x100000000ULL; \
104 psched_time_mark = cur; \
105 (stamp) = (psched_time_base + cur)>>psched_clock_scale; \
106 } else { \
107 (stamp) = cur>>psched_clock_scale; \
108 } \
109} while (0)
110#define PSCHED_US2JIFFIE(delay) (((delay)+psched_clock_per_hz-1)/psched_clock_per_hz)
111#define PSCHED_JIFFIE2US(delay) ((delay)*psched_clock_per_hz)
112
113#endif /* CONFIG_NET_SCH_CLK_CPU */
114
115#endif /* !CONFIG_NET_SCH_CLK_GETTIMEOFDAY */
116
117#ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
118#define PSCHED_TDIFF(tv1, tv2) \
119({ \
120 int __delta_sec = (tv1).tv_sec - (tv2).tv_sec; \
121 int __delta = (tv1).tv_usec - (tv2).tv_usec; \
122 if (__delta_sec) { \
123 switch (__delta_sec) { \
124 default: \
125 __delta = 0; \
126 case 2: \
Patrick McHardy538e43a2006-01-08 22:12:03 -0800127 __delta += USEC_PER_SEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 case 1: \
Patrick McHardy538e43a2006-01-08 22:12:03 -0800129 __delta += USEC_PER_SEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } \
131 } \
132 __delta; \
133})
134
135static inline int
136psched_tod_diff(int delta_sec, int bound)
137{
138 int delta;
139
Patrick McHardy538e43a2006-01-08 22:12:03 -0800140 if (bound <= USEC_PER_SEC || delta_sec > (0x7FFFFFFF/USEC_PER_SEC)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return bound;
Patrick McHardy538e43a2006-01-08 22:12:03 -0800142 delta = delta_sec * USEC_PER_SEC;
Nicolas Dichtel7b3c63a2005-04-28 12:14:37 -0700143 if (delta > bound || delta < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 delta = bound;
145 return delta;
146}
147
148#define PSCHED_TDIFF_SAFE(tv1, tv2, bound) \
149({ \
150 int __delta_sec = (tv1).tv_sec - (tv2).tv_sec; \
151 int __delta = (tv1).tv_usec - (tv2).tv_usec; \
152 switch (__delta_sec) { \
153 default: \
154 __delta = psched_tod_diff(__delta_sec, bound); break; \
155 case 2: \
Patrick McHardy538e43a2006-01-08 22:12:03 -0800156 __delta += USEC_PER_SEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case 1: \
Patrick McHardy538e43a2006-01-08 22:12:03 -0800158 __delta += USEC_PER_SEC; \
Nicolas Dichtel7b3c63a2005-04-28 12:14:37 -0700159 case 0: \
Patrick McHardy9dfa2772005-05-03 14:41:18 -0700160 if (__delta > bound || __delta < 0) \
161 __delta = bound; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } \
163 __delta; \
164})
165
166#define PSCHED_TLESS(tv1, tv2) (((tv1).tv_usec < (tv2).tv_usec && \
167 (tv1).tv_sec <= (tv2).tv_sec) || \
168 (tv1).tv_sec < (tv2).tv_sec)
169
170#define PSCHED_TADD2(tv, delta, tv_res) \
171({ \
Shuya MAEDA4ee303d2006-06-28 01:40:35 -0700172 int __delta = (delta); \
173 (tv_res) = (tv); \
174 while(__delta >= USEC_PER_SEC){ \
175 (tv_res).tv_sec++; \
176 __delta -= USEC_PER_SEC; \
177 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 (tv_res).tv_usec = __delta; \
179})
180
181#define PSCHED_TADD(tv, delta) \
182({ \
Shuya MAEDA4ee303d2006-06-28 01:40:35 -0700183 int __delta = (delta); \
184 while(__delta >= USEC_PER_SEC){ \
185 (tv).tv_sec++; \
186 __delta -= USEC_PER_SEC; \
187 } \
188 (tv).tv_usec = __delta; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189})
190
191/* Set/check that time is in the "past perfect";
192 it depends on concrete representation of system time
193 */
194
195#define PSCHED_SET_PASTPERFECT(t) ((t).tv_sec = 0)
196#define PSCHED_IS_PASTPERFECT(t) ((t).tv_sec == 0)
197
198#define PSCHED_AUDIT_TDIFF(t) ({ if ((t) > 2000000) (t) = 2000000; })
199
200#else /* !CONFIG_NET_SCH_CLK_GETTIMEOFDAY */
201
202#define PSCHED_TDIFF(tv1, tv2) (long)((tv1) - (tv2))
203#define PSCHED_TDIFF_SAFE(tv1, tv2, bound) \
204 min_t(long long, (tv1) - (tv2), bound)
205
206
207#define PSCHED_TLESS(tv1, tv2) ((tv1) < (tv2))
208#define PSCHED_TADD2(tv, delta, tv_res) ((tv_res) = (tv) + (delta))
209#define PSCHED_TADD(tv, delta) ((tv) += (delta))
210#define PSCHED_SET_PASTPERFECT(t) ((t) = 0)
211#define PSCHED_IS_PASTPERFECT(t) ((t) == 0)
212#define PSCHED_AUDIT_TDIFF(t)
213
214#endif /* !CONFIG_NET_SCH_CLK_GETTIMEOFDAY */
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216extern struct Qdisc_ops pfifo_qdisc_ops;
217extern struct Qdisc_ops bfifo_qdisc_ops;
218
219extern int register_qdisc(struct Qdisc_ops *qops);
220extern int unregister_qdisc(struct Qdisc_ops *qops);
221extern struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle);
222extern struct Qdisc *qdisc_lookup_class(struct net_device *dev, u32 handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223extern struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
224 struct rtattr *tab);
225extern void qdisc_put_rtab(struct qdisc_rate_table *tab);
226
Herbert Xu48d83322006-06-19 23:57:59 -0700227extern void __qdisc_run(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229static inline void qdisc_run(struct net_device *dev)
230{
Herbert Xu48d83322006-06-19 23:57:59 -0700231 if (!netif_queue_stopped(dev) &&
232 !test_and_set_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
233 __qdisc_run(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236extern int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
237 struct tcf_result *res);
238
239/* Calculate maximal size of packet seen by hard_start_xmit
240 routine of this device.
241 */
242static inline unsigned psched_mtu(struct net_device *dev)
243{
244 unsigned mtu = dev->mtu;
245 return dev->hard_header ? mtu + dev->hard_header_len : mtu;
246}
247
248#endif