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 | |
Eric Dumazet | 8334bb3 | 2013-11-12 14:34:07 -0800 | [diff] [blame^] | 59 | unsigned tc_calc_xmittime(__u64 rate, unsigned size) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 60 | { |
Eric Dumazet | 8334bb3 | 2013-11-12 14:34:07 -0800 | [diff] [blame^] | 61 | return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Eric Dumazet | 8334bb3 | 2013-11-12 14:34:07 -0800 | [diff] [blame^] | 64 | unsigned tc_calc_xmitsize(__u64 rate, unsigned ticks) |
Patrick McHardy | 76dc0aa | 2007-03-04 20:14:57 +0100 | [diff] [blame] | 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 | */ |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 79 | static unsigned tc_align_to_atm(unsigned size) |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 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 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 90 | static unsigned tc_adjust_size(unsigned sz, unsigned mpu, enum link_layer linklayer) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 91 | { |
| 92 | if (sz < mpu) |
| 93 | sz = mpu; |
| 94 | |
| 95 | switch (linklayer) { |
| 96 | case LINKLAYER_ATM: |
| 97 | return tc_align_to_atm(sz); |
| 98 | case LINKLAYER_ETHERNET: |
| 99 | default: |
| 100 | // No size adjustments on Ethernet |
| 101 | return sz; |
| 102 | } |
| 103 | } |
| 104 | |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 105 | /* Notice, the rate table calculated here, have gotten replaced in the |
| 106 | * kernel and is no-longer used for lookups. |
| 107 | * |
| 108 | * This happened in kernel release v3.8 caused by kernel |
| 109 | * - commit 56b765b79 ("htb: improved accuracy at high rates"). |
| 110 | * This change unfortunately caused breakage of tc overhead and |
| 111 | * linklayer parameters. |
| 112 | * |
| 113 | * Kernel overhead handling got fixed in kernel v3.10 by |
| 114 | * - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling) |
| 115 | * |
| 116 | * Kernel linklayer handling got fixed in kernel v3.11 by |
| 117 | * - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling) |
| 118 | */ |
| 119 | |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 120 | /* |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 121 | rtab[pkt_len>>cell_log] = pkt_xmit_time |
| 122 | */ |
| 123 | |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 124 | int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab, |
| 125 | int cell_log, unsigned mtu, |
| 126 | enum link_layer linklayer) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | { |
| 128 | int i; |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 129 | unsigned sz; |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 130 | unsigned bps = r->rate; |
| 131 | unsigned mpu = r->mpu; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | |
| 133 | if (mtu == 0) |
| 134 | mtu = 2047; |
| 135 | |
| 136 | if (cell_log < 0) { |
| 137 | cell_log = 0; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 138 | while ((mtu >> cell_log) > 255) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 139 | cell_log++; |
| 140 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 141 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 142 | for (i=0; i<256; i++) { |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 143 | sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer); |
Patrick McHardy | 476daa7 | 2007-03-04 20:14:56 +0100 | [diff] [blame] | 144 | rtab[i] = tc_calc_xmittime(bps, sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 145 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 146 | |
Jesper Dangaard Brouer | eeee367 | 2007-09-05 15:24:51 +0200 | [diff] [blame] | 147 | r->cell_align=-1; // Due to the sz calc |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 148 | r->cell_log=cell_log; |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 149 | r->linklayer = (linklayer & TC_LINKLAYER_MASK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 150 | return cell_log; |
| 151 | } |
| 152 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 153 | /* |
| 154 | stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log |
| 155 | */ |
| 156 | |
| 157 | int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab) |
| 158 | { |
| 159 | int i; |
| 160 | enum link_layer linklayer = s->linklayer; |
| 161 | unsigned int sz; |
| 162 | |
| 163 | if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) { |
| 164 | /* don't need data table in this case (only overhead set) */ |
| 165 | s->mtu = 0; |
| 166 | s->tsize = 0; |
| 167 | s->cell_log = 0; |
| 168 | s->cell_align = 0; |
| 169 | *stab = NULL; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | if (s->mtu == 0) |
| 174 | s->mtu = 2047; |
| 175 | if (s->tsize == 0) |
| 176 | s->tsize = 512; |
| 177 | |
| 178 | s->cell_log = 0; |
| 179 | while ((s->mtu >> s->cell_log) > s->tsize - 1) |
| 180 | s->cell_log++; |
| 181 | |
| 182 | *stab = malloc(s->tsize * sizeof(__u16)); |
| 183 | if (!*stab) |
| 184 | return -1; |
| 185 | |
| 186 | again: |
| 187 | for (i = s->tsize - 1; i >= 0; i--) { |
| 188 | sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer); |
| 189 | if ((sz >> s->size_log) > UINT16_MAX) { |
| 190 | s->size_log++; |
| 191 | goto again; |
| 192 | } |
| 193 | (*stab)[i] = sz >> s->size_log; |
| 194 | } |
| 195 | |
| 196 | s->cell_align = -1; // Due to the sz calc |
| 197 | return 0; |
| 198 | } |
| 199 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 200 | int tc_core_init(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | { |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 202 | FILE *fp; |
| 203 | __u32 clock_res; |
| 204 | __u32 t2us; |
| 205 | __u32 us2t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 207 | fp = fopen("/proc/net/psched", "r"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 208 | if (fp == NULL) |
| 209 | return -1; |
| 210 | |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 211 | if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 212 | fclose(fp); |
| 213 | return -1; |
| 214 | } |
| 215 | fclose(fp); |
Patrick McHardy | 147e1d4 | 2007-03-04 20:15:03 +0100 | [diff] [blame] | 216 | |
| 217 | /* compatibility hack: for old iproute binaries (ignoring |
| 218 | * the kernel clock resolution) the kernel advertises a |
| 219 | * tick multiplier of 1000 in case of nano-second resolution, |
| 220 | * which really is 1. */ |
| 221 | if (clock_res == 1000000000) |
| 222 | t2us = us2t; |
| 223 | |
| 224 | clock_factor = (double)clock_res / TIME_UNITS_PER_SEC; |
| 225 | tick_in_usec = (double)t2us / us2t * clock_factor; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |