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 30long tc_core_usec2tick(long usec) 31{ 32 return usec*tick_in_usec; 33} 34 35long tc_core_tick2usec(long tick) 36{ 37 return tick/tick_in_usec; 38} 39 40unsigned 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 49int tc_calc_rtable(unsigned bps, __u32 *rtab, int cell_log, unsigned mtu, 50 unsigned mpu) 51{ 52 int i; 53 unsigned overhead = (mpu >> 8) & 0xFF; 54 mpu = mpu & 0xFF; 55 56 if (mtu == 0) 57 mtu = 2047; 58 59 if (cell_log < 0) { 60 cell_log = 0; 61 while ((mtu>>cell_log) > 255) 62 cell_log++; 63 } 64 for (i=0; i<256; i++) { 65 unsigned sz = (i<<cell_log); 66 if (overhead) 67 sz += overhead; 68 if (sz < mpu) 69 sz = mpu; 70 rtab[i] = tc_core_usec2tick(1000000*((double)sz/bps)); 71 } 72 return cell_log; 73} 74 75int tc_core_init() 76{ 77 FILE *fp = fopen("/proc/net/psched", "r"); 78 79 if (fp == NULL) 80 return -1; 81 82 if (fscanf(fp, "%08x%08x", &t2us, &us2t) != 2) { 83 fclose(fp); 84 return -1; 85 } 86 fclose(fp); 87 tick_in_usec = (double)t2us/us2t; 88 return 0; 89} 90