blob: 855c11546570f9b32ae5c70339ee01463dfe0484 [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 */
79unsigned 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!shemmingeraba5acd2004-04-15 20:56:59 +000091 rtab[pkt_len>>cell_log] = pkt_xmit_time
92 */
93
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020094int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
95 int cell_log, unsigned mtu,
96 enum link_layer linklayer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000097{
98 int i;
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +020099 unsigned bps = r->rate;
100 unsigned mpu = r->mpu;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101
102 if (mtu == 0)
103 mtu = 2047;
104
105 if (cell_log < 0) {
106 cell_log = 0;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200107 while ((mtu >> cell_log) > 255)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 cell_log++;
109 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200110
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000111 for (i=0; i<256; i++) {
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200112 unsigned sz = (i+1)<<cell_log;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000113 if (sz < mpu)
114 sz = mpu;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200115
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 McHardy476daa72007-03-04 20:14:56 +0100127 rtab[i] = tc_calc_xmittime(bps, sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000128 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200129
Jesper Dangaard Brouereeee3672007-09-05 15:24:51 +0200130 r->cell_align=-1; // Due to the sz calc
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200131 r->cell_log=cell_log;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000132 return cell_log;
133}
134
135int tc_core_init()
136{
Patrick McHardy147e1d42007-03-04 20:15:03 +0100137 FILE *fp;
138 __u32 clock_res;
139 __u32 t2us;
140 __u32 us2t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000141
Patrick McHardy147e1d42007-03-04 20:15:03 +0100142 fp = fopen("/proc/net/psched", "r");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143 if (fp == NULL)
144 return -1;
145
Patrick McHardy147e1d42007-03-04 20:15:03 +0100146 if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000147 fclose(fp);
148 return -1;
149 }
150 fclose(fp);
Patrick McHardy147e1d42007-03-04 20:15:03 +0100151
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!shemmingeraba5acd2004-04-15 20:56:59 +0000161 return 0;
162}