Project

General

Profile

Download (116 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
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
/* XXX: needs some reducing on include. */
27
/* include all configuration functions. */
28
require_once("globals.inc");
29
require_once("functions.inc");
30
require_once("util.inc");
31
require_once("notices.inc");
32

    
33
/*
34
 * I admit :) this is derived from xmlparse.inc StartElement()
35
 */
36
function &get_reference_to_me_in_config(&$mypath) {
37
	global $config;
38

    
39
	$ptr =& $config['shaper'];
40
	foreach ($mypath as $indeks) {
41
		$ptr =& $ptr['queue'][$indeks];
42
	}
43

    
44
	return $ptr;
45
}
46

    
47
function unset_object_by_reference(&$mypath) {
48
	global $config;
49

    
50
	$ptr =& $config['shaper'];
51
	for ($i = 0; $i < count($mypath) - 1; $i++) {
52
		$ptr =& $ptr['queue'][$mypath[$i]];
53
	}
54
	unset($ptr['queue'][$mypath[$i]]);
55
}
56

    
57
function &get_dn_reference_to_me_in_config(&$mypath) {
58
	global $config;
59

    
60
	$ptr =& $config['dnshaper'];
61
	foreach ($mypath as $indeks) {
62
		$ptr =& $ptr['queue'][$indeks];
63
	}
64

    
65
	return $ptr;
66
}
67

    
68
function unset_dn_object_by_reference(&$mypath) {
69
	global $config;
70

    
71
	$ptr =& $config['dnshaper'];
72
	for ($i = 0; $i < count($mypath) - 1; $i++) {
73
		$ptr =& $ptr['queue'][$mypath[$i]];
74
	}
75
	unset($ptr['queue'][$mypath[$i]]);
76
}
77

    
78
function clean_child_queues($type, $mypath) {
79
	$ref = &get_reference_to_me_in_config($mypath);
80

    
81
	switch ($type) {
82
		case 'HFSC':
83
			if (isset($ref['borrow'])) {
84
				unset($ref['borrow']);
85
			}
86
			if (isset($ref['hogs'])) {
87
				unset($ref['hogs']);
88
			}
89
			if (isset($ref['buckets'])) {
90
				unset($ref['buckets']);
91
			}
92
			break;
93
		case 'PRIQ':
94
			if (isset($ref['borrow'])) {
95
				unset($ref['borrow']);
96
			}
97
			if (isset($ref['bandwidth'])) {
98
				unset($ref['bandwidth']);
99
			}
100
			if (isset($ref['bandwidthtype'])) {
101
				unset($ref['bandwidthtype']);
102
			}
103
			/* fall through */
104
		case 'FAIRQ':
105
			if (isset($ref['borrow'])) {
106
				unset($ref['borrow']);
107
			}
108
			/* fall through */
109
		case 'CBQ':
110
			if (isset($ref['realtime'])) {
111
				unset($ref['realtime']);
112
			}
113
			if (isset($ref['realtime1'])) {
114
				unset($ref['realtime1']);
115
			}
116
			if (isset($ref['realtime2'])) {
117
				unset($ref['realtime2']);
118
			}
119
			if (isset($ref['realtime3'])) {
120
				unset($ref['realtime3']);
121
			}
122
			if (isset($ref['upperlimit'])) {
123
				unset($ref['upperlimit']);
124
			}
125
			if (isset($ref['upperlimit1'])) {
126
				unset($ref['upperlimit1']);
127
			}
128
			if (isset($ref['upperlimit2'])) {
129
				unset($ref['upperlimit2']);
130
			}
131
			if (isset($ref['upperlimit3'])) {
132
				unset($ref['upperlimit3']);
133
			}
134
			if (isset($ref['linkshare'])) {
135
				unset($ref['linkshare']);
136
			}
137
			if (isset($ref['linkshare1'])) {
138
				unset($ref['linkshare1']);
139
			}
140
			if (isset($ref['linkshare2'])) {
141
				unset($ref['linkshare2']);
142
			}
143
			if (isset($ref['linkshare3'])) {
144
				unset($ref['linkshare3']);
145
			}
146
			if (isset($ref['hogs'])) {
147
				unset($ref['hogs']);
148
			}
149
			if (isset($ref['buckets'])) {
150
				unset($ref['buckets']);
151
			}
152
			break;
153
	}
154
}
155

    
156
function get_bandwidthtype_scale($type) {
157
	switch ($type) {
158
		case "Gb":
159
			$factor = 1024 * 1024 * 1024;
160
			break;
161
		case "Mb":
162
			$factor = 1024 * 1024;
163
			break;
164
		case "Kb":
165
			$factor = 1024;
166
			break;
167
		case "b":
168
		default:
169
			$factor = 1;
170
			break;
171
	}
172
	return intval($factor);
173
}
174

    
175
function get_bandwidth($bw, $scale, $obj) {
176

    
177
	$pattern= "/(b|Kb|Mb|Gb|%)/";
178
	if (!preg_match($pattern, $scale, $match))
179
		return 0;
180

    
181
	switch ($match[1]) {
182
		case '%':
183
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
184
			break;
185
		default:
186
			$objbw = $bw * get_bandwidthtype_scale($scale);
187
			break;
188
	}
189

    
190
	return floatval($objbw);
191
}
192

    
193
/*
194
 * XXX - unused
195
 *
196
function get_hfsc_bandwidth($object, $bw) {
197
	$pattern= "/[0-9]+/";
198
	if (preg_match($pattern, $bw, $match)) {
199
		$bw_1 = $match[1];
200
	} else {
201
		return 0;
202
	}
203
	$pattern= "/(b|Kb|Mb|Gb|%)/";
204
	if (preg_match($pattern, $bw, $match)) {
205
		switch ($match[1]) {
206
			case '%':
207
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
208
				break;
209
			default:
210
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
211
				break;
212
		}
213
		return floatval($bw_1);
214
	} else {
215
		return 0;
216
	}
217
}
218
*/
219

    
220
function get_queue_bandwidth($obj) {
221
	$bw = $obj->GetBandwidth();
222
	$scale = $obj->GetBwscale();
223

    
224
	$pattern= "/(b|Kb|Mb|Gb|%)/";
225
	if (!preg_match($pattern, $scale, $match))
226
		return 0;
227

    
228
	switch ($match[1]) {
229
		case '%':
230
			$objbw = ($bw / 100) * get_queue_bandwidth($obj->GetParent());
231
			break;
232
		default:
233
			$objbw = $bw * get_bandwidthtype_scale($scale);
234
			break;
235
	}
236

    
237
	return floatval($objbw);
238
}
239

    
240
function get_interface_bandwidth($object) {
241
	global $altq_list_queues;
242

    
243
	$int = $object->GetInterface();
244
	$altq =& $altq_list_queues[$int];
245
	if ($altq) {
246
		$bw_3 = $altq->GetBandwidth();
247
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
248
		return floatval($bw_3);
249
	} else {
250
		return 0;
251
	}
252
}
253

    
254
/*
255
 * This is duplicated here since we cannot include guiconfig.inc.
256
 * Including it makes all stuff break.
257
 */
258
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
259

    
260
	/* check for bad control characters */
261
	foreach ($postdata as $pn => $pd) {
262
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
263
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
264
		}
265
	}
266

    
267
	for ($i = 0; $i < count($reqdfields); $i++) {
268
		if ($postdata[$reqdfields[$i]] == "") {
269
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
270
		}
271
	}
272
}
273

    
274
function cleanup_queue_from_rules($queue) {
275
	global $config;
276

    
277
	foreach ($config['filter']['rule'] as $rule) {
278
		if ($rule['defaultqueue'] == $queue) {
279
			unset($rule['defaultqueue']);
280
		}
281
		if ($rule['ackqueue'] == $queue) {
282
			unset($rule['ackqueue']);
283
		}
284
	}
285
}
286

    
287
function cleanup_dnqueue_from_rules($queue) {
288
	global $config;
289

    
290
	foreach ($config['filter']['rule'] as $rule) {
291
		if ($rule['dnpipe'] == $queue) {
292
			unset($rule['dnpipe']);
293
		}
294
		if ($rule['pdnpipe'] == $queue) {
295
			unset($rule['pdnpipe']);
296
		}
297
	}
298
}
299

    
300
class altq_root_queue {
301
	var $interface;
302
	var $tbrconfig ;
303
	var $bandwidth;
304
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
305
	var $scheduler;
306
	var $qlimit;
307
	var $queues = array();
308
	var $qenabled = false;
309
	var $link;
310

    
311
	/* Accessor functions */
312
	function GetDefaultQueuePresent() {
313
		if (!empty($this->queues)) {
314
			foreach ($this->queues as $q) {
315
				if ($q->GetDefault()) {
316
					return true;
317
				}
318
			}
319
		}
320

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

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

    
410
	function CheckBandwidth($bw, $bwtype) {
411
		$sum = $this->GetTotalBw();
412
		if ($sum > $bw * get_bandwidthtype_scale($bwtype))
413
			return 1;
414
		foreach ($this->queues as $q) {
415
			if ($q->CheckBandwidth(0, ''))
416
				return 1;
417
		}
418

    
419
		return 0;
420
	}
421

    
422
	function GetTotalBw($qignore = NULL) {
423
		$sum = 0;
424
		foreach ($this->queues as $q) {
425
			if ($qignore != NULL && $qignore == $q)
426
				continue;
427
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
428
		}
429

    
430
		return $sum;
431
	}
432

    
433
	function validate_input($data, &$input_errors) {
434

    
435
		$reqdfields[] = "bandwidth";
436
		$reqdfieldsn[] = gettext("Bandwidth");
437
		$reqdfields[] = "bandwidthtype";
438
		$reqdfieldsn[] = gettext("Bandwidthtype");
439

    
440
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
441

    
442
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
443
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
444
		}
445
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
446
			$input_errors[] = gettext("Bandwidth must be an integer.");
447
		}
448
		if ($data['bandwidth'] < 0) {
449
			$input_errors[] = gettext("Bandwidth cannot be negative.");
450
		}
451
		if ($data['bandwidthtype'] == "%") {
452
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
453
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
454
			}
455
		}
456
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
457
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
458

    
459
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
460
			$input_errors[] = gettext("Qlimit must be an integer.");
461
		}
462
		if ($data['qlimit'] < 0) {
463
			$input_errors[] = gettext("Qlimit must be positive.");
464
		}
465
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
466
			$input_errors[] = gettext("Tbrsize must be an integer.");
467
		}
468
		if ($data['tbrconfig'] < 0) {
469
			$input_errors[] = gettext("Tbrsize must be positive.");
470
		}
471
	}
472

    
473
	/* Implement this to shorten some code on the frontend page */
474
	function ReadConfig(&$conf) {
475
		if (isset($conf['tbrconfig'])) {
476
			$this->SetTbrConfig($conf['tbrconfig']);
477
		} else {
478
			$this->SetTbrConfig($conf['tbrconfig']);
479
		}
480
		$this->SetBandwidth($conf['bandwidth']);
481
		if ($conf['bandwidthtype'] <> "") {
482
			$this->SetBwscale($conf['bandwidthtype']);
483
		}
484
		if (isset($conf['scheduler'])) {
485
			if ($this->GetScheduler() != $conf['scheduler']) {
486
				foreach ($this->queues as $q) {
487
					clean_child_queues($conf['scheduler'], $this->GetLink());
488
					$q->clean_queue($conf['scheduler']);
489
				}
490
			}
491
			$this->SetScheduler($conf['scheduler']);
492
		}
493
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
494
			$this->SetQlimit($conf['qlimit']);
495
		} else {
496
			$this->SetQlimit("");
497
		}
498
		if (isset($conf['name'])) {
499
			$this->SetQname($conf['name']);
500
		}
501
		if (!empty($conf['enabled'])) {
502
			$this->SetEnabled($conf['enabled']);
503
		} else {
504
			$this->SetEnabled("");
505
		}
506
	}
507

    
508
	function copy_queue($interface, &$cflink) {
509
		$cflink['interface'] = $interface;
510
		$cflink['name'] = $interface;
511
		$cflink['scheduler'] = $this->GetScheduler();
512
		$cflink['bandwidth'] = $this->GetBandwidth();
513
		$cflink['bandwidthtype'] = $this->GetBwscale();
514
		$cflink['qlimit'] = $this->GetQlimit();
515
		$cflink['tbrconfig'] = $this->GetTbrConfig();
516
		$cflink['enabled'] = $this->GetEnabled();
517
		if (is_array($this->queues)) {
518
			$cflink['queue'] = array();
519
			foreach ($this->queues as $q) {
520
				$cflink['queue'][$q->GetQname()] = array();
521
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
522
			}
523
		}
524
	}
525

    
526
	function &get_queue_list(&$q = null) {
527
		$qlist = array();
528

    
529
		//$qlist[$this->GetQname()] = & $this;
530
		if (is_array($this->queues)) {
531
			foreach ($this->queues as $queue) {
532
				$queue->get_queue_list($qlist);
533
			}
534
		}
535
		return $qlist;
536
	}
537

    
538
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
539

    
540
		if (!is_array($this->queues)) {
541
			$this->queues = array();
542
		}
543

    
544
		switch ($this->GetScheduler()) {
545
			case "PRIQ":
546
				$q =& new priq_queue();
547
				break;
548
			case "HFSC":
549
				$q =& new hfsc_queue();
550
				break;
551
			case "CBQ":
552
				$q =& new cbq_queue();
553
				break;
554
			case "FAIRQ":
555
				$q =& new fairq_queue();
556
				break;
557
			default:
558
				/* XXX: but should not happen anyway */
559
				return;
560
				break;
561
		}
562
		$q->SetLink($path);
563
		$q->SetInterface($this->GetInterface());
564
		$q->SetEnabled("on");
565
		$q->SetParent($this);
566
		$q->ReadConfig($queue);
567
		$q->validate_input($queue, $input_errors);
568

    
569
		$this->queues[$q->GetQname()] = &$q;
570
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
571
		if (is_array($queue['queue'])) {
572
			foreach ($queue['queue'] as $key1 => $que) {
573
				array_push($path, $key1);
574
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
575
				array_pop($path);
576
			}
577
		}
578

    
579
		return $q;
580
	}
581

    
582
	/* interface here might be optional */
583
	function &find_queue($interface, $qname) {
584
		if ($qname == $this->GetQname()) {
585
			return $this;
586
		}
587
		foreach ($this->queues as $q) {
588
			$result =& $q->find_queue("", $qname);
589
			if ($result) {
590
				return $result;
591
			}
592
		}
593
	}
594

    
595
	function &find_parentqueue($interface, $qname) {
596
		if ($qname == $interface) {
597
			$result = NULL;
598
		} else if ($this->queues[$qname]) {
599
			$result = $this;
600
		} else if ($this->GetScheduler() <> "PRIQ") {
601
			foreach ($this->queues as $q) {
602
				$result = $q->find_parentqueue("", $qname);
603
				if ($result) {
604
					return $result;
605
				}
606
			}
607
		}
608
	}
609

    
610
	function build_tree() {
611
		global $shaperIFlist;
612

    
613
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
614
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
615
		if (is_array($this->queues)) {
616
			$tree .= "<ul>";
617
			foreach ($this->queues as $q) {
618
				$tree .= $q->build_tree();
619
			}
620
			$tree .= "</ul>";
621
		}
622
		$tree .= "</li>";
623
		return $tree;
624
	}
625

    
626
	function delete_queue() {
627
		foreach ($this->queues as $q)
628
			$q->delete_queue();
629
		unset_object_by_reference($this->GetLink());
630
	}
631

    
632
	function delete_all() {
633
		if (count($this->queues)) {
634
			foreach ($this->queues as $q) {
635
				$q->delete_all();
636
				unset_object_by_reference($q->GetLink());
637
				unset($q);
638
			}
639
			unset($this->queues);
640
		}
641
	}
