Project

General

Profile

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

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

    
1007
		if ($bwtype != "%") {
1008
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1009

    
1010
			if ($bw > 0) {
1011
				$sum += get_bandwidth($bw, $bwtype, $parent);
1012
			}
1013

    
1014
			if ($sum > get_queue_bandwidth($parent)) {
1015
				return 1;
1016
			}
1017
		}
1018

    
1019
		foreach ($this->subqueues as $q) {
1020
			if ($q->CheckBandwidth(0, '')) {
1021
				return 1;
1022
			}
1023
		}
1024

    
1025
		return 0;
1026
	}
1027
	function GetTotalBw($qignore = NULL) {
1028
		$sum = 0;
1029
		foreach ($this->subqueues as $q) {
1030
			if ($qignore != NULL && $qignore == $q)
1031
				continue;
1032
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1033
		}
1034

    
1035
		return $sum;
1036
	}
1037
	function GetBwscale() {
1038
		return $this->qbandwidthtype;
1039
	}
1040
	function SetBwscale($scale) {
1041
		$this->qbandwidthtype = $scale;
1042
	}
1043
	function GetDefaultQueuePresent() {
1044
		if ($this->GetDefault()) {
1045
			return true;
1046
		}
1047
		if (!empty($this->subqueues)) {
1048
			foreach ($this->subqueues as $q) {
1049
				if ($q->GetDefault()) {
1050
					return true;
1051
				}
1052
			}
1053
		}
1054

    
1055
		return false;
1056
	}
1057
	function GetDefault() {
1058
		return $this->qdefault;
1059
	}
1060
	function SetDefault($value = false) {
1061
		$this->qdefault = $value;
1062
	}
1063
	function GetCodel() {
1064
		return $this->codel;
1065
	}
1066
	function SetCodel($codel = false) {
1067
		$this->codel = $codel;
1068
	}
1069
	function GetRed() {
1070
		return $this->qred;
1071
	}
1072
	function SetRed($red = false) {
1073
		$this->qred = $red;
1074
	}
1075
	function GetRio() {
1076
		return $this->qrio;
1077
	}
1078
	function SetRio($rio = false) {
1079
		$this->qrio = $rio;
1080
	}
1081
	function GetEcn() {
1082
		return $this->qecn;
1083
	}
1084
	function SetEcn($ecn = false) {
1085
		$this->qecn = $ecn;
1086
	}
1087
	function GetAck() {
1088
		return $this->qack;
1089
	}
1090
	function SetAck($ack = false) {
1091
		$this->qack = $ack;
1092
	}
1093

    
1094
	function GetBwscaleText() {
1095
		switch ($this->qbandwidthtype) {
1096
			case "b":
1097
				$bwscaletext = "Bit/s";
1098
				break;
1099
			case "Kb":
1100
				$bwscaletext = "Kbit/s";
1101
				break;
1102
			case "Mb":
1103
				$bwscaletext = "Mbit/s";
1104
				break;
1105
			case "Gb":
1106
				$bwscaletext = "Gbit/s";
1107
				break;
1108
			default:
1109
				/* For others that do not need translating like % */
1110
				$bwscaletext = $this->qbandwidthtype;
1111
				break;
1112
		}
1113
		return $bwscaletext;
1114
	}
1115

    
1116
	function build_javascript() {
1117
		$javascript = "<script type=\"text/javascript\">";
1118
		$javascript .= "//<![CDATA[\n";
1119
		$javascript .= "function mySuspend() { \n";
1120
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1121
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1122
		$javascript .= "else if (document.all)\n";
1123
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1124
		$javascript .= "}\n";
1125

    
1126
		$javascript .= "function myResume() {\n";
1127
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1128
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1129
		$javascript .= "else if (document.all)\n";
1130
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1131
		$javascript .= "}\n";
1132
		$javascript .= "//]]>";
1133
		$javascript .= "</script>";
1134

    
1135
		return $javascript;
1136
	}
1137

    
1138
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1139

    
1140
	/*
1141
	 * Currently this will not be called unless we decide to clone a whole
1142
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1143
	 */
1144
	function copy_queue($interface, &$cflink) {
1145

    
1146
		$cflink['name'] = $this->GetQname();
1147
		$cflink['interface'] = $interface;
1148
		$cflink['qlimit'] = $this->GetQlimit();
1149
		$cflink['priority'] = $this->GetQpriority();
1150
		$cflink['description'] = $this->GetDescription();
1151
		$cflink['enabled'] = $this->GetEnabled();
1152
		$cflink['default'] = $this->GetDefault();
1153
		$cflink['red'] = $this->GetRed();
1154
		$cflink['codel'] = $this->GetCodel();
1155
		$cflink['rio'] = $this->GetRio();
1156
		$cflink['ecn'] = $this->GetEcn();
1157

    
1158
		if (is_array($this->subqueues)) {
1159
			$cflinkp['queue'] = array();
1160
			foreach ($this->subqueues as $q) {
1161
				$cflink['queue'][$q->GetQname()] = array();
1162
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1163
			}
1164
		}
1165
	}
1166

    
1167
	function clean_queue($sched) {
1168
		clean_child_queues($sched, $this->GetLink());
1169
		if (is_array($this->subqueues)) {
1170
			foreach ($this->subqueues as $q) {
1171
				$q->clean_queue($sched);
1172
			}
1173
		}
1174
	}
1175

    
1176
	function &get_queue_list(&$qlist) {
1177

    
1178
		$qlist[$this->GetQname()] = & $this;
1179
		if (is_array($this->subqueues)) {
1180
			foreach ($this->subqueues as $queue) {
1181
				$queue->get_queue_list($qlist);
1182
			}
1183
		}
1184
	}
1185

    
1186
	function delete_queue() {
1187
		unref_on_altq_queue_list($this->GetQname());
1188
		cleanup_queue_from_rules($this->GetQname());
1189
		unset_object_by_reference($this->GetLink());
1190
	}
1191

    
1192
	function delete_all() {
1193
		if (count($this->subqueues)) {
1194
			foreach ($this->subqueues as $q) {
1195
				$q->delete_all();
1196
				unset_object_by_reference($q->GetLink());
1197
				unset($q);
1198
			}
1199
			unset($this->subqueues);
1200
		}
1201
	}
1202

    
1203
	function &find_queue($interface, $qname) {
1204
		if ($qname == $this->GetQname()) {
1205
			return $this;
1206
		}
1207
	}
1208

    
1209
	function find_parentqueue($interface, $qname) { return; }
1210

    
1211
	function validate_input($data, &$input_errors) {
1212

    
1213
		$reqdfields[] = "name";
1214
		$reqdfieldsn[] = gettext("Name");
1215
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1216

    
1217
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1218
			$input_errors[] = gettext("Bandwidth must be an integer.");
1219
		}
1220
		if ($data['bandwidth'] < 0) {
1221
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1222
		}
1223
		if ($data['bandwidthtype'] == "%") {
1224
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1225
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1226
			}
1227
		}
1228
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1229
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1230

    
1231
		if (isset($data['priority']) && (!is_numeric($data['priority']) ||
1232
		    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1233
			$input_errors[] = gettext("The priority must be an integer between 1 and 15.");
1234
		}
1235
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1236
				$input_errors[] = gettext("Queue limit must be an integer");
1237
		}
1238
		if ($data['qlimit'] < 0) {
1239
				$input_errors[] = gettext("Queue limit must be positive");
1240
		}
1241
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1242
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1243
		}
1244
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1245
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1246
		}
1247
		$default = $this->GetDefault();
1248
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1249
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1250
		}
1251
	}
1252

    
1253
	function ReadConfig(&$q) {
1254
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1255
			$this->SetQname($q['newname']);
1256
		} else if (!empty($q['newname'])) {
1257
			$this->SetQname($q['newname']);
1258
		} else if (isset($q['name'])) {
1259
			$this->SetQname($q['name']);
1260
		}
1261
		if (isset($q['interface'])) {
1262
			$this->SetInterface($q['interface']);
1263
		}
1264
		$this->SetBandwidth($q['bandwidth']);
1265
		if ($q['bandwidthtype'] <> "") {
1266
			$this->SetBwscale($q['bandwidthtype']);
1267
		}
1268
		if (!empty($q['qlimit'])) {
1269
			$this->SetQlimit($q['qlimit']);
1270
		} else {
1271
			$this->SetQlimit(""); // Default
1272
		}
1273
		if (is_numeric($q['priority'])) {
1274
			$this->SetQPriority($q['priority']);
1275
		} else {
1276
			$this->SetQpriority("");
1277
		}
1278
		if (!empty($q['description'])) {
1279
			$this->SetDescription($q['description']);
1280
		} else {
1281
			$this->SetDescription("");
1282
		}
1283
		if (!empty($q['red'])) {
1284
			$this->SetRed($q['red']);
1285
		} else {
1286
			$this->SetRed();
1287
		}
1288
		if (!empty($q['codel'])) {
1289
			$this->SetCodel($q['codel']);
1290
		} else {
1291
			$this->SetCodel();
1292
		}
1293
		if (!empty($q['rio'])) {
1294
			$this->SetRio($q['rio']);
1295
		} else {
1296
			$this->SetRio();
1297
		}
1298
		if (!empty($q['ecn'])) {
1299
			$this->SetEcn($q['ecn']);
1300
		} else {
1301
			$this->SetEcn();
1302
		}
1303
		if (!empty($q['default'])) {
1304
			$this->SetDefault($q['default']);
1305
		} else {
1306
			$this->SetDefault();
1307
		}
1308
		if (!empty($q['enabled'])) {
1309
			$this->SetEnabled($q['enabled']);
1310
		} else {
1311
			$this->SetEnabled("");
1312
		}
1313
	}
1314

    
1315
	function build_tree() {
1316
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". $this->GetQname()."&amp;action=show";
1317
		$tree .= "\" ";
1318
		$tmpvalue = $this->GetDefault();
1319
		if (!empty($tmpvalue)) {
1320
			$tree .= " class=\"navlnk\"";
1321
		}
1322
		$tree .= " >" . $this->GetQname() . "</a>";
1323
		/*
1324
		 * Not needed here!
1325
		 * if (is_array($queues) {
1326
		 *	  $tree .= "<ul>";
1327
		 *	  foreach ($q as $queues)
1328
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1329
		 *	  endforeach
1330
		 *	  $tree .= "</ul>";
1331
		 * }
1332
		 */
1333

    
1334
		$tree .= "</li>";
1335

    
1336
		return $tree;
1337
	}
1338

    
1339
	/* Should return something like:
1340
	 * queue $qname on $qinterface bandwidth ....
1341
	 */
1342
	function build_rules(&$default = false) {
1343
		$pfq_rule = " queue ". $this->qname;
1344
		if ($this->GetInterface()) {
1345
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1346
		}
1347
		$tmpvalue = $this->GetQpriority();
1348
		if (is_numeric($tmpvalue)) {
1349
			$pfq_rule .= " priority ".$this->GetQpriority();
1350
		}
1351
		$tmpvalue = $this->GetQlimit();
1352
		if (!empty($tmpvalue)) {
1353
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1354
		}
1355
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1356
			$pfq_rule .= " priq ( ";
1357
			$tmpvalue = $this->GetRed();
1358
			if (!empty($tmpvalue)) {
1359
				$comma = 1;
1360
				$pfq_rule .= " red ";
1361
			}
1362
			$tmpvalue = $this->GetRio();
1363
			if (!empty($tmpvalue)) {
1364
				if ($comma) {
1365
					$pfq_rule .= " ,";
1366
				}
1367
				$comma = 1;
1368
				$pfq_rule .= " rio ";
1369
			}
1370
			$tmpvalue = $this->GetEcn();
1371
			if (!empty($tmpvalue)) {
1372
				if ($comma) {
1373
					$pfq_rule .= " ,";
1374
				}
1375
				$comma = 1;
1376
				$pfq_rule .= " ecn ";
1377
			}
1378
			$tmpvalue = $this->GetCodel();
1379
			if (!empty($tmpvalue)) {
1380
				if ($comma) {
1381
					$pfq_rule .= " ,";
1382
				}
1383
				$comma = 1;
1384
				$pfq_rule .= " codel ";
1385
			}
1386
			$tmpvalue = $this->GetDefault();
1387
			if (!empty($tmpvalue)) {
1388
				if ($comma) {
1389
					$pfq_rule .= " ,";
1390
				}
1391
				$pfq_rule .= " default ";
1392
				$default = true;
1393
			}
1394
			$pfq_rule .= " ) ";
1395
		}
1396

    
1397
		$pfq_rule .= " \n";
1398

    
1399
		return $pfq_rule;
1400
	}
1401

    
1402
	/*
1403
	 * To return the html form to show to user
1404
	 * for getting the parameters.
1405
	 * Should do even for first time when the
1406
	 * object is created and later when we may
1407
	 * need to update it. (2)
1408
	 */
1409

    
1410
	function build_form() {
1411

    
1412
		$sform = new Form();
1413

    
1414
		$sform->setAction("firewall_shaper.php");
1415

    
1416
		$section = new Form_Section("");
1417

    
1418
		$section->addInput(new Form_Checkbox(
1419
			'enabled',
1420
			'Enable/Disable',
1421
			'Enable/disable discipline and its children',
1422
			($this->GetEnabled() == "on"),
1423
			'on'
1424
		));
1425

    
1426
		$section->addInput(new Form_Input(
1427
			'newname',
1428
			'*Name',
1429
			'text',
1430
			$this->GetQname()
1431
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1432

    
1433
		$section->addInput(new Form_Input(
1434
			'name',
1435
			null,
1436
			'hidden',
1437
			$this->GetQname()
1438
		));
1439

    
1440
		if (!is_a($this, "hfsc_queue")) {
1441
			$section->addInput(new Form_Input(
1442
				'priority',
1443
				'Priority',
1444
				'number',
1445
				$this->GetQpriority(),
1446
				['min' => '0', 'max'=> '']
1447
			))->setHelp('For cbq and fairq the range is 0 to 7. The default is 1. For priq the range is 0 to 15, queues with a higher priority are preferred in the case of overload.');
1448
		}
1449

    
1450
		$section->addInput(new Form_Input(
1451
			'qlimit',
1452
			'Queue Limit',
1453
			'number',
1454
			$this->GetQlimit()
1455
		))->setHelp('Queue limit in packets.');
1456

    
1457
		$group = new Form_Group('Scheduler options');
1458

    
1459
		if (empty($this->subqueues)) {
1460
			$group->add(new Form_Checkbox(
1461
				'default',
1462
				null,
1463
				null,
1464
				$this->GetDefault(),
1465
				'default'
1466
			))->setHelp('Default Queue');
1467
		}
1468

    
1469
		$group->add(new Form_Checkbox(
1470
			'red',
1471
			null,
1472
			null,
1473
			!empty($this->GetRed())
1474
		))->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>');
1475

    
1476
		$group->add(new Form_Checkbox(
1477
			'rio',
1478
			null,
1479
			null,
1480
			!empty($this->GetRio())
1481
		))->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>');
1482

    
1483
		$group->add(new Form_Checkbox(
1484
			'ecn',
1485
			null,
1486
			null,
1487
			!empty($this->GetEcn())
1488
		))->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>');
1489

    
1490
		$group->add(new Form_Checkbox(
1491
			'codel',
1492
			null,
1493
			null,
1494
			!empty($this->GetCodel())
1495
		))->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>');
1496

    
1497
		$group->setHelp('Select options for this queue');
1498

    
1499
		$section->add($group);
1500

    
1501
		$section->addInput(new Form_Input(
1502
			'description',
1503
			'Description',
1504
			'text',
1505
			$this->GetDescription()
1506
		));
1507

    
1508
		$sform->add($section);
1509

    
1510
		$sform->addGlobal(new Form_Input(
1511
			'interface',
1512
			null,
1513
			'hidden',
1514
			$this->GetInterface()
1515
		));
1516

    
1517
		$sform->addGlobal(new Form_Input(
1518
			'name',
1519
			null,
1520
			'hidden',
1521
			$this->GetQname()
1522
		));
1523

    
1524
		return($sform);
1525
	}
1526

    
1527
	function build_shortform() {
1528
		/* XXX: Hacks in sight. Mostly layer violations!  */
1529
		global $g, $altq_list_queues;
1530
		global $shaperIFlist;
1531

    
1532
		$altq =& $altq_list_queues[$this->GetInterface()];
1533

    
1534
		if ($altq) {
1535
			$scheduler = $altq->GetScheduler();
1536
		}
1537

    
1538
		$form = '<dl class="dl-horizontal">';
1539
		$form .= '	<dt>';
1540
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1541
		$form .= '	</dt>';
1542
		$form .= '	<dd>';
1543
		$form .=		$scheduler;
1544
		$form .= '	</dd>';
1545

    
1546
		$form .= '	<dt>';
1547
		$form .=		'Bandwidth';
1548
		$form .= '	</dt>';
1549
		$form .= '	<dd>';
1550
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1551
		$form .= '	</dd>';
1552

    
1553
		$tmpvalue = $this->GetQpriority();
1554
		if (!empty($tmpvalue)) {
1555
			$form .= '	<dt>';
1556
			$form .=		'Priority';
1557
			$form .= '	<dt>';
1558
			$form .= '	<dd>';
1559
			$form .=		'On';
1560
			$form .= '	</dd>';
1561
		}
1562

    
1563
		$tmpvalue = $this->GetDefault();
1564
		if (!empty($tmpvalue)) {
1565
			$form .= '	<dt>';
1566
			$form .=		'Default';
1567
			$form .= '	<dt>';
1568
			$form .= '	<dd>';
1569
			$form .=		'On';
1570
			$form .= '	</dd>';
1571
		}
1572

    
1573
			$form .= '	<dt>';
1574
			$form .= 'Delete';
1575
			$form .= '	<dt>';
1576
			$form .= '	<dd>';
1577

    
1578
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1579
			$form .= $this->GetInterface() . '&amp;queue=';
1580
			$form .= $this->GetQname() . '&amp;action=delete">';
1581
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1582
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1583

    
1584
			$form .= '	</dd>';
1585

    
1586
			$form .= '</dl>';
1587

    
1588
		return $form;
1589

    
1590
	}
1591

    
1592
	function update_altq_queue_data(&$q) {
1593
		$this->ReadConfig($q);
1594
	}
1595

    
1596
	function wconfig() {
1597
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1598
		if (!is_array($cflink)) {
1599
			$cflink = array();
1600
		}
1601
		$cflink['name'] = $this->GetQname();
1602
		$cflink['interface'] = $this->GetInterface();
1603
		$cflink['qlimit'] = trim($this->GetQlimit());
1604
		if (empty($cflink['qlimit'])) {
1605
			unset($cflink['qlimit']);
1606
		}
1607
		$cflink['priority'] = trim($this->GetQpriority());
1608
		if (!is_numeric($cflink['priority'])) {
1609
			unset($cflink['priority']);
1610
		}
1611
		$cflink['description'] = trim($this->GetDescription());
1612
		if (empty($cflink['description'])) {
1613
			unset($cflink['description']);
1614
		}
1615
		$cflink['enabled'] = trim($this->GetEnabled());
1616
		if (empty($cflink['enabled'])) {
1617
			unset($cflink['enabled']);
1618
		}
1619
		$cflink['default'] = trim($this->GetDefault());
1620
		if (empty($cflink['default'])) {
1621
			unset($cflink['default']);
1622
		}
1623
		$cflink['red'] = trim($this->GetRed());
1624
		if (empty($cflink['red'])) {
1625
			unset($cflink['red']);
1626
		}
1627
		$cflink['codel'] = trim($this->GetCodel());
1628
		if (empty($cflink['codel'])) {
1629
			unset($cflink['codel']);
1630
		}
1631
		$cflink['rio'] = trim($this->GetRio());
1632
		if (empty($cflink['rio'])) {
1633
			unset($cflink['rio']);
1634
		}
1635
		$cflink['ecn'] = trim($this->GetEcn());
1636
		if (empty($cflink['ecn'])) {
1637
			unset($cflink['ecn']);
1638
		}
1639
	}
1640
}
1641

    
1642
class hfsc_queue extends priq_queue {
1643
	/* realtime */
1644
	var $realtime;
1645
	var $r_m1;
1646
	var $r_d;
1647
	var $r_m2;
1648
	/* linkshare */
1649
	var $linkshare;
1650
	var $l_m1;
1651
	var $l_d;
1652
	var $l_m2;
1653
	/* upperlimit */
1654
	var $upperlimit;
1655
	var $u_m1;
1656
	var $u_d;
1657
	var $u_m2;
1658

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

    
1747
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
1748

    
1749
		if (!is_array($this->subqueues)) {
1750
			$this->subqueues = array();
1751
		}
1752
		$q =& new hfsc_queue();
1753
		$q->SetInterface($this->GetInterface());
1754
		$q->SetParent($this);
1755
		$q->ReadConfig($qname);
1756
		$q->validate_input($qname, $input_errors);
1757

    
1758
		$q->SetEnabled("on");
1759
		$q->SetLink($path);
1760

    
1761
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
1762
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
1763
		if (is_array($qname['queue'])) {
1764
			foreach ($qname['queue'] as $key1 => $que) {
1765
				array_push($path, $key1);
1766
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
1767
				array_pop($path);
1768
			}
1769
		}
1770

    
1771
		return $q;
1772
	}
1773

    
1774
	function copy_queue($interface, &$cflink) {
1775

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

    
1867
		if (is_array($this->subqueues)) {
1868
			$cflinkp['queue'] = array();
1869
			foreach ($this->subqueues as $q) {
1870
				$cflink['queue'][$q->GetQname()] = array();
1871
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1872
			}
1873
		}
1874
	}
1875

    
1876
	function delete_queue() {
1877
		unref_on_altq_queue_list($this->GetQname());
1878
		cleanup_queue_from_rules($this->GetQname());
1879
		$parent =& $this->GetParent();
1880
		foreach ($this->subqueues as $q)
1881
			$q->delete_queue();
1882
		unset_object_by_reference($this->GetLink());
1883
	}
1884

    
1885
	/*
1886
	 * Should search even its children
1887
	 */
1888
	function &find_queue($interface, $qname) {
1889
		if ($qname == $this->GetQname()) {
1890
			return $this;
1891
		}
1892

    
1893
		foreach ($this->subqueues as $q) {
1894
			$result =& $q->find_queue("", $qname);
1895
			if ($result) {
1896
				return $result;
1897
			}
1898
		}
1899
	}
1900

    
1901
	function &find_parentqueue($interface, $qname) {
1902
		if ($this->subqueues[$qname]) {
1903
			return $this;
1904
		}
1905
		foreach ($this->subqueues as $q) {
1906
			$result = $q->find_parentqueue("", $qname);
1907
			if ($result) {
1908
				return $result;
1909
			}
1910
		}
1911
	}
1912

    
1913
	function validate_input($data, &$input_errors) {
1914
		parent::validate_input($data, $input_errors);
1915

    
1916
		$reqdfields[] = "bandwidth";
1917
		$reqdfieldsn[] = gettext("Bandwidth");
1918
		$reqdfields[] = "bandwidthtype";
1919
		$reqdfieldsn[] = gettext("Bandwidthtype");
1920

    
1921
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1922

    
1923
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
1924
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1925
				$input_errors[] = gettext("Bandwidth must be an integer.");
1926
			}
1927

    
1928
			if ($data['bandwidth'] < 0) {
1929
				$input_errors[] = gettext("Bandwidth cannot be negative.");
1930
			}
1931

    
1932
			if ($data['bandwidthtype'] == "%") {
1933
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1934
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1935
				}
1936
			}
1937
		}
1938

    
1939
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
1940
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
1941
		}
1942
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
1943
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
1944
		}
1945
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
1946
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
1947
		}
1948
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
1949
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
1950
		}
1951
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
1952
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
1953
		}
1954

    
1955
		/*
1956
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
1957
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
1958
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
1959
			if (floatval($bw_1) < floatval($bw_2)) {
1960
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
1961
			}
1962

    
1963
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
1964
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
1965
			}
1966
		}
1967
		*/
1968
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
1969
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
1970
		}
1971
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
1972
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
1973
		}
1974
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
1975
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
1976
		}
1977
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
1978
			$input_errors[] = gettext("linkshare d value needs to be numeric");
1979
		}
1980
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
1981
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
1982
		}
1983
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
1984
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
1985
		}
1986
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
1987
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
1988
		}
1989

    
1990
		/*
1991
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
1992
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
1993
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
1994
			if (floatval($bw_1) < floatval($bw_2)) {
1995
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
1996
			}
1997

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

    
2004
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2005
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2006
		}
2007
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2008
			$input_errors[] = gettext("realtime d value needs to be numeric");
2009
		}
2010
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2011
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2012
		}
2013

    
2014
		/*
2015
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2016
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2017
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2018
			if (floatval($bw_1) < floatval($bw_2)) {
2019
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2020
			}
2021

    
2022
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2023
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2024
			}
2025
		}
2026
		*/
2027
	}
2028

    
2029
	function ReadConfig(&$cflink) {
2030
		if (!empty($cflink['linkshare'])) {
2031
			if (!empty($cflink['linkshare1'])) {
2032
				$this->SetL_m1($cflink['linkshare1']);
2033
				$this->SetL_d($cflink['linkshare2']);
2034
				$this->SetLinkshare();
2035
			} else {
2036
				$this->SetL_m1("");
2037
				$this->SetL_d("");
2038
				$this->DisableLinkshare();
2039
			}
2040
			if (!empty($cflink['linkshare3'])) {
2041
				$this->SetL_m2($cflink['linkshare3']);
2042
				$this->SetLinkshare();
2043
			}
2044
		} else {
2045
			$this->DisableLinkshare();
2046
		}
2047
		if (!empty($cflink['realtime'])) {
2048
			if (!empty($cflink['realtime1'])) {
2049
				$this->SetR_m1($cflink['realtime1']);
2050
				$this->SetR_d($cflink['realtime2']);
2051
				$this->SetRealtime();
2052
			} else {
2053
				$this->SetR_m1("");
2054
				$this->SetR_d("");
2055
				$this->DisableRealtime();
2056
			}
2057
			if (!empty($cflink['realtime3'])) {
2058
				$this->SetR_m2($cflink['realtime3']);
2059
				$this->SetRealtime();
2060
			}
2061
		} else {
2062
			$this->DisableRealtime();
2063
		}
2064
		if (!empty($cflink['upperlimit'])) {
2065
			if (!empty($cflink['upperlimit1'])) {
2066
				$this->SetU_m1($cflink['upperlimit1']);
2067
				$this->SetU_d($cflink['upperlimit2']);
2068
				$this->SetUpperlimit();
2069
			} else {
2070
				$this->SetU_m1("");
2071
				$this->SetU_d("");
2072
				$this->DisableUpperlimit();
2073
			}
2074
			if (!empty($cflink['upperlimit3'])) {
2075
				$this->SetU_m2($cflink['upperlimit3']);
2076
				$this->SetUpperlimit();
2077
			}
2078
		} else {
2079
			$this->DisableUpperlimit();
2080
		}
2081
		parent::ReadConfig($cflink);
2082
	}
2083

    
2084
	function build_tree() {
2085
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . $this->GetQname()."&amp;action=show";
2086
		$tree .= "\" ";
2087
		$tmpvalue = $this->GetDefault();
2088
		if (!empty($tmpvalue)) {
2089
			$tree .= " class=\"navlnk\"";
2090
		}
2091
		$tree .= " >" . $this->GetQname() . "</a>";
2092
		if (is_array($this->subqueues)) {
2093
			$tree .= "<ul>";
2094
			foreach ($this->subqueues as $q) {
2095
				$tree .= $q->build_tree();
2096
			}
2097
			$tree .= "</ul>";
2098
		}
2099
		$tree .= "</li>";
2100
		return $tree;
2101
	}
2102

    
2103
	/* Even this should take children into consideration */
2104
	function build_rules(&$default = false) {
2105

    
2106
		$pfq_rule = " queue ". $this->qname;
2107
		if ($this->GetInterface()) {
2108
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2109
		}
2110
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2111
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2112
		}
2113

    
2114
		$tmpvalue = $this->GetQlimit();
2115
		if (!empty($tmpvalue)) {
2116
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2117
		}
2118
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2119
			$pfq_rule .= " hfsc ( ";
2120
			$tmpvalue = $this->GetRed();
2121
			if (!empty($tmpvalue)) {
2122
				$comma = 1;
2123
				$pfq_rule .= " red ";
2124
			}
2125

    
2126
			$tmpvalue = $this->GetRio();
2127
			if (!empty($tmpvalue)) {
2128
				if ($comma) {
2129
					$pfq_rule .= " ,";
2130
				}
2131
				$comma = 1;
2132
				$pfq_rule .= " rio ";
2133
			}
2134
			$tmpvalue = $this->GetEcn();
2135
			if (!empty($tmpvalue)) {
2136
				if ($comma) {
2137
					$pfq_rule .= " ,";
2138
				}
2139
				$comma = 1;
2140
				$pfq_rule .= " ecn ";
2141
			}
2142
			$tmpvalue = $this->GetCodel();
2143
			if (!empty($tmpvalue)) {
2144
				if ($comma) {
2145
					$pfq_rule .= " ,";
2146
				}
2147
				$comma = 1;
2148
				$pfq_rule .= " codel ";
2149
			}
2150
			$tmpvalue = $this->GetDefault();
2151
			if (!empty($tmpvalue)) {
2152
				if ($comma) {
2153
					$pfq_rule .= " ,";
2154
				}
2155
				$comma = 1;
2156
				$pfq_rule .= " default ";
2157
				$default = true;
2158
			}
2159

    
2160
			if ($this->GetRealtime() <> "") {
2161
				if ($comma) {
2162
					$pfq_rule .= " , ";
2163
				}
2164
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2165
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2166
				} else if ($this->GetR_m2() <> "") {
2167
					$pfq_rule .= " realtime " . $this->GetR_m2();
2168
				}
2169
				$comma = 1;
2170
			}
2171
			if ($this->GetLinkshare() <> "") {
2172
				if ($comma) {
2173
					$pfq_rule .= " ,";
2174
				}
2175
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2176
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2177
				} else if ($this->GetL_m2() <> "") {
2178
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2179
				}
2180
				$comma = 1;
2181
			}
2182
			if ($this->GetUpperlimit() <> "") {
2183
				if ($comma) {
2184
					$pfq_rule .= " ,";
2185
				}
2186
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2187
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2188
				} else if ($this->GetU_m2() <> "") {
2189
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2190
				}
2191
			}
2192
			$pfq_rule .= " ) ";
2193
		}
2194
		if (count($this->subqueues)) {
2195
			$i = count($this->subqueues);
2196
			$pfq_rule .= " { ";
2197
			foreach ($this->subqueues as $qkey => $qnone) {
2198
				if ($i > 1) {
2199
					$i--;
2200
					$pfq_rule .= " {$qkey}, ";
2201
				} else {
2202
					$pfq_rule .= " {$qkey} ";
2203
				}
2204
			}
2205
			$pfq_rule .= " } \n";
2206
			foreach ($this->subqueues as $q) {
2207
				$pfq_rule .= $q->build_rules($default);
2208
			}
2209
		}
2210

    
2211
		$pfq_rule .= " \n";
2212

    
2213
		return $pfq_rule;
2214
	}
2215

    
2216
	function build_javascript() {
2217

    
2218
		$javascript = <<<EOJS
2219
<script type="text/javascript">
2220
//<![CDATA[
2221
	events.push(function(){
2222

    
2223
		// Disables the specified input element
2224
		function disableInput(id, disable) {
2225
			$('#' + id).prop("disabled", disable);
2226
		}
2227

    
2228
		// Upperlimit
2229
		function enable_upperlimit() {
2230
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2231
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2232
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2233
		}
2234

    
2235
		$('#upperlimit').click(function () {
2236
			enable_upperlimit();
2237
		});
2238

    
2239
		enable_upperlimit();
2240

    
2241
		// realtime
2242
		function enable_realtime() {
2243
			disableInput('realtime1', !$('#realtime').prop('checked'));
2244
			disableInput('realtime2', !$('#realtime').prop('checked'));
2245
			disableInput('realtime3', !$('#realtime').prop('checked'));
2246
		}
2247

    
2248
		$('#realtime').click(function () {
2249
			enable_realtime();
2250
		});
2251

    
2252
		enable_realtime();
2253

    
2254
		// linkshare
2255
		function enable_linkshare() {
2256
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2257
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2258
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2259
		}
2260

    
2261
		$('#linkshare').click(function () {
2262
			enable_linkshare();
2263
		});
2264

    
2265
		enable_linkshare();
2266
	});
2267
//]]>
2268
</script>
2269
EOJS;
2270

    
2271
		return $javascript;
2272
	}
2273

    
2274
	function build_form() {
2275

    
2276
		$sform = parent::build_form();
2277

    
2278
		$section = new Form_Section('Service Curve (sc)');
2279

    
2280
		$group = new Form_Group('Bandwidth');
2281

    
2282
		$group->add(new Form_Input(
2283
			'bandwidth',
2284
			null,
2285
			'number',
2286
			$this->GetBandwidth(),
2287
			['step' => 'any', 'min' => '0.000']
2288
		));
2289

    
2290
		$group->add(new Form_Select(
2291
			'bandwidthtype',
2292
			null,
2293
			$this->GetBwscale(),
2294
			array('Kb' => 'Kbit/s',
2295
				  'Mb' => 'Mbit/s',
2296
				  'Gb' => 'Gbit/s',
2297
				  'b' => 'Bit/s',
2298
				  '%' => '%')
2299
		));
2300

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

    
2303
		$section->add($group);
2304

    
2305
		$group = new Form_Group('Max bandwidth for queue.');
2306

    
2307
		$group->add(new Form_Checkbox(
2308
			'upperlimit',
2309
			null,
2310
			'Upper Limit',
2311
			($this->GetUpperlimit()<> "")
2312
		));
2313

    
2314
		$group->add(new Form_Input(
2315
			'upperlimit1',
2316
			null,
2317
			'text',
2318
			$this->GetU_m1()
2319
		))->setHelp('m1');
2320

    
2321
		$group->add(new Form_Input(
2322
			'upperlimit2',
2323
			null,
2324
			'text',
2325
			$this->GetU_d()
2326
		))->setHelp('d');
2327

    
2328
		$group->add(new Form_Input(
2329
			'upperlimit3',
2330
			null,
2331
			'text',
2332
			$this->GetU_m2()
2333
		))->setHelp('m2');
2334

    
2335

    
2336
		$section->add($group);
2337

    
2338
		$group = new Form_Group('Min bandwidth for queue.');
2339

    
2340
		$group->add(new Form_Checkbox(
2341
			'realtime',
2342
			null,
2343
			'Real Time',
2344
			($this->GetRealtime()<> "")
2345
		));
2346

    
2347
		$group->add(new Form_Input(
2348
			'realtime1',
2349
			null,
2350
			'text',
2351
			$this->GetR_m1()
2352
		))->setHelp('m1');
2353

    
2354
		$group->add(new Form_Input(
2355
			'realtime2',
2356
			null,
2357
			'text',
2358
			$this->GetR_d()
2359
		))->setHelp('d');
2360

    
2361
		$group->add(new Form_Input(
2362
			'realtime3',
2363
			null,
2364
			'text',
2365
			$this->GetR_m2()
2366
		))->setHelp('m2');
2367

    
2368
		$section->add($group);
2369

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

    
2372
		$group->add(new Form_Checkbox(
2373
			'linkshare',
2374
			null,
2375
			'Link Share',
2376
			($this->GetLinkshare()<> "")
2377
		));
2378

    
2379
		$group->add(new Form_Input(
2380
			'linkshare1',
2381
			null,
2382
			'text',
2383
			$this->GetL_m1()
2384
		))->setHelp('m1');
2385

    
2386
		$group->add(new Form_Input(
2387
			'linkshare2',
2388
			null,
2389
			'text',
2390
			$this->GetL_d()
2391
		))->setHelp('d');
2392

    
2393
		$group->add(new Form_Input(
2394
			'linkshare3',
2395
			null,
2396
			'text',
2397
			$this->GetL_m2()
2398
		))->setHelp('m2');
2399

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

    
2406
		$section->add($group);
2407

    
2408
		$sform->add($section);
2409

    
2410
		return($sform);
2411
	}
2412

    
2413
	function update_altq_queue_data(&$data) {
2414
		$this->ReadConfig($data);
2415
	}
2416

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

    
2534
class cbq_queue extends priq_queue {
2535
	var $qborrow = "";
2536

    
2537
	function GetBorrow() {
2538
		return $this->qborrow;
2539
	}
2540
	function SetBorrow($borrow) {
2541
		$this->qborrow = $borrow;
2542
	}
2543
	function CanHaveChildren() {
2544
		return true;
2545
	}
2546

    
2547
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2548

    
2549
		if (!is_array($this->subqueues)) {
2550
			$this->subqueues = array();
2551
		}
2552
		$q =& new cbq_queue();
2553
		$q->SetInterface($this->GetInterface());
2554
		$q->SetParent($this);
2555
		$q->ReadConfig($qname);
2556
		$q->validate_input($qname, $input_errors);
2557

    
2558
		$q->SetEnabled("on");
2559
		$q->SetLink($path);
2560
		$this->subqueues[$q->GetQName()] = &$q;
2561
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2562
		if (is_array($qname['queue'])) {
2563
			foreach ($qname['queue'] as $key1 => $que) {
2564
				array_push($path, $key1);
2565
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2566
				array_pop($path);
2567
			}
2568
		}
2569

    
2570
		return $q;
2571
	}
2572

    
2573
	function copy_queue($interface, &$cflink) {
2574

    
2575
		$cflink['interface'] = $interface;
2576
		$cflink['qlimit'] = trim($this->GetQlimit());
2577
		if (empty($clink['qlimit'])) {
2578
			unset($cflink['qlimit']);
2579
		}
2580
		$cflink['priority'] = trim($this->GetQpriority());
2581
		if (!is_numeric($cflink['priority'])) {
2582
			unset($cflink['priority']);
2583
		}
2584
		$cflink['name'] = $this->GetQname();
2585
		$cflink['description'] = trim($this->GetDescription());
2586
		if (empty($cflink['description'])) {
2587
			unset($cflink['description']);
2588
		}
2589
		$cflink['bandwidth'] = $this->GetBandwidth();
2590
		$cflink['bandwidthtype'] = $this->GetBwscale();
2591
		$cflink['enabled'] = trim($this->GetEnabled());
2592
		if (empty($cflink['enabled'])) {
2593
			unset($cflink['enabled']);
2594
		}
2595
		$cflink['default'] = trim($this->GetDefault());
2596
		if (empty($cflink['default'])) {
2597
			unset($cflink['default']);
2598
		}
2599
		$cflink['red'] = trim($this->GetRed());
2600
		if (empty($cflink['red'])) {
2601
			unset($cflink['red']);
2602
		}
2603
		$cflink['rio'] = trim($this->GetRio());
2604
		if (empty($cflink['rio'])) {
2605
			unset($cflink['rio']);
2606
		}
2607
		$cflink['ecn'] = trim($this->GetEcn());
2608
		if (empty($cflink['ecn'])) {
2609
			unset($cflink['ecn']);
2610
		}
2611
		$cflink['borrow'] = trim($this->GetBorrow());
2612
		if (empty($cflink['borrow'])) {
2613
			unset($cflink['borrow']);
2614
		}
2615
		if (is_array($this->queues)) {
2616
			$cflinkp['queue'] = array();
2617
			foreach ($this->subqueues as $q) {
2618
				$cflink['queue'][$q->GetQname()] = array();
2619
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2620
			}
2621
		}
2622
	}
2623

    
2624
	/*
2625
	 * Should search even its children
2626
	 */
2627
	function &find_queue($interface, $qname) {
2628
		if ($qname == $this->GetQname()) {
2629
			return $this;
2630
		}
2631
		foreach ($this->subqueues as $q) {
2632
			$result =& $q->find_queue("", $qname);
2633
			if ($result) {
2634
				return $result;
2635
			}
2636
		}
2637
	}
2638

    
2639
	function &find_parentqueue($interface, $qname) {
2640
		if ($this->subqueues[$qname]) {
2641
			return $this;
2642
		}
2643
		foreach ($this->subqueues as $q) {
2644
			$result = $q->find_parentqueue("", $qname);
2645
			if ($result) {
2646
				return $result;
2647
			}
2648
		}
2649
	}
2650

    
2651
	function delete_queue() {
2652
		unref_on_altq_queue_list($this->GetQname());
2653
		cleanup_queue_from_rules($this->GetQname());
2654
		foreach ($this->subqueues as $q)
2655
			$q->delete_queue();
2656
		unset_object_by_reference($this->GetLink());
2657
	}
2658

    
2659
	function validate_input($data, &$input_errors) {
2660
		parent::validate_input($data, $input_errors);
2661

    
2662
		if ($data['priority'] > 7) {
2663
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
2664
		}
2665
		$reqdfields[] = "bandwidth";
2666
		$reqdfieldsn[] = gettext("Bandwidth");
2667
		$reqdfields[] = "bandwidthtype";
2668
		$reqdfieldsn[] = gettext("Bandwidthtype");
2669

    
2670
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2671
	}
2672

    
2673
	function ReadConfig(&$q) {
2674
		parent::ReadConfig($q);
2675
		if (!empty($q['borrow'])) {
2676
			$this->SetBorrow("on");
2677
		} else {
2678
			$this->SetBorrow("");
2679
		}
2680
	}
2681

    
2682
	function build_javascript() {
2683
		return parent::build_javascript();
2684
	}
2685

    
2686
	function build_tree() {
2687
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2688
		$tree .= "\" ";
2689
		$tmpvalue = trim($this->GetDefault());
2690
		if (!empty($tmpvalue)) {
2691
			$tree .= " class=\"navlnk\"";
2692
		}
2693
		$tree .= " >" . $this->GetQname() . "</a>";
2694
		if (is_array($this->subqueues)) {
2695
			$tree .= "<ul>";
2696
			foreach ($this->subqueues as $q) {
2697
				$tree .= $q->build_tree();
2698
			}
2699
			$tree .= "</ul>";
2700
		}
2701
		$tree .= "</li>";
2702
		return $tree;
2703
	}
2704

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

    
2785
		$pfq_rule .= " \n";
2786
		return $pfq_rule;
2787
	}
2788

    
2789
	function build_form() {
2790
		$sform = parent::build_form();
2791

    
2792
		$section = new Form_Section('NOTITLE');
2793

    
2794
		$group = new Form_Group('Bandwidth');
2795

    
2796
		$group->add(new Form_Input(
2797
			'bandwidth',
2798
			null,
2799
			'number',
2800
			$this->GetBandwidth()
2801
		));
2802

    
2803
		$group->add(new Form_Select(
2804
			'bandwidthtype',
2805
			null,
2806
			$this->GetBwscale(),
2807
			array('Kb' => 'Kbit/s',
2808
				  'Mb' => 'Mbit/s',
2809
				  'Gb' => 'Gbit/s',
2810
				  'b' => 'Bit/s',
2811
				  '%' => '%')
2812
		));
2813

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

    
2816
		$section->add($group);
2817

    
2818
		$section->addInput(new Form_Checkbox(
2819
			'borrow',
2820
			'Scheduler option',
2821
			'Borrow from other queues when available',
2822
			($this->GetBorrow() == "on")
2823
		));
2824

    
2825
		$sform->add($section);
2826

    
2827
		return $sform;
2828
	}
2829

    
2830
	function update_altq_queue_data(&$data) {
2831
		$this->ReadConfig($data);
2832
	}
2833

    
2834
	function wconfig() {
2835
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2836
		if (!is_array($cflink)) {
2837
			$cflink = array();
2838
		}
2839
		$cflink['interface'] = $this->GetInterface();
2840
		$cflink['qlimit'] = trim($this->GetQlimit());
2841
		if (empty($cflink['qlimit'])) {
2842
			unset($cflink['qlimit']);
2843
		}
2844
		$cflink['priority'] = $this->GetQpriority();
2845
		if (!is_numeric($cflink['priority'])) {
2846
			unset($cflink['priority']);
2847
		}
2848
		$cflink['name'] = $this->GetQname();
2849
		$cflink['description'] = $this->GetDescription();
2850
		if (empty($cflink['description'])) {
2851
			unset($cflink['description']);
2852
		}
2853
		$cflink['bandwidth'] = $this->GetBandwidth();
2854
		$cflink['bandwidthtype'] = $this->GetBwscale();
2855
		$cflink['enabled'] = trim($this->GetEnabled());
2856
		if (empty($cflink['enabled'])) {
2857
			unset($cflink['enabled']);
2858
		}
2859
		$cflink['default'] = trim($this->GetDefault());
2860
		if (empty($cflink['default'])) {
2861
			unset($cflink['default']);
2862
		}
2863
		$cflink['red'] = trim($this->GetRed());
2864
		if (empty($cflink['red'])) {
2865
			unset($cflink['red']);
2866
		}
2867
		$cflink['rio'] = trim($this->GetRio());
2868
		if (empty($cflink['rio'])) {
2869
			unset($cflink['rio']);
2870
		}
2871
		$cflink['ecn'] = trim($this->GetEcn());
2872
		if (empty($cflink['ecn'])) {
2873
			unset($cflink['ecn']);
2874
		}
2875
		$cflink['codel'] = trim($this->GetCodel());
2876
		if (empty($cflink['codel'])) {
2877
			unset($cflink['codel']);
2878
		}
2879
		$cflink['borrow'] = trim($this->GetBorrow());
2880
		if (empty($cflink['borrow'])) {
2881
			unset($cflink['borrow']);
2882
		}
2883
	}
2884
}
2885

    
2886
class fairq_queue extends priq_queue {
2887
	var $hogs;
2888
	var $buckets;
2889

    
2890
	function GetBuckets() {
2891
		return $this->buckets;
2892
	}
2893
	function SetBuckets($buckets) {
2894
		$this->buckets = $buckets;
2895
	}
2896
	function GetHogs() {
2897
		return $this->hogs;
2898
	}
2899
	function SetHogs($hogs) {
2900
		$this->hogs = $hogs;
2901
	}
2902
	function CanHaveChildren() {
2903
		return false;
2904
	}
2905

    
2906

    
2907
	function copy_queue($interface, &$cflink) {
2908
		$cflink['interface'] = $interface;
2909
		$cflink['qlimit'] = $this->GetQlimit();
2910
		$cflink['priority'] = $this->GetQpriority();
2911
		$cflink['name'] = $this->GetQname();
2912
		$cflink['description'] = $this->GetDescription();
2913
		$cflink['bandwidth'] = $this->GetBandwidth();
2914
		$cflink['bandwidthtype'] = $this->GetBwscale();
2915
		$cflink['enabled'] = $this->GetEnabled();
2916
		$cflink['default'] = $this->GetDefault();
2917
		$cflink['red'] = $this->GetRed();
2918
		$cflink['rio'] = $this->GetRio();
2919
		$cflink['ecn'] = $this->GetEcn();
2920
		$cflink['buckets'] = $this->GetBuckets();
2921
		$cflink['hogs'] = $this->GetHogs();
2922
	}
2923

    
2924
	/*
2925
	 * Should search even its children
2926
	 */
2927
	function &find_queue($interface, $qname) {
2928
		if ($qname == $this->GetQname()) {
2929
			return $this;
2930
		}
2931
	}
2932

    
2933
	function find_parentqueue($interface, $qname) { return; }
2934

    
2935
	function delete_queue() {
2936
		unref_on_altq_queue_list($this->GetQname());
2937
		cleanup_queue_from_rules($this->GetQname());
2938
		unset_object_by_reference($this->GetLink());
2939
	}
2940

    
2941
	function validate_input($data, &$input_errors) {
2942
		parent::validate_input($data, $input_errors);
2943

    
2944
		if ($data['priority'] > 7) {
2945
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
2946
		}
2947
		$reqdfields[] = "bandwidth";
2948
		$reqdfieldsn[] = gettext("Bandwidth");
2949
		$reqdfields[] = "bandwidthtype";
2950
		$reqdfieldsn[] = gettext("Bandwidthtype");
2951

    
2952
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2953
	}
2954

    
2955
	function ReadConfig(&$q) {
2956
		parent::ReadConfig($q);
2957
		if (!empty($q['buckets'])) {
2958
			$this->SetBuckets($q['buckets']);
2959
		} else {
2960
			$this->SetBuckets("");
2961
		}
2962
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
2963
			$this->SetHogs($q['hogs']);
2964
		} else {
2965
			$this->SetHogs("");
2966
		}
2967
	}
2968

    
2969
	function build_javascript() {
2970
		return parent::build_javascript();
2971
	}
2972

    
2973
	function build_tree() {
2974
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
2975
		$this->GetInterface()."&amp;queue=" . $this->GetQname()."&amp;action=show";
2976
		$tree .= "\" ";
2977
		$tmpvalue = trim($this->GetDefault());
2978
		if (!empty($tmpvalue)) {
2979
			$tree .= " class=\"navlnk\"";
2980
		}
2981
		$tree .= " >" . $this->GetQname() . "</a>";
2982
		$tree .= "</li>";
2983
		return $tree;
2984
	}
2985

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

    
3058
		$pfq_rule .= " \n";
3059
		return $pfq_rule;
3060
	}
3061

    
3062
	function build_form() {
3063
		$form = parent::build_form();
3064

    
3065
		$section = new Form_Section('');
3066

    
3067
		$group = new Form_Group('Bandwidth');
3068

    
3069
		$group->add(new Form_Input(
3070
			'bandwidth',
3071
			null,
3072
			'number',
3073
			$this->GetBandwidth()
3074
		));
3075

    
3076
		$group->add(new Form_Select(
3077
			'bandwidthtype',
3078
			null,
3079
			$this->GetBwscale(),
3080
			array('Kb' => 'Kbit/s',
3081
				  'Mb' => 'Mbit/s',
3082
				  'Gb' => 'Gbit/s',
3083
				  'b' => 'Bit/s',
3084
				  '%' => '%')
3085
		));
3086

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

    
3089
		$section->add($group);
3090

    
3091
		$section->addInput(new Form_Input(
3092
			'buckets',
3093
			'Scheduler specific options',
3094
			'text',
3095
			$this->GetBuckets()
3096
		))->setHelp('Number of buckets available');
3097

    
3098
		$section->addInput(new Form_Input(
3099
			'hogs',
3100
			'',
3101
			'text',
3102
			$this->GetHogs()
3103
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3104

    
3105
		$form->add($section);
3106
		return $form;
3107
	}
3108

    
3109
	function update_altq_queue_data(&$data) {
3110
		$this->ReadConfig($data);
3111
	}
3112

    
3113
	function wconfig() {
3114
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3115
		if (!is_array($cflink)) {
3116
			$cflink = array();
3117
		}
3118
		$cflink['interface'] = $this->GetInterface();
3119
		$cflink['qlimit'] = trim($this->GetQlimit());
3120
		if (empty($cflink['qlimit'])) {
3121
			unset($cflink['qlimit']);
3122
		}
3123
		$cflink['priority'] = trim($this->GetQpriority());
3124
		if (!is_numeric($cflink['priority'])) {
3125
			unset($cflink['priority']);
3126
		}
3127
		$cflink['name'] = $this->GetQname();
3128
		$cflink['description'] = trim($this->GetDescription());
3129
		if (empty($cflink['description'])) {
3130
			unset($cflink['description']);
3131
		}
3132
		$cflink['bandwidth'] = $this->GetBandwidth();
3133
		$cflink['bandwidthtype'] = $this->GetBwscale();
3134
		$cflink['enabled'] = $this->GetEnabled();
3135
		if (empty($cflink['enabled'])) {
3136
			unset($cflink['enabled']);
3137
		}
3138
		$cflink['default'] = trim($this->GetDefault());
3139
		if (empty($cflink['default'])) {
3140
			unset($cflink['default']);
3141
		}
3142
		$cflink['red'] = trim($this->GetRed());
3143
		if (empty($cflink['red'])) {
3144
			unset($cflink['red']);
3145
		}
3146
		$cflink['rio'] = trim($this->GetRio());
3147
		if (empty($cflink['rio'])) {
3148
			unset($cflink['rio']);
3149
		}
3150
		$cflink['ecn'] = trim($this->GetEcn());
3151
		if (empty($cflink['ecn'])) {
3152
			unset($cflink['ecn']);
3153
		}
3154
		$cflink['codel'] = trim($this->GetCodel());
3155
		if (empty($cflink['codel'])) {
3156
			unset($cflink['codel']);
3157
		}
3158
		$cflink['buckets'] = trim($this->GetBuckets());
3159
		if (empty($cflink['buckets'])) {
3160
			unset($cflink['buckets']);
3161
		}
3162
		$cflink['hogs'] = trim($this->GetHogs());
3163
		if (empty($cflink['hogs'])) {
3164
			unset($cflink['hogs']);
3165
		}
3166
	}
3167
}
3168

    
3169

    
3170
/*
3171
 * dummynet(4) wrappers.
3172
 */
3173

    
3174

    
3175
/*
3176
 * List of respective objects!
3177
 */
3178
$dummynet_pipe_list = array();
3179

    
3180
class dummynet_class {
3181
	var $qname;
3182
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3183
	var $qlimit;
3184
	var $description;
3185
	var $qenabled;
3186
	var $link;
3187
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3188
	var $plr;
3189

    
3190
	var $buckets;
3191
	/* mask parameters */
3192
	var $mask;
3193
	var $noerror;
3194

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

    
3272
	function build_javascript() {
3273
		$javascript .= "<script type=\"text/javascript\">\n";
3274
		$javascript .= "//<![CDATA[\n";
3275
		$javascript .= "function enable_maskbits(enable_over) {\n";
3276
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3277
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3278
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3279
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3280
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3281
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3282
		$javascript .= "} else {\n";
3283
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3284
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3285
		$javascript .= "}}\n";
3286
		$javascript .= "//]]>\n";
3287
		$javascript .= "</script>\n";
3288
		return $javascript;
3289
	}
3290

    
3291
	function validate_input($data, &$input_errors) {
3292
		$reqdfields[] = "bandwidth";
3293
		$reqdfieldsn[] = gettext("Bandwidth");
3294
		/*$reqdfields[] = "burst";
3295
		$reqdfieldsn[] = gettext("Burst"); */
3296
		$reqdfields[] = "bandwidthtype";
3297
		$reqdfieldsn[] = gettext("Bandwidthtype");
3298
		$reqdfields[] = "newname";
3299
		$reqdfieldsn[] = gettext("Name");
3300

    
3301
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3302

    
3303
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3304
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3305
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3306
		}
3307
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3308
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3309
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3310
		}
3311
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3312
			$input_errors[] = gettext("Queue limit must be an integer");
3313
		}
3314
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3315
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3316
		}
3317
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3318
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3319
		}
3320
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3321
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3322
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3323
			}
3324
		}
3325
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3326
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3327
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3328
			}
3329
		}
3330
	}
3331

    
3332
	function build_mask_rules(&$pfq_rule) {
3333
		$mask = $this->GetMask();
3334
		if (!empty($mask['type'])) {
3335
			if ($mask['type'] <> 'none') {
3336
				$pfq_rule .= " mask";
3337
			}
3338
			switch ($mask['type']) {
3339
				case 'srcaddress':
3340
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3341
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3342
					} else {
3343
						$pfq_rule .= " src-ip6 /128";
3344
					}
3345
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3346
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3347
					} else {
3348
						$pfq_rule .= " src-ip 0xffffffff";
3349
					}
3350
					break;
3351
				case 'dstaddress':
3352
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3353
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3354
					} else {
3355
						$pfq_rule .= " dst-ip6 /128";
3356
					}
3357
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3358
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3359
					} else {
3360
						$pfq_rule .= " dst-ip 0xffffffff";
3361
					}
3362
					break;
3363
				default:
3364
					break;
3365
			}
3366
		}
3367
	}
3368

    
3369
}
3370

    
3371
class dnpipe_class extends dummynet_class {
3372
	var $delay;
3373
	var $qbandwidth = array();
3374
	var $qbandwidthtype;
3375

    
3376
		/* This is here to help on form building and building rules/lists */
3377
	var $subqueues = array();
3378

    
3379
	function CanHaveChildren() {
3380
		return true;
3381
	}
3382
	function SetDelay($delay) {
3383
		$this->delay = $delay;
3384
	}
3385
	function GetDelay() {
3386
		return $this->delay;
3387
	}
3388
	function delete_queue() {
3389
		cleanup_dnqueue_from_rules($this->GetQname());
3390
		foreach ($this->subqueues as $q) {
3391
			$q->delete_queue();
3392
		}
3393
		unset_dn_object_by_reference($this->GetLink());
3394
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3395
	}
3396
	function GetBandwidth() {
3397
		return $this->qbandwidth;
3398
	}
3399
	function SetBandwidth($bandwidth) {
3400
		$this->qbandwidth = $bandwidth;
3401
	}
3402
	function GetBurst() {
3403
		return $this->qburst;
3404
	}
3405
	function SetBurst($burst) {
3406
		$this->qburst = $burst;
3407
	}
3408

    
3409
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3410

    
3411
		if (!is_array($this->subqueues)) {
3412
			$this->subqueues = array();
3413
		}
3414

    
3415
		$q =& new dnqueue_class();
3416
		$q->SetLink($path);
3417
		$q->SetEnabled("on");
3418
		$q->SetPipe($this->GetQname());
3419
		$q->SetParent($this);
3420
		$q->ReadConfig($queue);
3421
		$q->validate_input($queue, $input_errors);
3422
		if (count($input_errors)) {
3423
			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)));
3424
			return $q;
3425
		}
3426
		$number = dnqueue_find_nextnumber();
3427
		$q->SetNumber($number);
3428
		$this->subqueues[$q->GetQname()] = &$q;
3429

    
3430
		return $q;
3431
	}
3432

    
3433
	function &get_queue_list(&$q = null) {
3434
		$qlist = array();
3435

    
3436
		$qlist[$this->GetQname()] = $this->GetNumber();
3437
		if (is_array($this->subqueues)) {
3438
			foreach ($this->subqueues as $queue) {
3439
				$queue->get_queue_list($qlist);
3440
			}
3441
		}
3442
		return $qlist;
3443
	}
3444

    
3445
	/*
3446
	 * Should search even its children
3447
	 */
3448
	function &find_queue($pipe, $qname) {
3449
		if ($qname == $this->GetQname()) {
3450
			return $this;
3451
		}
3452
		foreach ($this->subqueues as $q) {
3453
			$result =& $q->find_queue("", $qname);
3454
			if ($result) {
3455
				return $result;
3456
			}
3457
		}
3458
	}
3459

    
3460
	function &find_parentqueue($pipe, $qname) {
3461
		return NULL;
3462
	}
3463

    
3464
	function validate_input($data, &$input_errors) {
3465
		parent::validate_input($data, $input_errors);
3466

    
3467
		$schedule = 0;
3468
		$schedulenone = 0;
3469
		$entries = 0;
3470
		/* XXX: Really no better way? */
3471
		for ($i = 0; $i < 2900; $i++) {
3472
			if (!empty($data["bwsched{$i}"])) {
3473
				if ($data["bwsched{$i}"] != "none") {
3474
					$schedule++;
3475
				} else {
3476
					$schedulenone++;
3477
				}
3478
			}
3479
			if (!empty($data["bandwidth{$i}"])) {
3480
				if (!is_numeric($data["bandwidth{$i}"])) {
3481
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3482
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3483
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3484
				} else {
3485
					$entries++;
3486
				}
3487
			}
3488
		}
3489
		if ($schedule == 0 && $entries > 1) {
3490
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3491
		}
3492
		if ($schedulenone > 0 && $entries > 1) {
3493
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3494
		}
3495
		if ($entries == 0) {
3496
			$input_errors[] = gettext("At least one bw specification is necessary.");
3497
		}
3498
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3499
			$input_errors[] = gettext("Delay must be an integer.");
3500
		}
3501
	}
3502

    
3503
	function ReadConfig(&$q) {
3504
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3505
			$this->SetQname($q['newname']);
3506
		} else if (!empty($q['newname'])) {
3507
			$this->SetQname($q['newname']);
3508
		} else {
3509
			$this->SetQname($q['name']);
3510
		}
3511
		$this->SetNumber($q['number']);
3512

    
3513
		if (!empty($_POST)) {
3514
			$bandwidth = array();
3515
			/* XXX: Really no better way? */
3516
			for ($i = 0; $i < 2900; $i++) {
3517
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3518
					$bw = array();
3519
					$bw['bw'] = $q["bandwidth{$i}"];
3520
					$bw['burst'] = $q["burst{$i}"];
3521
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3522
						$bw['bwscale'] = $q["bwtype{$i}"];
3523
					}
3524
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3525
						$bw['bwsched'] = $q["bwsched{$i}"];
3526
					}
3527
					$bandwidth[] = $bw;
3528
				}
3529
			}
3530
			$this->SetBandwidth($bandwidth);
3531
		}
3532

    
3533
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3534
			$this->SetBandwidth($q['bandwidth']['item']);
3535
			$this->SetBurst($q['burst']['item']);
3536
		}
3537

    
3538
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3539
			$this->SetQlimit($q['qlimit']);
3540
		} else {
3541
			$this->SetQlimit("");
3542
		}
3543
		if (isset($q['mask']) && $q['mask'] <> "") {
3544
			$masktype = $q['mask'];
3545
		} else {
3546
			$masktype = "";
3547
		}
3548
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3549
			$maskbits = $q['maskbits'];
3550
		} else {
3551
			$maskbits = "";
3552
		}
3553
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3554
			$maskbitsv6 = $q['maskbitsv6'];
3555
		} else {
3556
			$maskbitsv6 = "";
3557
		}
3558
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3559
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3560
			$this->SetBuckets($q['buckets']);
3561
		} else {
3562
			$this->SetBuckets("");
3563
		}
3564
		if (isset($q['plr']) && $q['plr'] <> "") {
3565
			$this->SetPlr($q['plr']);
3566
		} else {
3567
			$this->SetPlr("");
3568
		}
3569
		if (isset($q['delay']) && $q['delay'] <> "") {
3570
			$this->SetDelay($q['delay']);
3571
		} else {
3572
			$this->SetDelay(0);
3573
		}
3574
		if (isset($q['description']) && $q['description'] <> "") {
3575
			$this->SetDescription($q['description']);
3576
		} else {
3577
			$this->SetDescription("");
3578
		}
3579
		$this->SetEnabled($q['enabled']);
3580

    
3581
	}
3582

    
3583
	function build_tree() {
3584
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $this->GetQname() ."&amp;queue=".$this->GetQname() ."&amp;action=show\">";
3585
		$tree .= $this->GetQname() . "</a>";
3586
		if (is_array($this->subqueues)) {
3587
			$tree .= "<ul>";
3588
			foreach ($this->subqueues as $q) {
3589
				$tree .= $q->build_tree();
3590
			}
3591
			$tree .= "</ul>";
3592
		}
3593
		$tree .= "</li>";
3594

    
3595
		return $tree;
3596
	}
3597

    
3598
	function build_rules() {
3599
		global $config, $time_based_rules;
3600

    
3601
		if ($this->GetEnabled() == "") {
3602
			return;
3603
		}
3604

    
3605
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
3606
		$found = false;
3607
		$bandwidth = $this->GetBandwidth();
3608
		if (is_array($bandwidth)) {
3609
			foreach ($bandwidth as $bw) {
3610
				if ($bw['bwsched'] != "none") {
3611
					$time_based_rules = true;
3612
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3613
						foreach ($config['schedules']['schedule'] as $schedule) {
3614
							if ($bw['bwsched'] == $schedule['name']) {
3615
								if (filter_get_time_based_rule_status($schedule)) {
3616
									$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3617
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3618
										$pfq_rule .= " burst ".trim($bw['burst']);
3619
									}
3620
									$found = true;
3621
									break;
3622
								}
3623
							}
3624
						}
3625
					} else {
3626
						$pfq_rule .= " bw 0";
3627
						$found = true;
3628
						break;
3629
					}
3630
				} else {
3631
					$pfq_rule .= " bw ".trim($bw['bw']).$bw['bwscale'];
3632
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
3633
						$pfq_rule .= " burst ".trim($bw['burst']);
3634
					}
3635
					$found = true;
3636
					break;
3637
				}
3638
			}
3639
			if ($found == false) {
3640
				$pfq_rule .= " bw 0";
3641
			}
3642
		} else {
3643
			$pfq_rule .= " bw 0";
3644
		}
3645

    
3646
		if ($this->GetQlimit()) {
3647
			$pfq_rule .= " queue " . $this->GetQlimit();
3648
		}
3649
		if ($this->GetPlr()) {
3650
			$pfq_rule .= " plr " . $this->GetPlr();
3651
		}
3652
		if ($this->GetBuckets()) {
3653
			$pfq_rule .= " buckets " . $this->GetBuckets();
3654
		}
3655
		if ($this->GetDelay()) {
3656
			$pfq_rule .= " delay " . $this->GetDelay();
3657
		}
3658
		$this->build_mask_rules($pfq_rule);
3659

    
3660
		$pfq_rule .= "\n";
3661

    
3662
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
3663
			foreach ($this->subqueues as $q) {
3664
				$pfq_rule .= $q->build_rules();
3665
			}
3666
		}
3667
		$pfq_rule .= " \n";
3668

    
3669
		return $pfq_rule;
3670
	}
3671

    
3672
	function update_dn_data(&$data) {
3673
		$this->ReadConfig($data);
3674
	}
3675

    
3676
	function build_javascript() {
3677
		global $g, $config;
3678

    
3679
		$javasr = parent::build_javascript();
3680

    
3681
		//build list of schedules
3682
		$schedules = "<option value='none'>none</option>";
3683
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3684
			foreach ($config['schedules']['schedule'] as $schedule) {
3685
				if ($schedule['name'] <> "") {
3686
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
3687
				}
3688
			}
3689
		}
3690
		$bwopt = "";
3691
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
3692
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
3693
		}
3694

    
3695
		$javasr .= <<<EOD
3696
<script type='text/javascript'>
3697
//<![CDATA[
3698
var addBwRowTo = (function() {
3699

    
3700
	return (function (tableId) {
3701

    
3702
	var table = document.getElementById(tableId);
3703
	var totalrows = table.rows.length -1;
3704

    
3705
	var row = table.insertRow(totalrows + 1);
3706
	var cell1 = row.insertCell(0);
3707
	var cell2 = row.insertCell(1);
3708
	var cell3 = row.insertCell(2);
3709
	var cell4 = row.insertCell(3);
3710

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

    
3716
	});
3717
})();
3718

    
3719
function removeBwRow(el) {
3720
	var d = el.parentNode.parentNode.rowIndex;
3721
	document.getElementById('maintable').deleteRow(d);
3722
}
3723
//]]>
3724
</script>
3725

    
3726
EOD;
3727

    
3728
		return $javasr;
3729
	}
3730

    
3731
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
3732
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
3733
	// with the javascript in this class
3734
	function build_bwtable() {
3735
		global $config;
3736

    
3737
		$bandwidth = $this->GetBandwidth();
3738
				//build list of schedules
3739
		$schedules = array();
3740
		$schedules[] = "none";//leave none to leave rule enabled all the time
3741
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
3742
			foreach ($config['schedules']['schedule'] as $schedule) {
3743
				if ($schedule['name'] != "") {
3744
					$schedules[] = $schedule['name'];
3745
				}
3746
			}
3747
		}
3748

    
3749
		$form = '<div class="table-responsive">';
3750
		$form .= '<table id="maintable" class="table table-hover table-striped">';
3751
		$form .= "<thead><tr>";
3752
		$form .= "<th>Bandwidth</th>";
3753
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
3754
		$form .= "<th>Bw type</th>";
3755
		$form .= "<th>Schedule</th>";
3756
		$form .= "<th></th>";
3757
		$form .= "</tr></thead>";
3758
		$form .= "<tbody>";
3759

    
3760
		// If there are no bandwidths defined, make a blank one for convenience
3761
		if (empty($bandwidth)) {
3762
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
3763
		}
3764

    
3765
		if (is_array($bandwidth)) {
3766
			foreach ($bandwidth as $bwidx => $bw) {
3767
				$form .= '<tr>';
3768
				$form .= '<td class="col-xs-4">';
3769
				$form .= "<input class='form-control' type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"{$bw['bw']}\" />";
3770
				//$form .= "</td><td width='20%'>";
3771
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
3772
				$form .= "</td>";
3773
				$form .= '<td class="col-xs-4">';
3774
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
3775

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

    
3779
					if ($bw['bwscale'] == $bwsidx) {
3780
						$form .= " selected";
3781
					}
3782

    
3783
					$form .= ">{$bwscale}</option>";
3784
				}
3785

    
3786
				$form .= "</select>";
3787
				$form .= "</td>";
3788
				$form .= '<td class="col-xs-4">';
3789
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
3790

    
3791
				foreach ($schedules as $schd) {
3792
					$selected = "";
3793
					if ($bw['bwsched'] == $schd) {
3794
						$selected = "selected";
3795
					}
3796

    
3797
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
3798
				}
3799

    
3800
				$form .= "</select>";
3801
				$form .= "</td>";
3802
				$form .= '<td>';
3803
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
3804
				$form .= "</td></tr>";
3805
			}
3806
		}
3807
		$form .= "</tbody></table></div><br />";
3808

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

    
3813
		return($form);
3814
	}
3815

    
3816
	function build_form() {
3817
		global $g, $config, $pipe, $action, $qname;
3818

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

    
3830

    
3831
		$sform = new Form();
3832
		$sform->setAction("firewall_shaper.php");
3833

    
3834
		$section = new Form_Section('Limiters');
3835

    
3836
		$section->addInput(new Form_Checkbox(
3837
			'enabled',
3838
			'Enable',
3839
			'Enable limiter and its children',
3840
			($this->GetEnabled() == "on"),
3841
			'on'
3842
		));
3843

    
3844
		$section->addInput(new Form_Input(
3845
			'newname',
3846
			'*Name',
3847
			'text',
3848
			$this->GetQname()
3849
		));
3850

    
3851
		$section->addInput(new Form_Input(
3852
			'name',
3853
			null,
3854
			'hidden',
3855
			$this->GetQname()
3856
		));
3857

    
3858
		if ($this->GetNumber() > 0) {
3859
			$section->addInput(new Form_Input(
3860
				'number',
3861
				null,
3862
				'hidden',
3863
				$this->GetNumber()
3864
			));
3865
		}
3866

    
3867
		$bandwidth = $this->GetBandwidth();
3868

    
3869
		if (is_array($bandwidth)) {
3870
				$section->addInput(new Form_StaticText(
3871
				'Bandwidth',
3872
				$this->build_bwtable()
3873
			));
3874
		}
3875

    
3876
		$mask = $this->GetMask();
3877

    
3878
		$section->addInput(new Form_Select(
3879
			'mask',
3880
			'Mask',
3881
			$mask['type'],
3882
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
3883
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
3884
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
3885
					'This makes it possible to easily specify bandwidth limits per host.');
3886

    
3887
		$group = new Form_Group(null);
3888

    
3889
		$group->add(new Form_Select(
3890
			'maskbits',
3891
			null,
3892
			$mask['bits'],
3893
			array_combine(range(32, 1, -1), range(32, 1, -1))
3894
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
3895

    
3896
		$group->add(new Form_Select(
3897
			'maskbitsv6',
3898
			null,
3899
			$mask['bitsv6'],
3900
			array_combine(range(128, 1, -1), range(128, 1, -1))
3901
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
3902

    
3903
		$section->add($group);
3904

    
3905
		$section->addInput(new Form_Input(
3906
			'description',
3907
			'Description',
3908
			'text',
3909
			$this->GetDescription()
3910
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
3911

    
3912
		$sform->add($section);
3913

    
3914
		$section = new Form_Section('Advanced Options');
3915

    
3916
		$section->addInput(new Form_Input(
3917
			'delay',
3918
			'Delay (ms)',
3919
			'text',
3920
			$this->GetDelay() > 0 ? $this->GetDelay():null
3921
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
3922

    
3923
		$section->addInput(new Form_Input(
3924
			'plr',
3925
			'Packet Loss Rate',
3926
			'number',
3927
			$this->GetPlr(),
3928
			['step' => '0.001', 'min' => '0.000']
3929
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
3930
					'A value of 0.001 means one packet in 1000 gets dropped.');
3931

    
3932
		$section->addInput(new Form_Input(
3933
			'qlimit',
3934
			'Queue size (slots)',
3935
			'number',
3936
			$this->GetQlimit()
3937
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
3938
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
3939

    
3940
		$section->addInput(new Form_Input(
3941
			'buckets',
3942
			'Bucket size (slots)',
3943
			'number',
3944
			$this->GetBuckets()
3945
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
3946

    
3947
		$sform->add($section);
3948

    
3949
		return($sform);
3950
		}
3951

    
3952
	function wconfig() {
3953
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
3954
		if (!is_array($cflink)) {
3955
			$cflink = array();
3956
		}
3957
		$cflink['name'] = $this->GetQname();
3958
		$cflink['number'] = $this->GetNumber();
3959
		$cflink['qlimit'] = $this->GetQlimit();
3960
		$cflink['plr'] = $this->GetPlr();
3961
		$cflink['description'] = $this->GetDescription();
3962

    
3963
		$bandwidth = $this->GetBandwidth();
3964
		if (is_array($bandwidth)) {
3965
			$cflink['bandwidth'] = array();
3966
			$cflink['bandwidth']['item'] = array();
3967
			foreach ($bandwidth as $bwidx => $bw) {
3968
				$cflink['bandwidth']['item'][] = $bw;
3969
			}
3970
		}
3971

    
3972
		$cflink['enabled'] = $this->GetEnabled();
3973
		$cflink['buckets'] = $this->GetBuckets();
3974
		$mask = $this->GetMask();
3975
		$cflink['mask'] = $mask['type'];
3976
		$cflink['maskbits'] = $mask['bits'];
3977
		$cflink['maskbitsv6'] = $mask['bitsv6'];
3978
		$cflink['delay'] = $this->GetDelay();
3979
	}
3980

    
3981
}
3982

    
3983
class dnqueue_class extends dummynet_class {
3984
	var $pipeparent;
3985
	var $weight;
3986

    
3987
	function GetWeight() {
3988
		return $this->weight;
3989
	}
3990
	function SetWeight($weight) {
3991
		$this->weight = $weight;
3992
	}
3993
	function GetPipe() {
3994
		return $this->pipeparent;
3995
	}
3996
	function SetPipe($pipe) {
3997
		$this->pipeparent = $pipe;
3998
	}
3999

    
4000
	/* Just a stub in case we ever try to call this from the frontend. */
4001
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4002
		return;
4003
	}
4004

    
4005
	function delete_queue() {
4006
		cleanup_dnqueue_from_rules($this->GetQname());
4007
		unset_dn_object_by_reference($this->GetLink());
4008
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4009
	}
4010

    
4011
	function validate_input($data, &$input_errors) {
4012
		parent::validate_input($data, $input_errors);
4013

    
4014
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4015
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4016
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4017
		}
4018
	}
4019

    
4020
	/*
4021
	 * Should search even its children
4022
	 */
4023
	function &find_queue($pipe, $qname) {
4024
		if ($qname == $this->GetQname()) {
4025
			return $this;
4026
		} else {
4027
			return NULL;
4028
		}
4029
	}
4030

    
4031
	function &find_parentqueue($pipe, $qname) {
4032
		return $this->qparent;
4033
	}
4034

    
4035
	function &get_queue_list(&$qlist) {
4036
		if ($this->GetEnabled() == "") {
4037
			return;
4038
		}
4039
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4040
	}
4041

    
4042
	function ReadConfig(&$q) {
4043
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4044
			$this->SetQname($q['newname']);
4045
		} else if (!empty($q['newname'])) {
4046
			$this->SetQname($q['newname']);
4047
		} else {
4048
			$this->SetQname($q['name']);
4049
		}
4050
		$this->SetNumber($q['number']);
4051
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4052
			$this->SetQlimit($q['qlimit']);
4053
		} else {
4054
			$this->SetQlimit("");
4055
		}
4056
		if (isset($q['mask']) && $q['mask'] <> "") {
4057
			$masktype = $q['mask'];
4058
		} else {
4059
			$masktype = "";
4060
		}
4061
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4062
			$maskbits = $q['maskbits'];
4063
		} else {
4064
			$maskbits = "";
4065
		}
4066
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4067
			$maskbitsv6 = $q['maskbitsv6'];
4068
		} else {
4069
			$maskbitsv6 = "";
4070
		}
4071
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4072
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4073
			$this->SetBuckets($q['buckets']);
4074
		} else {
4075
			$this->SetBuckets("");
4076
		}
4077
		if (isset($q['plr']) && $q['plr'] <> "") {
4078
			$this->SetPlr($q['plr']);
4079
		} else {
4080
			$this->SetPlr("");
4081
		}
4082
		if (isset($q['weight']) && $q['weight'] <> "") {
4083
			$this->SetWeight($q['weight']);
4084
		} else {
4085
			$this->SetWeight("");
4086
		}
4087
		if (isset($q['description']) && $q['description'] <> "") {
4088
			$this->SetDescription($q['description']);
4089
		} else {
4090
			$this->SetDescription("");
4091
		}
4092
		$this->SetEnabled($q['enabled']);
4093
	}
4094

    
4095
	function build_tree() {
4096
		$parent =& $this->GetParent();
4097
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . $parent->GetQname() ."&amp;queue=" . $this->GetQname() ."&amp;action=show\">";
4098
		$tree .= $this->GetQname() . "</a>";
4099
		$tree .= "</li>";
4100

    
4101
		return $tree;
4102
	}
4103

    
4104
	function build_rules() {
4105
		if ($this->GetEnabled() == "") {
4106
			return;
4107
		}
4108

    
4109
		$parent =& $this->GetParent();
4110
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4111
		if ($this->GetQlimit()) {
4112
			$pfq_rule .= " queue " . $this->GetQlimit();
4113
		}
4114
		if ($this->GetWeight()) {
4115
			$pfq_rule .= " weight " . $this->GetWeight();
4116
		}
4117
		if ($this->GetBuckets()) {
4118
			$pfq_rule .= " buckets " . $this->GetBuckets();
4119
		}
4120
		$this->build_mask_rules($pfq_rule);
4121
		$pfq_rule .= "\n";
4122

    
4123
		return $pfq_rule;
4124
	}
4125

    
4126
	function build_javascript() {
4127
		return parent::build_javascript();
4128
	}
4129

    
4130
	function build_form() {
4131
		global $g, $config, $pipe, $action, $qname;
4132

    
4133
		//build list of schedules
4134
		$schedules = array();
4135
		$schedules[] = "none";//leave none to leave rule enabled all the time
4136
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4137
			foreach ($config['schedules']['schedule'] as $schedule) {
4138
				if ($schedule['name'] <> "") {
4139
					$schedules[] = $schedule['name'];
4140
				}
4141
			}
4142
		}
4143

    
4144

    
4145
		$sform = new Form();
4146
		$sform->setAction("firewall_shaper.php");
4147
		$section = new Form_Section('Limiters');
4148

    
4149
		$section->addInput(new Form_Checkbox(
4150
			'enabled',
4151
			'Enable',
4152
			'Enable this queue',
4153
			($this->GetEnabled() == "on"),
4154
			'on'
4155
		));
4156

    
4157
		$section->addInput(new Form_Input(
4158
			'newname',
4159
			'*Name',
4160
			'text',
4161
			$this->GetQname()
4162
		));
4163

    
4164
		$section->addInput(new Form_Input(
4165
			'name',
4166
			null,
4167
			'hidden',
4168
			$this->GetQname()
4169
		));
4170

    
4171
		if ($this->GetNumber() > 0) {
4172
			$section->addInput(new Form_Input(
4173
				'number',
4174
				null,
4175
				'hidden',
4176
				$this->GetNumber()
4177
			));
4178
		}
4179

    
4180
		$mask = $this->GetMask();
4181

    
4182
		$section->addInput(new Form_Select(
4183
			'mask',
4184
			'Mask',
4185
			$mask['type'],
4186
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4187
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4188
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4189
					'This makes it possible to easily specify bandwidth limits per host.');
4190

    
4191
		$group = new Form_Group(null);
4192

    
4193
		$group->add(new Form_Select(
4194
			'maskbits',
4195
			null,
4196
			$mask['bits'],
4197
			array_combine(range(32, 1, -1), range(32, 1, -1))
4198
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4199

    
4200
		$group->add(new Form_Select(
4201
			'maskbitsv6',
4202
			null,
4203
			$mask['bitsv6'],
4204
			array_combine(range(128, 1, -1), range(128, 1, -1))
4205
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4206

    
4207
		$section->add($group);
4208

    
4209
		$section->addInput(new Form_Input(
4210
			'description',
4211
			'Description',
4212
			'text',
4213
			$this->GetDescription()
4214
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4215

    
4216
		$sform->add($section);
4217

    
4218
		$section = new Form_Section('Advanced Options');
4219

    
4220
		$section->addInput(new Form_Input(
4221
			'weight',
4222
			'Weight',
4223
			'number',
4224
			$this->GetWeight(),
4225
			['min' => '1', 'max' => '100']
4226
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4227
					' it can be left blank otherwise.');
4228

    
4229
		$section->addInput(new Form_Input(
4230
			'plr',
4231
			'Packet Loss Rate',
4232
			'number',
4233
			$this->GetPlr(),
4234
			['step' => '0.001', 'min' => '0.000']
4235
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4236
					'A value of 0.001 means one packet in 1000 gets dropped');
4237

    
4238
		$section->addInput(new Form_Input(
4239
			'qlimit',
4240
			'Queue size (slots)',
4241
			'number',
4242
			$this->GetQlimit()
4243
		))->setHelp('In most cases, the field should be left empty. All packets in this pipe are placed into a fixed-size queue first, ' .
4244
					'then they are delayed by value specified in the Delay field, and then they are delivered to their destination.');
4245

    
4246
		$section->addInput(new Form_Input(
4247
			'buckets',
4248
			'Bucket size (slots)',
4249
			'number',
4250
			$this->GetBuckets()
4251
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4252

    
4253
		$section->addInput(new Form_Input(
4254
			'pipe',
4255
			null,
4256
			'hidden',
4257
			$this->GetPipe()
4258
		));
4259

    
4260
		$sform->add($section);
4261

    
4262
		return($sform);
4263
	}
4264

    
4265
	function update_dn_data(&$data) {
4266
		$this->ReadConfig($data);
4267
	}
4268

    
4269
	function wconfig() {
4270
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4271
		if (!is_array($cflink)) {
4272
			$cflink = array();
4273
		}
4274
		$cflink['name'] = $this->GetQname();
4275
		$cflink['number'] = $this->GetNumber();
4276
		$cflink['qlimit'] = $this->GetQlimit();
4277
		$cflink['description'] = $this->GetDescription();
4278
		$cflink['weight'] = $this->GetWeight();
4279
		$cflink['enabled'] = $this->GetEnabled();
4280
		$cflink['buckets'] = $this->GetBuckets();
4281
		$mask = $this->GetMask();
4282
		$cflink['mask'] = $mask['type'];
4283
		$cflink['maskbits'] = $mask['bits'];
4284
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4285
	}
4286
}
4287

    
4288
function get_dummynet_name_list() {
4289

    
4290
	$dn_name_list =& get_unique_dnqueue_list();
4291
	$dn_name = array();
4292
	if (is_array($dn_name_list)) {
4293
		foreach ($dn_name_list as $key => $value) {
4294
			$dn_name[] = $key;
4295
		}
4296
	}
4297

    
4298
	return $dn_name;
4299

    
4300
}
4301

    
4302
function get_altq_name_list() {
4303
	$altq_name_list =& get_unique_queue_list();
4304
	$altq_name = array();
4305
	if (is_array($altq_name_list)) {
4306
		foreach ($altq_name_list as $key => $aqobj) {
4307
			$altq_name[] = $key;
4308
		}
4309
	}
4310

    
4311
	return $altq_name;
4312
}
4313

    
4314
/*
4315
 * XXX: TODO Make a class shaper to hide all these functions
4316
 * from the global namespace.
4317
 */
4318

    
4319
/*
4320
 * This is a layer violation but for now there is no way
4321
 * I can find to properly do this with PHP.
4322
 */
4323
function altq_get_default_queue($interface) {
4324
	global $altq_list_queues;
4325

    
4326
	$altq_tmp = $altq_list_queues[$interface];
4327
	if ($altq_tmp) {
4328
		return $altq_tmp->GetDefaultQueuePresent();
4329
	} else {
4330
		return false;
4331
	}
4332
}
4333

    
4334
function altq_check_default_queues() {
4335
	global $altq_list_queues;
4336

    
4337
	$count = 0;
4338
	if (is_array($altq_list_queues)) {
4339
		foreach ($altq_list_queues as $altq) {
4340
			if ($altq->GetDefaultQueuePresent()) {
4341
				$count++;
4342
			}
4343
		}
4344
	}
4345
	else {
4346
		$count++;
4347
	}
4348

    
4349
	return 0;
4350
}
4351

    
4352
function &get_unique_queue_list() {
4353
	global $altq_list_queues;
4354

    
4355
	$qlist = array();
4356
	if (is_array($altq_list_queues)) {
4357
		foreach ($altq_list_queues as $altq) {
4358
			if ($altq->GetEnabled() == "") {
4359
				continue;
4360
			}
4361
			$tmplist =& $altq->get_queue_list();
4362
			foreach ($tmplist as $qname => $link) {
4363
				if ($link->GetEnabled() <> "") {
4364
					$qlist[$qname] = $link;
4365
				}
4366
			}
4367
		}
4368
	}
4369
	return $qlist;
4370
}
4371

    
4372
function &get_unique_dnqueue_list() {
4373
	global $dummynet_pipe_list;
4374

    
4375
	$qlist = array();
4376
	if (is_array($dummynet_pipe_list)) {
4377
		foreach ($dummynet_pipe_list as $dn) {
4378
			if ($dn->GetEnabled() == "") {
4379
				continue;
4380
			}
4381
			$tmplist =& $dn->get_queue_list();
4382
			foreach ($tmplist as $qname => $link) {
4383
				$qlist[$qname] = $link;
4384
			}
4385
		}
4386
	}
4387
	return $qlist;
4388
}
4389

    
4390
function ref_on_altq_queue_list($parent, $qname) {
4391
	if (isset($GLOBALS['queue_list'][$qname])) {
4392
		$GLOBALS['queue_list'][$qname]++;
4393
	} else {
4394
		$GLOBALS['queue_list'][$qname] = 1;
4395
	}
4396

    
4397
	unref_on_altq_queue_list($parent);
4398
}
4399

    
4400
function unref_on_altq_queue_list($qname) {
4401
	$GLOBALS['queue_list'][$qname]--;
4402
	if ($GLOBALS['queue_list'][$qname] <= 1) {
4403
		unset($GLOBALS['queue_list'][$qname]);
4404
	}
4405
}
4406

    
4407
function read_altq_config() {
4408
	global $altq_list_queues, $config;
4409
	$path = array();
4410

    
4411
	if (!is_array($config['shaper'])) {
4412
		$config['shaper'] = array();
4413
	}
4414
	if (!is_array($config['shaper']['queue'])) {
4415
		$config['shaper']['queue'] = array();
4416
	}
4417
	$a_int = &$config['shaper']['queue'];
4418

    
4419
	$altq_list_queues = array();
4420

    
4421
	if (!is_array($config['shaper']['queue'])) {
4422
		return;
4423
	}
4424

    
4425
	foreach ($a_int as $key => $conf) {
4426
		$int = $conf['interface'];
4427
		$root =& new altq_root_queue();
4428
		$root->SetInterface($int);
4429
		$altq_list_queues[$root->GetInterface()] = &$root;
4430
		$root->ReadConfig($conf);
4431
		array_push($path, $key);
4432
		$root->SetLink($path);
4433
		if (is_array($conf['queue'])) {
4434
			foreach ($conf['queue'] as $key1 => $q) {
4435
				array_push($path, $key1);
4436
				/*
4437
				 * XXX: we completely ignore errors here but anyway we must have
4438
				 *	checked them before so no harm should be come from this.
4439
				 */
4440
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
4441
				array_pop($path);
4442
			}
4443
		}
4444
		array_pop($path);
4445
	}
4446
}
4447

    
4448
function read_dummynet_config() {
4449
	global $dummynet_pipe_list, $config;
4450
	$path = array();
4451

    
4452
	if (!is_array($config['dnshaper'])) {
4453
		$config['dnshaper'] = array();
4454
	}
4455
	if (!is_array($config['dnshaper']['queue'])) {
4456
		$config['dnshaper']['queue'] = array();
4457
	}
4458
	$a_int = &$config['dnshaper']['queue'];
4459

    
4460
	$dummynet_pipe_list = array();
4461

    
4462
	if (!is_array($config['dnshaper']['queue']) ||
4463
	    !count($config['dnshaper']['queue'])) {
4464
		return;
4465
	}
4466

    
4467
	foreach ($a_int as $key => $conf) {
4468
		if (empty($conf['name'])) {
4469
			continue; /* XXX: grrrrrr at php */
4470
		}
4471
		$root =& new dnpipe_class();
4472
		$root->ReadConfig($conf);
4473
		$dummynet_pipe_list[$root->GetQname()] = &$root;
4474
		array_push($path, $key);
4475
		$root->SetLink($path);
4476
		if (is_array($conf['queue'])) {
4477
			foreach ($conf['queue'] as $key1 => $q) {
4478
				array_push($path, $key1);
4479
				/*
4480
				 * XXX: we completely ignore errors here but anyway we must have
4481
				 *	checked them before so no harm should be come from this.
4482
				 */
4483
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
4484
				array_pop($path);
4485
			}
4486
		}
4487
		array_pop($path);
4488
	}
4489
}
4490

    
4491
function get_interface_list_to_show() {
4492
	global $altq_list_queues, $config;
4493
	global $shaperIFlist;
4494

    
4495
	$tree = "";
4496
	foreach ($shaperIFlist as $shif => $shDescr) {
4497
		if ($altq_list_queues[$shif]) {
4498
			continue;
4499
		} else {
4500
			if (!is_altq_capable(get_real_interface($shif))) {
4501
				continue;
4502
			}
4503
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
4504
		}
4505
	}
4506

    
4507
	return $tree;
4508
}
4509

    
4510
function filter_generate_altq_queues() {
4511
	global $altq_list_queues;
4512

    
4513
	read_altq_config();
4514

    
4515
	$altq_rules = "";
4516
	foreach ($altq_list_queues as $altq) {
4517
		$altq_rules .= $altq->build_rules();
4518
	}
4519

    
4520
	return $altq_rules;
4521
}
4522

    
4523
function dnqueue_find_nextnumber() {
4524
	global $dummynet_pipe_list;
4525

    
4526
	$dnused = array();
4527
	if (is_array($dummynet_pipe_list)) {
4528
		foreach ($dummynet_pipe_list as $dn) {
4529
			$tmplist =& $dn->get_queue_list();
4530
			foreach ($tmplist as $qname => $link) {
4531
				if ($link[0] == "?") {
4532
					$dnused[$qname] = substr($link, 1);
4533
				}
4534
			}
4535
		}
4536
	}
4537

    
4538
	sort($dnused, SORT_NUMERIC);
4539
	$dnnumber = 0;
4540
	$found = false;
4541
	foreach ($dnused as $dnnum) {
4542
		if (($dnnum - $dnnumber) > 1) {
4543
			$dnnumber = $dnnum - 1;
4544
			$found = true;
4545
			break;
4546
		} else {
4547
			$dnnumber = $dnnum;
4548
		}
4549
	}
4550

    
4551
	if ($found == false) {
4552
		$dnnumber++;
4553
	}
4554

    
4555
	unset($dnused, $dnnum, $found);
4556
	return $dnnumber;
4557
}
4558

    
4559
function dnpipe_find_nextnumber() {
4560
	global $dummynet_pipe_list;
4561

    
4562
	$dnused = array();
4563
	foreach ($dummynet_pipe_list as $dn) {
4564
		$dnused[] = $dn->GetNumber();
4565
	}
4566

    
4567
	sort($dnused, SORT_NUMERIC);
4568
	$dnnumber = 0;
4569
	$found = false;
4570
	foreach ($dnused as $dnnum) {
4571
		if (($dnnum - $dnnumber) > 1) {
4572
			$dnnumber = $dnnum - 1;
4573
			$found = true;
4574
			break;
4575
		} else {
4576
			$dnnumber = $dnnum;
4577
		}
4578
	}
4579

    
4580
	if ($found == false) {
4581
		$dnnumber++;
4582
	}
4583

    
4584
	unset($dnused, $dnnum, $found);
4585
	return $dnnumber;
4586
}
4587

    
4588
function filter_generate_dummynet_rules() {
4589
	global $g, $dummynet_pipe_list;
4590

    
4591
	read_dummynet_config();
4592

    
4593
	$dn_rules = "";
4594
	$max_qlimit = "100"; // OS default
4595
	foreach ($dummynet_pipe_list as $dn) {
4596
		$dn_rules .= $dn->build_rules();
4597
		$this_qlimit = $dn->GetQlimit();
4598
		if ($this_qlimit > $max_qlimit) {
4599
			$max_qlimit = $this_qlimit;
4600
		}
4601
	}
4602
	if (!is_numericint($max_qlimit)) {
4603
		$max_qlimit = "100";
4604
	}
4605
	if (!empty($dn_rules)) {
4606
		if (!is_module_loaded("dummynet.ko")) {
4607
			mwexec("/sbin/kldload dummynet");
4608
		}
4609
		set_sysctl(array(
4610
				"net.inet.ip.dummynet.io_fast" => "1",
4611
				"net.inet.ip.dummynet.hash_size" => "256",
4612
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
4613
		));
4614
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
4615
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
4616
	}
4617
}
4618

    
4619
function build_iface_without_this_queue($iface, $qname) {
4620
	global $g, $altq_list_queues;
4621
	global $shaperIFlist;
4622

    
4623
	$altq =& $altq_list_queues[$iface];
4624

    
4625
	if ($altq) {
4626
		$scheduler = $altq->GetScheduler();
4627
	}
4628

    
4629
	$form = '<dl class="dl-horizontal">';
4630

    
4631
	$form .= '	<dt>';
4632
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
4633
	$form .= '	</dt>';
4634
	$form .= '	<dd>';
4635
	$form .=		$scheduler;
4636
	$form .= '	</dd>';
4637

    
4638
	$form .= '	<dt>';
4639
	$form .= 'Clone';
4640
	$form .= '	</dt>';
4641
	$form .= '	<dd>';
4642
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
4643
	$form .= $iface . '&amp;queue=';
4644
	$form .= $qname . '&amp;action=add">';
4645
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
4646
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
4647
	$form .= '	</dd>';
4648

    
4649
	$form .= '</dl>';
4650

    
4651
	return $form;
4652

    
4653
}
4654

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

    
4658
$shaper_msg = gettext("The tree on the left navigates through the %s.");
4659
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
4660
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
4661

    
4662
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
4663
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
4664
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
4665

    
4666
// Check to see if the specified interface has a queue configured
4667
function interface_has_queue($if) {
4668
	global $config;
4669

    
4670
	if ($config['shaper']) {
4671
		foreach ($config['shaper']['queue'] as $queue) {
4672
			if ($queue['interface'] === $if) {
4673
				return true;
4674
			}
4675
		}
4676
	}
4677

    
4678
	return false;
4679
}
4680
?>
(42-42/54)