summaryrefslogtreecommitdiffstats
path: root/python/netlink/route/qdisc/htb.py
blob: 2d3ea94de0829bf5cad65a91643795f8ddb12c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
#

"""HTB qdisc

"""

from __future__ import absolute_import

from ... import core as netlink
from ... import util as util
from ..  import capi as capi
from ..  import tc as tc

class HTBQdisc(object):
    def __init__(self, qdisc):
        self._qdisc = qdisc

    @netlink.nlattr('qdisc.htb.default_class', type=int)
    @property
    def default_class(self):
        return tc.Handle(capi.rtnl_htb_get_defcls(self._qdisc._rtnl_qdisc))

    @default_class.setter
    def default_class(self, value):
        capi.rtnl_htb_set_defcls(self._qdisc._rtnl_qdisc, int(value))

    @netlink.nlattr('qdisc.htb.r2q', type=int)
    @property
    def r2q(self):
        return capi.rtnl_htb_get_rate2quantum(self._qdisc._rtnl_qdisc)

    @r2q.setter
    def r2q(self, value):
        capi.rtnl_htb_get_rate2quantum(self._qdisc._rtnl_qdisc,
                           int(value))

    def brief(self):
        fmt = util.MyFormatter(self)

        ret = ' {s|default-class!k} {a|default_class}'

        if self.r2q:
            ret += ' {s|r2q!k} {a|r2q}'

        return fmt.format(ret)

class HTBClass(object):
    def __init__(self, cl):
        self._class = cl

    @netlink.nlattr('class.htb.rate', type=str)
    @property
    def rate(self):
        rate = capi.rtnl_htb_get_rate(self._class._rtnl_class)
        return util.Rate(rate)

    @rate.setter
    def rate(self, value):
        capi.rtnl_htb_set_rate(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.ceil', type=str)
    @property
    def ceil(self):
        ceil = capi.rtnl_htb_get_ceil(self._class._rtnl_class)
        return util.Rate(ceil)

    @ceil.setter
    def ceil(self, value):
        capi.rtnl_htb_set_ceil(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.burst', type=str)
    @property
    def burst(self):
        burst = capi.rtnl_htb_get_rbuffer(self._class._rtnl_class)
        return util.Size(burst)

    @burst.setter
    def burst(self, value):
        capi.rtnl_htb_set_rbuffer(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.ceil_burst', type=str)
    @property
    def ceil_burst(self):
        burst = capi.rtnl_htb_get_cbuffer(self._class._rtnl_class)
        return util.Size(burst)

    @ceil_burst.setter
    def ceil_burst(self, value):
        capi.rtnl_htb_set_cbuffer(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.prio', type=int)
    @property
    def prio(self):
        return capi.rtnl_htb_get_prio(self._class._rtnl_class)

    @prio.setter
    def prio(self, value):
        capi.rtnl_htb_set_prio(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.quantum', type=int)
    @property
    def quantum(self):
        return capi.rtnl_htb_get_quantum(self._class._rtnl_class)

    @quantum.setter
    def quantum(self, value):
        capi.rtnl_htb_set_quantum(self._class._rtnl_class, int(value))

    @netlink.nlattr('class.htb.level', type=int)
    @property
    def level(self):
        return capi.rtnl_htb_get_level(self._class._rtnl_class)

    @level.setter
    def level(self, value):
        capi.rtnl_htb_set_level(self._class._rtnl_class, int(value))

    def brief(self):
        fmt = util.MyFormatter(self)

        ret = ' {t|prio} {t|rate}'

        if self.rate != self.ceil:
            ret += ' {s|borrow-up-to!k} {a|ceil}'

        ret += ' {t|burst}'

        return fmt.format(ret)

    def details(self):
        fmt = util.MyFormatter(self)

        return fmt.nl('\t{t|level} {t|quantum}')

def init_qdisc(qdisc):
    qdisc.htb = HTBQdisc(qdisc)
    return qdisc.htb

def init_class(cl):
    cl.htb = HTBClass(cl)
    return cl.htb

#extern void rtnl_htb_set_quantum(struct rtnl_class *, uint32_t quantum);