Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
	return $ptr;
332
}
333

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

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

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

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

    
354
	return $ptr;
355
}
356

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

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

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

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

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

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

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

    
480
	return floatval($objbw);
481
}
482

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

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

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

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

    
532
	return floatval($objbw);
533
}
534

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
712
	function GetBwscaleText() {
713
		switch ($this->bandwidthtype) {
714
			case "b":
715
				$bwscaletext = "Bit/s";
716
				break;
717
			case "Kb":
718
				$bwscaletext = "Kbit/s";
719
				break;
720
			case "Mb":
721
				$bwscaletext = "Mbit/s";
722
				break;
723
			case "Gb":
724
				$bwscaletext = "Gbit/s";
725
				break;
726
			default:
727
				/* For others that do not need translating like % */
728
				$bwscaletext = $this->bandwidthtype;
729
				break;
730
		}
731
		return $bwscaletext;
732
	}
733

    
734
	function CheckBandwidth($bw, $bwtype) {
735
		$sum = $this->GetTotalBw();
736
		if ($sum > ($bw * get_bandwidthtype_scale($bwtype))) {
737
			return 1;
738
		}
739
		foreach ($this->queues as $q) {
740
			if ($q->CheckBandwidth(0, '')) {
741
				return 1;
742
			}
743
		}
744

    
745
		return 0;
746
	}
747

    
748
	function GetTotalBw($qignore = NULL) {
749
		$sum = 0;
750
		foreach ($this->queues as $q) {
751
			if (($qignore != NULL) && ($qignore == $q)) {
752
				continue;
753
			}
754
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
755
		}
756

    
757
		return $sum;
758
	}
759

    
760
	function validate_input($data, &$input_errors) {
761

    
762
		$reqdfields[] = "bandwidth";
763
		$reqdfieldsn[] = gettext("Bandwidth");
764
		$reqdfields[] = "bandwidthtype";
765
		$reqdfieldsn[] = gettext("Bandwidthtype");
766

    
767
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
768

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

    
786
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
787
			$input_errors[] = gettext("Qlimit must be an integer.");
788
		}
789
		if ($data['qlimit'] < 0) {
790
			$input_errors[] = gettext("Qlimit must be positive.");
791
		}
792
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
793
			$input_errors[] = gettext("Tbrsize must be an integer.");
794
		}
795
		if ($data['tbrconfig'] < 0) {
796
			$input_errors[] = gettext("Tbrsize must be positive.");
797
		}
798
	}
799

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

    
835
	function copy_queue($interface, &$cflink) {
836
		$cflink['interface'] = $interface;
837
		$cflink['name'] = $interface;
838
		$cflink['scheduler'] = $this->GetScheduler();
839
		$cflink['bandwidth'] = $this->GetBandwidth();
840
		$cflink['bandwidthtype'] = $this->GetBwscale();
841
		$cflink['qlimit'] = $this->GetQlimit();
842
		$cflink['tbrconfig'] = $this->GetTbrConfig();
843
		$cflink['enabled'] = $this->GetEnabled();
844
		if (is_array($this->queues)) {
845
			$cflink['queue'] = array();
846
			foreach ($this->queues as $q) {
847
				$cflink['queue'][$q->GetQname()] = array();
848
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
849
			}
850
		}
851
	}
852

    
853
	function &get_queue_list(&$q = null) {
854
		$qlist = array();
855

    
856
		//$qlist[$this->GetQname()] = & $this;
857
		if (is_array($this->queues)) {
858
			foreach ($this->queues as $queue) {
859
				$queue->get_queue_list($qlist);
860
			}
861
		}
862
		return $qlist;
863
	}
864

    
865
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
866

    
867
		if (!is_array($this->queues)) {
868
			$this->queues = array();
869
		}
870

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

    
896
		$this->queues[$q->GetQname()] = &$q;
897
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
898
		if (is_array($queue['queue'])) {
899
			foreach ($queue['queue'] as $key1 => $que) {
900
				array_push($path, $key1);
901
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
902
				array_pop($path);
903
			}
904
		}
905

    
906
		return $q;
907
	}
908

    
909
	/* interface here might be optional */
910
	function &find_queue($interface, $qname) {
911
		if ($qname == $this->GetQname()) {
912
			return $this;
913
		}
914
		foreach ($this->queues as $q) {
915
			$result =& $q->find_queue("", $qname);
916
			if ($result) {
917
				return $result;
918
			}
919
		}
920
	}
921

    
922
	function &find_parentqueue($interface, $qname) {
923
		if ($qname == $interface) {
924
			$result = NULL;
925
		} else if ($this->queues[$qname]) {
926
			$result = $this;
927
		} else if ($this->GetScheduler() <> "PRIQ") {
928
			foreach ($this->queues as $q) {
929
				$result = $q->find_parentqueue("", $qname);
930
				if ($result) {
931
					return $result;
932
				}
933
			}
934
		}
935
	}
936

    
937
	function build_tree() {
938
		global $shaperIFlist;
939

    
940
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
941
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
942
		if (is_array($this->queues)) {
943
			$tree .= "<ul>";
944
			foreach ($this->queues as $q) {
945
				$tree .= $q->build_tree();
946
			}
947
			$tree .= "</ul>";
948
		}
949
		$tree .= "</li>";
950
		return $tree;
951
	}
952

    
953
	function delete_queue() {
954
		foreach ($this->queues as $q) {
955
			$q->delete_queue();
956
		}
957
		unset_object_by_reference($this->GetLink());
958
	}
959

    
960
	function delete_all() {
961
		if (count($this->queues)) {
962
			foreach ($this->queues as $q) {
963
				$q->delete_all();
964
				unset_object_by_reference($q->GetLink());
965
				unset($q);
966
			}
967
			unset($this->queues);
968
		}
969
	}
970

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

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

    
1040
			$rules .= " queue";
1041
		}
1042

    
1043
		$rules .= " \n";
1044
		return $rules;
1045
	}
1046

    
1047
	function build_javascript() {
1048
		$javascript = "<script type=\"text/javascript\">";
1049
		$javascript .= "//<![CDATA[\n";
1050
		$javascript .= "function mySuspend() {";
1051
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1052
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
1053
		$javascript .= "else if (document.all)";
1054
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
1055
		$javascript .= "}";
1056

    
1057
		$javascript .= "function myResume() {";
1058
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1059
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
1060
		$javascript .= "else if (document.all) ";
1061
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
1062
		$javascript .= "}";
1063
		$javascript .= "//]]>";
1064
		$javascript .= "</script>";
1065

    
1066
		return $javascript;
1067
	}
1068

    
1069
	function build_shortform() {
1070
		global $g;
1071

    
1072
		$altq =& $this;
1073

    
1074
		if ($altq) {
1075
			$scheduler = ": " . $altq->GetScheduler();
1076
		}
1077

    
1078
		$form = '<dl class="dl-horizontal">';
1079
		$form .= '	<dt>';
1080
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1081
		$form .= '	</dt>';
1082
		$form .= '	<dd>';
1083
		$form .=		$scheduler;
1084
		$form .= '	</dd>';
1085

    
1086
		$form .= '	<dt>';
1087
		$form .=		'Bandwidth';
1088
		$form .= '	</dt>';
1089
		$form .= '	<dd>';
1090
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1091
		$form .= '	</dd>';
1092

    
1093
		$form .= '	<dt>';
1094
		$form .= 'Disable';
1095
		$form .= '	<dt>';
1096
		$form .= '	<dd>';
1097

    
1098
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1099
		$form .= $this->GetInterface() . '&amp;queue=';
1100
		$form .= $this->GetQname() . '&amp;action=delete">';
1101
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1102
		$form .= gettext("Remove shaper from this interface") . '</a>';
1103

    
1104
		$form .= '	</dd>';
1105

    
1106
		$form .= '</dl>';
1107

    
1108
		return $form;
1109

    
1110
	}
1111

    
1112
	/*
1113
	 * For requesting the parameters of the root queues
1114
	 * to the user like the traffic wizard does.
1115
	 */
1116
	function build_form() {
1117

    
1118
		$sform = new Form();
1119

    
1120
		$sform->setAction("firewall_shaper.php");
1121

    
1122
		$section = new Form_Section(null);
1123

    
1124
		$section->addInput(new Form_Checkbox(
1125
			'enabled',
1126
			'Enable/Disable',
1127
			'Enable/disable discipline and its children',
1128
			($this->GetEnabled() == "on"),
1129
			'on'
1130
		));
1131

    
1132
		$section->addInput(new Form_StaticText(
1133
			'*Name',
1134
			$this->GetQname()
1135
		));
1136

    
1137
		$section->addInput(new Form_Select(
1138
			'scheduler',
1139
			'Scheduler Type',
1140
			$this->GetScheduler(),
1141
			array('HFSC' => 'HFSC',
1142
				  'CBQ' => 'CBQ',
1143
				  'FAIRQ' => 'FAIRQ',
1144
				  'CODELQ' => 'CODELQ',
1145
				  'PRIQ' => 'PRIQ')
1146
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
1147

    
1148
		$group = new Form_group('Bandwidth');
1149

    
1150
		$group->add(new Form_Input(
1151
			'bandwidth',
1152
			null,
1153
			'number',
1154
			$this->GetBandwidth()
1155
		));
1156

    
1157
		$group->add(new Form_Select(
1158
			'bandwidthtype',
1159
			null,
1160
			$this->GetBwscale(),
1161
			array('Kb' => 'Kbit/s',
1162
				  'Mb' => 'Mbit/s',
1163
				  'Gb' => 'Gbit/s',
1164
				  'b' => 'Bit/s',
1165
				  '%' => '%')
1166
		));
1167

    
1168
		$section->add($group);
1169

    
1170
		$section->addInput(new Form_Input(
1171
			'qlimit',
1172
			'Queue Limit',
1173
			'number',
1174
			$this->GetQlimit()
1175
		));
1176

    
1177
		$section->addInput(new Form_Input(
1178
			'tbrconfig',
1179
			'TBR Size',
1180
			'number',
1181
			$this->GetTbrConfig()
1182
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
1183
					'bandwidth are used to determine the size.');
1184

    
1185
		$section->addInput(new Form_Input(
1186
			'interface',
1187
			null,
1188
			'hidden',
1189
			$this->GetInterface()
1190
		));
1191

    
1192
		$section->addInput(new Form_Input(
1193
			'name',
1194
			null,
1195
			'hidden',
1196
			$this->GetQname()
1197
		));
1198

    
1199
		$sform->add($section);
1200

    
1201
		return($sform);
1202
	}
1203

    
1204
	function update_altq_queue_data(&$data) {
1205
		$this->ReadConfig($data);
1206
	}
1207

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

    
1236
}
1237

    
1238
class priq_queue {
1239
	var $qname;
1240
	var $qinterface;
1241
	var $qlimit;
1242
	var $qpriority;
1243
	var $description;
1244
	var $isparent;
1245
	var $qbandwidth;
1246
	var $qbandwidthtype;
1247
	var $qdefault = "";
1248
	var $qrio = "";
1249
	var $qred = "";
1250
	var $qcodel = "";
1251
	var $qecn = "";
1252
	var $qack;
1253
	var $qenabled = "";
1254
	var $qparent;
1255
	var $link;
1256

    
1257
	/* This is here to help with form building and building rules/lists */
1258
	var $subqueues = array();
1259

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

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

    
1335
		if ($bwtype != "%") {
1336
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1337

    
1338
			if ($bw > 0) {
1339
				$sum += get_bandwidth($bw, $bwtype, $parent);
1340
			}
1341

    
1342
			if ($sum > get_queue_bandwidth($parent)) {
1343
				return 1;
1344
			}
1345
		}
1346

    
1347
		foreach ($this->subqueues as $q) {
1348
			if ($q->CheckBandwidth(0, '')) {
1349
				return 1;
1350
			}
1351
		}
1352

    
1353
		return 0;
1354
	}
1355
	function GetTotalBw($qignore = NULL) {
1356
		$sum = 0;
1357
		foreach ($this->subqueues as $q) {
1358
			if ($qignore != NULL && $qignore == $q)
1359
				continue;
1360
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1361
		}
1362

    
1363
		return $sum;
1364
	}
1365
	function GetBwscale() {
1366
		return $this->qbandwidthtype;
1367
	}
1368
	function SetBwscale($scale) {
1369
		$this->qbandwidthtype = $scale;
1370
	}
1371
	function GetDefaultQueuePresent() {
1372
		if ($this->GetDefault()) {
1373
			return true;
1374
		}
1375
		if (!empty($this->subqueues)) {
1376
			foreach ($this->subqueues as $q) {
1377
				if ($q->GetDefault()) {
1378
					return true;
1379
				}
1380
			}
1381
		}
1382

    
1383
		return false;
1384
	}
1385
	function GetDefault() {
1386
		return $this->qdefault;
1387
	}
1388
	function SetDefault($value = false) {
1389
		$this->qdefault = $value;
1390
	}
1391
	function GetCodel() {
1392
		return $this->codel;
1393
	}
1394
	function SetCodel($codel = false) {
1395
		$this->codel = $codel;
1396
	}
1397
	function GetRed() {
1398
		return $this->qred;
1399
	}
1400
	function SetRed($red = false) {
1401
		$this->qred = $red;
1402
	}
1403
	function GetRio() {
1404
		return $this->qrio;
1405
	}
1406
	function SetRio($rio = false) {
1407
		$this->qrio = $rio;
1408
	}
1409
	function GetEcn() {
1410
		return $this->qecn;
1411
	}
1412
	function SetEcn($ecn = false) {
1413
		$this->qecn = $ecn;
1414
	}
1415
	function GetAck() {
1416
		return $this->qack;
1417
	}
1418
	function SetAck($ack = false) {
1419
		$this->qack = $ack;
1420
	}
1421

    
1422
	function GetBwscaleText() {
1423
		switch ($this->qbandwidthtype) {
1424
			case "b":
1425
				$bwscaletext = "Bit/s";
1426
				break;
1427
			case "Kb":
1428
				$bwscaletext = "Kbit/s";
1429
				break;
1430
			case "Mb":
1431
				$bwscaletext = "Mbit/s";
1432
				break;
1433
			case "Gb":
1434
				$bwscaletext = "Gbit/s";
1435
				break;
1436
			default:
1437
				/* For others that do not need translating like % */
1438
				$bwscaletext = $this->qbandwidthtype;
1439
				break;
1440
		}
1441
		return $bwscaletext;
1442
	}
1443

    
1444
	function build_javascript() {
1445
		$javascript = "<script type=\"text/javascript\">";
1446
		$javascript .= "//<![CDATA[\n";
1447
		$javascript .= "function mySuspend() { \n";
1448
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1449
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1450
		$javascript .= "else if (document.all)\n";
1451
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1452
		$javascript .= "}\n";
1453

    
1454
		$javascript .= "function myResume() {\n";
1455
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1456
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1457
		$javascript .= "else if (document.all)\n";
1458
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1459
		$javascript .= "}\n";
1460
		$javascript .= "//]]>";
1461
		$javascript .= "</script>";
1462

    
1463
		return $javascript;
1464
	}
1465

    
1466
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1467

    
1468
	/*
1469
	 * Currently this will not be called unless we decide to clone a whole
1470
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1471
	 */
1472
	function copy_queue($interface, &$cflink) {
1473

    
1474
		$cflink['name'] = $this->GetQname();
1475
		$cflink['interface'] = $interface;
1476
		$cflink['qlimit'] = $this->GetQlimit();
1477
		$cflink['priority'] = $this->GetQpriority();
1478
		$cflink['description'] = $this->GetDescription();
1479
		$cflink['enabled'] = $this->GetEnabled();
1480
		$cflink['default'] = $this->GetDefault();
1481
		$cflink['red'] = $this->GetRed();
1482
		$cflink['codel'] = $this->GetCodel();
1483
		$cflink['rio'] = $this->GetRio();
1484
		$cflink['ecn'] = $this->GetEcn();
1485

    
1486
		if (is_array($this->subqueues)) {
1487
			$cflinkp['queue'] = array();
1488
			foreach ($this->subqueues as $q) {
1489
				$cflink['queue'][$q->GetQname()] = array();
1490
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1491
			}
1492
		}
1493
	}
1494

    
1495
	function clean_queue($sched) {
1496
		clean_child_queues($sched, $this->GetLink());
1497
		if (is_array($this->subqueues)) {
1498
			foreach ($this->subqueues as $q) {
1499
				$q->clean_queue($sched);
1500
			}
1501
		}
1502
	}
1503

    
1504
	function &get_queue_list(&$qlist) {
1505

    
1506
		$qlist[$this->GetQname()] = & $this;
1507
		if (is_array($this->subqueues)) {
1508
			foreach ($this->subqueues as $queue) {
1509
				$queue->get_queue_list($qlist);
1510
			}
1511
		}
1512
	}
1513

    
1514
	function delete_queue() {
1515
		unref_on_altq_queue_list($this->GetQname());
1516
		cleanup_queue_from_rules($this->GetQname());
1517
		unset_object_by_reference($this->GetLink());
1518
	}
1519

    
1520
	function delete_all() {
1521
		if (count($this->subqueues)) {
1522
			foreach ($this->subqueues as $q) {
1523
				$q->delete_all();
1524
				unset_object_by_reference($q->GetLink());
1525
				unset($q);
1526
			}
1527
			unset($this->subqueues);
1528
		}
1529
	}
1530

    
1531
	function &find_queue($interface, $qname) {
1532
		if ($qname == $this->GetQname()) {
1533
			return $this;
1534
		}
1535
	}
1536

    
1537
	function find_parentqueue($interface, $qname) { return; }
1538

    
1539
	function validate_input($data, &$input_errors) {
1540

    
1541
		$reqdfields[] = "name";
1542
		$reqdfieldsn[] = gettext("Name");
1543
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1544

    
1545
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
1546
			$input_errors[] = gettext("Bandwidth must be an integer.");
1547
		}
1548
		if ($data['bandwidth'] < 0) {
1549
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1550
		}
1551
		if ($data['bandwidthtype'] == "%") {
1552
			if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
1553
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1554
			}
1555
		}
1556
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1557
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1558

    
1559
		if (isset($data['priority']) && (!is_numeric($data['priority']) ||
1560
		    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1561
			$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1562
		}
1563
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1564
				$input_errors[] = gettext("Queue limit must be an integer");
1565
		}
1566
		if ($data['qlimit'] < 0) {
1567
				$input_errors[] = gettext("Queue limit must be positive");
1568
		}
1569
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1570
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1571
		}
1572
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1573
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1574
		}
1575
		$default = $this->GetDefault();
1576
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1577
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1578
		}
1579

    
1580
		$parent = $this->GetParent();
1581
		if (is_array($parent->queues)) {
1582
			foreach ($parent->queues as $q) {
1583
				if (($data['newname'] == $q->GetQname()) && ($data['name'] != $data['newname'])) { 
1584
					$input_errors[] = gettext("A queue with the same name already exists.");
1585
				}
1586
				if (in_array($parent->GetScheduler(), array('CBQ', 'FAIRQ','PRIQ')) &&
1587
				    ($data['priority'] == $q->GetQpriority()) && ($data['name'] != $q->GetQname())) { 
1588
					$input_errors[] = sprintf(gettext("The priority %d is already used by queue %s."),
1589
							$data['priority'], $q->GetQname());
1590
				}
1591
			}
1592
		}
1593
	}
1594

    
1595
	function ReadConfig(&$q) {
1596
		if (!empty($q['name']) && !empty($q['newname']) && ($q['name'] != $q['newname'])) {
1597
			$this->SetQname($q['newname']);
1598
			rename_queue_in_rules($q['name'], $q['newname']);
1599
		} else if (!empty($q['newname'])) {
1600
			$this->SetQname($q['newname']);
1601
		} elseif (isset($q['name'])) {
1602
			$this->SetQname($q['name']);
1603
		}
1604
		if (isset($q['interface'])) {
1605
			$this->SetInterface($q['interface']);
1606
		}
1607
		$this->SetBandwidth($q['bandwidth']);
1608
		if (!empty($q['bandwidthtype'])) {
1609
			$this->SetBwscale($q['bandwidthtype']);
1610
		}
1611
		if (!empty($q['qlimit'])) {
1612
			$this->SetQlimit($q['qlimit']);
1613
		} else {
1614
			$this->SetQlimit(""); // Default
1615
		}
1616
		if (is_numeric($q['priority'])) {
1617
			$this->SetQPriority($q['priority']);
1618
		} else {
1619
			$this->SetQpriority("");
1620
		}
1621
		if (!empty($q['description'])) {
1622
			$this->SetDescription($q['description']);
1623
		} else {
1624
			$this->SetDescription("");
1625
		}
1626
		if (!empty($q['red'])) {
1627
			$this->SetRed($q['red']);
1628
		} else {
1629
			$this->SetRed();
1630
		}
1631
		if (!empty($q['codel'])) {
1632
			$this->SetCodel($q['codel']);
1633
		} else {
1634
			$this->SetCodel();
1635
		}
1636
		if (!empty($q['rio'])) {
1637
			$this->SetRio($q['rio']);
1638
		} else {
1639
			$this->SetRio();
1640
		}
1641
		if (!empty($q['ecn'])) {
1642
			$this->SetEcn($q['ecn']);
1643
		} else {
1644
			$this->SetEcn();
1645
		}
1646
		if (!empty($q['default'])) {
1647
			$this->SetDefault($q['default']);
1648
		} else {
1649
			$this->SetDefault();
1650
		}
1651
		if (!empty($q['enabled'])) {
1652
			$this->SetEnabled($q['enabled']);
1653
		} else {
1654
			$this->SetEnabled("");
1655
		}
1656
	}
1657

    
1658
	function build_tree() {
1659
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". htmlspecialchars($this->GetQname())."&amp;action=show";
1660
		$tree .= "\" ";
1661
		$tmpvalue = $this->GetDefault();
1662
		if (!empty($tmpvalue)) {
1663
			$tree .= " class=\"navlnk\"";
1664
		}
1665
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
1666
		/*
1667
		 * Not needed here!
1668
		 * if (is_array($queues) {
1669
		 *	  $tree .= "<ul>";
1670
		 *	  foreach ($q as $queues)
1671
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1672
		 *	  endforeach
1673
		 *	  $tree .= "</ul>";
1674
		 * }
1675
		 */
1676

    
1677
		$tree .= "</li>";
1678

    
1679
		return $tree;
1680
	}
1681

    
1682
	/* Should return something like:
1683
	 * queue $qname on $qinterface bandwidth ....
1684
	 */
1685
	function build_rules(&$default = false) {
1686
		$pfq_rule = " queue ". $this->qname;
1687
		if ($this->GetInterface()) {
1688
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1689
		}
1690
		$tmpvalue = $this->GetQpriority();
1691
		if (is_numeric($tmpvalue)) {
1692
			$pfq_rule .= " priority ".$this->GetQpriority();
1693
		}
1694
		$tmpvalue = $this->GetQlimit();
1695
		if (!empty($tmpvalue)) {
1696
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1697
		}
1698
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1699
			$pfq_rule .= " priq ( ";
1700
			$tmpvalue = $this->GetRed();
1701
			if (!empty($tmpvalue)) {
1702
				$comma = 1;
1703
				$pfq_rule .= " red ";
1704
			}
1705
			$tmpvalue = $this->GetRio();
1706
			if (!empty($tmpvalue)) {
1707
				if ($comma) {
1708
					$pfq_rule .= " ,";
1709
				}
1710
				$comma = 1;
1711
				$pfq_rule .= " rio ";
1712
			}
1713
			$tmpvalue = $this->GetEcn();
1714
			if (!empty($tmpvalue)) {
1715
				if ($comma) {
1716
					$pfq_rule .= " ,";
1717
				}
1718
				$comma = 1;
1719
				$pfq_rule .= " ecn ";
1720
			}
1721
			$tmpvalue = $this->GetCodel();
1722
			if (!empty($tmpvalue)) {
1723
				if ($comma) {
1724
					$pfq_rule .= " ,";
1725
				}
1726
				$comma = 1;
1727
				$pfq_rule .= " codel ";
1728
			}
1729
			$tmpvalue = $this->GetDefault();
1730
			if (!empty($tmpvalue)) {
1731
				if ($comma) {
1732
					$pfq_rule .= " ,";
1733
				}
1734
				$pfq_rule .= " default ";
1735
				$default = true;
1736
			}
1737
			$pfq_rule .= " ) ";
1738
		}
1739

    
1740
		$pfq_rule .= " \n";
1741

    
1742
		return $pfq_rule;
1743
	}
1744

    
1745
	/*
1746
	 * To return the html form to show to user
1747
	 * for getting the parameters.
1748
	 * Should do even for first time when the
1749
	 * object is created and later when we may
1750
	 * need to update it. (2)
1751
	 */
1752

    
1753
	function build_form() {
1754

    
1755
		$sform = new Form();
1756

    
1757
		$sform->setAction("firewall_shaper.php");
1758

    
1759
		$section = new Form_Section("");
1760

    
1761
		$section->addInput(new Form_Checkbox(
1762
			'enabled',
1763
			'Enable/Disable',
1764
			'Enable/disable discipline and its children',
1765
			($this->GetEnabled() == "on"),
1766
			'on'
1767
		));
1768

    
1769
		$section->addInput(new Form_Input(
1770
			'newname',
1771
			'*Name',
1772
			'text',
1773
			$this->GetQname()
1774
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1775

    
1776
		$section->addInput(new Form_Input(
1777
			'name',
1778
			null,
1779
			'hidden',
1780
			$this->GetQname()
1781
		));
1782

    
1783
		if (!is_a($this, "hfsc_queue")) {
1784
			$section->addInput(new Form_Input(
1785
				'priority',
1786
				'Priority',
1787
				'number',
1788
				$this->GetQpriority(),
1789
				['min' => '0', 'max'=> '']
1790
			))->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.');
1791
		}
1792

    
1793
		$section->addInput(new Form_Input(
1794
			'qlimit',
1795
			'Queue Limit',
1796
			'number',
1797
			$this->GetQlimit()
1798
		))->setHelp('Queue limit in packets.');
1799

    
1800
		$group = new Form_Group('Scheduler options');
1801

    
1802
		if (empty($this->subqueues)) {
1803
			$group->add(new Form_Checkbox(
1804
				'default',
1805
				null,
1806
				null,
1807
				$this->GetDefault(),
1808
				'default'
1809
			))->setHelp('Default Queue');
1810
		}
1811

    
1812
		$group->add(new Form_Checkbox(
1813
			'red',
1814
			null,
1815
			null,
1816
			!empty($this->GetRed())
1817
		))->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>');
1818

    
1819
		$group->add(new Form_Checkbox(
1820
			'rio',
1821
			null,
1822
			null,
1823
			!empty($this->GetRio())
1824
		))->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>');
1825

    
1826
		$group->add(new Form_Checkbox(
1827
			'ecn',
1828
			null,
1829
			null,
1830
			!empty($this->GetEcn())
1831
		))->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>');
1832

    
1833
		$group->add(new Form_Checkbox(
1834
			'codel',
1835
			null,
1836
			null,
1837
			!empty($this->GetCodel())
1838
		))->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>');
1839

    
1840
		$group->setHelp('Select options for this queue');
1841

    
1842
		$section->add($group);
1843

    
1844
		$section->addInput(new Form_Input(
1845
			'description',
1846
			'Description',
1847
			'text',
1848
			$this->GetDescription()
1849
		));
1850

    
1851
		$sform->add($section);
1852

    
1853
		$sform->addGlobal(new Form_Input(
1854
			'interface',
1855
			null,
1856
			'hidden',
1857
			$this->GetInterface()
1858
		));
1859

    
1860
		$sform->addGlobal(new Form_Input(
1861
			'name',
1862
			null,
1863
			'hidden',
1864
			$this->GetQname()
1865
		));
1866

    
1867
		return($sform);
1868
	}
1869

    
1870
	function build_shortform() {
1871
		/* XXX: Hacks in sight. Mostly layer violations!  */
1872
		global $g, $altq_list_queues;
1873
		global $shaperIFlist;
1874

    
1875
		$altq =& $altq_list_queues[$this->GetInterface()];
1876

    
1877
		if ($altq) {
1878
			$scheduler = $altq->GetScheduler();
1879
		}
1880

    
1881
		$form = '<dl class="dl-horizontal">';
1882
		$form .= '	<dt>';
1883
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1884
		$form .= '	</dt>';
1885
		$form .= '	<dd>';
1886
		$form .=		$scheduler;
1887
		$form .= '	</dd>';
1888

    
1889
		$form .= '	<dt>';
1890
		$form .=		'Bandwidth';
1891
		$form .= '	</dt>';
1892
		$form .= '	<dd>';
1893
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1894
		$form .= '	</dd>';
1895

    
1896
		$tmpvalue = $this->GetQpriority();
1897
		if (!empty($tmpvalue)) {
1898
			$form .= '	<dt>';
1899
			$form .=		'Priority';
1900
			$form .= '	<dt>';
1901
			$form .= '	<dd>';
1902
			$form .=		'On';
1903
			$form .= '	</dd>';
1904
		}
1905

    
1906
		$tmpvalue = $this->GetDefault();
1907
		if (!empty($tmpvalue)) {
1908
			$form .= '	<dt>';
1909
			$form .=		'Default';
1910
			$form .= '	<dt>';
1911
			$form .= '	<dd>';
1912
			$form .=		'On';
1913
			$form .= '	</dd>';
1914
		}
1915

    
1916
			$form .= '	<dt>';
1917
			$form .= 'Delete';
1918
			$form .= '	<dt>';
1919
			$form .= '	<dd>';
1920

    
1921
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1922
			$form .= $this->GetInterface() . '&amp;queue=';
1923
			$form .= $this->GetQname() . '&amp;action=delete">';
1924
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1925
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1926

    
1927
			$form .= '	</dd>';
1928

    
1929
			$form .= '</dl>';
1930

    
1931
		return $form;
1932

    
1933
	}
1934

    
1935
	function update_altq_queue_data(&$q) {
1936
		$this->ReadConfig($q);
1937
	}
1938

    
1939
	function wconfig() {
1940
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1941
		if (!is_array($cflink)) {
1942
			$cflink = array();
1943
		}
1944
		$cflink['name'] = $this->GetQname();
1945
		$cflink['interface'] = $this->GetInterface();
1946
		$cflink['qlimit'] = trim($this->GetQlimit());
1947
		if (empty($cflink['qlimit'])) {
1948
			unset($cflink['qlimit']);
1949
		}
1950
		$cflink['priority'] = trim($this->GetQpriority());
1951
		if (!is_numeric($cflink['priority'])) {
1952
			unset($cflink['priority']);
1953
		}
1954
		$cflink['description'] = trim($this->GetDescription());
1955
		if (empty($cflink['description'])) {
1956
			unset($cflink['description']);
1957
		}
1958
		$cflink['enabled'] = trim($this->GetEnabled());
1959
		if (empty($cflink['enabled'])) {
1960
			unset($cflink['enabled']);
1961
		}
1962
		$cflink['default'] = trim($this->GetDefault());
1963
		if (empty($cflink['default'])) {
1964
			unset($cflink['default']);
1965
		}
1966
		$cflink['red'] = trim($this->GetRed());
1967
		if (empty($cflink['red'])) {
1968
			unset($cflink['red']);
1969
		}
1970
		$cflink['codel'] = trim($this->GetCodel());
1971
		if (empty($cflink['codel'])) {
1972
			unset($cflink['codel']);
1973
		}
1974
		$cflink['rio'] = trim($this->GetRio());
1975
		if (empty($cflink['rio'])) {
1976
			unset($cflink['rio']);
1977
		}
1978
		$cflink['ecn'] = trim($this->GetEcn());
1979
		if (empty($cflink['ecn'])) {
1980
			unset($cflink['ecn']);
1981
		}
1982
	}
1983
}
1984

    
1985
class hfsc_queue extends priq_queue {
1986
	/* realtime */
1987
	var $realtime;
1988
	var $r_m1;
1989
	var $r_d;
1990
	var $r_m2;
1991
	/* linkshare */
1992
	var $linkshare;
1993
	var $l_m1;
1994
	var $l_d;
1995
	var $l_m2;
1996
	/* upperlimit */
1997
	var $upperlimit;
1998
	var $u_m1;
1999
	var $u_d;
2000
	var $u_m2;
2001

    
2002
	/*
2003
	 * HFSC can have nested queues.
2004
	 */
2005
	function CanHaveChildren() {
2006
		return true;
2007
	}
2008
	function GetRealtime() {
2009
		return $this->realtime;
2010
	}
2011
	function GetR_m1() {
2012
		return $this->r_m1;
2013
	}
2014
	function GetR_d() {
2015
		return $this->r_d;
2016
	}
2017
	function GetR_m2() {
2018
		return $this->r_m2;
2019
	}
2020
	function SetRealtime() {
2021
		$this->realtime = "on";
2022
	}
2023
	function DisableRealtime() {
2024
		$this->realtime = "";
2025
	}
2026
	function SetR_m1($value) {
2027
		$this->r_m1 = $value;
2028
	}
2029
	function SetR_d($value) {
2030
		$this->r_d = $value;
2031
	}
2032
	function SetR_m2($value) {
2033
		$this->r_m2 = $value;
2034
	}
2035
	function GetLinkshare() {
2036
		return $this->linkshare;
2037
	}
2038
	function DisableLinkshare() {
2039
		$this->linkshare = "";
2040
	}
2041
	function GetL_m1() {
2042
		return $this->l_m1;
2043
	}
2044
	function GetL_d() {
2045
		return $this->l_d;
2046
	}
2047
	function GetL_m2() {
2048
		return $this->l_m2;
2049
	}
2050
	function SetLinkshare() {
2051
		$this->linkshare = "on";
2052
	}
2053
	function SetL_m1($value) {
2054
		$this->l_m1 = $value;
2055
	}
2056
	function SetL_d($value) {
2057
		$this->l_d = $value;
2058
	}
2059
	function SetL_m2($value) {
2060
		$this->l_m2 = $value;
2061
	}
2062
	function GetUpperlimit() {
2063
		return $this->upperlimit;
2064
	}
2065
	function GetU_m1() {
2066
		return $this->u_m1;
2067
	}
2068
	function GetU_d() {
2069
		return $this->u_d;
2070
	}
2071
	function GetU_m2() {
2072
		return $this->u_m2;
2073
	}
2074
	function SetUpperlimit() {
2075
		$this->upperlimit = "on";
2076
	}
2077
	function DisableUpperlimit() {
2078
		$this->upperlimit = "";
2079
	}
2080
	function SetU_m1($value) {
2081
		$this->u_m1 = $value;
2082
	}
2083
	function SetU_d($value) {
2084
		$this->u_d = $value;
2085
	}
2086
	function SetU_m2($value) {
2087
		$this->u_m2 = $value;
2088
	}
2089

    
2090
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2091

    
2092
		if (!is_array($this->subqueues)) {
2093
			$this->subqueues = array();
2094
		}
2095
		$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
2096
		$q->SetInterface($this->GetInterface());
2097
		$q->SetParent($this);
2098
		$q->ReadConfig($qname);
2099
		$q->validate_input($qname, $input_errors);
2100

    
2101
		$q->SetEnabled("on");
2102
		$q->SetLink($path);
2103

    
2104
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
2105
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2106
		if (is_array($qname['queue'])) {
2107
			foreach ($qname['queue'] as $key1 => $que) {
2108
				array_push($path, $key1);
2109
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2110
				array_pop($path);
2111
			}
2112
		}
2113

    
2114
		return $q;
2115
	}
2116

    
2117
	function copy_queue($interface, &$cflink) {
2118

    
2119
		$cflink['name'] = $this->GetQname();
2120
		$cflink['interface'] = $interface;
2121
		$cflink['qlimit'] = trim($this->GetQlimit());
2122
		if (empty($cflink['qlimit'])) {
2123
			unset($cflink['qlimit']);
2124
		}
2125
		$cflink['priority'] = trim($this->GetQpriority());
2126
		if (empty($cflink['priority'])) {
2127
			unset($cflink['priority']);
2128
		}
2129
		$cflink['description'] = trim($this->GetDescription());
2130
		if (empty($cflink['description'])) {
2131
			unset($cflink['description']);
2132
		}
2133
		$cflink['bandwidth'] = $this->GetBandwidth();
2134
		$cflink['bandwidthtype'] = $this->GetBwscale();
2135
		$cflink['enabled'] = trim($this->GetEnabled());
2136
		if (empty($cflink['enabled'])) {
2137
			unset($cflink['enabled']);
2138
		}
2139
		$cflink['default'] = trim($this->GetDefault());
2140
		if (empty($cflink['default'])) {
2141
			unset($cflink['default']);
2142
		}
2143
		$cflink['red'] = trim($this->GetRed());
2144
		if (empty($cflink['red'])) {
2145
			unset($cflink['red']);
2146
		}
2147
		$cflink['rio'] = trim($this->GetRio());
2148
		if (empty($cflink['rio'])) {
2149
			unset($cflink['rio']);
2150
		}
2151
		$cflink['ecn'] = trim($this->GetEcn());
2152
		if (empty($cflink['ecn'])) {
2153
			unset($cflink['ecn']);
2154
		}
2155
		if ($this->GetLinkshare() <> "") {
2156
			if ($this->GetL_m1() <> "") {
2157
				$cflink['linkshare1'] = $this->GetL_m1();
2158
				$cflink['linkshare2'] = $this->GetL_d();
2159
				$cflink['linkshare'] = "on";
2160
			} else {
2161
				unset($cflink['linkshare1']);
2162
				unset($cflink['linkshare2']);
2163
				unset($cflink['linkshare']);
2164
			}
2165
			if ($this->GetL_m2() <> "") {
2166
				$cflink['linkshare3'] = $this->GetL_m2();
2167
				$cflink['linkshare'] = "on";
2168
			} else {
2169
				unset($cflink['linkshare3']);
2170
				unset($cflink['linkshare']);
2171
			}
2172
		}
2173
		if ($this->GetRealtime() <> "") {
2174
			if ($this->GetR_m1() <> "") {
2175
				$cflink['realtime1'] = $this->GetR_m1();
2176
				$cflink['realtime2'] = $this->GetR_d();
2177
				$cflink['realtime'] = "on";
2178
			} else {
2179
				unset($cflink['realtime1']);
2180
				unset($cflink['realtime2']);
2181
				unset($cflink['realtime']);
2182
			}
2183
			if ($this->GetR_m2() <> "") {
2184
				$cflink['realtime3'] = $this->GetR_m2();
2185
				$cflink['realtime'] = "on";
2186
			} else {
2187
				unset($cflink['realtime3']);
2188
				unset($cflink['realtime']);
2189
			}
2190
		}
2191
		if ($this->GetUpperlimit() <> "") {
2192
			if ($this->GetU_m1() <> "") {
2193
				$cflink['upperlimit1'] = $this->GetU_m1();
2194
				$cflink['upperlimit2'] = $this->GetU_d();
2195
				$cflink['upperlimit'] = "on";
2196
			} else {
2197
				unset($cflink['upperlimit']);
2198
				unset($cflink['upperlimit1']);
2199
				unset($cflink['upperlimit2']);
2200
			}
2201
			if ($this->GetU_m2() <> "") {
2202
				$cflink['upperlimit3'] = $this->GetU_m2();
2203
				$cflink['upperlimit'] = "on";
2204
			} else {
2205
				unset($cflink['upperlimit3']);
2206
				unset($cflink['upperlimit']);
2207
			}
2208
		}
2209

    
2210
		if (is_array($this->subqueues)) {
2211
			$cflinkp['queue'] = array();
2212
			foreach ($this->subqueues as $q) {
2213
				$cflink['queue'][$q->GetQname()] = array();
2214
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2215
			}
2216
		}
2217
	}
2218

    
2219
	function delete_queue() {
2220
		unref_on_altq_queue_list($this->GetQname());
2221
		cleanup_queue_from_rules($this->GetQname());
2222
		$parent =& $this->GetParent();
2223
		foreach ($this->subqueues as $q) {
2224
			$q->delete_queue();
2225
		}
2226
		unset_object_by_reference($this->GetLink());
2227
	}
2228

    
2229
	/*
2230
	 * Should search even its children
2231
	 */
2232
	function &find_queue($interface, $qname) {
2233
		if ($qname == $this->GetQname()) {
2234
			return $this;
2235
		}
2236

    
2237
		foreach ($this->subqueues as $q) {
2238
			$result =& $q->find_queue("", $qname);
2239
			if ($result) {
2240
				return $result;
2241
			}
2242
		}
2243
	}
2244

    
2245
	function &find_parentqueue($interface, $qname) {
2246
		if ($this->subqueues[$qname]) {
2247
			return $this;
2248
		}
2249
		foreach ($this->subqueues as $q) {
2250
			$result = $q->find_parentqueue("", $qname);
2251
			if ($result) {
2252
				return $result;
2253
			}
2254
		}
2255
	}
2256

    
2257
	function validate_input($data, &$input_errors) {
2258
		parent::validate_input($data, $input_errors);
2259

    
2260
		$reqdfields[] = "bandwidth";
2261
		$reqdfieldsn[] = gettext("Bandwidth");
2262
		$reqdfields[] = "bandwidthtype";
2263
		$reqdfieldsn[] = gettext("Bandwidthtype");
2264

    
2265
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2266

    
2267
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2268
			if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2269
				$input_errors[] = gettext("Bandwidth must be an integer.");
2270
			}
2271

    
2272
			if ($data['bandwidth'] < 0) {
2273
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2274
			}
2275

    
2276
			if ($data['bandwidthtype'] == "%") {
2277
				if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
2278
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2279
				}
2280
			}
