Project

General

Profile

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

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

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

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

    
76
	return $ptr;
77
}
78

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

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

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

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

    
97
	return $ptr;
98
}
99

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

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

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

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

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

    
207
function get_bandwidth($bw, $scale, $obj) {
208

    
209
	$pattern= "/(b|Kb|Mb|Gb|%)/";
210
	if (!preg_match($pattern, $scale, $match))
211
		return 0;
212

    
213
	switch ($match[1]) {
214
		case '%':
215
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
216
			break;
217
		default:
218
			$objbw = $bw * get_bandwidthtype_scale($scale);
219
			break;
220
	}
221

    
222
	return floatval($objbw);
223
}
224

    
225
/*
226
 * XXX - unused
227
 *
228
function get_hfsc_bandwidth($object, $bw) {
229
	$pattern= "/[0-9]+/";
230
	if (preg_match($pattern, $bw, $match)) {
231
		$bw_1 = $match[1];
232
	} else {
233
		return 0;
234
	}
235
	$pattern= "/(b|Kb|Mb|Gb|%)/";
236
	if (preg_match($pattern, $bw, $match)) {
237
		switch ($match[1]) {
238
			case '%':
239
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
240
				break;
241
			default:
242
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
243
				break;
244
		}
245
		return floatval($bw_1);
246
	} else {
247
		return 0;
248
	}
249
}
250
*/
251

    
252
function get_queue_bandwidth($obj) {
253
	$bw = $obj->GetBandwidth();
254
	$scale = $obj->GetBwscale();
255

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

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

    
269
	return floatval($objbw);
270
}
271

    
272
function get_interface_bandwidth($object) {
273
	global $altq_list_queues;
274

    
275
	$int = $object->GetInterface();
276
	$altq =& $altq_list_queues[$int];
277
	if ($altq) {
278
		$bw_3 = $altq->GetBandwidth();
279
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
280
		return floatval($bw_3);
281
	} else {
282
		return 0;
283
	}
284
}
285

    
286
/*
287
 * This is duplicated here since we cannot include guiconfig.inc.
288
 * Including it makes all stuff break.
289
 */
290
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
291

    
292
	/* check for bad control characters */
293
	foreach ($postdata as $pn => $pd) {
294
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
295
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
296
		}
297
	}
298

    
299
	for ($i = 0; $i < count($reqdfields); $i++) {
300
		if ($postdata[$reqdfields[$i]] == "") {
301
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
302
		}
303
	}
304
}
305

    
306
function cleanup_queue_from_rules($queue) {
307
	global $config;
308

    
309
	foreach ($config['filter']['rule'] as $rule) {
310
		if ($rule['defaultqueue'] == $queue) {
311
			unset($rule['defaultqueue']);
312
		}
313
		if ($rule['ackqueue'] == $queue) {
314
			unset($rule['ackqueue']);
315
		}
316
	}
317
}
318

    
319
function cleanup_dnqueue_from_rules($queue) {
320
	global $config;
321

    
322
	foreach ($config['filter']['rule'] as $rule) {
323
		if ($rule['dnpipe'] == $queue) {
324
			unset($rule['dnpipe']);
325
		}
326
		if ($rule['pdnpipe'] == $queue) {
327
			unset($rule['pdnpipe']);
328
		}
329
	}
330
}
331

    
332
class altq_root_queue {
333
	var $interface;
334
	var $tbrconfig ;
335
	var $bandwidth;
336
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
337
	var $scheduler;
338
	var $qlimit;
339
	var $queues = array();
340
	var $qenabled = false;
341
	var $link;
342

    
343
	/* Accessor functions */
344
	function GetDefaultQueuePresent() {
345
		if (!empty($this->queues)) {
346
			foreach ($this->queues as $q) {
347
				if ($q->GetDefault()) {
348
					return true;
349
				}
350
			}
351
		}
352

    
353
		return false;
354
	}
355
	function SetLink($link) {
356
		$this->link = $link;
357
	}
358
	function GetLink() {
359
		return $this->link;
360
	}
361
	function GetEnabled() {
362
		return $this->qenabled;
363
	}
364
	function SetEnabled($value) {
365
		$this->qenabled = $value;
366
	}
367
	function CanHaveChildren() {
368
		if ($this->GetScheduler() == "CODELQ") {
369
			return false;
370
		} else {
371
			return true;
372
		}
373
	}
374
	function CanBeDeleted() {
375
		return false;
376
	}
377
	function GetQname() {
378
		return $this->interface;
379
	}
380
	function SetQname($name) {
381
		$this->interface = trim($name);
382
	}
383
	function GetInterface() {
384
		return $this->interface;
385
	}
386
	function SetInterface($name) {
387
		$this->interface = trim($name);
388
	}
389
	function GetTbrConfig() {
390
		return $this->tbrconfig;
391
	}
392
	function SetTbrConfig($tbrconfig) {
393
		$this->tbrconfig = $tbrconfig;
394
	}
395
	function GetBandwidth() {
396
		return $this->bandwidth;
397
	}
398
	function SetBandwidth($bw) {
399
		$this->bandwidth = $bw;
400
	}
401
	function GetBwscale() {
402
		return $this->bandwidthtype;
403
	}
404
	function SetBwscale($bwscale) {
405
		$this->bandwidthtype = $bwscale;
406
	}
407
	function GetScheduler() {
408
		return $this->scheduler;
409
	}
410
	function SetScheduler($scheduler) {
411
		$this->scheduler = trim($scheduler);
412
	}
413
	function GetQlimit() {
414
		return $this->qlimit;
415
	}
416
	function SetQlimit($limit) {
417
		$this->qlimit = $limit;
418
	}
419

    
420
	function GetBwscaleText() {
421
		switch ($this->bandwidthtype) {
422
			case "b":
423
				$bwscaletext = "Bit/s";
424
				break;
425
			case "Kb":
426
				$bwscaletext = "Kbit/s";
427
				break;
428
			case "Mb":
429
				$bwscaletext = "Mbit/s";
430
				break;
431
			case "Gb":
432
				$bwscaletext = "Gbit/s";
433
				break;
434
			default:
435
				/* For others that do not need translating like % */
436
				$bwscaletext = $this->bandwidthtype;
437
				break;
438
		}
439
		return $bwscaletext;
440
	}
441

    
442
	function CheckBandwidth($bw, $bwtype) {
443
		$sum = $this->GetTotalBw();
444
		if ($sum > $bw * get_bandwidthtype_scale($bwtype))
445
			return 1;
446
		foreach ($this->queues as $q) {
447
			if ($q->CheckBandwidth(0, ''))
448
				return 1;
449
		}
450

    
451
		return 0;
452
	}
453

    
454
	function GetTotalBw($qignore = NULL) {
455
		$sum = 0;
456
		foreach ($this->queues as $q) {
457
			if ($qignore != NULL && $qignore == $q)
458
				continue;
459
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
460
		}
461

    
462
		return $sum;
463
	}
464

    
465
	function validate_input($data, &$input_errors) {
466

    
467
		$reqdfields[] = "bandwidth";
468
		$reqdfieldsn[] = gettext("Bandwidth");
469
		$reqdfields[] = "bandwidthtype";
470
		$reqdfieldsn[] = gettext("Bandwidthtype");
471

    
472
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
473

    
474
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
475
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
476
		}
477
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
478
			$input_errors[] = gettext("Bandwidth must be an integer.");
479
		}
480
		if ($data['bandwidth'] < 0) {
481
			$input_errors[] = gettext("Bandwidth cannot be negative.");
482
		}
483
		if ($data['bandwidthtype'] == "%") {
484
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
485
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
486
			}
487
		}
488
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
489
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
490

    
491
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
492
			$input_errors[] = gettext("Qlimit must be an integer.");
493
		}
494
		if ($data['qlimit'] < 0) {
495
			$input_errors[] = gettext("Qlimit must be positive.");
496
		}
497
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
498
			$input_errors[] = gettext("Tbrsize must be an integer.");
499
		}
500
		if ($data['tbrconfig'] < 0) {
501
			$input_errors[] = gettext("Tbrsize must be positive.");
502
		}
503
	}
504

    
505
	/* Implement this to shorten some code on the frontend page */
506
	function ReadConfig(&$conf) {
507
		if (isset($conf['tbrconfig'])) {
508
			$this->SetTbrConfig($conf['tbrconfig']);
509
		} else {
510
			$this->SetTbrConfig($conf['tbrconfig']);
511
		}
512
		$this->SetBandwidth($conf['bandwidth']);
513
		if ($conf['bandwidthtype'] <> "") {
514
			$this->SetBwscale($conf['bandwidthtype']);
515
		}
516
		if (isset($conf['scheduler'])) {
517
			if ($this->GetScheduler() != $conf['scheduler']) {
518
				foreach ($this->queues as $q) {
519
					clean_child_queues($conf['scheduler'], $this->GetLink());
520
					$q->clean_queue($conf['scheduler']);
521
				}
522
			}
523
			$this->SetScheduler($conf['scheduler']);
524
		}
525
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
526
			$this->SetQlimit($conf['qlimit']);
527
		} else {
528
			$this->SetQlimit("");
529
		}
530
		if (isset($conf['name'])) {
531
			$this->SetQname($conf['name']);
532
		}
533
		if (!empty($conf['enabled'])) {
534
			$this->SetEnabled($conf['enabled']);
535
		} else {
536
			$this->SetEnabled("");
537
		}
538
	}
539

    
540
	function copy_queue($interface, &$cflink) {
541
		$cflink['interface'] = $interface;
542
		$cflink['name'] = $interface;
543
		$cflink['scheduler'] = $this->GetScheduler();
544
		$cflink['bandwidth'] = $this->GetBandwidth();
545
		$cflink['bandwidthtype'] = $this->GetBwscale();
546
		$cflink['qlimit'] = $this->GetQlimit();
547
		$cflink['tbrconfig'] = $this->GetTbrConfig();
548
		$cflink['enabled'] = $this->GetEnabled();
549
		if (is_array($this->queues)) {
550
			$cflink['queue'] = array();
551
			foreach ($this->queues as $q) {
552
				$cflink['queue'][$q->GetQname()] = array();
553
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
554
			}
555
		}
556
	}
557

    
558
	function &get_queue_list(&$q = null) {
559
		$qlist = array();
560

    
561
		//$qlist[$this->GetQname()] = & $this;
562
		if (is_array($this->queues)) {
563
			foreach ($this->queues as $queue) {
564
				$queue->get_queue_list($qlist);
565
			}
566
		}
567
		return $qlist;
568
	}
569

    
570
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
571

    
572
		if (!is_array($this->queues)) {
573
			$this->queues = array();
574
		}
575

    
576
		switch ($this->GetScheduler()) {
577
			case "PRIQ":
578
				$q =& new priq_queue();
579
				break;
580
			case "HFSC":
581
				$q =& new hfsc_queue();
582
				break;
583
			case "CBQ":
584
				$q =& new cbq_queue();
585
				break;
586
			case "FAIRQ":
587
				$q =& new fairq_queue();
588
				break;
589
			default:
590
				/* XXX: but should not happen anyway */
591
				return;
592
				break;
593
		}
594
		$q->SetLink($path);
595
		$q->SetInterface($this->GetInterface());
596
		$q->SetEnabled("on");
597
		$q->SetParent($this);
598
		$q->ReadConfig($queue);
599
		$q->validate_input($queue, $input_errors);
600

    
601
		$this->queues[$q->GetQname()] = &$q;
602
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
603
		if (is_array($queue['queue'])) {
604
			foreach ($queue['queue'] as $key1 => $que) {
605
				array_push($path, $key1);
606
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
607
				array_pop($path);
608
			}
609
		}
610

    
611
		return $q;
612
	}
613

    
614
	/* interface here might be optional */
615
	function &find_queue($interface, $qname) {
616
		if ($qname == $this->GetQname()) {
617
			return $this;
618
		}
619
		foreach ($this->queues as $q) {
620
			$result =& $q->find_queue("", $qname);
621
			if ($result) {
622
				return $result;
623
			}
624
		}
625
	}
626

    
627
	function &find_parentqueue($interface, $qname) {
628
		if ($qname == $interface) {
629
			$result = NULL;
630
		} else if ($this->queues[$qname]) {
631
			$result = $this;
632
		} else if ($this->GetScheduler() <> "PRIQ") {
633
			foreach ($this->queues as $q) {
634
				$result = $q->find_parentqueue("", $qname);
635
				if ($result) {
636
					return $result;
637
				}
638
			}
639
		}
640
	}
641

    
642
	function build_tree() {
643
		global $shaperIFlist;
644

    
645
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
646
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
647
		if (is_array($this->queues)) {
648
			$tree .= "<ul>";
649
			foreach ($this->queues as $q) {
650
				$tree .= $q->build_tree();
651
			}
652
			$tree .= "</ul>";
653
		}
654
		$tree .= "</li>";
655
		return $tree;
656
	}
657

    
658
	function delete_queue() {
659
		foreach ($this->queues as $q)
660
			$q->delete_queue();
661
		unset_object_by_reference($this->GetLink());
662
	}
663

    
664
	function delete_all() {
665
		if (count($this->queues)) {
666
			foreach ($this->queues as $q) {
667
				$q->delete_all();
668
				unset_object_by_reference($q->GetLink());
669
				unset($q);
670
			}
671
			unset($this->queues);
672
		}
673
	}
674

    
675
	/*
676
	 * First it spits:
677
	 * altq on $interface ..............
678
	 *	then it goes like
679
	 *	foreach ($queues as $qkey => $queue) {
680
	 *		this->queues[$qkey]->build_rule();
681
	 *	}
682
	 */
683
	function build_rules(&$default = false) {
684
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
685
			$default = false;
686
			$rules = " altq on " . get_real_interface($this->GetInterface());
687
			if ($this->GetScheduler()) {
688
				$rules .= " ".strtolower($this->GetScheduler());
689
			}
690
			if ($this->GetQlimit() > 0) {
691
				$rules .= " qlimit " . $this->GetQlimit() . " ";
692
			}
693
			if ($this->GetBandwidth()) {
694
				$rules .= " bandwidth ".trim($this->GetBandwidth());
695
				if ($this->GetBwscale()) {
696
					$rules .= $this->GetBwscale();
697
				}
698
			}
699
			if ($this->GetTbrConfig()) {
700
				$rules .= " tbrsize ".$this->GetTbrConfig();
701
			}
702
			if (count($this->queues)) {
703
				$i = count($this->queues);
704
				$rules .= " queue { ";
705
				foreach ($this->queues as $qkey => $qnone) {
706
					if ($i > 1) {
707
						$i--;
708
						$rules .= " {$qkey}, ";
709
					} else {
710
						$rules .= " {$qkey} ";
711
					}
712
				}
713
				$rules .= " } \n";
714
				foreach ($this->queues as $q) {
715
					$rules .= $q->build_rules($default);
716
				}
717
			}
718

    
719
			if ($default == false) {
720
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
721
				file_notice("Shaper", $error, "Error occurred", "");
722
				unset($error);
723
				return "\n";
724
			}
725
			$frule .= $rules;
726
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
727
			$rules = " altq on " . get_real_interface($this->GetInterface());
728
			if ($this->GetScheduler()) {
729
				$rules .= " ".strtolower($this->GetScheduler());
730
			}
731
			if ($this->GetQlimit() > 0) {
732
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
733
			}
734
			if ($this->GetBandwidth()) {
735
				$rules .= " bandwidth ".trim($this->GetBandwidth());
736
				if ($this->GetBwscale()) {
737
					$rules .= $this->GetBwscale();
738
				}
739
			}
740
			if ($this->GetTbrConfig()) {
741
				$rules .= " tbrsize ".$this->GetTbrConfig();
742
			}
743

    
744
			$rules .= " queue";
745
		}
746

    
747
		$rules .= " \n";
748
		return $rules;
749
	}
750

    
751
	function build_javascript() {
752
		$javascript = "<script type=\"text/javascript\">";
753
		$javascript .= "//<![CDATA[\n";
754
		$javascript .= "function mySuspend() {";
755
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
756
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
757
		$javascript .= "else if (document.all)";
758
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
759
		$javascript .= "}";
760

    
761
		$javascript .= "function myResume() {";
762
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
763
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
764
		$javascript .= "else if (document.all) ";
765
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
766
		$javascript .= "}";
767
		$javascript .= "//]]>";
768
		$javascript .= "</script>";
769

    
770
		return $javascript;
771
	}
772

    
773
	function build_shortform() {
774
		global $g;
775

    
776
		$altq =& $this;
777

    
778
		if ($altq) {
779
			$scheduler = ": " . $altq->GetScheduler();
780
		}
781

    
782
		$form = '<dl class="dl-horizontal">';
783
		$form .= '	<dt>';
784
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
785
		$form .= '	</dt>';
786
		$form .= '	<dd>';
787
		$form .=		$scheduler;
788
		$form .= '	</dd>';
789

    
790
		$form .= '	<dt>';
791
		$form .=		'Bandwidth';
792
		$form .= '	</dt>';
793
		$form .= '	<dd>';
794
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
795
		$form .= '	</dd>';
796

    
797
		$form .= '	<dt>';
798
		$form .= 'Disable';
799
		$form .= '	<dt>';
800
		$form .= '	<dd>';
801

    
802
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
803
		$form .= $this->GetInterface() . '&amp;queue=';
804
		$form .= $this->GetQname() . '&amp;action=delete">';
805
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
806
		$form .= gettext("Remove shaper from this interface") . '</a>';
807

    
808
		$form .= '	</dd>';
809

    
810
		$form .= '</dl>';
811

    
812
		return $form;
813

    
814
	}
815

    
816
	/*
817
	 * For requesting the parameters of the root queues
818
	 * to the user like the traffic wizard does.
819
	 */
820
	function build_form() {
821

    
822
		$sform = new Form();
823

    
824
		$sform->setAction("firewall_shaper.php");
825

    
826
		$section = new Form_Section(null);
827

    
828
		$section->addInput(new Form_Checkbox(
829
			'enabled',
830
			'Enable/Disable',
831
			'Enable/disable discipline and its children',
832
			($this->GetEnabled() == "on"),
833
			'on'
834
		));
835

    
836
		$section->addInput(new Form_StaticText(
837
			'Name',
838
			$this->GetQname()
839
		));
840

    
841
		$section->addInput(new Form_Select(
842
			'scheduler',
843
			'Scheduler Type',
844
			$this->GetScheduler(),
845
			array('HFSC' => 'HFSC',
846
				  'CBQ' => 'CBQ',
847
				  'FAIRQ' => 'FAIRQ',
848
				  'CODELQ' => 'CODELQ',
849
				  'PRIQ' => 'PRIQ')
850
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
851

    
852
		$group = new Form_group('Bandwidth');
853

    
854
		$group->add(new Form_Input(
855
			'bandwidth',
856
			null,
857
			'number',
858
			$this->GetBandwidth()
859
		));
860

    
861
		$group->add(new Form_Select(
862
			'bandwidthtype',
863
			null,
864
			$this->GetBwscale(),
865
			array('Kb' => 'Kbit/s',
866
				  'Mb' => 'Mbit/s',
867
				  'Gb' => 'Gbit/s',
868
				  'b' => 'Bit/s',
869
				  '%' => '%')
870
		));
871

    
872
		$section->add($group);
873

    
874
		$section->addInput(new Form_Input(
875
			'qlimit',
876
			'Queue Limit',
877
			'number',
878
			$this->GetQlimit()
879
		));
880

    
881
		$section->addInput(new Form_Input(
882
			'tbrconfig',
883
			'TBR Size',
884
			'number',
885
			$this->GetTbrConfig()
886
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
887
					'bandwidth are used to determine the size.');
888

    
889
		$section->addInput(new Form_Input(
890
			'interface',
891
			null,
892
			'hidden',
893
			$this->GetInterface()
894
		));
895

    
896
		$section->addInput(new Form_Input(
897
			'name',
898
			null,
899
			'hidden',
900
			$this->GetQname()
901
		));
902

    
903
		$sform->add($section);
904

    
905
		return($sform);
906
	}
907

    
908
	function update_altq_queue_data(&$data) {
909
		$this->ReadConfig($data);
910
	}
911

    
912
	/*
913
	 * Should call on each of it queues and subqueues
914
	 * the same function much like build_rules();
915
	 */
916
	function wconfig() {
917
		$cflink = &get_reference_to_me_in_config($this->GetLink());
918
		if (!is_array($cflink)) {
919
			$cflink = array();
920
		}
921
		$cflink['interface'] = $this->GetInterface();
922
		$cflink['name'] = $this->GetQname();
923
		$cflink['scheduler'] = $this->GetScheduler();
924
		$cflink['bandwidth'] = $this->GetBandwidth();
925
		$cflink['bandwidthtype'] = $this->GetBwscale();
926
		$cflink['qlimit'] = trim($this->GetQlimit());
927
		if (empty($cflink['qlimit'])) {
928
			unset($cflink['qlimit']);
929
		}
930
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
931
		if (empty($cflink['tbrconfig'])) {
932
			unset($cflink['tbrconfig']);
933
		}
934
		$cflink['enabled'] = $this->GetEnabled();
935
		if (empty($cflink['enabled'])) {
936
			unset($cflink['enabled']);
937
		}
938
	}
939

    
940
}
941

    
942
class priq_queue {
943
	var $qname;
944
	var $qinterface;
945
	var $qlimit;
946
	var $qpriority;
947
	var $description;
948
	var $isparent;
949
	var $qbandwidth;
950
	var $qbandwidthtype;
951
	var $qdefault = "";
952
	var $qrio = "";
953
	var $qred = "";
954
	var $qcodel = "";
955
	var $qecn = "";
956
	var $qack;
957
	var $qenabled = "";
958
	var $qparent;
959
	var $link;
960

    
961
	/* This is here to help with form building and building rules/lists */
962
	var $subqueues = array();
963

    
964
	/* Accessor functions */
965
	function SetLink($link) {
966
		$this->link = $link;
967
	}
968
	function GetLink() {
969
		return $this->link;
970
	}
971
	function &GetParent() {
972
		return $this->qparent;
973
	}
974
	function SetParent(&$parent) {
975
		$this->qparent = &$parent;
976
	}
977
	function GetEnabled() {
978
		return $this->qenabled;
979
	}
980
	function SetEnabled($value) {
981
		$this->qenabled = $value;
982
	}
983
	function CanHaveChildren() {
984
		return false;
985
	}
986
	function CanBeDeleted() {
987
		return true;
988
	}
989
	function GetQname() {
990
		return $this->qname;
991
	}
992
	function SetQname($name) {
993
		$this->qname = trim($name);
994
	}
995
	function GetBandwidth() {
996
		return $this->qbandwidth;
997
	}
998
	function SetBandwidth($bandwidth) {
999
		$this->qbandwidth = $bandwidth;
1000
	}
1001
	function GetInterface() {
1002
		return $this->qinterface;
1003
	}
1004
	function SetInterface($name) {
1005
		$this->qinterface = trim($name);
1006
	}
1007
	function GetQlimit() {
1008
		return $this->qlimit;
1009
	}
1010
	function SetQlimit($limit) {
1011
		$this->qlimit = $limit;
1012
	}
1013
	function GetQpriority() {
1014
		return $this->qpriority;
1015
	}
1016
	function SetQpriority($priority) {
1017
		$this->qpriority = $priority;
1018
	}
1019
	function GetDescription() {
1020
		return $this->description;
1021
	}
1022
	function SetDescription($str) {
1023
		$this->description = trim($str);
1024
	}
1025
	function GetFirstime() {
1026
		return $this->firsttime;
1027
	}
1028
	function SetFirsttime($number) {
1029
		$this->firsttime = $number;
1030
	}
1031
	function CheckBandwidth($bw, $bwtype) {
1032
		$parent = $this->GetParent();
1033
		$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1034
		if ($bw > 0)
1035
			$sum += get_bandwidth($bw, $bwtype, $parent);
1036
		if ($sum > get_queue_bandwidth($parent))
1037
			return 1;
1038

    
1039
		foreach ($this->subqueues as $q) {
1040
			if ($q->CheckBandwidth(0, ''))
1041
				return 1;
1042
		}
1043

    
1044
		return 0;
1045
	}
1046
	function GetTotalBw($qignore = NULL) {
1047
		$sum = 0;
1048
		foreach ($this->subqueues as $q) {
1049
			if ($qignore != NULL && $qignore == $q)
1050
				continue;
1051
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1052
		}
1053

    
1054
		return $sum;
1055
	}
1056
	function GetBwscale() {
1057
		return $this->qbandwidthtype;
1058
	}
1059
	function SetBwscale($scale) {
1060
		$this->qbandwidthtype = $scale;
1061
	}
1062
	function GetDefaultQueuePresent() {
1063
		if ($this->GetDefault()) {
1064
			return true;
1065
		}
1066
		if (!empty($this->subqueues)) {
1067
			foreach ($this->subqueues as $q) {
1068
				if ($q->GetDefault()) {
1069
					return true;
1070
				}
1071
			}
1072
		}
1073

    
1074
		return false;
1075
	}
1076
	function GetDefault() {
1077
		return $this->qdefault;
1078
	}
1079
	function SetDefault($value = false) {
1080
		$this->qdefault = $value;
1081
	}
1082
	function GetCodel() {
1083
		return $this->codel;
1084
	}
1085
	function SetCodel($codel = false) {
1086
		$this->codel = $codel;
1087
	}
1088
	function GetRed() {
1089
		return $this->qred;
1090
	}
1091
	function SetRed($red = false) {
1092
		$this->qred = $red;
1093
	}
1094
	function GetRio() {
1095
		return $this->qrio;
1096
	}
1097
	function SetRio($rio = false) {
1098
		$this->qrio = $rio;
1099
	}
1100
	function GetEcn() {
1101
		return $this->qecn;
1102
	}
1103
	function SetEcn($ecn = false) {
1104
		$this->qecn = $ecn;
1105
	}
1106
	function GetAck() {
1107
		return $this->qack;
1108
	}
1109
	function SetAck($ack = false) {
1110
		$this->qack = $ack;
1111
	}
1112

    
1113
	function GetBwscaleText() {
1114
		switch ($this->qbandwidthtype) {
1115
			case "b":
1116
				$bwscaletext = "Bit/s";
1117
				break;
1118
			case "Kb":
1119
				$bwscaletext = "Kbit/s";
1120
				break;
1121
			case "Mb":
1122
				$bwscaletext = "Mbit/s";
1123
				break;
1124
			case "Gb":
1125
				$bwscaletext = "Gbit/s";
1126
				break;
1127
			default:
1128
				/* For others that do not need translating like % */
1129
				$bwscaletext = $this->qbandwidthtype;
1130
				break;
1131
		}
1132
		return $bwscaletext;
1133
	}
1134

    
1135
	function build_javascript() {
1136
		$javascript = "<script type=\"text/javascript\">";
1137
		$javascript .= "//<![CDATA[\n";
1138
		$javascript .= "function mySuspend() { \n";
1139
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1140
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1141
		$javascript .= "else if (document.all)\n";
1142
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1143
		$javascript .= "}\n";
1144

    
1145
		$javascript .= "function myResume() {\n";
1146
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1147
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1148
		$javascript .= "else if (document.all)\n";
1149
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1150
		$javascript .= "}\n";
1151
		$javascript .= "//]]>";
1152
		$javascript .= "</script>";
1153

    
1154
		return $javascript;
1155
	}
1156

    
1157
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1158

    
1159
	/*
1160
	 * Currently this will not be called unless we decide to clone a whole
1161
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1162
	 */
1163
	function copy_queue($interface, &$cflink) {
1164

    
1165
		$cflink['name'] = $this->GetQname();
1166
		$cflink['interface'] = $interface;
1167
		$cflink['qlimit'] = $this->GetQlimit();
1168
		$cflink['priority'] = $this->GetQpriority();
1169
		$cflink['description'] = $this->GetDescription();
1170
		$cflink['enabled'] = $this->GetEnabled();
1171
		$cflink['default'] = $this->GetDefault();
1172
		$cflink['red'] = $this->GetRed();
1173
		$cflink['codel'] = $this->GetCodel();
1174
		$cflink['rio'] = $this->GetRio();
1175
		$cflink['ecn'] = $this->GetEcn();
1176

    
1177
		if (is_array($this->subqueues)) {
1178
			$cflinkp['queue'] = array();
1179
			foreach ($this->subqueues as $q) {
1180
				$cflink['queue'][$q->GetQname()] = array();
1181
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1182
			}
1183
		}
1184
	}
1185

    
1186
	function clean_queue($sched) {
1187
		clean_child_queues($sched, $this->GetLink());
1188
		if (is_array($this->subqueues)) {
1189
			foreach ($this->subqueues as $q) {
1190
				$q->clean_queue($sched);
1191
			}
1192
		}
1193
	}
1194

    
1195
	function &get_queue_list(&$qlist) {
1196

    
1197
		$qlist[$this->GetQname()] = & $this;
1198
		if (is_array($this->subqueues)) {
1199
			foreach ($this->subqueues as $queue) {
1200
				$queue->get_queue_list($qlist);
1201
			}
1202
		}
1203
	}
1204

    
1205
	function delete_queue() {
1206
		unref_on_altq_queue_list($this->GetQname());
1207
		cleanup_queue_from_rules($this->GetQname());
1208
		unset_object_by_reference($this->GetLink());
1209
	}
1210

    
1211
	function delete_all() {
1212
		if (count($this->subqueues)) {
1213
			foreach ($this->subqueues as $q) {
1214
				$q->delete_all();
1215
				unset_object_by_reference($q->GetLink());
1216
				unset($q);
1217
			}
1218
			unset($this->subqueues);
1219
		}
1220
	}
1221

    
1222
	function &find_queue($interface, $qname) {
1223
		if ($qname == $this->GetQname()) {
1224
			return $this;
1225
		}
1226
	}
1227

    
1228
	function find_parentqueue($interface, $qname) { return; }
1229

    
1230
	function validate_input($data, &$input_errors) {
1231

    
1232
		$reqdfields[] = "name";
1233
		$reqdfieldsn[] = gettext("Name");
1234
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1235

    
1236
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1237
			$input_errors[] = gettext("Bandwidth must be an integer.");
1238
		}
1239
		if ($data['bandwidth'] < 0) {
1240
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1241
		}
1242
		if ($data['bandwidthtype'] == "%") {
1243
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1244
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1245
			}
1246
		}
1247
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1248
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1249
		if ($data['priority'] && (!is_numeric($data['priority']) ||
1250
		    ($data['priority'] < 1) || ($data['priority'] > 15))) {
1251
			$input_errors[] = gettext("The priority must be an integer between 1 and 15.");
1252
		}
1253
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1254
				$input_errors[] = gettext("Queue limit must be an integer");
1255
		}
1256
		if ($data['qlimit'] < 0) {
1257
				$input_errors[] = gettext("Queue limit must be positive");
1258
		}
1259
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1260
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1261
		}
1262
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1263
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1264
		}
1265
		$default = $this->GetDefault();
1266
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1267
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1268
		}
1269
	}
1270

    
1271
	function ReadConfig(&$q) {
1272
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1273
			$this->SetQname($q['newname']);
1274
		} else if (!empty($q['newname'])) {
1275
			$this->SetQname($q['newname']);
1276
		} else if (isset($q['name'])) {
1277
			$this->SetQname($q['name']);
1278
		}
1279
		if (isset($q['interface'])) {
1280
			$this->SetInterface($q['interface']);
1281
		}
1282
		$this->SetBandwidth($q['bandwidth']);
1283
		if ($q['bandwidthtype'] <> "") {
1284
			$this->SetBwscale($q['bandwidthtype']);
1285
		}
1286
		if (!empty($q['qlimit'])) {
1287
			$this->SetQlimit($q['qlimit']);
1288
		} else {
1289
			$this->SetQlimit(""); // Default
1290
		}
1291
		if (!empty($q['priority'])) {
1292
			$this->SetQPriority($q['priority']);
1293
		} else {
1294
			$this->SetQpriority("");
1295
		}
1296
		if (!empty($q['description'])) {
1297
			$this->SetDescription($q['description']);
1298
		} else {
1299
			$this->SetDescription("");
1300
		}
1301
		if (!empty($q['red'])) {
1302
			$this->SetRed($q['red']);
1303
		} else {
1304
			$this->SetRed();
1305
		}
1306
		if (!empty($q['codel'])) {
1307
			$this->SetCodel($q['codel']);
1308
		} else {
1309
			$this->SetCodel();
1310
		}
1311
		if (!empty($q['rio'])) {
1312
			$this->SetRio($q['rio']);
1313
		} else {
1314
			$this->SetRio();
1315
		}
1316
		if (!empty($q['ecn'])) {
1317
			$this->SetEcn($q['ecn']);
1318
		} else {
1319
			$this->SetEcn();
1320
		}
1321
		if (!empty($q['default'])) {
1322
			$this->SetDefault($q['default']);
1323
		} else {
1324
			$this->SetDefault();
1325
		}
1326
		if (!empty($q['enabled'])) {
1327
			$this->SetEnabled($q['enabled']);
1328
		} else {
1329
			$this->SetEnabled("");
1330
		}
1331
	}
1332

    
1333
	function build_tree() {
1334
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1335
		$tree .= "\" ";
1336
		$tmpvalue = $this->GetDefault();
1337
		if (!empty($tmpvalue)) {
1338
			$tree .= " class=\"navlnk\"";
1339
		}
1340
		$tree .= " >" . $this->GetQname() . "</a>";
1341
		/*
1342
		 * Not needed here!
1343
		 * if (is_array($queues) {
1344
		 *	  $tree .= "<ul>";
1345
		 *	  foreach ($q as $queues)
1346
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1347
		 *	  endforeach
1348
		 *	  $tree .= "</ul>";
1349
		 * }
1350
		 */
1351

    
1352
		$tree .= "</li>";
1353

    
1354
		return $tree;
1355
	}
1356

    
1357
	/* Should return something like:
1358
	 * queue $qname on $qinterface bandwidth ....
1359
	 */
1360
	function build_rules(&$default = false) {
1361
		$pfq_rule = " queue ". $this->qname;
1362
		if ($this->GetInterface()) {
1363
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1364
		}
1365
		$tmpvalue = $this->GetQpriority();
1366
		if (!empty($tmpvalue)) {
1367
			$pfq_rule .= " priority ".$this->GetQpriority();
1368
		}
1369
		$tmpvalue = $this->GetQlimit();
1370
		if (!empty($tmpvalue)) {
1371
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1372
		}
1373
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1374
			$pfq_rule .= " priq ( ";
1375
			$tmpvalue = $this->GetRed();
1376
			if (!empty($tmpvalue)) {
1377
				$comma = 1;
1378
				$pfq_rule .= " red ";
1379
			}
1380
			$tmpvalue = $this->GetRio();
1381
			if (!empty($tmpvalue)) {
1382
				if ($comma) {
1383
					$pfq_rule .= " ,";
1384
				}
1385
				$comma = 1;
1386
				$pfq_rule .= " rio ";
1387
			}
1388
			$tmpvalue = $this->GetEcn();
1389
			if (!empty($tmpvalue)) {
1390
				if ($comma) {
1391
					$pfq_rule .= " ,";
1392
				}
1393
				$comma = 1;
1394
				$pfq_rule .= " ecn ";
1395
			}
1396
			$tmpvalue = $this->GetCodel();
1397
			if (!empty($tmpvalue)) {
1398
				if ($comma) {
1399
					$pfq_rule .= " ,";
1400
				}
1401
				$comma = 1;
1402
				$pfq_rule .= " codel ";
1403
			}
1404
			$tmpvalue = $this->GetDefault();
1405
			if (!empty($tmpvalue)) {
1406
				if ($comma) {
1407
					$pfq_rule .= " ,";
1408
				}
1409
				$pfq_rule .= " default ";
1410
				$default = true;
1411
			}
1412
			$pfq_rule .= " ) ";
1413
		}
1414

    
1415
		$pfq_rule .= " \n";
1416

    
1417
		return $pfq_rule;
1418
	}
1419

    
1420
	/*
1421
	 * To return the html form to show to user
1422
	 * for getting the parameters.
1423
	 * Should do even for first time when the
1424
	 * object is created and later when we may
1425
	 * need to update it. (2)
1426
	 */
1427

    
1428
	function build_form() {
1429

    
1430
		$sform = new Form();
1431

    
1432
		$sform->setAction("firewall_shaper.php");
1433

    
1434
		$section = new Form_Section("");
1435

    
1436
		$section->addInput(new Form_Checkbox(
1437
			'enabled',
1438
			'Enable/Disable',
1439
			'Enable/disable discipline and its children',
1440
			($this->GetEnabled() == "on"),
1441
			'on'
1442
		));
1443

    
1444
		$section->addInput(new Form_Input(
1445
			'newname',
1446
			'Name',
1447
			'text',
1448
			$this->GetQname()
1449
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1450

    
1451
		$section->addInput(new Form_Input(
1452
			'name',
1453
			null,
1454
			'hidden',
1455
			$this->GetQname()
1456
		));
1457

    
1458
		$section->addInput(new Form_Input(
1459
			'priority',
1460
			'Priority',
1461
			'number',
1462
			$this->GetQpriority(),
1463
			['min' => '0', 'max'=> '7']
1464
		))->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.');
1465

    
1466
		$section->addInput(new Form_Input(
1467
			'qlimit',
1468
			'Queue Limit',
1469
			'number',
1470
			$this->GetQlimit()
1471
		))->setHelp('Queue limit in packets.');
1472

    
1473
		$group = new Form_Group('Scheduler options');
1474

    
1475
		if (empty($this->subqueues)) {
1476
			$group->add(new Form_Checkbox(
1477
				'default',
1478
				null,
1479
				null,
1480
				$this->GetDefault(),
1481
				'default'
1482
			))->setHelp('Default Queue');
1483
		}
1484

    
1485
		$group->add(new Form_Checkbox(
1486
			'red',
1487
			null,
1488
			null,
1489
			!empty($this->GetRed())
1490
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#red">' . gettext('Random Early Detection') . '</a>');
1491

    
1492
		$group->add(new Form_Checkbox(
1493
			'rio',
1494
			null,
1495
			null,
1496
			!empty($this->GetRio())
1497
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#rio">' . gettext('Random Early Detection In and Out') . '</a>');
1498

    
1499
		$group->add(new Form_Checkbox(
1500
			'ecn',
1501
			null,
1502
			null,
1503
			!empty($this->GetEcn())
1504
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Explicit Congestion Notification') . '</a>');
1505

    
1506
		$group->add(new Form_Checkbox(
1507
			'codel',
1508
			null,
1509
			null,
1510
			!empty($this->GetCodel())
1511
		))->setHelp('<a target="_new" href="http://www.openbsd.org/faq/pf/queueing.html#ecn">' . gettext('Codel Active Queue') . '</a>');
1512

    
1513
		$group->setHelp('Select options for this queue');
1514

    
1515
		$section->add($group);
1516

    
1517
		$section->addInput(new Form_Input(
1518
			'description',
1519
			'Description',
1520
			'text',
1521
			$this->GetDescription()
1522
		));
1523

    
1524
		$sform->add($section);
1525

    
1526
		$sform->addGlobal(new Form_Input(
1527
			'interface',
1528
			null,
1529
			'hidden',
1530
			$this->GetInterface()
1531
		));
1532

    
1533
		$sform->addGlobal(new Form_Input(
1534
			'name',
1535
			null,
1536
			'hidden',
1537
			$this->GetQname()
1538
		));
1539

    
1540
		return($sform);
1541
	}
1542

    
1543
	function build_shortform() {
1544
		/* XXX: Hacks in sight. Mostly layer violations!  */
1545
		global $g, $altq_list_queues;
1546
		global $shaperIFlist;
1547

    
1548
		$altq =& $altq_list_queues[$this->GetInterface()];
1549

    
1550
		if ($altq) {
1551
			$scheduler = $altq->GetScheduler();
1552
		}
1553

    
1554
		$form = '<dl class="dl-horizontal">';
1555
		$form .= '	<dt>';
1556
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1557
		$form .= '	</dt>';
1558
		$form .= '	<dd>';
1559
		$form .=		$scheduler;
1560
		$form .= '	</dd>';
1561

    
1562
		$form .= '	<dt>';
1563
		$form .=		'Bandwidth';
1564
		$form .= '	</dt>';
1565
		$form .= '	<dd>';
1566
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1567
		$form .= '	</dd>';
1568

    
1569
		$tmpvalue = $this->GetQpriority();
1570
		if (!empty($tmpvalue)) {
1571
			$form .= '	<dt>';
1572
			$form .=		'Priority';
1573
			$form .= '	<dt>';
1574
			$form .= '	<dd>';
1575
			$form .=		'On';
1576
			$form .= '	</dd>';
1577
		}
1578

    
1579
		$tmpvalue = $this->GetDefault();
1580
		if (!empty($tmpvalue)) {
1581
			$form .= '	<dt>';
1582
			$form .=		'Default';
1583
			$form .= '	<dt>';
1584
			$form .= '	<dd>';
1585
			$form .=		'On';
1586
			$form .= '	</dd>';
1587
		}
1588

    
1589
			$form .= '	<dt>';
1590
			$form .= 'Delete';
1591
			$form .= '	<dt>';
1592
			$form .= '	<dd>';
1593

    
1594
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1595
			$form .= $this->GetInterface() . '&amp;queue=';
1596
			$form .= $this->GetQname() . '&amp;action=delete">';
1597
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1598
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1599

    
1600
			$form .= '	</dd>';
1601

    
1602
			$form .= '</dl>';
1603

    
1604
		return $form;
1605

    
1606
	}
1607

    
1608
	function update_altq_queue_data(&$q) {
1609
		$this->ReadConfig($q);
1610
	}
1611

    
1612
	function wconfig() {
1613
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1614
		if (!is_array($cflink)) {
1615
			$cflink = array();
1616
		}
1617
		$cflink['name'] = $this->GetQname();
1618
		$cflink['interface'] = $this->GetInterface();
1619
		$cflink['qlimit'] = trim($this->GetQlimit());
1620
		if (empty($cflink['qlimit'])) {
1621
			unset($cflink['qlimit']);
1622
		}
1623
		$cflink['priority'] = trim($this->GetQpriority());
1624
		if (empty($cflink['priority'])) {
1625
			unset($cflink['priority']);
1626
		}
1627
		$cflink['description'] = trim($this->GetDescription());
1628
		if (empty($cflink['description'])) {
1629
			unset($cflink['description']);
1630
		}
1631
		$cflink['enabled'] = trim($this->GetEnabled());
1632
		if (empty($cflink['enabled'])) {
1633
			unset($cflink['enabled']);
1634
		}
1635
		$cflink['default'] = trim($this->GetDefault());
1636
		if (empty($cflink['default'])) {
1637
			unset($cflink['default']);
1638
		}
1639
		$cflink['red'] = trim($this->GetRed());
1640
		if (empty($cflink['red'])) {
1641
			unset($cflink['red']);
1642
		}
1643
		$cflink['codel'] = trim($this->GetCodel());
1644
		if (empty($cflink['codel'])) {
1645
			unset($cflink['codel']);
1646
		}
1647
		$cflink['rio'] = trim($this->GetRio());
1648
		if (empty($cflink['rio'])) {
1649
			unset($cflink['rio']);
1650
		}
1651
		$cflink['ecn'] = trim($this->GetEcn());
1652
		if (empty($cflink['ecn'])) {
1653
			unset($cflink['ecn']);
1654
		}
1655
	}
1656
}
1657

    
1658
class hfsc_queue extends priq_queue {
1659
	/* realtime */
1660
	var $realtime;
1661
	var $r_m1;
1662
	var $r_d;
1663
	var $r_m2;
1664
	/* linkshare */
1665
	var $linkshare;
1666
	var $l_m1;
1667
	var $l_d;
1668
	var $l_m2;
1669
	/* upperlimit */
1670
	var $upperlimit;
1671
	var $u_m1;
1672
	var $u_d;
1673
	var $u_m2;
1674

    
1675
	/*
1676
	 * HFSC can have nested queues.
1677
	 */
1678
	function CanHaveChildren() {
1679
		return true;
1680
	}
1681
	function GetRealtime() {
1682
		return $this->realtime;
1683
	}
1684
	function GetR_m1() {
1685
		return $this->r_m1;
1686
	}
1687
	function GetR_d() {
1688
		return $this->r_d;
1689
	}
1690
	function GetR_m2() {
1691
		return $this->r_m2;
1692
	}
1693
	function SetRealtime() {
1694
		$this->realtime = "on";
1695
	}
1696
	function DisableRealtime() {
1697
		$this->realtime = "";
1698
	}
1699
	function SetR_m1($value) {
1700
		$this->r_m1 = $value;
1701
	}
1702
	function SetR_d($value) {
1703
		$this->r_d = $value;
1704
	}
1705
	function SetR_m2($value) {
1706
		$this->r_m2 = $value;
1707
	}
1708
	function GetLinkshare() {
1709
		return $this->linkshare;
1710
	}
1711
	function DisableLinkshare() {
1712
		$this->linkshare = "";
1713
	}
1714
	function GetL_m1() {
1715
		return $this->l_m1;
1716
	}
1717
	function GetL_d() {
1718
		return $this->l_d;
1719
	}
1720
	function GetL_m2() {
1721
		return $this->l_m2;
1722
	}
1723
	function SetLinkshare() {
1724
		$this->linkshare = "on";
1725
	}
1726
	function SetL_m1($value) {
1727
		$this->l_m1 = $value;
1728
	}
1729
	function SetL_d($value) {
1730
		$this->l_d = $value;
1731
	}
1732
	function SetL_m2($value) {
1733
		$this->l_m2 = $value;
1734
	}
1735
	function GetUpperlimit() {
1736
		return $this->upperlimit;
1737
	}
1738
	function GetU_m1() {
1739
		return $this->u_m1;
1740
	}
1741
	function GetU_d() {
1742
		return $this->u_d;
1743
	}
1744
	function GetU_m2() {
1745
		return $this->u_m2;
1746
	}
1747
	function SetUpperlimit() {
1748
		$this->upperlimit = "on";
1749
	}
1750
	function DisableUpperlimit() {
1751
		$this->upperlimit = "";
1752
	}
1753
	function SetU_m1($value) {
1754
		$this->u_m1 = $value;
1755
	}
1756
	function SetU_d($value) {
1757
		$this->u_d = $value;
1758
	}
1759
	function SetU_m2($value) {
1760
		$this->u_m2 = $value;
1761
	}
1762

    
1763
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1764

    
1765
		if (!is_array($this->subqueues)) {
1766
			$this->subqueues = array();
1767
		}
1768
		$q =& new hfsc_queue();
1769
		$q->SetInterface($this->GetInterface());
1770
		$q->SetParent($this);
1771
		$q->ReadConfig($qname);
1772
		$q->validate_input($qname, $input_errors);
1773

    
1774
		$q->SetEnabled("on");
1775
		$q->SetLink($path);
1776

    
1777
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1778
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1779
		if (is_array($qname['queue'])) {
1780
			foreach ($qname['queue'] as $key1 => $que) {
1781
				array_push($path, $key1);
1782
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1783
				array_pop($path);
1784
			}
1785
		}
1786

    
1787
		return $q;
1788
	}
1789

    
1790
	function copy_queue($interface, &$cflink) {
1791

    
1792
		$cflink['name'] = $this->GetQname();
1793
		$cflink['interface'] = $interface;
1794
		$cflink['qlimit'] = trim($this->GetQlimit());
1795
		if (empty($cflink['qlimit'])) {
1796
			unset($cflink['qlimit']);
1797
		}
1798
		$cflink['priority'] = trim($this->GetQpriority());
1799
		if (empty($cflink['priority'])) {
1800
			unset($cflink['priority']);
1801
		}
1802
		$cflink['description'] = trim($this->GetDescription());
1803
		if (empty($cflink['description'])) {
1804
			unset($cflink['description']);
1805
		}
1806
		$cflink['bandwidth'] = $this->GetBandwidth();
1807
		$cflink['bandwidthtype'] = $this->GetBwscale();
1808
		$cflink['enabled'] = trim($this->GetEnabled());
1809
		if (empty($cflink['enabled'])) {
1810
			unset($cflink['enabled']);
1811
		}
1812
		$cflink['default'] = trim($this->GetDefault());
1813
		if (empty($cflink['default'])) {
1814
			unset($cflink['default']);
1815
		}
1816
		$cflink['red'] = trim($this->GetRed());
1817
		if (empty($cflink['red'])) {
1818
			unset($cflink['red']);
1819
		}
1820
		$cflink['rio'] = trim($this->GetRio());
1821
		if (empty($cflink['rio'])) {
1822
			unset($cflink['rio']);
1823
		}
1824
		$cflink['ecn'] = trim($this->GetEcn());
1825
		if (empty($cflink['ecn'])) {
1826
			unset($cflink['ecn']);
1827
		}
1828
		if ($this->GetLinkshare() <> "") {
1829
			if ($this->GetL_m1() <> "") {
1830
				$cflink['linkshare1'] = $this->GetL_m1();
1831
				$cflink['linkshare2'] = $this->GetL_d();
1832
				$cflink['linkshare'] = "on";
1833
			} else {
1834
				unset($cflink['linkshare1']);
1835
				unset($cflink['linkshare2']);
1836
				unset($cflink['linkshare']);
1837
			}
1838
			if ($this->GetL_m2() <> "") {
1839
				$cflink['linkshare3'] = $this->GetL_m2();
1840
				$cflink['linkshare'] = "on";
1841
			} else {
1842
				unset($cflink['linkshare3']);
1843
				unset($cflink['linkshare']);
1844
			}
1845
		}
1846
		if ($this->GetRealtime() <> "") {
1847
			if ($this->GetR_m1() <> "") {
1848
				$cflink['realtime1'] = $this->GetR_m1();
1849
				$cflink['realtime2'] = $this->GetR_d();
1850
				$cflink['realtime'] = "on";
1851
			} else {
1852
				unset($cflink['realtime1']);
1853
				unset($cflink['realtime2']);
1854
				unset($cflink['realtime']);
1855
			}
1856
			if ($this->GetR_m2() <> "") {
1857
				$cflink['realtime3'] = $this->GetR_m2();
1858
				$cflink['realtime'] = "on";
1859
			} else {
1860
				unset($cflink['realtime3']);
1861
				unset($cflink['realtime']);
1862
			}
1863
		}
1864
		if ($this->GetUpperlimit() <> "") {
1865
			if ($this->GetU_m1() <> "") {
1866
				$cflink['upperlimit1'] = $this->GetU_m1();
1867
				$cflink['upperlimit2'] = $this->GetU_d();
1868
				$cflink['upperlimit'] = "on";
1869
			} else {
1870
				unset($cflink['upperlimit']);
1871
				unset($cflink['upperlimit1']);
1872
				unset($cflink['upperlimit2']);
1873
			}
1874
			if ($this->GetU_m2() <> "") {
1875
				$cflink['upperlimit3'] = $this->GetU_m2();
1876
				$cflink['upperlimit'] = "on";
1877
			} else {
1878
				unset($cflink['upperlimit3']);
1879
				unset($cflink['upperlimit']);
1880
			}
1881
		}
1882

    
1883
		if (is_array($this->subqueues)) {
1884
			$cflinkp['queue'] = array();
1885
			foreach ($this->subqueues as $q) {
1886
				$cflink['queue'][$q->GetQname()] = array();
1887
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1888
			}
1889
		}
1890
	}
1891

    
1892
	function delete_queue() {
1893
		unref_on_altq_queue_list($this->GetQname());
1894
		cleanup_queue_from_rules($this->GetQname());
1895
		$parent =& $this->GetParent();
1896
		foreach ($this->subqueues as $q)
1897
			$q->delete_queue();
1898
		unset_object_by_reference($this->GetLink());
1899
	}
1900

    
1901
	/*
1902
	 * Should search even its children
1903
	 */
1904
	function &find_queue($interface, $qname) {
1905
		if ($qname == $this->GetQname()) {
1906
			return $this;
1907
		}
1908

    
1909
		foreach ($this->subqueues as $q) {
1910
			$result =& $q->find_queue("", $qname);
1911
			if ($result) {
1912
				return $result;
1913
			}
1914
		}
1915
	}
1916

    
1917
	function &find_parentqueue($interface, $qname) {
1918
		if ($this->subqueues[$qname]) {
1919
			return $this;
1920
		}
1921
		foreach ($this->subqueues as $q) {
1922
			$result = $q->find_parentqueue("", $qname);
1923
			if ($result) {
1924
				return $result;
1925
			}
1926
		}
1927
	}
1928

    
1929
	function validate_input($data, &$input_errors) {
1930
		parent::validate_input($data, $input_errors);
1931

    
1932
		$reqdfields[] = "bandwidth";
1933
		$reqdfieldsn[] = gettext("Bandwidth");
1934
		$reqdfields[] = "bandwidthtype";
1935
		$reqdfieldsn[] = gettext("Bandwidthtype");
1936

    
1937
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1938

    
1939
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
1940
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1941
				$input_errors[] = gettext("Bandwidth must be an integer.");
1942
			}
1943

    
1944
			if ($data['bandwidth'] < 0) {
1945
				$input_errors[] = gettext("Bandwidth cannot be negative.");
1946
			}
1947

    
1948
			if ($data['bandwidthtype'] == "%") {
1949
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1950
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1951
				}
1952
			}
1953
		}
1954

    
1955
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
1956
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
1957
		}
1958
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
1959
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
1960
		}
1961
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
1962
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
1963
		}
1964
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
1965
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
1966
		}
1967
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
1968
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
1969
		}
1970

    
1971
		/*
1972
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
1973
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
1974
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
1975
			if (floatval($bw_1) < floatval($bw_2)) {
1976
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
1977
			}
1978

    
1979
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1980
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
1981
			}
1982
		}
1983
		*/
1984
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
1985
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
1986
		}
1987
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
1988
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
1989
		}
1990
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
1991
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
1992
		}
1993
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
1994
			$input_errors[] = gettext("linkshare d value needs to be numeric");
1995
		}
1996
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
1997
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
1998
		}
1999
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2000
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2001
		}
2002
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2003
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2004
		}
2005

    
2006
		/*
2007
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2008
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2009
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2010
			if (floatval($bw_1) < floatval($bw_2)) {
2011
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2012
			}
2013

    
2014
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2015
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2016
			}
2017
		}
2018
		*/
2019

    
2020
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2021
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2022
		}
2023
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2024
			$input_errors[] = gettext("realtime d value needs to be numeric");
2025
		}
2026
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2027
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2028
		}
2029

    
2030
		/*
2031
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2032
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2033
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2034
			if (floatval($bw_1) < floatval($bw_2)) {
2035
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2036
			}
2037

    
2038
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2039
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2040
			}
2041
		}
2042
		*/
2043
	}
2044

    
2045
	function ReadConfig(&$cflink) {
2046
		if (!empty($cflink['linkshare'])) {
2047
			if (!empty($cflink['linkshare1'])) {
2048
				$this->SetL_m1($cflink['linkshare1']);
2049
				$this->SetL_d($cflink['linkshare2']);
2050
				$this->SetLinkshare();
2051
			} else {
2052
				$this->SetL_m1("");
2053
				$this->SetL_d("");
2054
				$this->DisableLinkshare();
2055
			}
2056
			if (!empty($cflink['linkshare3'])) {
2057
				$this->SetL_m2($cflink['linkshare3']);
2058
				$this->SetLinkshare();
2059
			}
2060
		} else {
2061
			$this->DisableLinkshare();
2062
		}
2063
		if (!empty($cflink['realtime'])) {
2064
			if (!empty($cflink['realtime1'])) {
2065
				$this->SetR_m1($cflink['realtime1']);
2066
				$this->SetR_d($cflink['realtime2']);
2067
				$this->SetRealtime();
2068
			} else {
2069
				$this->SetR_m1("");
2070
				$this->SetR_d("");
2071
				$this->DisableRealtime();
2072
			}
2073
			if (!empty($cflink['realtime3'])) {
2074
				$this->SetR_m2($cflink['realtime3']);
2075
				$this->SetRealtime();
2076
			}
2077
		} else {
2078
			$this->DisableRealtime();
2079
		}
2080
		if (!empty($cflink['upperlimit'])) {
2081
			if (!empty($cflink['upperlimit1'])) {
2082
				$this->SetU_m1($cflink['upperlimit1']);
2083
				$this->SetU_d($cflink['upperlimit2']);
2084
				$this->SetUpperlimit();
2085
			} else {
2086
				$this->SetU_m1("");
2087
				$this->SetU_d("");
2088
				$this->DisableUpperlimit();
2089
			}
2090
			if (!empty($cflink['upperlimit3'])) {
2091
				$this->SetU_m2($cflink['upperlimit3']);
2092
				$this->SetUpperlimit();
2093
			}
2094
		} else {
2095
			$this->DisableUpperlimit();
2096
		}
2097
		parent::ReadConfig($cflink);
2098
	}
2099

    
2100
	function build_tree() {
2101
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2102
		$tree .= "\" ";
2103
		$tmpvalue = $this->GetDefault();
2104
		if (!empty($tmpvalue)) {
2105
			$tree .= " class=\"navlnk\"";
2106
		}
2107
		$tree .= " >" . $this->GetQname() . "</a>";
2108
		if (is_array($this->subqueues)) {
2109
			$tree .= "<ul>";
2110
			foreach ($this->subqueues as $q) {
2111
				$tree .= $q->build_tree();
2112
			}
2113
			$tree .= "</ul>";
2114
		}
2115
		$tree .= "</li>";
2116
		return $tree;
2117
	}
2118

    
2119
	/* Even this should take children into consideration */
2120
	function build_rules(&$default = false) {
2121

    
2122
		$pfq_rule = " queue ". $this->qname;
2123
		if ($this->GetInterface()) {
2124
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2125
		}
2126
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2127
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2128
		}
2129

    
2130
		$tmpvalue = $this->GetQlimit();
2131
		if (!empty($tmpvalue)) {
2132
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2133
		}
2134
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2135
			$pfq_rule .= " hfsc ( ";
2136
			$tmpvalue = $this->GetRed();
2137
			if (!empty($tmpvalue)) {
2138
				$comma = 1;
2139
				$pfq_rule .= " red ";
2140
			}
2141

    
2142
			$tmpvalue = $this->GetRio();
2143
			if (!empty($tmpvalue)) {
2144
				if ($comma) {
2145
					$pfq_rule .= " ,";
2146
				}
2147
				$comma = 1;
2148
				$pfq_rule .= " rio ";
2149
			}
2150
			$tmpvalue = $this->GetEcn();
2151
			if (!empty($tmpvalue)) {
2152
				if ($comma) {
2153
					$pfq_rule .= " ,";
2154
				}
2155
				$comma = 1;
2156
				$pfq_rule .= " ecn ";
2157
			}
2158
			$tmpvalue = $this->GetCodel();
2159
			if (!empty($tmpvalue)) {
2160
				if ($comma) {
2161
					$pfq_rule .= " ,";
2162
				}
2163
				$comma = 1;
2164
				$pfq_rule .= " codel ";
2165
			}
2166
			$tmpvalue = $this->GetDefault();
2167
			if (!empty($tmpvalue)) {
2168
				if ($comma) {
2169
					$pfq_rule .= " ,";
2170
				}
2171
				$comma = 1;
2172
				$pfq_rule .= " default ";
2173
				$default = true;
2174
			}
2175

    
2176
			if ($this->GetRealtime() <> "") {
2177
				if ($comma) {
2178
					$pfq_rule .= " , ";
2179
				}
2180
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2181
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2182
				} else if ($this->GetR_m2() <> "") {
2183
					$pfq_rule .= " realtime " . $this->GetR_m2();
2184
				}
2185
				$comma = 1;
2186
			}
2187
			if ($this->GetLinkshare() <> "") {
2188
				if ($comma) {
2189
					$pfq_rule .= " ,";
2190
				}
2191
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2192
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2193
				} else if ($this->GetL_m2() <> "") {
2194
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2195
				}
2196
				$comma = 1;
2197
			}
2198
			if ($this->GetUpperlimit() <> "") {
2199
				if ($comma) {
2200
					$pfq_rule .= " ,";
2201
				}
2202
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2203
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2204
				} else if ($this->GetU_m2() <> "") {
2205
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2206
				}
2207
			}
2208
			$pfq_rule .= " ) ";
2209
		}
2210
		if (count($this->subqueues)) {
2211
			$i = count($this->subqueues);
2212
			$pfq_rule .= " { ";
2213
			foreach ($this->subqueues as $qkey => $qnone) {
2214
				if ($i > 1) {
2215
					$i--;
2216
					$pfq_rule .= " {$qkey}, ";
2217
				} else {
2218
					$pfq_rule .= " {$qkey} ";
2219
				}
2220
			}
2221
			$pfq_rule .= " } \n";
2222
			foreach ($this->subqueues as $q) {
2223
				$pfq_rule .= $q->build_rules($default);
2224
			}
2225
		}
2226

    
2227
		$pfq_rule .= " \n";
2228

    
2229
		return $pfq_rule;
2230
	}
2231

    
2232
	function build_javascript() {
2233

    
2234
		$javascript = <<<EOJS
2235
<script type="text/javascript">
2236
//<![CDATA[
2237
	events.push(function(){
2238

    
2239
		// Disables the specified input element
2240
		function disableInput(id, disable) {
2241
			$('#' + id).prop("disabled", disable);
2242
		}
2243

    
2244
		// Upperlimit
2245
		function enable_upperlimit() {
2246
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2247
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2248
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2249
		}
2250

    
2251
		$('#upperlimit').click(function () {
2252
			enable_upperlimit();
2253
		});
2254

    
2255
		enable_upperlimit();
2256

    
2257
		// realtime
2258
		function enable_realtime() {
2259
			disableInput('realtime1', !$('#realtime').prop('checked'));
2260
			disableInput('realtime2', !$('#realtime').prop('checked'));
2261
			disableInput('realtime3', !$('#realtime').prop('checked'));
2262
		}
2263

    
2264
		$('#realtime').click(function () {
2265
			enable_realtime();
2266
		});
2267

    
2268
		enable_realtime();
2269

    
2270
		// linkshare
2271
		function enable_linkshare() {
2272
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2273
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2274
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2275
		}
2276

    
2277
		$('#linkshare').click(function () {
2278
			enable_linkshare();
2279
		});
2280

    
2281
		enable_linkshare();
2282
	});
2283
//]]>
2284
</script>
2285
EOJS;
2286

    
2287
		return $javascript;
2288
	}
2289

    
2290
	function build_form() {
2291

    
2292
		$sform = parent::build_form();
2293

    
2294
		$section = new Form_Section('Service Curve (sc)');
2295

    
2296
		$group = new Form_Group('Bandwidth');
2297

    
2298
		$group->add(new Form_Input(
2299
			'bandwidth',
2300
			null,
2301
			'number',
2302
			$this->GetBandwidth()
2303
		));
2304

    
2305
		$group->add(new Form_Select(
2306
			'bandwidthtype',
2307
			null,
2308
			$this->GetBwscale(),
2309
			array('Kb' => 'Kbit/s',
2310
				  'Mb' => 'Mbit/s',
2311
				  'Gb' => 'Gbit/s',
2312
				  'b' => 'Bit/s',
2313
				  '%' => '%')
2314
		));
2315

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

    
2318
		$section->add($group);
2319

    
2320
		$group = new Form_Group('Max bandwidth for queue.');
2321

    
2322
		$group->add(new Form_Checkbox(
2323
			'upperlimit',
2324
			null,
2325
			'Upper Limit',
2326
			($this->GetUpperlimit()<> "")
2327
		));
2328

    
2329
		$group->add(new Form_Input(
2330
			'upperlimit1',
2331
			null,
2332
			'text',
2333
			$this->GetU_m1()
2334
		))->setHelp('m1');
2335

    
2336
		$group->add(new Form_Input(
2337
			'upperlimit2',
2338
			null,
2339
			'text',
2340
			$this->GetU_d()
2341
		))->setHelp('d');
2342

    
2343
		$group->add(new Form_Input(
2344
			'upperlimit3',
2345
			null,
2346
			'text',
2347
			$this->GetU_m2()
2348
		))->setHelp('m2');
2349

    
2350

    
2351
		$section->add($group);
2352

    
2353
		$group = new Form_Group('Min bandwidth for queue.');
2354

    
2355
		$group->add(new Form_Checkbox(
2356
			'realtime',
2357
			null,
2358
			'Real Time',
2359
			($this->GetRealtime()<> "")
2360
		));
2361

    
2362
		$group->add(new Form_Input(
2363
			'realtime1',
2364
			null,
2365
			'text',
2366
			$this->GetR_m1()
2367
		))->setHelp('m1');
2368

    
2369
		$group->add(new Form_Input(
2370
			'realtime2',
2371
			null,
2372
			'text',
2373
			$this->GetR_d()
2374
		))->setHelp('d');
2375

    
2376
		$group->add(new Form_Input(
2377
			'realtime3',
2378
			null,
2379
			'text',
2380
			$this->GetR_m2()
2381
		))->setHelp('m2');
2382

    
2383
		$section->add($group);
2384

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

    
2387
		$group->add(new Form_Checkbox(
2388
			'linkshare',
2389
			null,
2390
			'Link Share',
2391
			($this->GetLinkshare()<> "")
2392
		));
2393

    
2394
		$group->add(new Form_Input(
2395
			'linkshare1',
2396
			null,
2397
			'text',
2398
			$this->GetL_m1()
2399
		))->setHelp('m1');
2400

    
2401
		$group->add(new Form_Input(
2402
			'linkshare2',
2403
			null,
2404
			'text',
2405
			$this->GetL_d()
2406
		))->setHelp('d');
2407

    
2408
		$group->add(new Form_Input(
2409
			'linkshare3',
2410
			null,
2411
			'text',
2412
			$this->GetL_m2()
2413
		))->setHelp('m2');
2414

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

    
2420
		$section->add($group);
2421

    
2422
		$sform->add($section);
2423

    
2424
		return($sform);
2425
	}
2426

    
2427
	function update_altq_queue_data(&$data) {
2428
		$this->ReadConfig($data);
2429
	}
2430

    
2431
	function wconfig() {
2432
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2433
		if (!is_array($cflink)) {
2434
			$cflink = array();
2435
		}
2436
		$cflink['name'] = $this->GetQname();
2437
		$cflink['interface'] = $this->GetInterface();
2438
		$cflink['qlimit'] = trim($this->GetQlimit());
2439
		if (empty($cflink['qlimit'])) {
2440
			unset($cflink['qlimit']);
2441
		}
2442
		$cflink['priority'] = $this->GetQpriority();
2443
		if (empty($cflink['priority'])) {
2444
			unset($cflink['priority']);
2445
		}
2446
		$cflink['description'] = $this->GetDescription();
2447
		if (empty($cflink['description'])) {
2448
			unset($cflink['description']);
2449
		}
2450
		$cflink['bandwidth'] = $this->GetBandwidth();
2451
		$cflink['bandwidthtype'] = $this->GetBwscale();
2452
		$cflink['enabled'] = $this->GetEnabled();
2453
		if (empty($cflink['enabled'])) {
2454
			unset($cflink['enabled']);
2455
		}
2456
		$cflink['default'] = $this->GetDefault();
2457
		if (empty($cflink['default'])) {
2458
			unset($cflink['default']);
2459
		}
2460
		$cflink['red'] = trim($this->GetRed());
2461
		if (empty($cflink['red'])) {
2462
			unset($cflink['red']);
2463
		}
2464
		$cflink['rio'] = $this->GetRio();
2465
		if (empty($cflink['rio'])) {
2466
			unset($cflink['rio']);
2467
		}
2468
		$cflink['ecn'] = trim($this->GetEcn());
2469
		if (empty($cflink['ecn'])) {
2470
			unset($cflink['ecn']);
2471
		}
2472
		$cflink['codel'] = trim($this->GetCodel());
2473
		if (empty($cflink['codel'])) {
2474
			unset($cflink['codel']);
2475
		}
2476
		if ($this->GetLinkshare() <> "") {
2477
			if ($this->GetL_m1() <> "") {
2478
				$cflink['linkshare1'] = $this->GetL_m1();
2479
				$cflink['linkshare2'] = $this->GetL_d();
2480
				$cflink['linkshare'] = "on";
2481
			} else {
2482
				unset($cflink['linkshare']);
2483
				unset($cflink['linkshare1']);
2484
				unset($cflink['linkshare2']);
2485
			}
2486
			if ($this->GetL_m2() <> "") {
2487
				$cflink['linkshare3'] = $this->GetL_m2();
2488
				$cflink['linkshare'] = "on";
2489
			} else {
2490
				unset($cflink['linkshare']);
2491
				unset($cflink['linkshare3']);
2492
			}
2493
		} else {
2494
			unset($cflink['linkshare']);
2495
			unset($cflink['linkshare1']);
2496
			unset($cflink['linkshare2']);
2497
			unset($cflink['linkshare3']);
2498
		}
2499
		if ($this->GetRealtime() <> "") {
2500
			if ($this->GetR_m1() <> "") {
2501
				$cflink['realtime1'] = $this->GetR_m1();
2502
				$cflink['realtime2'] = $this->GetR_d();
2503
				$cflink['realtime'] = "on";
2504
			} else {
2505
				unset($cflink['realtime']);
2506
				unset($cflink['realtime1']);
2507
				unset($cflink['realtime2']);
2508
			}
2509
			if ($this->GetR_m2() <> "") {
2510
				$cflink['realtime3'] = $this->GetR_m2();
2511
				$cflink['realtime'] = "on";
2512
			} else {
2513
				unset($cflink['realtime']);
2514
				unset($cflink['realtime3']);
2515
			}
2516
		} else {
2517
			unset($cflink['realtime']);
2518
			unset($cflink['realtime1']);
2519
			unset($cflink['realtime2']);
2520
			unset($cflink['realtime3']);
2521
		}
2522
		if ($this->GetUpperlimit() <> "") {
2523
			if ($this->GetU_m1() <> "") {
2524
				$cflink['upperlimit1'] = $this->GetU_m1();
2525
				$cflink['upperlimit2'] = $this->GetU_d();
2526
				$cflink['upperlimit'] = "on";
2527
			} else {
2528
				unset($cflink['upperlimit']);
2529
				unset($cflink['upperlimit1']);
2530
				unset($cflink['upperlimit2']);
2531
			}
2532
			if ($this->GetU_m2() <> "") {
2533
				$cflink['upperlimit3'] = $this->GetU_m2();
2534
				$cflink['upperlimit'] = "on";
2535
			} else {
2536
				unset($cflink['upperlimit']);
2537
				unset($cflink['upperlimit3']);
2538
			}
2539
		} else {
2540
			unset($cflink['upperlimit']);
2541
			unset($cflink['upperlimit1']);
2542
			unset($cflink['upperlimit2']);
2543
			unset($cflink['upperlimit3']);
2544
		}
2545
	}
2546
}
2547

    
2548
class cbq_queue extends priq_queue {
2549
	var $qborrow = "";
2550

    
2551
	function GetBorrow() {
2552
		return $this->qborrow;
2553
	}
2554
	function SetBorrow($borrow) {
2555
		$this->qborrow = $borrow;
2556
	}
2557
	function CanHaveChildren() {
2558
		return true;
2559
	}
2560

    
2561
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2562

    
2563
		if (!is_array($this->subqueues)) {
2564
			$this->subqueues = array();
2565
		}
2566
		$q =& new cbq_queue();
2567
		$q->SetInterface($this->GetInterface());
2568
		$q->SetParent($this);
2569
		$q->ReadConfig($qname);
2570
		$q->validate_input($qname, $input_errors);
2571

    
2572
		$q->SetEnabled("on");
2573
		$q->SetLink($path);
2574
		$this->subqueues[$q->GetQName()] = &$q;
2575
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2576
		if (is_array($qname['queue'])) {
2577
			foreach ($qname['queue'] as $key1 => $que) {
2578
				array_push($path, $key1);
2579
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2580
				array_pop($path);
2581
			}
2582
		}
2583

    
2584
		return $q;
2585
	}
2586

    
2587
	function copy_queue($interface, &$cflink) {
2588

    
2589
		$cflink['interface'] = $interface;
2590
		$cflink['qlimit'] = trim($this->GetQlimit());
2591
		if (empty($clink['qlimit'])) {
2592
			unset($cflink['qlimit']);
2593
		}
2594
		$cflink['priority'] = trim($this->GetQpriority());
2595
		if (empty($cflink['priority'])) {
2596
			unset($cflink['priority']);
2597
		}
2598
		$cflink['name'] = $this->GetQname();
2599
		$cflink['description'] = trim($this->GetDescription());
2600
		if (empty($cflink['description'])) {
2601
			unset($cflink['description']);
2602
		}
2603
		$cflink['bandwidth'] = $this->GetBandwidth();
2604
		$cflink['bandwidthtype'] = $this->GetBwscale();
2605
		$cflink['enabled'] = trim($this->GetEnabled());
2606
		if (empty($cflink['enabled'])) {
2607
			unset($cflink['enabled']);
2608
		}
2609
		$cflink['default'] = trim($this->GetDefault());
2610
		if (empty($cflink['default'])) {
2611
			unset($cflink['default']);
2612
		}
2613
		$cflink['red'] = trim($this->GetRed());
2614
		if (empty($cflink['red'])) {
2615
			unset($cflink['red']);
2616
		}
2617
		$cflink['rio'] = trim($this->GetRio());
2618
		if (empty($cflink['rio'])) {
2619
			unset($cflink['rio']);
2620
		}
2621
		$cflink['ecn'] = trim($this->GetEcn());
2622
		if (empty($cflink['ecn'])) {
2623
			unset($cflink['ecn']);
2624
		}
2625
		$cflink['borrow'] = trim($this->GetBorrow());
2626
		if (empty($cflink['borrow'])) {
2627
			unset($cflink['borrow']);
2628
		}
2629
		if (is_array($this->queues)) {
2630
			$cflinkp['queue'] = array();
2631
			foreach ($this->subqueues as $q) {
2632
				$cflink['queue'][$q->GetQname()] = array();
2633
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2634
			}
2635
		}
2636
	}
2637

    
2638
	/*
2639
	 * Should search even its children
2640
	 */
2641
	function &find_queue($interface, $qname) {
2642
		if ($qname == $this->GetQname()) {
2643
			return $this;
2644
		}
2645
		foreach ($this->subqueues as $q) {
2646
			$result =& $q->find_queue("", $qname);
2647
			if ($result) {
2648
				return $result;
2649
			}
2650
		}
2651
	}
2652

    
2653
	function &find_parentqueue($interface, $qname) {
2654
		if ($this->subqueues[$qname]) {
2655
			return $this;
2656
		}
2657
		foreach ($this->subqueues as $q) {
2658
			$result = $q->find_parentqueue("", $qname);
2659
			if ($result) {
2660
				return $result;
2661
			}
2662
		}
2663
	}
2664

    
2665
	function delete_queue() {
2666
		unref_on_altq_queue_list($this->GetQname());
2667
		cleanup_queue_from_rules($this->GetQname());
2668
		foreach ($this->subqueues as $q)
2669
			$q->delete_queue();
2670
		unset_object_by_reference($this->GetLink());
2671
	}
2672

    
2673
	function validate_input($data, &$input_errors) {
2674
		parent::validate_input($data, $input_errors);
2675

    
2676
		if ($data['priority'] > 7) {
2677
				$input_errors[] = gettext("Priority must be an integer between 1 and 7.");
2678
		}
2679
		$reqdfields[] = "bandwidth";
2680
		$reqdfieldsn[] = gettext("Bandwidth");
2681
		$reqdfields[] = "bandwidthtype";
2682
		$reqdfieldsn[] = gettext("Bandwidthtype");
2683

    
2684
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2685
	}
2686

    
2687
	function ReadConfig(&$q) {
2688
		parent::ReadConfig($q);
2689
		if (!empty($q['borrow'])) {
2690
			$this->SetBorrow("on");
2691
		} else {
2692
			$this->SetBorrow("");
2693
		}
2694
	}
2695

    
2696
	function build_javascript() {
2697
		return parent::build_javascript();
2698
	}
2699

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

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

    
2799
		$pfq_rule .= " \n";
2800
		return $pfq_rule;
2801
	}
2802

    
2803
	function build_form() {
2804
		$sform = parent::build_form();
2805

    
2806
		$section = new Form_Section('NOTITLE');
2807

    
2808
		$group = new Form_Group('Bandwidth');
2809

    
2810
		$group->add(new Form_Input(
2811
			'bandwidth',
2812
			null,
2813
			'number',
2814
			$this->GetBandwidth()
2815
		));
2816

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

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

    
2830
		$section->add($group);
2831

    
2832
		$section->addInput(new Form_Checkbox(
2833
			'borrow',
2834
			'Scheduler option',
2835
			'Borrow from other queues when available',
2836
			($this->GetBorrow() == "on")
2837
		));
2838

    
2839
		$sform->add($section);
2840

    
2841
		return $sform;
2842
	}
2843

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

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

    
2900
class fairq_queue extends priq_queue {
2901
	var $hogs;
2902
	var $buckets;
2903

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

    
2920

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

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

    
2947
	function find_parentqueue($interface, $qname) { return; }
2948

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

    
2955
	function validate_input($data, &$input_errors) {
2956
		parent::validate_input($data, $input_errors);
2957

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

    
2966
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2967
	}
2968

    
2969
	function ReadConfig(&$q) {
2970
		parent::ReadConfig($q);
2971
		if (!empty($q['buckets'])) {
2972
			$this->SetBuckets($q['buckets']);
2973
		} else {
2974
			$this->SetBuckets("");
2975
		}
2976
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
2977
			$this->SetHogs($q['hogs']);
2978
		} else {
2979
			$this->SetHogs("");
2980
		}
2981
	}
2982

    
2983
	function build_javascript() {
2984
		return parent::build_javascript();
2985
	}
2986

    
2987
	function build_tree() {
2988
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
2989
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2990
		$tree .= "\" ";
2991
		$tmpvalue = trim($this->GetDefault());
2992
		if (!empty($tmpvalue)) {
2993
			$tree .= " class=\"navlnk\"";
2994
		}
2995
		$tree .= " >" . $this->GetQname() . "</a>";
2996
		$tree .= "</li>";
2997
		return $tree;
2998
	}
2999

    
3000
	/* Even this should take children into consideration */
3001
	function build_rules(&$default = false) {
3002
		$pfq_rule = "queue ". $this->qname;
3003
		if ($this->GetInterface()) {
3004
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3005
		}
3006
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3007
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3008
		}
3009
		$tmpvalue = trim($this->GetQpriority());
3010
		if (!empty($tmpvalue)) {
3011
			$pfq_rule .= " priority " . $this->GetQpriority();
3012
		}
3013
		$tmpvalue = trim($this->GetQlimit());
3014
		if (!empty($tmpvalue)) {
3015
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3016
		}
3017
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3018
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3019
			$pfq_rule .= " fairq ( ";
3020
			$tmpvalue = trim($this->GetRed());
3021
			if (!empty($tmpvalue)) {
3022
				$comma = 1;
3023
				$pfq_rule .= " red ";
3024
			}
3025
			$tmpvalue = trim($this->GetCodel());
3026
			if (!empty($tmpvalue)) {
3027
				$comma = 1;
3028
				$pfq_rule .= " codel ";
3029
			}
3030
			$tmpvalue = trim($this->GetRio());
3031
			if (!empty($tmpvalue)) {
3032
				if ($comma) {
3033
					$pfq_rule .= " ,";
3034
				}
3035
				$comma = 1;
3036
				$pfq_rule .= " rio ";
3037
			}
3038
			$tmpvalue = trim($this->GetEcn());
3039
			if (!empty($tmpvalue)) {
3040
				if ($comma) {
3041
					$pfq_rule .= " ,";
3042
				}
3043
				$comma = 1;
3044
				$pfq_rule .= " ecn ";
3045
			}
3046
			$tmpvalue = trim($this->GetDefault());
3047
			if (!empty($tmpvalue)) {
3048
				if ($comma) {
3049
					$pfq_rule .= " ,";
3050
				}
3051
				$comma = 1;
3052
				$pfq_rule .= " default ";
3053
				$default = true;
3054
			}
3055
			$tmpvalue = trim($this->GetBuckets());
3056
			if (!empty($tmpvalue)) {
3057
				if ($comma) {
3058
					$pfq_rule .= ", ";
3059
				}
3060
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3061
			}
3062
			$tmpvalue = trim($this->GetHogs());
3063
			if (!empty($tmpvalue)) {
3064
				if ($comma) {
3065
					$pfq_rule .= ", ";
3066
				}
3067
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3068
			}
3069
			$pfq_rule .= " ) ";
3070
		}
3071

    
3072
		$pfq_rule .= " \n";
3073
		return $pfq_rule;
3074
	}
3075

    
3076
	function build_form() {
3077
		$form = parent::build_form();
3078

    
3079
		$section = new Form_Section('');
3080

    
3081
		$group = new Form_Group('Bandwidth');
3082

    
3083
		$group->add(new Form_Input(
3084
			'bandwidth',
3085
			null,
3086
			'number',
3087
			$this->GetBandwidth()
3088
		));
3089

    
3090
		$group->add(new Form_Select(
3091
			'bandwidthtype',
3092
			null,
3093
			$this->GetBwscale(),
3094
			array('Kb' => 'Kbit/s',
3095
				  'Mb' => 'Mbit/s',
3096
				  'Gb' => 'Gbit/s',
3097
				  'b' => 'Bit/s',
3098
				  '%' => '%')
3099
		));
3100

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

    
3103
		$section->add($group);
3104

    
3105
		$section->addInput(new Form_Input(
3106
			'buckets',
3107
			'Scheduler specific options',
3108
			'text',
3109
			$this->GetBuckets()
3110
		))->setHelp('Number of buckets available');
3111

    
3112
		$section->addInput(new Form_Input(
3113
			'hogs',
3114
			'',
3115
			'text',
3116
			$this->GetHogs()
3117
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3118

    
3119
		$form->add($section);
3120
		return $form;
3121
	}
3122

    
3123
	function update_altq_queue_data(&$data) {
3124
		$this->ReadConfig($data);
3125
	}
3126

    
3127
	function wconfig() {
3128
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3129
		if (!is_array($cflink)) {
3130
			$cflink = array();
3131
		}
3132
		$cflink['interface'] = $this->GetInterface();
3133
		$cflink['qlimit'] = trim($this->GetQlimit());
3134
		if (empty($cflink['qlimit'])) {
3135
			unset($cflink['qlimit']);
3136
		}
3137
		$cflink['priority'] = trim($this->GetQpriority());
3138
		if (empty($cflink['priority'])) {
3139
			unset($cflink['priority']);
3140
		}
3141
		$cflink['name'] = $this->GetQname();
3142
		$cflink['description'] = trim($this->GetDescription());
3143
		if (empty($cflink['description'])) {
3144
			unset($cflink['description']);
3145
		}
3146
		$cflink['bandwidth'] = $this->GetBandwidth();
3147
		$cflink['bandwidthtype'] = $this->GetBwscale();
3148
		$cflink['enabled'] = $this->GetEnabled();
3149
		if (empty($cflink['enabled'])) {
3150
			unset($cflink['enabled']);
3151
		}
3152
		$cflink['default'] = trim($this->GetDefault());
3153
		if (empty($cflink['default'])) {
3154
			unset($cflink['default']);
3155
		}
3156
		$cflink['red'] = trim($this->GetRed());
3157
		if (empty($cflink['red'])) {
3158
			unset($cflink['red']);
3159
		}
3160
		$cflink['rio'] = trim($this->GetRio());
3161
		if (empty($cflink['rio'])) {
3162
			unset($cflink['rio']);
3163
		}
3164
		$cflink['ecn'] = trim($this->GetEcn());
3165
		if (empty($cflink['ecn'])) {
3166
			unset($cflink['ecn']);
3167
		}
3168
		$cflink['codel'] = trim($this->GetCodel());
3169
		if (empty($cflink['codel'])) {
3170
			unset($cflink['codel']);
3171
		}
3172
		$cflink['buckets'] = trim($this->GetBuckets());
3173
		if (empty($cflink['buckets'])) {
3174
			unset($cflink['buckets']);
3175
		}
3176
		$cflink['hogs'] = trim($this->GetHogs());
3177
		if (empty($cflink['hogs'])) {
3178
			unset($cflink['hogs']);
3179
		}
3180
	}
3181
}
3182

    
3183

    
3184
/*
3185
 * dummynet(4) wrappers.
3186
 */
3187

    
3188

    
3189
/*
3190
 * List of respective objects!
3191
 */
3192
$dummynet_pipe_list = array();
3193

    
3194
class dummynet_class {
3195
	var $qname;
3196
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3197
	var $qlimit;
3198
	var $description;
3199
	var $qenabled;
3200
	var $link;
3201
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3202
	var $plr;
3203

    
3204
	var $buckets;
3205
	/* mask parameters */
3206
	var $mask;
3207
	var $noerror;
3208

    
3209
	/* Accessor functions */
3210
	function SetLink($link) {
3211
		$this->link = $link;
3212
	}
3213
	function GetLink() {
3214
		return $this->link;
3215
	}
3216
	function GetMask() {
3217
		if (!isset($this->mask["type"])) {
3218
			$this->mask["type"] = "none";
3219
		}
3220
		return $this->mask;
3221
	}
3222
	function SetMask($mask) {
3223
		$this->mask = $mask;
3224
	}
3225
	function &GetParent() {
3226
		return $this->qparent;
3227
	}
3228
	function SetParent(&$parent) {
3229
		$this->qparent = &$parent;
3230
	}
3231
	function GetEnabled() {
3232
		return $this->qenabled;
3233
	}
3234
	function SetEnabled($value) {
3235
		$this->qenabled = $value;
3236
	}
3237
	function CanHaveChildren() {
3238
		return false;
3239
	}
3240
	function CanBeDeleted() {
3241
		return true;
3242
	}
3243
	function GetQname() {
3244
		return $this->qname;
3245
	}
3246
	function SetQname($name) {
3247
		$this->qname = trim($name);
3248
	}
3249
	function GetQlimit() {
3250
		return $this->qlimit;
3251
	}
3252
	function SetQlimit($limit) {
3253
		$this->qlimit = $limit;
3254
	}
3255
	function GetDescription() {
3256
		return $this->description;
3257
	}
3258
	function SetDescription($str) {
3259
		$this->description = trim($str);
3260
	}
3261
	function GetFirstime() {
3262
		return $this->firsttime;
3263
	}
3264
	function SetFirsttime($number) {
3265
		$this->firsttime = $number;
3266
	}
3267
	function GetBuckets() {
3268
		return $this->buckets;
3269
	}
3270
	function SetBuckets($buckets) {
3271
		$this->buckets = $buckets;
3272
	}
3273
	function SetNumber($number) {
3274
		$this->qnumber = $number;
3275
	}
3276
	function GetNumber() {
3277
		return $this->qnumber;
3278
	}
3279
	function GetPlr() {
3280
		return $this->plr;
3281
	}
3282
	function SetPlr($plr) {
3283
		$this->plr = $plr;
3284
	}
3285

    
3286
	function build_javascript() {
3287
		$javascript .= "<script type=\"text/javascript\">\n";
3288
		$javascript .= "//<![CDATA[\n";
3289
		$javascript .= "function enable_maskbits(enable_over) {\n";
3290
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3291
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3292
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3293
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3294
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3295
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3296
		$javascript .= "} else {\n";
3297
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3298
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3299
		$javascript .= "}}\n";
3300
		$javascript .= "//]]>\n";
3301
		$javascript .= "</script>\n";
3302
		return $javascript;
3303
	}
3304

    
3305
	function validate_input($data, &$input_errors) {
3306
		$reqdfields[] = "bandwidth";
3307
		$reqdfieldsn[] = gettext("Bandwidth");
3308
		/*$reqdfields[] = "burst";
3309
		$reqdfieldsn[] = gettext("Burst"); */
3310
		$reqdfields[] = "bandwidthtype";
3311
		$reqdfieldsn[] = gettext("Bandwidthtype");
3312
		$reqdfields[] = "newname";
3313
		$reqdfieldsn[] = gettext("Name");
3314

    
3315
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3316

    
3317
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3318
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3319
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3320
		}
3321
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3322
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3323
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3324
		}
3325
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3326
			$input_errors[] = gettext("Queue limit must be an integer");
3327
		}
3328
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3329
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3330
		}
3331
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3332
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3333
		}
3334
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3335
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3336
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3337
			}
3338
		}
3339
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3340
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3341
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3342
			}
3343
		}
3344
	}
3345

    
3346
	function build_mask_rules(&$pfq_rule) {
3347
		$mask = $this->GetMask();
3348
		if (!empty($mask['type'])) {
3349
			if ($mask['type'] <> 'none') {
3350
				$pfq_rule .= " mask";
3351
			}
3352
			switch ($mask['type']) {
3353
				case 'srcaddress':
3354
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3355
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3356
					} else {
3357
						$pfq_rule .= " src-ip6 /128";
3358
					}
3359
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3360
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3361
					} else {
3362
						$pfq_rule .= " src-ip 0xffffffff";
3363
					}
3364
					break;
3365
				case 'dstaddress':
3366
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3367
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3368
					} else {
3369
						$pfq_rule .= " dst-ip6 /128";
3370
					}
3371
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3372
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3373
					} else {
3374
						$pfq_rule .= " dst-ip 0xffffffff";
3375
					}
3376
					break;
3377
				default:
3378
					break;
3379
			}
3380
		}
3381
	}
3382

    
3383
}
3384

    
3385
class dnpipe_class extends dummynet_class {
3386
	var $delay;
3387
	var $qbandwidth = array();
3388
	var $qbandwidthtype;
3389

    
3390
		/* This is here to help on form building and building rules/lists */
3391
	var $subqueues = array();
3392

    
3393
	function CanHaveChildren() {
3394
		return true;
3395
	}
3396
	function SetDelay($delay) {
3397
		$this->delay = $delay;
3398
	}
3399
	function GetDelay() {
3400
		return $this->delay;
3401
	}
3402
	function delete_queue() {
3403
		cleanup_dnqueue_from_rules($this->GetQname());
3404
		foreach ($this->subqueues as $q) {
3405
			$q->delete_queue();
3406
		}
3407
		unset_dn_object_by_reference($this->GetLink());
3408
		@pfSense_pipe_action("pipe delete " . $this->GetNumber());
3409
	}
3410
	function GetBandwidth() {
3411
		return $this->qbandwidth;
3412
	}
3413
	function SetBandwidth($bandwidth) {
3414
		$this->qbandwidth = $bandwidth;
3415
	}
3416
	function GetBurst() {
3417
		return $this->qburst;
3418
	}
3419
	function SetBurst($burst) {
3420
		$this->qburst = $burst;
3421
	}
3422

    
3423
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3424

    
3425
		if (!is_array($this->subqueues)) {
3426
			$this->subqueues = array();
3427
		}
3428

    
3429
		$q =& new dnqueue_class();
3430
		$q->SetLink($path);
3431
		$q->SetEnabled("on");
3432
		$q->SetPipe($this->GetQname());
3433
		$q->SetParent($this);
3434
		$q->ReadConfig($queue);
3435
		$q->validate_input($queue, $input_errors);
3436
		if (count($input_errors)) {
3437
			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)));
3438
			return $q;
3439
		}
3440
		$number = dnqueue_find_nextnumber();
3441
		$q->SetNumber($number);
3442
		$this->subqueues[$q->GetQname()] = &$q;
3443

    
3444
		return $q;
3445
	}
3446

    
3447
	function &get_queue_list(&$q = null) {
3448
		$qlist = array();
3449

    
3450
		$qlist[$this->GetQname()] = $this->GetNumber();
3451
		if (is_array($this->subqueues)) {
3452
			foreach ($this->subqueues as $queue) {
3453
				$queue->get_queue_list($qlist);
3454
			}
3455
		}
3456
		return $qlist;
3457
	}
3458

    
3459
	/*
3460
	 * Should search even its children
3461
	 */
3462
	function &find_queue($pipe, $qname) {
3463
		if ($qname == $this->GetQname()) {
3464
			return $this;
3465
		}
3466
		foreach ($this->subqueues as $q) {
3467
			$result =& $q->find_queue("", $qname);
3468
			if ($result) {
3469
				return $result;
3470
			}
3471
		}
3472
	}
3473

    
3474
	function &find_parentqueue($pipe, $qname) {
3475
		return NULL;
3476
	}
3477

    
3478
	function validate_input($data, &$input_errors) {
3479
		parent::validate_input($data, $input_errors);
3480

    
3481
		$schedule = 0;
3482
		$schedulenone = 0;
3483
		$entries = 0;
3484
		/* XXX: Really no better way? */
3485
		for ($i = 0; $i < 2900; $i++) {
3486
			if (!empty($data["bwsched{$i}"])) {
3487
				if ($data["bwsched{$i}"] != "none") {
3488
					$schedule++;
3489
				} else {
3490
					$schedulenone++;
3491
				}
3492
			}
3493
			if (!empty($data["bandwidth{$i}"])) {
3494
				if (!is_numeric($data["bandwidth{$i}"])) {
3495
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3496
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3497
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3498
				} else {
3499
					$entries++;
3500
				}
3501
			}
3502
		}
3503
		if ($schedule == 0 && $entries > 1) {
3504
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3505
		}
3506
		if ($schedulenone > 0 && $entries > 1) {
3507
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3508
		}
3509
		if ($entries == 0) {
3510
			$input_errors[] = gettext("At least one bw specification is necessary.");
3511
		}
3512
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3513
			$input_errors[] = gettext("Delay must be an integer.");
3514
		}
3515
	}
3516

    
3517
	function ReadConfig(&$q) {
3518
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3519
			$this->SetQname($q['newname']);
3520
		} else if (!empty($q['newname'])) {
3521
			$this->SetQname($q['newname']);
3522
		} else {
3523
			$this->SetQname($q['name']);
3524
		}
3525
		$this->SetNumber($q['number']);
3526

    
3527
		if (!empty($_POST)) {
3528
			$bandwidth = array();
3529
			/* XXX: Really no better way? */
3530
			for ($i = 0; $i < 2900; $i++) {
3531
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3532
					$bw = array();
3533
					$bw['bw'] = $q["bandwidth{$i}"];
3534
					$bw['burst'] = $q["burst{$i}"];
3535
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3536
						$bw['bwscale'] = $q["bwtype{$i}"];
3537
					}
3538
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3539
						$bw['bwsched'] = $q["bwsched{$i}"];
3540
					}
3541
					$bandwidth[] = $bw;
3542
				}
3543
			}
3544
			$this->SetBandwidth($bandwidth);
3545
		}
3546

    
3547
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3548
			$this->SetBandwidth($q['bandwidth']['item']);
3549
			$this->SetBurst($q['burst']['item']);
3550
		}
3551

    
3552
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3553
			$this->SetQlimit($q['qlimit']);
3554
		} else {
3555
			$this->SetQlimit("");
3556
		}
3557
		if (isset($q['mask']) && $q['mask'] <> "") {
3558
			$masktype = $q['mask'];
3559
		} else {
3560
			$masktype = "";
3561
		}
3562
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3563
			$maskbits = $q['maskbits'];
3564
		} else {
3565
			$maskbits = "";
3566
		}
3567
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3568
			$maskbitsv6 = $q['maskbitsv6'];
3569
		} else {
3570
			$maskbitsv6 = "";
3571
		}
3572
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3573
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3574
			$this->SetBuckets($q['buckets']);
3575
		} else {
3576
			$this->SetBuckets("");
3577
		}
3578
		if (isset($q['plr']) && $q['plr'] <> "") {
3579
			$this->SetPlr($q['plr']);
3580
		} else {
3581
			$this->SetPlr("");
3582
		}
3583
		if (isset($q['delay']) && $q['delay'] <> "") {
3584
			$this->SetDelay($q['delay']);
3585
		} else {
3586
			$this->SetDelay(0);
3587
		}
3588
		if (isset($q['description']) && $q['description'] <> "") {
3589
			$this->SetDescription($q['description']);
3590
		} else {
3591
			$this->SetDescription("");
3592
		}
3593
		$this->SetEnabled($q['enabled']);
3594

    
3595
	}
3596

    
3597
	function build_tree() {
3598
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3599
		$tree .= $this->GetQname() . "</a>";
3600
		if (is_array($this->subqueues)) {
3601
			$tree .= "<ul>";
3602
			foreach ($this->subqueues as $q) {
3603
				$tree .= $q->build_tree();
3604
			}
3605
			$tree .= "</ul>";
3606
		}
3607
		$tree .= "</li>";
3608

    
3609
		return $tree;
3610
	}
3611

    
3612
	function build_rules() {
3613
		global $config, $time_based_rules;
3614

    
3615
		if ($this->GetEnabled() == "") {
3616
			return;
3617
		}
3618

    
3619
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3620
		$found = false;
3621
		$bandwidth = $this->GetBandwidth();
3622
		if (is_array($bandwidth)) {
3623
			foreach ($bandwidth as $bw) {
3624
				if ($bw['bwsched'] != "none") {
3625
					$time_based_rules = true;
3626
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3627
						foreach ($config['schedules']['schedule'] as $schedule) {
3628
							if ($bw['bwsched'] == $schedule['name']) {
3629
								if (filter_get_time_based_rule_status($schedule)) {
3630
									$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3631
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3632
										$pfq_rule .= " burst ".trim($bw['burst']);
3633
									}
3634
									$found = true;
3635
									break;
3636
								}
3637
							}
3638
						}
3639
					} else {
3640
						$pfq_rule .= " bw 0";
3641
						$found = true;
3642
						break;
3643
					}
3644
				} else {
3645
					$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3646
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3647
						$pfq_rule .= " burst ".trim($bw['burst']);
3648
					}
3649
					$found = true;
3650
					break;
3651
				}
3652
			}
3653
			if ($found == false) {
3654
				$pfq_rule .= " bw 0";
3655
			}
3656
		} else {
3657
			$pfq_rule .= " bw 0";
3658
		}
3659

    
3660
		if ($this->GetQlimit()) {
3661
			$pfq_rule .= " queue " . $this->GetQlimit();
3662
		}
3663
		if ($this->GetPlr()) {
3664
			$pfq_rule .= " plr " . $this->GetPlr();
3665
		}
3666
		if ($this->GetBuckets()) {
3667
			$pfq_rule .= " buckets " . $this->GetBuckets();
3668
		}
3669
		if ($this->GetDelay()) {
3670
			$pfq_rule .= " delay " . $this->GetDelay();
3671
		}
3672
		$this->build_mask_rules($pfq_rule);
3673

    
3674
		$pfq_rule .= "\n";
3675

    
3676
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3677
			foreach ($this->subqueues as $q) {
3678
				$pfq_rule .= $q->build_rules();
3679
			}
3680
		}
3681
		$pfq_rule .= " \n";
3682

    
3683
		return $pfq_rule;
3684
	}
3685

    
3686
	function update_dn_data(&$data) {
3687
		$this->ReadConfig($data);
3688
	}
3689

    
3690
	function build_javascript() {
3691
		global $g, $config;
3692

    
3693
		$javasr = parent::build_javascript();
3694

    
3695
		//build list of schedules
3696
		$schedules = "<option value='none'>none</option>";
3697
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3698
			foreach ($config['schedules']['schedule'] as $schedule) {
3699
				if ($schedule['name'] <> "") {
3700
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3701
				}
3702
			}
3703
		}
3704
		$bwopt = "";
3705
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3706
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3707
		}
3708

    
3709
		$javasr .= <<<EOD
3710
<script type='text/javascript'>
3711
//<![CDATA[
3712
var addBwRowTo = (function() {
3713

    
3714
	return (function (tableId) {
3715

    
3716
	var table = document.getElementById(tableId);
3717
	var totalrows = table.rows.length -1;
3718

    
3719
	var row = table.insertRow(totalrows + 1);
3720
	var cell1 = row.insertCell(0);
3721
	var cell2 = row.insertCell(1);
3722
	var cell3 = row.insertCell(2);
3723
	var cell4 = row.insertCell(3);
3724

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

    
3730
	});
3731
})();
3732

    
3733
function removeBwRow(el) {
3734
	var d = el.parentNode.parentNode.rowIndex;
3735
	document.getElementById('maintable').deleteRow(d);
3736
}
3737
//]]>
3738
</script>
3739

    
3740
EOD;
3741

    
3742
		return $javasr;
3743
	}
3744

    
3745
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3746
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3747
	// with the javascript in this class
3748
	function build_bwtable() {
3749
		global $config;
3750

    
3751
		$bandwidth = $this->GetBandwidth();
3752
				//build list of schedules
3753
		$schedules = array();
3754
		$schedules[] = "none";//leave none to leave rule enabled all the time
3755
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3756
			foreach ($config['schedules']['schedule'] as $schedule) {
3757
				if ($schedule['name'] != "") {
3758
					$schedules[] = $schedule['name'];
3759
				}
3760
			}
3761
		}
3762

    
3763
		$form = '<div class="table-responsive">';
3764
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3765
		$form .= "<thead><tr>";
3766
		$form .= "<th>Bandwidth</th>";
3767
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3768
		$form .= "<th>Bw type</th>";
3769
		$form .= "<th>Schedule</th>";
3770
		$form .= "<th></th>";
3771
		$form .= "</tr></thead>";
3772
		$form .= "<tbody>";
3773

    
3774
		// If there are no bandwidths defined, make a blank one for convenience
3775
		if (empty($bandwidth)) {
3776
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3777
		}
3778

    
3779
		if (is_array($bandwidth)) {
3780
			foreach ($bandwidth as $bwidx => $bw) {
3781
				$form .= '<tr>';
3782
				$form .= '<td class="col-xs-4">';
3783
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3784
				//$form .= "</td><td width='20%'>";
3785
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3786
				$form .= "</td>";
3787
				$form .= '<td class="col-xs-4">';
3788
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3789

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

    
3793
					if ($bw['bwscale'] == $bwsidx) {
3794
						$form .= " selected";
3795
					}
3796

    
3797
					$form .= ">{$bwscale}</option>";
3798
				}
3799

    
3800
				$form .= "</select>";
3801
				$form .= "</td>";
3802
				$form .= '<td class="col-xs-4">';
3803
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3804

    
3805
				foreach ($schedules as $schd) {
3806
					$selected = "";
3807
					if ($bw['bwsched'] == $schd) {
3808
						$selected = "selected";
3809
					}
3810

    
3811
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3812
				}
3813

    
3814
				$form .= "</select>";
3815
				$form .= "</td>";
3816
				$form .= '<td>';
3817
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
3818
				$form .= "</td></tr>";
3819
			}
3820
		}
3821
		$form .= "</tbody></table></div><br />";
3822

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

    
3827
		return($form);
3828
	}
3829

    
3830
	function build_form() {
3831
		global $g, $config, $pipe, $action, $qname;
3832

    
3833
		//build list of schedules
3834
		$schedules = array();
3835
		$schedules[] = "none";//leave none to leave rule enabled all the time
3836
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3837
			foreach ($config['schedules']['schedule'] as $schedule) {
3838
				if ($schedule['name'] <> "") {
3839
					$schedules[] = $schedule['name'];
3840
				}
3841
			}
3842
		}
3843

    
3844

    
3845
		$sform = new Form();
3846
		$sform->setAction("firewall_shaper.php");
3847

    
3848
		$section = new Form_Section('Limiters');
3849

    
3850
		$section->addInput(new Form_Checkbox(
3851
			'enabled',
3852
			'Enable',
3853
			'Enable limiter and its children',
3854
			($this->GetEnabled() == "on"),
3855
			'on'
3856
		));
3857

    
3858
		$section->addInput(new Form_Input(
3859
			'newname',
3860
			'Name',
3861
			'text',
3862
			$this->GetQname()
3863
		));
3864

    
3865
		$section->addInput(new Form_Input(
3866
			'name',
3867
			null,
3868
			'hidden',
3869
			$this->GetQname()
3870
		));
3871

    
3872
		if ($this->GetNumber() > 0) {
3873
			$section->addInput(new Form_Input(
3874
				'number',
3875
				null,
3876
				'hidden',
3877
				$this->GetNumber()
3878
			));
3879
		}
3880

    
3881
		$bandwidth = $this->GetBandwidth();
3882

    
3883
		if (is_array($bandwidth)) {
3884
				$section->addInput(new Form_StaticText(
3885
				'Bandwidth',
3886
				$this->build_bwtable()
3887
			));
3888
		}
3889

    
3890
		$mask = $this->GetMask();
