Project

General

Profile

Download (119 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * shaper.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * 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
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * 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
 */
25

    
26
/* XXX: needs some reducing on include. */
27
/* include all configuration functions. */
28
require_once("globals.inc");
29
require_once("functions.inc");
30
require_once("util.inc");
31
require_once("notices.inc");
32
include_once("interfaces.inc");//required for convert_real_interface_to_friendly_interface_name() in get_queue_stats() to get altq interface name.
33

    
34
function get_queue_stats() {
35
	// due to sysutils\qstats not providing accurate stats with 'currently' valid numbers 
36
	// in its current implementation this php function does the job for now..
37
	$result = array();
38
	$result['timestamp'] = gettimeofday(true);
39
	$r = exec("/sbin/pfctl -s queue -v", $output, $ret);
40
	$interfacestats = array();
41
	foreach($output as $line) {
42
		$partial = explode('{', $line);
43
		$items = explode(" ", $partial[0]);
44
		if (isset($partial[1])) {
45
			$contains = explode(", ", substr($partial[1], 0, strlen($partial[1]) - 1));
46
		} else {
47
			unset($contains);
48
		}
49
		if ($items[0] == "queue") {
50
			$level = 1;
51
			while (empty($items[$level])) {
52
				$level++;
53
			}
54
			unset($newqueue);
55
			$newqueue = array();
56
			$currentqueuename = $items[$level];
57
			$currentqueueif = $items[$level+2];
58
			$newqueue['name'] = $currentqueuename;
59
			$newqueue['interface'] = $items[$level+2];
60
			
61
			if ($items[$level+3] == "bandwidth") {
62
				$level += 2;
63
			}
64
			if ($items[$level+3] == "priority") {
65
				$level += 2;
66
			}
67
			if ($items[$level+3] == "qlimit") {
68
				$level += 2;
69
			}
70
			$tmp = $items[$level+3];
71
			if (substr($tmp,strlen($tmp)-1) == "(") {
72
				$newqueue['shapertype'] = substr($tmp, 0, strlen($tmp)-1);
73
			}
74
			
75
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
76
			if (is_array($contains)) {
77
				$newqueue['contains'] = $contains;
78
			}
79
		} elseif ($items[0] == "altq") {
80
			unset($newqueue);
81
			$newqueue = array();
82
			$currentqueuename = convert_real_interface_to_friendly_interface_name($items[2]);
83
			$currentqueueif = $items[2];
84
			$newqueue['name'] = $currentqueuename;
85
			$newqueue['interface'] = $items[2];
86
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
87
		} else {
88
			if ($items[3] == "pkts:") {
89
				$newqueue["pkts"] = trim(substr($line, 10, 10));
90
				$newqueue["bytes"] = trim(substr($line, 29, 10));
91
				$newqueue["droppedpkts"] = trim(substr($line, 55, 6));
92
				$newqueue["droppedbytes"] = trim(substr($line, 69, 6));
93
			}
94
			if ($items[3] == "qlength:") {
95
				$subitems = explode("/", substr($line, 13, 8));
96
				$newqueue["qlengthitems"] = trim($subitems[0]);
97
				$newqueue["qlengthsize"] = trim($subitems[1]);
98
				if (substr($line, 22, 8) == "borrows:") {
99
					$newqueue["borrows"] = trim(substr($line, 31, 7));
100
				}
101
				if (substr($line, 39, 9) == "suspends:") {
102
					$newqueue["suspends"] = trim(substr($line, 49, 7));
103
				}
104
			}
105
		}
106
	}
107
	$result['interfacestats'] = $interfacestats;
108
	return $result;
109
}
110

    
111
/*
112
 * I admit :) this is derived from xmlparse.inc StartElement()
113
 */
114
function &get_reference_to_me_in_config(&$mypath) {
115
	global $config;
116

    
117
	$ptr =& $config['shaper'];
118
	foreach ($mypath as $indeks) {
119
		$ptr =& $ptr['queue'][$indeks];
120
	}
121

    
122
	return $ptr;
123
}
124

    
125
function unset_object_by_reference(&$mypath) {
126
	global $config;
127

    
128
	$ptr =& $config['shaper'];
129
	for ($i = 0; $i < count($mypath) - 1; $i++) {
130
		$ptr =& $ptr['queue'][$mypath[$i]];
131
	}
132
	unset($ptr['queue'][$mypath[$i]]);
133
}
134

    
135
function &get_dn_reference_to_me_in_config(&$mypath) {
136
	global $config;
137

    
138
	$ptr =& $config['dnshaper'];
139
	foreach ($mypath as $indeks) {
140
		$ptr =& $ptr['queue'][$indeks];
141
	}
142

    
143
	return $ptr;
144
}
145

    
146
function unset_dn_object_by_reference(&$mypath) {
147
	global $config;
148

    
149
	$ptr =& $config['dnshaper'];
150
	for ($i = 0; $i < count($mypath) - 1; $i++) {
151
		$ptr =& $ptr['queue'][$mypath[$i]];
152
	}
153
	unset($ptr['queue'][$mypath[$i]]);
154
}
155

    
156
function clean_child_queues($type, $mypath) {
157
	$ref = &get_reference_to_me_in_config($mypath);
158

    
159
	switch ($type) {
160
		case 'HFSC':
161
			if (isset($ref['borrow'])) {
162
				unset($ref['borrow']);
163
			}
164
			if (isset($ref['hogs'])) {
165
				unset($ref['hogs']);
166
			}
167
			if (isset($ref['buckets'])) {
168
				unset($ref['buckets']);
169
			}
170
			break;
171
		case 'PRIQ':
172
			if (isset($ref['borrow'])) {
173
				unset($ref['borrow']);
174
			}
175
			if (isset($ref['bandwidth'])) {
176
				unset($ref['bandwidth']);
177
			}
178
			if (isset($ref['bandwidthtype'])) {
179
				unset($ref['bandwidthtype']);
180
			}
181
			/* fall through */
182
		case 'FAIRQ':
183
			if (isset($ref['borrow'])) {
184
				unset($ref['borrow']);
185
			}
186
			/* fall through */
187
		case 'CBQ':
188
			if (isset($ref['realtime'])) {
189
				unset($ref['realtime']);
190
			}
191
			if (isset($ref['realtime1'])) {
192
				unset($ref['realtime1']);
193
			}
194
			if (isset($ref['realtime2'])) {
195
				unset($ref['realtime2']);
196
			}
197
			if (isset($ref['realtime3'])) {
198
				unset($ref['realtime3']);
199
			}
200
			if (isset($ref['upperlimit'])) {
201
				unset($ref['upperlimit']);
202
			}
203
			if (isset($ref['upperlimit1'])) {
204
				unset($ref['upperlimit1']);
205
			}
206
			if (isset($ref['upperlimit2'])) {
207
				unset($ref['upperlimit2']);
208
			}
209
			if (isset($ref['upperlimit3'])) {
210
				unset($ref['upperlimit3']);
211
			}
212
			if (isset($ref['linkshare'])) {
213
				unset($ref['linkshare']);
214
			}
215
			if (isset($ref['linkshare1'])) {
216
				unset($ref['linkshare1']);
217
			}
218
			if (isset($ref['linkshare2'])) {
219
				unset($ref['linkshare2']);
220
			}
221
			if (isset($ref['linkshare3'])) {
222
				unset($ref['linkshare3']);
223
			}
224
			if (isset($ref['hogs'])) {
225
				unset($ref['hogs']);
226
			}
227
			if (isset($ref['buckets'])) {
228
				unset($ref['buckets']);
229
			}
230
			break;
231
	}
232
}
233

    
234
function get_bandwidthtype_scale($type) {
235
	switch ($type) {
236
		case "Gb":
237
			$factor = 1024 * 1024 * 1024;
238
			break;
239
		case "Mb":
240
			$factor = 1024 * 1024;
241
			break;
242
		case "Kb":
243
			$factor = 1024;
244
			break;
245
		case "b":
246
		default:
247
			$factor = 1;
248
			break;
249
	}
250
	return intval($factor);
251
}
252

    
253
function get_bandwidth($bw, $scale, $obj) {
254

    
255
	$pattern= "/(b|Kb|Mb|Gb|%)/";
256
	if (!preg_match($pattern, $scale, $match))
257
		return 0;
258

    
259
	switch ($match[1]) {
260
		case '%':
261
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
262
			break;
263
		default:
264
			$objbw = $bw * get_bandwidthtype_scale($scale);
265
			break;
266
	}
267

    
268
	return floatval($objbw);
269
}
270

    
271
/*
272
 * XXX - unused
273
 *
274
function get_hfsc_bandwidth($object, $bw) {
275
	$pattern= "/[0-9]+/";
276
	if (preg_match($pattern, $bw, $match)) {
277
		$bw_1 = $match[1];
278
	} else {
279
		return 0;
280
	}
281
	$pattern= "/(b|Kb|Mb|Gb|%)/";
282
	if (preg_match($pattern, $bw, $match)) {
283
		switch ($match[1]) {
284
			case '%':
285
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
286
				break;
287
			default:
288
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
289
				break;
290
		}
291
		return floatval($bw_1);
292
	} else {
293
		return 0;
294
	}
295
}
296
*/
297

    
298
function get_queue_bandwidth($obj) {
299
	$bw = $obj->GetBandwidth();
300
	$scale = $obj->GetBwscale();
301

    
302
	$pattern= "/(b|Kb|Mb|Gb|%)/";
303
	if (!preg_match($pattern, $scale, $match))
304
		return 0;
305

    
306
	switch ($match[1]) {
307
		case '%':
308
			$objbw = ($bw / 100) * get_queue_bandwidth($obj->GetParent());
309
			break;
310
		default:
311
			$objbw = $bw * get_bandwidthtype_scale($scale);
312
			break;
313
	}
314

    
315
	return floatval($objbw);
316
}
317

    
318
function get_interface_bandwidth($object) {
319
	global $altq_list_queues;
320

    
321
	$int = $object->GetInterface();
322
	$altq =& $altq_list_queues[$int];
323
	if ($altq) {
324
		$bw_3 = $altq->GetBandwidth();
325
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
326
		return floatval($bw_3);
327
	} else {
328
		return 0;
329
	}
330
}
331

    
332
/*
333
 * This is duplicated here since we cannot include guiconfig.inc.
334
 * Including it makes all stuff break.
335
 */
336
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
337

    
338
	/* check for bad control characters */
339
	foreach ($postdata as $pn => $pd) {
340
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
341
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
342
		}
343
	}
344

    
345
	for ($i = 0; $i < count($reqdfields); $i++) {
346
		if ($postdata[$reqdfields[$i]] == "") {
347
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
348
		}
349
	}
350
}
351

    
352
function cleanup_queue_from_rules($queue) {
353
	global $config;
354

    
355
	foreach ($config['filter']['rule'] as $rule) {
356
		if ($rule['defaultqueue'] == $queue) {
357
			unset($rule['defaultqueue']);
358
		}
359
		if ($rule['ackqueue'] == $queue) {
360
			unset($rule['ackqueue']);
361
		}
362
	}
363
}
364

    
365
function cleanup_dnqueue_from_rules($queue) {
366
	global $config;
367

    
368
	foreach ($config['filter']['rule'] as $rule) {
369
		if ($rule['dnpipe'] == $queue) {
370
			unset($rule['dnpipe']);
371
		}
372
		if ($rule['pdnpipe'] == $queue) {
373
			unset($rule['pdnpipe']);
374
		}
375
	}
376
}
377

    
378
class altq_root_queue {
379
	var $interface;
380
	var $tbrconfig ;
381
	var $bandwidth;
382
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
383
	var $scheduler;
384
	var $qlimit;
385
	var $queues = array();
386
	var $qenabled = false;
387
	var $link;
388

    
389
	/* Accessor functions */
390
	function GetDefaultQueuePresent() {
391
		if (!empty($this->queues)) {
392
			foreach ($this->queues as $q) {
393
				if ($q->GetDefault()) {
394
					return true;
395
				}
396
			}
397
		}
398

    
399
		return false;
400
	}
401
	function SetLink($link) {
402
		$this->link = $link;
403
	}
404
	function GetLink() {
405
		return $this->link;
406
	}
407
	function GetEnabled() {
408
		return $this->qenabled;
409
	}
410
	function SetEnabled($value) {
411
		$this->qenabled = $value;
412
	}
413
	function CanHaveChildren() {
414
		if ($this->GetScheduler() == "CODELQ") {
415
			return false;
416
		} else {
417
			return true;
418
		}
419
	}
420
	function CanBeDeleted() {
421
		return false;
422
	}
423
	function GetQname() {
424
		return $this->interface;
425
	}
426
	function SetQname($name) {
427
		$this->interface = trim($name);
428
	}
429
	function GetInterface() {
430
		return $this->interface;
431
	}
432
	function SetInterface($name) {
433
		$this->interface = trim($name);
434
	}
435
	function GetTbrConfig() {
436
		return $this->tbrconfig;
437
	}
438
	function SetTbrConfig($tbrconfig) {
439
		$this->tbrconfig = $tbrconfig;
440
	}
441
	function GetBandwidth() {
442
		return $this->bandwidth;
443
	}
444
	function SetBandwidth($bw) {
445
		$this->bandwidth = $bw;
446
	}
447
	function GetBwscale() {
448
		return $this->bandwidthtype;
449
	}
450
	function SetBwscale($bwscale) {
451
		$this->bandwidthtype = $bwscale;
452
	}
453
	function GetScheduler() {
454
		return $this->scheduler;
455
	}
456
	function SetScheduler($scheduler) {
457
		$this->scheduler = trim($scheduler);
458
	}
459
	function GetQlimit() {
460
		return $this->qlimit;
461
	}
462
	function SetQlimit($limit) {
463
		$this->qlimit = $limit;
464
	}
465

    
466
	function GetBwscaleText() {
467
		switch ($this->bandwidthtype) {
468
			case "b":
469
				$bwscaletext = "Bit/s";
470
				break;
471
			case "Kb":
472
				$bwscaletext = "Kbit/s";
473
				break;
474
			case "Mb":
475
				$bwscaletext = "Mbit/s";
476
				break;
477
			case "Gb":
478
				$bwscaletext = "Gbit/s";
479
				break;
480
			default:
481
				/* For others that do not need translating like % */
482
				$bwscaletext = $this->bandwidthtype;
483
				break;
484
		}
485
		return $bwscaletext;
486
	}
487

    
488
	function CheckBandwidth($bw, $bwtype) {
489
		$sum = $this->GetTotalBw();
490
		if ($sum > $bw * get_bandwidthtype_scale($bwtype))
491
			return 1;
492
		foreach ($this->queues as $q) {
493
			if ($q->CheckBandwidth(0, ''))
494
				return 1;
495
		}
496

    
497
		return 0;
498
	}
499

    
500
	function GetTotalBw($qignore = NULL) {
501
		$sum = 0;
502
		foreach ($this->queues as $q) {
503
			if ($qignore != NULL && $qignore == $q)
504
				continue;
505
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
506
		}
507

    
508
		return $sum;
509
	}
510

    
511
	function validate_input($data, &$input_errors) {
512

    
513
		$reqdfields[] = "bandwidth";
514
		$reqdfieldsn[] = gettext("Bandwidth");
515
		$reqdfields[] = "bandwidthtype";
516
		$reqdfieldsn[] = gettext("Bandwidthtype");
517

    
518
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
519

    
520
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
521
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
522
		}
523
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
524
			$input_errors[] = gettext("Bandwidth must be an integer.");
525
		}
526
		if ($data['bandwidth'] < 0) {
527
			$input_errors[] = gettext("Bandwidth cannot be negative.");
528
		}
529
		if ($data['bandwidthtype'] == "%") {
530
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
531
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
532
			}
533
		}
534
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
535
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
536

    
537
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
538
			$input_errors[] = gettext("Qlimit must be an integer.");
539
		}
540
		if ($data['qlimit'] < 0) {
541
			$input_errors[] = gettext("Qlimit must be positive.");
542
		}
543
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
544
			$input_errors[] = gettext("Tbrsize must be an integer.");
545
		}
546
		if ($data['tbrconfig'] < 0) {
547
			$input_errors[] = gettext("Tbrsize must be positive.");
548
		}
549
	}
550

    
551
	/* Implement this to shorten some code on the frontend page */
552
	function ReadConfig(&$conf) {
553
		if (isset($conf['tbrconfig'])) {
554
			$this->SetTbrConfig($conf['tbrconfig']);
555
		} else {
556
			$this->SetTbrConfig($conf['tbrconfig']);
557
		}
558
		$this->SetBandwidth($conf['bandwidth']);
559
		if ($conf['bandwidthtype'] <> "") {
560
			$this->SetBwscale($conf['bandwidthtype']);
561
		}
562
		if (isset($conf['scheduler'])) {
563
			if ($this->GetScheduler() != $conf['scheduler']) {
564
				foreach ($this->queues as $q) {
565
					clean_child_queues($conf['scheduler'], $this->GetLink());
566
					$q->clean_queue($conf['scheduler']);
567
				}
568
			}
569
			$this->SetScheduler($conf['scheduler']);
570
		}
571
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
572
			$this->SetQlimit($conf['qlimit']);
573
		} else {
574
			$this->SetQlimit("");
575
		}
576
		if (isset($conf['name'])) {
577
			$this->SetQname($conf['name']);
578
		}
579
		if (!empty($conf['enabled'])) {
580
			$this->SetEnabled($conf['enabled']);
581
		} else {
582
			$this->SetEnabled("");
583
		}
584
	}
585

    
586
	function copy_queue($interface, &$cflink) {
587
		$cflink['interface'] = $interface;
588
		$cflink['name'] = $interface;
589
		$cflink['scheduler'] = $this->GetScheduler();
590
		$cflink['bandwidth'] = $this->GetBandwidth();
591
		$cflink['bandwidthtype'] = $this->GetBwscale();
592
		$cflink['qlimit'] = $this->GetQlimit();
593
		$cflink['tbrconfig'] = $this->GetTbrConfig();
594
		$cflink['enabled'] = $this->GetEnabled();
595
		if (is_array($this->queues)) {
596
			$cflink['queue'] = array();
597
			foreach ($this->queues as $q) {
598
				$cflink['queue'][$q->GetQname()] = array();
599
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
600
			}
601
		}
602
	}
603

    
604
	function &get_queue_list(&$q = null) {
605
		$qlist = array();
606

    
607
		//$qlist[$this->GetQname()] = & $this;
608
		if (is_array($this->queues)) {
609
			foreach ($this->queues as $queue) {
610
				$queue->get_queue_list($qlist);
611
			}
612
		}
613
		return $qlist;
614
	}
615

    
616
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
617

    
618
		if (!is_array($this->queues)) {
619
			$this->queues = array();
620
		}
621

    
622
		switch ($this->GetScheduler()) {
623
			case "PRIQ":
624
				$q =& new priq_queue();
625
				break;
626
			case "HFSC":
627
				$q =& new hfsc_queue();
628
				break;
629
			case "CBQ":
630
				$q =& new cbq_queue();
631
				break;
632
			case "FAIRQ":
633
				$q =& new fairq_queue();
634
				break;
635
			default:
636
				/* XXX: but should not happen anyway */
637
				return;
638
				break;
639
		}
640
		$q->SetLink($path);
641
		$q->SetInterface($this->GetInterface());
642
		$q->SetEnabled("on");
643
		$q->SetParent($this);
644
		$q->ReadConfig($queue);
645
		$q->validate_input($queue, $input_errors);
646

    
647
		$this->queues[$q->GetQname()] = &$q;
648
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
649
		if (is_array($queue['queue'])) {
650
			foreach ($queue['queue'] as $key1 => $que) {
651
				array_push($path, $key1);
652
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
653
				array_pop($path);
654
			}
655
		}
656

    
657
		return $q;
658
	}
659

    
660
	/* interface here might be optional */
661
	function &find_queue($interface, $qname) {
662
		if ($qname == $this->GetQname()) {
663
			return $this;
664
		}
665
		foreach ($this->queues as $q) {
666
			$result =& $q->find_queue("", $qname);
667
			if ($result) {
668
				return $result;
669
			}
670
		}
671
	}
672

    
673
	function &find_parentqueue($interface, $qname) {
674
		if ($qname == $interface) {
675
			$result = NULL;
676
		} else if ($this->queues[$qname]) {
677
			$result = $this;
678
		} else if ($this->GetScheduler() <> "PRIQ") {
679
			foreach ($this->queues as $q) {
680
				$result = $q->find_parentqueue("", $qname);
681
				if ($result) {
682
					return $result;
683
				}
684
			}
685
		}
686
	}
687

    
688
	function build_tree() {
689
		global $shaperIFlist;
690

    
691
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
692
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
693
		if (is_array($this->queues)) {
694
			$tree .= "<ul>";
695
			foreach ($this->queues as $q) {
696
				$tree .= $q->build_tree();
697
			}
698
			$tree .= "</ul>";
699
		}
700
		$tree .= "</li>";
701
		return $tree;
702
	}
703

    
704
	function delete_queue() {
705
		foreach ($this->queues as $q)
706
			$q->delete_queue();
707
		unset_object_by_reference($this->GetLink());
708
	}
709

    
710
	function delete_all() {
711
		if (count($this->queues)) {
712
			foreach ($this->queues as $q) {
713
				$q->delete_all();
714
				unset_object_by_reference($q->GetLink());
715
				unset($q);
716
			}
717
			unset($this->queues);
718
		}
719
	}
720

    
721
	/*
722
	 * First it spits:
723
	 * altq on $interface ..............
724
	 *	then it goes like
725
	 *	foreach ($queues as $qkey => $queue) {
726
	 *		this->queues[$qkey]->build_rule();
727
	 *	}
728
	 */
729
	function build_rules(&$default = false) {
730
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
731
			$default = false;
732
			$rules = " altq on " . get_real_interface($this->GetInterface());
733
			if ($this->GetScheduler()) {
734
				$rules .= " ".strtolower($this->GetScheduler());
735
			}
736
			if ($this->GetQlimit() > 0) {
737
				$rules .= " qlimit " . $this->GetQlimit() . " ";
738
			}
739
			if ($this->GetBandwidth()) {
740
				$rules .= " bandwidth ".trim($this->GetBandwidth());
741
				if ($this->GetBwscale()) {
742
					$rules .= $this->GetBwscale();
743
				}
744
			}
745
			if ($this->GetTbrConfig()) {
746
				$rules .= " tbrsize ".$this->GetTbrConfig();
747
			}
748
			if (count($this->queues)) {
749
				$i = count($this->queues);
750
				$rules .= " queue { ";
751
				foreach ($this->queues as $qkey => $qnone) {
752
					if ($i > 1) {
753
						$i--;
754
						$rules .= " {$qkey}, ";
755
					} else {
756
						$rules .= " {$qkey} ";
757
					}
758
				}
759
				$rules .= " } \n";
760
				foreach ($this->queues as $q) {
761
					$rules .= $q->build_rules($default);
762
				}
763
			}
764

    
765
			if ($default == false) {
766
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
767
				file_notice("Shaper", $error, "Error occurred", "");
768
				unset($error);
769
				return "\n";
770
			}
771
			$frule .= $rules;
772
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
773
			$rules = " altq on " . get_real_interface($this->GetInterface());
774
			if ($this->GetScheduler()) {
775
				$rules .= " ".strtolower($this->GetScheduler());
776
			}
777
			if ($this->GetQlimit() > 0) {
778
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
779
			}
780
			if ($this->GetBandwidth()) {
781
				$rules .= " bandwidth ".trim($this->GetBandwidth());
782
				if ($this->GetBwscale()) {
783
					$rules .= $this->GetBwscale();
784
				}
785
			}
786
			if ($this->GetTbrConfig()) {
787
				$rules .= " tbrsize ".$this->GetTbrConfig();
788
			}
789

    
790
			$rules .= " queue";
791
		}
792

    
793
		$rules .= " \n";
794
		return $rules;
795
	}
796

    
797
	function build_javascript() {
798
		$javascript = "<script type=\"text/javascript\">";
799
		$javascript .= "//<![CDATA[\n";
800
		$javascript .= "function mySuspend() {";
801
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
802
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
803
		$javascript .= "else if (document.all)";
804
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
805
		$javascript .= "}";
806

    
807
		$javascript .= "function myResume() {";
808
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
809
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
810
		$javascript .= "else if (document.all) ";
811
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
812
		$javascript .= "}";
813
		$javascript .= "//]]>";
814
		$javascript .= "</script>";
815

    
816
		return $javascript;
817
	}
818

    
819
	function build_shortform() {
820
		global $g;
821

    
822
		$altq =& $this;
823

    
824
		if ($altq) {
825
			$scheduler = ": " . $altq->GetScheduler();
826
		}
827

    
828
		$form = '<dl class="dl-horizontal">';
829
		$form .= '	<dt>';
830
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
831
		$form .= '	</dt>';
832
		$form .= '	<dd>';
833
		$form .=		$scheduler;
834
		$form .= '	</dd>';
835

    
836
		$form .= '	<dt>';
837
		$form .=		'Bandwidth';
838
		$form .= '	</dt>';
839
		$form .= '	<dd>';
840
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
841
		$form .= '	</dd>';
842

    
843
		$form .= '	<dt>';
844
		$form .= 'Disable';
845
		$form .= '	<dt>';
846
		$form .= '	<dd>';
847

    
848
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
849
		$form .= $this->GetInterface() . '&amp;queue=';
850
		$form .= $this->GetQname() . '&amp;action=delete">';
851
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
852
		$form .= gettext("Remove shaper from this interface") . '</a>';
853

    
854
		$form .= '	</dd>';
855

    
856
		$form .= '</dl>';
857

    
858
		return $form;
859

    
860
	}
861

    
862
	/*
863
	 * For requesting the parameters of the root queues
864
	 * to the user like the traffic wizard does.
865
	 */
866
	function build_form() {
867

    
868
		$sform = new Form();
869

    
870
		$sform->setAction("firewall_shaper.php");
871

    
872
		$section = new Form_Section(null);
873

    
874
		$section->addInput(new Form_Checkbox(
875
			'enabled',
876
			'Enable/Disable',
877
			'Enable/disable discipline and its children',
878
			($this->GetEnabled() == "on"),
879
			'on'
880
		));
881

    
882
		$section->addInput(new Form_StaticText(
883
			'*Name',
884
			$this->GetQname()
885
		));
886

    
887
		$section->addInput(new Form_Select(
888
			'scheduler',
889
			'Scheduler Type',
890
			$this->GetScheduler(),
891
			array('HFSC' => 'HFSC',
892
				  'CBQ' => 'CBQ',
893
				  'FAIRQ' => 'FAIRQ',
894
				  'CODELQ' => 'CODELQ',
895
				  'PRIQ' => 'PRIQ')
896
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
897

    
898
		$group = new Form_group('Bandwidth');
899

    
900
		$group->add(new Form_Input(
901
			'bandwidth',
902
			null,
903
			'number',
904
			$this->GetBandwidth()
905
		));
906

    
907
		$group->add(new Form_Select(
908
			'bandwidthtype',
909
			null,
910
			$this->GetBwscale(),
911
			array('Kb' => 'Kbit/s',
912
				  'Mb' => 'Mbit/s',
913
				  'Gb' => 'Gbit/s',
914
				  'b' => 'Bit/s',
915
				  '%' => '%')
916
		));
917

    
918
		$section->add($group);
919

    
920
		$section->addInput(new Form_Input(
921
			'qlimit',
922
			'Queue Limit',
923
			'number',
924
			$this->GetQlimit()
925
		));
926

    
927
		$section->addInput(new Form_Input(
928
			'tbrconfig',
929
			'TBR Size',
930
			'number',
931
			$this->GetTbrConfig()
932
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
933
					'bandwidth are used to determine the size.');
934

    
935
		$section->addInput(new Form_Input(
936
			'interface',
937
			null,
938
			'hidden',
939
			$this->GetInterface()
940
		));
941

    
942
		$section->addInput(new Form_Input(
943
			'name',
944
			null,
945
			'hidden',
946
			$this->GetQname()
947
		));
948

    
949
		$sform->add($section);
950

    
951
		return($sform);
952
	}
953

    
954
	function update_altq_queue_data(&$data) {
955
		$this->ReadConfig($data);
956
	}
957

    
958
	/*
959
	 * Should call on each of it queues and subqueues
960
	 * the same function much like build_rules();
961
	 */
962
	function wconfig() {
963
		$cflink = &get_reference_to_me_in_config($this->GetLink());
964
		if (!is_array($cflink)) {
965
			$cflink = array();
966
		}
967
		$cflink['interface'] = $this->GetInterface();
968
		$cflink['name'] = $this->GetQname();
969
		$cflink['scheduler'] = $this->GetScheduler();
970
		$cflink['bandwidth'] = $this->GetBandwidth();
971
		$cflink['bandwidthtype'] = $this->GetBwscale();
972
		$cflink['qlimit'] = trim($this->GetQlimit());
973
		if (empty($cflink['qlimit'])) {
974
			unset($cflink['qlimit']);
975
		}
976
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
977
		if (empty($cflink['tbrconfig'])) {
978
			unset($cflink['tbrconfig']);
979
		}
980
		$cflink['enabled'] = $this->GetEnabled();
981
		if (empty($cflink['enabled'])) {
982
			unset($cflink['enabled']);
983
		}
984
	}
985

    
986
}
987

    
988
class priq_queue {
989
	var $qname;
990
	var $qinterface;
991
	var $qlimit;
992
	var $qpriority;
993
	var $description;
994
	var $isparent;
995
	var $qbandwidth;
996
	var $qbandwidthtype;
997
	var $qdefault = "";
998
	var $qrio = "";
999
	var $qred = "";
1000
	var $qcodel = "";
1001
	var $qecn = "";
1002
	var $qack;
1003
	var $qenabled = "";
1004
	var $qparent;
1005
	var $link;
1006

    
1007
	/* This is here to help with form building and building rules/lists */
1008
	var $subqueues = array();
1009

    
1010
	/* Accessor functions */
1011
	function SetLink($link) {
1012
		$this->link = $link;
1013
	}
1014
	function GetLink() {
1015
		return $this->link;
1016
	}
1017
	function &GetParent() {
1018
		return $this->qparent;
1019
	}
1020
	function SetParent(&$parent) {
1021
		$this->qparent = &$parent;
1022
	}
1023
	function GetEnabled() {
1024
		return $this->qenabled;
1025
	}
1026
	function SetEnabled($value) {
1027
		$this->qenabled = $value;
1028
	}
1029
	function CanHaveChildren() {
1030
		return false;
1031
	}
1032
	function CanBeDeleted() {
1033
		return true;
1034
	}
1035
	function GetQname() {
1036
		return $this->qname;
1037
	}
1038
	function SetQname($name) {
1039
		$this->qname = trim($name);
1040
	}
1041
	function GetBandwidth() {
1042
		return $this->qbandwidth;
1043
	}
1044
	function SetBandwidth($bandwidth) {
1045
		$this->qbandwidth = $bandwidth;
1046
	}
1047
	function GetInterface() {
1048
		return $this->qinterface;
1049
	}
1050
	function SetInterface($name) {
1051
		$this->qinterface = trim($name);
1052
	}
1053
	function GetQlimit() {
1054
		return $this->qlimit;
1055
	}
1056
	function SetQlimit($limit) {
1057
		$this->qlimit = $limit;
1058
	}
1059
	function GetQpriority() {
1060
		return $this->qpriority;
1061
	}
1062
	function SetQpriority($priority) {
1063
		$this->qpriority = $priority;
1064
	}
1065
	function GetDescription() {
1066
		return $this->description;
1067
	}
1068
	function SetDescription($str) {
1069
		$this->description = trim($str);
1070
	}
1071
	function GetFirstime() {
1072
		return $this->firsttime;
1073
	}
1074
	function SetFirsttime($number) {
1075
		$this->firsttime = $number;
1076
	}
1077
	function CheckBandwidth($bw, $bwtype) {
1078
		$parent = $this->GetParent();
1079

    
1080
		// If the child queues have bandwidth specified in %, there is no point trying
1081
		// to compare the total to the parent's bandwidth, which is defined in Kb/S or Mb/S etc.
1082
		// ToDo: Add check for total % > 100
1083
		//		 If one child is in %, they must all be
1084

    
1085
		if ($bwtype != "%") {
1086
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1087

    
1088
			if ($bw > 0) {
1089
				$sum += get_bandwidth($bw, $bwtype, $parent);
1090
			}
1091

    
1092
			if ($sum > get_queue_bandwidth($parent)) {
1093
				return 1;
1094
			}
1095
		}
1096

    
1097
		foreach ($this->subqueues as $q) {
1098
			if ($q->CheckBandwidth(0, '')) {
1099
				return 1;
1100
			}
1101
		}
1102

    
1103
		return 0;
1104
	}
1105
	function GetTotalBw($qignore = NULL) {
1106
		$sum = 0;
1107
		foreach ($this->subqueues as $q) {
1108
			if ($qignore != NULL && $qignore == $q)
1109
				continue;
1110
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1111
		}
1112

    
1113
		return $sum;
1114
	}
1115
	function GetBwscale() {
1116
		return $this->qbandwidthtype;
1117
	}
1118
	function SetBwscale($scale) {
1119
		$this->qbandwidthtype = $scale;
1120
	}
1121
	function GetDefaultQueuePresent() {
1122
		if ($this->GetDefault()) {
1123
			return true;
1124
		}
1125
		if (!empty($this->subqueues)) {
1126
			foreach ($this->subqueues as $q) {
1127
				if ($q->GetDefault()) {
1128
					return true;
1129
				}
1130
			}
1131
		}
1132

    
1133
		return false;
1134
	}
1135
	function GetDefault() {
1136
		return $this->qdefault;
1137
	}
1138
	function SetDefault($value = false) {
1139
		$this->qdefault = $value;
1140
	}
1141
	function GetCodel() {
1142
		return $this->codel;
1143
	}
1144
	function SetCodel($codel = false) {
1145
		$this->codel = $codel;
1146
	}
1147
	function GetRed() {
1148
		return $this->qred;
1149
	}
1150
	function SetRed($red = false) {
1151
		$this->qred = $red;
1152
	}
1153
	function GetRio() {
1154
		return $this->qrio;
1155
	}
1156
	function SetRio($rio = false) {
1157
		$this->qrio = $rio;
1158
	}
1159
	function GetEcn() {
1160
		return $this->qecn;
1161
	}
1162
	function SetEcn($ecn = false) {
1163
		$this->qecn = $ecn;
1164
	}
1165
	function GetAck() {
1166
		return $this->qack;
1167
	}
1168
	function SetAck($ack = false) {
1169
		$this->qack = $ack;
1170
	}
1171

    
1172
	function GetBwscaleText() {
1173
		switch ($this->qbandwidthtype) {
1174
			case "b":
1175
				$bwscaletext = "Bit/s";
1176
				break;
1177
			case "Kb":
1178
				$bwscaletext = "Kbit/s";
1179
				break;
1180
			case "Mb":
1181
				$bwscaletext = "Mbit/s";
1182
				break;
1183
			case "Gb":
1184
				$bwscaletext = "Gbit/s";
1185
				break;
1186
			default:
1187
				/* For others that do not need translating like % */
1188
				$bwscaletext = $this->qbandwidthtype;
1189
				break;
1190
		}
1191
		return $bwscaletext;
1192
	}
1193

    
1194
	function build_javascript() {
1195
		$javascript = "<script type=\"text/javascript\">";
1196
		$javascript .= "//<![CDATA[\n";
1197
		$javascript .= "function mySuspend() { \n";
1198
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1199
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1200
		$javascript .= "else if (document.all)\n";
1201
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1202
		$javascript .= "}\n";
1203

    
1204
		$javascript .= "function myResume() {\n";
1205
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1206
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1207
		$javascript .= "else if (document.all)\n";
1208
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1209
		$javascript .= "}\n";
1210
		$javascript .= "//]]>";
1211
		$javascript .= "</script>";
1212

    
1213
		return $javascript;
1214
	}
1215

    
1216
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1217

    
1218
	/*
1219
	 * Currently this will not be called unless we decide to clone a whole
1220
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1221
	 */
1222
	function copy_queue($interface, &$cflink) {
1223

    
1224
		$cflink['name'] = $this->GetQname();
1225
		$cflink['interface'] = $interface;
1226
		$cflink['qlimit'] = $this->GetQlimit();
1227
		$cflink['priority'] = $this->GetQpriority();
1228
		$cflink['description'] = $this->GetDescription();
1229
		$cflink['enabled'] = $this->GetEnabled();
1230
		$cflink['default'] = $this->GetDefault();
1231
		$cflink['red'] = $this->GetRed();
1232
		$cflink['codel'] = $this->GetCodel();
1233
		$cflink['rio'] = $this->GetRio();
1234
		$cflink['ecn'] = $this->GetEcn();
1235

    
1236
		if (is_array($this->subqueues)) {
1237
			$cflinkp['queue'] = array();
1238
			foreach ($this->subqueues as $q) {
1239
				$cflink['queue'][$q->GetQname()] = array();
1240
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1241
			}
1242
		}
1243
	}
1244

    
1245
	function clean_queue($sched) {
1246
		clean_child_queues($sched, $this->GetLink());
1247
		if (is_array($this->subqueues)) {
1248
			foreach ($this->subqueues as $q) {
1249
				$q->clean_queue($sched);
1250
			}
1251
		}
1252
	}
1253

    
1254
	function &get_queue_list(&$qlist) {
1255

    
1256
		$qlist[$this->GetQname()] = & $this;
1257
		if (is_array($this->subqueues)) {
1258
			foreach ($this->subqueues as $queue) {
1259
				$queue->get_queue_list($qlist);
1260
			}
1261
		}
1262
	}
1263

    
1264
	function delete_queue() {
1265
		unref_on_altq_queue_list($this->GetQname());
1266
		cleanup_queue_from_rules($this->GetQname());
1267
		unset_object_by_reference($this->GetLink());
1268
	}
1269

    
1270
	function delete_all() {
1271
		if (count($this->subqueues)) {
1272
			foreach ($this->subqueues as $q) {
1273
				$q->delete_all();
1274
				unset_object_by_reference($q->GetLink());
1275
				unset($q);
1276
			}
1277
			unset($this->subqueues);
1278
		}
1279
	}
1280

    
1281
	function &find_queue($interface, $qname) {
1282
		if ($qname == $this->GetQname()) {
1283
			return $this;
1284
		}
1285
	}
1286

    
1287
	function find_parentqueue($interface, $qname) { return; }
1288

    
1289
	function validate_input($data, &$input_errors) {
1290

    
1291
		$reqdfields[] = "name";
1292
		$reqdfieldsn[] = gettext("Name");
1293
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1294

    
1295
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1296
			$input_errors[] = gettext("Bandwidth must be an integer.");
1297
		}
1298
		if ($data['bandwidth'] < 0) {
1299
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1300
		}
1301
		if ($data['bandwidthtype'] == "%") {
1302
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1303
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1304
			}
1305
		}
1306
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1307
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1308

    
1309
		if (isset($data['priority']) && (!is_numeric($data['priority']) ||
1310
		    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1311
			$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1312
		}
1313
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1314
				$input_errors[] = gettext("Queue limit must be an integer");
1315
		}
1316
		if ($data['qlimit'] < 0) {
1317
				$input_errors[] = gettext("Queue limit must be positive");
1318
		}
1319
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1320
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1321
		}
1322
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1323
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1324
		}
1325
		$default = $this->GetDefault();
1326
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1327
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1328
		}
1329
	}
1330

    
1331
	function ReadConfig(&$q) {
1332
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1333
			$this->SetQname($q['newname']);
1334
		} else if (!empty($q['newname'])) {
1335
			$this->SetQname($q['newname']);
1336
		} else if (isset($q['name'])) {
1337
			$this->SetQname($q['name']);
1338
		}
1339
		if (isset($q['interface'])) {
1340
			$this->SetInterface($q['interface']);
1341
		}
1342
		$this->SetBandwidth($q['bandwidth']);
1343
		if ($q['bandwidthtype'] <> "") {
1344
			$this->SetBwscale($q['bandwidthtype']);
1345
		}
1346
		if (!empty($q['qlimit'])) {
1347
			$this->SetQlimit($q['qlimit']);
1348
		} else {
1349
			$this->SetQlimit(""); // Default
1350
		}
1351
		if (is_numeric($q['priority'])) {
1352
			$this->SetQPriority($q['priority']);
1353
		} else {
1354
			$this->SetQpriority("");
1355
		}
1356
		if (!empty($q['description'])) {
1357
			$this->SetDescription($q['description']);
1358
		} else {
1359
			$this->SetDescription("");
1360
		}
1361
		if (!empty($q['red'])) {
1362
			$this->SetRed($q['red']);
1363
		} else {
1364
			$this->SetRed();
1365
		}
1366
		if (!empty($q['codel'])) {
1367
			$this->SetCodel($q['codel']);
1368
		} else {
1369
			$this->SetCodel();
1370
		}
1371
		if (!empty($q['rio'])) {
1372
			$this->SetRio($q['rio']);
1373
		} else {
1374
			$this->SetRio();
1375
		}
1376
		if (!empty($q['ecn'])) {
1377
			$this->SetEcn($q['ecn']);
1378
		} else {
1379
			$this->SetEcn();
1380
		}
1381
		if (!empty($q['default'])) {
1382
			$this->SetDefault($q['default']);
1383
		} else {
1384
			$this->SetDefault();
1385
		}
1386
		if (!empty($q['enabled'])) {
1387
			$this->SetEnabled($q['enabled']);
1388
		} else {
1389
			$this->SetEnabled("");
1390
		}
1391
	}
1392

    
1393
	function build_tree() {
1394
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1395
		$tree .= "\" ";
1396
		$tmpvalue = $this->GetDefault();
1397
		if (!empty($tmpvalue)) {
1398
			$tree .= " class=\"navlnk\"";
1399
		}
1400
		$tree .= " >" . $this->GetQname() . "</a>";
1401
		/*
1402
		 * Not needed here!
1403
		 * if (is_array($queues) {
1404
		 *	  $tree .= "<ul>";
1405
		 *	  foreach ($q as $queues)
1406
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1407
		 *	  endforeach
1408
		 *	  $tree .= "</ul>";
1409
		 * }
1410
		 */
1411

    
1412
		$tree .= "</li>";
1413

    
1414
		return $tree;
1415
	}
1416

    
1417
	/* Should return something like:
1418
	 * queue $qname on $qinterface bandwidth ....
1419
	 */
1420
	function build_rules(&$default = false) {
1421
		$pfq_rule = " queue ". $this->qname;
1422
		if ($this->GetInterface()) {
1423
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1424
		}
1425
		$tmpvalue = $this->GetQpriority();
1426
		if (is_numeric($tmpvalue)) {
1427
			$pfq_rule .= " priority ".$this->GetQpriority();
1428
		}
1429
		$tmpvalue = $this->GetQlimit();
1430
		if (!empty($tmpvalue)) {
1431
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1432
		}
1433
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1434
			$pfq_rule .= " priq ( ";
1435
			$tmpvalue = $this->GetRed();
1436
			if (!empty($tmpvalue)) {
1437
				$comma = 1;
1438
				$pfq_rule .= " red ";
1439
			}
1440
			$tmpvalue = $this->GetRio();
1441
			if (!empty($tmpvalue)) {
1442
				if ($comma) {
1443
					$pfq_rule .= " ,";
1444
				}
1445
				$comma = 1;
1446
				$pfq_rule .= " rio ";
1447
			}
1448
			$tmpvalue = $this->GetEcn();
1449
			if (!empty($tmpvalue)) {
1450
				if ($comma) {
1451
					$pfq_rule .= " ,";
1452
				}
1453
				$comma = 1;
1454
				$pfq_rule .= " ecn ";
1455
			}
1456
			$tmpvalue = $this->GetCodel();
1457
			if (!empty($tmpvalue)) {
1458
				if ($comma) {
1459
					$pfq_rule .= " ,";
1460
				}
1461
				$comma = 1;
1462
				$pfq_rule .= " codel ";
1463
			}
1464
			$tmpvalue = $this->GetDefault();
1465
			if (!empty($tmpvalue)) {
1466
				if ($comma) {
1467
					$pfq_rule .= " ,";
1468
				}
1469
				$pfq_rule .= " default ";
1470
				$default = true;
1471
			}
1472
			$pfq_rule .= " ) ";
1473
		}
1474

    
1475
		$pfq_rule .= " \n";
1476

    
1477
		return $pfq_rule;
1478
	}
1479

    
1480
	/*
1481
	 * To return the html form to show to user
1482
	 * for getting the parameters.
1483
	 * Should do even for first time when the
1484
	 * object is created and later when we may
1485
	 * need to update it. (2)
1486
	 */
1487

    
1488
	function build_form() {
1489

    
1490
		$sform = new Form();
1491

    
1492
		$sform->setAction("firewall_shaper.php");
1493

    
1494
		$section = new Form_Section("");
1495

    
1496
		$section->addInput(new Form_Checkbox(
1497
			'enabled',
1498
			'Enable/Disable',
1499
			'Enable/disable discipline and its children',
1500
			($this->GetEnabled() == "on"),
1501
			'on'
1502
		));
1503

    
1504
		$section->addInput(new Form_Input(
1505
			'newname',
1506
			'*Name',
1507
			'text',
1508
			$this->GetQname()
1509
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1510

    
1511
		$section->addInput(new Form_Input(
1512
			'name',
1513
			null,
1514
			'hidden',
1515
			$this->GetQname()
1516
		));
1517

    
1518
		if (!is_a($this, "hfsc_queue")) {
1519
			$section->addInput(new Form_Input(
1520
				'priority',
1521
				'Priority',
1522
				'number',
1523
				$this->GetQpriority(),
1524
				['min' => '0', 'max'=> '']
1525
			))->setHelp('For cbq and fairq the range is 0 to 7. The default is 1. For priq the range is 0 to 15, queues with a higher priority are preferred in the case of overload.');
1526
		}
1527

    
1528
		$section->addInput(new Form_Input(
1529
			'qlimit',
1530
			'Queue Limit',
1531
			'number',
1532
			$this->GetQlimit()
1533
		))->setHelp('Queue limit in packets.');
1534

    
1535
		$group = new Form_Group('Scheduler options');
1536

    
1537
		if (empty($this->subqueues)) {
1538
			$group->add(new Form_Checkbox(
1539
				'default',
1540
				null,
1541
				null,
1542
				$this->GetDefault(),
1543
				'default'
1544
			))->setHelp('Default Queue');
1545
		}
1546

    
1547
		$group->add(new Form_Checkbox(
1548
			'red',
1549
			null,
1550
			null,
1551
			!empty($this->GetRed())
1552
		))->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>');
1553

    
1554
		$group->add(new Form_Checkbox(
1555
			'rio',
1556
			null,
1557
			null,
1558
			!empty($this->GetRio())
1559
		))->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>');
1560

    
1561
		$group->add(new Form_Checkbox(
1562
			'ecn',
1563
			null,
1564
			null,
1565
			!empty($this->GetEcn())
1566
		))->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>');
1567

    
1568
		$group->add(new Form_Checkbox(
1569
			'codel',
1570
			null,
1571
			null,
1572
			!empty($this->GetCodel())
1573
		))->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>');
1574

    
1575
		$group->setHelp('Select options for this queue');
1576

    
1577
		$section->add($group);
1578

    
1579
		$section->addInput(new Form_Input(
1580
			'description',
1581
			'Description',
1582
			'text',
1583
			$this->GetDescription()
1584
		));
1585

    
1586
		$sform->add($section);
1587

    
1588
		$sform->addGlobal(new Form_Input(
1589
			'interface',
1590
			null,
1591
			'hidden',
1592
			$this->GetInterface()
1593
		));
1594

    
1595
		$sform->addGlobal(new Form_Input(
1596
			'name',
1597
			null,
1598
			'hidden',
1599
			$this->GetQname()
1600
		));
1601

    
1602
		return($sform);
1603
	}
1604

    
1605
	function build_shortform() {
1606
		/* XXX: Hacks in sight. Mostly layer violations!  */
1607
		global $g, $altq_list_queues;
1608
		global $shaperIFlist;
1609

    
1610
		$altq =& $altq_list_queues[$this->GetInterface()];
1611

    
1612
		if ($altq) {
1613
			$scheduler = $altq->GetScheduler();
1614
		}
1615

    
1616
		$form = '<dl class="dl-horizontal">';
1617
		$form .= '	<dt>';
1618
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1619
		$form .= '	</dt>';
1620
		$form .= '	<dd>';
1621
		$form .=		$scheduler;
1622
		$form .= '	</dd>';
1623

    
1624
		$form .= '	<dt>';
1625
		$form .=		'Bandwidth';
1626
		$form .= '	</dt>';
1627
		$form .= '	<dd>';
1628
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1629
		$form .= '	</dd>';
1630

    
1631
		$tmpvalue = $this->GetQpriority();
1632
		if (!empty($tmpvalue)) {
1633
			$form .= '	<dt>';
1634
			$form .=		'Priority';
1635
			$form .= '	<dt>';
1636
			$form .= '	<dd>';
1637
			$form .=		'On';
1638
			$form .= '	</dd>';
1639
		}
1640

    
1641
		$tmpvalue = $this->GetDefault();
1642
		if (!empty($tmpvalue)) {
1643
			$form .= '	<dt>';
1644
			$form .=		'Default';
1645
			$form .= '	<dt>';
1646
			$form .= '	<dd>';
1647
			$form .=		'On';
1648
			$form .= '	</dd>';
1649
		}
1650

    
1651
			$form .= '	<dt>';
1652
			$form .= 'Delete';
1653
			$form .= '	<dt>';
1654
			$form .= '	<dd>';
1655

    
1656
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1657
			$form .= $this->GetInterface() . '&amp;queue=';
1658
			$form .= $this->GetQname() . '&amp;action=delete">';
1659
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1660
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1661

    
1662
			$form .= '	</dd>';
1663

    
1664
			$form .= '</dl>';
1665

    
1666
		return $form;
1667

    
1668
	}
1669

    
1670
	function update_altq_queue_data(&$q) {
1671
		$this->ReadConfig($q);
1672
	}
1673

    
1674
	function wconfig() {
1675
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1676
		if (!is_array($cflink)) {
1677
			$cflink = array();
1678
		}
1679
		$cflink['name'] = $this->GetQname();
1680
		$cflink['interface'] = $this->GetInterface();
1681
		$cflink['qlimit'] = trim($this->GetQlimit());
1682
		if (empty($cflink['qlimit'])) {
1683
			unset($cflink['qlimit']);
1684
		}
1685
		$cflink['priority'] = trim($this->GetQpriority());
1686
		if (!is_numeric($cflink['priority'])) {
1687
			unset($cflink['priority']);
1688
		}
1689
		$cflink['description'] = trim($this->GetDescription());
1690
		if (empty($cflink['description'])) {
1691
			unset($cflink['description']);
1692
		}
1693
		$cflink['enabled'] = trim($this->GetEnabled());
1694
		if (empty($cflink['enabled'])) {
1695
			unset($cflink['enabled']);
1696
		}
1697
		$cflink['default'] = trim($this->GetDefault());
1698
		if (empty($cflink['default'])) {
1699
			unset($cflink['default']);
1700
		}
1701
		$cflink['red'] = trim($this->GetRed());
1702
		if (empty($cflink['red'])) {
1703
			unset($cflink['red']);
1704
		}
1705
		$cflink['codel'] = trim($this->GetCodel());
1706
		if (empty($cflink['codel'])) {
1707
			unset($cflink['codel']);
1708
		}
1709
		$cflink['rio'] = trim($this->GetRio());
1710
		if (empty($cflink['rio'])) {
1711
			unset($cflink['rio']);
1712
		}
1713
		$cflink['ecn'] = trim($this->GetEcn());
1714
		if (empty($cflink['ecn'])) {
1715
			unset($cflink['ecn']);
1716
		}
1717
	}
1718
}
1719

    
1720
class hfsc_queue extends priq_queue {
1721
	/* realtime */
1722
	var $realtime;
1723
	var $r_m1;
1724
	var $r_d;
1725
	var $r_m2;
1726
	/* linkshare */
1727
	var $linkshare;
1728
	var $l_m1;
1729
	var $l_d;
1730
	var $l_m2;
1731
	/* upperlimit */
1732
	var $upperlimit;
1733
	var $u_m1;
1734
	var $u_d;
1735
	var $u_m2;
1736

    
1737
	/*
1738
	 * HFSC can have nested queues.
1739
	 */
1740
	function CanHaveChildren() {
1741
		return true;
1742
	}
1743
	function GetRealtime() {
1744
		return $this->realtime;
1745
	}
1746
	function GetR_m1() {
1747
		return $this->r_m1;
1748
	}
1749
	function GetR_d() {
1750
		return $this->r_d;
1751
	}
1752
	function GetR_m2() {
1753
		return $this->r_m2;
1754
	}
1755
	function SetRealtime() {
1756
		$this->realtime = "on";
1757
	}
1758
	function DisableRealtime() {
1759
		$this->realtime = "";
1760
	}
1761
	function SetR_m1($value) {
1762
		$this->r_m1 = $value;
1763
	}
1764
	function SetR_d($value) {
1765
		$this->r_d = $value;
1766
	}
1767
	function SetR_m2($value) {
1768
		$this->r_m2 = $value;
1769
	}
1770
	function GetLinkshare() {
1771
		return $this->linkshare;
1772
	}
1773
	function DisableLinkshare() {
1774
		$this->linkshare = "";
1775
	}
1776
	function GetL_m1() {
1777
		return $this->l_m1;
1778
	}
1779
	function GetL_d() {
1780
		return $this->l_d;
1781
	}
1782
	function GetL_m2() {
1783
		return $this->l_m2;
1784
	}
1785
	function SetLinkshare() {
1786
		$this->linkshare = "on";
1787
	}
1788
	function SetL_m1($value) {
1789
		$this->l_m1 = $value;
1790
	}
1791
	function SetL_d($value) {
1792
		$this->l_d = $value;
1793
	}
1794
	function SetL_m2($value) {
1795
		$this->l_m2 = $value;
1796
	}
1797
	function GetUpperlimit() {
1798
		return $this->upperlimit;
1799
	}
1800
	function GetU_m1() {
1801
		return $this->u_m1;
1802
	}
1803
	function GetU_d() {
1804
		return $this->u_d;
1805
	}
1806
	function GetU_m2() {
1807
		return $this->u_m2;
1808
	}
1809
	function SetUpperlimit() {
1810
		$this->upperlimit = "on";
1811
	}
1812
	function DisableUpperlimit() {
1813
		$this->upperlimit = "";
1814
	}
1815
	function SetU_m1($value) {
1816
		$this->u_m1 = $value;
1817
	}
1818
	function SetU_d($value) {
1819
		$this->u_d = $value;
1820
	}
1821
	function SetU_m2($value) {
1822
		$this->u_m2 = $value;
1823
	}
1824

    
1825
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1826

    
1827
		if (!is_array($this->subqueues)) {
1828
			$this->subqueues = array();
1829
		}
1830
		$q =& new hfsc_queue();
1831
		$q->SetInterface($this->GetInterface());
1832
		$q->SetParent($this);
1833
		$q->ReadConfig($qname);
1834
		$q->validate_input($qname, $input_errors);
1835

    
1836
		$q->SetEnabled("on");
1837
		$q->SetLink($path);
1838

    
1839
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1840
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1841
		if (is_array($qname['queue'])) {
1842
			foreach ($qname['queue'] as $key1 => $que) {
1843
				array_push($path, $key1);
1844
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1845
				array_pop($path);
1846
			}
1847
		}
1848

    
1849
		return $q;
1850
	}
1851

    
1852
	function copy_queue($interface, &$cflink) {
1853

    
1854
		$cflink['name'] = $this->GetQname();
1855
		$cflink['interface'] = $interface;
1856
		$cflink['qlimit'] = trim($this->GetQlimit());
1857
		if (empty($cflink['qlimit'])) {
1858
			unset($cflink['qlimit']);
1859
		}
1860
		$cflink['priority'] = trim($this->GetQpriority());
1861
		if (empty($cflink['priority'])) {
1862
			unset($cflink['priority']);
1863
		}
1864
		$cflink['description'] = trim($this->GetDescription());
1865
		if (empty($cflink['description'])) {
1866
			unset($cflink['description']);
1867
		}
1868
		$cflink['bandwidth'] = $this->GetBandwidth();
1869
		$cflink['bandwidthtype'] = $this->GetBwscale();
1870
		$cflink['enabled'] = trim($this->GetEnabled());
1871
		if (empty($cflink['enabled'])) {
1872
			unset($cflink['enabled']);
1873
		}
1874
		$cflink['default'] = trim($this->GetDefault());
1875
		if (empty($cflink['default'])) {
1876
			unset($cflink['default']);
1877
		}
1878
		$cflink['red'] = trim($this->GetRed());
1879
		if (empty($cflink['red'])) {
1880
			unset($cflink['red']);
1881
		}
1882
		$cflink['rio'] = trim($this->GetRio());
1883
		if (empty($cflink['rio'])) {
1884
			unset($cflink['rio']);
1885
		}
1886
		$cflink['ecn'] = trim($this->GetEcn());
1887
		if (empty($cflink['ecn'])) {
1888
			unset($cflink['ecn']);
1889
		}
1890
		if ($this->GetLinkshare() <> "") {
1891
			if ($this->GetL_m1() <> "") {
1892
				$cflink['linkshare1'] = $this->GetL_m1();
1893
				$cflink['linkshare2'] = $this->GetL_d();
1894
				$cflink['linkshare'] = "on";
1895
			} else {
1896
				unset($cflink['linkshare1']);
1897
				unset($cflink['linkshare2']);
1898
				unset($cflink['linkshare']);
1899
			}
1900
			if ($this->GetL_m2() <> "") {
1901
				$cflink['linkshare3'] = $this->GetL_m2();
1902
				$cflink['linkshare'] = "on";
1903
			} else {
1904
				unset($cflink['linkshare3']);
1905
				unset($cflink['linkshare']);
1906
			}
1907
		}
1908
		if ($this->GetRealtime() <> "") {
1909
			if ($this->GetR_m1() <> "") {
1910
				$cflink['realtime1'] = $this->GetR_m1();
1911
				$cflink['realtime2'] = $this->GetR_d();
1912
				$cflink['realtime'] = "on";
1913
			} else {
1914
				unset($cflink['realtime1']);
1915
				unset($cflink['realtime2']);
1916
				unset($cflink['realtime']);
1917
			}
1918
			if ($this->GetR_m2() <> "") {
1919
				$cflink['realtime3'] = $this->GetR_m2();
1920
				$cflink['realtime'] = "on";
1921
			} else {
1922
				unset($cflink['realtime3']);
1923
				unset($cflink['realtime']);
1924
			}
1925
		}
1926
		if ($this->GetUpperlimit() <> "") {
1927
			if ($this->GetU_m1() <> "") {
1928
				$cflink['upperlimit1'] = $this->GetU_m1();
1929
				$cflink['upperlimit2'] = $this->GetU_d();
1930
				$cflink['upperlimit'] = "on";
1931
			} else {
1932
				unset($cflink['upperlimit']);
1933
				unset($cflink['upperlimit1']);
1934
				unset($cflink['upperlimit2']);
1935
			}
1936
			if ($this->GetU_m2() <> "") {
1937
				$cflink['upperlimit3'] = $this->GetU_m2();
1938
				$cflink['upperlimit'] = "on";
1939
			} else {
1940
				unset($cflink['upperlimit3']);
1941
				unset($cflink['upperlimit']);
1942
			}
1943
		}
1944

    
1945
		if (is_array($this->subqueues)) {
1946
			$cflinkp['queue'] = array();
1947
			foreach ($this->subqueues as $q) {
1948
				$cflink['queue'][$q->GetQname()] = array();
1949
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1950
			}
1951
		}
1952
	}
1953

    
1954
	function delete_queue() {
1955
		unref_on_altq_queue_list($this->GetQname());
1956
		cleanup_queue_from_rules($this->GetQname());
1957
		$parent =& $this->GetParent();
1958
		foreach ($this->subqueues as $q)
1959
			$q->delete_queue();
1960
		unset_object_by_reference($this->GetLink());
1961
	}
1962

    
1963
	/*
1964
	 * Should search even its children
1965
	 */
1966
	function &find_queue($interface, $qname) {
1967
		if ($qname == $this->GetQname()) {
1968
			return $this;
1969
		}
1970

    
1971
		foreach ($this->subqueues as $q) {
1972
			$result =& $q->find_queue("", $qname);
1973
			if ($result) {
1974
				return $result;
1975
			}
1976
		}
1977
	}
1978

    
1979
	function &find_parentqueue($interface, $qname) {
1980
		if ($this->subqueues[$qname]) {
1981
			return $this;
1982
		}
1983
		foreach ($this->subqueues as $q) {
1984
			$result = $q->find_parentqueue("", $qname);
1985
			if ($result) {
1986
				return $result;
1987
			}
1988
		}
1989
	}
1990

    
1991
	function validate_input($data, &$input_errors) {
1992
		parent::validate_input($data, $input_errors);
1993

    
1994
		$reqdfields[] = "bandwidth";
1995
		$reqdfieldsn[] = gettext("Bandwidth");
1996
		$reqdfields[] = "bandwidthtype";
1997
		$reqdfieldsn[] = gettext("Bandwidthtype");
1998

    
1999
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2000

    
2001
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
2002
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
2003
				$input_errors[] = gettext("Bandwidth must be an integer.");
2004
			}
2005

    
2006
			if ($data['bandwidth'] < 0) {
2007
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2008
			}
2009

    
2010
			if ($data['bandwidthtype'] == "%") {
2011
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
2012
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2013
				}
2014
			}
