Project

General

Profile

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

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

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

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

    
175
	$selectedAlgorithm = $array[$selected];
176

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

    
181
	$parameters = $selectedAlgorithm["parameters"];
182

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

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

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

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

    
212
			$form .= '</tr>';
213
		}
214

    
215
		$form .= "</tbody></table></div><br />";
216
	}
217

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

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

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

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

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

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

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

    
328
	return $ptr;
329
}
330

    
331
function unset_object_by_reference(&$mypath) {
332
	global $config;
333

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

    
342
function &get_dn_reference_to_me_in_config(&$mypath) {
343
	global $config;
344

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

    
351
	return $ptr;
352
}
353

    
354
function unset_dn_object_by_reference(&$mypath) {
355
	global $config;
356

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

    
365
function clean_child_queues($type, $mypath) {
366
	$ref = &get_reference_to_me_in_config($mypath);
367

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

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

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

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

    
477
	return floatval($objbw);
478
}
479

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

    
507
function get_queue_bandwidth($obj) {
508
	$bw = $obj->GetBandwidth();
509
	$scale = $obj->GetBwscale();
510

    
511
	$pattern= "/(b|Kb|Mb|Gb|%)/";
512
	if (!preg_match($pattern, $scale, $match))
513
		return 0;
514

    
515
	switch ($match[1]) {
516
		case '%':
517
			$objbw = ($bw / 100) * get_queue_bandwidth($obj->GetParent());
518
			break;
519
		default:
520
			$objbw = $bw * get_bandwidthtype_scale($scale);
521
			break;
522
	}
523

    
524
	return floatval($objbw);
525
}
526

    
527
function get_interface_bandwidth($object) {
528
	global $altq_list_queues;
529

    
530
	$int = $object->GetInterface();
531
	if (isset($altq_list_queues[$int])) {
532
		$altq = &$altq_list_queues[$int];
533
		$bw_3 = $altq->GetBandwidth();
534
		$bw_3 = $bw_3 * get_bandwidthtype_scale($altq->GetBwscale());
535
		return floatval($bw_3);
536
	} else {
537
		return 0;
538
	}
539
}
540

    
541
/*
542
 * This is duplicated here since we cannot include guiconfig.inc.
543
 * Including it makes all stuff break.
544
 */
545
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
546

    
547
	/* check for bad control characters */
548
	foreach ($postdata as $pn => $pd) {
549
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
550
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
551
		}
552
	}
553

    
554
	for ($i = 0; $i < count($reqdfields); $i++) {
555
		if ($postdata[$reqdfields[$i]] == "") {
556
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
557
		}
558
	}
559
}
560

    
561
function cleanup_queue_from_rules($queue) {
562
	global $config;
563

    
564
	foreach ($config['filter']['rule'] as $rule) {
565
		if ($rule['defaultqueue'] == $queue) {
566
			unset($rule['defaultqueue']);
567
		}
568
		if ($rule['ackqueue'] == $queue) {
569
			unset($rule['ackqueue']);
570
		}
571
	}
572
}
573

    
574
function cleanup_dnqueue_from_rules($queue) {
575
	global $config;
576

    
577
	foreach ($config['filter']['rule'] as $rule) {
578
		if ($rule['dnpipe'] == $queue) {
579
			unset($rule['dnpipe']);
580
		}
581
		if ($rule['pdnpipe'] == $queue) {
582
			unset($rule['pdnpipe']);
583
		}
584
	}
585
}
586

    
587
class altq_root_queue {
588
	var $interface;
589
	var $tbrconfig ;
590
	var $bandwidth;
591
	var $bandwidthtype; /* b, Kb, Mb, Gb, % */
592
	var $scheduler;
593
	var $qlimit;
594
	var $queues = array();
595
	var $qenabled = false;
596
	var $link;
597

    
598
	/* Accessor functions */
599
	function GetDefaultQueuePresent() {
600
		if (!empty($this->queues)) {
601
			foreach ($this->queues as $q) {
602
				if ($q->GetDefault()) {
603
					return true;
604
				}
605
			}
606
		}
607

    
608
		return false;
609
	}
610
	function SetLink($link) {
611
		$this->link = $link;
612
	}
613
	function GetLink() {
614
		return $this->link;
615
	}
616
	function GetEnabled() {
617
		return $this->qenabled;
618
	}
619
	function SetEnabled($value) {
620
		$this->qenabled = $value;
621
	}
622
	function CanHaveChildren() {
623
		if ($this->GetScheduler() == "CODELQ") {
624
			return false;
625
		} else {
626
			return true;
627
		}
628
	}
629
	function CanBeDeleted() {
630
		return false;
631
	}
632
	function GetQname() {
633
		return $this->interface;
634
	}
635
	function SetQname($name) {
636
		$this->interface = trim($name);
637
	}
638
	function GetInterface() {
639
		return $this->interface;
640
	}
641
	function SetInterface($name) {
642
		$this->interface = trim($name);
643
	}
644
	function GetTbrConfig() {
645
		return $this->tbrconfig;
646
	}
647
	function SetTbrConfig($tbrconfig) {
648
		$this->tbrconfig = $tbrconfig;
649
	}
650
	function GetBandwidth() {
651
		return $this->bandwidth;
652
	}
653
	function SetBandwidth($bw) {
654
		$this->bandwidth = $bw;
655
	}
656
	function GetBwscale() {
657
		return $this->bandwidthtype;
658
	}
659
	function SetBwscale($bwscale) {
660
		$this->bandwidthtype = $bwscale;
661
	}
662
	function GetScheduler() {
663
		return $this->scheduler;
664
	}
665
	function SetScheduler($scheduler) {
666
		$this->scheduler = trim($scheduler);
667
	}
668
	function GetQlimit() {
669
		return $this->qlimit;
670
	}
671
	function SetQlimit($limit) {
672
		$this->qlimit = $limit;
673
	}
674

    
675
	function GetBwscaleText() {
676
		switch ($this->bandwidthtype) {
677
			case "b":
678
				$bwscaletext = "Bit/s";
679
				break;
680
			case "Kb":
681
				$bwscaletext = "Kbit/s";
682
				break;
683
			case "Mb":
684
				$bwscaletext = "Mbit/s";
685
				break;
686
			case "Gb":
687
				$bwscaletext = "Gbit/s";
688
				break;
689
			default:
690
				/* For others that do not need translating like % */
691
				$bwscaletext = $this->bandwidthtype;
692
				break;
693
		}
694
		return $bwscaletext;
695
	}
696

    
697
	function CheckBandwidth($bw, $bwtype) {
698
		$sum = $this->GetTotalBw();
699
		if ($sum > $bw * get_bandwidthtype_scale($bwtype))
700
			return 1;
701
		foreach ($this->queues as $q) {
702
			if ($q->CheckBandwidth(0, ''))
703
				return 1;
704
		}
705

    
706
		return 0;
707
	}
708

    
709
	function GetTotalBw($qignore = NULL) {
710
		$sum = 0;
711
		foreach ($this->queues as $q) {
712
			if ($qignore != NULL && $qignore == $q)
713
				continue;
714
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $this);
715
		}
716

    
717
		return $sum;
718
	}
719

    
720
	function validate_input($data, &$input_errors) {
721

    
722
		$reqdfields[] = "bandwidth";
723
		$reqdfieldsn[] = gettext("Bandwidth");
724
		$reqdfields[] = "bandwidthtype";
725
		$reqdfieldsn[] = gettext("Bandwidthtype");
726

    
727
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
728

    
729
		if (!isset($data['bandwidth']) || strlen($data['bandwidth']) == 0) {
730
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
731
		}
732
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
733
			$input_errors[] = gettext("Bandwidth must be an integer.");
734
		}
735
		if ($data['bandwidth'] < 0) {
736
			$input_errors[] = gettext("Bandwidth cannot be negative.");
737
		}
738
		if ($data['bandwidthtype'] == "%") {
739
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
740
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
741
			}
742
		}
743
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
744
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
745

    
746
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
747
			$input_errors[] = gettext("Qlimit must be an integer.");
748
		}
749
		if ($data['qlimit'] < 0) {
750
			$input_errors[] = gettext("Qlimit must be positive.");
751
		}
752
		if ($data['tbrconfig'] && (!is_numeric($data['tbrconfig']))) {
753
			$input_errors[] = gettext("Tbrsize must be an integer.");
754
		}
755
		if ($data['tbrconfig'] < 0) {
756
			$input_errors[] = gettext("Tbrsize must be positive.");
757
		}
758
	}
759

    
760
	/* Implement this to shorten some code on the frontend page */
761
	function ReadConfig(&$conf) {
762
		if (isset($conf['tbrconfig'])) {
763
			$this->SetTbrConfig($conf['tbrconfig']);
764
		} else {
765
			$this->SetTbrConfig($conf['tbrconfig']);
766
		}
767
		$this->SetBandwidth($conf['bandwidth']);
768
		if ($conf['bandwidthtype'] <> "") {
769
			$this->SetBwscale($conf['bandwidthtype']);
770
		}
771
		if (isset($conf['scheduler'])) {
772
			if ($this->GetScheduler() != $conf['scheduler']) {
773
				foreach ($this->queues as $q) {
774
					clean_child_queues($conf['scheduler'], $this->GetLink());
775
					$q->clean_queue($conf['scheduler']);
776
				}
777
			}
778
			$this->SetScheduler($conf['scheduler']);
779
		}
780
		if (isset($conf['qlimit']) && $conf['qlimit'] <> "") {
781
			$this->SetQlimit($conf['qlimit']);
782
		} else {
783
			$this->SetQlimit("");
784
		}
785
		if (isset($conf['name'])) {
786
			$this->SetQname($conf['name']);
787
		}
788
		if (!empty($conf['enabled'])) {
789
			$this->SetEnabled($conf['enabled']);
790
		} else {
791
			$this->SetEnabled("");
792
		}
793
	}
794

    
795
	function copy_queue($interface, &$cflink) {
796
		$cflink['interface'] = $interface;
797
		$cflink['name'] = $interface;
798
		$cflink['scheduler'] = $this->GetScheduler();
799
		$cflink['bandwidth'] = $this->GetBandwidth();
800
		$cflink['bandwidthtype'] = $this->GetBwscale();
801
		$cflink['qlimit'] = $this->GetQlimit();
802
		$cflink['tbrconfig'] = $this->GetTbrConfig();
803
		$cflink['enabled'] = $this->GetEnabled();
804
		if (is_array($this->queues)) {
805
			$cflink['queue'] = array();
806
			foreach ($this->queues as $q) {
807
				$cflink['queue'][$q->GetQname()] = array();
808
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
809
			}
810
		}
811
	}
812

    
813
	function &get_queue_list(&$q = null) {
814
		$qlist = array();
815

    
816
		//$qlist[$this->GetQname()] = & $this;
817
		if (is_array($this->queues)) {
818
			foreach ($this->queues as $queue) {
819
				$queue->get_queue_list($qlist);
820
			}
821
		}
822
		return $qlist;
823
	}
824

    
825
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
826

    
827
		if (!is_array($this->queues)) {
828
			$this->queues = array();
829
		}
830

    
831
		switch ($this->GetScheduler()) {
832
			case "PRIQ":
833
				$__tmp_q = new priq_queue(); $q =& $__tmp_q;
834
				break;
835
			case "HFSC":
836
				$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
837
				break;
838
			case "CBQ":
839
				$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
840
				break;
841
			case "FAIRQ":
842
				$__tmp_q = new fairq_queue(); $q =& $__tmp_q;
843
				break;
844
			default:
845
				/* XXX: but should not happen anyway */
846
				return;
847
				break;
848
		}
849
		$q->SetLink($path);
850
		$q->SetInterface($this->GetInterface());
851
		$q->SetEnabled("on");
852
		$q->SetParent($this);
853
		$q->ReadConfig($queue);
854
		$q->validate_input($queue, $input_errors);
855

    
856
		$this->queues[$q->GetQname()] = &$q;
857
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
858
		if (is_array($queue['queue'])) {
859
			foreach ($queue['queue'] as $key1 => $que) {
860
				array_push($path, $key1);
861
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
862
				array_pop($path);
863
			}
864
		}
865

    
866
		return $q;
867
	}
868

    
869
	/* interface here might be optional */
870
	function &find_queue($interface, $qname) {
871
		if ($qname == $this->GetQname()) {
872
			return $this;
873
		}
874
		foreach ($this->queues as $q) {
875
			$result =& $q->find_queue("", $qname);
876
			if ($result) {
877
				return $result;
878
			}
879
		}
880
	}
881

    
882
	function &find_parentqueue($interface, $qname) {
883
		if ($qname == $interface) {
884
			$result = NULL;
885
		} else if ($this->queues[$qname]) {
886
			$result = $this;
887
		} else if ($this->GetScheduler() <> "PRIQ") {
888
			foreach ($this->queues as $q) {
889
				$result = $q->find_parentqueue("", $qname);
890
				if ($result) {
891
					return $result;
892
				}
893
			}
894
		}
895
	}
896

    
897
	function build_tree() {
898
		global $shaperIFlist;
899

    
900
		$tree = " <li><a href=\"firewall_shaper.php?interface=".$this->GetInterface()."&amp;queue=". $this->GetInterface()."&amp;action=show";
901
		$tree .= "\">" . $shaperIFlist[$this->GetInterface()] . "</a>";
902
		if (is_array($this->queues)) {
903
			$tree .= "<ul>";
904
			foreach ($this->queues as $q) {
905
				$tree .= $q->build_tree();
906
			}
907
			$tree .= "</ul>";
908
		}
909
		$tree .= "</li>";
910
		return $tree;
911
	}
912

    
913
	function delete_queue() {
914
		foreach ($this->queues as $q)
915
			$q->delete_queue();
916
		unset_object_by_reference($this->GetLink());
917
	}
918

    
919
	function delete_all() {
920
		if (count($this->queues)) {
921
			foreach ($this->queues as $q) {
922
				$q->delete_all();
923
				unset_object_by_reference($q->GetLink());
924
				unset($q);
925
			}
926
			unset($this->queues);
927
		}
928
	}
929

    
930
	/*
931
	 * First it spits:
932
	 * altq on $interface ..............
933
	 *	then it goes like
934
	 *	foreach ($queues as $qkey => $queue) {
935
	 *		this->queues[$qkey]->build_rule();
936
	 *	}
937
	 */
938
	function build_rules(&$default = false) {
939
		if (count($this->queues) > 0 && $this->GetEnabled() == "on") {
940
			$default = false;
941
			$rules = " altq on " . get_real_interface($this->GetInterface());
942
			if ($this->GetScheduler()) {
943
				$rules .= " ".strtolower($this->GetScheduler());
944
			}
945
			if ($this->GetQlimit() > 0) {
946
				$rules .= " qlimit " . $this->GetQlimit() . " ";
947
			}
948
			if ($this->GetBandwidth()) {
949
				$rules .= " bandwidth ".trim($this->GetBandwidth());
950
				if ($this->GetBwscale()) {
951
					$rules .= $this->GetBwscale();
952
				}
953
			}
954
			if ($this->GetTbrConfig()) {
955
				$rules .= " tbrsize ".$this->GetTbrConfig();
956
			}
957
			if (count($this->queues)) {
958
				$i = count($this->queues);
959
				$rules .= " queue { ";
960
				foreach ($this->queues as $qkey => $qnone) {
961
					if ($i > 1) {
962
						$i--;
963
						$rules .= " {$qkey}, ";
964
					} else {
965
						$rules .= " {$qkey} ";
966
					}
967
				}
968
				$rules .= " } \n";
969
				foreach ($this->queues as $q) {
970
					$rules .= $q->build_rules($default);
971
				}
972
			}
973

    
974
			if ($default == false) {
975
				$error = sprintf(gettext("SHAPER: no default queue specified for interface %s."), $this->GetInterface()) . " " . gettext("The interface queue will be enforced as default.");
976
				file_notice("Shaper", $error, "Error occurred", "");
977
				unset($error);
978
				return "\n";
979
			}
980
			$frule .= $rules;
981
		} else if ($this->GetEnabled() == "on" && $this->GetScheduler() == "CODELQ") {
982
			$rules = " altq on " . get_real_interface($this->GetInterface());
983
			if ($this->GetScheduler()) {
984
				$rules .= " ".strtolower($this->GetScheduler());
985
			}
986
			if ($this->GetQlimit() > 0) {
987
				$rules .= " ( qlimit " . $this->GetQlimit() . " ) ";
988
			}
989
			if ($this->GetBandwidth()) {
990
				$rules .= " bandwidth ".trim($this->GetBandwidth());
991
				if ($this->GetBwscale()) {
992
					$rules .= $this->GetBwscale();
993
				}
994
			}
995
			if ($this->GetTbrConfig()) {
996
				$rules .= " tbrsize ".$this->GetTbrConfig();
997
			}
998

    
999
			$rules .= " queue";
1000
		}
1001

    
1002
		$rules .= " \n";
1003
		return $rules;
1004
	}
1005

    
1006
	function build_javascript() {
1007
		$javascript = "<script type=\"text/javascript\">";
1008
		$javascript .= "//<![CDATA[\n";
1009
		$javascript .= "function mySuspend() {";
1010
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1011
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden'; ";
1012
		$javascript .= "else if (document.all)";
1013
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';";
1014
		$javascript .= "}";
1015

    
1016
		$javascript .= "function myResume() {";
1017
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null) ";
1018
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';";
1019
		$javascript .= "else if (document.all) ";
1020
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';";
1021
		$javascript .= "}";
1022
		$javascript .= "//]]>";
1023
		$javascript .= "</script>";
1024

    
1025
		return $javascript;
1026
	}
1027

    
1028
	function build_shortform() {
1029
		global $g;
1030

    
1031
		$altq =& $this;
1032

    
1033
		if ($altq) {
1034
			$scheduler = ": " . $altq->GetScheduler();
1035
		}
1036

    
1037
		$form = '<dl class="dl-horizontal">';
1038
		$form .= '	<dt>';
1039
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1040
		$form .= '	</dt>';
1041
		$form .= '	<dd>';
1042
		$form .=		$scheduler;
1043
		$form .= '	</dd>';
1044

    
1045
		$form .= '	<dt>';
1046
		$form .=		'Bandwidth';
1047
		$form .= '	</dt>';
1048
		$form .= '	<dd>';
1049
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1050
		$form .= '	</dd>';
1051

    
1052
		$form .= '	<dt>';
1053
		$form .= 'Disable';
1054
		$form .= '	<dt>';
1055
		$form .= '	<dd>';
1056

    
1057
		$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1058
		$form .= $this->GetInterface() . '&amp;queue=';
1059
		$form .= $this->GetQname() . '&amp;action=delete">';
1060
		$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1061
		$form .= gettext("Remove shaper from this interface") . '</a>';
1062

    
1063
		$form .= '	</dd>';
1064

    
1065
		$form .= '</dl>';
1066

    
1067
		return $form;
1068

    
1069
	}
1070

    
1071
	/*
1072
	 * For requesting the parameters of the root queues
1073
	 * to the user like the traffic wizard does.
1074
	 */
1075
	function build_form() {
1076

    
1077
		$sform = new Form();
1078

    
1079
		$sform->setAction("firewall_shaper.php");
1080

    
1081
		$section = new Form_Section(null);
1082

    
1083
		$section->addInput(new Form_Checkbox(
1084
			'enabled',
1085
			'Enable/Disable',
1086
			'Enable/disable discipline and its children',
1087
			($this->GetEnabled() == "on"),
1088
			'on'
1089
		));
1090

    
1091
		$section->addInput(new Form_StaticText(
1092
			'*Name',
1093
			$this->GetQname()
1094
		));
1095

    
1096
		$section->addInput(new Form_Select(
1097
			'scheduler',
1098
			'Scheduler Type',
1099
			$this->GetScheduler(),
1100
			array('HFSC' => 'HFSC',
1101
				  'CBQ' => 'CBQ',
1102
				  'FAIRQ' => 'FAIRQ',
1103
				  'CODELQ' => 'CODELQ',
1104
				  'PRIQ' => 'PRIQ')
1105
		))->setHelp('Changing this changes all child queues! Beware information can be lost.');
1106

    
1107
		$group = new Form_group('Bandwidth');
1108

    
1109
		$group->add(new Form_Input(
1110
			'bandwidth',
1111
			null,
1112
			'number',
1113
			$this->GetBandwidth()
1114
		));
1115

    
1116
		$group->add(new Form_Select(
1117
			'bandwidthtype',
1118
			null,
1119
			$this->GetBwscale(),
1120
			array('Kb' => 'Kbit/s',
1121
				  'Mb' => 'Mbit/s',
1122
				  'Gb' => 'Gbit/s',
1123
				  'b' => 'Bit/s',
1124
				  '%' => '%')
1125
		));
1126

    
1127
		$section->add($group);
1128

    
1129
		$section->addInput(new Form_Input(
1130
			'qlimit',
1131
			'Queue Limit',
1132
			'number',
1133
			$this->GetQlimit()
1134
		));
1135

    
1136
		$section->addInput(new Form_Input(
1137
			'tbrconfig',
1138
			'TBR Size',
1139
			'number',
1140
			$this->GetTbrConfig()
1141
		))->setHelp('Adjusts the size, in bytes, of the token bucket regulator. If not specified, heuristics based on the interface ' .
1142
					'bandwidth are used to determine the size.');
1143

    
1144
		$section->addInput(new Form_Input(
1145
			'interface',
1146
			null,
1147
			'hidden',
1148
			$this->GetInterface()
1149
		));
1150

    
1151
		$section->addInput(new Form_Input(
1152
			'name',
1153
			null,
1154
			'hidden',
1155
			$this->GetQname()
1156
		));
1157

    
1158
		$sform->add($section);
1159

    
1160
		return($sform);
1161
	}
1162

    
1163
	function update_altq_queue_data(&$data) {
1164
		$this->ReadConfig($data);
1165
	}
1166

    
1167
	/*
1168
	 * Should call on each of it queues and subqueues
1169
	 * the same function much like build_rules();
1170
	 */
1171
	function wconfig() {
1172
		$cflink = &get_reference_to_me_in_config($this->GetLink());
1173
		if (!is_array($cflink)) {
1174
			$cflink = array();
1175
		}
1176
		$cflink['interface'] = $this->GetInterface();
1177
		$cflink['name'] = $this->GetQname();
1178
		$cflink['scheduler'] = $this->GetScheduler();
1179
		$cflink['bandwidth'] = $this->GetBandwidth();
1180
		$cflink['bandwidthtype'] = $this->GetBwscale();
1181
		$cflink['qlimit'] = trim($this->GetQlimit());
1182
		if (empty($cflink['qlimit'])) {
1183
			unset($cflink['qlimit']);
1184
		}
1185
		$cflink['tbrconfig'] = trim($this->GetTbrConfig());
1186
		if (empty($cflink['tbrconfig'])) {
1187
			unset($cflink['tbrconfig']);
1188
		}
1189
		$cflink['enabled'] = $this->GetEnabled();
1190
		if (empty($cflink['enabled'])) {
1191
			unset($cflink['enabled']);
1192
		}
1193
	}
1194

    
1195
}
1196

    
1197
class priq_queue {
1198
	var $qname;
1199
	var $qinterface;
1200
	var $qlimit;
1201
	var $qpriority;
1202
	var $description;
1203
	var $isparent;
1204
	var $qbandwidth;
1205
	var $qbandwidthtype;
1206
	var $qdefault = "";
1207
	var $qrio = "";
1208
	var $qred = "";
1209
	var $qcodel = "";
1210
	var $qecn = "";
1211
	var $qack;
1212
	var $qenabled = "";
1213
	var $qparent;
1214
	var $link;
1215

    
1216
	/* This is here to help with form building and building rules/lists */
1217
	var $subqueues = array();
1218

    
1219
	/* Accessor functions */
1220
	function SetLink($link) {
1221
		$this->link = $link;
1222
	}
1223
	function GetLink() {
1224
		return $this->link;
1225
	}
1226
	function &GetParent() {
1227
		return $this->qparent;
1228
	}
1229
	function SetParent(&$parent) {
1230
		$this->qparent = &$parent;
1231
	}
1232
	function GetEnabled() {
1233
		return $this->qenabled;
1234
	}
1235
	function SetEnabled($value) {
1236
		$this->qenabled = $value;
1237
	}
1238
	function CanHaveChildren() {
1239
		return false;
1240
	}
1241
	function CanBeDeleted() {
1242
		return true;
1243
	}
1244
	function GetQname() {
1245
		return $this->qname;
1246
	}
1247
	function SetQname($name) {
1248
		$this->qname = trim($name);
1249
	}
1250
	function GetBandwidth() {
1251
		return $this->qbandwidth;
1252
	}
1253
	function SetBandwidth($bandwidth) {
1254
		$this->qbandwidth = $bandwidth;
1255
	}
1256
	function GetInterface() {
1257
		return $this->qinterface;
1258
	}
1259
	function SetInterface($name) {
1260
		$this->qinterface = trim($name);
1261
	}
1262
	function GetQlimit() {
1263
		return $this->qlimit;
1264
	}
1265
	function SetQlimit($limit) {
1266
		$this->qlimit = $limit;
1267
	}
1268
	function GetQpriority() {
1269
		return $this->qpriority;
1270
	}
1271
	function SetQpriority($priority) {
1272
		$this->qpriority = $priority;
1273
	}
1274
	function GetDescription() {
1275
		return $this->description;
1276
	}
1277
	function SetDescription($str) {
1278
		$this->description = trim($str);
1279
	}
1280
	function GetFirstime() {
1281
		return $this->firsttime;
1282
	}
1283
	function SetFirsttime($number) {
1284
		$this->firsttime = $number;
1285
	}
1286
	function CheckBandwidth($bw, $bwtype) {
1287
		$parent = $this->GetParent();
1288

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

    
1294
		if ($bwtype != "%") {
1295
			$sum = $parent->GetTotalBw(($bw > 0) ? $this : NULL);
1296

    
1297
			if ($bw > 0) {
1298
				$sum += get_bandwidth($bw, $bwtype, $parent);
1299
			}
1300

    
1301
			if ($sum > get_queue_bandwidth($parent)) {
1302
				return 1;
1303
			}
1304
		}
1305

    
1306
		foreach ($this->subqueues as $q) {
1307
			if ($q->CheckBandwidth(0, '')) {
1308
				return 1;
1309
			}
1310
		}
1311

    
1312
		return 0;
1313
	}
1314
	function GetTotalBw($qignore = NULL) {
1315
		$sum = 0;
1316
		foreach ($this->subqueues as $q) {
1317
			if ($qignore != NULL && $qignore == $q)
1318
				continue;
1319
			$sum += get_bandwidth($q->GetBandwidth(), $q->GetBwscale(), $q->GetParent());
1320
		}
1321

    
1322
		return $sum;
1323
	}
1324
	function GetBwscale() {
1325
		return $this->qbandwidthtype;
1326
	}
1327
	function SetBwscale($scale) {
1328
		$this->qbandwidthtype = $scale;
1329
	}
1330
	function GetDefaultQueuePresent() {
1331
		if ($this->GetDefault()) {
1332
			return true;
1333
		}
1334
		if (!empty($this->subqueues)) {
1335
			foreach ($this->subqueues as $q) {
1336
				if ($q->GetDefault()) {
1337
					return true;
1338
				}
1339
			}
1340
		}
1341

    
1342
		return false;
1343
	}
1344
	function GetDefault() {
1345
		return $this->qdefault;
1346
	}
1347
	function SetDefault($value = false) {
1348
		$this->qdefault = $value;
1349
	}
1350
	function GetCodel() {
1351
		return $this->codel;
1352
	}
1353
	function SetCodel($codel = false) {
1354
		$this->codel = $codel;
1355
	}
1356
	function GetRed() {
1357
		return $this->qred;
1358
	}
1359
	function SetRed($red = false) {
1360
		$this->qred = $red;
1361
	}
1362
	function GetRio() {
1363
		return $this->qrio;
1364
	}
1365
	function SetRio($rio = false) {
1366
		$this->qrio = $rio;
1367
	}
1368
	function GetEcn() {
1369
		return $this->qecn;
1370
	}
1371
	function SetEcn($ecn = false) {
1372
		$this->qecn = $ecn;
1373
	}
1374
	function GetAck() {
1375
		return $this->qack;
1376
	}
1377
	function SetAck($ack = false) {
1378
		$this->qack = $ack;
1379
	}
1380

    
1381
	function GetBwscaleText() {
1382
		switch ($this->qbandwidthtype) {
1383
			case "b":
1384
				$bwscaletext = "Bit/s";
1385
				break;
1386
			case "Kb":
1387
				$bwscaletext = "Kbit/s";
1388
				break;
1389
			case "Mb":
1390
				$bwscaletext = "Mbit/s";
1391
				break;
1392
			case "Gb":
1393
				$bwscaletext = "Gbit/s";
1394
				break;
1395
			default:
1396
				/* For others that do not need translating like % */
1397
				$bwscaletext = $this->qbandwidthtype;
1398
				break;
1399
		}
1400
		return $bwscaletext;
1401
	}
1402

    
1403
	function build_javascript() {
1404
		$javascript = "<script type=\"text/javascript\">";
1405
		$javascript .= "//<![CDATA[\n";
1406
		$javascript .= "function mySuspend() { \n";
1407
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1408
		$javascript .= "document.layers['shaperarea'].visibility = 'hidden';\n";
1409
		$javascript .= "else if (document.all)\n";
1410
		$javascript .= "document.all['shaperarea'].style.visibility = 'hidden';\n";
1411
		$javascript .= "}\n";
1412

    
1413
		$javascript .= "function myResume() {\n";
1414
		$javascript .= "if (document.layers && document.layers['shaperarea'] != null)\n";
1415
		$javascript .= "document.layers['shaperarea'].visibility = 'visible';\n";
1416
		$javascript .= "else if (document.all)\n";
1417
		$javascript .= "document.all['shaperarea'].style.visibility = 'visible';\n";
1418
		$javascript .= "}\n";
1419
		$javascript .= "//]]>";
1420
		$javascript .= "</script>";
1421

    
1422
		return $javascript;
1423
	}
1424

    
1425
	function &add_queue($interface, &$qname, &$path, &$input_errors) { return; }
1426

    
1427
	/*
1428
	 * Currently this will not be called unless we decide to clone a whole
1429
	 * queue tree on the 'By Queues' view or support drag&drop on the tree/list
1430
	 */
1431
	function copy_queue($interface, &$cflink) {
1432

    
1433
		$cflink['name'] = $this->GetQname();
1434
		$cflink['interface'] = $interface;
1435
		$cflink['qlimit'] = $this->GetQlimit();
1436
		$cflink['priority'] = $this->GetQpriority();
1437
		$cflink['description'] = $this->GetDescription();
1438
		$cflink['enabled'] = $this->GetEnabled();
1439
		$cflink['default'] = $this->GetDefault();
1440
		$cflink['red'] = $this->GetRed();
1441
		$cflink['codel'] = $this->GetCodel();
1442
		$cflink['rio'] = $this->GetRio();
1443
		$cflink['ecn'] = $this->GetEcn();
1444

    
1445
		if (is_array($this->subqueues)) {
1446
			$cflinkp['queue'] = array();
1447
			foreach ($this->subqueues as $q) {
1448
				$cflink['queue'][$q->GetQname()] = array();
1449
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
1450
			}
1451
		}
1452
	}
1453

    
1454
	function clean_queue($sched) {
1455
		clean_child_queues($sched, $this->GetLink());
1456
		if (is_array($this->subqueues)) {
1457
			foreach ($this->subqueues as $q) {
1458
				$q->clean_queue($sched);
1459
			}
1460
		}
1461
	}
1462

    
1463
	function &get_queue_list(&$qlist) {
1464

    
1465
		$qlist[$this->GetQname()] = & $this;
1466
		if (is_array($this->subqueues)) {
1467
			foreach ($this->subqueues as $queue) {
1468
				$queue->get_queue_list($qlist);
1469
			}
1470
		}
1471
	}
1472

    
1473
	function delete_queue() {
1474
		unref_on_altq_queue_list($this->GetQname());
1475
		cleanup_queue_from_rules($this->GetQname());
1476
		unset_object_by_reference($this->GetLink());
1477
	}
1478

    
1479
	function delete_all() {
1480
		if (count($this->subqueues)) {
1481
			foreach ($this->subqueues as $q) {
1482
				$q->delete_all();
1483
				unset_object_by_reference($q->GetLink());
1484
				unset($q);
1485
			}
1486
			unset($this->subqueues);
1487
		}
1488
	}
1489

    
1490
	function &find_queue($interface, $qname) {
1491
		if ($qname == $this->GetQname()) {
1492
			return $this;
1493
		}
1494
	}
1495

    
1496
	function find_parentqueue($interface, $qname) { return; }
1497

    
1498
	function validate_input($data, &$input_errors) {
1499

    
1500
		$reqdfields[] = "name";
1501
		$reqdfieldsn[] = gettext("Name");
1502
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1503

    
1504
		if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
1505
			$input_errors[] = gettext("Bandwidth must be an integer.");
1506
		}
1507
		if ($data['bandwidth'] < 0) {
1508
			$input_errors[] = gettext("Bandwidth cannot be negative.");
1509
		}
1510
		if ($data['bandwidthtype'] == "%") {
1511
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
1512
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
1513
			}
1514
		}
1515
		if ($this->CheckBandwidth($data['bandwidth'], $data['bandwidthtype']))
1516
			$input_errors[] = "The sum of child bandwidth is higher than parent.";
1517

    
1518
		if (isset($data['priority']) && (!is_numeric($data['priority']) ||
1519
		    ($data['priority'] < 0) || ($data['priority'] > 15))) {
1520
			$input_errors[] = gettext("The priority must be an integer between 0 and 15.");
1521
		}
1522
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
1523
				$input_errors[] = gettext("Queue limit must be an integer");
1524
		}
1525
		if ($data['qlimit'] < 0) {
1526
				$input_errors[] = gettext("Queue limit must be positive");
1527
		}
1528
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['newname'])) {
1529
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1530
		}
1531
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]*$/", $data['name'])) {
1532
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
1533
		}
1534
		$default = $this->GetDefault();
1535
		if (!empty($data['default']) && altq_get_default_queue($data['interface']) && empty($default)) {
1536
			$input_errors[] = gettext("Only one default queue per interface is allowed.");
1537
		}
1538
	}
1539

    
1540
	function ReadConfig(&$q) {
1541
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
1542
			$this->SetQname($q['newname']);
1543
		} else if (!empty($q['newname'])) {
1544
			$this->SetQname($q['newname']);
1545
		} else if (isset($q['name'])) {
1546
			$this->SetQname($q['name']);
1547
		}
1548
		if (isset($q['interface'])) {
1549
			$this->SetInterface($q['interface']);
1550
		}
1551
		$this->SetBandwidth($q['bandwidth']);
1552
		if ($q['bandwidthtype'] <> "") {
1553
			$this->SetBwscale($q['bandwidthtype']);
1554
		}
1555
		if (!empty($q['qlimit'])) {
1556
			$this->SetQlimit($q['qlimit']);
1557
		} else {
1558
			$this->SetQlimit(""); // Default
1559
		}
1560
		if (is_numeric($q['priority'])) {
1561
			$this->SetQPriority($q['priority']);
1562
		} else {
1563
			$this->SetQpriority("");
1564
		}
1565
		if (!empty($q['description'])) {
1566
			$this->SetDescription($q['description']);
1567
		} else {
1568
			$this->SetDescription("");
1569
		}
1570
		if (!empty($q['red'])) {
1571
			$this->SetRed($q['red']);
1572
		} else {
1573
			$this->SetRed();
1574
		}
1575
		if (!empty($q['codel'])) {
1576
			$this->SetCodel($q['codel']);
1577
		} else {
1578
			$this->SetCodel();
1579
		}
1580
		if (!empty($q['rio'])) {
1581
			$this->SetRio($q['rio']);
1582
		} else {
1583
			$this->SetRio();
1584
		}
1585
		if (!empty($q['ecn'])) {
1586
			$this->SetEcn($q['ecn']);
1587
		} else {
1588
			$this->SetEcn();
1589
		}
1590
		if (!empty($q['default'])) {
1591
			$this->SetDefault($q['default']);
1592
		} else {
1593
			$this->SetDefault();
1594
		}
1595
		if (!empty($q['enabled'])) {
1596
			$this->SetEnabled($q['enabled']);
1597
		} else {
1598
			$this->SetEnabled("");
1599
		}
1600
	}
1601

    
1602
	function build_tree() {
1603
		$tree = " <li><a href=\"firewall_shaper.php?interface=". $this->GetInterface()."&amp;queue=". htmlspecialchars($this->GetQname())."&amp;action=show";
1604
		$tree .= "\" ";
1605
		$tmpvalue = $this->GetDefault();
1606
		if (!empty($tmpvalue)) {
1607
			$tree .= " class=\"navlnk\"";
1608
		}
1609
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
1610
		/*
1611
		 * Not needed here!
1612
		 * if (is_array($queues) {
1613
		 *	  $tree .= "<ul>";
1614
		 *	  foreach ($q as $queues)
1615
		 *		  $tree .= $queues['$q->GetName()']->build_tree();
1616
		 *	  endforeach
1617
		 *	  $tree .= "</ul>";
1618
		 * }
1619
		 */
1620

    
1621
		$tree .= "</li>";
1622

    
1623
		return $tree;
1624
	}
1625

    
1626
	/* Should return something like:
1627
	 * queue $qname on $qinterface bandwidth ....
1628
	 */
1629
	function build_rules(&$default = false) {
1630
		$pfq_rule = " queue ". $this->qname;
1631
		if ($this->GetInterface()) {
1632
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
1633
		}
1634
		$tmpvalue = $this->GetQpriority();
1635
		if (is_numeric($tmpvalue)) {
1636
			$pfq_rule .= " priority ".$this->GetQpriority();
1637
		}
1638
		$tmpvalue = $this->GetQlimit();
1639
		if (!empty($tmpvalue)) {
1640
			$pfq_rule .= " qlimit " . $this->GetQlimit();
1641
		}
1642
		if ($this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetDefault() || $this->GetCodel()) {
1643
			$pfq_rule .= " priq ( ";
1644
			$tmpvalue = $this->GetRed();
1645
			if (!empty($tmpvalue)) {
1646
				$comma = 1;
1647
				$pfq_rule .= " red ";
1648
			}
1649
			$tmpvalue = $this->GetRio();
1650
			if (!empty($tmpvalue)) {
1651
				if ($comma) {
1652
					$pfq_rule .= " ,";
1653
				}
1654
				$comma = 1;
1655
				$pfq_rule .= " rio ";
1656
			}
1657
			$tmpvalue = $this->GetEcn();
1658
			if (!empty($tmpvalue)) {
1659
				if ($comma) {
1660
					$pfq_rule .= " ,";
1661
				}
1662
				$comma = 1;
1663
				$pfq_rule .= " ecn ";
1664
			}
1665
			$tmpvalue = $this->GetCodel();
1666
			if (!empty($tmpvalue)) {
1667
				if ($comma) {
1668
					$pfq_rule .= " ,";
1669
				}
1670
				$comma = 1;
1671
				$pfq_rule .= " codel ";
1672
			}
1673
			$tmpvalue = $this->GetDefault();
1674
			if (!empty($tmpvalue)) {
1675
				if ($comma) {
1676
					$pfq_rule .= " ,";
1677
				}
1678
				$pfq_rule .= " default ";
1679
				$default = true;
1680
			}
1681
			$pfq_rule .= " ) ";
1682
		}
1683

    
1684
		$pfq_rule .= " \n";
1685

    
1686
		return $pfq_rule;
1687
	}
1688

    
1689
	/*
1690
	 * To return the html form to show to user
1691
	 * for getting the parameters.
1692
	 * Should do even for first time when the
1693
	 * object is created and later when we may
1694
	 * need to update it. (2)
1695
	 */
1696

    
1697
	function build_form() {
1698

    
1699
		$sform = new Form();
1700

    
1701
		$sform->setAction("firewall_shaper.php");
1702

    
1703
		$section = new Form_Section("");
1704

    
1705
		$section->addInput(new Form_Checkbox(
1706
			'enabled',
1707
			'Enable/Disable',
1708
			'Enable/disable discipline and its children',
1709
			($this->GetEnabled() == "on"),
1710
			'on'
1711
		));
1712

    
1713
		$section->addInput(new Form_Input(
1714
			'newname',
1715
			'*Name',
1716
			'text',
1717
			$this->GetQname()
1718
		))->setHelp('Enter the name of the queue here. Do not use spaces and limit the size to 15 characters.');
1719

    
1720
		$section->addInput(new Form_Input(
1721
			'name',
1722
			null,
1723
			'hidden',
1724
			$this->GetQname()
1725
		));
1726

    
1727
		if (!is_a($this, "hfsc_queue")) {
1728
			$section->addInput(new Form_Input(
1729
				'priority',
1730
				'Priority',
1731
				'number',
1732
				$this->GetQpriority(),
1733
				['min' => '0', 'max'=> '']
1734
			))->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.');
1735
		}
1736

    
1737
		$section->addInput(new Form_Input(
1738
			'qlimit',
1739
			'Queue Limit',
1740
			'number',
1741
			$this->GetQlimit()
1742
		))->setHelp('Queue limit in packets.');
1743

    
1744
		$group = new Form_Group('Scheduler options');
1745

    
1746
		if (empty($this->subqueues)) {
1747
			$group->add(new Form_Checkbox(
1748
				'default',
1749
				null,
1750
				null,
1751
				$this->GetDefault(),
1752
				'default'
1753
			))->setHelp('Default Queue');
1754
		}
1755

    
1756
		$group->add(new Form_Checkbox(
1757
			'red',
1758
			null,
1759
			null,
1760
			!empty($this->GetRed())
1761
		))->setHelp('%1$sRandom Early Detection%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#red">', '</a>');
1762

    
1763
		$group->add(new Form_Checkbox(
1764
			'rio',
1765
			null,
1766
			null,
1767
			!empty($this->GetRio())
1768
		))->setHelp('%1$sRandom Early Detection In and Out%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#rio">', '</a>');
1769

    
1770
		$group->add(new Form_Checkbox(
1771
			'ecn',
1772
			null,
1773
			null,
1774
			!empty($this->GetEcn())
1775
		))->setHelp('%1$sExplicit Congestion Notification%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">', '</a>');
1776

    
1777
		$group->add(new Form_Checkbox(
1778
			'codel',
1779
			null,
1780
			null,
1781
			!empty($this->GetCodel())
1782
		))->setHelp('%1$sCodel Active Queue%2$s', '<a target="_new" href="https://web.archive.org/web/20160404153707/http://www.openbsd.org/faq/pf/queueing.html#ecn">', '</a>');
1783

    
1784
		$group->setHelp('Select options for this queue');
1785

    
1786
		$section->add($group);
1787

    
1788
		$section->addInput(new Form_Input(
1789
			'description',
1790
			'Description',
1791
			'text',
1792
			$this->GetDescription()
1793
		));
1794

    
1795
		$sform->add($section);
1796

    
1797
		$sform->addGlobal(new Form_Input(
1798
			'interface',
1799
			null,
1800
			'hidden',
1801
			$this->GetInterface()
1802
		));
1803

    
1804
		$sform->addGlobal(new Form_Input(
1805
			'name',
1806
			null,
1807
			'hidden',
1808
			$this->GetQname()
1809
		));
1810

    
1811
		return($sform);
1812
	}
