blob: fb898766972f704de0d8690f9d5a36448e14445e [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
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000026static double tick_in_usec = 1;
Patrick McHardy147e1d42007-03-04 20:15:03 +010027static double clock_factor = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000028
Patrick McHardy8f34caa2007-03-04 20:15:00 +010029int tc_core_time2big(long time)
Stephen Hemmingerfa565132006-10-19 13:10:26 -070030{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010031 __u64 t = time;
Stephen Hemmingerfa565132006-10-19 13:10:26 -070032
33 t *= tick_in_usec;
34 return (t >> 32) != 0;
35}
36
37
Andreas Henriksson44759842007-10-12 10:56:46 +020038unsigned tc_core_time2tick(unsigned time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000039{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010040 return time*tick_in_usec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000041}
42
Andreas Henriksson44759842007-10-12 10:56:46 +020043unsigned tc_core_tick2time(unsigned tick)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044{
45 return tick/tick_in_usec;
46}
47
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010048long tc_core_time2ktime(long time)
49{
Patrick McHardy147e1d42007-03-04 20:15:03 +010050 return time * clock_factor;
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010051}
52
53long tc_core_ktime2time(long ktime)
54{
Patrick McHardy147e1d42007-03-04 20:15:03 +010055 return ktime / clock_factor;
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010056}
57
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000058unsigned tc_calc_xmittime(unsigned rate, unsigned size)
59{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010060 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000061}
62
Patrick McHardy76dc0aa2007-03-04 20:14:57 +010063unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks)
64{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010065 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
Patrick McHardy76dc0aa2007-03-04 20:14:57 +010066}
67
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000068/*
69 rtab[pkt_len>>cell_log] = pkt_xmit_time
70 */
71
72int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu,
73 unsigned mpu)
74{
75 int i;
osdl.net!shemminger934677a2004-07-30 20:26:15 +000076 unsigned overhead = (mpu >> 8) & 0xFF;
77 mpu = mpu & 0xFF;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000078
79 if (mtu == 0)
80 mtu = 2047;
81
82 if (cell_log < 0) {
83 cell_log = 0;
84 while ((mtu>>cell_log) > 255)
85 cell_log++;
86 }
87 for (i=0; i<256; i++) {
88 unsigned sz = (i<<cell_log);
osdl.net!shemminger934677a2004-07-30 20:26:15 +000089 if (overhead)
90 sz += overhead;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000091 if (sz < mpu)
92 sz = mpu;
Patrick McHardy476daa72007-03-04 20:14:56 +010093 rtab[i] = tc_calc_xmittime(bps, sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000094 }
95 return cell_log;
96}
97
98int tc_core_init()
99{
Patrick McHardy147e1d42007-03-04 20:15:03 +0100100 FILE *fp;
101 __u32 clock_res;
102 __u32 t2us;
103 __u32 us2t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104
Patrick McHardy147e1d42007-03-04 20:15:03 +0100105 fp = fopen("/proc/net/psched", "r");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 if (fp == NULL)
107 return -1;
108
Patrick McHardy147e1d42007-03-04 20:15:03 +0100109 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000110 fclose(fp);
111 return -1;
112 }
113 fclose(fp);
Patrick McHardy147e1d42007-03-04 20:15:03 +0100114
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)
120 t2us = us2t;
121
122 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
123 tick_in_usec = (double)t2us / us2t * clock_factor;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000124 return 0;
125}