2015
		}
2016

    
2017
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
2018
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
2019
		}
2020
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
2021
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
2022
		}
2023
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
2024
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
2025
		}
2026
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
2027
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
2028
		}
2029
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
2030
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
2031
		}
2032

    
2033
		/*
2034
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
2035
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
2036
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
2037
			if (floatval($bw_1) < floatval($bw_2)) {
2038
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
2039
			}
2040

    
2041
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2042
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
2043
			}
2044
		}
2045
		*/
2046
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
2047
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
2048
		}
2049
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
2050
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
2051
		}
2052
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
2053
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
2054
		}
2055
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
2056
			$input_errors[] = gettext("linkshare d value needs to be numeric");
2057
		}
2058
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
2059
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
2060
		}
2061
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2062
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2063
		}
2064
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2065
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2066
		}
2067

    
2068
		/*
2069
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2070
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2071
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2072
			if (floatval($bw_1) < floatval($bw_2)) {
2073
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2074
			}
2075

    
2076
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2077
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2078
			}
2079
		}
2080
		*/
2081

    
2082
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2083
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2084
		}
2085
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2086
			$input_errors[] = gettext("realtime d value needs to be numeric");
2087
		}
2088
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2089
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2090
		}
2091

    
2092
		/*
2093
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2094
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2095
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2096
			if (floatval($bw_1) < floatval($bw_2)) {
2097
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2098
			}
2099

    
2100
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2101
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2102
			}
2103
		}
2104
		*/
2105
	}
2106

    
2107
	function ReadConfig(&$cflink) {
2108
		if (!empty($cflink['linkshare'])) {
2109
			if (!empty($cflink['linkshare1'])) {
2110
				$this->SetL_m1($cflink['linkshare1']);
2111
				$this->SetL_d($cflink['linkshare2']);
2112
				$this->SetLinkshare();
2113
			} else {
2114
				$this->SetL_m1("");
2115
				$this->SetL_d("");
2116
				$this->DisableLinkshare();
2117
			}
2118
			if (!empty($cflink['linkshare3'])) {
2119
				$this->SetL_m2($cflink['linkshare3']);
2120
				$this->SetLinkshare();
2121
			}
2122
		} else {
2123
			$this->DisableLinkshare();
2124
		}
2125
		if (!empty($cflink['realtime'])) {
2126
			if (!empty($cflink['realtime1'])) {
2127
				$this->SetR_m1($cflink['realtime1']);
2128
				$this->SetR_d($cflink['realtime2']);
2129
				$this->SetRealtime();
2130
			} else {
2131
				$this->SetR_m1("");
2132
				$this->SetR_d("");
2133
				$this->DisableRealtime();
2134
			}
2135
			if (!empty($cflink['realtime3'])) {
2136
				$this->SetR_m2($cflink['realtime3']);
2137
				$this->SetRealtime();
2138
			}
2139
		} else {
2140
			$this->DisableRealtime();
2141
		}
2142
		if (!empty($cflink['upperlimit'])) {
2143
			if (!empty($cflink['upperlimit1'])) {
2144
				$this->SetU_m1($cflink['upperlimit1']);
2145
				$this->SetU_d($cflink['upperlimit2']);
2146
				$this->SetUpperlimit();
2147
			} else {
2148
				$this->SetU_m1("");
2149
				$this->SetU_d("");
2150
				$this->DisableUpperlimit();
2151
			}
2152
			if (!empty($cflink['upperlimit3'])) {
2153
				$this->SetU_m2($cflink['upperlimit3']);
2154
				$this->SetUpperlimit();
2155
			}
2156
		} else {
2157
			$this->DisableUpperlimit();
2158
		}
2159
		parent::ReadConfig($cflink);
2160
	}
2161

    
2162
	function build_tree() {
2163
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2164
		$tree .= "\" ";
2165
		$tmpvalue = $this->GetDefault();
2166
		if (!empty($tmpvalue)) {
2167
			$tree .= " class=\"navlnk\"";
2168
		}
2169
		$tree .= " >" . $this->GetQname() . "</a>";
2170
		if (is_array($this->subqueues)) {
2171
			$tree .= "<ul>";
2172
			foreach ($this->subqueues as $q) {
2173
				$tree .= $q->build_tree();
2174
			}
2175
			$tree .= "</ul>";
2176
		}
2177
		$tree .= "</li>";
2178
		return $tree;
2179
	}
2180

    
2181
	/* Even this should take children into consideration */
2182
	function build_rules(&$default = false) {
2183

    
2184
		$pfq_rule = " queue ". $this->qname;
2185
		if ($this->GetInterface()) {
2186
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2187
		}
2188
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2189
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2190
		}
2191

    
2192
		$tmpvalue = $this->GetQlimit();
2193
		if (!empty($tmpvalue)) {
2194
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2195
		}
2196
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2197
			$pfq_rule .= " hfsc ( ";
2198
			$tmpvalue = $this->GetRed();
2199
			if (!empty($tmpvalue)) {
2200
				$comma = 1;
2201
				$pfq_rule .= " red ";
2202
			}
2203

    
2204
			$tmpvalue = $this->GetRio();
2205
			if (!empty($tmpvalue)) {
2206
				if ($comma) {
2207
					$pfq_rule .= " ,";
2208
				}
2209
				$comma = 1;
2210
				$pfq_rule .= " rio ";
2211
			}
2212
			$tmpvalue = $this->GetEcn();
2213
			if (!empty($tmpvalue)) {
2214
				if ($comma) {
2215
					$pfq_rule .= " ,";
2216
				}
2217
				$comma = 1;
2218
				$pfq_rule .= " ecn ";
2219
			}
2220
			$tmpvalue = $this->GetCodel();
2221
			if (!empty($tmpvalue)) {
2222
				if ($comma) {
2223
					$pfq_rule .= " ,";
2224
				}
2225
				$comma = 1;
2226
				$pfq_rule .= " codel ";
2227
			}
2228
			$tmpvalue = $this->GetDefault();
2229
			if (!empty($tmpvalue)) {
2230
				if ($comma) {
2231
					$pfq_rule .= " ,";
2232
				}
2233
				$comma = 1;
2234
				$pfq_rule .= " default ";
2235
				$default = true;
2236
			}
2237

    
2238
			if ($this->GetRealtime() <> "") {
2239
				if ($comma) {
2240
					$pfq_rule .= " , ";
2241
				}
2242
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2243
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2244
				} else if ($this->GetR_m2() <> "") {
2245
					$pfq_rule .= " realtime " . $this->GetR_m2();
2246
				}
2247
				$comma = 1;
2248
			}
2249
			if ($this->GetLinkshare() <> "") {
2250
				if ($comma) {
2251
					$pfq_rule .= " ,";
2252
				}
2253
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2254
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2255
				} else if ($this->GetL_m2() <> "") {
2256
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2257
				}
2258
				$comma = 1;
2259
			}
2260
			if ($this->GetUpperlimit() <> "") {
2261
				if ($comma) {
2262
					$pfq_rule .= " ,";
2263
				}
2264
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2265
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2266
				} else if ($this->GetU_m2() <> "") {
2267
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2268
				}
2269
			}
2270
			$pfq_rule .= " ) ";
2271
		}
2272
		if (count($this->subqueues)) {
2273
			$i = count($this->subqueues);
2274
			$pfq_rule .= " { ";
2275
			foreach ($this->subqueues as $qkey => $qnone) {
2276
				if ($i > 1) {
2277
					$i--;
2278
					$pfq_rule .= " {$qkey}, ";
2279
				} else {
2280
					$pfq_rule .= " {$qkey} ";
2281
				}
2282
			}
2283
			$pfq_rule .= " } \n";
2284
			foreach ($this->subqueues as $q) {
2285
				$pfq_rule .= $q->build_rules($default);
2286
			}
2287
		}
2288

    
2289
		$pfq_rule .= " \n";
2290

    
2291
		return $pfq_rule;
2292
	}
2293

    
2294
	function build_javascript() {
2295

    
2296
		$javascript = <<<EOJS
2297
<script type="text/javascript">
2298
//<![CDATA[
2299
	events.push(function(){
2300

    
2301
		// Disables the specified input element
2302
		function disableInput(id, disable) {
2303
			$('#' + id).prop("disabled", disable);
2304
		}
2305

    
2306
		// Upperlimit
2307
		function enable_upperlimit() {
2308
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2309
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2310
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2311
		}
2312

    
2313
		$('#upperlimit').click(function () {
2314
			enable_upperlimit();
2315
		});
2316

    
2317
		enable_upperlimit();
2318

    
2319
		// realtime
2320
		function enable_realtime() {
2321
			disableInput('realtime1', !$('#realtime').prop('checked'));
2322
			disableInput('realtime2', !$('#realtime').prop('checked'));
2323
			disableInput('realtime3', !$('#realtime').prop('checked'));
2324
		}
2325

    
2326
		$('#realtime').click(function () {
2327
			enable_realtime();
2328
		});
2329

    
2330
		enable_realtime();
2331

    
2332
		// linkshare
2333
		function enable_linkshare() {
2334
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2335
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2336
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2337
		}
2338

    
2339
		$('#linkshare').click(function () {
2340
			enable_linkshare();
2341
		});
2342

    
2343
		enable_linkshare();
2344
	});
2345
//]]>
2346
</script>
2347
EOJS;
2348

    
2349
		return $javascript;
2350
	}
2351

    
2352
	function build_form() {
2353

    
2354
		$sform = parent::build_form();
2355

    
2356
		$section = new Form_Section('Service Curve (sc)');
2357

    
2358
		$group = new Form_Group('Bandwidth');
2359

    
2360
		$group->add(new Form_Input(
2361
			'bandwidth',
2362
			null,
2363
			'number',
2364
			$this->GetBandwidth(),
2365
			['step' => 'any', 'min' => '0.000']
2366
		));
2367

    
2368
		$group->add(new Form_Select(
2369
			'bandwidthtype',
2370
			null,
2371
			$this->GetBwscale(),
2372
			array('Kb' => 'Kbit/s',
2373
				  'Mb' => 'Mbit/s',
2374
				  'Gb' => 'Gbit/s',
2375
				  'b' => 'Bit/s',
2376
				  '%' => '%')
2377
		));
2378

    
2379
		$group->setHelp('Choose the amount of bandwidth for this queue');
2380

    
2381
		$section->add($group);
2382

    
2383
		$group = new Form_Group('Max bandwidth for queue.');
2384

    
2385
		$group->add(new Form_Checkbox(
2386
			'upperlimit',
2387
			null,
2388
			'Upper Limit',
2389
			($this->GetUpperlimit()<> "")
2390
		));
2391

    
2392
		$group->add(new Form_Input(
2393
			'upperlimit1',
2394
			null,
2395
			'text',
2396
			$this->GetU_m1()
2397
		))->setHelp('m1');
2398

    
2399
		$group->add(new Form_Input(
2400
			'upperlimit2',
2401
			null,
2402
			'text',
2403
			$this->GetU_d()
2404
		))->setHelp('d');
2405

    
2406
		$group->add(new Form_Input(
2407
			'upperlimit3',
2408
			null,
2409
			'text',
2410
			$this->GetU_m2()
2411
		))->setHelp('m2');
2412

    
2413

    
2414
		$section->add($group);
2415

    
2416
		$group = new Form_Group('Min bandwidth for queue.');
2417

    
2418
		$group->add(new Form_Checkbox(
2419
			'realtime',
2420
			null,
2421
			'Real Time',
2422
			($this->GetRealtime()<> "")
2423
		));
2424

    
2425
		$group->add(new Form_Input(
2426
			'realtime1',
2427
			null,
2428
			'text',
2429
			$this->GetR_m1()
2430
		))->setHelp('m1');
2431

    
2432
		$group->add(new Form_Input(
2433
			'realtime2',
2434
			null,
2435
			'text',
2436
			$this->GetR_d()
2437
		))->setHelp('d');
2438

    
2439
		$group->add(new Form_Input(
2440
			'realtime3',
2441
			null,
2442
			'text',
2443
			$this->GetR_m2()
2444
		))->setHelp('m2');
2445

    
2446
		$section->add($group);
2447

    
2448
		$group = new Form_Group('B/W share of a backlogged queue.');
2449

    
2450
		$group->add(new Form_Checkbox(
2451
			'linkshare',
2452
			null,
2453
			'Link Share',
2454
			($this->GetLinkshare()<> "")
2455
		));
2456

    
2457
		$group->add(new Form_Input(
2458
			'linkshare1',
2459
			null,
2460
			'text',
2461
			$this->GetL_m1()
2462
		))->setHelp('m1');
2463

    
2464
		$group->add(new Form_Input(
2465
			'linkshare2',
2466
			null,
2467
			'text',
2468
			$this->GetL_d()
2469
		))->setHelp('d');
2470

    
2471
		$group->add(new Form_Input(
2472
			'linkshare3',
2473
			null,
2474
			'text',
2475
			$this->GetL_m2()
2476
		))->setHelp('m2');
2477

    
2478
		$group->sethelp('Bandwidth share overrides priority.%s' .
2479
						'The format for service curve specifications is (m1, d, m2). m2 controls the bandwidth assigned to the queue. ' .
2480
						'm1 and d are optional and can be used to control the initial bandwidth assignment. ' .
2481
						'For the first d milliseconds the queue gets the bandwidth given as m1, afterwards the value given in m2.',
2482
						'<br />');
2483

    
2484
		$section->add($group);
2485

    
2486
		$sform->add($section);
2487

    
2488
		return($sform);
2489
	}
