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" |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 25 | #include <linux/atm.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 26 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 27 | static double tick_in_usec = 1; |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 28 | static double clock_factor = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
Andreas Henriksson | 64e2ad5 | 2007-10-12 14:37:09 +0200 | [diff] [blame] | 30 | int tc_core_time2big(unsigned time) |
Stephen Hemminger | fa56513 | 2006-10-19 13:10:26 -0700 | [diff] [blame] | 31 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 32 | __u64 t = time; |
Stephen Hemminger | fa56513 | 2006-10-19 13:10:26 -0700 | [diff] [blame] | 33 | |
| 34 | t *= tick_in_usec; |
| 35 | return (t >> 32) != 0; |
| 36 | } |
| 37 | |
| 38 | |
Andreas Henriksson | 4475984 | 2007-10-12 10:56:46 +0200 | [diff] [blame] | 39 | unsigned tc_core_time2tick(unsigned time) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 40 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 41 | return time*tick_in_usec; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Andreas Henriksson | 4475984 | 2007-10-12 10:56:46 +0200 | [diff] [blame] | 44 | unsigned tc_core_tick2time(unsigned tick) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | { |
| 46 | return tick/tick_in_usec; |
| 47 | } |
| 48 | |
Andreas Henriksson | 57a800d | 2007-10-12 13:49:49 +0200 | [diff] [blame] | 49 | unsigned tc_core_time2ktime(unsigned time) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 50 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 51 | return time * clock_factor; |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Andreas Henriksson | 57a800d | 2007-10-12 13:49:49 +0200 | [diff] [blame] | 54 | unsigned tc_core_ktime2time(unsigned ktime) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 55 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 56 | return ktime / clock_factor; |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 57 | } |
| 58 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 59 | unsigned tc_calc_xmittime(unsigned rate, unsigned size) |
| 60 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 61 | return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Patrick McHardy | 76dc0aa | 2007-03-04 20:14:57 +0100 | [diff] [blame] | 64 | unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks) |
| 65 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 66 | return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC; |
Patrick McHardy | 76dc0aa | 2007-03-04 20:14:57 +0100 | [diff] [blame] | 67 | } |
| 68 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 69 | /* |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 70 | * The align to ATM cells is used for determining the (ATM) SAR |
| 71 | * alignment overhead at the ATM layer. (SAR = Segmentation And |
| 72 | * Reassembly). This is for example needed when scheduling packet on |
| 73 | * an ADSL connection. Note that the extra ATM-AAL overhead is _not_ |
| 74 | * included in this calculation. This overhead is added in the kernel |
| 75 | * before doing the rate table lookup, as this gives better precision |
| 76 | * (as the table will always be aligned for 48 bytes). |
| 77 | * --Hawk, d.7/11-2004. <hawk@diku.dk> |
| 78 | */ |
| 79 | unsigned tc_align_to_atm(unsigned size) |
| 80 | { |
| 81 | int linksize, cells; |
| 82 | cells = size / ATM_CELL_PAYLOAD; |
| 83 | if ((size % ATM_CELL_PAYLOAD) > 0) |
| 84 | cells++; |
| 85 | |
| 86 | linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */ |
| 87 | return linksize; |
| 88 | } |
| 89 | |
| 90 | /* |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | rtab[pkt_len>>cell_log] = pkt_xmit_time |
| 92 | */ |
| 93 | |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 94 | int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab, |
| 95 | int cell_log, unsigned mtu, |
| 96 | enum link_layer linklayer) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 97 | { |
| 98 | int i; |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 99 | unsigned bps = r->rate; |
| 100 | unsigned mpu = r->mpu; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 101 | |
| 102 | if (mtu == 0) |
| 103 | mtu = 2047; |
| 104 | |
| 105 | if (cell_log < 0) { |
| 106 | cell_log = 0; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 107 | while ((mtu >> cell_log) > 255) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 108 | cell_log++; |
| 109 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 110 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 111 | for (i=0; i<256; i++) { |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 112 | unsigned sz = (i+1)<<cell_log; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 113 | if (sz < mpu) |
| 114 | sz = mpu; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 115 | |
| 116 | switch (linklayer) { |
| 117 | case LINKLAYER_ATM: |
| 118 | sz = tc_align_to_atm(sz); |
| 119 | break; |
| 120 | case LINKLAYER_ETHERNET: |
| 121 | // No size adjustments on Ethernet |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | |
Patrick McHardy | 476daa7 | 2007-03-04 20:14:56 +0100 | [diff] [blame] | 127 | rtab[i] = tc_calc_xmittime(bps, sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 128 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame^] | 129 | |
Jesper Dangaard Brouer | eeee367 | 2007-09-05 15:24:51 +0200 | [diff] [blame] | 130 | r->cell_align=-1; // Due to the sz calc |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 131 | r->cell_log=cell_log; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | return cell_log; |
| 133 | } |
| 134 | |
| 135 | int tc_core_init() |
| 136 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 137 | FILE *fp; |
| 138 | __u32 clock_res; |
| 139 | __u32 t2us; |
| 140 | __u32 us2t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 141 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 142 | fp = fopen("/proc/net/psched", "r"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | if (fp == NULL) |
| 144 | return -1; |
| 145 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 146 | if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | fclose(fp); |
| 148 | return -1; |
| 149 | } |
| 150 | fclose(fp); |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 151 | |
| 152 | /* compatibility hack: for old iproute binaries (ignoring |
| 153 | * the kernel clock resolution) the kernel advertises a |
| 154 | * tick multiplier of 1000 in case of nano-second resolution, |
| 155 | * which really is 1. */ |
| 156 | if (clock_res == 1000000000) |
| 157 | t2us = us2t; |
| 158 | |
| 159 | clock_factor = (double)clock_res / TIME_UNITS_PER_SEC; |
| 160 | tick_in_usec = (double)t2us / us2t * clock_factor; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |