Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
	return $ptr;
332
}
333

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

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

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

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

    
354
	return $ptr;
355
}
356

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

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

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

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

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

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

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

    
480
	return floatval($objbw);
481
}
482

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

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

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

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

    
532
	return floatval($objbw);
533
}
534

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
753
		return 0;
754
	}
755

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

    
765
		return $sum;
766
	}
767

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

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

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

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

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

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

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

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

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

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

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

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

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

    
914
		return $q;
915
	}
916

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

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

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

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

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

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

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

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

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

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

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

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

    
1074
		return $javascript;
1075
	}
1076

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

    
1080
		$altq =& $this;
1081

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

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

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

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

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

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

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

    
1116
		return $form;
1117

    
1118
	}
1119

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

    
1126
		$sform = new Form();
1127

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

    
1130
		$section = new Form_Section(null);
1131

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

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

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

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

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

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

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

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

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

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

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

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

    
1209
		return($sform);
1210
	}
1211

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

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

    
1244
}
1245

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1483
		return $javascript;
1484
	}
1485

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

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

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

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

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

    
1524
	function &get_queue_list(&$qlist) {
1525

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1705
		return $tree;
1706
	}
1707

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

    
1766
		$pfq_rule .= " \n";
1767

    
1768
		return $pfq_rule;
1769
	}
1770

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

    
1779
	function build_form() {
1780

    
1781
		$sform = new Form();
1782

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1893
		return($sform);
1894
	}
1895

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

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

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

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

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

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

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

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

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

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

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

    
1957
		return $form;
1958

    
1959
	}
1960

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

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

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

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

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

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

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

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

    
2140
		return $q;
2141
	}
2142

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2581
		$pfq_rule .= " \n";
2582

    
2583
		return $pfq_rule;
2584
	}
2585

    
2586
	function build_javascript() {
2587

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

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

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

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

    
2609
		enable_upperlimit();
2610

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

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

    
2622
		enable_realtime();
2623

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

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

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

    
2641
		return $javascript;
2642
	}
2643

    
2644
	function build_form() {
2645

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

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

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

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

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

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

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

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

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

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

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

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

    
2705

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2780
		return($sform);
2781
	}
2782

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

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

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

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

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

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

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

    
2940
		return $q;
2941
	}
2942

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

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

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

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

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

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

    
3033
		$reqdfields[] = "bandwidth";
3034
		$reqdfieldsn[] = gettext("Bandwidth");
3035
		$reqdfields[] = "bandwidthtype";
3036
		$reqdfieldsn[] = gettext("Bandwidthtype");
3037

    
3038
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3039

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

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

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

    
3060
	function build_javascript() {
3061
		return parent::build_javascript();
3062
	}
3063

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

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

    
3163
		$pfq_rule .= " \n";
3164
		return $pfq_rule;
3165
	}
3166

    
3167
	function build_form() {
3168
		$sform = parent::build_form();
3169

    
3170
		$section = new Form_Section('NOTITLE');
3171

    
3172
		$group = new Form_Group('Bandwidth');
3173

    
3174
		$group->add(new Form_Input(
3175
			'bandwidth',
3176
			null,
3177
			'number',
3178
			$this->GetBandwidth()
3179
		));
3180

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

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

    
3194
		$section->add($group);
3195

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

    
3203
		$sform->add($section);
3204

    
3205
		return $sform;
3206
	}
3207

    
3208
	function update_altq_queue_data(&$data) {
3209
		$this->ReadConfig($data);
3210
	}
3211

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

    
3264
class fairq_queue extends priq_queue {
3265
	var $hogs;
3266
	var $buckets;
3267

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

    
3284

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

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

    
3311
	function find_parentqueue($interface, $qname) { return; }
3312

    
3313
	function delete_queue() {
3314
		unref_on_altq_queue_list($this->GetQname());
3315
		cleanup_queue_from_rules($this->GetQname());
3316
		unset_object_by_reference($this->GetLink());
3317
	}
3318

    
3319
	function validate_input($data, &$input_errors) {
3320
		parent::validate_input($data, $input_errors);
3321

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

    
3330
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3331
	}
3332

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

    
3347
	function build_javascript() {
3348
		return parent::build_javascript();
3349
	}
3350

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

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

    
3436
		$pfq_rule .= " \n";
3437
		return $pfq_rule;
3438
	}
3439

    
3440
	function build_form() {
3441
		$form = parent::build_form();
3442

    
3443
		$section = new Form_Section('');
3444

    
3445
		$group = new Form_Group('Bandwidth');
3446

    
3447
		$group->add(new Form_Input(
3448
			'bandwidth',
3449
			null,
3450
			'number',
3451
			$this->GetBandwidth()
3452
		));
3453

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

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

    
3467
		$section->add($group);
3468

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

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

    
3483
		$form->add($section);
3484
		return $form;
3485
	}
3486

    
3487
	function update_altq_queue_data(&$data) {
3488
		$this->ReadConfig($data);
3489
	}
3490

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

    
3547

    
3548
/*
3549
 * dummynet(4) wrappers.
3550
 */
3551

    
3552

    
3553
/*
3554
 * List of respective objects!
3555
 */
3556
$dummynet_pipe_list = array();
3557

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

    
3568
	var $buckets;
3569
	/* mask parameters */
3570
	var $mask;
3571
	var $noerror;
3572

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

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

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

    
3679
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3680

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

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

    
3747
}
3748

    
3749
class dnpipe_class extends dummynet_class {
3750
	var $delay;
3751
	var $qbandwidth = array();
3752
	var $qbandwidthtype;
3753

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

    
3804
		/* This is here to help on form building and building rules/lists */
3805
	var $subqueues = array();
3806

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

    
3838
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3839

    
3840
		if (!is_array($this->subqueues)) {
3841
			$this->subqueues = array();
3842
		}
3843

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

    
3852
		if (!is_array($input_errors)) {
3853
			$input_errors = array();
3854
		}
3855

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

    
3864
		return $q;
3865
	}
3866

    
3867
	function &get_queue_list(&$q = null) {
3868
		$qlist = array();
3869

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

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

    
3894
	function &find_parentqueue($pipe, $qname) {
3895
		return NULL;
3896
	}
3897

    
3898
	function validate_input($data, &$input_errors) {
3899
		parent::validate_input($data, $input_errors);
3900

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

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

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

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

    
3986
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3987
			$this->SetBandwidth($q['bandwidth']['item']);
3988
			$this->SetBurst($q['burst']['item']);
3989
		}
3990

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

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

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

    
4072
		// ecn flag
4073
		$this->SetECN($q['ecn']);
4074
		/* End limiter patch */
4075
	}
4076

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

    
4089
		return $tree;
4090
	}
4091

    
4092
	function build_rules() {
4093
		global $config, $time_based_rules;
4094

    
4095
		if ($this->GetEnabled() == "") {
4096
			return;
4097
		}
4098

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

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

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

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

    
4191
		$pfq_rule .= " \n";
4192

    
4193
		return $pfq_rule;
4194
	}
4195

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

    
4200
	function build_javascript() {
4201
		global $g, $config;
4202

    
4203
		$javasr = parent::build_javascript();
4204

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

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

    
4224
	return (function (tableId) {
4225

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

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

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

    
4240
	});
4241
})();
4242

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

    
4248
function ceil_func(el){
4249
	el.value = Math.ceil(el.value);
4250

    
4251
}
4252
//]]>
4253
</script>
4254

    
4255
EOD;
4256

    
4257
		return $javasr;
4258
	}
4259

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

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

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

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

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

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

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

    
4312
					$form .= ">{$bwscale}</option>";
4313
				}
4314

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

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

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

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

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

    
4342
		return($form);
4343
	}
4344

    
4345
	function build_form() {
4346
		global $g, $config, $pipe, $action, $qname;
4347

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

    
4359

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

    
4363
		$section = new Form_Section('Limiters');
4364

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

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

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

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

    
4396
		$bandwidth = $this->GetBandwidth();
4397

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

    
4405
		$mask = $this->GetMask();
4406

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

    
4416
		$group = new Form_Group(null);
4417

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

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

    
4432
		$section->add($group);
4433

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

    
4441
		$sform->add($section);
4442

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

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

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

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

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

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

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

    
4494
		$sform->add($section);
4495
		/* End limiter patch */
4496

    
4497
		$section = new Form_Section('Advanced Options');
4498

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

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

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

    
4522
		$sform->add($section);
4523

    
4524
		return($sform);
4525
		}
4526

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

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

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

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

    
4574
}
4575

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

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

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

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

    
4633
	function validate_input($data, &$input_errors) {
4634
		parent::validate_input($data, $input_errors);
4635

    
4636

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

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

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

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

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

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

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

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

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

    
4756
		return $tree;
4757
	}