2490

    
2491
	function update_altq_queue_data(&$data) {
2492
		$this->ReadConfig($data);
2493
	}
2494

    
2495
	function wconfig() {
2496
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2497
		if (!is_array($cflink)) {
2498
			$cflink = array();
2499
		}
2500
		$cflink['name'] = $this->GetQname();
2501
		$cflink['interface'] = $this->GetInterface();
2502
		$cflink['qlimit'] = trim($this->GetQlimit());
2503
		if (empty($cflink['qlimit'])) {
2504
			unset($cflink['qlimit']);
2505
		}
2506
		$cflink['priority'] = $this->GetQpriority();
2507
		if (!is_numericint($cflink['priority'])) {
2508
			unset($cflink['priority']);
2509
		}
2510
		$cflink['description'] = $this->GetDescription();
2511
		if (empty($cflink['description'])) {
2512
			unset($cflink['description']);
2513
		}
2514
		$cflink['bandwidth'] = $this->GetBandwidth();
2515
		$cflink['bandwidthtype'] = $this->GetBwscale();
2516
		$cflink['enabled'] = $this->GetEnabled();
2517
		if (empty($cflink['enabled'])) {
2518
			unset($cflink['enabled']);
2519
		}
2520
		$cflink['default'] = $this->GetDefault();
2521
		if (empty($cflink['default'])) {
2522
			unset($cflink['default']);
2523
		}
2524
		$cflink['red'] = trim($this->GetRed());
2525
		if (empty($cflink['red'])) {
2526
			unset($cflink['red']);
2527
		}
2528
		$cflink['rio'] = $this->GetRio();
2529
		if (empty($cflink['rio'])) {
2530
			unset($cflink['rio']);
2531
		}
2532
		$cflink['ecn'] = trim($this->GetEcn());
2533
		if (empty($cflink['ecn'])) {
2534
			unset($cflink['ecn']);
2535
		}
2536
		$cflink['codel'] = trim($this->GetCodel());
2537
		if (empty($cflink['codel'])) {
2538
			unset($cflink['codel']);
2539
		}
2540
		if ($this->GetLinkshare() <> "") {
2541
			if ($this->GetL_m1() <> "") {
2542
				$cflink['linkshare1'] = $this->GetL_m1();
2543
				$cflink['linkshare2'] = $this->GetL_d();
2544
				$cflink['linkshare'] = "on";
2545
			} else {
2546
				unset($cflink['linkshare']);
2547
				unset($cflink['linkshare1']);
2548
				unset($cflink['linkshare2']);
2549
			}
2550
			if ($this->GetL_m2() <> "") {
2551
				$cflink['linkshare3'] = $this->GetL_m2();
2552
				$cflink['linkshare'] = "on";
2553
			} else {
2554
				unset($cflink['linkshare']);
2555
				unset($cflink['linkshare3']);
2556
			}
2557
		} else {
2558
			unset($cflink['linkshare']);
2559
			unset($cflink['linkshare1']);
2560
			unset($cflink['linkshare2']);
2561
			unset($cflink['linkshare3']);
2562
		}
2563
		if ($this->GetRealtime() <> "") {
2564
			if ($this->GetR_m1() <> "") {
2565
				$cflink['realtime1'] = $this->GetR_m1();
2566
				$cflink['realtime2'] = $this->GetR_d();
2567
				$cflink['realtime'] = "on";
2568
			} else {
2569
				unset($cflink['realtime']);
2570
				unset($cflink['realtime1']);
2571
				unset($cflink['realtime2']);
2572
			}
2573
			if ($this->GetR_m2() <> "") {
2574
				$cflink['realtime3'] = $this->GetR_m2();
2575
				$cflink['realtime'] = "on";
2576
			} else {
2577
				unset($cflink['realtime']);
2578
				unset($cflink['realtime3']);
2579
			}
2580
		} else {
2581
			unset($cflink['realtime']);
2582
			unset($cflink['realtime1']);
2583
			unset($cflink['realtime2']);
2584
			unset($cflink['realtime3']);
2585
		}
2586
		if ($this->GetUpperlimit() <> "") {
2587
			if ($this->GetU_m1() <> "") {
2588
				$cflink['upperlimit1'] = $this->GetU_m1();
2589
				$cflink['upperlimit2'] = $this->GetU_d();
2590
				$cflink['upperlimit'] = "on";
2591
			} else {
2592
				unset($cflink['upperlimit']);
2593
				unset($cflink['upperlimit1']);
2594
				unset($cflink['upperlimit2']);
2595
			}
2596
			if ($this->GetU_m2() <> "") {
2597
				$cflink['upperlimit3'] = $this->GetU_m2();
2598
				$cflink['upperlimit'] = "on";
2599
			} else {
2600
				unset($cflink['upperlimit']);
2601
				unset($cflink['upperlimit3']);
2602
			}
2603
		} else {
2604
			unset($cflink['upperlimit']);
2605
			unset($cflink['upperlimit1']);
2606
			unset($cflink['upperlimit2']);
2607
			unset($cflink['upperlimit3']);
2608
		}
2609
	}
2610
}
2611

    
2612
class cbq_queue extends priq_queue {
2613
	var $qborrow = "";
2614

    
2615
	function GetBorrow() {
2616
		return $this->qborrow;
2617
	}
2618
	function SetBorrow($borrow) {
2619
		$this->qborrow = $borrow;
2620
	}
2621
	function CanHaveChildren() {
2622
		return true;
2623
	}
2624

    
2625
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2626

    
2627
		if (!is_array($this->subqueues)) {
2628
			$this->subqueues = array();
2629
		}
2630
		$q =& new cbq_queue();
2631
		$q->SetInterface($this->GetInterface());
2632
		$q->SetParent($this);
2633
		$q->ReadConfig($qname);
2634
		$q->validate_input($qname, $input_errors);
2635

    
2636
		$q->SetEnabled("on");
2637
		$q->SetLink($path);
2638
		$this->subqueues[$q->GetQName()] = &$q;
2639
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2640
		if (is_array($qname['queue'])) {
2641
			foreach ($qname['queue'] as $key1 => $que) {
2642
				array_push($path, $key1);
2643
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2644
				array_pop($path);
2645
			}
2646
		}
2647

    
2648
		return $q;
2649
	}
2650

    
2651
	function copy_queue($interface, &$cflink) {
2652

    
2653
		$cflink['interface'] = $interface;
2654
		$cflink['qlimit'] = trim($this->GetQlimit());
2655
		if (empty($clink['qlimit'])) {
2656
			unset($cflink['qlimit']);
2657
		}
2658
		$cflink['priority'] = trim($this->GetQpriority());
2659
		if (!is_numeric($cflink['priority'])) {
2660
			unset($cflink['priority']);
2661
		}
2662
		$cflink['name'] = $this->GetQname();
2663
		$cflink['description'] = trim($this->GetDescription());
2664
		if (empty($cflink['description'])) {
2665
			unset($cflink['description']);
2666
		}
2667
		$cflink['bandwidth'] = $this->GetBandwidth();
2668
		$cflink['bandwidthtype'] = $this->GetBwscale();
2669
		$cflink['enabled'] = trim($this->GetEnabled());
2670
		if (empty($cflink['enabled'])) {
2671
			unset($cflink['enabled']);
2672
		}
2673
		$cflink['default'] = trim($this->GetDefault());
2674
		if (empty($cflink['default'])) {
2675
			unset($cflink['default']);
2676
		}
2677
		$cflink['red'] = trim($this->GetRed());
2678
		if (empty($cflink['red'])) {
2679
			unset($cflink['red']);
2680
		}
2681
		$cflink['rio'] = trim($this->GetRio());
2682
		if (empty($cflink['rio'])) {
2683
			unset($cflink['rio']);
2684
		}
2685
		$cflink['ecn'] = trim($this->GetEcn());
2686
		if (empty($cflink['ecn'])) {
2687
			unset($cflink['ecn']);
2688
		}
2689
		$cflink['borrow'] = trim($this->GetBorrow());
2690
		if (empty($cflink['borrow'])) {
2691
			unset($cflink['borrow']);
2692
		}
2693
		if (is_array($this->queues)) {
2694
			$cflinkp['queue'] = array();
2695
			foreach ($this->subqueues as $q) {
2696
				$cflink['queue'][$q->GetQname()] = array();
2697
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2698
			}
2699
		}
2700
	}
2701

    
2702
	/*
2703
	 * Should search even its children
2704
	 */
2705
	function &find_queue($interface, $qname) {
2706
		if ($qname == $this->GetQname()) {
2707
			return $this;
2708
		}
2709
		foreach ($this->subqueues as $q) {
2710
			$result =& $q->find_queue("", $qname);
2711
			if ($result) {
2712
				return $result;
2713
			}
2714
		}
2715
	}
2716

    
2717
	function &find_parentqueue($interface, $qname) {
2718
		if ($this->subqueues[$qname]) {
2719
			return $this;
2720
		}
2721
		foreach ($this->subqueues as $q) {
2722
			$result = $q->find_parentqueue("", $qname);
2723
			if ($result) {
2724
				return $result;
2725
			}
2726
		}
2727
	}
2728

    
2729
	function delete_queue() {
2730
		unref_on_altq_queue_list($this->GetQname());
2731
		cleanup_queue_from_rules($this->GetQname());
2732
		foreach ($this->subqueues as $q)
2733
			$q->delete_queue();
2734
		unset_object_by_reference($this->GetLink());
2735
	}
2736

    
2737
	function validate_input($data, &$input_errors) {
2738
		parent::validate_input($data, $input_errors);
2739

    
2740
		if ($data['priority'] > 7) {
2741
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
2742
		}
2743
		$reqdfields[] = "bandwidth";
2744
		$reqdfieldsn[] = gettext("Bandwidth");
2745
		$reqdfields[] = "bandwidthtype";
2746
		$reqdfieldsn[] = gettext("Bandwidthtype");
2747

    
2748
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2749
	}
2750

    
2751
	function ReadConfig(&$q) {
2752
		parent::ReadConfig($q);
2753
		if (!empty($q['borrow'])) {
2754
			$this->SetBorrow("on");
2755
		} else {
2756
			$this->SetBorrow("");
2757
		}
2758
	}
2759

    
2760
	function build_javascript() {
2761
		return parent::build_javascript();
2762
	}
2763

    
2764
	function build_tree() {
2765
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2766
		$tree .= "\" ";
2767
		$tmpvalue = trim($this->GetDefault());
2768
		if (!empty($tmpvalue)) {
2769
			$tree .= " class=\"navlnk\"";
2770
		}
2771
		$tree .= " >" . $this->GetQname() . "</a>";
2772
		if (is_array($this->subqueues)) {
2773
			$tree .= "<ul>";
2774
			foreach ($this->subqueues as $q) {
2775
				$tree .= $q->build_tree();
2776
			}
2777
			$tree .= "</ul>";
2778
		}
2779
		$tree .= "</li>";
2780
		return $tree;
2781
	}
2782

    
2783
	/* Even this should take children into consideration */
2784
	function build_rules(&$default = false) {
2785
		$pfq_rule = "queue ". $this->qname;
2786
		if ($this->GetInterface()) {
2787
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2788
		}
2789
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2790
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2791
		}
2792
		$tmpvalue = $this->GetQpriority();
2793
		if (is_numeric($tmpvalue)) {
2794
			$pfq_rule .= " priority " . $this->GetQpriority();
2795
		}
2796
		$tmpvalue = trim($this->GetQlimit());
2797
		if (!empty($tmpvalue)) {
2798
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2799
		}
2800
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
2801
			$pfq_rule .= " cbq ( ";
2802
			$tmpvalue = trim($this->GetRed());
2803
			if (!empty($tmpvalue)) {
2804
				$comma = 1;
2805
				$pfq_rule .= " red ";
2806
			}
2807
			$tmpvalue = trim($this->GetCodel());
2808
			if (!empty($tmpvalue)) {
2809
				$comma = 1;
2810
				$pfq_rule .= " codel ";
2811
			}
2812
			$tmpvalue = trim($this->GetRio());
2813
			if (!empty($tmpvalue)) {
2814
				if ($comma) {
2815
					$pfq_rule .= " ,";
2816
				}
2817
				$comma = 1;
2818
				$pfq_rule .= " rio ";
2819
			}
2820
			$tmpvalue = trim($this->GetEcn());
2821
			if (!empty($tmpvalue)) {
2822
				if ($comma) {
2823
					$pfq_rule .= " ,";
2824
				}
2825
				$comma = 1;
2826
				$pfq_rule .= " ecn ";
2827
			}
2828
			$tmpvalue = trim($this->GetDefault());
2829
			if (!empty($tmpvalue)) {
2830
				if ($comma) {
2831
					$pfq_rule .= " ,";
2832
				}
2833
				$comma = 1;
2834
				$pfq_rule .= " default ";
2835
				$default = true;
2836
			}
2837
			$tmpvalue = trim($this->GetBorrow());
2838
			if (!empty($tmpvalue)) {
2839
				if ($comma) {
2840
					$pfq_rule .= ", ";
2841
				}
2842
				$pfq_rule .= " borrow ";
2843
			}
2844
			$pfq_rule .= " ) ";
2845
		}
2846
		if (count($this->subqueues)) {
2847
			$i = count($this->subqueues);
2848
			$pfq_rule .= " { ";
2849
			foreach ($this->subqueues as $qkey => $qnone) {
2850
				if ($i > 1) {
2851
					$i--;
2852
					$pfq_rule .= " {$qkey}, ";
2853
				} else {
2854
					$pfq_rule .= " {$qkey} ";
2855
				}
2856
			}
2857
			$pfq_rule .= " } \n";
2858
			foreach ($this->subqueues as $q) {
2859
				$pfq_rule .= $q->build_rules($default);
2860
			}
2861
		}
2862

    
2863
		$pfq_rule .= " \n";
2864
		return $pfq_rule;
2865
	}
2866

    
2867
	function build_form() {
2868
		$sform = parent::build_form();
2869

    
2870
		$section = new Form_Section('NOTITLE');
2871

    
2872
		$group = new Form_Group('Bandwidth');
2873

    
2874
		$group->add(new Form_Input(
2875
			'bandwidth',
2876
			null,
2877
			'number',
2878
			$this->GetBandwidth()
2879
		));
2880

    
2881
		$group->add(new Form_Select(
2882
			'bandwidthtype',
2883
			null,
2884
			$this->GetBwscale(),
2885
			array('Kb' => 'Kbit/s',
2886
				  'Mb' => 'Mbit/s',
2887
				  'Gb' => 'Gbit/s',
2888
				  'b' => 'Bit/s',
2889
				  '%' => '%')
2890
		));
2891

    
2892
		$group->setHelp('Choose the amount of bandwidth for this queue');
2893

    
2894
		$section->add($group);
2895

    
2896
		$section->addInput(new Form_Checkbox(
2897
			'borrow',
2898
			'Scheduler option',
2899
			'Borrow from other queues when available',
2900
			($this->GetBorrow() == "on")
2901
		));
2902

    
2903
		$sform->add($section);
2904

    
2905
		return $sform;
2906
	}
2907

    
2908
	function update_altq_queue_data(&$data) {
2909
		$this->ReadConfig($data);
2910
	}
2911

    
2912
	function wconfig() {
2913
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2914
		if (!is_array($cflink)) {
2915
			$cflink = array();
2916
		}
2917
		$cflink['interface'] = $this->GetInterface();
2918
		$cflink['qlimit'] = trim($this->GetQlimit());
2919
		if (empty($cflink['qlimit'])) {
2920
			unset($cflink['qlimit']);
2921
		}
2922
		$cflink['priority'] = $this->GetQpriority();
2923
		if (!is_numeric($cflink['priority'])) {
2924
			unset($cflink['priority']);
2925
		}
2926
		$cflink['name'] = $this->GetQname();
2927
		$cflink['description'] = $this->GetDescription();
2928
		if (empty($cflink['description'])) {
2929
			unset($cflink['description']);
2930
		}
2931
		$cflink['bandwidth'] = $this->GetBandwidth();
2932
		$cflink['bandwidthtype'] = $this->GetBwscale();
2933
		$cflink['enabled'] = trim($this->GetEnabled());
2934
		if (empty($cflink['enabled'])) {
2935
			unset($cflink['enabled']);
2936
		}
2937
		$cflink['default'] = trim($this->GetDefault());
2938
		if (empty($cflink['default'])) {
2939
			unset($cflink['default']);
2940
		}
2941
		$cflink['red'] = trim($this->GetRed());
2942
		if (empty($cflink['red'])) {
2943
			unset($cflink['red']);
2944
		}
2945
		$cflink['rio'] = trim($this->GetRio());
2946
		if (empty($cflink['rio'])) {
2947
			unset($cflink['rio']);
2948
		}
2949
		$cflink['ecn'] = trim($this->GetEcn());
2950
		if (empty($cflink['ecn'])) {
2951
			unset($cflink['ecn']);
2952
		}
2953
		$cflink['codel'] = trim($this->GetCodel());
2954
		if (empty($cflink['codel'])) {
2955
			unset($cflink['codel']);
2956
		}
2957
		$cflink['borrow'] = trim($this->GetBorrow());
2958
		if (empty($cflink['borrow'])) {
2959
			unset($cflink['borrow']);
2960
		}
2961
	}
