Project

General

Profile

Download (117 KB) Statistics
| Branch: | Tag: | Revision:
1 061f78b1 Bill Marquette
<?php
2
/*
3 8acd654a Renato Botelho
 * shaper.inc
4 ba360ed7 Stephen Beaver
 *
5 8acd654a Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 8acd654a Renato Botelho
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 8acd654a Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10 aaec5634 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11 8acd654a Renato Botelho
 * All rights reserved.
12 fd9ebcd5 Stephen Beaver
 *
13 8acd654a Renato Botelho
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15 fd9ebcd5 Stephen Beaver
 *
16 8acd654a Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18 fd9ebcd5 Stephen Beaver
 *
19 8acd654a Renato Botelho
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23 fd9ebcd5 Stephen Beaver
 *
24 8acd654a Renato Botelho
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28 fd9ebcd5 Stephen Beaver
 *
29 8acd654a Renato Botelho
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33 fd9ebcd5 Stephen Beaver
 *
34 8acd654a Renato Botelho
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37 fd9ebcd5 Stephen Beaver
 *
38 8acd654a Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40 fd9ebcd5 Stephen Beaver
 *
41 8acd654a Renato Botelho
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43 fd9ebcd5 Stephen Beaver
 *
44 8acd654a Renato Botelho
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56 fd9ebcd5 Stephen Beaver
 */
57 061f78b1 Bill Marquette
58 f5881023 Ermal Lu?i
/* XXX: needs some reducing on include. */
59
/* include all configuration functions. */
60 77b86ada Ermal
require_once("globals.inc");
61 061f78b1 Bill Marquette
require_once("functions.inc");
62 8633930d Ermal
require_once("util.inc");
63
require_once("notices.inc");
64 061f78b1 Bill Marquette
65 197bfe96 Ermal Luçi
/*
66 61e047a5 Phil Davis
 * I admit :) this is derived from xmlparse.inc StartElement()
67 197bfe96 Ermal Luçi
 */
68 61e047a5 Phil Davis
function &get_reference_to_me_in_config(&$mypath) {
69 197bfe96 Ermal Luçi
	global $config;
70
71 420b4538 Renato Botelho
	$ptr =& $config['shaper'];
72 197bfe96 Ermal Luçi
	foreach ($mypath as $indeks) {
73 f5881023 Ermal Lu?i
		$ptr =& $ptr['queue'][$indeks];
74
	}
75 061f78b1 Bill Marquette
76 197bfe96 Ermal Luçi
	return $ptr;
77 061f78b1 Bill Marquette
}
78 d62ba478 Ermal Luçi
79 61e047a5 Phil Davis
function unset_object_by_reference(&$mypath) {
80 197bfe96 Ermal Luçi
	global $config;
81 061f78b1 Bill Marquette
82 f5881023 Ermal Lu?i
	$ptr =& $config['shaper'];
83
	for ($i = 0; $i < count($mypath) - 1; $i++) {
84
		$ptr =& $ptr['queue'][$mypath[$i]];
85
	}
86 197bfe96 Ermal Luçi
	unset($ptr['queue'][$mypath[$i]]);
87
}
88 d62ba478 Ermal Luçi
89 61e047a5 Phil Davis
function &get_dn_reference_to_me_in_config(&$mypath) {
90 c25a6b6a Ermal Luçi
	global $config;
91
92 f5881023 Ermal Lu?i
	$ptr =& $config['dnshaper'];
93 c25a6b6a Ermal Luçi
	foreach ($mypath as $indeks) {
94 f5881023 Ermal Lu?i
		$ptr =& $ptr['queue'][$indeks];
95
	}
96 c25a6b6a Ermal Luçi
97
	return $ptr;
98
}
99 d62ba478 Ermal Luçi
100 61e047a5 Phil Davis
function unset_dn_object_by_reference(&$mypath) {
101 c25a6b6a Ermal Luçi
	global $config;
102
103 f5881023 Ermal Lu?i
	$ptr =& $config['dnshaper'];
104
	for ($i = 0; $i < count($mypath) - 1; $i++) {
105
		$ptr =& $ptr['queue'][$mypath[$i]];
106
	}
107 c25a6b6a Ermal Luçi
	unset($ptr['queue'][$mypath[$i]]);
108
}
109
110 61e047a5 Phil Davis
function clean_child_queues($type, $mypath) {
111 197bfe96 Ermal Luçi
	$ref = &get_reference_to_me_in_config($mypath);
112
113
	switch ($type) {
114 61e047a5 Phil Davis
		case 'HFSC':
115
			if (isset($ref['borrow'])) {
116
				unset($ref['borrow']);
117
			}
118
			if (isset($ref['hogs'])) {
119
				unset($ref['hogs']);
120
			}
121
			if (isset($ref['buckets'])) {
122
				unset($ref['buckets']);
123
			}
124
			break;
125
		case 'PRIQ':
126
			if (isset($ref['borrow'])) {
127
				unset($ref['borrow']);
128
			}
129
			if (isset($ref['bandwidth'])) {
130
				unset($ref['bandwidth']);
131
			}
132
			if (isset($ref['bandwidthtype'])) {
133
				unset($ref['bandwidthtype']);
134
			}
135
			/* fall through */
136
		case 'FAIRQ':
137
			if (isset($ref['borrow'])) {
138
				unset($ref['borrow']);
139
			}
140
			/* fall through */
141
		case 'CBQ':
142
			if (isset($ref['realtime'])) {
143
				unset($ref['realtime']);
144
			}
145
			if (isset($ref['realtime1'])) {
146
				unset($ref['realtime1']);
147
			}
148
			if (isset($ref['realtime2'])) {
149
				unset($ref['realtime2']);
150
			}
151
			if (isset($ref['realtime3'])) {
152
				unset($ref['realtime3']);
153
			}
154
			if (isset($ref['upperlimit'])) {
155
				unset($ref['upperlimit']);
156
			}
157
			if (isset($ref['upperlimit1'])) {
158
				unset($ref['upperlimit1']);
159
			}
160
			if (isset($ref['upperlimit2'])) {
161
				unset($ref['upperlimit2']);
162
			}
163
			if (isset($ref['upperlimit3'])) {
164
				unset($ref['upperlimit3']);
165
			}
166
			if (isset($ref['linkshare'])) {
167
				unset($ref['linkshare']);
168
			}
169
			if (isset($ref['linkshare1'])) {
170
				unset($ref['linkshare1']);
171
			}
172
			if (isset($ref['linkshare2'])) {
173
				unset($ref['linkshare2']);
174
			}
175
			if (isset($ref['linkshare3'])) {
176
				unset($ref['linkshare3']);
177
			}
178
			if (isset($ref['hogs'])) {
179
				unset($ref['hogs']);
180
			}
181
			if (isset($ref['buckets'])) {
182
				unset($ref['buckets']);
183
			}
184
			break;
185 197bfe96 Ermal Luçi
	}
186
}
187 061f78b1 Bill Marquette
188 61e047a5 Phil Davis
function get_bandwidthtype_scale($type) {
189 420b4538 Renato Botelho
	switch ($type) {
190 61e047a5 Phil Davis
		case "Gb":
191
			$factor = 1024 * 1024 * 1024;
192
			break;
193
		case "Mb":
194
			$factor = 1024 * 1024;
195
			break;
196
		case "Kb":
197
			$factor = 1024;
198
			break;
199
		case "b":
200
		default:
201
			$factor = 1;
202
			break;
203 420b4538 Renato Botelho
	}
204
	return intval($factor);
205 40de74f5 Ermal Luçi
}
206
207 45eeb038 Luiz Otavio O Souza
function get_bandwidth($bw, $scale, $obj) {
208 2638ddec Luiz Otavio O Souza
209
	$pattern= "/(b|Kb|Mb|Gb|%)/";
210
	if (!preg_match($pattern, $scale, $match))
211
		return 0;
212
213
	switch ($match[1]) {
214
		case '%':
215 45eeb038 Luiz Otavio O Souza
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
216 2638ddec Luiz Otavio O Souza
			break;
217
		default:
218
			$objbw = $bw * get_bandwidthtype_scale($scale);
219
			break;
220
	}
221
222
	return floatval($objbw);
223
}
224
225 45eeb038 Luiz Otavio O Souza
/*
226
 * XXX - unused
227
 *
228 61e047a5 Phil Davis
function get_hfsc_bandwidth($object, $bw) {
229 40de74f5 Ermal Luçi
	$pattern= "/[0-9]+/";
230 61e047a5 Phil Davis
	if (preg_match($pattern, $bw, $match)) {
231 420b4538 Renato Botelho
		$bw_1 = $match[1];
232 61e047a5 Phil Davis
	} else {
233 420b4538 Renato Botelho
		return 0;
234 61e047a5 Phil Davis
	}
235 420b4538 Renato Botelho
	$pattern= "/(b|Kb|Mb|Gb|%)/";
236
	if (preg_match($pattern, $bw, $match)) {
237
		switch ($match[1]) {
238 61e047a5 Phil Davis
			case '%':
239 45eeb038 Luiz Otavio O Souza
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
240 61e047a5 Phil Davis
				break;
241
			default:
242
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
243
				break;
244 420b4538 Renato Botelho
		}
245 3a54efed Ermal Luçi
		return floatval($bw_1);
246 61e047a5 Phil Davis
	} else {
247 420b4538 Renato Botelho
		return 0;
248 61e047a5 Phil Davis
	}
249 40de74f5 Ermal Luçi
}
250 45eeb038 Luiz Otavio O Souza
*/
251
252
function get_queue_bandwidth($obj) {
253
	$bw = $obj->GetBandwidth();
254
	$scale = $obj->GetBwscale();
255
256
	$pattern= "/(b|Kb|Mb|Gb|%)/";
257
	if (!preg_match($pattern, $scale, $match))
258
		return 0;
259
260
	switch ($match[1]) {
261
		case '%':
262
			$objbw = ($bw / 100) * get_queue_bandwidth($obj->GetParent());
263
			break;
264
		default:
265
			$objbw = $bw * get_bandwidthtype_scale($scale);
266
			break;
267
	}
268
269
	return floatval($objbw);
270
}
271 40de74f5 Ermal Luçi
272 61e047a5 Phil Davis
function get_interface_bandwidth($object) {
273 40de74f5 Ermal Luçi
	global $altq_list_queues;
274
275 420b4538 Renato Botelho
	$int = $object->GetInterface();
276
	$altq =& $altq_list_queues[$int];
277
	if ($altq) {
278
		$bw_3 = $altq->GetBandwidth();
279 4de8f7ba Phil Davis
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
280 3a54efed Ermal Luçi
		return floatval($bw_3);
281 61e047a5 Phil Davis
	} else {
282 f5881023 Ermal Lu?i
		return 0;
283 61e047a5 Phil Davis
	}
284 40de74f5 Ermal Luçi
}
285
286
/*
287
 * This is duplicated here since we cannot include guiconfig.inc.
288
 * Including it makes all stuff break.
289
 */
290 61e047a5 Phil Davis
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
291 40de74f5 Ermal Luçi
292 420b4538 Renato Botelho
	/* check for bad control characters */
293
	foreach ($postdata as $pn => $pd) {
294
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
295
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
296
		}
297
	}
298
299
	for ($i = 0; $i < count($reqdfields); $i++) {
300
		if ($postdata[$reqdfields[$i]] == "") {
301
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
302
		}
303
	}
304 40de74f5 Ermal Luçi
}
305
306 61e047a5 Phil Davis
function cleanup_queue_from_rules($queue) {
307 197bfe96 Ermal Luçi
	global $config;
308 061f78b1 Bill Marquette
309 197bfe96 Ermal Luçi
	foreach ($config['filter']['rule'] as $rule) {
310 61e047a5 Phil Davis
		if ($rule['defaultqueue'] == $queue) {
311 197bfe96 Ermal Luçi
			unset($rule['defaultqueue']);
312 61e047a5 Phil Davis
		}
313
		if ($rule['ackqueue'] == $queue) {
314 197bfe96 Ermal Luçi
			unset($rule['ackqueue']);
315 61e047a5 Phil Davis
		}
316 197bfe96 Ermal Luçi
	}
317 061f78b1 Bill Marquette
}
318
319 61e047a5 Phil Davis
function cleanup_dnqueue_from_rules($queue) {
320 c25a6b6a Ermal Luçi
	global $config;
321
322
	foreach ($config['filter']['rule'] as $rule) {
323 61e047a5 Phil Davis
		if ($rule['dnpipe'] == $queue) {
324 c25a6b6a Ermal Luçi
			unset($rule['dnpipe']);
325 61e047a5 Phil Davis
		}
326
		if ($rule['pdnpipe'] == $queue) {
327 c25a6b6a Ermal Luçi
			unset($rule['pdnpipe']);
328 61e047a5 Phil Davis
		}
329 c25a6b6a Ermal Luçi
	}
330
}
331
332 197bfe96 Ermal Luçi
class altq_root_queue {
333 f5881023 Ermal Lu?i
	var $interface;
334
	var $tbrconfig ;
335
	var $bandwidth;
336 bfc94df0 Phil Davis
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
337 f5881023 Ermal Lu?i
	var $scheduler;
338
	var $qlimit;
339
	var $queues = array();
340
	var $qenabled = false;
341
	var $link;
342
343 d6fa899d Phil Davis
	/* Accessor functions */
344 197bfe96 Ermal Luçi
	function GetDefaultQueuePresent() {
345 f61dc8e6 Ermal
		if (!empty($this->queues)) {
346
			foreach ($this->queues as $q) {
347 61e047a5 Phil Davis
				if ($q->GetDefault()) {
348 f61dc8e6 Ermal
					return true;
349 61e047a5 Phil Davis
				}
350 f61dc8e6 Ermal
			}
351
		}
352
353
		return false;
354 197bfe96 Ermal Luçi
	}
355
	function SetLink($link) {
356
		$this->link = $link;
357
	}
358
	function GetLink() {
359
		return $this->link;
360 420b4538 Renato Botelho
	}
361 197bfe96 Ermal Luçi
	function GetEnabled() {
362
		return $this->qenabled;
363
	}
364 92125c97 Ermal Luçi
	function SetEnabled($value) {
365 197bfe96 Ermal Luçi
		$this->qenabled = $value;
366
	}
367 70b139a3 Chris Buechler
	function CanHaveChildren() {
368 61e047a5 Phil Davis
		if ($this->GetScheduler() == "CODELQ") {
369 8edaa92c Ermal
			return false;
370 61e047a5 Phil Davis
		} else {
371 8edaa92c Ermal
			return true;
372 61e047a5 Phil Davis
		}
373 197bfe96 Ermal Luçi
	}
374
	function CanBeDeleted() {
375
		return false;
376
	}
377
	function GetQname() {
378
		return $this->interface;
379
	}
380
	function SetQname($name) {
381
		$this->interface = trim($name);
382
	}
383 92125c97 Ermal Luçi
	function GetInterface() {
384 f5881023 Ermal Lu?i
		return $this->interface;
385 92125c97 Ermal Luçi
	}
386
	function SetInterface($name) {
387 f5881023 Ermal Lu?i
		$this->interface = trim($name);
388 92125c97 Ermal Luçi
	}
389
	function GetTbrConfig() {
390 f5881023 Ermal Lu?i
		return $this->tbrconfig;
391 92125c97 Ermal Luçi
	}
392
	function SetTbrConfig($tbrconfig) {
393 f5881023 Ermal Lu?i
		$this->tbrconfig = $tbrconfig;
394 92125c97 Ermal Luçi
	}
395
	function GetBandwidth() {
396 f5881023 Ermal Lu?i
		return $this->bandwidth;
397 92125c97 Ermal Luçi
	}
398
	function SetBandwidth($bw) {
399 f5881023 Ermal Lu?i
		$this->bandwidth = $bw;
400 92125c97 Ermal Luçi
	}
401
	function GetBwscale() {
402 f5881023 Ermal Lu?i
		return $this->bandwidthtype;
403 92125c97 Ermal Luçi
	}
404
	function SetBwscale($bwscale) {
405 f5881023 Ermal Lu?i
		$this->bandwidthtype = $bwscale;
406 92125c97 Ermal Luçi
	}
407
	function GetScheduler() {
408 f5881023 Ermal Lu?i
		return $this->scheduler;
409 92125c97 Ermal Luçi
	}
410
	function SetScheduler($scheduler) {
411 f5881023 Ermal Lu?i
		$this->scheduler = trim($scheduler);
412 92125c97 Ermal Luçi
	}
413 197bfe96 Ermal Luçi
	function GetQlimit() {
414
		return $this->qlimit;
415
	}
416
	function SetQlimit($limit) {
417
		$this->qlimit = $limit;
418
	}
419 f5881023 Ermal Lu?i
420 bfc94df0 Phil Davis
	function GetBwscaleText() {
421
		switch ($this->bandwidthtype) {
422
			case "b":
423
				$bwscaletext = "Bit/s";
424
				break;
425
			case "Kb":
426
				$bwscaletext = "Kbit/s";
427
				break;
428
			case "Mb":
429
				$bwscaletext = "Mbit/s";
430
				break;
431
			case "Gb":
432
				$bwscaletext = "Gbit/s";
433
				break;
434
			default:
435
				/* For others that do not need translating like % */
436
				$bwscaletext = $this->bandwidthtype;
437
				break;
438
		}
439
		return $bwscaletext;
440
	}
441
442 45eeb038 Luiz Otavio O Souza
	function CheckBandwidth($bw, $bwtype) {
443
		$sum = $this->GetTotalBw();
444
		if ($sum > $bw * get_bandwidthtype_scale($bwtype))
445
			return 1;
446
		foreach ($this->queues as $q) {
447
			if ($q->CheckBandwidth(0, ''))
448
				return 1;
449
		}
450
451
		return 0;
452
	}
453
454
	function GetTotalBw($qignore = NULL) {
455 2638ddec Luiz Otavio O Souza
		$sum = 0;
456
		foreach ($this->queues as $q) {
457 45eeb038 Luiz Otavio O Souza
			if ($qignore != NULL && $qignore == $q)
458
				continue;
459
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
460 2638ddec Luiz Otavio O Souza
		}
461
462
		return $sum;
463
	}
464
465 197bfe96 Ermal Luçi
	function validate_input($data, &$input_errors) {
466 ce0117f7 Colin Fleming
467 2c072899 jim-p
		$reqdfields[] = "bandwidth";
468 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidth");
469 2c072899 jim-p
		$reqdfields[] = "bandwidthtype";
470 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidthtype");
471 ce0117f7 Colin Fleming
472 40de74f5 Ermal Luçi
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
473 ce0117f7 Colin Fleming
474 2638ddec Luiz Otavio O Souza
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
475
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
476
		}
477 61e047a5 Phil Davis
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
478 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Bandwidth must be an integer.");
479 61e047a5 Phil Davis
		}
480
		if ($data['bandwidth'] < 0) {
481 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Bandwidth cannot be negative.");
482 61e047a5 Phil Davis
		}
483 45eeb038 Luiz Otavio O Souza
		if ($data['bandwidthtype'] == "%") {
484
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
485
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
486
			}
487
		}
488
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
489
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
490
491 61e047a5 Phil Davis
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
492 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Qlimit must be an integer.");
493 61e047a5 Phil Davis
		}
494
		if ($data['qlimit'] < 0) {
495 794195d1 Phil Davis
			$input_errors[] = gettext("Qlimit must be positive.");
496 61e047a5 Phil Davis
		}
497
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
498 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Tbrsize must be an integer.");
499 61e047a5 Phil Davis
		}
500
		if ($data['tbrconfig'] < 0) {
501 794195d1 Phil Davis
			$input_errors[] = gettext("Tbrsize must be positive.");
502 61e047a5 Phil Davis
		}
503 92125c97 Ermal Luçi
	}
504 197bfe96 Ermal Luçi
505
	/* Implement this to shorten some code on the frontend page */
506 f5881023 Ermal Lu?i
	function ReadConfig(&$conf) {
507 61e047a5 Phil Davis
		if (isset($conf['tbrconfig'])) {
508 f5881023 Ermal Lu?i
			$this->SetTbrConfig($conf['tbrconfig']);
509 61e047a5 Phil Davis
		} else {
510 06f3e447 Ermal
			$this->SetTbrConfig($conf['tbrconfig']);
511 61e047a5 Phil Davis
		}
512 1941345c Ermal
		$this->SetBandwidth($conf['bandwidth']);
513 61e047a5 Phil Davis
		if ($conf['bandwidthtype'] <> "") {
514 1941345c Ermal
			$this->SetBwscale($conf['bandwidthtype']);
515 61e047a5 Phil Davis
		}
516 197bfe96 Ermal Luçi
		if (isset($conf['scheduler'])) {
517 f5881023 Ermal Lu?i
			if ($this->GetScheduler() != $conf['scheduler']) {
518
				foreach ($this->queues as $q) {
519
					clean_child_queues($conf['scheduler'], $this->GetLink());
520
					$q->clean_queue($conf['scheduler']);
521 061f78b1 Bill Marquette
				}
522
			}
523 f5881023 Ermal Lu?i
			$this->SetScheduler($conf['scheduler']);
524
		}
525 61e047a5 Phil Davis
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
526 197bfe96 Ermal Luçi
			$this->SetQlimit($conf['qlimit']);
527 61e047a5 Phil Davis
		} else {
528 06f3e447 Ermal
			$this->SetQlimit("");
529 61e047a5 Phil Davis
		}
530
		if (isset($conf['name'])) {
531 420b4538 Renato Botelho
			$this->SetQname($conf['name']);
532 61e047a5 Phil Davis
		}
533
		if (!empty($conf['enabled'])) {
534 f5881023 Ermal Lu?i
			$this->SetEnabled($conf['enabled']);
535 61e047a5 Phil Davis
		} else {
536 ea25d26d Ermal Lu?i
			$this->SetEnabled("");
537 61e047a5 Phil Davis
		}
538 197bfe96 Ermal Luçi
	}
539 f5881023 Ermal Lu?i
540 a843b04f Ermal Luçi
	function copy_queue($interface, &$cflink) {
541 420b4538 Renato Botelho
		$cflink['interface'] = $interface;
542
		$cflink['name'] = $interface;
543
		$cflink['scheduler'] = $this->GetScheduler();
544
		$cflink['bandwidth'] = $this->GetBandwidth();
545
		$cflink['bandwidthtype'] = $this->GetBwscale();
546
		$cflink['qlimit'] = $this->GetQlimit();
547
		$cflink['tbrconfig'] = $this->GetTbrConfig();
548
		$cflink['enabled'] = $this->GetEnabled();
549 a843b04f Ermal Luçi
		if (is_array($this->queues)) {
550
			$cflink['queue'] = array();
551
			foreach ($this->queues as $q) {
552
				$cflink['queue'][$q->GetQname()] = array();
553 dbf2dde4 Renato Botelho
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
554 a843b04f Ermal Luçi
			}
555 92125c97 Ermal Luçi
		}
556 061f78b1 Bill Marquette
	}
557
558 ea51e9f8 Renato Botelho
	function &get_queue_list(&$q = null) {
559 f5881023 Ermal Lu?i
		$qlist = array();
560 b7ff5e40 Scott Ullrich
561 8484586f Ermal
		//$qlist[$this->GetQname()] = & $this;
562 f5881023 Ermal Lu?i
		if (is_array($this->queues)) {
563 61e047a5 Phil Davis
			foreach ($this->queues as $queue) {
564 ea51e9f8 Renato Botelho
				$queue->get_queue_list($qlist);
565 61e047a5 Phil Davis
			}
566 fe93b17b Ermal Luçi
		}
567 f5881023 Ermal Lu?i
		return $qlist;
568
	}
569 fe93b17b Ermal Luçi
570 f5881023 Ermal Lu?i
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
571 92125c97 Ermal Luçi
572 61e047a5 Phil Davis
		if (!is_array($this->queues)) {
573 f5881023 Ermal Lu?i
			$this->queues = array();
574 61e047a5 Phil Davis
		}
575 f5881023 Ermal Lu?i
576
		switch ($this->GetScheduler()) {
577 61e047a5 Phil Davis
			case "PRIQ":
578
				$q =& new priq_queue();
579
				break;
580
			case "HFSC":
581
				$q =& new hfsc_queue();
582
				break;
583
			case "CBQ":
584
				$q =& new cbq_queue();
585
				break;
586
			case "FAIRQ":
587
				$q =& new fairq_queue();
588
				break;
589
			default:
590
				/* XXX: but should not happen anyway */
591
				return;
592
				break;
593 f5881023 Ermal Lu?i
		}
594
		$q->SetLink($path);
595
		$q->SetInterface($this->GetInterface());
596
		$q->SetEnabled("on");
597 ea51e9f8 Renato Botelho
		$q->SetParent($this);
598 f5881023 Ermal Lu?i
		$q->ReadConfig($queue);
599
		$q->validate_input($queue, $input_errors);
600 197bfe96 Ermal Luçi
601 420b4538 Renato Botelho
		$this->queues[$q->GetQname()] = &$q;
602 f5881023 Ermal Lu?i
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
603
		if (is_array($queue['queue'])) {
604
			foreach ($queue['queue'] as $key1 => $que) {
605
				array_push($path, $key1);
606 ea51e9f8 Renato Botelho
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
607 f5881023 Ermal Lu?i
				array_pop($path);
608
			}
609
		}
610
611
		return $q;
612
	}
613
614
	/* interface here might be optional */
615
	function &find_queue($interface, $qname) {
616
		if ($qname == $this->GetQname()) {
617
			return $this;
618 420b4538 Renato Botelho
		}
619 f5881023 Ermal Lu?i
		foreach ($this->queues as $q) {
620
			$result =& $q->find_queue("", $qname);
621 61e047a5 Phil Davis
			if ($result) {
622 f5881023 Ermal Lu?i
				return $result;
623 61e047a5 Phil Davis
			}
624 f5881023 Ermal Lu?i
		}
625
	}
626 197bfe96 Ermal Luçi
627
	function &find_parentqueue($interface, $qname) {
628
		if ($qname == $interface) {
629 4de8f7ba Phil Davis
			$result = NULL;
630 420b4538 Renato Botelho
		} else if ($this->queues[$qname]) {
631 197bfe96 Ermal Luçi
			$result = $this;
632
		} else if ($this->GetScheduler() <> "PRIQ") {
633
			foreach ($this->queues as $q) {
634
				$result = $q->find_parentqueue("", $qname);
635 61e047a5 Phil Davis
				if ($result) {
636 197bfe96 Ermal Luçi
					return $result;
637 61e047a5 Phil Davis
				}
638 b7ff5e40 Scott Ullrich
			}
639 061f78b1 Bill Marquette
		}
640
	}
641
642 197bfe96 Ermal Luçi
	function build_tree() {
643 057399e4 Ermal Luçi
		global $shaperIFlist;
644
645 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
646 057399e4 Ermal Luçi
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
647 197bfe96 Ermal Luçi
		if (is_array($this->queues)) {
648
			$tree .= "<ul>";
649 4de8f7ba Phil Davis
			foreach ($this->queues as $q) {
650 197bfe96 Ermal Luçi
				$tree .= $q->build_tree();
651
			}
652 61e047a5 Phil Davis
			$tree .= "</ul>";
653 197bfe96 Ermal Luçi
		}
654
		$tree .= "</li>";
655
		return $tree;
656
	}
657 ce0117f7 Colin Fleming
658 420b4538 Renato Botelho
	function delete_queue() {
659 45eeb038 Luiz Otavio O Souza
		foreach ($this->queues as $q)
660 420b4538 Renato Botelho
			$q->delete_queue();
661 197bfe96 Ermal Luçi
		unset_object_by_reference($this->GetLink());
662 420b4538 Renato Botelho
	}
663 92125c97 Ermal Luçi
664 b0262cb2 Ermal Luçi
	function delete_all() {
665 420b4538 Renato Botelho
		if (count($this->queues)) {
666
			foreach ($this->queues as $q) {
667
				$q->delete_all();
668
				unset_object_by_reference($q->GetLink());
669
				unset($q);
670
			}
671
			unset($this->queues);
672
		}
673
	}
674 b0262cb2 Ermal Luçi
675 92125c97 Ermal Luçi
	/*
676
	 * First it spits:
677
	 * altq on $interface ..............
678 6990ad35 Phil Davis
	 *	then it goes like
679
	 *	foreach ($queues as $qkey => $queue) {
680
	 *		this->queues[$qkey]->build_rule();
681
	 *	}
682 92125c97 Ermal Luçi
	 */
683 9d0b0635 Ermal
	function build_rules(&$default = false) {
684 34a3694b Ermal
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
685 ef8fca71 Ermal
			$default = false;
686 4de8f7ba Phil Davis
			$rules = " altq on " . get_real_interface($this->GetInterface());
687 61e047a5 Phil Davis
			if ($this->GetScheduler()) {
688 f5881023 Ermal Lu?i
				$rules .= " ".strtolower($this->GetScheduler());
689 61e047a5 Phil Davis
			}
690
			if ($this->GetQlimit() > 0) {
691 a061ddb9 Chris Buechler
				$rules .= " qlimit " . $this->GetQlimit() . " ";
692 61e047a5 Phil Davis
			}
693 1941345c Ermal
			if ($this->GetBandwidth()) {
694 f5881023 Ermal Lu?i
				$rules .= " bandwidth ".trim($this->GetBandwidth());
695 61e047a5 Phil Davis
				if ($this->GetBwscale()) {
696 1941345c Ermal
					$rules .= $this->GetBwscale();
697 61e047a5 Phil Davis
				}
698 1941345c Ermal
			}
699 61e047a5 Phil Davis
			if ($this->GetTbrConfig()) {
700 f5881023 Ermal Lu?i
				$rules .= " tbrsize ".$this->GetTbrConfig();
701 61e047a5 Phil Davis
			}
702 92125c97 Ermal Luçi
			if (count($this->queues)) {
703 f5881023 Ermal Lu?i
				$i = count($this->queues);
704
				$rules .= " queue { ";
705
				foreach ($this->queues as $qkey => $qnone) {
706
					if ($i > 1) {
707
						$i--;
708
						$rules .= " {$qkey}, ";
709 61e047a5 Phil Davis
					} else {
710 f5881023 Ermal Lu?i
						$rules .= " {$qkey} ";
711 61e047a5 Phil Davis
					}
712 f5881023 Ermal Lu?i
				}
713
				$rules .= " } \n";
714
				foreach ($this->queues as $q) {
715 9d0b0635 Ermal
					$rules .= $q->build_rules($default);
716 f5881023 Ermal Lu?i
				}
717 92125c97 Ermal Luçi
			}
718 8edaa92c Ermal
719 ef8fca71 Ermal
			if ($default == false) {
720 e8c516a0 Phil Davis
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
721 8633930d Ermal
				file_notice("Shaper", $error, "Error occurred", "");
722
				unset($error);
723 ef8fca71 Ermal
				return "\n";
724 420b4538 Renato Botelho
			}
725 ef8fca71 Ermal
			$frule .= $rules;
726 8edaa92c Ermal
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
727 4de8f7ba Phil Davis
			$rules = " altq on " . get_real_interface($this->GetInterface());
728 61e047a5 Phil Davis
			if ($this->GetScheduler()) {
729 8edaa92c Ermal
				$rules .= " ".strtolower($this->GetScheduler());
730 61e047a5 Phil Davis
			}
731
			if ($this->GetQlimit() > 0) {
732 8edaa92c Ermal
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
733 61e047a5 Phil Davis
			}
734 8edaa92c Ermal
			if ($this->GetBandwidth()) {
735
				$rules .= " bandwidth ".trim($this->GetBandwidth());
736 61e047a5 Phil Davis
				if ($this->GetBwscale()) {
737 8edaa92c Ermal
					$rules .= $this->GetBwscale();
738 61e047a5 Phil Davis
				}
739 8edaa92c Ermal
			}
740 61e047a5 Phil Davis
			if ($this->GetTbrConfig()) {
741 8edaa92c Ermal
				$rules .= " tbrsize ".$this->GetTbrConfig();
742 61e047a5 Phil Davis
			}
743 8edaa92c Ermal
744
			$rules .= " queue";
745 197bfe96 Ermal Luçi
		}
746 8edaa92c Ermal
747 197bfe96 Ermal Luçi
		$rules .= " \n";
748 92125c97 Ermal Luçi
		return $rules;
749
	}
750 197bfe96 Ermal Luçi
751
	function build_javascript() {
752 f5881023 Ermal Lu?i
		$javascript = "<script type=\"text/javascript\">";
753 ee02550a Michele Di Maria
		$javascript .= "//<![CDATA[\n";
754 f5881023 Ermal Lu?i
		$javascript .= "function mySuspend() {";
755 01890f6a Ermal
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
756
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
757 f5881023 Ermal Lu?i
		$javascript .= "else if (document.all)";
758
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
759
		$javascript .= "}";
760
761
		$javascript .= "function myResume() {";
762 01890f6a Ermal
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
763 f5881023 Ermal Lu?i
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
764 01890f6a Ermal
		$javascript .= "else if (document.all) ";
765 f5881023 Ermal Lu?i
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
766
		$javascript .= "}";
767 0fa05f45 Colin Fleming
		$javascript .= "//]]>";
768 f5881023 Ermal Lu?i
		$javascript .= "</script>";
769
770
		return $javascript;
771 92125c97 Ermal Luçi
	}
772 ce0117f7 Colin Fleming
773 92125c97 Ermal Luçi
	function build_shortform() {
774 f5881023 Ermal Lu?i
		global $g;
775
776
		$altq =& $this;
777 d17c4ee9 Stephen Beaver
778 61e047a5 Phil Davis
		if ($altq) {
779 f5881023 Ermal Lu?i
			$scheduler = ": " . $altq->GetScheduler();
780 61e047a5 Phil Davis
		}
781 f5881023 Ermal Lu?i
782 b28e1512 Stephen Beaver
		$form = '<dl class="dl-horizontal">';
783
		$form .= '	<dt>';
784
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
785
		$form .= '	</dt>';
786
		$form .= '	<dd>';
787 d17c4ee9 Stephen Beaver
		$form .=		$scheduler;
788 b28e1512 Stephen Beaver
		$form .= '	</dd>';
789 d17c4ee9 Stephen Beaver
790 b28e1512 Stephen Beaver
		$form .= '	<dt>';
791 d17c4ee9 Stephen Beaver
		$form .=		'Bandwidth';
792 b28e1512 Stephen Beaver
		$form .= '	</dt>';
793
		$form .= '	<dd>';
794 bfc94df0 Phil Davis
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
795 b28e1512 Stephen Beaver
		$form .= '	</dd>';
796 d17c4ee9 Stephen Beaver
797
		$form .= '	<dt>';
798 b28e1512 Stephen Beaver
		$form .= 'Disable';
799
		$form .= '	<dt>';
800
		$form .= '	<dd>';
801
802 27d6a45b jim-p
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
803 b28e1512 Stephen Beaver
		$form .= $this->GetInterface() . '&amp;queue=';
804
		$form .= $this->GetQname() . '&amp;action=delete">';
805 27d6a45b jim-p
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
806
		$form .= gettext("Remove shaper from this interface") . '</a>';
807 b28e1512 Stephen Beaver
808 d17c4ee9 Stephen Beaver
		$form .= '	</dd>';
809
810
		$form .= '</dl>';
811
812 f5881023 Ermal Lu?i
		return $form;
813 92125c97 Ermal Luçi
814
	}
815 aef9d8fe Stephen Beaver
816 f5881023 Ermal Lu?i
	/*
817 ce0117f7 Colin Fleming
	 * For requesting the parameters of the root queues
818 f5881023 Ermal Lu?i
	 * to the user like the traffic wizard does.
819 806942d0 Stephen Beaver
	 */
820 420b4538 Renato Botelho
	function build_form() {
821 806942d0 Stephen Beaver
822 27d6a45b jim-p
		$sform = new Form();
823 806942d0 Stephen Beaver
824 a51c7c7f Stephen Beaver
		$sform->setAction("firewall_shaper.php");
825
826 aef9d8fe Stephen Beaver
		$section = new Form_Section(null);
827 806942d0 Stephen Beaver
828 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Checkbox(
829
			'enabled',
830
			'Enable/Disable',
831
			'Enable/disable discipline and its children',
832
			($this->GetEnabled() == "on"),
833
			'on'
834
		));
835 806942d0 Stephen Beaver
836 aef9d8fe Stephen Beaver
		$section->addInput(new Form_StaticText(
837 85877e3c Phil Davis
			'*Name',
838 aef9d8fe Stephen Beaver
			$this->GetQname()
839
		));
840 806942d0 Stephen Beaver
841 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Select(
842
			'scheduler',
843
			'Scheduler Type',
844
			$this->GetScheduler(),
845 806942d0 Stephen Beaver
			array('HFSC' => 'HFSC',
846 aef9d8fe Stephen Beaver
				  'CBQ' => 'CBQ',
847
				  'FAIRQ' => 'FAIRQ',
848
				  'CODELQ' => 'CODELQ',
849
				  'PRIQ' => 'PRIQ')
850 1c92c5b1 Stephen Beaver
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
851 806942d0 Stephen Beaver
852 aef9d8fe Stephen Beaver
		$group = new Form_group('Bandwidth');
853 806942d0 Stephen Beaver
854 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
855
			'bandwidth',
856
			null,
857
			'number',
858
			$this->GetBandwidth()
859
		));
860 806942d0 Stephen Beaver
861 aef9d8fe Stephen Beaver
		$group->add(new Form_Select(
862
			'bandwidthtype',
863
			null,
864
			$this->GetBwscale(),
865 bfc94df0 Phil Davis
			array('Kb' => 'Kbit/s',
866
				  'Mb' => 'Mbit/s',
867
				  'Gb' => 'Gbit/s',
868
				  'b' => 'Bit/s',
869 820519e5 Stephen Beaver
				  '%' => '%')
870 aef9d8fe Stephen Beaver
		));
871 806942d0 Stephen Beaver
872 aef9d8fe Stephen Beaver
		$section->add($group);
873 806942d0 Stephen Beaver
874 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
875
			'qlimit',
876
			'Queue Limit',
877
			'number',
878
			$this->GetQlimit()
879
		));
880 806942d0 Stephen Beaver
881 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
882
			'tbrconfig',
883 0503e477 Ben Cook
			'TBR Size',
884 aef9d8fe Stephen Beaver
			'number',
885
			$this->GetTbrConfig()
886
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
887 806942d0 Stephen Beaver
					'bandwidth are used to determine the size.');
888
889 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
890
			'interface',
891
			null,
892
			'hidden',
893
			$this->GetInterface()
894
		));
895 806942d0 Stephen Beaver
896 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
897
			'name',
898
			null,
899
			'hidden',
900
			$this->GetQname()
901
		));
902 806942d0 Stephen Beaver
903 aef9d8fe Stephen Beaver
		$sform->add($section);
904 806942d0 Stephen Beaver
905 aef9d8fe Stephen Beaver
		return($sform);
906 197bfe96 Ermal Luçi
	}
907 061f78b1 Bill Marquette
908 420b4538 Renato Botelho
	function update_altq_queue_data(&$data) {
909 197bfe96 Ermal Luçi
		$this->ReadConfig($data);
910 061f78b1 Bill Marquette
	}
911 ce0117f7 Colin Fleming
912 92125c97 Ermal Luçi
	/*
913
	 * Should call on each of it queues and subqueues
914
	 * the same function much like build_rules();
915
	 */
916 420b4538 Renato Botelho
	function wconfig() {
917 92125c97 Ermal Luçi
		$cflink = &get_reference_to_me_in_config($this->GetLink());
918 61e047a5 Phil Davis
		if (!is_array($cflink)) {
919 92125c97 Ermal Luçi
			$cflink = array();
920 61e047a5 Phil Davis
		}
921 420b4538 Renato Botelho
		$cflink['interface'] = $this->GetInterface();
922 92125c97 Ermal Luçi
		$cflink['name'] = $this->GetQname();
923
		$cflink['scheduler'] = $this->GetScheduler();
924
		$cflink['bandwidth'] = $this->GetBandwidth();
925
		$cflink['bandwidthtype'] = $this->GetBwscale();
926 f5881023 Ermal Lu?i
		$cflink['qlimit'] = trim($this->GetQlimit());
927 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
928 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
929 61e047a5 Phil Davis
		}
930 f5881023 Ermal Lu?i
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
931 61e047a5 Phil Davis
		if (empty($cflink['tbrconfig'])) {
932 f5881023 Ermal Lu?i
			unset($cflink['tbrconfig']);
933 61e047a5 Phil Davis
		}
934 92125c97 Ermal Luçi
		$cflink['enabled'] = $this->GetEnabled();
935 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
936 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
937 61e047a5 Phil Davis
		}
938 92125c97 Ermal Luçi
	}
939 061f78b1 Bill Marquette
940 197bfe96 Ermal Luçi
}
941 061f78b1 Bill Marquette
942 197bfe96 Ermal Luçi
class priq_queue {
943 92125c97 Ermal Luçi
	var $qname;
944 420b4538 Renato Botelho
	var $qinterface;
945 92125c97 Ermal Luçi
	var $qlimit;
946
	var $qpriority;
947
	var $description;
948
	var $isparent;
949 197bfe96 Ermal Luçi
	var $qbandwidth;
950
	var $qbandwidthtype;
951 f5881023 Ermal Lu?i
	var $qdefault = "";
952
	var $qrio = "";
953
	var $qred = "";
954 8edaa92c Ermal
	var $qcodel = "";
955 f5881023 Ermal Lu?i
	var $qecn = "";
956 197bfe96 Ermal Luçi
	var $qack;
957 f5881023 Ermal Lu?i
	var $qenabled = "";
958 197bfe96 Ermal Luçi
	var $qparent;
959
	var $link;
960 92125c97 Ermal Luçi
961 70b139a3 Chris Buechler
	/* This is here to help with form building and building rules/lists */
962 57c448d0 Ermal
	var $subqueues = array();
963 92125c97 Ermal Luçi
964 d6fa899d Phil Davis
	/* Accessor functions */
965 92125c97 Ermal Luçi
	function SetLink($link) {
966 f5881023 Ermal Lu?i
		$this->link = $link;
967 92125c97 Ermal Luçi
	}
968
	function GetLink() {
969 f5881023 Ermal Lu?i
		return $this->link;
970 92125c97 Ermal Luçi
	}
971 197bfe96 Ermal Luçi
	function &GetParent() {
972
		return $this->qparent;
973
	}
974
	function SetParent(&$parent) {
975
		$this->qparent = &$parent;
976
	}
977 92125c97 Ermal Luçi
	function GetEnabled() {
978 f5881023 Ermal Lu?i
		return $this->qenabled;
979 92125c97 Ermal Luçi
	}
980
	function SetEnabled($value) {
981 f5881023 Ermal Lu?i
		$this->qenabled = $value;
982 92125c97 Ermal Luçi
	}
983 70b139a3 Chris Buechler
	function CanHaveChildren() {
984 f5881023 Ermal Lu?i
		return false;
985 92125c97 Ermal Luçi
	}
986 197bfe96 Ermal Luçi
	function CanBeDeleted() {
987 f5881023 Ermal Lu?i
		return true;
988 92125c97 Ermal Luçi
	}
989
	function GetQname() {
990 f5881023 Ermal Lu?i
		return $this->qname;
991 92125c97 Ermal Luçi
	}
992
	function SetQname($name) {
993 f5881023 Ermal Lu?i
		$this->qname = trim($name);
994 92125c97 Ermal Luçi
	}
995
	function GetBandwidth() {
996 f5881023 Ermal Lu?i
		return $this->qbandwidth;
997 92125c97 Ermal Luçi
	}
998
	function SetBandwidth($bandwidth) {
999 f5881023 Ermal Lu?i
		$this->qbandwidth = $bandwidth;
1000 92125c97 Ermal Luçi
	}
1001
	function GetInterface() {
1002 f5881023 Ermal Lu?i
		return $this->qinterface;
1003 92125c97 Ermal Luçi
	}
1004
	function SetInterface($name) {
1005 f5881023 Ermal Lu?i
		$this->qinterface = trim($name);
1006 92125c97 Ermal Luçi
	}
1007
	function GetQlimit() {
1008 f5881023 Ermal Lu?i
		return $this->qlimit;
1009 92125c97 Ermal Luçi
	}
1010
	function SetQlimit($limit) {
1011 f5881023 Ermal Lu?i
		$this->qlimit = $limit;
1012 92125c97 Ermal Luçi
	}
1013
	function GetQpriority() {
1014 f5881023 Ermal Lu?i
		return $this->qpriority;
1015 92125c97 Ermal Luçi
	}
1016
	function SetQpriority($priority) {
1017 f5881023 Ermal Lu?i
		$this->qpriority = $priority;
1018 92125c97 Ermal Luçi
	}
1019
	function GetDescription() {
1020 f5881023 Ermal Lu?i
		return $this->description;
1021 92125c97 Ermal Luçi
	}
1022
	function SetDescription($str) {
1023 f5881023 Ermal Lu?i
		$this->description = trim($str);
1024 92125c97 Ermal Luçi
	}
1025
	function GetFirstime() {
1026 f5881023 Ermal Lu?i
		return $this->firsttime;
1027 92125c97 Ermal Luçi
	}
1028
	function SetFirsttime($number) {
1029 f5881023 Ermal Lu?i
		$this->firsttime = $number;
1030 92125c97 Ermal Luçi
	}
1031 45eeb038 Luiz Otavio O Souza
	function CheckBandwidth($bw, $bwtype) {
1032
		$parent = $this->GetParent();
1033
		$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1034
		if ($bw > 0)
1035
			$sum += get_bandwidth($bw, $bwtype, $parent);
1036
		if ($sum > get_queue_bandwidth($parent))
1037
			return 1;
1038
1039
		foreach ($this->subqueues as $q) {
1040
			if ($q->CheckBandwidth(0, ''))
1041
				return 1;
1042
		}
1043
1044
		return 0;
1045
	}
1046
	function GetTotalBw($qignore = NULL) {
1047 2638ddec Luiz Otavio O Souza
		$sum = 0;
1048
		foreach ($this->subqueues as $q) {
1049 45eeb038 Luiz Otavio O Souza
			if ($qignore != NULL && $qignore == $q)
1050
				continue;
1051
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1052 2638ddec Luiz Otavio O Souza
		}
1053
1054
		return $sum;
1055
	}
1056 92125c97 Ermal Luçi
	function GetBwscale() {
1057 f5881023 Ermal Lu?i
		return $this->qbandwidthtype;
1058 92125c97 Ermal Luçi
	}
1059
	function SetBwscale($scale) {
1060 f5881023 Ermal Lu?i
		$this->qbandwidthtype = $scale;
1061 92125c97 Ermal Luçi
	}
1062 f61dc8e6 Ermal
	function GetDefaultQueuePresent() {
1063 61e047a5 Phil Davis
		if ($this->GetDefault()) {
1064 f61dc8e6 Ermal
			return true;
1065 61e047a5 Phil Davis
		}
1066 f61dc8e6 Ermal
		if (!empty($this->subqueues)) {
1067
			foreach ($this->subqueues as $q) {
1068 61e047a5 Phil Davis
				if ($q->GetDefault()) {
1069 f61dc8e6 Ermal
					return true;
1070 61e047a5 Phil Davis
				}
1071 f61dc8e6 Ermal
			}
1072
		}
1073
1074
		return false;
1075
	}
1076 197bfe96 Ermal Luçi
	function GetDefault() {
1077 f5881023 Ermal Lu?i
		return $this->qdefault;
1078 92125c97 Ermal Luçi
	}
1079
	function SetDefault($value = false) {
1080 f5881023 Ermal Lu?i
		$this->qdefault = $value;
1081 92125c97 Ermal Luçi
	}
1082 8edaa92c Ermal
	function GetCodel() {
1083
		return $this->codel;
1084
	}
1085
	function SetCodel($codel = false) {
1086
		$this->codel = $codel;
1087
	}
1088 197bfe96 Ermal Luçi
	function GetRed() {
1089 f5881023 Ermal Lu?i
		return $this->qred;
1090 92125c97 Ermal Luçi
	}
1091
	function SetRed($red = false) {
1092 f5881023 Ermal Lu?i
		$this->qred = $red;
1093 92125c97 Ermal Luçi
	}
1094 197bfe96 Ermal Luçi
	function GetRio() {
1095 f5881023 Ermal Lu?i
		return $this->qrio;
1096 92125c97 Ermal Luçi
	}
1097
	function SetRio($rio = false) {
1098 f5881023 Ermal Lu?i
		$this->qrio = $rio;
1099 92125c97 Ermal Luçi
	}
1100 f5881023 Ermal Lu?i
	function GetEcn() {
1101
		return $this->qecn;
1102 92125c97 Ermal Luçi
	}
1103
	function SetEcn($ecn = false) {
1104 f5881023 Ermal Lu?i
		$this->qecn = $ecn;
1105 92125c97 Ermal Luçi
	}
1106 197bfe96 Ermal Luçi
	function GetAck() {
1107 f5881023 Ermal Lu?i
		return $this->qack;
1108 92125c97 Ermal Luçi
	}
1109
	function SetAck($ack = false) {
1110 f5881023 Ermal Lu?i
		$this->qack = $ack;
1111 92125c97 Ermal Luçi
	}
1112 197bfe96 Ermal Luçi
1113 bfc94df0 Phil Davis
	function GetBwscaleText() {
1114
		switch ($this->qbandwidthtype) {
1115
			case "b":
1116
				$bwscaletext = "Bit/s";
1117
				break;
1118
			case "Kb":
1119
				$bwscaletext = "Kbit/s";
1120
				break;
1121
			case "Mb":
1122
				$bwscaletext = "Mbit/s";
1123
				break;
1124
			case "Gb":
1125
				$bwscaletext = "Gbit/s";
1126
				break;
1127
			default:
1128
				/* For others that do not need translating like % */
1129
				$bwscaletext = $this->qbandwidthtype;
1130
				break;
1131
		}
1132
		return $bwscaletext;
1133
	}
1134
1135 197bfe96 Ermal Luçi
	function build_javascript() {
1136
		$javascript = "<script type=\"text/javascript\">";
1137 ee02550a Michele Di Maria
		$javascript .= "//<![CDATA[\n";
1138 197bfe96 Ermal Luçi
		$javascript .= "function mySuspend() { \n";
1139 01890f6a Ermal
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1140 92125c97 Ermal Luçi
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1141
		$javascript .= "else if (document.all)\n";
1142
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1143
		$javascript .= "}\n";
1144
1145
		$javascript .= "function myResume() {\n";
1146
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1147
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1148
		$javascript .= "else if (document.all)\n";
1149
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1150
		$javascript .= "}\n";
1151 0fa05f45 Colin Fleming
		$javascript .= "//]]>";
1152 92125c97 Ermal Luçi
		$javascript .= "</script>";
1153 ce0117f7 Colin Fleming
1154 197bfe96 Ermal Luçi
		return $javascript;
1155
	}
1156 ce0117f7 Colin Fleming
1157 6f627d41 Ermal Luçi
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1158
1159 420b4538 Renato Botelho
	/*
1160
	 * Currently this will not be called unless we decide to clone a whole
1161 197bfe96 Ermal Luçi
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1162
	 */
1163 420b4538 Renato Botelho
	function copy_queue($interface, &$cflink) {
1164
1165
		$cflink['name'] = $this->GetQname();
1166
		$cflink['interface'] = $interface;
1167
		$cflink['qlimit'] = $this->GetQlimit();
1168
		$cflink['priority'] = $this->GetQpriority();
1169
		$cflink['description'] = $this->GetDescription();
1170
		$cflink['enabled'] = $this->GetEnabled();
1171
		$cflink['default'] = $this->GetDefault();
1172
		$cflink['red'] = $this->GetRed();
1173
		$cflink['codel'] = $this->GetCodel();
1174
		$cflink['rio'] = $this->GetRio();
1175
		$cflink['ecn'] = $this->GetEcn();
1176
1177
		if (is_array($this->subqueues)) {
1178
			$cflinkp['queue'] = array();
1179
			foreach ($this->subqueues as $q) {
1180
				$cflink['queue'][$q->GetQname()] = array();
1181
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1182 a843b04f Ermal Luçi
			}
1183 420b4538 Renato Botelho
		}
1184
	}
1185 197bfe96 Ermal Luçi
1186
	function clean_queue($sched) {
1187 92125c97 Ermal Luçi
		clean_child_queues($sched, $this->GetLink());
1188
		if (is_array($this->subqueues)) {
1189 61e047a5 Phil Davis
			foreach ($this->subqueues as $q) {
1190 f5881023 Ermal Lu?i
				$q->clean_queue($sched);
1191 61e047a5 Phil Davis
			}
1192 92125c97 Ermal Luçi
		}
1193 f5881023 Ermal Lu?i
	}
1194 197bfe96 Ermal Luçi
1195 420b4538 Renato Botelho
	function &get_queue_list(&$qlist) {
1196 ce0117f7 Colin Fleming
1197 f5881023 Ermal Lu?i
		$qlist[$this->GetQname()] = & $this;
1198
		if (is_array($this->subqueues)) {
1199 61e047a5 Phil Davis
			foreach ($this->subqueues as $queue) {
1200 f5881023 Ermal Lu?i
				$queue->get_queue_list($qlist);
1201 61e047a5 Phil Davis
			}
1202 f5881023 Ermal Lu?i
		}
1203
	}
1204 fe93b17b Ermal Luçi
1205 197bfe96 Ermal Luçi
	function delete_queue() {
1206 92125c97 Ermal Luçi
		unref_on_altq_queue_list($this->GetQname());
1207
		cleanup_queue_from_rules($this->GetQname());
1208
		unset_object_by_reference($this->GetLink());
1209
	}
1210 ce0117f7 Colin Fleming
1211 b0262cb2 Ermal Luçi
	function delete_all() {
1212 420b4538 Renato Botelho
		if (count($this->subqueues)) {
1213
			foreach ($this->subqueues as $q) {
1214
				$q->delete_all();
1215
				unset_object_by_reference($q->GetLink());
1216
				unset($q);
1217
			}
1218
			unset($this->subqueues);
1219
		}
1220
	}
1221
1222
	function &find_queue($interface, $qname) {
1223 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
1224 420b4538 Renato Botelho
			return $this;
1225 61e047a5 Phil Davis
		}
1226 197bfe96 Ermal Luçi
	}
1227 ce0117f7 Colin Fleming
1228 62150088 Ermal Lu?i
	function find_parentqueue($interface, $qname) { return; }
1229 ce0117f7 Colin Fleming
1230 92125c97 Ermal Luçi
	function validate_input($data, &$input_errors) {
1231 ce0117f7 Colin Fleming
1232 2c072899 jim-p
		$reqdfields[] = "name";
1233 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Name");
1234 40de74f5 Ermal Luçi
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1235 197bfe96 Ermal Luçi
1236 61e047a5 Phil Davis
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1237 e8c516a0 Phil Davis
			$input_errors[] = gettext("Bandwidth must be an integer.");
1238 61e047a5 Phil Davis
		}
1239
		if ($data['bandwidth'] < 0) {
1240 e8c516a0 Phil Davis
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1241 61e047a5 Phil Davis
		}
1242 45eeb038 Luiz Otavio O Souza
		if ($data['bandwidthtype'] == "%") {
1243
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1244
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1245
			}
1246
		}
