Project

General

Profile

Download (142 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * shaper.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
/* XXX: needs some reducing on include. */
29
/* include all configuration functions. */
30
require_once("globals.inc");
31
require_once("functions.inc");
32
require_once("util.inc");
33
require_once("notices.inc");
34
include_once("interfaces.inc");//required for convert_real_interface_to_friendly_interface_name() in get_queue_stats() to get altq interface name.
35

    
36
/* Limiter patch */
37
// I need this crazy looking model/struct to specify various algos, ecn caps, and params.
38
// update as needed when dummynet changes
39

    
40
// List of schedulers ('type' command on scheduler/pipe) that dummynet/ipfw is supposed to support
41
function getSchedulers() {
42
	return array(
43
		"wf2q+" => array(
44
			"name" => "Worst-case Weighted fair Queueing (default)",
45
			"parameter_format" => "type wf2q+",
46
			"description" => "Worst-case Weighted fair Queueing (WF2Q+) schedules flows with an associated weight. ".
47
					 "WF2Q+ is the default algorithm used by previous versions.",
48
			"parameters" => array(),
49
			"ecn" => false
50
		),
51
		"fifo" => array(
52
			"name" => "FIFO",
53
			"parameter_format" => "type fifo",
54
			"description" => "First-in-First-out is a fundamental queueing discipline which ensures packet sequence. ".
55
					 "Dynamic queues are not supported with the FIFO scheduler (all packets are stored in a single queue).",
56
			"parameters" => array(),
57
			"ecn" => false
58
		),
59
		"qfq" => array(
60
			"name" => "Quick Fair Queueing",
61
			"parameter_format" => "type qfq",
62
			"description" => "QFQ is a fast, low-complexity Approximated Fair Queueing scheduler.",
63
			"parameters" => array(),
64
			"ecn" => false
65
		),
66
		"rr" => array(
67
			"name" => "Round Robin",
68
			"parameter_format" => "type rr",
69
			"description" => "Round Robin (RR) schedules packets in a round-robin fashion.",
70
			"parameters" => array(),
71
			"ecn" => false
72
		),
73
		"prio" => array(
74
			"name" => "PRIO",
75
			"parameter_format" => "type prio",
76
			"description" => "PRIO schedules packets by their corresponding priority.",
77
			"parameters" => array(),
78
			"ecn" => false
79
		),
80
		"fq_codel" => array(
81
			"name" => "FQ_CODEL",
82
			"parameter_format" => "type fq_codel target %sms interval %sms quantum %s limit %s flows %s",
83
			"description" => "CoDel is a novel \"no knobs\", \"just works\", \"handles variable bandwidth and RTT\", and simple AQM algorithm."
84
							. " As a scheduler, FQ_CODEL implements several flows (defined below), each having their own CoDel AQM.",
85
			"parameters" => array(
86
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqcodel.target")) / 1000),
87
				"interval" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqcodel.interval")) / 1000),
88
				"quantum" => array("name" => "quantum", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqcodel.quantum")),
89
				"limit" => array("name" => "limit", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqcodel.limit")),
90
				"flows" => array("name" => "flows", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqcodel.flows"))
91
			),
92
			"ecn" => true
93
		),
94
		"fq_pie" => array(
95
			"name" => "FQ_PIE",
96
			"parameter_format" => "type fq_pie target %sms tupdate %sms alpha %s beta %s max_burst %s max_ecnth %s",
97
			"description" => "Fair Queue Proportional Integral controller Enhanced AQM (FQ_PIE) is similar to FQ_CODEL but uses a slightly different algorithm.",
98
			"parameters" => array(
99
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.target")) / 1000),
100
				"tupdate" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.tupdate")) / 1000),
101
				"alpha" => array("name" => "alpha", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.alpha")),
102
				"beta" => array("name" => "beta", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.beta")),
103
				"max_burst" => array("name" => "max_burst", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.max_burst")),
104
				"max_ecnth" => array("name" => "max_ecnth", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.max_ecnth"))
105
			),
106
			"ecn" => true
107
		)
108
	);
109
}
110
// list of AQMs that dummynet supports
111
function getAQMs() {
112
	return array(
113
		"droptail" => array(
114
			"name" => "Tail Drop",
115
			"description" => "Tail Drop is a fundamental queue management algorithm which drops inbound packets once the queue is full.",
116
			"parameter_format" => "droptail",
117
			"parameters" => array(),
118
			"ecn" => false
119
		),
120
		"codel" => array(
121
			"name" => "CoDel",
122
			"description" => "CoDel is a novel \"no knobs\", \"just works\", \"handles variable bandwidth and RTT\", and simple AQM algorithm.",
123
			"parameter_format" => "codel target %sms interval %sms",
124
			"parameters" => array(
125
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.codel.target")) / 1000),
126
				"interval" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.codel.interval")) / 1000),
127
			),
128
			"ecn" => true
129
		),
130
		"pie" => array(
131
			"name" => "PIE",
132
			"description" => "Proportional Integral controller Enhanced AQM (PIE) is similar to CoDel but uses a slightly different algorithm.",
133
			"parameter_format" => "pie target %sms tupdate %sms alpha %s beta %s max_burst %s max_ecnth %s",
134
			"parameters" => array(
135
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.target")) / 1000),
136
				"tupdate" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.tupdate")) / 1000),
137
				"alpha" => array("name" => "alpha", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.alpha")),
138
				"beta" => array("name" => "beta", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.beta")),
139
				"max_burst" => array("name" => "max_burst", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.max_burst")),
140
				"max_ecnth" => array("name" => "max_ecnth", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.max_ecnth"))
141
			),
142
			"ecn" => true
143
		),
144
		"red" => array(
145
			"name" => "Random Early Detection (RED)",
146
			"description" => "Random Early Detection (RED) drops packets based on probability, which increases as the queue increases in size.",
147
			"parameter_format" => "red %s/%s/%s/%s",
148
			"parameters" => array(
149
				"w_q" => array("name" => "w_q", "type" => "number", "default" => 1),
150
				"min_th" => array("name" => "min_th", "type" => "number", "default" => 0),
151
				"max_th" => array("name" => "max_th", "type" => "number", "default" => 1),
152
				"max_p" => array("name" => "max_p", "type" => "number", "default" => 1)
153
			),
154
			"ecn" => true
155
		),
156
		"gred" => array(
157
			"name" => "Gentle Random Early Detection (GRED)",
158
			"description" => "Gentle Random Early Detection (GRED) drops packets based on probability, which increases as the queue increases in size.",
159
			"parameter_format" => "gred %s/%s/%s/%s",
160
			"parameters" => array(
161
				"w_q" => array("name" => "w_q", "type" => "number", "default" => 1),
162
				"min_th" => array("name" => "min_th", "type" => "number", "default" => 0),
163
				"max_th" => array("name" => "max_th", "type" => "number", "default" => 1),
164
				"max_p" => array("name" => "max_p", "type" => "number", "default" => 1)
165
			),
166
			"ecn" => true
167
		)
168
	);
169
}
170
// used to map above
171
function array_map_assoc(callable $f, array $a) {
172
	return array_column(array_map($f, array_keys($a), $a), 1, 0);
173
}
174
function build_queue_params($array, $selected, $params, $id) {
175
	$form .= '<div id="params_' . $id . '">';
176

    
177
	$selectedAlgorithm = $array[$selected];
178

    
179
	if ($selectedAlgorithm) {
180
		$form .= '<span class="help-block">' . gettext($selectedAlgorithm['description']) . '</span><br/>';
181
	}
182

    
183
	$parameters = $selectedAlgorithm["parameters"];
184

    
185
	if (!$parameters || sizeof($parameters) <= 0) {
186
		if (!$selectedAlgorithm) {
187
			$form .= 'No parameters for selected algorithm.';
188
		} else {
189
			$form .= 'No parameters for ' . $selectedAlgorithm["name"] . '.';
190
		}
191
	} else {
192
		$form .= '<div class="table-responsive">';
193
		$form .= '<table id="maintable" class="table table-hover table-striped">';
194
		$form .= "<thead><tr>";
195
		$form .= "<th>Parameter</th>";
196
		$form .= "<th>Value</th>";
197
		$form .= "</tr></thead>";
198
		$form .= "<tbody>";
199

    
200
		foreach ($parameters as $key => $value) {
201
			$form .= '<tr>';
202

    
203
			// get current value, or default.
204
			$currentValue = $params[$key];
205
			if (!$currentValue || $currentValue == '') {
206
				$currentValue = $value["default"]; // default to default
207
			}
208

    
209
			$form .= "<td class=\"col-xs-4\">" . htmlspecialchars($key) . "</td>";
210
			$form .= "<td class=\"col-xs-8\"><input class=\"form-control\" type=" . gettext($value["type"])
211
					. " name=\"param_" . $selected . "_" . $key . "\" placeholder=\"" .
212
					htmlspecialchars($value["default"]) . "\" value=\"" . htmlspecialchars($currentValue) . "\"/></td>";
213

    
214
			$form .= '</tr>';
215
		}
216

    
217
		$form .= "</tbody></table></div><br />";
218
	}
219

    
220
	$form .= '</div>';
221
	$form .= '<script type="text/javascript">';
222
	$form .= 'events.push(function() {$("#' . $id . '").change(function() {$("#params_' .
223
		gettext($id) . '").html("Save this limiter to see algorithm parameters.");})});</script>';
224

    
225
	return($form);
226
}
227
function FormatParameters($format_string, $params) {
228
	return vsprintf($format_string, $params);
229
}
230
/* End limiter patch */
231

    
232
function get_queue_stats() {
233
	// due to sysutils\qstats not providing accurate stats with 'currently' valid numbers
234
	// in its current implementation this php function does the job for now..
235
	$result = array();
236
	$result['timestamp'] = gettimeofday(true);
237
	$r = exec("/sbin/pfctl -s queue -v", $output, $ret);
238
	$interfacestats = array();
239
	foreach($output as $line) {
240
		$partial = explode('{', $line);
241
		$items = explode(" ", $partial[0]);
242
		if (isset($partial[1])) {
243
			$contains = explode(", ", substr($partial[1], 0, strlen($partial[1]) - 1));
244
		} else {
245
			unset($contains);
246
		}
247
		if ($items[0] == "queue") {
248
			$level = 1;
249
			while (empty($items[$level])) {
250
				$level++;
251
			}
252
			unset($newqueue);
253
			$newqueue = array();
254
			$currentqueuename = $items[$level];
255
			$currentqueueif = $items[$level+2];
256
			$newqueue['name'] = $currentqueuename;
257
			$newqueue['interface'] = $items[$level+2];
258

    
259
			if ($items[$level+3] == "bandwidth") {
260
				$level += 2;
261
			}
262
			if ($items[$level+3] == "priority") {
263
				$level += 2;
264
			}
265
			if ($items[$level+3] == "qlimit") {
266
				$level += 2;
267
			}
268
			$tmp = $items[$level+3];
269
			if (substr($tmp,strlen($tmp)-1) == "(") {
270
				$newqueue['shapertype'] = substr($tmp, 0, strlen($tmp)-1);
271
			}
272

    
273
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
274
			if (is_array($contains)) {
275
				$newqueue['contains'] = $contains;
276
			}
277
		} elseif ($items[0] == "altq") {
278
			unset($newqueue);
279
			$newqueue = array();
280
			$currentqueuename = convert_real_interface_to_friendly_interface_name($items[2]);
281
			$currentqueueif = $items[2];
282
			$newqueue['name'] = $currentqueuename;
283
			$newqueue['interface'] = $items[2];
284
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
285
		} else {
286
			if ($items[3] == "pkts:") {
287
				preg_match('/pkts:\s+(\d+)\s+bytes:\s+(\d+)\s+dropped pkts:\s+(\d+)\s+bytes:\s+(\d+)/', $line, $matches);
288
				$newqueue["pkts"] = $matches[1];
289
				$newqueue["bytes"] = $matches[2];
290
				$newqueue["droppedpkts"] = $matches[3];
291
				$newqueue["droppedbytes"] = $matches[4];
292
			}
293
			if ($items[3] == "qlength:") {
294
				$subitems = explode("/", substr($line, 13, 8));
295
				$newqueue["qlengthitems"] = trim($subitems[0]);
296
				$newqueue["qlengthsize"] = trim($subitems[1]);
297
				if (substr($line, 22, 8) == "borrows:") {
298
					$newqueue["borrows"] = trim(substr($line, 31, 7));
299
				}
300
				if (substr($line, 39, 9) == "suspends:") {
301
					$newqueue["suspends"] = trim(substr($line, 49, 7));
302
				}
303
			}
304
		}
305
	}
306
	$result['interfacestats'] = $interfacestats;
307
	return $result;
308
}
309

    
310
/*
311
 * I admit :) this is derived from xmlparse.inc StartElement()
312
 */
313
function &get_reference_to_me_in_config(&$mypath) {
314
	global $config;
315

    
316
	init_config_arr(array('shaper'));
317
	$ptr = &$config['shaper'];
318
	foreach ($mypath as $indeks) {
319
		if (!is_array($ptr)) {
320
			$ptr = array();
321
		}
322
		if (!is_array($ptr['queue'])) {
323
			$ptr['queue'] = array();
324
		}
325
		if (!is_array($ptr['queue'][$indeks])) {
326
			$ptr['queue'][$indeks] = array();
327
		}
328
		$ptr = &$ptr['queue'][$indeks];
329
	}
330

    
331
	return $ptr;
332
}
333

    
334
function unset_object_by_reference(&$mypath) {
335
	global $config;
336

    
337
	init_config_arr(array('shaper'));
338
	$ptr = &$config['shaper'];
339
	for ($i = 0; $i < count($mypath) - 1; $i++) {
340
		$ptr = &$ptr['queue'][$mypath[$i]];
341
	}
342
	unset($ptr['queue'][$mypath[$i]]);
343
}
344

    
345
function &get_dn_reference_to_me_in_config(&$mypath) {
346
	global $config;
347

    
348
	init_config_arr(array('dnshaper'));
349
	$ptr = &$config['dnshaper'];
350
	foreach ($mypath as $indeks) {
351
		$ptr = &$ptr['queue'][$indeks];
352
	}
353

    
354
	return $ptr;
355
}
356

    
357
function unset_dn_object_by_reference(&$mypath) {
358
	global $config;
359

    
360
	init_config_arr(array('dnshaper'));
361
	$ptr = &$config['dnshaper'];
362
	for ($i = 0; $i < count($mypath) - 1; $i++) {
363
		$ptr = &$ptr['queue'][$mypath[$i]];
364
	}
365
	unset($ptr['queue'][$mypath[$i]]);
366
}
367

    
368
function clean_child_queues($type, $mypath) {
369
	$ref = &get_reference_to_me_in_config($mypath);
370

    
371
	switch ($type) {
372
		case 'HFSC':
373
			if (isset($ref['borrow'])) {
374
				unset($ref['borrow']);
375
			}
376
			if (isset($ref['hogs'])) {
377
				unset($ref['hogs']);
378
			}
379
			if (isset($ref['buckets'])) {
380
				unset($ref['buckets']);
381
			}
382
			break;
383
		case 'PRIQ':
384
			if (isset($ref['borrow'])) {
385
				unset($ref['borrow']);
386
			}
387
			if (isset($ref['bandwidth'])) {
388
				unset($ref['bandwidth']);
389
			}
390
			if (isset($ref['bandwidthtype'])) {
391
				unset($ref['bandwidthtype']);
392
			}
393
			/* fall through */
394
		case 'FAIRQ':
395
			if (isset($ref['borrow'])) {
396
				unset($ref['borrow']);
397
			}
398
			/* fall through */
399
		case 'CBQ':
400
			if (isset($ref['realtime'])) {
401
				unset($ref['realtime']);
402
			}
403
			if (isset($ref['realtime1'])) {
404
				unset($ref['realtime1']);
405
			}
406
			if (isset($ref['realtime2'])) {
407
				unset($ref['realtime2']);
408
			}
409
			if (isset($ref['realtime3'])) {
410
				unset($ref['realtime3']);
411
			}
412
			if (isset($ref['upperlimit'])) {
413
				unset($ref['upperlimit']);
414
			}
415
			if (isset($ref['upperlimit1'])) {
416
				unset($ref['upperlimit1']);
417
			}
418
			if (isset($ref['upperlimit2'])) {
419
				unset($ref['upperlimit2']);
420
			}
421
			if (isset($ref['upperlimit3'])) {
422
				unset($ref['upperlimit3']);
423
			}
424
			if (isset($ref['linkshare'])) {
425
				unset($ref['linkshare']);
426
			}
427
			if (isset($ref['linkshare1'])) {
428
				unset($ref['linkshare1']);
429
			}
430
			if (isset($ref['linkshare2'])) {
431
				unset($ref['linkshare2']);
432
			}
433
			if (isset($ref['linkshare3'])) {
434
				unset($ref['linkshare3']);
435
			}
436
			if (isset($ref['hogs'])) {
437
				unset($ref['hogs']);
438
			}
439
			if (isset($ref['buckets'])) {
440
				unset($ref['buckets']);
441
			}
442
			break;
443
	}
444
}
445

    
446
function get_bandwidthtype_scale($type) {
447
	switch ($type) {
448
		case "Gb":
449
			$factor = 1024 * 1024 * 1024;
450
			break;
451
		case "Mb":
452
			$factor = 1024 * 1024;
453
			break;
454
		case "Kb":
455
			$factor = 1024;
456
			break;
457
		case "b":
458
		default:
459
			$factor = 1;
460
			break;
461
	}
462
	return intval($factor);
463
}
464

    
465
function get_bandwidth($bw, $scale, $obj) {
466
	$bw = (int) $bw;
467
	$pattern= "/(b|Kb|Mb|Gb|%)/";
468
	if (!preg_match($pattern, $scale, $match))
469
		return 0;
470

    
471
	switch ($match[1]) {
472
		case '%':
473
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
474
			break;
475
		default:
476
			$objbw = $bw * get_bandwidthtype_scale($scale);
477
			break;
478
	}
479

    
480
	return floatval($objbw);
481
}
482

    
483
/*
484
 * XXX - unused
485
 *
486
function get_hfsc_bandwidth($object, $bw) {
487
	$pattern= "/[0-9]+/";
488
	if (preg_match($pattern, $bw, $match)) {
489
		$bw_1 = $match[1];
490
	} else {
491
		return 0;
492
	}
493
	$pattern= "/(b|Kb|Mb|Gb|%)/";
494
	if (preg_match($pattern, $bw, $match)) {
495
		switch ($match[1]) {
496
			case '%':
497
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
498
				break;
499
			default:
500
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
501
				break;
502
		}
503
		return floatval($bw_1);
504
	} else {
505
		return 0;
506
	}
507
}
508
*/
509

    
510
function get_queue_bandwidth($obj) {
511
	$bw = $obj->GetBandwidth();
512
	$scale = $obj->GetBwscale();
513

    
514
	$pattern= "/(b|Kb|Mb|Gb|%)/";
515
	if (!preg_match($pattern, $scale, $match))
516
		return 0;
517

    
518
	switch ($match[1]) {
519
		case '%':
520
			if (method_exists($obj, 'GetParent')) {
521
				$getobjbw = get_queue_bandwidth($obj->GetParent());
522
			} else {
523
				$getobjbw = $obj->bandwidth;
524
			}
525
			$objbw = ($bw / 100) * $getobjbw;
526
			break;
527
		default:
528
			$objbw = $bw * get_bandwidthtype_scale($scale);
529
			break;
530
	}
531

    
532
	return floatval($objbw);
533
}
534

    
535
function get_interface_bandwidth($object) {
536
	global $altq_list_queues;
537

    
538
	$int = $object->GetInterface();
539
	if (isset($altq_list_queues[$int])) {
540
		$altq = &$altq_list_queues[$int];
541
		$bw_3 = $altq->GetBandwidth();
542
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
543
		return floatval($bw_3);
544
	} else {
545
		return 0;
546
	}
547
}
548

    
549
/*
550
 * This is duplicated here since we cannot include guiconfig.inc.
551
 * Including it makes all stuff break.
552
 */
553
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
554

    
555
	/* check for bad control characters */
556
	foreach ($postdata as $pn => $pd) {
557
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
558
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
559
		}
560
	}
561

    
562
	for ($i = 0; $i < count($reqdfields); $i++) {
563
		if ($postdata[$reqdfields[$i]] == "") {
564
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
565
		}
566
	}
567
}
568

    
569
function cleanup_queue_from_rules($queue) {
570
	global $config;
571

    
572
	foreach ($config['filter']['rule'] as & $rule) {
573
		if ($rule['defaultqueue'] == $queue) {
574
			unset($rule['defaultqueue']);
575
			if (isset($rule['ackqueue'])) {
576
				unset($rule['ackqueue']);
577
			}
578
		}
579
		if ($rule['ackqueue'] == $queue) {
580
			unset($rule['ackqueue']);
581
		}
582
	}
583
}
584

    
585
function cleanup_dnqueue_from_rules($queue) {
586
	global $config;
587

    
588
	foreach ($config['filter']['rule'] as & $rule) {
589
		if ($rule['dnpipe'] == $queue) {
590
			unset($rule['dnpipe']);
591
		}
592
		if ($rule['pdnpipe'] == $queue) {
593
			unset($rule['pdnpipe']);
594
		}
595
	}
596
}
597

    
598
function rename_queue_in_rules($name, $newname) {
599
	global $config;
600

    
601
	foreach ($config['filter']['rule'] as & $rule) {
602
		if ($rule['defaultqueue'] == $name) {
603
			$rule['defaultqueue'] = $newname;
604
		}
605
		if ($rule['ackqueue'] == $name) {
606
			$rule['ackqueue'] = $newname;
607
		}
608
	}
609
}
610

    
611
function rename_dnqueue_in_rules($name, $newname) {
612
	global $config;
613

    
614
	foreach ($config['filter']['rule'] as & $rule) {
615
		if ($rule['dnpipe'] == $name) {
616
			$rule['dnpipe'] = $newname;
617
		}
618
		if ($rule['pdnpipe'] == $name) {
619
			$rule['pdnpipe'] = $newname;
620
		}
621
	}
622
}
623

    
624
class altq_root_queue {
625
	var $interface;
626
	var $tbrconfig ;
627
	var $bandwidth;
628
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
629
	var $scheduler;
630
	var $qlimit;
631
	var $queues = array();
632
	var $qenabled = false;
633
	var $link;
634

    
635
	/* Accessor functions */
636
	function GetDefaultQueuePresent() {
637
		if (!empty($this->queues)) {
638
			foreach ($this->queues as $q) {
639
				if ($q->GetDefault()) {
640
					return true;
641
				}
642
			}
643
		}
644

    
645
		return false;
646
	}
647
	function SetLink($link) {
648
		$this->link = $link;
649
	}
650
	function GetLink() {
651
		return $this->link;
652
	}
653
	function GetEnabled() {
654
		return $this->qenabled;
655
	}
656
	function SetEnabled($value) {
657
		$this->qenabled = $value;
658
	}
659
	function CanHaveChildren() {
660
		if ($this->GetScheduler() == "CODELQ") {
661
			return false;
662
		} else {
663
			return true;
664
		}
665
	}
666
	function CanBeDeleted() {
667
		return false;
668
	}
669
	function GetQname() {
670
		return $this->interface;
671
	}
672
	function SetQname($name) {
673
		$this->interface = trim($name);
674
	}
675
	function GetInterface() {
676
		return $this->interface;
677
	}
678
	function SetInterface($name) {
679
		$this->interface = trim($name);
680
	}
681
	function GetTbrConfig() {
682
		return $this->tbrconfig;
683
	}
684
	function SetTbrConfig($tbrconfig) {
685
		$this->tbrconfig = $tbrconfig;
686
	}
687
	function GetBandwidth() {
688
		return $this->bandwidth;
689
	}
690
	function SetBandwidth($bw) {
691
		$this->bandwidth = $bw;
692
	}
693
	function GetBwscale() {
694
		return $this->bandwidthtype;
695
	}
696
	function FormGetBwscale() {
697
		if ($this->GetBwscale()) {
698
			$bwscale = $this->GetBwscale();
699
		} else {
700
			$bwscale = 'Mb';
701
		}
702
		return $bwscale;
703
	}
704
	function SetBwscale($bwscale) {
705
		$this->bandwidthtype = $bwscale;
706
	}
707
	function GetScheduler() {
708
		return $this->scheduler;
709
	}
710
	function SetScheduler($scheduler) {
711
		$this->scheduler = trim($scheduler);
712
	}
713
	function GetQlimit() {
714
		return $this->qlimit;
715
	}
716
	function SetQlimit($limit) {
717
		$this->qlimit = $limit;
718
	}
719

    
720
	function GetBwscaleText() {
721
		switch ($this->bandwidthtype) {
722
			case "b":
723
				$bwscaletext = "Bit/s";
724
				break;
725
			case "Kb":
726
				$bwscaletext = "Kbit/s";
727
				break;
728
			case "Mb":
729
				$bwscaletext = "Mbit/s";
730
				break;
731
			case "Gb":
732
				$bwscaletext = "Gbit/s";
733
				break;
734
			default:
735
				/* For others that do not need translating like % */
736
				$bwscaletext = $this->bandwidthtype;
737
				break;
738
		}
739
		return $bwscaletext;
740
	}
741

    
742
	function CheckBandwidth($bw, $bwtype) {
743
		$sum = $this->GetTotalBw();
744
		if ($sum > ($bw * get_bandwidthtype_scale($bwtype))) {
745
			return 1;
746
		}
747
		foreach ($this->queues as $q) {
748
			if ($q->CheckBandwidth(0, '')) {
749
				return 1;
750
			}
751
		}
752

    
753
		return 0;
754
	}
755

    
756
	function GetTotalBw($qignore = NULL) {
757
		$sum = 0;
758
		foreach ($this->queues as $q) {
759
			if (($qignore != NULL) && ($qignore == $q)) {
760
				continue;
761
			}
762
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
763
		}
764

    
765
		return $sum;
766
	}
767

    
768
	function validate_input($data, &$input_errors) {
769

    
770
		$reqdfields[] = "bandwidth";
771
		$reqdfieldsn[] = gettext("Bandwidth");
772
		$reqdfields[] = "bandwidthtype";
773
		$reqdfieldsn[] = gettext("Bandwidthtype");
774

    
775
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
776

    
777
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
778
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
779
		}
780
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
781
			$input_errors[] = gettext("Bandwidth must be an integer.");
782
		}
783
		if ($data['bandwidth'] < 0) {
784
			$input_errors[] = gettext("Bandwidth cannot be negative.");
785
		}
786
		if ($data['bandwidthtype'] == "%") {
787
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
788
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
789
			}
790
		}
791
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
792
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
793

    
794
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
795
			$input_errors[] = gettext("Qlimit must be an integer.");
796
		}
797
		if ($data['qlimit'] < 0) {
798
			$input_errors[] = gettext("Qlimit must be positive.");
799
		}
800
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
801
			$input_errors[] = gettext("Tbrsize must be an integer.");
802
		}
803
		if ($data['tbrconfig'] < 0) {
804
			$input_errors[] = gettext("Tbrsize must be positive.");
805
		}
806
	}
807

    
808
	/* Implement this to shorten some code on the frontend page */
809
	function ReadConfig(&$conf) {
810
		if (isset($conf['tbrconfig'])) {
811
			$this->SetTbrConfig($conf['tbrconfig']);
812
		} else {
813
			$this->SetTbrConfig($conf['tbrconfig']);
814
		}
815
		$this->SetBandwidth($conf['bandwidth']);
816
		if ($conf['bandwidthtype'] <> "") {
817
			$this->SetBwscale($conf['bandwidthtype']);
818
		}
819
		if (isset($conf['scheduler'])) {
820
			if ($this->GetScheduler() != $conf['scheduler']) {
821
				foreach ($this->queues as $q) {
822
					clean_child_queues($conf['scheduler'], $this->GetLink());
823
					$q->clean_queue($conf['scheduler']);
824
				}
825
			}
826
			$this->SetScheduler($conf['scheduler']);
827
		}
828
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
829
			$this->SetQlimit($conf['qlimit']);
830
		} else {
831
			$this->SetQlimit("");
832
		}
833
		if (isset($conf['name'])) {
834
			$this->SetQname($conf['name']);
835
		}
836
		if (!empty($conf['enabled'])) {
837
			$this->SetEnabled($conf['enabled']);
838
		} else {
839
			$this->SetEnabled("");
840
		}
841
	}
842

    
843
	function copy_queue($interface, &$cflink) {
844
		$cflink['interface'] = $interface;
845
		$cflink['name'] = $interface;
846
		$cflink['scheduler'] = $this->GetScheduler();
847
		$cflink['bandwidth'] = $this->GetBandwidth();
848
		$cflink['bandwidthtype'] = $this->GetBwscale();
849
		$cflink['qlimit'] = $this->GetQlimit();
850
		$cflink['tbrconfig'] = $this->GetTbrConfig();
851
		$cflink['enabled'] = $this->GetEnabled();
852
		if (is_array($this->queues)) {
853
			$cflink['queue'] = array();
854
			foreach ($this->queues as $q) {
855
				$cflink['queue'][$q->GetQname()] = array();
856
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
857
			}
858
		}
859
	}
860

    
861
	function &get_queue_list(&$q = null) {
862
		$qlist = array();
863

    
864
		//$qlist[$this->GetQname()] = & $this;
865
		if (is_array($this->queues)) {
866
			foreach ($this->queues as $queue) {
867
				$queue->get_queue_list($qlist);
868
			}
869
		}
870
		return $qlist;
871
	}
872

    
873
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
874

    
875
		if (!is_array($this->queues)) {
876
			$this->queues = array();
877
		}
878

    
879
		switch ($this->GetScheduler()) {
880
			case "PRIQ":
881
				$__tmp_q = new priq_queue(); $q =& $__tmp_q;
882
				break;
883
			case "HFSC":
884
				$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
885
				break;
886
			case "CBQ":
887
				$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
888
				break;
889
			case "FAIRQ":
890
				$__tmp_q = new fairq_queue(); $q =& $__tmp_q;
891
				break;
892
			default:
893
				/* XXX: but should not happen anyway */
894
				return;
895
				break;
896
		}
897
		$q->SetLink($path);
898
		$q->SetInterface($this->GetInterface());
899
		$q->SetEnabled("on");
900
		$q->SetParent($this);
901
		$q->ReadConfig($queue);
902
		$q->validate_input($queue, $input_errors);
903

    
904
		$this->queues[$q->GetQname()] = &$q;
905
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
906
		if (is_array($queue['queue'])) {
907
			foreach ($queue['queue'] as $key1 => $que) {
908
				array_push($path, $key1);
909
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
910
				array_pop($path);
911
			}
912
		}
913

    
914
		return $q;
915
	}
916

    
917
	/* interface here might be optional */
918
	function &find_queue($interface, $qname) {
919
		if ($qname == $this->GetQname()) {
920
			return $this;
921
		}
922
		foreach ($this->queues as $q) {
923
			$result =& $q->find_queue("", $qname);
924
			if ($result) {
925
				return $result;
926
			}
927
		}
928
	}
929

    
930
	function &find_parentqueue($interface, $qname) {
931
		if ($qname == $interface) {
932
			$result = NULL;
933
		} else if ($this->queues[$qname]) {
934
			$result = $this;
935
		} else if ($this->GetScheduler() <> "PRIQ") {
936
			foreach ($this->queues as $q) {
937
				$result = $q->find_parentqueue("", $qname);
938
				if ($result) {
939
					return $result;
940
				}
941
			}
942
		}
943
	}
944

    
945
	function build_tree() {
946
		global $shaperIFlist;
947

    
948
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
949
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
950
		if (is_array($this->queues)) {
951
			$tree .= "<ul>";
952
			foreach ($this->queues as $q) {
953
				$tree .= $q->build_tree();
954
			}
955
			$tree .= "</ul>";
956
		}
957
		$tree .= "</li>";
958
		return $tree;
959
	}
960

    
961
	function delete_queue() {
962
		foreach ($this->queues as $q) {
963
			$q->delete_queue();
964
		}
965
		unset_object_by_reference($this->GetLink());
966
	}
967

    
968
	function delete_all() {
969
		if (count($this->queues)) {
970
			foreach ($this->queues as $q) {
971
				$q->delete_all();
972
				unset_object_by_reference($q->GetLink());
973
				unset($q);
974
			}
975
			unset($this->queues);
976
		}
977
	}
978

    
979
	/*
980
	 * First it spits:
981
	 * altq on $interface ..............
982
	 *	then it goes like
983
	 *	foreach ($queues as $qkey => $queue) {
984
	 *		this->queues[$qkey]->build_rule();
985
	 *	}
986
	 */
987
	function build_rules(&$default = false) {
988
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
989
			$default = false;
990
			$rules = " altq on " . get_real_interface($this->GetInterface());
991
			if ($this->GetScheduler()) {
992
				$rules .= " ".strtolower($this->GetScheduler());
993
			}
994
			if ($this->GetQlimit() > 0) {
995
				$rules .= " qlimit " . $this->GetQlimit() . " ";
996
			}
997
			if ($this->GetBandwidth()) {
998
				$rules .= " bandwidth ".trim($this->GetBandwidth());
999
				if ($this->GetBwscale()) {
1000
					$rules .= $this->GetBwscale();
1001
				}
1002
			}
1003
			if ($this->GetTbrConfig()) {
1004
				$rules .= " tbrsize ".$this->GetTbrConfig();
1005
			}
1006
			if (count($this->queues)) {
1007
				$i = count($this->queues);
1008
				$rules .= " queue { ";
1009
				foreach ($this->queues as $qkey => $qnone) {
1010
					if ($i > 1) {
1011
						$i--;
1012
						$rules .= " {$qkey}, ";
1013
					} else {
1014
						$rules .= " {$qkey} ";
1015
					}
1016
				}
1017
				$rules .= " } \n";
1018
				foreach ($this->queues as $q) {
1019
					$rules .= $q->build_rules($default);
1020
				}
1021
			}
1022

    
1023
			if ($default == false) {
1024
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
1025
				file_notice("Shaper", $error, "Error occurred", "");
1026
				unset($error);
1027
				return "\n";
1028
			}
1029
			$frule .= $rules;
1030
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
1031
			$rules = " altq on " . get_real_interface($this->GetInterface());
1032
			if ($this->GetScheduler()) {
1033
				$rules .= " ".strtolower($this->GetScheduler());
1034
			}
1035
			if ($this->GetQlimit() > 0) {
1036
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
1037
			}
1038
			if ($this->GetBandwidth()) {
1039
				$rules .= " bandwidth ".trim($this->GetBandwidth());
1040
				if ($this->GetBwscale()) {
1041
					$rules .= $this->GetBwscale();
1042
				}
1043
			}
1044
			if ($this->GetTbrConfig()) {
1045
				$rules .= " tbrsize ".$this->GetTbrConfig();
1046
			}
1047

    
1048
			$rules .= " queue";
1049
		}
1050

    
1051
		$rules .= " \n";
1052
		return $rules;
1053
	}
1054

    
1055
	function build_javascript() {
1056
		$javascript = "<script type=\"text/javascript\">";
1057
		$javascript .= "//<![CDATA[\n";
1058
		$javascript .= "function mySuspend() {";
1059
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1060
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
1061
		$javascript .= "else if (document.all)";
1062
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
1063
		$javascript .= "}";
1064

    
1065
		$javascript .= "function myResume() {";
1066
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1067
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
1068
		$javascript .= "else if (document.all) ";
1069
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
1070
		$javascript .= "}";
1071
		$javascript .= "//]]>";
1072
		$javascript .= "</script>";
1073

    
1074
		return $javascript;
1075
	}
1076

    
1077
	function build_shortform() {
1078
		global $g;
1079

    
1080
		$altq =& $this;
1081

    
1082
		if ($altq) {
1083
			$scheduler = ": " . $altq->GetScheduler();
1084
		}
1085

    
1086
		$form = '<dl class="dl-horizontal">';
1087
		$form .= '	<dt>';
1088
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1089
		$form .= '	</dt>';
1090
		$form .= '	<dd>';
1091
		$form .=		$scheduler;
1092
		$form .= '	</dd>';
1093

    
1094
		$form .= '	<dt>';
1095
		$form .=		'Bandwidth';
1096
		$form .= '	</dt>';
1097
		$form .= '	<dd>';
1098
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1099
		$form .= '	</dd>';
1100

    
1101
		$form .= '	<dt>';
1102
		$form .= 'Disable';
1103
		$form .= '	<dt>';
1104
		$form .= '	<dd>';
1105

    
1106
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1107
		$form .= $this->GetInterface() . '&amp;queue=';
1108
		$form .= $this->GetQname() . '&amp;action=delete">';
1109
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1110
		$form .= gettext("Remove shaper from this interface") . '</a>';
1111

    
1112
		$form .= '	</dd>';
1113

    
1114
		$form .= '</dl>';
1115

    
1116
		return $form;
1117

    
1118
	}
1119

    
1120
	/*
1121
	 * For requesting the parameters of the root queues
1122
	 * to the user like the traffic wizard does.
1123
	 */
1124
	function build_form() {
1125

    
1126
		$sform = new Form();
1127

    
1128
		$sform->setAction("firewall_shaper.php");
1129

    
1130
		$section = new Form_Section(null);
1131

    
1132
		$section->addInput(new Form_Checkbox(
1133
			'enabled',
1134
			'Enable/Disable',
1135
			'Enable/disable discipline and its children',
1136
			($this->GetEnabled() == "on"),
1137
			'on'
1138
		));
1139

    
1140
		$section->addInput(new Form_StaticText(
1141
			'*Name',
1142
			$this->GetQname()
1143
		));
1144

    
1145
		$section->addInput(new Form_Select(
1146
			'scheduler',
1147
			'Scheduler Type',
1148
			$this->GetScheduler(),
1149
			array('HFSC' => 'HFSC',
1150
				  'CBQ' => 'CBQ',
1151
				  'FAIRQ' => 'FAIRQ',
1152
				  'CODELQ' => 'CODELQ',
1153
				  'PRIQ' => 'PRIQ')
1154
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
1155

    
1156
		$group = new Form_group('Bandwidth');
1157

    
1158
		$group->add(new Form_Input(
1159
			'bandwidth',
1160
			null,
1161
			'number',
1162
			$this->GetBandwidth()
1163
		));
1164

    
1165
		$group->add(new Form_Select(
1166
			'bandwidthtype',
1167
			null,
1168
			$this->FormGetBwscale(),
1169
			array('Kb' => 'Kbit/s',
1170
				  'Mb' => 'Mbit/s',
1171
				  'Gb' => 'Gbit/s',
1172
				  'b' => 'Bit/s',
1173
				  '%' => '%')
1174
		));
1175

    
1176
		$section->add($group);
1177

    
1178
		$section->addInput(new Form_Input(
1179
			'qlimit',
1180
			'Queue Limit',
1181
			'number',
1182
			$this->GetQlimit()
1183
		));
1184

    
1185
		$section->addInput(new Form_Input(
1186
			'tbrconfig',
1187
			'TBR Size',
1188
			'number',
1189
			$this->GetTbrConfig()
1190
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
1191
					'bandwidth are used to determine the size.');
1192

    
1193
		$section->addInput(new Form_Input(
1194
			'interface',
1195
			null,
1196
			'hidden',
1197
			$this->GetInterface()
1198
		));
1199

    
1200
		$section->addInput(new Form_Input(
1201
			'name',
1202
			null,
1203
			'hidden',
1204
			$this->GetQname()
1205
		));
1206

    
1207
		$sform->add($section);
1208

    
1209
		return($sform);
1210
	}
1211

    
1212
	function update_altq_queue_data(&$data) {
1213
		$this->ReadConfig($data);
1214
	}
1215

    
1216
	/*
1217
	 * Should call on each of it queues and subqueues
1218
	 * the same function much like build_rules();
1219
	 */
1220
	function wconfig() {
1221
		$cflink = &get_reference_to_me_in_config($this->GetLink());
1222
		if (!is_array($cflink)) {
1223
			$cflink = array();
1224
		}
1225
		$cflink['interface'] = $this->GetInterface();
1226
		$cflink['name'] = $this->GetQname();
1227
		$cflink['scheduler'] = $this->GetScheduler();
1228
		$cflink['bandwidth'] = $this->GetBandwidth();
1229
		$cflink['bandwidthtype'] = $this->GetBwscale();
1230
		$cflink['qlimit'] = trim($this->GetQlimit());
1231
		if (empty($cflink['qlimit'])) {
1232
			unset($cflink['qlimit']);
1233
		}
1234
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
1235
		if (empty($cflink['tbrconfig'])) {
1236
			unset($cflink['tbrconfig']);
1237
		}
1238
		$cflink['enabled'] = $this->GetEnabled();
1239
		if (empty($cflink['enabled'])) {
1240
			unset($cflink['enabled']);
1241
		}
1242
	}
1243

    
1244
}
1245

    
1246
class priq_queue {
1247
	var $qname;
1248
	var $qinterface;
1249
	var $qlimit;
1250
	var $qpriority;
1251
	var $description;
1252
	var $isparent;
1253
	var $qbandwidth;
1254
	var $qbandwidthtype;
1255
	var $qdefault = "";
1256
	var $qrio = "";
1257
	var $qred = "";
1258
	var $qcodel = "";
1259
	var $qecn = "";
1260
	var $qack;
1261
	var $qenabled = "";
1262
	var $qparent;
1263
	var $link;
1264

    
1265
	/* This is here to help with form building and building rules/lists */
1266
	var $subqueues = array();
1267

    
1268
	/* Accessor functions */
1269
	function SetLink($link) {
1270
		$this->link = $link;
1271
	}
1272
	function GetLink() {
1273
		return $this->link;
1274
	}
1275
	function &GetParent() {
1276
		return $this->qparent;
1277
	}
1278
	function SetParent(&$parent) {
1279
		$this->qparent = &$parent;
1280
	}
1281
	function GetEnabled() {
1282
		return $this->qenabled;
1283
	}
1284
	function SetEnabled($value) {
1285
		$this->qenabled = $value;
1286
	}
1287
	function CanHaveChildren() {
1288
		return false;
1289
	}
1290
	function CanBeDeleted() {
1291
		return true;
1292
	}
1293
	function GetQname() {
1294
		return $this->qname;
1295
	}
1296
	function SetQname($name) {
1297
		$this->qname = trim($name);
1298
	}
1299
	function GetBandwidth() {
1300
		return $this->qbandwidth;
1301
	}
1302
	function SetBandwidth($bandwidth) {
1303
		$this->qbandwidth = $bandwidth;
1304
	}
1305
	function GetInterface() {
1306
		return $this->qinterface;
1307
	}
1308
	function SetInterface($name) {
1309
		$this->qinterface = trim($name);
1310
	}
1311
	function GetQlimit() {
1312
		return $this->qlimit;
1313
	}
1314
	function SetQlimit($limit) {
1315
		$this->qlimit = $limit;
1316
	}
1317
	function GetQpriority() {
1318
		if (is_numeric($this->qpriority)) {
1319
			return $this->qpriority;
1320
		} else {
1321
			return '1';
1322
		}
1323
	}
1324
	function SetQpriority($priority) {
1325
		$this->qpriority = $priority;
1326
	}
1327
	function GetDescription() {
1328
		return $this->description;
1329
	}
1330
	function SetDescription($str) {
1331
		$this->description = trim($str);
1332
	}
1333
	function GetFirstime() {
1334
		return $this->firsttime;
1335
	}
1336
	function SetFirsttime($number) {
1337
		$this->firsttime = $number;
1338
	}
1339
	function CheckBandwidth($bw, $bwtype) {
1340
		$parent = $this->GetParent();
1341

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

    
1347
		if ($bwtype != "%") {
1348
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1349

    
1350
			if ($bw > 0) {
1351
				$sum += get_bandwidth($bw, $bwtype, $parent);
1352
			}
1353

    
1354
			if ($sum > get_queue_bandwidth($parent)) {
1355
				return 1;
1356
			}
1357
		}
1358

    
1359
		foreach ($this->subqueues as $q) {
1360
			if ($q->CheckBandwidth(0, '')) {
1361
				return 1;
1362
			}
1363
		}
1364

    
1365
		return 0;
1366
	}
1367
	function GetTotalBw($qignore = NULL) {
1368
		$sum = 0;
1369
		foreach ($this->subqueues as $q) {
1370
			if ($qignore != NULL && $qignore == $q)
1371
				continue;
1372
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1373
		}
1374

    
1375
		return $sum;
1376
	}
1377
	function GetBwscale() {
1378
		return $this->qbandwidthtype;
1379
	}
1380
	function FormGetBwscale() {
1381
		if ($this->GetBwscale()) {
1382
			$bwscale = $this->GetBwscale();
1383
		} else {
1384
			$bwscale = 'Mb';
1385
		}
1386
		return $bwscale;
1387
	}
1388
	function SetBwscale($scale) {
1389
		$this->qbandwidthtype = $scale;
1390
	}
1391
	function GetDefaultQueuePresent() {
1392
		if ($this->GetDefault()) {
1393
			return true;
1394
		}
1395
		if (!empty($this->subqueues)) {
1396
			foreach ($this->subqueues as $q) {
1397
				if ($q->GetDefault()) {
1398
					return true;
1399
				}
1400
			}
1401
		}
1402

    
1403
		return false;
1404
	}
1405
	function GetDefault() {
1406
		return $this->qdefault;
1407
	}
1408
	function SetDefault($value = false) {
1409
		$this->qdefault = $value;
1410
	}
1411
	function GetCodel() {
1412
		return $this->codel;
1413
	}
1414
	function SetCodel($codel = false) {
1415
		$this->codel = $codel;
1416
	}
1417
	function GetRed() {
1418
		return $this->qred;
1419
	}
1420
	function SetRed($red = false) {
1421
		$this->qred = $red;
1422
	}
1423
	function GetRio() {
1424
		return $this->qrio;
1425
	}
1426
	function SetRio($rio = false) {
1427
		$this->qrio = $rio;
1428
	}
1429
	function GetEcn() {
1430
		return $this->qecn;
1431
	}
1432
	function SetEcn($ecn = false) {
1433
		$this->qecn = $ecn;
1434
	}
1435
	function GetAck() {
1436
		return $this->qack;
1437
	}
1438
	function SetAck($ack = false) {
1439
		$this->qack = $ack;
1440
	}
1441

    
1442
	function GetBwscaleText() {
1443
		switch ($this->qbandwidthtype) {
1444
			case "b":
1445
				$bwscaletext = "Bit/s";
1446
				break;
1447
			case "Kb":
1448
				$bwscaletext = "Kbit/s";
1449
				break;
1450
			case "Mb":
1451
				$bwscaletext = "Mbit/s";
1452
				break;
1453
			case "Gb":
1454
				$bwscaletext = "Gbit/s";
1455
				break;
1456
			default:
1457
				/* For others that do not need translating like % */
1458
				$bwscaletext = $this->qbandwidthtype;
1459
				break;
1460
		}
1461
		return $bwscaletext;
1462
	}
1463

    
1464
	function build_javascript() {
1465
		$javascript = "<script type=\"text/javascript\">";
1466
		$javascript .= "//<![CDATA[\n";
1467
		$javascript .= "function mySuspend() { \n";
1468
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1469
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1470
		$javascript .= "else if (document.all)\n";
1471
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1472
		$javascript .= "}\n";
1473

    
1474
		$javascript .= "function myResume() {\n";
1475
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1476
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1477
		$javascript .= "else if (document.all)\n";
1478
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1479
		$javascript .= "}\n";
1480
		$javascript .= "//]]>";
1481
		$javascript .= "</script>";
1482

    
1483
		return $javascript;
1484
	}
1485

    
1486
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1487

    
1488
	/*
1489
	 * Currently this will not be called unless we decide to clone a whole
1490
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1491
	 */
1492
	function copy_queue($interface, &$cflink) {
1493

    
1494
		$cflink['name'] = $this->GetQname();
1495
		$cflink['interface'] = $interface;
1496
		$cflink['qlimit'] = $this->GetQlimit();
1497
		$cflink['priority'] = $this->GetQpriority();
1498
		$cflink['description'] = $this->GetDescription();
1499
		$cflink['enabled'] = $this->GetEnabled();
1500
		$cflink['default'] = $this->GetDefault();
1501
		$cflink['red'] = $this->GetRed();
1502
		$cflink['codel'] = $this->GetCodel();
1503
		$cflink['rio'] = $this->GetRio();
1504
		$cflink['ecn'] = $this->GetEcn();
1505

    
1506
		if (is_array($this->subqueues)) {
1507
			$cflinkp['queue'] = array();
1508
			foreach ($this->subqueues as $q) {
1509
				$cflink['queue'][$q->GetQname()] = array();
1510
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1511
			}
1512
		}
1513
	}
1514

    
1515
	function clean_queue($sched) {
1516
		clean_child_queues($sched, $this->GetLink());
1517
		if (is_array($this->subqueues)) {
1518
			foreach ($this->subqueues as $q) {
1519
				$q->clean_queue($sched);
1520
			}
1521
		}
1522
	}
1523

    
1524
	function &get_queue_list(&$qlist) {
1525

    
1526
		$qlist[$this->GetQname()] = & $this;
1527
		if (is_array($this->subqueues)) {
1528
			foreach ($this->subqueues as $queue) {
1529
				$queue->get_queue_list($qlist);
1530
			}
1531
		}
1532
	}
1533

    
1534
	function delete_queue() {
1535
		unref_on_altq_queue_list($this->GetQname());
1536
		cleanup_queue_from_rules($this->GetQname());
1537
		unset_object_by_reference($this->GetLink());
1538
	}
1539

    
1540
	function delete_all() {
1541
		if (count($this->subqueues)) {
1542
			foreach ($this->subqueues as $q) {
1543
				$q->delete_all();
1544
				unset_object_by_reference($q->GetLink());
1545
				unset($q);
1546
			}
1547
			unset($this->subqueues);
1548
		}
1549
	}
1550

    
1551
	function &find_queue($interface, $qname) {
1552
		if ($qname == $this->GetQname()) {
1553
			return $this;
1554
		}
1555
	}
1556

    
1557
	function find_parentqueue($interface, $qname) { return; }
1558

    
1559
	function validate_input($data, &$input_errors) {
1560
		global $altq_list_queues;
1561

    
1562
		$reqdfields[] = "name";
1563
		$reqdfieldsn[] = gettext("Name");
1564
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1565
		$parent = $altq_list_queues[$this->GetInterface()];
1566

    
1567
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
1568
			$input_errors[] = gettext("Bandwidth must be an integer.");
1569
		}
1570
		if ($data['bandwidth'] < 0) {
1571
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1572
		}
1573
		if ($data['bandwidthtype'] == "%") {
1574
			if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
1575
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1576
			}
1577
		}
1578
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1579
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1580

    
1581
		if (($parent->GetScheduler() == 'PRIQ') && (!is_numeric($data['priority']) ||
1582
		    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1583
			$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1584
		} elseif (in_array($parent->GetScheduler(), array('FAIRQ','CBQ')) && (!is_numeric($data['priority']) ||
1585
		    ($data['priority'] < 0) || ($data['priority'] > 7))) {
1586
			$input_errors[] = gettext("The priority must be an integer between 0 and 7.");
1587
		}
1588

    
1589
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1590
				$input_errors[] = gettext("Queue limit must be an integer");
1591
		}
1592
		if ($data['qlimit'] < 0) {
1593
				$input_errors[] = gettext("Queue limit must be positive");
1594
		}
1595
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1596
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1597
		}
1598
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1599
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1600
		}
1601
		$default = $this->GetDefault();
1602
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1603
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1604
		}
1605

    
1606
		if (is_array($parent->queues)) {
1607
			foreach ($parent->queues as $q) {
1608
				if (($data['newname'] == $q->GetQname()) && ($data['name'] != $data['newname'])) { 
1609
					$input_errors[] = gettext("A queue with the same name already exists.");
1610
				}
1611
				if (in_array($parent->GetScheduler(), array('FAIRQ','PRIQ')) &&
1612
				    ($data['priority'] == $q->GetQpriority()) && ($data['name'] != $q->GetQname())) { 
1613
					$input_errors[] = sprintf(gettext("The priority %d is already used by queue %s."),
1614
							$data['priority'], $q->GetQname());
1615
				}
1616
			}
1617
		}
1618
	}
1619

    
1620
	function ReadConfig(&$q) {
1621
		if (!empty($q['name']) && !empty($q['newname']) && ($q['name'] != $q['newname'])) {
1622
			$this->SetQname($q['newname']);
1623
			rename_queue_in_rules($q['name'], $q['newname']);
1624
		} else if (!empty($q['newname'])) {
1625
			$this->SetQname($q['newname']);
1626
		} elseif (isset($q['name'])) {
1627
			$this->SetQname($q['name']);
1628
		}
1629
		if (isset($q['interface'])) {
1630
			$this->SetInterface($q['interface']);
1631
		}
1632
		$this->SetBandwidth($q['bandwidth']);
1633
		if (!empty($q['bandwidthtype'])) {
1634
			$this->SetBwscale($q['bandwidthtype']);
1635
		}
1636
		if (!empty($q['qlimit'])) {
1637
			$this->SetQlimit($q['qlimit']);
1638
		} else {
1639
			$this->SetQlimit(""); // Default
1640
		}
1641
		if (is_numeric($q['priority'])) {
1642
			$this->SetQPriority($q['priority']);
1643
		} else {
1644
			$this->SetQpriority("");
1645
		}
1646
		if (!empty($q['description'])) {
1647
			$this->SetDescription($q['description']);
1648
		} else {
1649
			$this->SetDescription("");
1650
		}
1651
		if (!empty($q['red'])) {
1652
			$this->SetRed($q['red']);
1653
		} else {
1654
			$this->SetRed();
1655
		}
1656
		if (!empty($q['codel'])) {
1657
			$this->SetCodel($q['codel']);
1658
		} else {
1659
			$this->SetCodel();
1660
		}
1661
		if (!empty($q['rio'])) {
1662
			$this->SetRio($q['rio']);
1663
		} else {
1664
			$this->SetRio();
1665
		}
1666
		if (!empty($q['ecn'])) {
1667
			$this->SetEcn($q['ecn']);
1668
		} else {
1669
			$this->SetEcn();
1670
		}
1671
		if (!empty($q['default'])) {
1672
			$this->SetDefault($q['default']);
1673
		} else {
1674
			$this->SetDefault();
1675
		}
1676
		if (!empty($q['enabled'])) {
1677
			$this->SetEnabled($q['enabled']);
1678
		} else {
1679
			$this->SetEnabled("");
1680
		}
1681
	}
1682

    
1683
	function build_tree() {
1684
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". htmlspecialchars($this->GetQname())."&amp;action=show";
1685
		$tree .= "\" ";
1686
		$tmpvalue = $this->GetDefault();
1687
		if (!empty($tmpvalue)) {
1688
			$tree .= " class=\"navlnk\"";
1689
		}
1690
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
1691
		/*
1692
		 * Not needed here!
1693
		 * if (is_array($queues) {
1694
		 *	  $tree .= "<ul>";
1695
		 *	  foreach ($q as $queues)
1696
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1697
		 *	  endforeach
1698
		 *	  $tree .= "</ul>";
1699
		 * }
1700
		 */
1701

    
1702
		$tree .= "</li>";
1703

    
1704
		return $tree;
1705
	}
1706

    
1707
	/* Should return something like:
1708
	 * queue $qname on $qinterface bandwidth ....
1709
	 */
1710
	function build_rules(&$default = false) {
1711
		$pfq_rule = " queue ". $this->qname;
1712
		if ($this->GetInterface()) {
1713
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1714
		}
1715
		$tmpvalue = $this->GetQpriority();
1716
		if (is_numeric($tmpvalue)) {
1717
			$pfq_rule .= " priority ".$this->GetQpriority();
1718
		}
1719
		$tmpvalue = $this->GetQlimit();
1720
		if (!empty($tmpvalue)) {
1721
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1722
		}
1723
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1724
			$pfq_rule .= " priq ( ";
1725
			$tmpvalue = $this->GetRed();
1726
			if (!empty($tmpvalue)) {
1727
				$comma = 1;
1728
				$pfq_rule .= " red ";
1729
			}
1730
			$tmpvalue = $this->GetRio();
1731
			if (!empty($tmpvalue)) {
1732
				if ($comma) {
1733
					$pfq_rule .= " ,";
1734
				}
1735
				$comma = 1;
1736
				$pfq_rule .= " rio ";
1737
			}
1738
			$tmpvalue = $this->GetEcn();
1739
			if (!empty($tmpvalue)) {
1740
				if ($comma) {
1741
					$pfq_rule .= " ,";
1742
				}
1743
				$comma = 1;
1744
				$pfq_rule .= " ecn ";
1745
			}
1746
			$tmpvalue = $this->GetCodel();
1747
			if (!empty($tmpvalue)) {
1748
				if ($comma) {
1749
					$pfq_rule .= " ,";
1750
				}
1751
				$comma = 1;
1752
				$pfq_rule .= " codel ";
1753
			}
1754
			$tmpvalue = $this->GetDefault();
1755
			if (!empty($tmpvalue)) {
1756
				if ($comma) {
1757
					$pfq_rule .= " ,";
1758
				}
1759
				$pfq_rule .= " default ";
1760
				$default = true;
1761
			}
1762
			$pfq_rule .= " ) ";
1763
		}
1764

    
1765
		$pfq_rule .= " \n";
1766

    
1767
		return $pfq_rule;
1768
	}
