]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_core.c
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>
25 #include <linux/atm.h>
27 static double tick_in_usec
= 1;
28 static double clock_factor
= 1;
30 int tc_core_time2big(unsigned time
)
35 return (t
>> 32) != 0;
39 unsigned tc_core_time2tick(unsigned time
)
41 return time
*tick_in_usec
;
44 unsigned tc_core_tick2time(unsigned tick
)
46 return tick
/tick_in_usec
;
49 unsigned tc_core_time2ktime(unsigned time
)
51 return time
* clock_factor
;
54 unsigned tc_core_ktime2time(unsigned ktime
)
56 return ktime
/ clock_factor
;
59 unsigned tc_calc_xmittime(__u64 rate
, unsigned size
)
61 return tc_core_time2tick(TIME_UNITS_PER_SEC
*((double)size
/(double)rate
));
64 unsigned tc_calc_xmitsize(__u64 rate
, unsigned ticks
)
66 return ((double)rate
*tc_core_tick2time(ticks
))/TIME_UNITS_PER_SEC
;
70 * The align to ATM cells is used for determining the (ATM) SAR
71 * alignment overhead at the ATM layer. (SAR = Segmentation And
72 * Reassembly). This is for example needed when scheduling packet on
73 * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
74 * included in this calculation. This overhead is added in the kernel
75 * before doing the rate table lookup, as this gives better precision
76 * (as the table will always be aligned for 48 bytes).
77 * --Hawk, d.7/11-2004. <hawk@diku.dk>
79 static unsigned tc_align_to_atm(unsigned size
)
82 cells
= size
/ ATM_CELL_PAYLOAD
;
83 if ((size
% ATM_CELL_PAYLOAD
) > 0)
86 linksize
= cells
* ATM_CELL_SIZE
; /* Use full cell size to add ATM tax */
90 static unsigned tc_adjust_size(unsigned sz
, unsigned mpu
, enum link_layer linklayer
)
97 return tc_align_to_atm(sz
);
98 case LINKLAYER_ETHERNET
:
100 // No size adjustments on Ethernet
105 /* Notice, the rate table calculated here, have gotten replaced in the
106 * kernel and is no-longer used for lookups.
108 * This happened in kernel release v3.8 caused by kernel
109 * - commit 56b765b79 ("htb: improved accuracy at high rates").
110 * This change unfortunately caused breakage of tc overhead and
111 * linklayer parameters.
113 * Kernel overhead handling got fixed in kernel v3.10 by
114 * - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling)
116 * Kernel linklayer handling got fixed in kernel v3.11 by
117 * - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling)
121 rtab[pkt_len>>cell_log] = pkt_xmit_time
124 int tc_calc_rtable(struct tc_ratespec
*r
, __u32
*rtab
,
125 int cell_log
, unsigned mtu
,
126 enum link_layer linklayer
)
130 unsigned bps
= r
->rate
;
131 unsigned mpu
= r
->mpu
;
138 while ((mtu
>> cell_log
) > 255)
142 for (i
=0; i
<256; i
++) {
143 sz
= tc_adjust_size((i
+ 1) << cell_log
, mpu
, linklayer
);
144 rtab
[i
] = tc_calc_xmittime(bps
, sz
);
147 r
->cell_align
=-1; // Due to the sz calc
148 r
->cell_log
=cell_log
;
149 r
->linklayer
= (linklayer
& TC_LINKLAYER_MASK
);
154 stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
157 int tc_calc_size_table(struct tc_sizespec
*s
, __u16
**stab
)
160 enum link_layer linklayer
= s
->linklayer
;
163 if (linklayer
<= LINKLAYER_ETHERNET
&& s
->mpu
== 0) {
164 /* don't need data table in this case (only overhead set) */
179 while ((s
->mtu
>> s
->cell_log
) > s
->tsize
- 1)
182 *stab
= malloc(s
->tsize
* sizeof(__u16
));
187 for (i
= s
->tsize
- 1; i
>= 0; i
--) {
188 sz
= tc_adjust_size((i
+ 1) << s
->cell_log
, s
->mpu
, linklayer
);
189 if ((sz
>> s
->size_log
) > UINT16_MAX
) {
193 (*stab
)[i
] = sz
>> s
->size_log
;
196 s
->cell_align
= -1; // Due to the sz calc
200 int tc_core_init(void)
207 fp
= fopen("/proc/net/psched", "r");
211 if (fscanf(fp
, "%08x%08x%08x", &t2us
, &us2t
, &clock_res
) != 3) {
217 /* compatibility hack: for old iproute binaries (ignoring
218 * the kernel clock resolution) the kernel advertises a
219 * tick multiplier of 1000 in case of nano-second resolution,
220 * which really is 1. */
221 if (clock_res
== 1000000000)
224 clock_factor
= (double)clock_res
/ TIME_UNITS_PER_SEC
;
225 tick_in_usec
= (double)t2us
/ us2t
* clock_factor
;