4758

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

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

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

    
4791
		$pfq_rule .= "\n";
4792

    
4793
		return $pfq_rule;
4794
	}
4795

    
4796
	function build_javascript() {
4797
		return parent::build_javascript();
4798
	}
4799

    
4800
	function build_form() {
4801
		global $g, $config, $pipe, $action, $qname;
4802

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

    
4814

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

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

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

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

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

    
4850
		$mask = $this->GetMask();
4851

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

    
4863
		$group = new Form_Group(null);
4864

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

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

    
4879
		$section->add($group);
4880

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

    
4888
		$sform->add($section);
4889

    
4890
		/* Begin limiter patch */
4891
		$aqm_map = getAQMs();
4892

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

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

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

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

    
4925
		$sform->add($section);
4926
		/* End limiter patch */
4927

    
4928
		$section = new Form_Section('Advanced Options');
4929

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

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

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

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

    
4962
		$sform->add($section);
4963

    
4964
		return($sform);
4965
	}
4966

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

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

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

    
5001
function get_dummynet_name_list() {
5002

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

    
5011
	return $dn_name;
5012

    
5013
}
5014

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

    
5024
	return $altq_name;
5025
}
5026

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

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

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

    
5047
function altq_check_default_queues() {
5048
	global $altq_list_queues;
5049

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

    
5062
	return 0;
5063
}
5064

    
5065
function &get_unique_queue_list() {
5066
	global $altq_list_queues;
5067

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

    
5085
function &get_unique_dnqueue_list() {
5086
	global $dummynet_pipe_list;
5087

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

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

    
5110
	unref_on_altq_queue_list($parent);
5111
}
5112

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

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

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

    
5127
	$altq_list_queues = array();
5128

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

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

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

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

    
5163
	$dummynet_pipe_list = array();
5164

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

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

    
5193
function get_interface_list_to_show() {
5194
	global $altq_list_queues, $config;
5195
	global $shaperIFlist;
5196

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

    
5209
	return $tree;
5210
}
5211

    
5212
function filter_generate_altq_queues() {
5213
	global $altq_list_queues;
5214

    
5215
	read_altq_config();
5216

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

    
5222
	return $altq_rules;
5223
}
5224

    
5225
function dnqueue_find_nextnumber() {
5226
	global $dummynet_pipe_list;
5227

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

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

    
5253
	if ($found == false) {
5254
		$dnnumber++;
5255
	}
5256

    
5257
	unset($dnused, $dnnum, $found);
5258
	return $dnnumber;
5259
}
5260

    
5261
function dnpipe_find_nextnumber() {
5262
	global $dummynet_pipe_list;
5263

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

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

    
5282
	if ($found == false) {
5283
		$dnnumber++;
5284
	}
5285

    
5286
	unset($dnused, $dnnum, $found);
5287
	return $dnnumber;
5288
}
5289

    
5290
function filter_generate_dummynet_rules() {
5291
	global $config, $g, $dummynet_pipe_list;
5292

    
5293
	read_dummynet_config();
5294

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

    
5330
function build_iface_without_this_queue($iface, $qname) {
5331
	global $g, $altq_list_queues;
5332
	global $shaperIFlist;
5333

    
5334
	$altq =& $altq_list_queues[$iface];
5335

    
5336
	if ($altq) {
5337
		$scheduler = $altq->GetScheduler();
5338
	}
5339

    
5340
	$form = '<dl class="dl-horizontal">';
5341

    
5342
	$form .= '	<dt>';
5343
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5344
	$form .= '	</dt>';
5345
	$form .= '	<dd>';
5346
	$form .=		$scheduler;
5347
	$form .= '	</dd>';
5348

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

    
5360
	$form .= '</dl>';
5361

    
5362
	return $form;
5363

    
5364
}
5365

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

    
5369
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5370
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5371
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5372

    
5373
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5374
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5375
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5376

    
5377
// Check to see if the specified interface has a queue configured
5378
function interface_has_queue($if) {
5379
	global $config;
5380

    
5381
	if ($config['shaper']) {
5382
		foreach ($config['shaper']['queue'] as $queue) {
5383
			if ($queue['interface'] === $if) {
5384
				return true;
5385
			}
5386
		}
5387
	}
5388

    
5389
	return false;
5390
}
5391
?>
(47-47/61)