2281
		}
2282

    
2283
		if (!empty($data['upperlimit1']) && empty($data['upperlimit2'])) {
2284
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
2285
		}
2286
		if (!empty($data['upperlimit2']) && empty($data['upperlimit1'])) {
2287
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
2288
		}
2289
		if (!empty($data['upperlimit1']) && !is_valid_shaperbw($data['upperlimit1'])) {
2290
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
2291
		}
2292
		if (!empty($data['upperlimit2']) && !is_numeric($data['upperlimit2'])) {
2293
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
2294
		}
2295
		if (!empty($data['upperlimit3']) && !is_valid_shaperbw($data['upperlimit3'])) {
2296
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
2297
		}
2298

    
2299
		/*
2300
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
2301
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
2302
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
2303
			if (floatval($bw_1) < floatval($bw_2)) {
2304
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
2305
			}
2306

    
2307
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2308
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
2309
			}
2310
		}
2311
		*/
2312
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
2313
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
2314
		}
2315
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
2316
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
2317
		}
2318
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
2319
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
2320
		}
2321
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
2322
			$input_errors[] = gettext("linkshare d value needs to be numeric");
2323
		}
2324
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
2325
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
2326
		}
2327
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2328
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2329
		}
2330
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2331
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2332
		}
2333

    
2334
		/*
2335
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2336
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2337
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2338
			if (floatval($bw_1) < floatval($bw_2)) {
2339
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2340
			}
2341

    
2342
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2343
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2344
			}
2345
		}
2346
		*/
2347

    
2348
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2349
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2350
		}
2351
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2352
			$input_errors[] = gettext("realtime d value needs to be numeric");
2353
		}
2354
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2355
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2356
		}
2357

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

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

    
2373
	function ReadConfig(&$cflink) {
2374
		if (!empty($cflink['linkshare'])) {
2375
			if (!empty($cflink['linkshare1'])) {
2376
				$this->SetL_m1($cflink['linkshare1']);
2377
				$this->SetL_d($cflink['linkshare2']);
2378
				$this->SetLinkshare();
2379
			} else {
2380
				$this->SetL_m1("");
2381
				$this->SetL_d("");
2382
				$this->DisableLinkshare();
2383
			}
2384
			if (!empty($cflink['linkshare3'])) {
2385
				$this->SetL_m2($cflink['linkshare3']);
2386
				$this->SetLinkshare();
2387
			}
2388
		} else {
2389
			$this->DisableLinkshare();
2390
		}
2391
		if (!empty($cflink['realtime'])) {
2392
			if (!empty($cflink['realtime1'])) {
2393
				$this->SetR_m1($cflink['realtime1']);
2394
				$this->SetR_d($cflink['realtime2']);
2395
				$this->SetRealtime();
2396
			} else {
2397
				$this->SetR_m1("");
2398
				$this->SetR_d("");
2399
				$this->DisableRealtime();
2400
			}
2401
			if (!empty($cflink['realtime3'])) {
2402
				$this->SetR_m2($cflink['realtime3']);
2403
				$this->SetRealtime();
2404
			}
2405
		} else {
2406
			$this->DisableRealtime();
2407
		}
2408
		if (!empty($cflink['upperlimit'])) {
2409
			if (!empty($cflink['upperlimit1'])) {
2410
				$this->SetU_m1($cflink['upperlimit1']);
2411
				$this->SetU_d($cflink['upperlimit2']);
2412
				$this->SetUpperlimit();
2413
			} else {
2414
				$this->SetU_m1("");
2415
				$this->SetU_d("");
2416
				$this->DisableUpperlimit();
2417
			}
2418
			if (!empty($cflink['upperlimit3'])) {
2419
				$this->SetU_m2($cflink['upperlimit3']);
2420
				$this->SetUpperlimit();
2421
			}
2422
		} else {
2423
			$this->DisableUpperlimit();
2424
		}
2425
		parent::ReadConfig($cflink);
2426
	}
2427

    
2428
	function build_tree() {
2429
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
2430
		$tree .= "\" ";
2431
		$tmpvalue = $this->GetDefault();
2432
		if (!empty($tmpvalue)) {
2433
			$tree .= " class=\"navlnk\"";
2434
		}
2435
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
2436
		if (is_array($this->subqueues)) {
2437
			$tree .= "<ul>";
2438
			foreach ($this->subqueues as $q) {
2439
				$tree .= $q->build_tree();
2440
			}
2441
			$tree .= "</ul>";
2442
		}
2443
		$tree .= "</li>";
2444
		return $tree;
2445
	}
2446

    
2447
	/* Even this should take children into consideration */
2448
	function build_rules(&$default = false) {
2449

    
2450
		$pfq_rule = " queue ". $this->qname;
2451
		if ($this->GetInterface()) {
2452
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2453
		}
2454
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2455
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2456
		}
2457

    
2458
		$tmpvalue = $this->GetQlimit();
2459
		if (!empty($tmpvalue)) {
2460
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2461
		}
2462
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2463
			$pfq_rule .= " hfsc ( ";
2464
			$tmpvalue = $this->GetRed();
2465
			if (!empty($tmpvalue)) {
2466
				$comma = 1;
2467
				$pfq_rule .= " red ";
2468
			}
2469

    
2470
			$tmpvalue = $this->GetRio();
2471
			if (!empty($tmpvalue)) {
2472
				if ($comma) {
2473
					$pfq_rule .= " ,";
2474
				}
2475
				$comma = 1;
2476
				$pfq_rule .= " rio ";
2477
			}
2478
			$tmpvalue = $this->GetEcn();
2479
			if (!empty($tmpvalue)) {
2480
				if ($comma) {
2481
					$pfq_rule .= " ,";
2482
				}
2483
				$comma = 1;
2484
				$pfq_rule .= " ecn ";
2485
			}
2486
			$tmpvalue = $this->GetCodel();
2487
			if (!empty($tmpvalue)) {
2488
				if ($comma) {
2489
					$pfq_rule .= " ,";
2490
				}
2491
				$comma = 1;
2492
				$pfq_rule .= " codel ";
2493
			}
2494
			$tmpvalue = $this->GetDefault();
2495
			if (!empty($tmpvalue)) {
2496
				if ($comma) {
2497
					$pfq_rule .= " ,";
2498
				}
2499
				$comma = 1;
2500
				$pfq_rule .= " default ";
2501
				$default = true;
2502
			}
2503

    
2504
			if ($this->GetRealtime() <> "") {
2505
				if ($comma) {
2506
					$pfq_rule .= " , ";
2507
				}
2508
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2509
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2510
				} else if ($this->GetR_m2() <> "") {
2511
					$pfq_rule .= " realtime " . $this->GetR_m2();
2512
				}
2513
				$comma = 1;
2514
			}
2515
			if ($this->GetLinkshare() <> "") {
2516
				if ($comma) {
2517
					$pfq_rule .= " ,";
2518
				}
2519
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2520
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2521
				} else if ($this->GetL_m2() <> "") {
2522
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2523
				}
2524
				$comma = 1;
2525
			}
2526
			if ($this->GetUpperlimit() <> "") {
2527
				if ($comma) {
2528
					$pfq_rule .= " ,";
2529
				}
2530
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2531
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2532
				} else if ($this->GetU_m2() <> "") {
2533
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2534
				}
2535
			}
2536
			$pfq_rule .= " ) ";
2537
		}
2538
		if (count($this->subqueues)) {
2539
			$i = count($this->subqueues);
2540
			$pfq_rule .= " { ";
2541
			foreach ($this->subqueues as $qkey => $qnone) {
2542
				if ($i > 1) {
2543
					$i--;
2544
					$pfq_rule .= " {$qkey}, ";
2545
				} else {
2546
					$pfq_rule .= " {$qkey} ";
2547
				}
2548
			}
2549
			$pfq_rule .= " } \n";
2550
			foreach ($this->subqueues as $q) {
2551
				$pfq_rule .= $q->build_rules($default);
2552
			}
2553
		}
2554

    
2555
		$pfq_rule .= " \n";
2556

    
2557
		return $pfq_rule;
2558
	}
2559

    
2560
	function build_javascript() {
2561

    
2562
		$javascript = <<<EOJS
2563
<script type="text/javascript">
2564
//<![CDATA[
2565
	events.push(function(){
2566

    
2567
		// Disables the specified input element
2568
		function disableInput(id, disable) {
2569
			$('#' + id).prop("disabled", disable);
2570
		}
2571

    
2572
		// Upperlimit
2573
		function enable_upperlimit() {
2574
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2575
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2576
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2577
		}
2578

    
2579
		$('#upperlimit').click(function () {
2580
			enable_upperlimit();
2581
		});
2582

    
2583
		enable_upperlimit();
2584

    
2585
		// realtime
2586
		function enable_realtime() {
2587
			disableInput('realtime1', !$('#realtime').prop('checked'));
2588
			disableInput('realtime2', !$('#realtime').prop('checked'));
2589
			disableInput('realtime3', !$('#realtime').prop('checked'));
2590
		}
2591

    
2592
		$('#realtime').click(function () {
2593
			enable_realtime();
2594
		});
2595

    
2596
		enable_realtime();
2597

    
2598
		// linkshare
2599
		function enable_linkshare() {
2600
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2601
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2602
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2603
		}
2604

    
2605
		$('#linkshare').click(function () {
2606
			enable_linkshare();
2607
		});
2608

    
2609
		enable_linkshare();
2610
	});
2611
//]]>
2612
</script>
2613
EOJS;
2614

    
2615
		return $javascript;
2616
	}