1813

    
1814
	function build_shortform() {
1815
		/* XXX: Hacks in sight. Mostly layer violations!  */
1816
		global $g, $altq_list_queues;
1817
		global $shaperIFlist;
1818

    
1819
		$altq =& $altq_list_queues[$this->GetInterface()];
1820

    
1821
		if ($altq) {
1822
			$scheduler = $altq->GetScheduler();
1823
		}
1824

    
1825
		$form = '<dl class="dl-horizontal">';
1826
		$form .= '	<dt>';
1827
		$form .= '		<a href="firewall_shaper.php?interface=' . $this->GetInterface() . '&amp;queue=' . $this->GetQname() . '&amp;action=show">' . $shaperIFlist[$this->GetInterface()] . '</a>';
1828
		$form .= '	</dt>';
1829
		$form .= '	<dd>';
1830
		$form .=		$scheduler;
1831
		$form .= '	</dd>';
1832

    
1833
		$form .= '	<dt>';
1834
		$form .=		'Bandwidth';
1835
		$form .= '	</dt>';
1836
		$form .= '	<dd>';
1837
		$form .=		$this->GetBandwidth() . '&nbsp;' . $this->GetBwscaleText();
1838
		$form .= '	</dd>';
1839

    
1840
		$tmpvalue = $this->GetQpriority();
1841
		if (!empty($tmpvalue)) {
1842
			$form .= '	<dt>';
1843
			$form .=		'Priority';
1844
			$form .= '	<dt>';
1845
			$form .= '	<dd>';
1846
			$form .=		'On';
1847
			$form .= '	</dd>';
1848
		}
1849

    
1850
		$tmpvalue = $this->GetDefault();
1851
		if (!empty($tmpvalue)) {
1852
			$form .= '	<dt>';
1853
			$form .=		'Default';
1854
			$form .= '	<dt>';
1855
			$form .= '	<dd>';
1856
			$form .=		'On';
1857
			$form .= '	</dd>';
1858
		}
1859

    
1860
			$form .= '	<dt>';
1861
			$form .= 'Delete';
1862
			$form .= '	<dt>';
1863
			$form .= '	<dd>';
1864

    
1865
			$form .= '<a class="btn btn-danger btn-xs" href="firewall_shaper_queues.php?interface=';
1866
			$form .= $this->GetInterface() . '&amp;queue=';
1867
			$form .= $this->GetQname() . '&amp;action=delete">';
1868
			$form .= '<i class="fa fa-trash icon-embed-btn"></i>';
1869
			$form .= gettext("Delete Queue from this Interface") . '</a>';
1870

    
1871
			$form .= '	</dd>';
1872

    
1873
			$form .= '</dl>';
1874

    
1875
		return $form;
1876

    
1877
	}
1878

    
1879
	function update_altq_queue_data(&$q) {
1880
		$this->ReadConfig($q);
1881
	}
1882

    
1883
	function wconfig() {
1884
		$cflink =& get_reference_to_me_in_config($this->GetLink());
1885
		if (!is_array($cflink)) {
1886
			$cflink = array();
1887
		}
1888
		$cflink['name'] = $this->GetQname();
1889
		$cflink['interface'] = $this->GetInterface();
1890
		$cflink['qlimit'] = trim($this->GetQlimit());
1891
		if (empty($cflink['qlimit'])) {
1892
			unset($cflink['qlimit']);
1893
		}
1894
		$cflink['priority'] = trim($this->GetQpriority());
1895
		if (!is_numeric($cflink['priority'])) {
1896
			unset($cflink['priority']);
1897
		}
1898
		$cflink['description'] = trim($this->GetDescription());
1899
		if (empty($cflink['description'])) {
1900
			unset($cflink['description']);
1901
		}
1902
		$cflink['enabled'] = trim($this->GetEnabled());
1903
		if (empty($cflink['enabled'])) {
1904
			unset($cflink['enabled']);
1905
		}
1906
		$cflink['default'] = trim($this->GetDefault());
1907
		if (empty($cflink['default'])) {
1908
			unset($cflink['default']);
1909
		}
1910
		$cflink['red'] = trim($this->GetRed());
1911
		if (empty($cflink['red'])) {
1912
			unset($cflink['red']);
1913
		}
1914
		$cflink['codel'] = trim($this->GetCodel());
1915
		if (empty($cflink['codel'])) {
1916
			unset($cflink['codel']);
1917
		}
1918
		$cflink['rio'] = trim($this->GetRio());
1919
		if (empty($cflink['rio'])) {
1920
			unset($cflink['rio']);
1921
		}
1922
		$cflink['ecn'] = trim($this->GetEcn());
1923
		if (empty($cflink['ecn'])) {
1924
			unset($cflink['ecn']);
1925
		}
1926
	}
1927
}
1928

    
1929
class hfsc_queue extends priq_queue {
1930
	/* realtime */
1931
	var $realtime;
1932
	var $r_m1;
1933
	var $r_d;
1934
	var $r_m2;
1935
	/* linkshare */
1936
	var $linkshare;
1937
	var $l_m1;
1938
	var $l_d;
1939
	var $l_m2;
1940
	/* upperlimit */
1941
	var $upperlimit;
1942
	var $u_m1;
1943
	var $u_d;
1944
	var $u_m2;
1945

    
1946
	/*
1947
	 * HFSC can have nested queues.
1948
	 */
1949
	function CanHaveChildren() {
1950
		return true;
1951
	}
1952
	function GetRealtime() {
1953
		return $this->realtime;
1954
	}
1955
	function GetR_m1() {
1956
		return $this->r_m1;
1957
	}
1958
	function GetR_d() {
1959
		return $this->r_d;
1960
	}
1961
	function GetR_m2() {
1962
		return $this->r_m2;
1963
	}
1964
	function SetRealtime() {
1965
		$this->realtime = "on";
1966
	}
1967
	function DisableRealtime() {
1968
		$this->realtime = "";
1969
	}
1970
	function SetR_m1($value) {
1971
		$this->r_m1 = $value;
1972
	}
1973
	function SetR_d($value) {
1974
		$this->r_d = $value;
1975
	}
1976
	function SetR_m2($value) {
1977
		$this->r_m2 = $value;
1978
	}
1979
	function GetLinkshare() {
1980
		return $this->linkshare;
1981
	}
1982
	function DisableLinkshare() {
1983
		$this->linkshare = "";
1984
	}
1985
	function GetL_m1() {
1986
		return $this->l_m1;
1987
	}
1988
	function GetL_d() {
1989
		return $this->l_d;
1990
	}
1991
	function GetL_m2() {
1992
		return $this->l_m2;
1993
	}
1994
	function SetLinkshare() {
1995
		$this->linkshare = "on";
1996
	}
1997
	function SetL_m1($value) {
1998
		$this->l_m1 = $value;
1999
	}
2000
	function SetL_d($value) {
2001
		$this->l_d = $value;
2002
	}
2003
	function SetL_m2($value) {
2004
		$this->l_m2 = $value;
2005
	}
2006
	function GetUpperlimit() {
2007
		return $this->upperlimit;
2008
	}
2009
	function GetU_m1() {
2010
		return $this->u_m1;
2011
	}
2012
	function GetU_d() {
2013
		return $this->u_d;
2014
	}
2015
	function GetU_m2() {
2016
		return $this->u_m2;
2017
	}
2018
	function SetUpperlimit() {
2019
		$this->upperlimit = "on";
2020
	}
2021
	function DisableUpperlimit() {
2022
		$this->upperlimit = "";
2023
	}
2024
	function SetU_m1($value) {
2025
		$this->u_m1 = $value;
2026
	}
2027
	function SetU_d($value) {
2028
		$this->u_d = $value;
2029
	}
2030
	function SetU_m2($value) {
2031
		$this->u_m2 = $value;
2032
	}
2033

    
2034
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2035

    
2036
		if (!is_array($this->subqueues)) {
2037
			$this->subqueues = array();
2038
		}
2039
		$__tmp_q = new hfsc_queue(); $q =& $__tmp_q;
2040
		$q->SetInterface($this->GetInterface());
2041
		$q->SetParent($this);
2042
		$q->ReadConfig($qname);
2043
		$q->validate_input($qname, $input_errors);
2044

    
2045
		$q->SetEnabled("on");
2046
		$q->SetLink($path);
2047

    
2048
		$this->subqueues[$q->GetQname()] =& $q; //new hfsc_queue()
2049
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2050
		if (is_array($qname['queue'])) {
2051
			foreach ($qname['queue'] as $key1 => $que) {
2052
				array_push($path, $key1);
2053
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2054
				array_pop($path);
2055
			}
2056
		}
2057

    
2058
		return $q;
2059
	}
2060

    
2061
	function copy_queue($interface, &$cflink) {
2062

    
2063
		$cflink['name'] = $this->GetQname();
2064
		$cflink['interface'] = $interface;
2065
		$cflink['qlimit'] = trim($this->GetQlimit());
2066
		if (empty($cflink['qlimit'])) {
2067
			unset($cflink['qlimit']);
2068
		}
2069
		$cflink['priority'] = trim($this->GetQpriority());
2070
		if (empty($cflink['priority'])) {
2071
			unset($cflink['priority']);
2072
		}
2073
		$cflink['description'] = trim($this->GetDescription());
2074
		if (empty($cflink['description'])) {
2075
			unset($cflink['description']);
2076
		}
2077
		$cflink['bandwidth'] = $this->GetBandwidth();
2078
		$cflink['bandwidthtype'] = $this->GetBwscale();
2079
		$cflink['enabled'] = trim($this->GetEnabled());
2080
		if (empty($cflink['enabled'])) {
2081
			unset($cflink['enabled']);
2082
		}
2083
		$cflink['default'] = trim($this->GetDefault());
2084
		if (empty($cflink['default'])) {
2085
			unset($cflink['default']);
2086
		}
2087
		$cflink['red'] = trim($this->GetRed());
2088
		if (empty($cflink['red'])) {
2089
			unset($cflink['red']);
2090
		}
2091
		$cflink['rio'] = trim($this->GetRio());
2092
		if (empty($cflink['rio'])) {
2093
			unset($cflink['rio']);
2094
		}
2095
		$cflink['ecn'] = trim($this->GetEcn());
2096
		if (empty($cflink['ecn'])) {
2097
			unset($cflink['ecn']);
2098
		}
2099
		if ($this->GetLinkshare() <> "") {
2100
			if ($this->GetL_m1() <> "") {
2101
				$cflink['linkshare1'] = $this->GetL_m1();
2102
				$cflink['linkshare2'] = $this->GetL_d();
2103
				$cflink['linkshare'] = "on";
2104
			} else {
2105
				unset($cflink['linkshare1']);
2106
				unset($cflink['linkshare2']);
2107
				unset($cflink['linkshare']);
2108
			}
2109
			if ($this->GetL_m2() <> "") {
2110
				$cflink['linkshare3'] = $this->GetL_m2();
2111
				$cflink['linkshare'] = "on";
2112
			} else {
2113
				unset($cflink['linkshare3']);
2114
				unset($cflink['linkshare']);
2115
			}
2116
		}
2117
		if ($this->GetRealtime() <> "") {
2118
			if ($this->GetR_m1() <> "") {
2119
				$cflink['realtime1'] = $this->GetR_m1();
2120
				$cflink['realtime2'] = $this->GetR_d();
2121
				$cflink['realtime'] = "on";
2122
			} else {
2123
				unset($cflink['realtime1']);
2124
				unset($cflink['realtime2']);
2125
				unset($cflink['realtime']);
2126
			}
2127
			if ($this->GetR_m2() <> "") {
2128
				$cflink['realtime3'] = $this->GetR_m2();
2129
				$cflink['realtime'] = "on";
2130
			} else {
2131
				unset($cflink['realtime3']);
2132
				unset($cflink['realtime']);
2133
			}
2134
		}
2135
		if ($this->GetUpperlimit() <> "") {
2136
			if ($this->GetU_m1() <> "") {
2137
				$cflink['upperlimit1'] = $this->GetU_m1();
2138
				$cflink['upperlimit2'] = $this->GetU_d();
2139
				$cflink['upperlimit'] = "on";
2140
			} else {
2141
				unset($cflink['upperlimit']);
2142
				unset($cflink['upperlimit1']);
2143
				unset($cflink['upperlimit2']);
2144
			}
2145
			if ($this->GetU_m2() <> "") {
2146
				$cflink['upperlimit3'] = $this->GetU_m2();
2147
				$cflink['upperlimit'] = "on";
2148
			} else {
2149
				unset($cflink['upperlimit3']);
2150
				unset($cflink['upperlimit']);
2151
			}
2152
		}
2153

    
2154
		if (is_array($this->subqueues)) {
2155
			$cflinkp['queue'] = array();
2156
			foreach ($this->subqueues as $q) {
2157
				$cflink['queue'][$q->GetQname()] = array();
2158
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2159
			}
2160
		}
2161
	}
2162

    
2163
	function delete_queue() {
2164
		unref_on_altq_queue_list($this->GetQname());
2165
		cleanup_queue_from_rules($this->GetQname());
2166
		$parent =& $this->GetParent();
2167
		foreach ($this->subqueues as $q)
2168
			$q->delete_queue();
2169
		unset_object_by_reference($this->GetLink());
2170
	}
2171

    
2172
	/*
2173
	 * Should search even its children
2174
	 */
2175
	function &find_queue($interface, $qname) {
2176
		if ($qname == $this->GetQname()) {
2177
			return $this;
2178
		}
2179

    
2180
		foreach ($this->subqueues as $q) {
2181
			$result =& $q->find_queue("", $qname);
2182
			if ($result) {
2183
				return $result;
2184
			}
2185
		}
2186
	}
2187

    
2188
	function &find_parentqueue($interface, $qname) {
2189
		if ($this->subqueues[$qname]) {
2190
			return $this;
2191
		}
2192
		foreach ($this->subqueues as $q) {
2193
			$result = $q->find_parentqueue("", $qname);
2194
			if ($result) {
2195
				return $result;
2196
			}
2197
		}
2198
	}
2199

    
2200
	function validate_input($data, &$input_errors) {
2201
		parent::validate_input($data, $input_errors);
2202

    
2203
		$reqdfields[] = "bandwidth";
2204
		$reqdfieldsn[] = gettext("Bandwidth");
2205
		$reqdfields[] = "bandwidthtype";
2206
		$reqdfieldsn[] = gettext("Bandwidthtype");
2207

    
2208
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2209

    
2210
		if (isset($data['linkshare3']) && $data['linkshare3'] <> "") {
2211
			if ($data['bandwidth'] && (!is_numeric($data['bandwidth']))) {
2212
				$input_errors[] = gettext("Bandwidth must be an integer.");
2213
			}
2214

    
2215
			if ($data['bandwidth'] < 0) {
2216
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2217
			}
2218

    
2219
			if ($data['bandwidthtype'] == "%") {
2220
				if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
2221
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2222
				}
2223
			}
2224
		}
2225

    
2226
		if ($data['upperlimit1'] <> "" && $data['upperlimit2'] == "") {
2227
			$input_errors[] = gettext("upperlimit service curve defined but missing (d) value");
2228
		}
2229
		if ($data['upperlimit2'] <> "" && $data['upperlimit1'] == "") {
2230
			$input_errors[] = gettext("upperlimit service curve defined but missing initial bandwidth (m1) value");
2231
		}
2232
		if ($data['upperlimit1'] <> "" && !is_valid_shaperbw($data['upperlimit1'])) {
2233
			$input_errors[] = gettext("upperlimit m1 value needs to be Kb, Mb, Gb, or %");
2234
		}
2235
		if ($data['upperlimit2'] <> "" && !is_numeric($data['upperlimit2'])) {
2236
			$input_errors[] = gettext("upperlimit d value needs to be numeric");
2237
		}
2238
		if ($data['upperlimit3'] <> "" && !is_valid_shaperbw($data['upperlimit3'])) {
2239
			$input_errors[] = gettext("upperlimit m2 value needs to be Kb, Mb, Gb, or %");
2240
		}
2241

    
2242
		/*
2243
		if (isset($data['upperlimit']) && $data['upperlimit3'] <> "" && $data['upperlimit1'] <> "") {
2244
			$bw_1 = get_hfsc_bandwidth($this, $data['upperlimit1']);
2245
			$bw_2 = get_hfsc_bandwidth($this, $data['upperlimit3']);
2246
			if (floatval($bw_1) < floatval($bw_2)) {
2247
				$input_errors[] = ("upperlimit m1 cannot be smaller than m2");
2248
			}
2249

    
2250
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2251
				$input_errors[] = ("upperlimit specification exceeds 80% of allowable allocation.");
2252
			}
2253
		}
2254
		*/
2255
		if ($data['linkshare1'] <> "" && $data['linkshare2'] == "") {
2256
			$input_errors[] = gettext("linkshare service curve defined but missing (d) value");
2257
		}
2258
		if ($data['linkshare2'] <> "" && $data['linkshare1'] == "") {
2259
			$input_errors[] = gettext("linkshare service curve defined but missing initial bandwidth (m1) value");
2260
		}
2261
		if ($data['linkshare1'] <> "" && !is_valid_shaperbw($data['linkshare1'])) {
2262
			$input_errors[] = gettext("linkshare m1 value needs to be Kb, Mb, Gb, or %");
2263
		}
2264
		if ($data['linkshare2'] <> "" && !is_numeric($data['linkshare2'])) {
2265
			$input_errors[] = gettext("linkshare d value needs to be numeric");
2266
		}
2267
		if ($data['linkshare3'] <> "" && !is_valid_shaperbw($data['linkshare3'])) {
2268
			$input_errors[] = gettext("linkshare m2 value needs to be Kb, Mb, Gb, or %");
2269
		}
2270
		if ($data['realtime1'] <> "" && $data['realtime2'] == "") {
2271
			$input_errors[] = gettext("realtime service curve defined but missing (d) value");
2272
		}
2273
		if ($data['realtime2'] <> "" && $data['realtime1'] == "") {
2274
			$input_errors[] = gettext("realtime service curve defined but missing initial bandwidth (m1) value");
2275
		}
2276

    
2277
		/*
2278
		if (isset($data['linkshare']) && $data['linkshare3'] <> "" && $data['linkshare1'] <> "" && 0) {
2279
			$bw_1 = get_hfsc_bandwidth($this, $data['linkshare1']);
2280
			$bw_2 = get_hfsc_bandwidth($this, $data['linkshare3']);
2281
			if (floatval($bw_1) < floatval($bw_2)) {
2282
				$input_errors[] = ("linkshare m1 cannot be smaller than m2");
2283
			}
2284

    
2285
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2286
				$input_errors[] = ("linkshare specification exceeds 80% of allowable allocation.");
2287
			}
2288
		}
2289
		*/
2290

    
2291
		if ($data['realtime1'] <> "" && !is_valid_shaperbw($data['realtime1'])) {
2292
			$input_errors[] = gettext("realtime m1 value needs to be Kb, Mb, Gb, or %");
2293
		}
2294
		if ($data['realtime2'] <> "" && !is_numeric($data['realtime2'])) {
2295
			$input_errors[] = gettext("realtime d value needs to be numeric");
2296
		}
2297
		if ($data['realtime3'] <> "" && !is_valid_shaperbw($data['realtime3'])) {
2298
			$input_errors[] = gettext("realtime m2 value needs to be Kb, Mb, Gb, or %");
2299
		}
2300

    
2301
		/*
2302
		if (isset($data['realtime']) && $data['realtime3'] <> "" && $data['realtime1'] <> "" && 0) {
2303
			$bw_1 = get_hfsc_bandwidth($this, $data['realtime1']);
2304
			$bw_2 = get_hfsc_bandwidth($this, $data['realtime3']);
2305
			if (floatval($bw_1) < floatval($bw_2)) {
2306
				$input_errors[] = ("realtime m1 cannot be smaller than m2");
2307
			}
2308

    
2309
			if (get_interface_bandwidth($this) < (0.8 * (floatval($bw_1) + floatval($bw_2)))) {
2310
				$input_errors[] = ("realtime specification exceeds 80% of allowable allocation.");
2311
			}
2312
		}
2313
		*/
2314
	}
2315

    
2316
	function ReadConfig(&$cflink) {
2317
		if (!empty($cflink['linkshare'])) {
2318
			if (!empty($cflink['linkshare1'])) {
2319
				$this->SetL_m1($cflink['linkshare1']);
2320
				$this->SetL_d($cflink['linkshare2']);
2321
				$this->SetLinkshare();
2322
			} else {
2323
				$this->SetL_m1("");
2324
				$this->SetL_d("");
2325
				$this->DisableLinkshare();
2326
			}
2327
			if (!empty($cflink['linkshare3'])) {
2328
				$this->SetL_m2($cflink['linkshare3']);
2329
				$this->SetLinkshare();
2330
			}
2331
		} else {
2332
			$this->DisableLinkshare();
2333
		}
2334
		if (!empty($cflink['realtime'])) {
2335
			if (!empty($cflink['realtime1'])) {
2336
				$this->SetR_m1($cflink['realtime1']);
2337
				$this->SetR_d($cflink['realtime2']);
2338
				$this->SetRealtime();
2339
			} else {
2340
				$this->SetR_m1("");
2341
				$this->SetR_d("");
2342
				$this->DisableRealtime();
2343
			}
2344
			if (!empty($cflink['realtime3'])) {
2345
				$this->SetR_m2($cflink['realtime3']);
2346
				$this->SetRealtime();
2347
			}
2348
		} else {
2349
			$this->DisableRealtime();
2350
		}
2351
		if (!empty($cflink['upperlimit'])) {
2352
			if (!empty($cflink['upperlimit1'])) {
2353
				$this->SetU_m1($cflink['upperlimit1']);
2354
				$this->SetU_d($cflink['upperlimit2']);
2355
				$this->SetUpperlimit();
2356
			} else {
2357
				$this->SetU_m1("");
2358
				$this->SetU_d("");
2359
				$this->DisableUpperlimit();
2360
			}
2361
			if (!empty($cflink['upperlimit3'])) {
2362
				$this->SetU_m2($cflink['upperlimit3']);
2363
				$this->SetUpperlimit();
2364
			}
2365
		} else {
2366
			$this->DisableUpperlimit();
2367
		}
2368
		parent::ReadConfig($cflink);
2369
	}
2370

    
2371
	function build_tree() {
2372
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface() ."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
2373
		$tree .= "\" ";
2374
		$tmpvalue = $this->GetDefault();
2375
		if (!empty($tmpvalue)) {
2376
			$tree .= " class=\"navlnk\"";
2377
		}
2378
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
2379
		if (is_array($this->subqueues)) {
2380
			$tree .= "<ul>";
2381
			foreach ($this->subqueues as $q) {
2382
				$tree .= $q->build_tree();
2383
			}
2384
			$tree .= "</ul>";
2385
		}
2386
		$tree .= "</li>";
2387
		return $tree;
2388
	}
2389

    
2390
	/* Even this should take children into consideration */
2391
	function build_rules(&$default = false) {
2392

    
2393
		$pfq_rule = " queue ". $this->qname;
2394
		if ($this->GetInterface()) {
2395
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2396
		}
2397
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2398
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
2399
		}
2400

    
2401
		$tmpvalue = $this->GetQlimit();
2402
		if (!empty($tmpvalue)) {
2403
			$pfq_rule .= " qlimit " . $this->GetQlimit();
2404
		}
2405
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetCodel() || $this->GetRealtime() <> "" || $this->GetLinkshare() <> "" || $this->GetUpperlimit() <> "") {
2406
			$pfq_rule .= " hfsc ( ";
2407
			$tmpvalue = $this->GetRed();
2408
			if (!empty($tmpvalue)) {
2409
				$comma = 1;
2410
				$pfq_rule .= " red ";
2411
			}
2412

    
2413
			$tmpvalue = $this->GetRio();
2414
			if (!empty($tmpvalue)) {
2415
				if ($comma) {
2416
					$pfq_rule .= " ,";
2417
				}
2418
				$comma = 1;
2419
				$pfq_rule .= " rio ";
2420
			}
2421
			$tmpvalue = $this->GetEcn();
2422
			if (!empty($tmpvalue)) {
2423
				if ($comma) {
2424
					$pfq_rule .= " ,";
2425
				}
2426
				$comma = 1;
2427
				$pfq_rule .= " ecn ";
2428
			}
2429
			$tmpvalue = $this->GetCodel();
2430
			if (!empty($tmpvalue)) {
2431
				if ($comma) {
2432
					$pfq_rule .= " ,";
2433
				}
2434
				$comma = 1;
2435
				$pfq_rule .= " codel ";
2436
			}
2437
			$tmpvalue = $this->GetDefault();
2438
			if (!empty($tmpvalue)) {
2439
				if ($comma) {
2440
					$pfq_rule .= " ,";
2441
				}
2442
				$comma = 1;
2443
				$pfq_rule .= " default ";
2444
				$default = true;
2445
			}
2446

    
2447
			if ($this->GetRealtime() <> "") {
2448
				if ($comma) {
2449
					$pfq_rule .= " , ";
2450
				}
2451
				if ($this->GetR_m1() <> "" && $this->GetR_d() <> "" && $this->GetR_m2() <> "") {
2452
					$pfq_rule .= " realtime (".$this->GetR_m1() . ", " . $this->GetR_d().", ". $this->GetR_m2() .") ";
2453
				} else if ($this->GetR_m2() <> "") {
2454
					$pfq_rule .= " realtime " . $this->GetR_m2();
2455
				}
2456
				$comma = 1;
2457
			}
2458
			if ($this->GetLinkshare() <> "") {
2459
				if ($comma) {
2460
					$pfq_rule .= " ,";
2461
				}
2462
				if ($this->GetL_m1() <> "" && $this->GetL_d() <> "" && $this->GetL_m2() <> "") {
2463
					$pfq_rule .= " linkshare (".$this->GetL_m1(). ", ". $this->GetL_d(). ", ". $this->GetL_m2(). ") ";
2464
				} else if ($this->GetL_m2() <> "") {
2465
					$pfq_rule .= " linkshare " . $this->GetL_m2() . " ";
2466
				}
2467
				$comma = 1;
2468
			}
2469
			if ($this->GetUpperlimit() <> "") {
2470
				if ($comma) {
2471
					$pfq_rule .= " ,";
2472
				}
2473
				if ($this->GetU_m1() <> "" && $this->GetU_d() <> "" && $this->GetU_m2() <> "") {
2474
							$pfq_rule .= " upperlimit (".$this->GetU_m1().", ". $this->GetU_d().", ". $this->GetU_m2(). ") ";
2475
				} else if ($this->GetU_m2() <> "") {
2476
					$pfq_rule .= " upperlimit " . $this->GetU_m2() . " ";
2477
				}
2478
			}
2479
			$pfq_rule .= " ) ";
2480
		}
2481
		if (count($this->subqueues)) {
2482
			$i = count($this->subqueues);
2483
			$pfq_rule .= " { ";
2484
			foreach ($this->subqueues as $qkey => $qnone) {
2485
				if ($i > 1) {
2486
					$i--;
2487
					$pfq_rule .= " {$qkey}, ";
2488
				} else {
2489
					$pfq_rule .= " {$qkey} ";
2490
				}
2491
			}
2492
			$pfq_rule .= " } \n";
2493
			foreach ($this->subqueues as $q) {
2494
				$pfq_rule .= $q->build_rules($default);
2495
			}
2496
		}
2497

    
2498
		$pfq_rule .= " \n";
2499

    
2500
		return $pfq_rule;
2501
	}
2502

    
2503
	function build_javascript() {
2504

    
2505
		$javascript = <<<EOJS
2506
<script type="text/javascript">
2507
//<![CDATA[
2508
	events.push(function(){
2509

    
2510
		// Disables the specified input element
2511
		function disableInput(id, disable) {
2512
			$('#' + id).prop("disabled", disable);
2513
		}
2514

    
2515
		// Upperlimit
2516
		function enable_upperlimit() {
2517
			disableInput('upperlimit1', !$('#upperlimit').prop('checked'));
2518
			disableInput('upperlimit2', !$('#upperlimit').prop('checked'));
2519
			disableInput('upperlimit3', !$('#upperlimit').prop('checked'));
2520
		}
2521

    
2522
		$('#upperlimit').click(function () {
2523
			enable_upperlimit();
2524
		});
2525

    
2526
		enable_upperlimit();
2527

    
2528
		// realtime
2529
		function enable_realtime() {
2530
			disableInput('realtime1', !$('#realtime').prop('checked'));
2531
			disableInput('realtime2', !$('#realtime').prop('checked'));
2532
			disableInput('realtime3', !$('#realtime').prop('checked'));
2533
		}
2534

    
2535
		$('#realtime').click(function () {
2536
			enable_realtime();
2537
		});
2538

    
2539
		enable_realtime();
2540

    
2541
		// linkshare
2542
		function enable_linkshare() {
2543
			disableInput('linkshare1', !$('#linkshare').prop('checked'));
2544
			disableInput('linkshare2', !$('#linkshare').prop('checked'));
2545
			disableInput('linkshare3', !$('#linkshare').prop('checked'));
2546
		}
2547

    
2548
		$('#linkshare').click(function () {
2549
			enable_linkshare();
2550
		});
2551

    
2552
		enable_linkshare();
2553
	});
2554
//]]>
2555
</script>
2556
EOJS;
2557

    
2558
		return $javascript;
2559
	}
2560

    
2561
	function build_form() {
2562

    
2563
		$sform = parent::build_form();
2564

    
2565
		$section = new Form_Section('Service Curve (sc)');
2566

    
2567
		$group = new Form_Group('Bandwidth');
2568

    
2569
		$group->add(new Form_Input(
2570
			'bandwidth',
2571
			null,
2572
			'number',
2573
			$this->GetBandwidth(),
2574
			['step' => 'any', 'min' => '0.000']
2575
		));
2576

    
2577
		$group->add(new Form_Select(
2578
			'bandwidthtype',
2579
			null,
2580
			$this->GetBwscale(),
2581
			array('Kb' => 'Kbit/s',
2582
				  'Mb' => 'Mbit/s',
2583
				  'Gb' => 'Gbit/s',
2584
				  'b' => 'Bit/s',
2585
				  '%' => '%')
2586
		));
2587

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

    
2590
		$section->add($group);
2591

    
2592
		$group = new Form_Group('Max bandwidth for queue.');
2593

    
2594
		$group->add(new Form_Checkbox(
2595
			'upperlimit',
2596
			null,
2597
			'Upper Limit',
2598
			($this->GetUpperlimit()<> "")
2599
		));
2600

    
2601
		$group->add(new Form_Input(
2602
			'upperlimit1',
2603
			null,
2604
			'text',
2605
			$this->GetU_m1()
2606
		))->setHelp('m1');
2607

    
2608
		$group->add(new Form_Input(
2609
			'upperlimit2',
2610
			null,
2611
			'text',
2612
			$this->GetU_d()
2613
		))->setHelp('d');
2614

    
2615
		$group->add(new Form_Input(
2616
			'upperlimit3',
2617
			null,
2618
			'text',
2619
			$this->GetU_m2()
2620
		))->setHelp('m2');
2621

    
2622

    
2623
		$section->add($group);
2624

    
2625
		$group = new Form_Group('Min bandwidth for queue.');
2626

    
2627
		$group->add(new Form_Checkbox(
2628
			'realtime',
2629
			null,
2630
			'Real Time',
2631
			($this->GetRealtime()<> "")
2632
		));
2633

    
2634
		$group->add(new Form_Input(
2635
			'realtime1',
2636
			null,
2637
			'text',
2638
			$this->GetR_m1()
2639
		))->setHelp('m1');
2640

    
2641
		$group->add(new Form_Input(
2642
			'realtime2',
2643
			null,
2644
			'text',
2645
			$this->GetR_d()
2646
		))->setHelp('d');
2647

    
2648
		$group->add(new Form_Input(
2649
			'realtime3',
2650
			null,
2651
			'text',
2652
			$this->GetR_m2()
2653
		))->setHelp('m2');
2654

    
2655
		$section->add($group);
2656

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

    
2659
		$group->add(new Form_Checkbox(
2660
			'linkshare',
2661
			null,
2662
			'Link Share',
2663
			($this->GetLinkshare()<> "")
2664
		));
2665

    
2666
		$group->add(new Form_Input(
2667
			'linkshare1',
2668
			null,
2669
			'text',
2670
			$this->GetL_m1()
2671
		))->setHelp('m1');
2672

    
2673
		$group->add(new Form_Input(
2674
			'linkshare2',
2675
			null,
2676
			'text',
2677
			$this->GetL_d()
2678
		))->setHelp('d');
2679

    
2680
		$group->add(new Form_Input(
2681
			'linkshare3',
2682
			null,
2683
			'text',
2684
			$this->GetL_m2()
2685
		))->setHelp('m2');
2686

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

    
2693
		$section->add($group);
2694

    
2695
		$sform->add($section);
2696

    
2697
		return($sform);
2698
	}
2699

    
2700
	function update_altq_queue_data(&$data) {
2701
		$this->ReadConfig($data);
2702
	}
2703

    
2704
	function wconfig() {
2705
		$cflink =& get_reference_to_me_in_config($this->GetLink());
2706
		if (!is_array($cflink)) {
2707
			$cflink = array();
2708
		}
2709
		$cflink['name'] = $this->GetQname();
2710
		$cflink['interface'] = $this->GetInterface();
2711
		$cflink['qlimit'] = trim($this->GetQlimit());
2712
		if (empty($cflink['qlimit'])) {
2713
			unset($cflink['qlimit']);
2714
		}
2715
		$cflink['priority'] = $this->GetQpriority();
2716
		if (!is_numericint($cflink['priority'])) {
2717
			unset($cflink['priority']);
2718
		}
2719
		$cflink['description'] = $this->GetDescription();
2720
		if (empty($cflink['description'])) {
2721
			unset($cflink['description']);
2722
		}
2723
		$cflink['bandwidth'] = $this->GetBandwidth();
2724
		$cflink['bandwidthtype'] = $this->GetBwscale();
2725
		$cflink['enabled'] = $this->GetEnabled();
2726
		if (empty($cflink['enabled'])) {
2727
			unset($cflink['enabled']);
2728
		}
2729
		$cflink['default'] = $this->GetDefault();
2730
		if (empty($cflink['default'])) {
2731
			unset($cflink['default']);
2732
		}
2733
		$cflink['red'] = trim($this->GetRed());
2734
		if (empty($cflink['red'])) {
2735
			unset($cflink['red']);
2736
		}
2737
		$cflink['rio'] = $this->GetRio();
2738
		if (empty($cflink['rio'])) {
2739
			unset($cflink['rio']);
2740
		}
2741
		$cflink['ecn'] = trim($this->GetEcn());
2742
		if (empty($cflink['ecn'])) {
2743
			unset($cflink['ecn']);
2744
		}
2745
		$cflink['codel'] = trim($this->GetCodel());
2746
		if (empty($cflink['codel'])) {
2747
			unset($cflink['codel']);
2748
		}
2749
		if ($this->GetLinkshare() <> "") {
2750
			if ($this->GetL_m1() <> "") {
2751
				$cflink['linkshare1'] = $this->GetL_m1();
2752
				$cflink['linkshare2'] = $this->GetL_d();
2753
				$cflink['linkshare'] = "on";
2754
			} else {
2755
				unset($cflink['linkshare']);
2756
				unset($cflink['linkshare1']);
2757
				unset($cflink['linkshare2']);
2758
			}
2759
			if ($this->GetL_m2() <> "") {
2760
				$cflink['linkshare3'] = $this->GetL_m2();
2761
				$cflink['linkshare'] = "on";
2762
			} else {
2763
				unset($cflink['linkshare']);
2764
				unset($cflink['linkshare3']);
2765
			}
2766
		} else {
2767
			unset($cflink['linkshare']);
2768
			unset($cflink['linkshare1']);
2769
			unset($cflink['linkshare2']);
2770
			unset($cflink['linkshare3']);
2771
		}
2772
		if ($this->GetRealtime() <> "") {
2773
			if ($this->GetR_m1() <> "") {
2774
				$cflink['realtime1'] = $this->GetR_m1();
2775
				$cflink['realtime2'] = $this->GetR_d();
2776
				$cflink['realtime'] = "on";
2777
			} else {
2778
				unset($cflink['realtime']);
2779
				unset($cflink['realtime1']);
2780
				unset($cflink['realtime2']);
2781
			}
2782
			if ($this->GetR_m2() <> "") {
2783
				$cflink['realtime3'] = $this->GetR_m2();
2784
				$cflink['realtime'] = "on";
2785
			} else {
2786
				unset($cflink['realtime']);
2787
				unset($cflink['realtime3']);
2788
			}
2789
		} else {
2790
			unset($cflink['realtime']);
2791
			unset($cflink['realtime1']);
2792
			unset($cflink['realtime2']);
2793
			unset($cflink['realtime3']);
2794
		}
2795
		if ($this->GetUpperlimit() <> "") {
2796
			if ($this->GetU_m1() <> "") {
2797
				$cflink['upperlimit1'] = $this->GetU_m1();
2798
				$cflink['upperlimit2'] = $this->GetU_d();
2799
				$cflink['upperlimit'] = "on";
2800
			} else {
2801
				unset($cflink['upperlimit']);
2802
				unset($cflink['upperlimit1']);
2803
				unset($cflink['upperlimit2']);
2804
			}
2805
			if ($this->GetU_m2() <> "") {
2806
				$cflink['upperlimit3'] = $this->GetU_m2();
2807
				$cflink['upperlimit'] = "on";
2808
			} else {
2809
				unset($cflink['upperlimit']);
2810
				unset($cflink['upperlimit3']);
2811
			}
2812
		} else {
2813
			unset($cflink['upperlimit']);
2814
			unset($cflink['upperlimit1']);
2815
			unset($cflink['upperlimit2']);
2816
			unset($cflink['upperlimit3']);
2817
		}
2818
	}
2819
}
2820

    
2821
class cbq_queue extends priq_queue {
2822
	var $qborrow = "";
2823

    
2824
	function GetBorrow() {
2825
		return $this->qborrow;
2826
	}
2827
	function SetBorrow($borrow) {
2828
		$this->qborrow = $borrow;
2829
	}
2830
	function CanHaveChildren() {
2831
		return true;
2832
	}
2833

    
2834
	function &add_queue($interface, &$qname, &$path, &$input_errors) {
2835

    
2836
		if (!is_array($this->subqueues)) {
2837
			$this->subqueues = array();
2838
		}
2839
		$__tmp_q = new cbq_queue(); $q =& $__tmp_q;
2840
		$q->SetInterface($this->GetInterface());
2841
		$q->SetParent($this);
2842
		$q->ReadConfig($qname);
2843
		$q->validate_input($qname, $input_errors);
2844

    
2845
		$q->SetEnabled("on");
2846
		$q->SetLink($path);
2847
		$this->subqueues[$q->GetQName()] = &$q;
2848
		ref_on_altq_queue_list($this->GetQname(), $q->GetQname());
2849
		if (is_array($qname['queue'])) {
2850
			foreach ($qname['queue'] as $key1 => $que) {
2851
				array_push($path, $key1);
2852
				$q->add_queue($q->GetInterface(), $que, $path, $input_errors);
2853
				array_pop($path);
2854
			}
2855
		}
2856

    
2857
		return $q;
2858
	}
2859

    
2860
	function copy_queue($interface, &$cflink) {
2861

    
2862
		$cflink['interface'] = $interface;
2863
		$cflink['qlimit'] = trim($this->GetQlimit());
2864
		if (empty($clink['qlimit'])) {
2865
			unset($cflink['qlimit']);
2866
		}
2867
		$cflink['priority'] = trim($this->GetQpriority());
2868
		if (!is_numeric($cflink['priority'])) {
2869
			unset($cflink['priority']);
2870
		}
2871
		$cflink['name'] = $this->GetQname();
2872
		$cflink['description'] = trim($this->GetDescription());
2873
		if (empty($cflink['description'])) {
2874
			unset($cflink['description']);
2875
		}
2876
		$cflink['bandwidth'] = $this->GetBandwidth();
2877
		$cflink['bandwidthtype'] = $this->GetBwscale();
2878
		$cflink['enabled'] = trim($this->GetEnabled());
2879
		if (empty($cflink['enabled'])) {
2880
			unset($cflink['enabled']);
2881
		}
2882
		$cflink['default'] = trim($this->GetDefault());
2883
		if (empty($cflink['default'])) {
2884
			unset($cflink['default']);
2885
		}
2886
		$cflink['red'] = trim($this->GetRed());
2887
		if (empty($cflink['red'])) {
2888
			unset($cflink['red']);
2889
		}
2890
		$cflink['rio'] = trim($this->GetRio());
2891
		if (empty($cflink['rio'])) {
2892
			unset($cflink['rio']);
2893
		}
2894
		$cflink['ecn'] = trim($this->GetEcn());
2895
		if (empty($cflink['ecn'])) {
2896
			unset($cflink['ecn']);
2897
		}
2898
		$cflink['borrow'] = trim($this->GetBorrow());
2899
		if (empty($cflink['borrow'])) {
2900
			unset($cflink['borrow']);
2901
		}
2902
		if (is_array($this->queues)) {
2903
			$cflinkp['queue'] = array();
2904
			foreach ($this->subqueues as $q) {
2905
				$cflink['queue'][$q->GetQname()] = array();
2906
				$q->copy_queue($interface, $cflink['queue'][$q->GetQname()]);
2907
			}
2908
		}
2909
	}
2910

    
2911
	/*
2912
	 * Should search even its children
2913
	 */
2914
	function &find_queue($interface, $qname) {
2915
		if ($qname == $this->GetQname()) {
2916
			return $this;
2917
		}
2918
		foreach ($this->subqueues as $q) {
2919
			$result =& $q->find_queue("", $qname);
2920
			if ($result) {
2921
				return $result;
2922
			}
2923
		}
2924
	}
2925

    
2926
	function &find_parentqueue($interface, $qname) {
2927
		if ($this->subqueues[$qname]) {
2928
			return $this;
2929
		}
2930
		foreach ($this->subqueues as $q) {
2931
			$result = $q->find_parentqueue("", $qname);
2932
			if ($result) {
2933
				return $result;
2934
			}
2935
		}
2936
	}
2937

    
2938
	function delete_queue() {
2939
		unref_on_altq_queue_list($this->GetQname());
2940
		cleanup_queue_from_rules($this->GetQname());
2941
		foreach ($this->subqueues as $q)
2942
			$q->delete_queue();
2943
		unset_object_by_reference($this->GetLink());
2944
	}
2945

    
2946
	function validate_input($data, &$input_errors) {
2947
		parent::validate_input($data, $input_errors);
2948

    
2949
		if ($data['priority'] > 7) {
2950
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
2951
		}
2952
		$reqdfields[] = "bandwidth";
2953
		$reqdfieldsn[] = gettext("Bandwidth");
2954
		$reqdfields[] = "bandwidthtype";
2955
		$reqdfieldsn[] = gettext("Bandwidthtype");
2956

    
2957
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2958
	}
2959

    
2960
	function ReadConfig(&$q) {
2961
		parent::ReadConfig($q);
2962
		if (!empty($q['borrow'])) {
2963
			$this->SetBorrow("on");
2964
		} else {
2965
			$this->SetBorrow("");
2966
		}
2967
	}
2968

    
2969
	function build_javascript() {
2970
		return parent::build_javascript();
2971
	}
2972

    
2973
	function build_tree() {
2974
		$tree = " <li><a href=\"firewall_shaper.php?interface=" . $this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
2975
		$tree .= "\" ";
2976
		$tmpvalue = trim($this->GetDefault());
2977
		if (!empty($tmpvalue)) {
2978
			$tree .= " class=\"navlnk\"";
2979
		}
2980
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
2981
		if (is_array($this->subqueues)) {
2982
			$tree .= "<ul>";
2983
			foreach ($this->subqueues as $q) {
2984
				$tree .= $q->build_tree();
2985
			}
2986
			$tree .= "</ul>";
2987
		}
2988
		$tree .= "</li>";
2989
		return $tree;
2990
	}
2991

    
2992
	/* Even this should take children into consideration */
2993
	function build_rules(&$default = false) {
2994
		$pfq_rule = "queue ". $this->qname;
2995
		if ($this->GetInterface()) {
2996
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
2997
		}
2998
		if ($this->GetBandwidth() && $this->GetBwscale()) {
2999
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3000
		}
3001
		$tmpvalue = $this->GetQpriority();
3002
		if (is_numeric($tmpvalue)) {
3003
			$pfq_rule .= " priority " . $this->GetQpriority();
3004
		}
3005
		$tmpvalue = trim($this->GetQlimit());
3006
		if (!empty($tmpvalue)) {
3007
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3008
		}
3009
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() || $this->GetEcn() || $this->GetBorrow() || $this->GetCodel()) {
3010
			$pfq_rule .= " cbq ( ";
3011
			$tmpvalue = trim($this->GetRed());
3012
			if (!empty($tmpvalue)) {
3013
				$comma = 1;
3014
				$pfq_rule .= " red ";
3015
			}
3016
			$tmpvalue = trim($this->GetCodel());
3017
			if (!empty($tmpvalue)) {
3018
				$comma = 1;
3019
				$pfq_rule .= " codel ";
3020
			}
3021
			$tmpvalue = trim($this->GetRio());
3022
			if (!empty($tmpvalue)) {
3023
				if ($comma) {
3024
					$pfq_rule .= " ,";
3025
				}
3026
				$comma = 1;
3027
				$pfq_rule .= " rio ";
3028
			}
3029
			$tmpvalue = trim($this->GetEcn());
3030
			if (!empty($tmpvalue)) {
3031
				if ($comma) {
3032
					$pfq_rule .= " ,";
3033
				}
3034
				$comma = 1;
3035
				$pfq_rule .= " ecn ";
3036
			}
3037
			$tmpvalue = trim($this->GetDefault());
3038
			if (!empty($tmpvalue)) {
3039
				if ($comma) {
3040
					$pfq_rule .= " ,";
3041
				}
3042
				$comma = 1;
3043
				$pfq_rule .= " default ";
3044
				$default = true;
3045
			}
3046
			$tmpvalue = trim($this->GetBorrow());
3047
			if (!empty($tmpvalue)) {
3048
				if ($comma) {
3049
					$pfq_rule .= ", ";
3050
				}
3051
				$pfq_rule .= " borrow ";
3052
			}
3053
			$pfq_rule .= " ) ";
3054
		}
3055
		if (count($this->subqueues)) {
3056
			$i = count($this->subqueues);
3057
			$pfq_rule .= " { ";
3058
			foreach ($this->subqueues as $qkey => $qnone) {
3059
				if ($i > 1) {
3060
					$i--;
3061
					$pfq_rule .= " {$qkey}, ";
3062
				} else {
3063
					$pfq_rule .= " {$qkey} ";
3064
				}
3065
			}
3066
			$pfq_rule .= " } \n";
3067
			foreach ($this->subqueues as $q) {
3068
				$pfq_rule .= $q->build_rules($default);
3069
			}
3070
		}
3071

    
3072
		$pfq_rule .= " \n";
3073
		return $pfq_rule;
3074
	}
3075

    
3076
	function build_form() {
3077
		$sform = parent::build_form();
3078

    
3079
		$section = new Form_Section('NOTITLE');
3080

    
3081
		$group = new Form_Group('Bandwidth');
3082

    
3083
		$group->add(new Form_Input(
3084
			'bandwidth',
3085
			null,
3086
			'number',
3087
			$this->GetBandwidth()
3088
		));
3089

    
3090
		$group->add(new Form_Select(
3091
			'bandwidthtype',
3092
			null,
3093
			$this->GetBwscale(),
3094
			array('Kb' => 'Kbit/s',
3095
				  'Mb' => 'Mbit/s',
3096
				  'Gb' => 'Gbit/s',
3097
				  'b' => 'Bit/s',
3098
				  '%' => '%')
3099
		));
3100

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

    
3103
		$section->add($group);
3104

    
3105
		$section->addInput(new Form_Checkbox(
3106
			'borrow',
3107
			'Scheduler option',
3108
			'Borrow from other queues when available',
3109
			($this->GetBorrow() == "on")
3110
		));
3111

    
3112
		$sform->add($section);
3113

    
3114
		return $sform;
3115
	}
3116

    
3117
	function update_altq_queue_data(&$data) {
3118
		$this->ReadConfig($data);
3119
	}
3120

    
3121
	function wconfig() {
3122
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3123
		if (!is_array($cflink)) {
3124
			$cflink = array();
3125
		}
3126
		$cflink['interface'] = $this->GetInterface();
3127
		$cflink['qlimit'] = trim($this->GetQlimit());
3128
		if (empty($cflink['qlimit'])) {
3129
			unset($cflink['qlimit']);
3130
		}
3131
		$cflink['priority'] = $this->GetQpriority();
3132
		if (!is_numeric($cflink['priority'])) {
3133
			unset($cflink['priority']);
3134
		}
3135
		$cflink['name'] = $this->GetQname();
3136
		$cflink['description'] = $this->GetDescription();
3137
		if (empty($cflink['description'])) {
3138
			unset($cflink['description']);
3139
		}
3140
		$cflink['bandwidth'] = $this->GetBandwidth();
3141
		$cflink['bandwidthtype'] = $this->GetBwscale();
3142
		$cflink['enabled'] = trim($this->GetEnabled());
3143
		if (empty($cflink['enabled'])) {
3144
			unset($cflink['enabled']);
3145
		}
3146
		$cflink['default'] = trim($this->GetDefault());
3147
		if (empty($cflink['default'])) {
3148
			unset($cflink['default']);
3149
		}
3150
		$cflink['red'] = trim($this->GetRed());
3151
		if (empty($cflink['red'])) {
3152
			unset($cflink['red']);
3153
		}
3154
		$cflink['rio'] = trim($this->GetRio());
3155
		if (empty($cflink['rio'])) {
3156
			unset($cflink['rio']);
3157
		}
3158
		$cflink['ecn'] = trim($this->GetEcn());
3159
		if (empty($cflink['ecn'])) {
3160
			unset($cflink['ecn']);
3161
		}
3162
		$cflink['codel'] = trim($this->GetCodel());
3163
		if (empty($cflink['codel'])) {
3164
			unset($cflink['codel']);
3165
		}
3166
		$cflink['borrow'] = trim($this->GetBorrow());
3167
		if (empty($cflink['borrow'])) {
3168
			unset($cflink['borrow']);
3169
		}
3170
	}
3171
}
3172

    
3173
class fairq_queue extends priq_queue {
3174
	var $hogs;
3175
	var $buckets;
3176

    
3177
	function GetBuckets() {
3178
		return $this->buckets;
3179
	}
3180
	function SetBuckets($buckets) {
3181
		$this->buckets = $buckets;
3182
	}
3183
	function GetHogs() {
3184
		return $this->hogs;
3185
	}
3186
	function SetHogs($hogs) {
3187
		$this->hogs = $hogs;
3188
	}
3189
	function CanHaveChildren() {
3190
		return false;
3191
	}
3192

    
3193

    
3194
	function copy_queue($interface, &$cflink) {
3195
		$cflink['interface'] = $interface;
3196
		$cflink['qlimit'] = $this->GetQlimit();
3197
		$cflink['priority'] = $this->GetQpriority();
3198
		$cflink['name'] = $this->GetQname();
3199
		$cflink['description'] = $this->GetDescription();
3200
		$cflink['bandwidth'] = $this->GetBandwidth();
3201
		$cflink['bandwidthtype'] = $this->GetBwscale();
3202
		$cflink['enabled'] = $this->GetEnabled();
3203
		$cflink['default'] = $this->GetDefault();
3204
		$cflink['red'] = $this->GetRed();
3205
		$cflink['rio'] = $this->GetRio();
3206
		$cflink['ecn'] = $this->GetEcn();
3207
		$cflink['buckets'] = $this->GetBuckets();
3208
		$cflink['hogs'] = $this->GetHogs();
3209
	}
3210

    
3211
	/*
3212
	 * Should search even its children
3213
	 */
3214
	function &find_queue($interface, $qname) {
3215
		if ($qname == $this->GetQname()) {
3216
			return $this;
3217
		}
3218
	}
3219

    
3220
	function find_parentqueue($interface, $qname) { return; }
3221

    
3222
	function delete_queue() {
3223
		unref_on_altq_queue_list($this->GetQname());
3224
		cleanup_queue_from_rules($this->GetQname());
3225
		unset_object_by_reference($this->GetLink());
3226
	}
3227

    
3228
	function validate_input($data, &$input_errors) {
3229
		parent::validate_input($data, $input_errors);
3230

    
3231
		if ($data['priority'] > 7) {
3232
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3233
		}
3234
		$reqdfields[] = "bandwidth";
3235
		$reqdfieldsn[] = gettext("Bandwidth");
3236
		$reqdfields[] = "bandwidthtype";
3237
		$reqdfieldsn[] = gettext("Bandwidthtype");
3238

    
3239
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3240
	}
3241

    
3242
	function ReadConfig(&$q) {
3243
		parent::ReadConfig($q);
3244
		if (!empty($q['buckets'])) {
3245
			$this->SetBuckets($q['buckets']);
3246
		} else {
3247
			$this->SetBuckets("");
3248
		}
3249
		if (!empty($q['hogs']) && is_valid_shaperbw($q['hogs'])) {
3250
			$this->SetHogs($q['hogs']);
3251
		} else {
3252
			$this->SetHogs("");
3253
		}
3254
	}
3255

    
3256
	function build_javascript() {
3257
		return parent::build_javascript();
3258
	}
3259

    
3260
	function build_tree() {
3261
		$tree = " <li><a href=\"firewall_shaper.php?interface=" .
3262
		$this->GetInterface()."&amp;queue=" . htmlspecialchars($this->GetQname())."&amp;action=show";
3263
		$tree .= "\" ";
3264
		$tmpvalue = trim($this->GetDefault());
3265
		if (!empty($tmpvalue)) {
3266
			$tree .= " class=\"navlnk\"";
3267
		}
3268
		$tree .= " >" . htmlspecialchars($this->GetQname()) . "</a>";
3269
		$tree .= "</li>";
3270
		return $tree;
3271
	}
3272

    
3273
	/* Even this should take children into consideration */
3274
	function build_rules(&$default = false) {
3275
		$pfq_rule = "queue ". $this->qname;
3276
		if ($this->GetInterface()) {
3277
			$pfq_rule .= " on ".get_real_interface($this->GetInterface());
3278
		}
3279
		if ($this->GetBandwidth() && $this->GetBwscale()) {
3280
			$pfq_rule .= " bandwidth ".trim($this->GetBandwidth()).$this->GetBwscale();
3281
		}
3282
		$tmpvalue = trim($this->GetQpriority());
3283
		if (is_numeric($tmpvalue)) {
3284
			$pfq_rule .= " priority " . $this->GetQpriority();
3285
		}
3286
		$tmpvalue = trim($this->GetQlimit());
3287
		if (!empty($tmpvalue)) {
3288
			$pfq_rule .= " qlimit " . $this->GetQlimit();
3289
		}
3290
		if ($this->GetDefault() || $this->GetRed() || $this->GetRio() ||
3291
		    $this->GetEcn() || $this->GetBuckets() || $this->GetHogs() || $this->GetCodel()) {
3292
			$pfq_rule .= " fairq ( ";
3293
			$tmpvalue = trim($this->GetRed());
3294
			if (!empty($tmpvalue)) {
3295
				$comma = 1;
3296
				$pfq_rule .= " red ";
3297
			}
3298
			$tmpvalue = trim($this->GetCodel());
3299
			if (!empty($tmpvalue)) {
3300
				$comma = 1;
3301
				$pfq_rule .= " codel ";
3302
			}
3303
			$tmpvalue = trim($this->GetRio());
3304
			if (!empty($tmpvalue)) {
3305
				if ($comma) {
3306
					$pfq_rule .= " ,";
3307
				}
3308
				$comma = 1;
3309
				$pfq_rule .= " rio ";
3310
			}
3311
			$tmpvalue = trim($this->GetEcn());
3312
			if (!empty($tmpvalue)) {
3313
				if ($comma) {
3314
					$pfq_rule .= " ,";
3315
				}
3316
				$comma = 1;
3317
				$pfq_rule .= " ecn ";
3318
			}
3319
			$tmpvalue = trim($this->GetDefault());
3320
			if (!empty($tmpvalue)) {
3321
				if ($comma) {
3322
					$pfq_rule .= " ,";
3323
				}
3324
				$comma = 1;
3325
				$pfq_rule .= " default ";
3326
				$default = true;
3327
			}
3328
			$tmpvalue = trim($this->GetBuckets());
3329
			if (!empty($tmpvalue)) {
3330
				if ($comma) {
3331
					$pfq_rule .= ", ";
3332
				}
3333
				$pfq_rule .= " buckets " . $this->GetBuckets() . " ";
3334
			}
3335
			$tmpvalue = trim($this->GetHogs());
3336
			if (!empty($tmpvalue)) {
3337
				if ($comma) {
3338
					$pfq_rule .= ", ";
3339
				}
3340
				$pfq_rule .= " hogs " . $this->GetHogs() . " ";
3341
			}
3342
			$pfq_rule .= " ) ";
3343
		}
3344

    
3345
		$pfq_rule .= " \n";
3346
		return $pfq_rule;
3347
	}
3348

    
3349
	function build_form() {
3350
		$form = parent::build_form();
3351

    
3352
		$section = new Form_Section('');
3353

    
3354
		$group = new Form_Group('Bandwidth');
3355

    
3356
		$group->add(new Form_Input(
3357
			'bandwidth',
3358
			null,
3359
			'number',
3360
			$this->GetBandwidth()
3361
		));
3362

    
3363
		$group->add(new Form_Select(
3364
			'bandwidthtype',
3365
			null,
3366
			$this->GetBwscale(),
3367
			array('Kb' => 'Kbit/s',
3368
				  'Mb' => 'Mbit/s',
3369
				  'Gb' => 'Gbit/s',
3370
				  'b' => 'Bit/s',
3371
				  '%' => '%')
3372
		));
3373

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

    
3376
		$section->add($group);
3377

    
3378
		$section->addInput(new Form_Input(
3379
			'buckets',
3380
			'Scheduler specific options',
3381
			'text',
3382
			$this->GetBuckets()
3383
		))->setHelp('Number of buckets available');
3384

    
3385
		$section->addInput(new Form_Input(
3386
			'hogs',
3387
			'',
3388
			'text',
3389
			$this->GetHogs()
3390
			))->setHelp('Bandwidth limit for hosts to not saturate link');
3391

    
3392
		$form->add($section);
3393
		return $form;
3394
	}
3395

    
3396
	function update_altq_queue_data(&$data) {
3397
		$this->ReadConfig($data);
3398
	}
3399

    
3400
	function wconfig() {
3401
		$cflink =& get_reference_to_me_in_config($this->GetLink());
3402
		if (!is_array($cflink)) {
3403
			$cflink = array();
3404
		}
3405
		$cflink['interface'] = $this->GetInterface();
3406
		$cflink['qlimit'] = trim($this->GetQlimit());
3407
		if (empty($cflink['qlimit'])) {
3408
			unset($cflink['qlimit']);
3409
		}
3410
		$cflink['priority'] = trim($this->GetQpriority());
3411
		if (!is_numeric($cflink['priority'])) {
3412
			unset($cflink['priority']);
3413
		}
3414
		$cflink['name'] = $this->GetQname();
3415
		$cflink['description'] = trim($this->GetDescription());
3416
		if (empty($cflink['description'])) {
3417
			unset($cflink['description']);
3418
		}
3419
		$cflink['bandwidth'] = $this->GetBandwidth();
3420
		$cflink['bandwidthtype'] = $this->GetBwscale();
3421
		$cflink['enabled'] = $this->GetEnabled();
3422
		if (empty($cflink['enabled'])) {
3423
			unset($cflink['enabled']);
3424
		}
3425
		$cflink['default'] = trim($this->GetDefault());
3426
		if (empty($cflink['default'])) {
3427
			unset($cflink['default']);
3428
		}
3429
		$cflink['red'] = trim($this->GetRed());
3430
		if (empty($cflink['red'])) {
3431
			unset($cflink['red']);
3432
		}
3433
		$cflink['rio'] = trim($this->GetRio());
3434
		if (empty($cflink['rio'])) {
3435
			unset($cflink['rio']);
3436
		}
3437
		$cflink['ecn'] = trim($this->GetEcn());
3438
		if (empty($cflink['ecn'])) {
3439
			unset($cflink['ecn']);
3440
		}
3441
		$cflink['codel'] = trim($this->GetCodel());
3442
		if (empty($cflink['codel'])) {
3443
			unset($cflink['codel']);
3444
		}
3445
		$cflink['buckets'] = trim($this->GetBuckets());
3446
		if (empty($cflink['buckets'])) {
3447
			unset($cflink['buckets']);
3448
		}
3449
		$cflink['hogs'] = trim($this->GetHogs());
3450
		if (empty($cflink['hogs'])) {
3451
			unset($cflink['hogs']);
3452
		}
3453
	}
3454
}
3455

    
3456

    
3457
/*
3458
 * dummynet(4) wrappers.
3459
 */
3460

    
3461

    
3462
/*
3463
 * List of respective objects!
3464
 */
3465
$dummynet_pipe_list = array();
3466

    
3467
class dummynet_class {
3468
	var $qname;
3469
	var $qnumber; /* dummynet(4) uses numbers instead of names; maybe integrate with pf the same as altq does?! */
3470
	var $qlimit;
3471
	var $description;
3472
	var $qenabled;
3473
	var $link;
3474
	var $qparent; /* link to upper class so we do things easily on WF2Q+ rule creation */
3475
	var $plr;
3476

    
3477
	var $buckets;
3478
	/* mask parameters */
3479
	var $mask;
3480
	var $noerror;
3481

    
3482
	/* Accessor functions */
3483
	function SetLink($link) {
3484
		$this->link = $link;
3485
	}
3486
	function GetLink() {
3487
		return $this->link;
3488
	}
3489
	function GetMask() {
3490
		if (!isset($this->mask["type"])) {
3491
			$this->mask["type"] = "none";
3492
		}
3493
		return $this->mask;
3494
	}
3495
	function SetMask($mask) {
3496
		$this->mask = $mask;
3497
	}
3498
	function &GetParent() {
3499
		return $this->qparent;
3500
	}
3501
	function SetParent(&$parent) {
3502
		$this->qparent = &$parent;
3503
	}
3504
	function GetEnabled() {
3505
		return $this->qenabled;
3506
	}
3507
	function SetEnabled($value) {
3508
		$this->qenabled = $value;
3509
	}
3510
	function CanHaveChildren() {
3511
		return false;
3512
	}
3513
	function CanBeDeleted() {
3514
		return true;
3515
	}
3516
	function GetQname() {
3517
		return $this->qname;
3518
	}
3519
	function SetQname($name) {
3520
		$this->qname = trim($name);
3521
	}
3522
	function GetQlimit() {
3523
		return $this->qlimit;
3524
	}
3525
	function SetQlimit($limit) {
3526
		$this->qlimit = $limit;
3527
	}
3528
	function GetDescription() {
3529
		return $this->description;
3530
	}
3531
	function SetDescription($str) {
3532
		$this->description = trim($str);
3533
	}
3534
	function GetFirstime() {
3535
		return $this->firsttime;
3536
	}
3537
	function SetFirsttime($number) {
3538
		$this->firsttime = $number;
3539
	}
3540
	function GetBuckets() {
3541
		return $this->buckets;
3542
	}
3543
	function SetBuckets($buckets) {
3544
		$this->buckets = $buckets;
3545
	}
3546
	function SetNumber($number) {
3547
		$this->qnumber = $number;
3548
	}
3549
	function GetNumber() {
3550
		return $this->qnumber;
3551
	}
3552
	function GetPlr() {
3553
		return $this->plr;
3554
	}
3555
	function SetPlr($plr) {
3556
		$this->plr = $plr;
3557
	}
3558

    
3559
	function build_javascript() {
3560
		$javascript .= "<script type=\"text/javascript\">\n";
3561
		$javascript .= "//<![CDATA[\n";
3562
		$javascript .= "function enable_maskbits(enable_over) {\n";
3563
		$javascript .= "var e = document.getElementById(\"mask\");\n";
3564
		$javascript .= "if ((e.options[e.selectedIndex].text == \"none\") || enable_over) {\n";
3565
		$javascript .= "document.iform.maskbits.disabled = 1;\n";
3566
		$javascript .= "document.iform.maskbits.value = \"\";\n";
3567
		$javascript .= "document.iform.maskbitsv6.disabled = 1;\n";
3568
		$javascript .= "document.iform.maskbitsv6.value = \"\";\n";
3569
		$javascript .= "} else {\n";
3570
		$javascript .= "document.iform.maskbits.disabled = 0;\n";
3571
		$javascript .= "document.iform.maskbitsv6.disabled = 0;\n";
3572
		$javascript .= "}}\n";
3573
		$javascript .= "//]]>\n";
3574
		$javascript .= "</script>\n";
3575
		return $javascript;
3576
	}
3577

    
3578
	function validate_input($data, &$input_errors) {
3579
		$reqdfields[] = "bandwidth";
3580
		$reqdfieldsn[] = gettext("Bandwidth");
3581
		/*$reqdfields[] = "burst";
3582
		$reqdfieldsn[] = gettext("Burst"); */
3583
		$reqdfields[] = "bandwidthtype";
3584
		$reqdfieldsn[] = gettext("Bandwidthtype");
3585
		$reqdfields[] = "newname";
3586
		$reqdfieldsn[] = gettext("Name");
3587

    
3588
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3589

    
3590
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3591
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {
3592
			$input_errors[] = gettext("Packet Loss Rate must be a value between 0 and 1.");
3593
		}
3594
		if ($data['buckets'] && (!is_numeric($data['buckets']) ||
3595
		    ($data['buckets'] < 16) || ($data['buckets'] > 65535))) {
3596
			$input_errors[] = gettext("Buckets must be an integer between 16 and 65535.");
3597
		}
3598
		if ($data['qlimit'] && (!is_numeric($data['qlimit']))) {
3599
			$input_errors[] = gettext("Queue limit must be an integer");
3600
		}
3601
		if (!empty($data['newname']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['newname'])) {
3602
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3603
		}
3604
		if (!empty($data['name']) && !preg_match("/^[a-zA-Z0-9_-]+$/", $data['name'])) {
3605
			$input_errors[] = gettext("Queue names must be alphanumeric and _ or - only.");
3606
		}
3607
		if (isset($data['maskbits']) && ($data['maskbits'] <> "")) {
3608
			if ((!is_numeric($data['maskbits'])) || ($data['maskbits'] <= 0) || ($data['maskbits'] > 32)) {
3609
				$input_errors[] = gettext("IPv4 bit mask must be blank or numeric value between 1 and 32.");
3610
			}
3611
		}
3612
		if (isset($data['maskbitsv6']) && ($data['maskbitsv6'] <> "")) {
3613
			if ((!is_numeric($data['maskbitsv6'])) || ($data['maskbitsv6'] <= 0) || ($data['maskbitsv6'] > 128)) {
3614
				$input_errors[] = gettext("IPv6 bit mask must be blank or numeric value between 1 and 128.");
3615
			}
3616
		}
3617
	}
3618

    
3619
	function build_mask_rules(&$pfq_rule) {
3620
		$mask = $this->GetMask();
3621
		if (!empty($mask['type'])) {
3622
			if ($mask['type'] <> 'none') {
3623
				$pfq_rule .= " mask";
3624
			}
3625
			switch ($mask['type']) {
3626
				case 'srcaddress':
3627
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3628
						$pfq_rule .= " src-ip6 /" . $mask['bitsv6'];
3629
					} else {
3630
						$pfq_rule .= " src-ip6 /128";
3631
					}
3632
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3633
						$pfq_rule .= sprintf(" src-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3634
					} else {
3635
						$pfq_rule .= " src-ip 0xffffffff";
3636
					}
3637
					break;
3638
				case 'dstaddress':
3639
					if (!empty($mask['bitsv6']) && ($mask['bitsv6'] <> "")) {
3640
						$pfq_rule .= " dst-ip6 /" . $mask['bitsv6'];
3641
					} else {
3642
						$pfq_rule .= " dst-ip6 /128";
3643
					}
3644
					if (!empty($mask['bits']) && ($mask['bits'] <> "")) {
3645
						$pfq_rule .= sprintf(" dst-ip 0x%x", gen_subnet_mask_long($mask['bits']));
3646
					} else {
3647
						$pfq_rule .= " dst-ip 0xffffffff";
3648
					}
3649
					break;
3650
				default:
3651
					break;
3652
			}
3653
		}
3654
	}
3655

    
3656
}
3657

    
3658
class dnpipe_class extends dummynet_class {
3659
	var $delay;
3660
	var $qbandwidth = array();
3661
	var $qbandwidthtype;
3662

    
3663
	/* Limiter queue patch */
3664
    var $ecn; // ecn 'on' or 'off'
3665
    var $aqm; // key to aqm_map
3666
    var $aqm_params = array(); // AQM params
3667
    var $scheduler;	// key to scheduler_map
3668
    var $scheduler_params = array(); // AQM params
3669
	function GetAQM() {
3670
			return $this->aqm;
3671
	}
3672
	function SetAQM($aqm) {
3673
			$this->aqm = $aqm;
3674
	}
3675
	function GetAQMParameters() {
3676
			return $this->aqm_params;
3677
	}
3678
	function GetAQMParameter($parameter) {
3679
			return $this->aqm_params[$parameter];
3680
	}
3681
	function SetAQMParameter($key, $value) {
3682
			return $this->aqm_params[$key] = $value;
3683
	}
3684
	function SetAQMParameters($params) {
3685
			$this->aqm_params = $params;
3686
	}
3687
	function GetECN() {
3688
			return $this->ecn;
3689
	}
3690
	function SetECN($ecn) {
3691
			$this->ecn = $ecn;
3692
	}
3693
	function GetScheduler() {
3694
			return $this->scheduler;
3695
	}
3696
	function SetScheduler($scheduler) {
3697
			$this->scheduler = $scheduler;
3698
	}
3699
	function GetSchedulerParameters() {
3700
			return $this->scheduler_params;
3701
	}
3702
	function SetSchedulerParameters($params) {
3703
			$this->scheduler_params = $params;
3704
	}
3705
	function SetSchedulerParameter($key, $value) {
3706
			$this->scheduler_params[$key] = $value;
3707
	}
3708
	function GetSchedulerParameter($key) {
3709
			return $this->scheduler_params[$key];
3710
	}
3711
	/* End limiter queue patch */
3712

    
3713
		/* This is here to help on form building and building rules/lists */
3714
	var $subqueues = array();
3715

    
3716
	function CanHaveChildren() {
3717
		return true;
3718
	}
3719
	function SetDelay($delay) {
3720
		$this->delay = $delay;
3721
	}
3722
	function GetDelay() {
3723
		return $this->delay;
3724
	}
3725
	function delete_queue() {
3726
		cleanup_dnqueue_from_rules($this->GetQname());
3727
		foreach ($this->subqueues as $q) {
3728
			$q->delete_queue();
3729
		}
3730
		unset_dn_object_by_reference($this->GetLink());
3731
		@pfSense_ipfw_pipe("pipe delete " . $this->GetNumber());
3732
		@pfSense_ipfw_pipe("sched delete " . $this->GetNumber());
3733
	}
3734
	function GetBandwidth() {
3735
		return $this->qbandwidth;
3736
	}
3737
	function SetBandwidth($bandwidth) {
3738
		$this->qbandwidth = $bandwidth;
3739
	}
3740
	function GetBurst() {
3741
		return $this->qburst;
3742
	}
3743
	function SetBurst($burst) {
3744
		$this->qburst = $burst;
3745
	}
3746

    
3747
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
3748

    
3749
		if (!is_array($this->subqueues)) {
3750
			$this->subqueues = array();
3751
		}
3752

    
3753
		$__tmp_q = new dnqueue_class(); $q =& $__tmp_q;
3754
		$q->SetLink($path);
3755
		$q->SetEnabled("on");
3756
		$q->SetPipe($this->GetQname());
3757
		$q->SetParent($this);
3758
		$q->ReadConfig($queue);
3759
		$q->validate_input($queue, $input_errors);
3760

    
3761
		if (!is_array($input_errors)) {
3762
			$input_errors = array();
3763
		}
3764

    
3765
		if (count($input_errors)) {
3766
			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)));
3767
			return $q;
3768
		}
3769
		$number = dnqueue_find_nextnumber();
3770
		$q->SetNumber($number);
3771
		$this->subqueues[$q->GetQname()] = &$q;
3772

    
3773
		return $q;
3774
	}
3775

    
3776
	function &get_queue_list(&$q = null) {
3777
		$qlist = array();
3778

    
3779
		$qlist[$this->GetQname()] = $this->GetNumber();
3780
		if (is_array($this->subqueues)) {
3781
			foreach ($this->subqueues as $queue) {
3782
				$queue->get_queue_list($qlist);
3783
			}
3784
		}
3785
		return $qlist;
3786
	}
3787

    
3788
	/*
3789
	 * Should search even its children
3790
	 */
3791
	function &find_queue($pipe, $qname) {
3792
		if ($qname == $this->GetQname()) {
3793
			return $this;
3794
		}
3795
		foreach ($this->subqueues as $q) {
3796
			$result =& $q->find_queue("", $qname);
3797
			if ($result) {
3798
				return $result;
3799
			}
3800
		}
3801
	}
3802

    
3803
	function &find_parentqueue($pipe, $qname) {
3804
		return NULL;
3805
	}
3806

    
3807
	function validate_input($data, &$input_errors) {
3808
		parent::validate_input($data, $input_errors);
3809

    
3810
		$schedule = 0;
3811
		$schedulenone = 0;
3812
		$entries = 0;
3813
		/* XXX: Really no better way? */
3814
		for ($i = 0; $i < 2900; $i++) {
3815
			if (!empty($data["bwsched{$i}"])) {
3816
				if ($data["bwsched{$i}"] != "none") {
3817
					$schedule++;
3818
				} else {
3819
					$schedulenone++;
3820
				}
3821
			}
3822
			if (!empty($data["bandwidth{$i}"])) {
3823
				if (!is_numeric($data["bandwidth{$i}"])) {
3824
					$input_errors[] = sprintf(gettext("Bandwidth for schedule %s must be an integer."), $data["bwsched{$i}"]);
3825
				} else if (($data["burst{$i}"] != "") && (!is_numeric($data["burst{$i}"]))) {
3826
					$input_errors[] = sprintf(gettext("Burst for schedule %s must be an integer."), $data["bwsched{$i}"]);
3827
				} else {
3828
					$entries++;
3829
				}
3830
			}
3831
		}
3832
		if ($schedule == 0 && $entries > 1) {
3833
			$input_errors[] = gettext("A schedule needs to be specified for every additional entry.");
3834
		}
3835
		if ($schedulenone > 0 && $entries > 1) {
3836
			$input_errors[] = gettext("If more than one bandwidth configured all schedules need to be selected.");
3837
		}
3838
		if ($entries == 0) {
3839
			$input_errors[] = gettext("At least one bw specification is necessary.");
3840
		}
3841
		if ($data['delay'] && (!is_numeric($data['delay']))) {
3842
			$input_errors[] = gettext("Delay must be an integer.");
3843
		}
3844

    
3845
		/* Limiter patch */
3846
		$selectedScheduler = getSchedulers()[$data['sched']];
3847
		if (!$selectedScheduler) {
3848
			$input_errors[] = gettext("Selected scheduler not recognized.");
3849
		}
3850
		$selectedAqm = getAQMs()[$data['aqm']];
3851
		if (!empty($data['aqm']) && !$selectedAqm) {
3852
			$input_errors[] = gettext("Selected AQM not recognized.");
3853
		}
3854
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedScheduler["ecn"] && !$selectedAqm["ecn"]) {
3855
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but neither " . $selectedAqm["name"] . " nor " . $selectedScheduler["name"] . " support it.");
3856
		}
3857
		/* End limiter patch */
3858
	}
3859

    
3860
	function ReadConfig(&$q) {
3861
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
3862
			$this->SetQname($q['newname']);
3863
		} else if (!empty($q['newname'])) {
3864
			$this->SetQname($q['newname']);
3865
		} else {
3866
			$this->SetQname($q['name']);
3867
		}
3868
		$this->SetNumber($q['number']);
3869

    
3870
		if (!empty($_POST)) {
3871
			$bandwidth = array();
3872
			/* XXX: Really no better way? */
3873
			for ($i = 0; $i < 2900; $i++) {
3874
				if (isset($q["bandwidth{$i}"]) && $q["bandwidth{$i}"] <> "") {
3875
					$bw = array();
3876
					$bw['bw'] = $q["bandwidth{$i}"];
3877
					$bw['burst'] = $q["burst{$i}"];
3878
					if (isset($q["bwtype{$i}"]) && $q["bwtype{$i}"]) {
3879
						$bw['bwscale'] = $q["bwtype{$i}"];
3880
					}
3881
					if (isset($q["bwsched{$i}"]) && $q["bwsched{$i}"]) {
3882
						$bw['bwsched'] = $q["bwsched{$i}"];
3883
					}
3884
					$bandwidth[] = $bw;
3885
				}
3886
			}
3887
			$this->SetBandwidth($bandwidth);
3888
		}
3889

    
3890
		if (is_array($q['bandwidth']) && is_array($q['bandwidth']['item'])) {
3891
			$this->SetBandwidth($q['bandwidth']['item']);
3892
			$this->SetBurst($q['burst']['item']);
3893
		}
3894

    
3895
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
3896
			$this->SetQlimit($q['qlimit']);
3897
		} else {
3898
			$this->SetQlimit("");
3899
		}
3900
		if (isset($q['mask']) && $q['mask'] <> "") {
3901
			$masktype = $q['mask'];
3902
		} else {
3903
			$masktype = "";
3904
		}
3905
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
3906
			$maskbits = $q['maskbits'];
3907
		} else {
3908
			$maskbits = "";
3909
		}
3910
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
3911
			$maskbitsv6 = $q['maskbitsv6'];
3912
		} else {
3913
			$maskbitsv6 = "";
3914
		}
3915
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
3916
		if (isset($q['buckets']) && $q['buckets'] <> "") {
3917
			$this->SetBuckets($q['buckets']);
3918
		} else {
3919
			$this->SetBuckets("");
3920
		}
3921
		if (isset($q['plr']) && $q['plr'] <> "") {
3922
			$this->SetPlr($q['plr']);
3923
		} else {
3924
			$this->SetPlr("");
3925
		}
3926
		if (isset($q['delay']) && $q['delay'] <> "") {
3927
			$this->SetDelay($q['delay']);
3928
		} else {
3929
			$this->SetDelay(0);
3930
		}
3931
		if (isset($q['description']) && $q['description'] <> "") {
3932
			$this->SetDescription($q['description']);
3933
		} else {
3934
			$this->SetDescription("");
3935
		}
3936
		$this->SetEnabled($q['enabled']);
3937

    
3938
		/* Limiter patch */
3939
		if (isset($q['aqm']) && $q['aqm'] <> "") {
3940
				$this->SetAQM($q['aqm']);
3941
		} else {
3942
				$this->SetAQM('droptail');
3943
		}
3944
		// parse AQM arguments
3945
		$aqm_map = getAQMs();
3946
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
3947
		if (is_array($selectedParameters)) {
3948
			foreach ($selectedParameters as $key => $value) {
3949
				$config_key = 'param_' . $this->GetAQM() . '_' . $key;
3950
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
3951
					$this->SetAQMParameter($key, $q[$config_key]);
3952
				} else {
3953
					$this->SetAQMParameter($key, $value["default"]);
3954
				}
3955
			}
3956
		}
3957

    
3958
		if (isset($q['sched']) && $q['sched'] <> "") {
3959
				$this->SetScheduler($q['sched']);
3960
		} else {
3961
				$this->SetScheduler('fifo');
3962
		}
3963
		$scheduler_map = getSchedulers();
3964
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
3965
		if (is_array($selectedParameters)) {
3966
			foreach ($selectedParameters as $key => $value) {
3967
				$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
3968
				if (isset($q[$config_key]) && $q[$config_key] <> "") {
3969
					$this->SetSchedulerParameter($key, $q[$config_key]);
3970
				} else {
3971
					$this->SetSchedulerParameter($key, $value["default"]);
3972
				}
3973
			}
3974
		}
3975

    
3976
		// ecn flag
3977
		$this->SetECN($q['ecn']);
3978
		/* End limiter patch */
3979
	}
3980

    
3981
	function build_tree() {
3982
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($this->GetQname()) ."&amp;queue=".htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
3983
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
3984
		if (is_array($this->subqueues)) {
3985
			$tree .= "<ul>";
3986
			foreach ($this->subqueues as $q) {
3987
				$tree .= $q->build_tree();
3988
			}
3989
			$tree .= "</ul>";
3990
		}
3991
		$tree .= "</li>";
3992

    
3993
		return $tree;
3994
	}
3995

    
3996
	function build_rules() {
3997
		global $config, $time_based_rules;
3998

    
3999
		if ($this->GetEnabled() == "") {
4000
			return;
4001
		}
4002

    
4003
		$pfq_rule = "\npipe ". $this->GetNumber() . " config ";
4004
		$found = false;
4005
		$bandwidth = $this->GetBandwidth();
4006
		if (is_array($bandwidth)) {
4007
			foreach ($bandwidth as $bw) {
4008
				if ($bw['bwsched'] != "none") {
4009
					$time_based_rules = true;
4010
					if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4011
						foreach ($config['schedules']['schedule'] as $schedule) {
4012
							if ($bw['bwsched'] == $schedule['name']) {
4013
								if (filter_get_time_based_rule_status($schedule)) {
4014
									/* pipe throughputs must always be an integer, enforce that restriction again here. */
4015
									$pfq_rule .= " bw ".round(trim($bw['bw']),0).$bw['bwscale'];
4016
									if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4017
										$pfq_rule .= " burst ".trim($bw['burst']);
4018
									}
4019
									$found = true;
4020
									break;
4021
								}
4022
							}
4023
						}
4024
					} else {
4025
						$pfq_rule .= " bw 0";
4026
						$found = true;
4027
						break;
4028
					}
4029
				} else {
4030
					/* pipe throughputs must always be an integer, enforce that restriction again here. */
4031
					$pfq_rule .= " bw ".round(trim($bw['bw']), 0).$bw['bwscale'];
4032
					if (is_numeric($bw['burst']) && ($bw['burst'] > 0)) {
4033
						$pfq_rule .= " burst ".trim($bw['burst']);
4034
					}
4035
					$found = true;
4036
					break;
4037
				}
4038
			}
4039
			if ($found == false) {
4040
				$pfq_rule .= " bw 0";
4041
			}
4042
		} else {
4043
			$pfq_rule .= " bw 0";
4044
		}
4045

    
4046
		if ($this->GetQlimit()) {
4047
			$pfq_rule .= " queue " . $this->GetQlimit();
4048
		}
4049
		if ($this->GetPlr()) {
4050
			$pfq_rule .= " plr " . $this->GetPlr();
4051
		}
4052
		if ($this->GetBuckets()) {
4053
			$pfq_rule .= " buckets " . $this->GetBuckets();
4054
		}
4055
		if ($this->GetDelay()) {
4056
			$pfq_rule .= " delay " . $this->GetDelay();
4057
		}
4058
		$this->build_mask_rules($pfq_rule);
4059

    
4060
		/* Limiter patch */
4061
		$selectedAQM = getAQMs()[$this->getAQM()];
4062
		if ($selectedAQM) {
4063
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4064
			if ($selectedAQM["ecn"]) {
4065
				if ($this->getECN() == 'on') {
4066
					$pfq_rule .= ' ecn';
4067
				} else {
4068
					$pfq_rule .= ' noecn';
4069
				}
4070
			}
4071
		}
4072
		$selectedScheduler = getSchedulers()[$this->getScheduler()];
4073
		if ($selectedScheduler) {
4074
			$pfq_rule .= "\nsched ". $this->GetNumber() . " config pipe " . $this->GetNumber() . " " . FormatParameters($selectedScheduler["parameter_format"], $this->GetSchedulerParameters());
4075
			if ($selectedScheduler["ecn"]) {
4076
				if ($this->getECN() == 'on') {
4077
					$pfq_rule .= ' ecn';
4078
				} else {
4079
					$pfq_rule .= ' noecn';
4080
				}
4081
			}
4082
		}
4083
		$pfq_rule .= "\n";
4084
		/* End patch */
4085

    
4086
		if (!empty($this->subqueues) && count($this->subqueues) > 0) {
4087
			foreach ($this->subqueues as $q) {
4088
				$pfq_rule .= $q->build_rules();
4089
			}
4090
		}
4091

    
4092
		$pfq_rule .= " \n";
4093

    
4094
		return $pfq_rule;
4095
	}
4096

    
4097
	function update_dn_data(&$data) {
4098
		$this->ReadConfig($data);
4099
	}
4100

    
4101
	function build_javascript() {
4102
		global $g, $config;
4103

    
4104
		$javasr = parent::build_javascript();
4105

    
4106
		//build list of schedules
4107
		$schedules = "<option value='none'>none</option>";
4108
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4109
			foreach ($config['schedules']['schedule'] as $schedule) {
4110
				if ($schedule['name'] <> "") {
4111
					$schedules .= "<option value='{$schedule['name']}'>{$schedule['name']}</option>";
4112
				}
4113
			}
4114
		}
4115
		$bwopt = "";
4116
		foreach (array("Kb" => "Kbit/s", "Mb" => "Mbit/s", "Gb" => "Gbit/s", "b" => "Bit/s") as $bwidx => $bw) {
4117
			$bwopt .= "<option value='{$bwidx}'>{$bw}</option>";
4118
		}
4119

    
4120
		$javasr .= <<<EOD
4121
<script type='text/javascript'>
4122
//<![CDATA[
4123
var addBwRowTo = (function() {
4124

    
4125
	return (function (tableId) {
4126

    
4127
	var table = document.getElementById(tableId);
4128
	var totalrows = table.rows.length -1;
4129

    
4130
	var row = table.insertRow(totalrows + 1);
4131
	var cell1 = row.insertCell(0);
4132
	var cell2 = row.insertCell(1);
4133
	var cell3 = row.insertCell(2);
4134
	var cell4 = row.insertCell(3);
4135

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

    
4141
	});
4142
})();
4143

    
4144
function removeBwRow(el) {
4145
	var d = el.parentNode.parentNode.rowIndex;
4146
	document.getElementById('maintable').deleteRow(d);
4147
}
4148

    
4149
function ceil_func(el){
4150
	el.value = Math.ceil(el.value);
4151

    
4152
}
4153
//]]>
4154
</script>
4155

    
4156
EOD;
4157

    
4158
		return $javasr;
4159
	}
4160

    
4161
	// Compose a table of bandwidths that can then be inserted into the form using a Form_StaticText
4162
	// The table has been "Bootstrapped" to match the web design while maintaining compatibility with
4163
	// with the javascript in this class
4164
	function build_bwtable() {
4165
		global $config;
4166

    
4167
		$bandwidth = $this->GetBandwidth();
4168
				//build list of schedules
4169
		$schedules = array();
4170
		$schedules[] = "none";//leave none to leave rule enabled all the time
4171
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4172
			foreach ($config['schedules']['schedule'] as $schedule) {
4173
				if ($schedule['name'] != "") {
4174
					$schedules[] = $schedule['name'];
4175
				}
4176
			}
4177
		}
4178

    
4179
		$form = '<div class="table-responsive">';
4180
		$form .= '<table id="maintable" class="table table-hover table-striped">';
4181
		$form .= "<thead><tr>";
4182
		$form .= "<th>Bandwidth</th>";
4183
		//$form .= "<td width='35%'><div id='fifthcolumn'>Burst</div></td>";
4184
		$form .= "<th>Bw type</th>";
4185
		$form .= "<th>Schedule</th>";
4186
		$form .= "<th></th>";
4187
		$form .= "</tr></thead>";
4188
		$form .= "<tbody>";
4189

    
4190
		// If there are no bandwidths defined, make a blank one for convenience
4191
		if (empty($bandwidth)) {
4192
			$bandwidth = array(0 => array('bw' => '', 'bwscale' => 'Kb', 'bwsched' => 'none'));
4193
		}
4194

    
4195
		if (is_array($bandwidth)) {
4196
			foreach ($bandwidth as $bwidx => $bw) {
4197
				$form .= '<tr>';
4198
				$form .= '<td class="col-xs-4">';
4199
				$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\"/>";
4200
				//$form .= "</td><td width='20%'>";
4201
				//$form .= "<input class='formfld unknown' size='10' type=\"text\" id=\"burst{$bwidx}\" name=\"burst{$bwidx}\" value=\"{$bw['burst']}\" />";
4202
				$form .= "</td>";
4203
				$form .= '<td class="col-xs-4">';
4204
				$form .= "<select id=\"bwtype{$bwidx}\" name=\"bwtype{$bwidx}\" class=\"form-control\">";
4205

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

    
4209
					if ($bw['bwscale'] == $bwsidx) {
4210
						$form .= " selected";
4211
					}
4212

    
4213
					$form .= ">{$bwscale}</option>";
4214
				}
4215

    
4216
				$form .= "</select>";
4217
				$form .= "</td>";
4218
				$form .= '<td class="col-xs-4">';
4219
				$form .= "<select id=\"bwsched{$bwidx}\" name=\"bwsched{$bwidx}\" class=\"form-control\">";
4220

    
4221
				foreach ($schedules as $schd) {
4222
					$selected = "";
4223
					if ($bw['bwsched'] == $schd) {
4224
						$selected = "selected";
4225
					}
4226

    
4227
					$form .= "<option value='{$schd}' {$selected}>{$schd}</option>";
4228
				}
4229

    
4230
				$form .= "</select>";
4231
				$form .= "</td>";
4232
				$form .= '<td>';
4233
				$form .= '<a class="btn btn-warning" onclick="removeBwRow(this); return false;"><i class="fa fa-trash icon-embed-btn"></i>' . gettext('Delete') . '</a>';
4234
				$form .= "</td></tr>";
4235
			}
4236
		}
4237
		$form .= "</tbody></table></div><br />";
4238

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

    
4243
		return($form);
4244
	}
4245

    
4246
	function build_form() {
4247
		global $g, $config, $pipe, $action, $qname;
4248

    
4249
		//build list of schedules
4250
		$schedules = array();
4251
		$schedules[] = "none";//leave none to leave rule enabled all the time
4252
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4253
			foreach ($config['schedules']['schedule'] as $schedule) {
4254
				if ($schedule['name'] <> "") {
4255
					$schedules[] = $schedule['name'];
4256
				}
4257
			}
4258
		}
4259

    
4260

    
4261
		$sform = new Form();
4262
		$sform->setAction("firewall_shaper.php");
4263

    
4264
		$section = new Form_Section('Limiters');
4265

    
4266
		$section->addInput(new Form_Checkbox(
4267
			'enabled',
4268
			'Enable',
4269
			'Enable limiter and its children',
4270
			($this->GetEnabled() == "on"),
4271
			'on'
4272
		));
4273

    
4274
		$section->addInput(new Form_Input(
4275
			'newname',
4276
			'*Name',
4277
			'text',
4278
			$this->GetQname()
4279
		));
4280

    
4281
		$section->addInput(new Form_Input(
4282
			'name',
4283
			null,
4284
			'hidden',
4285
			$this->GetQname()
4286
		));
4287

    
4288
		if ($this->GetNumber() > 0) {
4289
			$section->addInput(new Form_Input(
4290
				'number',
4291
				null,
4292
				'hidden',
4293
				$this->GetNumber()
4294
			));
4295
		}
4296

    
4297
		$bandwidth = $this->GetBandwidth();
4298

    
4299
		if (is_array($bandwidth)) {
4300
				$section->addInput(new Form_StaticText(
4301
				'Bandwidth',
4302
				$this->build_bwtable()
4303
			));
4304
		}
4305

    
4306
		$mask = $this->GetMask();
4307

    
4308
		$section->addInput(new Form_Select(
4309
			'mask',
4310
			'Mask',
4311
			$mask['type'],
4312
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4313
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4314
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4315
					'This makes it possible to easily specify bandwidth limits per host.');
4316

    
4317
		$group = new Form_Group(null);
4318

    
4319
		$group->add(new Form_Select(
4320
			'maskbits',
4321
			null,
4322
			$mask['bits'],
4323
			array_combine(range(32, 1, -1), range(32, 1, -1))
4324
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4325

    
4326
		$group->add(new Form_Select(
4327
			'maskbitsv6',
4328
			null,
4329
			$mask['bitsv6'],
4330
			array_combine(range(128, 1, -1), range(128, 1, -1))
4331
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4332

    
4333
		$section->add($group);
4334

    
4335
		$section->addInput(new Form_Input(
4336
			'description',
4337
			'Description',
4338
			'text',
4339
			$this->GetDescription()
4340
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4341

    
4342
		$sform->add($section);
4343

    
4344
		/* Begin limiter patch */
4345
		$aqm_map = getAQMs();
4346
		$scheduler_map = getSchedulers();
4347

    
4348
		$section = new Form_Section('Queue');
4349
		$section->addInput(new Form_Select(
4350
				'aqm',
4351
				'Queue Management Algorithm',
4352
				$this->GetAQM(),
4353
				array_map_assoc(function ($k, $v) {
4354
					return [$k, $v["name"]];
4355
				}, $aqm_map)
4356
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside the limiter, ' .
4357
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4358
						'network congestion.');
4359

    
4360
		$section->addInput(new Form_StaticText(
4361
			'',
4362
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4363
		))->setHelp('Specifies the queue management algorithm parameters.');
4364

    
4365
		$section->addInput(new Form_Select(
4366
				'sched',
4367
				'Scheduler',
4368
				$this->GetScheduler(),
4369
				array_map_assoc(function ($k, $v) {
4370
					return [$k, $v["name"]];
4371
				}, $scheduler_map)
4372
		))->setHelp('The scheduler manages the sequence of network packets in the limiter\'s queue.');
4373

    
4374
		$section->addInput(new Form_StaticText(
4375
			'',
4376
			build_queue_params($scheduler_map, $this->GetScheduler(), $this->GetSchedulerParameters(), "sched")
4377
		))->setHelp('Specifies the scheduler parameters.');
4378

    
4379
		$section->addInput(new Form_Input(
4380
				'qlimit',
4381
				'Queue length',
4382
				'number',
4383
				$this->GetQlimit()
4384
		))->setHelp('Specifies the length of the limiter\'s queue, which the scheduler and AQM are responsible for. ' .
4385
			'This field may be left empty.');
4386

    
4387
		$section->addInput(new Form_Checkbox(
4388
			'ecn',
4389
			'ECN',
4390
			'Enable Explicit Congestion Notification (ECN)',
4391
			($this->GetECN() == "on"),
4392
			'on'
4393
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4394

    
4395
		$sform->add($section);
4396
		/* End limiter patch */
4397

    
4398
		$section = new Form_Section('Advanced Options');
4399

    
4400
		$section->addInput(new Form_Input(
4401
			'delay',
4402
			'Delay (ms)',
4403
			'text',
4404
			$this->GetDelay() > 0 ? $this->GetDelay():null
4405
		))->setHelp('In most cases, zero (0) should specified here (or leave the field empty).');
4406

    
4407
		$section->addInput(new Form_Input(
4408
			'plr',
4409
			'Packet Loss Rate',
4410
			'number',
4411
			$this->GetPlr(),
4412
			['step' => '0.001', 'min' => '0.000']
4413
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4414
					'A value of 0.001 means one packet in 1000 gets dropped.');
4415

    
4416
		$section->addInput(new Form_Input(
4417
			'buckets',
4418
			'Bucket size (slots)',
4419
			'number',
4420
			$this->GetBuckets()
4421
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set.');
4422

    
4423
		$sform->add($section);
4424

    
4425
		return($sform);
4426
		}
4427

    
4428
	function wconfig() {
4429
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4430
		if (!is_array($cflink)) {
4431
			$cflink = array();
4432
		}
4433
		$cflink['name'] = $this->GetQname();
4434
		$cflink['number'] = $this->GetNumber();
4435
		$cflink['qlimit'] = $this->GetQlimit();
4436
		$cflink['plr'] = $this->GetPlr();
4437
		$cflink['description'] = $this->GetDescription();
4438

    
4439
		$bandwidth = $this->GetBandwidth();
4440
		if (is_array($bandwidth)) {
4441
			$cflink['bandwidth'] = array();
4442
			$cflink['bandwidth']['item'] = array();
4443
			foreach ($bandwidth as $bwidx => $bw) {
4444
				$cflink['bandwidth']['item'][] = $bw;
4445
			}
4446
		}
4447

    
4448
		$cflink['enabled'] = $this->GetEnabled();
4449
		$cflink['buckets'] = $this->GetBuckets();
4450
		$mask = $this->GetMask();
4451
		$cflink['mask'] = $mask['type'];
4452
		$cflink['maskbits'] = $mask['bits'];
4453
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4454
		$cflink['delay'] = $this->GetDelay();
4455

    
4456
		/* Limiter queue patch */
4457
		$cflink['sched'] = $this->GetScheduler();
4458
		$scheduler_map = getSchedulers();
4459
		$selectedParameters = $scheduler_map[$this->getScheduler()]["parameters"];
4460
		foreach ($selectedParameters as $key => $value) {
4461
			$config_key = 'param_' . $this->GetScheduler() . '_' . $key;
4462
			$cflink[$config_key] = $this->GetSchedulerParameter($key);
4463
		}
4464
		$cflink['aqm'] = $this->GetAQM();
4465
		$aqm_map = GetAQMs();
4466
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4467
		foreach ($selectedParameters as $key => $value) {
4468
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4469
			$cflink[$config_key] = $this->GetAQMParameter($key);
4470
		}
4471
		$cflink['ecn'] = $this->GetECN();
4472
		/* End limiter queue patch */
4473
	}
4474

    
4475
}
4476

    
4477
class dnqueue_class extends dummynet_class {
4478
	var $pipeparent;
4479
	var $weight;
4480
	/* Limiter queue patch */
4481
    var $ecn; // ecn 'on' or 'off'
4482
    var $aqm; // key to aqm_map
4483
    var $aqm_params = array(); // AQM params
4484
	function GetAQM() {
4485
			return $this->aqm;
4486
	}
4487
	function SetAQM($aqm) {
4488
			$this->aqm = $aqm;
4489
	}
4490
	function GetAQMParameters() {
4491
			return $this->aqm_params;
4492
	}
4493
	function GetAQMParameter($parameter) {
4494
			return $this->aqm_params[$parameter];
4495
	}
4496
	function SetAQMParameter($key, $value) {
4497
			return $this->aqm_params[$key] = $value;
4498
	}
4499
	function SetAQMParameters($params) {
4500
			$this->aqm_params = $params;
4501
	}
4502
	function GetECN() {
4503
			return $this->ecn;
4504
	}
4505
	function SetECN($ecn) {
4506
			$this->ecn = $ecn;
4507
	}
4508
	/* End limiter queue patch */
4509

    
4510
	function GetWeight() {
4511
		return $this->weight;
4512
	}
4513
	function SetWeight($weight) {
4514
		$this->weight = $weight;
4515
	}
4516
	function GetPipe() {
4517
		return $this->pipeparent;
4518
	}
4519
	function SetPipe($pipe) {
4520
		$this->pipeparent = $pipe;
4521
	}
4522

    
4523
	/* Just a stub in case we ever try to call this from the frontend. */
4524
	function &add_queue($interface, &$queue, &$path, &$input_errors) {
4525
		return;
4526
	}
4527

    
4528
	function delete_queue() {
4529
		cleanup_dnqueue_from_rules($this->GetQname());
4530
		unset_dn_object_by_reference($this->GetLink());
4531
		@pfSense_ipfw_pipe("queue delete " . $this->GetNumber());
4532
	}
4533

    
4534
	function validate_input($data, &$input_errors) {
4535
		parent::validate_input($data, $input_errors);
4536

    
4537

    
4538
		/* Limiter patch */
4539
		$selectedAqm = getAQMs()[$data['aqm']];
4540
		if (!empty($data['aqm']) && !$selectedAqm) {
4541
			$input_errors[] = gettext("Selected AQM not recognized.");
4542
		}
4543
		if ($data['ecn'] && $data['ecn'] == 'on' && !$selectedAqm["ecn"]) {
4544
			$input_errors[] = gettext("Explicit Congestion Notification is selected, but " . $selectedAqm["name"] . " does not support it.");
4545
		}
4546
		/* End limiter patch */
4547

    
4548
		if ($data['weight'] && ((!is_numeric($data['weight'])) ||
4549
		    ($data['weight'] < 1 && $data['weight'] > 100))) {
4550
			$input_errors[] = gettext("Weight must be an integer between 1 and 100.");
4551
		}
4552
	}
4553

    
4554
	/*
4555
	 * Should search even its children
4556
	 */
4557
	function &find_queue($pipe, $qname) {
4558
		if ($qname == $this->GetQname()) {
4559
			return $this;
4560
		} else {
4561
			return NULL;
4562
		}
4563
	}
4564

    
4565
	function &find_parentqueue($pipe, $qname) {
4566
		return $this->qparent;
4567
	}
4568

    
4569
	function &get_queue_list(&$qlist) {
4570
		if ($this->GetEnabled() == "") {
4571
			return;
4572
		}
4573
		$qlist[$this->GetQname()] = "?" .$this->GetNumber();
4574
	}
4575

    
4576
	function ReadConfig(&$q) {
4577
		if (!empty($q['name']) && !empty($q['newname']) && $q['name'] != $q['newname']) {
4578
			$this->SetQname($q['newname']);
4579
		} else if (!empty($q['newname'])) {
4580
			$this->SetQname($q['newname']);
4581
		} else {
4582
			$this->SetQname($q['name']);
4583
		}
4584
		$this->SetNumber($q['number']);
4585
		if (isset($q['qlimit']) && $q['qlimit'] <> "") {
4586
			$this->SetQlimit($q['qlimit']);
4587
		} else {
4588
			$this->SetQlimit("");
4589
		}
4590
		if (isset($q['mask']) && $q['mask'] <> "") {
4591
			$masktype = $q['mask'];
4592
		} else {
4593
			$masktype = "";
4594
		}
4595
		if (isset($q['maskbits']) && $q['maskbits'] <> "") {
4596
			$maskbits = $q['maskbits'];
4597
		} else {
4598
			$maskbits = "";
4599
		}
4600
		if (isset($q['maskbitsv6']) && $q['maskbitsv6'] <> "") {
4601
			$maskbitsv6 = $q['maskbitsv6'];
4602
		} else {
4603
			$maskbitsv6 = "";
4604
		}
4605
		$this->SetMask(array("type" => $masktype, "bits" => $maskbits, "bitsv6" => $maskbitsv6));
4606
		if (isset($q['buckets']) && $q['buckets'] <> "") {
4607
			$this->SetBuckets($q['buckets']);
4608
		} else {
4609
			$this->SetBuckets("");
4610
		}
4611
		if (isset($q['plr']) && $q['plr'] <> "") {
4612
			$this->SetPlr($q['plr']);
4613
		} else {
4614
			$this->SetPlr("");
4615
		}
4616
		if (isset($q['weight']) && $q['weight'] <> "") {
4617
			$this->SetWeight($q['weight']);
4618
		} else {
4619
			$this->SetWeight("");
4620
		}
4621
		if (isset($q['description']) && $q['description'] <> "") {
4622
			$this->SetDescription($q['description']);
4623
		} else {
4624
			$this->SetDescription("");
4625
		}
4626
		$this->SetEnabled($q['enabled']);
4627

    
4628
		/* Limiter patch */
4629
		if (isset($q['aqm']) && $q['aqm'] <> "") {
4630
				$this->SetAQM($q['aqm']);
4631
		} else {
4632
				$this->SetAQM('droptail');
4633
		}
4634
		// parse AQM arguments
4635
		$aqm_map = getAQMs();
4636
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4637
		foreach ($selectedParameters as $key => $value) {
4638
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4639
			if (isset($q[$config_key]) && $q[$config_key] <> "") {
4640
				$this->SetAQMParameter($key, $q[$config_key]);
4641
			} else {
4642
				$this->SetAQMParameter($key, $value["default"]);
4643
			}
4644
		}
4645

    
4646
		// ecn flag
4647
		$this->SetECN($q['ecn']);
4648
		/* End limiter patch */
4649
	}
4650

    
4651
	function build_tree() {
4652
		$parent =& $this->GetParent();
4653
		$tree = " <li><a href=\"firewall_shaper_vinterface.php?pipe=" . htmlspecialchars($parent->GetQname()) ."&amp;queue=" . htmlspecialchars($this->GetQname()) ."&amp;action=show\">";
4654
		$tree .= htmlspecialchars($this->GetQname()) . "</a>";
4655
		$tree .= "</li>";
4656

    
4657
		return $tree;
4658
	}
4659

    
4660
	function build_rules() {
4661
		if ($this->GetEnabled() == "") {
4662
			return;
4663
		}
4664

    
4665
		$parent =& $this->GetParent();
4666
		$pfq_rule = "queue ". $this->GetNumber() . " config pipe " . $parent->GetNumber();
4667
		if ($this->GetQlimit()) {
4668
			$pfq_rule .= " queue " . $this->GetQlimit();
4669
		}
4670
		if ($this->GetWeight()) {
4671
			$pfq_rule .= " weight " . $this->GetWeight();
4672
		}
4673
		if ($this->GetBuckets()) {
4674
			$pfq_rule .= " buckets " . $this->GetBuckets();
4675
		}
4676
		$this->build_mask_rules($pfq_rule);
4677

    
4678
		/* Limiter patch */
4679
		$selectedAQM = getAQMs()[$this->getAQM()];
4680
		if ($selectedAQM) {
4681
			$pfq_rule .= " " . FormatParameters($selectedAQM["parameter_format"], $this->GetAQMParameters());
4682
			if ($selectedAQM["ecn"]) {
4683
				if ($this->getECN() == 'on') {
4684
					$pfq_rule .= ' ecn';
4685
				} else {
4686
					$pfq_rule .= ' noecn';
4687
				}
4688
			}
4689
		}
4690
		/* End patch */
4691

    
4692
		$pfq_rule .= "\n";
4693

    
4694
		return $pfq_rule;
4695
	}
4696

    
4697
	function build_javascript() {
4698
		return parent::build_javascript();
4699
	}
4700

    
4701
	function build_form() {
4702
		global $g, $config, $pipe, $action, $qname;
4703

    
4704
		//build list of schedules
4705
		$schedules = array();
4706
		$schedules[] = "none";//leave none to leave rule enabled all the time
4707
		if (is_array($config['schedules']) && is_array($config['schedules']['schedule'])) {
4708
			foreach ($config['schedules']['schedule'] as $schedule) {
4709
				if ($schedule['name'] <> "") {
4710
					$schedules[] = $schedule['name'];
4711
				}
4712
			}
4713
		}
4714

    
4715

    
4716
		$sform = new Form();
4717
		$sform->setAction("firewall_shaper.php");
4718
		$section = new Form_Section('Limiters');
4719

    
4720
		$section->addInput(new Form_Checkbox(
4721
			'enabled',
4722
			'Enable',
4723
			'Enable this queue',
4724
			($this->GetEnabled() == "on"),
4725
			'on'
4726
		));
4727

    
4728
		$section->addInput(new Form_Input(
4729
			'newname',
4730
			'*Name',
4731
			'text',
4732
			$this->GetQname()
4733
		));
4734

    
4735
		$section->addInput(new Form_Input(
4736
			'name',
4737
			null,
4738
			'hidden',
4739
			$this->GetQname()
4740
		));
4741

    
4742
		if ($this->GetNumber() > 0) {
4743
			$section->addInput(new Form_Input(
4744
				'number',
4745
				null,
4746
				'hidden',
4747
				$this->GetNumber()
4748
			));
4749
		}
4750

    
4751
		$mask = $this->GetMask();
4752

    
4753
		$section->addInput(new Form_Select(
4754
			'mask',
4755
			'Mask',
4756
			$mask['type'],
4757
			array('none' => gettext('None'), 'srcaddress' => gettext('Source addresses'), 'dstaddress' => gettext('Destination addresses'))
4758
		))->setHelp('If "source" or "destination" slots is chosen a dynamic pipe with the bandwidth, delay, packet loss ' .
4759
					'and queue size given above will be created for each source/destination IP address encountered, respectively. ' .
4760
					'This makes it possible to easily specify bandwidth limits per host.');
4761

    
4762
		$group = new Form_Group(null);
4763

    
4764
		$group->add(new Form_Select(
4765
			'maskbits',
4766
			null,
4767
			$mask['bits'],
4768
			array_combine(range(32, 1, -1), range(32, 1, -1))
4769
		))->setHelp('IPv4 mask bits%1$s%2$s', '<br />', '255.255.255.255/?');
4770

    
4771
		$group->add(new Form_Select(
4772
			'maskbitsv6',
4773
			null,
4774
			$mask['bitsv6'],
4775
			array_combine(range(128, 1, -1), range(128, 1, -1))
4776
		))->setHelp('IPv6 mask bits%1$s%2$s', '<br />', '<span style="font-family:consolas">ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/?</span>');
4777

    
4778
		$section->add($group);
4779

    
4780
		$section->addInput(new Form_Input(
4781
			'description',
4782
			'Description',
4783
			'text',
4784
			$this->GetDescription()
4785
		))->setHelp('A description may be entered here for administrative reference (not parsed).');
4786

    
4787
		$sform->add($section);
4788

    
4789
		/* Begin limiter patch */
4790
		$aqm_map = getAQMs();
4791

    
4792
		$section = new Form_Section('Queue');
4793
		$section->addInput(new Form_Select(
4794
				'aqm',
4795
				'Queue Management Algorithm',
4796
				$this->GetAQM(),
4797
				array_map_assoc(function ($k, $v) {
4798
					return [$k, $v["name"]];
4799
				}, $aqm_map)
4800
		))->setHelp('Active queue management (AQM) is the intelligent drop of network packets inside this limiter\'s queue, ' .
4801
						'when it becomes full or gets close to becoming full, with the goal of reducing ' .
4802
						'network congestion.');
4803

    
4804
		$section->addInput(new Form_StaticText(
4805
			'',
4806
			build_queue_params($aqm_map, $this->GetAQM(), $this->GetAQMParameters(), "aqm")
4807
		))->setHelp('Specifies the queue management algorithm parameters.');
4808

    
4809
		$section->addInput(new Form_Input(
4810
				'qlimit',
4811
				'Queue length',
4812
				'number',
4813
				$this->GetQlimit()
4814
		))->setHelp('Specifies the length of this queue, which the AQM is responsible for.  This field may be left empty.');
4815

    
4816
		$section->addInput(new Form_Checkbox(
4817
			'ecn',
4818
			'ECN',
4819
			'Enable Explicit Congestion Notification (ECN)',
4820
			($this->GetECN() == "on"),
4821
			'on'
4822
		))->setHelp('ECN sets a reserved TCP flag when the queue is nearing or exceeding capacity. Not all AQMs or schedulers support this.');
4823

    
4824
		$sform->add($section);
4825
		/* End limiter patch */
4826

    
4827
		$section = new Form_Section('Advanced Options');
4828

    
4829
		$section->addInput(new Form_Input(
4830
			'weight',
4831
			'Weight',
4832
			'number',
4833
			$this->GetWeight(),
4834
			['min' => '1', 'max' => '100']
4835
		))->setHelp('For queues under the same parent this specifies the share that a queue gets(values range from 1 to 100),' .
4836
					' it can be left blank otherwise.');
4837

    
4838
		$section->addInput(new Form_Input(
4839
			'plr',
4840
			'Packet Loss Rate',
4841
			'number',
4842
			$this->GetPlr(),
4843
			['step' => '0.001', 'min' => '0.000']
4844
		))->setHelp('In most cases, zero (0) should be specified here (or leave the field empty). ' .
4845
					'A value of 0.001 means one packet in 1000 gets dropped');
4846

    
4847
		$section->addInput(new Form_Input(
4848
			'buckets',
4849
			'Bucket size (slots)',
4850
			'number',
4851
			$this->GetBuckets()
4852
		))->setHelp('In most cases, this field should be left empty. It increases the hash size set');
4853

    
4854
		$section->addInput(new Form_Input(
4855
			'pipe',
4856
			null,
4857
			'hidden',
4858
			$this->GetPipe()
4859
		));
4860

    
4861
		$sform->add($section);
4862

    
4863
		return($sform);
4864
	}
4865

    
4866
	function update_dn_data(&$data) {
4867
		$this->ReadConfig($data);
4868
	}
4869

    
4870
	function wconfig() {
4871
		$cflink =& get_dn_reference_to_me_in_config($this->GetLink());
4872
		if (!is_array($cflink)) {
4873
			$cflink = array();
4874
		}
4875
		$cflink['name'] = $this->GetQname();
4876
		$cflink['number'] = $this->GetNumber();
4877
		$cflink['qlimit'] = $this->GetQlimit();
4878
		$cflink['description'] = $this->GetDescription();
4879
		$cflink['weight'] = $this->GetWeight();
4880
		$cflink['enabled'] = $this->GetEnabled();
4881
		$cflink['buckets'] = $this->GetBuckets();
4882
		$mask = $this->GetMask();
4883
		$cflink['mask'] = $mask['type'];
4884
		$cflink['maskbits'] = $mask['bits'];
4885
		$cflink['maskbitsv6'] = $mask['bitsv6'];
4886

    
4887
		/* Limiter queue patch */
4888
		$cflink['aqm'] = $this->GetAQM();
4889
		$aqm_map = GetAQMs();
4890
		$selectedParameters = $aqm_map[$this->getAQM()]["parameters"];
4891
		foreach ($selectedParameters as $key => $value) {
4892
			$config_key = 'param_' . $this->GetAQM() . '_' . $key;
4893
			$cflink[$config_key] = $this->GetAQMParameter($key);
4894
		}
4895
		$cflink['ecn'] = $this->GetECN();
4896
		/* End limiter queue patch */
4897
	}
4898
}
4899

    
4900
function get_dummynet_name_list() {
4901

    
4902
	$dn_name_list =& get_unique_dnqueue_list();
4903
	$dn_name = array();
4904
	if (is_array($dn_name_list)) {
4905
		foreach ($dn_name_list as $key => $value) {
4906
			$dn_name[] = $key;
4907
		}
4908
	}
4909

    
4910
	return $dn_name;
4911

    
4912
}
4913

    
4914
function get_altq_name_list() {
4915
	$altq_name_list =& get_unique_queue_list();
4916
	$altq_name = array();
4917
	if (is_array($altq_name_list)) {
4918
		foreach ($altq_name_list as $key => $aqobj) {
4919
			$altq_name[] = $key;
4920
		}
4921
	}
4922

    
4923
	return $altq_name;
4924
}
4925

    
4926
/*
4927
 * XXX: TODO Make a class shaper to hide all these functions
4928
 * from the global namespace.
4929
 */
4930

    
4931
/*
4932
 * This is a layer violation but for now there is no way
4933
 * I can find to properly do this with PHP.
4934
 */
4935
function altq_get_default_queue($interface) {
4936
	global $altq_list_queues;
4937

    
4938
	$altq_tmp = $altq_list_queues[$interface];
4939
	if ($altq_tmp) {
4940
		return $altq_tmp->GetDefaultQueuePresent();
4941
	} else {
4942
		return false;
4943
	}
4944
}
4945

    
4946
function altq_check_default_queues() {
4947
	global $altq_list_queues;
4948

    
4949
	$count = 0;
4950
	if (is_array($altq_list_queues)) {
4951
		foreach ($altq_list_queues as $altq) {
4952
			if ($altq->GetDefaultQueuePresent()) {
4953
				$count++;
4954
			}
4955
		}
4956
	}
4957
	else {
4958
		$count++;
4959
	}
4960

    
4961
	return 0;
4962
}
4963

    
4964
function &get_unique_queue_list() {
4965
	global $altq_list_queues;
4966

    
4967
	$qlist = array();
4968
	if (is_array($altq_list_queues)) {
4969
		foreach ($altq_list_queues as $altq) {
4970
			if ($altq->GetEnabled() == "") {
4971
				continue;
4972
			}
4973
			$tmplist =& $altq->get_queue_list();
4974
			foreach ($tmplist as $qname => $link) {
4975
				if ($link->GetEnabled() <> "") {
4976
					$qlist[$qname] = $link;
4977
				}
4978
			}
4979
		}
4980
	}
4981
	return $qlist;
4982
}
4983

    
4984
function &get_unique_dnqueue_list() {
4985
	global $dummynet_pipe_list;
4986

    
4987
	$qlist = array();
4988
	if (is_array($dummynet_pipe_list)) {
4989
		foreach ($dummynet_pipe_list as $dn) {
4990
			if ($dn->GetEnabled() == "") {
4991
				continue;
4992
			}
4993
			$tmplist =& $dn->get_queue_list();
4994
			foreach ($tmplist as $qname => $link) {
4995
				$qlist[$qname] = $link;
4996
			}
4997
		}
4998
	}
4999
	return $qlist;
5000
}
5001

    
5002
function ref_on_altq_queue_list($parent, $qname) {
5003
	if (isset($GLOBALS['queue_list'][$qname])) {
5004
		$GLOBALS['queue_list'][$qname]++;
5005
	} else {
5006
		$GLOBALS['queue_list'][$qname] = 1;
5007
	}
5008

    
5009
	unref_on_altq_queue_list($parent);
5010
}
5011

    
5012
function unref_on_altq_queue_list($qname) {
5013
	$GLOBALS['queue_list'][$qname]--;
5014
	if ($GLOBALS['queue_list'][$qname] <= 1) {
5015
		unset($GLOBALS['queue_list'][$qname]);
5016
	}
5017
}
5018

    
5019
function read_altq_config() {
5020
	global $altq_list_queues, $config;
5021
	$path = array();
5022

    
5023
	init_config_arr(array('shaper', 'queue'));
5024
	$a_int = &$config['shaper']['queue'];
5025

    
5026
	$altq_list_queues = array();
5027

    
5028
	if (!is_array($config['shaper']['queue'])) {
5029
		return;
5030
	}
5031

    
5032
	foreach ($a_int as $key => $conf) {
5033
		$int = $conf['interface'];
5034
		$root = new altq_root_queue();
5035
		$root->SetInterface($int);
5036
		$altq_list_queues[$root->GetInterface()] = $root;
5037
		$root->ReadConfig($conf);
5038
		array_push($path, $key);
5039
		$root->SetLink($path);
5040
		if (is_array($conf['queue'])) {
5041
			foreach ($conf['queue'] as $key1 => $q) {
5042
				array_push($path, $key1);
5043
				/*
5044
				 * XXX: we completely ignore errors here but anyway we must have
5045
				 *	checked them before so no harm should be come from this.
5046
				 */
5047
				$root->add_queue($root->GetInterface(), $q, $path, $input_errors);
5048
				array_pop($path);
5049
			}
5050
		}
5051
		array_pop($path);
5052
	}
5053
}
5054

    
5055
function read_dummynet_config() {
5056
	global $dummynet_pipe_list, $config;
5057
	$path = array();
5058

    
5059
	init_config_arr(array('dnshaper', 'queue'));
5060
	$a_int = &$config['dnshaper']['queue'];
5061

    
5062
	$dummynet_pipe_list = array();
5063

    
5064
	if (!count($config['dnshaper']['queue'])) {
5065
		return;
5066
	}
5067

    
5068
	foreach ($a_int as $key => $conf) {
5069
		if (empty($conf['name'])) {
5070
			continue; /* XXX: grrrrrr at php */
5071
		}
5072
		$root = new dnpipe_class();
5073
		$root->ReadConfig($conf);
5074
		$dummynet_pipe_list[$root->GetQname()] = $root;
5075
		array_push($path, $key);
5076
		$root->SetLink($path);
5077
		if (is_array($conf['queue'])) {
5078
			foreach ($conf['queue'] as $key1 => $q) {
5079
				array_push($path, $key1);
5080
				/*
5081
				 * XXX: we completely ignore errors here but anyway we must have
5082
				 *	checked them before so no harm should be come from this.
5083
				 */
5084
				$root->add_queue($root->GetQname(), $q, $path, $input_errors);
5085
				array_pop($path);
5086
			}
5087
		}
5088
		array_pop($path);
5089
	}
5090
}
5091

    
5092
function get_interface_list_to_show() {
5093
	global $altq_list_queues, $config;
5094
	global $shaperIFlist;
5095

    
5096
	$tree = "";
5097
	foreach ($shaperIFlist as $shif => $shDescr) {
5098
		if ($altq_list_queues[$shif]) {
5099
			continue;
5100
		} else {
5101
			if (!is_altq_capable(get_real_interface($shif))) {
5102
				continue;
5103
			}
5104
			$tree .= " <li><a href=\"firewall_shaper.php?interface=".$shif."&amp;action=add\">".$shDescr."</a></li>";
5105
		}
5106
	}
5107

    
5108
	return $tree;
5109
}
5110

    
5111
function filter_generate_altq_queues() {
5112
	global $altq_list_queues;
5113

    
5114
	read_altq_config();
5115

    
5116
	$altq_rules = "";
5117
	foreach ($altq_list_queues as $altq) {
5118
		$altq_rules .= $altq->build_rules();
5119
	}
5120

    
5121
	return $altq_rules;
5122
}
5123

    
5124
function dnqueue_find_nextnumber() {
5125
	global $dummynet_pipe_list;
5126

    
5127
	$dnused = array();
5128
	if (is_array($dummynet_pipe_list)) {
5129
		foreach ($dummynet_pipe_list as $dn) {
5130
			$tmplist =& $dn->get_queue_list();
5131
			foreach ($tmplist as $qname => $link) {
5132
				if ($link[0] == "?") {
5133
					$dnused[$qname] = substr($link, 1);
5134
				}
5135
			}
5136
		}
5137
	}
5138

    
5139
	sort($dnused, SORT_NUMERIC);
5140
	$dnnumber = 0;
5141
	$found = false;
5142
	foreach ($dnused as $dnnum) {
5143
		if (($dnnum - $dnnumber) > 1) {
5144
			$dnnumber = $dnnum - 1;
5145
			$found = true;
5146
			break;
5147
		} else {
5148
			$dnnumber = $dnnum;
5149
		}
5150
	}
5151

    
5152
	if ($found == false) {
5153
		$dnnumber++;
5154
	}
5155

    
5156
	unset($dnused, $dnnum, $found);
5157
	return $dnnumber;
5158
}
5159

    
5160
function dnpipe_find_nextnumber() {
5161
	global $dummynet_pipe_list;
5162

    
5163
	$dnused = array();
5164
	foreach ($dummynet_pipe_list as $dn) {
5165
		$dnused[] = $dn->GetNumber();
5166
	}
5167

    
5168
	sort($dnused, SORT_NUMERIC);
5169
	$dnnumber = 0;
5170
	$found = false;
5171
	foreach ($dnused as $dnnum) {
5172
		if (($dnnum - $dnnumber) > 1) {
5173
			$dnnumber = $dnnum - 1;
5174
			$found = true;
5175
			break;
5176
		} else {
5177
			$dnnumber = $dnnum;
5178
		}
5179
	}
5180

    
5181
	if ($found == false) {
5182
		$dnnumber++;
5183
	}
5184

    
5185
	unset($dnused, $dnnum, $found);
5186
	return $dnnumber;
5187
}
5188

    
5189
function filter_generate_dummynet_rules() {
5190
	global $g, $dummynet_pipe_list;
5191

    
5192
	read_dummynet_config();
5193

    
5194
	$dn_rules = "";
5195
	$max_qlimit = "100"; // OS default
5196
	foreach ($dummynet_pipe_list as $dn) {
5197
		$dn_rules .= $dn->build_rules();
5198
		$this_qlimit = $dn->GetQlimit();
5199
		if ($this_qlimit > $max_qlimit) {
5200
			$max_qlimit = $this_qlimit;
5201
		}
5202
	}
5203
	if (!is_numericint($max_qlimit)) {
5204
		$max_qlimit = "100";
5205
	}
5206
	if (!empty($dn_rules)) {
5207
		if (!is_module_loaded("dummynet.ko")) {
5208
			mwexec("/sbin/kldload dummynet");
5209
		}
5210
		set_sysctl(array(
5211
				"net.inet.ip.dummynet.io_fast" => "1",
5212
				"net.inet.ip.dummynet.hash_size" => "256",
5213
				"net.inet.ip.dummynet.pipe_slot_limit" => $max_qlimit
5214
		));
5215
		file_put_contents("{$g['tmp_path']}/rules.limiter", $dn_rules);
5216
		mwexec("/sbin/ipfw {$g['tmp_path']}/rules.limiter");
5217
	}
5218
}
5219

    
5220
function build_iface_without_this_queue($iface, $qname) {
5221
	global $g, $altq_list_queues;
5222
	global $shaperIFlist;
5223

    
5224
	$altq =& $altq_list_queues[$iface];
5225

    
5226
	if ($altq) {
5227
		$scheduler = $altq->GetScheduler();
5228
	}
5229

    
5230
	$form = '<dl class="dl-horizontal">';
5231

    
5232
	$form .= '	<dt>';
5233
	$form .= '		<a href="firewall_shaper.php?interface=' . $iface . '&amp;queue=' . $iface . '&amp;action=show">' . $shaperIFlist[$iface] . '</a>';
5234
	$form .= '	</dt>';
5235
	$form .= '	<dd>';
5236
	$form .=		$scheduler;
5237
	$form .= '	</dd>';
5238

    
5239
	$form .= '	<dt>';
5240
	$form .= 'Clone';
5241
	$form .= '	</dt>';
5242
	$form .= '	<dd>';
5243
	$form .= '<a class="btn btn-info btn-xs" href="firewall_shaper_queues.php?interface=';
5244
	$form .= $iface . '&amp;queue=';
5245
	$form .= $qname . '&amp;action=add">';
5246
	$form .= '<i class="fa fa-clone icon-embed-btn"></i>';
5247
	$form .= gettext("Clone Shaper to this Interface") . '</a>';
5248
	$form .= '	</dd>';
5249

    
5250
	$form .= '</dl>';
5251

    
5252
	return $form;
5253

    
5254
}
5255

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

    
5259
$shaper_msg = gettext("The tree on the left navigates through the %s.");
5260
$default_shaper_msg .= sprintf($shaper_msg, gettext("queues")) . "<br />";
5261
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiters")) . "<br />";
5262

    
5263
$shaper_msg = gettext("Buttons at the bottom represent %s actions and are activated accordingly.");
5264
$default_shaper_msg .= sprintf($shaper_msg, gettext("queue"));
5265
$dn_default_shaper_msg .= sprintf($shaper_msg, gettext("limiter"));
5266

    
5267
// Check to see if the specified interface has a queue configured
5268
function interface_has_queue($if) {
5269
	global $config;
5270

    
5271
	if ($config['shaper']) {
5272
		foreach ($config['shaper']['queue'] as $queue) {
5273
			if ($queue['interface'] === $if) {
5274
				return true;
5275
			}
5276
		}
5277
	}
5278

    
5279
	return false;
5280
}
5281
?>
(47-47/59)