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 | |
| 26 | static __u32 t2us=1; |
| 27 | static __u32 us2t=1; |
| 28 | static double tick_in_usec = 1; |
| 29 | |
Stephen Hemminger | fa56513 | 2006-10-19 13:10:26 -0700 | [diff] [blame] | 30 | int 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!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | long tc_core_usec2tick(long usec) |
| 40 | { |
| 41 | return usec*tick_in_usec; |
| 42 | } |
| 43 | |
| 44 | long tc_core_tick2usec(long tick) |
| 45 | { |
| 46 | return tick/tick_in_usec; |
| 47 | } |
| 48 | |
| 49 | unsigned 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 | |
| 58 | int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu, |
| 59 | unsigned mpu) |
| 60 | { |
| 61 | int i; |
osdl.net!shemminger | 934677a | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 62 | unsigned overhead = (mpu >> 8) & 0xFF; |
| 63 | mpu = mpu & 0xFF; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 64 | |
| 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!shemminger | 934677a | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 75 | if (overhead) |
| 76 | sz += overhead; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | if (sz < mpu) |
| 78 | sz = mpu; |
Patrick McHardy | 476daa7 | 2007-03-04 20:14:56 +0100 | [diff] [blame^] | 79 | rtab[i] = tc_calc_xmittime(bps, sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | } |
| 81 | return cell_log; |
| 82 | } |
| 83 | |
| 84 | int 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 | } |