Project

General

Profile

Download (119 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	shaper.inc
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Some or all of this file is based on the m0n0wall project which is
9
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13
 *
14
 *  1. Redistributions of source code must retain the above copyright notice,
15
 *      this list of conditions and the following disclaimer.
16
 *
17
 *  2. Redistributions in binary form must reproduce the above copyright
18
 *      notice, this list of conditions and the following disclaimer in
19
 *      the documentation and/or other materials provided with the
20
 *      distribution.
21
 *
22
 *  3. All advertising materials mentioning features or use of this software
23
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
27
 *  4. The names "pfSense" and "pfSense Project" must not be used to
28
 *       endorse or promote products derived from this software without
29
 *       prior written permission. For written permission, please contact
30
 *       coreteam@pfsense.org.
31
 *
32
 *  5. Products derived from this software may not be called "pfSense"
33
 *      nor may "pfSense" appear in their names without prior written
34
 *      permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *  6. Redistributions of any form whatsoever must retain the following
37
 *      acknowledgment:
38
 *
39
 *  "This product includes software developed by the pfSense Project
40
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
41
 *
42
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *  ====================================================================
56
 *
57
 */
58

    
59
/* XXX: needs some reducing on include. */
60
/* include all configuration functions. */
61
require_once("globals.inc");
62
require_once("functions.inc");
63
require_once("util.inc");
64
require_once("notices.inc");
65

    
66
/*
67
 * I admit :) this is derived from xmlparse.inc StartElement()
68
 */
69
function &get_reference_to_me_in_config(&$mypath) {
70
	global $config;
71

    
72
	$ptr =& $config['shaper'];
73
	foreach ($mypath as $indeks) {
74
		$ptr =& $ptr['queue'][$indeks];
75
	}
76

    
77
	return $ptr;
78
}
79

    
80
function unset_object_by_reference(&$mypath) {
81
	global $config;
82

    
83
	$ptr =& $config['shaper'];
84
	for ($i = 0; $i < count($mypath) - 1; $i++) {
85
		$ptr =& $ptr['queue'][$mypath[$i]];
86
	}
87
	unset($ptr['queue'][$mypath[$i]]);
88
}
89

    
90
function &get_dn_reference_to_me_in_config(&$mypath) {
91
	global $config;
92

    
93
	$ptr =& $config['dnshaper'];
94
	foreach ($mypath as $indeks) {
95
		$ptr =& $ptr['queue'][$indeks];
96
	}
97

    
98
	return $ptr;
99
}
100

    
101
function unset_dn_object_by_reference(&$mypath) {
102
	global $config;
103

    
104
	$ptr =& $config['dnshaper'];
105
	for ($i = 0; $i < count($mypath) - 1; $i++) {
106
		$ptr =& $ptr['queue'][$mypath[$i]];
107
	}
108
	unset($ptr['queue'][$mypath[$i]]);
109
}
110

    
111
function clean_child_queues($type, $mypath) {
112
	$ref = &get_reference_to_me_in_config($mypath);
113

    
114
	switch ($type) {
115
		case 'HFSC':
116
			if (isset($ref['borrow'])) {
117
				unset($ref['borrow']);
118
			}
119
			if (isset($ref['hogs'])) {
120
				unset($ref['hogs']);
121
			}
122
			if (isset($ref['buckets'])) {
123
				unset($ref['buckets']);
124
			}
125
			break;
126
		case 'PRIQ':
127
			if (isset($ref['borrow'])) {
128
				unset($ref['borrow']);
129
			}
130
			if (isset($ref['bandwidth'])) {
131
				unset($ref['bandwidth']);
132
			}
133
			if (isset($ref['bandwidthtype'])) {
134
				unset($ref['bandwidthtype']);
135
			}
136
			/* fall through */
137
		case 'FAIRQ':
138
			if (isset($ref['borrow'])) {
139
				unset($ref['borrow']);
140
			}
141
			/* fall through */
142
		case 'CBQ':
143
			if (isset($ref['realtime'])) {
144
				unset($ref['realtime']);
145
			}
146
			if (isset($ref['realtime1'])) {
147
				unset($ref['realtime1']);
148
			}
149
			if (isset($ref['realtime2'])) {
150
				unset($ref['realtime2']);
151
			}
152
			if (isset($ref['realtime3'])) {
153
				unset($ref['realtime3']);
154
			}
155
			if (isset($ref['upperlimit'])) {
156
				unset($ref['upperlimit']);
157
			}
158
			if (isset($ref['upperlimit1'])) {
159
				unset($ref['upperlimit1']);
160
			}
161
			if (isset($ref['upperlimit2'])) {
162
				unset($ref['upperlimit2']);
163
			}
164
			if (isset($ref['upperlimit3'])) {
165
				unset($ref['upperlimit3']);
166
			}
167
			if (isset($ref['linkshare'])) {
168
				unset($ref['linkshare']);
169
			}
170
			if (isset($ref['linkshare1'])) {
171
				unset($ref['linkshare1']);
172
			}
173
			if (isset($ref['linkshare2'])) {
174
				unset($ref['linkshare2']);
175
			}
176
			if (isset($ref['linkshare3'])) {
177
				unset($ref['linkshare3']);
178
			}
179
			if (isset($ref['hogs'])) {
180
				unset($ref['hogs']);
181
			}
182
			if (isset($ref['buckets'])) {
183
				unset($ref['buckets']);
184
			}
185
			break;
186
	}
187
}
188

    
189
function get_bandwidthtype_scale($type) {
190
	switch ($type) {
191
		case "Gb":
192
			$factor = 1024 * 1024 * 1024;
193
			break;
194
		case "Mb":
195
			$factor = 1024 * 1024;
196
			break;
197
		case "Kb":
198
			$factor = 1024;
199
			break;
200
		case "b":
201
		default:
202
			$factor = 1;
203
			break;
204
	}
205
	return intval($factor);
206
}
207

    
208
function get_hfsc_bandwidth($object, $bw) {
209
	$pattern= "/[0-9]+/";
210
	if (preg_match($pattern, $bw, $match)) {
211
		$bw_1 = $match[1];
212
	} else {
213
		return 0;
214
	}
215
	$pattern= "/(b|Kb|Mb|Gb|%)/";
216
	if (preg_match($pattern, $bw, $match)) {
217
		switch ($match[1]) {
218
			case '%':
219
				$bw_1 = $bw_1 / 100 * get_interface_bandwidth($object);
220
				break;
221
			default:
222
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
223
				break;
224
		}
225
		return floatval($bw_1);
226
	} else {
227
		return 0;
228
	}
229
}
230

    
231
function get_interface_bandwidth($object) {
232
	global $altq_list_queues;
233

    
234
	$int = $object->GetInterface();
235
	$altq =& $altq_list_queues[$int];
236
	if ($altq) {
237
		$bw_3 = $altq->GetBandwidth();
238
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
239
		return floatval($bw_3);
240
	} else {
241
		return 0;
242
	}
243
}
244

    
245
/*
246
 * This is duplicated here since we cannot include guiconfig.inc.
247
 * Including it makes all stuff break.
248
 */
249
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
250

    
251
	/* check for bad control characters */
252
	foreach ($postdata as $pn => $pd) {
253
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
254
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
255
		}
256
	}
257

    
258
	for ($i = 0; $i < count($reqdfields); $i++) {
259
		if ($postdata[$reqdfields[$i]] == "") {
260
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
261
		}
262
	}
263
}
264

    
265
function cleanup_queue_from_rules($queue) {
266
	global $config;
267

    
268
	foreach ($config['filter']['rule'] as $rule) {
269
		if ($rule['defaultqueue'] == $queue) {
270
			unset($rule['defaultqueue']);
271
		}
272
		if ($rule['ackqueue'] == $queue) {
273
			unset($rule['ackqueue']);
274
		}
275
	}
276
}
277

    
278
function cleanup_dnqueue_from_rules($queue) {
279
	global $config;
280

    
281
	foreach ($config['filter']['rule'] as $rule) {
282
		if ($rule['dnpipe'] == $queue) {
283
			unset($rule['dnpipe']);
284
		}
285
		if ($rule['pdnpipe'] == $queue) {
286
			unset($rule['pdnpipe']);
287
		}
288
	}
289
}
290

    
291
class altq_root_queue {
292
	var $interface;
293
	var $tbrconfig ;
294
	var $bandwidth;
295
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
296
	var $scheduler;
297
	var $qlimit;
298
	var $queues = array();
299
	var $qenabled = false;
300
	var $link;
301
	var $available_bw; /* in b/s */
302

    
303
	/* Accessor functions */
304
	function GetAvailableBandwidth() {
305
		return $this->available_bw;
306
	}
307
	function SetAvailableBandwidth($bw) {
308
		$this->available_bw = $bw;
309
	}
310
	function GetDefaultQueuePresent() {
311
		if (!empty($this->queues)) {
312
			foreach ($this->queues as $q) {
313
				if ($q->GetDefault()) {
314
					return true;
315
				}
316
			}
317
		}
318

    
319
		return false;
320
	}
321
	function SetLink($link) {
322
		$this->link = $link;
323
	}
324
	function GetLink() {
325
		return $this->link;
326
	}
327
	function GetEnabled() {
328
		return $this->qenabled;
329
	}
330
	function SetEnabled($value) {
331
		$this->qenabled = $value;
332
	}
333
	function CanHaveChildren() {
334
		if ($this->GetScheduler() == "CODELQ") {
335
			return false;
336
		} else {
337
			return true;
338
		}
339
	}
340
	function CanBeDeleted() {
341
		return false;
342
	}
343
	function GetQname() {
344
		return $this->interface;
345
	}
346
	function SetQname($name) {
347
		$this->interface = trim($name);
348
	}
349
	function GetInterface() {
350
		return $this->interface;
351
	}
352
	function SetInterface($name) {
353
		$this->interface = trim($name);
354
	}
355
	function GetTbrConfig() {
356
		return $this->tbrconfig;
357
	}
358
	function SetTbrConfig($tbrconfig) {
359
		$this->tbrconfig = $tbrconfig;
360
	}
361
	function GetBandwidth() {
362
		return $this->bandwidth;
363
	}
364
	function SetBandwidth($bw) {
365
		$this->bandwidth = $bw;
366
	}
367
	function GetBwscale() {
368
		return $this->bandwidthtype;
369
	}
370
	function SetBwscale($bwscale) {
371
		$this->bandwidthtype = $bwscale;
372
	}
373
	function GetScheduler() {
374
		return $this->scheduler;
375
	}
376
	function SetScheduler($scheduler) {
377
		$this->scheduler = trim($scheduler);
378
	}
379
	function GetQlimit() {
380
		return $this->qlimit;
381
	}
382
	function SetQlimit($limit) {
383
		$this->qlimit = $limit;
384
	}
385

    
386
	function GetBwscaleText() {
387
		switch ($this->bandwidthtype) {
388
			case "b":
389
				$bwscaletext = "Bit/s";
390
				break;
391
			case "Kb":
392
				$bwscaletext = "Kbit/s";
393
				break;
394
			case "Mb":
395
				$bwscaletext = "Mbit/s";
396
				break;
397
			case "Gb":
398
				$bwscaletext = "Gbit/s";
399
				break;
400
			default:
401
				/* For others that do not need translating like % */
402
				$bwscaletext = $this->bandwidthtype;
403
				break;
404
		}
405
		return $bwscaletext;
406
	}
407

    
408
	function validate_input($data, &$input_errors) {
409

    
410
		$reqdfields[] = "bandwidth";
411
		$reqdfieldsn[] = gettext("Bandwidth");
412
		$reqdfields[] = "bandwidthtype";
413
		$reqdfieldsn[] = gettext("Bandwidthtype");
414

    
415
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
416

    
417
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
418
			$input_errors[] = gettext("Bandwidth must be an integer.");
419
		}
420
		if ($data['bandwidth'] < 0) {
421
			$input_errors[] = gettext("Bandwidth cannot be negative.");
422
		}
423
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
424
			$input_errors[] = gettext("Qlimit must be an integer.");
425
		}
426
		if ($data['qlimit'] < 0) {
427
			$input_errors[] = gettext("Qlimit must be positive.");
428
		}
429
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
430
			$input_errors[] = gettext("Tbrsize must be an integer.");
431
		}
432
		if ($data['tbrconfig'] < 0) {
433
			$input_errors[] = gettext("Tbrsize must be positive.");
434
		}
435
	}
436

    
437
	/* Implement this to shorten some code on the frontend page */
438
	function ReadConfig(&$conf) {
439
		if (isset($conf['tbrconfig'])) {
440
			$this->SetTbrConfig($conf['tbrconfig']);
441
		} else {
442
			$this->SetTbrConfig($conf['tbrconfig']);
443
		}
444
		$this->SetBandwidth($conf['bandwidth']);
445
		if ($conf['bandwidthtype'] <> "") {
446
			$this->SetBwscale($conf['bandwidthtype']);
447
		}
448
		if (isset($conf['scheduler'])) {
449
			if ($this->GetScheduler() != $conf['scheduler']) {
450
				foreach ($this->queues as $q) {
451
					clean_child_queues($conf['scheduler'], $this->GetLink());
452
					$q->clean_queue($conf['scheduler']);
453
				}
454
			}
455
			$this->SetScheduler($conf['scheduler']);
456
		}
457
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
458
			$this->SetQlimit($conf['qlimit']);
459
		} else {
460
			$this->SetQlimit("");
461
		}
462
		if (isset($conf['name'])) {
463
			$this->SetQname($conf['name']);
464
		}
465
		if (!empty($conf['enabled'])) {
466
			$this->SetEnabled($conf['enabled']);
467
		} else {
468
			$this->SetEnabled("");
469
		}
470
	}
471

    
472
	function copy_queue($interface, &$cflink) {
473
		$cflink['interface'] = $interface;
474
		$cflink['name'] = $interface;
475
		$cflink['scheduler'] = $this->GetScheduler();
476
		$cflink['bandwidth'] = $this->GetBandwidth();
477
		$cflink['bandwidthtype'] = $this->GetBwscale();
478
		$cflink['qlimit'] = $this->GetQlimit();
479
		$cflink['tbrconfig'] = $this->GetTbrConfig();
480
		$cflink['enabled'] = $this->GetEnabled();
481
		if (is_array($this->queues)) {
482
			$cflink['queue'] = array();
483
			foreach ($this->queues as $q) {
484
				$cflink['queue'][$q->GetQname()] = array();
485
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
486
			}
487
		}
488
	}
489

    
490
	function &get_queue_list(&$q = null) {
491
		$qlist = array();
492

    
493
		//$qlist[$this->GetQname()] = & $this;
494
		if (is_array($this->queues)) {
495
			foreach ($this->queues as $queue) {
496
				$queue->get_queue_list($qlist);
497
			}
498
		}
499
		return $qlist;
500
	}
501

    
502
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
503

    
504
		if (!is_array($this->queues)) {
505
			$this->queues = array();
506
		}
507

    
508
		switch ($this->GetScheduler()) {
509
			case "PRIQ":
510
				$q =& new priq_queue();
511
				break;
512
			case "HFSC":
513
				$q =& new hfsc_queue();
514
				break;
515
			case "CBQ":
516
				$q =& new cbq_queue();
517
				break;
518
			case "FAIRQ":
519
				$q =& new fairq_queue();
520
				break;
521
			default:
522
				/* XXX: but should not happen anyway */
523
				return;
524
				break;
525
		}
526
		$q->SetLink($path);
527
		$q->SetInterface($this->GetInterface());
528
		$q->SetEnabled("on");
529
		$q->SetParent($this);
530
		$q->ReadConfig($queue);
531
		$q->validate_input($queue, $input_errors);
532
		if (count($input_errors)) {
533
			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)));
534
			return $q;
535
		}
536

    
537
		if (isset($queue['bandwidth'])) {
538
			switch ($queue['bandwidthtype']) {
539
				case "%":
540
					$myBw = $this->GetAvailableBandwidth() * $queue['bandwidth'] / 100;
541
					break;
542
				default:
543
					$myBw = $queue['bandwidth'] * get_bandwidthtype_scale($queue['bandwidthtype']);
544
					break;
545
			}
546
		}
547
		$q->SetAvailableBandwidth($myBw);
548
		$this->SetAvailableBandwidth($this->GetAvailableBandwidth() - $myBw);
549
		$this->queues[$q->GetQname()] = &$q;
550
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
551
		if (is_array($queue['queue'])) {
552
			foreach ($queue['queue'] as $key1 => $que) {
553
				array_push($path, $key1);
554
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
555
				array_pop($path);
556
			}
557
		}
558

    
559
		return $q;
560
	}
561

    
562
	/* interface here might be optional */
563
	function &find_queue($interface, $qname) {
564
		if ($qname == $this->GetQname()) {
565
			return $this;
566
		}
567
		foreach ($this->queues as $q) {
568
			$result =& $q->find_queue("", $qname);
569
			if ($result) {
570
				return $result;
571
			}
572
		}
573
	}
574

    
575
	function &find_parentqueue($interface, $qname) {
576
		if ($qname == $interface) {
577
			$result = NULL;
578
		} else if ($this->queues[$qname]) {
579
			$result = $this;
580
		} else if ($this->GetScheduler() <> "PRIQ") {
581
			foreach ($this->queues as $q) {
582
				$result = $q->find_parentqueue("", $qname);
583
				if ($result) {
584
					return $result;
585
				}
586
			}
587
		}
588
	}
589

    
590
	function build_tree() {
591
		global $shaperIFlist;
592

    
593
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
594
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
595
		if (is_array($this->queues)) {
596
			$tree .= "<ul>";
597
			foreach ($this->queues as $q) {
598
				$tree .= $q->build_tree();
599
			}
600
			$tree .= "</ul>";
601
		}
602
		$tree .= "</li>";
603
		return $tree;
604
	}
605

    
606
	function delete_queue() {
607
		foreach ($this->queues as $q) {
608
			$this->SetAvailableBandwidth($this->GetAvailableBandwidth() + $q->GetAvailableBandwidth());
609
			$q->delete_queue();
610
		}
611
		unset_object_by_reference($this->GetLink());
612
	}
613

    
614
	function delete_all() {
615
		if (count($this->queues)) {
616
			foreach ($this->queues as $q) {
617
				$q->delete_all();
618
				unset_object_by_reference($q->GetLink());
619
				unset($q);
620
			}
621
			unset($this->queues);
622
		}
623
	}
624

    
625
	/*
626
	 * First it spits:
627
	 * altq on $interface ..............
628
	 *	then it goes like
629
	 *	foreach ($queues as $qkey => $queue) {
630
	 *		this->queues[$qkey]->build_rule();
631
	 *	}
632
	 */
