blob: 10c375ef8f852125f170ea586b87e38e48e11b96 [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
54/*
55 rtab[pkt_len>>cell_log] = pkt_xmit_time
56 */
57
58int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu,
59 unsigned mpu)
60{
61 int i;
osdl.net!shemminger934677a2004-07-30 20:26:15 +000062 unsigned overhead = (mpu >> 8) & 0xFF;
63 mpu = mpu & 0xFF;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000064
65 if (mtu == 0)
66 mtu = 2047;
67
68 if (cell_log < 0) {
69 cell_log = 0;
70 while ((mtu>>cell_log) > 255)
71 cell_log++;
72 }
73 for (i=0; i<256; i++) {
74 unsigned sz = (i<<cell_log);
osdl.net!shemminger934677a2004-07-30 20:26:15 +000075 if (overhead)
76 sz += overhead;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000077 if (sz < mpu)
78 sz = mpu;
79 rtab[i] = tc_core_usec2tick(1000000*((double)sz/bps));
80 }
81 return cell_log;
82}
83
84int tc_core_init()
85{
86 FILE *fp = fopen("/proc/net/psched", "r");
87
88 if (fp == NULL)
89 return -1;
90
91 if (fscanf(fp, "%08x%08x", &t2us, &us2t) != 2) {
92 fclose(fp);
93 return -1;
94 }
95 fclose(fp);
96 tick_in_usec = (double)t2us/us2t;
97 return 0;
98}