]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * tc_core.c TC core library. | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.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 "tc_core.h" | |
25 | ||
26 | static __u32 t2us=1; | |
27 | static __u32 us2t=1; | |
28 | static double tick_in_usec = 1; | |
29 | ||
fa565130 SH |
30 | int tc_core_usec2big(long usec) |
31 | { | |
32 | __u64 t = usec; | |
33 | ||
34 | t *= tick_in_usec; | |
35 | return (t >> 32) != 0; | |
36 | } | |
37 | ||
38 | ||
aba5acdf SH |
39 | long tc_core_usec2tick(long usec) |
40 | { | |
41 | return usec*tick_in_usec; | |
42 | } | |
43 | ||
44 | long tc_core_tick2usec(long tick) | |
45 | { | |
46 | return tick/tick_in_usec; | |
47 | } | |
48 | ||
f0bda7e5 PM |
49 | long tc_core_time2ktime(long time) |
50 | { | |
51 | return time; | |
52 | } | |
53 | ||
54 | long tc_core_ktime2time(long ktime) | |
55 | { | |
56 | return ktime; | |
57 | } | |
58 | ||
aba5acdf SH |
59 | unsigned tc_calc_xmittime(unsigned rate, unsigned size) |
60 | { | |
f0bda7e5 | 61 | return tc_core_usec2tick(TIME_UNITS_PER_SEC*((double)size/rate)); |
aba5acdf SH |
62 | } |
63 | ||
76dc0aa2 PM |
64 | unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks) |
65 | { | |
f0bda7e5 | 66 | return ((double)rate*tc_core_tick2usec(ticks))/TIME_UNITS_PER_SEC; |
76dc0aa2 PM |
67 | } |
68 | ||
aba5acdf SH |
69 | /* |
70 | rtab[pkt_len>>cell_log] = pkt_xmit_time | |
71 | */ | |
72 | ||
73 | int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu, | |
74 | unsigned mpu) | |
75 | { | |
76 | int i; | |
934677a2 SH |
77 | unsigned overhead = (mpu >> 8) & 0xFF; |
78 | mpu = mpu & 0xFF; | |
aba5acdf SH |
79 | |
80 | if (mtu == 0) | |
81 | mtu = 2047; | |
82 | ||
83 | if (cell_log < 0) { | |
84 | cell_log = 0; | |
85 | while ((mtu>>cell_log) > 255) | |
86 | cell_log++; | |
87 | } | |
88 | for (i=0; i<256; i++) { | |
89 | unsigned sz = (i<<cell_log); | |
934677a2 SH |
90 | if (overhead) |
91 | sz += overhead; | |
aba5acdf SH |
92 | if (sz < mpu) |
93 | sz = mpu; | |
476daa72 | 94 | rtab[i] = tc_calc_xmittime(bps, sz); |
aba5acdf SH |
95 | } |
96 | return cell_log; | |
97 | } | |
98 | ||
99 | int tc_core_init() | |
100 | { | |
101 | FILE *fp = fopen("/proc/net/psched", "r"); | |
102 | ||
103 | if (fp == NULL) | |
104 | return -1; | |
105 | ||
106 | if (fscanf(fp, "%08x%08x", &t2us, &us2t) != 2) { | |
107 | fclose(fp); | |
108 | return -1; | |
109 | } | |
110 | fclose(fp); | |
111 | tick_in_usec = (double)t2us/us2t; | |
112 | return 0; | |
113 | } |