1769

    
1770
	/*
1771
	 * To return the html form to show to user
1772
	 * for getting the parameters.
1773
	 * Should do even for first time when the
1774
	 * object is created and later when we may
1775
	 * need to update it. (2)
1776
	 */
1777

    
1778
	function build_form() {
1779

    
1780
		$sform = new Form();
1781

    
1782
		$sform->setAction("firewall_shaper.php");
1783

    
1784
		$section = new Form_Section("");
1785

    
1786
		$section->addInput(new Form_Checkbox(
1787
			'enabled',
1788
			'Enable/Disable',
1789
			'Enable/disable discipline and its children',
1790
			($this->GetEnabled() == "on"),
1791
			'on'
1792
		));
1793

    
1794
		$section->addInput(new Form_Input(
1795
			'newname',
1796
			'*Name',
1797
			'text',
1798
			$this->GetQname()
1799
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1800

    
1801
		$section->addInput(new Form_Input(
1802
			'name',
1803
			null,
1804
			'hidden',
1805
			$this->GetQname()
1806
		));
1807

    
1808
		if (!is_a($this, "hfsc_queue")) {
1809
			$section->addInput(new Form_Input(
1810
				'priority',
1811
				'Priority',
1812
				'number',
1813
				$this->GetQpriority(),
1814
				['min' => '0', 'max'=> '']
1815
			))->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.');
1816
		}
1817

    
1818
		$section->addInput(new Form_Input(
1819
			'qlimit',
1820
			'Queue Limit',
1821
			'number',
1822
			$this->GetQlimit()
1823
		))->setHelp('Queue limit in packets.');
1824

    
1825
		$group = new Form_Group('Scheduler options');
1826

    
1827
		if (empty($this->subqueues)) {
1828
			$group->add(new Form_Checkbox(
1829
				'default',
1830
				null,
1831
				null,
1832
				$this->GetDefault(),
1833
				'default'
1834
			))->setHelp('Default Queue');
1835
		}
1836

    
1837
		$group->add(new Form_Checkbox(
1838
			'red',
1839
			null,
1840
			null,
1841
			!empty($this->GetRed())
1842
		))->setHelp('%1$sRandom Early Detection%2$s', '<a target="_new" href="https://docs.netgate.com/pfsense/en/latest/trafficshaper/advanced.html#editing-shaper-queues">', '</a>');
1843

    
1844
		$group->add(new Form_Checkbox(
1845
			'rio',
1846
			null,
1847
			null,
1848
			!empty($this->GetRio())
1849
		))->setHelp('%1$sRandom Early Detection In and Out%2$s', '<a target="_new" href="https://docs.netgate.com/pfsense/en/latest/trafficshaper/advanced.html#editing-shaper-queues">', '</a>');
1850

    
1851
		$group->add(new Form_Checkbox(
1852
			'ecn',
1853
			null,
1854
			null,
1855
			!empty($this->GetEcn())
1856
		))->setHelp('%1$sExplicit Congestion Notification%2$s', '<a target="_new" href="https://docs.netgate.com/pfsense/en/latest/trafficshaper/advanced.html#editing-shaper-queues">', '</a>');
1857

    
1858
		$group->add(new Form_Checkbox(
1859
			'codel',
1860
			null,
1861
			null,
1862
			!empty($this->GetCodel())
1863
		))->setHelp('%1$sCodel Active Queue%2$s', '<a target="_new" href="https://docs.netgate.com/pfsense/en/latest/trafficshaper/altq-scheduler-types.html#codel-active-queue-management">', '</a>');
1864

    
1865
		$group->setHelp('Select options for this queue');
1866

    
1867
		$section->add($group);
1868

    
1869
		$section->addInput(new Form_Input(
1870
			'description',
1871
			'Description',
1872
			'text',
1873
			$this->GetDescription()
1874
		));
1875

    
1876
		$sform->add($section);
1877

    
1878
		$sform->addGlobal(new Form_Input(
1879
			'interface',
1880
			null,
1881
			'hidden',
1882
			$this->GetInterface()
1883
		));
1884

    
1885
		$sform->addGlobal(new Form_Input(
1886
			'name',
1887
			null,
1888
			'hidden',
1889
			$this->GetQname()
1890
		));
1891

    
1892
		return($sform);
1893
	}
1894

    
1895
	function build_shortform() {
1896
		/* XXX: Hacks in sight. Mostly layer violations!  */
1897
		global $g, $altq_list_queues;
1898
		global $shaperIFlist;
1899

    
1900
		$altq =& $altq_list_queues[$this->GetInterface()];
1901

    
1902
		if ($altq) {
1903
			$scheduler = $altq->GetScheduler();
1904
		}
1905

    
1906
		$form = '<dl class="dl-horizontal">';
1907
		$form .= '	<dt>';
1908
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1909
		$form .= '	</dt>';
1910
		$form .= '	<dd>';
1911
		$form .=		$scheduler;
1912
		$form .= '	</dd>';
1913

    
1914
		$form .= '	<dt>';
1915
		$form .=		'Bandwidth';
1916
		$form .= '	</dt>';
1917
		$form .= '	<dd>';
1918
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1919
		$form .= '	</dd>';
1920

    
1921
		$tmpvalue = $this->GetQpriority();
1922
		if (!empty($tmpvalue)) {
1923
			$form .= '	<dt>';
1924
			$form .=		'Priority';
1925
			$form .= '	<dt>';
1926
			$form .= '	<dd>';
1927
			$form .=		'On';
1928
			$form .= '	</dd>';
1929
		}
1930

    
1931
		$tmpvalue = $this->GetDefault();
1932
		if (!empty($tmpvalue)) {
1933
			$form .= '	<dt>';
1934
			$form .=		'Default';
1935
			$form .= '	<dt>';
1936
			$form .= '	<dd>';
1937
			$form .=		'On';
1938
			$form .= '	</dd>';
1939
		}
1940

    
1941
			$form .= '	<dt>';
1942
			$form .= 'Delete';
1943
			$form .= '	<dt>';
1944
			$form .= '	<dd>';
1945

    
1946
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1947
			$form .= $this->GetInterface() . '&amp;queue=';
1948
			$form .= $this->GetQname() . '&amp;action=delete">';
1949
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1950
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1951

    
1952
			$form .= '	</dd>';
1953

    
1954
			$form .= '</dl>';
1955

    
1956
		return $form;
1957

    
1958
	}
1959

    
1960
	function update_altq_queue_data(&$q) {
1961
		$this->ReadConfig($q);
1962
	}
1963

    
1964
	function wconfig() {
1965
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1966
		if (!is_array($cflink)) {
1967
			$cflink = array();
1968
		}
1969
		$cflink['name'] = $this->GetQname();
1970
		$cflink['interface'] = $this->GetInterface();
1971
		$cflink['qlimit'] = trim($this->GetQlimit());
1972
		if (empty($cflink['qlimit'])) {
1973
			unset($cflink['qlimit']);
1974
		}
1975
		$cflink['priority'] = trim($this->GetQpriority());
1976
		if (!is_numeric($cflink['priority'])) {
1977
			unset($cflink['priority']);
1978
		}
1979
		$cflink['description'] = trim($this->GetDescription());
1980
		if (empty($cflink['description'])) {
1981
			unset($cflink['description']);
1982
		}
1983
		$cflink['enabled'] = trim($this->GetEnabled());
1984
		if (empty($cflink['enabled'])) {
1985
			unset($cflink['enabled']);
1986
		}
1987
		$cflink['default'] = trim($this->GetDefault());
1988
		if (empty($cflink['default'])) {
1989
			unset($cflink['default']);
1990
		}
1991
		$cflink['red'] = trim($this->GetRed());
1992
		if (empty($cflink['red'])) {
1993
			unset($cflink['red']);
1994
		}
1995
		$cflink['codel'] = trim($this->GetCodel());
1996
		if (empty($cflink['codel'])) {
1997
			unset($cflink['codel']);
1998
		}
1999
		$cflink['rio'] = trim($this->GetRio());
2000
		if (empty($cflink['rio'])) {
2001
			unset($cflink['rio']);
2002
		}
2003
		$cflink['ecn'] = trim($this->GetEcn());
2004
		if (empty($cflink['ecn'])) {
2005
			unset($cflink['ecn']);
2006
		}
2007
	}
2008
}
2009

    
2010
class hfsc_queue extends priq_queue {
2011
	/* realtime */
2012
	var $realtime;
2013
	var $r_m1;
2014
	var $r_d;
2015
	var $r_m2;
2016
	/* linkshare */
2017
	var $linkshare;
2018
	var $l_m1;
2019
	var $l_d;
2020
	var $l_m2;
2021
	/* upperlimit */
2022
	var $upperlimit;
2023
	var $u_m1;
2024
	var $u_d;
2025
	var $u_m2;
2026

    
2027
	/*
2028
	 * HFSC can have nested queues.
2029
	 */
2030
	function CanHaveChildren() {
2031
		return true;
2032
	}
2033
	function GetRealtime() {
2034
		return $this->realtime;
2035
	}
2036
	function GetR_m1() {
2037
		return $this->r_m1;
2038
	}
2039
	function GetR_d() {
2040
		return $this->r_d;
2041
	}
2042
	function GetR_m2() {
2043
		return $this->r_m2;
2044
	}
2045
	function SetRealtime() {
2046
		$this->realtime = "on";
2047
	}
2048
	function DisableRealtime() {
2049
		$this->realtime = "";
2050
	}
2051
	function SetR_m1($value) {
2052
		$this->r_m1 = $value;
2053
	}
2054
	function SetR_d($value) {
2055
		$this->r_d = $value;
2056
	}
2057
	function SetR_m2($value) {
2058
		$this->r_m2 = $value;
2059
	}
2060
	function GetLinkshare() {
2061
		return $this->linkshare;
2062
	}
2063
	function DisableLinkshare() {
2064
		$this->linkshare = "";
2065
	}
2066
	function GetL_m1() {
2067
		return $this->l_m1;
2068
	}
2069
	function GetL_d() {
2070
		return $this->l_d;
2071
	}
2072
	function GetL_m2() {
2073
		return $this->l_m2;
2074
	}
2075
	function SetLinkshare() {
2076
		$this->linkshare = "on";
2077
	}
2078
	function SetL_m1($value) {
2079
		$this->l_m1 = $value;
2080
	}
2081
	function SetL_d($value) {
2082
		$this->l_d = $value;
2083
	}
2084
	function SetL_m2($value) {
2085
		$this->l_m2 = $value;
2086
	}
2087
	function GetUpperlimit() {
2088
		return $this->upperlimit;
2089
	}
2090
	function GetU_m1() {
2091
		return $this->u_m1;
2092
	}
2093
	function GetU_d() {
2094
		return $this->u_d;
2095
	}
2096
	function GetU_m2() {
2097
		return $this->u_m2;
2098
	}
2099
	function SetUpperlimit() {
2100
		$this->upperlimit = "on";
2101
	}
2102
	function DisableUpperlimit() {
2103
		$this->upperlimit = "";
2104
	}
2105
	function SetU_m1($value) {
2106
		$this->u_m1 = $value;
2107
	}
2108
	function SetU_d($value) {
2109
		$this->u_d = $value;
2110
	}
2111
	function SetU_m2($value) {
2112
		$this->u_m2 = $value;
2113
	}
2114

    
2115
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2116

    
2117
		if (!is_array($this->subqueues)) {
2118
			$this->subqueues = array();
2119
		}
2120
		$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
2121
		$q->SetInterface($this->GetInterface());
2122
		$q->SetParent($this);
2123
		$q->ReadConfig($qname);
2124
		$q->validate_input($qname, $input_errors);
2125

    
2126
		$q->SetEnabled("on");
2127
		$q->SetLink($path);
2128

    
2129
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
2130
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2131
		if (is_array($qname['queue'])) {
2132
			foreach ($qname['queue'] as $key1 => $que) {
2133
				array_push($path, $key1);
2134
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2135
				array_pop($path);
2136
			}
2137
		}
2138

    
2139
		return $q;
2140
	}
2141

    
2142
	function copy_queue($interface, &$cflink) {
2143

    
2144
		$cflink['name'] = $this->GetQname();
2145
		$cflink['interface'] = $interface;
2146
		$cflink['qlimit'] = trim($this->GetQlimit());
2147
		if (empty($cflink['qlimit'])) {
2148
			unset($cflink['qlimit']);
2149
		}
2150
		$cflink['priority'] = trim($this->GetQpriority());
2151
		if (empty($cflink['priority'])) {
2152
			unset($cflink['priority']);
2153
		}
2154
		$cflink['description'] = trim($this->GetDescription());
2155
		if (empty($cflink['description'])) {
2156
			unset($cflink['description']);
2157
		}
2158
		$cflink['bandwidth'] = $this->GetBandwidth();
2159
		$cflink['bandwidthtype'] = $this->GetBwscale();
2160
		$cflink['enabled'] = trim($this->GetEnabled());
2161
		if (empty($cflink['enabled'])) {
2162
			unset($cflink['enabled']);
2163
		}
2164
		$cflink['default'] = trim($this->GetDefault());
2165
		if (empty($cflink['default'])) {
2166
			unset($cflink['default']);
2167
		}
2168
		$cflink['red'] = trim($this->GetRed());
2169
		if (empty($cflink['red'])) {
2170
			unset($cflink['red']);
2171
		}
2172
		$cflink['rio'] = trim($this->GetRio());
2173
		if (empty($cflink['rio'])) {
2174
			unset($cflink['rio']);
2175
		}
2176
		$cflink['ecn'] = trim($this->GetEcn());
2177
		if (empty($cflink['ecn'])) {
2178
			unset($cflink['ecn']);
2179
		}
2180
		if ($this->GetLinkshare() <> "") {
2181
			if ($this->GetL_m1() <> "") {
2182
				$cflink['linkshare1'] = $this->GetL_m1();
2183
				$cflink['linkshare2'] = $this->GetL_d();
2184
				$cflink['linkshare'] = "on";
2185
			} else {
2186
				unset($cflink['linkshare1']);
2187
				unset($cflink['linkshare2']);
2188
				unset($cflink['linkshare']);
2189
			}
2190
			if ($this->GetL_m2() <> "") {
2191
				$cflink['linkshare3'] = $this->GetL_m2();
2192
				$cflink['linkshare'] = "on";
2193
			} else {
2194
				unset($cflink['linkshare3']);
2195
				unset($cflink['linkshare']);
2196
			}
2197
		}
2198
		if ($this->GetRealtime() <> "") {
2199
			if ($this->GetR_m1() <> "") {
2200
				$cflink['realtime1'] = $this->GetR_m1();
2201
				$cflink['realtime2'] = $this->GetR_d();
2202
				$cflink['realtime'] = "on";
2203
			} else {
2204
				unset($cflink['realtime1']);
2205
				unset($cflink['realtime2']);
2206
				unset($cflink['realtime']);
2207
			}
2208
			if ($this->GetR_m2() <> "") {
2209
				$cflink['realtime3'] = $this->GetR_m2();
2210
				$cflink['realtime'] = "on";
2211
			} else {
2212
				unset($cflink['realtime3']);
2213
				unset($cflink['realtime']);
2214
			}
2215
		}
2216
		if ($this->GetUpperlimit() <> "") {
2217
			if ($this->GetU_m1() <> "") {
2218
				$cflink['upperlimit1'] = $this->GetU_m1();
2219
				$cflink['upperlimit2'] = $this->GetU_d();
2220
				$cflink['upperlimit'] = "on";
2221
			} else {
2222
				unset($cflink['upperlimit']);
2223
				unset($cflink['upperlimit1']);
2224
				unset($cflink['upperlimit2']);
2225
			}
2226
			if ($this->GetU_m2() <> "") {
2227
				$cflink['upperlimit3'] = $this->GetU_m2();
2228
				$cflink['upperlimit'] = "on";
2229
			} else {
2230
				unset($cflink['upperlimit3']);
2231
				unset($cflink['upperlimit']);
2232
			}
2233
		}
2234

    
2235
		if (is_array($this->subqueues)) {
2236
			$cflinkp['queue'] = array();
2237
			foreach ($this->subqueues as $q) {
2238
				$cflink['queue'][$q->GetQname()] = array();
2239
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2240
			}
2241
		}
2242
	}
2243

    
2244
	function delete_queue() {
2245
		unref_on_altq_queue_list($this->GetQname());
2246
		cleanup_queue_from_rules($this->GetQname());
2247
		$parent =& $this->GetParent();
2248
		foreach ($this->subqueues as $q) {
2249
			$q->delete_queue();
2250
		}
2251
		unset_object_by_reference($this->GetLink());
2252
	}
2253

    
2254
	/*
2255
	 * Should search even its children
2256
	 */
2257
	function &find_queue($interface, $qname) {
2258
		if ($qname == $this->GetQname()) {
2259
			return $this;
2260
		}
2261

    
2262
		foreach ($this->subqueues as $q) {
2263
			$result =& $q->find_queue("", $qname);
2264
			if ($result) {
2265
				return $result;
2266
			}
2267
		}
2268
	}
2269

    
2270
	function &find_parentqueue($interface, $qname) {
2271
		if ($this->subqueues[$qname]) {
2272
			return $this;
2273
		}
2274
		foreach ($this->subqueues as $q) {
2275
			$result = $q->find_parentqueue("", $qname);
2276
			if ($result) {
2277
				return $result;
2278
			}
2279
		}
2280
	}
2281

    
2282
	function validate_input($data, &$input_errors) {
2283
		parent::validate_input($data, $input_errors);
2284

    
2285
		$reqdfields[] = "bandwidth";
2286
		$reqdfieldsn[] = gettext("Bandwidth");
2287
		$reqdfields[] = "bandwidthtype";
2288
		$reqdfieldsn[] = gettext("Bandwidthtype");
2289

    
2290
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2291

    
2292
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2293
			if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2294
				$input_errors[] = gettext("Bandwidth must be an integer.");
2295
			}
2296

    
2297
			if ($data['bandwidth'] < 0) {
2298
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2299
			}
2300

    
2301
			if ($data['bandwidthtype'] == "%") {
2302
				if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
2303
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2304
				}
2305
			}
2306
		}
2307

    
2308
		if (!empty($data['upperlimit1']) && empty($data['upperlimit2'])) {
2309
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
2310
		}
2311
		if (!empty($data['upperlimit2']) && empty($data['upperlimit1'])) {
2312
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
2313
		}
2314
		if (!empty($data['upperlimit1']) && !is_valid_shaperbw($data['upperlimit1'])) {
2315
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
2316
		}
2317
		if (!empty($data['upperlimit2']) && !is_numeric($data['upperlimit2'])) {
2318
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
2319
		}
2320
		if (!empty($data['upperlimit3']) && !is_valid_shaperbw($data['upperlimit3'])) {
2321
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
2322
		}
2323

    
2324
		/*
2325
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
2326
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
2327
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
2328
			if (floatval($bw_1) < floatval($bw_2)) {
2329
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
2330
			}
2331

    
2332
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2333
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
2334
			}
2335
		}
2336
		*/
2337
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
2338
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
2339
		}
2340
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
2341
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
2342
		}
2343
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
2344
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
2345
		}
2346
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
2347
			$input_errors[] = gettext("linkshare d value needs to be numeric");
2348
		}
2349
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
2350
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
2351
		}
2352
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2353
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2354
		}
2355
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2356
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2357
		}
2358

    
2359
		/*
2360
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2361
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2362
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2363
			if (floatval($bw_1) < floatval($bw_2)) {
2364
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2365
			}
2366

    
2367
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2368
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2369
			}
2370
		}
2371
		*/