642

    
643
	/*
644
	 * First it spits:
645
	 * altq on $interface ..............
646
	 *	then it goes like
647
	 *	foreach ($queues as $qkey => $queue) {
648
	 *		this->queues[$qkey]->build_rule();
649
	 *	}
650
	 */
651
	function build_rules(&$default = false) {
652
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
653
			$default = false;
654
			$rules = " altq on " . get_real_interface($this->GetInterface());
655
			if ($this->GetScheduler()) {
656
				$rules .= " ".strtolower($this->GetScheduler());
657
			}
658
			if ($this->GetQlimit() > 0) {
659
				$rules .= " qlimit " . $this->GetQlimit() . " ";
660
			}
661
			if ($this->GetBandwidth()) {
662
				$rules .= " bandwidth ".trim($this->GetBandwidth());
663
				if ($this->GetBwscale()) {
664
					$rules .= $this->GetBwscale();
665
				}
666
			}
667
			if ($this->GetTbrConfig()) {
668
				$rules .= " tbrsize ".$this->GetTbrConfig();
669
			}
670
			if (count($this->queues)) {
671
				$i = count($this->queues);
672
				$rules .= " queue { ";
673
				foreach ($this->queues as $qkey => $qnone) {
674
					if ($i > 1) {
675
						$i--;
676
						$rules .= " {$qkey}, ";
677
					} else {
678
						$rules .= " {$qkey} ";
679
					}
680
				}
681
				$rules .= " } \n";
682
				foreach ($this->queues as $q) {
683
					$rules .= $q->build_rules($default);
684
				}
685
			}
686

    
687
			if ($default == false) {
688
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
689
				file_notice("Shaper", $error, "Error occurred", "");
690
				unset($error);
691
				return "\n";
692
			}
693
			$frule .= $rules;
694
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
695
			$rules = " altq on " . get_real_interface($this->GetInterface());
696
			if ($this->GetScheduler()) {
697
				$rules .= " ".strtolower($this->GetScheduler());
698
			}
699
			if ($this->GetQlimit() > 0) {
700
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
701
			}
702
			if ($this->GetBandwidth()) {
703
				$rules .= " bandwidth ".trim($this->GetBandwidth());
704
				if ($this->GetBwscale()) {
705
					$rules .= $this->GetBwscale();
706
				}
707
			}
708
			if ($this->GetTbrConfig()) {
709
				$rules .= " tbrsize ".$this->GetTbrConfig();
710
			}
711

    
712
			$rules .= " queue";
713
		}
714

    
715
		$rules .= " \n";
716
		return $rules;
717
	}
718

    
719
	function build_javascript() {
720
		$javascript = "<script type=\"text/javascript\">";
721
		$javascript .= "//<![CDATA[\n";
722
		$javascript .= "function mySuspend() {";
723
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
724
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
725
		$javascript .= "else if (document.all)";
726
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
727
		$javascript .= "}";
728

    
729
		$javascript .= "function myResume() {";
730
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
731
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
732
		$javascript .= "else if (document.all) ";
733
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
734
		$javascript .= "}";
735
		$javascript .= "//]]>";
736
		$javascript .= "</script>";
737

    
738
		return $javascript;
739
	}
740

    
741
	function build_shortform() {
742
		global $g;
743

    
744
		$altq =& $this;
745

    
746
		if ($altq) {
747
			$scheduler = ": " . $altq->GetScheduler();
748
		}
749

    
750
		$form = '<dl class="dl-horizontal">';
751
		$form .= '	<dt>';
752
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
753
		$form .= '	</dt>';
754
		$form .= '	<dd>';
755
		$form .=		$scheduler;
756
		$form .= '	</dd>';
757

    
758
		$form .= '	<dt>';
759
		$form .=		'Bandwidth';
760
		$form .= '	</dt>';
761
		$form .= '	<dd>';
762
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
763
		$form .= '	</dd>';
764

    
765
		$form .= '	<dt>';
766
		$form .= 'Disable';
767
		$form .= '	<dt>';
768
		$form .= '	<dd>';
769

    
770
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
771
		$form .= $this->GetInterface() . '&amp;queue=';
772
		$form .= $this->GetQname() . '&amp;action=delete">';
773
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
774
		$form .= gettext("Remove shaper from this interface") . '</a>';
775

    
776
		$form .= '	</dd>';
777

    
778
		$form .= '</dl>';
779

    
780
		return $form;
781

    
782
	}
783

    
784
	/*
785
	 * For requesting the parameters of the root queues
786
	 * to the user like the traffic wizard does.
787
	 */
788
	function build_form() {
789

    
790
		$sform = new Form();
791

    
792
		$sform->setAction("firewall_shaper.php");
793

    
794
		$section = new Form_Section(null);
795

    
796
		$section->addInput(new Form_Checkbox(
797
			'enabled',
798
			'Enable/Disable',
799
			'Enable/disable discipline and its children',
800
			($this->GetEnabled() == "on"),
801
			'on'
802
		));
803

    
804
		$section->addInput(new Form_StaticText(
805
			'*Name',
806
			$this->GetQname()
807
		));
808

    
809
		$section->addInput(new Form_Select(
810
			'scheduler',
811
			'Scheduler Type',
812
			$this->GetScheduler(),
813
			array('HFSC' => 'HFSC',
814
				  'CBQ' => 'CBQ',
815
				  'FAIRQ' => 'FAIRQ',
816
				  'CODELQ' => 'CODELQ',
817
				  'PRIQ' => 'PRIQ')
818
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
819

    
820
		$group = new Form_group('Bandwidth');
821

    
822
		$group->add(new Form_Input(
823
			'bandwidth',
824
			null,
825
			'number',
826
			$this->GetBandwidth()
827
		));
828

    
829
		$group->add(new Form_Select(
830
			'bandwidthtype',
831
			null,
832
			$this->GetBwscale(),
833
			array('Kb' => 'Kbit/s',
834
				  'Mb' => 'Mbit/s',
835
				  'Gb' => 'Gbit/s',
836
				  'b' => 'Bit/s',
837
				  '%' => '%')
838
		));
839

    
840
		$section->add($group);
841

    
842
		$section->addInput(new Form_Input(
843
			'qlimit',
844
			'Queue Limit',
845
			'number',
846
			$this->GetQlimit()
847
		));
848

    
849
		$section->addInput(new Form_Input(
850
			'tbrconfig',
851
			'TBR Size',
852
			'number',
853
			$this->GetTbrConfig()
854
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
855
					'bandwidth are used to determine the size.');
856

    
857
		$section->addInput(new Form_Input(
858
			'interface',
859
			null,
860
			'hidden',
861
			$this->GetInterface()
862
		));
863

    
864
		$section->addInput(new Form_Input(
865
			'name',
866
			null,
867
			'hidden',
868
			$this->GetQname()
869
		));
870

    
871
		$sform->add($section);
872

    
873
		return($sform);
874
	}
875

    
876
	function update_altq_queue_data(&$data) {
877
		$this->ReadConfig($data);
878
	}
879

    
880
	/*
881
	 * Should call on each of it queues and subqueues
882
	 * the same function much like build_rules();
883
	 */
884
	function wconfig() {
885
		$cflink = &get_reference_to_me_in_config($this->GetLink());
886
		if (!is_array($cflink)) {
887
			$cflink = array();
888
		}
889
		$cflink['interface'] = $this->GetInterface();
890
		$cflink['name'] = $this->GetQname();
891
		$cflink['scheduler'] = $this->GetScheduler();
892
		$cflink['bandwidth'] = $this->GetBandwidth();
893
		$cflink['bandwidthtype'] = $this->GetBwscale();
894
		$cflink['qlimit'] = trim($this->GetQlimit());
895
		if (empty($cflink['qlimit'])) {
896
			unset($cflink['qlimit']);
897
		}
898
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
899
		if (empty($cflink['tbrconfig'])) {
900
			unset($cflink['tbrconfig']);
901
		}
902
		$cflink['enabled'] = $this->GetEnabled();
903
		if (empty($cflink['enabled'])) {
904
			unset($cflink['enabled']);
905
		}
906
	}
907

    
908
}
909

    
910
class priq_queue {
911
	var $qname;
912
	var $qinterface;
913
	var $qlimit;
914
	var $qpriority;
915
	var $description;
916
	var $isparent;
917
	var $qbandwidth;
918
	var $qbandwidthtype;
919
	var $qdefault = "";
920
	var $qrio = "";
921
	var $qred = "";
922
	var $qcodel = "";
923
	var $qecn = "";
924
	var $qack;
925
	var $qenabled = "";
926
	var $qparent;
927
	var $link;
928

    
929
	/* This is here to help with form building and building rules/lists */
930
	var $subqueues = array();
931

    
932
	/* Accessor functions */
933
	function SetLink($link) {
934
		$this->link = $link;
935
	}
936
	function GetLink() {
937
		return $this->link;
938
	}
939
	function &GetParent() {
940
		return $this->qparent;
941
	}
942
	function SetParent(&$parent) {
943
		$this->qparent = &$parent;
944
	}
945
	function GetEnabled() {
946
		return $this->qenabled;
947
	}
948
	function SetEnabled($value) {
949
		$this->qenabled = $value;
950
	}
951
	function CanHaveChildren() {
952
		return false;
953
	}
954
	function CanBeDeleted() {
955
		return true;
956
	}
957
	function GetQname() {
958
		return $this->qname;
959
	}
960
	function SetQname($name) {
961
		$this->qname = trim($name);
962
	}
963
	function GetBandwidth() {
964
		return $this->qbandwidth;
965
	}
966
	function SetBandwidth($bandwidth) {
967
		$this->qbandwidth = $bandwidth;
968
	}
969
	function GetInterface() {
970
		return $this->qinterface;
971
	}
972
	function SetInterface($name) {
973
		$this->qinterface = trim($name);
974
	}
975
	function GetQlimit() {
976
		return $this->qlimit;
977
	}
978
	function SetQlimit($limit) {
979
		$this->qlimit = $limit;
980
	}
981
	function GetQpriority() {
982
		return $this->qpriority;
983
	}
984
	function SetQpriority($priority) {
985
		$this->qpriority = $priority;
986
	}
987
	function GetDescription() {
988
		return $this->description;
989
	}
990
	function SetDescription($str) {
991
		$this->description = trim($str);
992
	}
993
	function GetFirstime() {
994
		return $this->firsttime;
995
	}
996
	function SetFirsttime($number) {
997
		$this->firsttime = $number;
998
	}
999
	function CheckBandwidth($bw, $bwtype) {
1000
		$parent = $this->GetParent();
1001
		$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1002
		if ($bw > 0)
1003
			$sum += get_bandwidth($bw, $bwtype, $parent);
1004
		if ($sum > get_queue_bandwidth($parent))
1005
			return 1;
1006

    
1007
		foreach ($this->subqueues as $q) {
1008
			if ($q->CheckBandwidth(0, ''))
1009
				return 1;
1010
		}
1011

    
1012
		return 0;
1013
	}
1014
	function GetTotalBw($qignore = NULL) {
1015
		$sum = 0;
1016
		foreach ($this->subqueues as $q) {
1017
			if ($qignore != NULL && $qignore == $q)
1018
				continue;
1019
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1020
		}
1021

    
1022
		return $sum;
1023
	}
1024
	function GetBwscale() {
1025
		return $this->qbandwidthtype;
1026
	}
1027
	function SetBwscale($scale) {
1028
		$this->qbandwidthtype = $scale;
1029
	}
1030
	function GetDefaultQueuePresent() {
1031
		if ($this->GetDefault()) {
1032
			return true;
1033
		}
1034
		if (!empty($this->subqueues)) {
1035
			foreach ($this->subqueues as $q) {
1036
				if ($q->GetDefault()) {
1037
					return true;
1038
				}
1039
			}
1040
		}
1041

    
1042
		return false;
1043
	}
1044
	function GetDefault() {
1045
		return $this->qdefault;
1046
	}
1047
	function SetDefault($value = false) {
1048
		$this->qdefault = $value;
1049
	}
1050
	function GetCodel() {
1051
		return $this->codel;
1052
	}
1053
	function SetCodel($codel = false) {
1054
		$this->codel = $codel;
1055
	}
1056
	function GetRed() {
1057
		return $this->qred;
1058
	}
1059
	function SetRed($red = false) {
1060
		$this->qred = $red;
1061
	}
1062
	function GetRio() {
1063
		return $this->qrio;
1064
	}
1065
	function SetRio($rio = false) {
1066
		$this->qrio = $rio;
1067
	}
1068
	function GetEcn() {
1069
		return $this->qecn;
1070
	}
1071
	function SetEcn($ecn = false) {
1072
		$this->qecn = $ecn;
1073
	}
1074
	function GetAck() {
1075
		return $this->qack;
1076
	}
1077
	function SetAck($ack = false) {
1078
		$this->qack = $ack;
1079
	}
1080

    
1081
	function GetBwscaleText() {
1082
		switch ($this->qbandwidthtype) {
1083
			case "b":
1084
				$bwscaletext = "Bit/s";
1085
				break;
1086
			case "Kb":
1087
				$bwscaletext = "Kbit/s";
1088
				break;
1089
			case "Mb":
1090
				$bwscaletext = "Mbit/s";
1091
				break;
1092
			case "Gb":
1093
				$bwscaletext = "Gbit/s";
1094
				break;
1095
			default:
1096
				/* For others that do not need translating like % */
1097
				$bwscaletext = $this->qbandwidthtype;
1098
				break;
1099
		}
1100
		return $bwscaletext;
1101
	}
1102

    
1103
	function build_javascript() {
1104
		$javascript = "<script type=\"text/javascript\">";
1105
		$javascript .= "//<![CDATA[\n";
1106
		$javascript .= "function mySuspend() { \n";
1107
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1108
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1109
		$javascript .= "else if (document.all)\n";
1110
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1111
		$javascript .= "}\n";
1112

    
1113
		$javascript .= "function myResume() {\n";
1114
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1115
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1116
		$javascript .= "else if (document.all)\n";
1117
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1118
		$javascript .= "}\n";
1119
		$javascript .= "//]]>";
1120
		$javascript .= "</script>";
1121

    
1122
		return $javascript;
1123
	}
1124

    
1125
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1126

    
1127
	/*
1128
	 * Currently this will not be called unless we decide to clone a whole
1129
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1130
	 */
1131
	function copy_queue($interface, &$cflink) {
1132

    
1133
		$cflink['name'] = $this->GetQname();
1134
		$cflink['interface'] = $interface;
1135
		$cflink['qlimit'] = $this->GetQlimit();
1136
		$cflink['priority'] = $this->GetQpriority();
1137
		$cflink['description'] = $this->GetDescription();
1138
		$cflink['enabled'] = $this->GetEnabled();
1139
		$cflink['default'] = $this->GetDefault();
1140
		$cflink['red'] = $this->GetRed();
1141
		$cflink['codel'] = $this->GetCodel();
1142
		$cflink['rio'] = $this->GetRio();
1143
		$cflink['ecn'] = $this->GetEcn();
1144

    
1145
		if (is_array($this->subqueues)) {
1146
			$cflinkp['queue'] = array();
1147
			foreach ($this->subqueues as $q) {
1148
				$cflink['queue'][$q->GetQname()] = array();
1149
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1150
			}
1151
		}
1152
	}
1153

    
1154
	function clean_queue($sched) {
1155
		clean_child_queues($sched, $this->GetLink());
1156
		if (is_array($this->subqueues)) {
1157
			foreach ($this->subqueues as $q) {
1158
				$q->clean_queue($sched);
1159
			}
1160
		}
1161
	}
1162

    
1163
	function &get_queue_list(&$qlist) {
1164

    
1165
		$qlist[$this->GetQname()] = & $this;
1166
		if (is_array($this->subqueues)) {
1167
			foreach ($this->subqueues as $queue) {
1168
				$queue->get_queue_list($qlist);
1169
			}
1170
		}
1171
	}
1172

    
1173
	function delete_queue() {
1174
		unref_on_altq_queue_list($this->GetQname());
1175
		cleanup_queue_from_rules($this->GetQname());
1176
		unset_object_by_reference($this->GetLink());
1177
	}
1178

    
1179
	function delete_all() {
1180
		if (count($this->subqueues)) {
1181
			foreach ($this->subqueues as $q) {
1182
				$q->delete_all();
1183
				unset_object_by_reference($q->GetLink());
1184
				unset($q);
1185
			}
1186
			unset($this->subqueues);
1187
		}
1188
	}
1189

    
1190
	function &find_queue($interface, $qname) {
1191
		if ($qname == $this->GetQname()) {
1192
			return $this;
1193
		}
1194
	}
1195

    
1196
	function find_parentqueue($interface, $qname) { return; }
1197

    
1198
	function validate_input($data, &$input_errors) {
1199

    
1200
		$reqdfields[] = "name";
1201
		$reqdfieldsn[] = gettext("Name");
1202
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1203

    
1204
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1205
			$input_errors[] = gettext("Bandwidth must be an integer.");
1206
		}
1207
		if ($data['bandwidth'] < 0) {
1208
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1209
		}
1210
		if ($data['bandwidthtype'] == "%") {
1211
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1212
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1213
			}
1214
		}
1215
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1216
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1217
		if ($data['priority'] && (!is_numeric($data['priority']) ||
1218
		    ($data['priority'] < 1) || ($data['priority'] > 15))) {
1219
			$input_errors[] = gettext("The priority must be an integer between 1 and 15.");
1220
		}
1221
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1222
				$input_errors[] = gettext("Queue limit must be an integer");
1223
		}
1224
		if ($data['qlimit'] < 0) {
1225
				$input_errors[] = gettext("Queue limit must be positive");
1226
		}
1227
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1228
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1229
		}