2617

    
2618
	function build_form() {
2619

    
2620
		$sform = parent::build_form();
2621

    
2622
		$section = new Form_Section('Service Curve (sc)');
2623

    
2624
		$group = new Form_Group('Bandwidth');
2625

    
2626
		$group->add(new Form_Input(
2627
			'bandwidth',
2628
			null,
2629
			'number',
2630
			$this->GetBandwidth(),
2631
			['step' => 'any', 'min' => '0.000']
2632
		));
2633

    
2634
		$group->add(new Form_Select(
2635
			'bandwidthtype',
2636
			null,
2637
			$this->GetBwscale(),
2638
			array('Kb' => 'Kbit/s',
2639
				  'Mb' => 'Mbit/s',
2640
				  'Gb' => 'Gbit/s',
2641
				  'b' => 'Bit/s',
2642
				  '%' => '%')
2643
		));
2644

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

    
2647
		$section->add($group);
2648

    
2649
		$group = new Form_Group('Max bandwidth for queue.');
2650

    
2651
		$group->add(new Form_Checkbox(
2652
			'upperlimit',
2653
			null,
2654
			'Upper Limit',
2655
			($this->GetUpperlimit()<> "")
2656
		));
2657

    
2658
		$group->add(new Form_Input(
2659
			'upperlimit1',
2660
			null,
2661
			'text',
2662
			$this->GetU_m1()
2663
		))->setHelp('m1');
2664

    
2665
		$group->add(new Form_Input(
2666
			'upperlimit2',
2667
			null,
2668
			'text',
2669
			$this->GetU_d()
2670
		))->setHelp('d');
2671

    
2672
		$group->add(new Form_Input(
2673
			'upperlimit3',
2674
			null,
2675
			'text',
2676
			$this->GetU_m2()
2677
		))->setHelp('m2');
2678

    
2679

    
2680
		$section->add($group);
2681

    
2682
		$group = new Form_Group('Min bandwidth for queue.');
2683

    
2684
		$group->add(new Form_Checkbox(
2685
			'realtime',
2686
			null,
2687
			'Real Time',
2688
			($this->GetRealtime()<> "")
2689
		));
2690

    
2691
		$group->add(new Form_Input(
2692
			'realtime1',
2693
			null,
2694
			'text',
2695
			$this->GetR_m1()
2696
		))->setHelp('m1');
2697

    
2698
		$group->add(new Form_Input(
2699
			'realtime2',
2700
			null,
2701
			'text',
2702
			$this->GetR_d()
2703
		))->setHelp('d');
2704

    
2705
		$group->add(new Form_Input(
2706
			'realtime3',
2707
			null,
2708
			'text',
2709
			$this->GetR_m2()
2710
		))->setHelp('m2');
2711

    
2712
		$section->add($group);
2713

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

    
2716
		$group->add(new Form_Checkbox(
2717
			'linkshare',
2718
			null,
2719
			'Link Share',
2720
			($this->GetLinkshare()<> "")
2721
		));
2722

    
2723
		$group->add(new Form_Input(
2724
			'linkshare1',
2725
			null,
2726
			'text',
2727
			$this->GetL_m1()
2728
		))->setHelp('m1');
2729

    
2730
		$group->add(new Form_Input(
2731
			'linkshare2',
2732
			null,
2733
			'text',
2734
			$this->GetL_d()
2735
		))->setHelp('d');
2736

    
2737
		$group->add(new Form_Input(
2738
			'linkshare3',
2739
			null,
2740
			'text',
2741
			$this->GetL_m2()
2742
		))->setHelp('m2');
2743

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

    
2750
		$section->add($group);
2751

    
2752
		$sform->add($section);
2753

    
2754
		return($sform);
2755
	}
2756

    
2757
	function update_altq_queue_data(&$data) {
2758
		$this->ReadConfig($data);
2759
	}
2760

    
2761
	function wconfig() {
2762
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2763
		if (!is_array($cflink)) {
2764
			$cflink = array();
2765
		}
2766
		$cflink['name'] = $this->GetQname();
2767
		$cflink['interface'] = $this->GetInterface();
2768
		$cflink['qlimit'] = trim($this->GetQlimit());
2769
		if (empty($cflink['qlimit'])) {
2770
			unset($cflink['qlimit']);
2771
		}
2772
		$cflink['priority'] = $this->GetQpriority();
2773
		if (!is_numericint($cflink['priority'])) {
2774
			unset($cflink['priority']);
2775
		}
2776
		$cflink['description'] = $this->GetDescription();
2777
		if (empty($cflink['description'])) {
2778
			unset($cflink['description']);
2779
		}
2780
		$cflink['bandwidth'] = $this->GetBandwidth();
2781
		$cflink['bandwidthtype'] = $this->GetBwscale();
2782
		$cflink['enabled'] = $this->GetEnabled();
2783
		if (empty($cflink['enabled'])) {
2784
			unset($cflink['enabled']);
2785
		}
2786
		$cflink['default'] = $this->GetDefault();
2787
		if (empty($cflink['default'])) {
2788
			unset($cflink['default']);
2789
		}
2790
		$cflink['red'] = trim($this->GetRed());
2791
		if (empty($cflink['red'])) {
2792
			unset($cflink['red']);
2793
		}
2794
		$cflink['rio'] = $this->GetRio();
2795
		if (empty($cflink['rio'])) {
2796
			unset($cflink['rio']);
2797
		}
2798
		$cflink['ecn'] = trim($this->GetEcn());
2799
		if (empty($cflink['ecn'])) {
2800
			unset($cflink['ecn']);
2801
		}
2802
		$cflink['codel'] = trim($this->GetCodel());
2803
		if (empty($cflink['codel'])) {
2804
			unset($cflink['codel']);
2805
		}
2806
		if ($this->GetLinkshare() <> "") {
2807
			if ($this->GetL_m1() <> "") {
2808
				$cflink['linkshare1'] = $this->GetL_m1();
2809
				$cflink['linkshare2'] = $this->GetL_d();
2810
				$cflink['linkshare'] = "on";
2811
			} else {
2812
				unset($cflink['linkshare']);
2813
				unset($cflink['linkshare1']);
2814
				unset($cflink['linkshare2']);
2815
			}
2816
			if ($this->GetL_m2() <> "") {
2817
				$cflink['linkshare3'] = $this->GetL_m2();
2818
				$cflink['linkshare'] = "on";
2819
			} else {
2820
				unset($cflink['linkshare']);
2821
				unset($cflink['linkshare3']);
2822
			}
2823
		} else {
2824
			unset($cflink['linkshare']);
2825
			unset($cflink['linkshare1']);
2826
			unset($cflink['linkshare2']);
2827
			unset($cflink['linkshare3']);
2828
		}
2829
		if ($this->GetRealtime() <> "") {
2830
			if ($this->GetR_m1() <> "") {
2831
				$cflink['realtime1'] = $this->GetR_m1();
2832
				$cflink['realtime2'] = $this->GetR_d();
2833
				$cflink['realtime'] = "on";
2834
			} else {
2835
				unset($cflink['realtime']);
2836
				unset($cflink['realtime1']);
2837
				unset($cflink['realtime2']);
2838
			}
2839
			if ($this->GetR_m2() <> "") {
2840
				$cflink['realtime3'] = $this->GetR_m2();
2841
				$cflink['realtime'] = "on";
2842
			} else {
2843
				unset($cflink['realtime']);
2844
				unset($cflink['realtime3']);
2845
			}
2846
		} else {
2847
			unset($cflink['realtime']);
2848
			unset($cflink['realtime1']);
2849
			unset($cflink['realtime2']);
2850
			unset($cflink['realtime3']);
2851
		}
2852
		if ($this->GetUpperlimit() <> "") {
2853
			if ($this->GetU_m1() <> "") {
2854
				$cflink['upperlimit1'] = $this->GetU_m1();
2855
				$cflink['upperlimit2'] = $this->GetU_d();
2856
				$cflink['upperlimit'] = "on";
2857
			} else {
2858
				unset($cflink['upperlimit']);
2859
				unset($cflink['upperlimit1']);
2860
				unset($cflink['upperlimit2']);
2861
			}
2862
			if ($this->GetU_m2() <> "") {
2863
				$cflink['upperlimit3'] = $this->GetU_m2();
2864
				$cflink['upperlimit'] = "on";
2865
			} else {
2866
				unset($cflink['upperlimit']);
2867
				unset($cflink['upperlimit3']);
2868
			}
2869
		} else {
2870
			unset($cflink['upperlimit']);
2871
			unset($cflink['upperlimit1']);
2872
			unset($cflink['upperlimit2']);
2873
			unset($cflink['upperlimit3']);
2874
		}
2875
	}
2876
}
2877

    
2878
class cbq_queue extends priq_queue {
2879
	var $qborrow = "";
2880

    
2881
	function GetBorrow() {
2882
		return $this->qborrow;
2883
	}
2884
	function SetBorrow($borrow) {
2885
		$this->qborrow = $borrow;
2886
	}
2887
	function CanHaveChildren() {
2888
		return true;
2889
	}
2890

    
2891
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2892

    
2893
		if (!is_array($this->subqueues)) {
2894
			$this->subqueues = array();
2895
		}
2896
		$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
2897
		$q->SetInterface($this->GetInterface());
2898
		$q->SetParent($this);
2899
		$q->ReadConfig($qname);
2900
		$q->validate_input($qname, $input_errors);
2901

    
2902
		$q->SetEnabled("on");
2903
		$q->SetLink($path);
2904
		$this->subqueues[$q->GetQName()] = &$q;
2905
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2906
		if (is_array($qname['queue'])) {
2907
			foreach ($qname['queue'] as $key1 => $que) {
2908
				array_push($path, $key1);
2909
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2910
				array_pop($path);
2911
			}
2912
		}
2913

    
2914
		return $q;
2915
	}
2916

    
2917
	function copy_queue($interface, &$cflink) {
2918

    
2919
		$cflink['interface'] = $interface;
2920
		$cflink['qlimit'] = trim($this->GetQlimit());
2921
		if (empty($clink['qlimit'])) {
2922
			unset($cflink['qlimit']);
2923
		}
2924
		$cflink['priority'] = trim($this->GetQpriority());
2925
		if (!is_numeric($cflink['priority'])) {
2926
			unset($cflink['priority']);
2927
		}
2928
		$cflink['name'] = $this->GetQname();
2929
		$cflink['description'] = trim($this->GetDescription());
2930
		if (empty($cflink['description'])) {
2931
			unset($cflink['description']);
2932
		}
2933
		$cflink['bandwidth'] = $this->GetBandwidth();
2934
		$cflink['bandwidthtype'] = $this->GetBwscale();
2935
		$cflink['enabled'] = trim($this->GetEnabled());
2936
		if (empty($cflink['enabled'])) {
2937
			unset($cflink['enabled']);
2938
		}
2939
		$cflink['default'] = trim($this->GetDefault());
2940
		if (empty($cflink['default'])) {
2941
			unset($cflink['default']);
2942
		}
2943
		$cflink['red'] = trim($this->GetRed());
2944
		if (empty($cflink['red'])) {
2945
			unset($cflink['red']);
2946
		}
2947
		$cflink['rio'] = trim($this->GetRio());
2948
		if (empty($cflink['rio'])) {
2949
			unset($cflink['rio']);
2950
		}
2951
		$cflink['ecn'] = trim($this->GetEcn());
2952
		if (empty($cflink['ecn'])) {
2953
			unset($cflink['ecn']);
2954
		}
2955
		$cflink['borrow'] = trim($this->GetBorrow());
2956
		if (empty($cflink['borrow'])) {
2957
			unset($cflink['borrow']);
2958
		}
2959
		if (is_array($this->queues)) {
2960
			$cflinkp['queue'] = array();
2961
			foreach ($this->subqueues as $q) {
2962
				$cflink['queue'][$q->GetQname()] = array();
2963
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2964
			}
2965
		}
2966
	}
2967

    
2968
	/*
2969
	 * Should search even its children
2970
	 */
2971
	function &find_queue($interface, $qname) {
2972
		if ($qname == $this->GetQname()) {
2973
			return $this;
2974
		}
2975
		foreach ($this->subqueues as $q) {
2976
			$result =& $q->find_queue("", $qname);
2977
			if ($result) {
2978
				return $result;
2979
			}
2980
		}
2981
	}
2982

    
2983
	function &find_parentqueue($interface, $qname) {
2984
		if ($this->subqueues[$qname]) {
2985
			return $this;
2986
		}
2987
		foreach ($this->subqueues as $q) {
2988
			$result = $q->find_parentqueue("", $qname);
2989
			if ($result) {
2990
				return $result;
2991
			}
2992
		}
2993
	}
2994

    
2995
	function delete_queue() {
2996
		unref_on_altq_queue_list($this->GetQname());
2997
		cleanup_queue_from_rules($this->GetQname());
2998
		foreach ($this->subqueues as $q) {
2999
			$q->delete_queue();
3000
		}
3001
		unset_object_by_reference($this->GetLink());
3002
	}
3003

    
3004
	function validate_input($data, &$input_errors) {
3005
		parent::validate_input($data, $input_errors);
3006

    
3007
		if ($data['priority'] > 7) {
3008
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3009
		}
3010
		$reqdfields[] = "bandwidth";
3011
		$reqdfieldsn[] = gettext("Bandwidth");
3012
		$reqdfields[] = "bandwidthtype";
3013
		$reqdfieldsn[] = gettext("Bandwidthtype");
3014

    
3015
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3016
	}
3017

    
3018
	function ReadConfig(&$q) {
3019
		parent::ReadConfig($q);
3020
		if (!empty($q['borrow'])) {
3021
			$this->SetBorrow("on");
3022
		} else {
3023
			$this->SetBorrow("");
3024
		}
3025
	}
3026

    
3027
	function build_javascript() {
3028
		return parent::build_javascript();
3029
	}
3030

    
3031
	function build_tree() {
3032
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3033
		$tree .= "\" ";
3034
		$tmpvalue = trim($this->GetDefault());
3035
		if (!empty($tmpvalue)) {
3036
			$tree .= " class=\"navlnk\"";
3037
		}
3038
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3039
		if (is_array($this->subqueues)) {
3040
			$tree .= "<ul>";
3041
			foreach ($this->subqueues as $q) {
3042
				$tree .= $q->build_tree();
3043
			}
3044
			$tree .= "</ul>";
3045
		}
3046
		$tree .= "</li>";
3047
		return $tree;
3048
	}
3049

    
3050
	/* Even this should take children into consideration */
3051
	function build_rules(&$default = false) {
3052
		$pfq_rule = "queue ". $this->qname;
3053
		if ($this->GetInterface()) {
3054
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3055
		}
3056
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3057
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3058
		}
3059
		$tmpvalue = $this->GetQpriority();
3060
		if (is_numeric($tmpvalue)) {
3061
			$pfq_rule .= " priority " . $this->GetQpriority();
3062
		}
3063
		$tmpvalue = trim($this->GetQlimit());
3064
		if (!empty($tmpvalue)) {
3065
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3066
		}
3067
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
3068
			$pfq_rule .= " cbq ( ";
3069
			$tmpvalue = trim($this->GetRed());
3070
			if (!empty($tmpvalue)) {
3071
				$comma = 1;
3072
				$pfq_rule .= " red ";
3073
			}
3074
			$tmpvalue = trim($this->GetCodel());
3075
			if (!empty($tmpvalue)) {
3076
				$comma = 1;
3077
				$pfq_rule .= " codel ";
3078
			}
3079
			$tmpvalue = trim($this->GetRio());
3080
			if (!empty($tmpvalue)) {
3081
				if ($comma) {
3082
					$pfq_rule .= " ,";
3083
				}
3084
				$comma = 1;
3085
				$pfq_rule .= " rio ";
3086
			}
3087
			$tmpvalue = trim($this->GetEcn());
3088
			if (!empty($tmpvalue)) {
3089
				if ($comma) {
3090
					$pfq_rule .= " ,";
3091
				}
3092
				$comma = 1;
3093
				$pfq_rule .= " ecn ";
3094
			}
3095
			$tmpvalue = trim($this->GetDefault());
3096
			if (!empty($tmpvalue)) {
3097
				if ($comma) {
3098
					$pfq_rule .= " ,";
3099
				}
3100
				$comma = 1;
3101
				$pfq_rule .= " default ";
3102
				$default = true;
3103
			}
3104
			$tmpvalue = trim($this->GetBorrow());
3105
			if (!empty($tmpvalue)) {
3106
				if ($comma) {
3107
					$pfq_rule .= ", ";
3108
				}
3109
				$pfq_rule .= " borrow ";
3110
			}
3111
			$pfq_rule .= " ) ";
3112
		}
3113
		if (count($this->subqueues)) {
3114
			$i = count($this->subqueues);
3115
			$pfq_rule .= " { ";
3116
			foreach ($this->subqueues as $qkey => $qnone) {
3117
				if ($i > 1) {
3118
					$i--;
3119
					$pfq_rule .= " {$qkey}, ";
3120
				} else {
3121
					$pfq_rule .= " {$qkey} ";
3122
				}
3123
			}
3124
			$pfq_rule .= " } \n";
3125
			foreach ($this->subqueues as $q) {
3126
				$pfq_rule .= $q->build_rules($default);
3127
			}
3128
		}
3129

    
3130
		$pfq_rule .= " \n";
3131
		return $pfq_rule;
3132
	}
3133

    
3134
	function build_form() {
3135
		$sform = parent::build_form();
3136

    
3137
		$section = new Form_Section('NOTITLE');
3138

    
3139
		$group = new Form_Group('Bandwidth');
3140

    
3141
		$group->add(new Form_Input(
3142
			'bandwidth',
3143
			null,
3144
			'number',
3145
			$this->GetBandwidth()
3146
		));
3147

    
3148
		$group->add(new Form_Select(
3149
			'bandwidthtype',
3150
			null,
3151
			$this->GetBwscale(),
3152
			array('Kb' => 'Kbit/s',
3153
				  'Mb' => 'Mbit/s',
3154
				  'Gb' => 'Gbit/s',
3155
				  'b' => 'Bit/s',
3156
				  '%' => '%')
3157
		));
3158

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

    
3161
		$section->add($group);
3162

    
3163
		$section->addInput(new Form_Checkbox(
3164
			'borrow',
3165
			'Scheduler option',
3166
			'Borrow from other queues when available',
3167
			($this->GetBorrow() == "on")
3168
		));
3169

    
3170
		$sform->add($section);
3171

    
3172
		return $sform;
3173
	}
3174

    
3175
	function update_altq_queue_data(&$data) {
3176
		$this->ReadConfig($data);
3177
	}
3178

    
3179
	function wconfig() {
3180
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3181
		if (!is_array($cflink)) {
3182
			$cflink = array();
3183
		}
3184
		$cflink['interface'] = $this->GetInterface();
3185
		$cflink['qlimit'] = trim($this->GetQlimit());
3186
		if (empty($cflink['qlimit'])) {
3187
			unset($cflink['qlimit']);
3188
		}
3189
		$cflink['priority'] = $this->GetQpriority();
3190
		if (!is_numeric($cflink['priority'])) {
3191
			unset($cflink['priority']);
3192
		}
3193
		$cflink['name'] = $this->GetQname();
3194
		$cflink['description'] = $this->GetDescription();
3195
		if (empty($cflink['description'])) {
3196
			unset($cflink['description']);
3197
		}
3198
		$cflink['bandwidth'] = $this->GetBandwidth();
3199
		$cflink['bandwidthtype'] = $this->GetBwscale();
3200
		$cflink['enabled'] = trim($this->GetEnabled());
3201
		if (empty($cflink['enabled'])) {
3202
			unset($cflink['enabled']);
3203
		}
3204
		$cflink['default'] = trim($this->GetDefault());
3205
		if (empty($cflink['default'])) {
3206
			unset($cflink['default']);
3207
		}
3208
		$cflink['red'] = trim($this->GetRed());
3209
		if (empty($cflink['red'])) {
3210
			unset($cflink['red']);
3211
		}
3212
		$cflink['rio'] = trim($this->GetRio());
3213
		if (empty($cflink['rio'])) {
3214
			unset($cflink['rio']);
3215
		}
3216
		$cflink['ecn'] = trim($this->GetEcn());
3217
		if (empty($cflink['ecn'])) {
3218
			unset($cflink['ecn']);
3219
		}
3220
		$cflink['codel'] = trim($this->GetCodel());
3221
		if (empty($cflink['codel'])) {
3222
			unset($cflink['codel']);
3223
		}
3224
		$cflink['borrow'] = trim($this->GetBorrow());
3225
		if (empty($cflink['borrow'])) {
3226
			unset($cflink['borrow']);
3227
		}
3228
	}
3229
}
3230

    
3231
class fairq_queue extends priq_queue {
3232
	var $hogs;
3233
	var $buckets;
3234

    
3235
	function GetBuckets() {
3236
		return $this->buckets;
3237
	}
3238
	function SetBuckets($buckets) {
3239
		$this->buckets = $buckets;
3240
	}
3241
	function GetHogs() {
3242
		return $this->hogs;
3243
	}
3244
	function SetHogs($hogs) {
3245
		$this->hogs = $hogs;
3246
	}
3247
	function CanHaveChildren() {
3248
		return false;
3249
	}
3250

    
3251

    
3252
	function copy_queue($interface, &$cflink) {
3253
		$cflink['interface'] = $interface;
3254
		$cflink['qlimit'] = $this->GetQlimit();
3255
		$cflink['priority'] = $this->GetQpriority();
3256
		$cflink['name'] = $this->GetQname();
3257
		$cflink['description'] = $this->GetDescription();
3258
		$cflink['bandwidth'] = $this->GetBandwidth();
3259
		$cflink['bandwidthtype'] = $this->GetBwscale();
3260
		$cflink['enabled'] = $this->GetEnabled();
3261
		$cflink['default'] = $this->GetDefault();
3262
		$cflink['red'] = $this->GetRed();
3263
		$cflink['rio'] = $this->GetRio();
3264
		$cflink['ecn'] = $this->GetEcn();
3265
		$cflink['buckets'] = $this->GetBuckets();
3266
		$cflink['hogs'] = $this->GetHogs();
3267
	}
3268

    
3269
	/*
3270
	 * Should search even its children
3271
	 */
3272
	function &find_queue($interface, $qname) {
3273
		if ($qname == $this->GetQname()) {
3274
			return $this;
3275
		}
3276
	}
3277

    
3278
	function find_parentqueue($interface, $qname) { return; }
3279

    
3280
	function delete_queue() {
3281
		unref_on_altq_queue_list($this->GetQname());
3282
		cleanup_queue_from_rules($this->GetQname());
3283
		unset_object_by_reference($this->GetLink());
3284
	}
3285

    
3286
	function validate_input($data, &$input_errors) {
3287
		parent::validate_input($data, $input_errors);
3288

    
3289
		if ($data['priority'] > 7) {
3290
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3291
		}
3292
		$reqdfields[] = "bandwidth";
3293
		$reqdfieldsn[] = gettext("Bandwidth");
3294
		$reqdfields[] = "bandwidthtype";
3295
		$reqdfieldsn[] = gettext("Bandwidthtype");
3296

    
3297
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3298
	}
3299

    
3300
	function ReadConfig(&$q) {
3301
		parent::ReadConfig($q);
3302
		if (!empty($q['buckets'])) {
3303
			$this->SetBuckets($q['buckets']);
3304
		} else {
3305
			$this->SetBuckets("");
3306
		}
3307
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3308
			$this->SetHogs($q['hogs']);
3309
		} else {
3310
			$this->SetHogs("");
3311
		}
3312
	}
3313

    
3314
	function build_javascript() {
3315
		return parent::build_javascript();
3316
	}
3317

    
3318
	function build_tree() {
3319
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3320
		$this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3321
		$tree .= "\" ";
3322
		$tmpvalue = trim($this->GetDefault());
3323
		if (!empty($tmpvalue)) {
3324
			$tree .= " class=\"navlnk\"";
3325
		}
3326
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3327
		$tree .= "</li>";
3328
		return $tree;
3329
	}
3330

    
3331
	/* Even this should take children into consideration */
3332
	function build_rules(&$default = false) {
3333
		$pfq_rule = "queue ". $this->qname;
3334
		if ($this->GetInterface()) {
3335
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3336
		}
3337
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3338
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3339
		}
3340
		$tmpvalue = trim($this->GetQpriority());
3341
		if (is_numeric($tmpvalue)) {
3342
			$pfq_rule .= " priority " . $this->GetQpriority();
3343
		}
3344
		$tmpvalue = trim($this->GetQlimit());
3345
		if (!empty($tmpvalue)) {
3346
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3347
		}
3348
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3349
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3350
			$pfq_rule .= " fairq ( ";
3351
			$tmpvalue = trim($this->GetRed());
3352
			if (!empty($tmpvalue)) {
3353
				$comma = 1;
3354
				$pfq_rule .= " red ";
3355
			}
3356
			$tmpvalue = trim($this->GetCodel());
3357
			if (!empty($tmpvalue)) {
3358
				$comma = 1;
3359
				$pfq_rule .= " codel ";
3360
			}
3361
			$tmpvalue = trim($this->GetRio());
3362
			if (!empty($tmpvalue)) {
3363
				if ($comma) {
3364
					$pfq_rule .= " ,";
3365
				}
3366
				$comma = 1;
3367
				$pfq_rule .= " rio ";
3368
			}
3369
			$tmpvalue = trim($this->GetEcn());
3370
			if (!empty($tmpvalue)) {
3371
				if ($comma) {
3372
					$pfq_rule .= " ,";
3373
				}
3374
				$comma = 1;
3375
				$pfq_rule .= " ecn ";
3376
			}
3377
			$tmpvalue = trim($this->GetDefault());
3378
			if (!empty($tmpvalue)) {
3379
				if ($comma) {
3380
					$pfq_rule .= " ,";
3381
				}
3382
				$comma = 1;
3383
				$pfq_rule .= " default ";
3384
				$default = true;
3385
			}
3386
			$tmpvalue = trim($this->GetBuckets());
3387
			if (!empty($tmpvalue)) {
3388
				if ($comma) {
3389
					$pfq_rule .= ", ";
3390
				}
3391
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3392
			}
3393
			$tmpvalue = trim($this->GetHogs());
3394
			if (!empty($tmpvalue)) {
3395
				if ($comma) {
3396
					$pfq_rule .= ", ";
3397
				}
3398
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3399
			}
3400
			$pfq_rule .= " ) ";
3401
		}
3402

    
3403
		$pfq_rule .= " \n";
3404
		return $pfq_rule;
3405
	}
3406

    
3407
	function build_form() {
3408
		$form = parent::build_form();
3409

    
3410
		$section = new Form_Section('');
3411

    
3412
		$group = new Form_Group('Bandwidth');
3413

    
3414
		$group->add(new Form_Input(
3415
			'bandwidth',
3416
			null,
3417
			'number',
3418
			$this->GetBandwidth()
3419
		));
3420

    
3421
		$group->add(new Form_Select(
3422
			'bandwidthtype',
3423
			null,
3424
			$this->GetBwscale(),
3425
			array('Kb' => 'Kbit/s',
3426
				  'Mb' => 'Mbit/s',
3427
				  'Gb' => 'Gbit/s',
3428
				  'b' => 'Bit/s',
3429
				  '%' => '%')
3430
		));
3431

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

    
3434
		$section->add($group);
3435

    
3436
		$section->addInput(new Form_Input(
3437
			'buckets',
3438
			'Scheduler specific options',
3439
			'text',
3440
			$this->GetBuckets()
3441
		))->setHelp('Number of buckets available');
3442

    
3443
		$section->addInput(new Form_Input(
3444
			'hogs',
3445
			'',
3446
			'text',
3447
			$this->GetHogs()
3448
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3449

    
3450
		$form->add($section);
3451
		return $form;
3452
	}
3453

    
3454
	function update_altq_queue_data(&$data) {
3455
		$this->ReadConfig($data);
3456
	}
3457

    
3458
	function wconfig() {
3459
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3460
		if (!is_array($cflink)) {
3461
			$cflink = array();
3462
		}
3463
		$cflink['interface'] = $this->GetInterface();
3464
		$cflink['qlimit'] = trim($this->GetQlimit());
3465
		if (empty($cflink['qlimit'])) {
3466
			unset($cflink['qlimit']);
3467
		}
3468
		$cflink['priority'] = trim($this->GetQpriority());
3469
		if (!is_numeric($cflink['priority'])) {
3470
			unset($cflink['priority']);
3471
		}
3472
		$cflink['name'] = $this->GetQname();
3473
		$cflink['description'] = trim($this->GetDescription());
3474
		if (empty($cflink['description'])) {
3475
			unset($cflink['description']);
3476
		}
3477
		$cflink['bandwidth'] = $this->GetBandwidth();
3478
		$cflink['bandwidthtype'] = $this->GetBwscale();
3479
		$cflink['enabled'] = $this->GetEnabled();
3480
		if (empty($cflink['enabled'])) {
3481
			unset($cflink['enabled']);
3482
		}
3483
		$cflink['default'] = trim($this->GetDefault());
3484
		if (empty($cflink['default'])) {
3485
			unset($cflink['default']);
3486
		}
3487
		$cflink['red'] = trim($this->GetRed());
3488
		if (empty($cflink['red'])) {
3489
			unset($cflink['red']);
3490
		}
3491
		$cflink['rio'] = trim($this->GetRio());
3492
		if (empty($cflink['rio'])) {
3493
			unset($cflink['rio']);
3494
		}
3495
		$cflink['ecn'] = trim($this->GetEcn());
3496
		if (empty($cflink['ecn'])) {
3497
			unset($cflink['ecn']);
3498
		}
3499
		$cflink['codel'] = trim($this->GetCodel());
3500
		if (empty($cflink['codel'])) {
3501
			unset($cflink['codel']);
3502
		}
3503
		$cflink['buckets'] = trim($this->GetBuckets());
3504
		if (empty($cflink['buckets'])) {
3505
			unset($cflink['buckets']);
3506
		}
3507
		$cflink['hogs'] = trim($this->GetHogs());
3508
		if (empty($cflink['hogs'])) {
3509
			unset($cflink['hogs']);
3510
		}
3511
	}
3512
}
3513

    
3514

    
3515
/*
3516
 * dummynet(4) wrappers.
3517
 */
3518

    
3519

    
3520
/*
3521
 * List of respective objects!
3522
 */
3523
$dummynet_pipe_list = array();
3524

    
3525
class dummynet_class {
3526
	var $qname;
3527
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3528
	var $qlimit;
3529
	var $description;
3530
	var $qenabled;
3531
	var $link;
3532
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3533
	var $plr;
3534

    
3535
	var $buckets;
3536
	/* mask parameters */
3537
	var $mask;
3538
	var $noerror;
3539

    
3540
	/* Accessor functions */
3541
	function SetLink($link) {
3542
		$this->link = $link;
3543
	}
3544
	function GetLink() {
3545
		return $this->link;
3546
	}
3547
	function GetMask() {
3548
		if (!isset($this->mask["type"])) {
3549
			$this->mask["type"] = "none";
3550
		}
3551
		return $this->mask;
3552
	}
3553
	function SetMask($mask) {
3554
		$this->mask = $mask;
3555
	}
3556
	function &GetParent() {
3557
		return $this->qparent;
3558
	}
3559
	function SetParent(&$parent) {
3560
		$this->qparent = &$parent;
3561
	}
3562
	function GetEnabled() {
3563
		return $this->qenabled;
3564
	}
3565
	function SetEnabled($value) {
3566
		$this->qenabled = $value;
3567
	}
3568
	function CanHaveChildren() {
3569
		return false;
3570
	}
3571
	function CanBeDeleted() {
3572
		return true;
3573
	}
3574
	function GetQname() {
3575
		return $this->qname;
3576
	}
3577
	function SetQname($name) {
3578
		$this->qname = trim($name);
3579
	}
3580
	function GetQlimit() {
3581
		return $this->qlimit;
3582
	}
3583
	function SetQlimit($limit) {
3584
		$this->qlimit = $limit;
3585
	}
3586
	function GetDescription() {
3587
		return $this->description;
3588
	}
3589
	function SetDescription($str) {
3590
		$this->description = trim($str);
3591
	}
3592
	function GetFirstime() {
3593
		return $this->firsttime;
3594
	}
3595
	function SetFirsttime($number) {
3596
		$this->firsttime = $number;
3597
	}
3598
	function GetBuckets() {
3599
		return $this->buckets;
3600
	}
3601
	function SetBuckets($buckets) {
3602
		$this->buckets = $buckets;
3603
	}
3604
	function SetNumber($number) {
3605
		$this->qnumber = $number;
3606
	}
3607
	function GetNumber() {
3608
		return $this->qnumber;
3609
	}
3610
	function GetPlr() {
3611
		return $this->plr;
3612
	}
3613
	function SetPlr($plr) {
3614
		$this->plr = $plr;
3615
	}
3616

    
3617
	function build_javascript() {
3618
		$javascript .= "<script type=\"text/javascript\">\n";
3619
		$javascript .= "//<![CDATA[\n";
3620
		$javascript .= "function enable_maskbits(enable_over) {\n";
3621
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3622
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3623
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3624
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3625
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3626
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3627
		$javascript .= "} else {\n";
3628
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3629
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3630
		$javascript .= "}}\n";
3631
		$javascript .= "//]]>\n";
3632
		$javascript .= "</script>\n";
3633
		return $javascript;
3634
	}
3635

    
3636
	function validate_input($data, &$input_errors) {
3637
		$reqdfields[] = "bandwidth";
3638
		$reqdfieldsn[] = gettext("Bandwidth");
3639
		/*$reqdfields[] = "burst";
3640
		$reqdfieldsn[] = gettext("Burst"); */
3641
		$reqdfields[] = "bandwidthtype";
3642
		$reqdfieldsn[] = gettext("Bandwidthtype");
3643
		$reqdfields[] = "newname";
3644
		$reqdfieldsn[] = gettext("Name");
3645

    
3646
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3647

    
3648
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3649
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3650
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3651
		}
3652
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3653
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3654
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3655
		}
3656
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3657
			$input_errors[] = gettext("Queue limit must be an integer");
3658
		}
3659
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3660
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3661
		}
3662
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3663
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3664
		}
3665
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3666
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3667
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3668
			}
3669
		}
3670
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3671
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3672
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3673
			}
3674
		}
3675
	}
3676

    
3677
	function build_mask_rules(&$pfq_rule) {
3678
		$mask = $this->GetMask();
3679
		if (!empty($mask['type'])) {
3680
			if ($mask['type'] <> 'none') {
3681
				$pfq_rule .= " mask";
3682
			}
3683
			switch ($mask['type']) {
3684
				case 'srcaddress':
3685
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3686
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3687
					} else {
3688
						$pfq_rule .= " src-ip6 /128";
3689
					}
3690
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3691
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3692
					} else {
3693
						$pfq_rule .= " src-ip 0xffffffff";
3694
					}
3695
					break;
3696
				case 'dstaddress':
3697
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3698
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3699
					} else {
3700
						$pfq_rule .= " dst-ip6 /128";
3701
					}
3702
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3703
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3704
					} else {
3705
						$pfq_rule .= " dst-ip 0xffffffff";
3706
					}
3707
					break;
3708
				default:
3709
					break;