2372

    
2373
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2374
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2375
		}
2376
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2377
			$input_errors[] = gettext("realtime d value needs to be numeric");
2378
		}
2379
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2380
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2381
		}
2382

    
2383
		/*
2384
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2385
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2386
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2387
			if (floatval($bw_1) < floatval($bw_2)) {
2388
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2389
			}
2390

    
2391
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2392
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2393
			}
2394
		}
2395
		*/
2396
	}
2397

    
2398
	function ReadConfig(&$cflink) {
2399
		if (!empty($cflink['linkshare'])) {
2400
			if (!empty($cflink['linkshare1'])) {
2401
				$this->SetL_m1($cflink['linkshare1']);
2402
				$this->SetL_d($cflink['linkshare2']);
2403
				$this->SetLinkshare();
2404
			} else {
2405
				$this->SetL_m1("");
2406
				$this->SetL_d("");
2407
				$this->DisableLinkshare();
2408
			}
2409
			if (!empty($cflink['linkshare3'])) {
2410
				$this->SetL_m2($cflink['linkshare3']);
2411
				$this->SetLinkshare();
2412
			}
2413
		} else {
2414
			$this->DisableLinkshare();
2415
		}
2416
		if (!empty($cflink['realtime'])) {
2417
			if (!empty($cflink['realtime1'])) {
2418
				$this->SetR_m1($cflink['realtime1']);
2419
				$this->SetR_d($cflink['realtime2']);
2420
				$this->SetRealtime();
2421
			} else {
2422
				$this->SetR_m1("");
2423
				$this->SetR_d("");
2424
				$this->DisableRealtime();
2425
			}
2426
			if (!empty($cflink['realtime3'])) {
2427
				$this->SetR_m2($cflink['realtime3']);
2428
				$this->SetRealtime();
2429
			}
2430
		} else {
2431
			$this->DisableRealtime();
2432
		}
2433
		if (!empty($cflink['upperlimit'])) {
2434
			if (!empty($cflink['upperlimit1'])) {
2435
				$this->SetU_m1($cflink['upperlimit1']);
2436
				$this->SetU_d($cflink['upperlimit2']);
2437
				$this->SetUpperlimit();
2438
			} else {
2439
				$this->SetU_m1("");
2440
				$this->SetU_d("");
2441
				$this->DisableUpperlimit();
2442
			}
2443
			if (!empty($cflink['upperlimit3'])) {
2444
				$this->SetU_m2($cflink['upperlimit3']);
2445
				$this->SetUpperlimit();
2446
			}
2447
		} else {
2448
			$this->DisableUpperlimit();
2449
		}
2450
		parent::ReadConfig($cflink);
2451
	}
2452

    
2453
	function build_tree() {
2454
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
2455
		$tree .= "\" ";
2456
		$tmpvalue = $this->GetDefault();
2457
		if (!empty($tmpvalue)) {
2458
			$tree .= " class=\"navlnk\"";
2459
		}
2460
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
2461
		if (is_array($this->subqueues)) {
2462
			$tree .= "<ul>";
2463
			foreach ($this->subqueues as $q) {
2464
				$tree .= $q->build_tree();
2465
			}
2466
			$tree .= "</ul>";
2467
		}
2468
		$tree .= "</li>";
2469
		return $tree;
2470
	}
2471

    
2472
	/* Even this should take children into consideration */
2473
	function build_rules(&$default = false) {
2474

    
2475
		$pfq_rule = " queue ". $this->qname;
2476
		if ($this->GetInterface()) {
2477
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2478
		}
2479
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2480
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2481
		}
2482

    
2483
		$tmpvalue = $this->GetQlimit();
2484
		if (!empty($tmpvalue)) {
2485
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2486
		}
2487
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2488
			$pfq_rule .= " hfsc ( ";
2489
			$tmpvalue = $this->GetRed();
2490
			if (!empty($tmpvalue)) {
2491
				$comma = 1;
2492
				$pfq_rule .= " red ";
2493
			}
2494

    
2495
			$tmpvalue = $this->GetRio();
2496
			if (!empty($tmpvalue)) {
2497
				if ($comma) {
2498
					$pfq_rule .= " ,";
2499
				}
2500
				$comma = 1;
2501
				$pfq_rule .= " rio ";
2502
			}
2503
			$tmpvalue = $this->GetEcn();
2504
			if (!empty($tmpvalue)) {
2505
				if ($comma) {
2506
					$pfq_rule .= " ,";
2507
				}
2508
				$comma = 1;
2509
				$pfq_rule .= " ecn ";
2510
			}
2511
			$tmpvalue = $this->GetCodel();
2512
			if (!empty($tmpvalue)) {
2513
				if ($comma) {
2514
					$pfq_rule .= " ,";
2515
				}
2516
				$comma = 1;
2517
				$pfq_rule .= " codel ";
2518
			}
2519
			$tmpvalue = $this->GetDefault();
2520
			if (!empty($tmpvalue)) {
2521
				if ($comma) {
2522
					$pfq_rule .= " ,";
2523
				}
2524
				$comma = 1;
2525
				$pfq_rule .= " default ";
2526
				$default = true;
2527
			}
2528

    
2529
			if ($this->GetRealtime() <> "") {
2530
				if ($comma) {
2531
					$pfq_rule .= " , ";
2532
				}
2533
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2534
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2535
				} else if ($this->GetR_m2() <> "") {
2536
					$pfq_rule .= " realtime " . $this->GetR_m2();
2537
				}
2538
				$comma = 1;
2539
			}
2540
			if ($this->GetLinkshare() <> "") {
2541
				if ($comma) {
2542
					$pfq_rule .= " ,";
2543
				}
2544
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2545
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2546
				} else if ($this->GetL_m2() <> "") {
2547
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2548
				}
2549
				$comma = 1;
2550
			}
2551
			if ($this->GetUpperlimit() <> "") {
2552
				if ($comma) {
2553
					$pfq_rule .= " ,";
2554
				}
2555
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2556
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2557
				} else if ($this->GetU_m2() <> "") {
2558
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2559
				}
2560
			}
2561
			$pfq_rule .= " ) ";
2562
		}
2563
		if (count($this->subqueues)) {
2564
			$i = count($this->subqueues);
2565
			$pfq_rule .= " { ";
2566
			foreach ($this->subqueues as $qkey => $qnone) {
2567
				if ($i > 1) {
2568
					$i--;
2569
					$pfq_rule .= " {$qkey}, ";
2570
				} else {
2571
					$pfq_rule .= " {$qkey} ";
2572
				}
2573
			}
2574
			$pfq_rule .= " } \n";
2575
			foreach ($this->subqueues as $q) {
2576
				$pfq_rule .= $q->build_rules($default);
2577
			}
2578
		}
2579

    
2580
		$pfq_rule .= " \n";
2581

    
2582
		return $pfq_rule;
2583
	}
2584

    
2585
	function build_javascript() {
2586

    
2587
		$javascript = <<<EOJS
2588
<script type="text/javascript">
2589
//<![CDATA[
2590
	events.push(function(){
2591

    
2592
		// Disables the specified input element
2593
		function disableInput(id, disable) {
2594
			$('#' + id).prop("disabled", disable);
2595
		}
2596

    
2597
		// Upperlimit
2598
		function enable_upperlimit() {
2599
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2600
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2601
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2602
		}
2603

    
2604
		$('#upperlimit').click(function () {
2605
			enable_upperlimit();
2606
		});
2607

    
2608
		enable_upperlimit();
2609

    
2610
		// realtime
2611
		function enable_realtime() {
2612
			disableInput('realtime1', !$('#realtime').prop('checked'));
2613
			disableInput('realtime2', !$('#realtime').prop('checked'));
2614
			disableInput('realtime3', !$('#realtime').prop('checked'));
2615
		}
2616

    
2617
		$('#realtime').click(function () {
2618
			enable_realtime();
2619
		});
2620

    
2621
		enable_realtime();
2622

    
2623
		// linkshare
2624
		function enable_linkshare() {
2625
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2626
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2627
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2628
		}
2629

    
2630
		$('#linkshare').click(function () {
2631
			enable_linkshare();
2632
		});
2633

    
2634
		enable_linkshare();
2635
	});
2636
//]]>
2637
</script>
2638
EOJS;
2639

    
2640
		return $javascript;
2641
	}
2642

    
2643
	function build_form() {
2644

    
2645
		$sform = parent::build_form();
2646

    
2647
		$section = new Form_Section('Service Curve (sc)');
2648

    
2649
		$group = new Form_Group('Bandwidth');
2650

    
2651
		$group->add(new Form_Input(
2652
			'bandwidth',
2653
			null,
2654
			'number',
2655
			$this->GetBandwidth(),
2656
			['step' => 'any', 'min' => '0.000']
2657
		));
2658

    
2659
		$group->add(new Form_Select(
2660
			'bandwidthtype',
2661
			null,
2662
			$this->FormGetBwscale(),
2663
			array('Kb' => 'Kbit/s',
2664
				  'Mb' => 'Mbit/s',
2665
				  'Gb' => 'Gbit/s',
2666
				  'b' => 'Bit/s',
2667
				  '%' => '%')
2668
		));
2669

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

    
2672
		$section->add($group);
2673

    
2674
		$group = new Form_Group('Max bandwidth for queue.');
2675

    
2676
		$group->add(new Form_Checkbox(
2677
			'upperlimit',
2678
			null,
2679
			'Upper Limit',
2680
			($this->GetUpperlimit()<> "")
2681
		));
2682

    
2683
		$group->add(new Form_Input(
2684
			'upperlimit1',
2685
			null,
2686
			'text',
2687
			$this->GetU_m1()
2688
		))->setHelp('m1');
2689

    
2690
		$group->add(new Form_Input(
2691
			'upperlimit2',
2692
			null,
2693
			'text',
2694
			$this->GetU_d()
2695
		))->setHelp('d');
2696

    
2697
		$group->add(new Form_Input(
2698
			'upperlimit3',
2699
			null,
2700
			'text',
2701
			$this->GetU_m2()
2702
		))->setHelp('m2');
2703

    
2704

    
2705
		$section->add($group);
2706

    
2707
		$group = new Form_Group('Min bandwidth for queue.');
2708

    
2709
		$group->add(new Form_Checkbox(
2710
			'realtime',
2711
			null,
2712
			'Real Time',
2713
			($this->GetRealtime()<> "")
2714
		));
2715

    
2716
		$group->add(new Form_Input(
2717
			'realtime1',
2718
			null,
2719
			'text',
2720
			$this->GetR_m1()
2721
		))->setHelp('m1');
2722

    
2723
		$group->add(new Form_Input(
2724
			'realtime2',
2725
			null,
2726
			'text',
2727
			$this->GetR_d()
2728
		))->setHelp('d');
2729

    
2730
		$group->add(new Form_Input(
2731
			'realtime3',
2732
			null,
2733
			'text',
2734
			$this->GetR_m2()
2735
		))->setHelp('m2');
2736

    
2737
		$section->add($group);
2738

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

    
2741
		$group->add(new Form_Checkbox(
2742
			'linkshare',
2743
			null,
2744
			'Link Share',
2745
			($this->GetLinkshare()<> "")
2746
		));
2747

    
2748
		$group->add(new Form_Input(
2749
			'linkshare1',
2750
			null,
2751
			'text',
2752
			$this->GetL_m1()
2753
		))->setHelp('m1');
2754

    
2755
		$group->add(new Form_Input(
2756
			'linkshare2',
2757
			null,
2758
			'text',
2759
			$this->GetL_d()
2760
		))->setHelp('d');
2761

    
2762
		$group->add(new Form_Input(
2763
			'linkshare3',
2764
			null,
2765
			'text',
2766
			$this->GetL_m2()
2767
		))->setHelp('m2');
2768

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

    
2775
		$section->add($group);
2776

    
2777
		$sform->add($section);
2778

    
2779
		return($sform);
2780
	}
2781

    
2782
	function update_altq_queue_data(&$data) {
2783
		$this->ReadConfig($data);
2784
	}
2785

    
2786
	function wconfig() {
2787
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2788
		if (!is_array($cflink)) {
2789
			$cflink = array();
2790
		}
2791
		$cflink['name'] = $this->GetQname();
2792
		$cflink['interface'] = $this->GetInterface();
2793
		$cflink['qlimit'] = trim($this->GetQlimit());
2794
		if (empty($cflink['qlimit'])) {
2795
			unset($cflink['qlimit']);
2796
		}
2797
		$cflink['priority'] = $this->GetQpriority();
2798
		if (!is_numericint($cflink['priority'])) {
2799
			unset($cflink['priority']);
2800
		}
2801
		$cflink['description'] = $this->GetDescription();
2802
		if (empty($cflink['description'])) {
2803
			unset($cflink['description']);
2804
		}
2805
		$cflink['bandwidth'] = $this->GetBandwidth();
2806
		$cflink['bandwidthtype'] = $this->GetBwscale();
2807
		$cflink['enabled'] = $this->GetEnabled();
2808
		if (empty($cflink['enabled'])) {
2809
			unset($cflink['enabled']);
2810
		}
2811
		$cflink['default'] = $this->GetDefault();
2812
		if (empty($cflink['default'])) {
2813
			unset($cflink['default']);
2814
		}
2815
		$cflink['red'] = trim($this->GetRed());
2816
		if (empty($cflink['red'])) {
2817
			unset($cflink['red']);
2818
		}
2819
		$cflink['rio'] = $this->GetRio();
2820
		if (empty($cflink['rio'])) {
2821
			unset($cflink['rio']);
2822
		}
2823
		$cflink['ecn'] = trim($this->GetEcn());
2824
		if (empty($cflink['ecn'])) {
2825
			unset($cflink['ecn']);
2826
		}
2827
		$cflink['codel'] = trim($this->GetCodel());
2828
		if (empty($cflink['codel'])) {
2829
			unset($cflink['codel']);
2830
		}
2831
		if ($this->GetLinkshare() <> "") {
2832
			if ($this->GetL_m1() <> "") {
2833
				$cflink['linkshare1'] = $this->GetL_m1();
2834
				$cflink['linkshare2'] = $this->GetL_d();
2835
				$cflink['linkshare'] = "on";
2836
			} else {
2837
				unset($cflink['linkshare']);
2838
				unset($cflink['linkshare1']);
2839
				unset($cflink['linkshare2']);
2840
			}
2841
			if ($this->GetL_m2() <> "") {
2842
				$cflink['linkshare3'] = $this->GetL_m2();
2843
				$cflink['linkshare'] = "on";
2844
			} else {
2845
				unset($cflink['linkshare']);
2846
				unset($cflink['linkshare3']);
2847
			}
2848
		} else {
2849
			unset($cflink['linkshare']);
2850
			unset($cflink['linkshare1']);
2851
			unset($cflink['linkshare2']);
2852
			unset($cflink['linkshare3']);
2853
		}
2854
		if ($this->GetRealtime() <> "") {
2855
			if ($this->GetR_m1() <> "") {
2856
				$cflink['realtime1'] = $this->GetR_m1();
2857
				$cflink['realtime2'] = $this->GetR_d();
2858
				$cflink['realtime'] = "on";
2859
			} else {
2860
				unset($cflink['realtime']);
2861
				unset($cflink['realtime1']);
2862
				unset($cflink['realtime2']);
2863
			}
2864
			if ($this->GetR_m2() <> "") {
2865
				$cflink['realtime3'] = $this->GetR_m2();
2866
				$cflink['realtime'] = "on";
2867
			} else {
2868
				unset($cflink['realtime']);
2869
				unset($cflink['realtime3']);
2870
			}
2871
		} else {
2872
			unset($cflink['realtime']);
2873
			unset($cflink['realtime1']);
2874
			unset($cflink['realtime2']);
2875
			unset($cflink['realtime3']);
2876
		}
2877
		if ($this->GetUpperlimit() <> "") {
2878
			if ($this->GetU_m1() <> "") {
2879
				$cflink['upperlimit1'] = $this->GetU_m1();
2880
				$cflink['upperlimit2'] = $this->GetU_d();
2881
				$cflink['upperlimit'] = "on";
2882
			} else {
2883
				unset($cflink['upperlimit']);
2884
				unset($cflink['upperlimit1']);
2885
				unset($cflink['upperlimit2']);
2886
			}
2887
			if ($this->GetU_m2() <> "") {
2888
				$cflink['upperlimit3'] = $this->GetU_m2();
2889
				$cflink['upperlimit'] = "on";
2890
			} else {
2891
				unset($cflink['upperlimit']);
2892
				unset($cflink['upperlimit3']);
2893
			}
2894
		} else {
2895
			unset($cflink['upperlimit']);
2896
			unset($cflink['upperlimit1']);
2897
			unset($cflink['upperlimit2']);
2898
			unset($cflink['upperlimit3']);
2899
		}
2900
	}
2901
}
2902

    
2903
class cbq_queue extends priq_queue {
2904
	var $qborrow = "";
2905

    
2906
	function GetBorrow() {
2907
		return $this->qborrow;
2908
	}
2909
	function SetBorrow($borrow) {
2910
		$this->qborrow = $borrow;
2911
	}
2912
	function CanHaveChildren() {
2913
		return true;
2914
	}
2915

    
2916
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2917

    
2918
		if (!is_array($this->subqueues)) {
2919
			$this->subqueues = array();
2920
		}
2921
		$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
2922
		$q->SetInterface($this->GetInterface());
2923
		$q->SetParent($this);
2924
		$q->ReadConfig($qname);
2925
		$q->validate_input($qname, $input_errors);
2926

    
2927
		$q->SetEnabled("on");
2928
		$q->SetLink($path);
2929
		$this->subqueues[$q->GetQName()] = &$q;
2930
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2931
		if (is_array($qname['queue'])) {
2932
			foreach ($qname['queue'] as $key1 => $que) {
2933
				array_push($path, $key1);
2934
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2935
				array_pop($path);
2936
			}
2937
		}
2938

    
2939
		return $q;
2940
	}
2941

    
2942
	function copy_queue($interface, &$cflink) {
2943

    
2944
		$cflink['interface'] = $interface;
2945
		$cflink['qlimit'] = trim($this->GetQlimit());
2946
		if (empty($clink['qlimit'])) {
2947
			unset($cflink['qlimit']);
2948
		}
2949
		$cflink['priority'] = trim($this->GetQpriority());
2950
		if (!is_numeric($cflink['priority'])) {
2951
			unset($cflink['priority']);
2952
		}
2953
		$cflink['name'] = $this->GetQname();
2954
		$cflink['description'] = trim($this->GetDescription());
2955
		if (empty($cflink['description'])) {
2956
			unset($cflink['description']);
2957
		}
2958
		$cflink['bandwidth'] = $this->GetBandwidth();
2959
		$cflink['bandwidthtype'] = $this->GetBwscale();
2960
		$cflink['enabled'] = trim($this->GetEnabled());
2961
		if (empty($cflink['enabled'])) {
2962
			unset($cflink['enabled']);
2963
		}
2964
		$cflink['default'] = trim($this->GetDefault());
2965
		if (empty($cflink['default'])) {
2966
			unset($cflink['default']);
2967
		}
2968
		$cflink['red'] = trim($this->GetRed());
2969
		if (empty($cflink['red'])) {
2970
			unset($cflink['red']);
2971
		}
2972
		$cflink['rio'] = trim($this->GetRio());
2973
		if (empty($cflink['rio'])) {
2974
			unset($cflink['rio']);
2975
		}
2976
		$cflink['ecn'] = trim($this->GetEcn());
2977
		if (empty($cflink['ecn'])) {
2978
			unset($cflink['ecn']);
2979
		}
2980
		$cflink['borrow'] = trim($this->GetBorrow());
2981
		if (empty($cflink['borrow'])) {
2982
			unset($cflink['borrow']);
2983
		}
2984
		if (is_array($this->queues)) {
2985
			$cflinkp['queue'] = array();
2986
			foreach ($this->subqueues as $q) {
2987
				$cflink['queue'][$q->GetQname()] = array();
2988
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2989
			}
2990
		}
2991
	}
2992

    
2993
	/*
2994
	 * Should search even its children
2995
	 */
2996
	function &find_queue($interface, $qname) {
2997
		if ($qname == $this->GetQname()) {
2998
			return $this;
2999
		}
3000
		foreach ($this->subqueues as $q) {
3001
			$result =& $q->find_queue("", $qname);
3002
			if ($result) {
3003
				return $result;
3004
			}
3005
		}
3006
	}
3007

    
3008
	function &find_parentqueue($interface, $qname) {
3009
		if ($this->subqueues[$qname]) {
3010
			return $this;
3011
		}
3012
		foreach ($this->subqueues as $q) {
3013
			$result = $q->find_parentqueue("", $qname);
3014
			if ($result) {
3015
				return $result;
3016
			}
3017
		}
3018
	}
3019

    
3020
	function delete_queue() {
3021
		unref_on_altq_queue_list($this->GetQname());
3022
		cleanup_queue_from_rules($this->GetQname());
3023
		foreach ($this->subqueues as $q) {
3024
			$q->delete_queue();
3025
		}
3026
		unset_object_by_reference($this->GetLink());
3027
	}
3028

    
3029
	function validate_input($data, &$input_errors) {
3030
		parent::validate_input($data, $input_errors);
3031

    
3032
		if ($data['priority'] > 7) {
3033
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3034
		}
3035
		$reqdfields[] = "bandwidth";
3036
		$reqdfieldsn[] = gettext("Bandwidth");
3037
		$reqdfields[] = "bandwidthtype";
3038
		$reqdfieldsn[] = gettext("Bandwidthtype");
3039

    
3040
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3041
	}
3042

    
3043
	function ReadConfig(&$q) {
3044
		parent::ReadConfig($q);
3045
		if (!empty($q['borrow'])) {
3046
			$this->SetBorrow("on");
3047
		} else {
3048
			$this->SetBorrow("");
3049
		}
3050
	}
3051

    
3052
	function build_javascript() {
3053
		return parent::build_javascript();
3054
	}
3055

    
3056
	function build_tree() {
3057
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3058
		$tree .= "\" ";
3059
		$tmpvalue = trim($this->GetDefault());
3060
		if (!empty($tmpvalue)) {
3061
			$tree .= " class=\"navlnk\"";
3062
		}
3063
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3064
		if (is_array($this->subqueues)) {
3065
			$tree .= "<ul>";
3066
			foreach ($this->subqueues as $q) {
3067
				$tree .= $q->build_tree();
3068
			}
3069
			$tree .= "</ul>";
3070
		}
3071
		$tree .= "</li>";
3072
		return $tree;
3073
	}
3074

    
3075
	/* Even this should take children into consideration */
3076
	function build_rules(&$default = false) {
3077
		$pfq_rule = "queue ". $this->qname;
3078
		if ($this->GetInterface()) {
3079
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3080
		}
3081
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3082
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3083
		}
3084
		$tmpvalue = $this->GetQpriority();
3085
		if (is_numeric($tmpvalue)) {
3086
			$pfq_rule .= " priority " . $this->GetQpriority();
3087
		}
3088
		$tmpvalue = trim($this->GetQlimit());
3089
		if (!empty($tmpvalue)) {
3090
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3091
		}
3092
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
3093
			$pfq_rule .= " cbq ( ";
3094
			$tmpvalue = trim($this->GetRed());
3095
			if (!empty($tmpvalue)) {
3096
				$comma = 1;
3097
				$pfq_rule .= " red ";
3098
			}
3099
			$tmpvalue = trim($this->GetCodel());
3100
			if (!empty($tmpvalue)) {
3101
				$comma = 1;
3102
				$pfq_rule .= " codel ";
3103
			}
3104
			$tmpvalue = trim($this->GetRio());
3105
			if (!empty($tmpvalue)) {
3106
				if ($comma) {
3107
					$pfq_rule .= " ,";
3108
				}
3109
				$comma = 1;
3110
				$pfq_rule .= " rio ";
3111
			}
3112
			$tmpvalue = trim($this->GetEcn());
3113
			if (!empty($tmpvalue)) {
3114
				if ($comma) {
3115
					$pfq_rule .= " ,";
3116
				}
3117
				$comma = 1;
3118
				$pfq_rule .= " ecn ";
3119
			}
3120
			$tmpvalue = trim($this->GetDefault());
3121
			if (!empty($tmpvalue)) {
3122
				if ($comma) {
3123
					$pfq_rule .= " ,";
3124
				}
3125
				$comma = 1;
3126
				$pfq_rule .= " default ";
3127
				$default = true;
3128
			}
3129
			$tmpvalue = trim($this->GetBorrow());
3130
			if (!empty($tmpvalue)) {
3131
				if ($comma) {
3132
					$pfq_rule .= ", ";
3133
				}
3134
				$pfq_rule .= " borrow ";
3135
			}
3136
			$pfq_rule .= " ) ";
3137
		}
3138
		if (count($this->subqueues)) {
3139
			$i = count($this->subqueues);
3140
			$pfq_rule .= " { ";
3141
			foreach ($this->subqueues as $qkey => $qnone) {
3142
				if ($i > 1) {
3143
					$i--;
3144
					$pfq_rule .= " {$qkey}, ";
3145
				} else {
3146
					$pfq_rule .= " {$qkey} ";
3147
				}
3148
			}
3149
			$pfq_rule .= " } \n";
3150
			foreach ($this->subqueues as $q) {
3151
				$pfq_rule .= $q->build_rules($default);
3152
			}
3153
		}
3154

    
3155
		$pfq_rule .= " \n";
3156
		return $pfq_rule;
3157
	}
3158

    
3159
	function build_form() {
3160
		$sform = parent::build_form();
3161

    
3162
		$section = new Form_Section('NOTITLE');
3163

    
3164
		$group = new Form_Group('Bandwidth');
3165

    
3166
		$group->add(new Form_Input(
3167
			'bandwidth',
3168
			null,
3169
			'number',
3170
			$this->GetBandwidth()
3171
		));
3172

    
3173
		$group->add(new Form_Select(
3174
			'bandwidthtype',
3175
			null,
3176
			$this->FormGetBwscale(),
3177
			array('Kb' => 'Kbit/s',
3178
				  'Mb' => 'Mbit/s',
3179
				  'Gb' => 'Gbit/s',
3180
				  'b' => 'Bit/s',
3181
				  '%' => '%')
3182
		));
3183

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

    
3186
		$section->add($group);
3187

    
3188
		$section->addInput(new Form_Checkbox(
3189
			'borrow',
3190
			'Scheduler option',
3191
			'Borrow from other queues when available',
3192
			($this->GetBorrow() == "on")
3193
		));
3194

    
3195
		$sform->add($section);
3196

    
3197
		return $sform;
3198
	}
3199

    
3200
	function update_altq_queue_data(&$data) {
3201
		$this->ReadConfig($data);
3202
	}
3203

    
3204
	function wconfig() {
3205
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3206
		if (!is_array($cflink)) {
3207
			$cflink = array();
3208
		}
3209
		$cflink['interface'] = $this->GetInterface();
3210
		$cflink['qlimit'] = trim($this->GetQlimit());
3211
		if (empty($cflink['qlimit'])) {
3212
			unset($cflink['qlimit']);
3213
		}
3214
		$cflink['priority'] = $this->GetQpriority();
3215
		if (!is_numeric($cflink['priority'])) {
3216
			unset($cflink['priority']);
3217
		}
3218
		$cflink['name'] = $this->GetQname();
3219
		$cflink['description'] = $this->GetDescription();
3220
		if (empty($cflink['description'])) {
3221
			unset($cflink['description']);
3222
		}
3223
		$cflink['bandwidth'] = $this->GetBandwidth();
3224
		$cflink['bandwidthtype'] = $this->GetBwscale();
3225
		$cflink['enabled'] = trim($this->GetEnabled());
3226
		if (empty($cflink['enabled'])) {
3227
			unset($cflink['enabled']);
3228
		}
3229
		$cflink['default'] = trim($this->GetDefault());
3230
		if (empty($cflink['default'])) {
3231
			unset($cflink['default']);
3232
		}
3233
		$cflink['red'] = trim($this->GetRed());
3234
		if (empty($cflink['red'])) {
3235
			unset($cflink['red']);
3236
		}
3237
		$cflink['rio'] = trim($this->GetRio());
3238
		if (empty($cflink['rio'])) {
3239
			unset($cflink['rio']);
3240
		}
3241
		$cflink['ecn'] = trim($this->GetEcn());
3242
		if (empty($cflink['ecn'])) {
3243
			unset($cflink['ecn']);
3244
		}
3245
		$cflink['codel'] = trim($this->GetCodel());
3246
		if (empty($cflink['codel'])) {
3247
			unset($cflink['codel']);
3248
		}
3249
		$cflink['borrow'] = trim($this->GetBorrow());
3250
		if (empty($cflink['borrow'])) {
3251
			unset($cflink['borrow']);
3252
		}
3253
	}
3254
}
3255

    
3256
class fairq_queue extends priq_queue {
3257
	var $hogs;
3258
	var $buckets;
3259

    
3260
	function GetBuckets() {
3261
		return $this->buckets;
3262
	}
3263
	function SetBuckets($buckets) {
3264
		$this->buckets = $buckets;
3265
	}
3266
	function GetHogs() {
3267
		return $this->hogs;
3268
	}
3269
	function SetHogs($hogs) {
3270
		$this->hogs = $hogs;
3271
	}
3272
	function CanHaveChildren() {
3273
		return false;
3274
	}
3275

    
3276

    
3277
	function copy_queue($interface, &$cflink) {
3278
		$cflink['interface'] = $interface;
3279
		$cflink['qlimit'] = $this->GetQlimit();
3280
		$cflink['priority'] = $this->GetQpriority();
3281
		$cflink['name'] = $this->GetQname();
3282
		$cflink['description'] = $this->GetDescription();
3283
		$cflink['bandwidth'] = $this->GetBandwidth();
3284
		$cflink['bandwidthtype'] = $this->GetBwscale();
3285
		$cflink['enabled'] = $this->GetEnabled();
3286
		$cflink['default'] = $this->GetDefault();
3287
		$cflink['red'] = $this->GetRed();
3288
		$cflink['rio'] = $this->GetRio();
3289
		$cflink['ecn'] = $this->GetEcn();
3290
		$cflink['buckets'] = $this->GetBuckets();
3291
		$cflink['hogs'] = $this->GetHogs();
3292
	}
3293

    
3294
	/*
3295
	 * Should search even its children
3296
	 */
3297
	function &find_queue($interface, $qname) {
3298
		if ($qname == $this->GetQname()) {
3299
			return $this;
3300
		}
3301
	}
3302

    
3303
	function find_parentqueue($interface, $qname) { return; }
3304

    
3305
	function delete_queue() {
3306
		unref_on_altq_queue_list($this->GetQname());
3307
		cleanup_queue_from_rules($this->GetQname());
3308
		unset_object_by_reference($this->GetLink());
3309
	}
3310

    
3311
	function validate_input($data, &$input_errors) {
3312
		parent::validate_input($data, $input_errors);
3313

    
3314
		if ($data['priority'] > 7) {
3315
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3316
		}
3317
		$reqdfields[] = "bandwidth";
3318
		$reqdfieldsn[] = gettext("Bandwidth");
3319
		$reqdfields[] = "bandwidthtype";
3320
		$reqdfieldsn[] = gettext("Bandwidthtype");
3321

    
3322
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3323
	}
3324

    
3325
	function ReadConfig(&$q) {
3326
		parent::ReadConfig($q);
3327
		if (!empty($q['buckets'])) {
3328
			$this->SetBuckets($q['buckets']);
3329
		} else {
3330
			$this->SetBuckets("");
3331
		}
3332
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3333
			$this->SetHogs($q['hogs']);
3334
		} else {
3335
			$this->SetHogs("");
3336
		}
3337
	}