633
	function build_rules(&$default = false) {
634
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
635
			$default = false;
636
			$rules = " altq on " . get_real_interface($this->GetInterface());
637
			if ($this->GetScheduler()) {
638
				$rules .= " ".strtolower($this->GetScheduler());
639
			}
640
			if ($this->GetQlimit() > 0) {
641
				$rules .= " qlimit " . $this->GetQlimit() . " ";
642
			}
643
			if ($this->GetBandwidth()) {
644
				$rules .= " bandwidth ".trim($this->GetBandwidth());
645
				if ($this->GetBwscale()) {
646
					$rules .= $this->GetBwscale();
647
				}
648
			}
649
			if ($this->GetTbrConfig()) {
650
				$rules .= " tbrsize ".$this->GetTbrConfig();
651
			}
652
			if (count($this->queues)) {
653
				$i = count($this->queues);
654
				$rules .= " queue { ";
655
				foreach ($this->queues as $qkey => $qnone) {
656
					if ($i > 1) {
657
						$i--;
658
						$rules .= " {$qkey}, ";
659
					} else {
660
						$rules .= " {$qkey} ";
661
					}
662
				}
663
				$rules .= " } \n";
664
				foreach ($this->queues as $q) {
665
					$rules .= $q->build_rules($default);
666
				}
667
			}
668

    
669
			if ($default == false) {
670
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
671
				file_notice("Shaper", $error, "Error occurred", "");
672
				unset($error);
673
				return "\n";
674
			}
675
			$frule .= $rules;
676
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
677
			$rules = " altq on " . get_real_interface($this->GetInterface());
678
			if ($this->GetScheduler()) {
679
				$rules .= " ".strtolower($this->GetScheduler());
680
			}
681
			if ($this->GetQlimit() > 0) {
682
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
683
			}
684
			if ($this->GetBandwidth()) {
685
				$rules .= " bandwidth ".trim($this->GetBandwidth());
686
				if ($this->GetBwscale()) {
687
					$rules .= $this->GetBwscale();
688
				}
689
			}
690
			if ($this->GetTbrConfig()) {
691
				$rules .= " tbrsize ".$this->GetTbrConfig();
692
			}
693

    
694
			$rules .= " queue";
695
		}
696

    
697
		$rules .= " \n";
698
		return $rules;
699
	}
700

    
701
	function build_javascript() {
702
		$javascript = "<script type=\"text/javascript\">";
703
		$javascript .= "//<![CDATA[\n";
704
		$javascript .= "function mySuspend() {";
705
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
706
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
707
		$javascript .= "else if (document.all)";
708
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
709
		$javascript .= "}";
710

    
711
		$javascript .= "function myResume() {";
712
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
713
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
714
		$javascript .= "else if (document.all) ";
715
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
716
		$javascript .= "}";
717
		$javascript .= "//]]>";
718
		$javascript .= "</script>";
719

    
720
		return $javascript;
721
	}
722

    
723
	function build_shortform() {
724
		global $g;
725

    
726
		$altq =& $this;
727

    
728
		if ($altq) {
729
			$scheduler = ": " . $altq->GetScheduler();
730
		}
731

    
732
		$form = '<dl class="dl-horizontal">';
733
		$form .= '	<dt>';
734
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
735
		$form .= '	</dt>';
736
		$form .= '	<dd>';
737
		$form .=		$scheduler;
738
		$form .= '	</dd>';
739

    
740
		$form .= '	<dt>';
741
		$form .=		'Bandwidth';
742
		$form .= '	</dt>';
743
		$form .= '	<dd>';
744
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
745
		$form .= '	</dd>';
746

    
747
		$form .= '	<dt>';
748
		$form .= 'Disable';
749
		$form .= '	<dt>';
750
		$form .= '	<dd>';
751

    
752
		$form .= '<a class="btn btn-default btn-xs" href="firewall_shaper_queues.php?interface=';
753
		$form .= $this->GetInterface() . '&amp;queue=';
754
		$form .= $this->GetQname() . '&amp;action=delete">';
755
		$form .= gettext("Disable shaper on interface") . '</a>';
756

    
757
		$form .= '	</dd>';
758

    
759
		$form .= '</dl>';
760

    
761
		return $form;
762

    
763
	}
764

    
765
	/*
766
	 * For requesting the parameters of the root queues
767
	 * to the user like the traffic wizard does.
768
	 */
769
	function build_form() {
770

    
771
		$sform = new Form(new Form_Button(
772
			'Submit',
773
			'Save'
774
		));
775

    
776
		$sform->setAction("firewall_shaper.php");
777

    
778
		$section = new Form_Section(null);
779

    
780
		$section->addInput(new Form_Checkbox(
781
			'enabled',
782
			'Enable/Disable',
783
			'Enable/disable discipline and its children',
784
			($this->GetEnabled() == "on"),
785
			'on'
786
		));
787

    
788
		$section->addInput(new Form_StaticText(
789
			'Name',
790
			$this->GetQname()
791
		));
792

    
793
		$section->addInput(new Form_Select(
794
			'scheduler',
795
			'Scheduler Type',
796
			$this->GetScheduler(),
797
			array('HFSC' => 'HFSC',
798
				  'CBQ' => 'CBQ',
799
				  'FAIRQ' => 'FAIRQ',
800
				  'CODELQ' => 'CODELQ',
801
				  'PRIQ' => 'PRIQ')
802
		))->setHelp('Changing this changes all child queues! Beware you can lose information.');
803

    
804
		$group = new Form_group('Bandwidth');
805

    
806
		$group->add(new Form_Input(
807
			'bandwidth',
808
			null,
809
			'number',
810
			$this->GetBandwidth()
811
		));
812

    
813
		$group->add(new Form_Select(
814
			'bandwidthtype',
815
			null,
816
			$this->GetBwscale(),
817
			array('Kb' => 'Kbit/s',
818
				  'Mb' => 'Mbit/s',
819
				  'Gb' => 'Gbit/s',
820
				  'b' => 'Bit/s',
821
				  '%' => '%')
822
		));
823

    
824
		$section->add($group);
825

    
826
		$section->addInput(new Form_Input(
827
			'qlimit',
828
			'Queue Limit',
829
			'number',
830
			$this->GetQlimit()
831
		));
832

    
833
		$section->addInput(new Form_Input(
834
			'tbrconfig',
835
			'TRB Size',
836
			'number',
837
			$this->GetTbrConfig()
838
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
839
					'bandwidth are used to determine the size.');
840

    
841
		$section->addInput(new Form_Input(
842
			'interface',
843
			null,
844
			'hidden',
845
			$this->GetInterface()
846
		));
847

    
848
		$section->addInput(new Form_Input(
849
			'name',
850
			null,
851
			'hidden',
852
			$this->GetQname()
853
		));
854

    
855
		$sform->add($section);
856

    
857
		return($sform);
858
	}
859

    
860
	function update_altq_queue_data(&$data) {
861
		$this->ReadConfig($data);
862
	}
863

    
864
	/*
865
	 * Should call on each of it queues and subqueues
866
	 * the same function much like build_rules();
867
	 */
868
	function wconfig() {
869
		$cflink = &get_reference_to_me_in_config($this->GetLink());
870
		if (!is_array($cflink)) {
871
			$cflink = array();
872
		}
873
		$cflink['interface'] = $this->GetInterface();
874
		$cflink['name'] = $this->GetQname();
875
		$cflink['scheduler'] = $this->GetScheduler();
876
		$cflink['bandwidth'] = $this->GetBandwidth();
877
		$cflink['bandwidthtype'] = $this->GetBwscale();
878
		$cflink['qlimit'] = trim($this->GetQlimit());
879
		if (empty($cflink['qlimit'])) {
880
			unset($cflink['qlimit']);
881
		}
882
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
883
		if (empty($cflink['tbrconfig'])) {
884
			unset($cflink['tbrconfig']);
885
		}
886
		$cflink['enabled'] = $this->GetEnabled();
887
		if (empty($cflink['enabled'])) {
888
			unset($cflink['enabled']);
889
		}
890
	}
891

    
892
}
893

    
894
class priq_queue {
895
	var $qname;
896
	var $qinterface;
897
	var $qlimit;
898
	var $qpriority;
899
	var $description;
900
	var $isparent;
901
	var $qbandwidth;
902
	var $qbandwidthtype;
903
	var $qdefault = "";
904
	var $qrio = "";
905
	var $qred = "";
906
	var $qcodel = "";
907
	var $qecn = "";
908
	var $qack;
909
	var $qenabled = "";
910
	var $qparent;
911
	var $link;
912
	var $available_bw; /* in b/s */
913

    
914
	/* This is here to help with form building and building rules/lists */
915
	var $subqueues = array();
916

    
917
	/* Accessor functions */
918
	function GetAvailableBandwidth() {
919
		return $this->available_bw;
920
	}
921
	function SetAvailableBandwidth($bw) {
922
		$this->available_bw = $bw;
923
	}
924
	function SetLink($link) {
925
		$this->link = $link;
926
	}
927
	function GetLink() {
928
		return $this->link;
929
	}
930
	function &GetParent() {
931
		return $this->qparent;
932
	}
933
	function SetParent(&$parent) {
934
		$this->qparent = &$parent;
935
	}
936
	function GetEnabled() {
937
		return $this->qenabled;
938
	}
939
	function SetEnabled($value) {
940
		$this->qenabled = $value;
941
	}
942
	function CanHaveChildren() {
943
		return false;
944
	}
945
	function CanBeDeleted() {
946
		return true;
947
	}
948
	function GetQname() {
949
		return $this->qname;
950
	}
951
	function SetQname($name) {
952
		$this->qname = trim($name);
953
	}
954
	function GetBandwidth() {
955
		return $this->qbandwidth;
956
	}
957
	function SetBandwidth($bandwidth) {
958
		$this->qbandwidth = $bandwidth;
959
	}
960
	function GetInterface() {
961
		return $this->qinterface;
962
	}
963
	function SetInterface($name) {
964
		$this->qinterface = trim($name);
965
	}
966
	function GetQlimit() {
967
		return $this->qlimit;
968
	}
969
	function SetQlimit($limit) {
970
		$this->qlimit = $limit;
971
	}
972
	function GetQpriority() {
973
		return $this->qpriority;
974
	}
975
	function SetQpriority($priority) {
976
		$this->qpriority = $priority;
977
	}
978
	function GetDescription() {
979
		return $this->description;
980
	}
981
	function SetDescription($str) {
982
		$this->description = trim($str);
983
	}
984
	function GetFirstime() {
985
		return $this->firsttime;
986
	}
987
	function SetFirsttime($number) {
988
		$this->firsttime = $number;
989
	}
990
	function GetBwscale() {
991
		return $this->qbandwidthtype;
992
	}
993
	function SetBwscale($scale) {
994
		$this->qbandwidthtype = $scale;
995
	}
996
	function GetDefaultQueuePresent() {
997
		if ($this->GetDefault()) {
998
			return true;
999
		}
1000
		if (!empty($this->subqueues)) {
1001
			foreach ($this->subqueues as $q) {
1002
				if ($q->GetDefault()) {
1003
					return true;
1004
				}
1005
			}
1006
		}
1007

    
1008
		return false;
1009
	}
1010
	function GetDefault() {
1011
		return $this->qdefault;
1012
	}
1013
	function SetDefault($value = false) {
1014
		$this->qdefault = $value;
1015
	}
1016
	function GetCodel() {
1017
		return $this->codel;
1018
	}
1019
	function SetCodel($codel = false) {
1020
		$this->codel = $codel;
1021
	}
1022
	function GetRed() {
1023
		return $this->qred;
1024
	}
1025
	function SetRed($red = false) {
1026
		$this->qred = $red;
1027
	}
1028
	function GetRio() {
1029
		return $this->qrio;
1030
	}
1031
	function SetRio($rio = false) {
1032
		$this->qrio = $rio;
1033
	}
1034
	function GetEcn() {
1035
		return $this->qecn;
1036
	}
1037
	function SetEcn($ecn = false) {
1038
		$this->qecn = $ecn;
1039
	}
1040
	function GetAck() {
1041
		return $this->qack;
1042
	}
1043
	function SetAck($ack = false) {
1044
		$this->qack = $ack;
1045
	}
1046

    
1047
	function GetBwscaleText() {
1048
		switch ($this->qbandwidthtype) {
1049
			case "b":
1050
				$bwscaletext = "Bit/s";
1051
				break;
1052
			case "Kb":
1053
				$bwscaletext = "Kbit/s";
1054
				break;
1055
			case "Mb":
1056
				$bwscaletext = "Mbit/s";
1057
				break;
1058
			case "Gb":
1059
				$bwscaletext = "Gbit/s";
1060
				break;
1061
			default:
1062
				/* For others that do not need translating like % */
1063
				$bwscaletext = $this->qbandwidthtype;
1064
				break;
1065
		}
1066
		return $bwscaletext;
1067
	}
1068

    
1069
	function build_javascript() {
1070
		$javascript = "<script type=\"text/javascript\">";
1071
		$javascript .= "//<![CDATA[\n";
1072
		$javascript .= "function mySuspend() { \n";
1073
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1074
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1075
		$javascript .= "else if (document.all)\n";
1076
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1077
		$javascript .= "}\n";
1078

    
1079
		$javascript .= "function myResume() {\n";
1080
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1081
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1082
		$javascript .= "else if (document.all)\n";
1083
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1084
		$javascript .= "}\n";
1085
		$javascript .= "//]]>";
1086
		$javascript .= "</script>";
1087

    
1088
		return $javascript;
1089
	}
1090

    
1091
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1092

    
1093
	/*
1094
	 * Currently this will not be called unless we decide to clone a whole
1095
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1096
	 */
1097
	function copy_queue($interface, &$cflink) {
1098

    
1099
		$cflink['name'] = $this->GetQname();
1100
		$cflink['interface'] = $interface;
1101
		$cflink['qlimit'] = $this->GetQlimit();
1102
		$cflink['priority'] = $this->GetQpriority();
1103
		$cflink['description'] = $this->GetDescription();
1104
		$cflink['enabled'] = $this->GetEnabled();
1105
		$cflink['default'] = $this->GetDefault();
1106
		$cflink['red'] = $this->GetRed();
1107
		$cflink['codel'] = $this->GetCodel();
1108
		$cflink['rio'] = $this->GetRio();
1109
		$cflink['ecn'] = $this->GetEcn();
1110

    
1111
		if (is_array($this->subqueues)) {
1112
			$cflinkp['queue'] = array();
1113
			foreach ($this->subqueues as $q) {
1114
				$cflink['queue'][$q->GetQname()] = array();
1115
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1116
			}
1117
		}
1118
	}
1119

    
1120
	function clean_queue($sched) {
1121
		clean_child_queues($sched, $this->GetLink());
1122
		if (is_array($this->subqueues)) {
1123
			foreach ($this->subqueues as $q) {
1124
				$q->clean_queue($sched);
1125
			}
1126
		}
1127
	}
1128

    
1129
	function &get_queue_list(&$qlist) {
1130

    
1131
		$qlist[$this->GetQname()] = & $this;
1132
		if (is_array($this->subqueues)) {
1133
			foreach ($this->subqueues as $queue) {
1134
				$queue->get_queue_list($qlist);
1135
			}
1136
		}
1137
	}
1138

    
1139
	function delete_queue() {
1140
		unref_on_altq_queue_list($this->GetQname());
1141
		cleanup_queue_from_rules($this->GetQname());
1142
		unset_object_by_reference($this->GetLink());
1143
	}
1144

    
1145
	function delete_all() {
1146
		if (count($this->subqueues)) {
1147
			foreach ($this->subqueues as $q) {
1148
				$q->delete_all();
1149
				unset_object_by_reference($q->GetLink());
1150
				unset($q);
1151
			}
1152
			unset($this->subqueues);
1153
		}
1154
	}
1155

    
1156
	function &find_queue($interface, $qname) {
1157
		if ($qname == $this->GetQname()) {
1158
			return $this;
1159
		}
1160
	}
1161

    
1162
	function find_parentqueue($interface, $qname) { return; }
1163

    
1164
	function validate_input($data, &$input_errors) {
1165

    
1166
		$reqdfields[] = "name";
1167
		$reqdfieldsn[] = gettext("Name");
1168
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1169

    
1170
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1171
			$input_errors[] = gettext("Bandwidth must be an integer.");
1172
		}
1173
		if ($data['bandwidth'] < 0) {
1174
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1175
		}
1176
		if ($data['priority'] && (!is_numeric($data['priority']) ||
1177
		    ($data['priority'] < 1) || ($data['priority'] > 15))) {
1178
			$input_errors[] = gettext("The priority must be an integer between 1 and 15.");
1179
		}
1180
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1181
				$input_errors[] = gettext("Queue limit must be an integer");
1182
		}
1183
		if ($data['qlimit'] < 0) {
1184
				$input_errors[] = gettext("Queue limit must be positive");
1185
		}
1186
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1187
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1188
		}
1189
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1190
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1191
		}
1192
		$default = $this->GetDefault();
1193
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1194
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1195
		}
1196
	}
1197

    
1198
	function ReadConfig(&$q) {
1199
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1200
			$this->SetQname($q['newname']);
1201
		} else if (!empty($q['newname'])) {
1202
			$this->SetQname($q['newname']);
1203
		} else if (isset($q['name'])) {
1204
			$this->SetQname($q['name']);
1205
		}
1206
		if (isset($q['interface'])) {
1207
			$this->SetInterface($q['interface']);
1208
		}
1209
		$this->SetBandwidth($q['bandwidth']);
1210
		if ($q['bandwidthtype'] <> "") {
1211
			$this->SetBwscale($q['bandwidthtype']);
1212
		}
1213
		if (!empty($q['qlimit'])) {
1214
			$this->SetQlimit($q['qlimit']);
1215
		} else {
1216
			$this->SetQlimit(""); // Default
1217
		}
1218
		if (!empty($q['priority'])) {
1219
			$this->SetQPriority($q['priority']);
1220
		} else {
1221
			$this->SetQpriority("");
1222
		}
1223
		if (!empty($q['description'])) {
1224
			$this->SetDescription($q['description']);
1225
		} else {
1226
			$this->SetDescription("");
1227
		}
1228
		if (!empty($q['red'])) {
1229
			$this->SetRed($q['red']);
1230
		} else {
1231
			$this->SetRed();
1232
		}
1233
		if (!empty($q['codel'])) {
1234
			$this->SetCodel($q['codel']);
1235
		} else {
1236
			$this->SetCodel();
1237
		}
1238
		if (!empty($q['rio'])) {
1239
			$this->SetRio($q['rio']);
1240
		} else {
1241
			$this->SetRio();
1242
		}
1243
		if (!empty($q['ecn'])) {
1244
			$this->SetEcn($q['ecn']);
1245
		} else {
1246
			$this->SetEcn();
1247
		}
1248
		if (!empty($q['default'])) {
1249
			$this->SetDefault($q['default']);
1250
		} else {
1251
			$this->SetDefault();
1252
		}
1253
		if (!empty($q['enabled'])) {
1254
			$this->SetEnabled($q['enabled']);
1255
		} else {
1256
			$this->SetEnabled("");
1257
		}
1258
	}
1259

    
1260
	function build_tree() {
1261
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1262
		$tree .= "\" ";
1263
		$tmpvalue = $this->GetDefault();
1264
		if (!empty($tmpvalue)) {
1265
			$tree .= " class=\"navlnk\"";
1266
		}
1267
		$tree .= " >" . $this->GetQname() . "</a>";
1268
		/*
1269
		 * Not needed here!
1270
		 * if (is_array($queues) {
1271
		 *	  $tree .= "<ul>";
1272
		 *	  foreach ($q as $queues)
1273
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1274
		 *	  endforeach
1275
		 *	  $tree .= "</ul>";
1276
		 * }
1277
		 */
1278

    
1279
		$tree .= "</li>";
1280

    
1281
		return $tree;
1282
	}
1283

    
1284
	/* Should return something like:
1285
	 * queue $qname on $qinterface bandwidth ....
1286
	 */
1287
	function build_rules(&$default = false) {
1288
		$pfq_rule = " queue ". $this->qname;
1289
		if ($this->GetInterface()) {
1290
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1291
		}
1292
		$tmpvalue = $this->GetQpriority();
1293
		if (!empty($tmpvalue)) {
1294
			$pfq_rule .= " priority ".$this->GetQpriority();
1295
		}
1296
		$tmpvalue = $this->GetQlimit();
1297
		if (!empty($tmpvalue)) {
1298
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1299
		}
1300
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1301
			$pfq_rule .= " priq ( ";
1302
			$tmpvalue = $this->GetRed();
1303
			if (!empty($tmpvalue)) {
1304
				$comma = 1;
1305
				$pfq_rule .= " red ";
1306
			}
1307
			$tmpvalue = $this->GetRio();
1308
			if (!empty($tmpvalue)) {
1309
				if ($comma) {
1310
					$pfq_rule .= " ,";
1311
				}
1312
				$comma = 1;
1313
				$pfq_rule .= " rio ";
1314
			}
1315
			$tmpvalue = $this->GetEcn();
1316
			if (!empty($tmpvalue)) {
1317
				if ($comma) {
1318
					$pfq_rule .= " ,";
1319
				}
1320
				$comma = 1;
1321
				$pfq_rule .= " ecn ";
1322
			}
1323
			$tmpvalue = $this->GetCodel();
1324
			if (!empty($tmpvalue)) {
1325
				if ($comma) {
1326
					$pfq_rule .= " ,";
1327
				}
1328
				$comma = 1;
1329
				$pfq_rule .= " codel ";
1330
			}
1331
			$tmpvalue = $this->GetDefault();
1332
			if (!empty($tmpvalue)) {
1333
				if ($comma) {
1334
					$pfq_rule .= " ,";
1335
				}
1336
				$pfq_rule .= " default ";
1337
				$default = true;
1338
			}
1339
			$pfq_rule .= " ) ";
1340
		}
1341

    
1342
		$pfq_rule .= " \n";
1343

    
1344
		return $pfq_rule;
1345
	}
1346

    
1347
	/*
1348
	 * To return the html form to show to user
1349
	 * for getting the parameters.
1350
	 * Should do even for first time when the
1351
	 * object is created and later when we may
1352
	 * need to update it. (2)
1353
	 */
1354

    
1355
	function build_form() {
1356

    
1357
		$sform = new Form(new Form_Button(
1358
			'Submit',
1359
			'Save'
1360
		));
1361

    
1362
		$sform->setAction("firewall_shaper.php");
1363

    
1364
		$section = new Form_Section("");
1365

    
1366
		$section->addInput(new Form_Checkbox(
1367
			'enabled',
1368
			'Enable/Disable',
1369
			'Enable/disable discipline and its children',
1370
			($this->GetEnabled() == "on"),
1371
			'on'
1372
		));
1373

    
1374
		$section->addInput(new Form_Input(
1375
			'newname',
1376
			'Name',
1377
			'text',
1378
			$this->GetQname()
1379
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1380

    
1381
		$section->addInput(new Form_Input(
1382
			'name',
1383
			null,
1384
			'hidden',
1385
			$this->GetQname()
1386
		));
1387

    
1388
		$section->addInput(new Form_Input(
1389
			'priority',
1390
			'Priority',
1391
			'number',
1392
			$this->GetQpriority(),
1393
			['min' => '0', 'max'=> '7']
1394
		))->setHelp('For hfsc, the range is 0 to 7. The default is 1. Hfsc queues with a higher priority are preferred in the case of overload.');
1395

    
1396
		$section->addInput(new Form_Input(
1397
			'qlimit',
1398
			'Queue Limit',
1399
			'number',
1400
			$this->GetQlimit()
1401
		))->setHelp('Queue limit in packets.');
1402

    
1403
		$group = new Form_Group('Scheduler options');
1404

    
1405
		if (empty($this->subqueues)) {
1406
			$group->add(new Form_Checkbox(
1407
				'default',
1408
				null,
1409
				null,
1410
				$this->GetDefault(),
1411
				'default'
1412
			))->setHelp('Default Queue');
1413
		}
1414

    
1415
		$group->add(new Form_Checkbox(
1416
			'red',
1417
			null,
1418
			null,
1419
			!empty($this->GetRed())
1420
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#red">' . gettext('Random Early Detection') . '</a>');
1421

    
1422
		$group->add(new Form_Checkbox(
1423
			'rio',
1424
			null,
1425
			null,
1426
			!empty($this->GetRio())
1427
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#rio">' . gettext('Random Early Detection In and Out') . '</a>');
1428

    
1429
		$group->add(new Form_Checkbox(
1430
			'ecn',
1431
			null,
1432
			null,
1433
			!empty($this->GetEcn())
1434
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Explicit Congestion Notification') . '</a>');
1435

    
1436
		$group->add(new Form_Checkbox(
1437
			'codel',
1438
			null,
1439
			null,
1440
			!empty($this->GetCodel())
1441
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Codel Active Queue') . '</a>');
1442

    
1443
		$group->setHelp('Select options for this queue');
1444

    
1445
		$section->add($group);
1446

    
1447
		$section->addInput(new Form_Input(
1448
			'description',
1449
			'Description',
1450
			'text',
1451
			$this->GetDescription()
1452
		));
1453

    
1454
		$sform->add($section);
1455

    
1456
		$sform->addGlobal(new Form_Input(
1457
			'interface',
1458
			null,
1459
			'hidden',
1460
			$this->GetInterface()
1461
		));
1462

    
1463
		$sform->addGlobal(new Form_Input(
1464
			'name',
1465
			null,
1466
			'hidden',
1467
			$this->GetQname()
1468
		));
1469

    
1470
		return($sform);
1471
	}
1472

    
1473
	function build_shortform() {
1474
		/* XXX: Hacks in sight. Mostly layer violations!  */
1475
		global $g, $altq_list_queues;
1476
		global $shaperIFlist;
1477

    
1478
		$altq =& $altq_list_queues[$this->GetInterface()];
1479

    
1480
		if ($altq) {
1481
			$scheduler = $altq->GetScheduler();
1482
		}
1483

    
1484
		$form = '<dl class="dl-horizontal">';
1485
		$form .= '	<dt>';
1486
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1487
		$form .= '	</dt>';
1488
		$form .= '	<dd>';
1489
		$form .=		$scheduler;
1490
		$form .= '	</dd>';
1491

    
1492
		$form .= '	<dt>';
1493
		$form .=		'Bandwidth';
1494
		$form .= '	</dt>';
1495
		$form .= '	<dd>';
1496
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1497
		$form .= '	</dd>';
1498

    
1499
		$tmpvalue = $this->GetQpriority();
1500
		if (!empty($tmpvalue)) {
1501
			$form .= '	<dt>';
1502
			$form .=		'Priority';
1503
			$form .= '	<dt>';
1504
			$form .= '	<dd>';
1505
			$form .=		'On';
1506
			$form .= '	</dd>';
1507
		}
1508

    
1509
		$tmpvalue = $this->GetDefault();
1510
		if (!empty($tmpvalue)) {
1511
			$form .= '	<dt>';
1512
			$form .=		'Default';
1513
			$form .= '	<dt>';
1514
			$form .= '	<dd>';
1515
			$form .=		'On';
1516
			$form .= '	</dd>';
1517
		}
1518

    
1519
			$form .= '	<dt>';
1520
			$form .= 'Delete';
1521
			$form .= '	<dt>';
1522
			$form .= '	<dd>';
1523

    
1524
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1525
			$form .= $this->GetInterface() . '&amp;queue=';
1526
			$form .= $this->GetQname() . '&amp;action=delete">';
1527
			$form .= gettext("Delete queue from interface") . '</a>';
1528

    
1529
			$form .= '	</dd>';
1530

    
1531
			$form .= '</dl>';
1532

    
1533
		return $form;
1534

    
1535
	}
1536

    
1537
	function update_altq_queue_data(&$q) {
1538
		$this->ReadConfig($q);
1539
	}
1540

    
1541
	function wconfig() {
1542
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1543
		if (!is_array($cflink)) {
1544
			$cflink = array();
1545
		}
1546
		$cflink['name'] = $this->GetQname();
1547
		$cflink['interface'] = $this->GetInterface();
1548
		$cflink['qlimit'] = trim($this->GetQlimit());
1549
		if (empty($cflink['qlimit'])) {
1550
			unset($cflink['qlimit']);
1551
		}
1552
		$cflink['priority'] = trim($this->GetQpriority());
1553
		if (empty($cflink['priority'])) {
1554
			unset($cflink['priority']);
1555
		}
1556
		$cflink['description'] = trim($this->GetDescription());
1557
		if (empty($cflink['description'])) {
1558
			unset($cflink['description']);
1559
		}
1560
		$cflink['enabled'] = trim($this->GetEnabled());
1561
		if (empty($cflink['enabled'])) {
1562
			unset($cflink['enabled']);
1563
		}
1564
		$cflink['default'] = trim($this->GetDefault());
1565
		if (empty($cflink['default'])) {
1566
			unset($cflink['default']);
1567
		}
1568
		$cflink['red'] = trim($this->GetRed());
1569
		if (empty($cflink['red'])) {
1570
			unset($cflink['red']);
1571
		}
1572
		$cflink['codel'] = trim($this->GetCodel());
1573
		if (empty($cflink['codel'])) {
1574
			unset($cflink['codel']);
1575
		}
1576
		$cflink['rio'] = trim($this->GetRio());
1577
		if (empty($cflink['rio'])) {
1578
			unset($cflink['rio']);
1579
		}
1580
		$cflink['ecn'] = trim($this->GetEcn());
1581
		if (empty($cflink['ecn'])) {
1582
			unset($cflink['ecn']);
1583
		}
1584
	}
1585
}
1586

    
1587
class hfsc_queue extends priq_queue {
1588
	/* realtime */
1589
	var $realtime;
1590
	var $r_m1;
1591
	var $r_d;
1592
	var $r_m2;
1593
	/* linkshare */
1594
	var $linkshare;
1595
	var $l_m1;
1596
	var $l_d;
1597
	var $l_m2;
1598
	/* upperlimit */
1599
	var $upperlimit;
1600
	var $u_m1;
1601
	var $u_d;
1602
	var $u_m2;
1603

    
1604
	/*
1605
	 * HFSC can have nested queues.
1606
	 */
1607
	function CanHaveChildren() {
1608
		return true;
1609
	}
1610
	function GetRealtime() {
1611
		return $this->realtime;
1612
	}
1613
	function GetR_m1() {
1614
		return $this->r_m1;
1615
	}
1616
	function GetR_d() {
1617
		return $this->r_d;
1618
	}
1619
	function GetR_m2() {
1620
		return $this->r_m2;
1621
	}
1622
	function SetRealtime() {
1623
		$this->realtime = "on";
1624
	}
1625
	function DisableRealtime() {
1626
		$this->realtime = "";
1627
	}
1628
	function SetR_m1($value) {
1629
		$this->r_m1 = $value;
1630
	}
1631
	function SetR_d($value) {
1632
		$this->r_d = $value;
1633
	}
1634
	function SetR_m2($value) {
1635
		$this->r_m2 = $value;
1636
	}
1637
	function GetLinkshare() {
1638
		return $this->linkshare;
1639
	}
1640
	function DisableLinkshare() {
1641
		$this->linkshare = "";
1642
	}
1643
	function GetL_m1() {
1644
		return $this->l_m1;
1645
	}
1646
	function GetL_d() {
1647
		return $this->l_d;
1648
	}
1649
	function GetL_m2() {
1650
		return $this->l_m2;
1651
	}
1652
	function SetLinkshare() {
1653
		$this->linkshare = "on";
1654
	}
1655
	function SetL_m1($value) {
1656
		$this->l_m1 = $value;
1657
	}
1658
	function SetL_d($value) {
1659
		$this->l_d = $value;
1660
	}
1661
	function SetL_m2($value) {
1662
		$this->l_m2 = $value;
1663
	}
1664
	function GetUpperlimit() {
1665
		return $this->upperlimit;
1666
	}
1667
	function GetU_m1() {
1668
		return $this->u_m1;
1669
	}
1670
	function GetU_d() {
1671
		return $this->u_d;
1672
	}
1673
	function GetU_m2() {
1674
		return $this->u_m2;
1675
	}
1676
	function SetUpperlimit() {
1677
		$this->upperlimit = "on";
1678
	}
1679
	function DisableUpperlimit() {
1680
		$this->upperlimit = "";
1681
	}
1682
	function SetU_m1($value) {
1683
		$this->u_m1 = $value;
1684
	}
1685
	function SetU_d($value) {
1686
		$this->u_d = $value;
1687
	}
1688
	function SetU_m2($value) {
1689
		$this->u_m2 = $value;
1690
	}
1691

    
1692
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1693

    
1694
		if (!is_array($this->subqueues)) {
1695
			$this->subqueues = array();
1696
		}
1697
		$q =& new hfsc_queue();
1698
		$q->SetInterface($this->GetInterface());
1699
		$q->SetParent($this);
1700
		$q->ReadConfig($qname);
1701
		$q->validate_input($qname, $input_errors);
1702
		if (count($input_errors)) {
1703
			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)));
1704
			return $q;
1705
		}
1706

    
1707
		$q->SetEnabled("on");
1708
		$q->SetLink($path);
1709
		switch ($q->GetBwscale()) {
1710
			case "%":
1711
				$myBw = $this->GetAvailableBandwidth() * $qname['bandwidth'] / 100;
1712
				break;
1713
			default:
1714
				$myBw = $qname['bandwidth'] * get_bandwidthtype_scale($q->GetBwscale());
1715
				break;
1716
		}
1717
		$q->SetAvailableBandwidth($myBw);
1718
		$this->SetAvailableBandwidth($this->GetAvailableBandwidth() - $myBw);
1719

    
1720
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1721
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1722
		if (is_array($qname['queue'])) {
1723
			foreach ($qname['queue'] as $key1 => $que) {
1724
				array_push($path, $key1);
1725
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1726
				array_pop($path);
1727
			}
1728
		}
1729

    
1730
		return $q;
1731
	}
1732

    
1733
	function copy_queue($interface, &$cflink) {
1734

    
1735
		$cflink['name'] = $this->GetQname();
1736
		$cflink['interface'] = $interface;
1737
		$cflink['qlimit'] = trim($this->GetQlimit());
1738
		if (empty($cflink['qlimit'])) {
1739
			unset($cflink['qlimit']);
1740
		}
1741
		$cflink['priority'] = trim($this->GetQpriority());
1742
		if (empty($cflink['priority'])) {
1743
			unset($cflink['priority']);
1744
		}
1745
		$cflink['description'] = trim($this->GetDescription());
1746
		if (empty($cflink['description'])) {
1747
			unset($cflink['description']);
1748
		}
1749
		$cflink['bandwidth'] = $this->GetBandwidth();
1750
		$cflink['bandwidthtype'] = $this->GetBwscale();
1751
		$cflink['enabled'] = trim($this->GetEnabled());
1752
		if (empty($cflink['enabled'])) {
1753
			unset($cflink['enabled']);
1754
		}
1755
		$cflink['default'] = trim($this->GetDefault());
1756
		if (empty($cflink['default'])) {
1757
			unset($cflink['default']);
1758
		}
1759
		$cflink['red'] = trim($this->GetRed());
1760
		if (empty($cflink['red'])) {
1761
			unset($cflink['red']);
1762
		}
1763
		$cflink['rio'] = trim($this->GetRio());
1764
		if (empty($cflink['rio'])) {
1765
			unset($cflink['rio']);
1766
		}
1767
		$cflink['ecn'] = trim($this->GetEcn());
1768
		if (empty($cflink['ecn'])) {
1769
			unset($cflink['ecn']);
1770
		}
1771
		if ($this->GetLinkshare() <> "") {
1772
			if ($this->GetL_m1() <> "") {
1773
				$cflink['linkshare1'] = $this->GetL_m1();
1774
				$cflink['linkshare2'] = $this->GetL_d();
1775
				$cflink['linkshare'] = "on";
1776
			} else {
1777
				unset($cflink['linkshare1']);
1778
				unset($cflink['linkshare2']);
1779
				unset($cflink['linkshare']);
1780
			}
1781
			if ($this->GetL_m2() <> "") {
1782
				$cflink['linkshare3'] = $this->GetL_m2();
1783
				$cflink['linkshare'] = "on";
1784
			} else {
1785
				unset($cflink['linkshare3']);
1786
				unset($cflink['linkshare']);
1787
			}
1788
		}
1789
		if ($this->GetRealtime() <> "") {
1790
			if ($this->GetR_m1() <> "") {
1791
				$cflink['realtime1'] = $this->GetR_m1();
1792
				$cflink['realtime2'] = $this->GetR_d();
1793
				$cflink['realtime'] = "on";
1794
			} else {
1795
				unset($cflink['realtime1']);
1796
				unset($cflink['realtime2']);
1797
				unset($cflink['realtime']);
1798
			}
1799
			if ($this->GetR_m2() <> "") {
1800
				$cflink['realtime3'] = $this->GetR_m2();
1801
				$cflink['realtime'] = "on";
1802
			} else {
1803
				unset($cflink['realtime3']);
1804
				unset($cflink['realtime']);
1805
			}
1806
		}
1807
		if ($this->GetUpperlimit() <> "") {
1808
			if ($this->GetU_m1() <> "") {
1809
				$cflink['upperlimit1'] = $this->GetU_m1();
1810
				$cflink['upperlimit2'] = $this->GetU_d();
1811
				$cflink['upperlimit'] = "on";
1812
			} else {
1813
				unset($cflink['upperlimit']);
1814
				unset($cflink['upperlimit1']);
1815
				unset($cflink['upperlimit2']);
1816
			}
1817
			if ($this->GetU_m2() <> "") {
1818
				$cflink['upperlimit3'] = $this->GetU_m2();
1819
				$cflink['upperlimit'] = "on";
1820
			} else {
1821
				unset($cflink['upperlimit3']);
1822
				unset($cflink['upperlimit']);
1823
			}
1824
		}
1825

    
1826
		if (is_array($this->subqueues)) {
1827
			$cflinkp['queue'] = array();
1828
			foreach ($this->subqueues as $q) {
1829
				$cflink['queue'][$q->GetQname()] = array();
1830
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1831
			}
1832
		}
1833
	}
1834

    
1835
	function delete_queue() {
1836
		unref_on_altq_queue_list($this->GetQname());
1837
		cleanup_queue_from_rules($this->GetQname());
1838
		$parent =& $this->GetParent();
1839
		foreach ($this->subqueues as $q) {
1840
			$this->SetAvailableBandwidth($this->GetAvailableBandwidth() + $q->GetAvailableBandwidth());
1841
				$q->delete_queue();
1842
		}
1843
		unset_object_by_reference($this->GetLink());
1844
	}
1845

    
1846
	/*
1847
	 * Should search even its children
1848
	 */
1849
	function &find_queue($interface, $qname) {
1850
		if ($qname == $this->GetQname()) {
1851
			return $this;
1852
		}
1853

    
1854
		foreach ($this->subqueues as $q) {
1855
			$result =& $q->find_queue("", $qname);
1856
			if ($result) {
1857
				return $result;
1858
			}
1859
		}
1860
	}
1861

    
1862
	function &find_parentqueue($interface, $qname) {
1863
		if ($this->subqueues[$qname]) {
1864
			return $this;
1865
		}
1866
		foreach ($this->subqueues as $q) {
1867
			$result = $q->find_parentqueue("", $qname);
1868
			if ($result) {
1869
				return $result;
1870
			}
1871
		}
1872
	}
1873

    
1874
	function validate_input($data, &$input_errors) {
1875
		parent::validate_input($data, $input_errors);
1876

    
1877
		$reqdfields[] = "bandwidth";
1878
		$reqdfieldsn[] = gettext("Bandwidth");
1879
		$reqdfields[] = "bandwidthtype";
1880
		$reqdfieldsn[] = gettext("Bandwidthtype");
1881

    
1882
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1883

    
1884
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
1885
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1886
				$input_errors[] = gettext("Bandwidth must be an integer.");
1887
			}
1888

    
1889
			if ($data['bandwidth'] < 0) {
1890
				$input_errors[] = gettext("Bandwidth cannot be negative.");
1891
			}
1892

    
1893
			if ($data['bandwidthtype'] == "%") {
1894
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1895
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100 bounds.");
1896
				}
1897
			}
1898
		/*
1899
			$parent =& $this->GetParent();
1900
			switch ($data['bandwidthtype']) {
1901
			case "%":
1902
				$myBw = $parent->GetAvailableBandwidth() * floatval($data['bandwidth']) / 100;
1903
			default:
1904
				$mybw = floatval($data['bandwidth']) * get_bandwidthtype_scale($data['bandwidthtype']);
1905
				break;
1906
			}
1907
			if ($parent->GetAvailableBandwidth() < $myBw) {
1908
				$input_errors[] = "The sum of children bandwidth exceeds that of the parent.";
1909
			}
1910
		*/
1911
		}
1912

    
1913
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
1914
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
1915
		}
1916
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
1917
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
1918
		}
1919
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
1920
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
1921
		}
1922
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
1923
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
1924
		}
1925
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
1926
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
1927
		}
1928

    
1929
		/*
1930
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
1931
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
1932
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
1933
			if (floatval($bw_1) < floatval($bw_2)) {
1934
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
1935
			}
1936

    
1937
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1938
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
1939
			}
1940
		}
1941
		*/
1942
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
1943
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
1944
		}
1945
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
1946
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
1947
		}
1948
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
1949
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
1950
		}
1951
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
1952
			$input_errors[] = gettext("linkshare d value needs to be numeric");
1953
		}
1954
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
1955
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
1956
		}
1957
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
1958
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
1959
		}
1960
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
1961
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
1962
		}
1963

    
1964
		/*
1965
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
1966
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
1967
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
1968
			if (floatval($bw_1) < floatval($bw_2)) {
1969
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
1970
			}
1971

    
1972
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1973
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
1974
			}
1975
		}
1976
		*/
1977

    
1978
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
1979
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
1980
		}
1981
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
1982
			$input_errors[] = gettext("realtime d value needs to be numeric");
1983
		}
1984
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
1985
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
1986
		}
1987

    
1988
		/*
1989
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
1990
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
1991
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
1992
			if (floatval($bw_1) < floatval($bw_2)) {
1993
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
1994
			}
1995

    
1996
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1997
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
1998
			}
1999
		}
2000
		*/
2001
	}
2002

    
2003
	function ReadConfig(&$cflink) {
2004
		if (!empty($cflink['linkshare'])) {
2005
			if (!empty($cflink['linkshare1'])) {
2006
				$this->SetL_m1($cflink['linkshare1']);
2007
				$this->SetL_d($cflink['linkshare2']);
2008
				$this->SetLinkshare();
2009
			} else {
2010
				$this->SetL_m1("");
2011
				$this->SetL_d("");
2012
				$this->DisableLinkshare();
2013
			}
2014
			if (!empty($cflink['linkshare3'])) {
2015
				$this->SetL_m2($cflink['linkshare3']);
2016
				$this->SetLinkshare();
2017
			}
2018
		} else {
2019
			$this->DisableLinkshare();
2020
		}
2021
		if (!empty($cflink['realtime'])) {
2022
			if (!empty($cflink['realtime1'])) {
2023
				$this->SetR_m1($cflink['realtime1']);
2024
				$this->SetR_d($cflink['realtime2']);
2025
				$this->SetRealtime();
2026
			} else {
2027
				$this->SetR_m1("");
2028
				$this->SetR_d("");
2029
				$this->DisableRealtime();
2030
			}
2031
			if (!empty($cflink['realtime3'])) {
2032
				$this->SetR_m2($cflink['realtime3']);
2033
				$this->SetRealtime();
2034
			}
2035
		} else {
2036
			$this->DisableRealtime();
2037
		}
2038
		if (!empty($cflink['upperlimit'])) {
2039
			if (!empty($cflink['upperlimit1'])) {
2040
				$this->SetU_m1($cflink['upperlimit1']);
2041
				$this->SetU_d($cflink['upperlimit2']);
2042
				$this->SetUpperlimit();
2043
			} else {
2044
				$this->SetU_m1("");
2045
				$this->SetU_d("");
2046
				$this->DisableUpperlimit();
2047
			}
2048
			if (!empty($cflink['upperlimit3'])) {
2049
				$this->SetU_m2($cflink['upperlimit3']);
2050
				$this->SetUpperlimit();
2051
			}
2052
		} else {
2053
			$this->DisableUpperlimit();
2054
		}
2055
		parent::ReadConfig($cflink);
2056
	}
2057

    
2058
	function build_tree() {
2059
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2060
		$tree .= "\" ";
2061
		$tmpvalue = $this->GetDefault();
2062
		if (!empty($tmpvalue)) {
2063
			$tree .= " class=\"navlnk\"";
2064
		}
2065
		$tree .= " >" . $this->GetQname() . "</a>";
2066
		if (is_array($this->subqueues)) {
2067
			$tree .= "<ul>";
2068
			foreach ($this->subqueues as $q) {
2069
				$tree .= $q->build_tree();
2070
			}
2071
			$tree .= "</ul>";
2072
		}
2073
		$tree .= "</li>";
2074
		return $tree;
2075
	}
2076

    
2077
	/* Even this should take children into consideration */
2078
	function build_rules(&$default = false) {
2079

    
2080
		$pfq_rule = " queue ". $this->qname;
2081
		if ($this->GetInterface()) {
2082
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2083
		}
2084
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2085
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2086
		}
2087

    
2088
		$tmpvalue = $this->GetQlimit();
2089
		if (!empty($tmpvalue)) {
2090
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2091
		}
2092
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2093
			$pfq_rule .= " hfsc ( ";
2094
			$tmpvalue = $this->GetRed();
2095
			if (!empty($tmpvalue)) {
2096
				$comma = 1;
2097
				$pfq_rule .= " red ";
2098
			}
2099

    
2100
			$tmpvalue = $this->GetRio();
2101
			if (!empty($tmpvalue)) {
2102
				if ($comma) {
2103
					$pfq_rule .= " ,";
2104
				}
2105
				$comma = 1;
2106
				$pfq_rule .= " rio ";
2107
			}
2108
			$tmpvalue = $this->GetEcn();
2109
			if (!empty($tmpvalue)) {
2110
				if ($comma) {
2111
					$pfq_rule .= " ,";
2112
				}
2113
				$comma = 1;
2114
				$pfq_rule .= " ecn ";
2115
			}
2116
			$tmpvalue = $this->GetCodel();
2117
			if (!empty($tmpvalue)) {
2118
				if ($comma) {
2119
					$pfq_rule .= " ,";
2120
				}
2121
				$comma = 1;
2122
				$pfq_rule .= " codel ";
2123
			}
2124
			$tmpvalue = $this->GetDefault();
2125
			if (!empty($tmpvalue)) {
2126
				if ($comma) {
2127
					$pfq_rule .= " ,";
2128
				}
2129
				$comma = 1;
2130
				$pfq_rule .= " default ";
2131
				$default = true;
2132
			}
2133

    
2134
			if ($this->GetRealtime() <> "") {
2135
				if ($comma) {
2136
					$pfq_rule .= " , ";
2137
				}
2138
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2139
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2140
				} else if ($this->GetR_m2() <> "") {
2141
					$pfq_rule .= " realtime " . $this->GetR_m2();
2142
				}
2143
				$comma = 1;
2144
			}
2145
			if ($this->GetLinkshare() <> "") {
2146
				if ($comma) {
2147
					$pfq_rule .= " ,";
2148
				}
2149
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2150
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2151
				} else if ($this->GetL_m2() <> "") {
2152
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2153
				}
2154
				$comma = 1;
2155
			}
2156
			if ($this->GetUpperlimit() <> "") {
2157
				if ($comma) {
2158
					$pfq_rule .= " ,";
2159
				}
2160
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2161
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2162
				} else if ($this->GetU_m2() <> "") {
2163
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2164
				}
2165
			}
2166
			$pfq_rule .= " ) ";
2167
		}
2168
		if (count($this->subqueues)) {
2169
			$i = count($this->subqueues);
2170
			$pfq_rule .= " { ";
2171
			foreach ($this->subqueues as $qkey => $qnone) {
2172
				if ($i > 1) {
2173
					$i--;
2174
					$pfq_rule .= " {$qkey}, ";
2175
				} else {
2176
					$pfq_rule .= " {$qkey} ";
2177
				}
2178
			}
2179
			$pfq_rule .= " } \n";
2180
			foreach ($this->subqueues as $q) {
2181
				$pfq_rule .= $q->build_rules($default);
2182
			}
2183
		}
2184

    
2185
		$pfq_rule .= " \n";
2186

    
2187
		return $pfq_rule;
2188
	}
2189

    
2190
	function build_javascript() {
2191

    
2192
		$javascript = <<<EOJS
2193
<script type="text/javascript">
2194
//<![CDATA[
2195
	events.push(function(){
2196

    
2197
		// Disables the specified input element
2198
		function disableInput(id, disable) {
2199
			$('#' + id).prop("disabled", disable);
2200
		}
2201

    
2202
		// Upperlimit
2203
		function enable_upperlimit() {
2204
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2205
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2206
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2207
		}
2208

    
2209
		$('#upperlimit').click(function () {
2210
			enable_upperlimit();
2211
		});
2212

    
2213
		enable_upperlimit();
2214

    
2215
		// realtime
2216
		function enable_realtime() {
2217
			disableInput('realtime1', !$('#realtime').prop('checked'));
2218
			disableInput('realtime2', !$('#realtime').prop('checked'));
2219
			disableInput('realtime3', !$('#realtime').prop('checked'));
2220
		}
2221

    
2222
		$('#realtime').click(function () {
2223
			enable_realtime();
2224
		});
2225

    
2226
		enable_realtime();
2227

    
2228
		// linkshare
2229
		function enable_linkshare() {
2230
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2231
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2232
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2233
		}
2234

    
2235
		$('#linkshare').click(function () {
2236
			enable_linkshare();
2237
		});
2238

    
2239
		enable_linkshare();
2240
	});
2241
//]]>
2242
</script>
2243
EOJS;
2244

    
2245
		return $javascript;
2246
	}
2247

    
2248
	function build_form() {
2249

    
2250
		$sform = parent::build_form();
2251

    
2252
		$section = new Form_Section('Service Curve (sc)');
2253

    
2254
		$group = new Form_Group('Bandwidth');
2255

    
2256
		$group->add(new Form_Input(
2257
			'bandwidth',
2258
			null,
2259
			'number',
2260
			$this->GetBandwidth()
2261
		));
2262

    
2263
		$group->add(new Form_Select(
2264
			'bandwidthtype',
2265
			null,
2266
			$this->GetBwscale(),
2267
			array('Kb' => 'Kbit/s',
2268
				  'Mb' => 'Mbit/s',
2269
				  'Gb' => 'Gbit/s',
2270
				  'b' => 'Bit/s',
2271
				  '%' => '%')
2272
		));
2273

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

    
2276
		$section->add($group);
2277

    
2278
		$group = new Form_Group('Max bandwidth for queue.');
2279

    
2280
		$group->add(new Form_Checkbox(
2281
			'upperlimit',
2282
			null,
2283
			'Upper Limit',
2284
			($this->GetUpperlimit()<> "")
2285
		));
2286

    
2287
		$group->add(new Form_Input(
2288
			'upperlimit1',
2289
			null,
2290
			'text',
2291
			$this->GetU_m1()
2292
		))->setHelp('m1');
2293

    
2294
		$group->add(new Form_Input(
2295
			'upperlimit2',
2296
			null,
2297
			'text',
2298
			$this->GetU_d()
2299
		))->setHelp('d');
2300

    
2301
		$group->add(new Form_Input(
2302
			'upperlimit3',
2303
			null,
2304
			'text',
2305
			$this->GetU_m2()
2306
		))->setHelp('m2');
2307

    
2308

    
2309
		$section->add($group);
2310

    
2311
		$group = new Form_Group('Min bandwidth for queue.');
2312

    
2313
		$group->add(new Form_Checkbox(
2314
			'realtime',
2315
			null,
2316
			'Real Time',
2317
			($this->GetRealtime()<> "")
2318
		));
2319

    
2320
		$group->add(new Form_Input(
2321
			'realtime1',
2322
			null,
2323
			'text',
2324
			$this->GetR_m1()
2325
		))->setHelp('m1');
2326

    
2327
		$group->add(new Form_Input(
2328
			'realtime2',
2329
			null,
2330
			'text',
2331
			$this->GetR_d()
2332
		))->setHelp('d');
2333

    
2334
		$group->add(new Form_Input(
2335
			'realtime3',
2336
			null,
2337
			'text',
2338
			$this->GetR_m2()
2339
		))->setHelp('m2');
2340

    
2341
		$section->add($group);
2342

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

    
2345
		$group->add(new Form_Checkbox(
2346
			'linkshare',
2347
			null,
2348
			'Link Share',
2349
			($this->GetLinkshare()<> "")
2350
		));
2351

    
2352
		$group->add(new Form_Input(
2353
			'linkshare1',
2354
			null,
2355
			'text',
2356
			$this->GetL_m1()
2357
		))->setHelp('m1');
2358

    
2359
		$group->add(new Form_Input(
2360
			'linkshare2',
2361
			null,
2362
			'text',
2363
			$this->GetL_d()
2364
		))->setHelp('d');
2365

    
2366
		$group->add(new Form_Input(
2367
			'linkshare3',
2368
			null,
2369
			'text',
2370
			$this->GetL_m2()
2371
		))->setHelp('m2');
2372

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

    
2378
		$section->add($group);
2379

    
2380
		$sform->add($section);
2381

    
2382
		return($sform);
2383
	}
2384

    
2385
	function update_altq_queue_data(&$data) {
2386
		$this->ReadConfig($data);
2387
	}
2388

    
2389
	function wconfig() {
2390
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2391
		if (!is_array($cflink)) {
2392
			$cflink = array();
2393
		}
2394
		$cflink['name'] = $this->GetQname();
2395
		$cflink['interface'] = $this->GetInterface();
2396
		$cflink['qlimit'] = trim($this->GetQlimit());
2397
		if (empty($cflink['qlimit'])) {
2398
			unset($cflink['qlimit']);
2399
		}
2400
		$cflink['priority'] = $this->GetQpriority();
2401
		if (empty($cflink['priority'])) {
2402
			unset($cflink['priority']);
2403
		}
2404
		$cflink['description'] = $this->GetDescription();
2405
		if (empty($cflink['description'])) {
2406
			unset($cflink['description']);
2407
		}
2408
		$cflink['bandwidth'] = $this->GetBandwidth();
2409
		$cflink['bandwidthtype'] = $this->GetBwscale();
2410
		$cflink['enabled'] = $this->GetEnabled();
2411
		if (empty($cflink['enabled'])) {
2412
			unset($cflink['enabled']);
2413
		}
2414
		$cflink['default'] = $this->GetDefault();
2415
		if (empty($cflink['default'])) {
2416
			unset($cflink['default']);
2417
		}
2418
		$cflink['red'] = trim($this->GetRed());
2419
		if (empty($cflink['red'])) {
2420
			unset($cflink['red']);
2421
		}
2422
		$cflink['rio'] = $this->GetRio();
2423
		if (empty($cflink['rio'])) {
2424
			unset($cflink['rio']);
2425
		}
2426
		$cflink['ecn'] = trim($this->GetEcn());
2427
		if (empty($cflink['ecn'])) {
2428
			unset($cflink['ecn']);
2429
		}
2430
		$cflink['codel'] = trim($this->GetCodel());
2431
		if (empty($cflink['codel'])) {
2432
			unset($cflink['codel']);
2433
		}
2434
		if ($this->GetLinkshare() <> "") {
2435
			if ($this->GetL_m1() <> "") {
2436
				$cflink['linkshare1'] = $this->GetL_m1();
2437
				$cflink['linkshare2'] = $this->GetL_d();
2438
				$cflink['linkshare'] = "on";
2439
			} else {
2440
				unset($cflink['linkshare']);
2441
				unset($cflink['linkshare1']);
2442
				unset($cflink['linkshare2']);
2443
			}
2444
			if ($this->GetL_m2() <> "") {
2445
				$cflink['linkshare3'] = $this->GetL_m2();
2446
				$cflink['linkshare'] = "on";
2447
			} else {
2448
				unset($cflink['linkshare']);
2449
				unset($cflink['linkshare3']);
2450
			}
2451
		} else {
2452
			unset($cflink['linkshare']);
2453
			unset($cflink['linkshare1']);
2454
			unset($cflink['linkshare2']);
2455
			unset($cflink['linkshare3']);
2456
		}
2457
		if ($this->GetRealtime() <> "") {
2458
			if ($this->GetR_m1() <> "") {
2459
				$cflink['realtime1'] = $this->GetR_m1();
2460
				$cflink['realtime2'] = $this->GetR_d();
2461
				$cflink['realtime'] = "on";
2462
			} else {
2463
				unset($cflink['realtime']);
2464
				unset($cflink['realtime1']);
2465
				unset($cflink['realtime2']);
2466
			}
2467
			if ($this->GetR_m2() <> "") {
2468
				$cflink['realtime3'] = $this->GetR_m2();
2469
				$cflink['realtime'] = "on";
2470
			} else {
2471
				unset($cflink['realtime']);
2472
				unset($cflink['realtime3']);
2473
			}
2474
		} else {
2475
			unset($cflink['realtime']);
2476
			unset($cflink['realtime1']);
2477
			unset($cflink['realtime2']);
2478
			unset($cflink['realtime3']);
2479
		}
2480
		if ($this->GetUpperlimit() <> "") {
2481
			if ($this->GetU_m1() <> "") {
2482
				$cflink['upperlimit1'] = $this->GetU_m1();
2483
				$cflink['upperlimit2'] = $this->GetU_d();
2484
				$cflink['upperlimit'] = "on";
2485
			} else {
2486
				unset($cflink['upperlimit']);
2487
				unset($cflink['upperlimit1']);
2488
				unset($cflink['upperlimit2']);
2489
			}
2490
			if ($this->GetU_m2() <> "") {
2491
				$cflink['upperlimit3'] = $this->GetU_m2();
2492
				$cflink['upperlimit'] = "on";
2493
			} else {
2494
				unset($cflink['upperlimit']);
2495
				unset($cflink['upperlimit3']);
2496
			}
2497
		} else {
2498
			unset($cflink['upperlimit']);
2499
			unset($cflink['upperlimit1']);
2500
			unset($cflink['upperlimit2']);
2501
			unset($cflink['upperlimit3']);
2502
		}
2503
	}
2504
}
2505

    
2506
class cbq_queue extends priq_queue {
2507
	var $qborrow = "";
2508

    
2509
	function GetBorrow() {
2510
		return $this->qborrow;
2511
	}
2512
	function SetBorrow($borrow) {
2513
		$this->qborrow = $borrow;
2514
	}
2515
	function CanHaveChildren() {
2516
		return true;
2517
	}
2518

    
2519
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2520

    
2521
		if (!is_array($this->subqueues)) {
2522
			$this->subqueues = array();
2523
		}
2524
		$q =& new cbq_queue();
2525
		$q->SetInterface($this->GetInterface());
2526
		$q->SetParent($this);
2527
		$q->ReadConfig($qname);
2528
		$q->validate_input($qname, $input_errors);
2529
		if (count($input_errors)) {
2530
			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)));
2531
			return $q;
2532
		}
2533
		switch ($q->GetBwscale()) {
2534
			case "%":
2535
				$myBw = $this->GetAvailableBandwidth() * $qname['bandwidth'] / 100;
2536
				break;
2537
			default:
2538
				$myBw = $qname['bandwidth'] * get_bandwidthtype_scale($q->GetBwscale());
2539
				break;
2540
		}
2541
		$q->SetAvailableBandwidth($myBw);
2542
		$this->SetAvailableBandwidth($this->GetAvailableBandwidth() - $myBw);
2543

    
2544
		$q->SetEnabled("on");
2545
		$q->SetLink($path);
2546
		$this->subqueues[$q->GetQName()] = &$q;
2547
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2548
		if (is_array($qname['queue'])) {
2549
			foreach ($qname['queue'] as $key1 => $que) {
2550
				array_push($path, $key1);
2551
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2552
				array_pop($path);
2553
			}
2554
		}
2555

    
2556
		return $q;
2557
	}
2558

    
2559
	function copy_queue($interface, &$cflink) {
2560

    
2561
		$cflink['interface'] = $interface;
2562
		$cflink['qlimit'] = trim($this->GetQlimit());
2563
		if (empty($clink['qlimit'])) {
2564
			unset($cflink['qlimit']);
2565
		}
2566
		$cflink['priority'] = trim($this->GetQpriority());
2567
		if (empty($cflink['priority'])) {
2568
			unset($cflink['priority']);
2569
		}
2570
		$cflink['name'] = $this->GetQname();
2571
		$cflink['description'] = trim($this->GetDescription());
2572
		if (empty($cflink['description'])) {
2573
			unset($cflink['description']);
2574
		}
2575
		$cflink['bandwidth'] = $this->GetBandwidth();
2576
		$cflink['bandwidthtype'] = $this->GetBwscale();
2577
		$cflink['enabled'] = trim($this->GetEnabled());
2578
		if (empty($cflink['enabled'])) {
2579
			unset($cflink['enabled']);
2580
		}
2581
		$cflink['default'] = trim($this->GetDefault());
2582
		if (empty($cflink['default'])) {
2583
			unset($cflink['default']);
2584
		}
2585
		$cflink['red'] = trim($this->GetRed());
2586
		if (empty($cflink['red'])) {
2587
			unset($cflink['red']);
2588
		}
2589
		$cflink['rio'] = trim($this->GetRio());
2590
		if (empty($cflink['rio'])) {
2591
			unset($cflink['rio']);
2592
		}
2593
		$cflink['ecn'] = trim($this->GetEcn());
2594
		if (empty($cflink['ecn'])) {
2595
			unset($cflink['ecn']);
2596
		}
2597
		$cflink['borrow'] = trim($this->GetBorrow());
2598
		if (empty($cflink['borrow'])) {
2599
			unset($cflink['borrow']);
2600
		}
2601
		if (is_array($this->queues)) {
2602
			$cflinkp['queue'] = array();
2603
			foreach ($this->subqueues as $q) {
2604
				$cflink['queue'][$q->GetQname()] = array();
2605
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2606
			}
2607
		}
2608
	}
2609

    
2610
	/*
2611
	 * Should search even its children
2612
	 */
2613
	function &find_queue($interface, $qname) {
2614
		if ($qname == $this->GetQname()) {
2615
			return $this;
2616
		}
2617
		foreach ($this->subqueues as $q) {
2618
			$result =& $q->find_queue("", $qname);
2619
			if ($result) {
2620
				return $result;
2621
			}
2622
		}
2623
	}
2624

    
2625
	function &find_parentqueue($interface, $qname) {
2626
		if ($this->subqueues[$qname]) {
2627
			return $this;
2628
		}
2629
		foreach ($this->subqueues as $q) {
2630
			$result = $q->find_parentqueue("", $qname);
2631
			if ($result) {
2632
				return $result;
2633
			}
2634
		}
2635
	}
2636

    
2637
	function delete_queue() {
2638
		unref_on_altq_queue_list($this->GetQname());
2639
		cleanup_queue_from_rules($this->GetQname());
2640
		foreach ($this->subqueues as $q) {
2641
			$this->SetAvailableBandwidth($this->GetAvailableBandwidth() + $q->GetAvailableBandwidth());
2642
				$q->delete_queue();
2643
		}
2644
		unset_object_by_reference($this->GetLink());
2645
	}
2646

    
2647
	function validate_input($data, &$input_errors) {
2648
		parent::validate_input($data, $input_errors);
2649

    
2650
		if ($data['priority'] > 7) {
2651
				$input_errors[] = gettext("Priority must be an integer between 1 and 7.");
2652
		}
2653
		$reqdfields[] = "bandwidth";
2654
		$reqdfieldsn[] = gettext("Bandwidth");
2655
		$reqdfields[] = "bandwidthtype";
2656
		$reqdfieldsn[] = gettext("Bandwidthtype");
2657

    
2658
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2659

    
2660
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2661
			$input_errors[] = gettext("Bandwidth must be an integer.");
2662
		}
2663

    
2664

    
2665
		if ($data['bandwidth'] < 0) {
2666
			$input_errors[] = gettext("Bandwidth cannot be negative.");
2667
		}
2668

    
2669
		if ($data['bandwidthtype'] == "%") {
2670
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
2671
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100 bounds.");
2672
			}
2673
		}
2674

    
2675
/*
2676
		$parent =& $this->GetParent();
2677
		switch ($data['bandwidthtype']) {
2678
		case "%":
2679
			$myBw = $parent->GetAvailableBandwidth() * floatval($data['bandwidth']) / 100;
2680
			break;
2681
		default:
2682
			$mybw = floatval($data['bandwidth']) * get_bandwidthtype_scale($data['bandwidthtype']);
2683
			break;
2684
		}
2685
		if ($parent->GetAvailableBandwidth() < floatval($myBw)) {
2686
			$input_errors[] = "The sum of the children bandwidth exceeds that of the parent.";
2687
		}
2688
 */
2689
	}
2690

    
2691
	function ReadConfig(&$q) {
2692
		parent::ReadConfig($q);
2693
		if (!empty($q['borrow'])) {
2694
			$this->SetBorrow("on");
2695
		} else {
2696
			$this->SetBorrow("");
2697
		}
2698
	}
2699

    
2700
	function build_javascript() {
2701
		return parent::build_javascript();
2702
	}
2703

    
2704
	function build_tree() {
2705
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2706
		$tree .= "\" ";
2707
		$tmpvalue = trim($this->GetDefault());
2708
		if (!empty($tmpvalue)) {
2709
			$tree .= " class=\"navlnk\"";
2710
		}
2711
		$tree .= " >" . $this->GetQname() . "</a>";
2712
		if (is_array($this->subqueues)) {
2713
			$tree .= "<ul>";
2714
			foreach ($this->subqueues as $q) {
2715
				$tree .= $q->build_tree();
2716
			}
2717
			$tree .= "</ul>";
2718
		}
2719
		$tree .= "</li>";
2720
		return $tree;
2721
	}
2722

    
2723
	/* Even this should take children into consideration */
2724
	function build_rules(&$default = false) {
2725
		$pfq_rule = "queue ". $this->qname;
2726
		if ($this->GetInterface()) {
2727
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2728
		}
2729
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2730
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2731
		}
2732
		$tmpvalue = $this->GetQpriority();
2733
		if (!empty($tmpvalue)) {
2734
			$pfq_rule .= " priority " . $this->GetQpriority();
2735
		}
2736
		$tmpvalue = trim($this->GetQlimit());
2737
		if (!empty($tmpvalue)) {
2738
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2739
		}
2740
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
2741
			$pfq_rule .= " cbq ( ";
2742
			$tmpvalue = trim($this->GetRed());
2743
			if (!empty($tmpvalue)) {
2744
				$comma = 1;
2745
				$pfq_rule .= " red ";
2746
			}
2747
			$tmpvalue = trim($this->GetCodel());
2748
			if (!empty($tmpvalue)) {
2749
				$comma = 1;
2750
				$pfq_rule .= " codel ";
2751
			}
2752
			$tmpvalue = trim($this->GetRio());
2753
			if (!empty($tmpvalue)) {
2754
				if ($comma) {
2755
					$pfq_rule .= " ,";
2756
				}
2757
				$comma = 1;
2758
				$pfq_rule .= " rio ";
2759
			}
2760
			$tmpvalue = trim($this->GetEcn());
2761
			if (!empty($tmpvalue)) {
2762
				if ($comma) {
2763
					$pfq_rule .= " ,";
2764
				}
2765
				$comma = 1;
2766
				$pfq_rule .= " ecn ";
2767
			}
2768
			$tmpvalue = trim($this->GetDefault());
2769
			if (!empty($tmpvalue)) {
2770
				if ($comma) {
2771
					$pfq_rule .= " ,";
2772
				}
2773
				$comma = 1;
2774
				$pfq_rule .= " default ";
2775
				$default = true;
2776
			}
2777
			$tmpvalue = trim($this->GetBorrow());
2778
			if (!empty($tmpvalue)) {
2779
				if ($comma) {
2780
					$pfq_rule .= ", ";
2781
				}
2782
				$pfq_rule .= " borrow ";
2783
			}
2784
			$pfq_rule .= " ) ";
2785
		}
2786
		if (count($this->subqueues)) {
2787
			$i = count($this->subqueues);
2788
			$pfq_rule .= " { ";
2789
			foreach ($this->subqueues as $qkey => $qnone) {
2790
				if ($i > 1) {
2791
					$i--;
2792
					$pfq_rule .= " {$qkey}, ";
2793
				} else {
2794
					$pfq_rule .= " {$qkey} ";
2795
				}
2796
			}
2797
			$pfq_rule .= " } \n";
2798
			foreach ($this->subqueues as $q) {
2799
				$pfq_rule .= $q->build_rules($default);
2800
			}
2801
		}
2802

    
2803
		$pfq_rule .= " \n";
2804
		return $pfq_rule;
2805
	}
2806

    
2807
	function build_form() {
2808
		$sform = parent::build_form();
2809

    
2810
		$section = new Form_Section('');
2811

    
2812
		$group = new Form_Group('Bandwidth');
2813

    
2814
		$group->add(new Form_Input(
2815
			'bandwidth',
2816
			null,
2817
			'number',
2818
			$this->GetBandwidth()
2819
		));
2820

    
2821
		$group->add(new Form_Select(
2822
			'bandwidthtype',
2823
			null,
2824
			$this->GetBwscale(),
2825
			array('Kb' => 'Kbit/s',
2826
				  'Mb' => 'Mbit/s',
2827
				  'Gb' => 'Gbit/s',
2828
				  'b' => 'Bit/s',
2829
				  '%' => '%')
2830
		));
2831

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

    
2834
		$section->add($group);
2835

    
2836
		$section->addInput(new Form_Checkbox(
2837
			'borrow',
2838
			'Scheduler option',
2839
			'Borrow from other queues when available',
2840
			($this->GetBorrow() == "on")
2841
		));
2842

    
2843
		return $sform;
2844
	}
2845

    
2846
	function update_altq_queue_data(&$data) {
2847
		$this->ReadConfig($data);
2848
	}
2849

    
2850
	function wconfig() {
2851
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2852
		if (!is_array($cflink)) {
2853
			$cflink = array();
2854
		}
2855
		$cflink['interface'] = $this->GetInterface();
2856
		$cflink['qlimit'] = trim($this->GetQlimit());
2857
		if (empty($cflink['qlimit'])) {
2858
			unset($cflink['qlimit']);
2859
		}
2860
		$cflink['priority'] = $this->GetQpriority();
2861
		if (empty($cflink['priority'])) {
2862
			unset($cflink['priority']);
2863
		}
2864
		$cflink['name'] = $this->GetQname();
2865
		$cflink['description'] = $this->GetDescription();
2866
		if (empty($cflink['description'])) {
2867
			unset($cflink['description']);
2868
		}
2869
		$cflink['bandwidth'] = $this->GetBandwidth();
2870
		$cflink['bandwidthtype'] = $this->GetBwscale();
2871
		$cflink['enabled'] = trim($this->GetEnabled());
2872
		if (empty($cflink['enabled'])) {
2873
			unset($cflink['enabled']);
2874
		}
2875
		$cflink['default'] = trim($this->GetDefault());
2876
		if (empty($cflink['default'])) {
2877
			unset($cflink['default']);
2878
		}
2879
		$cflink['red'] = trim($this->GetRed());
2880
		if (empty($cflink['red'])) {
2881
			unset($cflink['red']);
2882
		}
2883
		$cflink['rio'] = trim($this->GetRio());
2884
		if (empty($cflink['rio'])) {
2885
			unset($cflink['rio']);
2886
		}
2887
		$cflink['ecn'] = trim($this->GetEcn());
2888
		if (empty($cflink['ecn'])) {
2889
			unset($cflink['ecn']);
2890
		}
2891
		$cflink['codel'] = trim($this->GetCodel());
2892
		if (empty($cflink['codel'])) {
2893
			unset($cflink['codel']);
2894
		}
2895
		$cflink['borrow'] = trim($this->GetBorrow());
2896
		if (empty($cflink['borrow'])) {
2897
			unset($cflink['borrow']);
2898
		}
2899
	}
2900
}
2901

    
2902
class fairq_queue extends priq_queue {
2903
	var $hogs;
2904
	var $buckets;
2905

    
2906
	function GetBuckets() {
2907
		return $this->buckets;
2908
	}
2909
	function SetBuckets($buckets) {
2910
		$this->buckets = $buckets;
2911
	}
2912
	function GetHogs() {
2913
		return $this->hogs;
2914
	}
2915
	function SetHogs($hogs) {
2916
		$this->hogs = $hogs;
2917
	}
2918
	function CanHaveChildren() {
2919
		return false;
2920
	}
2921

    
2922

    
2923
	function copy_queue($interface, &$cflink) {
2924
		$cflink['interface'] = $interface;
2925
		$cflink['qlimit'] = $this->GetQlimit();
2926
		$cflink['priority'] = $this->GetQpriority();
2927
		$cflink['name'] = $this->GetQname();
2928
		$cflink['description'] = $this->GetDescription();
2929
		$cflink['bandwidth'] = $this->GetBandwidth();
2930
		$cflink['bandwidthtype'] = $this->GetBwscale();
2931
		$cflink['enabled'] = $this->GetEnabled();
2932
		$cflink['default'] = $this->GetDefault();
2933
		$cflink['red'] = $this->GetRed();
2934
		$cflink['rio'] = $this->GetRio();
2935
		$cflink['ecn'] = $this->GetEcn();
2936
		$cflink['buckets'] = $this->GetBuckets();
2937
		$cflink['hogs'] = $this->GetHogs();
2938
	}
2939

    
2940
	/*
2941
	 * Should search even its children
2942
	 */
2943
	function &find_queue($interface, $qname) {
2944
		if ($qname == $this->GetQname()) {
2945
			return $this;
2946
		}
2947
	}
2948

    
2949
	function find_parentqueue($interface, $qname) { return; }
2950

    
2951
	function delete_queue() {
2952
		unref_on_altq_queue_list($this->GetQname());
2953
		cleanup_queue_from_rules($this->GetQname());
2954
		unset_object_by_reference($this->GetLink());
2955
	}
2956

    
2957
	function validate_input($data, &$input_errors) {
2958
		parent::validate_input($data, $input_errors);
2959

    
2960
		if ($data['priority'] > 255) {
2961
				$input_errors[] = gettext("Priority must be an integer between 1 and 255.");
2962
		}
2963
		$reqdfields[] = "bandwidth";
2964
		$reqdfieldsn[] = gettext("Bandwidth");
2965
		$reqdfields[] = "bandwidthtype";
2966
		$reqdfieldsn[] = gettext("Bandwidthtype");
2967

    
2968
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2969

    
2970
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2971
			$input_errors[] = gettext("Bandwidth must be an integer.");
2972
		}
2973

    
2974

    
2975
		if ($data['bandwidth'] < 0) {
2976
			$input_errors[] = gettext("Bandwidth cannot be negative.");
2977
		}
2978

    
2979

    
2980
		if ($data['bandwidthtype'] == "%") {
2981
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
2982
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100 bounds.");
2983
			}
2984
		}
2985

    
2986
/*
2987
		$parent =& $this->GetParent();
2988
		switch ($data['bandwidthtype']) {
2989
		case "%":
2990
			$myBw = $parent->GetAvailableBandwidth() * floatval($data['bandwidth']) / 100;
2991
		default:
2992
			$mybw = floatval($data['bandwidth']) * get_bandwidthtype_scale($data['bandwidthtype']);
2993
			break;
2994
		}
2995
		if ($parent->GetAvailableBandwidth() < floatval($myBw)) {
2996
			$input_errors[] = "The sum of children bandwidth exceeds that of the parent.";
2997
		}
2998
*/
2999
	}
3000

    
3001
	function ReadConfig(&$q) {
3002
		parent::ReadConfig($q);
3003
		if (!empty($q['buckets'])) {
3004
			$this->SetBuckets($q['buckets']);
3005
		} else {
3006
			$this->SetBuckets("");
3007
		}
3008
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3009
			$this->SetHogs($q['hogs']);
3010
		} else {
3011
			$this->SetHogs("");
3012
		}
3013
	}
3014

    
3015
	function build_javascript() {
3016
		return parent::build_javascript();
3017
	}
3018

    
3019
	function build_tree() {
3020
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3021
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
3022
		$tree .= "\" ";
3023
		$tmpvalue = trim($this->GetDefault());
3024
		if (!empty($tmpvalue)) {
3025
			$tree .= " class=\"navlnk\"";
3026
		}
3027
		$tree .= " >" . $this->GetQname() . "</a>";
3028
		$tree .= "</li>";
3029
		return $tree;
3030
	}
3031

    
3032
	/* Even this should take children into consideration */
3033
	function build_rules(&$default = false) {
3034
		$pfq_rule = "queue ". $this->qname;
3035
		if ($this->GetInterface()) {
3036
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3037
		}
3038
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3039
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3040
		}
3041
		$tmpvalue = trim($this->GetQpriority());
3042
		if (!empty($tmpvalue)) {
3043
			$pfq_rule .= " priority " . $this->GetQpriority();
3044
		}
3045
		$tmpvalue = trim($this->GetQlimit());
3046
		if (!empty($tmpvalue)) {
3047
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3048
		}
3049
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3050
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3051
			$pfq_rule .= " fairq ( ";
3052
			$tmpvalue = trim($this->GetRed());
3053
			if (!empty($tmpvalue)) {
3054
				$comma = 1;
3055
				$pfq_rule .= " red ";
3056
			}
3057
			$tmpvalue = trim($this->GetCodel());
3058
			if (!empty($tmpvalue)) {
3059
				$comma = 1;
3060
				$pfq_rule .= " codel ";
3061
			}
3062
			$tmpvalue = trim($this->GetRio());
3063
			if (!empty($tmpvalue)) {
3064
				if ($comma) {
3065
					$pfq_rule .= " ,";
3066
				}
3067
				$comma = 1;
3068
				$pfq_rule .= " rio ";
3069
			}
3070
			$tmpvalue = trim($this->GetEcn());
3071
			if (!empty($tmpvalue)) {
3072
				if ($comma) {
3073
					$pfq_rule .= " ,";
3074
				}
3075
				$comma = 1;
3076
				$pfq_rule .= " ecn ";
3077
			}
3078
			$tmpvalue = trim($this->GetDefault());
3079
			if (!empty($tmpvalue)) {
3080
				if ($comma) {
3081
					$pfq_rule .= " ,";
3082
				}
3083
				$comma = 1;
3084
				$pfq_rule .= " default ";
3085
				$default = true;
3086
			}
3087
			$tmpvalue = trim($this->GetBuckets());
3088
			if (!empty($tmpvalue)) {
3089
				if ($comma) {
3090
					$pfq_rule .= ", ";
3091
				}
3092
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3093
			}
3094
			$tmpvalue = trim($this->GetHogs());
3095
			if (!empty($tmpvalue)) {
3096
				if ($comma) {
3097
					$pfq_rule .= ", ";
3098
				}
3099
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3100
			}
3101
			$pfq_rule .= " ) ";
3102
		}
3103

    
3104
		$pfq_rule .= " \n";
3105
		return $pfq_rule;
3106
	}
3107

    
3108
	function build_form() {
3109
		$form = parent::build_form();
3110

    
3111
		$section = new Form_Section('');
3112

    
3113
		$group = new Form_Group('Bandwidth');
3114

    
3115
		$group->add(new Form_Input(
3116
			'bandwidth',
3117
			null,
3118
			'number',
3119
			$this->GetBandwidth()
3120
		));
3121

    
3122
		$group->add(new Form_Select(
3123
			'bandwidthtype',
3124
			null,
3125
			$this->GetBwscale(),
3126
			array('Kb' => 'Kbit/s',
3127
				  'Mb' => 'Mbit/s',
3128
				  'Gb' => 'Gbit/s',
3129
				  'b' => 'Bit/s',
3130
				  '%' => '%')
3131
		));
3132

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

    
3135
		$section->add($group);
3136

    
3137
		$section->addInput(new Form_Input(
3138
			'buckets',
3139
			'Scheduler specific options',
3140
			'text',
3141
			$this->GetBuckets()
3142
		))->setHelp('Number of buckets available');
3143

    
3144
		$section->addInput(new Form_Input(
3145
			'hogs',
3146
			'',
3147
			'text',
3148
			$this->GetHogs()
3149
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3150

    
3151
		$form->add($section);
3152
		return $form;
3153
	}
3154

    
3155
	function update_altq_queue_data(&$data) {
3156
		$this->ReadConfig($data);
3157
	}
3158

    
3159
	function wconfig() {
3160
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3161
		if (!is_array($cflink)) {
3162
			$cflink = array();
3163
		}
3164
		$cflink['interface'] = $this->GetInterface();
3165
		$cflink['qlimit'] = trim($this->GetQlimit());
3166
		if (empty($cflink['qlimit'])) {
3167
			unset($cflink['qlimit']);
3168
		}
3169
		$cflink['priority'] = trim($this->GetQpriority());
3170
		if (empty($cflink['priority'])) {
3171
			unset($cflink['priority']);
3172
		}
3173
		$cflink['name'] = $this->GetQname();
3174
		$cflink['description'] = trim($this->GetDescription());
3175
		if (empty($cflink['description'])) {
3176
			unset($cflink['description']);
3177
		}
3178
		$cflink['bandwidth'] = $this->GetBandwidth();
3179
		$cflink['bandwidthtype'] = $this->GetBwscale();
3180
		$cflink['enabled'] = $this->GetEnabled();
3181
		if (empty($cflink['enabled'])) {
3182
			unset($cflink['enabled']);
3183
		}
3184
		$cflink['default'] = trim($this->GetDefault());
3185
		if (empty($cflink['default'])) {
3186
			unset($cflink['default']);
3187
		}
3188
		$cflink['red'] = trim($this->GetRed());
3189
		if (empty($cflink['red'])) {
3190
			unset($cflink['red']);
3191
		}
3192
		$cflink['rio'] = trim($this->GetRio());
3193
		if (empty($cflink['rio'])) {
3194
			unset($cflink['rio']);
3195
		}
3196
		$cflink['ecn'] = trim($this->GetEcn());
3197
		if (empty($cflink['ecn'])) {
3198
			unset($cflink['ecn']);
3199
		}
3200
		$cflink['codel'] = trim($this->GetCodel());
3201
		if (empty($cflink['codel'])) {
3202
			unset($cflink['codel']);
3203
		}
3204
		$cflink['buckets'] = trim($this->GetBuckets());
3205
		if (empty($cflink['buckets'])) {
3206
			unset($cflink['buckets']);
3207
		}
3208
		$cflink['hogs'] = trim($this->GetHogs());
3209
		if (empty($cflink['hogs'])) {
3210
			unset($cflink['hogs']);
3211
		}
3212
	}
3213
}
3214

    
3215

    
3216
/*
3217
 * dummynet(4) wrappers.
3218
 */
3219

    
3220

    
3221
/*
3222
 * List of respective objects!
3223
 */
3224
$dummynet_pipe_list = array();
3225

    
3226
class dummynet_class {
3227
	var $qname;
3228
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3229
	var $qlimit;
3230
	var $description;
3231
	var $qenabled;
3232
	var $link;
3233
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3234
	var $plr;
3235

    
3236
	var $buckets;
3237
	/* mask parameters */
3238
	var $mask;
3239
	var $noerror;
3240

    
3241
	/* Accessor functions */
3242
	function SetLink($link) {
3243
		$this->link = $link;
3244
	}
3245
	function GetLink() {
3246
		return $this->link;
3247
	}
3248
	function GetMask() {
3249
		if (!isset($this->mask["type"])) {
3250
			$this->mask["type"] = "none";
3251
		}
3252
		return $this->mask;
3253
	}
3254
	function SetMask($mask) {
3255
		$this->mask = $mask;
3256
	}
3257
	function &GetParent() {
3258
		return $this->qparent;
3259
	}
3260
	function SetParent(&$parent) {
3261
		$this->qparent = &$parent;
3262
	}
3263
	function GetEnabled() {
3264
		return $this->qenabled;
3265
	}
3266
	function SetEnabled($value) {
3267
		$this->qenabled = $value;
3268
	}
3269
	function CanHaveChildren() {
3270
		return false;
3271
	}
3272
	function CanBeDeleted() {
3273
		return true;
3274
	}
3275
	function GetQname() {
3276
		return $this->qname;
3277
	}
3278
	function SetQname($name) {
3279
		$this->qname = trim($name);
3280
	}
3281
	function GetQlimit() {
3282
		return $this->qlimit;
3283
	}
3284
	function SetQlimit($limit) {
3285
		$this->qlimit = $limit;
3286
	}
3287
	function GetDescription() {
3288
		return $this->description;
3289
	}
3290
	function SetDescription($str) {
3291
		$this->description = trim($str);
3292
	}
3293
	function GetFirstime() {
3294
		return $this->firsttime;
3295
	}
3296
	function SetFirsttime($number) {
3297
		$this->firsttime = $number;
3298
	}
3299
	function GetBuckets() {
3300
		return $this->buckets;
3301
	}
3302
	function SetBuckets($buckets) {
3303
		$this->buckets = $buckets;
3304
	}
3305
	function SetNumber($number) {
3306
		$this->qnumber = $number;
3307
	}
3308
	function GetNumber() {
3309
		return $this->qnumber;
3310
	}
3311
	function GetPlr() {
3312
		return $this->plr;
3313
	}
3314
	function SetPlr($plr) {
3315
		$this->plr = $plr;
3316
	}
3317

    
3318
	function build_javascript() {
3319
		$javascript .= "<script type=\"text/javascript\">\n";
3320
		$javascript .= "//<![CDATA[\n";
3321
		$javascript .= "function enable_maskbits(enable_over) {\n";
3322
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3323
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3324
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3325
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3326
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3327
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3328
		$javascript .= "} else {\n";
3329
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3330
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3331
		$javascript .= "}}\n";
3332
		$javascript .= "//]]>\n";
3333
		$javascript .= "</script>\n";
3334
		return $javascript;
3335
	}
3336

    
3337
	function validate_input($data, &$input_errors) {
3338
		$reqdfields[] = "bandwidth";
3339
		$reqdfieldsn[] = gettext("Bandwidth");
3340
		/*$reqdfields[] = "burst";
3341
		$reqdfieldsn[] = gettext("Burst"); */
3342
		$reqdfields[] = "bandwidthtype";
3343
		$reqdfieldsn[] = gettext("Bandwidthtype");
3344
		$reqdfields[] = "newname";
3345
		$reqdfieldsn[] = gettext("Name");
3346

    
3347
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3348

    
3349
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3350
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3351
			$input_errors[] = gettext("Plr must be a value between 0 and 1.");
3352
		}
3353
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3354
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3355
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3356
		}
3357
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3358
			$input_errors[] = gettext("Queue limit must be an integer");
3359
		}
3360
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3361
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3362
		}
3363
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3364
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3365
		}
3366
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3367
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3368
				$input_errors[] = gettext("IPV4 bit mask must be blank or numeric value between 1 and 32.");
3369
			}
3370
		}
3371
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3372
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3373
				$input_errors[] = gettext("IPV6 bit mask must be blank or numeric value between 1 and 128.");
3374
			}
3375
		}
3376
	}
3377

    
3378
	function build_mask_rules(&$pfq_rule) {
3379
		$mask = $this->GetMask();
3380
		if (!empty($mask['type'])) {
3381
			if ($mask['type'] <> 'none') {
3382
				$pfq_rule .= " mask";
3383
			}
3384
			switch ($mask['type']) {
3385
				case 'srcaddress':
3386
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3387
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3388
					} else {
3389
						$pfq_rule .= " src-ip6 /128";
3390
					}
3391
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3392
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3393
					} else {
3394
						$pfq_rule .= " src-ip 0xffffffff";
3395
					}
3396
					break;
3397
				case 'dstaddress':
3398
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3399
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3400
					} else {
3401
						$pfq_rule .= " dst-ip6 /128";
3402
					}
3403
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3404
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3405
					} else {
3406
						$pfq_rule .= " dst-ip 0xffffffff";
3407
					}
3408
					break;
3409
				default:
3410
					break;
3411
			}
3412
		}
3413
	}
3414

    
3415
}
3416

    
3417
class dnpipe_class extends dummynet_class {
3418
	var $delay;
3419
	var $qbandwidth = array();
3420
	var $qbandwidthtype;
3421

    
3422
		/* This is here to help on form building and building rules/lists */
3423
	var $subqueues = array();
3424

    
3425
	function CanHaveChildren() {
3426
		return true;
3427
	}
3428
	function SetDelay($delay) {
3429
		$this->delay = $delay;
3430
	}
3431
	function GetDelay() {
3432
		return $this->delay;
3433
	}
3434
	function delete_queue() {
3435
		cleanup_dnqueue_from_rules($this->GetQname());
3436
		foreach ($this->subqueues as $q) {
3437
			$q->delete_queue();
3438
		}
3439
		unset_dn_object_by_reference($this->GetLink());
3440
		@pfSense_pipe_action("pipe delete " . $this->GetNumber());
3441
	}
3442
	function GetBandwidth() {
3443
		return $this->qbandwidth;
3444
	}
3445
	function SetBandwidth($bandwidth) {
3446
		$this->qbandwidth = $bandwidth;
3447
	}
3448
	function GetBurst() {
3449
		return $this->qburst;
3450
	}
3451
	function SetBurst($burst) {
3452
		$this->qburst = $burst;
3453
	}
3454

    
3455
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3456

    
3457
		if (!is_array($this->subqueues)) {
3458
			$this->subqueues = array();
3459
		}
3460

    
3461
		$q =& new dnqueue_class();
3462
		$q->SetLink($path);
3463
		$q->SetEnabled("on");
3464
		$q->SetPipe($this->GetQname());
3465
		$q->SetParent($this);
3466
		$q->ReadConfig($queue);
3467
		$q->validate_input($queue, $input_errors);
3468
		if (count($input_errors)) {
3469
			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)));
3470
			return $q;
3471
		}
3472
		$number = dnqueue_find_nextnumber();
3473
		$q->SetNumber($number);
3474
		$this->subqueues[$q->GetQname()] = &$q;
3475

    
3476
		return $q;
3477
	}
3478

    
3479
	function &get_queue_list(&$q = null) {
3480
		$qlist = array();
3481

    
3482
		$qlist[$this->GetQname()] = $this->GetNumber();
3483
		if (is_array($this->subqueues)) {
3484
			foreach ($this->subqueues as $queue) {
3485
				$queue->get_queue_list($qlist);
3486
			}
3487
		}
3488
		return $qlist;
3489
	}
3490

    
3491
	/*
3492
	 * Should search even its children
3493
	 */
3494
	function &find_queue($pipe, $qname) {
3495
		if ($qname == $this->GetQname()) {
3496
			return $this;
3497
		}
3498
		foreach ($this->subqueues as $q) {
3499
			$result =& $q->find_queue("", $qname);
3500
			if ($result) {
3501
				return $result;
3502
			}
3503
		}
3504
	}
3505

    
3506
	function &find_parentqueue($pipe, $qname) {
3507
		return NULL;
3508
	}
3509

    
3510
	function validate_input($data, &$input_errors) {
3511
		parent::validate_input($data, $input_errors);
3512

    
3513
		$schedule = 0;
3514
		$schedulenone = 0;
3515
		$entries = 0;
3516
		/* XXX: Really no better way? */
3517
		for ($i = 0; $i < 2900; $i++) {
3518
			if (!empty($data["bwsched{$i}"])) {
3519
				if ($data["bwsched{$i}"] != "none") {
3520
					$schedule++;
3521
				} else {
3522
					$schedulenone++;
3523
				}
3524
			}
3525
			if (!empty($data["bandwidth{$i}"])) {
3526
				if (!is_numeric($data["bandwidth{$i}"])) {
3527
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3528
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3529
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3530
				} else {
3531
					$entries++;
3532
				}
3533
			}
3534
		}
3535
		if ($schedule == 0 && $entries > 1) {
3536
			$input_errors[] = gettext("You need to specify a schedule for every additional entry");
3537
		}
3538
		if ($schedulenone > 0 && $entries > 1) {
3539
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected");
3540
		}
3541
		if ($entries == 0) {
3542
			$input_errors[] = gettext("At least one bw specification is necessary");
3543
		}
3544
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3545
			$input_errors[] = gettext("Delay must be an integer.");
3546
		}
3547
	}
3548

    
3549
	function ReadConfig(&$q) {
3550
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3551
			$this->SetQname($q['newname']);
3552
		} else if (!empty($q['newname'])) {
3553
			$this->SetQname($q['newname']);
3554
		} else {
3555
			$this->SetQname($q['name']);
3556
		}
3557
		$this->SetNumber($q['number']);
3558

    
3559
		if (!empty($_POST)) {
3560
			$bandwidth = array();
3561
			/* XXX: Really no better way? */
3562
			for ($i = 0; $i < 2900; $i++) {
3563
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3564
					$bw = array();
3565
					$bw['bw'] = $q["bandwidth{$i}"];
3566
					$bw['burst'] = $q["burst{$i}"];
3567
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3568
						$bw['bwscale'] = $q["bwtype{$i}"];
3569
					}
3570
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3571
						$bw['bwsched'] = $q["bwsched{$i}"];
3572
					}
3573
					$bandwidth[] = $bw;
3574
				}
3575
			}
3576
			$this->SetBandwidth($bandwidth);
3577
		}
3578

    
3579
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3580
			$this->SetBandwidth($q['bandwidth']['item']);
3581
			$this->SetBurst($q['burst']['item']);
3582
		}
3583

    
3584
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3585
			$this->SetQlimit($q['qlimit']);
3586
		} else {
3587
			$this->SetQlimit("");
3588
		}
3589
		if (isset($q['mask']) && $q['mask'] <> "") {
3590
			$masktype = $q['mask'];
3591
		} else {
3592
			$masktype = "";
3593
		}
3594
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3595
			$maskbits = $q['maskbits'];
3596
		} else {
3597
			$maskbits = "";
3598
		}
3599
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3600
			$maskbitsv6 = $q['maskbitsv6'];
3601
		} else {
3602
			$maskbitsv6 = "";
3603
		}
3604
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3605
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3606
			$this->SetBuckets($q['buckets']);
3607
		} else {
3608
			$this->SetBuckets("");
3609
		}
3610
		if (isset($q['plr']) && $q['plr'] <> "") {
3611
			$this->SetPlr($q['plr']);
3612
		} else {
3613
			$this->SetPlr("");
3614
		}
3615
		if (isset($q['delay']) && $q['delay'] <> "") {
3616
			$this->SetDelay($q['delay']);
3617
		} else {
3618
			$this->SetDelay(0);
3619
		}
3620
		if (isset($q['description']) && $q['description'] <> "") {
3621
			$this->SetDescription($q['description']);
3622
		} else {
3623
			$this->SetDescription("");
3624
		}
3625
		$this->SetEnabled($q['enabled']);
3626

    
3627
	}
3628

    
3629
	function build_tree() {
3630
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3631
		$tree .= $this->GetQname() . "</a>";
3632
		if (is_array($this->subqueues)) {
3633
			$tree .= "<ul>";
3634
			foreach ($this->subqueues as $q) {
3635
				$tree .= $q->build_tree();
3636
			}
3637
			$tree .= "</ul>";
3638
		}
3639
		$tree .= "</li>";
3640

    
3641
		return $tree;
3642
	}
3643

    
3644
	function build_rules() {
3645
		global $config, $time_based_rules;
3646

    
3647
		if ($this->GetEnabled() == "") {
3648
			return;
3649
		}
3650

    
3651
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3652
		$found = false;
3653
		$bandwidth = $this->GetBandwidth();
3654
		if (is_array($bandwidth)) {
3655
			foreach ($bandwidth as $bw) {
3656
				if ($bw['bwsched'] != "none") {
3657
					$time_based_rules = true;
3658
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3659
						foreach ($config['schedules']['schedule'] as $schedule) {
3660
							if ($bw['bwsched'] == $schedule['name']) {
3661
								if (filter_get_time_based_rule_status($schedule)) {
3662
									$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3663
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3664
										$pfq_rule .= " burst ".trim($bw['burst']);
3665
									}
3666
									$found = true;
3667
									break;
3668
								}
3669
							}
3670
						}
3671
					} else {
3672
						$pfq_rule .= " bw 0";
3673
						$found = true;
3674
						break;
3675
					}
3676
				} else {
3677
					$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3678
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3679
						$pfq_rule .= " burst ".trim($bw['burst']);
3680
					}
3681
					$found = true;
3682
					break;
3683
				}
3684
			}
3685
			if ($found == false) {
3686
				$pfq_rule .= " bw 0";
3687
			}
3688
		} else {
3689
			$pfq_rule .= " bw 0";
3690
		}
3691

    
3692
		if ($this->GetQlimit()) {
3693
			$pfq_rule .= " queue " . $this->GetQlimit();
3694
		}
3695
		if ($this->GetPlr()) {
3696
			$pfq_rule .= " plr " . $this->GetPlr();
3697
		}
3698
		if ($this->GetBuckets()) {
3699
			$pfq_rule .= " buckets " . $this->GetBuckets();
3700
		}
3701
		if ($this->GetDelay()) {
3702
			$pfq_rule .= " delay " . $this->GetDelay();
3703
		}
3704
		$this->build_mask_rules($pfq_rule);
3705

    
3706
		$pfq_rule .= "\n";
3707

    
3708
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3709
			foreach ($this->subqueues as $q) {
3710
				$pfq_rule .= $q->build_rules();
3711
			}
3712
		}
3713
		$pfq_rule .= " \n";
3714

    
3715
		return $pfq_rule;
3716
	}
3717

    
3718
	function update_dn_data(&$data) {
3719
		$this->ReadConfig($data);
3720
	}
3721

    
3722
	function build_javascript() {
3723
		global $g, $config;
3724

    
3725
		$javasr = parent::build_javascript();
3726

    
3727
		//build list of schedules
3728
		$schedules = "<option value='none'>none</option>";
3729
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3730
			foreach ($config['schedules']['schedule'] as $schedule) {
3731
				if ($schedule['name'] <> "") {
3732
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3733
				}
3734
			}
3735
		}
3736
		$bwopt = "";
3737
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3738
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3739
		}
3740

    
3741
		$javasr .= <<<EOD
3742
<script type='text/javascript'>
3743
//<![CDATA[
3744
var addBwRowTo = (function() {
3745

    
3746
	return (function (tableId) {
3747

    
3748
	var table = document.getElementById(tableId);
3749
	var totalrows = table.rows.length -1;
3750

    
3751
	var row = table.insertRow(totalrows + 1);
3752
	var cell1 = row.insertCell(0);
3753
	var cell2 = row.insertCell(1);
3754
	var cell3 = row.insertCell(2);
3755
	var cell4 = row.insertCell(3);
3756

    
3757
	cell1.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bandwidth_row-" + totalrows + "' /><input type='text' class='form-control' name='bandwidth" + totalrows + "' id='bandwidth" + totalrows + "' />";
3758
	cell2.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwtype_row-" + totalrows + "' /><select class='form-control' name='bwtype" + totalrows + "'>{$bwopt}</select>";
3759
	cell3.innerHTML = "<input type='hidden' value='" + totalrows +"' name='bwsched_row-" + totalrows + "' /><select class='form-control' name='bwsched" + totalrows + "'>{$schedules}</select>";
3760
	cell4.innerHTML = '<a class="btn btn-default" onclick="removeBwRow(this); return false;" href="#">Remove</a>';
3761

    
3762
	});
3763
})();
3764

    
3765
function removeBwRow(el) {
3766
	var d = el.parentNode.parentNode.rowIndex;
3767
	document.getElementById('maintable').deleteRow(d);
3768
}
3769
//]]>
3770
</script>
3771

    
3772
EOD;
3773

    
3774
		return $javasr;
3775
	}
3776

    
3777
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3778
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3779
	// with the javascript in this class
3780
	function build_bwtable() {
3781
		global $config;
3782

    
3783
		$bandwidth = $this->GetBandwidth();
3784
				//build list of schedules
3785
		$schedules = array();
3786
		$schedules[] = "none";//leave none to leave rule enabled all the time
3787
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3788
			foreach ($config['schedules']['schedule'] as $schedule) {
3789
				if ($schedule['name'] != "") {
3790
					$schedules[] = $schedule['name'];
3791
				}
3792
			}
3793
		}
3794

    
3795
		$form = '<div class="table-responsive">';
3796
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3797
		$form .= "<thead><tr>";
3798
		$form .= "<th>Bandwidth</th>";
3799
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3800
		$form .= "<th>Bw type</th>";
3801
		$form .= "<th>Schedule</th>";
3802
		$form .= "<th></th>";
3803
		$form .= "</tr></thead>";
3804
		$form .= "<tbody>";
3805

    
3806
		// If there are no bandwidths defined, make a blank one for convenience
3807
		if (empty($bandwidth)) {
3808
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3809
		}
3810

    
3811
		if (is_array($bandwidth)) {
3812
			foreach ($bandwidth as $bwidx => $bw) {
3813
				$form .= '<tr>';
3814
				$form .= '<td class="col-xs-4">';
3815
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3816
				//$form .= "</td><td width='20%'>";
3817
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3818
				$form .= "</td>";
3819
				$form .= '<td class="col-xs-4">';
3820
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3821

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

    
3825
					if ($bw['bwscale'] == $bwsidx) {
3826
						$form .= " selected";
3827
					}
3828

    
3829
					$form .= ">{$bwscale}</option>";
3830
				}
3831

    
3832
				$form .= "</select>";
3833
				$form .= "</td>";
3834
				$form .= '<td class="col-xs-4">';
3835
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3836

    
3837
				foreach ($schedules as $schd) {
3838
					$selected = "";
3839
					if ($bw['bwsched'] == $schd) {
3840
						$selected = "selected";
3841
					}
3842

    
3843
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3844
				}
3845

    
3846
				$form .= "</select>";
3847
				$form .= "</td>";
3848
				$form .= '<td>';
3849
				$form .= '<a class="btn btn-default" onclick="removeBwRow(this); return false;">' . gettext('Remove') . '</a>';
3850
				$form .= "</td></tr>";
3851
			}
3852
		}
3853
		$form .= "</tbody></table></div><br />";
3854

    
3855
		$form .= '<a class="btn btn-sm btn-success" onclick="javascript:addBwRowTo(\'maintable\'); return false;" >';
3856
		$form .= gettext("Add another schedule") . "</a>";
3857

    
3858
		return($form);
3859
	}
3860

    
3861
	function build_form() {
3862
		global $g, $config, $pipe, $action, $qname;
3863

    
3864
		//build list of schedules
3865
		$schedules = array();
3866
		$schedules[] = "none";//leave none to leave rule enabled all the time
3867
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3868
			foreach ($config['schedules']['schedule'] as $schedule) {
3869
				if ($schedule['name'] <> "") {
3870
					$schedules[] = $schedule['name'];
3871
				}
3872
			}
3873
		}
3874

    
3875

    
3876
		$sform = new Form();
3877
		$sform->setAction("firewall_shaper.php");
3878

    
3879
		$section = new Form_Section('Limiters');
3880

    
3881
		$section->addInput(new Form_Checkbox(
3882
			'enabled',
3883
			'Enable',
3884
			'Enable limiter and its children',
3885
			($this->GetEnabled() == "on"),
3886
			'on'
3887
		));
3888

    
3889
		$section->addInput(new Form_Input(
3890
			'newname',
3891
			'Name',
3892
			'text',
3893
			$this->GetQname()
3894
		));
3895

    
3896
		$section->addInput(new Form_Input(
3897
			'name',
3898
			null,
3899
			'hidden',
3900
			$this->GetQname()
3901
		));
3902

    
3903
		if ($this->GetNumber() > 0) {
3904
			$section->addInput(new Form_Input(
3905
				'number',
3906
				null,
3907
				'hidden',
3908
				$this->GetNumber()
3909
			));
3910
		}
3911

    
3912
		$bandwidth = $this->GetBandwidth();
3913

    
3914
		// Delete a row
3915
//		if(isset($_GET['delbwrow']) && (count($bandwidth) > 0))
3916
//			unset($bandwidth[$_GET['delbwrow']]);
3917

    
3918
		// Add a row
3919
//		if($_GET['newbwrow']) {
3920
//			array_push($bandwidth, array(count($bandwidth) => array('bw' => '', 'burst' => '', 'bwscale' => 'Kb', 'bwsched' => 'none') ));
3921
//		}
3922

    
3923
		if (is_array($bandwidth)) {
3924
				$section->addInput(new Form_StaticText(
3925
				'Bandwidth',
3926
				$this->build_bwtable()
3927
			));
3928
		}
3929

    
3930
		$mask = $this->GetMask();
3931

    
3932
		$section->addInput(new Form_Select(
3933
			'mask',
3934
			'Mask',
3935
			$mask['type'],
3936
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3937
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3938
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3939
					'This makes it possible to easily specify bandwidth limits per host.');
3940

    
3941
		$group = new Form_Group(null);
3942

    
3943
		$group->add(new Form_Select(
3944
			'maskbits',
3945
			null,
3946
			$mask['bits'],
3947
			array_combine(range(32, 1, -1), range(32, 1, -1))
3948
		))->setHelp('IPV4 mask bits' . '<br />' . '255.255.255.255/?');
3949

    
3950
		$group->add(new Form_Select(
3951
			'maskbitsv6',
3952
			null,
3953
			$mask['bitsv6'],
3954
			array_combine(range(128, 1, -1), range(128, 1, -1))
3955
		))->setHelp('IPV6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3956

    
3957
		$section->add($group);
3958

    
3959
		$section->addInput(new Form_Input(
3960
			'description',
3961
			'Description',
3962
			'text',
3963
			$this->GetDescription()
3964
		))->setHelp('You may enter a description here for your reference (not parsed).');
3965

    
3966
		$sform->add($section);
3967

    
3968
		$section = new Form_Section('Advanced Options');
3969

    
3970
		$section->addInput(new Form_Input(
3971
			'delay',
3972
			'Delay (ms)',
3973
			'text',
3974
			$this->GetDelay() > 0 ? $this->GetDelay():null
3975
		))->setHelp('In most cases, you should specify 0 here (or leave the field empty)');
3976

    
3977
		$section->addInput(new Form_Input(
3978
			'plr',
3979
			'Packet Loss Rate',
3980
			'number',
3981
			$this->GetPlr(),
3982
			['step' => '0.001', 'min' => '0.000']
3983
		))->setHelp('In most cases, you should specify 0 here (or leave the field empty). ' .
3984
					'A value of 0.001 means one packet in 1000 gets dropped');
3985

    
3986
		$section->addInput(new Form_Input(
3987
			'qlimit',
3988
			'Queue size (slots)',
3989
			'number',
3990
			$this->GetQlimit()
3991
		))->setHelp('In most cases, you should leave the field empty. All packets in this pipe are placed into a fixed-size queue first, ' .
3992
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
3993

    
3994
		$section->addInput(new Form_Input(
3995
			'buckets',
3996
			'Bucket size (slots)',
3997
			'number',
3998
			$this->GetBuckets()
3999
		))->setHelp('In most cases, you should leave this field empty. It increases the hash size set');
4000

    
4001
		$sform->add($section);
4002

    
4003
		return($sform);
4004
		}
4005

    
4006
	function wconfig() {
4007
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4008
		if (!is_array($cflink)) {
4009
			$cflink = array();
4010
		}
4011
		$cflink['name'] = $this->GetQname();
4012
		$cflink['number'] = $this->GetNumber();
4013
		$cflink['qlimit'] = $this->GetQlimit();
4014
		$cflink['plr'] = $this->GetPlr();
4015
		$cflink['description'] = $this->GetDescription();
4016

    
4017
		$bandwidth = $this->GetBandwidth();
4018
		if (is_array($bandwidth)) {
4019
			$cflink['bandwidth'] = array();
4020
			$cflink['bandwidth']['item'] = array();
4021
			foreach ($bandwidth as $bwidx => $bw) {
4022
				$cflink['bandwidth']['item'][] = $bw;
4023
			}
4024
		}
4025

    
4026
		$cflink['enabled'] = $this->GetEnabled();
4027
		$cflink['buckets'] = $this->GetBuckets();
4028
		$mask = $this->GetMask();
4029
		$cflink['mask'] = $mask['type'];
4030
		$cflink['maskbits'] = $mask['bits'];
4031
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4032
		$cflink['delay'] = $this->GetDelay();
4033
	}
4034

    
4035
}
4036

    
4037
class dnqueue_class extends dummynet_class {
4038
	var $pipeparent;
4039
	var $weight;
4040

    
4041
	function GetWeight() {
4042
		return $this->weight;
4043
	}
4044
	function SetWeight($weight) {
4045
		$this->weight = $weight;
4046
	}
4047
	function GetPipe() {
4048
		return $this->pipeparent;
4049
	}
4050
	function SetPipe($pipe) {
4051
		$this->pipeparent = $pipe;
4052
	}
4053

    
4054
	/* Just a stub in case we ever try to call this from the frontend. */
4055
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4056
		return;
4057
	}
4058

    
4059
	function delete_queue() {
4060
		cleanup_dnqueue_from_rules($this->GetQname());
4061
		unset_dn_object_by_reference($this->GetLink());
4062
		@pfSense_pipe_action("queue delete " . $this->GetNumber());
4063
	}
4064

    
4065
	function validate_input($data, &$input_errors) {
4066
		parent::validate_input($data, $input_errors);
4067

    
4068
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4069
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4070
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4071
		}
4072
	}
4073

    
4074
	/*
4075
	 * Should search even its children
4076
	 */
4077
	function &find_queue($pipe, $qname) {
4078
		if ($qname == $this->GetQname()) {
4079
			return $this;
4080
		} else {
4081
			return NULL;
4082
		}
4083
	}
4084

    
4085
	function &find_parentqueue($pipe, $qname) {
4086
		return $this->qparent;
4087
	}
4088

    
4089
	function &get_queue_list(&$qlist) {
4090
		if ($this->GetEnabled() == "") {
4091
			return;
4092
		}
4093
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4094
	}
4095

    
4096
	function ReadConfig(&$q) {
4097
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4098
			$this->SetQname($q['newname']);
4099
		} else if (!empty($q['newname'])) {
4100
			$this->SetQname($q['newname']);
4101
		} else {
4102
			$this->SetQname($q['name']);
4103
		}
4104
		$this->SetNumber($q['number']);
4105
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4106
			$this->SetQlimit($q['qlimit']);
4107
		} else {
4108
			$this->SetQlimit("");
4109
		}
