blob: 1ca4583691ad21aba886b454e827810a29c7b4d8 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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
26static __u32 t2us=1;
27static __u32 us2t=1;
28static double tick_in_usec = 1;
29
Stephen Hemmingerfa565132006-10-19 13:10:26 -070030int tc_core_usec2big(long usec)
31{
32 __u64 t = usec;
33
34 t *= tick_in_usec;
35 return (t >> 32) != 0;
36}
37
38
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000039long tc_core_usec2tick(long usec)
40{
41 return usec*tick_in_usec;
42}
43
44long tc_core_tick2usec(long tick)
45{
46 return tick/tick_in_usec;
47}
48
49unsigned tc_calc_xmittime(unsigned rate, unsigned size)
50{
51 return tc_core_usec2tick(1000000*((double)size/rate));
52}
53
Patrick McHardy76dc0aa2007-03-04 20:14:57 +010054unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks)
55{
56 return ((double)rate*tc_core_tick2usec(ticks))/1000000;
57}
58
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000059/*
60 rtab[pkt_len>>cell_log] = pkt_xmit_time
61 */
62
63int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu,
64 unsigned mpu)
65{
66 int i;
osdl.net!shemminger934677a2004-07-30 20:26:15 +000067 unsigned overhead = (mpu >> 8) & 0xFF;
68 mpu = mpu & 0xFF;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000069
70 if (mtu == 0)
71 mtu = 2047;
72
73 if (cell_log < 0) {
74 cell_log = 0;
75 while ((mtu>>cell_log) > 255)
76 cell_log++;
77 }
78 for (i=0; i<256; i++) {
79 unsigned sz = (i<<cell_log);
osdl.net!shemminger934677a2004-07-30 20:26:15 +000080 if (overhead)
81 sz += overhead;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082 if (sz < mpu)
83 sz = mpu;
Patrick McHardy476daa72007-03-04 20:14:56 +010084 rtab[i] = tc_calc_xmittime(bps, sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000085 }
86 return cell_log;
87}
88
89int tc_core_init()
90{
91 FILE *fp = fopen("/proc/net/psched", "r");
92
93 if (fp == NULL)
94 return -1;
95
96 if (fscanf(fp, "%08x%08x", &t2us, &us2t) != 2) {
97 fclose(fp);
98 return -1;
99 }
100 fclose(fp);
101 tick_in_usec = (double)t2us/us2t;
102 return 0;
103}