3338

    
3339
	function build_javascript() {
3340
		return parent::build_javascript();
3341
	}
3342

    
3343
	function build_tree() {
3344
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3345
		$this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3346
		$tree .= "\" ";
3347
		$tmpvalue = trim($this->GetDefault());
3348
		if (!empty($tmpvalue)) {
3349
			$tree .= " class=\"navlnk\"";
3350
		}
3351
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3352
		$tree .= "</li>";
3353
		return $tree;
3354
	}
3355

    
3356
	/* Even this should take children into consideration */
3357
	function build_rules(&$default = false) {
3358
		$pfq_rule = "queue ". $this->qname;
3359
		if ($this->GetInterface()) {
3360
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3361
		}
3362
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3363
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3364
		}
3365
		$tmpvalue = trim($this->GetQpriority());
3366
		if (is_numeric($tmpvalue)) {
3367
			$pfq_rule .= " priority " . $this->GetQpriority();
3368
		}
3369
		$tmpvalue = trim($this->GetQlimit());
3370
		if (!empty($tmpvalue)) {
3371
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3372
		}
3373
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3374
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3375
			$pfq_rule .= " fairq ( ";
3376
			$tmpvalue = trim($this->GetRed());
3377
			if (!empty($tmpvalue)) {
3378
				$comma = 1;
3379
				$pfq_rule .= " red ";
3380
			}
3381
			$tmpvalue = trim($this->GetCodel());
3382
			if (!empty($tmpvalue)) {
3383
				$comma = 1;
3384
				$pfq_rule .= " codel ";
3385
			}
3386
			$tmpvalue = trim($this->GetRio());
3387
			if (!empty($tmpvalue)) {
3388
				if ($comma) {
3389
					$pfq_rule .= " ,";
3390
				}
3391
				$comma = 1;
3392
				$pfq_rule .= " rio ";
3393
			}
3394
			$tmpvalue = trim($this->GetEcn());
3395
			if (!empty($tmpvalue)) {
3396
				if ($comma) {
3397
					$pfq_rule .= " ,";
3398
				}
3399
				$comma = 1;
3400
				$pfq_rule .= " ecn ";
3401
			}
3402
			$tmpvalue = trim($this->GetDefault());
3403
			if (!empty($tmpvalue)) {
3404
				if ($comma) {
3405
					$pfq_rule .= " ,";
3406
				}
3407
				$comma = 1;
3408
				$pfq_rule .= " default ";
3409
				$default = true;
3410
			}
3411
			$tmpvalue = trim($this->GetBuckets());
3412
			if (!empty($tmpvalue)) {
3413
				if ($comma) {
3414
					$pfq_rule .= ", ";
3415
				}
3416
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3417
			}
3418
			$tmpvalue = trim($this->GetHogs());
3419
			if (!empty($tmpvalue)) {
3420
				if ($comma) {
3421
					$pfq_rule .= ", ";
3422
				}
3423
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3424
			}
3425
			$pfq_rule .= " ) ";
3426
		}
3427

    
3428
		$pfq_rule .= " \n";
3429
		return $pfq_rule;
3430
	}
3431

    
3432
	function build_form() {
3433
		$form = parent::build_form();
3434

    
3435
		$section = new Form_Section('');
3436

    
3437
		$group = new Form_Group('Bandwidth');
3438

    
3439
		$group->add(new Form_Input(
3440
			'bandwidth',
3441
			null,
3442
			'number',
3443
			$this->GetBandwidth()
3444
		));
3445

    
3446
		$group->add(new Form_Select(
3447
			'bandwidthtype',
3448
			null,
3449
			$this->FormGetBwscale(),
3450
			array('Kb' => 'Kbit/s',
3451
				  'Mb' => 'Mbit/s',
3452
				  'Gb' => 'Gbit/s',
3453
				  'b' => 'Bit/s',
3454
				  '%' => '%')
3455
		));
3456

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

    
3459
		$section->add($group);
3460

    
3461
		$section->addInput(new Form_Input(
3462
			'buckets',
3463
			'Scheduler specific options',
3464
			'text',
3465
			$this->GetBuckets()
3466
		))->setHelp('Number of buckets available');
3467

    
3468
		$section->addInput(new Form_Input(
3469
			'hogs',
3470
			'',
3471
			'text',
3472
			$this->GetHogs()
3473
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3474

    
3475
		$form->add($section);
3476
		return $form;
3477
	}
3478

    
3479
	function update_altq_queue_data(&$data) {
3480
		$this->ReadConfig($data);
3481
	}
3482

    
3483
	function wconfig() {
3484
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3485
		if (!is_array($cflink)) {
3486
			$cflink = array();
3487
		}
3488
		$cflink['interface'] = $this->GetInterface();
3489
		$cflink['qlimit'] = trim($this->GetQlimit());
3490
		if (empty($cflink['qlimit'])) {
3491
			unset($cflink['qlimit']);
3492
		}
3493
		$cflink['priority'] = trim($this->GetQpriority());
3494
		if (!is_numeric($cflink['priority'])) {
3495
			unset($cflink['priority']);
3496
		}
3497
		$cflink['name'] = $this->GetQname();
3498
		$cflink['description'] = trim($this->GetDescription());
3499
		if (empty($cflink['description'])) {
3500
			unset($cflink['description']);
3501
		}
3502
		$cflink['bandwidth'] = $this->GetBandwidth();
3503
		$cflink['bandwidthtype'] = $this->GetBwscale();
3504
		$cflink['enabled'] = $this->GetEnabled();
3505
		if (empty($cflink['enabled'])) {
3506
			unset($cflink['enabled']);
3507
		}
3508
		$cflink['default'] = trim($this->GetDefault());
3509
		if (empty($cflink['default'])) {
3510
			unset($cflink['default']);
3511
		}
3512
		$cflink['red'] = trim($this->GetRed());
3513
		if (empty($cflink['red'])) {
3514
			unset($cflink['red']);
3515
		}
3516
		$cflink['rio'] = trim($this->GetRio());
3517
		if (empty($cflink['rio'])) {
3518
			unset($cflink['rio']);
3519
		}
3520
		$cflink['ecn'] = trim($this->GetEcn());
3521
		if (empty($cflink['ecn'])) {
3522
			unset($cflink['ecn']);
3523
		}
3524
		$cflink['codel'] = trim($this->GetCodel());
3525
		if (empty($cflink['codel'])) {
3526
			unset($cflink['codel']);
3527
		}
3528
		$cflink['buckets'] = trim($this->GetBuckets());
3529
		if (empty($cflink['buckets'])) {
3530
			unset($cflink['buckets']);
3531
		}
3532
		$cflink['hogs'] = trim($this->GetHogs());
3533
		if (empty($cflink['hogs'])) {
3534
			unset($cflink['hogs']);
3535
		}
3536
	}
3537
}
3538

    
3539

    
3540
/*
3541
 * dummynet(4) wrappers.
3542
 */
3543

    
3544

    
3545
/*
3546
 * List of respective objects!
3547
 */
3548
$dummynet_pipe_list = array();
3549

    
3550
class dummynet_class {
3551
	var $qname;
3552
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3553
	var $qlimit;
3554
	var $description;
3555
	var $qenabled;
3556
	var $link;
3557
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3558
	var $plr;
3559

    
3560
	var $buckets;
3561
	/* mask parameters */
3562
	var $mask;
3563
	var $noerror;
3564

    
3565
	/* Accessor functions */
3566
	function SetLink($link) {
3567
		$this->link = $link;
3568
	}
3569
	function GetLink() {
3570
		return $this->link;
3571
	}
3572
	function GetMask() {
3573
		if (!isset($this->mask["type"])) {
3574
			$this->mask["type"] = "none";
3575
		}
3576
		return $this->mask;
3577
	}
3578
	function SetMask($mask) {
3579
		$this->mask = $mask;
3580
	}
3581
	function &GetParent() {
3582
		return $this->qparent;
3583
	}
3584
	function SetParent(&$parent) {
3585
		$this->qparent = &$parent;
3586
	}
3587
	function GetEnabled() {
3588
		return $this->qenabled;
3589
	}
3590
	function SetEnabled($value) {
3591
		$this->qenabled = $value;
3592
	}
3593
	function CanHaveChildren() {
3594
		return false;
3595
	}
3596
	function CanBeDeleted() {
3597
		return true;
3598
	}
3599
	function GetQname() {
3600
		return $this->qname;
3601
	}
3602
	function SetQname($name) {
3603
		$this->qname = trim($name);
3604
	}
3605
	function GetQlimit() {
3606
		return $this->qlimit;
3607
	}
3608
	function SetQlimit($limit) {
3609
		$this->qlimit = $limit;
3610
	}
3611
	function GetDescription() {
3612
		return $this->description;
3613
	}
3614
	function SetDescription($str) {
3615
		$this->description = trim($str);
3616
	}
3617
	function GetFirstime() {
3618
		return $this->firsttime;
3619
	}
3620
	function SetFirsttime($number) {
3621
		$this->firsttime = $number;
3622
	}
3623
	function GetBuckets() {
3624
		return $this->buckets;
3625
	}
3626
	function SetBuckets($buckets) {
3627
		$this->buckets = $buckets;
3628
	}
3629
	function SetNumber($number) {
3630
		$this->qnumber = $number;
3631
	}
3632
	function GetNumber() {
3633
		return $this->qnumber;
3634
	}
3635
	function GetPlr() {
3636
		return $this->plr;
3637
	}
3638
	function SetPlr($plr) {
3639
		$this->plr = $plr;
3640
	}
3641

    
3642
	function build_javascript() {
3643
		$javascript .= "<script type=\"text/javascript\">\n";
3644
		$javascript .= "//<![CDATA[\n";
3645
		$javascript .= "function enable_maskbits(enable_over) {\n";
3646
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3647
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3648
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3649
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3650
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3651
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3652
		$javascript .= "} else {\n";
3653
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3654
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3655
		$javascript .= "}}\n";
3656
		$javascript .= "//]]>\n";
3657
		$javascript .= "</script>\n";
3658
		return $javascript;
3659
	}
3660

    
3661
	function validate_input($data, &$input_errors) {
3662
		$reqdfields[] = "bandwidth";
3663
		$reqdfieldsn[] = gettext("Bandwidth");
3664
		/*$reqdfields[] = "burst";
3665
		$reqdfieldsn[] = gettext("Burst"); */
3666
		$reqdfields[] = "bandwidthtype";
3667
		$reqdfieldsn[] = gettext("Bandwidthtype");
3668
		$reqdfields[] = "newname";
3669
		$reqdfieldsn[] = gettext("Name");
3670

    
3671
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3672

    
3673
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3674
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3675
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3676
		}
3677
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3678
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3679
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3680
		}
3681
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3682
			$input_errors[] = gettext("Queue limit must be an integer");
3683
		}
3684
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3685
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3686
		}
3687
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3688
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3689
		}
3690
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3691
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3692
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3693
			}
3694
		}
3695
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3696
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3697
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3698
			}
3699
		}
3700
	}
3701

    
3702
	function build_mask_rules(&$pfq_rule) {
3703
		$mask = $this->GetMask();
3704
		if (!empty($mask['type'])) {
3705
			if ($mask['type'] <> 'none') {
3706
				$pfq_rule .= " mask";
3707
			}
3708
			switch ($mask['type']) {
3709
				case 'srcaddress':
3710
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3711
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3712
					} else {
3713
						$pfq_rule .= " src-ip6 /128";
3714
					}
3715
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3716
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3717
					} else {
3718
						$pfq_rule .= " src-ip 0xffffffff";
3719
					}
3720
					break;
3721
				case 'dstaddress':
3722
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3723
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3724
					} else {
3725
						$pfq_rule .= " dst-ip6 /128";
3726
					}
3727
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3728
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3729
					} else {
3730
						$pfq_rule .= " dst-ip 0xffffffff";
3731
					}
3732
					break;
3733
				default:
3734
					break;
3735
			}
3736
		}
3737
	}
3738

    
3739
}
3740

    
3741
class dnpipe_class extends dummynet_class {
3742
	var $delay;
3743
	var $qbandwidth = array();
3744
	var $qbandwidthtype;
3745

    
3746
	/* Limiter queue patch */
3747
	var $ecn; // ecn 'on' or 'off'
3748
	var $aqm; // key to aqm_map
3749
	var $aqm_params = array(); // AQM params
3750
	var $scheduler;	// key to scheduler_map
3751
	var $scheduler_params = array(); // AQM params
3752
	function GetAQM() {
3753
			return $this->aqm;
3754
	}
3755
	function SetAQM($aqm) {
3756
			$this->aqm = $aqm;
3757
	}
3758
	function GetAQMParameters() {
3759
			return $this->aqm_params;
3760
	}
3761
	function GetAQMParameter($parameter) {
3762
			return $this->aqm_params[$parameter];
3763
	}
3764
	function SetAQMParameter($key, $value) {
3765
			return $this->aqm_params[$key] = $value;
3766
	}
3767
	function SetAQMParameters($params) {
3768
			$this->aqm_params = $params;
3769
	}
3770
	function GetECN() {
3771
			return $this->ecn;
3772
	}
3773
	function SetECN($ecn) {
3774
			$this->ecn = $ecn;
3775
	}
3776
	function GetScheduler() {
3777
			return $this->scheduler;
3778
	}
3779
	function SetScheduler($scheduler) {
3780
			$this->scheduler = $scheduler;
3781
	}
3782
	function GetSchedulerParameters() {
3783
			return $this->scheduler_params;
3784
	}
3785
	function SetSchedulerParameters($params) {
3786
			$this->scheduler_params = $params;
3787
	}
3788
	function SetSchedulerParameter($key, $value) {
3789
			$this->scheduler_params[$key] = $value;
3790
	}
3791
	function GetSchedulerParameter($key) {
3792
			return $this->scheduler_params[$key];
3793
	}
3794
	/* End limiter queue patch */
3795

    
3796
		/* This is here to help on form building and building rules/lists */
3797
	var $subqueues = array();
3798

    
3799
	function CanHaveChildren() {
3800
		return true;
3801
	}
3802
	function SetDelay($delay) {
3803
		$this->delay = $delay;
3804
	}
3805
	function GetDelay() {
3806
		return $this->delay;
3807
	}
3808
	function delete_queue() {
3809
		cleanup_dnqueue_from_rules($this->GetQname());
3810
		foreach ($this->subqueues as $q) {
3811
			$q->delete_queue();
3812
		}
3813
		unset_dn_object_by_reference($this->GetLink());
3814
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3815
		@pfSense_ipfw_pipe("sched delete " . $this->GetNumber());
3816
	}
3817
	function GetBandwidth() {
3818
		return $this->qbandwidth;
3819
	}
3820
	function SetBandwidth($bandwidth) {
3821
		$this->qbandwidth = $bandwidth;
3822
	}
3823
	function GetBurst() {
3824
		return $this->qburst;
3825
	}
3826
	function SetBurst($burst) {
3827
		$this->qburst = $burst;
3828
	}
3829

    
3830
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3831

    
3832
		if (!is_array($this->subqueues)) {
3833
			$this->subqueues = array();
3834
		}
3835

    
3836
		$__tmp_q = new dnqueue_class(); $q =& $__tmp_q;
3837
		$q->SetLink($path);
3838
		$q->SetEnabled("on");
3839
		$q->SetPipe($this->GetQname());
3840
		$q->SetParent($this);
3841
		$q->ReadConfig($queue);
3842
		$q->validate_input($queue, $input_errors);
3843

    
3844
		if (!is_array($input_errors)) {
3845
			$input_errors = array();
3846
		}
3847

    
3848
		if (count($input_errors)) {
3849
			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)));
3850
			return $q;
3851
		}
3852
		$number = dnqueue_find_nextnumber();
3853
		$q->SetNumber($number);
3854
		$this->subqueues[$q->GetQname()] = &$q;
3855

    
3856
		return $q;
3857
	}
3858

    
3859
	function &get_queue_list(&$q = null) {
3860
		$qlist = array();
3861

    
3862
		$qlist[$this->GetQname()] = $this->GetNumber();
3863
		if (is_array($this->subqueues)) {
3864
			foreach ($this->subqueues as $queue) {
3865
				$queue->get_queue_list($qlist);
3866
			}
3867
		}
3868
		return $qlist;
3869
	}
3870

    
3871
	/*
3872
	 * Should search even its children
3873
	 */
3874
	function &find_queue($pipe, $qname) {
3875
		if ($qname == $this->GetQname()) {
3876
			return $this;
3877
		}
3878
		foreach ($this->subqueues as $q) {
3879
			$result =& $q->find_queue("", $qname);
3880
			if ($result) {
3881
				return $result;
3882
			}
3883
		}
3884
	}
3885

    
3886
	function &find_parentqueue($pipe, $qname) {
3887
		return NULL;
3888
	}
3889

    
3890
	function validate_input($data, &$input_errors) {
3891
		parent::validate_input($data, $input_errors);
3892

    
3893
		$schedule = 0;
3894
		$schedulenone = 0;
3895
		$entries = 0;
3896
		/* XXX: Really no better way? */
3897
		for ($i = 0; $i < 2900; $i++) {
3898
			if (!empty($data["bwsched{$i}"])) {
3899
				if ($data["bwsched{$i}"] != "none") {
3900
					$schedule++;
3901
				} else {
3902
					$schedulenone++;
3903
				}
3904
			}
3905
			if (!empty($data["bandwidth{$i}"])) {
3906
				if (!is_numeric($data["bandwidth{$i}"])) {
3907
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3908
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3909
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3910
				} else {
3911
					$entries++;
3912
				}
3913
			}
3914
		}
3915
		if ($schedule == 0 && $entries > 1) {
3916
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3917
		}
3918
		if ($schedulenone > 0 && $entries > 1) {
3919
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3920
		}
3921
		if ($entries == 0) {
3922
			$input_errors[] = gettext("At least one bw specification is necessary.");
3923
		}
3924
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3925
			$input_errors[] = gettext("Delay must be an integer.");
3926
		}
3927
		if ($data['delay'] && is_numeric($data['delay']) &&
3928
		    (($data['delay'] < 0) || ($data['delay'] > 10000))) {
3929
			$input_errors[] = gettext("Delay must be an integer between 0 and 10000.");
3930
		}
3931

    
3932
		/* Limiter patch */
3933
		$selectedScheduler = getSchedulers()[$data['sched']];
3934
		if (!$selectedScheduler) {
3935
			$input_errors[] = gettext("Selected scheduler not recognized.");
3936
		}
3937
		$selectedAqm = getAQMs()[$data['aqm']];
3938
		if (!empty($data['aqm']) && !$selectedAqm) {
3939
			$input_errors[] = gettext("Selected AQM not recognized.");
3940
		}
3941
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedScheduler["ecn"] && !$selectedAqm["ecn"]) {
3942
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but neither " . $selectedAqm["name"] . " nor " . $selectedScheduler["name"] . " support it.");
3943
		}
3944
		/* End limiter patch */
3945
	}
3946

    
3947
	function ReadConfig(&$q) {
3948
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3949
			$this->SetQname($q['newname']);
3950
			rename_dnqueue_in_rules($q['name'], $q['newname']);
3951
		} else if (!empty($q['newname'])) {
3952
			$this->SetQname($q['newname']);
3953
		} else {
3954
			$this->SetQname($q['name']);
3955
		}
3956
		$this->SetNumber($q['number']);
3957

    
3958
		if (!empty($_POST)) {
3959
			$bandwidth = array();
3960
			/* XXX: Really no better way? */
3961
			for ($i = 0; $i < 2900; $i++) {
3962
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3963
					$bw = array();
3964
					$bw['bw'] = $q["bandwidth{$i}"];
3965
					$bw['burst'] = $q["burst{$i}"];
3966
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3967
						$bw['bwscale'] = $q["bwtype{$i}"];
3968
					}
3969
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3970
						$bw['bwsched'] = $q["bwsched{$i}"];
3971
					}
3972
					$bandwidth[] = $bw;
3973
				}
3974
			}
3975
			$this->SetBandwidth($bandwidth);
3976
		}
3977

    
3978
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3979
			$this->SetBandwidth($q['bandwidth']['item']);
3980
			$this->SetBurst($q['burst']['item']);
3981
		}
3982

    
3983
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3984
			$this->SetQlimit($q['qlimit']);
3985
		} else {
3986
			$this->SetQlimit("");
3987
		}
3988
		if (isset($q['mask']) && $q['mask'] <> "") {
3989
			$masktype = $q['mask'];
3990
		} else {
3991
			$masktype = "";
3992
		}
3993
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3994
			$maskbits = $q['maskbits'];
3995
		} else {
3996
			$maskbits = "";
3997
		}
3998
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3999
			$maskbitsv6 = $q['maskbitsv6'];
4000
		} else {
4001
			$maskbitsv6 = "";
4002
		}
4003
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4004
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4005
			$this->SetBuckets($q['buckets']);
4006
		} else {
4007
			$this->SetBuckets("");
4008
		}
4009
		if (isset($q['plr']) && $q['plr'] <> "") {
4010
			$this->SetPlr($q['plr']);
4011
		} else {
4012
			$this->SetPlr("");
4013
		}