1230
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1231
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1232
		}
1233
		$default = $this->GetDefault();
1234
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1235
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1236
		}
1237
	}
1238

    
1239
	function ReadConfig(&$q) {
1240
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1241
			$this->SetQname($q['newname']);
1242
		} else if (!empty($q['newname'])) {
1243
			$this->SetQname($q['newname']);
1244
		} else if (isset($q['name'])) {
1245
			$this->SetQname($q['name']);
1246
		}
1247
		if (isset($q['interface'])) {
1248
			$this->SetInterface($q['interface']);
1249
		}
1250
		$this->SetBandwidth($q['bandwidth']);
1251
		if ($q['bandwidthtype'] <> "") {
1252
			$this->SetBwscale($q['bandwidthtype']);
1253
		}
1254
		if (!empty($q['qlimit'])) {
1255
			$this->SetQlimit($q['qlimit']);
1256
		} else {
1257
			$this->SetQlimit(""); // Default
1258
		}
1259
		if (!empty($q['priority'])) {
1260
			$this->SetQPriority($q['priority']);
1261
		} else {
1262
			$this->SetQpriority("");
1263
		}
1264
		if (!empty($q['description'])) {
1265
			$this->SetDescription($q['description']);
1266
		} else {
1267
			$this->SetDescription("");
1268
		}
1269
		if (!empty($q['red'])) {
1270
			$this->SetRed($q['red']);
1271
		} else {
1272
			$this->SetRed();
1273
		}
1274
		if (!empty($q['codel'])) {
1275
			$this->SetCodel($q['codel']);
1276
		} else {
1277
			$this->SetCodel();
1278
		}
1279
		if (!empty($q['rio'])) {
1280
			$this->SetRio($q['rio']);
1281
		} else {
1282
			$this->SetRio();
1283
		}
1284
		if (!empty($q['ecn'])) {
1285
			$this->SetEcn($q['ecn']);
1286
		} else {
1287
			$this->SetEcn();
1288
		}
1289
		if (!empty($q['default'])) {
1290
			$this->SetDefault($q['default']);
1291
		} else {
1292
			$this->SetDefault();
1293
		}
1294
		if (!empty($q['enabled'])) {
1295
			$this->SetEnabled($q['enabled']);
1296
		} else {
1297
			$this->SetEnabled("");
1298
		}
1299
	}
1300

    
1301
	function build_tree() {
1302
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1303
		$tree .= "\" ";
1304
		$tmpvalue = $this->GetDefault();
1305
		if (!empty($tmpvalue)) {
1306
			$tree .= " class=\"navlnk\"";
1307
		}
1308
		$tree .= " >" . $this->GetQname() . "</a>";
1309
		/*
1310
		 * Not needed here!
1311
		 * if (is_array($queues) {
1312
		 *	  $tree .= "<ul>";
1313
		 *	  foreach ($q as $queues)
1314
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1315
		 *	  endforeach
1316
		 *	  $tree .= "</ul>";
1317
		 * }
1318
		 */
1319

    
1320
		$tree .= "</li>";
1321

    
1322
		return $tree;
1323
	}
1324

    
1325
	/* Should return something like:
1326
	 * queue $qname on $qinterface bandwidth ....
1327
	 */
1328
	function build_rules(&$default = false) {
1329
		$pfq_rule = " queue ". $this->qname;
1330
		if ($this->GetInterface()) {
1331
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1332
		}
1333
		$tmpvalue = $this->GetQpriority();
1334
		if (!empty($tmpvalue)) {
1335
			$pfq_rule .= " priority ".$this->GetQpriority();
1336
		}
1337
		$tmpvalue = $this->GetQlimit();
1338
		if (!empty($tmpvalue)) {
1339
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1340
		}
1341
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1342
			$pfq_rule .= " priq ( ";
1343
			$tmpvalue = $this->GetRed();
1344
			if (!empty($tmpvalue)) {
1345
				$comma = 1;
1346
				$pfq_rule .= " red ";
1347
			}
1348
			$tmpvalue = $this->GetRio();
1349
			if (!empty($tmpvalue)) {
1350
				if ($comma) {
1351
					$pfq_rule .= " ,";
1352
				}
1353
				$comma = 1;
1354
				$pfq_rule .= " rio ";
1355
			}
1356
			$tmpvalue = $this->GetEcn();
1357
			if (!empty($tmpvalue)) {
1358
				if ($comma) {
1359
					$pfq_rule .= " ,";
1360
				}
1361
				$comma = 1;
1362
				$pfq_rule .= " ecn ";
1363
			}
1364
			$tmpvalue = $this->GetCodel();
1365
			if (!empty($tmpvalue)) {
1366
				if ($comma) {
1367
					$pfq_rule .= " ,";
1368
				}
1369
				$comma = 1;
1370
				$pfq_rule .= " codel ";
1371
			}
1372
			$tmpvalue = $this->GetDefault();
1373
			if (!empty($tmpvalue)) {
1374
				if ($comma) {
1375
					$pfq_rule .= " ,";
1376
				}
1377
				$pfq_rule .= " default ";
1378
				$default = true;
1379
			}
1380
			$pfq_rule .= " ) ";
1381
		}
1382

    
1383
		$pfq_rule .= " \n";
1384

    
1385
		return $pfq_rule;
1386
	}
1387

    
1388
	/*
1389
	 * To return the html form to show to user
1390
	 * for getting the parameters.
1391
	 * Should do even for first time when the
1392
	 * object is created and later when we may
1393
	 * need to update it. (2)
1394
	 */
1395

    
1396
	function build_form() {
1397

    
1398
		$sform = new Form();
1399

    
1400
		$sform->setAction("firewall_shaper.php");
1401

    
1402
		$section = new Form_Section("");
1403

    
1404
		$section->addInput(new Form_Checkbox(
1405
			'enabled',
1406
			'Enable/Disable',
1407
			'Enable/disable discipline and its children',
1408
			($this->GetEnabled() == "on"),
1409
			'on'
1410
		));
1411

    
1412
		$section->addInput(new Form_Input(
1413
			'newname',
1414
			'*Name',
1415
			'text',
1416
			$this->GetQname()
1417
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1418

    
1419
		$section->addInput(new Form_Input(
1420
			'name',
1421
			null,
1422
			'hidden',
1423
			$this->GetQname()
1424
		));
1425

    
1426
		$section->addInput(new Form_Input(
1427
			'priority',
1428
			'Priority',
1429
			'number',
1430
			$this->GetQpriority(),
1431
			['min' => '0', 'max'=> '7']
1432
		))->setHelp('For hfsc, the range is 0 to 7. The default is 1. Hfsc queues with a higher priority are preferred in the case of overload.');
1433

    
1434
		$section->addInput(new Form_Input(
1435
			'qlimit',
1436
			'Queue Limit',
1437
			'number',
1438
			$this->GetQlimit()
1439
		))->setHelp('Queue limit in packets.');
1440

    
1441
		$group = new Form_Group('Scheduler options');
1442

    
1443
		if (empty($this->subqueues)) {
1444
			$group->add(new Form_Checkbox(
1445
				'default',
1446
				null,
1447
				null,
1448
				$this->GetDefault(),
1449
				'default'
1450
			))->setHelp('Default Queue');
1451
		}
1452

    
1453
		$group->add(new Form_Checkbox(
1454
			'red',
1455
			null,
1456
			null,
1457
			!empty($this->GetRed())
1458
		))->setHelp('%1$sRandom Early Detection%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#red">', '</a>');
1459

    
1460
		$group->add(new Form_Checkbox(
1461
			'rio',
1462
			null,
1463
			null,
1464
			!empty($this->GetRio())
1465
		))->setHelp('%1$sRandom Early Detection In and Out%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#rio">', '</a>');
1466

    
1467
		$group->add(new Form_Checkbox(
1468
			'ecn',
1469
			null,
1470
			null,
1471
			!empty($this->GetEcn())
1472
		))->setHelp('%1$sExplicit Congestion Notification%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">', '</a>');
1473

    
1474
		$group->add(new Form_Checkbox(
1475
			'codel',
1476
			null,
1477
			null,
1478
			!empty($this->GetCodel())
1479
		))->setHelp('%1$sCodel Active Queue%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">', '</a>');
1480

    
1481
		$group->setHelp('Select options for this queue');
1482

    
1483
		$section->add($group);
1484

    
1485
		$section->addInput(new Form_Input(
1486
			'description',
1487
			'Description',
1488
			'text',
1489
			$this->GetDescription()
1490
		));
1491

    
1492
		$sform->add($section);
1493

    
1494
		$sform->addGlobal(new Form_Input(
1495
			'interface',
1496
			null,
1497
			'hidden',
1498
			$this->GetInterface()
1499
		));
1500

    
1501
		$sform->addGlobal(new Form_Input(
1502
			'name',
1503
			null,
1504
			'hidden',
1505
			$this->GetQname()
1506
		));
1507

    
1508
		return($sform);
1509
	}
1510

    
1511
	function build_shortform() {
1512
		/* XXX: Hacks in sight. Mostly layer violations!  */
1513
		global $g, $altq_list_queues;
1514
		global $shaperIFlist;
1515

    
1516
		$altq =& $altq_list_queues[$this->GetInterface()];
1517

    
1518
		if ($altq) {
1519
			$scheduler = $altq->GetScheduler();
1520
		}
1521

    
1522
		$form = '<dl class="dl-horizontal">';
1523
		$form .= '	<dt>';
1524
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1525
		$form .= '	</dt>';
1526
		$form .= '	<dd>';
1527
		$form .=		$scheduler;
1528
		$form .= '	</dd>';
1529

    
1530
		$form .= '	<dt>';
1531
		$form .=		'Bandwidth';
1532
		$form .= '	</dt>';
1533
		$form .= '	<dd>';
1534
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1535
		$form .= '	</dd>';
1536

    
1537
		$tmpvalue = $this->GetQpriority();
1538
		if (!empty($tmpvalue)) {
1539
			$form .= '	<dt>';
1540
			$form .=		'Priority';
1541
			$form .= '	<dt>';
1542
			$form .= '	<dd>';
1543
			$form .=		'On';
1544
			$form .= '	</dd>';
1545
		}
1546

    
1547
		$tmpvalue = $this->GetDefault();
1548
		if (!empty($tmpvalue)) {
1549
			$form .= '	<dt>';
1550
			$form .=		'Default';
1551
			$form .= '	<dt>';
1552
			$form .= '	<dd>';
1553
			$form .=		'On';
1554
			$form .= '	</dd>';
1555
		}
1556

    
1557
			$form .= '	<dt>';
1558
			$form .= 'Delete';
1559
			$form .= '	<dt>';
1560
			$form .= '	<dd>';
1561

    
1562
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1563
			$form .= $this->GetInterface() . '&amp;queue=';
1564
			$form .= $this->GetQname() . '&amp;action=delete">';
1565
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1566
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1567

    
1568
			$form .= '	</dd>';
1569

    
1570
			$form .= '</dl>';
1571

    
1572
		return $form;
1573

    
1574
	}
1575

    
1576
	function update_altq_queue_data(&$q) {
1577
		$this->ReadConfig($q);
1578
	}
1579

    
1580
	function wconfig() {
1581
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1582
		if (!is_array($cflink)) {
1583
			$cflink = array();
1584
		}
1585
		$cflink['name'] = $this->GetQname();
1586
		$cflink['interface'] = $this->GetInterface();
1587
		$cflink['qlimit'] = trim($this->GetQlimit());
1588
		if (empty($cflink['qlimit'])) {
1589
			unset($cflink['qlimit']);
1590
		}
1591
		$cflink['priority'] = trim($this->GetQpriority());
1592
		if (empty($cflink['priority'])) {
1593
			unset($cflink['priority']);
1594
		}
1595
		$cflink['description'] = trim($this->GetDescription());
1596
		if (empty($cflink['description'])) {
1597
			unset($cflink['description']);
1598
		}
1599
		$cflink['enabled'] = trim($this->GetEnabled());
1600
		if (empty($cflink['enabled'])) {
1601
			unset($cflink['enabled']);
1602
		}
1603
		$cflink['default'] = trim($this->GetDefault());
1604
		if (empty($cflink['default'])) {
1605
			unset($cflink['default']);
1606
		}
1607
		$cflink['red'] = trim($this->GetRed());
1608
		if (empty($cflink['red'])) {
1609
			unset($cflink['red']);
1610
		}
1611
		$cflink['codel'] = trim($this->GetCodel());
1612
		if (empty($cflink['codel'])) {
1613
			unset($cflink['codel']);
1614
		}
1615
		$cflink['rio'] = trim($this->GetRio());
1616
		if (empty($cflink['rio'])) {
1617
			unset($cflink['rio']);
1618
		}
1619
		$cflink['ecn'] = trim($this->GetEcn());
1620
		if (empty($cflink['ecn'])) {
1621
			unset($cflink['ecn']);
1622
		}
1623
	}
1624
}
1625

    
1626
class hfsc_queue extends priq_queue {
1627
	/* realtime */
1628
	var $realtime;
1629
	var $r_m1;
1630
	var $r_d;
1631
	var $r_m2;
1632
	/* linkshare */
1633
	var $linkshare;
1634
	var $l_m1;
1635
	var $l_d;
1636
	var $l_m2;
1637
	/* upperlimit */
1638
	var $upperlimit;
1639
	var $u_m1;
1640
	var $u_d;
1641
	var $u_m2;
1642

    
1643
	/*
1644
	 * HFSC can have nested queues.
1645
	 */
1646
	function CanHaveChildren() {
1647
		return true;
1648
	}
1649
	function GetRealtime() {
1650
		return $this->realtime;
1651
	}
1652
	function GetR_m1() {
1653
		return $this->r_m1;
1654
	}
1655
	function GetR_d() {
1656
		return $this->r_d;
1657
	}
1658
	function GetR_m2() {
1659
		return $this->r_m2;
1660
	}
1661
	function SetRealtime() {
1662
		$this->realtime = "on";
1663
	}
1664
	function DisableRealtime() {
1665
		$this->realtime = "";
1666
	}
1667
	function SetR_m1($value) {
1668
		$this->r_m1 = $value;
1669
	}
1670
	function SetR_d($value) {
1671
		$this->r_d = $value;
1672
	}
1673
	function SetR_m2($value) {
1674
		$this->r_m2 = $value;
1675
	}
1676
	function GetLinkshare() {
1677
		return $this->linkshare;
1678
	}
1679
	function DisableLinkshare() {
1680
		$this->linkshare = "";
1681
	}
1682
	function GetL_m1() {
1683
		return $this->l_m1;
1684
	}
1685
	function GetL_d() {
1686
		return $this->l_d;
1687
	}
1688
	function GetL_m2() {
1689
		return $this->l_m2;
1690
	}
1691
	function SetLinkshare() {
1692
		$this->linkshare = "on";
1693
	}
1694
	function SetL_m1($value) {
1695
		$this->l_m1 = $value;
1696
	}
1697
	function SetL_d($value) {
1698
		$this->l_d = $value;
1699
	}
1700
	function SetL_m2($value) {
1701
		$this->l_m2 = $value;
1702
	}
1703
	function GetUpperlimit() {
1704
		return $this->upperlimit;
1705
	}
1706
	function GetU_m1() {
1707
		return $this->u_m1;
1708
	}
1709
	function GetU_d() {
1710
		return $this->u_d;
1711
	}
1712
	function GetU_m2() {
1713
		return $this->u_m2;
1714
	}
1715
	function SetUpperlimit() {
1716
		$this->upperlimit = "on";
1717
	}
1718
	function DisableUpperlimit() {
1719
		$this->upperlimit = "";
1720
	}
1721
	function SetU_m1($value) {
1722
		$this->u_m1 = $value;
1723
	}
1724
	function SetU_d($value) {
1725
		$this->u_d = $value;
1726
	}
1727
	function SetU_m2($value) {
1728
		$this->u_m2 = $value;
1729
	}
1730

    
1731
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1732

    
1733
		if (!is_array($this->subqueues)) {
1734
			$this->subqueues = array();
1735
		}
1736
		$q =& new hfsc_queue();
1737
		$q->SetInterface($this->GetInterface());
1738
		$q->SetParent($this);
1739
		$q->ReadConfig($qname);
1740
		$q->validate_input($qname, $input_errors);
1741

    
1742
		$q->SetEnabled("on");
1743
		$q->SetLink($path);
1744

    
1745
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1746
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1747
		if (is_array($qname['queue'])) {
1748
			foreach ($qname['queue'] as $key1 => $que) {
1749
				array_push($path, $key1);
1750
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1751
				array_pop($path);
1752
			}
1753
		}
1754

    
1755
		return $q;
1756
	}
1757

    
1758
	function copy_queue($interface, &$cflink) {
1759

    
1760
		$cflink['name'] = $this->GetQname();
1761
		$cflink['interface'] = $interface;
1762
		$cflink['qlimit'] = trim($this->GetQlimit());
1763
		if (empty($cflink['qlimit'])) {
1764
			unset($cflink['qlimit']);
1765
		}
1766
		$cflink['priority'] = trim($this->GetQpriority());
1767
		if (empty($cflink['priority'])) {
1768
			unset($cflink['priority']);
1769
		}
1770
		$cflink['description'] = trim($this->GetDescription());
1771
		if (empty($cflink['description'])) {
1772
			unset($cflink['description']);
1773
		}
1774
		$cflink['bandwidth'] = $this->GetBandwidth();
1775
		$cflink['bandwidthtype'] = $this->GetBwscale();
1776
		$cflink['enabled'] = trim($this->GetEnabled());
1777
		if (empty($cflink['enabled'])) {
1778
			unset($cflink['enabled']);
1779
		}
1780
		$cflink['default'] = trim($this->GetDefault());
1781
		if (empty($cflink['default'])) {
1782
			unset($cflink['default']);
1783
		}
1784
		$cflink['red'] = trim($this->GetRed());
1785
		if (empty($cflink['red'])) {
1786
			unset($cflink['red']);
1787
		}
1788
		$cflink['rio'] = trim($this->GetRio());
1789
		if (empty($cflink['rio'])) {
1790
			unset($cflink['rio']);
1791
		}
1792
		$cflink['ecn'] = trim($this->GetEcn());
1793
		if (empty($cflink['ecn'])) {
1794
			unset($cflink['ecn']);
1795
		}
1796
		if ($this->GetLinkshare() <> "") {
1797
			if ($this->GetL_m1() <> "") {
1798
				$cflink['linkshare1'] = $this->GetL_m1();
1799
				$cflink['linkshare2'] = $this->GetL_d();
1800
				$cflink['linkshare'] = "on";
1801
			} else {
1802
				unset($cflink['linkshare1']);
1803
				unset($cflink['linkshare2']);
1804
				unset($cflink['linkshare']);
1805
			}
1806
			if ($this->GetL_m2() <> "") {
1807
				$cflink['linkshare3'] = $this->GetL_m2();
1808
				$cflink['linkshare'] = "on";
1809
			} else {
1810
				unset($cflink['linkshare3']);
1811
				unset($cflink['linkshare']);
1812
			}
1813
		}
1814
		if ($this->GetRealtime() <> "") {
1815
			if ($this->GetR_m1() <> "") {
1816
				$cflink['realtime1'] = $this->GetR_m1();
1817
				$cflink['realtime2'] = $this->GetR_d();
1818
				$cflink['realtime'] = "on";
1819
			} else {
1820
				unset($cflink['realtime1']);
1821
				unset($cflink['realtime2']);
1822
				unset($cflink['realtime']);
1823
			}
1824
			if ($this->GetR_m2() <> "") {
1825
				$cflink['realtime3'] = $this->GetR_m2();
1826
				$cflink['realtime'] = "on";
1827
			} else {
1828
				unset($cflink['realtime3']);
1829
				unset($cflink['realtime']);
1830
			}
1831
		}
1832
		if ($this->GetUpperlimit() <> "") {
1833
			if ($this->GetU_m1() <> "") {
1834
				$cflink['upperlimit1'] = $this->GetU_m1();
1835
				$cflink['upperlimit2'] = $this->GetU_d();
1836
				$cflink['upperlimit'] = "on";
1837
			} else {
1838
				unset($cflink['upperlimit']);
1839
				unset($cflink['upperlimit1']);
1840
				unset($cflink['upperlimit2']);
1841
			}
1842
			if ($this->GetU_m2() <> "") {
1843
				$cflink['upperlimit3'] = $this->GetU_m2();
1844
				$cflink['upperlimit'] = "on";
1845
			} else {
1846
				unset($cflink['upperlimit3']);
1847
				unset($cflink['upperlimit']);
1848
			}
1849
		}
1850

    
1851
		if (is_array($this->subqueues)) {
1852
			$cflinkp['queue'] = array();
1853
			foreach ($this->subqueues as $q) {
1854
				$cflink['queue'][$q->GetQname()] = array();
1855
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1856
			}
1857
		}
1858
	}
1859

    
1860
	function delete_queue() {
1861
		unref_on_altq_queue_list($this->GetQname());
1862
		cleanup_queue_from_rules($this->GetQname());
1863
		$parent =& $this->GetParent();
1864
		foreach ($this->subqueues as $q)
1865
			$q->delete_queue();
1866
		unset_object_by_reference($this->GetLink());
1867
	}
1868

    
1869
	/*
1870
	 * Should search even its children
1871
	 */
1872
	function &find_queue($interface, $qname) {
1873
		if ($qname == $this->GetQname()) {
1874
			return $this;
1875
		}
1876

    
1877
		foreach ($this->subqueues as $q) {
1878
			$result =& $q->find_queue("", $qname);
1879
			if ($result) {
1880
				return $result;
1881
			}
1882
		}
1883
	}
1884

    
1885
	function &find_parentqueue($interface, $qname) {
1886
		if ($this->subqueues[$qname]) {
1887
			return $this;
1888
		}
1889
		foreach ($this->subqueues as $q) {
1890
			$result = $q->find_parentqueue("", $qname);
1891
			if ($result) {
1892
				return $result;
1893
			}
1894
		}
1895
	}
1896

    
1897
	function validate_input($data, &$input_errors) {
1898
		parent::validate_input($data, $input_errors);
1899

    
1900
		$reqdfields[] = "bandwidth";
1901
		$reqdfieldsn[] = gettext("Bandwidth");
1902
		$reqdfields[] = "bandwidthtype";
1903
		$reqdfieldsn[] = gettext("Bandwidthtype");
1904

    
1905
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1906

    
1907
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
1908
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1909
				$input_errors[] = gettext("Bandwidth must be an integer.");
1910
			}
1911

    
1912
			if ($data['bandwidth'] < 0) {
1913
				$input_errors[] = gettext("Bandwidth cannot be negative.");
1914
			}
1915

    
1916
			if ($data['bandwidthtype'] == "%") {
1917
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1918
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1919
				}
1920
			}
1921
		}
1922

    
1923
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
1924
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
1925
		}
1926
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
1927
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
1928
		}
1929
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
1930
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
1931
		}
1932
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
1933
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
1934
		}
1935
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
1936
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
1937
		}
1938

    
1939
		/*
1940
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
1941
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
1942
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
1943
			if (floatval($bw_1) < floatval($bw_2)) {
1944
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
1945
			}
1946

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

    
1974
		/*
1975
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
1976
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
1977
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
1978
			if (floatval($bw_1) < floatval($bw_2)) {
1979
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
1980
			}
1981

    
1982
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1983
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
1984
			}
1985
		}
1986
		*/
1987

    
1988
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
1989
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
1990
		}
1991
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
1992
			$input_errors[] = gettext("realtime d value needs to be numeric");
1993
		}
1994
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
1995
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
1996
		}
1997

    
1998
		/*
1999
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2000
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2001
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2002
			if (floatval($bw_1) < floatval($bw_2)) {
2003
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2004
			}
2005

    
2006
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2007
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2008
			}
2009
		}
2010
		*/
2011
	}
2012

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

    
2068
	function build_tree() {
2069
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2070
		$tree .= "\" ";
2071
		$tmpvalue = $this->GetDefault();
2072
		if (!empty($tmpvalue)) {
2073
			$tree .= " class=\"navlnk\"";
2074
		}
2075
		$tree .= " >" . $this->GetQname() . "</a>";
2076
		if (is_array($this->subqueues)) {
2077
			$tree .= "<ul>";
2078
			foreach ($this->subqueues as $q) {
2079
				$tree .= $q->build_tree();
2080
			}
2081
			$tree .= "</ul>";
2082
		}
2083
		$tree .= "</li>";
2084
		return $tree;
2085
	}
2086

    
2087
	/* Even this should take children into consideration */
2088
	function build_rules(&$default = false) {
2089

    
2090
		$pfq_rule = " queue ". $this->qname;
2091
		if ($this->GetInterface()) {
2092
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2093
		}
2094
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2095
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2096
		}
2097

    
2098
		$tmpvalue = $this->GetQlimit();
2099
		if (!empty($tmpvalue)) {
2100
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2101
		}
2102
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2103
			$pfq_rule .= " hfsc ( ";
2104
			$tmpvalue = $this->GetRed();
2105
			if (!empty($tmpvalue)) {
2106
				$comma = 1;
2107
				$pfq_rule .= " red ";
2108
			}
2109

    
2110
			$tmpvalue = $this->GetRio();
2111
			if (!empty($tmpvalue)) {
2112
				if ($comma) {
2113
					$pfq_rule .= " ,";
2114
				}
2115
				$comma = 1;
2116
				$pfq_rule .= " rio ";
2117
			}
2118
			$tmpvalue = $this->GetEcn();
2119
			if (!empty($tmpvalue)) {
2120
				if ($comma) {
2121
					$pfq_rule .= " ,";
2122
				}
2123
				$comma = 1;
2124
				$pfq_rule .= " ecn ";
2125
			}
2126
			$tmpvalue = $this->GetCodel();
2127
			if (!empty($tmpvalue)) {
2128
				if ($comma) {
2129
					$pfq_rule .= " ,";
2130
				}
2131
				$comma = 1;
2132
				$pfq_rule .= " codel ";
2133
			}
2134
			$tmpvalue = $this->GetDefault();
2135
			if (!empty($tmpvalue)) {
2136
				if ($comma) {
2137
					$pfq_rule .= " ,";
2138
				}
2139
				$comma = 1;
2140
				$pfq_rule .= " default ";
2141
				$default = true;
2142
			}
2143

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

    
2195
		$pfq_rule .= " \n";
2196

    
2197
		return $pfq_rule;
2198
	}
2199

    
2200
	function build_javascript() {
2201

    
2202
		$javascript = <<<EOJS
2203
<script type="text/javascript">
2204
//<![CDATA[
2205
	events.push(function(){
2206

    
2207
		// Disables the specified input element
2208
		function disableInput(id, disable) {
2209
			$('#' + id).prop("disabled", disable);
2210
		}
2211

    
2212
		// Upperlimit
2213
		function enable_upperlimit() {
2214
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2215
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2216
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2217
		}
2218

    
2219
		$('#upperlimit').click(function () {
2220
			enable_upperlimit();
2221
		});
2222

    
2223
		enable_upperlimit();
2224

    
2225
		// realtime
2226
		function enable_realtime() {
2227
			disableInput('realtime1', !$('#realtime').prop('checked'));
2228
			disableInput('realtime2', !$('#realtime').prop('checked'));
2229
			disableInput('realtime3', !$('#realtime').prop('checked'));
2230
		}
2231

    
2232
		$('#realtime').click(function () {
2233
			enable_realtime();
2234
		});
2235

    
2236
		enable_realtime();
2237

    
2238
		// linkshare
2239
		function enable_linkshare() {
2240
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2241
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2242
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2243
		}
2244

    
2245
		$('#linkshare').click(function () {
2246
			enable_linkshare();
2247
		});
2248

    
2249
		enable_linkshare();
2250
	});
2251
//]]>
2252
</script>
2253
EOJS;
2254

    
2255
		return $javascript;
2256
	}
2257

    
2258
	function build_form() {
2259

    
2260
		$sform = parent::build_form();
2261

    
2262
		$section = new Form_Section('Service Curve (sc)');
2263

    
2264
		$group = new Form_Group('Bandwidth');
2265

    
2266
		$group->add(new Form_Input(
2267
			'bandwidth',
2268
			null,
2269
			'number',
2270
			$this->GetBandwidth(),
2271
			['step' => 'any', 'min' => '0.000']
2272
		));
2273

    
2274
		$group->add(new Form_Select(
2275
			'bandwidthtype',
2276
			null,
2277
			$this->GetBwscale(),
2278
			array('Kb' => 'Kbit/s',
2279
				  'Mb' => 'Mbit/s',
2280
				  'Gb' => 'Gbit/s',
2281
				  'b' => 'Bit/s',
2282
				  '%' => '%')
2283
		));
2284

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

    
2287
		$section->add($group);
2288

    
2289
		$group = new Form_Group('Max bandwidth for queue.');
2290

    
2291
		$group->add(new Form_Checkbox(
2292
			'upperlimit',
2293
			null,
2294
			'Upper Limit',
2295
			($this->GetUpperlimit()<> "")
2296
		));
2297

    
2298
		$group->add(new Form_Input(
2299
			'upperlimit1',
2300
			null,
2301
			'text',
2302
			$this->GetU_m1()
2303
		))->setHelp('m1');
2304

    
2305
		$group->add(new Form_Input(
2306
			'upperlimit2',
2307
			null,
2308
			'text',
2309
			$this->GetU_d()
2310
		))->setHelp('d');
2311

    
2312
		$group->add(new Form_Input(
2313
			'upperlimit3',
2314
			null,
2315
			'text',
2316
			$this->GetU_m2()
2317
		))->setHelp('m2');
2318

    
2319

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

    
2322
		$group = new Form_Group('Min bandwidth for queue.');
2323

    
2324
		$group->add(new Form_Checkbox(
2325
			'realtime',
2326
			null,
2327
			'Real Time',
2328
			($this->GetRealtime()<> "")
2329
		));
2330

    
2331
		$group->add(new Form_Input(
2332
			'realtime1',
2333
			null,
2334
			'text',
2335
			$this->GetR_m1()
2336
		))->setHelp('m1');
2337

    
2338
		$group->add(new Form_Input(
2339
			'realtime2',
2340
			null,
2341
			'text',
2342
			$this->GetR_d()
2343
		))->setHelp('d');
2344

    
2345
		$group->add(new Form_Input(
2346
			'realtime3',
2347
			null,
2348
			'text',
2349
			$this->GetR_m2()
2350
		))->setHelp('m2');
2351

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

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

    
2356
		$group->add(new Form_Checkbox(
2357
			'linkshare',
2358
			null,
2359
			'Link Share',
2360
			($this->GetLinkshare()<> "")
2361
		));
2362

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

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

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

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

    
2390
		$section->add($group);
2391

    
2392
		$sform->add($section);
2393

    
2394
		return($sform);
2395
	}
2396

    
2397
	function update_altq_queue_data(&$data) {
2398
		$this->ReadConfig($data);
2399
	}
2400

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

    
2518
class cbq_queue extends priq_queue {
2519
	var $qborrow = "";
2520

    
2521
	function GetBorrow() {
2522
		return $this->qborrow;
2523
	}
2524
	function SetBorrow($borrow) {
2525
		$this->qborrow = $borrow;
2526
	}
2527
	function CanHaveChildren() {
2528
		return true;
2529
	}
2530

    
2531
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2532

    
2533
		if (!is_array($this->subqueues)) {
2534
			$this->subqueues = array();
2535
		}
2536
		$q =& new cbq_queue();
2537
		$q->SetInterface($this->GetInterface());
2538
		$q->SetParent($this);
2539
		$q->ReadConfig($qname);
2540
		$q->validate_input($qname, $input_errors);
2541

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

    
2554
		return $q;
2555
	}
2556

    
2557
	function copy_queue($interface, &$cflink) {
2558

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

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

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

    
2635
	function delete_queue() {
2636
		unref_on_altq_queue_list($this->GetQname());
2637
		cleanup_queue_from_rules($this->GetQname());
2638
		foreach ($this->subqueues as $q)
2639
			$q->delete_queue();
2640
		unset_object_by_reference($this->GetLink());
2641
	}
2642

    
2643
	function validate_input($data, &$input_errors) {
2644
		parent::validate_input($data, $input_errors);
2645

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

    
2654
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2655
	}
2656

    
2657
	function ReadConfig(&$q) {
2658
		parent::ReadConfig($q);
2659
		if (!empty($q['borrow'])) {
2660
			$this->SetBorrow("on");
2661
		} else {
2662
			$this->SetBorrow("");
2663
		}
2664
	}
2665

    
2666
	function build_javascript() {
2667
		return parent::build_javascript();
2668
	}
2669

    
2670
	function build_tree() {
2671
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2672
		$tree .= "\" ";
2673
		$tmpvalue = trim($this->GetDefault());
2674
		if (!empty($tmpvalue)) {
2675
			$tree .= " class=\"navlnk\"";
2676
		}
2677
		$tree .= " >" . $this->GetQname() . "</a>";
2678
		if (is_array($this->subqueues)) {
2679
			$tree .= "<ul>";
2680
			foreach ($this->subqueues as $q) {
2681
				$tree .= $q->build_tree();
2682
			}
2683
			$tree .= "</ul>";
2684
		}
2685
		$tree .= "</li>";
2686
		return $tree;
2687
	}
2688

    
2689
	/* Even this should take children into consideration */
2690
	function build_rules(&$default = false) {
2691
		$pfq_rule = "queue ". $this->qname;
2692
		if ($this->GetInterface()) {
2693
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2694
		}
2695
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2696
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2697
		}
2698
		$tmpvalue = $this->GetQpriority();
2699
		if (!empty($tmpvalue)) {
2700
			$pfq_rule .= " priority " . $this->GetQpriority();
2701
		}
2702
		$tmpvalue = trim($this->GetQlimit());
2703
		if (!empty($tmpvalue)) {
2704
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2705
		}
2706
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
2707
			$pfq_rule .= " cbq ( ";
2708
			$tmpvalue = trim($this->GetRed());
2709
			if (!empty($tmpvalue)) {
2710
				$comma = 1;
2711
				$pfq_rule .= " red ";
2712
			}
2713
			$tmpvalue = trim($this->GetCodel());
2714
			if (!empty($tmpvalue)) {
2715
				$comma = 1;
2716
				$pfq_rule .= " codel ";
2717
			}
2718
			$tmpvalue = trim($this->GetRio());
2719
			if (!empty($tmpvalue)) {
2720
				if ($comma) {
2721
					$pfq_rule .= " ,";
2722
				}
2723
				$comma = 1;
2724
				$pfq_rule .= " rio ";
2725
			}
2726
			$tmpvalue = trim($this->GetEcn());
2727
			if (!empty($tmpvalue)) {
2728
				if ($comma) {
2729
					$pfq_rule .= " ,";
2730
				}
2731
				$comma = 1;
2732
				$pfq_rule .= " ecn ";
2733
			}
2734
			$tmpvalue = trim($this->GetDefault());
2735
			if (!empty($tmpvalue)) {
2736
				if ($comma) {
2737
					$pfq_rule .= " ,";
2738
				}
2739
				$comma = 1;
2740
				$pfq_rule .= " default ";
2741
				$default = true;
2742
			}
2743
			$tmpvalue = trim($this->GetBorrow());
2744
			if (!empty($tmpvalue)) {
2745
				if ($comma) {
2746
					$pfq_rule .= ", ";
2747
				}
2748
				$pfq_rule .= " borrow ";
2749
			}
2750
			$pfq_rule .= " ) ";
2751
		}
2752
		if (count($this->subqueues)) {
2753
			$i = count($this->subqueues);
2754
			$pfq_rule .= " { ";
2755
			foreach ($this->subqueues as $qkey => $qnone) {
2756
				if ($i > 1) {
2757
					$i--;
2758
					$pfq_rule .= " {$qkey}, ";
2759
				} else {
2760
					$pfq_rule .= " {$qkey} ";
2761
				}
2762
			}
2763
			$pfq_rule .= " } \n";
2764
			foreach ($this->subqueues as $q) {
2765
				$pfq_rule .= $q->build_rules($default);
2766
			}
2767
		}
2768

    
2769
		$pfq_rule .= " \n";
2770
		return $pfq_rule;
2771
	}
2772

    
2773
	function build_form() {
2774
		$sform = parent::build_form();
2775

    
2776
		$section = new Form_Section('NOTITLE');
2777

    
2778
		$group = new Form_Group('Bandwidth');
2779

    
2780
		$group->add(new Form_Input(
2781
			'bandwidth',
2782
			null,
2783
			'number',
2784
			$this->GetBandwidth()
2785
		));
2786

    
2787
		$group->add(new Form_Select(
2788
			'bandwidthtype',
2789
			null,
2790
			$this->GetBwscale(),
2791
			array('Kb' => 'Kbit/s',
2792
				  'Mb' => 'Mbit/s',
2793
				  'Gb' => 'Gbit/s',
2794
				  'b' => 'Bit/s',
2795
				  '%' => '%')
2796
		));
2797

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

    
2800
		$section->add($group);
2801

    
2802
		$section->addInput(new Form_Checkbox(
2803
			'borrow',
2804
			'Scheduler option',
2805
			'Borrow from other queues when available',
2806
			($this->GetBorrow() == "on")
2807
		));
2808

    
2809
		$sform->add($section);
2810

    
2811
		return $sform;
2812
	}
2813

    
2814
	function update_altq_queue_data(&$data) {
2815
		$this->ReadConfig($data);
2816
	}
2817

    
2818
	function wconfig() {
2819
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2820
		if (!is_array($cflink)) {
2821
			$cflink = array();
2822
		}
2823
		$cflink['interface'] = $this->GetInterface();
2824
		$cflink['qlimit'] = trim($this->GetQlimit());
2825
		if (empty($cflink['qlimit'])) {
2826
			unset($cflink['qlimit']);
2827
		}
2828
		$cflink['priority'] = $this->GetQpriority();
2829
		if (empty($cflink['priority'])) {
2830
			unset($cflink['priority']);
2831
		}
2832
		$cflink['name'] = $this->GetQname();
2833
		$cflink['description'] = $this->GetDescription();
2834
		if (empty($cflink['description'])) {
2835
			unset($cflink['description']);
2836
		}
2837
		$cflink['bandwidth'] = $this->GetBandwidth();
2838
		$cflink['bandwidthtype'] = $this->GetBwscale();
2839
		$cflink['enabled'] = trim($this->GetEnabled());
2840
		if (empty($cflink['enabled'])) {
2841
			unset($cflink['enabled']);
2842
		}
2843
		$cflink['default'] = trim($this->GetDefault());
2844
		if (empty($cflink['default'])) {
2845
			unset($cflink['default']);
2846
		}
2847
		$cflink['red'] = trim($this->GetRed());
2848
		if (empty($cflink['red'])) {
2849
			unset($cflink['red']);
2850
		}
2851
		$cflink['rio'] = trim($this->GetRio());
2852
		if (empty($cflink['rio'])) {
2853
			unset($cflink['rio']);
2854
		}
2855
		$cflink['ecn'] = trim($this->GetEcn());
2856
		if (empty($cflink['ecn'])) {
2857
			unset($cflink['ecn']);
2858
		}
2859
		$cflink['codel'] = trim($this->GetCodel());
2860
		if (empty($cflink['codel'])) {
2861
			unset($cflink['codel']);
2862
		}
2863
		$cflink['borrow'] = trim($this->GetBorrow());
2864
		if (empty($cflink['borrow'])) {
2865
			unset($cflink['borrow']);
2866
		}
2867
	}
2868
}
2869

    
2870
class fairq_queue extends priq_queue {
2871
	var $hogs;
2872
	var $buckets;
2873

    
2874
	function GetBuckets() {
2875
		return $this->buckets;
2876
	}
2877
	function SetBuckets($buckets) {
2878
		$this->buckets = $buckets;
2879
	}
2880
	function GetHogs() {
2881
		return $this->hogs;
2882
	}
2883
	function SetHogs($hogs) {
2884
		$this->hogs = $hogs;
2885
	}
2886
	function CanHaveChildren() {
2887
		return false;
2888
	}
2889

    
2890

    
2891
	function copy_queue($interface, &$cflink) {
2892
		$cflink['interface'] = $interface;
2893
		$cflink['qlimit'] = $this->GetQlimit();
2894
		$cflink['priority'] = $this->GetQpriority();
2895
		$cflink['name'] = $this->GetQname();
2896
		$cflink['description'] = $this->GetDescription();
2897
		$cflink['bandwidth'] = $this->GetBandwidth();
2898
		$cflink['bandwidthtype'] = $this->GetBwscale();
2899
		$cflink['enabled'] = $this->GetEnabled();
2900
		$cflink['default'] = $this->GetDefault();
2901
		$cflink['red'] = $this->GetRed();
2902
		$cflink['rio'] = $this->GetRio();
2903
		$cflink['ecn'] = $this->GetEcn();
2904
		$cflink['buckets'] = $this->GetBuckets();
2905
		$cflink['hogs'] = $this->GetHogs();
2906
	}
2907

    
2908
	/*
2909
	 * Should search even its children
2910
	 */
2911
	function &find_queue($interface, $qname) {
2912
		if ($qname == $this->GetQname()) {
2913
			return $this;
2914
		}
2915
	}
2916

    
2917
	function find_parentqueue($interface, $qname) { return; }
2918

    
2919
	function delete_queue() {
2920
		unref_on_altq_queue_list($this->GetQname());
2921
		cleanup_queue_from_rules($this->GetQname());
2922
		unset_object_by_reference($this->GetLink());
2923
	}
2924

    
2925
	function validate_input($data, &$input_errors) {
2926
		parent::validate_input($data, $input_errors);
2927

    
2928
		if ($data['priority'] > 255) {
2929
				$input_errors[] = gettext("Priority must be an integer between 1 and 255.");
2930
		}
2931
		$reqdfields[] = "bandwidth";
2932
		$reqdfieldsn[] = gettext("Bandwidth");
2933
		$reqdfields[] = "bandwidthtype";
2934
		$reqdfieldsn[] = gettext("Bandwidthtype");
2935

    
2936
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2937
	}
2938

    
2939
	function ReadConfig(&$q) {
2940
		parent::ReadConfig($q);
2941
		if (!empty($q['buckets'])) {
2942
			$this->SetBuckets($q['buckets']);
2943
		} else {
2944
			$this->SetBuckets("");
2945
		}
2946
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
2947
			$this->SetHogs($q['hogs']);
2948
		} else {
2949
			$this->SetHogs("");
2950
		}
2951
	}
2952

    
2953
	function build_javascript() {
2954
		return parent::build_javascript();
2955
	}
2956

    
2957
	function build_tree() {
2958
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
2959
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2960
		$tree .= "\" ";
2961
		$tmpvalue = trim($this->GetDefault());
2962
		if (!empty($tmpvalue)) {
2963
			$tree .= " class=\"navlnk\"";
2964
		}
2965
		$tree .= " >" . $this->GetQname() . "</a>";
2966
		$tree .= "</li>";
2967
		return $tree;
2968
	}
2969

    
2970
	/* Even this should take children into consideration */
2971
	function build_rules(&$default = false) {
2972
		$pfq_rule = "queue ". $this->qname;
2973
		if ($this->GetInterface()) {
2974
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2975
		}
2976
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2977
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2978
		}
2979
		$tmpvalue = trim($this->GetQpriority());
2980
		if (!empty($tmpvalue)) {
2981
			$pfq_rule .= " priority " . $this->GetQpriority();
2982
		}
2983
		$tmpvalue = trim($this->GetQlimit());
2984
		if (!empty($tmpvalue)) {
2985
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2986
		}
2987
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
2988
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
2989
			$pfq_rule .= " fairq ( ";
2990
			$tmpvalue = trim($this->GetRed());
2991
			if (!empty($tmpvalue)) {
2992
				$comma = 1;
2993
				$pfq_rule .= " red ";
2994
			}
2995
			$tmpvalue = trim($this->GetCodel());
2996
			if (!empty($tmpvalue)) {
2997
				$comma = 1;
2998
				$pfq_rule .= " codel ";
2999
			}
3000
			$tmpvalue = trim($this->GetRio());
3001
			if (!empty($tmpvalue)) {
3002
				if ($comma) {
3003
					$pfq_rule .= " ,";
3004
				}
3005
				$comma = 1;
3006
				$pfq_rule .= " rio ";
3007
			}
3008
			$tmpvalue = trim($this->GetEcn());
3009
			if (!empty($tmpvalue)) {
3010
				if ($comma) {
3011
					$pfq_rule .= " ,";
3012
				}
3013
				$comma = 1;
3014
				$pfq_rule .= " ecn ";
3015
			}
3016
			$tmpvalue = trim($this->GetDefault());
3017
			if (!empty($tmpvalue)) {
3018
				if ($comma) {
3019
					$pfq_rule .= " ,";
3020
				}
3021
				$comma = 1;
3022
				$pfq_rule .= " default ";
3023
				$default = true;
3024
			}
3025
			$tmpvalue = trim($this->GetBuckets());
3026
			if (!empty($tmpvalue)) {
3027
				if ($comma) {
3028
					$pfq_rule .= ", ";
3029
				}
3030
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3031
			}
3032
			$tmpvalue = trim($this->GetHogs());
3033
			if (!empty($tmpvalue)) {
3034
				if ($comma) {
3035
					$pfq_rule .= ", ";
3036
				}
3037
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3038
			}
3039
			$pfq_rule .= " ) ";
3040
		}
3041

    
3042
		$pfq_rule .= " \n";
3043
		return $pfq_rule;
3044
	}
3045

    
3046
	function build_form() {
3047
		$form = parent::build_form();
3048

    
3049
		$section = new Form_Section('');
3050

    
3051
		$group = new Form_Group('Bandwidth');
3052

    
3053
		$group->add(new Form_Input(
3054
			'bandwidth',
3055
			null,
3056
			'number',
3057
			$this->GetBandwidth()
3058
		));
3059

    
3060
		$group->add(new Form_Select(
3061
			'bandwidthtype',
3062
			null,
3063
			$this->GetBwscale(),
3064
			array('Kb' => 'Kbit/s',
3065
				  'Mb' => 'Mbit/s',
3066
				  'Gb' => 'Gbit/s',
3067
				  'b' => 'Bit/s',
3068
				  '%' => '%')
3069
		));
3070

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

    
3073
		$section->add($group);
3074

    
3075
		$section->addInput(new Form_Input(
3076
			'buckets',
3077
			'Scheduler specific options',
3078
			'text',
3079
			$this->GetBuckets()
3080
		))->setHelp('Number of buckets available');
3081

    
3082
		$section->addInput(new Form_Input(
3083
			'hogs',
3084
			'',
3085
			'text',
3086
			$this->GetHogs()
3087
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3088

    
3089
		$form->add($section);
3090
		return $form;
3091
	}
3092

    
3093
	function update_altq_queue_data(&$data) {
3094
		$this->ReadConfig($data);
3095
	}
3096

    
3097
	function wconfig() {
3098
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3099
		if (!is_array($cflink)) {
3100
			$cflink = array();
3101
		}
3102
		$cflink['interface'] = $this->GetInterface();
3103
		$cflink['qlimit'] = trim($this->GetQlimit());
3104
		if (empty($cflink['qlimit'])) {
3105
			unset($cflink['qlimit']);
3106
		}
3107
		$cflink['priority'] = trim($this->GetQpriority());
3108
		if (empty($cflink['priority'])) {
3109
			unset($cflink['priority']);
3110
		}
3111
		$cflink['name'] = $this->GetQname();
3112
		$cflink['description'] = trim($this->GetDescription());
3113
		if (empty($cflink['description'])) {
3114
			unset($cflink['description']);
3115
		}
3116
		$cflink['bandwidth'] = $this->GetBandwidth();
3117
		$cflink['bandwidthtype'] = $this->GetBwscale();
3118
		$cflink['enabled'] = $this->GetEnabled();
3119
		if (empty($cflink['enabled'])) {
3120
			unset($cflink['enabled']);
3121
		}
3122
		$cflink['default'] = trim($this->GetDefault());
3123
		if (empty($cflink['default'])) {
3124
			unset($cflink['default']);
3125
		}
3126
		$cflink['red'] = trim($this->GetRed());
3127
		if (empty($cflink['red'])) {
3128
			unset($cflink['red']);
3129
		}
3130
		$cflink['rio'] = trim($this->GetRio());
3131
		if (empty($cflink['rio'])) {
3132
			unset($cflink['rio']);
3133
		}
3134
		$cflink['ecn'] = trim($this->GetEcn());
3135
		if (empty($cflink['ecn'])) {
3136
			unset($cflink['ecn']);
3137
		}
3138
		$cflink['codel'] = trim($this->GetCodel());
3139
		if (empty($cflink['codel'])) {
3140
			unset($cflink['codel']);
3141
		}
3142
		$cflink['buckets'] = trim($this->GetBuckets());
3143
		if (empty($cflink['buckets'])) {
3144
			unset($cflink['buckets']);
3145
		}
3146
		$cflink['hogs'] = trim($this->GetHogs());
3147
		if (empty($cflink['hogs'])) {
3148
			unset($cflink['hogs']);
3149
		}
3150
	}
3151
}
3152

    
3153

    
3154
/*
3155
 * dummynet(4) wrappers.
3156
 */
3157

    
3158

    
3159
/*
3160
 * List of respective objects!
3161
 */
3162
$dummynet_pipe_list = array();
3163

    
3164
class dummynet_class {
3165
	var $qname;
3166
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3167
	var $qlimit;
3168
	var $description;
3169
	var $qenabled;
3170
	var $link;
3171
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3172
	var $plr;
3173

    
3174
	var $buckets;
3175
	/* mask parameters */
3176
	var $mask;
3177
	var $noerror;
3178

    
3179
	/* Accessor functions */
3180
	function SetLink($link) {
3181
		$this->link = $link;
3182
	}
3183
	function GetLink() {
3184
		return $this->link;
3185
	}
3186
	function GetMask() {
3187
		if (!isset($this->mask["type"])) {
3188
			$this->mask["type"] = "none";
3189
		}
3190
		return $this->mask;
3191
	}
3192
	function SetMask($mask) {
3193
		$this->mask = $mask;
3194
	}
3195
	function &GetParent() {
3196
		return $this->qparent;
3197
	}
3198
	function SetParent(&$parent) {
3199
		$this->qparent = &$parent;
3200
	}
3201
	function GetEnabled() {
3202
		return $this->qenabled;
3203
	}
3204
	function SetEnabled($value) {
3205
		$this->qenabled = $value;
3206
	}
3207
	function CanHaveChildren() {
3208
		return false;
3209
	}
3210
	function CanBeDeleted() {
3211
		return true;
3212
	}
3213
	function GetQname() {
3214
		return $this->qname;
3215
	}
3216
	function SetQname($name) {
3217
		$this->qname = trim($name);
3218
	}
3219
	function GetQlimit() {
3220
		return $this->qlimit;
3221
	}
3222
	function SetQlimit($limit) {
3223
		$this->qlimit = $limit;
3224
	}
3225
	function GetDescription() {
3226
		return $this->description;
3227
	}
3228
	function SetDescription($str) {
3229
		$this->description = trim($str);
3230
	}
3231
	function GetFirstime() {
3232
		return $this->firsttime;
3233
	}
3234
	function SetFirsttime($number) {
3235
		$this->firsttime = $number;
3236
	}
3237
	function GetBuckets() {
3238
		return $this->buckets;
3239
	}
3240
	function SetBuckets($buckets) {
3241
		$this->buckets = $buckets;
3242
	}
3243
	function SetNumber($number) {
3244
		$this->qnumber = $number;
3245
	}
3246
	function GetNumber() {
3247
		return $this->qnumber;
3248
	}
3249
	function GetPlr() {
3250
		return $this->plr;
3251
	}
3252
	function SetPlr($plr) {
3253
		$this->plr = $plr;
3254
	}
3255

    
3256
	function build_javascript() {
3257
		$javascript .= "<script type=\"text/javascript\">\n";
3258
		$javascript .= "//<![CDATA[\n";
3259
		$javascript .= "function enable_maskbits(enable_over) {\n";
3260
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3261
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3262
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3263
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3264
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3265
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3266
		$javascript .= "} else {\n";
3267
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3268
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3269
		$javascript .= "}}\n";
3270
		$javascript .= "//]]>\n";
3271
		$javascript .= "</script>\n";
3272
		return $javascript;
3273
	}
3274

    
3275
	function validate_input($data, &$input_errors) {
3276
		$reqdfields[] = "bandwidth";
3277
		$reqdfieldsn[] = gettext("Bandwidth");
3278
		/*$reqdfields[] = "burst";
3279
		$reqdfieldsn[] = gettext("Burst"); */
3280
		$reqdfields[] = "bandwidthtype";
3281
		$reqdfieldsn[] = gettext("Bandwidthtype");
3282
		$reqdfields[] = "newname";
3283
		$reqdfieldsn[] = gettext("Name");
3284

    
3285
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3286

    
3287
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3288
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3289
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3290
		}
3291
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3292
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3293
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3294
		}
3295
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3296
			$input_errors[] = gettext("Queue limit must be an integer");
3297
		}
3298
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3299
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3300
		}
3301
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3302
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3303
		}
3304
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3305
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3306
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3307
			}
3308
		}
3309
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3310
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3311
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3312
			}
3313
		}
3314
	}
3315

    
3316
	function build_mask_rules(&$pfq_rule) {
3317
		$mask = $this->GetMask();
3318
		if (!empty($mask['type'])) {
3319
			if ($mask['type'] <> 'none') {
3320
				$pfq_rule .= " mask";
3321
			}
3322
			switch ($mask['type']) {
3323
				case 'srcaddress':
3324
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3325
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3326
					} else {
3327
						$pfq_rule .= " src-ip6 /128";
3328
					}
3329
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3330
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3331
					} else {
3332
						$pfq_rule .= " src-ip 0xffffffff";
3333
					}
3334
					break;
3335
				case 'dstaddress':
3336
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3337
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3338
					} else {
3339
						$pfq_rule .= " dst-ip6 /128";
3340
					}
3341
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3342
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3343
					} else {
3344
						$pfq_rule .= " dst-ip 0xffffffff";
3345
					}
3346
					break;
3347
				default:
3348
					break;
3349
			}
3350
		}
3351
	}
3352

    
3353
}
3354

    
3355
class dnpipe_class extends dummynet_class {
3356
	var $delay;
3357
	var $qbandwidth = array();
3358
	var $qbandwidthtype;
3359

    
3360
		/* This is here to help on form building and building rules/lists */
3361
	var $subqueues = array();
3362

    
3363
	function CanHaveChildren() {
3364
		return true;
3365
	}
3366
	function SetDelay($delay) {
3367
		$this->delay = $delay;
3368
	}
3369
	function GetDelay() {
3370
		return $this->delay;
3371
	}
3372
	function delete_queue() {
3373
		cleanup_dnqueue_from_rules($this->GetQname());
3374
		foreach ($this->subqueues as $q) {
3375
			$q->delete_queue();
3376
		}
3377
		unset_dn_object_by_reference($this->GetLink());
3378
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3379
	}
3380
	function GetBandwidth() {
3381
		return $this->qbandwidth;
3382
	}
3383
	function SetBandwidth($bandwidth) {
3384
		$this->qbandwidth = $bandwidth;
3385
	}
3386
	function GetBurst() {
3387
		return $this->qburst;
3388
	}
3389
	function SetBurst($burst) {
3390
		$this->qburst = $burst;
3391
	}
3392

    
3393
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3394

    
3395
		if (!is_array($this->subqueues)) {
3396
			$this->subqueues = array();
3397
		}
3398

    
3399
		$q =& new dnqueue_class();
3400
		$q->SetLink($path);
3401
		$q->SetEnabled("on");
3402
		$q->SetPipe($this->GetQname());
3403
		$q->SetParent($this);
3404
		$q->ReadConfig($queue);
3405
		$q->validate_input($queue, $input_errors);
3406
		if (count($input_errors)) {
3407
			log_error(sprintf(gettext('SHAPER: Could not create queue %1$s on interface %2$s because: %3$s'), $q->GetQname(), $interface, print_r($input_errors, true)));
3408
			return $q;
3409
		}
3410
		$number = dnqueue_find_nextnumber();
3411
		$q->SetNumber($number);
3412
		$this->subqueues[$q->GetQname()] = &$q;
3413

    
3414
		return $q;
3415
	}
3416

    
3417
	function &get_queue_list(&$q = null) {
3418
		$qlist = array();
3419

    
3420
		$qlist[$this->GetQname()] = $this->GetNumber();
3421
		if (is_array($this->subqueues)) {
3422
			foreach ($this->subqueues as $queue) {
3423
				$queue->get_queue_list($qlist);
3424
			}
3425
		}
3426
		return $qlist;
3427
	}
3428

    
3429
	/*
3430
	 * Should search even its children
3431
	 */
3432
	function &find_queue($pipe, $qname) {
3433
		if ($qname == $this->GetQname()) {
3434
			return $this;
3435
		}
3436
		foreach ($this->subqueues as $q) {
3437
			$result =& $q->find_queue("", $qname);
3438
			if ($result) {
3439
				return $result;
3440
			}
3441
		}
3442
	}
3443

    
3444
	function &find_parentqueue($pipe, $qname) {
3445
		return NULL;
3446
	}
3447

    
3448
	function validate_input($data, &$input_errors) {
3449
		parent::validate_input($data, $input_errors);
3450

    
3451
		$schedule = 0;
3452
		$schedulenone = 0;
3453
		$entries = 0;
3454
		/* XXX: Really no better way? */
3455
		for ($i = 0; $i < 2900; $i++) {
3456
			if (!empty($data["bwsched{$i}"])) {
3457
				if ($data["bwsched{$i}"] != "none") {
3458
					$schedule++;
3459
				} else {
3460
					$schedulenone++;
3461
				}
3462
			}
3463
			if (!empty($data["bandwidth{$i}"])) {
3464
				if (!is_numeric($data["bandwidth{$i}"])) {
3465
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3466
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3467
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3468
				} else {
3469
					$entries++;
3470
				}
3471
			}
3472
		}
3473
		if ($schedule == 0 && $entries > 1) {
3474
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3475
		}
3476
		if ($schedulenone > 0 && $entries > 1) {
3477
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3478
		}
3479
		if ($entries == 0) {
3480
			$input_errors[] = gettext("At least one bw specification is necessary.");
3481
		}
3482
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3483
			$input_errors[] = gettext("Delay must be an integer.");
3484
		}
3485
	}
3486

    
3487
	function ReadConfig(&$q) {
3488
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3489
			$this->SetQname($q['newname']);
3490
		} else if (!empty($q['newname'])) {
3491
			$this->SetQname($q['newname']);
3492
		} else {
3493
			$this->SetQname($q['name']);
3494
		}
3495
		$this->SetNumber($q['number']);
3496

    
3497
		if (!empty($_POST)) {
3498
			$bandwidth = array();
3499
			/* XXX: Really no better way? */
3500
			for ($i = 0; $i < 2900; $i++) {
3501
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3502
					$bw = array();
3503
					$bw['bw'] = $q["bandwidth{$i}"];
3504
					$bw['burst'] = $q["burst{$i}"];
3505
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3506
						$bw['bwscale'] = $q["bwtype{$i}"];
3507
					}
3508
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3509
						$bw['bwsched'] = $q["bwsched{$i}"];
3510
					}
3511
					$bandwidth[] = $bw;
3512
				}
3513
			}
3514
			$this->SetBandwidth($bandwidth);
3515
		}
3516

    
3517
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3518
			$this->SetBandwidth($q['bandwidth']['item']);
3519
			$this->SetBurst($q['burst']['item']);
3520
		}
3521

    
3522
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3523
			$this->SetQlimit($q['qlimit']);
3524
		} else {
3525
			$this->SetQlimit("");
3526
		}
3527
		if (isset($q['mask']) && $q['mask'] <> "") {
3528
			$masktype = $q['mask'];
3529
		} else {
3530
			$masktype = "";
3531
		}
3532
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3533
			$maskbits = $q['maskbits'];
3534
		} else {
3535
			$maskbits = "";
3536
		}
3537
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3538
			$maskbitsv6 = $q['maskbitsv6'];
3539
		} else {
3540
			$maskbitsv6 = "";
3541
		}
3542
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3543
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3544
			$this->SetBuckets($q['buckets']);
3545
		} else {
3546
			$this->SetBuckets("");
3547
		}
3548
		if (isset($q['plr']) && $q['plr'] <> "") {
3549
			$this->SetPlr($q['plr']);
3550
		} else {
3551
			$this->SetPlr("");
3552
		}
3553
		if (isset($q['delay']) && $q['delay'] <> "") {
3554
			$this->SetDelay($q['delay']);
3555
		} else {
3556
			$this->SetDelay(0);
3557
		}
3558
		if (isset($q['description']) && $q['description'] <> "") {
3559
			$this->SetDescription($q['description']);
3560
		} else {
3561
			$this->SetDescription("");
3562
		}
3563
		$this->SetEnabled($q['enabled']);
3564

    
3565
	}
3566

    
3567
	function build_tree() {
3568
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3569
		$tree .= $this->GetQname() . "</a>";
3570
		if (is_array($this->subqueues)) {
3571
			$tree .= "<ul>";
3572
			foreach ($this->subqueues as $q) {
3573
				$tree .= $q->build_tree();
3574
			}
3575
			$tree .= "</ul>";
3576
		}
3577
		$tree .= "</li>";
3578

    
3579
		return $tree;
3580
	}
3581

    
3582
	function build_rules() {
3583
		global $config, $time_based_rules;
3584

    
3585
		if ($this->GetEnabled() == "") {
3586
			return;
3587
		}
3588

    
3589
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3590
		$found = false;
3591
		$bandwidth = $this->GetBandwidth();
3592
		if (is_array($bandwidth)) {
3593
			foreach ($bandwidth as $bw) {
3594
				if ($bw['bwsched'] != "none") {
3595
					$time_based_rules = true;
3596
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3597
						foreach ($config['schedules']['schedule'] as $schedule) {
3598
							if ($bw['bwsched'] == $schedule['name']) {
3599
								if (filter_get_time_based_rule_status($schedule)) {
3600
									$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3601
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3602
										$pfq_rule .= " burst ".trim($bw['burst']);
3603
									}
3604
									$found = true;
3605
									break;
3606
								}
3607
							}
3608
						}
3609
					} else {
3610
						$pfq_rule .= " bw 0";
3611
						$found = true;
3612
						break;
3613
					}
3614
				} else {
3615
					$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3616
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3617
						$pfq_rule .= " burst ".trim($bw['burst']);
3618
					}
3619
					$found = true;
3620
					break;
3621
				}
3622
			}
3623
			if ($found == false) {
3624
				$pfq_rule .= " bw 0";
3625
			}
3626
		} else {
3627
			$pfq_rule .= " bw 0";
3628
		}
3629

    
3630
		if ($this->GetQlimit()) {
3631
			$pfq_rule .= " queue " . $this->GetQlimit();
3632
		}
3633
		if ($this->GetPlr()) {
3634
			$pfq_rule .= " plr " . $this->GetPlr();
3635
		}
3636
		if ($this->GetBuckets()) {
3637
			$pfq_rule .= " buckets " . $this->GetBuckets();
3638
		}
3639
		if ($this->GetDelay()) {
3640
			$pfq_rule .= " delay " . $this->GetDelay();
3641
		}
3642
		$this->build_mask_rules($pfq_rule);
3643

    
3644
		$pfq_rule .= "\n";
3645

    
3646
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3647
			foreach ($this->subqueues as $q) {
3648
				$pfq_rule .= $q->build_rules();
3649
			}
3650
		}
3651
		$pfq_rule .= " \n";
3652

    
3653
		return $pfq_rule;
3654
	}
3655

    
3656
	function update_dn_data(&$data) {
3657
		$this->ReadConfig($data);
3658
	}
3659

    
3660
	function build_javascript() {
3661
		global $g, $config;
3662

    
3663
		$javasr = parent::build_javascript();
3664

    
3665
		//build list of schedules
3666
		$schedules = "<option value='none'>none</option>";
3667
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3668
			foreach ($config['schedules']['schedule'] as $schedule) {
3669
				if ($schedule['name'] <> "") {
3670
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3671
				}
3672
			}
3673
		}
3674
		$bwopt = "";
3675
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3676
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3677
		}
3678

    
3679
		$javasr .= <<<EOD
3680
<script type='text/javascript'>
3681
//<![CDATA[
3682
var addBwRowTo = (function() {
3683

    
3684
	return (function (tableId) {
3685

    
3686
	var table = document.getElementById(tableId);
3687
	var totalrows = table.rows.length -1;
3688

    
3689
	var row = table.insertRow(totalrows + 1);
3690
	var cell1 = row.insertCell(0);
3691
	var cell2 = row.insertCell(1);
3692
	var cell3 = row.insertCell(2);
3693
	var cell4 = row.insertCell(3);
3694

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

    
3700
	});
3701
})();
3702

    
3703
function removeBwRow(el) {
3704
	var d = el.parentNode.parentNode.rowIndex;
3705
	document.getElementById('maintable').deleteRow(d);
3706
}
3707
//]]>
3708
</script>
3709

    
3710
EOD;
3711

    
3712
		return $javasr;
3713
	}
3714

    
3715
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3716
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3717
	// with the javascript in this class
3718
	function build_bwtable() {
3719
		global $config;
3720

    
3721
		$bandwidth = $this->GetBandwidth();
3722
				//build list of schedules
3723
		$schedules = array();
3724
		$schedules[] = "none";//leave none to leave rule enabled all the time
3725
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3726
			foreach ($config['schedules']['schedule'] as $schedule) {
3727
				if ($schedule['name'] != "") {
3728
					$schedules[] = $schedule['name'];
3729
				}
3730
			}
3731
		}
3732

    
3733
		$form = '<div class="table-responsive">';
3734
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3735
		$form .= "<thead><tr>";
3736
		$form .= "<th>Bandwidth</th>";
3737
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3738
		$form .= "<th>Bw type</th>";
3739
		$form .= "<th>Schedule</th>";
3740
		$form .= "<th></th>";
3741
		$form .= "</tr></thead>";
3742
		$form .= "<tbody>";
3743

    
3744
		// If there are no bandwidths defined, make a blank one for convenience
3745
		if (empty($bandwidth)) {
3746
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3747
		}
3748

    
3749
		if (is_array($bandwidth)) {
3750
			foreach ($bandwidth as $bwidx => $bw) {
3751
				$form .= '<tr>';
3752
				$form .= '<td class="col-xs-4">';
3753
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3754
				//$form .= "</td><td width='20%'>";
3755
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3756
				$form .= "</td>";
3757
				$form .= '<td class="col-xs-4">';
3758
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3759

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

    
3763
					if ($bw['bwscale'] == $bwsidx) {
3764
						$form .= " selected";
3765
					}
3766

    
3767
					$form .= ">{$bwscale}</option>";
3768
				}
3769

    
3770
				$form .= "</select>";
3771
				$form .= "</td>";
3772
				$form .= '<td class="col-xs-4">';
3773
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3774

    
3775
				foreach ($schedules as $schd) {
3776
					$selected = "";
3777
					if ($bw['bwsched'] == $schd) {
3778
						$selected = "selected";
3779
					}
3780

    
3781
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3782
				}
3783

    
3784
				$form .= "</select>";
3785
				$form .= "</td>";
3786
				$form .= '<td>';
3787
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
3788
				$form .= "</td></tr>";
3789
			}
3790
		}
3791
		$form .= "</tbody></table></div><br />";
3792

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

    
3797
		return($form);
3798
	}
3799

    
3800
	function build_form() {
3801
		global $g, $config, $pipe, $action, $qname;
3802

    
3803
		//build list of schedules
3804
		$schedules = array();
3805
		$schedules[] = "none";//leave none to leave rule enabled all the time
3806
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3807
			foreach ($config['schedules']['schedule'] as $schedule) {
3808
				if ($schedule['name'] <> "") {
3809
					$schedules[] = $schedule['name'];
3810
				}
3811
			}
3812
		}
3813

    
3814

    
3815
		$sform = new Form();
3816
		$sform->setAction("firewall_shaper.php");
3817

    
3818
		$section = new Form_Section('Limiters');
3819

    
3820
		$section->addInput(new Form_Checkbox(
3821
			'enabled',
3822
			'Enable',
3823
			'Enable limiter and its children',
3824
			($this->GetEnabled() == "on"),
3825
			'on'
3826
		));
3827

    
3828
		$section->addInput(new Form_Input(
3829
			'newname',
3830
			'*Name',
3831
			'text',
3832
			$this->GetQname()
3833
		));
3834

    
3835
		$section->addInput(new Form_Input(
3836
			'name',
3837
			null,
3838
			'hidden',
3839
			$this->GetQname()
3840
		));
3841

    
3842
		if ($this->GetNumber() > 0) {
3843
			$section->addInput(new Form_Input(
3844
				'number',
3845
				null,
3846
				'hidden',
3847
				$this->GetNumber()
3848
			));
3849
		}
3850

    
3851
		$bandwidth = $this->GetBandwidth();
3852

    
3853
		if (is_array($bandwidth)) {
3854
				$section->addInput(new Form_StaticText(
3855
				'Bandwidth',
3856
				$this->build_bwtable()
3857
			));
3858
		}
3859

    
3860
		$mask = $this->GetMask();
