]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_core.c
Replace "usec" by "time" in function names
[mirror_iproute2.git] / tc / tc_core.c
CommitLineData
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
26static __u32 t2us=1;
27static __u32 us2t=1;
28static double tick_in_usec = 1;
29
8f34caaf 30int tc_core_time2big(long time)
fa565130 31{
8f34caaf 32 __u64 t = time;
fa565130
SH
33
34 t *= tick_in_usec;
35 return (t >> 32) != 0;
36}
37
38
8f34caaf 39long tc_core_time2tick(long time)
aba5acdf 40{
8f34caaf 41 return time*tick_in_usec;
aba5acdf
SH
42}
43
8f34caaf 44long tc_core_tick2time(long tick)
aba5acdf
SH
45{
46 return tick/tick_in_usec;
47}
48
f0bda7e5
PM
49long tc_core_time2ktime(long time)
50{
51 return time;
52}
53
54long tc_core_ktime2time(long ktime)
55{
56 return ktime;
57}
58
aba5acdf
SH
59unsigned tc_calc_xmittime(unsigned rate, unsigned size)
60{
8f34caaf 61 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate));
aba5acdf
SH
62}
63
76dc0aa2
PM
64unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks)
65{
8f34caaf 66 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
76dc0aa2
PM
67}
68
aba5acdf
SH
69/*
70 rtab[pkt_len>>cell_log] = pkt_xmit_time
71 */
72
73int 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
99int 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}