4014
		if (isset($q['delay']) && $q['delay'] <> "") {
4015
			$this->SetDelay($q['delay']);
4016
		} else {
4017
			$this->SetDelay(0);
4018
		}
4019
		if (isset($q['description']) && $q['description'] <> "") {
4020
			$this->SetDescription($q['description']);
4021
		} else {
4022
			$this->SetDescription("");
4023
		}
4024
		$this->SetEnabled($q['enabled']);
4025

    
4026
		/* Limiter patch */
4027
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4028
				$this->SetAQM($q['aqm']);
4029
		} else {
4030
				$this->SetAQM('droptail');
4031
		}
4032
		// parse AQM arguments
4033
		$aqm_map = getAQMs();
4034
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4035
		if (is_array($selectedParameters)) {
4036
			foreach ($selectedParameters as $key => $value) {
4037
				$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4038
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4039
					$this->SetAQMParameter($key, $q[$config_key]);
4040
				} else {
4041
					$this->SetAQMParameter($key, $value["default"]);
4042
				}
4043
			}
4044
		}
4045

    
4046
		if (isset($q['sched']) && $q['sched'] <> "") {
4047
				$this->SetScheduler($q['sched']);
4048
		} else {
4049
				$this->SetScheduler('fifo');
4050
		}
4051
		$scheduler_map = getSchedulers();
4052
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4053
		if (is_array($selectedParameters)) {
4054
			foreach ($selectedParameters as $key => $value) {
4055
				$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4056
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4057
					$this->SetSchedulerParameter($key, $q[$config_key]);
4058
				} else {
4059
					$this->SetSchedulerParameter($key, $value["default"]);
4060
				}
4061
			}
4062
		}
4063

    
4064
		// ecn flag
4065
		$this->SetECN($q['ecn']);
4066
		/* End limiter patch */
4067
	}
4068

    
4069
	function build_tree() {
4070
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($this->GetQname()) ."&amp;queue=".htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4071
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4072
		if (is_array($this->subqueues)) {
4073
			$tree .= "<ul>";
4074
			foreach ($this->subqueues as $q) {
4075
				$tree .= $q->build_tree();
4076
			}
4077
			$tree .= "</ul>";
4078
		}
4079
		$tree .= "</li>";
4080

    
4081
		return $tree;
4082
	}
4083

    
4084
	function build_rules() {
4085
		global $config, $time_based_rules;
4086

    
4087
		if ($this->GetEnabled() == "") {
4088
			return;
4089
		}
4090

    
4091
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
4092
		$found = false;
4093
		$bandwidth = $this->GetBandwidth();
4094
		if (is_array($bandwidth)) {
4095
			foreach ($bandwidth as $bw) {
4096
				if ($bw['bwsched'] != "none") {
4097
					$time_based_rules = true;
4098
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4099
						foreach ($config['schedules']['schedule'] as $schedule) {
4100
							if ($bw['bwsched'] == $schedule['name']) {
4101
								if (filter_get_time_based_rule_status($schedule)) {
4102
									/* pipe throughputs must always be an integer, enforce that restriction again here. */
4103
									$pfq_rule .= " bw ".round(trim($bw['bw']),0).$bw['bwscale'];
4104
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4105
										$pfq_rule .= " burst ".trim($bw['burst']);
4106
									}
4107
									$found = true;
4108
									break;
4109
								}
4110
							}
4111
						}
4112
					} else {
4113
						$pfq_rule .= " bw 0";
4114
						$found = true;
4115
						break;
4116
					}
4117
				} else {
4118
					/* pipe throughputs must always be an integer, enforce that restriction again here. */
4119
					$pfq_rule .= " bw ".round(trim($bw['bw']), 0).$bw['bwscale'];
4120
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4121
						$pfq_rule .= " burst ".trim($bw['burst']);
4122
					}
4123
					$found = true;
4124
					break;
4125
				}
4126
			}
4127
			if ($found == false) {
4128
				$pfq_rule .= " bw 0";
4129
			}
4130
		} else {
4131
			$pfq_rule .= " bw 0";
4132
		}
4133

    
4134
		if ($this->GetQlimit()) {
4135
			$pfq_rule .= " queue " . $this->GetQlimit();
4136
		}
4137
		if ($this->GetPlr()) {
4138
			$pfq_rule .= " plr " . $this->GetPlr();
4139
		}
4140
		if ($this->GetBuckets()) {
4141
			$pfq_rule .= " buckets " . $this->GetBuckets();
4142
		}
4143
		if ($this->GetDelay()) {
4144
			$pfq_rule .= " delay " . $this->GetDelay();
4145
		}
4146
		$this->build_mask_rules($pfq_rule);
4147

    
4148
		/* Limiter patch */
4149
		$selectedAQM = getAQMs()[$this->getAQM()];
4150
		if ($selectedAQM) {
4151
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4152
			if ($selectedAQM["ecn"]) {
4153
				if ($this->getECN() == 'on') {
4154
					$pfq_rule .= ' ecn';
4155
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4156
					$pfq_rule .= ' noecn';
4157
				}
4158
			}
4159
		}
4160
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4161
		if ($selectedScheduler) {
4162
			$pfq_rule .= "\nsched ". $this->GetNumber() . " config ";
4163
			$pfq_rule .= "pipe ". $this->GetNumber();
4164
			$this->build_mask_rules($pfq_rule);
4165
			$pfq_rule .= " " . FormatParameters($selectedScheduler["parameter_format"], $this->GetSchedulerParameters());			
4166
			if ($selectedScheduler["ecn"]) {
4167
				if ($this->getECN() == 'on') {
4168
					$pfq_rule .= ' ecn';
4169
				} else {
4170
					$pfq_rule .= ' noecn';
4171
				}
4172
			}
4173
		}
4174
		$pfq_rule .= "\n";
4175
		/* End patch */
4176

    
4177
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
4178
			foreach ($this->subqueues as $q) {
4179
				$pfq_rule .= $q->build_rules();
4180
			}
4181
		}
4182

    
4183
		$pfq_rule .= " \n";
4184

    
4185
		return $pfq_rule;
4186
	}
4187

    
4188
	function update_dn_data(&$data) {
4189
		$this->ReadConfig($data);
4190
	}
4191

    
4192
	function build_javascript() {
4193
		global $g, $config;
4194

    
4195
		$javasr = parent::build_javascript();
4196

    
4197
		//build list of schedules
4198
		$schedules = "<option value='none'>none</option>";
4199
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4200
			foreach ($config['schedules']['schedule'] as $schedule) {
4201
				if ($schedule['name'] <> "") {
4202
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
4203
				}
4204
			}
4205
		}
4206
		$bwopt = "";
4207
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
4208
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
4209
		}
4210

    
4211
		$javasr .= <<<EOD
4212
<script type='text/javascript'>
4213
//<![CDATA[
4214
var addBwRowTo = (function() {
4215

    
4216
	return (function (tableId) {
4217

    
4218
	var table = document.getElementById(tableId);
4219
	var totalrows = table.rows.length -1;
4220

    
4221
	var row = table.insertRow(totalrows + 1);
4222
	var cell1 = row.insertCell(0);
4223
	var cell2 = row.insertCell(1);
4224
	var cell3 = row.insertCell(2);
4225
	var cell4 = row.insertCell(3);
4226

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

    
4232
	});
4233
})();
4234

    
4235
function removeBwRow(el) {
4236
	var d = el.parentNode.parentNode.rowIndex;
4237
	document.getElementById('maintable').deleteRow(d);
4238
}
4239

    
4240
function ceil_func(el){
4241
	el.value = Math.ceil(el.value);
4242

    
4243
}
4244
//]]>
4245
</script>
4246

    
4247
EOD;
4248

    
4249
		return $javasr;
4250
	}
4251

    
4252
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
4253
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
4254
	// with the javascript in this class
4255
	function build_bwtable() {
4256
		global $config;
4257

    
4258
		$bandwidth = $this->GetBandwidth();
4259
				//build list of schedules
4260
		$schedules = array();
4261
		$schedules[] = "none";//leave none to leave rule enabled all the time
4262
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4263
			foreach ($config['schedules']['schedule'] as $schedule) {
4264
				if ($schedule['name'] != "") {
4265
					$schedules[] = $schedule['name'];
4266
				}
4267
			}
4268
		}
4269

    
4270
		$form = '<div class="table-responsive">';
4271
		$form .= '<table id="maintable" class="table table-hover table-striped">';
4272
		$form .= "<thead><tr>";
4273
		$form .= "<th>Bandwidth</th>";
4274
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
4275
		$form .= "<th>Bw type</th>";
4276
		$form .= "<th>Schedule</th>";
4277
		$form .= "<th></th>";
4278
		$form .= "</tr></thead>";
4279
		$form .= "<tbody>";
4280

    
4281
		// If there are no bandwidths defined, make a blank one for convenience
4282
		if (empty($bandwidth)) {
4283
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Mb', 'bwsched' => 'none'));
4284
		}
4285

    
4286
		if (is_array($bandwidth)) {
4287
			foreach ($bandwidth as $bwidx => $bw) {
4288
				$form .= '<tr>';
4289
				$form .= '<td class="col-xs-4">';
4290
				$form .= "<input class='form-control' onchange=\"ceil_func(this)\" type=\"number\" id=\"bandwidth{$bwidx}\" name=\"bandwidth{$bwidx}\" value=\"" . ceil($bw['bw']) ."\" min=\"0\" step=\"1\"/>";
4291
				//$form .= "</td><td width='20%'>";
4292
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
4293
				$form .= "</td>";
4294
				$form .= '<td class="col-xs-4">';
4295
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
4296

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

    
4300
					if ($bw['bwscale'] == $bwsidx) {
4301
						$form .= " selected";
4302
					}
4303

    
4304
					$form .= ">{$bwscale}</option>";
4305
				}
4306

    
4307
				$form .= "</select>";
4308
				$form .= "</td>";
4309
				$form .= '<td class="col-xs-4">';
4310
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
4311

    
4312
				foreach ($schedules as $schd) {
4313
					$selected = "";
4314
					if ($bw['bwsched'] == $schd) {
4315
						$selected = "selected";
4316
					}
4317

    
4318
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
4319
				}
4320

    
4321
				$form .= "</select>";
4322
				$form .= "</td>";
4323
				$form .= '<td>';
4324
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
4325
				$form .= "</td></tr>";
4326
			}
4327
		}
4328
		$form .= "</tbody></table></div><br />";
4329

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

    
4334
		return($form);
4335
	}
4336

    
4337
	function build_form() {
4338
		global $g, $config, $pipe, $action, $qname;
4339

    
4340
		//build list of schedules
4341
		$schedules = array();
4342
		$schedules[] = "none";//leave none to leave rule enabled all the time
4343
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4344
			foreach ($config['schedules']['schedule'] as $schedule) {
4345
				if ($schedule['name'] <> "") {
4346
					$schedules[] = $schedule['name'];
4347
				}
4348
			}
4349
		}
4350

    
4351

    
4352
		$sform = new Form();
4353
		$sform->setAction("firewall_shaper.php");
4354

    
4355
		$section = new Form_Section('Limiters');
4356

    
4357
		$section->addInput(new Form_Checkbox(
4358
			'enabled',
4359
			'Enable',
4360
			'Enable limiter and its children',
4361
			($this->GetEnabled() == "on"),
4362
			'on'
4363
		));
4364

    
4365
		$section->addInput(new Form_Input(
4366
			'newname',
4367
			'*Name',
4368
			'text',
4369
			$this->GetQname()
4370
		));
4371

    
4372
		$section->addInput(new Form_Input(
4373
			'name',
4374
			null,
4375
			'hidden',
4376
			$this->GetQname()
4377
		));
4378

    
4379
		if ($this->GetNumber() > 0) {
4380
			$section->addInput(new Form_Input(
4381
				'number',
4382
				null,
4383
				'hidden',
4384
				$this->GetNumber()
4385
			));
4386
		}
4387

    
4388
		$bandwidth = $this->GetBandwidth();
4389

    
4390
		if (is_array($bandwidth)) {
4391
				$section->addInput(new Form_StaticText(
4392
				'Bandwidth',
4393
				$this->build_bwtable()
4394
			));
4395
		}
4396

    
4397
		$mask = $this->GetMask();