3861

    
3862
		$section->addInput(new Form_Select(
3863
			'mask',
3864
			'Mask',
3865
			$mask['type'],
3866
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3867
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3868
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3869
					'This makes it possible to easily specify bandwidth limits per host.');
3870

    
3871
		$group = new Form_Group(null);
3872

    
3873
		$group->add(new Form_Select(
3874
			'maskbits',
3875
			null,
3876
			$mask['bits'],
3877
			array_combine(range(32, 1, -1), range(32, 1, -1))
3878
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
3879

    
3880
		$group->add(new Form_Select(
3881
			'maskbitsv6',
3882
			null,
3883
			$mask['bitsv6'],
3884
			array_combine(range(128, 1, -1), range(128, 1, -1))
3885
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3886

    
3887
		$section->add($group);
3888

    
3889
		$section->addInput(new Form_Input(
3890
			'description',
3891
			'Description',
3892
			'text',
3893
			$this->GetDescription()
3894
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
3895

    
3896
		$sform->add($section);
3897

    
3898
		$section = new Form_Section('Advanced Options');
3899

    
3900
		$section->addInput(new Form_Input(
3901
			'delay',
3902
			'Delay (ms)',
3903
			'text',
3904
			$this->GetDelay() > 0 ? $this->GetDelay():null
3905
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
3906

    
3907
		$section->addInput(new Form_Input(
3908
			'plr',
3909
			'Packet Loss Rate',
3910
			'number',
3911
			$this->GetPlr(),
3912
			['step' => '0.001', 'min' => '0.000']
3913
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
3914
					'A value of 0.001 means one packet in 1000 gets dropped.');
3915

    
3916
		$section->addInput(new Form_Input(
3917
			'qlimit',
3918
			'Queue size (slots)',
3919
			'number',
3920
			$this->GetQlimit()
3921
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
3922
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
3923

    
3924
		$section->addInput(new Form_Input(
3925
			'buckets',
3926
			'Bucket size (slots)',
3927
			'number',
3928
			$this->GetBuckets()
3929
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
3930

    
3931
		$sform->add($section);
3932

    
3933
		return($sform);
3934
		}
3935

    
3936
	function wconfig() {
3937
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
3938
		if (!is_array($cflink)) {
3939
			$cflink = array();
3940
		}
3941
		$cflink['name'] = $this->GetQname();
3942
		$cflink['number'] = $this->GetNumber();
3943
		$cflink['qlimit'] = $this->GetQlimit();
3944
		$cflink['plr'] = $this->GetPlr();
3945
		$cflink['description'] = $this->GetDescription();
3946

    
3947
		$bandwidth = $this->GetBandwidth();
3948
		if (is_array($bandwidth)) {
3949
			$cflink['bandwidth'] = array();
3950
			$cflink['bandwidth']['item'] = array();
3951
			foreach ($bandwidth as $bwidx => $bw) {
3952
				$cflink['bandwidth']['item'][] = $bw;
3953
			}
3954
		}
3955

    
3956
		$cflink['enabled'] = $this->GetEnabled();
3957
		$cflink['buckets'] = $this->GetBuckets();
3958
		$mask = $this->GetMask();
3959
		$cflink['mask'] = $mask['type'];
3960
		$cflink['maskbits'] = $mask['bits'];
3961
		$cflink['maskbitsv6'] = $mask['bitsv6'];
3962
		$cflink['delay'] = $this->GetDelay();
3963
	}
3964

    
3965
}
3966

    
3967
class dnqueue_class extends dummynet_class {
3968
	var $pipeparent;
3969
	var $weight;
3970

    
3971
	function GetWeight() {
3972
		return $this->weight;
3973
	}
3974
	function SetWeight($weight) {
3975
		$this->weight = $weight;
3976
	}
3977
	function GetPipe() {
3978
		return $this->pipeparent;
3979
	}
3980
	function SetPipe($pipe) {
3981
		$this->pipeparent = $pipe;
3982
	}
3983

    
3984
	/* Just a stub in case we ever try to call this from the frontend. */
3985
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3986
		return;
3987
	}
3988

    
3989
	function delete_queue() {
3990
		cleanup_dnqueue_from_rules($this->GetQname());
3991
		unset_dn_object_by_reference($this->GetLink());
3992
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
3993
	}
3994

    
3995
	function validate_input($data, &$input_errors) {
3996
		parent::validate_input($data, $input_errors);
3997

    
3998
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
3999
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4000
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4001
		}
4002
	}
4003

    
4004
	/*
4005
	 * Should search even its children
4006
	 */
4007
	function &find_queue($pipe, $qname) {
4008
		if ($qname == $this->GetQname()) {
4009
			return $this;
4010
		} else {
4011
			return NULL;
4012
		}
4013
	}
4014

    
4015
	function &find_parentqueue($pipe, $qname) {
4016
		return $this->qparent;
4017
	}
4018

    
4019
	function &get_queue_list(&$qlist) {
4020
		if ($this->GetEnabled() == "") {
4021
			return;
4022
		}
4023
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4024
	}
4025

    
4026
	function ReadConfig(&$q) {
4027
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4028
			$this->SetQname($q['newname']);
4029
		} else if (!empty($q['newname'])) {
4030
			$this->SetQname($q['newname']);
4031
		} else {
4032
			$this->SetQname($q['name']);
4033
		}
4034
		$this->SetNumber($q['number']);
4035
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4036
			$this->SetQlimit($q['qlimit']);
4037
		} else {
4038
			$this->SetQlimit("");
4039
		}
4040
		if (isset($q['mask']) && $q['mask'] <> "") {
4041
			$masktype = $q['mask'];
4042
		} else {
4043
			$masktype = "";
4044
		}
4045
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4046
			$maskbits = $q['maskbits'];
4047
		} else {
4048
			$maskbits = "";
4049
		}
4050
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4051
			$maskbitsv6 = $q['maskbitsv6'];
4052
		} else {
4053
			$maskbitsv6 = "";
4054
		}
4055
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4056
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4057
			$this->SetBuckets($q['buckets']);
4058
		} else {
4059
			$this->SetBuckets("");
4060
		}
4061
		if (isset($q['plr']) && $q['plr'] <> "") {
4062
			$this->SetPlr($q['plr']);
4063
		} else {
4064
			$this->SetPlr("");
4065
		}
4066
		if (isset($q['weight']) && $q['weight'] <> "") {
4067
			$this->SetWeight($q['weight']);
4068
		} else {
4069
			$this->SetWeight("");
4070
		}
4071
		if (isset($q['description']) && $q['description'] <> "") {
4072
			$this->SetDescription($q['description']);
4073
		} else {
4074
			$this->SetDescription("");
4075
		}
4076
		$this->SetEnabled($q['enabled']);
4077
	}
4078

    
4079
	function build_tree() {
4080
		$parent =& $this->GetParent();
4081
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4082
		$tree .= $this->GetQname() . "</a>";
4083
		$tree .= "</li>";
4084

    
4085
		return $tree;
4086
	}
4087

    
4088
	function build_rules() {
4089
		if ($this->GetEnabled() == "") {
4090
			return;
4091
		}
4092

    
4093
		$parent =& $this->GetParent();
4094
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4095
		if ($this->GetQlimit()) {
4096
			$pfq_rule .= " queue " . $this->GetQlimit();
4097
		}
4098
		if ($this->GetWeight()) {
4099
			$pfq_rule .= " weight " . $this->GetWeight();
4100
		}
4101
		if ($this->GetBuckets()) {
4102
			$pfq_rule .= " buckets " . $this->GetBuckets();
4103
		}
4104
		$this->build_mask_rules($pfq_rule);
4105
		$pfq_rule .= "\n";
4106

    
4107
		return $pfq_rule;
4108
	}
4109

    
4110
	function build_javascript() {
4111
		return parent::build_javascript();
4112
	}
4113

    
4114
	function build_form() {
4115
		global $g, $config, $pipe, $action, $qname;
4116

    
4117
		//build list of schedules
4118
		$schedules = array();
4119
		$schedules[] = "none";//leave none to leave rule enabled all the time
4120
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4121
			foreach ($config['schedules']['schedule'] as $schedule) {
4122
				if ($schedule['name'] <> "") {
4123
					$schedules[] = $schedule['name'];
4124
				}
4125
			}
4126
		}
4127

    
4128

    
4129
		$sform = new Form();
4130
		$sform->setAction("firewall_shaper.php");
4131
		$section = new Form_Section('Limiters');
4132

    
4133
		$section->addInput(new Form_Checkbox(
4134
			'enabled',
4135
			'Enable',
4136
			'Enable this queue',
4137
			($this->GetEnabled() == "on"),
4138
			'on'
4139
		));
4140

    
4141
		$section->addInput(new Form_Input(
4142
			'newname',
4143
			'*Name',
4144
			'text',
4145
			$this->GetQname()
4146
		));
4147

    
4148
		$section->addInput(new Form_Input(
4149
			'name',
4150
			null,
4151
			'hidden',
4152
			$this->GetQname()
4153
		));
4154

    
4155
		if ($this->GetNumber() > 0) {
4156
			$section->addInput(new Form_Input(
4157
				'number',
4158
				null,
4159
				'hidden',
4160
				$this->GetNumber()
4161
			));
4162
		}
4163

    
4164
		$mask = $this->GetMask();
4165

    
4166
		$section->addInput(new Form_Select(
4167
			'mask',
4168
			'Mask',
4169
			$mask['type'],
4170
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4171
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4172
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4173
					'This makes it possible to easily specify bandwidth limits per host.');
4174

    
4175
		$group = new Form_Group(null);
4176

    
4177
		$group->add(new Form_Select(
4178
			'maskbits',
4179
			null,
4180
			$mask['bits'],
4181
			array_combine(range(32, 1, -1), range(32, 1, -1))
4182
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4183

    
4184
		$group->add(new Form_Select(
4185
			'maskbitsv6',
4186
			null,
4187
			$mask['bitsv6'],
4188
			array_combine(range(128, 1, -1), range(128, 1, -1))
4189
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4190

    
4191
		$section->add($group);
4192

    
4193
		$section->addInput(new Form_Input(
4194
			'description',
4195
			'Description',
4196
			'text',
4197
			$this->GetDescription()
4198
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4199

    
4200
		$sform->add($section);
4201

    
4202
		$section = new Form_Section('Advanced Options');
4203

    
4204
		$section->addInput(new Form_Input(
4205
			'weight',
4206
			'Weight',
4207
			'number',
4208
			$this->GetWeight(),
4209
			['min' => '1', 'max' => '100']
4210
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4211
					' it can be left blank otherwise.');
4212

    
4213
		$section->addInput(new Form_Input(
4214
			'plr',
4215
			'Packet Loss Rate',
4216
			'number',
4217
			$this->GetPlr(),
4218
			['step' => '0.001', 'min' => '0.000']
4219
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4220
					'A value of 0.001 means one packet in 1000 gets dropped');
4221

    
4222
		$section->addInput(new Form_Input(
4223
			'qlimit',
4224
			'Queue size (slots)',
4225
			'number',
4226
			$this->GetQlimit()
4227
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4228
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4229

    
4230
		$section->addInput(new Form_Input(
4231
			'buckets',
4232
			'Bucket size (slots)',
4233
			'number',
4234
			$this->GetBuckets()
4235
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4236

    
4237
		$section->addInput(new Form_Input(
4238
			'pipe',
4239
			null,
4240
			'hidden',
4241
			$this->GetPipe()
4242
		));
4243

    
4244
		$sform->add($section);
4245

    
4246
		return($sform);
4247
	}
4248

    
4249
	function update_dn_data(&$data) {
4250
		$this->ReadConfig($data);
4251
	}
4252

    
4253
	function wconfig() {
4254
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4255
		if (!is_array($cflink)) {
4256
			$cflink = array();
4257
		}
4258
		$cflink['name'] = $this->GetQname();
4259
		$cflink['number'] = $this->GetNumber();
4260
		$cflink['qlimit'] = $this->GetQlimit();
4261
		$cflink['description'] = $this->GetDescription();
4262
		$cflink['weight'] = $this->GetWeight();
4263
		$cflink['enabled'] = $this->GetEnabled();
4264
		$cflink['buckets'] = $this->GetBuckets();
4265
		$mask = $this->GetMask();
4266
		$cflink['mask'] = $mask['type'];
4267
		$cflink['maskbits'] = $mask['bits'];
4268
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4269
	}
4270
}
4271

    
4272
function get_dummynet_name_list() {
4273

    
4274
	$dn_name_list =& get_unique_dnqueue_list();
4275
	$dn_name = array();
4276
	if (is_array($dn_name_list)) {
4277
		foreach ($dn_name_list as $key => $value) {
4278
			$dn_name[] = $key;
4279
		}
4280
	}
4281

    
4282
	return $dn_name;
4283

    
4284
}
4285

    
4286
function get_altq_name_list() {
4287
	$altq_name_list =& get_unique_queue_list();
4288
	$altq_name = array();
4289
	if (is_array($altq_name_list)) {
4290
		foreach ($altq_name_list as $key => $aqobj) {
4291
			$altq_name[] = $key;
4292
		}
4293
	}
4294

    
4295
	return $altq_name;
4296
}
4297

    
4298
/*
4299
 * XXX: TODO Make a class shaper to hide all these functions
4300
 * from the global namespace.
4301
 */
4302

    
4303
/*
4304
 * This is a layer violation but for now there is no way
4305
 * I can find to properly do this with PHP.
4306
 */
4307
function altq_get_default_queue($interface) {
4308
	global $altq_list_queues;
4309

    
4310
	$altq_tmp = $altq_list_queues[$interface];
4311
	if ($altq_tmp) {
4312
		return $altq_tmp->GetDefaultQueuePresent();
4313
	} else {
4314
		return false;
4315
	}
4316
}
4317

    
4318
function altq_check_default_queues() {
4319
	global $altq_list_queues;
4320

    
4321
	$count = 0;
4322
	if (is_array($altq_list_queues)) {
4323
		foreach ($altq_list_queues as $altq) {
4324
			if ($altq->GetDefaultQueuePresent()) {
4325
				$count++;
4326
			}
4327
		}
4328
	}
4329
	else {
4330
		$count++;
4331
	}
4332

    
4333
	return 0;
4334
}
4335

    
4336
function &get_unique_queue_list() {
4337
	global $altq_list_queues;
4338

    
4339
	$qlist = array();
4340
	if (is_array($altq_list_queues)) {
4341
		foreach ($altq_list_queues as $altq) {
4342
			if ($altq->GetEnabled() == "") {
4343
				continue;
4344
			}
4345
			$tmplist =& $altq->get_queue_list();
4346
			foreach ($tmplist as $qname => $link) {
4347
				if ($link->GetEnabled() <> "") {
4348
					$qlist[$qname] = $link;
4349
				}
4350
			}
4351
		}
4352
	}
4353
	return $qlist;
4354
}
4355

    
4356
function &get_unique_dnqueue_list() {
4357
	global $dummynet_pipe_list;
4358

    
4359
	$qlist = array();
4360
	if (is_array($dummynet_pipe_list)) {
4361
		foreach ($dummynet_pipe_list as $dn) {
4362
			if ($dn->GetEnabled() == "") {
4363
				continue;
4364
			}
4365
			$tmplist =& $dn->get_queue_list();
4366
			foreach ($tmplist as $qname => $link) {
4367
				$qlist[$qname] = $link;
4368
			}
4369
		}
4370
	}
4371
	return $qlist;
4372
}
4373

    
4374
function ref_on_altq_queue_list($parent, $qname) {
4375
	if (isset($GLOBALS['queue_list'][$qname])) {
4376
		$GLOBALS['queue_list'][$qname]++;
4377
	} else {
4378
		$GLOBALS['queue_list'][$qname] = 1;
4379
	}
4380

    
4381
	unref_on_altq_queue_list($parent);
4382
}
4383

    
4384
function unref_on_altq_queue_list($qname) {
4385
	$GLOBALS['queue_list'][$qname]--;
4386
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4387
		unset($GLOBALS['queue_list'][$qname]);
4388
	}
4389
}
4390

    
4391
function read_altq_config() {
4392
	global $altq_list_queues, $config;
4393
	$path = array();
4394

    
4395
	if (!is_array($config['shaper'])) {
4396
		$config['shaper'] = array();
4397
	}
4398
	if (!is_array($config['shaper']['queue'])) {
4399
		$config['shaper']['queue'] = array();
4400
	}
4401
	$a_int = &$config['shaper']['queue'];
4402

    
4403
	$altq_list_queues = array();
4404

    
4405
	if (!is_array($config['shaper']['queue'])) {
4406
		return;
4407
	}
4408

    
4409
	foreach ($a_int as $key => $conf) {
4410
		$int = $conf['interface'];
4411
		$root =& new altq_root_queue();
4412
		$root->SetInterface($int);
4413
		$altq_list_queues[$root->GetInterface()] = &$root;
4414
		$root->ReadConfig($conf);
4415
		array_push($path, $key);
4416
		$root->SetLink($path);
4417
		if (is_array($conf['queue'])) {
4418
			foreach ($conf['queue'] as $key1 => $q) {
4419
				array_push($path, $key1);
4420
				/*
4421
				 * XXX: we completely ignore errors here but anyway we must have
4422
				 *	checked them before so no harm should be come from this.
4423
				 */
4424
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4425
				array_pop($path);
4426
			}
4427
		}
4428
		array_pop($path);
4429
	}
4430
}
4431

    
4432
function read_dummynet_config() {
4433
	global $dummynet_pipe_list, $config;
4434
	$path = array();
4435

    
4436
	if (!is_array($config['dnshaper'])) {
4437
		$config['dnshaper'] = array();
4438
	}
4439
	if (!is_array($config['dnshaper']['queue'])) {
4440
		$config['dnshaper']['queue'] = array();
4441
	}
4442
	$a_int = &$config['dnshaper']['queue'];
4443

    
4444
	$dummynet_pipe_list = array();
4445

    
4446
	if (!is_array($config['dnshaper']['queue']) ||
4447
	    !count($config['dnshaper']['queue'])) {
4448
		return;
4449
	}
4450

    
4451
	foreach ($a_int as $key => $conf) {
4452
		if (empty($conf['name'])) {
4453
			continue; /* XXX: grrrrrr at php */
4454
		}
4455
		$root =& new dnpipe_class();
4456
		$root->ReadConfig($conf);
4457
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4458
		array_push($path, $key);
4459
		$root->SetLink($path);
4460
		if (is_array($conf['queue'])) {
4461
			foreach ($conf['queue'] as $key1 => $q) {
4462
				array_push($path, $key1);
4463
				/*
4464
				 * XXX: we completely ignore errors here but anyway we must have
4465
				 *	checked them before so no harm should be come from this.
4466
				 */
4467
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4468
				array_pop($path);
4469
			}
4470
		}
4471
		array_pop($path);
4472
	}
4473
}
4474

    
4475
function get_interface_list_to_show() {
4476
	global $altq_list_queues, $config;
4477
	global $shaperIFlist;
4478

    
4479
	$tree = "";
4480
	foreach ($shaperIFlist as $shif => $shDescr) {
4481
		if ($altq_list_queues[$shif]) {
4482
			continue;
4483
		} else {
4484
			if (!is_altq_capable(get_real_interface($shif))) {
4485
				continue;
4486
			}
4487
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4488
		}
4489
	}
4490

    
4491
	return $tree;
4492
}
4493

    
4494
function filter_generate_altq_queues() {
4495
	global $altq_list_queues;
4496

    
4497
	read_altq_config();
4498

    
4499
	$altq_rules = "";
4500
	foreach ($altq_list_queues as $altq) {
4501
		$altq_rules .= $altq->build_rules();
4502
	}
4503

    
4504
	return $altq_rules;
4505
}
4506

    
4507
function dnqueue_find_nextnumber() {
4508
	global $dummynet_pipe_list;
4509

    
4510
	$dnused = array();
4511
	if (is_array($dummynet_pipe_list)) {
4512
		foreach ($dummynet_pipe_list as $dn) {
4513
			$tmplist =& $dn->get_queue_list();
4514
			foreach ($tmplist as $qname => $link) {
4515
				if ($link[0] == "?") {
4516
					$dnused[$qname] = substr($link, 1);
4517
				}
4518
			}
4519
		}
4520
	}
4521

    
4522
	sort($dnused, SORT_NUMERIC);
4523
	$dnnumber = 0;
4524
	$found = false;
4525
	foreach ($dnused as $dnnum) {
4526
		if (($dnnum - $dnnumber) > 1) {
4527
			$dnnumber = $dnnum - 1;
4528
			$found = true;
4529
			break;
4530
		} else {
4531
			$dnnumber = $dnnum;
4532
		}
4533
	}
4534

    
4535
	if ($found == false) {
4536
		$dnnumber++;
4537
	}
4538

    
4539
	unset($dnused, $dnnum, $found);
4540
	return $dnnumber;
4541
}
4542

    
4543
function dnpipe_find_nextnumber() {
4544
	global $dummynet_pipe_list;
4545

    
4546
	$dnused = array();
4547
	foreach ($dummynet_pipe_list as $dn) {
4548
		$dnused[] = $dn->GetNumber();
4549
	}
4550

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

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

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

    
4572
function filter_generate_dummynet_rules() {
4573
	global $g, $dummynet_pipe_list;
4574

    
4575
	read_dummynet_config();
4576

    
4577
	$dn_rules = "";
4578
	$max_qlimit = "100"; // OS default
4579
	foreach ($dummynet_pipe_list as $dn) {
4580
		$dn_rules .= $dn->build_rules();
4581
		$this_qlimit = $dn->GetQlimit();
4582
		if ($this_qlimit > $max_qlimit) {
4583
			$max_qlimit = $this_qlimit;
4584
		}
4585
	}
4586
	if (!is_numericint($max_qlimit)) {
4587
		$max_qlimit = "100";
4588
	}
4589
	if (!empty($dn_rules)) {
4590
		if (!is_module_loaded("dummynet.ko")) {
4591
			mwexec("/sbin/kldload dummynet");
4592
		}
4593
		set_sysctl(array(
4594
				"net.inet.ip.dummynet.io_fast" => "1",
4595
				"net.inet.ip.dummynet.hash_size" => "256",
4596
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
4597
		));
4598
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4599
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4600
	}
4601
}
4602

    
4603
function build_iface_without_this_queue($iface, $qname) {
4604
	global $g, $altq_list_queues;
4605
	global $shaperIFlist;
4606

    
4607
	$altq =& $altq_list_queues[$iface];
4608

    
4609
	if ($altq) {
4610
		$scheduler = $altq->GetScheduler();
4611
	}
4612

    
4613
	$form = '<dl class="dl-horizontal">';
4614

    
4615
	$form .= '	<dt>';
4616
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4617
	$form .= '	</dt>';
4618
	$form .= '	<dd>';
4619
	$form .=		$scheduler;
4620
	$form .= '	</dd>';
4621

    
4622
	$form .= '	<dt>';
4623
	$form .= 'Clone';
4624
	$form .= '	</dt>';
4625
	$form .= '	<dd>';
4626
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4627
	$form .= $iface . '&amp;queue=';
4628
	$form .= $qname . '&amp;action=add">';
4629
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
4630
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
4631
	$form .= '	</dd>';
4632

    
4633
	$form .= '</dl>';
4634

    
4635
	return $form;
4636

    
4637
}
4638

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

    
4642
$shaper_msg = gettext("The tree on the left navigates through the %s.");
4643
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
4644
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
4645

    
4646
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
4647
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
4648
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
4649

    
4650
// Check to see if the specified interface has a queue configured
4651
function interface_has_queue($if) {
4652
	global $config;
4653

    
4654
	if ($config['shaper']) {
4655
		foreach ($config['shaper']['queue'] as $queue) {
4656
			if ($queue['interface'] === $if) {
4657
				return true;
4658
			}
4659
		}
4660
	}
4661

    
4662
	return false;
4663
}
4664
?>
(42-42/54)