3710
			}
3711
		}
3712
	}
3713

    
3714
}
3715

    
3716
class dnpipe_class extends dummynet_class {
3717
	var $delay;
3718
	var $qbandwidth = array();
3719
	var $qbandwidthtype;
3720

    
3721
	/* Limiter queue patch */
3722
	var $ecn; // ecn 'on' or 'off'
3723
	var $aqm; // key to aqm_map
3724
	var $aqm_params = array(); // AQM params
3725
	var $scheduler;	// key to scheduler_map
3726
	var $scheduler_params = array(); // AQM params
3727
	function GetAQM() {
3728
			return $this->aqm;
3729
	}
3730
	function SetAQM($aqm) {
3731
			$this->aqm = $aqm;
3732
	}
3733
	function GetAQMParameters() {
3734
			return $this->aqm_params;
3735
	}
3736
	function GetAQMParameter($parameter) {
3737
			return $this->aqm_params[$parameter];
3738
	}
3739
	function SetAQMParameter($key, $value) {
3740
			return $this->aqm_params[$key] = $value;
3741
	}
3742
	function SetAQMParameters($params) {
3743
			$this->aqm_params = $params;
3744
	}
3745
	function GetECN() {
3746
			return $this->ecn;
3747
	}
3748
	function SetECN($ecn) {
3749
			$this->ecn = $ecn;
3750
	}
3751
	function GetScheduler() {
3752
			return $this->scheduler;
3753
	}
3754
	function SetScheduler($scheduler) {
3755
			$this->scheduler = $scheduler;
3756
	}
3757
	function GetSchedulerParameters() {
3758
			return $this->scheduler_params;
3759
	}
3760
	function SetSchedulerParameters($params) {
3761
			$this->scheduler_params = $params;
3762
	}
3763
	function SetSchedulerParameter($key, $value) {
3764
			$this->scheduler_params[$key] = $value;
3765
	}
3766
	function GetSchedulerParameter($key) {
3767
			return $this->scheduler_params[$key];
3768
	}
3769
	/* End limiter queue patch */
3770

    
3771
		/* This is here to help on form building and building rules/lists */
3772
	var $subqueues = array();
3773

    
3774
	function CanHaveChildren() {
3775
		return true;
3776
	}
3777
	function SetDelay($delay) {
3778
		$this->delay = $delay;
3779
	}
3780
	function GetDelay() {
3781
		return $this->delay;
3782
	}
3783
	function delete_queue() {
3784
		cleanup_dnqueue_from_rules($this->GetQname());
3785
		foreach ($this->subqueues as $q) {
3786
			$q->delete_queue();
3787
		}
3788
		unset_dn_object_by_reference($this->GetLink());
3789
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3790
		@pfSense_ipfw_pipe("sched delete " . $this->GetNumber());
3791
	}
3792
	function GetBandwidth() {
3793
		return $this->qbandwidth;
3794
	}
3795
	function SetBandwidth($bandwidth) {
3796
		$this->qbandwidth = $bandwidth;
3797
	}
3798
	function GetBurst() {
3799
		return $this->qburst;
3800
	}
3801
	function SetBurst($burst) {
3802
		$this->qburst = $burst;
3803
	}
3804

    
3805
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3806

    
3807
		if (!is_array($this->subqueues)) {
3808
			$this->subqueues = array();
3809
		}
3810

    
3811
		$__tmp_q = new dnqueue_class(); $q =& $__tmp_q;
3812
		$q->SetLink($path);
3813
		$q->SetEnabled("on");
3814
		$q->SetPipe($this->GetQname());
3815
		$q->SetParent($this);
3816
		$q->ReadConfig($queue);
3817
		$q->validate_input($queue, $input_errors);
3818

    
3819
		if (!is_array($input_errors)) {
3820
			$input_errors = array();
3821
		}
3822

    
3823
		if (count($input_errors)) {
3824
			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)));
3825
			return $q;
3826
		}
3827
		$number = dnqueue_find_nextnumber();
3828
		$q->SetNumber($number);
3829
		$this->subqueues[$q->GetQname()] = &$q;
3830

    
3831
		return $q;
3832
	}
3833

    
3834
	function &get_queue_list(&$q = null) {
3835
		$qlist = array();
3836

    
3837
		$qlist[$this->GetQname()] = $this->GetNumber();
3838
		if (is_array($this->subqueues)) {
3839
			foreach ($this->subqueues as $queue) {
3840
				$queue->get_queue_list($qlist);
3841
			}
3842
		}
3843
		return $qlist;
3844
	}
3845

    
3846
	/*
3847
	 * Should search even its children
3848
	 */
3849
	function &find_queue($pipe, $qname) {
3850
		if ($qname == $this->GetQname()) {
3851
			return $this;
3852
		}
3853
		foreach ($this->subqueues as $q) {
3854
			$result =& $q->find_queue("", $qname);
3855
			if ($result) {
3856
				return $result;
3857
			}
3858
		}
3859
	}
3860

    
3861
	function &find_parentqueue($pipe, $qname) {
3862
		return NULL;
3863
	}
3864

    
3865
	function validate_input($data, &$input_errors) {
3866
		parent::validate_input($data, $input_errors);
3867

    
3868
		$schedule = 0;
3869
		$schedulenone = 0;
3870
		$entries = 0;
3871
		/* XXX: Really no better way? */
3872
		for ($i = 0; $i < 2900; $i++) {
3873
			if (!empty($data["bwsched{$i}"])) {
3874
				if ($data["bwsched{$i}"] != "none") {
3875
					$schedule++;
3876
				} else {
3877
					$schedulenone++;
3878
				}
3879
			}
3880
			if (!empty($data["bandwidth{$i}"])) {
3881
				if (!is_numeric($data["bandwidth{$i}"])) {
3882
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3883
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3884
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3885
				} else {
3886
					$entries++;
3887
				}
3888
			}
3889
		}
3890
		if ($schedule == 0 && $entries > 1) {
3891
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3892
		}
3893
		if ($schedulenone > 0 && $entries > 1) {
3894
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3895
		}
3896
		if ($entries == 0) {
3897
			$input_errors[] = gettext("At least one bw specification is necessary.");
3898
		}
3899
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3900
			$input_errors[] = gettext("Delay must be an integer.");
3901
		}
3902
		if ($data['delay'] && is_numeric($data['delay']) &&
3903
		    (($data['delay'] < 0) || ($data['delay'] > 10000))) {
3904
			$input_errors[] = gettext("Delay must be an integer between 0 and 10000.");
3905
		}
3906

    
3907
		/* Limiter patch */
3908
		$selectedScheduler = getSchedulers()[$data['sched']];
3909
		if (!$selectedScheduler) {
3910
			$input_errors[] = gettext("Selected scheduler not recognized.");
3911
		}
3912
		$selectedAqm = getAQMs()[$data['aqm']];
3913
		if (!empty($data['aqm']) && !$selectedAqm) {
3914
			$input_errors[] = gettext("Selected AQM not recognized.");
3915
		}
3916
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedScheduler["ecn"] && !$selectedAqm["ecn"]) {
3917
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but neither " . $selectedAqm["name"] . " nor " . $selectedScheduler["name"] . " support it.");
3918
		}
3919
		/* End limiter patch */
3920
	}
3921

    
3922
	function ReadConfig(&$q) {
3923
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3924
			$this->SetQname($q['newname']);
3925
			rename_dnqueue_in_rules($q['name'], $q['newname']);
3926
		} else if (!empty($q['newname'])) {
3927
			$this->SetQname($q['newname']);
3928
		} else {
3929
			$this->SetQname($q['name']);
3930
		}
3931
		$this->SetNumber($q['number']);
3932

    
3933
		if (!empty($_POST)) {
3934
			$bandwidth = array();
3935
			/* XXX: Really no better way? */
3936
			for ($i = 0; $i < 2900; $i++) {
3937
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3938
					$bw = array();
3939
					$bw['bw'] = $q["bandwidth{$i}"];
3940
					$bw['burst'] = $q["burst{$i}"];
3941
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3942
						$bw['bwscale'] = $q["bwtype{$i}"];
3943
					}
3944
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3945
						$bw['bwsched'] = $q["bwsched{$i}"];
3946
					}
3947
					$bandwidth[] = $bw;
3948
				}
3949
			}
3950
			$this->SetBandwidth($bandwidth);
3951
		}
3952

    
3953
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3954
			$this->SetBandwidth($q['bandwidth']['item']);
3955
			$this->SetBurst($q['burst']['item']);
3956
		}
3957

    
3958
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3959
			$this->SetQlimit($q['qlimit']);
3960
		} else {
3961
			$this->SetQlimit("");
3962
		}
3963
		if (isset($q['mask']) && $q['mask'] <> "") {
3964
			$masktype = $q['mask'];
3965
		} else {
3966
			$masktype = "";
3967
		}
3968
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3969
			$maskbits = $q['maskbits'];
3970
		} else {
3971
			$maskbits = "";
3972
		}
3973
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3974
			$maskbitsv6 = $q['maskbitsv6'];
3975
		} else {
3976
			$maskbitsv6 = "";
3977
		}
3978
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3979
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3980
			$this->SetBuckets($q['buckets']);
3981
		} else {
3982
			$this->SetBuckets("");
3983
		}
3984
		if (isset($q['plr']) && $q['plr'] <> "") {
3985
			$this->SetPlr($q['plr']);
3986
		} else {
3987
			$this->SetPlr("");
3988
		}
3989
		if (isset($q['delay']) && $q['delay'] <> "") {
3990
			$this->SetDelay($q['delay']);
3991
		} else {
3992
			$this->SetDelay(0);
3993
		}
3994
		if (isset($q['description']) && $q['description'] <> "") {
3995
			$this->SetDescription($q['description']);
3996
		} else {
3997
			$this->SetDescription("");
3998
		}
3999
		$this->SetEnabled($q['enabled']);
4000

    
4001
		/* Limiter patch */
4002
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4003
				$this->SetAQM($q['aqm']);
4004
		} else {
4005
				$this->SetAQM('droptail');
4006
		}
4007
		// parse AQM arguments
4008
		$aqm_map = getAQMs();
4009
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4010
		if (is_array($selectedParameters)) {
4011
			foreach ($selectedParameters as $key => $value) {
4012
				$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4013
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4014
					$this->SetAQMParameter($key, $q[$config_key]);
4015
				} else {
4016
					$this->SetAQMParameter($key, $value["default"]);
4017
				}
4018
			}
4019
		}
4020

    
4021
		if (isset($q['sched']) && $q['sched'] <> "") {
4022
				$this->SetScheduler($q['sched']);
4023
		} else {
4024
				$this->SetScheduler('fifo');
4025
		}
4026
		$scheduler_map = getSchedulers();
4027
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4028
		if (is_array($selectedParameters)) {
4029
			foreach ($selectedParameters as $key => $value) {
4030
				$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4031
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4032
					$this->SetSchedulerParameter($key, $q[$config_key]);
4033
				} else {
4034
					$this->SetSchedulerParameter($key, $value["default"]);
4035
				}
4036
			}
4037
		}
4038

    
4039
		// ecn flag
4040
		$this->SetECN($q['ecn']);
4041
		/* End limiter patch */
4042
	}
4043

    
4044
	function build_tree() {
4045
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($this->GetQname()) ."&amp;queue=".htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4046
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4047
		if (is_array($this->subqueues)) {
4048
			$tree .= "<ul>";
4049
			foreach ($this->subqueues as $q) {
4050
				$tree .= $q->build_tree();
4051
			}
4052
			$tree .= "</ul>";
4053
		}
4054
		$tree .= "</li>";
4055

    
4056
		return $tree;
4057
	}
4058

    
4059
	function build_rules() {
4060
		global $config, $time_based_rules;
4061

    
4062
		if ($this->GetEnabled() == "") {
4063
			return;
4064
		}
4065

    
4066
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
4067
		$found = false;
4068
		$bandwidth = $this->GetBandwidth();
4069
		if (is_array($bandwidth)) {
4070
			foreach ($bandwidth as $bw) {
4071
				if ($bw['bwsched'] != "none") {
4072
					$time_based_rules = true;
4073
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4074
						foreach ($config['schedules']['schedule'] as $schedule) {
4075
							if ($bw['bwsched'] == $schedule['name']) {
4076
								if (filter_get_time_based_rule_status($schedule)) {
4077
									/* pipe throughputs must always be an integer, enforce that restriction again here. */
4078
									$pfq_rule .= " bw ".round(trim($bw['bw']),0).$bw['bwscale'];
4079
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4080
										$pfq_rule .= " burst ".trim($bw['burst']);
4081
									}
4082
									$found = true;
4083
									break;
4084
								}
4085
							}
4086
						}
4087
					} else {
4088
						$pfq_rule .= " bw 0";
4089
						$found = true;
4090
						break;
4091
					}
4092
				} else {
4093
					/* pipe throughputs must always be an integer, enforce that restriction again here. */
4094
					$pfq_rule .= " bw ".round(trim($bw['bw']), 0).$bw['bwscale'];
4095
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4096
						$pfq_rule .= " burst ".trim($bw['burst']);
4097
					}
4098
					$found = true;
4099
					break;
4100
				}
4101
			}
4102
			if ($found == false) {
4103
				$pfq_rule .= " bw 0";
4104
			}
4105
		} else {
4106
			$pfq_rule .= " bw 0";
4107
		}
4108

    
4109
		if ($this->GetQlimit()) {
4110
			$pfq_rule .= " queue " . $this->GetQlimit();
4111
		}
4112
		if ($this->GetPlr()) {
4113
			$pfq_rule .= " plr " . $this->GetPlr();
4114
		}
4115
		if ($this->GetBuckets()) {
4116
			$pfq_rule .= " buckets " . $this->GetBuckets();
4117
		}
4118
		if ($this->GetDelay()) {
4119
			$pfq_rule .= " delay " . $this->GetDelay();
4120
		}
4121
		$this->build_mask_rules($pfq_rule);
4122

    
4123
		/* Limiter patch */
4124
		$selectedAQM = getAQMs()[$this->getAQM()];
4125
		if ($selectedAQM) {
4126
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4127
			if ($selectedAQM["ecn"]) {
4128
				if ($this->getECN() == 'on') {
4129
					$pfq_rule .= ' ecn';
4130
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4131
					$pfq_rule .= ' noecn';
4132
				}
4133
			}
4134
		}
4135
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4136
		if ($selectedScheduler) {
4137
			$pfq_rule .= "\nsched ". $this->GetNumber() . " config ";
4138
			$pfq_rule .= "pipe ". $this->GetNumber();
4139
			$this->build_mask_rules($pfq_rule);
4140
			$pfq_rule .= " " . FormatParameters($selectedScheduler["parameter_format"], $this->GetSchedulerParameters());			
4141
			if ($selectedScheduler["ecn"]) {
4142
				if ($this->getECN() == 'on') {
4143
					$pfq_rule .= ' ecn';
4144
				} else {
4145
					$pfq_rule .= ' noecn';
4146
				}
4147
			}
4148
		}
4149
		$pfq_rule .= "\n";
4150
		/* End patch */
4151

    
4152
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
4153
			foreach ($this->subqueues as $q) {
4154
				$pfq_rule .= $q->build_rules();
4155
			}
4156
		}
4157

    
4158
		$pfq_rule .= " \n";
4159

    
4160
		return $pfq_rule;
4161
	}
4162

    
4163
	function update_dn_data(&$data) {
4164
		$this->ReadConfig($data);
4165
	}
4166

    
4167
	function build_javascript() {
4168
		global $g, $config;
4169

    
4170
		$javasr = parent::build_javascript();
4171

    
4172
		//build list of schedules
4173
		$schedules = "<option value='none'>none</option>";
4174
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4175
			foreach ($config['schedules']['schedule'] as $schedule) {
4176
				if ($schedule['name'] <> "") {
4177
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
4178
				}
4179
			}
4180
		}
4181
		$bwopt = "";
4182
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
4183
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
4184
		}
4185

    
4186
		$javasr .= <<<EOD
4187
<script type='text/javascript'>
4188
//<![CDATA[
4189
var addBwRowTo = (function() {
4190

    
4191
	return (function (tableId) {
4192

    
4193
	var table = document.getElementById(tableId);
4194
	var totalrows = table.rows.length -1;
4195

    
4196
	var row = table.insertRow(totalrows + 1);
4197
	var cell1 = row.insertCell(0);
4198
	var cell2 = row.insertCell(1);
4199
	var cell3 = row.insertCell(2);
4200
	var cell4 = row.insertCell(3);
4201

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

    
4207
	});
4208
})();
4209

    
4210
function removeBwRow(el) {
4211
	var d = el.parentNode.parentNode.rowIndex;
4212
	document.getElementById('maintable').deleteRow(d);
4213
}
4214

    
4215
function ceil_func(el){
4216
	el.value = Math.ceil(el.value);
4217

    
4218
}
4219
//]]>
4220
</script>
4221

    
4222
EOD;
4223

    
4224
		return $javasr;
4225
	}
4226

    
4227
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
4228
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
4229
	// with the javascript in this class
4230
	function build_bwtable() {
4231
		global $config;
4232

    
4233
		$bandwidth = $this->GetBandwidth();
4234
				//build list of schedules
4235
		$schedules = array();
4236
		$schedules[] = "none";//leave none to leave rule enabled all the time
4237
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4238
			foreach ($config['schedules']['schedule'] as $schedule) {
4239
				if ($schedule['name'] != "") {
4240
					$schedules[] = $schedule['name'];
4241
				}
4242
			}
4243
		}
