예제 #1
0
def get_class_parameters(kwarg):
    ret = {'attrs': []}
    for key in ('rsc', 'fsc', 'usc'):
        if key in kwarg:
            ret['attrs'].append([
                'TCA_HFSC_%s' % key.upper(), {
                    'm1': get_rate(kwarg[key].get('m1', 0)),
                    'd': get_time(kwarg[key].get('d', 0)),
                    'm2': get_rate(kwarg[key].get('m2', 0))
                }
            ])
    return ret
예제 #2
0
def get_class_parameters(kwarg):
    prio = kwarg.get('prio', 0)
    mtu = kwarg.get('mtu', 1600)
    mpu = kwarg.get('mpu', 0)
    overhead = kwarg.get('overhead', 0)
    quantum = kwarg.get('quantum', 0)
    rate = get_rate(kwarg.get('rate', None))
    ceil = get_rate(kwarg.get('ceil', 0)) or rate

    burst = (
        kwarg.get('burst', None)
        or kwarg.get('maxburst', None)
        or kwarg.get('buffer', None)
    )

    if rate is not None:
        if burst is None:
            burst = rate / get_hz() + mtu
        burst = calc_xmittime(rate, burst)

    cburst = (
        kwarg.get('cburst', None)
        or kwarg.get('cmaxburst', None)
        or kwarg.get('cbuffer', None)
    )

    if ceil is not None:
        if cburst is None:
            cburst = ceil / get_hz() + mtu
        cburst = calc_xmittime(ceil, cburst)

    return {
        'attrs': [
            [
                'TCA_HTB_PARMS',
                {
                    'buffer': burst,
                    'cbuffer': cburst,
                    'quantum': quantum,
                    'prio': prio,
                    'rate': rate,
                    'ceil': ceil,
                    'ceil_overhead': overhead,
                    'rate_overhead': overhead,
                    'rate_mpu': mpu,
                    'ceil_mpu': mpu,
                },
            ],
            ['TCA_HTB_RTAB', True],
            ['TCA_HTB_CTAB', True],
        ]
    }
예제 #3
0
def get_parameters(kwarg):
    # The code is ported from iproute2
    avpkt = 1000
    probability = 0.02
    opt = {
        'limit': kwarg['limit'],  # required
        'qth_min': kwarg.get('min', 0),
        'qth_max': kwarg.get('max', 0),
        'Wlog': 0,
        'Plog': 0,
        'Scell_log': 0,
        'flags': 1 if kwarg.get('ecn') else 0,
    }

    rate = get_rate(kwarg['bandwith'])  # required
    burst = kwarg.get('burst', 0)
    avpkt = get_size(kwarg.get('avpkt', 1000))
    probability = kwarg.get('probability', 0.02)

    if not opt['qth_max']:
        opt['qth_max'] = opt['limit'] // 4
    if not opt['qth_min']:
        opt['qth_min'] = opt['qth_max'] // 3
    if not burst:
        burst = (2 * opt['qth_min'] + opt['qth_max']) // 3

    if opt['qth_max'] > opt['limit']:
        raise Exception('max is larger than limit')
    if opt['qth_min'] >= opt['qth_max']:
        raise Exception('min is not smaller than max')

    # Wlog
    opt['Wlog'] = red_eval_ewma(opt['qth_min'] * avpkt, burst, avpkt)
    if opt['Wlog'] < 0:
        raise Exception('failed to calculate EWMA')
    elif opt['Wlog'] > 10:
        log.warning('choke: burst %s seems to be too large' % burst)
    # Plog
    opt['Plog'] = red_eval_P(opt['qth_min'] * avpkt, opt['qth_max'] * avpkt,
                             probability)
    if opt['Plog'] < 0:
        raise Exception('choke: failed to calculate probability')
    # Scell_log, stab
    opt['Scell_log'], stab = red_eval_idle_damping(opt['Wlog'], avpkt, rate)
    if opt['Scell_log'] < 0:
        raise Exception('choke: failed to calculate idle damping table')

    return {
        'attrs': [
            ['TCA_CHOKE_PARMS', opt],
            ['TCA_CHOKE_STAB', stab],
            ['TCA_CHOKE_MAX_P',
             int(probability * pow(2, 32))],
        ]
    }
