• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
3#
4
5"""HTB qdisc
6
7"""
8
9from __future__ import absolute_import
10
11from ... import core as netlink
12from ... import util as util
13from ..  import capi as capi
14from ..  import tc as tc
15
16class HTBQdisc(object):
17    def __init__(self, qdisc):
18        self._qdisc = qdisc
19
20    @property
21    @netlink.nlattr(type=int)
22    def default_class(self):
23        return tc.Handle(capi.rtnl_htb_get_defcls(self._qdisc._rtnl_qdisc))
24
25    @default_class.setter
26    def default_class(self, value):
27        capi.rtnl_htb_set_defcls(self._qdisc._rtnl_qdisc, int(value))
28
29    @property
30    @netlink.nlattr('r2q', type=int)
31    def r2q(self):
32        return capi.rtnl_htb_get_rate2quantum(self._qdisc._rtnl_qdisc)
33
34    @r2q.setter
35    def r2q(self, value):
36        capi.rtnl_htb_get_rate2quantum(self._qdisc._rtnl_qdisc,
37                           int(value))
38
39    def brief(self):
40        fmt = util.MyFormatter(self)
41
42        ret = ' {s|default-class!k} {a|default_class}'
43
44        if self.r2q:
45            ret += ' {s|r2q!k} {a|r2q}'
46
47        return fmt.format(ret)
48
49class HTBClass(object):
50    def __init__(self, cl):
51        self._class = cl
52
53    @property
54    @netlink.nlattr(type=str)
55    def rate(self):
56        rate = capi.rtnl_htb_get_rate(self._class._rtnl_class)
57        return util.Rate(rate)
58
59    @rate.setter
60    def rate(self, value):
61        capi.rtnl_htb_set_rate(self._class._rtnl_class, int(value))
62
63    @property
64    @netlink.nlattr(type=str)
65    def ceil(self):
66        ceil = capi.rtnl_htb_get_ceil(self._class._rtnl_class)
67        return util.Rate(ceil)
68
69    @ceil.setter
70    def ceil(self, value):
71        capi.rtnl_htb_set_ceil(self._class._rtnl_class, int(value))
72
73    @property
74    @netlink.nlattr(type=str)
75    def burst(self):
76        burst = capi.rtnl_htb_get_rbuffer(self._class._rtnl_class)
77        return util.Size(burst)
78
79    @burst.setter
80    def burst(self, value):
81        capi.rtnl_htb_set_rbuffer(self._class._rtnl_class, int(value))
82
83    @property
84    @netlink.nlattr(type=str)
85    def ceil_burst(self):
86        burst = capi.rtnl_htb_get_cbuffer(self._class._rtnl_class)
87        return util.Size(burst)
88
89    @ceil_burst.setter
90    def ceil_burst(self, value):
91        capi.rtnl_htb_set_cbuffer(self._class._rtnl_class, int(value))
92
93    @property
94    @netlink.nlattr(type=int)
95    def prio(self):
96        return capi.rtnl_htb_get_prio(self._class._rtnl_class)
97
98    @prio.setter
99    def prio(self, value):
100        capi.rtnl_htb_set_prio(self._class._rtnl_class, int(value))
101
102    @property
103    @netlink.nlattr(type=int)
104    def quantum(self):
105        return capi.rtnl_htb_get_quantum(self._class._rtnl_class)
106
107    @quantum.setter
108    def quantum(self, value):
109        capi.rtnl_htb_set_quantum(self._class._rtnl_class, int(value))
110
111    @property
112    @netlink.nlattr(type=int)
113    def level(self):
114        return capi.rtnl_htb_get_level(self._class._rtnl_class)
115
116    @level.setter
117    def level(self, value):
118        capi.rtnl_htb_set_level(self._class._rtnl_class, int(value))
119
120    def brief(self):
121        fmt = util.MyFormatter(self)
122
123        ret = ' {t|prio} {t|rate}'
124
125        if self.rate != self.ceil:
126            ret += ' {s|borrow-up-to!k} {a|ceil}'
127
128        ret += ' {t|burst}'
129
130        return fmt.format(ret)
131
132    def details(self):
133        fmt = util.MyFormatter(self)
134
135        return fmt.nl('\t{t|level} {t|quantum}')
136
137def init_qdisc(qdisc):
138    qdisc.htb = HTBQdisc(qdisc)
139    return qdisc.htb
140
141def init_class(cl):
142    cl.htb = HTBClass(cl)
143    return cl.htb
144
145#extern void rtnl_htb_set_quantum(struct rtnl_class *, uint32_t quantum);
146