4244

    
4245
		$form = '<div class="table-responsive">';
4246
		$form .= '<table id="maintable" class="table table-hover table-striped">';
4247
		$form .= "<thead><tr>";
4248
		$form .= "<th>Bandwidth</th>";
4249
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
4250
		$form .= "<th>Bw type</th>";
4251
		$form .= "<th>Schedule</th>";
4252
		$form .= "<th></th>";
4253
		$form .= "</tr></thead>";
4254
		$form .= "<tbody>";
4255

    
4256
		// If there are no bandwidths defined, make a blank one for convenience
4257
		if (empty($bandwidth)) {
4258
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Mb', 'bwsched' => 'none'));
4259
		}
4260

    
4261
		if (is_array($bandwidth)) {
4262
			foreach ($bandwidth as $bwidx => $bw) {
4263
				$form .= '<tr>';
4264
				$form .= '<td class="col-xs-4">';
4265
				$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\"/>";
4266
				//$form .= "</td><td width='20%'>";
4267
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
4268
				$form .= "</td>";
4269
				$form .= '<td class="col-xs-4">';
4270
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
4271

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

    
4275
					if ($bw['bwscale'] == $bwsidx) {
4276
						$form .= " selected";
4277
					}
4278

    
4279
					$form .= ">{$bwscale}</option>";
4280
				}
4281

    
4282
				$form .= "</select>";
4283
				$form .= "</td>";
4284
				$form .= '<td class="col-xs-4">';
4285
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
4286

    
4287
				foreach ($schedules as $schd) {
4288
					$selected = "";
4289
					if ($bw['bwsched'] == $schd) {
4290
						$selected = "selected";
4291
					}
4292

    
4293
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
4294
				}
4295

    
4296
				$form .= "</select>";
4297
				$form .= "</td>";
4298
				$form .= '<td>';
4299
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
4300
				$form .= "</td></tr>";
4301
			}
4302
		}
4303
		$form .= "</tbody></table></div><br />";
4304

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

    
4309
		return($form);
4310
	}
4311

    
4312
	function build_form() {
4313
		global $g, $config, $pipe, $action, $qname;
4314

    
4315
		//build list of schedules
4316
		$schedules = array();
4317
		$schedules[] = "none";//leave none to leave rule enabled all the time
4318
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4319
			foreach ($config['schedules']['schedule'] as $schedule) {
4320
				if ($schedule['name'] <> "") {
4321
					$schedules[] = $schedule['name'];
4322
				}
4323
			}
4324
		}
4325

    
4326

    
4327
		$sform = new Form();
4328
		$sform->setAction("firewall_shaper.php");
4329

    
4330
		$section = new Form_Section('Limiters');
4331

    
4332
		$section->addInput(new Form_Checkbox(
4333
			'enabled',
4334
			'Enable',
4335
			'Enable limiter and its children',
4336
			($this->GetEnabled() == "on"),
4337
			'on'
4338
		));
4339

    
4340
		$section->addInput(new Form_Input(
4341
			'newname',
4342
			'*Name',
4343
			'text',
4344
			$this->GetQname()
4345
		));
4346

    
4347
		$section->addInput(new Form_Input(
4348
			'name',
4349
			null,
4350
			'hidden',
4351
			$this->GetQname()
4352
		));
4353

    
4354
		if ($this->GetNumber() > 0) {
4355
			$section->addInput(new Form_Input(
4356
				'number',
4357
				null,
4358
				'hidden',
4359
				$this->GetNumber()
4360
			));
4361
		}
4362

    
4363
		$bandwidth = $this->GetBandwidth();
4364

    
4365
		if (is_array($bandwidth)) {
4366
				$section->addInput(new Form_StaticText(
4367
				'Bandwidth',
4368
				$this->build_bwtable()
4369
			));
4370
		}
4371

    
4372
		$mask = $this->GetMask();
4373

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

    
4383
		$group = new Form_Group(null);