3891

    
3892
		$section->addInput(new Form_Select(
3893
			'mask',
3894
			'Mask',
3895
			$mask['type'],
3896
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3897
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3898
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3899
					'This makes it possible to easily specify bandwidth limits per host.');
3900

    
3901
		$group = new Form_Group(null);
3902

    
3903
		$group->add(new Form_Select(
3904
			'maskbits',
3905
			null,
3906
			$mask['bits'],
3907
			array_combine(range(32, 1, -1), range(32, 1, -1))
3908
		))->setHelp('IPv4 mask bits' . '<br />' . '255.255.255.255/?');
3909

    
3910
		$group->add(new Form_Select(
3911
			'maskbitsv6',
3912
			null,
3913
			$mask['bitsv6'],
3914
			array_combine(range(128, 1, -1), range(128, 1, -1))
3915
		))->setHelp('IPv6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3916

    
3917
		$section->add($group);
3918

    
3919
		$section->addInput(new Form_Input(
3920
			'description',
3921
			'Description',
3922
			'text',
3923
			$this->GetDescription()
3924
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
3925

    
3926
		$sform->add($section);
3927

    
3928
		$section = new Form_Section('Advanced Options');
3929

    
3930
		$section->addInput(new Form_Input(
3931
			'delay',
3932
			'Delay (ms)',
3933
			'text',
3934
			$this->GetDelay() > 0 ? $this->GetDelay():null
3935
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
3936

    
3937
		$section->addInput(new Form_Input(
3938
			'plr',
3939
			'Packet Loss Rate',
3940
			'number',
3941
			$this->GetPlr(),
3942
			['step' => '0.001', 'min' => '0.000']
3943
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
3944
					'A value of 0.001 means one packet in 1000 gets dropped.');
3945

    
3946
		$section->addInput(new Form_Input(
3947
			'qlimit',
3948
			'Queue size (slots)',
3949
			'number',
3950
			$this->GetQlimit()
3951
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
3952
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
3953

    
3954
		$section->addInput(new Form_Input(
3955
			'buckets',
3956
			'Bucket size (slots)',
3957
			'number',
3958
			$this->GetBuckets()
3959
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
3960

    
3961
		$sform->add($section);
3962

    
3963
		return($sform);
3964
		}
3965

    
3966
	function wconfig() {
3967
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
3968
		if (!is_array($cflink)) {
3969
			$cflink = array();
3970
		}
3971
		$cflink['name'] = $this->GetQname();
3972
		$cflink['number'] = $this->GetNumber();
3973
		$cflink['qlimit'] = $this->GetQlimit();
3974
		$cflink['plr'] = $this->GetPlr();
3975
		$cflink['description'] = $this->GetDescription();
3976

    
3977
		$bandwidth = $this->GetBandwidth();
3978
		if (is_array($bandwidth)) {
3979
			$cflink['bandwidth'] = array();
3980
			$cflink['bandwidth']['item'] = array();
3981
			foreach ($bandwidth as $bwidx => $bw) {
3982
				$cflink['bandwidth']['item'][] = $bw;
3983
			}
3984
		}
3985

    
3986
		$cflink['enabled'] = $this->GetEnabled();
3987
		$cflink['buckets'] = $this->GetBuckets();
3988
		$mask = $this->GetMask();
3989
		$cflink['mask'] = $mask['type'];
3990
		$cflink['maskbits'] = $mask['bits'];
3991
		$cflink['maskbitsv6'] = $mask['bitsv6'];
3992
		$cflink['delay'] = $this->GetDelay();
3993
	}
3994

    
3995
}
3996

    
3997
class dnqueue_class extends dummynet_class {
3998
	var $pipeparent;
3999
	var $weight;
4000

    
4001
	function GetWeight() {
4002
		return $this->weight;
4003
	}
4004
	function SetWeight($weight) {
4005
		$this->weight = $weight;
4006
	}
4007
	function GetPipe() {
4008
		return $this->pipeparent;
4009
	}
4010
	function SetPipe($pipe) {
4011
		$this->pipeparent = $pipe;
4012
	}
4013

    
4014
	/* Just a stub in case we ever try to call this from the frontend. */
4015
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4016
		return;
4017
	}
4018

    
4019
	function delete_queue() {
4020
		cleanup_dnqueue_from_rules($this->GetQname());
4021
		unset_dn_object_by_reference($this->GetLink());
4022
		@pfSense_pipe_action("queue delete " . $this->GetNumber());
4023
	}
4024

    
4025
	function validate_input($data, &$input_errors) {
4026
		parent::validate_input($data, $input_errors);
4027

    
4028
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4029
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4030
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4031
		}
4032
	}
4033

    
4034
	/*
4035
	 * Should search even its children
4036
	 */
4037
	function &find_queue($pipe, $qname) {
4038
		if ($qname == $this->GetQname()) {
4039
			return $this;
4040
		} else {
4041
			return NULL;
4042
		}
4043
	}
4044

    
4045
	function &find_parentqueue($pipe, $qname) {
4046
		return $this->qparent;
4047
	}
4048

    
4049
	function &get_queue_list(&$qlist) {
4050
		if ($this->GetEnabled() == "") {
4051
			return;
4052
		}
4053
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4054
	}
4055

    
4056
	function ReadConfig(&$q) {
4057
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4058
			$this->SetQname($q['newname']);
4059
		} else if (!empty($q['newname'])) {
4060
			$this->SetQname($q['newname']);
4061
		} else {
4062
			$this->SetQname($q['name']);
4063
		}
4064
		$this->SetNumber($q['number']);
4065
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4066
			$this->SetQlimit($q['qlimit']);
4067
		} else {
4068
			$this->SetQlimit("");
4069
		}
4070
		if (isset($q['mask']) && $q['mask'] <> "") {
4071
			$masktype = $q['mask'];
4072
		} else {
4073
			$masktype = "";
4074
		}
4075
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4076
			$maskbits = $q['maskbits'];
4077
		} else {
4078
			$maskbits = "";
4079
		}
4080
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4081
			$maskbitsv6 = $q['maskbitsv6'];
4082
		} else {
4083
			$maskbitsv6 = "";
4084
		}
4085
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4086
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4087
			$this->SetBuckets($q['buckets']);
4088
		} else {
4089
			$this->SetBuckets("");
4090
		}
4091
		if (isset($q['plr']) && $q['plr'] <> "") {
4092
			$this->SetPlr($q['plr']);
4093
		} else {
4094
			$this->SetPlr("");
4095
		}
4096
		if (isset($q['weight']) && $q['weight'] <> "") {
4097
			$this->SetWeight($q['weight']);
4098
		} else {
4099
			$this->SetWeight("");
4100
		}
4101
		if (isset($q['description']) && $q['description'] <> "") {
4102
			$this->SetDescription($q['description']);
4103
		} else {
4104
			$this->SetDescription("");
4105
		}
4106
		$this->SetEnabled($q['enabled']);
4107
	}
4108

    
4109
	function build_tree() {
4110
		$parent =& $this->GetParent();
4111
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4112
		$tree .= $this->GetQname() . "</a>";
4113
		$tree .= "</li>";
4114

    
4115
		return $tree;
4116
	}
4117

    
4118
	function build_rules() {
4119
		if ($this->GetEnabled() == "") {
4120
			return;
4121
		}
4122

    
4123
		$parent =& $this->GetParent();
4124
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4125
		if ($this->GetQlimit()) {
4126
			$pfq_rule .= " queue " . $this->GetQlimit();
4127
		}
4128
		if ($this->GetWeight()) {
4129
			$pfq_rule .= " weight " . $this->GetWeight();
4130
		}
4131
		if ($this->GetBuckets()) {
4132
			$pfq_rule .= " buckets " . $this->GetBuckets();
4133
		}
4134
		$this->build_mask_rules($pfq_rule);
4135
		$pfq_rule .= "\n";
4136

    
4137
		return $pfq_rule;
4138
	}
4139

    
4140
	function build_javascript() {
4141
		return parent::build_javascript();
4142
	}
4143

    
4144
	function build_form() {
4145
		global $g, $config, $pipe, $action, $qname;
4146

    
4147
		//build list of schedules
4148
		$schedules = array();
4149
		$schedules[] = "none";//leave none to leave rule enabled all the time
4150
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4151
			foreach ($config['schedules']['schedule'] as $schedule) {
4152
				if ($schedule['name'] <> "") {
4153
					$schedules[] = $schedule['name'];
4154
				}
4155
			}
4156
		}
4157

    
4158

    
4159
		$sform = new Form();
4160
		$sform->setAction("firewall_shaper.php");
4161
		$section = new Form_Section('Limiters');
4162

    
4163
		$section->addInput(new Form_Checkbox(
4164
			'enabled',
4165
			'Enable',
4166
			'Enable this queue',
4167
			($this->GetEnabled() == "on"),
4168
			'on'
4169
		));
4170

    
4171
		$section->addInput(new Form_Input(
4172
			'newname',
4173
			'Name',
4174
			'text',
4175
			$this->GetQname()
4176
		));
4177

    
4178
		$section->addInput(new Form_Input(
4179
			'name',
4180
			null,
4181
			'hidden',
4182
			$this->GetQname()
4183
		));
4184

    
4185
		if ($this->GetNumber() > 0) {
4186
			$section->addInput(new Form_Input(
4187
				'number',
4188
				null,
4189
				'hidden',
4190
				$this->GetNumber()
4191
			));
4192
		}
4193

    
4194
		$mask = $this->GetMask();
4195

    
4196
		$section->addInput(new Form_Select(
4197
			'mask',
4198
			'Mask',
4199
			$mask['type'],
4200
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4201
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4202
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4203
					'This makes it possible to easily specify bandwidth limits per host.');
4204

    
4205
		$group = new Form_Group(null);
4206

    
4207
		$group->add(new Form_Select(
4208
			'maskbits',
4209
			null,
4210
			$mask['bits'],
4211
			array_combine(range(32, 1, -1), range(32, 1, -1))
4212
		))->setHelp('IPv4 mask bits' . '<br />' . '255.255.255.255/?');
4213

    
4214
		$group->add(new Form_Select(
4215
			'maskbitsv6',
4216
			null,
4217
			$mask['bitsv6'],
4218
			array_combine(range(128, 1, -1), range(128, 1, -1))
4219
		))->setHelp('IPv6 mask bits' . '<br />' . '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4220

    
4221
		$section->add($group);
4222

    
4223
		$section->addInput(new Form_Input(
4224
			'description',
4225
			'Description',
4226
			'text',
4227
			$this->GetDescription()
4228
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4229

    
4230
		$sform->add($section);
4231

    
4232
		$section = new Form_Section('Advanced Options');
4233

    
4234
		$section->addInput(new Form_Input(
4235
			'weight',
4236
			'Weight',
4237
			'number',
4238
			$this->GetWeight(),
4239
			['min' => '1', 'max' => '100']
4240
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4241
					' it can be left blank otherwise.');
4242

    
4243
		$section->addInput(new Form_Input(
4244
			'plr',
4245
			'Packet Loss Rate',
4246
			'number',
4247
			$this->GetPlr(),
4248
			['step' => '0.001', 'min' => '0.000']
4249
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4250
					'A value of 0.001 means one packet in 1000 gets dropped');
4251

    
4252
		$section->addInput(new Form_Input(
4253
			'qlimit',
4254
			'Queue size (slots)',
4255
			'number',
4256
			$this->GetQlimit()
4257
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4258
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4259

    
4260
		$section->addInput(new Form_Input(
4261
			'buckets',
4262
			'Bucket size (slots)',
4263
			'number',
4264
			$this->GetBuckets()
4265
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4266

    
4267
		$section->addInput(new Form_Input(
4268
			'pipe',
4269
			null,
4270
			'hidden',
4271
			$this->GetPipe()
4272
		));
4273

    
4274
		$sform->add($section);
4275

    
4276
		return($sform);
4277
	}
4278

    
4279
	function update_dn_data(&$data) {
4280
		$this->ReadConfig($data);
4281
	}
4282

    
4283
	function wconfig() {
4284
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4285
		if (!is_array($cflink)) {
4286
			$cflink = array();
4287
		}
4288
		$cflink['name'] = $this->GetQname();
4289
		$cflink['number'] = $this->GetNumber();
4290
		$cflink['qlimit'] = $this->GetQlimit();
4291
		$cflink['description'] = $this->GetDescription();
4292
		$cflink['weight'] = $this->GetWeight();
4293
		$cflink['enabled'] = $this->GetEnabled();
4294
		$cflink['buckets'] = $this->GetBuckets();
4295
		$mask = $this->GetMask();
4296
		$cflink['mask'] = $mask['type'];
4297
		$cflink['maskbits'] = $mask['bits'];
4298
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4299
	}
4300
}
4301

    
4302
function get_dummynet_name_list() {
4303

    
4304
	$dn_name_list =& get_unique_dnqueue_list();
4305
	$dn_name = array();
4306
	if (is_array($dn_name_list)) {
4307
		foreach ($dn_name_list as $key => $value) {
4308
			$dn_name[] = $key;
4309
		}
4310
	}
4311

    
4312
	return $dn_name;
4313

    
4314
}
4315

    
4316
function get_altq_name_list() {
4317
	$altq_name_list =& get_unique_queue_list();
4318
	$altq_name = array();
4319
	if (is_array($altq_name_list)) {
4320
		foreach ($altq_name_list as $key => $aqobj) {
4321
			$altq_name[] = $key;
4322
		}
4323
	}
4324

    
4325
	return $altq_name;
4326
}
4327

    
4328
/*
4329
 * XXX: TODO Make a class shaper to hide all these functions
4330
 * from the global namespace.
4331
 */
4332

    
4333
/*
4334
 * This is a layer violation but for now there is no way
4335
 * I can find to properly do this with PHP.
4336
 */
4337
function altq_get_default_queue($interface) {
4338
	global $altq_list_queues;
4339

    
4340
	$altq_tmp = $altq_list_queues[$interface];
4341
	if ($altq_tmp) {
4342
		return $altq_tmp->GetDefaultQueuePresent();
4343
	} else {
4344
		return false;
4345
	}
4346
}
4347

    
4348
function altq_check_default_queues() {
4349
	global $altq_list_queues;
4350

    
4351
	$count = 0;
4352
	if (is_array($altq_list_queues)) {
4353
		foreach ($altq_list_queues as $altq) {
4354
			if ($altq->GetDefaultQueuePresent()) {
4355
				$count++;
4356
			}
4357
		}
4358
	}
4359
	else {
4360
		$count++;
4361
	}
4362

    
4363
	return 0;
4364
}
4365

    
4366
function &get_unique_queue_list() {
4367
	global $altq_list_queues;
4368

    
4369
	$qlist = array();
4370
	if (is_array($altq_list_queues)) {
4371
		foreach ($altq_list_queues as $altq) {
4372
			if ($altq->GetEnabled() == "") {
4373
				continue;
4374
			}
4375
			$tmplist =& $altq->get_queue_list();
4376
			foreach ($tmplist as $qname => $link) {
4377
				if ($link->GetEnabled() <> "") {
4378
					$qlist[$qname] = $link;
4379
				}
4380
			}
4381
		}
4382
	}
4383
	return $qlist;
4384
}
4385

    
4386
function &get_unique_dnqueue_list() {
4387
	global $dummynet_pipe_list;
4388

    
4389
	$qlist = array();
4390
	if (is_array($dummynet_pipe_list)) {
4391
		foreach ($dummynet_pipe_list as $dn) {
4392
			if ($dn->GetEnabled() == "") {
4393
				continue;
4394
			}
4395
			$tmplist =& $dn->get_queue_list();
4396
			foreach ($tmplist as $qname => $link) {
4397
				$qlist[$qname] = $link;
4398
			}
4399
		}
4400
	}
4401
	return $qlist;
4402
}
4403

    
4404
function ref_on_altq_queue_list($parent, $qname) {
4405
	if (isset($GLOBALS['queue_list'][$qname])) {
4406
		$GLOBALS['queue_list'][$qname]++;
4407
	} else {
4408
		$GLOBALS['queue_list'][$qname] = 1;
4409
	}
4410

    
4411
	unref_on_altq_queue_list($parent);
4412
}
4413

    
4414
function unref_on_altq_queue_list($qname) {
4415
	$GLOBALS['queue_list'][$qname]--;
4416
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4417
		unset($GLOBALS['queue_list'][$qname]);
4418
	}
4419
}
4420

    
4421
function read_altq_config() {
4422
	global $altq_list_queues, $config;
4423
	$path = array();
4424

    
4425
	if (!is_array($config['shaper'])) {
4426
		$config['shaper'] = array();
4427
	}
4428
	if (!is_array($config['shaper']['queue'])) {
4429
		$config['shaper']['queue'] = array();
4430
	}
4431
	$a_int = &$config['shaper']['queue'];
4432

    
4433
	$altq_list_queues = array();
4434

    
4435
	if (!is_array($config['shaper']['queue'])) {
4436
		return;
4437
	}
4438

    
4439
	foreach ($a_int as $key => $conf) {
4440
		$int = $conf['interface'];
4441
		$root =& new altq_root_queue();
4442
		$root->SetInterface($int);
4443
		$altq_list_queues[$root->GetInterface()] = &$root;
4444
		$root->ReadConfig($conf);
4445
		array_push($path, $key);
4446
		$root->SetLink($path);
4447
		if (is_array($conf['queue'])) {
4448
			foreach ($conf['queue'] as $key1 => $q) {
4449
				array_push($path, $key1);
4450
				/*
4451
				 * XXX: we completely ignore errors here but anyway we must have
4452
				 *	checked them before so no harm should be come from this.
4453
				 */
4454
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4455
				array_pop($path);
4456
			}
4457
		}
4458
		array_pop($path);
4459
	}
4460
}
4461

    
4462
function read_dummynet_config() {
4463
	global $dummynet_pipe_list, $config;
4464
	$path = array();
4465

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

    
4474
	$dummynet_pipe_list = array();
4475

    
4476
	if (!is_array($config['dnshaper']['queue']) ||
4477
	    !count($config['dnshaper']['queue'])) {
4478
		return;
4479
	}
4480

    
4481
	foreach ($a_int as $key => $conf) {
4482
		if (empty($conf['name'])) {
4483
			continue; /* XXX: grrrrrr at php */
4484
		}
4485
		$root =& new dnpipe_class();
4486
		$root->ReadConfig($conf);
4487
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4488
		array_push($path, $key);
4489
		$root->SetLink($path);
4490
		if (is_array($conf['queue'])) {
4491
			foreach ($conf['queue'] as $key1 => $q) {
4492
				array_push($path, $key1);
4493
				/*
4494
				 * XXX: we completely ignore errors here but anyway we must have
4495
				 *	checked them before so no harm should be come from this.
4496
				 */
4497
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4498
				array_pop($path);
4499
			}
4500
		}
4501
		array_pop($path);
4502
	}
4503
}
4504

    
4505
function get_interface_list_to_show() {
4506
	global $altq_list_queues, $config;
4507
	global $shaperIFlist;
4508

    
4509
	$tree = "";
4510
	foreach ($shaperIFlist as $shif => $shDescr) {
4511
		if ($altq_list_queues[$shif]) {
4512
			continue;
4513
		} else {
4514
			if (!is_altq_capable(get_real_interface($shif))) {
4515
				continue;
4516
			}
4517
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4518
		}
4519
	}
4520

    
4521
	return $tree;
4522
}
4523

    
4524
function filter_generate_altq_queues() {
4525
	global $altq_list_queues;
4526

    
4527
	read_altq_config();
4528

    
4529
	$altq_rules = "";
4530
	foreach ($altq_list_queues as $altq) {
4531
		$altq_rules .= $altq->build_rules();
4532
	}
4533

    
4534
	return $altq_rules;
4535
}
4536

    
4537
function dnqueue_find_nextnumber() {
4538
	global $dummynet_pipe_list;
4539

    
4540
	$dnused = array();
4541
	if (is_array($dummynet_pipe_list)) {
4542
		foreach ($dummynet_pipe_list as $dn) {
4543
			$tmplist =& $dn->get_queue_list();
4544
			foreach ($tmplist as $qname => $link) {
4545
				if ($link[0] == "?") {
4546
					$dnused[$qname] = substr($link, 1);
4547
				}
4548
			}
4549
		}
4550
	}
4551

    
4552
	sort($dnused, SORT_NUMERIC);
4553
	$dnnumber = 0;
4554
	$found = false;
4555
	foreach ($dnused as $dnnum) {
4556
		if (($dnnum - $dnnumber) > 1) {
4557
			$dnnumber = $dnnum - 1;
4558
			$found = true;
4559
			break;
4560
		} else {
4561
			$dnnumber = $dnnum;
4562
		}
4563
	}
4564

    
4565
	if ($found == false) {
4566
		$dnnumber++;
4567
	}
4568

    
4569
	unset($dnused, $dnnum, $found);
4570
	return $dnnumber;
4571
}
4572

    
4573
function dnpipe_find_nextnumber() {
4574
	global $dummynet_pipe_list;
4575

    
4576
	$dnused = array();
4577
	foreach ($dummynet_pipe_list as $dn) {
4578
		$dnused[] = $dn->GetNumber();
4579
	}
4580

    
4581
	sort($dnused, SORT_NUMERIC);
4582
	$dnnumber = 0;
4583
	$found = false;
4584
	foreach ($dnused as $dnnum) {
4585
		if (($dnnum - $dnnumber) > 1) {
4586
			$dnnumber = $dnnum - 1;
4587
			$found = true;
4588
			break;
4589
		} else {
4590
			$dnnumber = $dnnum;
4591
		}
4592
	}
4593

    
4594
	if ($found == false) {
4595
		$dnnumber++;
4596
	}
4597

    
4598
	unset($dnused, $dnnum, $found);
4599
	return $dnnumber;
4600
}
4601

    
4602
function filter_generate_dummynet_rules() {
4603
	global $g, $dummynet_pipe_list;
4604

    
4605
	read_dummynet_config();
4606

    
4607
	$dn_rules = "";
4608
	$max_qlimit = "100"; // OS default
4609
	foreach ($dummynet_pipe_list as $dn) {
4610
		$dn_rules .= $dn->build_rules();
4611
		$this_qlimit = $dn->GetQlimit();
4612
		if ($this_qlimit > $max_qlimit) {
4613
			$max_qlimit = $this_qlimit;
4614
		}
4615
	}
4616
	if (!is_numericint($max_qlimit)) {
4617
		$max_qlimit = "100";
4618
	}
4619
	if (!empty($dn_rules)) {
4620
		if (!is_module_loaded("dummynet.ko")) {
4621
			mwexec("/sbin/kldload dummynet");
4622
		}
4623
		set_sysctl(array(
4624
				"net.inet.ip.dummynet.io_fast" => "1",
4625
				"net.inet.ip.dummynet.hash_size" => "256",
4626
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
4627
		));
4628
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4629
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4630
	}
4631
}
4632

    
4633
function build_iface_without_this_queue($iface, $qname) {
4634
	global $g, $altq_list_queues;
4635
	global $shaperIFlist;
4636

    
4637
	$altq =& $altq_list_queues[$iface];
4638

    
4639
	if ($altq) {
4640
		$scheduler = $altq->GetScheduler();
4641
	}
4642

    
4643
	$form = '<dl class="dl-horizontal">';
4644

    
4645
	$form .= '	<dt>';
4646
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4647
	$form .= '	</dt>';
4648
	$form .= '	<dd>';
4649
	$form .=		$scheduler;
4650
	$form .= '	</dd>';
4651

    
4652
	$form .= '	<dt>';
4653
	$form .= 'Clone';
4654
	$form .= '	</dt>';
4655
	$form .= '	<dd>';
4656
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4657
	$form .= $iface . '&amp;queue=';
4658
	$form .= $qname . '&amp;action=add">';
4659
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
4660
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
4661
	$form .= '	</dd>';
4662

    
4663
	$form .= '</dl>';
4664

    
4665
	return $form;
4666

    
4667
}
4668

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

    
4672
$shaper_msg = gettext("The tree on the left navigates through the %s.");
4673
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
4674
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
4675

    
4676
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
4677
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
4678
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
4679

    
4680
?>
(50-50/65)