]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_core.c
(Logical change 1.3)
[mirror_iproute2.git] / tc / tc_core.c
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
26 static __u32 t2us=1;
27 static __u32 us2t=1;
28 static double tick_in_usec = 1;
29
30 long tc_core_usec2tick(long usec)
31 {
32 return usec*tick_in_usec;
33 }
34
35 long tc_core_tick2usec(long tick)
36 {
37 return tick/tick_in_usec;
38 }
39
40 unsigned tc_calc_xmittime(unsigned rate, unsigned size)
41 {
42 return tc_core_usec2tick(1000000*((double)size/rate));
43 }
44
45 /*
46 rtab[pkt_len>>cell_log] = pkt_xmit_time
47 */
48
49 int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu,
50 unsigned mpu)
51 {
52 int i;
53
54 if (mtu == 0)
55 mtu = 2047;
56
57 if (cell_log < 0) {
58 cell_log = 0;
59 while ((mtu>>cell_log) > 255)
60 cell_log++;
61 }
62 for (i=0; i<256; i++) {
63 unsigned sz = (i<<cell_log);
64 if (sz < mpu)
65 sz = mpu;
66 rtab[i] = tc_core_usec2tick(1000000*((double)sz/bps));
67 }
68 return cell_log;
69 }
70
71 int tc_core_init()
72 {
73 FILE *fp = fopen("/proc/net/psched", "r");
74
75 if (fp == NULL)
76 return -1;
77
78 if (fscanf(fp, "%08x%08x", &t2us, &us2t) != 2) {
79 fclose(fp);
80 return -1;
81 }
82 fclose(fp);
83 tick_in_usec = (double)t2us/us2t;
84 return 0;
85 }