iproute2/tc/tc_core.c
<<
>>
Prefs
   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 <stdint.h>
  16#include <unistd.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 "utils.h"
  25#include "tc_core.h"
  26#include <linux/atm.h>
  27
  28static double tick_in_usec = 1;
  29static double clock_factor = 1;
  30
  31int tc_core_time2big(unsigned int time)
  32{
  33        __u64 t = time;
  34
  35        t *= tick_in_usec;
  36        return (t >> 32) != 0;
  37}
  38
  39
  40unsigned int tc_core_time2tick(unsigned int time)
  41{
  42        return time*tick_in_usec;
  43}
  44
  45unsigned int tc_core_tick2time(unsigned int tick)
  46{
  47        return tick/tick_in_usec;
  48}
  49
  50unsigned int tc_core_time2ktime(unsigned int time)
  51{
  52        return time * clock_factor;
  53}
  54
  55unsigned int tc_core_ktime2time(unsigned int ktime)
  56{
  57        return ktime / clock_factor;
  58}
  59
  60unsigned int tc_calc_xmittime(__u64 rate, unsigned int size)
  61{
  62        return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate));
  63}
  64
  65unsigned int tc_calc_xmitsize(__u64 rate, unsigned int ticks)
  66{
  67        return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
  68}
  69
  70/*
  71 * The align to ATM cells is used for determining the (ATM) SAR
  72 * alignment overhead at the ATM layer. (SAR = Segmentation And
  73 * Reassembly).  This is for example needed when scheduling packet on
  74 * an ADSL connection.  Note that the extra ATM-AAL overhead is _not_
  75 * included in this calculation. This overhead is added in the kernel
  76 * before doing the rate table lookup, as this gives better precision
  77 * (as the table will always be aligned for 48 bytes).
  78 *  --Hawk, d.7/11-2004. <hawk@diku.dk>
  79 */
  80static unsigned int tc_align_to_atm(unsigned int size)
  81{
  82        int linksize, cells;
  83
  84        cells = size / ATM_CELL_PAYLOAD;
  85        if ((size % ATM_CELL_PAYLOAD) > 0)
  86                cells++;
  87
  88        linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
  89        return linksize;
  90}
  91
  92static unsigned int tc_adjust_size(unsigned int sz, unsigned int mpu, enum link_layer linklayer)
  93{
  94        if (sz < mpu)
  95                sz = mpu;
  96
  97        switch (linklayer) {
  98        case LINKLAYER_ATM:
  99                return tc_align_to_atm(sz);
 100        case LINKLAYER_ETHERNET:
 101        default:
 102                /* No size adjustments on Ethernet */
 103                return sz;
 104        }
 105}
 106
 107/* Notice, the rate table calculated here, have gotten replaced in the
 108 * kernel and is no-longer used for lookups.
 109 *
 110 * This happened in kernel release v3.8 caused by kernel
 111 *  - commit 56b765b79 ("htb: improved accuracy at high rates").
 112 * This change unfortunately caused breakage of tc overhead and
 113 * linklayer parameters.
 114 *
 115 * Kernel overhead handling got fixed in kernel v3.10 by
 116 * - commit 01cb71d2d47 (net_sched: restore "overhead xxx" handling)
 117 *
 118 * Kernel linklayer handling got fixed in kernel v3.11 by
 119 * - commit 8a8e3d84b17 (net_sched: restore "linklayer atm" handling)
 120 */
 121
 122/*
 123   rtab[pkt_len>>cell_log] = pkt_xmit_time
 124 */
 125
 126int tc_calc_rtable(struct tc_ratespec *r, __u32 *rtab,
 127                   int cell_log, unsigned int mtu,
 128                   enum link_layer linklayer)
 129{
 130        int i;
 131        unsigned int sz;
 132        unsigned int bps = r->rate;
 133        unsigned int mpu = r->mpu;
 134
 135        if (mtu == 0)
 136                mtu = 2047;
 137
 138        if (cell_log < 0) {
 139                cell_log = 0;
 140                while ((mtu >> cell_log) > 255)
 141                        cell_log++;
 142        }
 143
 144        for (i = 0; i < 256; i++) {
 145                sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
 146                rtab[i] = tc_calc_xmittime(bps, sz);
 147        }
 148
 149        r->cell_align =  -1;
 150        r->cell_log = cell_log;
 151        r->linklayer = (linklayer & TC_LINKLAYER_MASK);
 152        return cell_log;
 153}
 154
 155int tc_calc_rtable_64(struct tc_ratespec *r, __u32 *rtab,
 156                   int cell_log, unsigned int mtu,
 157                   enum link_layer linklayer, __u64 rate)
 158{
 159        int i;
 160        unsigned int sz;
 161        __u64 bps = rate;
 162        unsigned int mpu = r->mpu;
 163
 164        if (mtu == 0)
 165                mtu = 2047;
 166
 167        if (cell_log < 0) {
 168                cell_log = 0;
 169                while ((mtu >> cell_log) > 255)
 170                        cell_log++;
 171        }
 172
 173        for (i = 0; i < 256; i++) {
 174                sz = tc_adjust_size((i + 1) << cell_log, mpu, linklayer);
 175                rtab[i] = tc_calc_xmittime(bps, sz);
 176        }
 177
 178        r->cell_align =  -1;
 179        r->cell_log = cell_log;
 180        r->linklayer = (linklayer & TC_LINKLAYER_MASK);
 181        return cell_log;
 182}
 183
 184/*
 185   stab[pkt_len>>cell_log] = pkt_xmit_size>>size_log
 186 */
 187
 188int tc_calc_size_table(struct tc_sizespec *s, __u16 **stab)
 189{
 190        int i;
 191        enum link_layer linklayer = s->linklayer;
 192        unsigned int sz;
 193
 194        if (linklayer <= LINKLAYER_ETHERNET && s->mpu == 0) {
 195                /* don't need data table in this case (only overhead set) */
 196                s->mtu = 0;
 197                s->tsize = 0;
 198                s->cell_log = 0;
 199                s->cell_align = 0;
 200                *stab = NULL;
 201                return 0;
 202        }
 203
 204        if (s->mtu == 0)
 205                s->mtu = 2047;
 206        if (s->tsize == 0)
 207                s->tsize = 512;
 208
 209        s->cell_log = 0;
 210        while ((s->mtu >> s->cell_log) > s->tsize - 1)
 211                s->cell_log++;
 212
 213        *stab = malloc(s->tsize * sizeof(__u16));
 214        if (!*stab)
 215                return -1;
 216
 217again:
 218        for (i = s->tsize - 1; i >= 0; i--) {
 219                sz = tc_adjust_size((i + 1) << s->cell_log, s->mpu, linklayer);
 220                if ((sz >> s->size_log) > UINT16_MAX) {
 221                        s->size_log++;
 222                        goto again;
 223                }
 224                (*stab)[i] = sz >> s->size_log;
 225        }
 226
 227        s->cell_align = -1; /* Due to the sz calc */
 228        return 0;
 229}
 230
 231int tc_core_init(void)
 232{
 233        FILE *fp;
 234        __u32 clock_res;
 235        __u32 t2us;
 236        __u32 us2t;
 237
 238        fp = fopen("/proc/net/psched", "r");
 239        if (fp == NULL)
 240                return -1;
 241
 242        if (fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clock_res) != 3) {
 243                fclose(fp);
 244                return -1;
 245        }
 246        fclose(fp);
 247
 248        /* compatibility hack: for old iproute binaries (ignoring
 249         * the kernel clock resolution) the kernel advertises a
 250         * tick multiplier of 1000 in case of nano-second resolution,
 251         * which really is 1. */
 252        if (clock_res == 1000000000)
 253                t2us = us2t;
 254
 255        clock_factor  = (double)clock_res / TIME_UNITS_PER_SEC;
 256        tick_in_usec = (double)t2us / us2t * clock_factor;
 257        return 0;
 258}
 259