Project

General

Profile

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