Project

General

Profile

Download (148 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-2022 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 quantum %s limit %s flows %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 step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.alpha")) / 1000),
102
				"beta" => array("name" => "beta", "type" => "number step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.beta")) / 1000),
103
				"max_burst" => array("name" => "max_burst", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.max_burst")) / 1000),
104
				"max_ecnth" => array("name" => "max_ecnth", "type" => "number step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.max_ecnth")) / 1000),
105
				"quantum" => array("name" => "quantum", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.quantum")),
106
				"limit" => array("name" => "limit", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.limit")),
107
				"flows" => array("name" => "flows", "type" => "number", "default" => get_single_sysctl("net.inet.ip.dummynet.fqpie.flows"))
108
			),
109
			"ecn" => true,
110
			"pie_onoff" => true,
111
			"pie_capdrop" => true,
112
			"pie_qdelay" => true,
113
			"pie_pderand" => true
114
		)
115
	);
116
}
117
// list of AQMs that dummynet supports
118
function getAQMs() {
119
	return array(
120
		"droptail" => array(
121
			"name" => "Tail Drop",
122
			"description" => "Tail Drop is a fundamental queue management algorithm which drops inbound packets once the queue is full.",
123
			"parameter_format" => "droptail",
124
			"parameters" => array(),
125
			"ecn" => false
126
		),
127
		"codel" => array(
128
			"name" => "CoDel",
129
			"description" => "CoDel is a novel \"no knobs\", \"just works\", \"handles variable bandwidth and RTT\", and simple AQM algorithm.",
130
			"parameter_format" => "codel target %sms interval %sms",
131
			"parameters" => array(
132
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.codel.target")) / 1000),
133
				"interval" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.codel.interval")) / 1000),
134
			),
135
			"ecn" => true
136
		),
137
		"pie" => array(
138
			"name" => "PIE",
139
			"description" => "Proportional Integral controller Enhanced AQM (PIE) is similar to CoDel but uses a slightly different algorithm.",
140
			"parameter_format" => "pie target %sms tupdate %sms alpha %s beta %s max_burst %s max_ecnth %s",
141
			"parameters" => array(
142
				"target" => array("name" => "Target Delay (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.target")) / 1000),
143
				"tupdate" => array("name" => "Interval (ms)", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.tupdate")) / 1000),
144
				"alpha" => array("name" => "alpha", "type" => "number step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.alpha")) / 1000),
145
				"beta" => array("name" => "beta", "type" => "number step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.beta")) / 1000),
146
				"max_burst" => array("name" => "max_burst", "type" => "number", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.max_burst")) / 1000),
147
				"max_ecnth" => array("name" => "max_ecnth", "type" => "number step=any", "default" => intval(get_single_sysctl("net.inet.ip.dummynet.fqpie.max_ecnth")) / 1000)
148
			),
149
			"ecn" => true,
150
			"pie_onoff" => true,
151
			"pie_capdrop" => true,
152
			"pie_qdelay" => true,
153
			"pie_pderand" => true
154
		),
155
		"red" => array(
156
			"name" => "Random Early Detection (RED)",
157
			"description" => "Random Early Detection (RED) drops packets based on probability, which increases as the queue increases in size.",
158
			"parameter_format" => "red %s/%s/%s/%s",
159
			"parameters" => array(
160
				"w_q" => array("name" => "w_q", "type" => "number", "default" => 1),
161
				"min_th" => array("name" => "min_th", "type" => "number", "default" => 0),
162
				"max_th" => array("name" => "max_th", "type" => "number", "default" => 1),
163
				"max_p" => array("name" => "max_p", "type" => "number", "default" => 1)
164
			),
165
			"ecn" => true
166
		),
167
		"gred" => array(
168
			"name" => "Gentle Random Early Detection (GRED)",
169
			"description" => "Gentle Random Early Detection (GRED) drops packets based on probability, which increases as the queue increases in size.",
170
			"parameter_format" => "gred %s/%s/%s/%s",
171
			"parameters" => array(
172
				"w_q" => array("name" => "w_q", "type" => "number", "default" => 1),
173
				"min_th" => array("name" => "min_th", "type" => "number", "default" => 0),
174
				"max_th" => array("name" => "max_th", "type" => "number", "default" => 1),
175
				"max_p" => array("name" => "max_p", "type" => "number", "default" => 1)
176
			),
177
			"ecn" => true
178
		)
179
	);
180
}
181
// used to map above
182
function array_map_assoc(callable $f, array $a) {
183
	return array_column(array_map($f, array_keys($a), $a), 1, 0);
184
}
185
function build_queue_params($array, $selected, $params, $id) {
186
	$form .= '<div id="params_' . $id . '">';
187

    
188
	$selectedAlgorithm = $array[$selected];
189

    
190
	if ($selectedAlgorithm) {
191
		$form .= '<span class="help-block">' . gettext($selectedAlgorithm['description']) . '</span><br/>';
192
	}
193

    
194
	$parameters = $selectedAlgorithm["parameters"];
195

    
196
	if (!$parameters || sizeof($parameters) <= 0) {
197
		if (!$selectedAlgorithm) {
198
			$form .= 'No parameters for selected algorithm.';
199
		} else {
200
			$form .= 'No parameters for ' . $selectedAlgorithm["name"] . '.';
201
		}
202
	} else {
203
		$form .= '<div class="table-responsive">';
204
		$form .= '<table id="maintable" class="table table-hover table-striped">';
205
		$form .= "<thead><tr>";
206
		$form .= "<th>Parameter</th>";
207
		$form .= "<th>Value</th>";
208
		$form .= "</tr></thead>";
209
		$form .= "<tbody>";
210

    
211
		foreach ($parameters as $key => $value) {
212
			$form .= '<tr>';
213

    
214
			// get current value, or default.
215
			$currentValue = $params[$key];
216
			// check if param key is set. using isset allows the param key to be zero.
217
			if (! isset($currentValue)) {
218
				$currentValue = $value["default"]; // default to default
219
			}
220

    
221
			$form .= "<td class=\"col-xs-4\">" . htmlspecialchars($key) . "</td>";
222
			$form .= "<td class=\"col-xs-8\"><input class=\"form-control\" type=" . gettext($value["type"])
223
					. " name=\"param_" . $selected . "_" . $key . "\" placeholder=\"" .
224
					htmlspecialchars($value["default"]) . "\" value=\"" . htmlspecialchars($currentValue) . "\"/></td>";
225

    
226
			$form .= '</tr>';
227
		}
228

    
229
		$form .= "</tbody></table></div><br />";
230
	}
231

    
232
	$form .= '</div>';
233
	$form .= '<script type="text/javascript">';
234
	$form .= 'events.push(function() {$("#' . $id . '").change(function() {$("#params_' .
235
		gettext($id) . '").html("Save this limiter to see algorithm parameters.");})});</script>';
236

    
237
	return($form);
238
}
239
function FormatParameters($format_string, $params) {
240
	return vsprintf($format_string, $params);
241
}
242
/* End limiter patch */
243

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

    
271
			if ($items[$level+3] == "bandwidth") {
272
				$level += 2;
273
			}
274
			if ($items[$level+3] == "priority") {
275
				$level += 2;
276
			}
277
			if ($items[$level+3] == "qlimit") {
278
				$level += 2;
279
			}
280
			$tmp = $items[$level+3];
281
			if (substr($tmp,strlen($tmp)-1) == "(") {
282
				$newqueue['shapertype'] = substr($tmp, 0, strlen($tmp)-1);
283
			}
284

    
285
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
286
			if (is_array($contains)) {
287
				$newqueue['contains'] = $contains;
288
			}
289
		} elseif ($items[0] == "altq") {
290
			unset($newqueue);
291
			$newqueue = array();
292
			$currentqueuename = convert_real_interface_to_friendly_interface_name($items[2]);
293
			$currentqueueif = $items[2];
294
			$newqueue['name'] = $currentqueuename;
295
			$newqueue['interface'] = $items[2];
296
			$interfacestats[$currentqueueif][$currentqueuename] = &$newqueue;
297
		} else {
298
			if ($items[3] == "pkts:") {
299
				preg_match('/pkts:\s+(\d+)\s+bytes:\s+(\d+)\s+dropped pkts:\s+(\d+)\s+bytes:\s+(\d+)/', $line, $matches);
300
				$newqueue["pkts"] = $matches[1];
301
				$newqueue["bytes"] = $matches[2];
302
				$newqueue["droppedpkts"] = $matches[3];
303
				$newqueue["droppedbytes"] = $matches[4];
304
			}
305
			if ($items[3] == "qlength:") {
306
				$subitems = explode("/", substr($line, 13, 8));
307
				$newqueue["qlengthitems"] = trim($subitems[0]);
308
				$newqueue["qlengthsize"] = trim($subitems[1]);
309
				if (substr($line, 22, 8) == "borrows:") {
310
					$newqueue["borrows"] = trim(substr($line, 31, 7));
311
				}
312
				if (substr($line, 39, 9) == "suspends:") {
313
					$newqueue["suspends"] = trim(substr($line, 49, 7));
314
				}
315
			}
316
		}
317
	}
318
	$result['interfacestats'] = $interfacestats;
319
	return $result;
320
}
321

    
322
/*
323
 * I admit :) this is derived from xmlparse.inc StartElement()
324
 */
325
function &get_reference_to_me_in_config(&$mypath) {
326
	global $config;
327

    
328
	init_config_arr(array('shaper'));
329
	$ptr = &$config['shaper'];
330
	foreach ($mypath as $indeks) {
331
		if (!is_array($ptr)) {
332
			$ptr = array();
333
		}
334
		if (!is_array($ptr['queue'])) {
335
			$ptr['queue'] = array();
336
		}
337
		if (!is_array($ptr['queue'][$indeks])) {
338
			$ptr['queue'][$indeks] = array();
339
		}
340
		$ptr = &$ptr['queue'][$indeks];
341
	}
342

    
343
	return $ptr;
344
}
345

    
346
function unset_object_by_reference(&$mypath) {
347
	global $config;
348

    
349
	init_config_arr(array('shaper'));
350
	$ptr = &$config['shaper'];
351
	for ($i = 0; $i < count($mypath) - 1; $i++) {
352
		$ptr = &$ptr['queue'][$mypath[$i]];
353
	}
354
	unset($ptr['queue'][$mypath[$i]]);
355
}
356

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

    
360
	init_config_arr(array('dnshaper'));
361
	$ptr = &$config['dnshaper'];
362
	foreach ($mypath as $indeks) {
363
		$ptr = &$ptr['queue'][$indeks];
364
	}
365

    
366
	return $ptr;
367
}
368

    
369
function unset_dn_object_by_reference(&$mypath) {
370
	global $config;
371

    
372
	init_config_arr(array('dnshaper'));
373
	$ptr = &$config['dnshaper'];
374
	for ($i = 0; $i < count($mypath) - 1; $i++) {
375
		$ptr = &$ptr['queue'][$mypath[$i]];
376
	}
377
	unset($ptr['queue'][$mypath[$i]]);
378
}
379

    
380
function clean_child_queues($type, $mypath) {
381
	$ref = &get_reference_to_me_in_config($mypath);
382

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

    
458
function get_bandwidthtype_scale($type) {
459
	switch ($type) {
460
		case "Gb":
461
			$factor = 1024 * 1024 * 1024;
462
			break;
463
		case "Mb":
464
			$factor = 1024 * 1024;
465
			break;
466
		case "Kb":
467
			$factor = 1024;
468
			break;
469
		case "b":
470
		default:
471
			$factor = 1;
472
			break;
473
	}
474
	return intval($factor);
475
}
476

    
477
function get_bandwidth($bw, $scale, $obj) {
478
	$bw = (int) $bw;
479
	$pattern= "/(b|Kb|Mb|Gb|%)/";
480
	if (!preg_match($pattern, $scale, $match))
481
		return 0;
482

    
483
	switch ($match[1]) {
484
		case '%':
485
			$objbw = ($bw / 100) * get_queue_bandwidth($obj);
486
			break;
487
		default:
488
			$objbw = $bw * get_bandwidthtype_scale($scale);
489
			break;
490
	}
491

    
492
	return floatval($objbw);
493
}
494

    
495
/*
496
 * XXX - unused
497
 *
498
function get_hfsc_bandwidth($object, $bw) {
499
	$pattern= "/[0-9]+/";
500
	if (preg_match($pattern, $bw, $match)) {
501
		$bw_1 = $match[1];
502
	} else {
503
		return 0;
504
	}
505
	$pattern= "/(b|Kb|Mb|Gb|%)/";
506
	if (preg_match($pattern, $bw, $match)) {
507
		switch ($match[1]) {
508
			case '%':
509
				$bw_1 = ($bw_1 / 100) * get_interface_bandwidth($object);
510
				break;
511
			default:
512
				$bw_1 = $bw_1 * get_bandwidthtype_scale($match[0]);
513
				break;
514
		}
515
		return floatval($bw_1);
516
	} else {
517
		return 0;
518
	}
519
}
520
*/
521

    
522
function get_queue_bandwidth($obj) {
523
	$bw = $obj->GetBandwidth();
524
	$scale = $obj->GetBwscale();
525

    
526
	$pattern= "/(b|Kb|Mb|Gb|%)/";
527
	if (!preg_match($pattern, $scale, $match))
528
		return 0;
529

    
530
	switch ($match[1]) {
531
		case '%':
532
			if (method_exists($obj, 'GetParent')) {
533
				$getobjbw = get_queue_bandwidth($obj->GetParent());
534
			} else {
535
				$getobjbw = $obj->bandwidth;
536
			}
537
			$objbw = ($bw / 100) * $getobjbw;
538
			break;
539
		default:
540
			$objbw = $bw * get_bandwidthtype_scale($scale);
541
			break;
542
	}
543

    
544
	return floatval($objbw);
545
}
546

    
547
function get_interface_bandwidth($object) {
548
	global $altq_list_queues;
549

    
550
	$int = $object->GetInterface();
551
	if (isset($altq_list_queues[$int])) {
552
		$altq = &$altq_list_queues[$int];
553
		$bw_3 = $altq->GetBandwidth();
554
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
555
		return floatval($bw_3);
556
	} else {
557
		return 0;
558
	}
559
}
560

    
561
/*
562
 * This is duplicated here since we cannot include guiconfig.inc.
563
 * Including it makes all stuff break.
564
 */
565
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
566

    
567
	/* check for bad control characters */
568
	foreach ($postdata as $pn => $pd) {
569
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
570
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
571
		}
572
	}
573

    
574
	for ($i = 0; $i < count($reqdfields); $i++) {
575
		if ($postdata[$reqdfields[$i]] == "") {
576
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
577
		}
578
	}
579
}
580

    
581
function cleanup_queue_from_rules($queue) {
582
	global $config;
583

    
584
	foreach ($config['filter']['rule'] as & $rule) {
585
		if ($rule['defaultqueue'] == $queue) {
586
			unset($rule['defaultqueue']);
587
			if (isset($rule['ackqueue'])) {
588
				unset($rule['ackqueue']);
589
			}
590
		}
591
		if ($rule['ackqueue'] == $queue) {
592
			unset($rule['ackqueue']);
593
		}
594
	}
595
}
596

    
597
function cleanup_dnqueue_from_rules($queue) {
598
	global $config;
599

    
600
	foreach ($config['filter']['rule'] as & $rule) {
601
		if ($rule['dnpipe'] == $queue) {
602
			unset($rule['dnpipe']);
603
		}
604
		if ($rule['pdnpipe'] == $queue) {
605
			unset($rule['pdnpipe']);
606
		}
607
	}
608
}
609

    
610
function rename_queue_in_rules($name, $newname) {
611
	global $config;
612

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

    
623
function rename_dnqueue_in_rules($name, $newname) {
624
	global $config;
625

    
626
	foreach ($config['filter']['rule'] as & $rule) {
627
		if ($rule['dnpipe'] == $name) {
628
			$rule['dnpipe'] = $newname;
629
		}
630
		if ($rule['pdnpipe'] == $name) {
631
			$rule['pdnpipe'] = $newname;
632
		}
633
	}
634
}
635

    
636
class altq_root_queue {
637
	var $interface;
638
	var $tbrconfig ;
639
	var $bandwidth;
640
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
641
	var $scheduler;
642
	var $qlimit;
643
	var $queues = array();
644
	var $qenabled = false;
645
	var $link;
646

    
647
	/* Accessor functions */
648
	function GetDefaultQueuePresent() {
649
		if (!empty($this->queues)) {
650
			foreach ($this->queues as $q) {
651
				if ($q->GetDefault()) {
652
					return true;
653
				}
654
			}
655
		}
656

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

    
732
	function GetBwscaleText() {
733
		switch ($this->bandwidthtype) {
734
			case "b":
735
				$bwscaletext = "Bit/s";
736
				break;
737
			case "Kb":
738
				$bwscaletext = "Kbit/s";
739
				break;
740
			case "Mb":
741
				$bwscaletext = "Mbit/s";
742
				break;
743
			case "Gb":
744
				$bwscaletext = "Gbit/s";
745
				break;
746
			default:
747
				/* For others that do not need translating like % */
748
				$bwscaletext = $this->bandwidthtype;
749
				break;
750
		}
751
		return $bwscaletext;
752
	}
753

    
754
	function CheckBandwidth($bw, $bwtype) {
755
		$sum = $this->GetTotalBw();
756
		if ($sum > ($bw * get_bandwidthtype_scale($bwtype))) {
757
			return 1;
758
		}
759
		foreach ($this->queues as $q) {
760
			if ($q->CheckBandwidth(0, '')) {
761
				return 1;
762
			}
763
		}
764

    
765
		return 0;
766
	}
767

    
768
	function GetTotalBw($qignore = NULL) {
769
		$sum = 0;
770
		foreach ($this->queues as $q) {
771
			if (($qignore != NULL) && ($qignore == $q)) {
772
				continue;
773
			}
774
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
775
		}
776

    
777
		return $sum;
778
	}
779

    
780
	function validate_input($data, &$input_errors) {
781

    
782
		$reqdfields[] = "bandwidth";
783
		$reqdfieldsn[] = gettext("Bandwidth");
784
		$reqdfields[] = "bandwidthtype";
785
		$reqdfieldsn[] = gettext("Bandwidthtype");
786

    
787
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
788

    
789
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
790
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
791
		}
792
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
793
			$input_errors[] = gettext("Bandwidth must be an integer.");
794
		}
795
		if ($data['bandwidth'] < 0) {
796
			$input_errors[] = gettext("Bandwidth cannot be negative.");
797
		}
798
		if ($data['bandwidthtype'] == "%") {
799
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
800
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
801
			}
802
		}
803
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype'])) {
804
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
805
		}
806
		if ($data['qlimit'] && ($data['scheduler'] == 'CODELQ')) {
807
			$input_errors[] = gettext("CODELQ scheduler doesn't support Qlimit parameter.");
808
		}
809
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
810
			$input_errors[] = gettext("Qlimit must be an integer.");
811
		}
812
		if ($data['qlimit'] < 0) {
813
			$input_errors[] = gettext("Qlimit must be positive.");
814
		}
815
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
816
			$input_errors[] = gettext("Tbrsize must be an integer.");
817
		}
818
		if ($data['tbrconfig'] < 0) {
819
			$input_errors[] = gettext("Tbrsize must be positive.");
820
		}
821
	}
822

    
823
	/* Implement this to shorten some code on the frontend page */
824
	function ReadConfig(&$conf) {
825
		if (isset($conf['tbrconfig'])) {
826
			$this->SetTbrConfig($conf['tbrconfig']);
827
		} else {
828
			$this->SetTbrConfig($conf['tbrconfig']);
829
		}
830
		$this->SetBandwidth($conf['bandwidth']);
831
		if ($conf['bandwidthtype'] <> "") {
832
			$this->SetBwscale($conf['bandwidthtype']);
833
		}
834
		if (isset($conf['scheduler'])) {
835
			if ($this->GetScheduler() != $conf['scheduler']) {
836
				foreach ($this->queues as $q) {
837
					clean_child_queues($conf['scheduler'], $this->GetLink());
838
					$q->clean_queue($conf['scheduler']);
839
				}
840
			}
841
			$this->SetScheduler($conf['scheduler']);
842
		}
843
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
844
			$this->SetQlimit($conf['qlimit']);
845
		} else {
846
			$this->SetQlimit("");
847
		}
