Debian Codegraph @

tc/tc_core.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * tc_core.c TC core library.
4 *
5 * Authors: Alexey Kuznetsov, <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <math.h>
14 #include <limits.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <string.h>
19
20 #include "utils.h"
21 #include "tc_core.h"
22 #include <linux/atm.h>
23
24 static double tick_in_usec = 1;
25 static double clock_factor = 1;
26
27 static double tc_core_time2tick(double time)
28 {
29 return time * tick_in_usec;
30 }
31
32 double tc_core_tick2time(double tick)
33 {
34 return tick / tick_in_usec;
35 }
36
37 unsigned int tc_core_time2ktime(unsigned int time)
38 {
39 return time * clock_factor;
40 }
41
42 unsigned int tc_core_ktime2time(unsigned int ktime)
43 {
44 return ktime / clock_factor;
45 }
46
47 unsigned int tc_calc_xmittime(__u64 rate, __u64 size)
48 {
49 double val;
50
51 val = ceil(tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate)));
52 return val > UINT_MAX ? UINT_MAX : val;
53 }
54
55 __u64 tc_calc_xmitsize(__u64 rate, unsigned int ticks)
56 {
57 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
58 }
59
60 /*
61 * The align to ATM cells is used for determining the (ATM) SAR
62 * alignment overhead at the ATM layer. (SAR = Segmentation And
63 * Reassembly). This is for example needed when scheduling packet on
64 * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
65 * included in this calculation. This overhead is added in the kernel
66 * before doing the rate table lookup, as this gives better precision
67 * (as the table will always be aligned for 48 bytes).
68 * --Hawk, d.7/11-2004. <[email protected]>
69 */
70 static unsigned int tc_align_to_atm(unsigned int size)
71 {
72 int linksize, cells;
73
74 cells = size / ATM_CELL_PAYLOAD;
75 if ((size % ATM_CELL_PAYLOAD) > 0)
76 cells++;
77
78 linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
79 return linksize;
80 }
81
82 static unsigned int tc_adjust_size(unsigned int sz, unsigned int mpu, enum link_layer linklayer)
83 {
84 if (sz < mpu)
85 sz = mpu;
86
87 switch (linklayer) {
88 case LINKLAYER_ATM:
89 return tc_align_to_atm(sz);
90 case LINKLAYER_ETHERNET:
91 default:
92 /* No size adjustments on Ethernet */
93 return sz;
94 }
95 }
96
97 /* Notice, the rate table calculated here, have gotten replaced in the
98 * kernel and is no-longer used for lookups.
99 *
100 * This happened in kernel release v3.8 caused by kernel
101 * - commit 56b765b79 ("htb: improved accuracy at high rates").
102 * This change unfortunately caused breakage of tc overhead and
103 * linklayer parameters.
104 *
105 * Kernel overhead handling got fixed in kernel v3.10 by
106 * - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling)
107 *
108 * Kernel linklayer handling got fixed in kernel v3.11 by
109 * - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling)
110 */
111
112 /*
113 rtab[pkt_len>>cell_log] = pkt_xmit_time
114 */
115
116 int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
117 int cell_log, unsigned int mtu,
118 enum link_layer linklayer)
119 {
120 int i;
121 unsigned int sz;
122 unsigned int bps = r->rate;
123 unsigned int mpu = r->mpu;
124
125 if (mtu == 0)
126 mtu = 2047;
127
128 if (cell_log < 0) {
129 cell_log = 0;
130 while ((mtu >> cell_log) > 255)
131 cell_log++;
132 }
133
134 for (i = 0; i < 256; i++) {
135 sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
136 rtab[i] = tc_calc_xmittime(bps, sz);
137 }
138
139 r->cell_align = -1;
140 r->cell_log = cell_log;
141 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
142 return cell_log;
143 }
144
145 int tc_calc_rtable_64(struct tc_ratespec *r, __u32 *rtab,
146 int cell_log, unsigned int mtu,
147 enum link_layer linklayer, __u64 rate)
148 {
149 int i;
150 unsigned int sz;
151 __u64 bps = rate;
152 unsigned int mpu = r->mpu;
153
154 if (mtu == 0)
155 mtu = 2047;
156
157 if (cell_log < 0) {
158 cell_log = 0;
159 while ((mtu >> cell_log) > 255)
160 cell_log++;
161 }
162
163 for (i = 0; i < 256; i++) {
164 sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
165 rtab[i] = tc_calc_xmittime(bps, sz);
166 }
167
168 r->cell_align = -1;
169 r->cell_log = cell_log;
170 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
171 return cell_log;
172 }
173
174 /*
175 stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
176 */
177
178 int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
179 {
180 int i;
181 enum link_layer linklayer = s->linklayer;
182 unsigned int sz;
183
184 if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
185 /* don't need data table in this case (only overhead set) */
186 s->mtu = 0;
187 s->tsize = 0;
188 s->cell_log = 0;
189 s->cell_align = 0;
190 *stab = NULL;
191 return 0;
192 }
193
194 if (s->mtu == 0)
195 s->mtu = 2047;
196 if (s->tsize == 0)
197 s->tsize = 512;
198
199 s->cell_log = 0;
200 while ((s->mtu >> s->cell_log) > s->tsize - 1)
201 s->cell_log++;
202
203 *stab = malloc(s->tsize * sizeof(__u16));
204 if (!*stab)
205 return -1;
206
207 again:
208 for (i = s->tsize - 1; i >= 0; i--) {
209 sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer);
210 if ((sz >> s->size_log) > UINT16_MAX) {
211 s->size_log++;
212 goto again;
213 }
214 (*stab)[i] = sz >> s->size_log;
215 }
216
217 s->cell_align = -1; /* Due to the sz calc */
218 return 0;
219 }
220
221 int tc_core_init(void)
222 {
223 FILE *fp;
224 __u32 clock_res;
225 __u32 t2us;
226 __u32 us2t;
227
228 fp = fopen("/proc/net/psched", "r");
229 if (fp == NULL)
230 return -1;
231
232 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
233 fclose(fp);
234 return -1;
235 }
236 fclose(fp);
237
238 /* compatibility hack: for old iproute binaries (ignoring
239 * the kernel clock resolution) the kernel advertises a
240 * tick multiplier of 1000 in case of nano-second resolution,
241 * which really is 1. */
242 if (clock_res == 1000000000)
243 t2us = us2t;
244
245 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
246 tick_in_usec = (double)t2us / us2t * clock_factor;
247 return 0;
248 }