2 * tc_core.c TC core library.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
26 #include <linux/atm.h>
28 static double tick_in_usec = 1;
29 static double clock_factor = 1;
31 int tc_core_time2big(unsigned int time)
36 return (t >> 32) != 0;
40 unsigned int tc_core_time2tick(unsigned int time)
42 return time*tick_in_usec;
45 unsigned int tc_core_tick2time(unsigned int tick)
47 return tick/tick_in_usec;
50 unsigned int tc_core_time2ktime(unsigned int time)
52 return time * clock_factor;
55 unsigned int tc_core_ktime2time(unsigned int ktime)
57 return ktime / clock_factor;
60 unsigned int tc_calc_xmittime(__u64 rate, unsigned int size)
62 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate));
65 unsigned int tc_calc_xmitsize(__u64 rate, unsigned int ticks)
67 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
71 * The align to ATM cells is used for determining the (ATM) SAR
72 * alignment overhead at the ATM layer. (SAR = Segmentation And
73 * Reassembly). This is for example needed when scheduling packet on
74 * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
75 * included in this calculation. This overhead is added in the kernel
76 * before doing the rate table lookup, as this gives better precision
77 * (as the table will always be aligned for 48 bytes).
78 * --Hawk, d.7/11-2004. <hawk@diku.dk>
80 static unsigned int tc_align_to_atm(unsigned int size)
84 cells = size / ATM_CELL_PAYLOAD;
85 if ((size % ATM_CELL_PAYLOAD) > 0)
88 linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
92 static unsigned int tc_adjust_size(unsigned int sz, unsigned int mpu, enum link_layer linklayer)
99 return tc_align_to_atm(sz);
100 case LINKLAYER_ETHERNET:
102 /* No size adjustments on Ethernet */
107 /* Notice, the rate table calculated here, have gotten replaced in the
108 * kernel and is no-longer used for lookups.
110 * This happened in kernel release v3.8 caused by kernel
111 * - commit 56b765b79 ("htb: improved accuracy at high rates").
112 * This change unfortunately caused breakage of tc overhead and
113 * linklayer parameters.
115 * Kernel overhead handling got fixed in kernel v3.10 by
116 * - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling)
118 * Kernel linklayer handling got fixed in kernel v3.11 by
119 * - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling)
123 rtab[pkt_len>>cell_log] = pkt_xmit_time
126 int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
127 int cell_log, unsigned int mtu,
128 enum link_layer linklayer)
132 unsigned int bps = r->rate;
133 unsigned int mpu = r->mpu;
140 while ((mtu >> cell_log) > 255)
144 for (i = 0; i < 256; i++) {
145 sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
146 rtab[i] = tc_calc_xmittime(bps, sz);
150 r->cell_log = cell_log;
151 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
155 int tc_calc_rtable_64(struct tc_ratespec *r, __u32 *rtab,
156 int cell_log, unsigned int mtu,
157 enum link_layer linklayer, __u64 rate)
162 unsigned int mpu = r->mpu;
169 while ((mtu >> cell_log) > 255)
173 for (i = 0; i < 256; i++) {
174 sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
175 rtab[i] = tc_calc_xmittime(bps, sz);
179 r->cell_log = cell_log;
180 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
185 stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
188 int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
191 enum link_layer linklayer = s->linklayer;
194 if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
195 /* don't need data table in this case (only overhead set) */
210 while ((s->mtu >> s->cell_log) > s->tsize - 1)
213 *stab = malloc(s->tsize * sizeof(__u16));
218 for (i = s->tsize - 1; i >= 0; i--) {
219 sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer);
220 if ((sz >> s->size_log) > UINT16_MAX) {
224 (*stab)[i] = sz >> s->size_log;
227 s->cell_align = -1; /* Due to the sz calc */
231 int tc_core_init(void)
238 fp = fopen("/proc/net/psched", "r");
242 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
248 /* compatibility hack: for old iproute binaries (ignoring
249 * the kernel clock resolution) the kernel advertises a
250 * tick multiplier of 1000 in case of nano-second resolution,
251 * which really is 1. */
252 if (clock_res == 1000000000)
255 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
256 tick_in_usec = (double)t2us / us2t * clock_factor;