2962
}
2963

    
2964
class fairq_queue extends priq_queue {
2965
	var $hogs;
2966
	var $buckets;
2967

    
2968
	function GetBuckets() {
2969
		return $this->buckets;
2970
	}
2971
	function SetBuckets($buckets) {
2972
		$this->buckets = $buckets;
2973
	}
2974
	function GetHogs() {
2975
		return $this->hogs;
2976
	}
2977
	function SetHogs($hogs) {
2978
		$this->hogs = $hogs;
2979
	}
2980
	function CanHaveChildren() {
2981
		return false;
2982
	}
2983

    
2984

    
2985
	function copy_queue($interface, &$cflink) {
2986
		$cflink['interface'] = $interface;
2987
		$cflink['qlimit'] = $this->GetQlimit();
2988
		$cflink['priority'] = $this->GetQpriority();
2989
		$cflink['name'] = $this->GetQname();
2990
		$cflink['description'] = $this->GetDescription();
2991
		$cflink['bandwidth'] = $this->GetBandwidth();
2992
		$cflink['bandwidthtype'] = $this->GetBwscale();
2993
		$cflink['enabled'] = $this->GetEnabled();
2994
		$cflink['default'] = $this->GetDefault();
2995
		$cflink['red'] = $this->GetRed();
2996
		$cflink['rio'] = $this->GetRio();
2997
		$cflink['ecn'] = $this->GetEcn();
2998
		$cflink['buckets'] = $this->GetBuckets();
2999
		$cflink['hogs'] = $this->GetHogs();
3000
	}
3001

    
3002
	/*
3003
	 * Should search even its children
3004
	 */
3005
	function &find_queue($interface, $qname) {
3006
		if ($qname == $this->GetQname()) {
3007
			return $this;
3008
		}
3009
	}
3010

    
3011
	function find_parentqueue($interface, $qname) { return; }
3012

    
3013
	function delete_queue() {
3014
		unref_on_altq_queue_list($this->GetQname());
3015
		cleanup_queue_from_rules($this->GetQname());
3016
		unset_object_by_reference($this->GetLink());
3017
	}
3018

    
3019
	function validate_input($data, &$input_errors) {
3020
		parent::validate_input($data, $input_errors);
3021

    
3022
		if ($data['priority'] > 7) {
3023
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3024
		}
3025
		$reqdfields[] = "bandwidth";
3026
		$reqdfieldsn[] = gettext("Bandwidth");
3027
		$reqdfields[] = "bandwidthtype";
3028
		$reqdfieldsn[] = gettext("Bandwidthtype");
3029

    
3030
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3031
	}
3032

    
3033
	function ReadConfig(&$q) {
3034
		parent::ReadConfig($q);
3035
		if (!empty($q['buckets'])) {
3036
			$this->SetBuckets($q['buckets']);
3037
		} else {
3038
			$this->SetBuckets("");
3039
		}
3040
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3041
			$this->SetHogs($q['hogs']);
3042
		} else {
3043
			$this->SetHogs("");
3044
		}
3045
	}
3046

    
3047
	function build_javascript() {
3048
		return parent::build_javascript();
3049
	}
3050

    
3051
	function build_tree() {
3052
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3053
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
3054
		$tree .= "\" ";
3055
		$tmpvalue = trim($this->GetDefault());
3056
		if (!empty($tmpvalue)) {
3057
			$tree .= " class=\"navlnk\"";
3058
		}
3059
		$tree .= " >" . $this->GetQname() . "</a>";
3060
		$tree .= "</li>";
3061
		return $tree;
3062
	}
3063

    
3064
	/* Even this should take children into consideration */
3065
	function build_rules(&$default = false) {
3066
		$pfq_rule = "queue ". $this->qname;
3067
		if ($this->GetInterface()) {
3068
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3069
		}
3070
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3071
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3072
		}
3073
		$tmpvalue = trim($this->GetQpriority());
3074
		if (is_numeric($tmpvalue)) {
3075
			$pfq_rule .= " priority " . $this->GetQpriority();
3076
		}
3077
		$tmpvalue = trim($this->GetQlimit());
3078
		if (!empty($tmpvalue)) {
3079
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3080
		}
3081
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3082
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3083
			$pfq_rule .= " fairq ( ";
3084
			$tmpvalue = trim($this->GetRed());
3085
			if (!empty($tmpvalue)) {
3086
				$comma = 1;
3087
				$pfq_rule .= " red ";
3088
			}
3089
			$tmpvalue = trim($this->GetCodel());
3090
			if (!empty($tmpvalue)) {
3091
				$comma = 1;
3092
				$pfq_rule .= " codel ";
3093
			}
3094
			$tmpvalue = trim($this->GetRio());
3095
			if (!empty($tmpvalue)) {
3096
				if ($comma) {
3097
					$pfq_rule .= " ,";
3098
				}
3099
				$comma = 1;
3100
				$pfq_rule .= " rio ";
3101
			}
3102
			$tmpvalue = trim($this->GetEcn());
3103
			if (!empty($tmpvalue)) {
3104
				if ($comma) {
3105
					$pfq_rule .= " ,";
3106
				}
3107
				$comma = 1;
3108
				$pfq_rule .= " ecn ";
3109
			}
3110
			$tmpvalue = trim($this->GetDefault());
3111
			if (!empty($tmpvalue)) {
3112
				if ($comma) {
3113
					$pfq_rule .= " ,";
3114
				}
3115
				$comma = 1;
3116
				$pfq_rule .= " default ";
3117
				$default = true;
3118
			}
3119
			$tmpvalue = trim($this->GetBuckets());
3120
			if (!empty($tmpvalue)) {
3121
				if ($comma) {
3122
					$pfq_rule .= ", ";
3123
				}
3124
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3125
			}
3126
			$tmpvalue = trim($this->GetHogs());
3127
			if (!empty($tmpvalue)) {
3128
				if ($comma) {
3129
					$pfq_rule .= ", ";
3130
				}
3131
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3132
			}
3133
			$pfq_rule .= " ) ";
3134
		}
3135

    
3136
		$pfq_rule .= " \n";
3137
		return $pfq_rule;
3138
	}
3139

    
3140
	function build_form() {
3141
		$form = parent::build_form();
3142

    
3143
		$section = new Form_Section('');
3144

    
3145
		$group = new Form_Group('Bandwidth');
3146

    
3147
		$group->add(new Form_Input(
3148
			'bandwidth',
3149
			null,
3150
			'number',
3151
			$this->GetBandwidth()
3152
		));
3153

    
3154
		$group->add(new Form_Select(
3155
			'bandwidthtype',
3156
			null,
3157
			$this->GetBwscale(),
3158
			array('Kb' => 'Kbit/s',
3159
				  'Mb' => 'Mbit/s',
3160
				  'Gb' => 'Gbit/s',
3161
				  'b' => 'Bit/s',
3162
				  '%' => '%')
3163
		));
3164

    
3165
		$group->setHelp('Choose the amount of bandwidth for this queue');
3166

    
3167
		$section->add($group);
3168

    
3169
		$section->addInput(new Form_Input(
3170
			'buckets',
3171
			'Scheduler specific options',
3172
			'text',
3173
			$this->GetBuckets()
3174
		))->setHelp('Number of buckets available');
3175

    
3176
		$section->addInput(new Form_Input(
3177
			'hogs',
3178
			'',
3179
			'text',
3180
			$this->GetHogs()
3181
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3182

    
3183
		$form->add($section);
3184
		return $form;
3185
	}
3186

    
3187
	function update_altq_queue_data(&$data) {
3188
		$this->ReadConfig($data);
3189
	}
3190

    
3191
	function wconfig() {
3192
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3193
		if (!is_array($cflink)) {
3194
			$cflink = array();
3195
		}
3196
		$cflink['interface'] = $this->GetInterface();
3197
		$cflink['qlimit'] = trim($this->GetQlimit());
3198
		if (empty($cflink['qlimit'])) {
3199
			unset($cflink['qlimit']);
3200
		}
3201
		$cflink['priority'] = trim($this->GetQpriority());
3202
		if (!is_numeric($cflink['priority'])) {
3203
			unset($cflink['priority']);
3204
		}
3205
		$cflink['name'] = $this->GetQname();
3206
		$cflink['description'] = trim($this->GetDescription());
3207
		if (empty($cflink['description'])) {
3208
			unset($cflink['description']);
3209
		}
3210
		$cflink['bandwidth'] = $this->GetBandwidth();
3211
		$cflink['bandwidthtype'] = $this->GetBwscale();
3212
		$cflink['enabled'] = $this->GetEnabled();
3213
		if (empty($cflink['enabled'])) {
3214
			unset($cflink['enabled']);
3215
		}
3216
		$cflink['default'] = trim($this->GetDefault());
3217
		if (empty($cflink['default'])) {
3218
			unset($cflink['default']);
3219
		}
3220
		$cflink['red'] = trim($this->GetRed());
3221
		if (empty($cflink['red'])) {
3222
			unset($cflink['red']);
3223
		}
3224
		$cflink['rio'] = trim($this->GetRio());
3225
		if (empty($cflink['rio'])) {
3226
			unset($cflink['rio']);
3227
		}
3228
		$cflink['ecn'] = trim($this->GetEcn());
3229
		if (empty($cflink['ecn'])) {
3230
			unset($cflink['ecn']);
3231
		}
3232
		$cflink['codel'] = trim($this->GetCodel());
3233
		if (empty($cflink['codel'])) {
3234
			unset($cflink['codel']);
3235
		}
3236
		$cflink['buckets'] = trim($this->GetBuckets());
3237
		if (empty($cflink['buckets'])) {
3238
			unset($cflink['buckets']);
3239
		}
3240
		$cflink['hogs'] = trim($this->GetHogs());
3241
		if (empty($cflink['hogs'])) {
3242
			unset($cflink['hogs']);
3243
		}
3244
	}
3245
}
3246

    
3247

    
3248
/*
3249
 * dummynet(4) wrappers.
3250
 */
3251

    
3252

    
3253
/*
3254
 * List of respective objects!
3255
 */
3256
$dummynet_pipe_list = array();
3257

    
3258
class dummynet_class {
3259
	var $qname;
3260
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3261
	var $qlimit;
3262
	var $description;
3263
	var $qenabled;
3264
	var $link;
3265
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3266
	var $plr;
3267

    
3268
	var $buckets;
3269
	/* mask parameters */
3270
	var $mask;
3271
	var $noerror;
3272

    
3273
	/* Accessor functions */
3274
	function SetLink($link) {
3275
		$this->link = $link;
3276
	}
3277
	function GetLink() {
3278
		return $this->link;
3279
	}
3280
	function GetMask() {
3281
		if (!isset($this->mask["type"])) {
3282
			$this->mask["type"] = "none";
3283
		}
3284
		return $this->mask;
3285
	}
3286
	function SetMask($mask) {
3287
		$this->mask = $mask;
3288
	}
3289
	function &GetParent() {
3290
		return $this->qparent;
3291
	}
3292
	function SetParent(&$parent) {
3293
		$this->qparent = &$parent;
3294
	}
3295
	function GetEnabled() {
3296
		return $this->qenabled;
3297
	}
3298
	function SetEnabled($value) {
3299
		$this->qenabled = $value;
3300
	}
3301
	function CanHaveChildren() {
3302
		return false;
3303
	}
3304
	function CanBeDeleted() {
3305
		return true;
3306
	}
3307
	function GetQname() {
3308
		return $this->qname;
3309
	}
3310
	function SetQname($name) {
3311
		$this->qname = trim($name);
3312
	}
3313
	function GetQlimit() {
3314
		return $this->qlimit;
3315
	}
3316
	function SetQlimit($limit) {
3317
		$this->qlimit = $limit;
3318
	}
3319
	function GetDescription() {
3320
		return $this->description;
3321
	}
3322
	function SetDescription($str) {
3323
		$this->description = trim($str);
3324
	}
3325
	function GetFirstime() {
3326
		return $this->firsttime;
3327
	}
3328
	function SetFirsttime($number) {
3329
		$this->firsttime = $number;
3330
	}
3331
	function GetBuckets() {
3332
		return $this->buckets;
3333
	}
3334
	function SetBuckets($buckets) {
3335
		$this->buckets = $buckets;
3336
	}
3337
	function SetNumber($number) {
3338
		$this->qnumber = $number;
3339
	}
3340
	function GetNumber() {
3341
		return $this->qnumber;
3342
	}
3343
	function GetPlr() {
3344
		return $this->plr;
3345
	}
3346
	function SetPlr($plr) {
3347
		$this->plr = $plr;
3348
	}
3349

    
3350
	function build_javascript() {
3351
		$javascript .= "<script type=\"text/javascript\">\n";
3352
		$javascript .= "//<![CDATA[\n";
3353
		$javascript .= "function enable_maskbits(enable_over) {\n";
3354
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3355
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3356
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3357
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3358
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3359
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3360
		$javascript .= "} else {\n";
3361
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3362
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3363
		$javascript .= "}}\n";
3364
		$javascript .= "//]]>\n";
3365
		$javascript .= "</script>\n";
3366
		return $javascript;
3367
	}
3368

    
3369
	function validate_input($data, &$input_errors) {
3370
		$reqdfields[] = "bandwidth";
3371
		$reqdfieldsn[] = gettext("Bandwidth");
3372
		/*$reqdfields[] = "burst";
3373
		$reqdfieldsn[] = gettext("Burst"); */
3374
		$reqdfields[] = "bandwidthtype";
3375
		$reqdfieldsn[] = gettext("Bandwidthtype");
3376
		$reqdfields[] = "newname";
3377
		$reqdfieldsn[] = gettext("Name");
3378

    
3379
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3380

    
3381
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3382
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3383
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3384
		}
3385
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3386
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3387
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3388
		}
3389
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3390
			$input_errors[] = gettext("Queue limit must be an integer");
3391
		}
3392
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3393
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3394
		}
3395
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3396
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3397
		}
3398
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3399
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3400
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3401
			}
3402
		}
3403
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3404
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3405
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3406
			}
3407
		}
3408
	}
3409

    
3410
	function build_mask_rules(&$pfq_rule) {
3411
		$mask = $this->GetMask();
3412
		if (!empty($mask['type'])) {
3413
			if ($mask['type'] <> 'none') {
3414
				$pfq_rule .= " mask";
3415
			}
3416
			switch ($mask['type']) {
3417
				case 'srcaddress':
3418
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3419
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3420
					} else {
3421
						$pfq_rule .= " src-ip6 /128";
3422
					}
3423
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3424
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3425
					} else {
3426
						$pfq_rule .= " src-ip 0xffffffff";
3427
					}
3428
					break;
3429
				case 'dstaddress':
3430
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3431
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3432
					} else {
3433
						$pfq_rule .= " dst-ip6 /128";
3434
					}
3435
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3436
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3437
					} else {
3438
						$pfq_rule .= " dst-ip 0xffffffff";
3439
					}
3440
					break;
3441
				default:
3442
					break;
3443
			}
3444
		}
3445
	}
3446

    
3447
}
3448

    
3449
class dnpipe_class extends dummynet_class {
3450
	var $delay;
3451
	var $qbandwidth = array();
3452
	var $qbandwidthtype;
3453

    
3454
		/* This is here to help on form building and building rules/lists */
3455
	var $subqueues = array();
3456

    
3457
	function CanHaveChildren() {
3458
		return true;
3459
	}
3460
	function SetDelay($delay) {
3461
		$this->delay = $delay;
3462
	}
3463
	function GetDelay() {
3464
		return $this->delay;
3465
	}
3466
	function delete_queue() {
3467
		cleanup_dnqueue_from_rules($this->GetQname());
3468
		foreach ($this->subqueues as $q) {
3469
			$q->delete_queue();
3470
		}
3471
		unset_dn_object_by_reference($this->GetLink());
3472
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3473
	}
3474
	function GetBandwidth() {
3475
		return $this->qbandwidth;
3476
	}
3477
	function SetBandwidth($bandwidth) {
3478
		$this->qbandwidth = $bandwidth;
3479
	}
3480
	function GetBurst() {
3481
		return $this->qburst;
3482
	}
3483
	function SetBurst($burst) {
3484
		$this->qburst = $burst;
3485
	}
3486

    
3487
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3488

    
3489
		if (!is_array($this->subqueues)) {
3490
			$this->subqueues = array();
3491
		}
3492

    
3493
		$q =& new dnqueue_class();
3494
		$q->SetLink($path);
3495
		$q->SetEnabled("on");
3496
		$q->SetPipe($this->GetQname());
3497
		$q->SetParent($this);
3498
		$q->ReadConfig($queue);
3499
		$q->validate_input($queue, $input_errors);
3500
		if (count($input_errors)) {
3501
			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)));
3502
			return $q;
3503
		}
3504
		$number = dnqueue_find_nextnumber();
3505
		$q->SetNumber($number);
3506
		$this->subqueues[$q->GetQname()] = &$q;
3507

    
3508
		return $q;
3509
	}
3510

    
3511
	function &get_queue_list(&$q = null) {
3512
		$qlist = array();
3513

    
3514
		$qlist[$this->GetQname()] = $this->GetNumber();
3515
		if (is_array($this->subqueues)) {
3516
			foreach ($this->subqueues as $queue) {
3517
				$queue->get_queue_list($qlist);
3518
			}
3519
		}
3520
		return $qlist;
3521
	}
3522

    
3523
	/*
3524
	 * Should search even its children
3525
	 */
3526
	function &find_queue($pipe, $qname) {
3527
		if ($qname == $this->GetQname()) {
3528
			return $this;
3529
		}
3530
		foreach ($this->subqueues as $q) {
3531
			$result =& $q->find_queue("", $qname);
3532
			if ($result) {
3533
				return $result;
3534
			}
3535
		}
3536
	}
3537

    
3538
	function &find_parentqueue($pipe, $qname) {
3539
		return NULL;
3540
	}
3541

    
3542
	function validate_input($data, &$input_errors) {
3543
		parent::validate_input($data, $input_errors);
3544

    
3545
		$schedule = 0;
3546
		$schedulenone = 0;
3547
		$entries = 0;
3548
		/* XXX: Really no better way? */
3549
		for ($i = 0; $i < 2900; $i++) {
3550
			if (!empty($data["bwsched{$i}"])) {
3551
				if ($data["bwsched{$i}"] != "none") {
3552
					$schedule++;
3553
				} else {
3554
					$schedulenone++;
3555
				}
3556
			}
3557
			if (!empty($data["bandwidth{$i}"])) {
3558
				if (!is_numeric($data["bandwidth{$i}"])) {
3559
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3560
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3561
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3562
				} else {
3563
					$entries++;
3564
				}
3565
			}
3566
		}
3567
		if ($schedule == 0 && $entries > 1) {
3568
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3569
		}
3570
		if ($schedulenone > 0 && $entries > 1) {
3571
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3572
		}
3573
		if ($entries == 0) {
3574
			$input_errors[] = gettext("At least one bw specification is necessary.");
3575
		}
3576
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3577
			$input_errors[] = gettext("Delay must be an integer.");
3578
		}