4384

    
4385
		$group->add(new Form_Select(
4386
			'maskbits',
4387
			null,
4388
			$mask['bits'],
4389
			array_combine(range(32, 1, -1), range(32, 1, -1))
4390
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4391

    
4392
		$group->add(new Form_Select(
4393
			'maskbitsv6',
4394
			null,
4395
			$mask['bitsv6'],
4396
			array_combine(range(128, 1, -1), range(128, 1, -1))
4397
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4398

    
4399
		$section->add($group);
4400

    
4401
		$section->addInput(new Form_Input(
4402
			'description',
4403
			'Description',
4404
			'text',
4405
			$this->GetDescription()
4406
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4407

    
4408
		$sform->add($section);
4409

    
4410
		/* Begin limiter patch */
4411
		$aqm_map = getAQMs();
4412
		$scheduler_map = getSchedulers();
4413

    
4414
		$section = new Form_Section('Queue');
4415
		$section->addInput(new Form_Select(
4416
				'aqm',
4417
				'Queue Management Algorithm',
4418
				$this->GetAQM(),
4419
				array_map_assoc(function ($k, $v) {
4420
					return [$k, $v["name"]];
4421
				}, $aqm_map)
4422
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside the limiter, ' .
4423
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4424
						'network congestion.');
4425

    
4426
		$section->addInput(new Form_StaticText(
4427
			'',
4428
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4429
		))->setHelp('Specifies the queue management algorithm parameters.');
4430

    
4431
		$section->addInput(new Form_Select(
4432
				'sched',
4433
				'Scheduler',
4434
				$this->GetScheduler(),
4435
				array_map_assoc(function ($k, $v) {
4436
					return [$k, $v["name"]];
4437
				}, $scheduler_map)
4438
		))->setHelp('The scheduler manages the sequence of network packets in the limiter\'s queue.');
4439

    
4440
		$section->addInput(new Form_StaticText(
4441
			'',
4442
			build_queue_params($scheduler_map, $this->GetScheduler(), $this->GetSchedulerParameters(), "sched")
4443
		))->setHelp('Specifies the scheduler parameters.');
4444

    
4445
		$section->addInput(new Form_Input(
4446
				'qlimit',
4447
				'Queue length',
4448
				'number',
4449
				$this->GetQlimit()
4450
		))->setHelp('Specifies the length of the limiter\'s queue, which the scheduler and AQM are responsible for. ' .
4451
			'This field may be left empty.');
4452

    
4453
		$section->addInput(new Form_Checkbox(
4454
			'ecn',
4455
			'ECN',
4456
			'Enable Explicit Congestion Notification (ECN)',
4457
			($this->GetECN() == "on"),
4458
			'on'
4459
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4460

    
4461
		$sform->add($section);
4462
		/* End limiter patch */
4463

    
4464
		$section = new Form_Section('Advanced Options');
4465

    
4466
		$section->addInput(new Form_Input(
4467
			'delay',
4468
			'Delay (ms)',
4469
			'text',
4470
			$this->GetDelay() > 0 ? $this->GetDelay():null
4471
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4472

    
4473
		$section->addInput(new Form_Input(
4474
			'plr',
4475
			'Packet Loss Rate',
4476
			'number',
4477
			$this->GetPlr(),
4478
			['step' => '0.001', 'min' => '0.000']
4479
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4480
					'A value of 0.001 means one packet in 1000 gets dropped.');
4481

    
4482
		$section->addInput(new Form_Input(
4483
			'buckets',
4484
			'Bucket size (slots)',
4485
			'number',
4486
			$this->GetBuckets()
4487
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4488

    
4489
		$sform->add($section);
4490

    
4491
		return($sform);
4492
		}
4493

    
4494
	function wconfig() {
4495
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4496
		if (!is_array($cflink)) {
4497
			$cflink = array();
4498
		}
4499
		$cflink['name'] = $this->GetQname();
4500
		$cflink['number'] = $this->GetNumber();
4501
		$cflink['qlimit'] = $this->GetQlimit();
4502
		$cflink['plr'] = $this->GetPlr();
4503
		$cflink['description'] = $this->GetDescription();
4504

    
4505
		$bandwidth = $this->GetBandwidth();
4506
		if (is_array($bandwidth)) {
4507
			$cflink['bandwidth'] = array();
4508
			$cflink['bandwidth']['item'] = array();
4509
			foreach ($bandwidth as $bwidx => $bw) {
4510
				$cflink['bandwidth']['item'][] = $bw;
4511
			}
4512
		}
4513

    
4514
		$cflink['enabled'] = $this->GetEnabled();
4515
		$cflink['buckets'] = $this->GetBuckets();
4516
		$mask = $this->GetMask();
4517
		$cflink['mask'] = $mask['type'];
4518
		$cflink['maskbits'] = $mask['bits'];
4519
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4520
		$cflink['delay'] = $this->GetDelay();
4521

    
4522
		/* Limiter queue patch */
4523
		$cflink['sched'] = $this->GetScheduler();
4524
		$scheduler_map = getSchedulers();
4525
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4526
		foreach ($selectedParameters as $key => $value) {
4527
			$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4528
			$cflink[$config_key] = $this->GetSchedulerParameter($key);
4529
		}
4530
		$cflink['aqm'] = $this->GetAQM();
4531
		$aqm_map = GetAQMs();
4532
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4533
		foreach ($selectedParameters as $key => $value) {
4534
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4535
			$cflink[$config_key] = $this->GetAQMParameter($key);
4536
		}
4537
		$cflink['ecn'] = $this->GetECN();
4538
		/* End limiter queue patch */
4539
	}
4540

    
4541
}
4542

    
4543
class dnqueue_class extends dummynet_class {
4544
	var $pipeparent;
4545
	var $weight;
4546
	/* Limiter queue patch */
4547
    var $ecn; // ecn 'on' or 'off'
4548
    var $aqm; // key to aqm_map
4549
    var $aqm_params = array(); // AQM params
4550
	function GetAQM() {
4551
			return $this->aqm;
4552
	}
4553
	function SetAQM($aqm) {
4554
			$this->aqm = $aqm;
4555
	}
4556
	function GetAQMParameters() {
4557
			return $this->aqm_params;
4558
	}
4559
	function GetAQMParameter($parameter) {
4560
			return $this->aqm_params[$parameter];
4561
	}
4562
	function SetAQMParameter($key, $value) {
4563
			return $this->aqm_params[$key] = $value;
4564
	}
4565
	function SetAQMParameters($params) {
4566
			$this->aqm_params = $params;
4567
	}
4568
	function GetECN() {
4569
			return $this->ecn;
4570
	}
4571
	function SetECN($ecn) {
4572
			$this->ecn = $ecn;
4573
	}
4574
	/* End limiter queue patch */
4575

    
4576
	function GetWeight() {
4577
		return $this->weight;
4578
	}
4579
	function SetWeight($weight) {
4580
		$this->weight = $weight;
4581
	}
4582
	function GetPipe() {
4583
		return $this->pipeparent;
4584
	}
4585
	function SetPipe($pipe) {
4586
		$this->pipeparent = $pipe;
4587
	}
4588

    
4589
	/* Just a stub in case we ever try to call this from the frontend. */
4590
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4591
		return;
4592
	}
4593

    
4594
	function delete_queue() {
4595
		cleanup_dnqueue_from_rules($this->GetQname());
4596
		unset_dn_object_by_reference($this->GetLink());
4597
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4598
	}
4599

    
4600
	function validate_input($data, &$input_errors) {
4601
		parent::validate_input($data, $input_errors);
4602

    
4603

    
4604
		/* Limiter patch */
4605
		$selectedAqm = getAQMs()[$data['aqm']];
4606
		if (!empty($data['aqm']) && !$selectedAqm) {
4607
			$input_errors[] = gettext("Selected AQM not recognized.");
4608
		}
4609
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedAqm["ecn"]) {
4610
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but " . $selectedAqm["name"] . " does not support it.");
4611
		}
4612
		/* End limiter patch */
4613

    
4614
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4615
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4616
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4617
		}
4618
	}
4619

    
4620
	/*
4621
	 * Should search even its children
4622
	 */
4623
	function &find_queue($pipe, $qname) {
4624
		if ($qname == $this->GetQname()) {
4625
			return $this;
4626
		} else {
4627
			return NULL;
4628
		}
4629
	}
4630

    
4631
	function &find_parentqueue($pipe, $qname) {
4632
		return $this->qparent;
4633
	}
4634

    
4635
	function &get_queue_list(&$qlist) {
4636
		if ($this->GetEnabled() == "") {
4637
			return;
4638
		}
4639
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4640
	}
4641

    
4642
	function ReadConfig(&$q) {
4643
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4644
			$this->SetQname($q['newname']);
4645
		} else if (!empty($q['newname'])) {
4646
			$this->SetQname($q['newname']);
4647
		} else {
4648
			$this->SetQname($q['name']);
4649
		}
4650
		$this->SetNumber($q['number']);
4651
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4652
			$this->SetQlimit($q['qlimit']);
4653
		} else {
4654
			$this->SetQlimit("");
4655
		}
4656
		if (isset($q['mask']) && $q['mask'] <> "") {
4657
			$masktype = $q['mask'];
4658
		} else {
4659
			$masktype = "";
4660
		}
4661
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4662
			$maskbits = $q['maskbits'];
4663
		} else {
4664
			$maskbits = "";
4665
		}
4666
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4667
			$maskbitsv6 = $q['maskbitsv6'];
4668
		} else {
4669
			$maskbitsv6 = "";
4670
		}
4671
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4672
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4673
			$this->SetBuckets($q['buckets']);
4674
		} else {
4675
			$this->SetBuckets("");
4676
		}
4677
		if (isset($q['plr']) && $q['plr'] <> "") {
4678
			$this->SetPlr($q['plr']);
4679
		} else {
4680
			$this->SetPlr("");
4681
		}
4682
		if (isset($q['weight']) && $q['weight'] <> "") {
4683
			$this->SetWeight($q['weight']);
4684
		} else {
4685
			$this->SetWeight("");
4686
		}
4687
		if (isset($q['description']) && $q['description'] <> "") {
4688
			$this->SetDescription($q['description']);
4689
		} else {
4690
			$this->SetDescription("");
4691
		}
4692
		$this->SetEnabled($q['enabled']);
4693

    
4694
		/* Limiter patch */
4695
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4696
				$this->SetAQM($q['aqm']);
4697
		} else {
4698
				$this->SetAQM('droptail');
4699
		}
4700
		// parse AQM arguments
4701
		$aqm_map = getAQMs();
4702
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4703
		foreach ($selectedParameters as $key => $value) {
4704
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4705
			if (isset($q[$config_key]) && $q[$config_key] <> "") {
4706
				$this->SetAQMParameter($key, $q[$config_key]);
4707
			} else {
4708
				$this->SetAQMParameter($key, $value["default"]);
4709
			}
4710
		}
4711

    
4712
		// ecn flag
4713
		$this->SetECN($q['ecn']);
4714
		/* End limiter patch */
4715
	}
4716

    
4717
	function build_tree() {
4718
		$parent =& $this->GetParent();
4719
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($parent->GetQname()) ."&amp;queue=" . htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4720
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4721
		$tree .= "</li>";
4722

    
4723
		return $tree;
4724
	}
4725

    
4726
	function build_rules() {
4727
		if ($this->GetEnabled() == "") {
4728
			return;
4729
		}
4730

    
4731
		$parent =& $this->GetParent();
4732
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4733
		if ($this->GetQlimit()) {
4734
			$pfq_rule .= " queue " . $this->GetQlimit();
4735
		}
4736
		if ($this->GetWeight()) {
4737
			$pfq_rule .= " weight " . $this->GetWeight();
4738
		}
4739
		if ($this->GetBuckets()) {
4740
			$pfq_rule .= " buckets " . $this->GetBuckets();
4741
		}
4742
		$this->build_mask_rules($pfq_rule);
4743

    
4744
		/* Limiter patch */
4745
		$selectedAQM = getAQMs()[$this->getAQM()];
4746
		if ($selectedAQM) {
4747
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4748
			if ($selectedAQM["ecn"]) {
4749
				if ($this->getECN() == 'on') {
4750
					$pfq_rule .= ' ecn';
4751
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4752
					$pfq_rule .= ' noecn';
4753
				}
4754
			}
4755
		}
4756
		/* End patch */
4757

    
4758
		$pfq_rule .= "\n";
4759

    
4760
		return $pfq_rule;
4761
	}
4762

    
4763
	function build_javascript() {
4764
		return parent::build_javascript();
4765
	}
4766

    
4767
	function build_form() {
4768
		global $g, $config, $pipe, $action, $qname;
4769

    
4770
		//build list of schedules
4771
		$schedules = array();
4772
		$schedules[] = "none";//leave none to leave rule enabled all the time
4773
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4774
			foreach ($config['schedules']['schedule'] as $schedule) {
4775
				if ($schedule['name'] <> "") {
4776
					$schedules[] = $schedule['name'];
4777
				}
4778
			}
4779
		}
4780

    
4781

    
4782
		$sform = new Form();
4783
		$sform->setAction("firewall_shaper.php");
4784
		$section = new Form_Section('Limiters');
4785

    
4786
		$section->addInput(new Form_Checkbox(
4787
			'enabled',
4788
			'Enable',
4789
			'Enable this queue',
4790
			($this->GetEnabled() == "on"),
4791
			'on'
4792
		));
4793

    
4794
		$section->addInput(new Form_Input(
4795
			'newname',
4796
			'*Name',
4797
			'text',
4798
			$this->GetQname()
4799
		));
4800

    
4801
		$section->addInput(new Form_Input(
4802
			'name',
4803
			null,
4804
			'hidden',
4805
			$this->GetQname()
4806
		));
4807

    
4808
		if ($this->GetNumber() > 0) {
4809
			$section->addInput(new Form_Input(
4810
				'number',
4811
				null,
4812
				'hidden',
4813
				$this->GetNumber()
4814
			));
4815
		}
4816

    
4817
		$mask = $this->GetMask();
4818

    
4819
		$section->addInput(new Form_Select(
4820
			'mask',
4821
			'Mask',
4822
			$mask['type'],
4823
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4824
		))->setHelp('If "source" or "destination" slots is chosen a dynamic queue with the bandwidth, delay, packet loss ' .
4825
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4826
 					'This makes it possible to easily specify bandwidth limits per ' .
4827
 					'host or subnet, usually capped by the bandwidth of the parent ' .
4828
 					'limiter.');
4829

    
4830
		$group = new Form_Group(null);
4831

    
4832
		$group->add(new Form_Select(
4833
			'maskbits',
4834
			null,
4835
			$mask['bits'],
4836
			array_combine(range(32, 1, -1), range(32, 1, -1))
4837
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4838

    
4839
		$group->add(new Form_Select(
4840
			'maskbitsv6',
4841
			null,
4842
			$mask['bitsv6'],
4843
			array_combine(range(128, 1, -1), range(128, 1, -1))
4844
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4845

    
4846
		$section->add($group);
4847

    
4848
		$section->addInput(new Form_Input(
4849
			'description',
4850
			'Description',
4851
			'text',
4852
			$this->GetDescription()
4853
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4854

    
4855
		$sform->add($section);
4856

    
4857
		/* Begin limiter patch */
4858
		$aqm_map = getAQMs();
4859

    
4860
		$section = new Form_Section('Queue');
4861
		$section->addInput(new Form_Select(
4862
				'aqm',
4863
				'Queue Management Algorithm',
4864
				$this->GetAQM(),
4865
				array_map_assoc(function ($k, $v) {
4866
					return [$k, $v["name"]];
4867
				}, $aqm_map)
4868
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside this limiter\'s queue, ' .
4869
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4870
						'network congestion.');
4871

    
4872
		$section->addInput(new Form_StaticText(
4873
			'',
4874
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4875
		))->setHelp('Specifies the queue management algorithm parameters.');
4876

    
4877
		$section->addInput(new Form_Input(
4878
				'qlimit',
4879
				'Queue length',
4880
				'number',
4881
				$this->GetQlimit()
4882
		))->setHelp('Specifies the length of this queue, which the AQM is responsible for.  This field may be left empty.');
4883

    
4884
		$section->addInput(new Form_Checkbox(
4885
			'ecn',
4886
			'ECN',
4887
			'Enable Explicit Congestion Notification (ECN)',
4888
			($this->GetECN() == "on"),
4889
			'on'
4890
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4891

    
4892
		$sform->add($section);
4893
		/* End limiter patch */
4894

    
4895
		$section = new Form_Section('Advanced Options');
4896

    
4897
		$section->addInput(new Form_Input(
4898
			'weight',
4899
			'Weight',
4900
			'number',
4901
			$this->GetWeight(),
4902
			['min' => '1', 'max' => '100']
4903
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4904
					' it can be left blank otherwise.');
4905

    
4906
		$section->addInput(new Form_Input(
4907
			'plr',
4908
			'Packet Loss Rate',
4909
			'number',
4910
			$this->GetPlr(),
4911
			['step' => '0.001', 'min' => '0.000']
4912
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4913
					'A value of 0.001 means one packet in 1000 gets dropped');
4914

    
4915
		$section->addInput(new Form_Input(
4916
			'buckets',
4917
			'Bucket size (slots)',
4918
			'number',
4919
			$this->GetBuckets()
4920
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4921

    
4922
		$section->addInput(new Form_Input(
4923
			'pipe',
4924
			null,
4925
			'hidden',
4926
			$this->GetPipe()
4927
		));
4928

    
4929
		$sform->add($section);
4930

    
4931
		return($sform);
4932
	}
4933

    
4934
	function update_dn_data(&$data) {
4935
		$this->ReadConfig($data);
4936
	}
4937

    
4938
	function wconfig() {
4939
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4940
		if (!is_array($cflink)) {
4941
			$cflink = array();
4942
		}
4943
		$cflink['name'] = $this->GetQname();
4944
		$cflink['number'] = $this->GetNumber();
4945
		$cflink['qlimit'] = $this->GetQlimit();
4946
		$cflink['description'] = $this->GetDescription();
4947
		$cflink['weight'] = $this->GetWeight();
4948
		$cflink['enabled'] = $this->GetEnabled();
4949
		$cflink['buckets'] = $this->GetBuckets();
4950
		$mask = $this->GetMask();
4951
		$cflink['mask'] = $mask['type'];
4952
		$cflink['maskbits'] = $mask['bits'];
4953
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4954

    
4955
		/* Limiter queue patch */
4956
		$cflink['aqm'] = $this->GetAQM();
4957
		$aqm_map = GetAQMs();
4958
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4959
		foreach ($selectedParameters as $key => $value) {
4960
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4961
			$cflink[$config_key] = $this->GetAQMParameter($key);
4962
		}
4963
		$cflink['ecn'] = $this->GetECN();
4964
		/* End limiter queue patch */
4965
	}
4966
}
4967

    
4968
function get_dummynet_name_list() {
4969

    
4970
	$dn_name_list =& get_unique_dnqueue_list();
4971
	$dn_name = array();
4972
	if (is_array($dn_name_list)) {
4973
		foreach ($dn_name_list as $key => $value) {
4974
			$dn_name[] = $key;
4975
		}
4976
	}
4977

    
4978
	return $dn_name;
4979

    
4980
}
4981

    
4982
function get_altq_name_list() {
4983
	$altq_name_list =& get_unique_queue_list();
4984
	$altq_name = array();
4985
	if (is_array($altq_name_list)) {
4986
		foreach ($altq_name_list as $key => $aqobj) {
4987
			$altq_name[] = $key;
4988
		}
4989
	}
4990

    
4991
	return $altq_name;
4992
}
4993

    
4994
/*
4995
 * XXX: TODO Make a class shaper to hide all these functions
4996
 * from the global namespace.
4997
 */
4998

    
4999
/*
5000
 * This is a layer violation but for now there is no way
5001
 * I can find to properly do this with PHP.
5002
 */
5003
function altq_get_default_queue($interface) {
5004
	global $altq_list_queues;
5005

    
5006
	$altq_tmp = $altq_list_queues[$interface];
5007
	if ($altq_tmp) {
5008
		return $altq_tmp->GetDefaultQueuePresent();
5009
	} else {
5010
		return false;
5011
	}
5012
}
5013

    
5014
function altq_check_default_queues() {
5015
	global $altq_list_queues;
5016

    
5017
	$count = 0;
5018
	if (is_array($altq_list_queues)) {
5019
		foreach ($altq_list_queues as $altq) {
5020
			if ($altq->GetDefaultQueuePresent()) {
5021
				$count++;
5022
			}
5023
		}
5024
	}
5025
	else {
5026
		$count++;
5027
	}
5028

    
5029
	return 0;
5030
}
5031

    
5032
function &get_unique_queue_list() {
5033
	global $altq_list_queues;
5034

    
5035
	$qlist = array();
5036
	if (is_array($altq_list_queues)) {
5037
		foreach ($altq_list_queues as $altq) {
5038
			if ($altq->GetEnabled() == "") {
5039
				continue;
5040
			}
5041
			$tmplist =& $altq->get_queue_list();
5042
			foreach ($tmplist as $qname => $link) {
5043
				if ($link->GetEnabled() <> "") {
5044
					$qlist[$qname] = $link;
5045
				}
5046
			}
5047
		}
5048
	}
5049
	return $qlist;
5050
}
5051

    
5052
function &get_unique_dnqueue_list() {
5053
	global $dummynet_pipe_list;
5054

    
5055
	$qlist = array();
5056
	if (is_array($dummynet_pipe_list)) {
5057
		foreach ($dummynet_pipe_list as $dn) {
5058
			if ($dn->GetEnabled() == "") {
5059
				continue;
5060
			}
5061
			$tmplist =& $dn->get_queue_list();
5062
			foreach ($tmplist as $qname => $link) {
5063
				$qlist[$qname] = $link;
5064
			}
5065
		}
5066
	}
5067
	return $qlist;
5068
}
5069

    
5070
function ref_on_altq_queue_list($parent, $qname) {
5071
	if (isset($GLOBALS['queue_list'][$qname])) {
5072
		$GLOBALS['queue_list'][$qname]++;
5073
	} else {
5074
		$GLOBALS['queue_list'][$qname] = 1;
5075
	}
5076

    
5077
	unref_on_altq_queue_list($parent);
5078
}
5079

    
5080
function unref_on_altq_queue_list($qname) {
5081
	$GLOBALS['queue_list'][$qname]--;
5082
	if ($GLOBALS['queue_list'][$qname] <= 1) {
5083
		unset($GLOBALS['queue_list'][$qname]);
5084
	}
5085
}
5086

    
5087
function read_altq_config() {
5088
	global $altq_list_queues, $config;
5089
	$path = array();
5090

    
5091
	init_config_arr(array('shaper', 'queue'));
5092
	$a_int = &$config['shaper']['queue'];
5093

    
5094
	$altq_list_queues = array();
5095

    
5096
	if (!is_array($config['shaper']['queue'])) {
5097
		return;
5098
	}
5099

    
5100
	foreach ($a_int as $key => $conf) {
5101
		$int = $conf['interface'];
5102
		$root = new altq_root_queue();
5103
		$root->SetInterface($int);
5104
		$altq_list_queues[$root->GetInterface()] = $root;
5105
		$root->ReadConfig($conf);
5106
		array_push($path, $key);
5107
		$root->SetLink($path);
5108
		if (is_array($conf['queue'])) {
5109
			foreach ($conf['queue'] as $key1 => $q) {
5110
				array_push($path, $key1);
5111
				/*
5112
				 * XXX: we completely ignore errors here but anyway we must have
5113
				 *	checked them before so no harm should be come from this.
5114
				 */
5115
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
5116
				array_pop($path);
5117
			}
5118
		}
5119
		array_pop($path);
5120
	}
5121
}
5122

    
5123
function read_dummynet_config() {
5124
	global $dummynet_pipe_list, $config;
5125
	$path = array();
5126

    
5127
	init_config_arr(array('dnshaper', 'queue'));
5128
	$a_int = &$config['dnshaper']['queue'];
5129

    
5130
	$dummynet_pipe_list = array();
5131

    
5132
	if (!count($config['dnshaper']['queue'])) {
5133
		return;
5134
	}
5135

    
5136
	foreach ($a_int as $key => $conf) {
5137
		if (empty($conf['name'])) {
5138
			continue; /* XXX: grrrrrr at php */
5139
		}
5140
		$root = new dnpipe_class();
5141
		$root->ReadConfig($conf);
5142
		$dummynet_pipe_list[$root->GetQname()] = $root;
5143
		array_push($path, $key);
5144
		$root->SetLink($path);
5145
		if (is_array($conf['queue'])) {
5146
			foreach ($conf['queue'] as $key1 => $q) {
5147
				array_push($path, $key1);
5148
				/*
5149
				 * XXX: we completely ignore errors here but anyway we must have
5150
				 *	checked them before so no harm should be come from this.
5151
				 */
5152
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
5153
				array_pop($path);
5154
			}
5155
		}
5156
		array_pop($path);
5157
	}
5158
}
5159

    
5160
function get_interface_list_to_show() {
5161
	global $altq_list_queues, $config;
5162
	global $shaperIFlist;
5163

    
5164
	$tree = "";
5165
	foreach ($shaperIFlist as $shif => $shDescr) {
5166
		if ($altq_list_queues[$shif]) {
5167
			continue;
5168
		} else {
5169
			if (!is_altq_capable(get_real_interface($shif))) {
5170
				continue;
5171
			}
5172
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
5173
		}
5174
	}
5175

    
5176
	return $tree;
5177
}
5178

    
5179
function filter_generate_altq_queues() {
5180
	global $altq_list_queues;
5181

    
5182
	read_altq_config();
5183

    
5184
	$altq_rules = "";
5185
	foreach ($altq_list_queues as $altq) {
5186
		$altq_rules .= $altq->build_rules();
5187
	}
5188

    
5189
	return $altq_rules;
5190
}
5191

    
5192
function dnqueue_find_nextnumber() {
5193
	global $dummynet_pipe_list;
5194

    
5195
	$dnused = array();
5196
	if (is_array($dummynet_pipe_list)) {
5197
		foreach ($dummynet_pipe_list as $dn) {
5198
			$tmplist =& $dn->get_queue_list();
5199
			foreach ($tmplist as $qname => $link) {
5200
				if ($link[0] == "?") {
5201
					$dnused[$qname] = substr($link, 1);
5202
				}
5203
			}
5204
		}
5205
	}
5206

    
5207
	sort($dnused, SORT_NUMERIC);
5208
	$dnnumber = 0;
5209
	$found = false;
5210
	foreach ($dnused as $dnnum) {
5211
		if (($dnnum - $dnnumber) > 1) {
5212
			$dnnumber = $dnnum - 1;
5213
			$found = true;
5214
			break;
5215
		} else {
5216
			$dnnumber = $dnnum;
5217
		}
5218
	}
5219

    
5220
	if ($found == false) {
5221
		$dnnumber++;
5222
	}
5223

    
5224
	unset($dnused, $dnnum, $found);
5225
	return $dnnumber;
5226
}
5227

    
5228
function dnpipe_find_nextnumber() {
5229
	global $dummynet_pipe_list;
5230

    
5231
	$dnused = array();
5232
	foreach ($dummynet_pipe_list as $dn) {
5233
		$dnused[] = $dn->GetNumber();
5234
	}
5235

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

    
5249
	if ($found == false) {
5250
		$dnnumber++;
5251
	}
5252

    
5253
	unset($dnused, $dnnum, $found);
5254
	return $dnnumber;
5255
}
5256

    
5257
function filter_generate_dummynet_rules() {
5258
	global $config, $g, $dummynet_pipe_list;
5259

    
5260
	read_dummynet_config();
5261

    
5262
	$dn_rules = "";
5263
	$max_qlimit = "100"; // OS default
5264
	foreach ($dummynet_pipe_list as $dn) {
5265
		$dn_rules .= $dn->build_rules();
5266
		$this_qlimit = $dn->GetQlimit();
5267
		if ($this_qlimit > $max_qlimit) {
5268
			$max_qlimit = $this_qlimit;
5269
		}
5270
	}
5271
	if (!is_numericint($max_qlimit)) {
5272
		$max_qlimit = "100";
5273
	}
5274
	if (!empty($dn_rules)) {
5275
		if (!is_module_loaded("dummynet.ko")) {
5276
			mwexec("/sbin/kldload dummynet");
5277
		}
5278
		$sysctls = (array(
5279
				"net.inet.ip.dummynet.io_fast" => "1",
5280
				"net.inet.ip.dummynet.hash_size" => "256",
5281
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
5282
		));
5283
		init_config_arr(array('sysctl', 'item'));
5284
		if (!empty($config['sysctl']['item'])) {
5285
			foreach ($config['sysctl']['item'] as $item) {
5286
				if (preg_match('/net\.inet\.ip\.dummynet\./', $item['tunable'])) {
5287
					$sysctls[$item['tunable']] = $item['value'];
5288
				}
5289
			}
5290
		}
5291
		set_sysctl($sysctls);
5292
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
5293
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
5294
	}
5295
}
5296

    
5297
function build_iface_without_this_queue($iface, $qname) {
5298
	global $g, $altq_list_queues;
5299
	global $shaperIFlist;
5300

    
5301
	$altq =& $altq_list_queues[$iface];
5302

    
5303
	if ($altq) {
5304
		$scheduler = $altq->GetScheduler();
5305
	}
5306

    
5307
	$form = '<dl class="dl-horizontal">';
5308

    
5309
	$form .= '	<dt>';
5310
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5311
	$form .= '	</dt>';
5312
	$form .= '	<dd>';
5313
	$form .=		$scheduler;
5314
	$form .= '	</dd>';
5315

    
5316
	$form .= '	<dt>';
5317
	$form .= 'Clone';
5318
	$form .= '	</dt>';
5319
	$form .= '	<dd>';
5320
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
5321
	$form .= $iface . '&amp;queue=';
5322
	$form .= $qname . '&amp;action=add">';
5323
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
5324
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
5325
	$form .= '	</dd>';
5326

    
5327
	$form .= '</dl>';
5328

    
5329
	return $form;
5330

    
5331
}
5332

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

    
5336
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5337
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5338
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5339

    
5340
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5341
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5342
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5343

    
5344
// Check to see if the specified interface has a queue configured
5345
function interface_has_queue($if) {
5346
	global $config;
5347

    
5348
	if ($config['shaper']) {
5349
		foreach ($config['shaper']['queue'] as $queue) {
5350
			if ($queue['interface'] === $if) {
5351
				return true;
5352
			}
5353
		}
5354
	}
5355

    
5356
	return false;
5357
}
5358
?>
(47-47/61)