1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <stdint.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <math.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23
24#include "utils.h"
25#include "tc_core.h"
26#include <linux/atm.h>
27
28static double tick_in_usec = 1;
29static double clock_factor = 1;
30
31int tc_core_time2big(unsigned int time)
32{
33 __u64 t = time;
34
35 t *= tick_in_usec;
36 return (t >> 32) != 0;
37}
38
39
40unsigned int tc_core_time2tick(unsigned int time)
41{
42 return time*tick_in_usec;
43}
44
45unsigned int tc_core_tick2time(unsigned int tick)
46{
47 return tick/tick_in_usec;
48}
49
50unsigned int tc_core_time2ktime(unsigned int time)
51{
52 return time * clock_factor;
53}
54
55unsigned int tc_core_ktime2time(unsigned int ktime)
56{
57 return ktime / clock_factor;
58}
59
60unsigned int tc_calc_xmittime(__u64 rate, unsigned int size)
61{
62 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate));
63}
64
65unsigned int tc_calc_xmitsize(__u64 rate, unsigned int ticks)
66{
67 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
68}
69
70
71
72
73
74
75
76
77
78
79
80static unsigned int tc_align_to_atm(unsigned int size)
81{
82 int linksize, cells;
83
84 cells = size / ATM_CELL_PAYLOAD;
85 if ((size % ATM_CELL_PAYLOAD) > 0)
86 cells++;
87
88 linksize = cells * ATM_CELL_SIZE;
89 return linksize;
90}
91
92static unsigned int tc_adjust_size(unsigned int sz, unsigned int mpu, enum link_layer linklayer)
93{
94 if (sz < mpu)
95 sz = mpu;
96
97 switch (linklayer) {
98 case LINKLAYER_ATM:
99 return tc_align_to_atm(sz);
100 case LINKLAYER_ETHERNET:
101 default:
102
103 return sz;
104 }
105}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
127 int cell_log, unsigned int mtu,
128 enum link_layer linklayer)
129{
130 int i;
131 unsigned int sz;
132 unsigned int bps = r->rate;
133 unsigned int mpu = r->mpu;
134
135 if (mtu == 0)
136 mtu = 2047;
137
138 if (cell_log < 0) {
139 cell_log = 0;
140 while ((mtu >> cell_log) > 255)
141 cell_log++;
142 }
143
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);
147 }
148
149 r->cell_align = -1;
150 r->cell_log = cell_log;
151 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
152 return cell_log;
153}
154
155int tc_calc_rtable_64(struct tc_ratespec *r, __u32 *rtab,
156 int cell_log, unsigned int mtu,
157 enum link_layer linklayer, __u64 rate)
158{
159 int i;
160 unsigned int sz;
161 __u64 bps = rate;
162 unsigned int mpu = r->mpu;
163
164 if (mtu == 0)
165 mtu = 2047;
166
167 if (cell_log < 0) {
168 cell_log = 0;
169 while ((mtu >> cell_log) > 255)
170 cell_log++;
171 }
172
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);
176 }
177
178 r->cell_align = -1;
179 r->cell_log = cell_log;
180 r->linklayer = (linklayer & TC_LINKLAYER_MASK);
181 return cell_log;
182}
183
184
185
186
187
188int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
189{
190 int i;
191 enum link_layer linklayer = s->linklayer;
192 unsigned int sz;
193
194 if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
195
196 s->mtu = 0;
197 s->tsize = 0;
198 s->cell_log = 0;
199 s->cell_align = 0;
200 *stab = NULL;
201 return 0;
202 }
203
204 if (s->mtu == 0)
205 s->mtu = 2047;
206 if (s->tsize == 0)
207 s->tsize = 512;
208
209 s->cell_log = 0;
210 while ((s->mtu >> s->cell_log) > s->tsize - 1)
211 s->cell_log++;
212
213 *stab = malloc(s->tsize * sizeof(__u16));
214 if (!*stab)
215 return -1;
216
217again:
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) {
221 s->size_log++;
222 goto again;
223 }
224 (*stab)[i] = sz >> s->size_log;
225 }
226
227 s->cell_align = -1;
228 return 0;
229}
230
231int tc_core_init(void)
232{
233 FILE *fp;
234 __u32 clock_res;
235 __u32 t2us;
236 __u32 us2t;
237
238 fp = fopen("/proc/net/psched", "r");
239 if (fp == NULL)
240 return -1;
241
242 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
243 fclose(fp);
244 return -1;
245 }
246 fclose(fp);
247
248
249
250
251
252 if (clock_res == 1000000000)
253 t2us = us2t;
254
255 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
256 tick_in_usec = (double)t2us / us2t * clock_factor;
257 return 0;
258}
259