3579
	}
3580

    
3581
	function ReadConfig(&$q) {
3582
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3583
			$this->SetQname($q['newname']);
3584
		} else if (!empty($q['newname'])) {
3585
			$this->SetQname($q['newname']);
3586
		} else {
3587
			$this->SetQname($q['name']);
3588
		}
3589
		$this->SetNumber($q['number']);
3590

    
3591
		if (!empty($_POST)) {
3592
			$bandwidth = array();
3593
			/* XXX: Really no better way? */
3594
			for ($i = 0; $i < 2900; $i++) {
3595
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3596
					$bw = array();
3597
					$bw['bw'] = $q["bandwidth{$i}"];
3598
					$bw['burst'] = $q["burst{$i}"];
3599
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3600
						$bw['bwscale'] = $q["bwtype{$i}"];
3601
					}
3602
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3603
						$bw['bwsched'] = $q["bwsched{$i}"];
3604
					}
3605
					$bandwidth[] = $bw;
3606
				}
3607
			}
3608
			$this->SetBandwidth($bandwidth);
3609
		}
3610

    
3611
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3612
			$this->SetBandwidth($q['bandwidth']['item']);
3613
			$this->SetBurst($q['burst']['item']);
3614
		}
3615

    
3616
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3617
			$this->SetQlimit($q['qlimit']);
3618
		} else {
3619
			$this->SetQlimit("");
3620
		}
3621
		if (isset($q['mask']) && $q['mask'] <> "") {
3622
			$masktype = $q['mask'];
3623
		} else {
3624
			$masktype = "";
3625
		}
3626
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3627
			$maskbits = $q['maskbits'];
3628
		} else {
3629
			$maskbits = "";
3630
		}
3631
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3632
			$maskbitsv6 = $q['maskbitsv6'];
3633
		} else {
3634
			$maskbitsv6 = "";
3635
		}
3636
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3637
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3638
			$this->SetBuckets($q['buckets']);
3639
		} else {
3640
			$this->SetBuckets("");
3641
		}
3642
		if (isset($q['plr']) && $q['plr'] <> "") {
3643
			$this->SetPlr($q['plr']);
3644
		} else {
3645
			$this->SetPlr("");
3646
		}
3647
		if (isset($q['delay']) && $q['delay'] <> "") {
3648
			$this->SetDelay($q['delay']);
3649
		} else {
3650
			$this->SetDelay(0);
3651
		}
3652
		if (isset($q['description']) && $q['description'] <> "") {
3653
			$this->SetDescription($q['description']);
3654
		} else {
3655
			$this->SetDescription("");
3656
		}
3657
		$this->SetEnabled($q['enabled']);
3658

    
3659
	}
3660

    
3661
	function build_tree() {
3662
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3663
		$tree .= $this->GetQname() . "</a>";
3664
		if (is_array($this->subqueues)) {
3665
			$tree .= "<ul>";
3666
			foreach ($this->subqueues as $q) {
3667
				$tree .= $q->build_tree();
3668
			}
3669
			$tree .= "</ul>";
3670
		}
3671
		$tree .= "</li>";
3672

    
3673
		return $tree;
3674
	}
3675

    
3676
	function build_rules() {
3677
		global $config, $time_based_rules;
3678

    
3679
		if ($this->GetEnabled() == "") {
3680
			return;
3681
		}
3682

    
3683
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3684
		$found = false;
3685
		$bandwidth = $this->GetBandwidth();
3686
		if (is_array($bandwidth)) {
3687
			foreach ($bandwidth as $bw) {
3688
				if ($bw['bwsched'] != "none") {
3689
					$time_based_rules = true;
3690
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3691
						foreach ($config['schedules']['schedule'] as $schedule) {
3692
							if ($bw['bwsched'] == $schedule['name']) {
3693
								if (filter_get_time_based_rule_status($schedule)) {
3694
									/* pipe throughputs must always be an integer, enforce that restriction again here. */
3695
									$pfq_rule .= " bw ".round(trim($bw['bw']),0).$bw['bwscale'];
3696
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3697
										$pfq_rule .= " burst ".trim($bw['burst']);
3698
									}
3699
									$found = true;
3700
									break;
3701
								}
3702
							}
3703
						}
3704
					} else {
3705
						$pfq_rule .= " bw 0";
3706
						$found = true;
3707
						break;
3708
					}
3709
				} else {
3710
					/* pipe throughputs must always be an integer, enforce that restriction again here. */
3711
					$pfq_rule .= " bw ".round(trim($bw['bw']), 0).$bw['bwscale'];
3712
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3713
						$pfq_rule .= " burst ".trim($bw['burst']);
3714
					}
3715
					$found = true;
3716
					break;
3717
				}
3718
			}
3719
			if ($found == false) {
3720
				$pfq_rule .= " bw 0";
3721
			}
3722
		} else {
3723
			$pfq_rule .= " bw 0";
3724
		}
3725

    
3726
		if ($this->GetQlimit()) {
3727
			$pfq_rule .= " queue " . $this->GetQlimit();
3728
		}
3729
		if ($this->GetPlr()) {
3730
			$pfq_rule .= " plr " . $this->GetPlr();
3731
		}
3732
		if ($this->GetBuckets()) {
3733
			$pfq_rule .= " buckets " . $this->GetBuckets();
3734
		}
3735
		if ($this->GetDelay()) {
3736
			$pfq_rule .= " delay " . $this->GetDelay();
3737
		}
3738
		$this->build_mask_rules($pfq_rule);
3739

    
3740
		$pfq_rule .= "\n";
3741

    
3742
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3743
			foreach ($this->subqueues as $q) {
3744
				$pfq_rule .= $q->build_rules();
3745
			}
3746
		}
3747
		$pfq_rule .= " \n";
3748

    
3749
		return $pfq_rule;
3750
	}
3751

    
3752
	function update_dn_data(&$data) {
3753
		$this->ReadConfig($data);
3754
	}
3755

    
3756
	function build_javascript() {
3757
		global $g, $config;
3758

    
3759
		$javasr = parent::build_javascript();
3760

    
3761
		//build list of schedules
3762
		$schedules = "<option value='none'>none</option>";
3763
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3764
			foreach ($config['schedules']['schedule'] as $schedule) {
3765
				if ($schedule['name'] <> "") {
3766
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3767
				}
3768
			}
3769
		}
3770
		$bwopt = "";
3771
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3772
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3773
		}
3774

    
3775
		$javasr .= <<<EOD
3776
<script type='text/javascript'>
3777
//<![CDATA[
3778
var addBwRowTo = (function() {
3779

    
3780
	return (function (tableId) {
3781

    
3782
	var table = document.getElementById(tableId);
3783
	var totalrows = table.rows.length -1;
3784

    
3785
	var row = table.insertRow(totalrows + 1);
3786
	var cell1 = row.insertCell(0);
3787
	var cell2 = row.insertCell(1);
3788
	var cell3 = row.insertCell(2);
3789
	var cell4 = row.insertCell(3);
3790

    
3791
	cell1.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bandwidth_row-" + totalrows + "' /><input type='text' class='form-control' name='bandwidth" + totalrows + "' id='bandwidth" + totalrows + "' />";
3792
	cell2.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwtype_row-" + totalrows + "' /><select class='form-control' name='bwtype" + totalrows + "'>{$bwopt}</select>";
3793
	cell3.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwsched_row-" + totalrows + "' /><select class='form-control' name='bwsched" + totalrows + "'>{$schedules}</select>";
3794
	cell4.innerHTML = '<a class="btn btn-warning" onclick="removeBwRow(this); return false;" href="#"><i class="fa fa-trash icon-embed-btn"></i>Delete</a>';
3795

    
3796
	});
3797
})();
3798

    
3799
function removeBwRow(el) {
3800
	var d = el.parentNode.parentNode.rowIndex;
3801
	document.getElementById('maintable').deleteRow(d);
3802
}
3803
//]]>
3804
</script>
3805

    
3806
EOD;
3807

    
3808
		return $javasr;
3809
	}
3810

    
3811
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3812
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3813
	// with the javascript in this class
3814
	function build_bwtable() {
3815
		global $config;
3816

    
3817
		$bandwidth = $this->GetBandwidth();
3818
				//build list of schedules
3819
		$schedules = array();
3820
		$schedules[] = "none";//leave none to leave rule enabled all the time
3821
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3822
			foreach ($config['schedules']['schedule'] as $schedule) {
3823
				if ($schedule['name'] != "") {
3824
					$schedules[] = $schedule['name'];
3825
				}
3826
			}
3827
		}
3828

    
3829
		$form = '<div class="table-responsive">';
3830
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3831
		$form .= "<thead><tr>";
3832
		$form .= "<th>Bandwidth</th>";
3833
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3834
		$form .= "<th>Bw type</th>";
3835
		$form .= "<th>Schedule</th>";
3836
		$form .= "<th></th>";
3837
		$form .= "</tr></thead>";
3838
		$form .= "<tbody>";
3839

    
3840
		// If there are no bandwidths defined, make a blank one for convenience
3841
		if (empty($bandwidth)) {
3842
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3843
		}
3844

    
3845
		if (is_array($bandwidth)) {
3846
			foreach ($bandwidth as $bwidx => $bw) {
3847
				$form .= '<tr>';
3848
				$form .= '<td class="col-xs-4">';
3849
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3850
				//$form .= "</td><td width='20%'>";
3851
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3852
				$form .= "</td>";
3853
				$form .= '<td class="col-xs-4">';
3854
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3855

    
3856
				foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "b" => "Bit/s") as $bwsidx => $bwscale) {
3857
					$form .= "<option value=\"{$bwsidx}\"";
3858

    
3859
					if ($bw['bwscale'] == $bwsidx) {
3860
						$form .= " selected";
3861
					}
3862

    
3863
					$form .= ">{$bwscale}</option>";
3864
				}
3865

    
3866
				$form .= "</select>";
3867
				$form .= "</td>";
3868
				$form .= '<td class="col-xs-4">';
3869
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3870

    
3871
				foreach ($schedules as $schd) {
3872
					$selected = "";
3873
					if ($bw['bwsched'] == $schd) {
3874
						$selected = "selected";
3875
					}
3876

    
3877
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3878
				}
3879

    
3880
				$form .= "</select>";
3881
				$form .= "</td>";
3882
				$form .= '<td>';
3883
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
3884
				$form .= "</td></tr>";
3885
			}
3886
		}
3887
		$form .= "</tbody></table></div><br />";
3888

    
3889
		$form .= '<a class="btn btn-sm btn-success" onclick="javascript:addBwRowTo(\'maintable\'); return false;" >';
3890
		$form .= '<i class="fa fa-plus icon-embed-btn"></i>';
3891
		$form .= gettext("Add Schedule") . "</a>";
3892

    
3893
		return($form);
3894
	}
3895

    
3896
	function build_form() {
3897
		global $g, $config, $pipe, $action, $qname;
3898

    
3899
		//build list of schedules
3900
		$schedules = array();
3901
		$schedules[] = "none";//leave none to leave rule enabled all the time
3902
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3903
			foreach ($config['schedules']['schedule'] as $schedule) {
3904
				if ($schedule['name'] <> "") {
3905
					$schedules[] = $schedule['name'];
3906
				}
3907
			}
3908
		}
3909

    
3910

    
3911
		$sform = new Form();
3912
		$sform->setAction("firewall_shaper.php");
3913

    
3914
		$section = new Form_Section('Limiters');
3915

    
3916
		$section->addInput(new Form_Checkbox(
3917
			'enabled',
3918
			'Enable',
3919
			'Enable limiter and its children',
3920
			($this->GetEnabled() == "on"),
3921
			'on'
3922
		));
3923

    
3924
		$section->addInput(new Form_Input(
3925
			'newname',
3926
			'*Name',
3927
			'text',
3928
			$this->GetQname()
3929
		));
3930

    
3931
		$section->addInput(new Form_Input(
3932
			'name',
3933
			null,
3934
			'hidden',
3935
			$this->GetQname()
3936
		));
3937

    
3938
		if ($this->GetNumber() > 0) {
3939
			$section->addInput(new Form_Input(
3940
				'number',
3941
				null,
3942
				'hidden',
3943
				$this->GetNumber()
3944
			));
3945
		}
3946

    
3947
		$bandwidth = $this->GetBandwidth();
3948

    
3949
		if (is_array($bandwidth)) {
3950
				$section->addInput(new Form_StaticText(
3951
				'Bandwidth',
3952
				$this->build_bwtable()
3953
			));
3954
		}
3955

    
3956
		$mask = $this->GetMask();
