blob: 85b072ef45ce01e66102a728b40f00ce15c6ec5a [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 Brouer292f29b2008-04-09 23:01:01 +020025#include <linux/atm.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000026
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000027static double tick_in_usec = 1;
Patrick McHardy147e1d42007-03-04 20:15:03 +010028static double clock_factor = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
Andreas Henriksson64e2ad52007-10-12 14:37:09 +020030int tc_core_time2big(unsigned time)
Stephen Hemmingerfa565132006-10-19 13:10:26 -070031{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010032 __u64 t = time;
Stephen Hemmingerfa565132006-10-19 13:10:26 -070033
34 t *= tick_in_usec;
35 return (t >> 32) != 0;
36}
37
38
Andreas Henriksson44759842007-10-12 10:56:46 +020039unsigned tc_core_time2tick(unsigned time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010041 return time*tick_in_usec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000042}
43
Andreas Henriksson44759842007-10-12 10:56:46 +020044unsigned tc_core_tick2time(unsigned tick)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045{
46 return tick/tick_in_usec;
47}
48
Andreas Henriksson57a800d2007-10-12 13:49:49 +020049unsigned tc_core_time2ktime(unsigned time)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010050{
Patrick McHardy147e1d42007-03-04 20:15:03 +010051 return time * clock_factor;
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010052}
53
Andreas Henriksson57a800d2007-10-12 13:49:49 +020054unsigned tc_core_ktime2time(unsigned ktime)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010055{
Patrick McHardy147e1d42007-03-04 20:15:03 +010056 return ktime / clock_factor;
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010057}
58
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000059unsigned tc_calc_xmittime(unsigned rate, unsigned size)
60{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010061 return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/rate));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062}
63
Patrick McHardy76dc0aa2007-03-04 20:14:57 +010064unsigned tc_calc_xmitsize(unsigned rate, unsigned ticks)
65{
Patrick McHardy8f34caa2007-03-04 20:15:00 +010066 return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
Patrick McHardy76dc0aa2007-03-04 20:14:57 +010067}
68
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000069/*
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020070 * 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 Hemmingerd1f28cf2013-02-12 11:09:03 -080079static unsigned tc_align_to_atm(unsigned size)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020080{
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 Hemmingerd1f28cf2013-02-12 11:09:03 -080090static unsigned tc_adjust_size(unsigned sz, unsigned mpu, enum link_layer linklayer)
Jussi Kivilinna839c8452008-07-25 16:19:09 +030091{
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 Brouer292f29b2008-04-09 23:01:01 +0200105/*
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 rtab[pkt_len>>cell_log] = pkt_xmit_time
107 */
108
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200109int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
110 int cell_log, unsigned mtu,
111 enum link_layer linklayer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000112{
113 int i;
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300114 unsigned sz;
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200115 unsigned bps = r->rate;
116 unsigned mpu = r->mpu;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000117
118 if (mtu == 0)
119 mtu = 2047;
120
121 if (cell_log < 0) {
122 cell_log = 0;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200123 while ((mtu >> cell_log) > 255)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000124 cell_log++;
125 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200126
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127 for (i=0; i<256; i++) {
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300128 sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
Patrick McHardy476daa72007-03-04 20:14:56 +0100129 rtab[i] = tc_calc_xmittime(bps, sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200131
Jesper Dangaard Brouereeee3672007-09-05 15:24:51 +0200132 r->cell_align=-1; // Due to the sz calc
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200133 r->cell_log=cell_log;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000134 return cell_log;
135}
136
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300137/*
138 stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
139 */
140
141int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
142{
143 int i;
144 enum link_layer linklayer = s->linklayer;
145 unsigned int sz;
146
147 if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
148 /* don't need data table in this case (only overhead set) */
149 s->mtu = 0;
150 s->tsize = 0;
151 s->cell_log = 0;
152 s->cell_align = 0;
153 *stab = NULL;
154 return 0;
155 }
156
157 if (s->mtu == 0)
158 s->mtu = 2047;
159 if (s->tsize == 0)
160 s->tsize = 512;
161
162 s->cell_log = 0;
163 while ((s->mtu >> s->cell_log) > s->tsize - 1)
164 s->cell_log++;
165
166 *stab = malloc(s->tsize * sizeof(__u16));
167 if (!*stab)
168 return -1;
169
170again:
171 for (i = s->tsize - 1; i >= 0; i--) {
172 sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer);
173 if ((sz >> s->size_log) > UINT16_MAX) {
174 s->size_log++;
175 goto again;
176 }
177 (*stab)[i] = sz >> s->size_log;
178 }
179
180 s->cell_align = -1; // Due to the sz calc
181 return 0;
182}
183
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800184int tc_core_init(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000185{
Patrick McHardy147e1d42007-03-04 20:15:03 +0100186 FILE *fp;
187 __u32 clock_res;
188 __u32 t2us;
189 __u32 us2t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000190
Patrick McHardy147e1d42007-03-04 20:15:03 +0100191 fp = fopen("/proc/net/psched", "r");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000192 if (fp == NULL)
193 return -1;
194
Patrick McHardy147e1d42007-03-04 20:15:03 +0100195 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000196 fclose(fp);
197 return -1;
198 }
199 fclose(fp);
Patrick McHardy147e1d42007-03-04 20:15:03 +0100200
201 /* compatibility hack: for old iproute binaries (ignoring
202 * the kernel clock resolution) the kernel advertises a
203 * tick multiplier of 1000 in case of nano-second resolution,
204 * which really is 1. */
205 if (clock_res == 1000000000)
206 t2us = us2t;
207
208 clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
209 tick_in_usec = (double)t2us / us2t * clock_factor;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000210 return 0;
211}