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 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * 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="https://web.archive.org/web/20160404153707/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="https://web.archive.org/web/20160404153707/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="https://web.archive.org/web/20160404153707/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="https://web.archive.org/web/20160404153707/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
			['step' => 'any', 'min' => '0.000']
2304
		));
2305

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

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

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

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

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

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

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

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

    
2351

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2425
		return($sform);
2426
	}
2427

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

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

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

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

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

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

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

    
2585
		return $q;
2586
	}
2587

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2842
		return $sform;
2843
	}
2844

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

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

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

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

    
2921

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3184

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

    
3189

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

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

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

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

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

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

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

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

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

    
3384
}
3385

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

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

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

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

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

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

    
3445
		return $q;
3446
	}
3447

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

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

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

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

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

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

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

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

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

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

    
3596
	}
3597

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

    
3610
		return $tree;
3611
	}
3612

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

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

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

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

    
3675
		$pfq_rule .= "\n";
3676

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

    
3684
		return $pfq_rule;
3685
	}
3686

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

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

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

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

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

    
3715
	return (function (tableId) {
3716

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

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

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

    
3731
	});
3732
})();
3733

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

    
3741
EOD;
3742

    
3743
		return $javasr;
3744
	}
3745

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3828
		return($form);
3829
	}
3830

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

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

    
3845

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

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

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

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

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

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

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

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

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

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

    
3902
		$group = new Form_Group(null);
3903

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

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

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

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

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

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

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

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

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

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

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

    
3964
		return($sform);
3965
		}
3966

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

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

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

    
3996
}
3997

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

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

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

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

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

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

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

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

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

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

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

    
4116
		return $tree;
4117
	}
4118

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

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

    
4138
		return $pfq_rule;
4139
	}
4140

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

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

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

    
4159

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

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

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

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

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

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

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

    
4206
		$group = new Form_Group(null);
4207

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

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

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

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

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

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

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

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

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

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

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

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

    
4277
		return($sform);
4278
	}
4279

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

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

    
4303
function get_dummynet_name_list() {
4304

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

    
4313
	return $dn_name;
4314

    
4315
}
4316

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

    
4326
	return $altq_name;
4327
}
4328

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

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

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

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

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

    
4364
	return 0;
4365
}
4366

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

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

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

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

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

    
4412
	unref_on_altq_queue_list($parent);
4413
}
4414

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

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

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

    
4434
	$altq_list_queues = array();
4435

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

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

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

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

    
4475
	$dummynet_pipe_list = array();
4476

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

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

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

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

    
4522
	return $tree;
4523
}
4524

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

    
4528
	read_altq_config();
4529

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

    
4535
	return $altq_rules;
4536
}
4537

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

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

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

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

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

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

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

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

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

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

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

    
4606
	read_dummynet_config();
4607

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

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

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

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

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

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

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

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

    
4666
	return $form;
4667

    
4668
}
4669

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

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

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

    
4681
?>
(50-50/65)