1247
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1248
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1249 61e047a5 Phil Davis
		if ($data['priority'] && (!is_numeric($data['priority']) ||
1250 4de8f7ba Phil Davis
		    ($data['priority'] < 1) || ($data['priority'] > 15))) {
1251 f599d893 Vinicius Coque
			$input_errors[] = gettext("The priority must be an integer between 1 and 15.");
1252 92125c97 Ermal Luçi
		}
1253 61e047a5 Phil Davis
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1254 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Queue limit must be an integer");
1255 61e047a5 Phil Davis
		}
1256
		if ($data['qlimit'] < 0) {
1257 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Queue limit must be positive");
1258 61e047a5 Phil Davis
		}
1259
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1260 420b4538 Renato Botelho
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1261 61e047a5 Phil Davis
		}
1262
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1263 420b4538 Renato Botelho
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1264 61e047a5 Phil Davis
		}
1265 f8dca5a3 Renato Botelho
		$default = $this->GetDefault();
1266 61e047a5 Phil Davis
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1267 f61dc8e6 Ermal
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1268 61e047a5 Phil Davis
		}
1269 061f78b1 Bill Marquette
	}
1270
1271 92125c97 Ermal Luçi
	function ReadConfig(&$q) {
1272 30ef6f8d Ermal
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1273
			$this->SetQname($q['newname']);
1274
		} else if (!empty($q['newname'])) {
1275
			$this->SetQname($q['newname']);
1276 61e047a5 Phil Davis
		} else if (isset($q['name'])) {
1277 30ef6f8d Ermal
			$this->SetQname($q['name']);
1278 61e047a5 Phil Davis
		}
1279
		if (isset($q['interface'])) {
1280 f61dc8e6 Ermal
			$this->SetInterface($q['interface']);
1281 61e047a5 Phil Davis
		}
1282 0b13e3f9 Ermal
		$this->SetBandwidth($q['bandwidth']);
1283 61e047a5 Phil Davis
		if ($q['bandwidthtype'] <> "") {
1284 0b13e3f9 Ermal
			$this->SetBwscale($q['bandwidthtype']);
1285 61e047a5 Phil Davis
		}
1286
		if (!empty($q['qlimit'])) {
1287 f5881023 Ermal Lu?i
			$this->SetQlimit($q['qlimit']);
1288 61e047a5 Phil Davis
		} else {
1289 ea25d26d Ermal Lu?i
			$this->SetQlimit(""); // Default
1290 61e047a5 Phil Davis
		}
1291
		if (!empty($q['priority'])) {
1292 f5881023 Ermal Lu?i
			$this->SetQPriority($q['priority']);
1293 61e047a5 Phil Davis
		} else {
1294 ea25d26d Ermal Lu?i
			$this->SetQpriority("");
1295 61e047a5 Phil Davis
		}
1296
		if (!empty($q['description'])) {
1297 f5881023 Ermal Lu?i
			$this->SetDescription($q['description']);
1298 61e047a5 Phil Davis
		} else {
1299 da0ce7ee Chris Buechler
			$this->SetDescription("");
1300 61e047a5 Phil Davis
		}
1301
		if (!empty($q['red'])) {
1302 f5881023 Ermal Lu?i
			$this->SetRed($q['red']);
1303 61e047a5 Phil Davis
		} else {
1304 ea25d26d Ermal Lu?i
			$this->SetRed();
1305 61e047a5 Phil Davis
		}
1306
		if (!empty($q['codel'])) {
1307 8edaa92c Ermal
			$this->SetCodel($q['codel']);
1308 61e047a5 Phil Davis
		} else {
1309 8edaa92c Ermal
			$this->SetCodel();
1310 61e047a5 Phil Davis
		}
1311
		if (!empty($q['rio'])) {
1312 f5881023 Ermal Lu?i
			$this->SetRio($q['rio']);
1313 61e047a5 Phil Davis
		} else {
1314 ea25d26d Ermal Lu?i
			$this->SetRio();
1315 61e047a5 Phil Davis
		}
1316
		if (!empty($q['ecn'])) {
1317 f5881023 Ermal Lu?i
			$this->SetEcn($q['ecn']);
1318 61e047a5 Phil Davis
		} else {
1319 ea25d26d Ermal Lu?i
			$this->SetEcn();
1320 61e047a5 Phil Davis
		}
1321
		if (!empty($q['default'])) {
1322 f5881023 Ermal Lu?i
			$this->SetDefault($q['default']);
1323 61e047a5 Phil Davis
		} else {
1324 ea25d26d Ermal Lu?i
			$this->SetDefault();
1325 61e047a5 Phil Davis
		}
1326
		if (!empty($q['enabled'])) {
1327 f5881023 Ermal Lu?i
			$this->SetEnabled($q['enabled']);
1328 61e047a5 Phil Davis
		} else {
1329 ea25d26d Ermal Lu?i
			$this->SetEnabled("");
1330 61e047a5 Phil Davis
		}
1331 f5881023 Ermal Lu?i
	}
1332 197bfe96 Ermal Luçi
1333
	function build_tree() {
1334 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1335 f5881023 Ermal Lu?i
		$tree .= "\" ";
1336
		$tmpvalue = $this->GetDefault();
1337 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
1338 f5881023 Ermal Lu?i
			$tree .= " class=\"navlnk\"";
1339 61e047a5 Phil Davis
		}
1340 f5881023 Ermal Lu?i
		$tree .= " >" . $this->GetQname() . "</a>";
1341 420b4538 Renato Botelho
		/*
1342 f5881023 Ermal Lu?i
		 * Not needed here!
1343
		 * if (is_array($queues) {
1344
		 *	  $tree .= "<ul>";
1345 420b4538 Renato Botelho
		 *	  foreach ($q as $queues)
1346 f5881023 Ermal Lu?i
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1347 420b4538 Renato Botelho
		 *	  endforeach
1348 f5881023 Ermal Lu?i
		 *	  $tree .= "</ul>";
1349
		 * }
1350 92125c97 Ermal Luçi
		 */
1351 f5881023 Ermal Lu?i
1352 420b4538 Renato Botelho
		$tree .= "</li>";
1353 f5881023 Ermal Lu?i
1354
		return $tree;
1355
	}
1356 ce0117f7 Colin Fleming
1357 f5881023 Ermal Lu?i
	/* Should return something like:
1358
	 * queue $qname on $qinterface bandwidth ....
1359
	 */
1360 9d0b0635 Ermal
	function build_rules(&$default = false) {
1361 f5881023 Ermal Lu?i
		$pfq_rule = " queue ". $this->qname;
1362 61e047a5 Phil Davis
		if ($this->GetInterface()) {
1363 f5881023 Ermal Lu?i
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1364 61e047a5 Phil Davis
		}
1365 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetQpriority();
1366 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
1367 f5881023 Ermal Lu?i
			$pfq_rule .= " priority ".$this->GetQpriority();
1368 61e047a5 Phil Davis
		}
1369 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetQlimit();
1370 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
1371 f5881023 Ermal Lu?i
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1372 61e047a5 Phil Davis
		}
1373 8edaa92c Ermal
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1374 f5881023 Ermal Lu?i
			$pfq_rule .= " priq ( ";
1375
			$tmpvalue = $this->GetRed();
1376
			if (!empty($tmpvalue)) {
1377
				$comma = 1;
1378
				$pfq_rule .= " red ";
1379
			}
1380
			$tmpvalue = $this->GetRio();
1381
			if (!empty($tmpvalue)) {
1382 61e047a5 Phil Davis
				if ($comma) {
1383 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
1384 61e047a5 Phil Davis
				}
1385 f5881023 Ermal Lu?i
				$comma = 1;
1386
				$pfq_rule .= " rio ";
1387
			}
1388
			$tmpvalue = $this->GetEcn();
1389
			if (!empty($tmpvalue)) {
1390 61e047a5 Phil Davis
				if ($comma) {
1391 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
1392 61e047a5 Phil Davis
				}
1393 f5881023 Ermal Lu?i
				$comma = 1;
1394
				$pfq_rule .= " ecn ";
1395
			}
1396 8edaa92c Ermal
			$tmpvalue = $this->GetCodel();
1397
			if (!empty($tmpvalue)) {
1398 61e047a5 Phil Davis
				if ($comma) {
1399 8edaa92c Ermal
					$pfq_rule .= " ,";
1400 61e047a5 Phil Davis
				}
1401 8edaa92c Ermal
				$comma = 1;
1402
				$pfq_rule .= " codel ";
1403
			}
1404 f5881023 Ermal Lu?i
			$tmpvalue = $this->GetDefault();
1405
			if (!empty($tmpvalue)) {
1406 61e047a5 Phil Davis
				if ($comma) {
1407 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
1408 61e047a5 Phil Davis
				}
1409 f5881023 Ermal Lu?i
				$pfq_rule .= " default ";
1410 ef8fca71 Ermal
				$default = true;
1411 f5881023 Ermal Lu?i
			}
1412
			$pfq_rule .= " ) ";
1413 92125c97 Ermal Luçi
		}
1414
1415 f5881023 Ermal Lu?i
		$pfq_rule .= " \n";
1416
1417
		return $pfq_rule;
1418
	}
1419
1420
	/*
1421
	 * To return the html form to show to user
1422
	 * for getting the parameters.
1423
	 * Should do even for first time when the
1424
	 * object is created and later when we may
1425 aef9d8fe Stephen Beaver
	 * need to update it. (2)
1426 f5881023 Ermal Lu?i
	 */
1427 806942d0 Stephen Beaver
1428 f5881023 Ermal Lu?i
	function build_form() {
1429 806942d0 Stephen Beaver
1430 27d6a45b jim-p
		$sform = new Form();
1431 5605a0c4 Stephen Beaver
1432
		$sform->setAction("firewall_shaper.php");
1433 806942d0 Stephen Beaver
1434 3feefc07 Stephen Beaver
		$section = new Form_Section("");
1435 806942d0 Stephen Beaver
1436 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Checkbox(
1437
			'enabled',
1438
			'Enable/Disable',
1439
			'Enable/disable discipline and its children',
1440
			($this->GetEnabled() == "on"),
1441
			'on'
1442
		));
1443 806942d0 Stephen Beaver
1444 3feefc07 Stephen Beaver
		$section->addInput(new Form_Input(
1445 5605a0c4 Stephen Beaver
			'newname',
1446 85877e3c Phil Davis
			'*Name',
1447 3feefc07 Stephen Beaver
			'text',
1448 aef9d8fe Stephen Beaver
			$this->GetQname()
1449
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1450 806942d0 Stephen Beaver
1451 5605a0c4 Stephen Beaver
		$section->addInput(new Form_Input(
1452
			'name',
1453
			null,
1454
			'hidden',
1455
			$this->GetQname()
1456
		));
1457
1458 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
1459
			'priority',
1460
			'Priority',
1461
			'number',
1462
			$this->GetQpriority(),
1463
			['min' => '0', 'max'=> '7']
1464
		))->setHelp('For hfsc, the range is 0 to 7. The default is 1. Hfsc queues with a higher priority are preferred in the case of overload.');
1465
1466
		$section->addInput(new Form_Input(
1467
			'qlimit',
1468
			'Queue Limit',
1469
			'number',
1470
			$this->GetQlimit()
1471
		))->setHelp('Queue limit in packets.');
1472 806942d0 Stephen Beaver
1473 aef9d8fe Stephen Beaver
		$group = new Form_Group('Scheduler options');
1474 806942d0 Stephen Beaver
1475 bc788561 Ermal
		if (empty($this->subqueues)) {
1476 aef9d8fe Stephen Beaver
			$group->add(new Form_Checkbox(
1477
				'default',
1478
				null,
1479
				null,
1480 5605a0c4 Stephen Beaver
				$this->GetDefault(),
1481
				'default'
1482 806942d0 Stephen Beaver
			))->setHelp('Default Queue');
1483 aef9d8fe Stephen Beaver
		}
1484 806942d0 Stephen Beaver
1485 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
1486
			'red',
1487
			null,
1488
			null,
1489
			!empty($this->GetRed())
1490 7a48a7f7 jim-p
		))->setHelp('<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#red">' . gettext('Random Early Detection') . '</a>');
1491 806942d0 Stephen Beaver
1492 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
1493
			'rio',
1494
			null,
1495
			null,
1496
			!empty($this->GetRio())
1497 7a48a7f7 jim-p
		))->setHelp('<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#rio">' . gettext('Random Early Detection In and Out') . '</a>');
1498 806942d0 Stephen Beaver
1499 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
1500
			'ecn',
1501
			null,
1502
			null,
1503
			!empty($this->GetEcn())
1504 7a48a7f7 jim-p
		))->setHelp('<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Explicit Congestion Notification') . '</a>');
1505 806942d0 Stephen Beaver
1506 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
1507
			'codel',
1508
			null,
1509
			null,
1510
			!empty($this->GetCodel())
1511 7a48a7f7 jim-p
		))->setHelp('<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Codel Active Queue') . '</a>');
1512 aef9d8fe Stephen Beaver
1513
		$group->setHelp('Select options for this queue');
1514 806942d0 Stephen Beaver
1515
		$section->add($group);
1516
1517 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Input(
1518
			'description',
1519
			'Description',
1520
			'text',
1521
			$this->GetDescription()
1522
		));
1523 806942d0 Stephen Beaver
1524 5605a0c4 Stephen Beaver
		$sform->add($section);
1525
1526
		$sform->addGlobal(new Form_Input(
1527 aef9d8fe Stephen Beaver
			'interface',
1528
			null,
1529
			'hidden',
1530
			$this->GetInterface()
1531 806942d0 Stephen Beaver
		));
1532
1533 5605a0c4 Stephen Beaver
		$sform->addGlobal(new Form_Input(
1534
			'name',
1535
			null,
1536
			'hidden',
1537
			$this->GetQname()
1538
		));
1539 806942d0 Stephen Beaver
1540 aef9d8fe Stephen Beaver
		return($sform);
1541 806942d0 Stephen Beaver
	}
1542 197bfe96 Ermal Luçi
1543
	function build_shortform() {
1544 61e047a5 Phil Davis
		/* XXX: Hacks in sight. Mostly layer violations!  */
1545 197bfe96 Ermal Luçi
		global $g, $altq_list_queues;
1546 d34ade52 Michele Di Maria
		global $shaperIFlist;
1547 ce0117f7 Colin Fleming
1548 197bfe96 Ermal Luçi
		$altq =& $altq_list_queues[$this->GetInterface()];
1549 d17c4ee9 Stephen Beaver
1550 61e047a5 Phil Davis
		if ($altq) {
1551 b28e1512 Stephen Beaver
			$scheduler = $altq->GetScheduler();
1552
		}
1553 d17c4ee9 Stephen Beaver
1554 b28e1512 Stephen Beaver
		$form = '<dl class="dl-horizontal">';
1555
		$form .= '	<dt>';
1556
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1557
		$form .= '	</dt>';
1558
		$form .= '	<dd>';
1559 d17c4ee9 Stephen Beaver
		$form .=		$scheduler;
1560 b28e1512 Stephen Beaver
		$form .= '	</dd>';
1561 d17c4ee9 Stephen Beaver
1562 b28e1512 Stephen Beaver
		$form .= '	<dt>';
1563 d17c4ee9 Stephen Beaver
		$form .=		'Bandwidth';
1564 b28e1512 Stephen Beaver
		$form .= '	</dt>';
1565
		$form .= '	<dd>';
1566 bfc94df0 Phil Davis
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1567 b28e1512 Stephen Beaver
		$form .= '	</dd>';
1568 d17c4ee9 Stephen Beaver
1569 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetQpriority();
1570 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
1571 d17c4ee9 Stephen Beaver
			$form .= '	<dt>';
1572
			$form .=		'Priority';
1573 b28e1512 Stephen Beaver
			$form .= '	<dt>';
1574
			$form .= '	<dd>';
1575 d17c4ee9 Stephen Beaver
			$form .=		'On';
1576 b28e1512 Stephen Beaver
			$form .= '	</dd>';
1577
		}
1578 d17c4ee9 Stephen Beaver
1579 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetDefault();
1580 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
1581 d17c4ee9 Stephen Beaver
			$form .= '	<dt>';
1582
			$form .=		'Default';
1583 b28e1512 Stephen Beaver
			$form .= '	<dt>';
1584
			$form .= '	<dd>';
1585 d17c4ee9 Stephen Beaver
			$form .=		'On';
1586 b28e1512 Stephen Beaver
			$form .= '	</dd>';
1587
		}
1588 d17c4ee9 Stephen Beaver
1589
			$form .= '	<dt>';
1590 b28e1512 Stephen Beaver
			$form .= 'Delete';
1591
			$form .= '	<dt>';
1592
			$form .= '	<dd>';
1593
1594
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1595
			$form .= $this->GetInterface() . '&amp;queue=';
1596
			$form .= $this->GetQname() . '&amp;action=delete">';
1597 27d6a45b jim-p
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1598
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1599 b28e1512 Stephen Beaver
1600 d17c4ee9 Stephen Beaver
			$form .= '	</dd>';
1601
1602
			$form .= '</dl>';
1603 ce0117f7 Colin Fleming
1604 197bfe96 Ermal Luçi
		return $form;
1605 061f78b1 Bill Marquette
1606 197bfe96 Ermal Luçi
	}
1607 061f78b1 Bill Marquette
1608 61e047a5 Phil Davis
	function update_altq_queue_data(&$q) {
1609 197bfe96 Ermal Luçi
		$this->ReadConfig($q);
1610
	}
1611 061f78b1 Bill Marquette
1612 f5881023 Ermal Lu?i
	function wconfig() {
1613
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1614 61e047a5 Phil Davis
		if (!is_array($cflink)) {
1615 197bfe96 Ermal Luçi
			$cflink = array();
1616 61e047a5 Phil Davis
		}
1617 197bfe96 Ermal Luçi
		$cflink['name'] = $this->GetQname();
1618 f5881023 Ermal Lu?i
		$cflink['interface'] = $this->GetInterface();
1619
		$cflink['qlimit'] = trim($this->GetQlimit());
1620 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
1621 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
1622 61e047a5 Phil Davis
		}
1623 f5881023 Ermal Lu?i
		$cflink['priority'] = trim($this->GetQpriority());
1624 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
1625 f5881023 Ermal Lu?i
			unset($cflink['priority']);
1626 61e047a5 Phil Davis
		}
1627 f5881023 Ermal Lu?i
		$cflink['description'] = trim($this->GetDescription());
1628 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
1629 f5881023 Ermal Lu?i
			unset($cflink['description']);
1630 61e047a5 Phil Davis
		}
1631 f5881023 Ermal Lu?i
		$cflink['enabled'] = trim($this->GetEnabled());
1632 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
1633 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
1634 61e047a5 Phil Davis
		}
1635 f5881023 Ermal Lu?i
		$cflink['default'] = trim($this->GetDefault());
1636 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
1637 f5881023 Ermal Lu?i
			unset($cflink['default']);
1638 61e047a5 Phil Davis
		}
1639 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
1640 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
1641 f5881023 Ermal Lu?i
			unset($cflink['red']);
1642 61e047a5 Phil Davis
		}
1643 8edaa92c Ermal
		$cflink['codel'] = trim($this->GetCodel());
1644 61e047a5 Phil Davis
		if (empty($cflink['codel'])) {
1645 8edaa92c Ermal
			unset($cflink['codel']);
1646 61e047a5 Phil Davis
		}
1647 f5881023 Ermal Lu?i
		$cflink['rio'] = trim($this->GetRio());
1648 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
1649 f5881023 Ermal Lu?i
			unset($cflink['rio']);
1650 61e047a5 Phil Davis
		}
1651 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
1652 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
1653 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
1654 61e047a5 Phil Davis
		}
1655 061f78b1 Bill Marquette
	}
1656
}
1657
1658 197bfe96 Ermal Luçi
class hfsc_queue extends priq_queue {
1659 f5881023 Ermal Lu?i
	/* realtime */
1660 197bfe96 Ermal Luçi
	var $realtime;
1661 f5881023 Ermal Lu?i
	var $r_m1;
1662
	var $r_d;
1663
	var $r_m2;
1664
	/* linkshare */
1665 197bfe96 Ermal Luçi
	var $linkshare;
1666 f5881023 Ermal Lu?i
	var $l_m1;
1667
	var $l_d;
1668
	var $l_m2;
1669
	/* upperlimit */
1670 197bfe96 Ermal Luçi
	var $upperlimit;
1671 f5881023 Ermal Lu?i
	var $u_m1;
1672
	var $u_d;
1673
	var $u_m2;
1674 197bfe96 Ermal Luçi
1675 f5881023 Ermal Lu?i
	/*
1676
	 * HFSC can have nested queues.
1677
	 */
1678 70b139a3 Chris Buechler
	function CanHaveChildren() {
1679 f5881023 Ermal Lu?i
		return true;
1680
	}
1681 197bfe96 Ermal Luçi
	function GetRealtime() {
1682 420b4538 Renato Botelho
		return $this->realtime;
1683 f5881023 Ermal Lu?i
	}
1684
	function GetR_m1() {
1685
		return $this->r_m1;
1686
	}
1687
	function GetR_d() {
1688
		return $this->r_d;
1689
	}
1690
	function GetR_m2() {
1691
		return $this->r_m2;
1692
	}
1693
	function SetRealtime() {
1694
		$this->realtime = "on";
1695
	}
1696
	function DisableRealtime() {
1697
		$this->realtime = "";
1698
	}
1699
	function SetR_m1($value) {
1700
		$this->r_m1 = $value;
1701
	}
1702
	function SetR_d($value) {
1703
		$this->r_d = $value;
1704
	}
1705
	function SetR_m2($value) {
1706
		$this->r_m2 = $value;
1707
	}
1708
	function GetLinkshare() {
1709
		return $this->linkshare;
1710
	}
1711
	function DisableLinkshare() {
1712
		$this->linkshare = "";
1713
	}
1714
	function GetL_m1() {
1715
		return $this->l_m1;
1716
	}
1717
	function GetL_d() {
1718
		return $this->l_d;
1719
	}
1720
	function GetL_m2() {
1721
		return $this->l_m2;
1722
	}
1723
	function SetLinkshare() {
1724
		$this->linkshare = "on";
1725
	}
1726
	function SetL_m1($value) {
1727
		$this->l_m1 = $value;
1728
	}
1729
	function SetL_d($value) {
1730
		$this->l_d = $value;
1731
	}
1732
	function SetL_m2($value) {
1733
		$this->l_m2 = $value;
1734
	}
1735
	function GetUpperlimit() {
1736
		return $this->upperlimit;
1737
	}
1738
	function GetU_m1() {
1739
		return $this->u_m1;
1740
	}
1741
	function GetU_d() {
1742
		return $this->u_d;
1743
	}
1744
	function GetU_m2() {
1745
		return $this->u_m2;
1746
	}
1747
	function SetUpperlimit() {
1748
		$this->upperlimit = "on";
1749
	}
1750
	function DisableUpperlimit() {
1751
		$this->upperlimit = "";
1752
	}
1753
	function SetU_m1($value) {
1754
		$this->u_m1 = $value;
1755
	}
1756
	function SetU_d($value) {
1757
		$this->u_d = $value;
1758
	}
1759
	function SetU_m2($value) {
1760
		$this->u_m2 = $value;
1761
	}
1762
1763
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1764
1765 61e047a5 Phil Davis
		if (!is_array($this->subqueues)) {
1766 f5881023 Ermal Lu?i
			$this->subqueues = array();
1767 61e047a5 Phil Davis
		}
1768 23ca5695 Chris Buechler
		$q =& new hfsc_queue();
1769 f5881023 Ermal Lu?i
		$q->SetInterface($this->GetInterface());
1770 ea51e9f8 Renato Botelho
		$q->SetParent($this);
1771 f5881023 Ermal Lu?i
		$q->ReadConfig($qname);
1772
		$q->validate_input($qname, $input_errors);
1773 197bfe96 Ermal Luçi
1774 f5881023 Ermal Lu?i
		$q->SetEnabled("on");
1775
		$q->SetLink($path);
1776
1777
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1778
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1779
		if (is_array($qname['queue'])) {
1780
			foreach ($qname['queue'] as $key1 => $que) {
1781
				array_push($path, $key1);
1782 ea51e9f8 Renato Botelho
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1783 f5881023 Ermal Lu?i
				array_pop($path);
1784
			}
1785
		}
1786 ce0117f7 Colin Fleming
1787 f5881023 Ermal Lu?i
		return $q;
1788
	}
1789 a843b04f Ermal Luçi
1790 420b4538 Renato Botelho
	function copy_queue($interface, &$cflink) {
1791 a843b04f Ermal Luçi
1792 f5881023 Ermal Lu?i
		$cflink['name'] = $this->GetQname();
1793
		$cflink['interface'] = $interface;
1794
		$cflink['qlimit'] = trim($this->GetQlimit());
1795 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
1796 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
1797 61e047a5 Phil Davis
		}
1798 f5881023 Ermal Lu?i
		$cflink['priority'] = trim($this->GetQpriority());
1799 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
1800 f5881023 Ermal Lu?i
			unset($cflink['priority']);
1801 61e047a5 Phil Davis
		}
1802 f5881023 Ermal Lu?i
		$cflink['description'] = trim($this->GetDescription());
1803 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
1804 f5881023 Ermal Lu?i
			unset($cflink['description']);
1805 61e047a5 Phil Davis
		}
1806 f5881023 Ermal Lu?i
		$cflink['bandwidth'] = $this->GetBandwidth();
1807
		$cflink['bandwidthtype'] = $this->GetBwscale();
1808
		$cflink['enabled'] = trim($this->GetEnabled());
1809 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
1810 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
1811 61e047a5 Phil Davis
		}
1812 f5881023 Ermal Lu?i
		$cflink['default'] = trim($this->GetDefault());
1813 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
1814 f5881023 Ermal Lu?i
			unset($cflink['default']);
1815 61e047a5 Phil Davis
		}
1816 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
1817 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
1818 f5881023 Ermal Lu?i
			unset($cflink['red']);
1819 61e047a5 Phil Davis
		}
1820 f5881023 Ermal Lu?i
		$cflink['rio'] = trim($this->GetRio());
1821 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
1822 f5881023 Ermal Lu?i
			unset($cflink['rio']);
1823 61e047a5 Phil Davis
		}
1824 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
1825 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
1826 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
1827 61e047a5 Phil Davis
		}
1828 f5881023 Ermal Lu?i
		if ($this->GetLinkshare() <> "") {
1829
			if ($this->GetL_m1() <> "") {
1830
				$cflink['linkshare1'] = $this->GetL_m1();
1831
				$cflink['linkshare2'] = $this->GetL_d();
1832
				$cflink['linkshare'] = "on";
1833
			} else {
1834
				unset($cflink['linkshare1']);
1835
				unset($cflink['linkshare2']);
1836
				unset($cflink['linkshare']);
1837 fce82460 Ermal Luçi
			}
1838 f5881023 Ermal Lu?i
			if ($this->GetL_m2() <> "") {
1839
				$cflink['linkshare3'] = $this->GetL_m2();
1840
				$cflink['linkshare'] = "on";
1841
			} else {
1842
				unset($cflink['linkshare3']);
1843
				unset($cflink['linkshare']);
1844 92125c97 Ermal Luçi
			}
1845 f5881023 Ermal Lu?i
		}
1846
		if ($this->GetRealtime() <> "") {
1847
			if ($this->GetR_m1() <> "") {
1848
				$cflink['realtime1'] = $this->GetR_m1();
1849
				$cflink['realtime2'] = $this->GetR_d();
1850
				$cflink['realtime'] = "on";
1851
			} else {
1852
				unset($cflink['realtime1']);
1853 420b4538 Renato Botelho
				unset($cflink['realtime2']);
1854
				unset($cflink['realtime']);
1855 f5881023 Ermal Lu?i
			}
1856
			if ($this->GetR_m2() <> "") {
1857
				$cflink['realtime3'] = $this->GetR_m2();
1858
				$cflink['realtime'] = "on";
1859
			} else {
1860
				unset($cflink['realtime3']);
1861
				unset($cflink['realtime']);
1862 92125c97 Ermal Luçi
			}
1863 b7ff5e40 Scott Ullrich
		}
1864 f5881023 Ermal Lu?i
		if ($this->GetUpperlimit() <> "") {
1865
			if ($this->GetU_m1() <> "") {
1866
				$cflink['upperlimit1'] = $this->GetU_m1();
1867
				$cflink['upperlimit2'] = $this->GetU_d();
1868
				$cflink['upperlimit'] = "on";
1869
			} else {
1870
				unset($cflink['upperlimit']);
1871
				unset($cflink['upperlimit1']);
1872
				unset($cflink['upperlimit2']);
1873
			}
1874
			if ($this->GetU_m2() <> "") {
1875
				$cflink['upperlimit3'] = $this->GetU_m2();
1876
				$cflink['upperlimit'] = "on";
1877
			} else {
1878
				unset($cflink['upperlimit3']);
1879
				unset($cflink['upperlimit']);
1880
			}
1881 92125c97 Ermal Luçi
		}
1882 40de74f5 Ermal Luçi
1883 f5881023 Ermal Lu?i
		if (is_array($this->subqueues)) {
1884
			$cflinkp['queue'] = array();
1885
			foreach ($this->subqueues as $q) {
1886
				$cflink['queue'][$q->GetQname()] = array();
1887 ea51e9f8 Renato Botelho
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1888 f5881023 Ermal Lu?i
			}
1889
		}
1890
	}
1891
1892 420b4538 Renato Botelho
	function delete_queue() {
1893 f5881023 Ermal Lu?i
		unref_on_altq_queue_list($this->GetQname());
1894
		cleanup_queue_from_rules($this->GetQname());
1895
		$parent =& $this->GetParent();
1896 45eeb038 Luiz Otavio O Souza
		foreach ($this->subqueues as $q)
1897
			$q->delete_queue();
1898 f5881023 Ermal Lu?i
		unset_object_by_reference($this->GetLink());
1899
	}
1900
1901
	/*
1902
	 * Should search even its children
1903
	 */
1904
	function &find_queue($interface, $qname) {
1905 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
1906 f5881023 Ermal Lu?i
			return $this;
1907 61e047a5 Phil Davis
		}
1908 f5881023 Ermal Lu?i
1909
		foreach ($this->subqueues as $q) {
1910
			$result =& $q->find_queue("", $qname);
1911 61e047a5 Phil Davis
			if ($result) {
1912 f5881023 Ermal Lu?i
				return $result;
1913 61e047a5 Phil Davis
			}
1914 f5881023 Ermal Lu?i
		}
1915
	}
1916
1917
	function &find_parentqueue($interface, $qname) {
1918 61e047a5 Phil Davis
		if ($this->subqueues[$qname]) {
1919 f5881023 Ermal Lu?i
			return $this;
1920 61e047a5 Phil Davis
		}
1921 f5881023 Ermal Lu?i
		foreach ($this->subqueues as $q) {
1922
			$result = $q->find_parentqueue("", $qname);
1923 61e047a5 Phil Davis
			if ($result) {
1924 f5881023 Ermal Lu?i
				return $result;
1925 61e047a5 Phil Davis
			}
1926 f5881023 Ermal Lu?i
		}
1927
	}
1928 ce0117f7 Colin Fleming
1929 f5881023 Ermal Lu?i
	function validate_input($data, &$input_errors) {
1930
		parent::validate_input($data, $input_errors);
1931 ce0117f7 Colin Fleming
1932 f5881023 Ermal Lu?i
		$reqdfields[] = "bandwidth";
1933 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidth");
1934 f5881023 Ermal Lu?i
		$reqdfields[] = "bandwidthtype";
1935 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidthtype");
1936 f5881023 Ermal Lu?i
1937
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1938 ce0117f7 Colin Fleming
1939 f5881023 Ermal Lu?i
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
1940 61e047a5 Phil Davis
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1941 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Bandwidth must be an integer.");
1942 61e047a5 Phil Davis
			}
1943 f5881023 Ermal Lu?i
1944 61e047a5 Phil Davis
			if ($data['bandwidth'] < 0) {
1945 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Bandwidth cannot be negative.");
1946 61e047a5 Phil Davis
			}
1947 f5881023 Ermal Lu?i
1948
			if ($data['bandwidthtype'] == "%") {
1949 61e047a5 Phil Davis
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1950 45eeb038 Luiz Otavio O Souza
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1951 61e047a5 Phil Davis
				}
1952 f5881023 Ermal Lu?i
			}
1953 92125c97 Ermal Luçi
		}
1954
1955 4de8f7ba Phil Davis
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
1956 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
1957 61e047a5 Phil Davis
		}
1958 4de8f7ba Phil Davis
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
1959 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
1960 61e047a5 Phil Davis
		}
1961
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
1962 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
1963 61e047a5 Phil Davis
		}
1964
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
1965 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
1966 61e047a5 Phil Davis
		}
1967
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
1968 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
1969 61e047a5 Phil Davis
		}
1970 a843b04f Ermal Luçi
1971 f5881023 Ermal Lu?i
		/*
1972 40de74f5 Ermal Luçi
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
1973 f5881023 Ermal Lu?i
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
1974
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
1975 61e047a5 Phil Davis
			if (floatval($bw_1) < floatval($bw_2)) {
1976 f5881023 Ermal Lu?i
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
1977 61e047a5 Phil Davis
			}
1978 40de74f5 Ermal Luçi
1979 61e047a5 Phil Davis
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1980 794195d1 Phil Davis
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
1981 61e047a5 Phil Davis
			}
1982 40de74f5 Ermal Luçi
		}
1983 f5881023 Ermal Lu?i
		*/
1984 4de8f7ba Phil Davis
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
1985 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
1986 61e047a5 Phil Davis
		}
1987 4de8f7ba Phil Davis
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
1988 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
1989 61e047a5 Phil Davis
		}
1990
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
1991 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
1992 61e047a5 Phil Davis
		}
1993
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
1994 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("linkshare d value needs to be numeric");
1995 61e047a5 Phil Davis
		}
1996
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
1997 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
1998 61e047a5 Phil Davis
		}
1999 4de8f7ba Phil Davis
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2000 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2001 61e047a5 Phil Davis
		}
2002 4de8f7ba Phil Davis
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2003 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2004 61e047a5 Phil Davis
		}
2005 40de74f5 Ermal Luçi
2006 f5881023 Ermal Lu?i
		/*
2007 a843b04f Ermal Luçi
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2008 f5881023 Ermal Lu?i
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2009 420b4538 Renato Botelho
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2010 61e047a5 Phil Davis
			if (floatval($bw_1) < floatval($bw_2)) {
2011 420b4538 Renato Botelho
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2012 61e047a5 Phil Davis
			}
2013 40de74f5 Ermal Luçi
2014 61e047a5 Phil Davis
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2015 420b4538 Renato Botelho
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2016 61e047a5 Phil Davis
			}
2017 40de74f5 Ermal Luçi
		}
2018 f5881023 Ermal Lu?i
		*/
2019 a2f70da3 Ermal Luçi
2020 61e047a5 Phil Davis
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2021 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2022 61e047a5 Phil Davis
		}
2023
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2024 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("realtime d value needs to be numeric");
2025 61e047a5 Phil Davis
		}
2026
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2027 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2028 61e047a5 Phil Davis
		}
2029 40de74f5 Ermal Luçi
2030 f5881023 Ermal Lu?i
		/*
2031 a843b04f Ermal Luçi
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2032 f5881023 Ermal Lu?i
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2033 420b4538 Renato Botelho
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2034 61e047a5 Phil Davis
			if (floatval($bw_1) < floatval($bw_2)) {
2035 420b4538 Renato Botelho
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2036 61e047a5 Phil Davis
			}
2037 40de74f5 Ermal Luçi
2038 61e047a5 Phil Davis
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2039 794195d1 Phil Davis
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2040 61e047a5 Phil Davis
			}
2041 40de74f5 Ermal Luçi
		}
2042 f5881023 Ermal Lu?i
		*/
2043 061f78b1 Bill Marquette
	}
2044
2045 f5881023 Ermal Lu?i
	function ReadConfig(&$cflink) {
2046
		if (!empty($cflink['linkshare'])) {
2047
			if (!empty($cflink['linkshare1'])) {
2048 92125c97 Ermal Luçi
				$this->SetL_m1($cflink['linkshare1']);
2049 420b4538 Renato Botelho
				$this->SetL_d($cflink['linkshare2']);
2050 92125c97 Ermal Luçi
				$this->SetLinkshare();
2051 7ed9c6ac Ermal
			} else {
2052
				$this->SetL_m1("");
2053 420b4538 Renato Botelho
				$this->SetL_d("");
2054 7ed9c6ac Ermal
				$this->DisableLinkshare();
2055 92125c97 Ermal Luçi
			}
2056 f5881023 Ermal Lu?i
			if (!empty($cflink['linkshare3'])) {
2057 420b4538 Renato Botelho
				$this->SetL_m2($cflink['linkshare3']);
2058 92125c97 Ermal Luçi
				$this->SetLinkshare();
2059
			}
2060 61e047a5 Phil Davis
		} else {
2061 f5881023 Ermal Lu?i
			$this->DisableLinkshare();
2062 61e047a5 Phil Davis
		}
2063 f5881023 Ermal Lu?i
		if (!empty($cflink['realtime'])) {
2064 420b4538 Renato Botelho
			if (!empty($cflink['realtime1'])) {
2065
				$this->SetR_m1($cflink['realtime1']);
2066
				$this->SetR_d($cflink['realtime2']);
2067 92125c97 Ermal Luçi
				$this->SetRealtime();
2068 7ed9c6ac Ermal
			} else {
2069 420b4538 Renato Botelho
				$this->SetR_m1("");
2070
				$this->SetR_d("");
2071 7ed9c6ac Ermal
				$this->DisableRealtime();
2072 92125c97 Ermal Luçi
			}
2073 420b4538 Renato Botelho
			if (!empty($cflink['realtime3'])) {
2074
				$this->SetR_m2($cflink['realtime3']);
2075 92125c97 Ermal Luçi
				$this->SetRealtime();
2076
			}
2077 61e047a5 Phil Davis
		} else {
2078 420b4538 Renato Botelho
			$this->DisableRealtime();
2079 61e047a5 Phil Davis
		}
2080 f5881023 Ermal Lu?i
		if (!empty($cflink['upperlimit'])) {
2081 420b4538 Renato Botelho
			if (!empty($cflink['upperlimit1'])) {
2082
				$this->SetU_m1($cflink['upperlimit1']);
2083
				$this->SetU_d($cflink['upperlimit2']);
2084 92125c97 Ermal Luçi
				$this->SetUpperlimit();
2085 7ed9c6ac Ermal
			} else {
2086 420b4538 Renato Botelho
				$this->SetU_m1("");
2087
				$this->SetU_d("");
2088 7ed9c6ac Ermal
				$this->DisableUpperlimit();
2089 92125c97 Ermal Luçi
			}
2090 420b4538 Renato Botelho
			if (!empty($cflink['upperlimit3'])) {
2091
				$this->SetU_m2($cflink['upperlimit3']);
2092 92125c97 Ermal Luçi
				$this->SetUpperlimit();
2093
			}
2094 61e047a5 Phil Davis
		} else {
2095 f5881023 Ermal Lu?i
			$this->DisableUpperlimit();
2096 61e047a5 Phil Davis
		}
2097 92125c97 Ermal Luçi
		parent::ReadConfig($cflink);
2098 f5881023 Ermal Lu?i
	}
2099 197bfe96 Ermal Luçi
2100
	function build_tree() {
2101 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2102 197bfe96 Ermal Luçi
		$tree .= "\" ";
2103 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetDefault();
2104 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2105 f5881023 Ermal Lu?i
			$tree .= " class=\"navlnk\"";
2106 61e047a5 Phil Davis
		}
2107 197bfe96 Ermal Luçi
		$tree .= " >" . $this->GetQname() . "</a>";
2108
		if (is_array($this->subqueues)) {
2109
			$tree .= "<ul>";
2110 4de8f7ba Phil Davis
			foreach ($this->subqueues as $q) {
2111 197bfe96 Ermal Luçi
				$tree .= $q->build_tree();
2112 420b4538 Renato Botelho
			}
2113 197bfe96 Ermal Luçi
			$tree .= "</ul>";
2114
		}
2115
		$tree .= "</li>";
2116
		return $tree;
2117 061f78b1 Bill Marquette
	}
2118
2119 f5881023 Ermal Lu?i
	/* Even this should take children into consideration */
2120 9d0b0635 Ermal
	function build_rules(&$default = false) {
2121 197bfe96 Ermal Luçi
2122 f5881023 Ermal Lu?i
		$pfq_rule = " queue ". $this->qname;
2123 61e047a5 Phil Davis
		if ($this->GetInterface()) {
2124 f5881023 Ermal Lu?i
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2125 61e047a5 Phil Davis
		}
2126
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2127 f5881023 Ermal Lu?i
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2128 61e047a5 Phil Davis
		}
2129 ce0117f7 Colin Fleming
2130 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetQlimit();
2131 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2132 f5881023 Ermal Lu?i
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2133 61e047a5 Phil Davis
		}
2134 8edaa92c Ermal
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2135 f5881023 Ermal Lu?i
			$pfq_rule .= " hfsc ( ";
2136
			$tmpvalue = $this->GetRed();
2137
			if (!empty($tmpvalue)) {
2138
				$comma = 1;
2139
				$pfq_rule .= " red ";
2140
			}
2141 ce0117f7 Colin Fleming
2142 f5881023 Ermal Lu?i
			$tmpvalue = $this->GetRio();
2143
			if (!empty($tmpvalue)) {
2144 61e047a5 Phil Davis
				if ($comma) {
2145 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2146 61e047a5 Phil Davis
				}
2147 f5881023 Ermal Lu?i
				$comma = 1;
2148
				$pfq_rule .= " rio ";
2149
			}
2150
			$tmpvalue = $this->GetEcn();
2151
			if (!empty($tmpvalue)) {
2152 61e047a5 Phil Davis
				if ($comma) {
2153 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2154 61e047a5 Phil Davis
				}
2155 f5881023 Ermal Lu?i
				$comma = 1;
2156
				$pfq_rule .= " ecn ";
2157
			}
2158 8edaa92c Ermal
			$tmpvalue = $this->GetCodel();
2159
			if (!empty($tmpvalue)) {
2160 61e047a5 Phil Davis
				if ($comma) {
2161 8edaa92c Ermal
					$pfq_rule .= " ,";
2162 61e047a5 Phil Davis
				}
2163 8edaa92c Ermal
				$comma = 1;
2164
				$pfq_rule .= " codel ";
2165
			}
2166 f5881023 Ermal Lu?i
			$tmpvalue = $this->GetDefault();
2167
			if (!empty($tmpvalue)) {
2168 61e047a5 Phil Davis
				if ($comma) {
2169 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2170 61e047a5 Phil Davis
				}
2171 f5881023 Ermal Lu?i
				$comma = 1;
2172
				$pfq_rule .= " default ";
2173 ef8fca71 Ermal
				$default = true;
2174 f5881023 Ermal Lu?i
			}
2175
2176 4de8f7ba Phil Davis
			if ($this->GetRealtime() <> "") {
2177 61e047a5 Phil Davis
				if ($comma) {
2178 f5881023 Ermal Lu?i
					$pfq_rule .= " , ";
2179 61e047a5 Phil Davis
				}
2180 4de8f7ba Phil Davis
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2181 f5881023 Ermal Lu?i
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2182 4de8f7ba Phil Davis
				} else if ($this->GetR_m2() <> "") {
2183 92125c97 Ermal Luçi
					$pfq_rule .= " realtime " . $this->GetR_m2();
2184 61e047a5 Phil Davis
				}
2185 197bfe96 Ermal Luçi
				$comma = 1;
2186
			}
2187 f5881023 Ermal Lu?i
			if ($this->GetLinkshare() <> "") {
2188 61e047a5 Phil Davis
				if ($comma) {
2189 197bfe96 Ermal Luçi
					$pfq_rule .= " ,";
2190 61e047a5 Phil Davis
				}
2191
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2192 f5881023 Ermal Lu?i
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2193 61e047a5 Phil Davis
				} else if ($this->GetL_m2() <> "") {
2194 92125c97 Ermal Luçi
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2195 61e047a5 Phil Davis
				}
2196 197bfe96 Ermal Luçi
				$comma = 1;
2197
			}
2198 f5881023 Ermal Lu?i
			if ($this->GetUpperlimit() <> "") {
2199 61e047a5 Phil Davis
				if ($comma) {
2200 197bfe96 Ermal Luçi
					$pfq_rule .= " ,";
2201 61e047a5 Phil Davis
				}
2202
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2203 92125c97 Ermal Luçi
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2204 61e047a5 Phil Davis
				} else if ($this->GetU_m2() <> "") {
2205 92125c97 Ermal Luçi
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2206 61e047a5 Phil Davis
				}
2207 197bfe96 Ermal Luçi
			}
2208 f5881023 Ermal Lu?i
			$pfq_rule .= " ) ";
2209
		}
2210
		if (count($this->subqueues)) {
2211
			$i = count($this->subqueues);
2212
			$pfq_rule .= " { ";
2213
			foreach ($this->subqueues as $qkey => $qnone) {
2214
				if ($i > 1) {
2215
					$i--;
2216
					$pfq_rule .= " {$qkey}, ";
2217 61e047a5 Phil Davis
				} else {
2218 f5881023 Ermal Lu?i
					$pfq_rule .= " {$qkey} ";
2219 61e047a5 Phil Davis
				}
2220 f5881023 Ermal Lu?i
			}
2221
			$pfq_rule .= " } \n";
2222 61e047a5 Phil Davis
			foreach ($this->subqueues as $q) {
2223 ea51e9f8 Renato Botelho
				$pfq_rule .= $q->build_rules($default);
2224 61e047a5 Phil Davis
			}
2225 92125c97 Ermal Luçi
		}
2226 197bfe96 Ermal Luçi
2227 420b4538 Renato Botelho
		$pfq_rule .= " \n";
2228 ce0117f7 Colin Fleming
2229 f5881023 Ermal Lu?i
		return $pfq_rule;
2230
	}
2231
2232 197bfe96 Ermal Luçi
	function build_javascript() {
2233 806942d0 Stephen Beaver
2234 9ddd492c Stephen Beaver
		$javascript = <<<EOJS
2235
<script type="text/javascript">
2236 806942d0 Stephen Beaver
//<![CDATA[
2237 9ddd492c Stephen Beaver
	events.push(function(){
2238 806942d0 Stephen Beaver
2239
		// Disables the specified input element
2240
		function disableInput(id, disable) {
2241
			$('#' + id).prop("disabled", disable);
2242
		}
2243
2244
		// Upperlimit
2245
		function enable_upperlimit() {
2246
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2247
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2248
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2249
		}
2250
2251
		$('#upperlimit').click(function () {
2252
			enable_upperlimit();
2253
		});
2254
2255
		enable_upperlimit();
2256
2257
		// realtime
2258
		function enable_realtime() {
2259
			disableInput('realtime1', !$('#realtime').prop('checked'));
2260
			disableInput('realtime2', !$('#realtime').prop('checked'));
2261
			disableInput('realtime3', !$('#realtime').prop('checked'));
2262
		}
2263
2264
		$('#realtime').click(function () {
2265
			enable_realtime();
2266
		});
2267
2268
		enable_realtime();
2269
2270
		// linkshare
2271
		function enable_linkshare() {
2272
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2273
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2274
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2275
		}
2276
2277
		$('#linkshare').click(function () {
2278
			enable_linkshare();
2279
		});
2280
2281
		enable_linkshare();
2282 9ddd492c Stephen Beaver
	});
2283 806942d0 Stephen Beaver
//]]>
2284
</script>
2285 9ddd492c Stephen Beaver
EOJS;
2286 92125c97 Ermal Luçi
2287 197bfe96 Ermal Luçi
		return $javascript;
2288
	}
2289 061f78b1 Bill Marquette
2290 f5881023 Ermal Lu?i
	function build_form() {
2291 806942d0 Stephen Beaver
2292 aef9d8fe Stephen Beaver
		$sform = parent::build_form();
2293 806942d0 Stephen Beaver
2294 aef9d8fe Stephen Beaver
		$section = new Form_Section('Service Curve (sc)');
2295
2296
		$group = new Form_Group('Bandwidth');
2297 806942d0 Stephen Beaver
2298 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2299
			'bandwidth',
2300
			null,
2301
			'number',
2302 7c9f724c Steve Beaver
			$this->GetBandwidth(),
2303
			['step' => 'any', 'min' => '0.000']
2304 aef9d8fe Stephen Beaver
		));
2305 806942d0 Stephen Beaver
2306 aef9d8fe Stephen Beaver
		$group->add(new Form_Select(
2307
			'bandwidthtype',
2308
			null,
2309
			$this->GetBwscale(),
2310 bfc94df0 Phil Davis
			array('Kb' => 'Kbit/s',
2311
				  'Mb' => 'Mbit/s',
2312
				  'Gb' => 'Gbit/s',
2313
				  'b' => 'Bit/s',
2314 820519e5 Stephen Beaver
				  '%' => '%')
2315 aef9d8fe Stephen Beaver
		));
2316 806942d0 Stephen Beaver
2317 aef9d8fe Stephen Beaver
		$group->setHelp('Choose the amount of bandwidth for this queue');
2318 806942d0 Stephen Beaver
2319 aef9d8fe Stephen Beaver
		$section->add($group);
2320 806942d0 Stephen Beaver
2321 aef9d8fe Stephen Beaver
		$group = new Form_Group('Max bandwidth for queue.');
2322 806942d0 Stephen Beaver
2323 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
2324
			'upperlimit',
2325
			null,
2326
			'Upper Limit',
2327
			($this->GetUpperlimit()<> "")
2328 806942d0 Stephen Beaver
		));
2329
2330 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2331
			'upperlimit1',
2332
			null,
2333
			'text',
2334
			$this->GetU_m1()
2335 f7b9f8ee Stephen Beaver
		))->setHelp('m1');
2336 806942d0 Stephen Beaver
2337 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2338
			'upperlimit2',
2339
			null,
2340
			'text',
2341
			$this->GetU_d()
2342 f7b9f8ee Stephen Beaver
		))->setHelp('d');
2343 b37b4034 Phil Davis
2344 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2345
			'upperlimit3',
2346
			null,
2347
			'text',
2348
			$this->GetU_m2()
2349 f7b9f8ee Stephen Beaver
		))->setHelp('m2');
2350 806942d0 Stephen Beaver
2351
2352
		$section->add($group);
2353
2354 aef9d8fe Stephen Beaver
		$group = new Form_Group('Min bandwidth for queue.');
2355 806942d0 Stephen Beaver
2356 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
2357
			'realtime',
2358
			null,
2359 9ddd492c Stephen Beaver
			'Real Time',
2360 aef9d8fe Stephen Beaver
			($this->GetRealtime()<> "")
2361 806942d0 Stephen Beaver
		));
2362
2363 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2364
			'realtime1',
2365
			null,
2366
			'text',
2367
			$this->GetR_m1()
2368 f7b9f8ee Stephen Beaver
		))->setHelp('m1');
2369 806942d0 Stephen Beaver
2370 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2371
			'realtime2',
2372
			null,
2373
			'text',
2374
			$this->GetR_d()
2375 f7b9f8ee Stephen Beaver
		))->setHelp('d');
2376 b37b4034 Phil Davis
2377 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2378
			'realtime3',
2379
			null,
2380
			'text',
2381
			$this->GetR_m2()
2382 f7b9f8ee Stephen Beaver
		))->setHelp('m2');
2383 806942d0 Stephen Beaver
2384 aef9d8fe Stephen Beaver
		$section->add($group);
2385 806942d0 Stephen Beaver
2386 aef9d8fe Stephen Beaver
		$group = new Form_Group('B/W share of a backlogged queue.');
2387 806942d0 Stephen Beaver
2388 aef9d8fe Stephen Beaver
		$group->add(new Form_Checkbox(
2389
			'linkshare',
2390
			null,
2391
			'Link Share',
2392
			($this->GetLinkshare()<> "")
2393 806942d0 Stephen Beaver
		));
2394
2395 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2396
			'linkshare1',
2397
			null,
2398
			'text',
2399
			$this->GetL_m1()
2400 f7b9f8ee Stephen Beaver
		))->setHelp('m1');
2401 806942d0 Stephen Beaver
2402 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2403
			'linkshare2',
2404
			null,
2405
			'text',
2406
			$this->GetL_d()
2407 f7b9f8ee Stephen Beaver
		))->setHelp('d');
2408 b37b4034 Phil Davis
2409 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2410
			'linkshare3',
2411
			null,
2412
			'text',
2413
			$this->GetL_m2()
2414 f7b9f8ee Stephen Beaver
		))->setHelp('m2');
2415 806942d0 Stephen Beaver
2416 aef9d8fe Stephen Beaver
		$group->sethelp('Bandwidth share overrides priority.' . '<br />' .
2417
						'The format for service curve specifications is (m1, d, m2). m2 controls the bandwidth assigned to the queue. ' .
2418
						'm1 and d are optional and can be used to control the initial bandwidth assignment. ' .
2419
						'For the first d milliseconds the queue gets the bandwidth given as m1, afterwards the value given in m2.');
2420 806942d0 Stephen Beaver
2421 aef9d8fe Stephen Beaver
		$section->add($group);
2422 806942d0 Stephen Beaver
2423 aef9d8fe Stephen Beaver
		$sform->add($section);
2424 806942d0 Stephen Beaver
2425
		return($sform);
2426 f5881023 Ermal Lu?i
	}
2427 197bfe96 Ermal Luçi
2428 420b4538 Renato Botelho
	function update_altq_queue_data(&$data) {
2429 197bfe96 Ermal Luçi
		$this->ReadConfig($data);
2430 061f78b1 Bill Marquette
	}
2431
2432 92125c97 Ermal Luçi
	function wconfig() {
2433 197bfe96 Ermal Luçi
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2434 61e047a5 Phil Davis
		if (!is_array($cflink)) {
2435 92125c97 Ermal Luçi
			$cflink = array();
2436 61e047a5 Phil Davis
		}
2437 f5881023 Ermal Lu?i
		$cflink['name'] = $this->GetQname();
2438
		$cflink['interface'] = $this->GetInterface();
2439
		$cflink['qlimit'] = trim($this->GetQlimit());
2440 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
2441 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
2442 61e047a5 Phil Davis
		}
2443 f5881023 Ermal Lu?i
		$cflink['priority'] = $this->GetQpriority();
2444 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
2445 f5881023 Ermal Lu?i
			unset($cflink['priority']);
2446 61e047a5 Phil Davis
		}
2447 f5881023 Ermal Lu?i
		$cflink['description'] = $this->GetDescription();
2448 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
2449 f5881023 Ermal Lu?i
			unset($cflink['description']);
2450 61e047a5 Phil Davis
		}
2451 f5881023 Ermal Lu?i
		$cflink['bandwidth'] = $this->GetBandwidth();
2452
		$cflink['bandwidthtype'] = $this->GetBwscale();
2453
		$cflink['enabled'] = $this->GetEnabled();
2454 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
2455 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
2456 61e047a5 Phil Davis
		}
2457 f5881023 Ermal Lu?i
		$cflink['default'] = $this->GetDefault();
2458 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
2459 f5881023 Ermal Lu?i
			unset($cflink['default']);
2460 61e047a5 Phil Davis
		}
2461 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
2462 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
2463 f5881023 Ermal Lu?i
			unset($cflink['red']);
2464 61e047a5 Phil Davis
		}
2465 f5881023 Ermal Lu?i
		$cflink['rio'] = $this->GetRio();
2466 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
2467 f5881023 Ermal Lu?i
			unset($cflink['rio']);
2468 61e047a5 Phil Davis
		}
2469 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
2470 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
2471 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
2472 61e047a5 Phil Davis
		}
2473 9f6919e6 Richard Connon
		$cflink['codel'] = trim($this->GetCodel());
2474 61e047a5 Phil Davis
		if (empty($cflink['codel'])) {
2475 9f6919e6 Richard Connon
			unset($cflink['codel']);
2476 61e047a5 Phil Davis
		}
2477 f5881023 Ermal Lu?i
		if ($this->GetLinkshare() <> "") {
2478
			if ($this->GetL_m1() <> "") {
2479
				$cflink['linkshare1'] = $this->GetL_m1();
2480
				$cflink['linkshare2'] = $this->GetL_d();
2481
				$cflink['linkshare'] = "on";
2482
			} else {
2483
				unset($cflink['linkshare']);
2484
				unset($cflink['linkshare1']);
2485
				unset($cflink['linkshare2']);
2486
			}
2487
			if ($this->GetL_m2() <> "") {
2488
				$cflink['linkshare3'] = $this->GetL_m2();
2489
				$cflink['linkshare'] = "on";
2490
			} else {
2491
				unset($cflink['linkshare']);
2492
				unset($cflink['linkshare3']);
2493
			}
2494
		} else {
2495
			unset($cflink['linkshare']);
2496
			unset($cflink['linkshare1']);
2497
			unset($cflink['linkshare2']);
2498
			unset($cflink['linkshare3']);
2499
		}
2500
		if ($this->GetRealtime() <> "") {
2501
			if ($this->GetR_m1() <> "") {
2502
				$cflink['realtime1'] = $this->GetR_m1();
2503
				$cflink['realtime2'] = $this->GetR_d();
2504
				$cflink['realtime'] = "on";
2505
			} else {
2506
				unset($cflink['realtime']);
2507
				unset($cflink['realtime1']);
2508
				unset($cflink['realtime2']);
2509
			}
2510
			if ($this->GetR_m2() <> "") {
2511
				$cflink['realtime3'] = $this->GetR_m2();
2512
				$cflink['realtime'] = "on";
2513
			} else {
2514
				unset($cflink['realtime']);
2515
				unset($cflink['realtime3']);
2516
			}
2517
		} else {
2518
			unset($cflink['realtime']);
2519
			unset($cflink['realtime1']);
2520
			unset($cflink['realtime2']);
2521
			unset($cflink['realtime3']);
2522
		}
2523
		if ($this->GetUpperlimit() <> "") {
2524
			if ($this->GetU_m1() <> "") {
2525
				$cflink['upperlimit1'] = $this->GetU_m1();
2526
				$cflink['upperlimit2'] = $this->GetU_d();
2527
				$cflink['upperlimit'] = "on";
2528
			} else {
2529
				unset($cflink['upperlimit']);
2530
				unset($cflink['upperlimit1']);
2531
				unset($cflink['upperlimit2']);
2532
			}
2533
			if ($this->GetU_m2() <> "") {
2534
				$cflink['upperlimit3'] = $this->GetU_m2();
2535
				$cflink['upperlimit'] = "on";
2536
			} else {
2537
				unset($cflink['upperlimit']);
2538
				unset($cflink['upperlimit3']);
2539
			}
2540
		} else {
2541
			unset($cflink['upperlimit']);
2542
			unset($cflink['upperlimit1']);
2543
			unset($cflink['upperlimit2']);
2544
			unset($cflink['upperlimit3']);
2545 197bfe96 Ermal Luçi
		}
2546
	}
2547 f5881023 Ermal Lu?i
}
2548 061f78b1 Bill Marquette
2549 197bfe96 Ermal Luçi
class cbq_queue extends priq_queue {
2550 f5881023 Ermal Lu?i
	var $qborrow = "";
2551 061f78b1 Bill Marquette
2552 197bfe96 Ermal Luçi
	function GetBorrow() {
2553
		return $this->qborrow;
2554 061f78b1 Bill Marquette
	}
2555 197bfe96 Ermal Luçi
	function SetBorrow($borrow) {
2556
		$this->qborrow = $borrow;
2557
	}
2558 70b139a3 Chris Buechler
	function CanHaveChildren() {
2559 f5881023 Ermal Lu?i
		return true;
2560 92125c97 Ermal Luçi
	}
2561 197bfe96 Ermal Luçi
2562 92125c97 Ermal Luçi
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2563 197bfe96 Ermal Luçi
2564 61e047a5 Phil Davis
		if (!is_array($this->subqueues)) {
2565 f5881023 Ermal Lu?i
			$this->subqueues = array();
2566 61e047a5 Phil Davis
		}
2567 23ca5695 Chris Buechler
		$q =& new cbq_queue();
2568 197bfe96 Ermal Luçi
		$q->SetInterface($this->GetInterface());
2569 ea51e9f8 Renato Botelho
		$q->SetParent($this);
2570 92125c97 Ermal Luçi
		$q->ReadConfig($qname);
2571 420b4538 Renato Botelho
		$q->validate_input($qname, $input_errors);
2572 40de74f5 Ermal Luçi
2573 197bfe96 Ermal Luçi
		$q->SetEnabled("on");
2574 92125c97 Ermal Luçi
		$q->SetLink($path);
2575
		$this->subqueues[$q->GetQName()] = &$q;
2576 197bfe96 Ermal Luçi
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2577
		if (is_array($qname['queue'])) {
2578 f5881023 Ermal Lu?i
			foreach ($qname['queue'] as $key1 => $que) {
2579
				array_push($path, $key1);
2580 ea51e9f8 Renato Botelho
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2581 f5881023 Ermal Lu?i
				array_pop($path);
2582
			}
2583 92125c97 Ermal Luçi
		}
2584 197bfe96 Ermal Luçi
2585
		return $q;
2586 f5881023 Ermal Lu?i
	}
2587 a843b04f Ermal Luçi
2588 f5881023 Ermal Lu?i
	function copy_queue($interface, &$cflink) {
2589 a843b04f Ermal Luçi
2590 f5881023 Ermal Lu?i
		$cflink['interface'] = $interface;
2591
		$cflink['qlimit'] = trim($this->GetQlimit());
2592 61e047a5 Phil Davis
		if (empty($clink['qlimit'])) {
2593 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
2594 61e047a5 Phil Davis
		}
2595 f5881023 Ermal Lu?i
		$cflink['priority'] = trim($this->GetQpriority());
2596 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
2597 f5881023 Ermal Lu?i
			unset($cflink['priority']);
2598 61e047a5 Phil Davis
		}
2599 f5881023 Ermal Lu?i
		$cflink['name'] = $this->GetQname();
2600
		$cflink['description'] = trim($this->GetDescription());
2601 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
2602 f5881023 Ermal Lu?i
			unset($cflink['description']);
2603 61e047a5 Phil Davis
		}
2604 f5881023 Ermal Lu?i
		$cflink['bandwidth'] = $this->GetBandwidth();
2605
		$cflink['bandwidthtype'] = $this->GetBwscale();
2606
		$cflink['enabled'] = trim($this->GetEnabled());
2607 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
2608 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
2609 61e047a5 Phil Davis
		}
2610 f5881023 Ermal Lu?i
		$cflink['default'] = trim($this->GetDefault());
2611 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
2612 f5881023 Ermal Lu?i
			unset($cflink['default']);
2613 61e047a5 Phil Davis
		}
2614 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
2615 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
2616 f5881023 Ermal Lu?i
			unset($cflink['red']);
2617 61e047a5 Phil Davis
		}
2618 f5881023 Ermal Lu?i
		$cflink['rio'] = trim($this->GetRio());
2619 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
2620 f5881023 Ermal Lu?i
			unset($cflink['rio']);
2621 61e047a5 Phil Davis
		}
2622 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
2623 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
2624 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
2625 61e047a5 Phil Davis
		}
2626 f5881023 Ermal Lu?i
		$cflink['borrow'] = trim($this->GetBorrow());
2627 61e047a5 Phil Davis
		if (empty($cflink['borrow'])) {
2628 f5881023 Ermal Lu?i
			unset($cflink['borrow']);
2629 61e047a5 Phil Davis
		}
2630 f5881023 Ermal Lu?i
		if (is_array($this->queues)) {
2631
			$cflinkp['queue'] = array();
2632
			foreach ($this->subqueues as $q) {
2633
				$cflink['queue'][$q->GetQname()] = array();
2634 ea51e9f8 Renato Botelho
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2635 f5881023 Ermal Lu?i
			}
2636 a843b04f Ermal Luçi
		}
2637 f5881023 Ermal Lu?i
	}
2638 ce0117f7 Colin Fleming
2639 f5881023 Ermal Lu?i
	/*
2640
	 * Should search even its children
2641
	 */
2642
	function &find_queue($interface, $qname) {
2643 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
2644 f5881023 Ermal Lu?i
			return $this;
2645 61e047a5 Phil Davis
		}
2646 f5881023 Ermal Lu?i
		foreach ($this->subqueues as $q) {
2647
			$result =& $q->find_queue("", $qname);
2648 61e047a5 Phil Davis
			if ($result) {
2649 f5881023 Ermal Lu?i
				return $result;
2650 61e047a5 Phil Davis
			}
2651 92125c97 Ermal Luçi
		}
2652 f5881023 Ermal Lu?i
	}
2653 197bfe96 Ermal Luçi
2654
	function &find_parentqueue($interface, $qname) {
2655 61e047a5 Phil Davis
		if ($this->subqueues[$qname]) {
2656 f5881023 Ermal Lu?i
			return $this;
2657 61e047a5 Phil Davis
		}
2658 f5881023 Ermal Lu?i
		foreach ($this->subqueues as $q) {
2659
			$result = $q->find_parentqueue("", $qname);
2660 61e047a5 Phil Davis
			if ($result) {
2661 197bfe96 Ermal Luçi
				return $result;
2662 61e047a5 Phil Davis
			}
2663 197bfe96 Ermal Luçi
		}
2664 f5881023 Ermal Lu?i
	}
2665 197bfe96 Ermal Luçi
2666 f5881023 Ermal Lu?i
	function delete_queue() {
2667
		unref_on_altq_queue_list($this->GetQname());
2668
		cleanup_queue_from_rules($this->GetQname());
2669 45eeb038 Luiz Otavio O Souza
		foreach ($this->subqueues as $q)
2670
			$q->delete_queue();
2671 f5881023 Ermal Lu?i
		unset_object_by_reference($this->GetLink());
2672
	}
2673 ce0117f7 Colin Fleming
2674 197bfe96 Ermal Luçi
	function validate_input($data, &$input_errors) {
2675
		parent::validate_input($data, $input_errors);
2676 ce0117f7 Colin Fleming
2677 61e047a5 Phil Davis
		if ($data['priority'] > 7) {
2678 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Priority must be an integer between 1 and 7.");
2679 61e047a5 Phil Davis
		}
2680 2c072899 jim-p
		$reqdfields[] = "bandwidth";
2681 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidth");
2682 2c072899 jim-p
		$reqdfields[] = "bandwidthtype";
2683 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidthtype");
2684 061f78b1 Bill Marquette
2685 40de74f5 Ermal Luçi
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2686 92125c97 Ermal Luçi
	}
2687 f5881023 Ermal Lu?i
2688
	function ReadConfig(&$q) {
2689
		parent::ReadConfig($q);
2690 61e047a5 Phil Davis
		if (!empty($q['borrow'])) {
2691 197bfe96 Ermal Luçi
			$this->SetBorrow("on");
2692 61e047a5 Phil Davis
		} else {
2693 7ed9c6ac Ermal
			$this->SetBorrow("");
2694 61e047a5 Phil Davis
		}
2695 f5881023 Ermal Lu?i
	}
2696 ce0117f7 Colin Fleming
2697 197bfe96 Ermal Luçi
	function build_javascript() {
2698
		return parent::build_javascript();
2699
	}
2700 061f78b1 Bill Marquette
2701 197bfe96 Ermal Luçi
	function build_tree() {
2702 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2703 f5881023 Ermal Lu?i
		$tree .= "\" ";
2704
		$tmpvalue = trim($this->GetDefault());
2705 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2706 f5881023 Ermal Lu?i
			$tree .= " class=\"navlnk\"";
2707 61e047a5 Phil Davis
		}
2708 f5881023 Ermal Lu?i
		$tree .= " >" . $this->GetQname() . "</a>";
2709
		if (is_array($this->subqueues)) {
2710
			$tree .= "<ul>";
2711 4de8f7ba Phil Davis
			foreach ($this->subqueues as $q) {
2712 f5881023 Ermal Lu?i
				$tree .= $q->build_tree();
2713 420b4538 Renato Botelho
			}
2714 f5881023 Ermal Lu?i
			$tree .= "</ul>";
2715
		}
2716
		$tree .= "</li>";
2717
		return $tree;
2718 197bfe96 Ermal Luçi
	}
2719 ce0117f7 Colin Fleming
2720 70b139a3 Chris Buechler
	/* Even this should take children into consideration */
2721 9d0b0635 Ermal
	function build_rules(&$default = false) {
2722 f5881023 Ermal Lu?i
		$pfq_rule = "queue ". $this->qname;
2723 61e047a5 Phil Davis
		if ($this->GetInterface()) {
2724 f5881023 Ermal Lu?i
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2725 61e047a5 Phil Davis
		}
2726
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2727 f5881023 Ermal Lu?i
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2728 61e047a5 Phil Davis
		}
2729 f5881023 Ermal Lu?i
		$tmpvalue = $this->GetQpriority();
2730 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2731 f5881023 Ermal Lu?i
			$pfq_rule .= " priority " . $this->GetQpriority();
2732 61e047a5 Phil Davis
		}
2733 f5881023 Ermal Lu?i
		$tmpvalue = trim($this->GetQlimit());
2734 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2735 f5881023 Ermal Lu?i
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2736 61e047a5 Phil Davis
		}
2737 8edaa92c Ermal
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
2738 f5881023 Ermal Lu?i
			$pfq_rule .= " cbq ( ";
2739
			$tmpvalue = trim($this->GetRed());
2740
			if (!empty($tmpvalue)) {
2741
				$comma = 1;
2742
				$pfq_rule .= " red ";
2743
			}
2744 8edaa92c Ermal
			$tmpvalue = trim($this->GetCodel());
2745
			if (!empty($tmpvalue)) {
2746
				$comma = 1;
2747
				$pfq_rule .= " codel ";
2748
			}
2749 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetRio());
2750
			if (!empty($tmpvalue)) {
2751 61e047a5 Phil Davis
				if ($comma) {
2752 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2753 61e047a5 Phil Davis
				}
2754 f5881023 Ermal Lu?i
				$comma = 1;
2755
				$pfq_rule .= " rio ";
2756
			}
2757
			$tmpvalue = trim($this->GetEcn());
2758
			if (!empty($tmpvalue)) {
2759 61e047a5 Phil Davis
				if ($comma) {
2760 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2761 61e047a5 Phil Davis
				}
2762 f5881023 Ermal Lu?i
				$comma = 1;
2763
				$pfq_rule .= " ecn ";
2764
			}
2765
			$tmpvalue = trim($this->GetDefault());
2766
			if (!empty($tmpvalue)) {
2767 61e047a5 Phil Davis
				if ($comma) {
2768 f5881023 Ermal Lu?i
					$pfq_rule .= " ,";
2769 61e047a5 Phil Davis
				}
2770 f5881023 Ermal Lu?i
				$comma = 1;
2771
				$pfq_rule .= " default ";
2772 ef8fca71 Ermal
				$default = true;
2773 f5881023 Ermal Lu?i
			}
2774
			$tmpvalue = trim($this->GetBorrow());
2775
			if (!empty($tmpvalue)) {
2776 61e047a5 Phil Davis
				if ($comma) {
2777 f5881023 Ermal Lu?i
					$pfq_rule .= ", ";
2778 61e047a5 Phil Davis
				}
2779 f5881023 Ermal Lu?i
				$pfq_rule .= " borrow ";
2780 92125c97 Ermal Luçi
			}
2781 f5881023 Ermal Lu?i
			$pfq_rule .= " ) ";
2782 420b4538 Renato Botelho
		}
2783 f5881023 Ermal Lu?i
		if (count($this->subqueues)) {
2784
			$i = count($this->subqueues);
2785
			$pfq_rule .= " { ";
2786
			foreach ($this->subqueues as $qkey => $qnone) {
2787
				if ($i > 1) {
2788
					$i--;
2789
					$pfq_rule .= " {$qkey}, ";
2790 61e047a5 Phil Davis
				} else {
2791 f5881023 Ermal Lu?i
					$pfq_rule .= " {$qkey} ";
2792 61e047a5 Phil Davis
				}
2793 f5881023 Ermal Lu?i
			}
2794
			$pfq_rule .= " } \n";
2795 61e047a5 Phil Davis
			foreach ($this->subqueues as $q) {
2796 9d0b0635 Ermal
				$pfq_rule .= $q->build_rules($default);
2797 61e047a5 Phil Davis
			}
2798 f5881023 Ermal Lu?i
		}
2799 92125c97 Ermal Luçi
2800 f5881023 Ermal Lu?i
		$pfq_rule .= " \n";
2801
		return $pfq_rule;
2802 92125c97 Ermal Luçi
	}
2803
2804
	function build_form() {
2805 aef9d8fe Stephen Beaver
		$sform = parent::build_form();
2806 806942d0 Stephen Beaver
2807 fe179ed6 Stephen Beaver
		$section = new Form_Section('NOTITLE');
2808 aef9d8fe Stephen Beaver
2809
		$group = new Form_Group('Bandwidth');
2810 806942d0 Stephen Beaver
2811 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
2812
			'bandwidth',
2813
			null,
2814
			'number',
2815
			$this->GetBandwidth()
2816
		));
2817 806942d0 Stephen Beaver
2818 aef9d8fe Stephen Beaver
		$group->add(new Form_Select(
2819
			'bandwidthtype',
2820
			null,
2821
			$this->GetBwscale(),
2822 bfc94df0 Phil Davis
			array('Kb' => 'Kbit/s',
2823
				  'Mb' => 'Mbit/s',
2824
				  'Gb' => 'Gbit/s',
2825
				  'b' => 'Bit/s',
2826 820519e5 Stephen Beaver
				  '%' => '%')
2827 aef9d8fe Stephen Beaver
		));
2828 806942d0 Stephen Beaver
2829 aef9d8fe Stephen Beaver
		$group->setHelp('Choose the amount of bandwidth for this queue');
2830
2831
		$section->add($group);
2832 806942d0 Stephen Beaver
2833 aef9d8fe Stephen Beaver
		$section->addInput(new Form_Checkbox(
2834
			'borrow',
2835 9ddd492c Stephen Beaver
			'Scheduler option',
2836 aef9d8fe Stephen Beaver
			'Borrow from other queues when available',
2837
			($this->GetBorrow() == "on")
2838 806942d0 Stephen Beaver
		));
2839
2840 b6bd4012 Phil Davis
		$sform->add($section);
2841
2842 aef9d8fe Stephen Beaver
		return $sform;
2843 92125c97 Ermal Luçi
	}
2844 197bfe96 Ermal Luçi
2845 420b4538 Renato Botelho
	function update_altq_queue_data(&$data) {
2846 197bfe96 Ermal Luçi
		$this->ReadConfig($data);
2847
	}
2848 061f78b1 Bill Marquette
2849 92125c97 Ermal Luçi
	function wconfig() {
2850 197bfe96 Ermal Luçi
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2851 61e047a5 Phil Davis
		if (!is_array($cflink)) {
2852 f5881023 Ermal Lu?i
			$cflink = array();
2853 61e047a5 Phil Davis
		}
2854 f5881023 Ermal Lu?i
		$cflink['interface'] = $this->GetInterface();
2855
		$cflink['qlimit'] = trim($this->GetQlimit());
2856 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
2857 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
2858 61e047a5 Phil Davis
		}
2859 f5881023 Ermal Lu?i
		$cflink['priority'] = $this->GetQpriority();
2860 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
2861 f5881023 Ermal Lu?i
			unset($cflink['priority']);
2862 61e047a5 Phil Davis
		}
2863 f5881023 Ermal Lu?i
		$cflink['name'] = $this->GetQname();
2864
		$cflink['description'] = $this->GetDescription();
2865 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
2866 f5881023 Ermal Lu?i
			unset($cflink['description']);
2867 61e047a5 Phil Davis
		}
2868 f5881023 Ermal Lu?i
		$cflink['bandwidth'] = $this->GetBandwidth();
2869
		$cflink['bandwidthtype'] = $this->GetBwscale();
2870
		$cflink['enabled'] = trim($this->GetEnabled());
2871 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
2872 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
2873 61e047a5 Phil Davis
		}
2874 f5881023 Ermal Lu?i
		$cflink['default'] = trim($this->GetDefault());
2875 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
2876 f5881023 Ermal Lu?i
			unset($cflink['default']);
2877 61e047a5 Phil Davis
		}
2878 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
2879 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
2880 f5881023 Ermal Lu?i
			unset($cflink['red']);
2881 61e047a5 Phil Davis
		}
2882 f5881023 Ermal Lu?i
		$cflink['rio'] = trim($this->GetRio());
2883 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
2884 f5881023 Ermal Lu?i
			unset($cflink['rio']);
2885 61e047a5 Phil Davis
		}
2886 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
2887 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
2888 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
2889 61e047a5 Phil Davis
		}
2890 9f6919e6 Richard Connon
		$cflink['codel'] = trim($this->GetCodel());
2891 61e047a5 Phil Davis
		if (empty($cflink['codel'])) {
2892 9f6919e6 Richard Connon
			unset($cflink['codel']);
2893 61e047a5 Phil Davis
		}
2894 f5881023 Ermal Lu?i
		$cflink['borrow'] = trim($this->GetBorrow());
2895 61e047a5 Phil Davis
		if (empty($cflink['borrow'])) {
2896 f5881023 Ermal Lu?i
			unset($cflink['borrow']);
2897 61e047a5 Phil Davis
		}
2898 f5881023 Ermal Lu?i
	}
2899 197bfe96 Ermal Luçi
}
2900 061f78b1 Bill Marquette
2901 2b5caa0e Ermal Luçi
class fairq_queue extends priq_queue {
2902 f5881023 Ermal Lu?i
	var $hogs;
2903
	var $buckets;
2904 2b5caa0e Ermal Luçi
2905
	function GetBuckets() {
2906
		return $this->buckets;
2907
	}
2908
	function SetBuckets($buckets) {
2909 ae3d3adb Ermal Luçi
		$this->buckets = $buckets;
2910 2b5caa0e Ermal Luçi
	}
2911
	function GetHogs() {
2912
		return $this->hogs;
2913
	}
2914
	function SetHogs($hogs) {
2915 ae3d3adb Ermal Luçi
		$this->hogs = $hogs;
2916 2b5caa0e Ermal Luçi
	}
2917 70b139a3 Chris Buechler
	function CanHaveChildren() {
2918 2b5caa0e Ermal Luçi
		return false;
2919
	}
2920
2921
2922
	function copy_queue($interface, &$cflink) {
2923 420b4538 Renato Botelho
		$cflink['interface'] = $interface;
2924
		$cflink['qlimit'] = $this->GetQlimit();
2925
		$cflink['priority'] = $this->GetQpriority();
2926
		$cflink['name'] = $this->GetQname();
2927
		$cflink['description'] = $this->GetDescription();
2928
		$cflink['bandwidth'] = $this->GetBandwidth();
2929
		$cflink['bandwidthtype'] = $this->GetBwscale();
2930
		$cflink['enabled'] = $this->GetEnabled();
2931
		$cflink['default'] = $this->GetDefault();
2932
		$cflink['red'] = $this->GetRed();
2933
		$cflink['rio'] = $this->GetRio();
2934
		$cflink['ecn'] = $this->GetEcn();
2935
		$cflink['buckets'] = $this->GetBuckets();
2936 8774ca6e Ermal Luçi
		$cflink['hogs'] = $this->GetHogs();
2937 2b5caa0e Ermal Luçi
	}
2938 ce0117f7 Colin Fleming
2939 2b5caa0e Ermal Luçi
	/*
2940 70b139a3 Chris Buechler
	 * Should search even its children
2941 2b5caa0e Ermal Luçi
	 */
2942
	function &find_queue($interface, $qname) {
2943 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
2944 2b5caa0e Ermal Luçi
			return $this;
2945 61e047a5 Phil Davis
		}
2946 2b5caa0e Ermal Luçi
	}
2947
2948 62150088 Ermal Lu?i
	function find_parentqueue($interface, $qname) { return; }
2949 2b5caa0e Ermal Luçi
2950
	function delete_queue() {
2951
		unref_on_altq_queue_list($this->GetQname());
2952
		cleanup_queue_from_rules($this->GetQname());
2953
		unset_object_by_reference($this->GetLink());
2954
	}
2955 ce0117f7 Colin Fleming
2956 2b5caa0e Ermal Luçi
	function validate_input($data, &$input_errors) {
2957
		parent::validate_input($data, $input_errors);
2958 ce0117f7 Colin Fleming
2959 61e047a5 Phil Davis
		if ($data['priority'] > 255) {
2960 dbaf21d4 Renato Botelho
				$input_errors[] = gettext("Priority must be an integer between 1 and 255.");
2961 61e047a5 Phil Davis
		}
2962 2c072899 jim-p
		$reqdfields[] = "bandwidth";
2963 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidth");
2964 2c072899 jim-p
		$reqdfields[] = "bandwidthtype";
2965 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidthtype");
2966 2b5caa0e Ermal Luçi
2967
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2968
	}
2969 ce0117f7 Colin Fleming
2970 2b5caa0e Ermal Luçi
	function ReadConfig(&$q) {
2971
		parent::ReadConfig($q);
2972 61e047a5 Phil Davis
		if (!empty($q['buckets'])) {
2973 2b5caa0e Ermal Luçi
			$this->SetBuckets($q['buckets']);
2974 61e047a5 Phil Davis
		} else {
2975 7ed9c6ac Ermal
			$this->SetBuckets("");
2976 61e047a5 Phil Davis
		}
2977
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
2978 2b5caa0e Ermal Luçi
			$this->SetHogs($q['hogs']);
2979 61e047a5 Phil Davis
		} else {
2980 7ed9c6ac Ermal
			$this->SetHogs("");
2981 61e047a5 Phil Davis
		}
2982 2b5caa0e Ermal Luçi
	}
2983 ce0117f7 Colin Fleming
2984 2b5caa0e Ermal Luçi
	function build_javascript() {
2985
		return parent::build_javascript();
2986
	}
2987
2988
	function build_tree() {
2989 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
2990
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2991 f5881023 Ermal Lu?i
		$tree .= "\" ";
2992
		$tmpvalue = trim($this->GetDefault());
2993 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
2994 f5881023 Ermal Lu?i
			$tree .= " class=\"navlnk\"";
2995 61e047a5 Phil Davis
		}
2996 f5881023 Ermal Lu?i
		$tree .= " >" . $this->GetQname() . "</a>";
2997
		$tree .= "</li>";
2998
		return $tree;
2999 2b5caa0e Ermal Luçi
	}
3000 ce0117f7 Colin Fleming
3001 70b139a3 Chris Buechler
	/* Even this should take children into consideration */
3002 9d0b0635 Ermal
	function build_rules(&$default = false) {
3003 2b5caa0e Ermal Luçi
		$pfq_rule = "queue ". $this->qname;
3004 61e047a5 Phil Davis
		if ($this->GetInterface()) {
3005 85a5da13 Ermal Luçi
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3006 61e047a5 Phil Davis
		}
3007
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3008 2b5caa0e Ermal Luçi
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3009 61e047a5 Phil Davis
		}
3010 f5881023 Ermal Lu?i
		$tmpvalue = trim($this->GetQpriority());
3011 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
3012 2b5caa0e Ermal Luçi
			$pfq_rule .= " priority " . $this->GetQpriority();
3013 61e047a5 Phil Davis
		}
3014 f5881023 Ermal Lu?i
		$tmpvalue = trim($this->GetQlimit());
3015 61e047a5 Phil Davis
		if (!empty($tmpvalue)) {
3016 2b5caa0e Ermal Luçi
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3017 61e047a5 Phil Davis
		}
3018
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3019 4de8f7ba Phil Davis
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3020 ae3d3adb Ermal Luçi
			$pfq_rule .= " fairq ( ";
3021 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetRed());
3022
			if (!empty($tmpvalue)) {
3023 2b5caa0e Ermal Luçi
				$comma = 1;
3024
				$pfq_rule .= " red ";
3025
			}
3026 8edaa92c Ermal
			$tmpvalue = trim($this->GetCodel());
3027
			if (!empty($tmpvalue)) {
3028
				$comma = 1;
3029
				$pfq_rule .= " codel ";
3030
			}
3031 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetRio());
3032
			if (!empty($tmpvalue)) {
3033 61e047a5 Phil Davis
				if ($comma) {
3034 2b5caa0e Ermal Luçi
					$pfq_rule .= " ,";
3035 61e047a5 Phil Davis
				}
3036 2b5caa0e Ermal Luçi
				$comma = 1;
3037
				$pfq_rule .= " rio ";
3038
			}
3039 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetEcn());
3040
			if (!empty($tmpvalue)) {
3041 61e047a5 Phil Davis
				if ($comma) {
3042 2b5caa0e Ermal Luçi
					$pfq_rule .= " ,";
3043 61e047a5 Phil Davis
				}
3044 2b5caa0e Ermal Luçi
				$comma = 1;
3045
				$pfq_rule .= " ecn ";
3046
			}
3047 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetDefault());
3048
			if (!empty($tmpvalue)) {
3049 61e047a5 Phil Davis
				if ($comma) {
3050 2b5caa0e Ermal Luçi
					$pfq_rule .= " ,";
3051 61e047a5 Phil Davis
				}
3052 2b5caa0e Ermal Luçi
				$comma = 1;
3053
				$pfq_rule .= " default ";
3054 ef8fca71 Ermal
				$default = true;
3055 2b5caa0e Ermal Luçi
			}
3056 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetBuckets());
3057
			if (!empty($tmpvalue)) {
3058 61e047a5 Phil Davis
				if ($comma) {
3059 2b5caa0e Ermal Luçi
					$pfq_rule .= ", ";
3060 61e047a5 Phil Davis
				}
3061 2b5caa0e Ermal Luçi
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3062
			}
3063 f5881023 Ermal Lu?i
			$tmpvalue = trim($this->GetHogs());
3064
			if (!empty($tmpvalue)) {
3065 61e047a5 Phil Davis
				if ($comma) {
3066 2b5caa0e Ermal Luçi
					$pfq_rule .= ", ";
3067 61e047a5 Phil Davis
				}
3068 2b5caa0e Ermal Luçi
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3069
			}
3070 61e047a5 Phil Davis
			$pfq_rule .= " ) ";
3071 420b4538 Renato Botelho
		}
3072 2b5caa0e Ermal Luçi
3073
		$pfq_rule .= " \n";
3074
		return $pfq_rule;
3075
	}
3076
3077
	function build_form() {
3078 f5e511d3 Ermal
		$form = parent::build_form();
3079 806942d0 Stephen Beaver
3080 aef9d8fe Stephen Beaver
		$section = new Form_Section('');
3081
3082
		$group = new Form_Group('Bandwidth');
3083 806942d0 Stephen Beaver
3084 aef9d8fe Stephen Beaver
		$group->add(new Form_Input(
3085
			'bandwidth',
3086
			null,
3087
			'number',
3088
			$this->GetBandwidth()
3089
		));
3090 806942d0 Stephen Beaver
3091 aef9d8fe Stephen Beaver
		$group->add(new Form_Select(
3092
			'bandwidthtype',
3093
			null,
3094
			$this->GetBwscale(),
3095 bfc94df0 Phil Davis
			array('Kb' => 'Kbit/s',
3096
				  'Mb' => 'Mbit/s',
3097
				  'Gb' => 'Gbit/s',
3098
				  'b' => 'Bit/s',
3099 820519e5 Stephen Beaver
				  '%' => '%')
3100 aef9d8fe Stephen Beaver
		));
3101 806942d0 Stephen Beaver
3102 aef9d8fe Stephen Beaver
		$group->setHelp('Choose the amount of bandwidth for this queue');
3103
3104
		$section->add($group);
3105 806942d0 Stephen Beaver
3106 43568d7d Stephen Beaver
		$section->addInput(new Form_Input(
3107
			'buckets',
3108
			'Scheduler specific options',
3109
			'text',
3110
			$this->GetBuckets()
3111
		))->setHelp('Number of buckets available');
3112 aef9d8fe Stephen Beaver
3113 4700a731 Stephen Beaver
		$section->addInput(new Form_Input(
3114 43568d7d Stephen Beaver
			'hogs',
3115
			'',
3116
			'text',
3117
			$this->GetHogs()
3118
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3119
3120
		$form->add($section);
3121 2b5caa0e Ermal Luçi
		return $form;
3122
	}
3123
3124 420b4538 Renato Botelho
	function update_altq_queue_data(&$data) {
3125 2b5caa0e Ermal Luçi
		$this->ReadConfig($data);
3126
	}
3127
3128
	function wconfig() {
3129
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3130 61e047a5 Phil Davis
		if (!is_array($cflink)) {
3131 2b5caa0e Ermal Luçi
			$cflink = array();
3132 61e047a5 Phil Davis
		}
3133 2b5caa0e Ermal Luçi
		$cflink['interface'] = $this->GetInterface();
3134 f5881023 Ermal Lu?i
		$cflink['qlimit'] = trim($this->GetQlimit());
3135 61e047a5 Phil Davis
		if (empty($cflink['qlimit'])) {
3136 f5881023 Ermal Lu?i
			unset($cflink['qlimit']);
3137 61e047a5 Phil Davis
		}
3138 f5881023 Ermal Lu?i
		$cflink['priority'] = trim($this->GetQpriority());
3139 61e047a5 Phil Davis
		if (empty($cflink['priority'])) {
3140 f5881023 Ermal Lu?i
			unset($cflink['priority']);
3141 61e047a5 Phil Davis
		}
3142 2b5caa0e Ermal Luçi
		$cflink['name'] = $this->GetQname();
3143 f5881023 Ermal Lu?i
		$cflink['description'] = trim($this->GetDescription());
3144 61e047a5 Phil Davis
		if (empty($cflink['description'])) {
3145 f5881023 Ermal Lu?i
			unset($cflink['description']);
3146 61e047a5 Phil Davis
		}
3147 2b5caa0e Ermal Luçi
		$cflink['bandwidth'] = $this->GetBandwidth();
3148
		$cflink['bandwidthtype'] = $this->GetBwscale();
3149
		$cflink['enabled'] = $this->GetEnabled();
3150 61e047a5 Phil Davis
		if (empty($cflink['enabled'])) {
3151 f5881023 Ermal Lu?i
			unset($cflink['enabled']);
3152 61e047a5 Phil Davis
		}
3153 f5881023 Ermal Lu?i
		$cflink['default'] = trim($this->GetDefault());
3154 61e047a5 Phil Davis
		if (empty($cflink['default'])) {
3155 f5881023 Ermal Lu?i
			unset($cflink['default']);
3156 61e047a5 Phil Davis
		}
3157 f5881023 Ermal Lu?i
		$cflink['red'] = trim($this->GetRed());
3158 61e047a5 Phil Davis
		if (empty($cflink['red'])) {
3159 f5881023 Ermal Lu?i
			unset($cflink['red']);
3160 61e047a5 Phil Davis
		}
3161 f5881023 Ermal Lu?i
		$cflink['rio'] = trim($this->GetRio());
3162 61e047a5 Phil Davis
		if (empty($cflink['rio'])) {
3163 f5881023 Ermal Lu?i
			unset($cflink['rio']);
3164 61e047a5 Phil Davis
		}
3165 f5881023 Ermal Lu?i
		$cflink['ecn'] = trim($this->GetEcn());
3166 61e047a5 Phil Davis
		if (empty($cflink['ecn'])) {
3167 f5881023 Ermal Lu?i
			unset($cflink['ecn']);
3168 61e047a5 Phil Davis
		}
3169 9f6919e6 Richard Connon
		$cflink['codel'] = trim($this->GetCodel());
3170 61e047a5 Phil Davis
		if (empty($cflink['codel'])) {
3171 9f6919e6 Richard Connon
			unset($cflink['codel']);
3172 61e047a5 Phil Davis
		}
3173 f5881023 Ermal Lu?i
		$cflink['buckets'] = trim($this->GetBuckets());
3174 61e047a5 Phil Davis
		if (empty($cflink['buckets'])) {
3175 f5881023 Ermal Lu?i
			unset($cflink['buckets']);
3176 61e047a5 Phil Davis
		}
3177 f5881023 Ermal Lu?i
		$cflink['hogs'] = trim($this->GetHogs());
3178 61e047a5 Phil Davis
		if (empty($cflink['hogs'])) {
3179 f5881023 Ermal Lu?i
			unset($cflink['hogs']);
3180 61e047a5 Phil Davis
		}
3181 2b5caa0e Ermal Luçi
	}
3182
}
3183 061f78b1 Bill Marquette
3184
3185 c25a6b6a Ermal Luçi
/*
3186 0dc7b4aa Ermal
 * dummynet(4) wrappers.
3187 c25a6b6a Ermal Luçi
 */
3188
3189
3190
/*
3191
 * List of respective objects!
3192
 */
3193 d62ba478 Ermal Luçi
$dummynet_pipe_list = array();
3194 c25a6b6a Ermal Luçi
3195
class dummynet_class {
3196 420b4538 Renato Botelho
	var $qname;
3197 f5881023 Ermal Lu?i
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3198 420b4538 Renato Botelho
	var $qlimit;
3199
	var $description;
3200 f5881023 Ermal Lu?i
	var $qenabled;
3201
	var $link;
3202
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3203 420b4538 Renato Botelho
	var $plr;
3204
3205
	var $buckets;
3206
	/* mask parameters */
3207
	var $mask;
3208
	var $noerror;
3209
3210
	/* Accessor functions */
3211
	function SetLink($link) {
3212
		$this->link = $link;
3213
	}
3214
	function GetLink() {
3215
		return $this->link;
3216
	}
3217 0b4e7542 Jean Cyr
	function GetMask() {
3218 61e047a5 Phil Davis
		if (!isset($this->mask["type"])) {
3219 8c50a9d8 Jean Cyr
			$this->mask["type"] = "none";
3220 61e047a5 Phil Davis
		}
3221 f5881023 Ermal Lu?i
		return $this->mask;
3222
	}
3223
	function SetMask($mask) {
3224
		$this->mask = $mask;
3225
	}
3226
	function &GetParent() {
3227
		return $this->qparent;
3228
	}
3229
	function SetParent(&$parent) {
3230
		$this->qparent = &$parent;
3231
	}
3232 420b4538 Renato Botelho
	function GetEnabled() {
3233
		return $this->qenabled;
3234
	}
3235
	function SetEnabled($value) {
3236
		$this->qenabled = $value;
3237
	}
3238 f5881023 Ermal Lu?i
	function CanHaveChildren() {
3239
		return false;
3240 420b4538 Renato Botelho
	}
3241 f5881023 Ermal Lu?i
	function CanBeDeleted() {
3242 420b4538 Renato Botelho
		return true;
3243
	}
3244
	function GetQname() {
3245
		return $this->qname;
3246
	}
3247
	function SetQname($name) {
3248
		$this->qname = trim($name);
3249
	}
3250
	function GetQlimit() {
3251
		return $this->qlimit;
3252
	}
3253
	function SetQlimit($limit) {
3254
		$this->qlimit = $limit;
3255
	}
3256
	function GetDescription() {
3257
		return $this->description;
3258
	}
3259
	function SetDescription($str) {
3260
		$this->description = trim($str);
3261
	}
3262
	function GetFirstime() {
3263
		return $this->firsttime;
3264
	}
3265
	function SetFirsttime($number) {
3266
		$this->firsttime = $number;
3267
	}
3268
	function GetBuckets() {
3269
		return $this->buckets;
3270
	}
3271
	function SetBuckets($buckets) {
3272
		$this->buckets = $buckets;
3273
	}
3274 f5881023 Ermal Lu?i
	function SetNumber($number) {
3275
		$this->qnumber = $number;
3276
	}
3277
	function GetNumber() {
3278
		return $this->qnumber;
3279
	}
3280 420b4538 Renato Botelho
	function GetPlr() {
3281
		return $this->plr;
3282
	}
3283
	function SetPlr($plr) {
3284
		$this->plr = $plr;
3285
	}
3286 c25a6b6a Ermal Luçi
3287 8c50a9d8 Jean Cyr
	function build_javascript() {
3288 2d46e1e4 Jean Cyr
		$javascript .= "<script type=\"text/javascript\">\n";
3289 8c50a9d8 Jean Cyr
		$javascript .= "//<![CDATA[\n";
3290 2d46e1e4 Jean Cyr
		$javascript .= "function enable_maskbits(enable_over) {\n";
3291
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3292
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3293 8c50a9d8 Jean Cyr
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3294
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3295 853030ac Jean Cyr
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3296
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3297 2d46e1e4 Jean Cyr
		$javascript .= "} else {\n";
3298 8c50a9d8 Jean Cyr
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3299 420b4538 Renato Botelho
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3300 2d46e1e4 Jean Cyr
		$javascript .= "}}\n";
3301 8c50a9d8 Jean Cyr
		$javascript .= "//]]>\n";
3302 2d46e1e4 Jean Cyr
		$javascript .= "</script>\n";
3303 8c50a9d8 Jean Cyr
		return $javascript;
3304
	}
3305 d62ba478 Ermal Luçi
3306 f5881023 Ermal Lu?i
	function validate_input($data, &$input_errors) {
3307
		$reqdfields[] = "bandwidth";
3308 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidth");
3309 5274102e Chris Buechler
		/*$reqdfields[] = "burst";
3310
		$reqdfieldsn[] = gettext("Burst"); */
3311 f5881023 Ermal Lu?i
		$reqdfields[] = "bandwidthtype";
3312 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Bandwidthtype");
3313 6be14e38 Ermal
		$reqdfields[] = "newname";
3314 dbaf21d4 Renato Botelho
		$reqdfieldsn[] = gettext("Name");
3315 ce0117f7 Colin Fleming
3316 f5881023 Ermal Lu?i
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3317
3318 0f0f7b2c Renato Botelho
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3319 4de8f7ba Phil Davis
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3320 3481524a Chris Buechler
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3321 61e047a5 Phil Davis
		}
3322 0f0f7b2c Renato Botelho
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3323 4de8f7ba Phil Davis
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3324
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3325 61e047a5 Phil Davis
		}
3326
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3327 420b4538 Renato Botelho
			$input_errors[] = gettext("Queue limit must be an integer");
3328 61e047a5 Phil Davis
		}
3329
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3330 152ab4d0 Vinicius Coque
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3331 61e047a5 Phil Davis
		}
3332
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3333 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3334 61e047a5 Phil Davis
		}
3335
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3336
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3337 3481524a Chris Buechler
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3338 61e047a5 Phil Davis
			}
3339
		}
3340
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3341
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3342 3481524a Chris Buechler
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3343 61e047a5 Phil Davis
			}
3344
		}
3345 2d46e1e4 Jean Cyr
	}
3346 420b4538 Renato Botelho
3347 2d46e1e4 Jean Cyr
	function build_mask_rules(&$pfq_rule) {
3348
		$mask = $this->GetMask();
3349
		if (!empty($mask['type'])) {
3350 61e047a5 Phil Davis
			if ($mask['type'] <> 'none') {
3351 2d46e1e4 Jean Cyr
				$pfq_rule .= " mask";
3352 61e047a5 Phil Davis
			}
3353 2d46e1e4 Jean Cyr
			switch ($mask['type']) {
3354 61e047a5 Phil Davis
				case 'srcaddress':
3355
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3356
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3357
					} else {
3358
						$pfq_rule .= " src-ip6 /128";
3359
					}
3360
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3361
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3362
					} else {
3363
						$pfq_rule .= " src-ip 0xffffffff";
3364
					}
3365
					break;
3366
				case 'dstaddress':
3367
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3368
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3369
					} else {
3370
						$pfq_rule .= " dst-ip6 /128";
3371
					}
3372
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3373
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3374
					} else {
3375
						$pfq_rule .= " dst-ip 0xffffffff";
3376
					}
3377
					break;
3378
				default:
3379
					break;
3380 2d46e1e4 Jean Cyr
			}
3381 420b4538 Renato Botelho
		}
3382 f5881023 Ermal Lu?i
	}
3383 420b4538 Renato Botelho
3384 c25a6b6a Ermal Luçi
}
3385
3386
class dnpipe_class extends dummynet_class {
3387 420b4538 Renato Botelho
	var $delay;
3388 c9ba2f8a Ermal
	var $qbandwidth = array();
3389 d62ba478 Ermal Luçi
	var $qbandwidthtype;
3390 c25a6b6a Ermal Luçi
3391
		/* This is here to help on form building and building rules/lists */
3392 420b4538 Renato Botelho
	var $subqueues = array();
3393 c25a6b6a Ermal Luçi
3394 70b139a3 Chris Buechler
	function CanHaveChildren() {
3395 420b4538 Renato Botelho
		return true;
3396
	}
3397 d62ba478 Ermal Luçi
	function SetDelay($delay) {
3398
		$this->delay = $delay;
3399
	}
3400
	function GetDelay() {
3401
		return $this->delay;
3402
	}
3403
	function delete_queue() {
3404
		cleanup_dnqueue_from_rules($this->GetQname());
3405 61e047a5 Phil Davis
		foreach ($this->subqueues as $q) {
3406 d62ba478 Ermal Luçi
			$q->delete_queue();
3407 61e047a5 Phil Davis
		}
3408 d62ba478 Ermal Luçi
		unset_dn_object_by_reference($this->GetLink());
3409 f2c34876 Ermal
		@pfSense_pipe_action("pipe delete " . $this->GetNumber());
3410 420b4538 Renato Botelho
	}
3411
	function GetBandwidth() {
3412
		return $this->qbandwidth;
3413
	}
3414
	function SetBandwidth($bandwidth) {
3415
		$this->qbandwidth = $bandwidth;
3416
	}
3417 61e047a5 Phil Davis
	function GetBurst() {
3418
		return $this->qburst;
3419
	}
3420
	function SetBurst($burst) {
3421
		$this->qburst = $burst;
3422
	}
3423 c25a6b6a Ermal Luçi
3424 d62ba478 Ermal Luçi
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3425 c25a6b6a Ermal Luçi
3426 61e047a5 Phil Davis
		if (!is_array($this->subqueues)) {
3427 d62ba478 Ermal Luçi
			$this->subqueues = array();
3428 61e047a5 Phil Davis
		}
3429 ce0117f7 Colin Fleming
3430 23ca5695 Chris Buechler
		$q =& new dnqueue_class();
3431 d62ba478 Ermal Luçi
		$q->SetLink($path);
3432
		$q->SetEnabled("on");
3433
		$q->SetPipe($this->GetQname());
3434 ea51e9f8 Renato Botelho
		$q->SetParent($this);
3435 d62ba478 Ermal Luçi
		$q->ReadConfig($queue);
3436
		$q->validate_input($queue, $input_errors);
3437 00ca3fb1 Ermal
		if (count($input_errors)) {
3438 885af75c NOYB
			log_error(sprintf(gettext('SHAPER: Could not create queue %1$s on interface %2$s because: %3$s'), $q->GetQname(), $interface, print_r($input_errors, true)));
3439 d62ba478 Ermal Luçi
			return $q;
3440 00ca3fb1 Ermal
		}
3441 85a236e9 Ermal
		$number = dnqueue_find_nextnumber();
3442
		$q->SetNumber($number);
3443 d62ba478 Ermal Luçi
		$this->subqueues[$q->GetQname()] = &$q;
3444 420b4538 Renato Botelho
3445 d62ba478 Ermal Luçi
		return $q;
3446
	}
3447 c25a6b6a Ermal Luçi
3448 ea51e9f8 Renato Botelho
	function &get_queue_list(&$q = null) {
3449 d62ba478 Ermal Luçi
		$qlist = array();
3450 c25a6b6a Ermal Luçi
3451 64c7753b Ermal Luçi
		$qlist[$this->GetQname()] = $this->GetNumber();
3452 d62ba478 Ermal Luçi
		if (is_array($this->subqueues)) {
3453 61e047a5 Phil Davis
			foreach ($this->subqueues as $queue) {
3454 ea51e9f8 Renato Botelho
				$queue->get_queue_list($qlist);
3455 61e047a5 Phil Davis
			}
3456 c25a6b6a Ermal Luçi
		}
3457 d62ba478 Ermal Luçi
		return $qlist;
3458
	}
3459 ce0117f7 Colin Fleming
3460 420b4538 Renato Botelho
	/*
3461
	 * Should search even its children
3462
	 */
3463
	function &find_queue($pipe, $qname) {
3464 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
3465 420b4538 Renato Botelho
			return $this;
3466 61e047a5 Phil Davis
		}
3467 420b4538 Renato Botelho
		foreach ($this->subqueues as $q) {
3468
			$result =& $q->find_queue("", $qname);
3469 61e047a5 Phil Davis
			if ($result) {
3470 420b4538 Renato Botelho
				return $result;
3471 61e047a5 Phil Davis
			}
3472 420b4538 Renato Botelho
		}
3473
	}
3474 c25a6b6a Ermal Luçi
3475 d62ba478 Ermal Luçi
	function &find_parentqueue($pipe, $qname) {
3476
		return NULL;
3477 420b4538 Renato Botelho
	}
3478 c25a6b6a Ermal Luçi
3479 d62ba478 Ermal Luçi
	function validate_input($data, &$input_errors) {
3480
		parent::validate_input($data, $input_errors);
3481 c25a6b6a Ermal Luçi
3482 c9ba2f8a Ermal
		$schedule = 0;
3483
		$schedulenone = 0;
3484
		$entries = 0;
3485 e02ea742 Ermal
		/* XXX: Really no better way? */
3486
		for ($i = 0; $i < 2900; $i++) {
3487 c9ba2f8a Ermal
			if (!empty($data["bwsched{$i}"])) {
3488 61e047a5 Phil Davis
				if ($data["bwsched{$i}"] != "none") {
3489 c9ba2f8a Ermal
					$schedule++;
3490 61e047a5 Phil Davis
				} else {
3491 c9ba2f8a Ermal
					$schedulenone++;
3492 61e047a5 Phil Davis
				}
3493 c9ba2f8a Ermal
			}
3494
			if (!empty($data["bandwidth{$i}"])) {
3495 61e047a5 Phil Davis
				if (!is_numeric($data["bandwidth{$i}"])) {
3496 c9ba2f8a Ermal
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3497 61e047a5 Phil Davis
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3498 794195d1 Phil Davis
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3499 61e047a5 Phil Davis
				} else {
3500 c9ba2f8a Ermal
					$entries++;
3501 61e047a5 Phil Davis
				}
3502 c9ba2f8a Ermal
			}
3503
		}
3504 61e047a5 Phil Davis
		if ($schedule == 0 && $entries > 1) {
3505 885af75c NOYB
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3506 61e047a5 Phil Davis
		}
3507
		if ($schedulenone > 0 && $entries > 1) {
3508 885af75c NOYB
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3509 61e047a5 Phil Davis
		}
3510
		if ($entries == 0) {
3511 885af75c NOYB
			$input_errors[] = gettext("At least one bw specification is necessary.");
3512 61e047a5 Phil Davis
		}
3513
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3514 dbaf21d4 Renato Botelho
			$input_errors[] = gettext("Delay must be an integer.");
3515 61e047a5 Phil Davis
		}
3516 dbaf21d4 Renato Botelho
	}
3517 c25a6b6a Ermal Luçi
3518 d62ba478 Ermal Luçi
	function ReadConfig(&$q) {
3519 1cbe86f0 Ermal
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3520
			$this->SetQname($q['newname']);
3521
		} else if (!empty($q['newname'])) {
3522
			$this->SetQname($q['newname']);
3523
		} else {
3524
			$this->SetQname($q['name']);
3525
		}
3526 d62ba478 Ermal Luçi
		$this->SetNumber($q['number']);
3527 c9ba2f8a Ermal
3528
		if (!empty($_POST)) {
3529
			$bandwidth = array();
3530 e02ea742 Ermal
			/* XXX: Really no better way? */
3531
			for ($i = 0; $i < 2900; $i++) {
3532 420b4538 Renato Botelho
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3533 c9ba2f8a Ermal
					$bw = array();
3534
					$bw['bw'] = $q["bandwidth{$i}"];
3535 4981f881 Ermal
					$bw['burst'] = $q["burst{$i}"];
3536 61e047a5 Phil Davis
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3537 c9ba2f8a Ermal
						$bw['bwscale'] = $q["bwtype{$i}"];
3538 61e047a5 Phil Davis
					}
3539
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3540 c9ba2f8a Ermal
						$bw['bwsched'] = $q["bwsched{$i}"];
3541 61e047a5 Phil Davis
					}
3542 c9ba2f8a Ermal
					$bandwidth[] = $bw;
3543
				}
3544
			}
3545
			$this->SetBandwidth($bandwidth);
3546 d62ba478 Ermal Luçi
		}
3547 420b4538 Renato Botelho
3548 4981f881 Ermal
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3549 c9ba2f8a Ermal
			$this->SetBandwidth($q['bandwidth']['item']);
3550 4981f881 Ermal
			$this->SetBurst($q['burst']['item']);
3551
		}
3552 420b4538 Renato Botelho
3553 61e047a5 Phil Davis
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3554 420b4538 Renato Botelho
			$this->SetQlimit($q['qlimit']);
3555 61e047a5 Phil Davis
		} else {
3556 420b4538 Renato Botelho
			$this->SetQlimit("");
3557 61e047a5 Phil Davis
		}
3558
		if (isset($q['mask']) && $q['mask'] <> "") {
3559 0b4e7542 Jean Cyr
			$masktype = $q['mask'];
3560 61e047a5 Phil Davis
		} else {
3561 0b4e7542 Jean Cyr
			$masktype = "";
3562 61e047a5 Phil Davis
		}
3563
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3564 0b4e7542 Jean Cyr
			$maskbits = $q['maskbits'];
3565 61e047a5 Phil Davis
		} else {
3566 0b4e7542 Jean Cyr
			$maskbits = "";
3567 61e047a5 Phil Davis
		}
3568
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3569 2d46e1e4 Jean Cyr
			$maskbitsv6 = $q['maskbitsv6'];
3570 61e047a5 Phil Davis
		} else {
3571 2d46e1e4 Jean Cyr
			$maskbitsv6 = "";
3572 61e047a5 Phil Davis
		}
3573 2d46e1e4 Jean Cyr
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3574 61e047a5 Phil Davis
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3575 420b4538 Renato Botelho
			$this->SetBuckets($q['buckets']);
3576 61e047a5 Phil Davis
		} else {
3577 420b4538 Renato Botelho
			$this->SetBuckets("");
3578 61e047a5 Phil Davis
		}
3579
		if (isset($q['plr']) && $q['plr'] <> "") {
3580 420b4538 Renato Botelho
			$this->SetPlr($q['plr']);
3581 61e047a5 Phil Davis
		} else {
3582 420b4538 Renato Botelho
			$this->SetPlr("");
3583 61e047a5 Phil Davis
		}
3584
		if (isset($q['delay']) && $q['delay'] <> "") {
3585 420b4538 Renato Botelho
			$this->SetDelay($q['delay']);
3586 61e047a5 Phil Davis
		} else {
3587 c2461a56 Ermal
			$this->SetDelay(0);
3588 61e047a5 Phil Davis
		}
3589
		if (isset($q['description']) && $q['description'] <> "") {
3590 d62ba478 Ermal Luçi
			$this->SetDescription($q['description']);
3591 61e047a5 Phil Davis
		} else {
3592 c2461a56 Ermal
			$this->SetDescription("");
3593 61e047a5 Phil Davis
		}
3594 d62ba478 Ermal Luçi
		$this->SetEnabled($q['enabled']);
3595 c25a6b6a Ermal Luçi
3596 420b4538 Renato Botelho
	}
3597 c25a6b6a Ermal Luçi
3598 d62ba478 Ermal Luçi
	function build_tree() {
3599 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3600 d62ba478 Ermal Luçi
		$tree .= $this->GetQname() . "</a>";
3601
		if (is_array($this->subqueues)) {
3602
			$tree .= "<ul>";
3603 4de8f7ba Phil Davis
			foreach ($this->subqueues as $q) {
3604 d62ba478 Ermal Luçi
				$tree .= $q->build_tree();
3605 420b4538 Renato Botelho
			}
3606 d62ba478 Ermal Luçi
			$tree .= "</ul>";
3607 c25a6b6a Ermal Luçi
		}
3608 d62ba478 Ermal Luçi
		$tree .= "</li>";
3609 ce0117f7 Colin Fleming
3610 d62ba478 Ermal Luçi
		return $tree;
3611
	}
3612 c25a6b6a Ermal Luçi
3613 420b4538 Renato Botelho
	function build_rules() {
3614 c9ba2f8a Ermal
		global $config, $time_based_rules;
3615
3616 61e047a5 Phil Davis
		if ($this->GetEnabled() == "") {
3617 8cb1c6e3 Ermal Luçi
			return;
3618 61e047a5 Phil Davis
		}
3619 8cb1c6e3 Ermal Luçi
3620 e7ccf2a5 Ermal
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3621 fa29a6f0 Ermal
		$found = false;
3622 c9ba2f8a Ermal
		$bandwidth = $this->GetBandwidth();
3623
		if (is_array($bandwidth)) {
3624
			foreach ($bandwidth as $bw) {
3625 fbfed5ba Ermal
				if ($bw['bwsched'] != "none") {
3626 c9ba2f8a Ermal
					$time_based_rules = true;
3627 2ed21904 Ermal
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3628 d96b96b9 Ermal
						foreach ($config['schedules']['schedule'] as $schedule) {
3629
							if ($bw['bwsched'] == $schedule['name']) {
3630
								if (filter_get_time_based_rule_status($schedule)) {
3631
									$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3632 61e047a5 Phil Davis
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3633 c90d37f3 jim-p
										$pfq_rule .= " burst ".trim($bw['burst']);
3634 61e047a5 Phil Davis
									}
3635 fa29a6f0 Ermal
									$found = true;
3636 d96b96b9 Ermal
									break;
3637
								}
3638 c9ba2f8a Ermal
							}
3639
						}
3640 0b3a76ca Ermal
					} else {
3641 fa29a6f0 Ermal
						$pfq_rule .= " bw 0";
3642 02593e37 Ermal Luçi
						$found = true;
3643 0b3a76ca Ermal
						break;
3644
					}
3645 ec091c89 Ermal
				} else {
3646 c9ba2f8a Ermal
					$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3647 61e047a5 Phil Davis
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3648 c90d37f3 jim-p
						$pfq_rule .= " burst ".trim($bw['burst']);
3649 61e047a5 Phil Davis
					}
3650 02593e37 Ermal Luçi
					$found = true;
3651 ec091c89 Ermal
					break;
3652
				}
3653 c9ba2f8a Ermal
			}
3654 61e047a5 Phil Davis
			if ($found == false) {
3655 fa29a6f0 Ermal
				$pfq_rule .= " bw 0";
3656 61e047a5 Phil Davis
			}
3657
		} else {
3658 ec091c89 Ermal
			$pfq_rule .= " bw 0";
3659 61e047a5 Phil Davis
		}
3660 c9ba2f8a Ermal
3661 61e047a5 Phil Davis
		if ($this->GetQlimit()) {
3662 4981f881 Ermal
			$pfq_rule .= " queue " . $this->GetQlimit();
3663 61e047a5 Phil Davis
		}
3664
		if ($this->GetPlr()) {
3665 d62ba478 Ermal Luçi
			$pfq_rule .= " plr " . $this->GetPlr();
3666 61e047a5 Phil Davis
		}
3667
		if ($this->GetBuckets()) {
3668 d62ba478 Ermal Luçi
			$pfq_rule .= " buckets " . $this->GetBuckets();
3669 61e047a5 Phil Davis
		}
3670
		if ($this->GetDelay()) {
3671 d62ba478 Ermal Luçi
			$pfq_rule .= " delay " . $this->GetDelay();
3672 61e047a5 Phil Davis
		}
3673 2d46e1e4 Jean Cyr
		$this->build_mask_rules($pfq_rule);
3674 d62ba478 Ermal Luçi
3675 f7f33eb3 Ermal
		$pfq_rule .= "\n";
3676 f5130e64 Ermal
3677
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3678 61e047a5 Phil Davis
			foreach ($this->subqueues as $q) {
3679
				$pfq_rule .= $q->build_rules();
3680
			}
3681 f5130e64 Ermal
		}
3682 d62ba478 Ermal Luçi
		$pfq_rule .= " \n";
3683 c25a6b6a Ermal Luçi
3684 d62ba478 Ermal Luçi
		return $pfq_rule;
3685 420b4538 Renato Botelho
	}
3686 c25a6b6a Ermal Luçi
3687 420b4538 Renato Botelho
	function update_dn_data(&$data) {
3688 d62ba478 Ermal Luçi
		$this->ReadConfig($data);
3689
	}
3690 c25a6b6a Ermal Luçi
3691 c9ba2f8a Ermal
	function build_javascript() {
3692
		global $g, $config;
3693
3694 8c50a9d8 Jean Cyr
		$javasr = parent::build_javascript();
3695 420b4538 Renato Botelho
3696 c9ba2f8a Ermal
		//build list of schedules
3697
		$schedules = "<option value='none'>none</option>";
3698
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3699
			foreach ($config['schedules']['schedule'] as $schedule) {
3700 61e047a5 Phil Davis
				if ($schedule['name'] <> "") {
3701 c9ba2f8a Ermal
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3702 61e047a5 Phil Davis
				}
3703 c9ba2f8a Ermal
			}
3704
		}
3705
		$bwopt = "";
3706 61e047a5 Phil Davis
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3707 c9ba2f8a Ermal
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3708 61e047a5 Phil Davis
		}
3709 c9ba2f8a Ermal
3710 8c50a9d8 Jean Cyr
		$javasr .= <<<EOD
3711 7da5315d Colin Fleming
<script type='text/javascript'>
3712 0fa05f45 Colin Fleming
//<![CDATA[
3713 c9ba2f8a Ermal
var addBwRowTo = (function() {
3714 3b6dedf3 Stephen Beaver
3715 c9ba2f8a Ermal
	return (function (tableId) {
3716 3b6dedf3 Stephen Beaver
3717 d2466d40 Stephen Beaver
	var table = document.getElementById(tableId);
3718
	var totalrows = table.rows.length -1;
3719 3b6dedf3 Stephen Beaver
3720 d2466d40 Stephen Beaver
	var row = table.insertRow(totalrows + 1);
3721
	var cell1 = row.insertCell(0);
3722
	var cell2 = row.insertCell(1);
3723
	var cell3 = row.insertCell(2);
3724
	var cell4 = row.insertCell(3);
3725
3726
	cell1.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bandwidth_row-" + totalrows + "' /><input type='text' class='form-control' name='bandwidth" + totalrows + "' id='bandwidth" + totalrows + "' />";
3727
	cell2.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwtype_row-" + totalrows + "' /><select class='form-control' name='bwtype" + totalrows + "'>{$bwopt}</select>";
3728
	cell3.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwsched_row-" + totalrows + "' /><select class='form-control' name='bwsched" + totalrows + "'>{$schedules}</select>";
3729 27d6a45b jim-p
	cell4.innerHTML = '<a class="btn btn-warning" onclick="removeBwRow(this); return false;" href="#"><i class="fa fa-trash icon-embed-btn"></i>Delete</a>';
3730 d2466d40 Stephen Beaver
3731 c9ba2f8a Ermal
	});
3732
})();
3733
3734
function removeBwRow(el) {
3735 d2466d40 Stephen Beaver
	var d = el.parentNode.parentNode.rowIndex;
3736 3b6dedf3 Stephen Beaver
	document.getElementById('maintable').deleteRow(d);
3737 c9ba2f8a Ermal
}
3738 0fa05f45 Colin Fleming
//]]>
3739 c9ba2f8a Ermal
</script>
3740
3741
EOD;
3742
3743 420b4538 Renato Botelho
		return $javasr;
3744 c9ba2f8a Ermal
	}
3745
3746 d2466d40 Stephen Beaver
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3747 df67ba6a Stephen Beaver
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3748 d2466d40 Stephen Beaver
	// with the javascript in this class
3749
	function build_bwtable() {
3750
		global $config;
3751 3b6dedf3 Stephen Beaver
3752 d2466d40 Stephen Beaver
		$bandwidth = $this->GetBandwidth();
3753
				//build list of schedules
3754
		$schedules = array();
3755
		$schedules[] = "none";//leave none to leave rule enabled all the time
3756
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3757
			foreach ($config['schedules']['schedule'] as $schedule) {
3758 df67ba6a Stephen Beaver
				if ($schedule['name'] != "") {
3759 d2466d40 Stephen Beaver
					$schedules[] = $schedule['name'];
3760
				}
3761
			}
3762
		}
3763 3b6dedf3 Stephen Beaver
3764 df67ba6a Stephen Beaver
		$form = '<div class="table-responsive">';
3765
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3766 d2466d40 Stephen Beaver
		$form .= "<thead><tr>";
3767 df67ba6a Stephen Beaver
		$form .= "<th>Bandwidth</th>";
3768 d2466d40 Stephen Beaver
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3769 df67ba6a Stephen Beaver
		$form .= "<th>Bw type</th>";
3770
		$form .= "<th>Schedule</th>";
3771
		$form .= "<th></th>";
3772 d2466d40 Stephen Beaver
		$form .= "</tr></thead>";
3773
		$form .= "<tbody>";
3774 3b6dedf3 Stephen Beaver
3775 df67ba6a Stephen Beaver
		// If there are no bandwidths defined, make a blank one for convenience
3776 4e322e2c Phil Davis
		if (empty($bandwidth)) {
3777 df67ba6a Stephen Beaver
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3778 4e322e2c Phil Davis
		}
3779 b37b4034 Phil Davis
3780 d2466d40 Stephen Beaver
		if (is_array($bandwidth)) {
3781
			foreach ($bandwidth as $bwidx => $bw) {
3782 df67ba6a Stephen Beaver
				$form .= '<tr>';
3783
				$form .= '<td class="col-xs-4">';
3784
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3785 d2466d40 Stephen Beaver
				//$form .= "</td><td width='20%'>";
3786
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3787 df67ba6a Stephen Beaver
				$form .= "</td>";
3788
				$form .= '<td class="col-xs-4">';
3789 d2466d40 Stephen Beaver
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3790 b37b4034 Phil Davis
3791 f95cdbea Chris Buechler
				foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "b" => "Bit/s") as $bwsidx => $bwscale) {
3792 d2466d40 Stephen Beaver
					$form .= "<option value=\"{$bwsidx}\"";
3793 b37b4034 Phil Davis
3794 d2466d40 Stephen Beaver
					if ($bw['bwscale'] == $bwsidx) {
3795 c4b60a9a Colin Fleming
						$form .= " selected";
3796 d2466d40 Stephen Beaver
					}
3797 b37b4034 Phil Davis
3798 d2466d40 Stephen Beaver
					$form .= ">{$bwscale}</option>";
3799
				}
3800 b37b4034 Phil Davis
3801 d2466d40 Stephen Beaver
				$form .= "</select>";
3802 df67ba6a Stephen Beaver
				$form .= "</td>";
3803
				$form .= '<td class="col-xs-4">';
3804 d2466d40 Stephen Beaver
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3805 b37b4034 Phil Davis
3806 d2466d40 Stephen Beaver
				foreach ($schedules as $schd) {
3807
					$selected = "";
3808
					if ($bw['bwsched'] == $schd) {
3809 c4b60a9a Colin Fleming
						$selected = "selected";
3810 d2466d40 Stephen Beaver
					}
3811 b37b4034 Phil Davis
3812 d2466d40 Stephen Beaver
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3813
				}
3814 b37b4034 Phil Davis
3815 d2466d40 Stephen Beaver
				$form .= "</select>";
3816 df67ba6a Stephen Beaver
				$form .= "</td>";
3817
				$form .= '<td>';
3818 27d6a45b jim-p
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
3819 d2466d40 Stephen Beaver
				$form .= "</td></tr>";
3820
			}
3821
		}
3822 df67ba6a Stephen Beaver
		$form .= "</tbody></table></div><br />";
3823 3b6dedf3 Stephen Beaver
3824 b9bc38c1 NOYB
		$form .= '<a class="btn btn-sm btn-success" onclick="javascript:addBwRowTo(\'maintable\'); return false;" >';
3825 27d6a45b jim-p
		$form .= '<i class="fa fa-plus icon-embed-btn"></i>';
3826
		$form .= gettext("Add Schedule") . "</a>";
3827 3b6dedf3 Stephen Beaver
3828 d2466d40 Stephen Beaver
		return($form);
3829
	}
3830 3b6dedf3 Stephen Beaver
3831 7903dd5e Stephen Beaver
	function build_form() {
3832
		global $g, $config, $pipe, $action, $qname;
3833 c9ba2f8a Ermal
3834
		//build list of schedules
3835
		$schedules = array();
3836
		$schedules[] = "none";//leave none to leave rule enabled all the time
3837
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3838
			foreach ($config['schedules']['schedule'] as $schedule) {
3839 61e047a5 Phil Davis
				if ($schedule['name'] <> "") {
3840 c9ba2f8a Ermal
					$schedules[] = $schedule['name'];
3841 61e047a5 Phil Davis
				}
3842 c9ba2f8a Ermal
			}
3843
		}
3844
3845 7903dd5e Stephen Beaver
3846
		$sform = new Form();
3847 5605a0c4 Stephen Beaver
		$sform->setAction("firewall_shaper.php");
3848 7903dd5e Stephen Beaver
3849
		$section = new Form_Section('Limiters');
3850
3851
		$section->addInput(new Form_Checkbox(
3852
			'enabled',
3853
			'Enable',
3854
			'Enable limiter and its children',
3855
			($this->GetEnabled() == "on"),
3856
			'on'
3857
		));
3858
3859
		$section->addInput(new Form_Input(
3860
			'newname',
3861 85877e3c Phil Davis
			'*Name',
3862 7903dd5e Stephen Beaver
			'text',
3863
			$this->GetQname()
3864
		));
3865 d2466d40 Stephen Beaver
3866 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3867
			'name',
3868
			null,
3869
			'hidden',
3870
			$this->GetQname()
3871
		));
3872 d2466d40 Stephen Beaver
3873 85a236e9 Ermal
		if ($this->GetNumber() > 0) {
3874 7903dd5e Stephen Beaver
			$section->addInput(new Form_Input(
3875
				'number',
3876
				null,
3877
				'hidden',
3878
				$this->GetNumber()
3879
			));
3880
		}
3881 d2466d40 Stephen Beaver
3882
		$bandwidth = $this->GetBandwidth();
3883 7903dd5e Stephen Beaver
3884 c9ba2f8a Ermal
		if (is_array($bandwidth)) {
3885 df67ba6a Stephen Beaver
				$section->addInput(new Form_StaticText(
3886 7903dd5e Stephen Beaver
				'Bandwidth',
3887 d2466d40 Stephen Beaver
				$this->build_bwtable()
3888 3b6dedf3 Stephen Beaver
			));
3889 7903dd5e Stephen Beaver
		}
3890
3891 0b4e7542 Jean Cyr
		$mask = $this->GetMask();
3892 d2466d40 Stephen Beaver
3893 7903dd5e Stephen Beaver
		$section->addInput(new Form_Select(
3894 23f4c08f Stephen Beaver
			'mask',
3895 7903dd5e Stephen Beaver
			'Mask',
3896
			$mask['type'],
3897 b50d30c3 Stephen Beaver
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3898 7903dd5e Stephen Beaver
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3899
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3900
					'This makes it possible to easily specify bandwidth limits per host.');
3901 d2466d40 Stephen Beaver
3902 7903dd5e Stephen Beaver
		$group = new Form_Group(null);
3903 d2466d40 Stephen Beaver
3904 7903dd5e Stephen Beaver
		$group->add(new Form_Select(
3905
			'maskbits',
3906
			null,
3907
			$mask['bits'],
3908
			array_combine(range(32, 1, -1), range(32, 1, -1))
3909 3481524a Chris Buechler
		))->setHelp('IPv4 mask bits' . '<br />' . '255.255.255.255/?');
3910 d2466d40 Stephen Beaver
3911 7903dd5e Stephen Beaver
		$group->add(new Form_Select(
3912
			'maskbitsv6',
3913
			null,
3914
			$mask['bitsv6'],
3915
			array_combine(range(128, 1, -1), range(128, 1, -1))
3916 3481524a Chris Buechler
		))->setHelp('IPv6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3917 d2466d40 Stephen Beaver
3918 7903dd5e Stephen Beaver
		$section->add($group);
3919 c25a6b6a Ermal Luçi
3920 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3921
			'description',
3922
			'Description',
3923
			'text',
3924
			$this->GetDescription()
3925 1c92c5b1 Stephen Beaver
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
3926 d2466d40 Stephen Beaver
3927 7903dd5e Stephen Beaver
		$sform->add($section);
3928 d2466d40 Stephen Beaver
3929 5f88f964 k-paulius
		$section = new Form_Section('Advanced Options');
3930 d2466d40 Stephen Beaver
3931 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3932
			'delay',
3933 df67ba6a Stephen Beaver
			'Delay (ms)',
3934
			'text',
3935
			$this->GetDelay() > 0 ? $this->GetDelay():null
3936 885af75c NOYB
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
3937 d2466d40 Stephen Beaver
3938 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3939
			'plr',
3940
			'Packet Loss Rate',
3941
			'number',
3942
			$this->GetPlr(),
3943
			['step' => '0.001', 'min' => '0.000']
3944 1c92c5b1 Stephen Beaver
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
3945 885af75c NOYB
					'A value of 0.001 means one packet in 1000 gets dropped.');
3946 d2466d40 Stephen Beaver
3947 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3948
			'qlimit',
3949
			'Queue size (slots)',
3950
			'number',
3951
			$this->GetQlimit()
3952 1c92c5b1 Stephen Beaver
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
3953 7903dd5e Stephen Beaver
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
3954 d2466d40 Stephen Beaver
3955 7903dd5e Stephen Beaver
		$section->addInput(new Form_Input(
3956
			'buckets',
3957
			'Bucket size (slots)',
3958
			'number',
3959
			$this->GetBuckets()
3960 885af75c NOYB
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
3961 ce0117f7 Colin Fleming
3962 7903dd5e Stephen Beaver
		$sform->add($section);
3963 d2466d40 Stephen Beaver
3964 7903dd5e Stephen Beaver
		return($sform);
3965 c25a6b6a Ermal Luçi
		}
3966
3967 d62ba478 Ermal Luçi
	function wconfig() {
3968
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
3969 61e047a5 Phil Davis
		if (!is_array($cflink)) {
3970 420b4538 Renato Botelho
			$cflink = array();
3971 61e047a5 Phil Davis
		}
3972 d62ba478 Ermal Luçi
		$cflink['name'] = $this->GetQname();
3973
		$cflink['number'] = $this->GetNumber();
3974 420b4538 Renato Botelho
		$cflink['qlimit'] = $this->GetQlimit();
3975
		$cflink['plr'] = $this->GetPlr();
3976
		$cflink['description'] = $this->GetDescription();
3977
3978 c9ba2f8a Ermal
		$bandwidth = $this->GetBandwidth();
3979
		if (is_array($bandwidth)) {
3980
			$cflink['bandwidth'] = array();
3981
			$cflink['bandwidth']['item'] = array();
3982 61e047a5 Phil Davis
			foreach ($bandwidth as $bwidx => $bw) {
3983 c9ba2f8a Ermal
				$cflink['bandwidth']['item'][] = $bw;
3984 61e047a5 Phil Davis
			}
3985 c9ba2f8a Ermal
		}
3986
3987 d62ba478 Ermal Luçi
		$cflink['enabled'] = $this->GetEnabled();
3988
		$cflink['buckets'] = $this->GetBuckets();
3989 0b4e7542 Jean Cyr
		$mask = $this->GetMask();
3990
		$cflink['mask'] = $mask['type'];
3991
		$cflink['maskbits'] = $mask['bits'];
3992 420b4538 Renato Botelho
		$cflink['maskbitsv6'] = $mask['bitsv6'];
3993 d62ba478 Ermal Luçi
		$cflink['delay'] = $this->GetDelay();
3994
	}
3995 c25a6b6a Ermal Luçi
3996
}
3997
3998
class dnqueue_class extends dummynet_class {
3999 420b4538 Renato Botelho
	var $pipeparent;
4000
	var $weight;
4001
4002
	function GetWeight() {
4003
		return $this->weight;
4004
	}
4005
	function SetWeight($weight) {
4006
		$this->weight = $weight;
4007
	}
4008 d62ba478 Ermal Luçi
	function GetPipe() {
4009
		return $this->pipeparent;
4010
	}
4011
	function SetPipe($pipe) {
4012
		$this->pipeparent = $pipe;
4013
	}
4014 c25a6b6a Ermal Luçi
4015 d62ba478 Ermal Luçi
	/* Just a stub in case we ever try to call this from the frontend. */
4016 61e047a5 Phil Davis
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4017
		return;
4018
	}
4019 c25a6b6a Ermal Luçi
4020 d62ba478 Ermal Luçi
	function delete_queue() {
4021
		cleanup_dnqueue_from_rules($this->GetQname());
4022
		unset_dn_object_by_reference($this->GetLink());
4023 f2c34876 Ermal
		@pfSense_pipe_action("queue delete " . $this->GetNumber());
4024 420b4538 Renato Botelho
	}
4025 c25a6b6a Ermal Luçi
4026 d62ba478 Ermal Luçi
	function validate_input($data, &$input_errors) {
4027
		parent::validate_input($data, $input_errors);
4028 c25a6b6a Ermal Luçi
4029 d62ba478 Ermal Luçi
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4030 4de8f7ba Phil Davis
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4031 420b4538 Renato Botelho
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4032 61e047a5 Phil Davis
		}
4033 d62ba478 Ermal Luçi
	}
4034 c25a6b6a Ermal Luçi
4035 420b4538 Renato Botelho
	/*
4036
	 * Should search even its children
4037
	 */
4038
	function &find_queue($pipe, $qname) {
4039 61e047a5 Phil Davis
		if ($qname == $this->GetQname()) {
4040 420b4538 Renato Botelho
			return $this;
4041 61e047a5 Phil Davis
		} else {
4042 d62ba478 Ermal Luçi
			return NULL;
4043 61e047a5 Phil Davis
		}
4044 420b4538 Renato Botelho
	}
4045 c25a6b6a Ermal Luçi
4046 d62ba478 Ermal Luçi
	function &find_parentqueue($pipe, $qname) {
4047
		return $this->qparent;
4048 420b4538 Renato Botelho
	}
4049 c25a6b6a Ermal Luçi
4050 420b4538 Renato Botelho
	function &get_queue_list(&$qlist) {
4051 61e047a5 Phil Davis
		if ($this->GetEnabled() == "") {
4052 146f0fad Ermal
			return;
4053 61e047a5 Phil Davis
		}
4054 420b4538 Renato Botelho
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4055
	}
4056 c25a6b6a Ermal Luçi
4057 d62ba478 Ermal Luçi
	function ReadConfig(&$q) {
4058 1cbe86f0 Ermal
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4059
			$this->SetQname($q['newname']);
4060
		} else if (!empty($q['newname'])) {
4061
			$this->SetQname($q['newname']);
4062
		} else {
4063
			$this->SetQname($q['name']);
4064
		}
4065 d62ba478 Ermal Luçi
		$this->SetNumber($q['number']);
4066 61e047a5 Phil Davis
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4067 420b4538 Renato Botelho
			$this->SetQlimit($q['qlimit']);
4068 61e047a5 Phil Davis
		} else {
4069 420b4538 Renato Botelho
			$this->SetQlimit("");
4070 61e047a5 Phil Davis
		}
4071
		if (isset($q['mask']) && $q['mask'] <> "") {
4072 0b4e7542 Jean Cyr
			$masktype = $q['mask'];
4073 61e047a5 Phil Davis
		} else {
4074 0b4e7542 Jean Cyr
			$masktype = "";
4075 61e047a5 Phil Davis
		}
4076
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4077 0b4e7542 Jean Cyr
			$maskbits = $q['maskbits'];
4078 61e047a5 Phil Davis
		} else {
4079 0b4e7542 Jean Cyr
			$maskbits = "";
4080 61e047a5 Phil Davis
		}
4081
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4082 2d46e1e4 Jean Cyr
			$maskbitsv6 = $q['maskbitsv6'];
4083 61e047a5 Phil Davis
		} else {
4084 2d46e1e4 Jean Cyr
			$maskbitsv6 = "";
4085 61e047a5 Phil Davis
		}
4086 2d46e1e4 Jean Cyr
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4087 61e047a5 Phil Davis
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4088 dda9c67f Renato Botelho
			$this->SetBuckets($q['buckets']);
4089 61e047a5 Phil Davis
		} else {
4090 dda9c67f Renato Botelho
			$this->SetBuckets("");
4091 61e047a5 Phil Davis
		}
4092
		if (isset($q['plr']) && $q['plr'] <> "") {
4093 dda9c67f Renato Botelho
			$this->SetPlr($q['plr']);
4094 61e047a5 Phil Davis
		} else {
4095 dda9c67f Renato Botelho
			$this->SetPlr("");
4096 61e047a5 Phil Davis
		}
4097
		if (isset($q['weight']) && $q['weight'] <> "") {
4098 420b4538 Renato Botelho
			$this->SetWeight($q['weight']);
4099 61e047a5 Phil Davis
		} else {
4100 420b4538 Renato Botelho
			$this->SetWeight("");
4101 61e047a5 Phil Davis
		}
4102
		if (isset($q['description']) && $q['description'] <> "") {
4103 d62ba478 Ermal Luçi
			$this->SetDescription($q['description']);
4104 61e047a5 Phil Davis
		} else {
4105 daacb818 Ermal
			$this->SetDescription("");
4106 61e047a5 Phil Davis
		}
4107 d62ba478 Ermal Luçi
		$this->SetEnabled($q['enabled']);
4108 420b4538 Renato Botelho
	}
4109 c25a6b6a Ermal Luçi
4110 d62ba478 Ermal Luçi
	function build_tree() {
4111
		$parent =& $this->GetParent();
4112 420b4538 Renato Botelho
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4113 d62ba478 Ermal Luçi
		$tree .= $this->GetQname() . "</a>";
4114
		$tree .= "</li>";
4115 ce0117f7 Colin Fleming
4116 d62ba478 Ermal Luçi
		return $tree;
4117
	}
4118 c25a6b6a Ermal Luçi
4119 420b4538 Renato Botelho
	function build_rules() {
4120 61e047a5 Phil Davis
		if ($this->GetEnabled() == "") {
4121 420b4538 Renato Botelho
			return;
4122 61e047a5 Phil Davis
		}
4123 8cb1c6e3 Ermal Luçi
4124 d62ba478 Ermal Luçi
		$parent =& $this->GetParent();
4125 420b4538 Renato Botelho
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4126 61e047a5 Phil Davis
		if ($this->GetQlimit()) {
4127 420b4538 Renato Botelho
			$pfq_rule .= " queue " . $this->GetQlimit();
4128 61e047a5 Phil Davis
		}
4129
		if ($this->GetWeight()) {
4130 d62ba478 Ermal Luçi
			$pfq_rule .= " weight " . $this->GetWeight();
4131 61e047a5 Phil Davis
		}
4132
		if ($this->GetBuckets()) {
4133 d62ba478 Ermal Luçi
			$pfq_rule .= " buckets " . $this->GetBuckets();
4134 61e047a5 Phil Davis
		}
4135 2d46e1e4 Jean Cyr
		$this->build_mask_rules($pfq_rule);
4136 9b10a6ec Ermal
		$pfq_rule .= "\n";
4137 c25a6b6a Ermal Luçi
4138 d62ba478 Ermal Luçi
		return $pfq_rule;
4139
	}
4140
4141 420b4538 Renato Botelho
	function build_javascript() {
4142 8c50a9d8 Jean Cyr
		return parent::build_javascript();
4143
	}
4144
4145 d2466d40 Stephen Beaver
	function build_form() {
4146
		global $g, $config, $pipe, $action, $qname;
4147
4148
		//build list of schedules
4149
		$schedules = array();
4150
		$schedules[] = "none";//leave none to leave rule enabled all the time
4151
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4152
			foreach ($config['schedules']['schedule'] as $schedule) {
4153
				if ($schedule['name'] <> "") {
4154
					$schedules[] = $schedule['name'];
4155
				}
4156
			}
4157
		}
4158
4159
4160
		$sform = new Form();
4161 5605a0c4 Stephen Beaver
		$sform->setAction("firewall_shaper.php");
4162 d2466d40 Stephen Beaver
		$section = new Form_Section('Limiters');
4163
4164
		$section->addInput(new Form_Checkbox(
4165
			'enabled',
4166
			'Enable',
4167
			'Enable this queue',
4168
			($this->GetEnabled() == "on"),
4169
			'on'
4170
		));
4171
4172
		$section->addInput(new Form_Input(
4173
			'newname',
4174 85877e3c Phil Davis
			'*Name',
4175 d2466d40 Stephen Beaver
			'text',
4176
			$this->GetQname()
4177
		));
4178
4179
		$section->addInput(new Form_Input(
4180
			'name',
4181
			null,
4182
			'hidden',
4183
			$this->GetQname()
4184
		));
4185
4186 85a236e9 Ermal
		if ($this->GetNumber() > 0) {
4187 d2466d40 Stephen Beaver
			$section->addInput(new Form_Input(
4188
				'number',
4189
				null,
4190
				'hidden',
4191
				$this->GetNumber()
4192
			));
4193
		}
4194
4195 0b4e7542 Jean Cyr
		$mask = $this->GetMask();
4196 c25a6b6a Ermal Luçi
4197 d2466d40 Stephen Beaver
		$section->addInput(new Form_Select(
4198 49882789 Stephen Beaver
			'mask',
4199 d2466d40 Stephen Beaver
			'Mask',
4200
			$mask['type'],
4201 b50d30c3 Stephen Beaver
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4202 d2466d40 Stephen Beaver
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4203
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4204
					'This makes it possible to easily specify bandwidth limits per host.');
4205
4206
		$group = new Form_Group(null);
4207
4208
		$group->add(new Form_Select(
4209
			'maskbits',
4210
			null,
4211
			$mask['bits'],
4212
			array_combine(range(32, 1, -1), range(32, 1, -1))
4213 3481524a Chris Buechler
		))->setHelp('IPv4 mask bits' . '<br />' . '255.255.255.255/?');
4214 d2466d40 Stephen Beaver
4215
		$group->add(new Form_Select(
4216
			'maskbitsv6',
4217
			null,
4218
			$mask['bitsv6'],
4219
			array_combine(range(128, 1, -1), range(128, 1, -1))
4220 3481524a Chris Buechler
		))->setHelp('IPv6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4221 d2466d40 Stephen Beaver
4222
		$section->add($group);
4223
4224
		$section->addInput(new Form_Input(
4225
			'description',
4226
			'Description',
4227
			'text',
4228
			$this->GetDescription()
4229 1c92c5b1 Stephen Beaver
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4230 d2466d40 Stephen Beaver
4231
		$sform->add($section);
4232
4233 5f88f964 k-paulius
		$section = new Form_Section('Advanced Options');
4234 d2466d40 Stephen Beaver
4235
		$section->addInput(new Form_Input(
4236
			'weight',
4237
			'Weight',
4238
			'number',
4239
			$this->GetWeight(),
4240
			['min' => '1', 'max' => '100']
4241 3b6dedf3 Stephen Beaver
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4242 1c92c5b1 Stephen Beaver
					' it can be left blank otherwise.');
4243 ce0117f7 Colin Fleming
4244 d2466d40 Stephen Beaver
		$section->addInput(new Form_Input(
4245
			'plr',
4246
			'Packet Loss Rate',
4247
			'number',
4248
			$this->GetPlr(),
4249
			['step' => '0.001', 'min' => '0.000']
4250 1c92c5b1 Stephen Beaver
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4251 d2466d40 Stephen Beaver
					'A value of 0.001 means one packet in 1000 gets dropped');
4252
4253
		$section->addInput(new Form_Input(
4254
			'qlimit',
4255
			'Queue size (slots)',
4256
			'number',
4257
			$this->GetQlimit()
4258 1c92c5b1 Stephen Beaver
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4259 d2466d40 Stephen Beaver
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4260
4261
		$section->addInput(new Form_Input(
4262
			'buckets',
4263
			'Bucket size (slots)',
4264
			'number',
4265
			$this->GetBuckets()
4266 1c92c5b1 Stephen Beaver
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4267 d2466d40 Stephen Beaver
4268 3b6dedf3 Stephen Beaver
		$section->addInput(new Form_Input(
4269
			'pipe',
4270
			null,
4271
			'hidden',
4272
			$this->GetPipe()
4273
		));
4274
4275 d2466d40 Stephen Beaver
		$sform->add($section);
4276
4277
		return($sform);
4278 d62ba478 Ermal Luçi
	}
4279 c25a6b6a Ermal Luçi
4280 420b4538 Renato Botelho
	function update_dn_data(&$data) {
4281 d62ba478 Ermal Luçi
		$this->ReadConfig($data);
4282
	}
4283 c25a6b6a Ermal Luçi
4284 d62ba478 Ermal Luçi
	function wconfig() {
4285
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4286 61e047a5 Phil Davis
		if (!is_array($cflink)) {
4287 420b4538 Renato Botelho
			$cflink = array();
4288 61e047a5 Phil Davis
		}
4289 d62ba478 Ermal Luçi
		$cflink['name'] = $this->GetQname();
4290
		$cflink['number'] = $this->GetNumber();
4291 420b4538 Renato Botelho
		$cflink['qlimit'] = $this->GetQlimit();
4292
		$cflink['description'] = $this->GetDescription();
4293 d62ba478 Ermal Luçi
		$cflink['weight'] = $this->GetWeight();
4294
		$cflink['enabled'] = $this->GetEnabled();
4295
		$cflink['buckets'] = $this->GetBuckets();
4296 0b4e7542 Jean Cyr
		$mask = $this->GetMask();
4297
		$cflink['mask'] = $mask['type'];
4298
		$cflink['maskbits'] = $mask['bits'];
4299 420b4538 Renato Botelho
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4300 d62ba478 Ermal Luçi
	}
4301 c25a6b6a Ermal Luçi
}
4302
4303 daee46a5 Helder Pereira
function get_dummynet_name_list() {
4304 ce0117f7 Colin Fleming
4305 daee46a5 Helder Pereira
	$dn_name_list =& get_unique_dnqueue_list();
4306
	$dn_name = array();
4307 61e047a5 Phil Davis
	if (is_array($dn_name_list)) {
4308
		foreach ($dn_name_list as $key => $value) {
4309 daee46a5 Helder Pereira
			$dn_name[] = $key;
4310 61e047a5 Phil Davis
		}
4311
	}
4312 ce0117f7 Colin Fleming
4313 daee46a5 Helder Pereira
	return $dn_name;
4314 ce0117f7 Colin Fleming
4315 daee46a5 Helder Pereira
}
4316
4317
function get_altq_name_list() {
4318
	$altq_name_list =& get_unique_queue_list();
4319
	$altq_name = array();
4320 61e047a5 Phil Davis
	if (is_array($altq_name_list)) {
4321
		foreach ($altq_name_list as $key => $aqobj) {
4322 daee46a5 Helder Pereira
			$altq_name[] = $key;
4323 61e047a5 Phil Davis
		}
4324
	}
4325 ce0117f7 Colin Fleming
4326 daee46a5 Helder Pereira
	return $altq_name;
4327 f63d5b66 Helder Pereira
}
4328 c25a6b6a Ermal Luçi
4329 197bfe96 Ermal Luçi
/*
4330 61e047a5 Phil Davis
 * XXX: TODO Make a class shaper to hide all these functions
4331 197bfe96 Ermal Luçi
 * from the global namespace.
4332
 */
4333
4334 420b4538 Renato Botelho
/*
4335
 * This is a layer violation but for now there is no way
4336 61e047a5 Phil Davis
 * I can find to properly do this with PHP.
4337 197bfe96 Ermal Luçi
 */
4338
function altq_get_default_queue($interface) {
4339
	global $altq_list_queues;
4340 061f78b1 Bill Marquette
4341 197bfe96 Ermal Luçi
	$altq_tmp = $altq_list_queues[$interface];
4342 61e047a5 Phil Davis
	if ($altq_tmp) {
4343 420b4538 Renato Botelho
		return $altq_tmp->GetDefaultQueuePresent();
4344 61e047a5 Phil Davis
	} else {
4345 acebc1ec Ermal
		return false;
4346 61e047a5 Phil Davis
	}
4347 197bfe96 Ermal Luçi
}
4348 061f78b1 Bill Marquette
4349 21a0464c Ermal Luçi
function altq_check_default_queues() {
4350
	global $altq_list_queues;
4351
4352
	$count = 0;
4353
	if (is_array($altq_list_queues)) {
4354 61e047a5 Phil Davis
		foreach ($altq_list_queues as $altq) {
4355
			if ($altq->GetDefaultQueuePresent()) {
4356 21a0464c Ermal Luçi
				$count++;
4357 61e047a5 Phil Davis
			}
4358 21a0464c Ermal Luçi
		}
4359
	}
4360 61e047a5 Phil Davis
	else {
4361
		$count++;
4362
	}
4363 ce0117f7 Colin Fleming
4364 21a0464c Ermal Luçi
	return 0;
4365
}
4366
4367
function &get_unique_queue_list() {
4368
	global $altq_list_queues;
4369 ce0117f7 Colin Fleming
4370 21a0464c Ermal Luçi
	$qlist = array();
4371
	if (is_array($altq_list_queues)) {
4372
		foreach ($altq_list_queues as $altq) {
4373 61e047a5 Phil Davis
			if ($altq->GetEnabled() == "") {
4374 146f0fad Ermal
				continue;
4375 61e047a5 Phil Davis
			}
4376 21a0464c Ermal Luçi
			$tmplist =& $altq->get_queue_list();
4377 146f0fad Ermal
			foreach ($tmplist as $qname => $link) {
4378 61e047a5 Phil Davis
				if ($link->GetEnabled() <> "") {
4379 420b4538 Renato Botelho
					$qlist[$qname] = $link;
4380 61e047a5 Phil Davis
				}
4381 146f0fad Ermal
			}
4382 21a0464c Ermal Luçi
		}
4383
	}
4384
	return $qlist;
4385
}
4386
4387 309ffde9 Ermal Luçi
function &get_unique_dnqueue_list() {
4388 c25a6b6a Ermal Luçi
	global $dummynet_pipe_list;
4389 ce0117f7 Colin Fleming
4390 c25a6b6a Ermal Luçi
	$qlist = array();
4391
	if (is_array($dummynet_pipe_list)) {
4392
		foreach ($dummynet_pipe_list as $dn) {
4393 61e047a5 Phil Davis
			if ($dn->GetEnabled() == "") {
4394 146f0fad Ermal
				continue;
4395 61e047a5 Phil Davis
			}
4396 c25a6b6a Ermal Luçi
			$tmplist =& $dn->get_queue_list();
4397 146f0fad Ermal
			foreach ($tmplist as $qname => $link) {
4398 420b4538 Renato Botelho
				$qlist[$qname] = $link;
4399 146f0fad Ermal
			}
4400 c25a6b6a Ermal Luçi
		}
4401
	}
4402
	return $qlist;
4403
}
4404
4405 197bfe96 Ermal Luçi
function ref_on_altq_queue_list($parent, $qname) {
4406 61e047a5 Phil Davis
	if (isset($GLOBALS['queue_list'][$qname])) {
4407 197bfe96 Ermal Luçi
		$GLOBALS['queue_list'][$qname]++;
4408 61e047a5 Phil Davis
	} else {
4409 197bfe96 Ermal Luçi
		$GLOBALS['queue_list'][$qname] = 1;
4410 61e047a5 Phil Davis
	}
4411 061f78b1 Bill Marquette
4412 197bfe96 Ermal Luçi
	unref_on_altq_queue_list($parent);
4413
}
4414 061f78b1 Bill Marquette
4415 197bfe96 Ermal Luçi
function unref_on_altq_queue_list($qname) {
4416
	$GLOBALS['queue_list'][$qname]--;
4417 61e047a5 Phil Davis
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4418 420b4538 Renato Botelho
		unset($GLOBALS['queue_list'][$qname]);
4419 61e047a5 Phil Davis
	}
4420 197bfe96 Ermal Luçi
}
4421 061f78b1 Bill Marquette
4422 197bfe96 Ermal Luçi
function read_altq_config() {
4423
	global $altq_list_queues, $config;
4424
	$path = array();
4425 ce0117f7 Colin Fleming
4426 61e047a5 Phil Davis
	if (!is_array($config['shaper'])) {
4427 7849bdd5 Ermal Lu?i
		$config['shaper'] = array();
4428 61e047a5 Phil Davis
	}
4429
	if (!is_array($config['shaper']['queue'])) {
4430 7849bdd5 Ermal Lu?i
		$config['shaper']['queue'] = array();
4431 61e047a5 Phil Davis
	}
4432 197bfe96 Ermal Luçi
	$a_int = &$config['shaper']['queue'];
4433
4434
	$altq_list_queues = array();
4435 ce0117f7 Colin Fleming
4436 61e047a5 Phil Davis
	if (!is_array($config['shaper']['queue'])) {
4437 197bfe96 Ermal Luçi
		return;
4438 61e047a5 Phil Davis
	}
4439 197bfe96 Ermal Luçi
4440
	foreach ($a_int as $key => $conf) {
4441 520ad1a4 Ermal
		$int = $conf['interface'];
4442 23ca5695 Chris Buechler
		$root =& new altq_root_queue();
4443 520ad1a4 Ermal
		$root->SetInterface($int);
4444
		$altq_list_queues[$root->GetInterface()] = &$root;
4445
		$root->ReadConfig($conf);
4446 197bfe96 Ermal Luçi
		array_push($path, $key);
4447
		$root->SetLink($path);
4448
		if (is_array($conf['queue'])) {
4449
			foreach ($conf['queue'] as $key1 => $q) {
4450
				array_push($path, $key1);
4451 420b4538 Renato Botelho
				/*
4452
				 * XXX: we completely ignore errors here but anyway we must have
4453 92125c97 Ermal Luçi
				 *	checked them before so no harm should be come from this.
4454
				 */
4455 ea51e9f8 Renato Botelho
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4456 197bfe96 Ermal Luçi
				array_pop($path);
4457 420b4538 Renato Botelho
			}
4458 520ad1a4 Ermal
		}
4459 197bfe96 Ermal Luçi
		array_pop($path);
4460
	}
4461
}
4462 0a33f73e Scott Ullrich
4463 c25a6b6a Ermal Luçi
function read_dummynet_config() {
4464
	global $dummynet_pipe_list, $config;
4465
	$path = array();
4466 d62ba478 Ermal Luçi
4467 61e047a5 Phil Davis
	if (!is_array($config['dnshaper'])) {
4468 aa10aadc Ermal Lu?i
		$config['dnshaper'] = array();
4469 61e047a5 Phil Davis
	}
4470
	if (!is_array($config['dnshaper']['queue'])) {
4471 aa10aadc Ermal Lu?i
		$config['dnshaper']['queue'] = array();
4472 61e047a5 Phil Davis
	}
4473 c25a6b6a Ermal Luçi
	$a_int = &$config['dnshaper']['queue'];
4474 2f038c0b Scott Ullrich
4475 c25a6b6a Ermal Luçi
	$dummynet_pipe_list = array();
4476 ce0117f7 Colin Fleming
4477 61e047a5 Phil Davis
	if (!is_array($config['dnshaper']['queue']) ||
4478 4de8f7ba Phil Davis
	    !count($config['dnshaper']['queue'])) {
4479 c25a6b6a Ermal Luçi
		return;
4480 61e047a5 Phil Davis
	}
4481 c25a6b6a Ermal Luçi
4482
	foreach ($a_int as $key => $conf) {
4483 61e047a5 Phil Davis
		if (empty($conf['name'])) {
4484 420b4538 Renato Botelho
			continue; /* XXX: grrrrrr at php */
4485 61e047a5 Phil Davis
		}
4486 23ca5695 Chris Buechler
		$root =& new dnpipe_class();
4487 d62ba478 Ermal Luçi
		$root->ReadConfig($conf);
4488
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4489
		array_push($path, $key);
4490
		$root->SetLink($path);
4491
		if (is_array($conf['queue'])) {
4492
			foreach ($conf['queue'] as $key1 => $q) {
4493
				array_push($path, $key1);
4494 420b4538 Renato Botelho
				/*
4495
				 * XXX: we completely ignore errors here but anyway we must have
4496 d62ba478 Ermal Luçi
				 *	checked them before so no harm should be come from this.
4497 420b4538 Renato Botelho
				 */
4498 ea51e9f8 Renato Botelho
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4499 c25a6b6a Ermal Luçi
				array_pop($path);
4500 420b4538 Renato Botelho
			}
4501 d62ba478 Ermal Luçi
		}
4502
		array_pop($path);
4503 c25a6b6a Ermal Luçi
	}
4504
}
4505 0a33f73e Scott Ullrich
4506 197bfe96 Ermal Luçi
function get_interface_list_to_show() {
4507
	global $altq_list_queues, $config;
4508 057399e4 Ermal Luçi
	global $shaperIFlist;
4509
4510 197bfe96 Ermal Luçi
	$tree = "";
4511 057399e4 Ermal Luçi
	foreach ($shaperIFlist as $shif => $shDescr) {
4512
		if ($altq_list_queues[$shif]) {
4513 197bfe96 Ermal Luçi
			continue;
4514 4de8f7ba Phil Davis
		} else {
4515 61e047a5 Phil Davis
			if (!is_altq_capable(get_real_interface($shif))) {
4516 0b033d22 Ermal Luçi
				continue;
4517 61e047a5 Phil Davis
			}
4518 7da5315d Colin Fleming
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4519 197bfe96 Ermal Luçi
		}
4520
	}
4521 ce0117f7 Colin Fleming
4522 197bfe96 Ermal Luçi
	return $tree;
4523
}
4524 061f78b1 Bill Marquette
4525 197bfe96 Ermal Luçi
function filter_generate_altq_queues() {
4526
	global $altq_list_queues;
4527 ce0117f7 Colin Fleming
4528 197bfe96 Ermal Luçi
	read_altq_config();
4529 061f78b1 Bill Marquette
4530 197bfe96 Ermal Luçi
	$altq_rules = "";
4531 61e047a5 Phil Davis
	foreach ($altq_list_queues as $altq) {
4532 197bfe96 Ermal Luçi
		$altq_rules .= $altq->build_rules();
4533 61e047a5 Phil Davis
	}
4534 0f0c6a9e Scott Ullrich
4535 197bfe96 Ermal Luçi
	return $altq_rules;
4536
}
4537 0f0c6a9e Scott Ullrich
4538 85a236e9 Ermal
function dnqueue_find_nextnumber() {
4539
	global $dummynet_pipe_list;
4540
4541
	$dnused = array();
4542
	if (is_array($dummynet_pipe_list)) {
4543
		foreach ($dummynet_pipe_list as $dn) {
4544
			$tmplist =& $dn->get_queue_list();
4545
			foreach ($tmplist as $qname => $link) {
4546 61e047a5 Phil Davis
				if ($link[0] == "?") {
4547 85a236e9 Ermal
					$dnused[$qname] = substr($link, 1);
4548 61e047a5 Phil Davis
				}
4549 85a236e9 Ermal
			}
4550
		}
4551
	}
4552
4553
	sort($dnused, SORT_NUMERIC);
4554
	$dnnumber = 0;
4555
	$found = false;
4556
	foreach ($dnused as $dnnum) {
4557
		if (($dnnum - $dnnumber) > 1) {
4558 89cf3dc0 Renato Botelho
			$dnnumber = $dnnum - 1;
4559 85a236e9 Ermal
			$found = true;
4560
			break;
4561 61e047a5 Phil Davis
		} else {
4562 85a236e9 Ermal
			$dnnumber = $dnnum;
4563 61e047a5 Phil Davis
		}
4564 85a236e9 Ermal
	}
4565
4566 61e047a5 Phil Davis
	if ($found == false) {
4567 85a236e9 Ermal
		$dnnumber++;
4568 61e047a5 Phil Davis
	}
4569 85a236e9 Ermal
4570
	unset($dnused, $dnnum, $found);
4571
	return $dnnumber;
4572
}
4573
4574
function dnpipe_find_nextnumber() {
4575
	global $dummynet_pipe_list;
4576
4577
	$dnused = array();
4578 61e047a5 Phil Davis
	foreach ($dummynet_pipe_list as $dn) {
4579 85a236e9 Ermal
		$dnused[] = $dn->GetNumber();
4580 61e047a5 Phil Davis
	}
4581 85a236e9 Ermal
4582
	sort($dnused, SORT_NUMERIC);
4583
	$dnnumber = 0;
4584
	$found = false;
4585
	foreach ($dnused as $dnnum) {
4586
		if (($dnnum - $dnnumber) > 1) {
4587 89cf3dc0 Renato Botelho
			$dnnumber = $dnnum - 1;
4588 85a236e9 Ermal
			$found = true;
4589
			break;
4590 61e047a5 Phil Davis
		} else {
4591 85a236e9 Ermal
			$dnnumber = $dnnum;
4592 61e047a5 Phil Davis
		}
4593 85a236e9 Ermal
	}
4594
4595 61e047a5 Phil Davis
	if ($found == false) {
4596 85a236e9 Ermal
		$dnnumber++;
4597 61e047a5 Phil Davis
	}
4598 85a236e9 Ermal
4599
	unset($dnused, $dnnum, $found);
4600
	return $dnnumber;
4601
}
4602
4603 d62ba478 Ermal Luçi
function filter_generate_dummynet_rules() {
4604 d41e63b6 Ermal
	global $g, $dummynet_pipe_list;
4605 ce0117f7 Colin Fleming
4606 c25a6b6a Ermal Luçi
	read_dummynet_config();
4607 ce0117f7 Colin Fleming
4608 c25a6b6a Ermal Luçi
	$dn_rules = "";
4609 fcd9f64c Chris Buechler
	$max_qlimit = "100"; // OS default
4610 61e047a5 Phil Davis
	foreach ($dummynet_pipe_list as $dn) {
4611 c25a6b6a Ermal Luçi
		$dn_rules .= $dn->build_rules();
4612 fcd9f64c Chris Buechler
		$this_qlimit = $dn->GetQlimit();
4613
		if ($this_qlimit > $max_qlimit) {
4614
			$max_qlimit = $this_qlimit;
4615
		}
4616
	}
4617
	if (!is_numericint($max_qlimit)) {
4618
		$max_qlimit = "100";
4619 61e047a5 Phil Davis
	}
4620 e3e5160c Ermal
	if (!empty($dn_rules)) {
4621 1c73248c Ermal
		if (!is_module_loaded("dummynet.ko")) {
4622
			mwexec("/sbin/kldload dummynet");
4623
		}
4624 fcd9f64c Chris Buechler
		set_sysctl(array(
4625
				"net.inet.ip.dummynet.io_fast" => "1",
4626
				"net.inet.ip.dummynet.hash_size" => "256",
4627
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
4628
		));
4629 d41e63b6 Ermal
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4630
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4631
	}
4632 c25a6b6a Ermal Luçi
}
4633 0a33f73e Scott Ullrich
4634 197bfe96 Ermal Luçi
function build_iface_without_this_queue($iface, $qname) {
4635
	global $g, $altq_list_queues;
4636 d34ade52 Michele Di Maria
	global $shaperIFlist;
4637 0a33f73e Scott Ullrich
4638 197bfe96 Ermal Luçi
	$altq =& $altq_list_queues[$iface];
4639 d17c4ee9 Stephen Beaver
4640 61e047a5 Phil Davis
	if ($altq) {
4641 b28e1512 Stephen Beaver
		$scheduler = $altq->GetScheduler();
4642
	}
4643 d17c4ee9 Stephen Beaver
4644 b28e1512 Stephen Beaver
	$form = '<dl class="dl-horizontal">';
4645 d17c4ee9 Stephen Beaver
4646 b28e1512 Stephen Beaver
	$form .= '	<dt>';
4647
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4648
	$form .= '	</dt>';
4649
	$form .= '	<dd>';
4650 d17c4ee9 Stephen Beaver
	$form .=		$scheduler;
4651 b28e1512 Stephen Beaver
	$form .= '	</dd>';
4652 d17c4ee9 Stephen Beaver
4653
	$form .= '	<dt>';
4654 b28e1512 Stephen Beaver
	$form .= 'Clone';
4655
	$form .= '	</dt>';
4656
	$form .= '	<dd>';
4657
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4658
	$form .= $iface . '&amp;queue=';
4659
	$form .= $qname . '&amp;action=add">';
4660 27d6a45b jim-p
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
4661
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
4662 d17c4ee9 Stephen Beaver
	$form .= '	</dd>';
4663 b28e1512 Stephen Beaver
4664
	$form .= '</dl>';
4665 d17c4ee9 Stephen Beaver
4666 dbaf21d4 Renato Botelho
	return $form;
4667 0a33f73e Scott Ullrich
4668 197bfe96 Ermal Luçi
}
4669 0a33f73e Scott Ullrich
4670 6cb05fa7 Colin Fleming
$default_shaper_msg = sprintf(gettext("Welcome to the %s Traffic Shaper."), $g['product_name']) . "<br />";
4671 806942d0 Stephen Beaver
$dn_default_shaper_msg = $default_shaper_msg;
4672 d62ba478 Ermal Luçi
4673 170fbb18 Phil Davis
$shaper_msg = gettext("The tree on the left navigates through the %s.");
4674
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
4675
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
4676
4677
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
4678
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
4679
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
4680
4681 6955830f Ermal Lu?i
?>