848
		if (isset($conf['name'])) {
849
			$this->SetQname($conf['name']);
850
		}
851
		if (!empty($conf['enabled'])) {
852
			$this->SetEnabled($conf['enabled']);
853
		} else {
854
			$this->SetEnabled("");
855
		}
856
	}
857

    
858
	function copy_queue($interface, &$cflink) {
859
		$cflink['interface'] = $interface;
860
		$cflink['name'] = $interface;
861
		$cflink['scheduler'] = $this->GetScheduler();
862
		$cflink['bandwidth'] = $this->GetBandwidth();
863
		$cflink['bandwidthtype'] = $this->GetBwscale();
864
		$cflink['qlimit'] = $this->GetQlimit();
865
		$cflink['tbrconfig'] = $this->GetTbrConfig();
866
		$cflink['enabled'] = $this->GetEnabled();
867
		if (is_array($this->queues)) {
868
			$cflink['queue'] = array();
869
			foreach ($this->queues as $q) {
870
				$cflink['queue'][$q->GetQname()] = array();
871
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
872
			}
873
		}
874
	}
875

    
876
	function &get_queue_list(&$q = null) {
877
		$qlist = array();
878

    
879
		//$qlist[$this->GetQname()] = & $this;
880
		if (is_array($this->queues)) {
881
			foreach ($this->queues as $queue) {
882
				$queue->get_queue_list($qlist);
883
			}
884
		}
885
		return $qlist;
886
	}
887

    
888
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
889

    
890
		if (!is_array($this->queues)) {
891
			$this->queues = array();
892
		}
893

    
894
		switch ($this->GetScheduler()) {
895
			case "PRIQ":
896
				$__tmp_q = new priq_queue(); $q =& $__tmp_q;
897
				break;
898
			case "HFSC":
899
				$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
900
				break;
901
			case "CBQ":
902
				$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
903
				break;
904
			case "FAIRQ":
905
				$__tmp_q = new fairq_queue(); $q =& $__tmp_q;
906
				break;
907
			default:
908
				/* XXX: but should not happen anyway */
909
				return;
910
				break;
911
		}
912
		$q->SetLink($path);
913
		$q->SetInterface($this->GetInterface());
914
		$q->SetEnabled("on");
915
		$q->SetParent($this);
916
		$q->ReadConfig($queue);
917
		$q->validate_input($queue, $input_errors);
918

    
919
		$this->queues[$q->GetQname()] = &$q;
920
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
921
		if (is_array($queue['queue'])) {
922
			foreach ($queue['queue'] as $key1 => $que) {
923
				array_push($path, $key1);
924
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
925
				array_pop($path);
926
			}
927
		}
928

    
929
		return $q;
930
	}
931

    
932
	/* interface here might be optional */
933
	function &find_queue($interface, $qname) {
934
		if ($qname == $this->GetQname()) {
935
			return $this;
936
		}
937
		foreach ($this->queues as $q) {
938
			$result =& $q->find_queue("", $qname);
939
			if ($result) {
940
				return $result;
941
			}
942
		}
943
	}
944

    
945
	function &find_parentqueue($interface, $qname) {
946
		if ($qname == $interface) {
947
			$result = NULL;
948
		} else if ($this->queues[$qname]) {
949
			$result = $this;
950
		} else if ($this->GetScheduler() <> "PRIQ") {
951
			foreach ($this->queues as $q) {
952
				$result = $q->find_parentqueue("", $qname);
953
				if ($result) {
954
					return $result;
955
				}
956
			}
957
		}
958
	}
959

    
960
	function build_tree() {
961
		global $shaperIFlist;
962

    
963
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
964
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
965
		if (is_array($this->queues)) {
966
			$tree .= "<ul>";
967
			foreach ($this->queues as $q) {
968
				$tree .= $q->build_tree();
969
			}
970
			$tree .= "</ul>";
971
		}
972
		$tree .= "</li>";
973
		return $tree;
974
	}
975

    
976
	function delete_queue() {
977
		foreach ($this->queues as $q) {
978
			$q->delete_queue();
979
		}
980
		unset_object_by_reference($this->GetLink());
981
	}
982

    
983
	function delete_all() {
984
		if (count($this->queues)) {
985
			foreach ($this->queues as $q) {
986
				$q->delete_all();
987
				unset_object_by_reference($q->GetLink());
988
				unset($q);
989
			}
990
			unset($this->queues);
991
		}
992
	}
993

    
994
	/*
995
	 * First it spits:
996
	 * altq on $interface ..............
997
	 *	then it goes like
998
	 *	foreach ($queues as $qkey => $queue) {
999
	 *		this->queues[$qkey]->build_rule();
1000
	 *	}
1001
	 */
1002
	function build_rules(&$default = false) {
1003
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
1004
			$default = false;
1005
			$rules = " altq on " . get_real_interface($this->GetInterface());
1006
			if ($this->GetScheduler()) {
1007
				$rules .= " ".strtolower($this->GetScheduler());
1008
			}
1009
			if ($this->GetQlimit() > 0) {
1010
				$rules .= " qlimit " . $this->GetQlimit() . " ";
1011
			}
1012
			if ($this->GetBandwidth()) {
1013
				$rules .= " bandwidth ".trim($this->GetBandwidth());
1014
				if ($this->GetBwscale()) {
1015
					$rules .= $this->GetBwscale();
1016
				}
1017
			}
1018
			if ($this->GetTbrConfig()) {
1019
				$rules .= " tbrsize ".$this->GetTbrConfig();
1020
			}
1021
			if (count($this->queues)) {
1022
				$i = count($this->queues);
1023
				$rules .= " queue { ";
1024
				foreach ($this->queues as $qkey => $qnone) {
1025
					if ($i > 1) {
1026
						$i--;
1027
						$rules .= " {$qkey}, ";
1028
					} else {
1029
						$rules .= " {$qkey} ";
1030
					}
1031
				}
1032
				$rules .= " } \n";
1033
				foreach ($this->queues as $q) {
1034
					$rules .= $q->build_rules($default);
1035
				}
1036
			}
1037

    
1038
			if ($default == false) {
1039
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
1040
				file_notice("Shaper", $error, "Error occurred", "");
1041
				unset($error);
1042
				return "\n";
1043
			}
1044
			$frule .= $rules;
1045
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
1046
			$rules = " altq on " . get_real_interface($this->GetInterface());
1047
			if ($this->GetScheduler()) {
1048
				$rules .= " ".strtolower($this->GetScheduler());
1049
			}
1050
			if ($this->GetQlimit() > 0) {
1051
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
1052
			}
1053
			if ($this->GetBandwidth()) {
1054
				$rules .= " bandwidth ".trim($this->GetBandwidth());
1055
				if ($this->GetBwscale()) {
1056
					$rules .= $this->GetBwscale();
1057
				}
1058
			}
1059
			if ($this->GetTbrConfig()) {
1060
				$rules .= " tbrsize ".$this->GetTbrConfig();
1061
			}
1062

    
1063
			$rules .= " queue";
1064
		}
1065

    
1066
		$rules .= " \n";
1067
		return $rules;
1068
	}
1069

    
1070
	function build_javascript() {
1071
		$javascript = "<script type=\"text/javascript\">";
1072
		$javascript .= "//<![CDATA[\n";
1073
		$javascript .= "function mySuspend() {";
1074
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1075
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
1076
		$javascript .= "else if (document.all)";
1077
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
1078
		$javascript .= "}";
1079

    
1080
		$javascript .= "function myResume() {";
1081
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1082
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
1083
		$javascript .= "else if (document.all) ";
1084
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
1085
		$javascript .= "}";
1086
		$javascript .= "//]]>";
1087
		$javascript .= "</script>";
1088

    
1089
		return $javascript;
1090
	}
1091

    
1092
	function build_shortform() {
1093
		global $g;
1094

    
1095
		$altq =& $this;
1096

    
1097
		if ($altq) {
1098
			$scheduler = ": " . $altq->GetScheduler();
1099
		}
1100

    
1101
		$form = '<dl class="dl-horizontal">';
1102
		$form .= '	<dt>';
1103
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1104
		$form .= '	</dt>';
1105
		$form .= '	<dd>';
1106
		$form .=		$scheduler;
1107
		$form .= '	</dd>';
1108

    
1109
		$form .= '	<dt>';
1110
		$form .=		'Bandwidth';
1111
		$form .= '	</dt>';
1112
		$form .= '	<dd>';
1113
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1114
		$form .= '	</dd>';
1115

    
1116
		$form .= '	<dt>';
1117
		$form .= 'Disable';
1118
		$form .= '	<dt>';
1119
		$form .= '	<dd>';
1120

    
1121
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1122
		$form .= $this->GetInterface() . '&amp;queue=';
1123
		$form .= $this->GetQname() . '&amp;action=delete">';
1124
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1125
		$form .= gettext("Remove shaper from this interface") . '</a>';
1126

    
1127
		$form .= '	</dd>';
1128

    
1129
		$form .= '</dl>';
1130

    
1131
		return $form;
1132

    
1133
	}
1134

    
1135
	/*
1136
	 * For requesting the parameters of the root queues
1137
	 * to the user like the traffic wizard does.
1138
	 */
1139
	function build_form() {
1140

    
1141
		$sform = new Form();
1142

    
1143
		$sform->setAction("firewall_shaper.php");
1144

    
1145
		$section = new Form_Section(null);
1146

    
1147
		$section->addInput(new Form_Checkbox(
1148
			'enabled',
1149
			'Enable/Disable',
1150
			'Enable/disable discipline and its children',
1151
			($this->GetEnabled() == "on"),
1152
			'on'
1153
		));
1154

    
1155
		$section->addInput(new Form_StaticText(
1156
			'*Name',
1157
			$this->GetQname()
1158
		));
1159

    
1160
		$section->addInput(new Form_Select(
1161
			'scheduler',
1162
			'Scheduler Type',
1163
			$this->GetScheduler(),
1164
			array('HFSC' => 'HFSC',
1165
				  'CBQ' => 'CBQ',
1166
				  'FAIRQ' => 'FAIRQ',
1167
				  'CODELQ' => 'CODELQ',
1168
				  'PRIQ' => 'PRIQ')
1169
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
1170

    
1171
		$group = new Form_group('Bandwidth');
1172

    
1173
		$group->add(new Form_Input(
1174
			'bandwidth',
1175
			null,
1176
			'number',
1177
			$this->GetBandwidth()
1178
		));
1179

    
1180
		$group->add(new Form_Select(
1181
			'bandwidthtype',
1182
			null,
1183
			$this->FormGetBwscale(),
1184
			array('Kb' => 'Kbit/s',
1185
				  'Mb' => 'Mbit/s',
1186
				  'Gb' => 'Gbit/s',
1187
				  'b' => 'Bit/s',
1188
				  '%' => '%')
1189
		));
1190

    
1191
		$section->add($group);
1192

    
1193
		$section->addInput(new Form_Input(
1194
			'qlimit',
1195
			'Queue Limit',
1196
			'number',
1197
			$this->GetQlimit()
1198
		));
1199

    
1200
		$section->addInput(new Form_Input(
1201
			'tbrconfig',
1202
			'TBR Size',
1203
			'number',
1204
			$this->GetTbrConfig()
1205
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
1206
					'bandwidth are used to determine the size.');
1207

    
1208
		$section->addInput(new Form_Input(
1209
			'interface',
1210
			null,
1211
			'hidden',
1212
			$this->GetInterface()
1213
		));
1214

    
1215
		$section->addInput(new Form_Input(
1216
			'name',
1217
			null,
1218
			'hidden',
1219
			$this->GetQname()
1220
		));
1221

    
1222
		$sform->add($section);
1223

    
1224
		return($sform);
1225
	}
1226

    
1227
	function update_altq_queue_data(&$data) {
1228
		$this->ReadConfig($data);
1229
	}
1230

    
1231
	/*
1232
	 * Should call on each of it queues and subqueues
1233
	 * the same function much like build_rules();
1234
	 */
1235
	function wconfig() {
1236
		$cflink = &get_reference_to_me_in_config($this->GetLink());
1237
		if (!is_array($cflink)) {
1238
			$cflink = array();
1239
		}
1240
		$cflink['interface'] = $this->GetInterface();
1241
		$cflink['name'] = $this->GetQname();
1242
		$cflink['scheduler'] = $this->GetScheduler();
1243
		$cflink['bandwidth'] = $this->GetBandwidth();
1244
		$cflink['bandwidthtype'] = $this->GetBwscale();
1245
		$cflink['qlimit'] = trim($this->GetQlimit());
1246
		if (empty($cflink['qlimit'])) {
1247
			unset($cflink['qlimit']);
1248
		}
1249
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
1250
		if (empty($cflink['tbrconfig'])) {
1251
			unset($cflink['tbrconfig']);
1252
		}
1253
		$cflink['enabled'] = $this->GetEnabled();
1254
		if (empty($cflink['enabled'])) {
1255
			unset($cflink['enabled']);
1256
		}
1257
	}
1258

    
1259
}
1260

    
1261
class priq_queue {
1262
	var $qname;
1263
	var $qinterface;
1264
	var $qlimit;
1265
	var $qpriority;
1266
	var $description;
1267
	var $isparent;
1268
	var $qbandwidth;
1269
	var $qbandwidthtype;
1270
	var $qdefault = "";
1271
	var $qrio = "";
1272
	var $qred = "";
1273
	var $qcodel = "";
1274
	var $qecn = "";
1275
	var $qack;
1276
	var $qenabled = "";
1277
	var $qparent;
1278
	var $link;
1279

    
1280
	/* This is here to help with form building and building rules/lists */
1281
	var $subqueues = array();
1282

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

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

    
1362
		if ($bwtype != "%") {
1363
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1364

    
1365
			if ($bw > 0) {
1366
				$sum += get_bandwidth($bw, $bwtype, $parent);
1367
			}
1368

    
1369
			if ($sum > get_queue_bandwidth($parent)) {
1370
				return 1;
1371
			}
1372
		}
1373

    
1374
		foreach ($this->subqueues as $q) {
1375
			if ($q->CheckBandwidth(0, '')) {
1376
				return 1;
1377
			}
1378
		}
1379

    
1380
		return 0;
1381
	}
1382
	function GetTotalBw($qignore = NULL) {
1383
		$sum = 0;
1384
		foreach ($this->subqueues as $q) {
1385
			if ($qignore != NULL && $qignore == $q)
1386
				continue;
1387
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1388
		}
1389

    
1390
		return $sum;
1391
	}
1392
	function GetBwscale() {
1393
		return $this->qbandwidthtype;
1394
	}
1395
	function FormGetBwscale() {
1396
		if ($this->GetBwscale()) {
1397
			$bwscale = $this->GetBwscale();
1398
		} else {
1399
			$bwscale = 'Mb';
1400
		}
1401
		return $bwscale;
1402
	}
1403
	function SetBwscale($scale) {
1404
		$this->qbandwidthtype = $scale;
1405
	}
1406
	function GetDefaultQueuePresent() {
1407
		if ($this->GetDefault()) {
1408
			return true;
1409
		}
1410
		if (!empty($this->subqueues)) {
1411
			foreach ($this->subqueues as $q) {
1412
				if ($q->GetDefault()) {
1413
					return true;
1414
				}
1415
			}
1416
		}
1417

    
1418
		return false;
1419
	}
1420
	function GetDefault() {
1421
		return $this->qdefault;
1422
	}
1423
	function SetDefault($value = false) {
1424
		$this->qdefault = $value;
1425
	}
1426
	function GetCodel() {
1427
		return $this->codel;
1428
	}
1429
	function SetCodel($codel = false) {
1430
		$this->codel = $codel;
1431
	}
1432
	function GetRed() {
1433
		return $this->qred;
1434
	}
1435
	function SetRed($red = false) {
1436
		$this->qred = $red;
1437
	}
1438
	function GetRio() {
1439
		return $this->qrio;
1440
	}
1441
	function SetRio($rio = false) {
1442
		$this->qrio = $rio;
1443
	}
1444
	function GetEcn() {
1445
		return $this->qecn;
1446
	}
1447
	function SetEcn($ecn = false) {
1448
		$this->qecn = $ecn;
1449
	}
1450
	function GetAck() {
1451
		return $this->qack;
1452
	}
1453
	function SetAck($ack = false) {
1454
		$this->qack = $ack;
1455
	}
1456

    
1457
	function GetBwscaleText() {
1458
		switch ($this->qbandwidthtype) {
1459
			case "b":
1460
				$bwscaletext = "Bit/s";
1461
				break;
1462
			case "Kb":
1463
				$bwscaletext = "Kbit/s";
1464
				break;
1465
			case "Mb":
1466
				$bwscaletext = "Mbit/s";
1467
				break;
1468
			case "Gb":
1469
				$bwscaletext = "Gbit/s";
1470
				break;
1471
			default:
1472
				/* For others that do not need translating like % */
1473
				$bwscaletext = $this->qbandwidthtype;
1474
				break;
1475
		}
1476
		return $bwscaletext;
1477
	}
1478

    
1479
	function build_javascript() {
1480
		$javascript = "<script type=\"text/javascript\">";
1481
		$javascript .= "//<![CDATA[\n";
1482
		$javascript .= "function mySuspend() { \n";
1483
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1484
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1485
		$javascript .= "else if (document.all)\n";
1486
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1487
		$javascript .= "}\n";
1488

    
1489
		$javascript .= "function myResume() {\n";
1490
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1491
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1492
		$javascript .= "else if (document.all)\n";
1493
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1494
		$javascript .= "}\n";
1495
		$javascript .= "//]]>";
1496
		$javascript .= "</script>";
1497

    
1498
		return $javascript;
1499
	}
1500

    
1501
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1502

    
1503
	/*
1504
	 * Currently this will not be called unless we decide to clone a whole
1505
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1506
	 */
1507
	function copy_queue($interface, &$cflink) {
1508

    
1509
		$cflink['name'] = $this->GetQname();
1510
		$cflink['interface'] = $interface;
1511
		$cflink['qlimit'] = $this->GetQlimit();
1512
		$cflink['priority'] = $this->GetQpriority();
1513
		$cflink['description'] = $this->GetDescription();
1514
		$cflink['enabled'] = $this->GetEnabled();
1515
		$cflink['default'] = $this->GetDefault();
1516
		$cflink['red'] = $this->GetRed();
1517
		$cflink['codel'] = $this->GetCodel();
1518
		$cflink['rio'] = $this->GetRio();
1519
		$cflink['ecn'] = $this->GetEcn();
1520

    
1521
		if (is_array($this->subqueues)) {
1522
			$cflinkp['queue'] = array();
1523
			foreach ($this->subqueues as $q) {
1524
				$cflink['queue'][$q->GetQname()] = array();
1525
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1526
			}
1527
		}
1528
	}
1529

    
1530
	function clean_queue($sched) {
1531
		clean_child_queues($sched, $this->GetLink());
1532
		if (is_array($this->subqueues)) {
1533
			foreach ($this->subqueues as $q) {
1534
				$q->clean_queue($sched);
1535
			}
1536
		}
1537
	}
1538

    
1539
	function &get_queue_list(&$qlist) {
1540

    
1541
		$qlist[$this->GetQname()] = & $this;
1542
		if (is_array($this->subqueues)) {
1543
			foreach ($this->subqueues as $queue) {
1544
				$queue->get_queue_list($qlist);
1545
			}
1546
		}
1547
	}
1548

    
1549
	function delete_queue() {
1550
		unref_on_altq_queue_list($this->GetQname());
1551
		cleanup_queue_from_rules($this->GetQname());
1552
		unset_object_by_reference($this->GetLink());
1553
	}
1554

    
1555
	function delete_all() {
1556
		if (count($this->subqueues)) {
1557
			foreach ($this->subqueues as $q) {
1558
				$q->delete_all();
1559
				unset_object_by_reference($q->GetLink());
1560
				unset($q);
1561
			}
1562
			unset($this->subqueues);
1563
		}
1564
	}
1565

    
1566
	function &find_queue($interface, $qname) {
1567
		if ($qname == $this->GetQname()) {
1568
			return $this;
1569
		}
1570
	}
1571

    
1572
	function find_parentqueue($interface, $qname) { return; }
1573

    
1574
	function validate_input($data, &$input_errors) {
1575
		global $altq_list_queues;
1576

    
1577
		$reqdfields[] = "name";
1578
		$reqdfieldsn[] = gettext("Name");
1579
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1580
		$parent = $altq_list_queues[$this->GetInterface()];
1581

    
1582
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
1583
			$input_errors[] = gettext("Bandwidth must be an integer.");
1584
		}
1585
		if ($data['bandwidth'] < 0) {
1586
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1587
		}
1588
		if ($data['bandwidthtype'] == "%") {
1589
			if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
1590
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1591
			}
1592
		}
1593
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1594
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1595

    
1596
		if ($parent) {
1597
			if (($parent->GetScheduler() == 'PRIQ') && (!is_numeric($data['priority']) ||
1598
			    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1599
				$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1600
			} elseif (in_array($parent->GetScheduler(), array('FAIRQ','CBQ')) && (!is_numeric($data['priority']) ||
1601
			    ($data['priority'] < 0) || ($data['priority'] > 7))) {
1602
				$input_errors[] = gettext("The priority must be an integer between 0 and 7.");
1603
			}
1604
			if (is_array($parent->queues)) {
1605
				foreach ($parent->queues as $q) {
1606
					if (($data['newname'] == $q->GetQname()) && ($data['name'] != $data['newname'])) { 
1607
						$input_errors[] = gettext("A queue with the same name already exists.");
1608
					}
1609
					if (in_array($parent->GetScheduler(), array('FAIRQ','PRIQ')) &&
1610
					    ($data['priority'] == $q->GetQpriority()) && ($data['name'] != $q->GetQname())) { 
1611
						$input_errors[] = sprintf(gettext("The priority %d is already used by queue %s."),
1612
								$data['priority'], $q->GetQname());
1613
					}
1614
				}
1615
			}
1616
		}
1617

    
1618
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1619
				$input_errors[] = gettext("Queue limit must be an integer");
1620
		}
1621
		if ($data['qlimit'] < 0) {
1622
				$input_errors[] = gettext("Queue limit must be positive");
1623
		}
1624
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1625
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1626
		}
1627
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1628
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1629
		}
1630
		$default = $this->GetDefault();
1631
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1632
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1633
		}
1634
	}
1635

    
1636
	function ReadConfig(&$q) {
1637
		if (!empty($q['name']) && !empty($q['newname']) && ($q['name'] != $q['newname'])) {
1638
			$this->SetQname($q['newname']);
1639
			rename_queue_in_rules($q['name'], $q['newname']);
1640
		} else if (!empty($q['newname'])) {
1641
			$this->SetQname($q['newname']);
1642
		} elseif (isset($q['name'])) {
1643
			$this->SetQname($q['name']);
1644
		}
1645
		if (isset($q['interface'])) {
1646
			$this->SetInterface($q['interface']);
1647
		}
1648
		$this->SetBandwidth($q['bandwidth']);
1649
		if (!empty($q['bandwidthtype'])) {
1650
			$this->SetBwscale($q['bandwidthtype']);
1651
		}
1652
		if (!empty($q['qlimit'])) {
1653
			$this->SetQlimit($q['qlimit']);
1654
		} else {
1655
			$this->SetQlimit(""); // Default
1656
		}
1657
		if (is_numeric($q['priority'])) {
1658
			$this->SetQPriority($q['priority']);
1659
		} else {
1660
			$this->SetQpriority("");
1661
		}
1662
		if (!empty($q['description'])) {
1663
			$this->SetDescription($q['description']);
1664
		} else {
1665
			$this->SetDescription("");
1666
		}
1667
		if (!empty($q['red'])) {
1668
			$this->SetRed($q['red']);
1669
		} else {
1670
			$this->SetRed();
1671
		}
1672
		if (!empty($q['codel'])) {
1673
			$this->SetCodel($q['codel']);
1674
		} else {
1675
			$this->SetCodel();
1676
		}
1677
		if (!empty($q['rio'])) {
1678
			$this->SetRio($q['rio']);
1679
		} else {
1680
			$this->SetRio();
1681
		}
1682
		if (!empty($q['ecn'])) {
1683
			$this->SetEcn($q['ecn']);
1684
		} else {
1685
			$this->SetEcn();
1686
		}
1687
		if (!empty($q['default'])) {
1688
			$this->SetDefault($q['default']);
1689
		} else {
1690
			$this->SetDefault();
1691
		}
1692
		if (!empty($q['enabled'])) {
1693
			$this->SetEnabled($q['enabled']);
1694
		} else {
1695
			$this->SetEnabled("");
1696
		}
1697
	}
1698

    
1699
	function build_tree() {
1700
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". htmlspecialchars($this->GetQname())."&amp;action=show";
1701
		$tree .= "\" ";
1702
		$tmpvalue = $this->GetDefault();
1703
		if (!empty($tmpvalue)) {
1704
			$tree .= " class=\"navlnk\"";
1705
		}
1706
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
1707
		/*
1708
		 * Not needed here!
1709
		 * if (is_array($queues) {
1710
		 *	  $tree .= "<ul>";
1711
		 *	  foreach ($q as $queues)
1712
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1713
		 *	  endforeach
1714
		 *	  $tree .= "</ul>";
1715
		 * }
1716
		 */
1717

    
1718
		$tree .= "</li>";
1719

    
1720
		return $tree;
1721
	}
1722

    
1723
	/* Should return something like:
1724
	 * queue $qname on $qinterface bandwidth ....
1725
	 */
1726
	function build_rules(&$default = false) {
1727
		$pfq_rule = " queue ". $this->qname;
1728
		if ($this->GetInterface()) {
1729
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1730
		}
1731
		$tmpvalue = $this->GetQpriority();
1732
		if (is_numeric($tmpvalue)) {
1733
			$pfq_rule .= " priority ".$this->GetQpriority();
1734
		}
1735
		$tmpvalue = $this->GetQlimit();
1736
		if (!empty($tmpvalue)) {
1737
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1738
		}
1739
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1740
			$pfq_rule .= " priq ( ";
1741
			$tmpvalue = $this->GetRed();
1742
			if (!empty($tmpvalue)) {
1743
				$comma = 1;
1744
				$pfq_rule .= " red ";
1745
			}
1746
			$tmpvalue = $this->GetRio();
1747
			if (!empty($tmpvalue)) {
1748
				if ($comma) {
1749
					$pfq_rule .= " ,";
1750
				}
1751
				$comma = 1;
1752
				$pfq_rule .= " rio ";
1753
			}
1754
			$tmpvalue = $this->GetEcn();
1755
			if (!empty($tmpvalue)) {
1756
				if ($comma) {
1757
					$pfq_rule .= " ,";
1758
				}
1759
				$comma = 1;
1760
				$pfq_rule .= " ecn ";
1761
			}
1762
			$tmpvalue = $this->GetCodel();
1763
			if (!empty($tmpvalue)) {
1764
				if ($comma) {
1765
					$pfq_rule .= " ,";
1766
				}
1767
				$comma = 1;
1768
				$pfq_rule .= " codel ";
1769
			}
1770
			$tmpvalue = $this->GetDefault();
1771
			if (!empty($tmpvalue)) {
1772
				if ($comma) {
1773
					$pfq_rule .= " ,";
1774
				}
1775
				$pfq_rule .= " default ";
1776
				$default = true;
1777
			}
1778
			$pfq_rule .= " ) ";
1779
		}
1780

    
1781
		$pfq_rule .= " \n";
1782

    
1783
		return $pfq_rule;
1784
	}
1785

    
1786
	/*
1787
	 * To return the html form to show to user
1788
	 * for getting the parameters.
1789
	 * Should do even for first time when the
1790
	 * object is created and later when we may
1791
	 * need to update it. (2)
1792
	 */
1793

    
1794
	function build_form() {
1795

    
1796
		$sform = new Form();
1797

    
1798
		$sform->setAction("firewall_shaper.php");
1799

    
1800
		$section = new Form_Section("");
1801

    
1802
		$section->addInput(new Form_Checkbox(
1803
			'enabled',
1804
			'Enable/Disable',
1805
			'Enable/disable discipline and its children',
1806
			($this->GetEnabled() == "on"),
1807
			'on'
1808
		));
1809

    
1810
		$section->addInput(new Form_Input(
1811
			'newname',
1812
			'*Name',
1813
			'text',
1814
			$this->GetQname()
1815
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1816

    
1817
		$section->addInput(new Form_Input(
1818
			'name',
1819
			null,
1820
			'hidden',
1821
			$this->GetQname()
1822
		));
1823

    
1824
		if (!is_a($this, "hfsc_queue")) {
1825
			$section->addInput(new Form_Input(
1826
				'priority',
1827
				'Priority',
1828
				'number',
1829
				$this->GetQpriority(),
1830
				['min' => '0', 'max'=> '']
1831
			))->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.');
1832
		}
1833

    
1834
		$section->addInput(new Form_Input(
1835
			'qlimit',
1836
			'Queue Limit',
1837
			'number',
1838
			$this->GetQlimit()
1839
		))->setHelp('Queue limit in packets.');
1840

    
1841
		$group = new Form_Group('Scheduler options');
1842

    
1843
		if (empty($this->subqueues)) {
1844
			$group->add(new Form_Checkbox(
1845
				'default',
1846
				null,
1847
				null,
1848
				$this->GetDefault(),
1849
				'default'
1850
			))->setHelp('Default Queue');
1851
		}
1852

    
1853
		$group->add(new Form_Checkbox(
1854
			'red',
1855
			null,
1856
			null,
1857
			!empty($this->GetRed())
1858
		))->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>');
1859

    
1860
		$group->add(new Form_Checkbox(
1861
			'rio',
1862
			null,
1863
			null,
1864
			!empty($this->GetRio())
1865
		))->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>');
1866

    
1867
		$group->add(new Form_Checkbox(
1868
			'ecn',
1869
			null,
1870
			null,
1871
			!empty($this->GetEcn())
1872
		))->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>');
1873

    
1874
		$group->add(new Form_Checkbox(
1875
			'codel',
1876
			null,
1877
			null,
1878
			!empty($this->GetCodel())
1879
		))->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>');
1880

    
1881
		$group->setHelp('Select options for this queue');
1882

    
1883
		$section->add($group);
1884

    
1885
		$section->addInput(new Form_Input(
1886
			'description',
1887
			'Description',
1888
			'text',
1889
			$this->GetDescription()
1890
		));
1891

    
1892
		$sform->add($section);
1893

    
1894
		$sform->addGlobal(new Form_Input(
1895
			'interface',
1896
			null,
1897
			'hidden',
1898
			$this->GetInterface()
1899
		));
1900

    
1901
		$sform->addGlobal(new Form_Input(
1902
			'name',
1903
			null,
1904
			'hidden',
1905
			$this->GetQname()
1906
		));
1907

    
1908
		return($sform);
1909
	}
1910

    
1911
	function build_shortform() {
1912
		/* XXX: Hacks in sight. Mostly layer violations!  */
1913
		global $g, $altq_list_queues;
1914
		global $shaperIFlist;
1915

    
1916
		$altq =& $altq_list_queues[$this->GetInterface()];
1917

    
1918
		if ($altq) {
1919
			$scheduler = $altq->GetScheduler();
1920
		}
1921

    
1922
		$form = '<dl class="dl-horizontal">';
1923
		$form .= '	<dt>';
1924
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1925
		$form .= '	</dt>';
1926
		$form .= '	<dd>';
1927
		$form .=		$scheduler;
1928
		$form .= '	</dd>';
1929

    
1930
		$form .= '	<dt>';
1931
		$form .=		'Bandwidth';
1932
		$form .= '	</dt>';
1933
		$form .= '	<dd>';
1934
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1935
		$form .= '	</dd>';
1936

    
1937
		$tmpvalue = $this->GetQpriority();
1938
		if (!empty($tmpvalue)) {
1939
			$form .= '	<dt>';
1940
			$form .=		'Priority';
1941
			$form .= '	<dt>';
1942
			$form .= '	<dd>';
1943
			$form .=		'On';
1944
			$form .= '	</dd>';
1945
		}
1946

    
1947
		$tmpvalue = $this->GetDefault();
1948
		if (!empty($tmpvalue)) {
1949
			$form .= '	<dt>';
1950
			$form .=		'Default';
1951
			$form .= '	<dt>';
1952
			$form .= '	<dd>';
1953
			$form .=		'On';
1954
			$form .= '	</dd>';
1955
		}
1956

    
1957
			$form .= '	<dt>';
1958
			$form .= 'Delete';
1959
			$form .= '	<dt>';
1960
			$form .= '	<dd>';
1961

    
1962
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1963
			$form .= $this->GetInterface() . '&amp;queue=';
1964
			$form .= $this->GetQname() . '&amp;action=delete">';
1965
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1966
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1967

    
1968
			$form .= '	</dd>';
1969

    
1970
			$form .= '</dl>';
1971

    
1972
		return $form;
1973

    
1974
	}
1975

    
1976
	function update_altq_queue_data(&$q) {
1977
		$this->ReadConfig($q);
1978
	}
1979

    
1980
	function wconfig() {
1981
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1982
		if (!is_array($cflink)) {
1983
			$cflink = array();
1984
		}
1985
		$cflink['name'] = $this->GetQname();
1986
		$cflink['interface'] = $this->GetInterface();
1987
		$cflink['qlimit'] = trim($this->GetQlimit());
1988
		if (empty($cflink['qlimit'])) {
1989
			unset($cflink['qlimit']);
1990
		}
1991
		$cflink['priority'] = trim($this->GetQpriority());
1992
		if (!is_numeric($cflink['priority'])) {
1993
			unset($cflink['priority']);
1994
		}
1995
		$cflink['description'] = trim($this->GetDescription());
1996
		if (empty($cflink['description'])) {
1997
			unset($cflink['description']);
1998
		}
1999
		$cflink['enabled'] = trim($this->GetEnabled());
2000
		if (empty($cflink['enabled'])) {
2001
			unset($cflink['enabled']);
2002
		}
2003
		$cflink['default'] = trim($this->GetDefault());
2004
		if (empty($cflink['default'])) {
2005
			unset($cflink['default']);
2006
		}
2007
		$cflink['red'] = trim($this->GetRed());
2008
		if (empty($cflink['red'])) {
2009
			unset($cflink['red']);
2010
		}
2011
		$cflink['codel'] = trim($this->GetCodel());
2012
		if (empty($cflink['codel'])) {
2013
			unset($cflink['codel']);
2014
		}
2015
		$cflink['rio'] = trim($this->GetRio());
2016
		if (empty($cflink['rio'])) {
2017
			unset($cflink['rio']);
2018
		}
2019
		$cflink['ecn'] = trim($this->GetEcn());
2020
		if (empty($cflink['ecn'])) {
2021
			unset($cflink['ecn']);
2022
		}
2023
	}
2024
}
2025

    
2026
class hfsc_queue extends priq_queue {
2027
	/* realtime */
2028
	var $realtime;
2029
	var $r_m1;
2030
	var $r_d;
2031
	var $r_m2;
2032
	/* linkshare */
2033
	var $linkshare;
2034
	var $l_m1;
2035
	var $l_d;
2036
	var $l_m2;
2037
	/* upperlimit */
2038
	var $upperlimit;
2039
	var $u_m1;
2040
	var $u_d;
2041
	var $u_m2;
2042

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

    
2131
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2132

    
2133
		if (!is_array($this->subqueues)) {
2134
			$this->subqueues = array();
2135
		}
2136
		$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
2137
		$q->SetInterface($this->GetInterface());
2138
		$q->SetParent($this);
2139
		$q->ReadConfig($qname);
2140
		$q->validate_input($qname, $input_errors);
2141

    
2142
		$q->SetEnabled("on");
2143
		$q->SetLink($path);
2144

    
2145
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
2146
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2147
		if (is_array($qname['queue'])) {
2148
			foreach ($qname['queue'] as $key1 => $que) {
2149
				array_push($path, $key1);
2150
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2151
				array_pop($path);
2152
			}
2153
		}
2154

    
2155
		return $q;
2156
	}
2157

    
2158
	function copy_queue($interface, &$cflink) {
2159

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

    
2251
		if (is_array($this->subqueues)) {
2252
			$cflinkp['queue'] = array();
2253
			foreach ($this->subqueues as $q) {
2254
				$cflink['queue'][$q->GetQname()] = array();
2255
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2256
			}
2257
		}
2258
	}
2259

    
2260
	function delete_queue() {
2261
		unref_on_altq_queue_list($this->GetQname());
2262
		cleanup_queue_from_rules($this->GetQname());
2263
		$parent =& $this->GetParent();
2264
		foreach ($this->subqueues as $q) {
2265
			$q->delete_queue();
2266
		}
2267
		unset_object_by_reference($this->GetLink());
2268
	}
2269

    
2270
	/*
2271
	 * Should search even its children
2272
	 */
2273
	function &find_queue($interface, $qname) {
2274
		if ($qname == $this->GetQname()) {
2275
			return $this;
2276
		}
2277

    
2278
		foreach ($this->subqueues as $q) {
2279
			$result =& $q->find_queue("", $qname);
2280
			if ($result) {
2281
				return $result;
2282
			}
2283
		}
2284
	}
2285

    
2286
	function &find_parentqueue($interface, $qname) {
2287
		if ($this->subqueues[$qname]) {
2288
			return $this;
2289
		}
2290
		foreach ($this->subqueues as $q) {
2291
			$result = $q->find_parentqueue("", $qname);
2292
			if ($result) {
2293
				return $result;
2294
			}
2295
		}
2296
	}
2297

    
2298
	function validate_input($data, &$input_errors) {
2299
		parent::validate_input($data, $input_errors);
2300

    
2301
		$reqdfields[] = "bandwidth";
2302
		$reqdfieldsn[] = gettext("Bandwidth");
2303
		$reqdfields[] = "bandwidthtype";
2304
		$reqdfieldsn[] = gettext("Bandwidthtype");
2305

    
2306
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2307

    
2308
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2309
			if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2310
				$input_errors[] = gettext("Bandwidth must be an integer.");
2311
			}
2312

    
2313
			if ($data['bandwidth'] < 0) {
2314
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2315
			}
2316

    
2317
			if ($data['bandwidthtype'] == "%") {
2318
				if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
2319
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2320
				}
2321
			}
2322
		}
2323

    
2324
		if (!empty($data['upperlimit1']) && empty($data['upperlimit2'])) {
2325
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
2326
		}
2327
		if (!empty($data['upperlimit2']) && empty($data['upperlimit1'])) {
2328
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
2329
		}
2330
		if (!empty($data['upperlimit1']) && !is_valid_shaperbw($data['upperlimit1'])) {
2331
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
2332
		}
2333
		if (!empty($data['upperlimit2']) && !is_numeric($data['upperlimit2'])) {
2334
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
2335
		}
2336
		if (!empty($data['upperlimit3']) && !is_valid_shaperbw($data['upperlimit3'])) {
2337
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
2338
		}
2339

    
2340
		/*
2341
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
2342
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
2343
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
2344
			if (floatval($bw_1) < floatval($bw_2)) {
2345
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
2346
			}
2347

    
2348
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2349
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
2350
			}
2351
		}
2352
		*/
2353
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
2354
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
2355
		}
2356
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
2357
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
2358
		}
2359
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
2360
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
2361
		}
2362
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
2363
			$input_errors[] = gettext("linkshare d value needs to be numeric");
2364
		}
2365
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
2366
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
2367
		}
2368
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2369
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2370
		}
2371
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2372
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2373
		}
2374

    
2375
		/*
2376
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2377
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2378
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2379
			if (floatval($bw_1) < floatval($bw_2)) {
2380
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2381
			}
2382

    
2383
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2384
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2385
			}
2386
		}
2387
		*/
2388

    
2389
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2390
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2391
		}
2392
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2393
			$input_errors[] = gettext("realtime d value needs to be numeric");
2394
		}
2395
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2396
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2397
		}
2398

    
2399
		/*
2400
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2401
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2402
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2403
			if (floatval($bw_1) < floatval($bw_2)) {
2404
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2405
			}
2406

    
2407
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2408
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2409
			}
2410
		}
2411
		*/
2412
	}
2413

    
2414
	function ReadConfig(&$cflink) {
2415
		if (!empty($cflink['linkshare'])) {
2416
			if (!empty($cflink['linkshare1'])) {
2417
				$this->SetL_m1($cflink['linkshare1']);
2418
				$this->SetL_d($cflink['linkshare2']);
2419
				$this->SetLinkshare();
2420
			} else {
2421
				$this->SetL_m1("");
2422
				$this->SetL_d("");
2423
				$this->DisableLinkshare();
2424
			}
2425
			if (!empty($cflink['linkshare3'])) {
2426
				$this->SetL_m2($cflink['linkshare3']);
2427
				$this->SetLinkshare();
2428
			}
2429
		} else {
2430
			$this->DisableLinkshare();
2431
		}
2432
		if (!empty($cflink['realtime'])) {
2433
			if (!empty($cflink['realtime1'])) {
2434
				$this->SetR_m1($cflink['realtime1']);
2435
				$this->SetR_d($cflink['realtime2']);
2436
				$this->SetRealtime();
2437
			} else {
2438
				$this->SetR_m1("");
2439
				$this->SetR_d("");
2440
				$this->DisableRealtime();
2441
			}
2442
			if (!empty($cflink['realtime3'])) {
2443
				$this->SetR_m2($cflink['realtime3']);
2444
				$this->SetRealtime();
2445
			}
2446
		} else {
2447
			$this->DisableRealtime();
2448
		}
2449
		if (!empty($cflink['upperlimit'])) {
2450
			if (!empty($cflink['upperlimit1'])) {
2451
				$this->SetU_m1($cflink['upperlimit1']);
2452
				$this->SetU_d($cflink['upperlimit2']);
2453
				$this->SetUpperlimit();
2454
			} else {
2455
				$this->SetU_m1("");
2456
				$this->SetU_d("");
2457
				$this->DisableUpperlimit();
2458
			}
2459
			if (!empty($cflink['upperlimit3'])) {
2460
				$this->SetU_m2($cflink['upperlimit3']);
2461
				$this->SetUpperlimit();
2462
			}
2463
		} else {
2464
			$this->DisableUpperlimit();
2465
		}
2466
		parent::ReadConfig($cflink);
2467
	}
2468

    
2469
	function build_tree() {
2470
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
2471
		$tree .= "\" ";
2472
		$tmpvalue = $this->GetDefault();
2473
		if (!empty($tmpvalue)) {
2474
			$tree .= " class=\"navlnk\"";
2475
		}
2476
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
2477
		if (is_array($this->subqueues)) {
2478
			$tree .= "<ul>";
2479
			foreach ($this->subqueues as $q) {
2480
				$tree .= $q->build_tree();
2481
			}
2482
			$tree .= "</ul>";
2483
		}
2484
		$tree .= "</li>";
2485
		return $tree;
2486
	}
2487

    
2488
	/* Even this should take children into consideration */
2489
	function build_rules(&$default = false) {
2490

    
2491
		$pfq_rule = " queue ". $this->qname;
2492
		if ($this->GetInterface()) {
2493
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2494
		}
2495
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2496
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2497
		}
2498

    
2499
		$tmpvalue = $this->GetQlimit();
2500
		if (!empty($tmpvalue)) {
2501
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2502
		}
2503
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2504
			$pfq_rule .= " hfsc ( ";
2505
			$tmpvalue = $this->GetRed();
2506
			if (!empty($tmpvalue)) {
2507
				$comma = 1;
2508
				$pfq_rule .= " red ";
2509
			}
2510

    
2511
			$tmpvalue = $this->GetRio();
2512
			if (!empty($tmpvalue)) {
2513
				if ($comma) {
2514
					$pfq_rule .= " ,";
2515
				}
2516
				$comma = 1;
2517
				$pfq_rule .= " rio ";
2518
			}
2519
			$tmpvalue = $this->GetEcn();
2520
			if (!empty($tmpvalue)) {
2521
				if ($comma) {
2522
					$pfq_rule .= " ,";
2523
				}
2524
				$comma = 1;
2525
				$pfq_rule .= " ecn ";
2526
			}
2527
			$tmpvalue = $this->GetCodel();
2528
			if (!empty($tmpvalue)) {
2529
				if ($comma) {
2530
					$pfq_rule .= " ,";
2531
				}
2532
				$comma = 1;
2533
				$pfq_rule .= " codel ";
2534
			}
2535
			$tmpvalue = $this->GetDefault();
2536
			if (!empty($tmpvalue)) {
2537
				if ($comma) {
2538
					$pfq_rule .= " ,";
2539
				}
2540
				$comma = 1;
2541
				$pfq_rule .= " default ";
2542
				$default = true;
2543
			}
2544

    
2545
			if ($this->GetRealtime() <> "") {
2546
				if ($comma) {
2547
					$pfq_rule .= " , ";
2548
				}
2549
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2550
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2551
				} else if ($this->GetR_m2() <> "") {
2552
					$pfq_rule .= " realtime " . $this->GetR_m2();
2553
				}
2554
				$comma = 1;
2555
			}
2556
			if ($this->GetLinkshare() <> "") {
2557
				if ($comma) {
2558
					$pfq_rule .= " ,";
2559
				}
2560
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2561
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2562
				} else if ($this->GetL_m2() <> "") {
2563
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2564
				}
2565
				$comma = 1;
2566
			}
2567
			if ($this->GetUpperlimit() <> "") {
2568
				if ($comma) {
2569
					$pfq_rule .= " ,";
2570
				}
2571
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2572
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2573
				} else if ($this->GetU_m2() <> "") {
2574
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2575
				}
2576
			}
2577
			$pfq_rule .= " ) ";
2578
		}
2579
		if (count($this->subqueues)) {
2580
			$i = count($this->subqueues);
2581
			$pfq_rule .= " { ";
2582
			foreach ($this->subqueues as $qkey => $qnone) {
2583
				if ($i > 1) {
2584
					$i--;
2585
					$pfq_rule .= " {$qkey}, ";
2586
				} else {
2587
					$pfq_rule .= " {$qkey} ";
2588
				}
2589
			}
2590
			$pfq_rule .= " } \n";
2591
			foreach ($this->subqueues as $q) {
2592
				$pfq_rule .= $q->build_rules($default);
2593
			}
2594
		}
2595

    
2596
		$pfq_rule .= " \n";
2597

    
2598
		return $pfq_rule;
2599
	}
2600

    
2601
	function build_javascript() {
2602

    
2603
		$javascript = <<<EOJS
2604
<script type="text/javascript">
2605
//<![CDATA[
2606
	events.push(function(){
2607

    
2608
		// Disables the specified input element
2609
		function disableInput(id, disable) {
2610
			$('#' + id).prop("disabled", disable);
2611
		}
2612

    
2613
		// Upperlimit
2614
		function enable_upperlimit() {
2615
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2616
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2617
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2618
		}
2619

    
2620
		$('#upperlimit').click(function () {
2621
			enable_upperlimit();
2622
		});
2623

    
2624
		enable_upperlimit();
2625

    
2626
		// realtime
2627
		function enable_realtime() {
2628
			disableInput('realtime1', !$('#realtime').prop('checked'));
2629
			disableInput('realtime2', !$('#realtime').prop('checked'));
2630
			disableInput('realtime3', !$('#realtime').prop('checked'));
2631
		}
2632

    
2633
		$('#realtime').click(function () {
2634
			enable_realtime();
2635
		});
2636

    
2637
		enable_realtime();
2638

    
2639
		// linkshare
2640
		function enable_linkshare() {
2641
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2642
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2643
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2644
		}
2645

    
2646
		$('#linkshare').click(function () {
2647
			enable_linkshare();
2648
		});
2649

    
2650
		enable_linkshare();
2651
	});
2652
//]]>
2653
</script>
2654
EOJS;
2655

    
2656
		return $javascript;
2657
	}
2658

    
2659
	function build_form() {
2660

    
2661
		$sform = parent::build_form();
2662

    
2663
		$section = new Form_Section('Service Curve (sc)');
2664

    
2665
		$group = new Form_Group('Bandwidth');
2666

    
2667
		$group->add(new Form_Input(
2668
			'bandwidth',
2669
			null,
2670
			'number',
2671
			$this->GetBandwidth(),
2672
			['step' => 'any', 'min' => '0.000']
2673
		));
2674

    
2675
		$group->add(new Form_Select(
2676
			'bandwidthtype',
2677
			null,
2678
			$this->FormGetBwscale(),
2679
			array('Kb' => 'Kbit/s',
2680
				  'Mb' => 'Mbit/s',
2681
				  'Gb' => 'Gbit/s',
2682
				  'b' => 'Bit/s',
2683
				  '%' => '%')
2684
		));
2685

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

    
2688
		$section->add($group);
2689

    
2690
		$group = new Form_Group('Max bandwidth for queue.');
2691

    
2692
		$group->add(new Form_Checkbox(
2693
			'upperlimit',
2694
			null,
2695
			'Upper Limit',
2696
			($this->GetUpperlimit()<> "")
2697
		));
2698

    
2699
		$group->add(new Form_Input(
2700
			'upperlimit1',
2701
			null,
2702
			'text',
2703
			$this->GetU_m1()
2704
		))->setHelp('m1');
2705

    
2706
		$group->add(new Form_Input(
2707
			'upperlimit2',
2708
			null,
2709
			'text',
2710
			$this->GetU_d()
2711
		))->setHelp('d');
2712

    
2713
		$group->add(new Form_Input(
2714
			'upperlimit3',
2715
			null,
2716
			'text',
2717
			$this->GetU_m2()
2718
		))->setHelp('m2');
2719

    
2720

    
2721
		$section->add($group);
2722

    
2723
		$group = new Form_Group('Min bandwidth for queue.');
2724

    
2725
		$group->add(new Form_Checkbox(
2726
			'realtime',
2727
			null,
2728
			'Real Time',
2729
			($this->GetRealtime()<> "")
2730
		));
2731

    
2732
		$group->add(new Form_Input(
2733
			'realtime1',
2734
			null,
2735
			'text',
2736
			$this->GetR_m1()
2737
		))->setHelp('m1');
2738

    
2739
		$group->add(new Form_Input(
2740
			'realtime2',
2741
			null,
2742
			'text',
2743
			$this->GetR_d()
2744
		))->setHelp('d');
2745

    
2746
		$group->add(new Form_Input(
2747
			'realtime3',
2748
			null,
2749
			'text',
2750
			$this->GetR_m2()
2751
		))->setHelp('m2');
2752

    
2753
		$section->add($group);
2754

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

    
2757
		$group->add(new Form_Checkbox(
2758
			'linkshare',
2759
			null,
2760
			'Link Share',
2761
			($this->GetLinkshare()<> "")
2762
		));
2763

    
2764
		$group->add(new Form_Input(
2765
			'linkshare1',
2766
			null,
2767
			'text',
2768
			$this->GetL_m1()
2769
		))->setHelp('m1');
2770

    
2771
		$group->add(new Form_Input(
2772
			'linkshare2',
2773
			null,
2774
			'text',
2775
			$this->GetL_d()
2776
		))->setHelp('d');
2777

    
2778
		$group->add(new Form_Input(
2779
			'linkshare3',
2780
			null,
2781
			'text',
2782
			$this->GetL_m2()
2783
		))->setHelp('m2');
2784

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

    
2791
		$section->add($group);
2792

    
2793
		$sform->add($section);
2794

    
2795
		return($sform);
2796
	}
2797

    
2798
	function update_altq_queue_data(&$data) {
2799
		$this->ReadConfig($data);
2800
	}
2801

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

    
2919
class cbq_queue extends priq_queue {
2920
	var $qborrow = "";
2921

    
2922
	function GetBorrow() {
2923
		return $this->qborrow;
2924
	}
2925
	function SetBorrow($borrow) {
2926
		$this->qborrow = $borrow;
2927
	}
2928
	function CanHaveChildren() {
2929
		return true;
2930
	}
2931

    
2932
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2933

    
2934
		if (!is_array($this->subqueues)) {
2935
			$this->subqueues = array();
2936
		}
2937
		$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
2938
		$q->SetInterface($this->GetInterface());
2939
		$q->SetParent($this);
2940
		$q->ReadConfig($qname);
2941
		$q->validate_input($qname, $input_errors);
2942

    
2943
		$q->SetEnabled("on");
2944
		$q->SetLink($path);
2945
		$this->subqueues[$q->GetQName()] = &$q;
2946
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2947
		if (is_array($qname['queue'])) {
2948
			foreach ($qname['queue'] as $key1 => $que) {
2949
				array_push($path, $key1);
2950
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2951
				array_pop($path);
2952
			}
2953
		}
2954

    
2955
		return $q;
2956
	}
2957

    
2958
	function copy_queue($interface, &$cflink) {
2959

    
2960
		$cflink['interface'] = $interface;
2961
		$cflink['qlimit'] = trim($this->GetQlimit());
2962
		if (empty($clink['qlimit'])) {
2963
			unset($cflink['qlimit']);
2964
		}
2965
		$cflink['priority'] = trim($this->GetQpriority());
2966
		if (!is_numeric($cflink['priority'])) {
2967
			unset($cflink['priority']);
2968
		}
2969
		$cflink['name'] = $this->GetQname();
2970
		$cflink['description'] = trim($this->GetDescription());
2971
		if (empty($cflink['description'])) {
2972
			unset($cflink['description']);
2973
		}
2974
		$cflink['bandwidth'] = $this->GetBandwidth();
2975
		$cflink['bandwidthtype'] = $this->GetBwscale();
2976
		$cflink['enabled'] = trim($this->GetEnabled());
2977
		if (empty($cflink['enabled'])) {
2978
			unset($cflink['enabled']);
2979
		}
2980
		$cflink['default'] = trim($this->GetDefault());
2981
		if (empty($cflink['default'])) {
2982
			unset($cflink['default']);
2983
		}
2984
		$cflink['red'] = trim($this->GetRed());
2985
		if (empty($cflink['red'])) {
2986
			unset($cflink['red']);
2987
		}
2988
		$cflink['rio'] = trim($this->GetRio());
2989
		if (empty($cflink['rio'])) {
2990
			unset($cflink['rio']);
2991
		}
2992
		$cflink['ecn'] = trim($this->GetEcn());
2993
		if (empty($cflink['ecn'])) {
2994
			unset($cflink['ecn']);
2995
		}
2996
		$cflink['borrow'] = trim($this->GetBorrow());
2997
		if (empty($cflink['borrow'])) {
2998
			unset($cflink['borrow']);
2999
		}
3000
		if (is_array($this->queues)) {
3001
			$cflinkp['queue'] = array();
3002
			foreach ($this->subqueues as $q) {
3003
				$cflink['queue'][$q->GetQname()] = array();
3004
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
3005
			}
3006
		}
3007
	}
3008

    
3009
	/*
3010
	 * Should search even its children
3011
	 */
3012
	function &find_queue($interface, $qname) {
3013
		if ($qname == $this->GetQname()) {
3014
			return $this;
3015
		}
3016
		foreach ($this->subqueues as $q) {
3017
			$result =& $q->find_queue("", $qname);
3018
			if ($result) {
3019
				return $result;
3020
			}
3021
		}
3022
	}
3023

    
3024
	function &find_parentqueue($interface, $qname) {
3025
		if ($this->subqueues[$qname]) {
3026
			return $this;
3027
		}
3028
		foreach ($this->subqueues as $q) {
3029
			$result = $q->find_parentqueue("", $qname);
3030
			if ($result) {
3031
				return $result;
3032
			}
3033
		}
3034
	}
3035

    
3036
	function delete_queue() {
3037
		unref_on_altq_queue_list($this->GetQname());
3038
		cleanup_queue_from_rules($this->GetQname());
3039
		foreach ($this->subqueues as $q) {
3040
			$q->delete_queue();
3041
		}
3042
		unset_object_by_reference($this->GetLink());
3043
	}
3044

    
3045
	function validate_input($data, &$input_errors) {
3046
		parent::validate_input($data, $input_errors);
3047

    
3048
		$reqdfields[] = "bandwidth";
3049
		$reqdfieldsn[] = gettext("Bandwidth");
3050
		$reqdfields[] = "bandwidthtype";
3051
		$reqdfieldsn[] = gettext("Bandwidthtype");
3052

    
3053
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3054

    
3055
		if ($data['priority'] > 7) {
3056
			$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3057
		}
3058

    
3059
		$parent = $this->GetParent();
3060
		if (method_exists($parent, 'GetParent') && ($parent->GetBorrow() != "on") &&
3061
		    ($data['borrow'] == 'yes')) {
3062
			$input_errors[] = gettext("You cannot set 'Borrow' if the parent queue also does not borrow.");
3063
		}
3064
	}
3065

    
3066
	function ReadConfig(&$q) {
3067
		parent::ReadConfig($q);
3068
		if (!empty($q['borrow'])) {
3069
			$this->SetBorrow("on");
3070
		} else {
3071
			$this->SetBorrow("");
3072
		}
3073
	}
3074

    
3075
	function build_javascript() {
3076
		return parent::build_javascript();
3077
	}
3078

    
3079
	function build_tree() {
3080
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3081
		$tree .= "\" ";
3082
		$tmpvalue = trim($this->GetDefault());
3083
		if (!empty($tmpvalue)) {
3084
			$tree .= " class=\"navlnk\"";
3085
		}
3086
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3087
		if (is_array($this->subqueues)) {
3088
			$tree .= "<ul>";
3089
			foreach ($this->subqueues as $q) {
3090
				$tree .= $q->build_tree();
3091
			}
3092
			$tree .= "</ul>";
3093
		}
3094
		$tree .= "</li>";
3095
		return $tree;
3096
	}
3097

    
3098
	/* Even this should take children into consideration */
3099
	function build_rules(&$default = false) {
3100
		$pfq_rule = "queue ". $this->qname;
3101
		if ($this->GetInterface()) {
3102
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3103
		}
3104
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3105
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3106
		}
3107
		$tmpvalue = $this->GetQpriority();
3108
		if (is_numeric($tmpvalue)) {
3109
			$pfq_rule .= " priority " . $this->GetQpriority();
3110
		}
3111
		$tmpvalue = trim($this->GetQlimit());
3112
		if (!empty($tmpvalue)) {
3113
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3114
		}
3115
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
3116
			$pfq_rule .= " cbq ( ";
3117
			$tmpvalue = trim($this->GetRed());
3118
			if (!empty($tmpvalue)) {
3119
				$comma = 1;
3120
				$pfq_rule .= " red ";
3121
			}
3122
			$tmpvalue = trim($this->GetCodel());
3123
			if (!empty($tmpvalue)) {
3124
				$comma = 1;
3125
				$pfq_rule .= " codel ";
3126
			}
3127
			$tmpvalue = trim($this->GetRio());
3128
			if (!empty($tmpvalue)) {
3129
				if ($comma) {
3130
					$pfq_rule .= " ,";
3131
				}
3132
				$comma = 1;
3133
				$pfq_rule .= " rio ";
3134
			}
3135
			$tmpvalue = trim($this->GetEcn());
3136
			if (!empty($tmpvalue)) {
3137
				if ($comma) {
3138
					$pfq_rule .= " ,";
3139
				}
3140
				$comma = 1;
3141
				$pfq_rule .= " ecn ";
3142
			}
3143
			$tmpvalue = trim($this->GetDefault());
3144
			if (!empty($tmpvalue)) {
3145
				if ($comma) {
3146
					$pfq_rule .= " ,";
3147
				}
3148
				$comma = 1;
3149
				$pfq_rule .= " default ";
3150
				$default = true;
3151
			}
3152
			$tmpvalue = trim($this->GetBorrow());
3153
			if (!empty($tmpvalue)) {
3154
				if ($comma) {
3155
					$pfq_rule .= ", ";
3156
				}
3157
				$pfq_rule .= " borrow ";
3158
			}
3159
			$pfq_rule .= " ) ";
3160
		}
3161
		if (count($this->subqueues)) {
3162
			$i = count($this->subqueues);
3163
			$pfq_rule .= " { ";
3164
			foreach ($this->subqueues as $qkey => $qnone) {
3165
				if ($i > 1) {
3166
					$i--;
3167
					$pfq_rule .= " {$qkey}, ";
3168
				} else {
3169
					$pfq_rule .= " {$qkey} ";
3170
				}
3171
			}
3172
			$pfq_rule .= " } \n";
3173
			foreach ($this->subqueues as $q) {
3174
				$pfq_rule .= $q->build_rules($default);
3175
			}
3176
		}
3177

    
3178
		$pfq_rule .= " \n";
3179
		return $pfq_rule;
3180
	}
3181

    
3182
	function build_form() {
3183
		$sform = parent::build_form();
3184

    
3185
		$section = new Form_Section('NOTITLE');
3186

    
3187
		$group = new Form_Group('Bandwidth');
3188

    
3189
		$group->add(new Form_Input(
3190
			'bandwidth',
3191
			null,
3192
			'number',
3193
			$this->GetBandwidth()
3194
		));
3195

    
3196
		$group->add(new Form_Select(
3197
			'bandwidthtype',
3198
			null,
3199
			$this->FormGetBwscale(),
3200
			array('Kb' => 'Kbit/s',
3201
				  'Mb' => 'Mbit/s',
3202
				  'Gb' => 'Gbit/s',
3203
				  'b' => 'Bit/s',
3204
				  '%' => '%')
3205
		));
3206

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

    
3209
		$section->add($group);
3210

    
3211
		$section->addInput(new Form_Checkbox(
3212
			'borrow',
3213
			'Scheduler option',
3214
			'Borrow from other queues when available',
3215
			($this->GetBorrow() == "on")
3216
		));
3217

    
3218
		$sform->add($section);
3219

    
3220
		return $sform;
3221
	}
3222

    
3223
	function update_altq_queue_data(&$data) {
3224
		$this->ReadConfig($data);
3225
	}
3226

    
3227
	function wconfig() {
3228
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3229
		if (!is_array($cflink)) {
3230
			$cflink = array();
3231
		}
3232
		$cflink['interface'] = $this->GetInterface();
3233
		$cflink['qlimit'] = trim($this->GetQlimit());
3234
		if (empty($cflink['qlimit'])) {
3235
			unset($cflink['qlimit']);
3236
		}
3237
		$cflink['priority'] = $this->GetQpriority();
3238
		if (!is_numeric($cflink['priority'])) {
3239
			unset($cflink['priority']);
3240
		}
3241
		$cflink['name'] = $this->GetQname();
3242
		$cflink['description'] = $this->GetDescription();
3243
		if (empty($cflink['description'])) {
3244
			unset($cflink['description']);
3245
		}
3246
		$cflink['bandwidth'] = $this->GetBandwidth();
3247
		$cflink['bandwidthtype'] = $this->GetBwscale();
3248
		$cflink['enabled'] = trim($this->GetEnabled());
3249
		if (empty($cflink['enabled'])) {
3250
			unset($cflink['enabled']);
3251
		}
3252
		$cflink['default'] = trim($this->GetDefault());
3253
		if (empty($cflink['default'])) {
3254
			unset($cflink['default']);
3255
		}
3256
		$cflink['red'] = trim($this->GetRed());
3257
		if (empty($cflink['red'])) {
3258
			unset($cflink['red']);
3259
		}
3260
		$cflink['rio'] = trim($this->GetRio());
3261
		if (empty($cflink['rio'])) {
3262
			unset($cflink['rio']);
3263
		}
3264
		$cflink['ecn'] = trim($this->GetEcn());
3265
		if (empty($cflink['ecn'])) {
3266
			unset($cflink['ecn']);
3267
		}
3268
		$cflink['codel'] = trim($this->GetCodel());
3269
		if (empty($cflink['codel'])) {
3270
			unset($cflink['codel']);
3271
		}
3272
		$cflink['borrow'] = trim($this->GetBorrow());
3273
		if (empty($cflink['borrow'])) {
3274
			unset($cflink['borrow']);
3275
		}
3276
	}
3277
}
3278

    
3279
class fairq_queue extends priq_queue {
3280
	var $hogs;
3281
	var $buckets;
3282

    
3283
	function GetBuckets() {
3284
		return $this->buckets;
3285
	}
3286
	function SetBuckets($buckets) {
3287
		$this->buckets = $buckets;
3288
	}
3289
	function GetHogs() {
3290
		return $this->hogs;
3291
	}
3292
	function SetHogs($hogs) {
3293
		$this->hogs = $hogs;
3294
	}
3295
	function CanHaveChildren() {
3296
		return false;
3297
	}
3298

    
3299

    
3300
	function copy_queue($interface, &$cflink) {
3301
		$cflink['interface'] = $interface;
3302
		$cflink['qlimit'] = $this->GetQlimit();
3303
		$cflink['priority'] = $this->GetQpriority();
3304
		$cflink['name'] = $this->GetQname();
3305
		$cflink['description'] = $this->GetDescription();
3306
		$cflink['bandwidth'] = $this->GetBandwidth();
3307
		$cflink['bandwidthtype'] = $this->GetBwscale();
3308
		$cflink['enabled'] = $this->GetEnabled();
3309
		$cflink['default'] = $this->GetDefault();
3310
		$cflink['red'] = $this->GetRed();
3311
		$cflink['rio'] = $this->GetRio();
3312
		$cflink['ecn'] = $this->GetEcn();
3313
		$cflink['buckets'] = $this->GetBuckets();
3314
		$cflink['hogs'] = $this->GetHogs();
3315
	}
3316

    
3317
	/*
3318
	 * Should search even its children
3319
	 */
3320
	function &find_queue($interface, $qname) {
3321
		if ($qname == $this->GetQname()) {
3322
			return $this;
3323
		}
3324
	}
3325

    
3326
	function find_parentqueue($interface, $qname) { return; }
3327

    
3328
	function delete_queue() {
3329
		unref_on_altq_queue_list($this->GetQname());
3330
		cleanup_queue_from_rules($this->GetQname());
3331
		unset_object_by_reference($this->GetLink());
3332
	}
3333

    
3334
	function validate_input($data, &$input_errors) {
3335
		parent::validate_input($data, $input_errors);
3336

    
3337
		if ($data['priority'] > 7) {
3338
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3339
		}
3340
		$reqdfields[] = "bandwidth";
3341
		$reqdfieldsn[] = gettext("Bandwidth");
3342
		$reqdfields[] = "bandwidthtype";
3343
		$reqdfieldsn[] = gettext("Bandwidthtype");
3344

    
3345
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3346
	}
3347

    
3348
	function ReadConfig(&$q) {
3349
		parent::ReadConfig($q);
3350
		if (!empty($q['buckets'])) {
3351
			$this->SetBuckets($q['buckets']);
3352
		} else {
3353
			$this->SetBuckets("");
3354
		}
3355
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3356
			$this->SetHogs($q['hogs']);
3357
		} else {
3358
			$this->SetHogs("");
3359
		}
3360
	}
3361

    
3362
	function build_javascript() {
3363
		return parent::build_javascript();
3364
	}
3365

    
3366
	function build_tree() {
3367
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3368
		$this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3369
		$tree .= "\" ";
3370
		$tmpvalue = trim($this->GetDefault());
3371
		if (!empty($tmpvalue)) {
3372
			$tree .= " class=\"navlnk\"";
3373
		}
3374
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3375
		$tree .= "</li>";
3376
		return $tree;
3377
	}
3378

    
3379
	/* Even this should take children into consideration */
3380
	function build_rules(&$default = false) {
3381
		$pfq_rule = "queue ". $this->qname;
3382
		if ($this->GetInterface()) {
3383
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3384
		}
3385
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3386
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3387
		}
3388
		$tmpvalue = trim($this->GetQpriority());
3389
		if (is_numeric($tmpvalue)) {
3390
			$pfq_rule .= " priority " . $this->GetQpriority();
3391
		}
3392
		$tmpvalue = trim($this->GetQlimit());
3393
		if (!empty($tmpvalue)) {
3394
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3395
		}
3396
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3397
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3398
			$pfq_rule .= " fairq ( ";
3399
			$tmpvalue = trim($this->GetRed());
3400
			if (!empty($tmpvalue)) {
3401
				$comma = 1;
3402
				$pfq_rule .= " red ";
3403
			}
3404
			$tmpvalue = trim($this->GetCodel());
3405
			if (!empty($tmpvalue)) {
3406
				$comma = 1;
3407
				$pfq_rule .= " codel ";
3408
			}
3409
			$tmpvalue = trim($this->GetRio());
3410
			if (!empty($tmpvalue)) {
3411
				if ($comma) {
3412
					$pfq_rule .= " ,";
3413
				}
3414
				$comma = 1;
3415
				$pfq_rule .= " rio ";
3416
			}
3417
			$tmpvalue = trim($this->GetEcn());
3418
			if (!empty($tmpvalue)) {
3419
				if ($comma) {
3420
					$pfq_rule .= " ,";
3421
				}
3422
				$comma = 1;
3423
				$pfq_rule .= " ecn ";
3424
			}
3425
			$tmpvalue = trim($this->GetDefault());
3426
			if (!empty($tmpvalue)) {
3427
				if ($comma) {
3428
					$pfq_rule .= " ,";
3429
				}
3430
				$comma = 1;
3431
				$pfq_rule .= " default ";
3432
				$default = true;
3433
			}
3434
			$tmpvalue = trim($this->GetBuckets());
3435
			if (!empty($tmpvalue)) {
3436
				if ($comma) {
3437
					$pfq_rule .= ", ";
3438
				}
3439
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3440
			}
3441
			$tmpvalue = trim($this->GetHogs());
3442
			if (!empty($tmpvalue)) {
3443
				if ($comma) {
3444
					$pfq_rule .= ", ";
3445
				}
3446
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3447
			}
3448
			$pfq_rule .= " ) ";
3449
		}
3450

    
3451
		$pfq_rule .= " \n";
3452
		return $pfq_rule;
3453
	}
3454

    
3455
	function build_form() {
3456
		$form = parent::build_form();
3457

    
3458
		$section = new Form_Section('');
3459

    
3460
		$group = new Form_Group('Bandwidth');
3461

    
3462
		$group->add(new Form_Input(
3463
			'bandwidth',
3464
			null,
3465
			'number',
3466
			$this->GetBandwidth()
3467
		));
3468

    
3469
		$group->add(new Form_Select(
3470
			'bandwidthtype',
3471
			null,
3472
			$this->FormGetBwscale(),
3473
			array('Kb' => 'Kbit/s',
3474
				  'Mb' => 'Mbit/s',
3475
				  'Gb' => 'Gbit/s',
3476
				  'b' => 'Bit/s',
3477
				  '%' => '%')
3478
		));
3479

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

    
3482
		$section->add($group);
3483

    
3484
		$section->addInput(new Form_Input(
3485
			'buckets',
3486
			'Scheduler specific options',
3487
			'text',
3488
			$this->GetBuckets()
3489
		))->setHelp('Number of buckets available');
3490

    
3491
		$section->addInput(new Form_Input(
3492
			'hogs',
3493
			'',
3494
			'text',
3495
			$this->GetHogs()
3496
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3497

    
3498
		$form->add($section);
3499
		return $form;
3500
	}
3501

    
3502
	function update_altq_queue_data(&$data) {
3503
		$this->ReadConfig($data);
3504
	}
3505

    
3506
	function wconfig() {
3507
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3508
		if (!is_array($cflink)) {
3509
			$cflink = array();
3510
		}
3511
		$cflink['interface'] = $this->GetInterface();
3512
		$cflink['qlimit'] = trim($this->GetQlimit());
3513
		if (empty($cflink['qlimit'])) {
3514
			unset($cflink['qlimit']);
3515
		}
3516
		$cflink['priority'] = trim($this->GetQpriority());
3517
		if (!is_numeric($cflink['priority'])) {
3518
			unset($cflink['priority']);
3519
		}
3520
		$cflink['name'] = $this->GetQname();
3521
		$cflink['description'] = trim($this->GetDescription());
3522
		if (empty($cflink['description'])) {
3523
			unset($cflink['description']);
3524
		}
3525
		$cflink['bandwidth'] = $this->GetBandwidth();
3526
		$cflink['bandwidthtype'] = $this->GetBwscale();
3527
		$cflink['enabled'] = $this->GetEnabled();
3528
		if (empty($cflink['enabled'])) {
3529
			unset($cflink['enabled']);
3530
		}
3531
		$cflink['default'] = trim($this->GetDefault());
3532
		if (empty($cflink['default'])) {
3533
			unset($cflink['default']);
3534
		}
3535
		$cflink['red'] = trim($this->GetRed());
3536
		if (empty($cflink['red'])) {
3537
			unset($cflink['red']);
3538
		}
3539
		$cflink['rio'] = trim($this->GetRio());
3540
		if (empty($cflink['rio'])) {
3541
			unset($cflink['rio']);
3542
		}
3543
		$cflink['ecn'] = trim($this->GetEcn());
3544
		if (empty($cflink['ecn'])) {
3545
			unset($cflink['ecn']);
3546
		}
3547
		$cflink['codel'] = trim($this->GetCodel());
3548
		if (empty($cflink['codel'])) {
3549
			unset($cflink['codel']);
3550
		}
3551
		$cflink['buckets'] = trim($this->GetBuckets());
3552
		if (empty($cflink['buckets'])) {
3553
			unset($cflink['buckets']);
3554
		}
3555
		$cflink['hogs'] = trim($this->GetHogs());
3556
		if (empty($cflink['hogs'])) {
3557
			unset($cflink['hogs']);
3558
		}
3559
	}
3560
}
3561

    
3562

    
3563
/*
3564
 * dummynet(4) wrappers.
3565
 */
3566

    
3567

    
3568
/*
3569
 * List of respective objects!
3570
 */
3571
$dummynet_pipe_list = array();
3572

    
3573
class dummynet_class {
3574
	var $qname;
3575
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3576
	var $qlimit;
3577
	var $description;
3578
	var $qenabled;
3579
	var $link;
3580
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3581
	var $plr;
3582

    
3583
	var $buckets;
3584
	/* mask parameters */
3585
	var $mask;
3586
	var $noerror;
3587

    
3588
	/* Accessor functions */
3589
	function SetLink($link) {
3590
		$this->link = $link;
3591
	}
3592
	function GetLink() {
3593
		return $this->link;
3594
	}
3595
	function GetMask() {
3596
		if (!isset($this->mask["type"])) {
3597
			$this->mask["type"] = "none";
3598
		}
3599
		return $this->mask;
3600
	}
3601
	function SetMask($mask) {
3602
		$this->mask = $mask;
3603
	}
3604
	function &GetParent() {
3605
		return $this->qparent;
3606
	}
3607
	function SetParent(&$parent) {
3608
		$this->qparent = &$parent;
3609
	}
3610
	function GetEnabled() {
3611
		return $this->qenabled;
3612
	}
3613
	function SetEnabled($value) {
3614
		$this->qenabled = $value;
3615
	}
3616
	function CanHaveChildren() {
3617
		return false;
3618
	}
3619
	function CanBeDeleted() {
3620
		return true;
3621
	}
3622
	function GetQname() {
3623
		return $this->qname;
3624
	}
3625
	function SetQname($name) {
3626
		$this->qname = trim($name);
3627
	}
3628
	function GetQlimit() {
3629
		return $this->qlimit;
3630
	}
3631
	function SetQlimit($limit) {
3632
		$this->qlimit = $limit;
3633
	}
3634
	function GetDescription() {
3635
		return $this->description;
3636
	}
3637
	function SetDescription($str) {
3638
		$this->description = trim($str);
3639
	}
3640
	function GetFirstime() {
3641
		return $this->firsttime;
3642
	}
3643
	function SetFirsttime($number) {
3644
		$this->firsttime = $number;
3645
	}
3646
	function GetBuckets() {
3647
		return $this->buckets;
3648
	}
3649
	function SetBuckets($buckets) {
3650
		$this->buckets = $buckets;
3651
	}
3652
	function SetNumber($number) {
3653
		$this->qnumber = $number;
3654
	}
3655
	function GetNumber() {
3656
		return $this->qnumber;
3657
	}
3658
	function GetPlr() {
3659
		return $this->plr;
3660
	}
3661
	function SetPlr($plr) {
3662
		$this->plr = $plr;
3663
	}
3664

    
3665
	function build_javascript() {
3666
		$javascript .= "<script type=\"text/javascript\">\n";
3667
		$javascript .= "//<![CDATA[\n";
3668
		$javascript .= "function enable_maskbits(enable_over) {\n";
3669
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3670
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3671
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3672
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3673
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3674
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3675
		$javascript .= "} else {\n";
3676
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3677
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3678
		$javascript .= "}}\n";
3679
		$javascript .= "//]]>\n";
3680
		$javascript .= "</script>\n";
3681
		return $javascript;
3682
	}
3683

    
3684
	function validate_input($data, &$input_errors) {
3685
		$reqdfields[] = "bandwidth";
3686
		$reqdfieldsn[] = gettext("Bandwidth");
3687
		/*$reqdfields[] = "burst";
3688
		$reqdfieldsn[] = gettext("Burst"); */
3689
		$reqdfields[] = "bandwidthtype";
3690
		$reqdfieldsn[] = gettext("Bandwidthtype");
3691
		$reqdfields[] = "newname";
3692
		$reqdfieldsn[] = gettext("Name");
3693

    
3694
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3695

    
3696
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3697
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3698
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3699
		}
3700
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3701
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3702
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3703
		}
3704
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3705
			$input_errors[] = gettext("Queue limit must be an integer");
3706
		}
3707
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3708
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3709
		}
3710
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3711
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3712
		}
3713
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3714
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3715
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3716
			}
3717
		}
3718
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3719
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3720
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3721
			}
3722
		}
3723
	}
3724

    
3725
	function build_mask_rules(&$pfq_rule) {
3726
		$mask = $this->GetMask();
3727
		if (!empty($mask['type'])) {
3728
			if ($mask['type'] <> 'none') {
3729
				$pfq_rule .= " mask";
3730
			}
3731
			switch ($mask['type']) {
3732
				case 'srcaddress':
3733
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3734
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3735
					} else {
3736
						$pfq_rule .= " src-ip6 /128";
3737
					}
3738
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3739
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3740
					} else {
3741
						$pfq_rule .= " src-ip 0xffffffff";
3742
					}
3743
					break;
3744
				case 'dstaddress':
3745
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3746
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3747
					} else {
3748
						$pfq_rule .= " dst-ip6 /128";
3749
					}
3750
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3751
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3752
					} else {
3753
						$pfq_rule .= " dst-ip 0xffffffff";
3754
					}
3755
					break;
3756
				default:
3757
					break;
3758
			}
3759
		}
3760
	}
3761

    
3762
}
3763

    
3764
class dnpipe_class extends dummynet_class {
3765
	var $delay;
3766
	var $qbandwidth = array();
3767
	var $qbandwidthtype;
3768

    
3769
	/* Limiter queue patch */
3770
	var $ecn; // ecn 'on' or 'off'
3771
	var $pie_onoff;
3772
	var $pie_capdrop;
3773
	var $pie_qdelay;
3774
	var $pie_pderand;
3775
	var $aqm; // key to aqm_map
3776
	var $aqm_params = array(); // AQM params
3777
	var $scheduler;	// key to scheduler_map
3778
	var $scheduler_params = array(); // AQM params
3779
	function GetAQM() {
3780
			return $this->aqm;
3781
	}
3782
	function SetAQM($aqm) {
3783
			$this->aqm = $aqm;
3784
	}
3785
	function GetAQMParameters() {
3786
			return $this->aqm_params;
3787
	}
3788
	function GetAQMParameter($parameter) {
3789
			return $this->aqm_params[$parameter];
3790
	}
3791
	function SetAQMParameter($key, $value) {
3792
			return $this->aqm_params[$key] = $value;
3793
	}
3794
	function SetAQMParameters($params) {
3795
			$this->aqm_params = $params;
3796
	}
3797
	function GetECN() {
3798
			return $this->ecn;
3799
	}
3800
	function SetECN($ecn) {
3801
			$this->ecn = $ecn;
3802
	}
3803
	function GetPIE_ONOFF() {
3804
			return $this->pie_onoff;
3805
	}
3806
	function SetPIE_ONOFF($pie_onoff) {
3807
			$this->pie_onoff = $pie_onoff;
3808
	}
3809
	function GetPIE_CAPDROP() {
3810
			return $this->pie_capdrop;
3811
	}
3812
	function SetPIE_CAPDROP($pie_capdrop) {
3813
			$this->pie_capdrop = $pie_capdrop;
3814
	}
3815
	function GetPIE_QDELAY() {
3816
			return $this->pie_qdelay;
3817
	}
3818
	function SetPIE_QDELAY($pie_qdelay) {
3819
			$this->pie_qdelay = $pie_qdelay;
3820
	}
3821
	function GetPIE_PDERAND() {
3822
			return $this->pie_pderand;
3823
	}
3824
	function SetPIE_PDERAND($pie_pderand) {
3825
			$this->pie_pderand = $pie_pderand;
3826
	}
3827
	function GetScheduler() {
3828
			return $this->scheduler;
3829
	}
3830
	function SetScheduler($scheduler) {
3831
			$this->scheduler = $scheduler;
3832
	}
3833
	function GetSchedulerParameters() {
3834
			return $this->scheduler_params;
3835
	}
3836
	function SetSchedulerParameters($params) {
3837
			$this->scheduler_params = $params;
3838
	}
3839
	function SetSchedulerParameter($key, $value) {
3840
			$this->scheduler_params[$key] = $value;
3841
	}
3842
	function GetSchedulerParameter($key) {
3843
			return $this->scheduler_params[$key];
3844
	}
3845
	/* End limiter queue patch */
3846

    
3847
		/* This is here to help on form building and building rules/lists */
3848
	var $subqueues = array();
3849

    
3850
	function CanHaveChildren() {
3851
		return true;
3852
	}
3853
	function SetDelay($delay) {
3854
		$this->delay = $delay;
3855
	}
3856
	function GetDelay() {
3857
		return $this->delay;
3858
	}
3859
	function delete_queue() {
3860
		cleanup_dnqueue_from_rules($this->GetQname());
3861
		foreach ($this->subqueues as $q) {
3862
			$q->delete_queue();
3863
		}
3864
		unset_dn_object_by_reference($this->GetLink());
3865
		mwexec("/sbin/dnctl pipe delete " . $this->GetNumber());
3866
		mwexec("/sbin/dnctl sched delete " . $this->GetNumber());
3867
	}
3868
	function GetBandwidth() {
3869
		return $this->qbandwidth;
3870
	}
3871
	function SetBandwidth($bandwidth) {
3872
		$this->qbandwidth = $bandwidth;
3873
	}
3874
	function GetBurst() {
3875
		return $this->qburst;
3876
	}
3877
	function SetBurst($burst) {
3878
		$this->qburst = $burst;
3879
	}
3880

    
3881
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3882

    
3883
		if (!is_array($this->subqueues)) {
3884
			$this->subqueues = array();
3885
		}
3886

    
3887
		$__tmp_q = new dnqueue_class(); $q =& $__tmp_q;
3888
		$q->SetLink($path);
3889
		$q->SetEnabled("on");
3890
		$q->SetPipe($this->GetQname());
3891
		$q->SetParent($this);
3892
		$q->ReadConfig($queue);
3893
		$q->validate_input($queue, $input_errors);
3894

    
3895
		if (!is_array($input_errors)) {
3896
			$input_errors = array();
3897
		}
3898

    
3899
		if (count($input_errors)) {
3900
			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)));
3901
			return $q;
3902
		}
3903
		$number = dnqueue_find_nextnumber();
3904
		$q->SetNumber($number);
3905
		$this->subqueues[$q->GetQname()] = &$q;
3906

    
3907
		return $q;
3908
	}
3909

    
3910
	function &get_queue_list(&$q = null) {
3911
		$qlist = array();
3912

    
3913
		$qlist[$this->GetQname()] = $this->GetNumber();
3914
		if (is_array($this->subqueues)) {
3915
			foreach ($this->subqueues as $queue) {
3916
				$queue->get_queue_list($qlist);
3917
			}
3918
		}
3919
		return $qlist;
3920
	}
3921

    
3922
	/*
3923
	 * Should search even its children
3924
	 */
3925
	function &find_queue($pipe, $qname) {
3926
		if ($qname == $this->GetQname()) {
3927
			return $this;
3928
		}
3929
		foreach ($this->subqueues as $q) {
3930
			$result =& $q->find_queue("", $qname);
3931
			if ($result) {
3932
				return $result;
3933
			}
3934
		}
3935
	}
3936

    
3937
	function &find_parentqueue($pipe, $qname) {
3938
		return NULL;
3939
	}
3940

    
3941
	function validate_input($data, &$input_errors) {
3942
		parent::validate_input($data, $input_errors);
3943

    
3944
		$schedule = 0;
3945
		$schedulenone = 0;
3946
		$entries = 0;
3947
		/* XXX: Really no better way? */
3948
		for ($i = 0; $i < 2900; $i++) {
3949
			if (!empty($data["bwsched{$i}"])) {
3950
				if ($data["bwsched{$i}"] != "none") {
3951
					$schedule++;
3952
				} else {
3953
					$schedulenone++;
3954
				}
3955
			}
3956
			if (!empty($data["bandwidth{$i}"])) {
3957
				if (!is_numeric($data["bandwidth{$i}"])) {
3958
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3959
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3960
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3961
				} else {
3962
					$entries++;
3963
				}
3964
			}
3965
		}
3966
		if ($schedule == 0 && $entries > 1) {
3967
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3968
		}
3969
		if ($schedulenone > 0 && $entries > 1) {
3970
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3971
		}
3972
		if ($entries == 0) {
3973
			$input_errors[] = gettext("At least one bw specification is necessary.");
3974
		}
3975
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3976
			$input_errors[] = gettext("Delay must be an integer.");
3977
		}
3978
		if ($data['delay'] && is_numeric($data['delay']) &&
3979
		    (($data['delay'] < 0) || ($data['delay'] > 10000))) {
3980
			$input_errors[] = gettext("Delay must be an integer between 0 and 10000.");
3981
		}
3982

    
3983
		/* Limiter patch */
3984
		$selectedScheduler = getSchedulers()[$data['sched']];
3985
		if (!$selectedScheduler) {
3986
			$input_errors[] = gettext("Selected scheduler not recognized.");
3987
		}
3988
		$selectedAqm = getAQMs()[$data['aqm']];
3989
		if (!empty($data['aqm']) && !$selectedAqm) {
3990
			$input_errors[] = gettext("Selected AQM not recognized.");
3991
		}
3992
		/* End limiter patch */
3993
	}
3994

    
3995
	function ReadConfig(&$q) {
3996
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3997
			$this->SetQname($q['newname']);
3998
			rename_dnqueue_in_rules($q['name'], $q['newname']);
3999
		} else if (!empty($q['newname'])) {
4000
			$this->SetQname($q['newname']);
4001
		} else {
4002
			$this->SetQname($q['name']);
4003
		}
4004
		$this->SetNumber($q['number']);
4005

    
4006
		if (!empty($_POST)) {
4007
			$bandwidth = array();
4008
			/* XXX: Really no better way? */
4009
			for ($i = 0; $i < 2900; $i++) {
4010
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
4011
					$bw = array();
4012
					$bw['bw'] = $q["bandwidth{$i}"];
4013
					$bw['burst'] = $q["burst{$i}"];
4014
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
4015
						$bw['bwscale'] = $q["bwtype{$i}"];
4016
					}
4017
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
4018
						$bw['bwsched'] = $q["bwsched{$i}"];
4019
					}
4020
					$bandwidth[] = $bw;
4021
				}
4022
			}
4023
			$this->SetBandwidth($bandwidth);
4024
		}
4025

    
4026
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
4027
			$this->SetBandwidth($q['bandwidth']['item']);
4028
			$this->SetBurst($q['burst']['item']);
4029
		}
4030

    
4031
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4032
			$this->SetQlimit($q['qlimit']);
4033
		} else {
4034
			$this->SetQlimit("");
4035
		}
4036
		if (isset($q['mask']) && $q['mask'] <> "") {
4037
			$masktype = $q['mask'];
4038
		} else {
4039
			$masktype = "";
4040
		}
4041
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4042
			$maskbits = $q['maskbits'];
4043
		} else {
4044
			$maskbits = "";
4045
		}
4046
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4047
			$maskbitsv6 = $q['maskbitsv6'];
4048
		} else {
4049
			$maskbitsv6 = "";
4050
		}
4051
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4052
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4053
			$this->SetBuckets($q['buckets']);
4054
		} else {
4055
			$this->SetBuckets("");
4056
		}
4057
		if (isset($q['plr']) && $q['plr'] <> "") {
4058
			$this->SetPlr($q['plr']);
4059
		} else {
4060
			$this->SetPlr("");
4061
		}
4062
		if (isset($q['delay']) && $q['delay'] <> "") {
4063
			$this->SetDelay($q['delay']);
4064
		} else {
4065
			$this->SetDelay(0);
4066
		}
4067
		if (isset($q['description']) && $q['description'] <> "") {
4068
			$this->SetDescription($q['description']);
4069
		} else {
4070
			$this->SetDescription("");
4071
		}
4072
		$this->SetEnabled($q['enabled']);
4073

    
4074
		/* Limiter patch */
4075
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4076
				$this->SetAQM($q['aqm']);
4077
		} else {
4078
				$this->SetAQM('droptail');
4079
		}
4080
		// parse AQM arguments
4081
		$aqm_map = getAQMs();
4082
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4083
		if (is_array($selectedParameters)) {
4084
			foreach ($selectedParameters as $key => $value) {
4085
				$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4086
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4087
					$this->SetAQMParameter($key, $q[$config_key]);
4088
				} else {
4089
					$this->SetAQMParameter($key, $value["default"]);
4090
				}
4091
			}
4092
		}
4093

    
4094
		if (isset($q['sched']) && $q['sched'] <> "") {
4095
				$this->SetScheduler($q['sched']);
4096
		} else {
4097
				$this->SetScheduler('fifo');
4098
		}
4099
		$scheduler_map = getSchedulers();
4100
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4101
		if (is_array($selectedParameters)) {
4102
			foreach ($selectedParameters as $key => $value) {
4103
				$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4104
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4105
					$this->SetSchedulerParameter($key, $q[$config_key]);
4106
				} else {
4107
					$this->SetSchedulerParameter($key, $value["default"]);
4108
				}
4109
			}
4110
		}
4111

    
4112
		// ecn flag
4113
		$this->SetECN($q['ecn']);
4114
		// PIE Flags.
4115
		$this->SetPIE_ONOFF($q['pie_onoff']);
4116
		$this->SetPIE_CAPDROP($q['pie_capdrop']);
4117
		$this->SetPIE_QDELAY($q['pie_qdelay']);
4118
		$this->SetPIE_PDERAND($q['pie_pderand']);
4119
		/* End limiter patch */
4120
	}
4121

    
4122
	function build_tree() {
4123
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($this->GetQname()) ."&amp;queue=".htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4124
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4125
		if (is_array($this->subqueues)) {
4126
			$tree .= "<ul>";
4127
			foreach ($this->subqueues as $q) {
4128
				$tree .= $q->build_tree();
4129
			}
4130
			$tree .= "</ul>";
4131
		}
4132
		$tree .= "</li>";
4133

    
4134
		return $tree;
4135
	}
4136

    
4137
	function build_rules() {
4138
		global $config, $time_based_rules;
4139

    
4140
		if ($this->GetEnabled() == "") {
4141
			return;
4142
		}
4143

    
4144
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
4145
		$found = false;
4146
		$bandwidth = $this->GetBandwidth();
4147
		if (is_array($bandwidth)) {
4148
			foreach ($bandwidth as $bw) {
4149
				if ($bw['bwsched'] != "none") {
4150
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4151
						foreach ($config['schedules']['schedule'] as $schedule) {
4152
							if ($bw['bwsched'] == $schedule['name']) {
4153
								if (filter_get_time_based_rule_status($schedule)) {
4154
									/* pipe throughputs must always be an integer, enforce that restriction again here. */
4155
									$pfq_rule .= " bw ".round(trim($bw['bw']),0).$bw['bwscale'];
4156
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4157
										$pfq_rule .= " burst ".trim($bw['burst']);
4158
									}
4159
									$found = true;
4160
									break;
4161
								}
4162
							}
4163
						}
4164
					} else {
4165
						$pfq_rule .= " bw 0";
4166
						$found = true;
4167
						break;
4168
					}
4169
				} else {
4170
					/* pipe throughputs must always be an integer, enforce that restriction again here. */
4171
					$pfq_rule .= " bw ".round(trim($bw['bw']), 0).$bw['bwscale'];
4172
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4173
						$pfq_rule .= " burst ".trim($bw['burst']);
4174
					}
4175
					$found = true;
4176
					break;
4177
				}
4178
			}
4179
			if ($found == false) {
4180
				$pfq_rule .= " bw 0";
4181
			}
4182
		} else {
4183
			$pfq_rule .= " bw 0";
4184
		}
4185

    
4186
		if ($this->GetQlimit()) {
4187
			$pfq_rule .= " queue " . $this->GetQlimit();
4188
		}
4189
		if ($this->GetPlr()) {
4190
			$pfq_rule .= " plr " . $this->GetPlr();
4191
		}
4192
		if ($this->GetBuckets()) {
4193
			$pfq_rule .= " buckets " . $this->GetBuckets();
4194
		}
4195
		if ($this->GetDelay()) {
4196
			$pfq_rule .= " delay " . $this->GetDelay();
4197
		}
4198
		$this->build_mask_rules($pfq_rule);
4199

    
4200
		/* Limiter patch */
4201
		$selectedAQM = getAQMs()[$this->getAQM()];
4202
		if ($selectedAQM) {
4203
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4204
			if ($selectedAQM["ecn"]) {
4205
				if ($this->getECN() == 'on') {
4206
					$pfq_rule .= ' ecn';
4207
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4208
					$pfq_rule .= ' noecn';
4209
				}
4210
			}
4211
		}
4212
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4213
		if ($selectedScheduler) {
4214
			$pfq_rule .= "\nsched ". $this->GetNumber() . " config ";
4215
			$pfq_rule .= "pipe ". $this->GetNumber();
4216
			$this->build_mask_rules($pfq_rule);
4217
			$pfq_rule .= " " . FormatParameters($selectedScheduler["parameter_format"], $this->GetSchedulerParameters());			
4218
			if ($selectedScheduler["ecn"]) {
4219
				if ($this->getECN() == 'on') {
4220
					$pfq_rule .= ' ecn';
4221
				} else {
4222
					$pfq_rule .= ' noecn';
4223
				}
4224
			}
4225
			if ($selectedScheduler["pie_onoff"]) {
4226
				if ($this->getPIE_ONOFF() == 'on') {
4227
					$pfq_rule .= 'onoff';
4228
				} else {
4229
					$pfq_rule .= '';
4230
				}
4231
			}
4232
			if ($selectedScheduler["pie_capdrop"]) {
4233
				if ($this->getPIE_CAPDROP() == 'on') {
4234
					$pfq_rule .= ' capdrop';
4235
				} else {
4236
					$pfq_rule .= ' nocapdrop';
4237
				}
4238
			}
4239
			if ($selectedScheduler["pie_qdelay"]) {
4240
				if ($this->getPIE_QDELAY() == 'on') {
4241
					$pfq_rule .= ' ts';
4242
				} else {
4243
					$pfq_rule .= ' dre';
4244
				}
4245
			}
4246
			if ($selectedScheduler["pie_pderand"]) {
4247
				if ($this->getPIE_PDERAND() == 'on') {
4248
					$pfq_rule .= ' derand';
4249
				} else {
4250
					$pfq_rule .= ' noderand';
4251
				}
4252
			}
4253
		}
4254
		$pfq_rule .= "\n";
4255
		/* End patch */
4256

    
4257
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
4258
			foreach ($this->subqueues as $q) {
4259
				$pfq_rule .= $q->build_rules();
4260
			}
4261
		}
4262

    
4263
		$pfq_rule .= " \n";
4264

    
4265
		return $pfq_rule;
4266
	}
4267

    
4268
	function update_dn_data(&$data) {
4269
		$this->ReadConfig($data);
4270
	}
4271

    
4272
	function build_javascript() {
4273
		global $g, $config;
4274

    
4275
		$javasr = parent::build_javascript();
4276

    
4277
		//build list of schedules
4278
		$schedules = "<option value='none'>none</option>";
4279
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4280
			foreach ($config['schedules']['schedule'] as $schedule) {
4281
				if ($schedule['name'] <> "") {
4282
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
4283
				}
4284
			}
4285
		}
4286
		$bwopt = "";
4287
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
4288
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
4289
		}
4290

    
4291
		$javasr .= <<<EOD
4292
<script type='text/javascript'>
4293
//<![CDATA[
4294
var addBwRowTo = (function() {
4295

    
4296
	return (function (tableId) {
4297

    
4298
	var table = document.getElementById(tableId);
4299
	var totalrows = table.rows.length -1;
4300

    
4301
	var row = table.insertRow(totalrows + 1);
4302
	var cell1 = row.insertCell(0);
4303
	var cell2 = row.insertCell(1);
4304
	var cell3 = row.insertCell(2);
4305
	var cell4 = row.insertCell(3);
4306

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

    
4312
	});
4313
})();
4314

    
4315
function removeBwRow(el) {
4316
	var d = el.parentNode.parentNode.rowIndex;
4317
	document.getElementById('maintable').deleteRow(d);
4318
}
4319

    
4320
function ceil_func(el){
4321
	el.value = Math.ceil(el.value);
4322

    
4323
}
4324
//]]>
4325
</script>
4326

    
4327
EOD;
4328

    
4329
		return $javasr;
4330
	}
4331

    
4332
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
4333
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
4334
	// with the javascript in this class
4335
	function build_bwtable() {
4336
		global $config;
4337

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

    
4350
		$form = '<div class="table-responsive">';
4351
		$form .= '<table id="maintable" class="table table-hover table-striped">';
4352
		$form .= "<thead><tr>";
4353
		$form .= "<th>Bandwidth</th>";
4354
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
4355
		$form .= "<th>Bw type</th>";
4356
		$form .= "<th>Schedule</th>";
4357
		$form .= "<th></th>";
4358
		$form .= "</tr></thead>";
4359
		$form .= "<tbody>";
4360

    
4361
		// If there are no bandwidths defined, make a blank one for convenience
4362
		if (empty($bandwidth)) {
4363
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Mb', 'bwsched' => 'none'));
4364
		}
4365

    
4366
		if (is_array($bandwidth)) {
4367
			foreach ($bandwidth as $bwidx => $bw) {
4368
				$form .= '<tr>';
4369
				$form .= '<td class="col-xs-4">';
4370
				$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\"/>";
4371
				//$form .= "</td><td width='20%'>";
4372
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
4373
				$form .= "</td>";
4374
				$form .= '<td class="col-xs-4">';
4375
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
4376

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

    
4380
					if ($bw['bwscale'] == $bwsidx) {
4381
						$form .= " selected";
4382
					}
4383

    
4384
					$form .= ">{$bwscale}</option>";
4385
				}
4386

    
4387
				$form .= "</select>";
4388
				$form .= "</td>";
4389
				$form .= '<td class="col-xs-4">';
4390
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
4391

    
4392
				foreach ($schedules as $schd) {
4393
					$selected = "";
4394
					if ($bw['bwsched'] == $schd) {
4395
						$selected = "selected";
4396
					}
4397

    
4398
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
4399
				}
4400

    
4401
				$form .= "</select>";
4402
				$form .= "</td>";
4403
				$form .= '<td>';
4404
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
4405
				$form .= "</td></tr>";
4406
			}
4407
		}
4408
		$form .= "</tbody></table></div><br />";
4409

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

    
4414
		return($form);
4415
	}
4416

    
4417
	function build_form() {
4418
		global $g, $config, $pipe, $action, $qname;
4419

    
4420
		//build list of schedules
4421
		$schedules = array();
4422
		$schedules[] = "none";//leave none to leave rule enabled all the time
4423
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4424
			foreach ($config['schedules']['schedule'] as $schedule) {
4425
				if ($schedule['name'] <> "") {
4426
					$schedules[] = $schedule['name'];
4427
				}
4428
			}
4429
		}
4430

    
4431

    
4432
		$sform = new Form();
4433
		$sform->setAction("firewall_shaper.php");
4434

    
4435
		$section = new Form_Section('Limiters');
4436

    
4437
		$section->addInput(new Form_Checkbox(
4438
			'enabled',
4439
			'Enable',
4440
			'Enable limiter and its children',
4441
			($this->GetEnabled() == "on"),
4442
			'on'
4443
		));
4444

    
4445
		$section->addInput(new Form_Input(
4446
			'newname',
4447
			'*Name',
4448
			'text',
4449
			$this->GetQname()
4450
		));
4451

    
4452
		$section->addInput(new Form_Input(
4453
			'name',
4454
			null,
4455
			'hidden',
4456
			$this->GetQname()
4457
		));
4458

    
4459
		if ($this->GetNumber() > 0) {
4460
			$section->addInput(new Form_Input(
4461
				'number',
4462
				null,
4463
				'hidden',
4464
				$this->GetNumber()
4465
			));
4466
		}
4467

    
4468
		$bandwidth = $this->GetBandwidth();
4469

    
4470
		if (is_array($bandwidth)) {
4471
				$section->addInput(new Form_StaticText(
4472
				'Bandwidth',
4473
				$this->build_bwtable()
4474
			));
4475
		}
4476

    
4477
		$mask = $this->GetMask();
4478

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

    
4488
		$group = new Form_Group(null);
4489

    
4490
		$group->add(new Form_Select(
4491
			'maskbits',
4492
			null,
4493
			$mask['bits'],
4494
			array_combine(range(32, 1, -1), range(32, 1, -1))
4495
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4496

    
4497
		$group->add(new Form_Select(
4498
			'maskbitsv6',
4499
			null,
4500
			$mask['bitsv6'],
4501
			array_combine(range(128, 1, -1), range(128, 1, -1))
4502
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4503

    
4504
		$section->add($group);
4505

    
4506
		$section->addInput(new Form_Input(
4507
			'description',
4508
			'Description',
4509
			'text',
4510
			$this->GetDescription()
4511
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4512

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

    
4515
		/* Begin limiter patch */
4516
		$aqm_map = getAQMs();
4517
		$scheduler_map = getSchedulers();
4518

    
4519
		$section = new Form_Section('Queue');
4520
		$section->addInput(new Form_Select(
4521
				'aqm',
4522
				'Queue Management Algorithm',
4523
				$this->GetAQM(),
4524
				array_map_assoc(function ($k, $v) {
4525
					return [$k, $v["name"]];
4526
				}, $aqm_map)
4527
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside the limiter, ' .
4528
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4529
						'network congestion.');
4530

    
4531
		$section->addInput(new Form_StaticText(
4532
			'',
4533
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4534
		))->setHelp('Specifies the queue management algorithm parameters.');
4535

    
4536
		$section->addInput(new Form_Select(
4537
				'sched',
4538
				'Scheduler',
4539
				$this->GetScheduler(),
4540
				array_map_assoc(function ($k, $v) {
4541
					return [$k, $v["name"]];
4542
				}, $scheduler_map)
4543
		))->setHelp('The scheduler manages the sequence of network packets in the limiter\'s queue.');
4544

    
4545
		$section->addInput(new Form_StaticText(
4546
			'',
4547
			build_queue_params($scheduler_map, $this->GetScheduler(), $this->GetSchedulerParameters(), "sched")
4548
		))->setHelp('Specifies the scheduler parameters.');
4549

    
4550
		$section->addInput(new Form_Input(
4551
				'qlimit',
4552
				'Queue length',
4553
				'number',
4554
				$this->GetQlimit()
4555
		))->setHelp('Specifies the length of the limiter\'s queue, which the scheduler and AQM are responsible for. ' .
4556
			'This field may be left empty.');
4557

    
4558
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4559

    
4560
		if ($selectedScheduler["ecn"]) {
4561
			$section->addInput(new Form_Checkbox(
4562
				'ecn',
4563
				'ECN',
4564
				'Enable Explicit Congestion Notification (ECN)',
4565
				($this->GetECN() == "on"),
4566
				'on'
4567
			))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4568
		}
4569

    
4570
		if ($selectedScheduler["pie_onoff"]) {
4571
			$section->addInput(new Form_Checkbox(
4572
				'pie_onoff',
4573
				'ONOFF',
4574
				'Enable Onoff (onoff,)',
4575
				($this->GetPIE_ONOFF() == "on"),
4576
				'on'
4577
                	))->setHelp('Enable turning PIE on and off depending on queue load.');
4578
		}
4579

    
4580
		if ($selectedScheduler["pie_capdrop"]) {
4581
			$section->addInput(new Form_Checkbox(
4582
				'pie_capdrop',
4583
				'CAPDROP',
4584
				'Enable Capdrop (capdrop,nocapdrop)',
4585
				($this->GetPIE_CAPDROP() == "on"),
4586
				'on'
4587
			))->setHelp('Enable cap drop adjustment.');
4588
		}
4589

    
4590
		if ($selectedScheduler["pie_qdelay"]) {
4591
                        $section->addInput(new Form_Checkbox(
4592
                                'pie_qdelay',
4593
                                'QUEUE DELAY TYPE',
4594
                                'Enable Type Of Qdelay (ts,dre)',
4595
                                ($this->GetPIE_QDELAY() == "on"),
4596
                                'on'
4597
                        ))->setHelp('Set queue delay type to timestamps (checked) or departure rate estimation (unchecked).');
4598
                }
4599

    
4600
		if ($selectedScheduler["pie_pderand"]) {
4601
			$section->addInput(new Form_Checkbox(
4602
				'pie_pderand',
4603
				'PROB DERAND',
4604
				'Enable Drop Probability De-randomisation (derand,noderand)',
4605
				($this->GetPIE_PDERAND() == "on"),
4606
				'on'
4607
			))->setHelp('Enable (checked) or disable (unchecked) drop probability de-randomisation.');
4608
		}
4609

    
4610
		$sform->add($section);
4611
		/* End limiter patch */
4612

    
4613
		$section = new Form_Section('Advanced Options');
4614

    
4615
		$section->addInput(new Form_Input(
4616
			'delay',
4617
			'Delay (ms)',
4618
			'text',
4619
			$this->GetDelay() > 0 ? $this->GetDelay():null
4620
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4621

    
4622
		$section->addInput(new Form_Input(
4623
			'plr',
4624
			'Packet Loss Rate',
4625
			'number',
4626
			$this->GetPlr(),
4627
			['step' => '0.001', 'min' => '0.000']
4628
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4629
					'A value of 0.001 means one packet in 1000 gets dropped.');
4630

    
4631
		$section->addInput(new Form_Input(
4632
			'buckets',
4633
			'Bucket size (slots)',
4634
			'number',
4635
			$this->GetBuckets()
4636
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4637

    
4638
		$sform->add($section);
4639

    
4640
		return($sform);
4641
		}
4642

    
4643
	function wconfig() {
4644
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4645
		if (!is_array($cflink)) {
4646
			$cflink = array();
4647
		}
4648
		$cflink['name'] = $this->GetQname();
4649
		$cflink['number'] = $this->GetNumber();
4650
		$cflink['qlimit'] = $this->GetQlimit();
4651
		$cflink['plr'] = $this->GetPlr();
4652
		$cflink['description'] = $this->GetDescription();
4653

    
4654
		$bandwidth = $this->GetBandwidth();
4655
		if (is_array($bandwidth)) {
4656
			$cflink['bandwidth'] = array();
4657
			$cflink['bandwidth']['item'] = array();
4658
			foreach ($bandwidth as $bwidx => $bw) {
4659
				$cflink['bandwidth']['item'][] = $bw;
4660
			}
4661
		}
4662

    
4663
		$cflink['enabled'] = $this->GetEnabled();
4664
		$cflink['buckets'] = $this->GetBuckets();
4665
		$mask = $this->GetMask();
4666
		$cflink['mask'] = $mask['type'];
4667
		$cflink['maskbits'] = $mask['bits'];
4668
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4669
		$cflink['delay'] = $this->GetDelay();
4670

    
4671
		/* Limiter queue patch */
4672
		$cflink['sched'] = $this->GetScheduler();
4673
		$scheduler_map = getSchedulers();
4674
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4675
		foreach ($selectedParameters as $key => $value) {
4676
			$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4677
			$cflink[$config_key] = $this->GetSchedulerParameter($key);
4678
		}
4679
		$cflink['aqm'] = $this->GetAQM();
4680
		$aqm_map = GetAQMs();
4681
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4682
		foreach ($selectedParameters as $key => $value) {
4683
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4684
			$cflink[$config_key] = $this->GetAQMParameter($key);
4685
		}
4686
		$cflink['ecn'] = $this->GetECN();
4687

    
4688
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4689
		if ($selectedScheduler["pie_onoff"]) {
4690
			$cflink['pie_onoff'] = $this->GetPIE_ONOFF();
4691
		}
4692
		if ($selectedScheduler["pie_capdrop"]) {
4693
			$cflink['pie_capdrop'] = $this->GetPIE_CAPDROP();
4694
		}
4695
		if ($selectedScheduler["pie_qdelay"]) {
4696
			$cflink['pie_qdelay'] = $this->GetPIE_QDELAY();
4697
		}
4698
		if ($selectedScheduler["pie_pderand"]) {
4699
			$cflink['pie_pderand'] = $this->GetPIE_PDERAND();
4700
		}
4701
		/* End limiter queue patch */
4702
	}
4703

    
4704
}
4705

    
4706
class dnqueue_class extends dummynet_class {
4707
	var $pipeparent;
4708
	var $weight;
4709
	/* Limiter queue patch */
4710
    var $ecn; // ecn 'on' or 'off'
4711
	var $pie_onoff;
4712
	var $pie_capdrop;
4713
	var $pie_qdelay;
4714
	var $pie_pderand;
4715
    var $aqm; // key to aqm_map
4716
    var $aqm_params = array(); // AQM params
4717
	function GetAQM() {
4718
			return $this->aqm;
4719
	}
4720
	function SetAQM($aqm) {
4721
			$this->aqm = $aqm;
4722
	}
4723
	function GetAQMParameters() {
4724
			return $this->aqm_params;
4725
	}
4726
	function GetAQMParameter($parameter) {
4727
			return $this->aqm_params[$parameter];
4728
	}
4729
	function SetAQMParameter($key, $value) {
4730
			return $this->aqm_params[$key] = $value;
4731
	}
4732
	function SetAQMParameters($params) {
4733
			$this->aqm_params = $params;
4734
	}
4735
	function GetECN() {
4736
			return $this->ecn;
4737
	}
4738
	function SetECN($ecn) {
4739
			$this->ecn = $ecn;
4740
	}
4741
	function GetPIE_ONOFF() {
4742
			return $this->pie_onoff;
4743
	}
4744
	function SetPIE_ONOFF($pie_onoff) {
4745
			$this->pie_onoff = $pie_onoff;
4746
	}
4747
	function GetPIE_CAPDROP() {
4748
			return $this->pie_capdrop;
4749
	}
4750
	function SetPIE_CAPDROP($pie_capdrop) {
4751
			$this->pie_capdrop = $pie_capdrop;
4752
	}
4753
	function GetPIE_QDELAY() {
4754
			return $this->pie_qdelay;
4755
	}
4756
	function SetPIE_QDELAY($pie_qdelay) {
4757
			$this->pie_qdelay = $pie_qdelay;
4758
	}
4759
	function GetPIE_PDERAND() {
4760
			return $this->pie_pderand;
4761
	}
4762
	function SetPIE_PDERAND($pie_pderand) {
4763
			$this->pie_pderand = $pie_pderand;
4764
	}
4765
	/* End limiter queue patch */
4766

    
4767
	function GetWeight() {
4768
		return $this->weight;
4769
	}
4770
	function SetWeight($weight) {
4771
		$this->weight = $weight;
4772
	}
4773
	function GetPipe() {
4774
		return $this->pipeparent;
4775
	}
4776
	function SetPipe($pipe) {
4777
		$this->pipeparent = $pipe;
4778
	}
4779

    
4780
	/* Just a stub in case we ever try to call this from the frontend. */
4781
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4782
		return;
4783
	}
4784

    
4785
	function delete_queue() {
4786
		cleanup_dnqueue_from_rules($this->GetQname());
4787
		unset_dn_object_by_reference($this->GetLink());
4788
		mwexec("/sbin/dnctl queue delete " . $this->GetNumber());
4789
	}
4790

    
4791
	function validate_input($data, &$input_errors) {
4792
		parent::validate_input($data, $input_errors);
4793

    
4794

    
4795
		/* Limiter patch */
4796
		$selectedAqm = getAQMs()[$data['aqm']];
4797
		if (!empty($data['aqm']) && !$selectedAqm) {
4798
			$input_errors[] = gettext("Selected AQM not recognized.");
4799
		}
4800
		/* End limiter patch */
4801

    
4802
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4803
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4804
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4805
		}
4806
	}
4807

    
4808
	/*
4809
	 * Should search even its children
4810
	 */
4811
	function &find_queue($pipe, $qname) {
4812
		if ($qname == $this->GetQname()) {
4813
			return $this;
4814
		} else {
4815
			return NULL;
4816
		}
4817
	}
4818

    
4819
	function &find_parentqueue($pipe, $qname) {
4820
		return $this->qparent;
4821
	}
4822

    
4823
	function &get_queue_list(&$qlist) {
4824
		if ($this->GetEnabled() == "") {
4825
			return;
4826
		}
4827
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4828
	}
4829

    
4830
	function ReadConfig(&$q) {
4831
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4832
			$this->SetQname($q['newname']);
4833
		} else if (!empty($q['newname'])) {
4834
			$this->SetQname($q['newname']);
4835
		} else {
4836
			$this->SetQname($q['name']);
4837
		}
4838
		$this->SetNumber($q['number']);
4839
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4840
			$this->SetQlimit($q['qlimit']);
4841
		} else {
4842
			$this->SetQlimit("");
4843
		}
4844
		if (isset($q['mask']) && $q['mask'] <> "") {
4845
			$masktype = $q['mask'];
4846
		} else {
4847
			$masktype = "";
4848
		}
4849
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4850
			$maskbits = $q['maskbits'];
4851
		} else {
4852
			$maskbits = "";
4853
		}
4854
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4855
			$maskbitsv6 = $q['maskbitsv6'];
4856
		} else {
4857
			$maskbitsv6 = "";
4858
		}
4859
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4860
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4861
			$this->SetBuckets($q['buckets']);
4862
		} else {
4863
			$this->SetBuckets("");
4864
		}
4865
		if (isset($q['plr']) && $q['plr'] <> "") {
4866
			$this->SetPlr($q['plr']);
4867
		} else {
4868
			$this->SetPlr("");
4869
		}
4870
		if (isset($q['weight']) && $q['weight'] <> "") {
4871
			$this->SetWeight($q['weight']);
4872
		} else {
4873
			$this->SetWeight("");
4874
		}
4875
		if (isset($q['description']) && $q['description'] <> "") {
4876
			$this->SetDescription($q['description']);
4877
		} else {
4878
			$this->SetDescription("");
4879
		}
4880
		$this->SetEnabled($q['enabled']);
4881

    
4882
		/* Limiter patch */
4883
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4884
				$this->SetAQM($q['aqm']);
4885
		} else {
4886
				$this->SetAQM('droptail');
4887
		}
4888
		// parse AQM arguments
4889
		$aqm_map = getAQMs();
4890
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4891
		foreach ($selectedParameters as $key => $value) {
4892
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4893
			if (isset($q[$config_key]) && $q[$config_key] <> "") {
4894
				$this->SetAQMParameter($key, $q[$config_key]);
4895
			} else {
4896
				$this->SetAQMParameter($key, $value["default"]);
4897
			}
4898
		}
4899

    
4900
		// ecn flag
4901
		$this->SetECN($q['ecn']);
4902
		// PIE Flags.
4903
		$this->SetPIE_ONOFF($q['pie_onoff']);
4904
		$this->SetPIE_CAPDROP($q['pie_capdrop']);
4905
		$this->SetPIE_QDELAY($q['pie_qdelay']);
4906
		$this->SetPIE_PDERAND($q['pie_pderand']);
4907
		/* End limiter patch */
4908
	}
4909

    
4910
	function build_tree() {
4911
		$parent =& $this->GetParent();
4912
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($parent->GetQname()) ."&amp;queue=" . htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4913
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4914
		$tree .= "</li>";
4915

    
4916
		return $tree;
4917
	}
4918

    
4919
	function build_rules() {
4920
		if ($this->GetEnabled() == "") {
4921
			return;
4922
		}
4923

    
4924
		$parent =& $this->GetParent();
4925
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4926
		if ($this->GetQlimit()) {
4927
			$pfq_rule .= " queue " . $this->GetQlimit();
4928
		}
4929
		if ($this->GetWeight()) {
4930
			$pfq_rule .= " weight " . $this->GetWeight();
4931
		}
4932
		if ($this->GetBuckets()) {
4933
			$pfq_rule .= " buckets " . $this->GetBuckets();
4934
		}
4935
		$this->build_mask_rules($pfq_rule);
4936

    
4937
		/* Limiter patch */
4938
		$selectedAQM = getAQMs()[$this->getAQM()];
4939
		if ($selectedAQM) {
4940
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4941
			if ($selectedAQM["ecn"]) {
4942
				if ($this->getECN() == 'on') {
4943
					$pfq_rule .= ' ecn';
4944
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4945
					$pfq_rule .= ' noecn';
4946
				}
4947
			}
4948
			if ($selectedAQM["pie_onoff"]) {
4949
				if ($this->getPIE_ONOFF() == 'on') {
4950
				$pfq_rule .= 'onoff';
4951
				} else {
4952
					$pfq_rule .= '';
4953
				}
4954
			}
4955
			if ($selectedAQM["pie_capdrop"]) {
4956
				if ($this->getPIE_CAPDROP() == 'on') {
4957
					$pfq_rule .= ' capdrop';
4958
				} else {
4959
					$pfq_rule .= ' nocapdrop';
4960
				}
4961
			}
4962
			if ($selectedAQM["pie_qdelay"]) {
4963
				if ($this->getPIE_QDELAY() == 'on') {
4964
					$pfq_rule .= ' ts';
4965
				} else {
4966
					$pfq_rule .= ' dre';
4967
				}
4968
			}
4969
			if ($selectedAQM["pie_pderand"]) {
4970
				if ($this->getPIE_PDERAND() == 'on') {
4971
					$pfq_rule .= ' derand';
4972
				} else {
4973
					$pfq_rule .= ' noderand';
4974
				}
4975
			}
4976
		}
4977
		/* End patch */
4978

    
4979
		$pfq_rule .= "\n";
4980

    
4981
		return $pfq_rule;
4982
	}
4983

    
4984
	function build_javascript() {
4985
		return parent::build_javascript();
4986
	}
4987

    
4988
	function build_form() {
4989
		global $g, $config, $pipe, $action, $qname;
4990

    
4991
		//build list of schedules
4992
		$schedules = array();
4993
		$schedules[] = "none";//leave none to leave rule enabled all the time
4994
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4995
			foreach ($config['schedules']['schedule'] as $schedule) {
4996
				if ($schedule['name'] <> "") {
4997
					$schedules[] = $schedule['name'];
4998
				}
4999
			}
5000
		}
5001

    
5002

    
5003
		$sform = new Form();
5004
		$sform->setAction("firewall_shaper.php");
5005
		$section = new Form_Section('Limiters');
5006

    
5007
		$section->addInput(new Form_Checkbox(
5008
			'enabled',
5009
			'Enable',
5010
			'Enable this queue',
5011
			($this->GetEnabled() == "on"),
5012
			'on'
5013
		));
5014

    
5015
		$section->addInput(new Form_Input(
5016
			'newname',
5017
			'*Name',
5018
			'text',
5019
			$this->GetQname()
5020
		));
5021

    
5022
		$section->addInput(new Form_Input(
5023
			'name',
5024
			null,
5025
			'hidden',
5026
			$this->GetQname()
5027
		));
5028

    
5029
		if ($this->GetNumber() > 0) {
5030
			$section->addInput(new Form_Input(
5031
				'number',
5032
				null,
5033
				'hidden',
5034
				$this->GetNumber()
5035
			));
5036
		}
5037

    
5038
		$mask = $this->GetMask();
5039

    
5040
		$section->addInput(new Form_Select(
5041
			'mask',
5042
			'Mask',
5043
			$mask['type'],
5044
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
5045
		))->setHelp('If "source" or "destination" slots is chosen a dynamic queue with the bandwidth, delay, packet loss ' .
5046
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
5047
 					'This makes it possible to easily specify bandwidth limits per ' .
5048
 					'host or subnet, usually capped by the bandwidth of the parent ' .
5049
 					'limiter.');
5050

    
5051
		$group = new Form_Group(null);
5052

    
5053
		$group->add(new Form_Select(
5054
			'maskbits',
5055
			null,
5056
			$mask['bits'],
5057
			array_combine(range(32, 1, -1), range(32, 1, -1))
5058
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
5059

    
5060
		$group->add(new Form_Select(
5061
			'maskbitsv6',
5062
			null,
5063
			$mask['bitsv6'],
5064
			array_combine(range(128, 1, -1), range(128, 1, -1))
5065
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
5066

    
5067
		$section->add($group);
5068

    
5069
		$section->addInput(new Form_Input(
5070
			'description',
5071
			'Description',
5072
			'text',
5073
			$this->GetDescription()
5074
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
5075

    
5076
		$sform->add($section);
5077

    
5078
		/* Begin limiter patch */
5079
		$aqm_map = getAQMs();
5080

    
5081
		$section = new Form_Section('Queue');
5082
		$section->addInput(new Form_Select(
5083
				'aqm',
5084
				'Queue Management Algorithm',
5085
				$this->GetAQM(),
5086
				array_map_assoc(function ($k, $v) {
5087
					return [$k, $v["name"]];
5088
				}, $aqm_map)
5089
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside this limiter\'s queue, ' .
5090
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
5091
						'network congestion.');
5092

    
5093
		$section->addInput(new Form_StaticText(
5094
			'',
5095
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
5096
		))->setHelp('Specifies the queue management algorithm parameters.');
5097

    
5098
		$section->addInput(new Form_Input(
5099
				'qlimit',
5100
				'Queue length',
5101
				'number',
5102
				$this->GetQlimit()
5103
		))->setHelp('Specifies the length of this queue, which the AQM is responsible for.  This field may be left empty.');
5104

    
5105
		$selectedAQM = getAQMs()[$this->getAQM()];
5106

    
5107
		if ($selectedAQM["ecn"]) {
5108
			$section->addInput(new Form_Checkbox(
5109
				'ecn',
5110
				'ECN',
5111
				'Enable Explicit Congestion Notification (ECN)',
5112
				($this->GetECN() == "on"),
5113
				'on'
5114
			))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
5115
		}
5116

    
5117
		if ($selectedAQM["pie_onoff"]) {
5118
			$section->addInput(new Form_Checkbox(
5119
				'pie_onoff',
5120
				'ONOFF',
5121
				'Enable Onoff (onoff,)',
5122
				($this->GetPIE_ONOFF() == "on"),
5123
				'on'
5124
			))->setHelp('Enable turning PIE on and off depending on queue load.');
5125
		}
5126
                                 
5127
		if ($selectedAQM["pie_capdrop"]) {
5128
			$section->addInput(new Form_Checkbox(
5129
				'pie_capdrop',
5130
				'CAPDROP',
5131
				'Enable Capdrop (capdrop,nocapdrop)',
5132
				($this->GetPIE_CAPDROP() == "on"),
5133
				'on'
5134
			))->setHelp('Enable cap drop adjustment.');
5135
		}
5136
                 
5137
		if ($selectedAQM["pie_qdelay"]) {
5138
			$section->addInput(new Form_Checkbox(
5139
				'pie_qdelay',
5140
				'QUEUE DELAY TYPE',
5141
				'Enable Type Of Qdelay (ts,dre)',
5142
				($this->GetPIE_QDELAY() == "on"),
5143
				'on'
5144
			))->setHelp('Set queue delay type to timestamps (checked) or departure rate estimation (unchecked).');
5145
		}
5146

    
5147
		if ($selectedAQM["pie_pderand"]) {
5148
			$section->addInput(new Form_Checkbox(
5149
				'pie_pderand',
5150
				'PROB DERAND',
5151
				'Enable Drop Probability De-randomisation (derand,noderand)',
5152
				($this->GetPIE_PDERAND() == "on"),
5153
				'on'
5154
			))->setHelp('Enable (checked) or disable (unchecked) drop probability de-randomisation.');
5155
		}
5156
		$sform->add($section);
5157
		/* End limiter patch */
5158

    
5159
		$section = new Form_Section('Advanced Options');
5160

    
5161
		$section->addInput(new Form_Input(
5162
			'weight',
5163
			'Weight',
5164
			'number',
5165
			$this->GetWeight(),
5166
			['min' => '1', 'max' => '100']
5167
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
5168
					' it can be left blank otherwise.');
5169

    
5170
		$section->addInput(new Form_Input(
5171
			'plr',
5172
			'Packet Loss Rate',
5173
			'number',
5174
			$this->GetPlr(),
5175
			['step' => '0.001', 'min' => '0.000']
5176
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
5177
					'A value of 0.001 means one packet in 1000 gets dropped');
5178

    
5179
		$section->addInput(new Form_Input(
5180
			'buckets',
5181
			'Bucket size (slots)',
5182
			'number',
5183
			$this->GetBuckets()
5184
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
5185

    
5186
		$section->addInput(new Form_Input(
5187
			'pipe',
5188
			null,
5189
			'hidden',
5190
			$this->GetPipe()
5191
		));
5192

    
5193
		$sform->add($section);
5194

    
5195
		return($sform);
5196
	}
5197

    
5198
	function update_dn_data(&$data) {
5199
		$this->ReadConfig($data);
5200
	}
5201

    
5202
	function wconfig() {
5203
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
5204
		if (!is_array($cflink)) {
5205
			$cflink = array();
5206
		}
5207
		$cflink['name'] = $this->GetQname();
5208
		$cflink['number'] = $this->GetNumber();
5209
		$cflink['qlimit'] = $this->GetQlimit();
5210
		$cflink['description'] = $this->GetDescription();
5211
		$cflink['weight'] = $this->GetWeight();
5212
		$cflink['enabled'] = $this->GetEnabled();
5213
		$cflink['buckets'] = $this->GetBuckets();
5214
		$mask = $this->GetMask();
5215
		$cflink['mask'] = $mask['type'];
5216
		$cflink['maskbits'] = $mask['bits'];
5217
		$cflink['maskbitsv6'] = $mask['bitsv6'];
5218

    
5219
		/* Limiter queue patch */
5220
		$cflink['aqm'] = $this->GetAQM();
5221
		$aqm_map = GetAQMs();
5222
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
5223
		foreach ($selectedParameters as $key => $value) {
5224
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
5225
			$cflink[$config_key] = $this->GetAQMParameter($key);
5226
		}
5227
		$cflink['ecn'] = $this->GetECN();
5228

    
5229
		$selectedAQM = getAQMs()[$this->getAQM()];
5230
		if ($selectedAQM["pie_onoff"]) {
5231
			$cflink['pie_onoff'] = $this->GetPIE_ONOFF();
5232
		}
5233
		if ($selectedAQM["pie_capdrop"]) {
5234
			$cflink['pie_capdrop'] = $this->GetPIE_CAPDROP();
5235
		}
5236
		if ($selectedAQM["pie_qdelay"]) {
5237
			$cflink['pie_qdelay'] = $this->GetPIE_QDELAY();
5238
		}
5239
		if ($selectedAQM["pie_pderand"]) {
5240
			$cflink['pie_pderand'] = $this->GetPIE_PDERAND();
5241
		}
5242
		/* End limiter queue patch */
5243
	}
5244
}
5245

    
5246
function get_dummynet_name_list() {
5247

    
5248
	$dn_name_list =& get_unique_dnqueue_list();
5249
	$dn_name = array();
5250
	if (is_array($dn_name_list)) {
5251
		foreach ($dn_name_list as $key => $value) {
5252
			$dn_name[] = $key;
5253
		}
5254
	}
5255

    
5256
	return $dn_name;
5257

    
5258
}
5259

    
5260
function get_altq_name_list() {
5261
	$altq_name_list =& get_unique_queue_list();
5262
	$altq_name = array();
5263
	if (is_array($altq_name_list)) {
5264
		foreach ($altq_name_list as $key => $aqobj) {
5265
			$altq_name[] = $key;
5266
		}
5267
	}
5268

    
5269
	return $altq_name;
5270
}
5271

    
5272
/*
5273
 * XXX: TODO Make a class shaper to hide all these functions
5274
 * from the global namespace.
5275
 */
5276

    
5277
/*
5278
 * This is a layer violation but for now there is no way
5279
 * I can find to properly do this with PHP.
5280
 */
5281
function altq_get_default_queue($interface) {
5282
	global $altq_list_queues;
5283

    
5284
	$altq_tmp = $altq_list_queues[$interface];
5285
	if ($altq_tmp) {
5286
		return $altq_tmp->GetDefaultQueuePresent();
5287
	} else {
5288
		return false;
5289
	}
5290
}
5291

    
5292
function altq_check_default_queues() {
5293
	global $altq_list_queues;
5294

    
5295
	$count = 0;
5296
	if (is_array($altq_list_queues)) {
5297
		foreach ($altq_list_queues as $altq) {
5298
			if ($altq->GetDefaultQueuePresent()) {
5299
				$count++;
5300
			}
5301
		}
5302
	}
5303
	else {
5304
		$count++;
5305
	}
5306

    
5307
	return 0;
5308
}
5309

    
5310
function &get_unique_queue_list() {
5311
	global $altq_list_queues;
5312

    
5313
	$qlist = array();
5314
	if (is_array($altq_list_queues)) {
5315
		foreach ($altq_list_queues as $altq) {
5316
			if ($altq->GetEnabled() == "") {
5317
				continue;
5318
			}
5319
			$tmplist =& $altq->get_queue_list();
5320
			foreach ($tmplist as $qname => $link) {
5321
				if ($link->GetEnabled() <> "") {
5322
					$qlist[$qname] = $link;
5323
				}
5324
			}
5325
		}
5326
	}
5327
	return $qlist;
5328
}
5329

    
5330
function &get_unique_dnqueue_list() {
5331
	global $dummynet_pipe_list;
5332

    
5333
	$qlist = array();
5334
	if (is_array($dummynet_pipe_list)) {
5335
		foreach ($dummynet_pipe_list as $dn) {
5336
			if ($dn->GetEnabled() == "") {
5337
				continue;
5338
			}
5339
			$tmplist =& $dn->get_queue_list();
5340
			foreach ($tmplist as $qname => $link) {
5341
				$qlist[$qname] = $link;
5342
			}
5343
		}
5344
	}
5345
	return $qlist;
5346
}
5347

    
5348
function ref_on_altq_queue_list($parent, $qname) {
5349
	if (isset($GLOBALS['queue_list'][$qname])) {
5350
		$GLOBALS['queue_list'][$qname]++;
5351
	} else {
5352
		$GLOBALS['queue_list'][$qname] = 1;
5353
	}
5354

    
5355
	unref_on_altq_queue_list($parent);
5356
}
5357

    
5358
function unref_on_altq_queue_list($qname) {
5359
	$GLOBALS['queue_list'][$qname]--;
5360
	if ($GLOBALS['queue_list'][$qname] <= 1) {
5361
		unset($GLOBALS['queue_list'][$qname]);
5362
	}
5363
}
5364

    
5365
function read_altq_config() {
5366
	global $altq_list_queues, $config;
5367
	$path = array();
5368

    
5369
	init_config_arr(array('shaper', 'queue'));
5370
	$a_int = &$config['shaper']['queue'];
5371

    
5372
	$altq_list_queues = array();
5373

    
5374
	if (!is_array($config['shaper']['queue'])) {
5375
		return;
5376
	}
5377

    
5378
	foreach ($a_int as $key => $conf) {
5379
		$int = $conf['interface'];
5380
		$root = new altq_root_queue();
5381
		$root->SetInterface($int);
5382
		$altq_list_queues[$root->GetInterface()] = $root;
5383
		$root->ReadConfig($conf);
5384
		array_push($path, $key);
5385
		$root->SetLink($path);
5386
		if (is_array($conf['queue'])) {
5387
			foreach ($conf['queue'] as $key1 => $q) {
5388
				array_push($path, $key1);
5389
				/*
5390
				 * XXX: we completely ignore errors here but anyway we must have
5391
				 *	checked them before so no harm should be come from this.
5392
				 */
5393
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
5394
				array_pop($path);
5395
			}
5396
		}
5397
		array_pop($path);
5398
	}
5399
}
5400

    
5401
function read_dummynet_config() {
5402
	global $dummynet_pipe_list, $config;
5403
	$path = array();
5404

    
5405
	init_config_arr(array('dnshaper', 'queue'));
5406
	$a_int = &$config['dnshaper']['queue'];
5407

    
5408
	$dummynet_pipe_list = array();
5409

    
5410
	if (!count($config['dnshaper']['queue'])) {
5411
		return;
5412
	}
5413

    
5414
	foreach ($a_int as $key => $conf) {
5415
		if (empty($conf['name'])) {
5416
			continue; /* XXX: grrrrrr at php */
5417
		}
5418
		$root = new dnpipe_class();
5419
		$root->ReadConfig($conf);
5420
		$dummynet_pipe_list[$root->GetQname()] = $root;
5421
		array_push($path, $key);
5422
		$root->SetLink($path);
5423
		if (is_array($conf['queue'])) {
5424
			foreach ($conf['queue'] as $key1 => $q) {
5425
				array_push($path, $key1);
5426
				/*
5427
				 * XXX: we completely ignore errors here but anyway we must have
5428
				 *	checked them before so no harm should be come from this.
5429
				 */
5430
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
5431
				array_pop($path);
5432
			}
5433
		}
5434
		array_pop($path);
5435
	}
5436
}
5437

    
5438
function get_interface_list_to_show() {
5439
	global $altq_list_queues, $config;
5440
	global $shaperIFlist;
5441

    
5442
	$tree = "";
5443
	foreach ($shaperIFlist as $shif => $shDescr) {
5444
		if ($altq_list_queues[$shif]) {
5445
			continue;
5446
		} else {
5447
			if (!is_altq_capable(get_real_interface($shif))) {
5448
				continue;
5449
			}
5450
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
5451
		}
5452
	}
5453

    
5454
	return $tree;
5455
}
5456

    
5457
function filter_generate_altq_queues() {
5458
	global $altq_list_queues;
5459

    
5460
	read_altq_config();
5461

    
5462
	$altq_rules = "";
5463
	foreach ($altq_list_queues as $altq) {
5464
		$altq_rules .= $altq->build_rules();
5465
	}
5466

    
5467
	return $altq_rules;
5468
}
5469

    
5470
function dnqueue_find_nextnumber() {
5471
	global $dummynet_pipe_list;
5472

    
5473
	$dnused = array();
5474
	if (is_array($dummynet_pipe_list)) {
5475
		foreach ($dummynet_pipe_list as $dn) {
5476
			$tmplist =& $dn->get_queue_list();
5477
			foreach ($tmplist as $qname => $link) {
5478
				if ($link[0] == "?") {
5479
					$dnused[$qname] = substr($link, 1);
5480
				}
5481
			}
5482
		}
5483
	}
5484

    
5485
	sort($dnused, SORT_NUMERIC);
5486
	$dnnumber = 0;
5487
	$found = false;
5488
	foreach ($dnused as $dnnum) {
5489
		if (($dnnum - $dnnumber) > 1) {
5490
			$dnnumber = $dnnum - 1;
5491
			$found = true;
5492
			break;
5493
		} else {
5494
			$dnnumber = $dnnum;
5495
		}
5496
	}
5497

    
5498
	if ($found == false) {
5499
		$dnnumber++;
5500
	}
5501

    
5502
	unset($dnused, $dnnum, $found);
5503
	return $dnnumber;
5504
}
5505

    
5506
function dnpipe_find_nextnumber() {
5507
	global $dummynet_pipe_list;
5508

    
5509
	$dnused = array();
5510
	foreach ($dummynet_pipe_list as $dn) {
5511
		$dnused[] = $dn->GetNumber();
5512
	}
5513

    
5514
	sort($dnused, SORT_NUMERIC);
5515
	$dnnumber = 0;
5516
	$found = false;
5517
	foreach ($dnused as $dnnum) {
5518
		if (($dnnum - $dnnumber) > 1) {
5519
			$dnnumber = $dnnum - 1;
5520
			$found = true;
5521
			break;
5522
		} else {
5523
			$dnnumber = $dnnum;
5524
		}
5525
	}
5526

    
5527
	if ($found == false) {
5528
		$dnnumber++;
5529
	}
5530

    
5531
	unset($dnused, $dnnum, $found);
5532
	return $dnnumber;
5533
}
5534

    
5535
function filter_generate_dummynet_rules() {
5536
	global $config, $g, $dummynet_pipe_list;
5537

    
5538
	read_dummynet_config();
5539

    
5540
	$dn_rules = "";
5541
	$max_qlimit = "100"; // OS default
5542
	foreach ($dummynet_pipe_list as $dn) {
5543
		$dn_rules .= $dn->build_rules();
5544
		$this_qlimit = $dn->GetQlimit();
5545
		if ($this_qlimit > $max_qlimit) {
5546
			$max_qlimit = $this_qlimit;
5547
		}
5548
	}
5549
	if (!is_numericint($max_qlimit)) {
5550
		$max_qlimit = "100";
5551
	}
5552
	if (!empty($dn_rules)) {
5553
		dummynet_load_module($max_qlimit);
5554
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
5555
		mwexec("/sbin/dnctl {$g['tmp_path']}/rules.limiter");
5556
	}
5557
}
5558

    
5559
function build_iface_without_this_queue($iface, $qname) {
5560
	global $g, $altq_list_queues;
5561
	global $shaperIFlist;
5562

    
5563
	$altq =& $altq_list_queues[$iface];
5564

    
5565
	if ($altq) {
5566
		$scheduler = $altq->GetScheduler();
5567
	}
5568

    
5569
	$form = '<dl class="dl-horizontal">';
5570

    
5571
	$form .= '	<dt>';
5572
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5573
	$form .= '	</dt>';
5574
	$form .= '	<dd>';
5575
	$form .=		$scheduler;
5576
	$form .= '	</dd>';
5577

    
5578
	$form .= '	<dt>';
5579
	$form .= 'Clone';
5580
	$form .= '	</dt>';
5581
	$form .= '	<dd>';
5582
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
5583
	$form .= $iface . '&amp;queue=';
5584
	$form .= $qname . '&amp;action=add">';
5585
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
5586
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
5587
	$form .= '	</dd>';
5588

    
5589
	$form .= '</dl>';
5590

    
5591
	return $form;
5592

    
5593
}
5594

    
5595
$default_shaper_msg = sprintf(gettext("Welcome to the %s Traffic Shaper."), $g['product_label']) . "<br />";
5596
$dn_default_shaper_msg = $default_shaper_msg;
5597

    
5598
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5599
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5600
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5601

    
5602
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5603
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5604
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5605

    
5606
// Check to see if the specified interface has a queue configured
5607
function interface_has_queue($if) {
5608
	global $config;
5609

    
5610
	if ($config['shaper']) {
5611
		foreach ($config['shaper']['queue'] as $queue) {
5612
			if ($queue['interface'] === $if) {
5613
				return true;
5614
			}
5615
		}
5616
	}
5617

    
5618
	return false;
5619
}
5620
?>
(47-47/61)