osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 26 | static double tick_in_usec = 1; |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 27 | static double clock_factor = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 28 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 29 | int tc_core_time2big(long time) |
Stephen Hemminger | fa56513 | 2006-10-19 13:10:26 -0700 | [diff] [blame] | 30 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 31 | __u64 t = time; |
Stephen Hemminger | fa56513 | 2006-10-19 13:10:26 -0700 | [diff] [blame] | 32 | |
| 33 | t *= tick_in_usec; |
| 34 | return (t >> 32) != 0; |
| 35 | } |
| 36 | |
| 37 | |
Andreas Henriksson | 4475984 | 2007-10-12 10:56:46 +0200 | [diff] [blame^] | 38 | unsigned tc_core_time2tick(unsigned time) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 40 | return time*tick_in_usec; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Andreas Henriksson | 4475984 | 2007-10-12 10:56:46 +0200 | [diff] [blame^] | 43 | unsigned tc_core_tick2time(unsigned tick) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | { |
| 45 | return tick/tick_in_usec; |
| 46 | } |
| 47 | |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 48 | long tc_core_time2ktime(long time) |
| 49 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 50 | return time * clock_factor; |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | long tc_core_ktime2time(long ktime) |
| 54 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 55 | return ktime / clock_factor; |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 56 | } |
| 57 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 58 | unsigned tc_calc_xmittime(unsigned rate, unsigned size) |
| 59 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 60 | return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Patrick McHardy | 76dc0aa | 2007-03-04 20:14:57 +0100 | [diff] [blame] | 63 | unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks) |
| 64 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 65 | return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC; |
Patrick McHardy | 76dc0aa | 2007-03-04 20:14:57 +0100 | [diff] [blame] | 66 | } |
| 67 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 68 | /* |
| 69 | rtab[pkt_len>>cell_log] = pkt_xmit_time |
| 70 | */ |
| 71 | |
| 72 | int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu, |
| 73 | unsigned mpu) |
| 74 | { |
| 75 | int i; |
osdl.net!shemminger | 934677a | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 76 | unsigned overhead = (mpu >> 8) & 0xFF; |
| 77 | mpu = mpu & 0xFF; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 78 | |
| 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!shemminger | 934677a | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 89 | if (overhead) |
| 90 | sz += overhead; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | if (sz < mpu) |
| 92 | sz = mpu; |
Patrick McHardy | 476daa7 | 2007-03-04 20:14:56 +0100 | [diff] [blame] | 93 | rtab[i] = tc_calc_xmittime(bps, sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 94 | } |
| 95 | return cell_log; |
| 96 | } |
| 97 | |
| 98 | int tc_core_init() |
| 99 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 100 | FILE *fp; |
| 101 | __u32 clock_res; |
| 102 | __u32 t2us; |
| 103 | __u32 us2t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 104 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 105 | fp = fopen("/proc/net/psched", "r"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | if (fp == NULL) |
| 107 | return -1; |
| 108 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 109 | if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 110 | fclose(fp); |
| 111 | return -1; |
| 112 | } |
| 113 | fclose(fp); |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 114 | |
| 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!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |