2004-04-16 04:56:59 +08:00
|
|
|
/*
|
|
|
|
* tc_core.c TC core library.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-05-21 05:28:46 +08:00
|
|
|
#include <stdint.h>
|
2004-04-16 04:56:59 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-08-27 10:42:28 +08:00
|
|
|
#include "utils.h"
|
2004-04-16 04:56:59 +08:00
|
|
|
#include "tc_core.h"
|
2008-04-10 05:01:01 +08:00
|
|
|
#include <linux/atm.h>
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
static double tick_in_usec = 1;
|
2007-03-05 03:15:03 +08:00
|
|
|
static double clock_factor = 1;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int tc_core_time2big(unsigned int time)
|
2006-10-20 04:10:26 +08:00
|
|
|
{
|
2007-03-05 03:15:00 +08:00
|
|
|
__u64 t = time;
|
2006-10-20 04:10:26 +08:00
|
|
|
|
|
|
|
t *= tick_in_usec;
|
|
|
|
return (t >> 32) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_core_time2tick(unsigned int time)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2007-03-05 03:15:00 +08:00
|
|
|
return time*tick_in_usec;
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_core_tick2time(unsigned int tick)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
return tick/tick_in_usec;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_core_time2ktime(unsigned int time)
|
2007-03-05 03:14:59 +08:00
|
|
|
{
|
2007-03-05 03:15:03 +08:00
|
|
|
return time * clock_factor;
|
2007-03-05 03:14:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_core_ktime2time(unsigned int ktime)
|
2007-03-05 03:14:59 +08:00
|
|
|
{
|
2007-03-05 03:15:03 +08:00
|
|
|
return ktime / clock_factor;
|
2007-03-05 03:14:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_calc_xmittime(__u64 rate, unsigned int size)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2013-11-13 06:34:07 +08:00
|
|
|
return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate));
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int tc_calc_xmitsize(__u64 rate, unsigned int ticks)
|
2007-03-05 03:14:57 +08:00
|
|
|
{
|
2007-03-05 03:15:00 +08:00
|
|
|
return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
|
2007-03-05 03:14:57 +08:00
|
|
|
}
|
|
|
|
|
2008-04-10 05:01:01 +08:00
|
|
|
/*
|
|
|
|
* The align to ATM cells is used for determining the (ATM) SAR
|
|
|
|
* alignment overhead at the ATM layer. (SAR = Segmentation And
|
|
|
|
* Reassembly). This is for example needed when scheduling packet on
|
|
|
|
* an ADSL connection. Note that the extra ATM-AAL overhead is _not_
|
|
|
|
* included in this calculation. This overhead is added in the kernel
|
|
|
|
* before doing the rate table lookup, as this gives better precision
|
|
|
|
* (as the table will always be aligned for 48 bytes).
|
|
|
|
* --Hawk, d.7/11-2004. <hawk@diku.dk>
|
|
|
|
*/
|
2016-03-22 02:48:36 +08:00
|
|
|
static unsigned int tc_align_to_atm(unsigned int size)
|
2008-04-10 05:01:01 +08:00
|
|
|
{
|
|
|
|
int linksize, cells;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2008-04-10 05:01:01 +08:00
|
|
|
cells = size / ATM_CELL_PAYLOAD;
|
|
|
|
if ((size % ATM_CELL_PAYLOAD) > 0)
|
|
|
|
cells++;
|
|
|
|
|
|
|
|
linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
|
|
|
|
return linksize;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
static unsigned int tc_adjust_size(unsigned int sz, unsigned int mpu, enum link_layer linklayer)
|
2008-07-25 21:19:09 +08:00
|
|
|
{
|
|
|
|
if (sz < mpu)
|
|
|
|
sz = mpu;
|
|
|
|
|
|
|
|
switch (linklayer) {
|
|
|
|
case LINKLAYER_ATM:
|
|
|
|
return tc_align_to_atm(sz);
|
|
|
|
case LINKLAYER_ETHERNET:
|
|
|
|
default:
|
2016-03-22 02:48:36 +08:00
|
|
|
/* No size adjustments on Ethernet */
|
2008-07-25 21:19:09 +08:00
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 20:02:10 +08:00
|
|
|
/* Notice, the rate table calculated here, have gotten replaced in the
|
|
|
|
* kernel and is no-longer used for lookups.
|
|
|
|
*
|
|
|
|
* This happened in kernel release v3.8 caused by kernel
|
|
|
|
* - commit 56b765b79 ("htb: improved accuracy at high rates").
|
|
|
|
* This change unfortunately caused breakage of tc overhead and
|
|
|
|
* linklayer parameters.
|
|
|
|
*
|
|
|
|
* Kernel overhead handling got fixed in kernel v3.10 by
|
|
|
|
* - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling)
|
|
|
|
*
|
|
|
|
* Kernel linklayer handling got fixed in kernel v3.11 by
|
|
|
|
* - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling)
|
|
|
|
*/
|
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
/*
|
|
|
|
rtab[pkt_len>>cell_log] = pkt_xmit_time
|
|
|
|
*/
|
|
|
|
|
2008-04-10 05:01:01 +08:00
|
|
|
int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
|
2016-03-22 02:48:36 +08:00
|
|
|
int cell_log, unsigned int mtu,
|
2008-04-10 05:01:01 +08:00
|
|
|
enum link_layer linklayer)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
int i;
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned int sz;
|
|
|
|
unsigned int bps = r->rate;
|
|
|
|
unsigned int mpu = r->mpu;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
if (mtu == 0)
|
|
|
|
mtu = 2047;
|
|
|
|
|
|
|
|
if (cell_log < 0) {
|
|
|
|
cell_log = 0;
|
2008-04-10 05:01:01 +08:00
|
|
|
while ((mtu >> cell_log) > 255)
|
2004-04-16 04:56:59 +08:00
|
|
|
cell_log++;
|
|
|
|
}
|
2008-04-10 05:01:01 +08:00
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
for (i = 0; i < 256; i++) {
|
2008-07-25 21:19:09 +08:00
|
|
|
sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
|
2007-03-05 03:14:56 +08:00
|
|
|
rtab[i] = tc_calc_xmittime(bps, sz);
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
2008-04-10 05:01:01 +08:00
|
|
|
|
2016-03-28 01:47:46 +08:00
|
|
|
r->cell_align = -1;
|
2016-03-22 02:48:36 +08:00
|
|
|
r->cell_log = cell_log;
|
2013-08-30 20:02:10 +08:00
|
|
|
r->linklayer = (linklayer & TC_LINKLAYER_MASK);
|
2004-04-16 04:56:59 +08:00
|
|
|
return cell_log;
|
|
|
|
}
|
|
|
|
|
2008-07-25 21:19:09 +08:00
|
|
|
/*
|
|
|
|
stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
|
|
|
|
*/
|
|
|
|
|
|
|
|
int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
enum link_layer linklayer = s->linklayer;
|
|
|
|
unsigned int sz;
|
|
|
|
|
|
|
|
if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
|
|
|
|
/* don't need data table in this case (only overhead set) */
|
|
|
|
s->mtu = 0;
|
|
|
|
s->tsize = 0;
|
|
|
|
s->cell_log = 0;
|
|
|
|
s->cell_align = 0;
|
|
|
|
*stab = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mtu == 0)
|
|
|
|
s->mtu = 2047;
|
|
|
|
if (s->tsize == 0)
|
|
|
|
s->tsize = 512;
|
|
|
|
|
|
|
|
s->cell_log = 0;
|
|
|
|
while ((s->mtu >> s->cell_log) > s->tsize - 1)
|
|
|
|
s->cell_log++;
|
|
|
|
|
|
|
|
*stab = malloc(s->tsize * sizeof(__u16));
|
|
|
|
if (!*stab)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
again:
|
|
|
|
for (i = s->tsize - 1; i >= 0; i--) {
|
|
|
|
sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer);
|
|
|
|
if ((sz >> s->size_log) > UINT16_MAX) {
|
|
|
|
s->size_log++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
(*stab)[i] = sz >> s->size_log;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
s->cell_align = -1; /* Due to the sz calc */
|
2008-07-25 21:19:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
int tc_core_init(void)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2007-03-05 03:15:03 +08:00
|
|
|
FILE *fp;
|
|
|
|
__u32 clock_res;
|
|
|
|
__u32 t2us;
|
|
|
|
__u32 us2t;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2007-03-05 03:15:03 +08:00
|
|
|
fp = fopen("/proc/net/psched", "r");
|
2004-04-16 04:56:59 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2007-03-05 03:15:03 +08:00
|
|
|
if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
|
2004-04-16 04:56:59 +08:00
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2007-03-05 03:15:03 +08:00
|
|
|
|
|
|
|
/* compatibility hack: for old iproute binaries (ignoring
|
|
|
|
* the kernel clock resolution) the kernel advertises a
|
|
|
|
* tick multiplier of 1000 in case of nano-second resolution,
|
|
|
|
* which really is 1. */
|
|
|
|
if (clock_res == 1000000000)
|
|
|
|
t2us = us2t;
|
|
|
|
|
|
|
|
clock_factor = (double)clock_res / TIME_UNITS_PER_SEC;
|
|
|
|
tick_in_usec = (double)t2us / us2t * clock_factor;
|
2004-04-16 04:56:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|