예제 #4
0
def get_parameters(kwarg):
    delay = time2tick(kwarg.get('delay', 0))  # in microsecond
    limit = kwarg.get('limit', 1000)  # fifo limit (packets) see netem.c:230
    loss = percent2u32(kwarg.get('loss', 0))  # int percentage
    gap = kwarg.get('gap', 0)
    duplicate = kwarg.get('duplicate', 0)
    jitter = time2tick(kwarg.get('jitter', 0))  # in microsecond

    opts = {
        'delay': delay,
        'limit': limit,
        'loss': loss,
        'gap': gap,
        'duplicate': duplicate,
        'jitter': jitter,
        'attrs': [],
    }

    # correlation (delay, loss, duplicate)
    delay_corr = percent2u32(kwarg.get('delay_corr', 0))
    loss_corr = percent2u32(kwarg.get('loss_corr', 0))
    dup_corr = percent2u32(kwarg.get('dup_corr', 0))
    if delay_corr or loss_corr or dup_corr:
        # delay_corr requires that both jitter and delay are != 0
        if delay_corr and not (delay and jitter):
            raise Exception(
                'delay correlation requires delay' ' and jitter to be set'
            )
        # loss correlation and loss
        if loss_corr and not loss:
            raise Exception('loss correlation requires loss to be set')
        # duplicate correlation and duplicate
        if dup_corr and not duplicate:
            raise Exception(
                'duplicate correlation requires ' 'duplicate to be set'
            )

        opts['attrs'].append(
            [
                'TCA_NETEM_CORR',
                {
                    'delay_corr': delay_corr,
                    'loss_corr': loss_corr,
                    'dup_corr': dup_corr,
                },
            ]
        )

    # reorder (probability, correlation)
    prob_reorder = percent2u32(kwarg.get('prob_reorder', 0))
    corr_reorder = percent2u32(kwarg.get('corr_reorder', 0))
    if prob_reorder != 0:
        # gap defaults to 1 if equal to 0
        if gap == 0:
            opts['gap'] = gap = 1
        opts['attrs'].append(
            [
                'TCA_NETEM_REORDER',
                {'prob_reorder': prob_reorder, 'corr_reorder': corr_reorder},
            ]
        )
    else:
        if gap != 0:
            raise Exception('gap can only be set when prob_reorder is set')
        elif corr_reorder != 0:
            raise Exception(
                'corr_reorder can only be set when ' 'prob_reorder is set'
            )

    # corrupt (probability, correlation)
    prob_corrupt = percent2u32(kwarg.get('prob_corrupt', 0))
    corr_corrupt = percent2u32(kwarg.get('corr_corrupt', 0))
    if prob_corrupt:
        opts['attrs'].append(
            [
                'TCA_NETEM_CORRUPT',
                {'prob_corrupt': prob_corrupt, 'corr_corrupt': corr_corrupt},
            ]
        )
    elif corr_corrupt != 0:
        raise Exception(
            'corr_corrupt can only be set when ' 'prob_corrupt is set'
        )

    # rate (rate, packet_overhead, cell_size, cell_overhead)
    rate = get_rate(kwarg.get('rate', None))
    packet_overhead = kwarg.get('packet_overhead', 0)
    cell_size = kwarg.get('cell_size', 0)
    cell_overhead = kwarg.get('cell_overhead', 0)
    if rate is not None:
        opts['attrs'].append(
            [
                'TCA_NETEM_RATE',
                {
                    'rate': rate,
                    'packet_overhead': packet_overhead,
                    'cell_size': cell_size,
                    'cell_overhead': cell_overhead,
                },
            ]
        )
    elif packet_overhead != 0 or cell_size != 0 or cell_overhead != 0:
        raise Exception(
            'packet_overhead, cell_size and cell_overhead'
            'can only be set when rate is set'
        )

    # TODO
    # delay distribution (dist_size, dist_data)
    return opts