Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
	return $ptr;
332
}
333

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

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

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

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

    
354
	return $ptr;
355
}
356

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

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

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

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

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

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

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

    
480
	return floatval($objbw);
481
}
482

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

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

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

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

    
532
	return floatval($objbw);
533
}
534

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
753
		return 0;
754
	}
755

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

    
765
		return $sum;
766
	}
767

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

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

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

    
777
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
778
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
779
		}
780
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
781
			$input_errors[] = gettext("Bandwidth must be an integer.");
782
		}
783
		if ($data['bandwidth'] < 0) {
784
			$input_errors[] = gettext("Bandwidth cannot be negative.");
785
		}
786
		if ($data['bandwidthtype'] == "%") {
787
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
788
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
789
			}
790
		}
791
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype'])) {
792
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
793
		}
794
		if ($data['qlimit'] && ($data['scheduler'] == 'CODELQ')) {
795
			$input_errors[] = gettext("CODELQ scheduler doesn't support Qlimit parameter.");
796
		}
797
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
798
			$input_errors[] = gettext("Qlimit must be an integer.");
799
		}
800
		if ($data['qlimit'] < 0) {
801
			$input_errors[] = gettext("Qlimit must be positive.");
802
		}
803
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
804
			$input_errors[] = gettext("Tbrsize must be an integer.");
805
		}
806
		if ($data['tbrconfig'] < 0) {
807
			$input_errors[] = gettext("Tbrsize must be positive.");
808
		}
809
	}
810

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

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

    
864
	function &get_queue_list(&$q = null) {
865
		$qlist = array();
866

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

    
876
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
877

    
878
		if (!is_array($this->queues)) {
879
			$this->queues = array();
880
		}
881

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

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

    
917
		return $q;
918
	}
919

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

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

    
948
	function build_tree() {
949
		global $shaperIFlist;
950

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

    
964
	function delete_queue() {
965
		foreach ($this->queues as $q) {
966
			$q->delete_queue();
967
		}
968
		unset_object_by_reference($this->GetLink());
969
	}
970

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

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

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

    
1051
			$rules .= " queue";
1052
		}
1053

    
1054
		$rules .= " \n";
1055
		return $rules;
1056
	}
1057

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

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

    
1077
		return $javascript;
1078
	}
1079

    
1080
	function build_shortform() {
1081
		global $g;
1082

    
1083
		$altq =& $this;
1084

    
1085
		if ($altq) {
1086
			$scheduler = ": " . $altq->GetScheduler();
1087
		}
1088

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

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

    
1104
		$form .= '	<dt>';
1105
		$form .= 'Disable';
1106
		$form .= '	<dt>';
1107
		$form .= '	<dd>';
1108

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

    
1115
		$form .= '	</dd>';
1116

    
1117
		$form .= '</dl>';
1118

    
1119
		return $form;
1120

    
1121
	}
1122

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

    
1129
		$sform = new Form();
1130

    
1131
		$sform->setAction("firewall_shaper.php");
1132

    
1133
		$section = new Form_Section(null);
1134

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

    
1143
		$section->addInput(new Form_StaticText(
1144
			'*Name',
1145
			$this->GetQname()
1146
		));
1147

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

    
1159
		$group = new Form_group('Bandwidth');
1160

    
1161
		$group->add(new Form_Input(
1162
			'bandwidth',
1163
			null,
1164
			'number',
1165
			$this->GetBandwidth()
1166
		));
1167

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

    
1179
		$section->add($group);
1180

    
1181
		$section->addInput(new Form_Input(
1182
			'qlimit',
1183
			'Queue Limit',
1184
			'number',
1185
			$this->GetQlimit()
1186
		));
1187

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

    
1196
		$section->addInput(new Form_Input(
1197
			'interface',
1198
			null,
1199
			'hidden',
1200
			$this->GetInterface()
1201
		));
1202

    
1203
		$section->addInput(new Form_Input(
1204
			'name',
1205
			null,
1206
			'hidden',
1207
			$this->GetQname()
1208
		));
1209

    
1210
		$sform->add($section);
1211

    
1212
		return($sform);
1213
	}
1214

    
1215
	function update_altq_queue_data(&$data) {
1216
		$this->ReadConfig($data);
1217
	}
1218

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

    
1247
}
1248

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

    
1268
	/* This is here to help with form building and building rules/lists */
1269
	var $subqueues = array();
1270

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

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

    
1350
		if ($bwtype != "%") {
1351
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1352

    
1353
			if ($bw > 0) {
1354
				$sum += get_bandwidth($bw, $bwtype, $parent);
1355
			}
1356

    
1357
			if ($sum > get_queue_bandwidth($parent)) {
1358
				return 1;
1359
			}
1360
		}
1361

    
1362
		foreach ($this->subqueues as $q) {
1363
			if ($q->CheckBandwidth(0, '')) {
1364
				return 1;
1365
			}
1366
		}
1367

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

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

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

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

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

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

    
1486
		return $javascript;
1487
	}
1488

    
1489
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1490

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

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

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

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

    
1527
	function &get_queue_list(&$qlist) {
1528

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

    
1537
	function delete_queue() {
1538
		unref_on_altq_queue_list($this->GetQname());
1539
		cleanup_queue_from_rules($this->GetQname());
1540
		unset_object_by_reference($this->GetLink());
1541
	}
1542

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

    
1554
	function &find_queue($interface, $qname) {
1555
		if ($qname == $this->GetQname()) {
1556
			return $this;
1557
		}
1558
	}
1559

    
1560
	function find_parentqueue($interface, $qname) { return; }
1561

    
1562
	function validate_input($data, &$input_errors) {
1563
		global $altq_list_queues;
1564

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

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

    
1584
		if ($parent) {
1585
			if (($parent->GetScheduler() == 'PRIQ') && (!is_numeric($data['priority']) ||
1586
			    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1587
				$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1588
			} elseif (in_array($parent->GetScheduler(), array('FAIRQ','CBQ')) && (!is_numeric($data['priority']) ||
1589
			    ($data['priority'] < 0) || ($data['priority'] > 7))) {
1590
				$input_errors[] = gettext("The priority must be an integer between 0 and 7.");
1591
			}
1592
			if (is_array($parent->queues)) {
1593
				foreach ($parent->queues as $q) {
1594
					if (($data['newname'] == $q->GetQname()) && ($data['name'] != $data['newname'])) { 
1595
						$input_errors[] = gettext("A queue with the same name already exists.");
1596
					}
1597
					if (in_array($parent->GetScheduler(), array('FAIRQ','PRIQ')) &&
1598
					    ($data['priority'] == $q->GetQpriority()) && ($data['name'] != $q->GetQname())) { 
1599
						$input_errors[] = sprintf(gettext("The priority %d is already used by queue %s."),
1600
								$data['priority'], $q->GetQname());
1601
					}
1602
				}
1603
			}
1604
		}
1605

    
1606
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1607
				$input_errors[] = gettext("Queue limit must be an integer");
1608
		}
1609
		if ($data['qlimit'] < 0) {
1610
				$input_errors[] = gettext("Queue limit must be positive");
1611
		}
1612
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1613
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1614
		}
1615
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1616
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1617
		}
1618
		$default = $this->GetDefault();
1619
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1620
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1621
		}
1622
	}
1623

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

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

    
1706
		$tree .= "</li>";
1707

    
1708
		return $tree;
1709
	}
1710

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

    
1769
		$pfq_rule .= " \n";
1770

    
1771
		return $pfq_rule;
1772
	}
1773

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

    
1782
	function build_form() {
1783

    
1784
		$sform = new Form();
1785

    
1786
		$sform->setAction("firewall_shaper.php");
1787

    
1788
		$section = new Form_Section("");
1789

    
1790
		$section->addInput(new Form_Checkbox(
1791
			'enabled',
1792
			'Enable/Disable',
1793
			'Enable/disable discipline and its children',
1794
			($this->GetEnabled() == "on"),
1795
			'on'
1796
		));
1797

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

    
1805
		$section->addInput(new Form_Input(
1806
			'name',
1807
			null,
1808
			'hidden',
1809
			$this->GetQname()
1810
		));
1811

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

    
1822
		$section->addInput(new Form_Input(
1823
			'qlimit',
1824
			'Queue Limit',
1825
			'number',
1826
			$this->GetQlimit()
1827
		))->setHelp('Queue limit in packets.');
1828

    
1829
		$group = new Form_Group('Scheduler options');
1830

    
1831
		if (empty($this->subqueues)) {
1832
			$group->add(new Form_Checkbox(
1833
				'default',
1834
				null,
1835
				null,
1836
				$this->GetDefault(),
1837
				'default'
1838
			))->setHelp('Default Queue');
1839
		}
1840

    
1841
		$group->add(new Form_Checkbox(
1842
			'red',
1843
			null,
1844
			null,
1845
			!empty($this->GetRed())
1846
		))->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>');
1847

    
1848
		$group->add(new Form_Checkbox(
1849
			'rio',
1850
			null,
1851
			null,
1852
			!empty($this->GetRio())
1853
		))->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>');
1854

    
1855
		$group->add(new Form_Checkbox(
1856
			'ecn',
1857
			null,
1858
			null,
1859
			!empty($this->GetEcn())
1860
		))->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>');
1861

    
1862
		$group->add(new Form_Checkbox(
1863
			'codel',
1864
			null,
1865
			null,
1866
			!empty($this->GetCodel())
1867
		))->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>');
1868

    
1869
		$group->setHelp('Select options for this queue');
1870

    
1871
		$section->add($group);
1872

    
1873
		$section->addInput(new Form_Input(
1874
			'description',
1875
			'Description',
1876
			'text',
1877
			$this->GetDescription()
1878
		));
1879

    
1880
		$sform->add($section);
1881

    
1882
		$sform->addGlobal(new Form_Input(
1883
			'interface',
1884
			null,
1885
			'hidden',
1886
			$this->GetInterface()
1887
		));
1888

    
1889
		$sform->addGlobal(new Form_Input(
1890
			'name',
1891
			null,
1892
			'hidden',
1893
			$this->GetQname()
1894
		));
1895

    
1896
		return($sform);
1897
	}
1898

    
1899
	function build_shortform() {
1900
		/* XXX: Hacks in sight. Mostly layer violations!  */
1901
		global $g, $altq_list_queues;
1902
		global $shaperIFlist;
1903

    
1904
		$altq =& $altq_list_queues[$this->GetInterface()];
1905

    
1906
		if ($altq) {
1907
			$scheduler = $altq->GetScheduler();
1908
		}
1909

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

    
1918
		$form .= '	<dt>';
1919
		$form .=		'Bandwidth';
1920
		$form .= '	</dt>';
1921
		$form .= '	<dd>';
1922
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1923
		$form .= '	</dd>';
1924

    
1925
		$tmpvalue = $this->GetQpriority();
1926
		if (!empty($tmpvalue)) {
1927
			$form .= '	<dt>';
1928
			$form .=		'Priority';
1929
			$form .= '	<dt>';
1930
			$form .= '	<dd>';
1931
			$form .=		'On';
1932
			$form .= '	</dd>';
1933
		}
1934

    
1935
		$tmpvalue = $this->GetDefault();
1936
		if (!empty($tmpvalue)) {
1937
			$form .= '	<dt>';
1938
			$form .=		'Default';
1939
			$form .= '	<dt>';
1940
			$form .= '	<dd>';
1941
			$form .=		'On';
1942
			$form .= '	</dd>';
1943
		}
1944

    
1945
			$form .= '	<dt>';
1946
			$form .= 'Delete';
1947
			$form .= '	<dt>';
1948
			$form .= '	<dd>';
1949

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

    
1956
			$form .= '	</dd>';
1957

    
1958
			$form .= '</dl>';
1959

    
1960
		return $form;
1961

    
1962
	}
1963

    
1964
	function update_altq_queue_data(&$q) {
1965
		$this->ReadConfig($q);
1966
	}
1967

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

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

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

    
2119
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2120

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

    
2130
		$q->SetEnabled("on");
2131
		$q->SetLink($path);
2132

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

    
2143
		return $q;
2144
	}
2145

    
2146
	function copy_queue($interface, &$cflink) {
2147

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

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

    
2248
	function delete_queue() {
2249
		unref_on_altq_queue_list($this->GetQname());
2250
		cleanup_queue_from_rules($this->GetQname());
2251
		$parent =& $this->GetParent();
2252
		foreach ($this->subqueues as $q) {
2253
			$q->delete_queue();
2254
		}
2255
		unset_object_by_reference($this->GetLink());
2256
	}
2257

    
2258
	/*
2259
	 * Should search even its children
2260
	 */
2261
	function &find_queue($interface, $qname) {
2262
		if ($qname == $this->GetQname()) {
2263
			return $this;
2264
		}
2265

    
2266
		foreach ($this->subqueues as $q) {
2267
			$result =& $q->find_queue("", $qname);
2268
			if ($result) {
2269
				return $result;
2270
			}
2271
		}
2272
	}
2273

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

    
2286
	function validate_input($data, &$input_errors) {
2287
		parent::validate_input($data, $input_errors);
2288

    
2289
		$reqdfields[] = "bandwidth";
2290
		$reqdfieldsn[] = gettext("Bandwidth");
2291
		$reqdfields[] = "bandwidthtype";
2292
		$reqdfieldsn[] = gettext("Bandwidthtype");
2293

    
2294
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2295

    
2296
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2297
			if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2298
				$input_errors[] = gettext("Bandwidth must be an integer.");
2299
			}
2300

    
2301
			if ($data['bandwidth'] < 0) {
2302
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2303
			}
2304

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

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

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

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

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

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

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

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

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

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

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

    
2476
	/* Even this should take children into consideration */
2477
	function build_rules(&$default = false) {
2478

    
2479
		$pfq_rule = " queue ". $this->qname;
2480
		if ($this->GetInterface()) {
2481
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2482
		}
2483
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2484
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2485
		}
2486

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

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

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

    
2584
		$pfq_rule .= " \n";
2585

    
2586
		return $pfq_rule;
2587
	}
2588

    
2589
	function build_javascript() {
2590

    
2591
		$javascript = <<<EOJS
2592
<script type="text/javascript">
2593
//<![CDATA[
2594
	events.push(function(){
2595

    
2596
		// Disables the specified input element
2597
		function disableInput(id, disable) {
2598
			$('#' + id).prop("disabled", disable);
2599
		}
2600

    
2601
		// Upperlimit
2602
		function enable_upperlimit() {
2603
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2604
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2605
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2606
		}
2607

    
2608
		$('#upperlimit').click(function () {
2609
			enable_upperlimit();
2610
		});
2611

    
2612
		enable_upperlimit();
2613

    
2614
		// realtime
2615
		function enable_realtime() {
2616
			disableInput('realtime1', !$('#realtime').prop('checked'));
2617
			disableInput('realtime2', !$('#realtime').prop('checked'));
2618
			disableInput('realtime3', !$('#realtime').prop('checked'));
2619
		}
2620

    
2621
		$('#realtime').click(function () {
2622
			enable_realtime();
2623
		});
2624

    
2625
		enable_realtime();
2626

    
2627
		// linkshare
2628
		function enable_linkshare() {
2629
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2630
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2631
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2632
		}
2633

    
2634
		$('#linkshare').click(function () {
2635
			enable_linkshare();
2636
		});
2637

    
2638
		enable_linkshare();
2639
	});
2640
//]]>
2641
</script>
2642
EOJS;
2643

    
2644
		return $javascript;
2645
	}
2646

    
2647
	function build_form() {
2648

    
2649
		$sform = parent::build_form();
2650

    
2651
		$section = new Form_Section('Service Curve (sc)');
2652

    
2653
		$group = new Form_Group('Bandwidth');
2654

    
2655
		$group->add(new Form_Input(
2656
			'bandwidth',
2657
			null,
2658
			'number',
2659
			$this->GetBandwidth(),
2660
			['step' => 'any', 'min' => '0.000']
2661
		));
2662

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

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

    
2676
		$section->add($group);
2677

    
2678
		$group = new Form_Group('Max bandwidth for queue.');
2679

    
2680
		$group->add(new Form_Checkbox(
2681
			'upperlimit',
2682
			null,
2683
			'Upper Limit',
2684
			($this->GetUpperlimit()<> "")
2685
		));
2686

    
2687
		$group->add(new Form_Input(
2688
			'upperlimit1',
2689
			null,
2690
			'text',
2691
			$this->GetU_m1()
2692
		))->setHelp('m1');
2693

    
2694
		$group->add(new Form_Input(
2695
			'upperlimit2',
2696
			null,
2697
			'text',
2698
			$this->GetU_d()
2699
		))->setHelp('d');
2700

    
2701
		$group->add(new Form_Input(
2702
			'upperlimit3',
2703
			null,
2704
			'text',
2705
			$this->GetU_m2()
2706
		))->setHelp('m2');
2707

    
2708

    
2709
		$section->add($group);
2710

    
2711
		$group = new Form_Group('Min bandwidth for queue.');
2712

    
2713
		$group->add(new Form_Checkbox(
2714
			'realtime',
2715
			null,
2716
			'Real Time',
2717
			($this->GetRealtime()<> "")
2718
		));
2719

    
2720
		$group->add(new Form_Input(
2721
			'realtime1',
2722
			null,
2723
			'text',
2724
			$this->GetR_m1()
2725
		))->setHelp('m1');
2726

    
2727
		$group->add(new Form_Input(
2728
			'realtime2',
2729
			null,
2730
			'text',
2731
			$this->GetR_d()
2732
		))->setHelp('d');
2733

    
2734
		$group->add(new Form_Input(
2735
			'realtime3',
2736
			null,
2737
			'text',
2738
			$this->GetR_m2()
2739
		))->setHelp('m2');
2740

    
2741
		$section->add($group);
2742

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

    
2745
		$group->add(new Form_Checkbox(
2746
			'linkshare',
2747
			null,
2748
			'Link Share',
2749
			($this->GetLinkshare()<> "")
2750
		));
2751

    
2752
		$group->add(new Form_Input(
2753
			'linkshare1',
2754
			null,
2755
			'text',
2756
			$this->GetL_m1()
2757
		))->setHelp('m1');
2758

    
2759
		$group->add(new Form_Input(
2760
			'linkshare2',
2761
			null,
2762
			'text',
2763
			$this->GetL_d()
2764
		))->setHelp('d');
2765

    
2766
		$group->add(new Form_Input(
2767
			'linkshare3',
2768
			null,
2769
			'text',
2770
			$this->GetL_m2()
2771
		))->setHelp('m2');
2772

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

    
2779
		$section->add($group);
2780

    
2781
		$sform->add($section);
2782

    
2783
		return($sform);
2784
	}
2785

    
2786
	function update_altq_queue_data(&$data) {
2787
		$this->ReadConfig($data);
2788
	}
2789

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

    
2907
class cbq_queue extends priq_queue {
2908
	var $qborrow = "";
2909

    
2910
	function GetBorrow() {
2911
		return $this->qborrow;
2912
	}
2913
	function SetBorrow($borrow) {
2914
		$this->qborrow = $borrow;
2915
	}
2916
	function CanHaveChildren() {
2917
		return true;
2918
	}
2919

    
2920
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2921

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

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

    
2943
		return $q;
2944
	}
2945

    
2946
	function copy_queue($interface, &$cflink) {
2947

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

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

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

    
3024
	function delete_queue() {
3025
		unref_on_altq_queue_list($this->GetQname());
3026
		cleanup_queue_from_rules($this->GetQname());
3027
		foreach ($this->subqueues as $q) {
3028
			$q->delete_queue();
3029
		}
3030
		unset_object_by_reference($this->GetLink());
3031
	}
3032

    
3033
	function validate_input($data, &$input_errors) {
3034
		parent::validate_input($data, $input_errors);
3035

    
3036
		$reqdfields[] = "bandwidth";
3037
		$reqdfieldsn[] = gettext("Bandwidth");
3038
		$reqdfields[] = "bandwidthtype";
3039
		$reqdfieldsn[] = gettext("Bandwidthtype");
3040

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

    
3043
		if ($data['priority'] > 7) {
3044
			$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3045
		}
3046

    
3047
		$parent = $this->GetParent();
3048
		if (method_exists($parent, 'GetParent') && ($parent->GetBorrow() != "on") &&
3049
		    ($data['borrow'] == 'yes')) {
3050
			$input_errors[] = gettext("You cannot set 'Borrow' if the parent queue also does not borrow.");
3051
		}
3052
	}
3053

    
3054
	function ReadConfig(&$q) {
3055
		parent::ReadConfig($q);
3056
		if (!empty($q['borrow'])) {
3057
			$this->SetBorrow("on");
3058
		} else {
3059
			$this->SetBorrow("");
3060
		}
3061
	}
3062

    
3063
	function build_javascript() {
3064
		return parent::build_javascript();
3065
	}
3066

    
3067
	function build_tree() {
3068
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3069
		$tree .= "\" ";
3070
		$tmpvalue = trim($this->GetDefault());
3071
		if (!empty($tmpvalue)) {
3072
			$tree .= " class=\"navlnk\"";
3073
		}
3074
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3075
		if (is_array($this->subqueues)) {
3076
			$tree .= "<ul>";
3077
			foreach ($this->subqueues as $q) {
3078
				$tree .= $q->build_tree();
3079
			}
3080
			$tree .= "</ul>";
3081
		}
3082
		$tree .= "</li>";
3083
		return $tree;
3084
	}
3085

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

    
3166
		$pfq_rule .= " \n";
3167
		return $pfq_rule;
3168
	}
3169

    
3170
	function build_form() {
3171
		$sform = parent::build_form();
3172

    
3173
		$section = new Form_Section('NOTITLE');
3174

    
3175
		$group = new Form_Group('Bandwidth');
3176

    
3177
		$group->add(new Form_Input(
3178
			'bandwidth',
3179
			null,
3180
			'number',
3181
			$this->GetBandwidth()
3182
		));
3183

    
3184
		$group->add(new Form_Select(
3185
			'bandwidthtype',
3186
			null,
3187
			$this->FormGetBwscale(),
3188
			array('Kb' => 'Kbit/s',
3189
				  'Mb' => 'Mbit/s',
3190
				  'Gb' => 'Gbit/s',
3191
				  'b' => 'Bit/s',
3192
				  '%' => '%')
3193
		));
3194

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

    
3197
		$section->add($group);
3198

    
3199
		$section->addInput(new Form_Checkbox(
3200
			'borrow',
3201
			'Scheduler option',
3202
			'Borrow from other queues when available',
3203
			($this->GetBorrow() == "on")
3204
		));
3205

    
3206
		$sform->add($section);
3207

    
3208
		return $sform;
3209
	}
3210

    
3211
	function update_altq_queue_data(&$data) {
3212
		$this->ReadConfig($data);
3213
	}
3214

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

    
3267
class fairq_queue extends priq_queue {
3268
	var $hogs;
3269
	var $buckets;
3270

    
3271
	function GetBuckets() {
3272
		return $this->buckets;
3273
	}
3274
	function SetBuckets($buckets) {
3275
		$this->buckets = $buckets;
3276
	}
3277
	function GetHogs() {
3278
		return $this->hogs;
3279
	}
3280
	function SetHogs($hogs) {
3281
		$this->hogs = $hogs;
3282
	}
3283
	function CanHaveChildren() {
3284
		return false;
3285
	}
3286

    
3287

    
3288
	function copy_queue($interface, &$cflink) {
3289
		$cflink['interface'] = $interface;
3290
		$cflink['qlimit'] = $this->GetQlimit();
3291
		$cflink['priority'] = $this->GetQpriority();
3292
		$cflink['name'] = $this->GetQname();
3293
		$cflink['description'] = $this->GetDescription();
3294
		$cflink['bandwidth'] = $this->GetBandwidth();
3295
		$cflink['bandwidthtype'] = $this->GetBwscale();
3296
		$cflink['enabled'] = $this->GetEnabled();
3297
		$cflink['default'] = $this->GetDefault();
3298
		$cflink['red'] = $this->GetRed();
3299
		$cflink['rio'] = $this->GetRio();
3300
		$cflink['ecn'] = $this->GetEcn();
3301
		$cflink['buckets'] = $this->GetBuckets();
3302
		$cflink['hogs'] = $this->GetHogs();
3303
	}
3304

    
3305
	/*
3306
	 * Should search even its children
3307
	 */
3308
	function &find_queue($interface, $qname) {
3309
		if ($qname == $this->GetQname()) {
3310
			return $this;
3311
		}
3312
	}
3313

    
3314
	function find_parentqueue($interface, $qname) { return; }
3315

    
3316
	function delete_queue() {
3317
		unref_on_altq_queue_list($this->GetQname());
3318
		cleanup_queue_from_rules($this->GetQname());
3319
		unset_object_by_reference($this->GetLink());
3320
	}
3321

    
3322
	function validate_input($data, &$input_errors) {
3323
		parent::validate_input($data, $input_errors);
3324

    
3325
		if ($data['priority'] > 7) {
3326
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3327
		}
3328
		$reqdfields[] = "bandwidth";
3329
		$reqdfieldsn[] = gettext("Bandwidth");
3330
		$reqdfields[] = "bandwidthtype";
3331
		$reqdfieldsn[] = gettext("Bandwidthtype");
3332

    
3333
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3334
	}
3335

    
3336
	function ReadConfig(&$q) {
3337
		parent::ReadConfig($q);
3338
		if (!empty($q['buckets'])) {
3339
			$this->SetBuckets($q['buckets']);
3340
		} else {
3341
			$this->SetBuckets("");
3342
		}
3343
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3344
			$this->SetHogs($q['hogs']);
3345
		} else {
3346
			$this->SetHogs("");
3347
		}
3348
	}
3349

    
3350
	function build_javascript() {
3351
		return parent::build_javascript();
3352
	}
3353

    
3354
	function build_tree() {
3355
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3356
		$this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3357
		$tree .= "\" ";
3358
		$tmpvalue = trim($this->GetDefault());
3359
		if (!empty($tmpvalue)) {
3360
			$tree .= " class=\"navlnk\"";
3361
		}
3362
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3363
		$tree .= "</li>";
3364
		return $tree;
3365
	}
3366

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

    
3439
		$pfq_rule .= " \n";
3440
		return $pfq_rule;
3441
	}
3442

    
3443
	function build_form() {
3444
		$form = parent::build_form();
3445

    
3446
		$section = new Form_Section('');
3447

    
3448
		$group = new Form_Group('Bandwidth');
3449

    
3450
		$group->add(new Form_Input(
3451
			'bandwidth',
3452
			null,
3453
			'number',
3454
			$this->GetBandwidth()
3455
		));
3456

    
3457
		$group->add(new Form_Select(
3458
			'bandwidthtype',
3459
			null,
3460
			$this->FormGetBwscale(),
3461
			array('Kb' => 'Kbit/s',
3462
				  'Mb' => 'Mbit/s',
3463
				  'Gb' => 'Gbit/s',
3464
				  'b' => 'Bit/s',
3465
				  '%' => '%')
3466
		));
3467

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

    
3470
		$section->add($group);
3471

    
3472
		$section->addInput(new Form_Input(
3473
			'buckets',
3474
			'Scheduler specific options',
3475
			'text',
3476
			$this->GetBuckets()
3477
		))->setHelp('Number of buckets available');
3478

    
3479
		$section->addInput(new Form_Input(
3480
			'hogs',
3481
			'',
3482
			'text',
3483
			$this->GetHogs()
3484
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3485

    
3486
		$form->add($section);
3487
		return $form;
3488
	}
3489

    
3490
	function update_altq_queue_data(&$data) {
3491
		$this->ReadConfig($data);
3492
	}
3493

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

    
3550

    
3551
/*
3552
 * dummynet(4) wrappers.
3553
 */
3554

    
3555

    
3556
/*
3557
 * List of respective objects!
3558
 */
3559
$dummynet_pipe_list = array();
3560

    
3561
class dummynet_class {
3562
	var $qname;
3563
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3564
	var $qlimit;
3565
	var $description;
3566
	var $qenabled;
3567
	var $link;
3568
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3569
	var $plr;
3570

    
3571
	var $buckets;
3572
	/* mask parameters */
3573
	var $mask;
3574
	var $noerror;
3575

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

    
3653
	function build_javascript() {
3654
		$javascript .= "<script type=\"text/javascript\">\n";
3655
		$javascript .= "//<![CDATA[\n";
3656
		$javascript .= "function enable_maskbits(enable_over) {\n";
3657
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3658
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3659
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3660
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3661
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3662
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3663
		$javascript .= "} else {\n";
3664
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3665
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3666
		$javascript .= "}}\n";
3667
		$javascript .= "//]]>\n";
3668
		$javascript .= "</script>\n";
3669
		return $javascript;
3670
	}
3671

    
3672
	function validate_input($data, &$input_errors) {
3673
		$reqdfields[] = "bandwidth";
3674
		$reqdfieldsn[] = gettext("Bandwidth");
3675
		/*$reqdfields[] = "burst";
3676
		$reqdfieldsn[] = gettext("Burst"); */
3677
		$reqdfields[] = "bandwidthtype";
3678
		$reqdfieldsn[] = gettext("Bandwidthtype");
3679
		$reqdfields[] = "newname";
3680
		$reqdfieldsn[] = gettext("Name");
3681

    
3682
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3683

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

    
3713
	function build_mask_rules(&$pfq_rule) {
3714
		$mask = $this->GetMask();
3715
		if (!empty($mask['type'])) {
3716
			if ($mask['type'] <> 'none') {
3717
				$pfq_rule .= " mask";
3718
			}
3719
			switch ($mask['type']) {
3720
				case 'srcaddress':
3721
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3722
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3723
					} else {
3724
						$pfq_rule .= " src-ip6 /128";
3725
					}
3726
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3727
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3728
					} else {
3729
						$pfq_rule .= " src-ip 0xffffffff";
3730
					}
3731
					break;
3732
				case 'dstaddress':
3733
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3734
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3735
					} else {
3736
						$pfq_rule .= " dst-ip6 /128";
3737
					}
3738
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3739
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3740
					} else {
3741
						$pfq_rule .= " dst-ip 0xffffffff";
3742
					}
3743
					break;
3744
				default:
3745
					break;
3746
			}
3747
		}
3748
	}
3749

    
3750
}
3751

    
3752
class dnpipe_class extends dummynet_class {
3753
	var $delay;
3754
	var $qbandwidth = array();
3755
	var $qbandwidthtype;
3756

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

    
3807
		/* This is here to help on form building and building rules/lists */
3808
	var $subqueues = array();
3809

    
3810
	function CanHaveChildren() {
3811
		return true;
3812
	}
3813
	function SetDelay($delay) {
3814
		$this->delay = $delay;
3815
	}
3816
	function GetDelay() {
3817
		return $this->delay;
3818
	}
3819
	function delete_queue() {
3820
		cleanup_dnqueue_from_rules($this->GetQname());
3821
		foreach ($this->subqueues as $q) {
3822
			$q->delete_queue();
3823
		}
3824
		unset_dn_object_by_reference($this->GetLink());
3825
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3826
		@pfSense_ipfw_pipe("sched delete " . $this->GetNumber());
3827
	}
3828
	function GetBandwidth() {
3829
		return $this->qbandwidth;
3830
	}
3831
	function SetBandwidth($bandwidth) {
3832
		$this->qbandwidth = $bandwidth;
3833
	}
3834
	function GetBurst() {
3835
		return $this->qburst;
3836
	}
3837
	function SetBurst($burst) {
3838
		$this->qburst = $burst;
3839
	}
3840

    
3841
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3842

    
3843
		if (!is_array($this->subqueues)) {
3844
			$this->subqueues = array();
3845
		}
3846

    
3847
		$__tmp_q = new dnqueue_class(); $q =& $__tmp_q;
3848
		$q->SetLink($path);
3849
		$q->SetEnabled("on");
3850
		$q->SetPipe($this->GetQname());
3851
		$q->SetParent($this);
3852
		$q->ReadConfig($queue);
3853
		$q->validate_input($queue, $input_errors);
3854

    
3855
		if (!is_array($input_errors)) {
3856
			$input_errors = array();
3857
		}
3858

    
3859
		if (count($input_errors)) {
3860
			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)));
3861
			return $q;
3862
		}
3863
		$number = dnqueue_find_nextnumber();
3864
		$q->SetNumber($number);
3865
		$this->subqueues[$q->GetQname()] = &$q;
3866

    
3867
		return $q;
3868
	}
3869

    
3870
	function &get_queue_list(&$q = null) {
3871
		$qlist = array();
3872

    
3873
		$qlist[$this->GetQname()] = $this->GetNumber();
3874
		if (is_array($this->subqueues)) {
3875
			foreach ($this->subqueues as $queue) {
3876
				$queue->get_queue_list($qlist);
3877
			}
3878
		}
3879
		return $qlist;
3880
	}
3881

    
3882
	/*
3883
	 * Should search even its children
3884
	 */
3885
	function &find_queue($pipe, $qname) {
3886
		if ($qname == $this->GetQname()) {
3887
			return $this;
3888
		}
3889
		foreach ($this->subqueues as $q) {
3890
			$result =& $q->find_queue("", $qname);
3891
			if ($result) {
3892
				return $result;
3893
			}
3894
		}
3895
	}
3896

    
3897
	function &find_parentqueue($pipe, $qname) {
3898
		return NULL;
3899
	}
3900

    
3901
	function validate_input($data, &$input_errors) {
3902
		parent::validate_input($data, $input_errors);
3903

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

    
3943
		/* Limiter patch */
3944
		$selectedScheduler = getSchedulers()[$data['sched']];
3945
		if (!$selectedScheduler) {
3946
			$input_errors[] = gettext("Selected scheduler not recognized.");
3947
		}
3948
		$selectedAqm = getAQMs()[$data['aqm']];
3949
		if (!empty($data['aqm']) && !$selectedAqm) {
3950
			$input_errors[] = gettext("Selected AQM not recognized.");
3951
		}
3952
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedScheduler["ecn"] && !$selectedAqm["ecn"]) {
3953
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but neither " . $selectedAqm["name"] . " nor " . $selectedScheduler["name"] . " support it.");
3954
		}
3955
		/* End limiter patch */
3956
	}
3957

    
3958
	function ReadConfig(&$q) {
3959
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3960
			$this->SetQname($q['newname']);
3961
			rename_dnqueue_in_rules($q['name'], $q['newname']);
3962
		} else if (!empty($q['newname'])) {
3963
			$this->SetQname($q['newname']);
3964
		} else {
3965
			$this->SetQname($q['name']);
3966
		}
3967
		$this->SetNumber($q['number']);
3968

    
3969
		if (!empty($_POST)) {
3970
			$bandwidth = array();
3971
			/* XXX: Really no better way? */
3972
			for ($i = 0; $i < 2900; $i++) {
3973
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3974
					$bw = array();
3975
					$bw['bw'] = $q["bandwidth{$i}"];
3976
					$bw['burst'] = $q["burst{$i}"];
3977
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3978
						$bw['bwscale'] = $q["bwtype{$i}"];
3979
					}
3980
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3981
						$bw['bwsched'] = $q["bwsched{$i}"];
3982
					}
3983
					$bandwidth[] = $bw;
3984
				}
3985
			}
3986
			$this->SetBandwidth($bandwidth);
3987
		}
3988

    
3989
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3990
			$this->SetBandwidth($q['bandwidth']['item']);
3991
			$this->SetBurst($q['burst']['item']);
3992
		}
3993

    
3994
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3995
			$this->SetQlimit($q['qlimit']);
3996
		} else {
3997
			$this->SetQlimit("");
3998
		}
3999
		if (isset($q['mask']) && $q['mask'] <> "") {
4000
			$masktype = $q['mask'];
4001
		} else {
4002
			$masktype = "";
4003
		}
4004
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4005
			$maskbits = $q['maskbits'];
4006
		} else {
4007
			$maskbits = "";
4008
		}
4009
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4010
			$maskbitsv6 = $q['maskbitsv6'];
4011
		} else {
4012
			$maskbitsv6 = "";
4013
		}
4014
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4015
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4016
			$this->SetBuckets($q['buckets']);
4017
		} else {
4018
			$this->SetBuckets("");
4019
		}
4020
		if (isset($q['plr']) && $q['plr'] <> "") {
4021
			$this->SetPlr($q['plr']);
4022
		} else {
4023
			$this->SetPlr("");
4024
		}
4025
		if (isset($q['delay']) && $q['delay'] <> "") {
4026
			$this->SetDelay($q['delay']);
4027
		} else {
4028
			$this->SetDelay(0);
4029
		}
4030
		if (isset($q['description']) && $q['description'] <> "") {
4031
			$this->SetDescription($q['description']);
4032
		} else {
4033
			$this->SetDescription("");
4034
		}
4035
		$this->SetEnabled($q['enabled']);
4036

    
4037
		/* Limiter patch */
4038
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4039
				$this->SetAQM($q['aqm']);
4040
		} else {
4041
				$this->SetAQM('droptail');
4042
		}
4043
		// parse AQM arguments
4044
		$aqm_map = getAQMs();
4045
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4046
		if (is_array($selectedParameters)) {
4047
			foreach ($selectedParameters as $key => $value) {
4048
				$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4049
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4050
					$this->SetAQMParameter($key, $q[$config_key]);
4051
				} else {
4052
					$this->SetAQMParameter($key, $value["default"]);
4053
				}
4054
			}
4055
		}
4056

    
4057
		if (isset($q['sched']) && $q['sched'] <> "") {
4058
				$this->SetScheduler($q['sched']);
4059
		} else {
4060
				$this->SetScheduler('fifo');
4061
		}
4062
		$scheduler_map = getSchedulers();
4063
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4064
		if (is_array($selectedParameters)) {
4065
			foreach ($selectedParameters as $key => $value) {
4066
				$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4067
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
4068
					$this->SetSchedulerParameter($key, $q[$config_key]);
4069
				} else {
4070
					$this->SetSchedulerParameter($key, $value["default"]);
4071
				}
4072
			}
4073
		}
4074

    
4075
		// ecn flag
4076
		$this->SetECN($q['ecn']);
4077
		/* End limiter patch */
4078
	}
4079

    
4080
	function build_tree() {
4081
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($this->GetQname()) ."&amp;queue=".htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4082
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4083
		if (is_array($this->subqueues)) {
4084
			$tree .= "<ul>";
4085
			foreach ($this->subqueues as $q) {
4086
				$tree .= $q->build_tree();
4087
			}
4088
			$tree .= "</ul>";
4089
		}
4090
		$tree .= "</li>";
4091

    
4092
		return $tree;
4093
	}
4094

    
4095
	function build_rules() {
4096
		global $config, $time_based_rules;
4097

    
4098
		if ($this->GetEnabled() == "") {
4099
			return;
4100
		}
4101

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

    
4144
		if ($this->GetQlimit()) {
4145
			$pfq_rule .= " queue " . $this->GetQlimit();
4146
		}
4147
		if ($this->GetPlr()) {
4148
			$pfq_rule .= " plr " . $this->GetPlr();
4149
		}
4150
		if ($this->GetBuckets()) {
4151
			$pfq_rule .= " buckets " . $this->GetBuckets();
4152
		}
4153
		if ($this->GetDelay()) {
4154
			$pfq_rule .= " delay " . $this->GetDelay();
4155
		}
4156
		$this->build_mask_rules($pfq_rule);
4157

    
4158
		/* Limiter patch */
4159
		$selectedAQM = getAQMs()[$this->getAQM()];
4160
		if ($selectedAQM) {
4161
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4162
			if ($selectedAQM["ecn"]) {
4163
				if ($this->getECN() == 'on') {
4164
					$pfq_rule .= ' ecn';
4165
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4166
					$pfq_rule .= ' noecn';
4167
				}
4168
			}
4169
		}
4170
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4171
		if ($selectedScheduler) {
4172
			$pfq_rule .= "\nsched ". $this->GetNumber() . " config ";
4173
			$pfq_rule .= "pipe ". $this->GetNumber();
4174
			$this->build_mask_rules($pfq_rule);
4175
			$pfq_rule .= " " . FormatParameters($selectedScheduler["parameter_format"], $this->GetSchedulerParameters());			
4176
			if ($selectedScheduler["ecn"]) {
4177
				if ($this->getECN() == 'on') {
4178
					$pfq_rule .= ' ecn';
4179
				} else {
4180
					$pfq_rule .= ' noecn';
4181
				}
4182
			}
4183
		}
4184
		$pfq_rule .= "\n";
4185
		/* End patch */
4186

    
4187
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
4188
			foreach ($this->subqueues as $q) {
4189
				$pfq_rule .= $q->build_rules();
4190
			}
4191
		}
4192

    
4193
		$pfq_rule .= " \n";
4194

    
4195
		return $pfq_rule;
4196
	}
4197

    
4198
	function update_dn_data(&$data) {
4199
		$this->ReadConfig($data);
4200
	}
4201

    
4202
	function build_javascript() {
4203
		global $g, $config;
4204

    
4205
		$javasr = parent::build_javascript();
4206

    
4207
		//build list of schedules
4208
		$schedules = "<option value='none'>none</option>";
4209
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4210
			foreach ($config['schedules']['schedule'] as $schedule) {
4211
				if ($schedule['name'] <> "") {
4212
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
4213
				}
4214
			}
4215
		}
4216
		$bwopt = "";
4217
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
4218
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
4219
		}
4220

    
4221
		$javasr .= <<<EOD
4222
<script type='text/javascript'>
4223
//<![CDATA[
4224
var addBwRowTo = (function() {
4225

    
4226
	return (function (tableId) {
4227

    
4228
	var table = document.getElementById(tableId);
4229
	var totalrows = table.rows.length -1;
4230

    
4231
	var row = table.insertRow(totalrows + 1);
4232
	var cell1 = row.insertCell(0);
4233
	var cell2 = row.insertCell(1);
4234
	var cell3 = row.insertCell(2);
4235
	var cell4 = row.insertCell(3);
4236

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

    
4242
	});
4243
})();
4244

    
4245
function removeBwRow(el) {
4246
	var d = el.parentNode.parentNode.rowIndex;
4247
	document.getElementById('maintable').deleteRow(d);
4248
}
4249

    
4250
function ceil_func(el){
4251
	el.value = Math.ceil(el.value);
4252

    
4253
}
4254
//]]>
4255
</script>
4256

    
4257
EOD;
4258

    
4259
		return $javasr;
4260
	}
4261

    
4262
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
4263
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
4264
	// with the javascript in this class
4265
	function build_bwtable() {
4266
		global $config;
4267

    
4268
		$bandwidth = $this->GetBandwidth();
4269
				//build list of schedules
4270
		$schedules = array();
4271
		$schedules[] = "none";//leave none to leave rule enabled all the time
4272
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4273
			foreach ($config['schedules']['schedule'] as $schedule) {
4274
				if ($schedule['name'] != "") {
4275
					$schedules[] = $schedule['name'];
4276
				}
4277
			}
4278
		}
4279

    
4280
		$form = '<div class="table-responsive">';
4281
		$form .= '<table id="maintable" class="table table-hover table-striped">';
4282
		$form .= "<thead><tr>";
4283
		$form .= "<th>Bandwidth</th>";
4284
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
4285
		$form .= "<th>Bw type</th>";
4286
		$form .= "<th>Schedule</th>";
4287
		$form .= "<th></th>";
4288
		$form .= "</tr></thead>";
4289
		$form .= "<tbody>";
4290

    
4291
		// If there are no bandwidths defined, make a blank one for convenience
4292
		if (empty($bandwidth)) {
4293
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Mb', 'bwsched' => 'none'));
4294
		}
4295

    
4296
		if (is_array($bandwidth)) {
4297
			foreach ($bandwidth as $bwidx => $bw) {
4298
				$form .= '<tr>';
4299
				$form .= '<td class="col-xs-4">';
4300
				$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\"/>";
4301
				//$form .= "</td><td width='20%'>";
4302
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
4303
				$form .= "</td>";
4304
				$form .= '<td class="col-xs-4">';
4305
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
4306

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

    
4310
					if ($bw['bwscale'] == $bwsidx) {
4311
						$form .= " selected";
4312
					}
4313

    
4314
					$form .= ">{$bwscale}</option>";
4315
				}
4316

    
4317
				$form .= "</select>";
4318
				$form .= "</td>";
4319
				$form .= '<td class="col-xs-4">';
4320
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
4321

    
4322
				foreach ($schedules as $schd) {
4323
					$selected = "";
4324
					if ($bw['bwsched'] == $schd) {
4325
						$selected = "selected";
4326
					}
4327

    
4328
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
4329
				}
4330

    
4331
				$form .= "</select>";
4332
				$form .= "</td>";
4333
				$form .= '<td>';
4334
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
4335
				$form .= "</td></tr>";
4336
			}
4337
		}
4338
		$form .= "</tbody></table></div><br />";
4339

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

    
4344
		return($form);
4345
	}
4346

    
4347
	function build_form() {
4348
		global $g, $config, $pipe, $action, $qname;
4349

    
4350
		//build list of schedules
4351
		$schedules = array();
4352
		$schedules[] = "none";//leave none to leave rule enabled all the time
4353
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4354
			foreach ($config['schedules']['schedule'] as $schedule) {
4355
				if ($schedule['name'] <> "") {
4356
					$schedules[] = $schedule['name'];
4357
				}
4358
			}
4359
		}
4360

    
4361

    
4362
		$sform = new Form();
4363
		$sform->setAction("firewall_shaper.php");
4364

    
4365
		$section = new Form_Section('Limiters');
4366

    
4367
		$section->addInput(new Form_Checkbox(
4368
			'enabled',
4369
			'Enable',
4370
			'Enable limiter and its children',
4371
			($this->GetEnabled() == "on"),
4372
			'on'
4373
		));
4374

    
4375
		$section->addInput(new Form_Input(
4376
			'newname',
4377
			'*Name',
4378
			'text',
4379
			$this->GetQname()
4380
		));
4381

    
4382
		$section->addInput(new Form_Input(
4383
			'name',
4384
			null,
4385
			'hidden',
4386
			$this->GetQname()
4387
		));
4388

    
4389
		if ($this->GetNumber() > 0) {
4390
			$section->addInput(new Form_Input(
4391
				'number',
4392
				null,
4393
				'hidden',
4394
				$this->GetNumber()
4395
			));
4396
		}
4397

    
4398
		$bandwidth = $this->GetBandwidth();
4399

    
4400
		if (is_array($bandwidth)) {
4401
				$section->addInput(new Form_StaticText(
4402
				'Bandwidth',
4403
				$this->build_bwtable()
4404
			));
4405
		}
4406

    
4407
		$mask = $this->GetMask();
4408

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

    
4418
		$group = new Form_Group(null);
4419

    
4420
		$group->add(new Form_Select(
4421
			'maskbits',
4422
			null,
4423
			$mask['bits'],
4424
			array_combine(range(32, 1, -1), range(32, 1, -1))
4425
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4426

    
4427
		$group->add(new Form_Select(
4428
			'maskbitsv6',
4429
			null,
4430
			$mask['bitsv6'],
4431
			array_combine(range(128, 1, -1), range(128, 1, -1))
4432
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4433

    
4434
		$section->add($group);
4435

    
4436
		$section->addInput(new Form_Input(
4437
			'description',
4438
			'Description',
4439
			'text',
4440
			$this->GetDescription()
4441
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4442

    
4443
		$sform->add($section);
4444

    
4445
		/* Begin limiter patch */
4446
		$aqm_map = getAQMs();
4447
		$scheduler_map = getSchedulers();
4448

    
4449
		$section = new Form_Section('Queue');
4450
		$section->addInput(new Form_Select(
4451
				'aqm',
4452
				'Queue Management Algorithm',
4453
				$this->GetAQM(),
4454
				array_map_assoc(function ($k, $v) {
4455
					return [$k, $v["name"]];
4456
				}, $aqm_map)
4457
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside the limiter, ' .
4458
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4459
						'network congestion.');
4460

    
4461
		$section->addInput(new Form_StaticText(
4462
			'',
4463
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4464
		))->setHelp('Specifies the queue management algorithm parameters.');
4465

    
4466
		$section->addInput(new Form_Select(
4467
				'sched',
4468
				'Scheduler',
4469
				$this->GetScheduler(),
4470
				array_map_assoc(function ($k, $v) {
4471
					return [$k, $v["name"]];
4472
				}, $scheduler_map)
4473
		))->setHelp('The scheduler manages the sequence of network packets in the limiter\'s queue.');
4474

    
4475
		$section->addInput(new Form_StaticText(
4476
			'',
4477
			build_queue_params($scheduler_map, $this->GetScheduler(), $this->GetSchedulerParameters(), "sched")
4478
		))->setHelp('Specifies the scheduler parameters.');
4479

    
4480
		$section->addInput(new Form_Input(
4481
				'qlimit',
4482
				'Queue length',
4483
				'number',
4484
				$this->GetQlimit()
4485
		))->setHelp('Specifies the length of the limiter\'s queue, which the scheduler and AQM are responsible for. ' .
4486
			'This field may be left empty.');
4487

    
4488
		$section->addInput(new Form_Checkbox(
4489
			'ecn',
4490
			'ECN',
4491
			'Enable Explicit Congestion Notification (ECN)',
4492
			($this->GetECN() == "on"),
4493
			'on'
4494
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4495

    
4496
		$sform->add($section);
4497
		/* End limiter patch */
4498

    
4499
		$section = new Form_Section('Advanced Options');
4500

    
4501
		$section->addInput(new Form_Input(
4502
			'delay',
4503
			'Delay (ms)',
4504
			'text',
4505
			$this->GetDelay() > 0 ? $this->GetDelay():null
4506
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4507

    
4508
		$section->addInput(new Form_Input(
4509
			'plr',
4510
			'Packet Loss Rate',
4511
			'number',
4512
			$this->GetPlr(),
4513
			['step' => '0.001', 'min' => '0.000']
4514
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4515
					'A value of 0.001 means one packet in 1000 gets dropped.');
4516

    
4517
		$section->addInput(new Form_Input(
4518
			'buckets',
4519
			'Bucket size (slots)',
4520
			'number',
4521
			$this->GetBuckets()
4522
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4523

    
4524
		$sform->add($section);
4525

    
4526
		return($sform);
4527
		}
4528

    
4529
	function wconfig() {
4530
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4531
		if (!is_array($cflink)) {
4532
			$cflink = array();
4533
		}
4534
		$cflink['name'] = $this->GetQname();
4535
		$cflink['number'] = $this->GetNumber();
4536
		$cflink['qlimit'] = $this->GetQlimit();
4537
		$cflink['plr'] = $this->GetPlr();
4538
		$cflink['description'] = $this->GetDescription();
4539

    
4540
		$bandwidth = $this->GetBandwidth();
4541
		if (is_array($bandwidth)) {
4542
			$cflink['bandwidth'] = array();
4543
			$cflink['bandwidth']['item'] = array();
4544
			foreach ($bandwidth as $bwidx => $bw) {
4545
				$cflink['bandwidth']['item'][] = $bw;
4546
			}
4547
		}
4548

    
4549
		$cflink['enabled'] = $this->GetEnabled();
4550
		$cflink['buckets'] = $this->GetBuckets();
4551
		$mask = $this->GetMask();
4552
		$cflink['mask'] = $mask['type'];
4553
		$cflink['maskbits'] = $mask['bits'];
4554
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4555
		$cflink['delay'] = $this->GetDelay();
4556

    
4557
		/* Limiter queue patch */
4558
		$cflink['sched'] = $this->GetScheduler();
4559
		$scheduler_map = getSchedulers();
4560
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4561
		foreach ($selectedParameters as $key => $value) {
4562
			$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4563
			$cflink[$config_key] = $this->GetSchedulerParameter($key);
4564
		}
4565
		$cflink['aqm'] = $this->GetAQM();
4566
		$aqm_map = GetAQMs();
4567
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4568
		foreach ($selectedParameters as $key => $value) {
4569
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4570
			$cflink[$config_key] = $this->GetAQMParameter($key);
4571
		}
4572
		$cflink['ecn'] = $this->GetECN();
4573
		/* End limiter queue patch */
4574
	}
4575

    
4576
}
4577

    
4578
class dnqueue_class extends dummynet_class {
4579
	var $pipeparent;
4580
	var $weight;
4581
	/* Limiter queue patch */
4582
    var $ecn; // ecn 'on' or 'off'
4583
    var $aqm; // key to aqm_map
4584
    var $aqm_params = array(); // AQM params
4585
	function GetAQM() {
4586
			return $this->aqm;
4587
	}
4588
	function SetAQM($aqm) {
4589
			$this->aqm = $aqm;
4590
	}
4591
	function GetAQMParameters() {
4592
			return $this->aqm_params;
4593
	}
4594
	function GetAQMParameter($parameter) {
4595
			return $this->aqm_params[$parameter];
4596
	}
4597
	function SetAQMParameter($key, $value) {
4598
			return $this->aqm_params[$key] = $value;
4599
	}
4600
	function SetAQMParameters($params) {
4601
			$this->aqm_params = $params;
4602
	}
4603
	function GetECN() {
4604
			return $this->ecn;
4605
	}
4606
	function SetECN($ecn) {
4607
			$this->ecn = $ecn;
4608
	}
4609
	/* End limiter queue patch */
4610

    
4611
	function GetWeight() {
4612
		return $this->weight;
4613
	}
4614
	function SetWeight($weight) {
4615
		$this->weight = $weight;
4616
	}
4617
	function GetPipe() {
4618
		return $this->pipeparent;
4619
	}
4620
	function SetPipe($pipe) {
4621
		$this->pipeparent = $pipe;
4622
	}
4623

    
4624
	/* Just a stub in case we ever try to call this from the frontend. */
4625
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4626
		return;
4627
	}
4628

    
4629
	function delete_queue() {
4630
		cleanup_dnqueue_from_rules($this->GetQname());
4631
		unset_dn_object_by_reference($this->GetLink());
4632
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4633
	}
4634

    
4635
	function validate_input($data, &$input_errors) {
4636
		parent::validate_input($data, $input_errors);
4637

    
4638

    
4639
		/* Limiter patch */
4640
		$selectedAqm = getAQMs()[$data['aqm']];
4641
		if (!empty($data['aqm']) && !$selectedAqm) {
4642
			$input_errors[] = gettext("Selected AQM not recognized.");
4643
		}
4644
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedAqm["ecn"]) {
4645
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but " . $selectedAqm["name"] . " does not support it.");
4646
		}
4647
		/* End limiter patch */
4648

    
4649
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4650
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4651
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4652
		}
4653
	}
4654

    
4655
	/*
4656
	 * Should search even its children
4657
	 */
4658
	function &find_queue($pipe, $qname) {
4659
		if ($qname == $this->GetQname()) {
4660
			return $this;
4661
		} else {
4662
			return NULL;
4663
		}
4664
	}
4665

    
4666
	function &find_parentqueue($pipe, $qname) {
4667
		return $this->qparent;
4668
	}
4669

    
4670
	function &get_queue_list(&$qlist) {
4671
		if ($this->GetEnabled() == "") {
4672
			return;
4673
		}
4674
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4675
	}
4676

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

    
4729
		/* Limiter patch */
4730
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4731
				$this->SetAQM($q['aqm']);
4732
		} else {
4733
				$this->SetAQM('droptail');
4734
		}
4735
		// parse AQM arguments
4736
		$aqm_map = getAQMs();
4737
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4738
		foreach ($selectedParameters as $key => $value) {
4739
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4740
			if (isset($q[$config_key]) && $q[$config_key] <> "") {
4741
				$this->SetAQMParameter($key, $q[$config_key]);
4742
			} else {
4743
				$this->SetAQMParameter($key, $value["default"]);
4744
			}
4745
		}
4746

    
4747
		// ecn flag
4748
		$this->SetECN($q['ecn']);
4749
		/* End limiter patch */
4750
	}
4751

    
4752
	function build_tree() {
4753
		$parent =& $this->GetParent();
4754
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($parent->GetQname()) ."&amp;queue=" . htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4755
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4756
		$tree .= "</li>";
4757

    
4758
		return $tree;
4759
	}
4760

    
4761
	function build_rules() {
4762
		if ($this->GetEnabled() == "") {
4763
			return;
4764
		}
4765

    
4766
		$parent =& $this->GetParent();
4767
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4768
		if ($this->GetQlimit()) {
4769
			$pfq_rule .= " queue " . $this->GetQlimit();
4770
		}
4771
		if ($this->GetWeight()) {
4772
			$pfq_rule .= " weight " . $this->GetWeight();
4773
		}
4774
		if ($this->GetBuckets()) {
4775
			$pfq_rule .= " buckets " . $this->GetBuckets();
4776
		}
4777
		$this->build_mask_rules($pfq_rule);
4778

    
4779
		/* Limiter patch */
4780
		$selectedAQM = getAQMs()[$this->getAQM()];
4781
		if ($selectedAQM) {
4782
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4783
			if ($selectedAQM["ecn"]) {
4784
				if ($this->getECN() == 'on') {
4785
					$pfq_rule .= ' ecn';
4786
				} elseif (($this->getAQM() != 'red') && ($this->getAQM() != 'gred')) {
4787
					$pfq_rule .= ' noecn';
4788
				}
4789
			}
4790
		}
4791
		/* End patch */
4792

    
4793
		$pfq_rule .= "\n";
4794

    
4795
		return $pfq_rule;
4796
	}
4797

    
4798
	function build_javascript() {
4799
		return parent::build_javascript();
4800
	}
4801

    
4802
	function build_form() {
4803
		global $g, $config, $pipe, $action, $qname;
4804

    
4805
		//build list of schedules
4806
		$schedules = array();
4807
		$schedules[] = "none";//leave none to leave rule enabled all the time
4808
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4809
			foreach ($config['schedules']['schedule'] as $schedule) {
4810
				if ($schedule['name'] <> "") {
4811
					$schedules[] = $schedule['name'];
4812
				}
4813
			}
4814
		}
4815

    
4816

    
4817
		$sform = new Form();
4818
		$sform->setAction("firewall_shaper.php");
4819
		$section = new Form_Section('Limiters');
4820

    
4821
		$section->addInput(new Form_Checkbox(
4822
			'enabled',
4823
			'Enable',
4824
			'Enable this queue',
4825
			($this->GetEnabled() == "on"),
4826
			'on'
4827
		));
4828

    
4829
		$section->addInput(new Form_Input(
4830
			'newname',
4831
			'*Name',
4832
			'text',
4833
			$this->GetQname()
4834
		));
4835

    
4836
		$section->addInput(new Form_Input(
4837
			'name',
4838
			null,
4839
			'hidden',
4840
			$this->GetQname()
4841
		));
4842

    
4843
		if ($this->GetNumber() > 0) {
4844
			$section->addInput(new Form_Input(
4845
				'number',
4846
				null,
4847
				'hidden',
4848
				$this->GetNumber()
4849
			));
4850
		}
4851

    
4852
		$mask = $this->GetMask();
4853

    
4854
		$section->addInput(new Form_Select(
4855
			'mask',
4856
			'Mask',
4857
			$mask['type'],
4858
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4859
		))->setHelp('If "source" or "destination" slots is chosen a dynamic queue with the bandwidth, delay, packet loss ' .
4860
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4861
 					'This makes it possible to easily specify bandwidth limits per ' .
4862
 					'host or subnet, usually capped by the bandwidth of the parent ' .
4863
 					'limiter.');
4864

    
4865
		$group = new Form_Group(null);
4866

    
4867
		$group->add(new Form_Select(
4868
			'maskbits',
4869
			null,
4870
			$mask['bits'],
4871
			array_combine(range(32, 1, -1), range(32, 1, -1))
4872
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4873

    
4874
		$group->add(new Form_Select(
4875
			'maskbitsv6',
4876
			null,
4877
			$mask['bitsv6'],
4878
			array_combine(range(128, 1, -1), range(128, 1, -1))
4879
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4880

    
4881
		$section->add($group);
4882

    
4883
		$section->addInput(new Form_Input(
4884
			'description',
4885
			'Description',
4886
			'text',
4887
			$this->GetDescription()
4888
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4889

    
4890
		$sform->add($section);
4891

    
4892
		/* Begin limiter patch */
4893
		$aqm_map = getAQMs();
4894

    
4895
		$section = new Form_Section('Queue');
4896
		$section->addInput(new Form_Select(
4897
				'aqm',
4898
				'Queue Management Algorithm',
4899
				$this->GetAQM(),
4900
				array_map_assoc(function ($k, $v) {
4901
					return [$k, $v["name"]];
4902
				}, $aqm_map)
4903
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside this limiter\'s queue, ' .
4904
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4905
						'network congestion.');
4906

    
4907
		$section->addInput(new Form_StaticText(
4908
			'',
4909
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4910
		))->setHelp('Specifies the queue management algorithm parameters.');
4911

    
4912
		$section->addInput(new Form_Input(
4913
				'qlimit',
4914
				'Queue length',
4915
				'number',
4916
				$this->GetQlimit()
4917
		))->setHelp('Specifies the length of this queue, which the AQM is responsible for.  This field may be left empty.');
4918

    
4919
		$section->addInput(new Form_Checkbox(
4920
			'ecn',
4921
			'ECN',
4922
			'Enable Explicit Congestion Notification (ECN)',
4923
			($this->GetECN() == "on"),
4924
			'on'
4925
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4926

    
4927
		$sform->add($section);
4928
		/* End limiter patch */
4929

    
4930
		$section = new Form_Section('Advanced Options');
4931

    
4932
		$section->addInput(new Form_Input(
4933
			'weight',
4934
			'Weight',
4935
			'number',
4936
			$this->GetWeight(),
4937
			['min' => '1', 'max' => '100']
4938
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4939
					' it can be left blank otherwise.');
4940

    
4941
		$section->addInput(new Form_Input(
4942
			'plr',
4943
			'Packet Loss Rate',
4944
			'number',
4945
			$this->GetPlr(),
4946
			['step' => '0.001', 'min' => '0.000']
4947
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4948
					'A value of 0.001 means one packet in 1000 gets dropped');
4949

    
4950
		$section->addInput(new Form_Input(
4951
			'buckets',
4952
			'Bucket size (slots)',
4953
			'number',
4954
			$this->GetBuckets()
4955
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4956

    
4957
		$section->addInput(new Form_Input(
4958
			'pipe',
4959
			null,
4960
			'hidden',
4961
			$this->GetPipe()
4962
		));
4963

    
4964
		$sform->add($section);
4965

    
4966
		return($sform);
4967
	}
4968

    
4969
	function update_dn_data(&$data) {
4970
		$this->ReadConfig($data);
4971
	}
4972

    
4973
	function wconfig() {
4974
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4975
		if (!is_array($cflink)) {
4976
			$cflink = array();
4977
		}
4978
		$cflink['name'] = $this->GetQname();
4979
		$cflink['number'] = $this->GetNumber();
4980
		$cflink['qlimit'] = $this->GetQlimit();
4981
		$cflink['description'] = $this->GetDescription();
4982
		$cflink['weight'] = $this->GetWeight();
4983
		$cflink['enabled'] = $this->GetEnabled();
4984
		$cflink['buckets'] = $this->GetBuckets();
4985
		$mask = $this->GetMask();
4986
		$cflink['mask'] = $mask['type'];
4987
		$cflink['maskbits'] = $mask['bits'];
4988
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4989

    
4990
		/* Limiter queue patch */
4991
		$cflink['aqm'] = $this->GetAQM();
4992
		$aqm_map = GetAQMs();
4993
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4994
		foreach ($selectedParameters as $key => $value) {
4995
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4996
			$cflink[$config_key] = $this->GetAQMParameter($key);
4997
		}
4998
		$cflink['ecn'] = $this->GetECN();
4999
		/* End limiter queue patch */
5000
	}
5001
}
5002

    
5003
function get_dummynet_name_list() {
5004

    
5005
	$dn_name_list =& get_unique_dnqueue_list();
5006
	$dn_name = array();
5007
	if (is_array($dn_name_list)) {
5008
		foreach ($dn_name_list as $key => $value) {
5009
			$dn_name[] = $key;
5010
		}
5011
	}
5012

    
5013
	return $dn_name;
5014

    
5015
}
5016

    
5017
function get_altq_name_list() {
5018
	$altq_name_list =& get_unique_queue_list();
5019
	$altq_name = array();
5020
	if (is_array($altq_name_list)) {
5021
		foreach ($altq_name_list as $key => $aqobj) {
5022
			$altq_name[] = $key;
5023
		}
5024
	}
5025

    
5026
	return $altq_name;
5027
}
5028

    
5029
/*
5030
 * XXX: TODO Make a class shaper to hide all these functions
5031
 * from the global namespace.
5032
 */
5033

    
5034
/*
5035
 * This is a layer violation but for now there is no way
5036
 * I can find to properly do this with PHP.
5037
 */
5038
function altq_get_default_queue($interface) {
5039
	global $altq_list_queues;
5040

    
5041
	$altq_tmp = $altq_list_queues[$interface];
5042
	if ($altq_tmp) {
5043
		return $altq_tmp->GetDefaultQueuePresent();
5044
	} else {
5045
		return false;
5046
	}
5047
}
5048

    
5049
function altq_check_default_queues() {
5050
	global $altq_list_queues;
5051

    
5052
	$count = 0;
5053
	if (is_array($altq_list_queues)) {
5054
		foreach ($altq_list_queues as $altq) {
5055
			if ($altq->GetDefaultQueuePresent()) {
5056
				$count++;
5057
			}
5058
		}
5059
	}
5060
	else {
5061
		$count++;
5062
	}
5063

    
5064
	return 0;
5065
}
5066

    
5067
function &get_unique_queue_list() {
5068
	global $altq_list_queues;
5069

    
5070
	$qlist = array();
5071
	if (is_array($altq_list_queues)) {
5072
		foreach ($altq_list_queues as $altq) {
5073
			if ($altq->GetEnabled() == "") {
5074
				continue;
5075
			}
5076
			$tmplist =& $altq->get_queue_list();
5077
			foreach ($tmplist as $qname => $link) {
5078
				if ($link->GetEnabled() <> "") {
5079
					$qlist[$qname] = $link;
5080
				}
5081
			}
5082
		}
5083
	}
5084
	return $qlist;
5085
}
5086

    
5087
function &get_unique_dnqueue_list() {
5088
	global $dummynet_pipe_list;
5089

    
5090
	$qlist = array();
5091
	if (is_array($dummynet_pipe_list)) {
5092
		foreach ($dummynet_pipe_list as $dn) {
5093
			if ($dn->GetEnabled() == "") {
5094
				continue;
5095
			}
5096
			$tmplist =& $dn->get_queue_list();
5097
			foreach ($tmplist as $qname => $link) {
5098
				$qlist[$qname] = $link;
5099
			}
5100
		}
5101
	}
5102
	return $qlist;
5103
}
5104

    
5105
function ref_on_altq_queue_list($parent, $qname) {
5106
	if (isset($GLOBALS['queue_list'][$qname])) {
5107
		$GLOBALS['queue_list'][$qname]++;
5108
	} else {
5109
		$GLOBALS['queue_list'][$qname] = 1;
5110
	}
5111

    
5112
	unref_on_altq_queue_list($parent);
5113
}
5114

    
5115
function unref_on_altq_queue_list($qname) {
5116
	$GLOBALS['queue_list'][$qname]--;
5117
	if ($GLOBALS['queue_list'][$qname] <= 1) {
5118
		unset($GLOBALS['queue_list'][$qname]);
5119
	}
5120
}
5121

    
5122
function read_altq_config() {
5123
	global $altq_list_queues, $config;
5124
	$path = array();
5125

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

    
5129
	$altq_list_queues = array();
5130

    
5131
	if (!is_array($config['shaper']['queue'])) {
5132
		return;
5133
	}
5134

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

    
5158
function read_dummynet_config() {
5159
	global $dummynet_pipe_list, $config;
5160
	$path = array();
5161

    
5162
	init_config_arr(array('dnshaper', 'queue'));
5163
	$a_int = &$config['dnshaper']['queue'];
5164

    
5165
	$dummynet_pipe_list = array();
5166

    
5167
	if (!count($config['dnshaper']['queue'])) {
5168
		return;
5169
	}
5170

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

    
5195
function get_interface_list_to_show() {
5196
	global $altq_list_queues, $config;
5197
	global $shaperIFlist;
5198

    
5199
	$tree = "";
5200
	foreach ($shaperIFlist as $shif => $shDescr) {
5201
		if ($altq_list_queues[$shif]) {
5202
			continue;
5203
		} else {
5204
			if (!is_altq_capable(get_real_interface($shif))) {
5205
				continue;
5206
			}
5207
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
5208
		}
5209
	}
5210

    
5211
	return $tree;
5212
}
5213

    
5214
function filter_generate_altq_queues() {
5215
	global $altq_list_queues;
5216

    
5217
	read_altq_config();
5218

    
5219
	$altq_rules = "";
5220
	foreach ($altq_list_queues as $altq) {
5221
		$altq_rules .= $altq->build_rules();
5222
	}
5223

    
5224
	return $altq_rules;
5225
}
5226

    
5227
function dnqueue_find_nextnumber() {
5228
	global $dummynet_pipe_list;
5229

    
5230
	$dnused = array();
5231
	if (is_array($dummynet_pipe_list)) {
5232
		foreach ($dummynet_pipe_list as $dn) {
5233
			$tmplist =& $dn->get_queue_list();
5234
			foreach ($tmplist as $qname => $link) {
5235
				if ($link[0] == "?") {
5236
					$dnused[$qname] = substr($link, 1);
5237
				}
5238
			}
5239
		}
5240
	}
5241

    
5242
	sort($dnused, SORT_NUMERIC);
5243
	$dnnumber = 0;
5244
	$found = false;
5245
	foreach ($dnused as $dnnum) {
5246
		if (($dnnum - $dnnumber) > 1) {
5247
			$dnnumber = $dnnum - 1;
5248
			$found = true;
5249
			break;
5250
		} else {
5251
			$dnnumber = $dnnum;
5252
		}
5253
	}
5254

    
5255
	if ($found == false) {
5256
		$dnnumber++;
5257
	}
5258

    
5259
	unset($dnused, $dnnum, $found);
5260
	return $dnnumber;
5261
}
5262

    
5263
function dnpipe_find_nextnumber() {
5264
	global $dummynet_pipe_list;
5265

    
5266
	$dnused = array();
5267
	foreach ($dummynet_pipe_list as $dn) {
5268
		$dnused[] = $dn->GetNumber();
5269
	}
5270

    
5271
	sort($dnused, SORT_NUMERIC);
5272
	$dnnumber = 0;
5273
	$found = false;
5274
	foreach ($dnused as $dnnum) {
5275
		if (($dnnum - $dnnumber) > 1) {
5276
			$dnnumber = $dnnum - 1;
5277
			$found = true;
5278
			break;
5279
		} else {
5280
			$dnnumber = $dnnum;
5281
		}
5282
	}
5283

    
5284
	if ($found == false) {
5285
		$dnnumber++;
5286
	}
5287

    
5288
	unset($dnused, $dnnum, $found);
5289
	return $dnnumber;
5290
}
5291

    
5292
function filter_generate_dummynet_rules() {
5293
	global $config, $g, $dummynet_pipe_list;
5294

    
5295
	read_dummynet_config();
5296

    
5297
	$dn_rules = "";
5298
	$max_qlimit = "100"; // OS default
5299
	foreach ($dummynet_pipe_list as $dn) {
5300
		$dn_rules .= $dn->build_rules();
5301
		$this_qlimit = $dn->GetQlimit();
5302
		if ($this_qlimit > $max_qlimit) {
5303
			$max_qlimit = $this_qlimit;
5304
		}
5305
	}
5306
	if (!is_numericint($max_qlimit)) {
5307
		$max_qlimit = "100";
5308
	}
5309
	if (!empty($dn_rules)) {
5310
		if (!is_module_loaded("dummynet.ko")) {
5311
			mute_kernel_msgs();
5312
			mwexec("/sbin/kldload dummynet");
5313
			unmute_kernel_msgs();
5314
		}
5315
		$sysctls = (array(
5316
				"net.inet.ip.dummynet.io_fast" => "1",
5317
				"net.inet.ip.dummynet.hash_size" => "256",
5318
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
5319
		));
5320
		init_config_arr(array('sysctl', 'item'));
5321
		if (!empty($config['sysctl']['item'])) {
5322
			foreach ($config['sysctl']['item'] as $item) {
5323
				if (preg_match('/net\.inet\.ip\.dummynet\./', $item['tunable'])) {
5324
					$sysctls[$item['tunable']] = $item['value'];
5325
				}
5326
			}
5327
		}
5328
		set_sysctl($sysctls);
5329
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
5330
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
5331
	}
5332
}
5333

    
5334
function build_iface_without_this_queue($iface, $qname) {
5335
	global $g, $altq_list_queues;
5336
	global $shaperIFlist;
5337

    
5338
	$altq =& $altq_list_queues[$iface];
5339

    
5340
	if ($altq) {
5341
		$scheduler = $altq->GetScheduler();
5342
	}
5343

    
5344
	$form = '<dl class="dl-horizontal">';
5345

    
5346
	$form .= '	<dt>';
5347
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5348
	$form .= '	</dt>';
5349
	$form .= '	<dd>';
5350
	$form .=		$scheduler;
5351
	$form .= '	</dd>';
5352

    
5353
	$form .= '	<dt>';
5354
	$form .= 'Clone';
5355
	$form .= '	</dt>';
5356
	$form .= '	<dd>';
5357
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
5358
	$form .= $iface . '&amp;queue=';
5359
	$form .= $qname . '&amp;action=add">';
5360
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
5361
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
5362
	$form .= '	</dd>';
5363

    
5364
	$form .= '</dl>';
5365

    
5366
	return $form;
5367

    
5368
}
5369

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

    
5373
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5374
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5375
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5376

    
5377
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5378
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5379
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5380

    
5381
// Check to see if the specified interface has a queue configured
5382
function interface_has_queue($if) {
5383
	global $config;
5384

    
5385
	if ($config['shaper']) {
5386
		foreach ($config['shaper']['queue'] as $queue) {
5387
			if ($queue['interface'] === $if) {
5388
				return true;
5389
			}
5390
		}
5391
	}
5392

    
5393
	return false;
5394
}
5395
?>
(47-47/61)