4398

    
4399
		$section->addInput(new Form_Select(
4400
			'mask',
4401
			'Mask',
4402
			$mask['type'],
4403
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4404
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4405
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4406
					'This makes it possible to easily specify bandwidth limits per host or subnet.');
4407

    
4408
		$group = new Form_Group(null);
4409

    
4410
		$group->add(new Form_Select(
4411
			'maskbits',
4412
			null,
4413
			$mask['bits'],
4414
			array_combine(range(32, 1, -1), range(32, 1, -1))
4415
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4416

    
4417
		$group->add(new Form_Select(
4418
			'maskbitsv6',
4419
			null,
4420
			$mask['bitsv6'],
4421
			array_combine(range(128, 1, -1), range(128, 1, -1))
4422
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4423

    
4424
		$section->add($group);
4425

    
4426
		$section->addInput(new Form_Input(
4427
			'description',
4428
			'Description',
4429
			'text',
4430
			$this->GetDescription()
4431
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4432

    
4433
		$sform->add($section);
4434

    
4435
		/* Begin limiter patch */
4436
		$aqm_map = getAQMs();
4437
		$scheduler_map = getSchedulers();
4438

    
4439
		$section = new Form_Section('Queue');
4440
		$section->addInput(new Form_Select(
4441
				'aqm',
4442
				'Queue Management Algorithm',
4443
				$this->GetAQM(),
4444
				array_map_assoc(function ($k, $v) {
4445
					return [$k, $v["name"]];
4446
				}, $aqm_map)
4447
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside the limiter, ' .
4448
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4449
						'network congestion.');
4450

    
4451
		$section->addInput(new Form_StaticText(
4452
			'',
4453
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4454
		))->setHelp('Specifies the queue management algorithm parameters.');
4455

    
4456
		$section->addInput(new Form_Select(
4457
				'sched',
4458
				'Scheduler',
4459
				$this->GetScheduler(),
4460
				array_map_assoc(function ($k, $v) {
4461
					return [$k, $v["name"]];
4462
				}, $scheduler_map)
4463
		))->setHelp('The scheduler manages the sequence of network packets in the limiter\'s queue.');
4464

    
4465
		$section->addInput(new Form_StaticText(
4466
			'',
4467
			build_queue_params($scheduler_map, $this->GetScheduler(), $this->GetSchedulerParameters(), "sched")
4468
		))->setHelp('Specifies the scheduler parameters.');
4469

    
4470
		$section->addInput(new Form_Input(
4471
				'qlimit',
4472
				'Queue length',
4473
				'number',
4474
				$this->GetQlimit()
4475
		))->setHelp('Specifies the length of the limiter\'s queue, which the scheduler and AQM are responsible for. ' .
4476
			'This field may be left empty.');
4477

    
4478
		$section->addInput(new Form_Checkbox(
4479
			'ecn',
4480
			'ECN',
4481
			'Enable Explicit Congestion Notification (ECN)',
4482
			($this->GetECN() == "on"),
4483
			'on'
4484
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4485

    
4486
		$sform->add($section);
4487
		/* End limiter patch */
4488

    
4489
		$section = new Form_Section('Advanced Options');
4490

    
4491
		$section->addInput(new Form_Input(
4492
			'delay',
4493
			'Delay (ms)',
4494
			'text',
4495
			$this->GetDelay() > 0 ? $this->GetDelay():null
4496
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4497

    
4498
		$section->addInput(new Form_Input(
4499
			'plr',
4500
			'Packet Loss Rate',
4501
			'number',
4502
			$this->GetPlr(),
4503
			['step' => '0.001', 'min' => '0.000']
4504
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4505
					'A value of 0.001 means one packet in 1000 gets dropped.');
4506

    
4507
		$section->addInput(new Form_Input(
4508
			'buckets',
4509
			'Bucket size (slots)',
4510
			'number',
4511
			$this->GetBuckets()
4512
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4513

    
4514
		$sform->add($section);
4515

    
4516
		return($sform);
4517
		}
4518

    
4519
	function wconfig() {
4520
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4521
		if (!is_array($cflink)) {
4522
			$cflink = array();
4523
		}
4524
		$cflink['name'] = $this->GetQname();
4525
		$cflink['number'] = $this->GetNumber();
4526
		$cflink['qlimit'] = $this->GetQlimit();
4527
		$cflink['plr'] = $this->GetPlr();
4528
		$cflink['description'] = $this->GetDescription();
4529

    
4530
		$bandwidth = $this->GetBandwidth();
4531
		if (is_array($bandwidth)) {
4532
			$cflink['bandwidth'] = array();
4533
			$cflink['bandwidth']['item'] = array();
4534
			foreach ($bandwidth as $bwidx => $bw) {
4535
				$cflink['bandwidth']['item'][] = $bw;
4536
			}
4537
		}
4538

    
4539
		$cflink['enabled'] = $this->GetEnabled();
4540
		$cflink['buckets'] = $this->GetBuckets();
4541
		$mask = $this->GetMask();
4542
		$cflink['mask'] = $mask['type'];
4543
		$cflink['maskbits'] = $mask['bits'];
4544
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4545
		$cflink['delay'] = $this->GetDelay();
4546

    
4547
		/* Limiter queue patch */
4548
		$cflink['sched'] = $this->GetScheduler();
4549
		$scheduler_map = getSchedulers();
4550
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4551
		foreach ($selectedParameters as $key => $value) {
4552
			$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4553
			$cflink[$config_key] = $this->GetSchedulerParameter($key);
4554
		}
4555
		$cflink['aqm'] = $this->GetAQM();
4556
		$aqm_map = GetAQMs();
4557
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4558
		foreach ($selectedParameters as $key => $value) {
4559
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4560
			$cflink[$config_key] = $this->GetAQMParameter($key);
4561
		}
4562
		$cflink['ecn'] = $this->GetECN();
4563
		/* End limiter queue patch */
4564
	}
4565

    
4566
}
4567

    
4568
class dnqueue_class extends dummynet_class {
4569
	var $pipeparent;
4570
	var $weight;
4571
	/* Limiter queue patch */
4572
    var $ecn; // ecn 'on' or 'off'
4573
    var $aqm; // key to aqm_map
4574
    var $aqm_params = array(); // AQM params
4575
	function GetAQM() {
4576
			return $this->aqm;
4577
	}
4578
	function SetAQM($aqm) {
4579
			$this->aqm = $aqm;
4580
	}
4581
	function GetAQMParameters() {
4582
			return $this->aqm_params;
4583
	}
4584
	function GetAQMParameter($parameter) {
4585
			return $this->aqm_params[$parameter];
4586
	}
4587
	function SetAQMParameter($key, $value) {
4588
			return $this->aqm_params[$key] = $value;
4589
	}
4590
	function SetAQMParameters($params) {
4591
			$this->aqm_params = $params;
4592
	}
4593
	function GetECN() {
4594
			return $this->ecn;
4595
	}
4596
	function SetECN($ecn) {
4597
			$this->ecn = $ecn;
4598
	}
4599
	/* End limiter queue patch */
4600

    
4601
	function GetWeight() {
4602
		return $this->weight;
4603
	}
4604
	function SetWeight($weight) {
4605
		$this->weight = $weight;
4606
	}
4607
	function GetPipe() {
4608
		return $this->pipeparent;
4609
	}
4610
	function SetPipe($pipe) {
4611
		$this->pipeparent = $pipe;
4612
	}
4613

    
4614
	/* Just a stub in case we ever try to call this from the frontend. */
4615
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4616
		return;
4617
	}
4618

    
4619
	function delete_queue() {
4620
		cleanup_dnqueue_from_rules($this->GetQname());
4621
		unset_dn_object_by_reference($this->GetLink());
4622
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4623
	}
4624

    
4625
	function validate_input($data, &$input_errors) {
4626
		parent::validate_input($data, $input_errors);
4627

    
4628

    
4629
		/* Limiter patch */
4630
		$selectedAqm = getAQMs()[$data['aqm']];
4631
		if (!empty($data['aqm']) && !$selectedAqm) {
4632
			$input_errors[] = gettext("Selected AQM not recognized.");
4633
		}
4634
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedAqm["ecn"]) {
4635
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but " . $selectedAqm["name"] . " does not support it.");
4636
		}
4637
		/* End limiter patch */
4638

    
4639
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4640
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4641
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4642
		}
4643
	}
4644

    
4645
	/*
4646
	 * Should search even its children
4647
	 */
4648
	function &find_queue($pipe, $qname) {
4649
		if ($qname == $this->GetQname()) {
4650
			return $this;
4651
		} else {
4652
			return NULL;
4653
		}
4654
	}
4655

    
4656
	function &find_parentqueue($pipe, $qname) {
4657
		return $this->qparent;
4658
	}
4659

    
4660
	function &get_queue_list(&$qlist) {
4661
		if ($this->GetEnabled() == "") {
4662
			return;
4663
		}
4664
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4665
	}
4666

    
4667
	function ReadConfig(&$q) {
4668
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4669
			$this->SetQname($q['newname']);
4670
		} else if (!empty($q['newname'])) {
4671
			$this->SetQname($q['newname']);
4672
		} else {
4673
			$this->SetQname($q['name']);
4674
		}
4675
		$this->SetNumber($q['number']);
4676
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4677
			$this->SetQlimit($q['qlimit']);
4678
		} else {
4679
			$this->SetQlimit("");
4680
		}
4681
		if (isset($q['mask']) && $q['mask'] <> "") {
4682
			$masktype = $q['mask'];
4683
		} else {
4684
			$masktype = "";
4685
		}
4686
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4687
			$maskbits = $q['maskbits'];
4688
		} else {
4689
			$maskbits = "";
4690
		}
4691
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4692
			$maskbitsv6 = $q['maskbitsv6'];
4693
		} else {
4694
			$maskbitsv6 = "";
4695
		}
4696
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4697
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4698
			$this->SetBuckets($q['buckets']);
4699
		} else {
4700
			$this->SetBuckets("");
4701
		}
4702
		if (isset($q['plr']) && $q['plr'] <> "") {
4703
			$this->SetPlr($q['plr']);
4704
		} else {
4705
			$this->SetPlr("");
4706
		}
4707
		if (isset($q['weight']) && $q['weight'] <> "") {
4708
			$this->SetWeight($q['weight']);
4709
		} else {
4710
			$this->SetWeight("");
4711
		}
4712
		if (isset($q['description']) && $q['description'] <> "") {
4713
			$this->SetDescription($q['description']);
4714
		} else {
4715
			$this->SetDescription("");
4716
		}
4717
		$this->SetEnabled($q['enabled']);
4718

    
4719
		/* Limiter patch */
4720
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4721
				$this->SetAQM($q['aqm']);
4722
		} else {
4723
				$this->SetAQM('droptail');
4724
		}
4725
		// parse AQM arguments
4726
		$aqm_map = getAQMs();
4727
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4728
		foreach ($selectedParameters as $key => $value) {
4729
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4730
			if (isset($q[$config_key]) && $q[$config_key] <> "") {
4731
				$this->SetAQMParameter($key, $q[$config_key]);
4732
			} else {
4733
				$this->SetAQMParameter($key, $value["default"]);
4734
			}
4735
		}
4736

    
4737
		// ecn flag
4738
		$this->SetECN($q['ecn']);
4739
		/* End limiter patch */
4740
	}
4741

    
4742
	function build_tree() {
4743
		$parent =& $this->GetParent();
4744
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($parent->GetQname()) ."&amp;queue=" . htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4745
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4746
		$tree .= "</li>";
4747

    
4748
		return $tree;
4749
	}
4750

    
4751
	function build_rules() {
4752
		if ($this->GetEnabled() == "") {
4753
			return;
4754
		}
4755

    
4756
		$parent =& $this->GetParent();
4757
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4758
		if ($this->GetQlimit()) {
4759
			$pfq_rule .= " queue " . $this->GetQlimit();
4760
		}
4761
		if ($this->GetWeight()) {
4762
			$pfq_rule .= " weight " . $this->GetWeight();
4763
		}
4764
		if ($this->GetBuckets()) {
4765
			$pfq_rule .= " buckets " . $this->GetBuckets();
4766
		}
4767
		$this->build_mask_rules($pfq_rule);
4768

    
4769
		/* Limiter patch */
4770
		$selectedAQM = getAQMs()[$this->getAQM()];
4771
		if ($selectedAQM) {
4772
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4773
			if ($selectedAQM["ecn"]) {
4774
				if ($this->getECN() == 'on') {
4775
					$pfq_rule .= ' ecn';
4776
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4777
					$pfq_rule .= ' noecn';
4778
				}
4779
			}
4780
		}
4781
		/* End patch */
4782

    
4783
		$pfq_rule .= "\n";
4784

    
4785
		return $pfq_rule;
4786
	}
4787

    
4788
	function build_javascript() {
4789
		return parent::build_javascript();
4790
	}
4791

    
4792
	function build_form() {
4793
		global $g, $config, $pipe, $action, $qname;
4794

    
4795
		//build list of schedules
4796
		$schedules = array();
4797
		$schedules[] = "none";//leave none to leave rule enabled all the time
4798
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4799
			foreach ($config['schedules']['schedule'] as $schedule) {
4800
				if ($schedule['name'] <> "") {
4801
					$schedules[] = $schedule['name'];
4802
				}
4803
			}
4804
		}
4805

    
4806

    
4807
		$sform = new Form();
4808
		$sform->setAction("firewall_shaper.php");
4809
		$section = new Form_Section('Limiters');
4810

    
4811
		$section->addInput(new Form_Checkbox(
4812
			'enabled',
4813
			'Enable',
4814
			'Enable this queue',
4815
			($this->GetEnabled() == "on"),
4816
			'on'
4817
		));
4818

    
4819
		$section->addInput(new Form_Input(
4820
			'newname',
4821
			'*Name',
4822
			'text',
4823
			$this->GetQname()
4824
		));
4825

    
4826
		$section->addInput(new Form_Input(
4827
			'name',
4828
			null,
4829
			'hidden',
4830
			$this->GetQname()
4831
		));
4832

    
4833
		if ($this->GetNumber() > 0) {
4834
			$section->addInput(new Form_Input(
4835
				'number',
4836
				null,
4837
				'hidden',
4838
				$this->GetNumber()
4839
			));
4840
		}
4841

    
4842
		$mask = $this->GetMask();
4843

    
4844
		$section->addInput(new Form_Select(
4845
			'mask',
4846
			'Mask',
4847
			$mask['type'],
4848
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4849
		))->setHelp('If "source" or "destination" slots is chosen a dynamic queue with the bandwidth, delay, packet loss ' .
4850
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4851
 					'This makes it possible to easily specify bandwidth limits per ' .
4852
 					'host or subnet, usually capped by the bandwidth of the parent ' .
4853
 					'limiter.');
4854

    
4855
		$group = new Form_Group(null);
4856

    
4857
		$group->add(new Form_Select(
4858
			'maskbits',
4859
			null,
4860
			$mask['bits'],
4861
			array_combine(range(32, 1, -1), range(32, 1, -1))
4862
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4863

    
4864
		$group->add(new Form_Select(
4865
			'maskbitsv6',
4866
			null,
4867
			$mask['bitsv6'],
4868
			array_combine(range(128, 1, -1), range(128, 1, -1))
4869
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4870

    
4871
		$section->add($group);
4872

    
4873
		$section->addInput(new Form_Input(
4874
			'description',
4875
			'Description',
4876
			'text',
4877
			$this->GetDescription()
4878
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4879

    
4880
		$sform->add($section);
4881

    
4882
		/* Begin limiter patch */
4883
		$aqm_map = getAQMs();
4884

    
4885
		$section = new Form_Section('Queue');
4886
		$section->addInput(new Form_Select(
4887
				'aqm',
4888
				'Queue Management Algorithm',
4889
				$this->GetAQM(),
4890
				array_map_assoc(function ($k, $v) {
4891
					return [$k, $v["name"]];
4892
				}, $aqm_map)
4893
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside this limiter\'s queue, ' .
4894
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4895
						'network congestion.');
4896

    
4897
		$section->addInput(new Form_StaticText(
4898
			'',
4899
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4900
		))->setHelp('Specifies the queue management algorithm parameters.');
4901

    
4902
		$section->addInput(new Form_Input(
4903
				'qlimit',
4904
				'Queue length',
4905
				'number',
4906
				$this->GetQlimit()
4907
		))->setHelp('Specifies the length of this queue, which the AQM is responsible for.  This field may be left empty.');
4908

    
4909
		$section->addInput(new Form_Checkbox(
4910
			'ecn',
4911
			'ECN',
4912
			'Enable Explicit Congestion Notification (ECN)',
4913
			($this->GetECN() == "on"),
4914
			'on'
4915
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4916

    
4917
		$sform->add($section);
4918
		/* End limiter patch */
4919

    
4920
		$section = new Form_Section('Advanced Options');
4921

    
4922
		$section->addInput(new Form_Input(
4923
			'weight',
4924
			'Weight',
4925
			'number',
4926
			$this->GetWeight(),
4927
			['min' => '1', 'max' => '100']
4928
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4929
					' it can be left blank otherwise.');
4930

    
4931
		$section->addInput(new Form_Input(
4932
			'plr',
4933
			'Packet Loss Rate',
4934
			'number',
4935
			$this->GetPlr(),
4936
			['step' => '0.001', 'min' => '0.000']
4937
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4938
					'A value of 0.001 means one packet in 1000 gets dropped');
4939

    
4940
		$section->addInput(new Form_Input(
4941
			'buckets',
4942
			'Bucket size (slots)',
4943
			'number',
4944
			$this->GetBuckets()
4945
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4946

    
4947
		$section->addInput(new Form_Input(
4948
			'pipe',
4949
			null,
4950
			'hidden',
4951
			$this->GetPipe()
4952
		));
4953

    
4954
		$sform->add($section);
4955

    
4956
		return($sform);
4957
	}
4958

    
4959
	function update_dn_data(&$data) {
4960
		$this->ReadConfig($data);
4961
	}
4962

    
4963
	function wconfig() {
4964
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4965
		if (!is_array($cflink)) {
4966
			$cflink = array();
4967
		}
4968
		$cflink['name'] = $this->GetQname();
4969
		$cflink['number'] = $this->GetNumber();
4970
		$cflink['qlimit'] = $this->GetQlimit();
4971
		$cflink['description'] = $this->GetDescription();
4972
		$cflink['weight'] = $this->GetWeight();
4973
		$cflink['enabled'] = $this->GetEnabled();
4974
		$cflink['buckets'] = $this->GetBuckets();
4975
		$mask = $this->GetMask();
4976
		$cflink['mask'] = $mask['type'];
4977
		$cflink['maskbits'] = $mask['bits'];
4978
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4979

    
4980
		/* Limiter queue patch */
4981
		$cflink['aqm'] = $this->GetAQM();
4982
		$aqm_map = GetAQMs();
4983
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4984
		foreach ($selectedParameters as $key => $value) {
4985
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4986
			$cflink[$config_key] = $this->GetAQMParameter($key);
4987
		}
4988
		$cflink['ecn'] = $this->GetECN();
4989
		/* End limiter queue patch */
4990
	}
4991
}
4992

    
4993
function get_dummynet_name_list() {
4994

    
4995
	$dn_name_list =& get_unique_dnqueue_list();
4996
	$dn_name = array();
4997
	if (is_array($dn_name_list)) {
4998
		foreach ($dn_name_list as $key => $value) {
4999
			$dn_name[] = $key;
5000
		}
5001
	}
5002

    
5003
	return $dn_name;
5004

    
5005
}
5006

    
5007
function get_altq_name_list() {
5008
	$altq_name_list =& get_unique_queue_list();
5009
	$altq_name = array();
5010
	if (is_array($altq_name_list)) {
5011
		foreach ($altq_name_list as $key => $aqobj) {
5012
			$altq_name[] = $key;
5013
		}
5014
	}
5015

    
5016
	return $altq_name;
5017
}
5018

    
5019
/*
5020
 * XXX: TODO Make a class shaper to hide all these functions
5021
 * from the global namespace.
5022
 */
5023

    
5024
/*
5025
 * This is a layer violation but for now there is no way
5026
 * I can find to properly do this with PHP.
5027
 */
5028
function altq_get_default_queue($interface) {
5029
	global $altq_list_queues;
5030

    
5031
	$altq_tmp = $altq_list_queues[$interface];
5032
	if ($altq_tmp) {
5033
		return $altq_tmp->GetDefaultQueuePresent();
5034
	} else {
5035
		return false;
5036
	}
5037
}
5038

    
5039
function altq_check_default_queues() {
5040
	global $altq_list_queues;
5041

    
5042
	$count = 0;
5043
	if (is_array($altq_list_queues)) {
5044
		foreach ($altq_list_queues as $altq) {
5045
			if ($altq->GetDefaultQueuePresent()) {
5046
				$count++;
5047
			}
5048
		}
5049
	}
5050
	else {
5051
		$count++;
5052
	}
5053

    
5054
	return 0;
5055
}
5056

    
5057
function &get_unique_queue_list() {
5058
	global $altq_list_queues;
5059

    
5060
	$qlist = array();
5061
	if (is_array($altq_list_queues)) {
5062
		foreach ($altq_list_queues as $altq) {
5063
			if ($altq->GetEnabled() == "") {
5064
				continue;
5065
			}
5066
			$tmplist =& $altq->get_queue_list();
5067
			foreach ($tmplist as $qname => $link) {
5068
				if ($link->GetEnabled() <> "") {
5069
					$qlist[$qname] = $link;
5070
				}
5071
			}
5072
		}
5073
	}
5074
	return $qlist;
5075
}
5076

    
5077
function &get_unique_dnqueue_list() {
5078
	global $dummynet_pipe_list;
5079

    
5080
	$qlist = array();
5081
	if (is_array($dummynet_pipe_list)) {
5082
		foreach ($dummynet_pipe_list as $dn) {
5083
			if ($dn->GetEnabled() == "") {
5084
				continue;
5085
			}
5086
			$tmplist =& $dn->get_queue_list();
5087
			foreach ($tmplist as $qname => $link) {
5088
				$qlist[$qname] = $link;
5089
			}
5090
		}
5091
	}
5092
	return $qlist;
5093
}
5094

    
5095
function ref_on_altq_queue_list($parent, $qname) {
5096
	if (isset($GLOBALS['queue_list'][$qname])) {
5097
		$GLOBALS['queue_list'][$qname]++;
5098
	} else {
5099
		$GLOBALS['queue_list'][$qname] = 1;
5100
	}
5101

    
5102
	unref_on_altq_queue_list($parent);
5103
}
5104

    
5105
function unref_on_altq_queue_list($qname) {
5106
	$GLOBALS['queue_list'][$qname]--;
5107
	if ($GLOBALS['queue_list'][$qname] <= 1) {
5108
		unset($GLOBALS['queue_list'][$qname]);
5109
	}
5110
}
5111

    
5112
function read_altq_config() {
5113
	global $altq_list_queues, $config;
5114
	$path = array();
5115

    
5116
	init_config_arr(array('shaper', 'queue'));
5117
	$a_int = &$config['shaper']['queue'];
5118

    
5119
	$altq_list_queues = array();
5120

    
5121
	if (!is_array($config['shaper']['queue'])) {
5122
		return;
5123
	}
5124

    
5125
	foreach ($a_int as $key => $conf) {
5126
		$int = $conf['interface'];
5127
		$root = new altq_root_queue();
5128
		$root->SetInterface($int);
5129
		$altq_list_queues[$root->GetInterface()] = $root;
5130
		$root->ReadConfig($conf);
5131
		array_push($path, $key);
5132
		$root->SetLink($path);
5133
		if (is_array($conf['queue'])) {
5134
			foreach ($conf['queue'] as $key1 => $q) {
5135
				array_push($path, $key1);
5136
				/*
5137
				 * XXX: we completely ignore errors here but anyway we must have
5138
				 *	checked them before so no harm should be come from this.
5139
				 */
5140
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
5141
				array_pop($path);
5142
			}
5143
		}
5144
		array_pop($path);
5145
	}
5146
}
5147

    
5148
function read_dummynet_config() {
5149
	global $dummynet_pipe_list, $config;
5150
	$path = array();
5151

    
5152
	init_config_arr(array('dnshaper', 'queue'));
5153
	$a_int = &$config['dnshaper']['queue'];
5154

    
5155
	$dummynet_pipe_list = array();
5156

    
5157
	if (!count($config['dnshaper']['queue'])) {
5158
		return;
5159
	}
5160

    
5161
	foreach ($a_int as $key => $conf) {
5162
		if (empty($conf['name'])) {
5163
			continue; /* XXX: grrrrrr at php */
5164
		}
5165
		$root = new dnpipe_class();
5166
		$root->ReadConfig($conf);
5167
		$dummynet_pipe_list[$root->GetQname()] = $root;
5168
		array_push($path, $key);
5169
		$root->SetLink($path);
5170
		if (is_array($conf['queue'])) {
5171
			foreach ($conf['queue'] as $key1 => $q) {
5172
				array_push($path, $key1);
5173
				/*
5174
				 * XXX: we completely ignore errors here but anyway we must have
5175
				 *	checked them before so no harm should be come from this.
5176
				 */
5177
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
5178
				array_pop($path);
5179
			}
5180
		}
5181
		array_pop($path);
5182
	}
5183
}
5184

    
5185
function get_interface_list_to_show() {
5186
	global $altq_list_queues, $config;
5187
	global $shaperIFlist;
5188

    
5189
	$tree = "";
5190
	foreach ($shaperIFlist as $shif => $shDescr) {
5191
		if ($altq_list_queues[$shif]) {
5192
			continue;
5193
		} else {
5194
			if (!is_altq_capable(get_real_interface($shif))) {
5195
				continue;
5196
			}
5197
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
5198
		}
5199
	}
5200

    
5201
	return $tree;
5202
}
5203

    
5204
function filter_generate_altq_queues() {
5205
	global $altq_list_queues;
5206

    
5207
	read_altq_config();
5208

    
5209
	$altq_rules = "";
5210
	foreach ($altq_list_queues as $altq) {
5211
		$altq_rules .= $altq->build_rules();
5212
	}
5213

    
5214
	return $altq_rules;
5215
}
5216

    
5217
function dnqueue_find_nextnumber() {
5218
	global $dummynet_pipe_list;
5219

    
5220
	$dnused = array();
5221
	if (is_array($dummynet_pipe_list)) {
5222
		foreach ($dummynet_pipe_list as $dn) {
5223
			$tmplist =& $dn->get_queue_list();
5224
			foreach ($tmplist as $qname => $link) {
5225
				if ($link[0] == "?") {
5226
					$dnused[$qname] = substr($link, 1);
5227
				}
5228
			}
5229
		}
5230
	}
5231

    
5232
	sort($dnused, SORT_NUMERIC);
5233
	$dnnumber = 0;
5234
	$found = false;
5235
	foreach ($dnused as $dnnum) {
5236
		if (($dnnum - $dnnumber) > 1) {
5237
			$dnnumber = $dnnum - 1;
5238
			$found = true;
5239
			break;
5240
		} else {
5241
			$dnnumber = $dnnum;
5242
		}
5243
	}
5244

    
5245
	if ($found == false) {
5246
		$dnnumber++;
5247
	}
5248

    
5249
	unset($dnused, $dnnum, $found);
5250
	return $dnnumber;
5251
}
5252

    
5253
function dnpipe_find_nextnumber() {
5254
	global $dummynet_pipe_list;
5255

    
5256
	$dnused = array();
5257
	foreach ($dummynet_pipe_list as $dn) {
5258
		$dnused[] = $dn->GetNumber();
5259
	}
5260

    
5261
	sort($dnused, SORT_NUMERIC);
5262
	$dnnumber = 0;
5263
	$found = false;
5264
	foreach ($dnused as $dnnum) {
5265
		if (($dnnum - $dnnumber) > 1) {
5266
			$dnnumber = $dnnum - 1;
5267
			$found = true;
5268
			break;
5269
		} else {
5270
			$dnnumber = $dnnum;
5271
		}
5272
	}
5273

    
5274
	if ($found == false) {
5275
		$dnnumber++;
5276
	}
5277

    
5278
	unset($dnused, $dnnum, $found);
5279
	return $dnnumber;
5280
}
5281

    
5282
function filter_generate_dummynet_rules() {
5283
	global $config, $g, $dummynet_pipe_list;
5284

    
5285
	read_dummynet_config();
5286

    
5287
	$dn_rules = "";
5288
	$max_qlimit = "100"; // OS default
5289
	foreach ($dummynet_pipe_list as $dn) {
5290
		$dn_rules .= $dn->build_rules();
5291
		$this_qlimit = $dn->GetQlimit();
5292
		if ($this_qlimit > $max_qlimit) {
5293
			$max_qlimit = $this_qlimit;
5294
		}
5295
	}
5296
	if (!is_numericint($max_qlimit)) {
5297
		$max_qlimit = "100";
5298
	}
5299
	if (!empty($dn_rules)) {
5300
		if (!is_module_loaded("dummynet.ko")) {
5301
			mwexec("/sbin/kldload dummynet");
5302
		}
5303
		$sysctls = (array(
5304
				"net.inet.ip.dummynet.io_fast" => "1",
5305
				"net.inet.ip.dummynet.hash_size" => "256",
5306
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
5307
		));
5308
		init_config_arr(array('sysctl', 'item'));
5309
		if (!empty($config['sysctl']['item'])) {
5310
			foreach ($config['sysctl']['item'] as $item) {
5311
				if (preg_match('/net\.inet\.ip\.dummynet\./', $item['tunable'])) {
5312
					$sysctls[$item['tunable']] = $item['value'];
5313
				}
5314
			}
5315
		}
5316
		set_sysctl($sysctls);
5317
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
5318
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
5319
	}
5320
}
5321

    
5322
function build_iface_without_this_queue($iface, $qname) {
5323
	global $g, $altq_list_queues;
5324
	global $shaperIFlist;
5325

    
5326
	$altq =& $altq_list_queues[$iface];
5327

    
5328
	if ($altq) {
5329
		$scheduler = $altq->GetScheduler();
5330
	}
5331

    
5332
	$form = '<dl class="dl-horizontal">';
5333

    
5334
	$form .= '	<dt>';
5335
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5336
	$form .= '	</dt>';
5337
	$form .= '	<dd>';
5338
	$form .=		$scheduler;
5339
	$form .= '	</dd>';
5340

    
5341
	$form .= '	<dt>';
5342
	$form .= 'Clone';
5343
	$form .= '	</dt>';
5344
	$form .= '	<dd>';
5345
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
5346
	$form .= $iface . '&amp;queue=';
5347
	$form .= $qname . '&amp;action=add">';
5348
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
5349
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
5350
	$form .= '	</dd>';
5351

    
5352
	$form .= '</dl>';
5353

    
5354
	return $form;
5355

    
5356
}
5357

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

    
5361
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5362
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5363
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5364

    
5365
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5366
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5367
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5368

    
5369
// Check to see if the specified interface has a queue configured
5370
function interface_has_queue($if) {
5371
	global $config;
5372

    
5373
	if ($config['shaper']) {
5374
		foreach ($config['shaper']['queue'] as $queue) {
5375
			if ($queue['interface'] === $if) {
5376
				return true;
5377
			}
5378
		}
5379
	}
5380

    
5381
	return false;
5382
}
5383
?>
(47-47/61)