Project

General

Profile

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