3957

    
3958
		$section->addInput(new Form_Select(
3959
			'mask',
3960
			'Mask',
3961
			$mask['type'],
3962
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3963
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3964
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3965
					'This makes it possible to easily specify bandwidth limits per host.');
3966

    
3967
		$group = new Form_Group(null);
3968

    
3969
		$group->add(new Form_Select(
3970
			'maskbits',
3971
			null,
3972
			$mask['bits'],
3973
			array_combine(range(32, 1, -1), range(32, 1, -1))
3974
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
3975

    
3976
		$group->add(new Form_Select(
3977
			'maskbitsv6',
3978
			null,
3979
			$mask['bitsv6'],
3980
			array_combine(range(128, 1, -1), range(128, 1, -1))
3981
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3982

    
3983
		$section->add($group);
3984

    
3985
		$section->addInput(new Form_Input(
3986
			'description',
3987
			'Description',
3988
			'text',
3989
			$this->GetDescription()
3990
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
3991

    
3992
		$sform->add($section);
3993

    
3994
		$section = new Form_Section('Advanced Options');
3995

    
3996
		$section->addInput(new Form_Input(
3997
			'delay',
3998
			'Delay (ms)',
3999
			'text',
4000
			$this->GetDelay() > 0 ? $this->GetDelay():null
4001
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4002

    
4003
		$section->addInput(new Form_Input(
4004
			'plr',
4005
			'Packet Loss Rate',
4006
			'number',
4007
			$this->GetPlr(),
4008
			['step' => '0.001', 'min' => '0.000']
4009
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4010
					'A value of 0.001 means one packet in 1000 gets dropped.');
4011

    
4012
		$section->addInput(new Form_Input(
4013
			'qlimit',
4014
			'Queue size (slots)',
4015
			'number',
4016
			$this->GetQlimit()
4017
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4018
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4019

    
4020
		$section->addInput(new Form_Input(
4021
			'buckets',
4022
			'Bucket size (slots)',
4023
			'number',
4024
			$this->GetBuckets()
4025
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4026

    
4027
		$sform->add($section);
4028

    
4029
		return($sform);
4030
		}
4031

    
4032
	function wconfig() {
4033
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4034
		if (!is_array($cflink)) {
4035
			$cflink = array();
4036
		}
4037
		$cflink['name'] = $this->GetQname();
4038
		$cflink['number'] = $this->GetNumber();
4039
		$cflink['qlimit'] = $this->GetQlimit();
4040
		$cflink['plr'] = $this->GetPlr();
4041
		$cflink['description'] = $this->GetDescription();
4042

    
4043
		$bandwidth = $this->GetBandwidth();
4044
		if (is_array($bandwidth)) {
4045
			$cflink['bandwidth'] = array();
4046
			$cflink['bandwidth']['item'] = array();
4047
			foreach ($bandwidth as $bwidx => $bw) {
4048
				$cflink['bandwidth']['item'][] = $bw;
4049
			}
4050
		}
4051

    
4052
		$cflink['enabled'] = $this->GetEnabled();
4053
		$cflink['buckets'] = $this->GetBuckets();
4054
		$mask = $this->GetMask();
4055
		$cflink['mask'] = $mask['type'];
4056
		$cflink['maskbits'] = $mask['bits'];
4057
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4058
		$cflink['delay'] = $this->GetDelay();
4059
	}
4060

    
4061
}
4062

    
4063
class dnqueue_class extends dummynet_class {
4064
	var $pipeparent;
4065
	var $weight;
4066

    
4067
	function GetWeight() {
4068
		return $this->weight;
4069
	}
4070
	function SetWeight($weight) {
4071
		$this->weight = $weight;
4072
	}
4073
	function GetPipe() {
4074
		return $this->pipeparent;
4075
	}
4076
	function SetPipe($pipe) {
4077
		$this->pipeparent = $pipe;
4078
	}
4079

    
4080
	/* Just a stub in case we ever try to call this from the frontend. */
4081
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4082
		return;
4083
	}
4084

    
4085
	function delete_queue() {
4086
		cleanup_dnqueue_from_rules($this->GetQname());
4087
		unset_dn_object_by_reference($this->GetLink());
4088
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4089
	}
4090

    
4091
	function validate_input($data, &$input_errors) {
4092
		parent::validate_input($data, $input_errors);
4093

    
4094
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4095
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4096
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4097
		}
4098
	}
4099

    
4100
	/*
4101
	 * Should search even its children
4102
	 */
4103
	function &find_queue($pipe, $qname) {
4104
		if ($qname == $this->GetQname()) {
4105
			return $this;
4106
		} else {
4107
			return NULL;
4108
		}
4109
	}
4110

    
4111
	function &find_parentqueue($pipe, $qname) {
4112
		return $this->qparent;
4113
	}
4114

    
4115
	function &get_queue_list(&$qlist) {
4116
		if ($this->GetEnabled() == "") {
4117
			return;
4118
		}
4119
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4120
	}
4121

    
4122
	function ReadConfig(&$q) {
4123
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4124
			$this->SetQname($q['newname']);
4125
		} else if (!empty($q['newname'])) {
4126
			$this->SetQname($q['newname']);
4127
		} else {
4128
			$this->SetQname($q['name']);
4129
		}
4130
		$this->SetNumber($q['number']);
4131
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4132
			$this->SetQlimit($q['qlimit']);
4133
		} else {
4134
			$this->SetQlimit("");
4135
		}
4136
		if (isset($q['mask']) && $q['mask'] <> "") {
4137
			$masktype = $q['mask'];
4138
		} else {
4139
			$masktype = "";
4140
		}
4141
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4142
			$maskbits = $q['maskbits'];
4143
		} else {
4144
			$maskbits = "";
4145
		}
4146
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4147
			$maskbitsv6 = $q['maskbitsv6'];
4148
		} else {
4149
			$maskbitsv6 = "";
4150
		}
4151
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4152
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4153
			$this->SetBuckets($q['buckets']);
4154
		} else {
4155
			$this->SetBuckets("");
4156
		}
4157
		if (isset($q['plr']) && $q['plr'] <> "") {
4158
			$this->SetPlr($q['plr']);
4159
		} else {
4160
			$this->SetPlr("");
4161
		}
4162
		if (isset($q['weight']) && $q['weight'] <> "") {
4163
			$this->SetWeight($q['weight']);
4164
		} else {
4165
			$this->SetWeight("");
4166
		}
4167
		if (isset($q['description']) && $q['description'] <> "") {
4168
			$this->SetDescription($q['description']);
4169
		} else {
4170
			$this->SetDescription("");
4171
		}
4172
		$this->SetEnabled($q['enabled']);
4173
	}
4174

    
4175
	function build_tree() {
4176
		$parent =& $this->GetParent();
4177
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4178
		$tree .= $this->GetQname() . "</a>";
4179
		$tree .= "</li>";
4180

    
4181
		return $tree;
4182
	}
4183

    
4184
	function build_rules() {
4185
		if ($this->GetEnabled() == "") {
4186
			return;
4187
		}
4188

    
4189
		$parent =& $this->GetParent();
4190
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4191
		if ($this->GetQlimit()) {
4192
			$pfq_rule .= " queue " . $this->GetQlimit();
4193
		}
4194
		if ($this->GetWeight()) {
4195
			$pfq_rule .= " weight " . $this->GetWeight();
4196
		}
4197
		if ($this->GetBuckets()) {
4198
			$pfq_rule .= " buckets " . $this->GetBuckets();
4199
		}
4200
		$this->build_mask_rules($pfq_rule);
4201
		$pfq_rule .= "\n";
4202

    
4203
		return $pfq_rule;
4204
	}
4205

    
4206
	function build_javascript() {
4207
		return parent::build_javascript();
4208
	}
4209

    
4210
	function build_form() {
4211
		global $g, $config, $pipe, $action, $qname;
4212

    
4213
		//build list of schedules
4214
		$schedules = array();
4215
		$schedules[] = "none";//leave none to leave rule enabled all the time
4216
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4217
			foreach ($config['schedules']['schedule'] as $schedule) {
4218
				if ($schedule['name'] <> "") {
4219
					$schedules[] = $schedule['name'];
4220
				}
4221
			}
4222
		}
4223

    
4224

    
4225
		$sform = new Form();
4226
		$sform->setAction("firewall_shaper.php");
4227
		$section = new Form_Section('Limiters');
4228

    
4229
		$section->addInput(new Form_Checkbox(
4230
			'enabled',
4231
			'Enable',
4232
			'Enable this queue',
4233
			($this->GetEnabled() == "on"),
4234
			'on'
4235
		));
4236

    
4237
		$section->addInput(new Form_Input(
4238
			'newname',
4239
			'*Name',
4240
			'text',
4241
			$this->GetQname()
4242
		));
4243

    
4244
		$section->addInput(new Form_Input(
4245
			'name',
4246
			null,
4247
			'hidden',
4248
			$this->GetQname()
4249
		));
4250

    
4251
		if ($this->GetNumber() > 0) {
4252
			$section->addInput(new Form_Input(
4253
				'number',
4254
				null,
4255
				'hidden',
4256
				$this->GetNumber()
4257
			));
4258
		}
4259

    
4260
		$mask = $this->GetMask();
4261

    
4262
		$section->addInput(new Form_Select(
4263
			'mask',
4264
			'Mask',
4265
			$mask['type'],
4266
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4267
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4268
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4269
					'This makes it possible to easily specify bandwidth limits per host.');
4270

    
4271
		$group = new Form_Group(null);
4272

    
4273
		$group->add(new Form_Select(
4274
			'maskbits',
4275
			null,
4276
			$mask['bits'],
4277
			array_combine(range(32, 1, -1), range(32, 1, -1))
4278
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4279

    
4280
		$group->add(new Form_Select(
4281
			'maskbitsv6',
4282
			null,
4283
			$mask['bitsv6'],
4284
			array_combine(range(128, 1, -1), range(128, 1, -1))
4285
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4286

    
4287
		$section->add($group);
4288

    
4289
		$section->addInput(new Form_Input(
4290
			'description',
4291
			'Description',
4292
			'text',
4293
			$this->GetDescription()
4294
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4295

    
4296
		$sform->add($section);
4297

    
4298
		$section = new Form_Section('Advanced Options');
4299

    
4300
		$section->addInput(new Form_Input(
4301
			'weight',
4302
			'Weight',
4303
			'number',
4304
			$this->GetWeight(),
4305
			['min' => '1', 'max' => '100']
4306
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4307
					' it can be left blank otherwise.');
4308

    
4309
		$section->addInput(new Form_Input(
4310
			'plr',
4311
			'Packet Loss Rate',
4312
			'number',
4313
			$this->GetPlr(),
4314
			['step' => '0.001', 'min' => '0.000']
4315
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4316
					'A value of 0.001 means one packet in 1000 gets dropped');
4317

    
4318
		$section->addInput(new Form_Input(
4319
			'qlimit',
4320
			'Queue size (slots)',
4321
			'number',
4322
			$this->GetQlimit()
4323
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4324
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4325

    
4326
		$section->addInput(new Form_Input(
4327
			'buckets',
4328
			'Bucket size (slots)',
4329
			'number',
4330
			$this->GetBuckets()
4331
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4332

    
4333
		$section->addInput(new Form_Input(
4334
			'pipe',
4335
			null,
4336
			'hidden',
4337
			$this->GetPipe()
4338
		));
4339

    
4340
		$sform->add($section);
4341

    
4342
		return($sform);
4343
	}
4344

    
4345
	function update_dn_data(&$data) {
4346
		$this->ReadConfig($data);
4347
	}
4348

    
4349
	function wconfig() {
4350
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4351
		if (!is_array($cflink)) {
4352
			$cflink = array();
4353
		}
4354
		$cflink['name'] = $this->GetQname();
4355
		$cflink['number'] = $this->GetNumber();
4356
		$cflink['qlimit'] = $this->GetQlimit();
4357
		$cflink['description'] = $this->GetDescription();
4358
		$cflink['weight'] = $this->GetWeight();
4359
		$cflink['enabled'] = $this->GetEnabled();
4360
		$cflink['buckets'] = $this->GetBuckets();
4361
		$mask = $this->GetMask();
4362
		$cflink['mask'] = $mask['type'];
4363
		$cflink['maskbits'] = $mask['bits'];
4364
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4365
	}
4366
}
4367

    
4368
function get_dummynet_name_list() {
4369

    
4370
	$dn_name_list =& get_unique_dnqueue_list();
4371
	$dn_name = array();
4372
	if (is_array($dn_name_list)) {
4373
		foreach ($dn_name_list as $key => $value) {
4374
			$dn_name[] = $key;
4375
		}
4376
	}
4377

    
4378
	return $dn_name;
4379

    
4380
}
4381

    
4382
function get_altq_name_list() {
4383
	$altq_name_list =& get_unique_queue_list();
4384
	$altq_name = array();
4385
	if (is_array($altq_name_list)) {
4386
		foreach ($altq_name_list as $key => $aqobj) {
4387
			$altq_name[] = $key;
4388
		}
4389
	}
4390

    
4391
	return $altq_name;
4392
}
4393

    
4394
/*
4395
 * XXX: TODO Make a class shaper to hide all these functions
4396
 * from the global namespace.
4397
 */
4398

    
4399
/*
4400
 * This is a layer violation but for now there is no way
4401
 * I can find to properly do this with PHP.
4402
 */
4403
function altq_get_default_queue($interface) {
4404
	global $altq_list_queues;
4405

    
4406
	$altq_tmp = $altq_list_queues[$interface];
4407
	if ($altq_tmp) {
4408
		return $altq_tmp->GetDefaultQueuePresent();
4409
	} else {
4410
		return false;
4411
	}
4412
}
4413

    
4414
function altq_check_default_queues() {
4415
	global $altq_list_queues;
4416

    
4417
	$count = 0;
4418
	if (is_array($altq_list_queues)) {
4419
		foreach ($altq_list_queues as $altq) {
4420
			if ($altq->GetDefaultQueuePresent()) {
4421
				$count++;
4422
			}
4423
		}
4424
	}
4425
	else {
4426
		$count++;
4427
	}
4428

    
4429
	return 0;
4430
}
4431

    
4432
function &get_unique_queue_list() {
4433
	global $altq_list_queues;
4434

    
4435
	$qlist = array();
4436
	if (is_array($altq_list_queues)) {
4437
		foreach ($altq_list_queues as $altq) {
4438
			if ($altq->GetEnabled() == "") {
4439
				continue;
4440
			}
4441
			$tmplist =& $altq->get_queue_list();
4442
			foreach ($tmplist as $qname => $link) {
4443
				if ($link->GetEnabled() <> "") {
4444
					$qlist[$qname] = $link;
4445
				}
4446
			}
4447
		}
4448
	}
4449
	return $qlist;
4450
}
4451

    
4452
function &get_unique_dnqueue_list() {
4453
	global $dummynet_pipe_list;
4454

    
4455
	$qlist = array();
4456
	if (is_array($dummynet_pipe_list)) {
4457
		foreach ($dummynet_pipe_list as $dn) {
4458
			if ($dn->GetEnabled() == "") {
4459
				continue;
4460
			}
4461
			$tmplist =& $dn->get_queue_list();
4462
			foreach ($tmplist as $qname => $link) {
4463
				$qlist[$qname] = $link;
4464
			}
4465
		}
4466
	}
4467
	return $qlist;
4468
}
4469

    
4470
function ref_on_altq_queue_list($parent, $qname) {
4471
	if (isset($GLOBALS['queue_list'][$qname])) {
4472
		$GLOBALS['queue_list'][$qname]++;
4473
	} else {
4474
		$GLOBALS['queue_list'][$qname] = 1;
4475
	}
4476

    
4477
	unref_on_altq_queue_list($parent);
4478
}
4479

    
4480
function unref_on_altq_queue_list($qname) {
4481
	$GLOBALS['queue_list'][$qname]--;
4482
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4483
		unset($GLOBALS['queue_list'][$qname]);
4484
	}
4485
}
4486

    
4487
function read_altq_config() {
4488
	global $altq_list_queues, $config;
4489
	$path = array();
4490

    
4491
	if (!is_array($config['shaper'])) {
4492
		$config['shaper'] = array();
4493
	}
4494
	if (!is_array($config['shaper']['queue'])) {
4495
		$config['shaper']['queue'] = array();
4496
	}
4497
	$a_int = &$config['shaper']['queue'];
4498

    
4499
	$altq_list_queues = array();
4500

    
4501
	if (!is_array($config['shaper']['queue'])) {
4502
		return;
4503
	}
4504

    
4505
	foreach ($a_int as $key => $conf) {
4506
		$int = $conf['interface'];
4507
		$root =& new altq_root_queue();
4508
		$root->SetInterface($int);
4509
		$altq_list_queues[$root->GetInterface()] = &$root;
4510
		$root->ReadConfig($conf);
4511
		array_push($path, $key);
4512
		$root->SetLink($path);
4513
		if (is_array($conf['queue'])) {
4514
			foreach ($conf['queue'] as $key1 => $q) {
4515
				array_push($path, $key1);
4516
				/*
4517
				 * XXX: we completely ignore errors here but anyway we must have
4518
				 *	checked them before so no harm should be come from this.
4519
				 */
4520
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4521
				array_pop($path);
4522
			}
4523
		}
4524
		array_pop($path);
4525
	}
4526
}
4527

    
4528
function read_dummynet_config() {
4529
	global $dummynet_pipe_list, $config;
4530
	$path = array();
4531

    
4532
	if (!is_array($config['dnshaper'])) {
4533
		$config['dnshaper'] = array();
4534
	}
4535
	if (!is_array($config['dnshaper']['queue'])) {
4536
		$config['dnshaper']['queue'] = array();
4537
	}
4538
	$a_int = &$config['dnshaper']['queue'];
4539

    
4540
	$dummynet_pipe_list = array();
4541

    
4542
	if (!is_array($config['dnshaper']['queue']) ||
4543
	    !count($config['dnshaper']['queue'])) {
4544
		return;
4545
	}
4546

    
4547
	foreach ($a_int as $key => $conf) {
4548
		if (empty($conf['name'])) {
4549
			continue; /* XXX: grrrrrr at php */
4550
		}
4551
		$root =& new dnpipe_class();
4552
		$root->ReadConfig($conf);
4553
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4554
		array_push($path, $key);
4555
		$root->SetLink($path);
4556
		if (is_array($conf['queue'])) {
4557
			foreach ($conf['queue'] as $key1 => $q) {
4558
				array_push($path, $key1);
4559
				/*
4560
				 * XXX: we completely ignore errors here but anyway we must have
4561
				 *	checked them before so no harm should be come from this.
4562
				 */
4563
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4564
				array_pop($path);
4565
			}
4566
		}
4567
		array_pop($path);
4568
	}
4569
}
4570

    
4571
function get_interface_list_to_show() {
4572
	global $altq_list_queues, $config;
4573
	global $shaperIFlist;
4574

    
4575
	$tree = "";
4576
	foreach ($shaperIFlist as $shif => $shDescr) {
4577
		if ($altq_list_queues[$shif]) {
4578
			continue;
4579
		} else {
4580
			if (!is_altq_capable(get_real_interface($shif))) {
4581
				continue;
4582
			}
4583
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4584
		}
4585
	}
4586

    
4587
	return $tree;
4588
}
4589

    
4590
function filter_generate_altq_queues() {
4591
	global $altq_list_queues;
4592

    
4593
	read_altq_config();
4594

    
4595
	$altq_rules = "";
4596
	foreach ($altq_list_queues as $altq) {
4597
		$altq_rules .= $altq->build_rules();
4598
	}
4599

    
4600
	return $altq_rules;
4601
}
4602

    
4603
function dnqueue_find_nextnumber() {
4604
	global $dummynet_pipe_list;
4605

    
4606
	$dnused = array();
4607
	if (is_array($dummynet_pipe_list)) {
4608
		foreach ($dummynet_pipe_list as $dn) {
4609
			$tmplist =& $dn->get_queue_list();
4610
			foreach ($tmplist as $qname => $link) {
4611
				if ($link[0] == "?") {
4612
					$dnused[$qname] = substr($link, 1);
4613
				}
4614
			}
4615
		}
4616
	}
4617

    
4618
	sort($dnused, SORT_NUMERIC);
4619
	$dnnumber = 0;
4620
	$found = false;
4621
	foreach ($dnused as $dnnum) {
4622
		if (($dnnum - $dnnumber) > 1) {
4623
			$dnnumber = $dnnum - 1;
4624
			$found = true;
4625
			break;
4626
		} else {
4627
			$dnnumber = $dnnum;
4628
		}
4629
	}
4630

    
4631
	if ($found == false) {
4632
		$dnnumber++;
4633
	}
4634

    
4635
	unset($dnused, $dnnum, $found);
4636
	return $dnnumber;
4637
}
4638

    
4639
function dnpipe_find_nextnumber() {
4640
	global $dummynet_pipe_list;
4641

    
4642
	$dnused = array();
4643
	foreach ($dummynet_pipe_list as $dn) {
4644
		$dnused[] = $dn->GetNumber();
4645
	}
4646

    
4647
	sort($dnused, SORT_NUMERIC);
4648
	$dnnumber = 0;
4649
	$found = false;
4650
	foreach ($dnused as $dnnum) {
4651
		if (($dnnum - $dnnumber) > 1) {
4652
			$dnnumber = $dnnum - 1;
4653
			$found = true;
4654
			break;
4655
		} else {
4656
			$dnnumber = $dnnum;
4657
		}
4658
	}
4659

    
4660
	if ($found == false) {
4661
		$dnnumber++;
4662
	}
4663

    
4664
	unset($dnused, $dnnum, $found);
4665
	return $dnnumber;
4666
}
4667

    
4668
function filter_generate_dummynet_rules() {
4669
	global $g, $dummynet_pipe_list;
4670

    
4671
	read_dummynet_config();
4672

    
4673
	$dn_rules = "";
4674
	$max_qlimit = "100"; // OS default
4675
	foreach ($dummynet_pipe_list as $dn) {
4676
		$dn_rules .= $dn->build_rules();
4677
		$this_qlimit = $dn->GetQlimit();
4678
		if ($this_qlimit > $max_qlimit) {
4679
			$max_qlimit = $this_qlimit;
4680
		}
4681
	}
4682
	if (!is_numericint($max_qlimit)) {
4683
		$max_qlimit = "100";
4684
	}
4685
	if (!empty($dn_rules)) {
4686
		if (!is_module_loaded("dummynet.ko")) {
4687
			mwexec("/sbin/kldload dummynet");
4688
		}
4689
		set_sysctl(array(
4690
				"net.inet.ip.dummynet.io_fast" => "1",
4691
				"net.inet.ip.dummynet.hash_size" => "256",
4692
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
4693
		));
4694
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4695
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4696
	}
4697
}
4698

    
4699
function build_iface_without_this_queue($iface, $qname) {
4700
	global $g, $altq_list_queues;
4701
	global $shaperIFlist;
4702

    
4703
	$altq =& $altq_list_queues[$iface];
4704

    
4705
	if ($altq) {
4706
		$scheduler = $altq->GetScheduler();
4707
	}
4708

    
4709
	$form = '<dl class="dl-horizontal">';
4710

    
4711
	$form .= '	<dt>';
4712
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4713
	$form .= '	</dt>';
4714
	$form .= '	<dd>';
4715
	$form .=		$scheduler;
4716
	$form .= '	</dd>';
4717

    
4718
	$form .= '	<dt>';
4719
	$form .= 'Clone';
4720
	$form .= '	</dt>';
4721
	$form .= '	<dd>';
4722
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4723
	$form .= $iface . '&amp;queue=';
4724
	$form .= $qname . '&amp;action=add">';
4725
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
4726
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
4727
	$form .= '	</dd>';
4728

    
4729
	$form .= '</dl>';
4730

    
4731
	return $form;
4732

    
4733
}
4734

    
4735
$default_shaper_msg = sprintf(gettext("Welcome to the %s Traffic Shaper."), $g['product_name']) . "<br />";
4736
$dn_default_shaper_msg = $default_shaper_msg;
4737

    
4738
$shaper_msg = gettext("The tree on the left navigates through the %s.");
4739
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
4740
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
4741

    
4742
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
4743
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
4744
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
4745

    
4746
// Check to see if the specified interface has a queue configured
4747
function interface_has_queue($if) {
4748
	global $config;
4749

    
4750
	if ($config['shaper']) {
4751
		foreach ($config['shaper']['queue'] as $queue) {
4752
			if ($queue['interface'] === $if) {
4753
				return true;
4754
			}
4755
		}
4756
	}
4757

    
4758
	return false;
4759
}
4760
?>
(43-43/55)