summaryrefslogtreecommitdiff
path: root/python/vyos/qos/trafficshaper.py
blob: 8b0333c217d3bce6ee0f1dd9e4999ab4556cd652 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright 2022-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

from math import ceil
from vyos.qos.base import QoSBase

# Kernel limits on quantum (bytes)
MAXQUANTUM = 200000
MINQUANTUM = 1000

class TrafficShaper(QoSBase):
    _parent = 1
    qostype = 'shaper'

    # https://man7.org/linux/man-pages/man8/tc-htb.8.html
    def update(self, config, direction):
        class_id_max = 0
        if 'class' in config:
            tmp = list(config['class'])
            # Convert strings to integers
            tmp = [int(x) for x in tmp]
            class_id_max = max(tmp)

        r2q = 10
        # bandwidth is a mandatory CLI node
        speed = self._rate_convert(config['bandwidth'])
        speed_bps = int(speed) // 8

        # need a bigger r2q if going fast than 16 mbits/sec
        if (speed_bps // r2q) >= MAXQUANTUM: # integer division
            r2q = ceil(speed_bps / MAXQUANTUM)
        else:
            # if there is a slow class then may need smaller value
            if 'class' in config:
                min_speed = speed_bps
                for cls, cls_options in config['class'].items():
                    # find class with the lowest bandwidth used
                    if 'bandwidth' in cls_options:
                        bw_bps = int(self._rate_convert(cls_options['bandwidth'])) // 8 # bandwidth in bytes per second
                        if bw_bps < min_speed:
                            min_speed = bw_bps

                while (r2q > 1) and (min_speed // r2q) < MINQUANTUM:
                    tmp = r2q -1
                    if (speed_bps // tmp) >= MAXQUANTUM:
                        break
                    r2q = tmp


        default_minor_id = int(class_id_max) +1
        tmp = f'tc qdisc replace dev {self._interface} root handle {self._parent:x}: htb r2q {r2q} default {default_minor_id:x}' # default is in hex
        self._cmd(tmp)

        tmp = f'tc class replace dev {self._interface} parent {self._parent:x}: classid {self._parent:x}:1 htb rate {speed}'
        self._cmd(tmp)

        if 'class' in config:
            for cls, cls_config in config['class'].items():
                # class id is used later on and passed as hex, thus this needs to be an int
                cls = int(cls)

                # bandwidth is a mandatory CLI node
                # T5296 if bandwidth 'auto' or 'xx%' get value from config shaper total "bandwidth"
                # i.e from  set shaper test bandwidth '300mbit'
                # without it, it tries to get value from qos.base /sys/class/net/{self._interface}/speed
                if cls_config['bandwidth'] == 'auto':
                    rate = self._rate_convert(config['bandwidth'])
                elif cls_config['bandwidth'].endswith('%'):
                    percent = cls_config['bandwidth'].rstrip('%')
                    rate = self._rate_convert(config['bandwidth']) * int(percent) // 100
                else:
                    rate = self._rate_convert(cls_config['bandwidth'])

                burst = cls_config['burst']
                quantum = cls_config['codel_quantum']

                tmp = f'tc class replace dev {self._interface} parent {self._parent:x}:1 classid {self._parent:x}:{cls:x} htb rate {rate} burst {burst} quantum {quantum}'
                if 'priority' in cls_config:
                    priority = cls_config['priority']
                    tmp += f' prio {priority}'

                if 'ceiling' in cls_config:
                    f_ceil = self._rate_convert(cls_config['ceiling'])
                    tmp += f' ceil {f_ceil}'
                self._cmd(tmp)

                tmp = f'tc qdisc replace dev {self._interface} parent {self._parent:x}:{cls:x} sfq'
                self._cmd(tmp)

        if 'default' in config:
                if config['default']['bandwidth'].endswith('%'):
                    percent = config['default']['bandwidth'].rstrip('%')
                    rate = self._rate_convert(config['bandwidth']) * int(percent) // 100
                else:
                    rate = self._rate_convert(config['default']['bandwidth'])
                burst = config['default']['burst']
                quantum = config['default']['codel_quantum']
                tmp = f'tc class replace dev {self._interface} parent {self._parent:x}:1 classid {self._parent:x}:{default_minor_id:x} htb rate {rate} burst {burst} quantum {quantum}'
                if 'priority' in config['default']:
                    priority = config['default']['priority']
                    tmp += f' prio {priority}'
                if 'ceiling' in config['default']:
                    if config['default']['ceiling'].endswith('%'):
                        percent = config['default']['ceiling'].rstrip('%')
                        f_ceil = self._rate_convert(config['bandwidth']) * int(percent) // 100
                    else:
                        f_ceil = self._rate_convert(config['default']['ceiling'])
                    tmp += f' ceil {f_ceil}'
                self._cmd(tmp)

                tmp = f'tc qdisc replace dev {self._interface} parent {self._parent:x}:{default_minor_id:x} sfq'
                self._cmd(tmp)

        # call base class
        super().update(config, direction)

class TrafficShaperHFSC(QoSBase):
    _parent = 1
    qostype = 'shaper_hfsc'

    # https://man7.org/linux/man-pages/man8/tc-hfsc.8.html
    def update(self, config, direction):
        class_id_max = 0
        if 'class' in config:
            tmp = list(config['class'])
            tmp.sort()
            class_id_max = tmp[-1]

        r2q = 10
        # bandwidth is a mandatory CLI node
        speed = self._rate_convert(config['bandwidth'])
        speed_bps = int(speed) // 8

        # need a bigger r2q if going fast than 16 mbits/sec
        if (speed_bps // r2q) >= MAXQUANTUM: # integer division
            r2q = ceil(speed_bps // MAXQUANTUM)
        else:
            # if there is a slow class then may need smaller value
            if 'class' in config:
                min_speed = speed_bps
                for cls, cls_options in config['class'].items():
                    # find class with the lowest bandwidth used
                    if 'bandwidth' in cls_options:
                        bw_bps = int(self._rate_convert(cls_options['bandwidth'])) // 8 # bandwidth in bytes per second
                        if bw_bps < min_speed:
                            min_speed = bw_bps

                while (r2q > 1) and (min_speed // r2q) < MINQUANTUM:
                    tmp = r2q -1
                    if (speed_bps // tmp) >= MAXQUANTUM:
                        break
                    r2q = tmp

        default_minor_id = int(class_id_max) +1
        tmp = f'tc qdisc replace dev {self._interface} root handle {self._parent:x}: hfsc default {default_minor_id:x}' # default is in hex
        self._cmd(tmp)

        tmp = f'tc class replace dev {self._interface} parent {self._parent:x}: classid {self._parent:x}:1 hfsc sc rate {speed} ul rate {speed}'
        self._cmd(tmp)

        if 'class' in config:
            for cls, cls_config in config['class'].items():
                # class id is used later on and passed as hex, thus this needs to be an int
                cls = int(cls)
                # ls m1
                if cls_config.get('linkshare', {}).get('m1').endswith('%'):
                    percent = cls_config['linkshare']['m1'].rstrip('%')
                    m_one_rate = self._rate_convert(config['bandwidth']) * int(percent) // 100
                else:
                    m_one_rate = cls_config['linkshare']['m1']
                # ls m2
                if cls_config.get('linkshare', {}).get('m2').endswith('%'):
                    percent = cls_config['linkshare']['m2'].rstrip('%')
                    m_two_rate = self._rate_convert(config['bandwidth']) * int(percent) // 100
                else:
                    m_two_rate = self._rate_convert(cls_config['linkshare']['m2'])

                tmp = f'tc class replace dev {self._interface} parent {self._parent:x}:1 classid {self._parent:x}:{cls:x} hfsc ls m1 {m_one_rate} m2 {m_two_rate} '
                self._cmd(tmp)

                tmp = f'tc qdisc replace dev {self._interface} parent {self._parent:x}:{cls:x} sfq perturb 10'
                self._cmd(tmp)

        if 'default' in config:
            # ls m1
            if config.get('default', {}).get('linkshare', {}).get('m1').endswith('%'):
                percent = config['default']['linkshare']['m1'].rstrip('%')
                m_one_rate = self._rate_convert(config['default']['linkshare']['m1']) * int(percent) // 100
            else:
                m_one_rate = config['default']['linkshare']['m1']
            # ls m2
            if config.get('default', {}).get('linkshare', {}).get('m2').endswith('%'):
                percent = config['default']['linkshare']['m2'].rstrip('%')
                m_two_rate = self._rate_convert(config['default']['linkshare']['m2']) * int(percent) // 100
            else:
                m_two_rate = self._rate_convert(config['default']['linkshare']['m2'])
            tmp = f'tc class replace dev {self._interface} parent {self._parent:x}:1 classid {self._parent:x}:{default_minor_id:x} hfsc ls m1 {m_one_rate} m2 {m_two_rate} '
            self._cmd(tmp)

            tmp = f'tc qdisc replace dev {self._interface} parent {self._parent:x}:{default_minor_id:x} sfq perturb 10'
            self._cmd(tmp)

        # call base class
        super().update(config, direction)