4110
		if (isset($q['mask']) && $q['mask'] <> "") {
4111
			$masktype = $q['mask'];
4112
		} else {
4113
			$masktype = "";
4114
		}
4115
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4116
			$maskbits = $q['maskbits'];
4117
		} else {
4118
			$maskbits = "";
4119
		}
4120
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4121
			$maskbitsv6 = $q['maskbitsv6'];
4122
		} else {
4123
			$maskbitsv6 = "";
4124
		}
4125
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4126
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4127
			$this->SetBuckets($q['buckets']);
4128
		} else {
4129
			$this->SetBuckets("");
4130
		}
4131
		if (isset($q['plr']) && $q['plr'] <> "") {
4132
			$this->SetPlr($q['plr']);
4133
		} else {
4134
			$this->SetPlr("");
4135
		}
4136
		if (isset($q['weight']) && $q['weight'] <> "") {
4137
			$this->SetWeight($q['weight']);
4138
		} else {
4139
			$this->SetWeight("");
4140
		}
4141
		if (isset($q['description']) && $q['description'] <> "") {
4142
			$this->SetDescription($q['description']);
4143
		} else {
4144
			$this->SetDescription("");
4145
		}
4146
		$this->SetEnabled($q['enabled']);
4147
	}
4148

    
4149
	function build_tree() {
4150
		$parent =& $this->GetParent();
4151
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4152
		$tree .= $this->GetQname() . "</a>";
4153
		$tree .= "</li>";
4154

    
4155
		return $tree;
4156
	}
4157

    
4158
	function build_rules() {
4159
		if ($this->GetEnabled() == "") {
4160
			return;
4161
		}
4162

    
4163
		$parent =& $this->GetParent();
4164
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4165
		if ($this->GetQlimit()) {
4166
			$pfq_rule .= " queue " . $this->GetQlimit();
4167
		}
4168
		if ($this->GetWeight()) {
4169
			$pfq_rule .= " weight " . $this->GetWeight();
4170
		}
4171
		if ($this->GetBuckets()) {
4172
			$pfq_rule .= " buckets " . $this->GetBuckets();
4173
		}
4174
		$this->build_mask_rules($pfq_rule);
4175
		$pfq_rule .= "\n";
4176

    
4177
		return $pfq_rule;
4178
	}
4179

    
4180
	function build_javascript() {
4181
		return parent::build_javascript();
4182
	}
4183

    
4184
	function build_form() {
4185
		global $g, $config, $pipe, $action, $qname;
4186

    
4187
		//build list of schedules
4188
		$schedules = array();
4189
		$schedules[] = "none";//leave none to leave rule enabled all the time
4190
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4191
			foreach ($config['schedules']['schedule'] as $schedule) {
4192
				if ($schedule['name'] <> "") {
4193
					$schedules[] = $schedule['name'];
4194
				}
4195
			}
4196
		}
4197

    
4198

    
4199
		$sform = new Form();
4200
		$sform->setAction("firewall_shaper.php");
4201
		$section = new Form_Section('Limiters');
4202

    
4203
		$section->addInput(new Form_Checkbox(
4204
			'enabled',
4205
			'Enable',
4206
			'Enable this queue',
4207
			($this->GetEnabled() == "on"),
4208
			'on'
4209
		));
4210

    
4211
		$section->addInput(new Form_Input(
4212
			'newname',
4213
			'Name',
4214
			'text',
4215
			$this->GetQname()
4216
		));
4217

    
4218
		$section->addInput(new Form_Input(
4219
			'name',
4220
			null,
4221
			'hidden',
4222
			$this->GetQname()
4223
		));
4224

    
4225
		if ($this->GetNumber() > 0) {
4226
			$section->addInput(new Form_Input(
4227
				'number',
4228
				null,
4229
				'hidden',
4230
				$this->GetNumber()
4231
			));
4232
		}
4233

    
4234
		$mask = $this->GetMask();
4235

    
4236
		$section->addInput(new Form_Select(
4237
			'mask',
4238
			'Mask',
4239
			$mask['type'],
4240
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4241
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4242
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4243
					'This makes it possible to easily specify bandwidth limits per host.');
4244

    
4245
		$group = new Form_Group(null);
4246

    
4247
		$group->add(new Form_Select(
4248
			'maskbits',
4249
			null,
4250
			$mask['bits'],
4251
			array_combine(range(32, 1, -1), range(32, 1, -1))
4252
		))->setHelp('IPV4 mask bits' . '<br />' . '255.255.255.255/?');
4253

    
4254
		$group->add(new Form_Select(
4255
			'maskbitsv6',
4256
			null,
4257
			$mask['bitsv6'],
4258
			array_combine(range(128, 1, -1), range(128, 1, -1))
4259
		))->setHelp('IPV6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4260

    
4261
		$section->add($group);
4262

    
4263
		$section->addInput(new Form_Input(
4264
			'description',
4265
			'Description',
4266
			'text',
4267
			$this->GetDescription()
4268
		))->setHelp('You may enter a description here for your reference (not parsed).');
4269

    
4270
		$sform->add($section);
4271

    
4272
		$section = new Form_Section('Advanced Options');
4273

    
4274
		$section->addInput(new Form_Input(
4275
			'weight',
4276
			'Weight',
4277
			'number',
4278
			$this->GetWeight(),
4279
			['min' => '1', 'max' => '100']
4280
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4281
					' you can leave it blank otherwise');
4282

    
4283
		$section->addInput(new Form_Input(
4284
			'plr',
4285
			'Packet Loss Rate',
4286
			'number',
4287
			$this->GetPlr(),
4288
			['step' => '0.001', 'min' => '0.000']
4289
		))->setHelp('In most cases, you should specify 0 here (or leave the field empty). ' .
4290
					'A value of 0.001 means one packet in 1000 gets dropped');
4291

    
4292
		$section->addInput(new Form_Input(
4293
			'qlimit',
4294
			'Queue size (slots)',
4295
			'number',
4296
			$this->GetQlimit()
4297
		))->setHelp('In most cases, you should leave the field empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4298
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4299

    
4300
		$section->addInput(new Form_Input(
4301
			'buckets',
4302
			'Bucket size (slots)',
4303
			'number',
4304
			$this->GetBuckets()
4305
		))->setHelp('In most cases, you should leave this field empty. It increases the hash size set');
4306

    
4307
		$section->addInput(new Form_Input(
4308
			'pipe',
4309
			null,
4310
			'hidden',
4311
			$this->GetPipe()
4312
		));
4313

    
4314
		$sform->add($section);
4315

    
4316
		return($sform);
4317
	}
4318

    
4319
	function update_dn_data(&$data) {
4320
		$this->ReadConfig($data);
4321
	}
4322

    
4323
	function wconfig() {
4324
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4325
		if (!is_array($cflink)) {
4326
			$cflink = array();
4327
		}
4328
		$cflink['name'] = $this->GetQname();
4329
		$cflink['number'] = $this->GetNumber();
4330
		$cflink['qlimit'] = $this->GetQlimit();
4331
		$cflink['description'] = $this->GetDescription();
4332
		$cflink['weight'] = $this->GetWeight();
4333
		$cflink['enabled'] = $this->GetEnabled();
4334
		$cflink['buckets'] = $this->GetBuckets();
4335
		$mask = $this->GetMask();
4336
		$cflink['mask'] = $mask['type'];
4337
		$cflink['maskbits'] = $mask['bits'];
4338
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4339
	}
4340
}
4341

    
4342
function get_dummynet_name_list() {
4343

    
4344
	$dn_name_list =& get_unique_dnqueue_list();
4345
	$dn_name = array();
4346
	if (is_array($dn_name_list)) {
4347
		foreach ($dn_name_list as $key => $value) {
4348
			$dn_name[] = $key;
4349
		}
4350
	}
4351

    
4352
	return $dn_name;
4353

    
4354
}
4355

    
4356
function get_altq_name_list() {
4357
	$altq_name_list =& get_unique_queue_list();
4358
	$altq_name = array();
4359
	if (is_array($altq_name_list)) {
4360
		foreach ($altq_name_list as $key => $aqobj) {
4361
			$altq_name[] = $key;
4362
		}
4363
	}
4364

    
4365
	return $altq_name;
4366
}
4367

    
4368
/*
4369
 * XXX: TODO Make a class shaper to hide all these functions
4370
 * from the global namespace.
4371
 */
4372

    
4373
/*
4374
 * This is a layer violation but for now there is no way
4375
 * I can find to properly do this with PHP.
4376
 */
4377
function altq_get_default_queue($interface) {
4378
	global $altq_list_queues;
4379

    
4380
	$altq_tmp = $altq_list_queues[$interface];
4381
	if ($altq_tmp) {
4382
		return $altq_tmp->GetDefaultQueuePresent();
4383
	} else {
4384
		return false;
4385
	}
4386
}
4387

    
4388
function altq_check_default_queues() {
4389
	global $altq_list_queues;
4390

    
4391
	$count = 0;
4392
	if (is_array($altq_list_queues)) {
4393
		foreach ($altq_list_queues as $altq) {
4394
			if ($altq->GetDefaultQueuePresent()) {
4395
				$count++;
4396
			}
4397
		}
4398
	}
4399
	else {
4400
		$count++;
4401
	}
4402

    
4403
	return 0;
4404
}
4405

    
4406
function &get_unique_queue_list() {
4407
	global $altq_list_queues;
4408

    
4409
	$qlist = array();
4410
	if (is_array($altq_list_queues)) {
4411
		foreach ($altq_list_queues as $altq) {
4412
			if ($altq->GetEnabled() == "") {
4413
				continue;
4414
			}
4415
			$tmplist =& $altq->get_queue_list();
4416
			foreach ($tmplist as $qname => $link) {
4417
				if ($link->GetEnabled() <> "") {
4418
					$qlist[$qname] = $link;
4419
				}
4420
			}
4421
		}
4422
	}
4423
	return $qlist;
4424
}
4425

    
4426
function &get_unique_dnqueue_list() {
4427
	global $dummynet_pipe_list;
4428

    
4429
	$qlist = array();
4430
	if (is_array($dummynet_pipe_list)) {
4431
		foreach ($dummynet_pipe_list as $dn) {
4432
			if ($dn->GetEnabled() == "") {
4433
				continue;
4434
			}
4435
			$tmplist =& $dn->get_queue_list();
4436
			foreach ($tmplist as $qname => $link) {
4437
				$qlist[$qname] = $link;
4438
			}
4439
		}
4440
	}
4441
	return $qlist;
4442
}
4443

    
4444
function ref_on_altq_queue_list($parent, $qname) {
4445
	if (isset($GLOBALS['queue_list'][$qname])) {
4446
		$GLOBALS['queue_list'][$qname]++;
4447
	} else {
4448
		$GLOBALS['queue_list'][$qname] = 1;
4449
	}
4450

    
4451
	unref_on_altq_queue_list($parent);
4452
}
4453

    
4454
function unref_on_altq_queue_list($qname) {
4455
	$GLOBALS['queue_list'][$qname]--;
4456
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4457
		unset($GLOBALS['queue_list'][$qname]);
4458
	}
4459
}
4460

    
4461
function read_altq_config() {
4462
	global $altq_list_queues, $config;
4463
	$path = array();
4464

    
4465
	if (!is_array($config['shaper'])) {
4466
		$config['shaper'] = array();
4467
	}
4468
	if (!is_array($config['shaper']['queue'])) {
4469
		$config['shaper']['queue'] = array();
4470
	}
4471
	$a_int = &$config['shaper']['queue'];
4472

    
4473
	$altq_list_queues = array();
4474

    
4475
	if (!is_array($config['shaper']['queue'])) {
4476
		return;
4477
	}
4478

    
4479
	foreach ($a_int as $key => $conf) {
4480
		$int = $conf['interface'];
4481
		$root =& new altq_root_queue();
4482
		$root->SetInterface($int);
4483
		$altq_list_queues[$root->GetInterface()] = &$root;
4484
		$root->ReadConfig($conf);
4485
		array_push($path, $key);
4486
		$root->SetLink($path);
4487
		if (is_array($conf['queue'])) {
4488
			foreach ($conf['queue'] as $key1 => $q) {
4489
				array_push($path, $key1);
4490
				/*
4491
				 * XXX: we completely ignore errors here but anyway we must have
4492
				 *	checked them before so no harm should be come from this.
4493
				 */
4494
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4495
				array_pop($path);
4496
			}
4497
		}
4498
		array_pop($path);
4499
	}
4500
}
4501

    
4502
function read_dummynet_config() {
4503
	global $dummynet_pipe_list, $config;
4504
	$path = array();
4505

    
4506
	if (!is_array($config['dnshaper'])) {
4507
		$config['dnshaper'] = array();
4508
	}
4509
	if (!is_array($config['dnshaper']['queue'])) {
4510
		$config['dnshaper']['queue'] = array();
4511
	}
4512
	$a_int = &$config['dnshaper']['queue'];
4513

    
4514
	$dummynet_pipe_list = array();
4515

    
4516
	if (!is_array($config['dnshaper']['queue']) ||
4517
	    !count($config['dnshaper']['queue'])) {
4518
		return;
4519
	}
4520

    
4521
	foreach ($a_int as $key => $conf) {
4522
		if (empty($conf['name'])) {
4523
			continue; /* XXX: grrrrrr at php */
4524
		}
4525
		$root =& new dnpipe_class();
4526
		$root->ReadConfig($conf);
4527
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4528
		array_push($path, $key);
4529
		$root->SetLink($path);
4530
		if (is_array($conf['queue'])) {
4531
			foreach ($conf['queue'] as $key1 => $q) {
4532
				array_push($path, $key1);
4533
				/*
4534
				 * XXX: we completely ignore errors here but anyway we must have
4535
				 *	checked them before so no harm should be come from this.
4536
				 */
4537
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4538
				array_pop($path);
4539
			}
4540
		}
4541
		array_pop($path);
4542
	}
4543
}
4544

    
4545
function get_interface_list_to_show() {
4546
	global $altq_list_queues, $config;
4547
	global $shaperIFlist;
4548

    
4549
	$tree = "";
4550
	foreach ($shaperIFlist as $shif => $shDescr) {
4551
		if ($altq_list_queues[$shif]) {
4552
			continue;
4553
		} else {
4554
			if (!is_altq_capable(get_real_interface($shif))) {
4555
				continue;
4556
			}
4557
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4558
		}
4559
	}
4560

    
4561
	return $tree;
4562
}
4563

    
4564
function filter_generate_altq_queues() {
4565
	global $altq_list_queues;
4566

    
4567
	read_altq_config();
4568

    
4569
	$altq_rules = "";
4570
	foreach ($altq_list_queues as $altq) {
4571
		$altq_rules .= $altq->build_rules();
4572
	}
4573

    
4574
	return $altq_rules;
4575
}
4576

    
4577
function dnqueue_find_nextnumber() {
4578
	global $dummynet_pipe_list;
4579

    
4580
	$dnused = array();
4581
	if (is_array($dummynet_pipe_list)) {
4582
		foreach ($dummynet_pipe_list as $dn) {
4583
			$tmplist =& $dn->get_queue_list();
4584
			foreach ($tmplist as $qname => $link) {
4585
				if ($link[0] == "?") {
4586
					$dnused[$qname] = substr($link, 1);
4587
				}
4588
			}
4589
		}
4590
	}
4591

    
4592
	sort($dnused, SORT_NUMERIC);
4593
	$dnnumber = 0;
4594
	$found = false;
4595
	foreach ($dnused as $dnnum) {
4596
		if (($dnnum - $dnnumber) > 1) {
4597
			$dnnumber = $dnnum - 1;
4598
			$found = true;
4599
			break;
4600
		} else {
4601
			$dnnumber = $dnnum;
4602
		}
4603
	}
4604

    
4605
	if ($found == false) {
4606
		$dnnumber++;
4607
	}
4608

    
4609
	unset($dnused, $dnnum, $found);
4610
	return $dnnumber;
4611
}
4612

    
4613
function dnpipe_find_nextnumber() {
4614
	global $dummynet_pipe_list;
4615

    
4616
	$dnused = array();
4617
	foreach ($dummynet_pipe_list as $dn) {
4618
		$dnused[] = $dn->GetNumber();
4619
	}
4620

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

    
4634
	if ($found == false) {
4635
		$dnnumber++;
4636
	}
4637

    
4638
	unset($dnused, $dnnum, $found);
4639
	return $dnnumber;
4640
}
4641

    
4642
function filter_generate_dummynet_rules() {
4643
	global $g, $dummynet_pipe_list;
4644

    
4645
	read_dummynet_config();
4646

    
4647
	$dn_rules = "";
4648
	foreach ($dummynet_pipe_list as $dn) {
4649
		$dn_rules .= $dn->build_rules();
4650
	}
4651

    
4652
	if (!empty($dn_rules)) {
4653
		if (!is_module_loaded("dummynet.ko")) {
4654
			mwexec("/sbin/kldload dummynet");
4655
			set_sysctl(array(
4656
				"net.inet.ip.dummynet.io_fast" => "1",
4657
				"net.inet.ip.dummynet.hash_size" => "256"
4658
			));
4659
		}
4660
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4661
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4662
	}
4663
}
4664

    
4665
function build_iface_without_this_queue($iface, $qname) {
4666
	global $g, $altq_list_queues;
4667
	global $shaperIFlist;
4668

    
4669
	$altq =& $altq_list_queues[$iface];
4670

    
4671
	if ($altq) {
4672
		$scheduler = $altq->GetScheduler();
4673
	}
4674

    
4675
	$form = '<dl class="dl-horizontal">';
4676

    
4677
	$form .= '	<dt>';
4678
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4679
	$form .= '	</dt>';
4680
	$form .= '	<dd>';
4681
	$form .=		$scheduler;
4682
	$form .= '	</dd>';
4683

    
4684
	$form .= '	<dt>';
4685
	$form .= 'Clone';
4686
	$form .= '	</dt>';
4687
	$form .= '	<dd>';
4688
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4689
	$form .= $iface . '&amp;queue=';
4690
	$form .= $qname . '&amp;action=add">';
4691
	$form .= gettext("Clone shaper on the I/F") . '</a>';
4692
	$form .= '	</dd>';
4693

    
4694
	$form .= '</dl>';
4695

    
4696
	return $form;
4697

    
4698
}
4699

    
4700
$default_shaper_msg = sprintf(gettext("Welcome to the %s Traffic Shaper."), $g['product_name']) . "<br />";
4701
$default_shaper_msg .= gettext("The tree on the left helps you navigate through the queues.<br />"
4702
	. "Buttons at the bottom represent queue actions and are activated accordingly.");
4703

    
4704
$dn_default_shaper_msg = $default_shaper_msg;
4705

    
4706
?>
(50-50/65)