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 static double tick_in_usec = 1;
27 static double clock_factor = 1;
29 int tc_core_time2big(long time)
34 return (t >> 32) != 0;
38 unsigned tc_core_time2tick(unsigned time)
40 return time*tick_in_usec;
43 unsigned tc_core_tick2time(unsigned tick)
45 return tick/tick_in_usec;
48 unsigned tc_core_time2ktime(unsigned time)
50 return time * clock_factor;
53 unsigned tc_core_ktime2time(unsigned ktime)
55 return ktime / clock_factor;
58 unsigned tc_calc_xmittime(unsigned rate, unsigned size)
60 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate));
63 unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks)
65 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
69 rtab[pkt_len>>cell_log] = pkt_xmit_time
72 int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu,
76 unsigned overhead = (mpu >> 8) & 0xFF;
84 while ((mtu>>cell_log) > 255)
87 for (i=0; i<256; i++) {
88 unsigned sz = (i<<cell_log);
93 rtab[i] = tc_calc_xmittime(bps, sz);
105 fp = fopen("/proc/net/psched", "r");
109 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
115 /* compatibility hack: for old iproute binaries (ignoring
116 * the kernel clock resolution) the kernel advertises a
117 * tick multiplier of 1000 in case of nano-second resolution,
118 * which really is 1. */
119 if (clock_res == 1000000000)
122 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
123 tick_in_usec = (double)t2us / us2t * clock_factor;