Project

General

Profile

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