Skip to content

Commit 983a066

Browse files
Interfaces: Devices: Bridge - refactor to MVC closes opnsense#8353 (opnsense#8534)
* Interfaces: Devices: Bridge - refactor to MVC for opnsense#8353 * move existing properties to model which overlays existing config path * add a simple wrapper script for [re]configuration which diffs and applies using the new _interfaces_bridge_configure() implementation * Update src/opnsense/mvc/app/models/OPNsense/Interfaces/Bridge.xml Co-authored-by: Franco Fichtner <franco@opnsense.org> --------- Co-authored-by: Franco Fichtner <franco@opnsense.org>
1 parent 09bd2d9 commit 983a066

File tree

14 files changed

+695
-744
lines changed

14 files changed

+695
-744
lines changed

plist

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@
360360
/usr/local/opnsense/mvc/app/controllers/OPNsense/IPsec/forms/dialogSPD.xml
361361
/usr/local/opnsense/mvc/app/controllers/OPNsense/IPsec/forms/dialogVTI.xml
362362
/usr/local/opnsense/mvc/app/controllers/OPNsense/IPsec/forms/settings.xml
363+
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/BridgeSettingsController.php
363364
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/GifSettingsController.php
364365
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/GreSettingsController.php
365366
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/LaggSettingsController.php
@@ -369,6 +370,7 @@
369370
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VipSettingsController.php
370371
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VlanSettingsController.php
371372
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/Api/VxlanSettingsController.php
373+
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/BridgeController.php
372374
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/GifController.php
373375
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/GreController.php
374376
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/LaggController.php
@@ -378,6 +380,7 @@
378380
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VipController.php
379381
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VlanController.php
380382
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/VxlanController.php
383+
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogBridge.xml
381384
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogGif.xml
382385
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogGre.xml
383386
/usr/local/opnsense/mvc/app/controllers/OPNsense/Interfaces/forms/dialogLagg.xml
@@ -752,6 +755,9 @@
752755
/usr/local/opnsense/mvc/app/models/OPNsense/IPsec/Swanctl.php
753756
/usr/local/opnsense/mvc/app/models/OPNsense/IPsec/Swanctl.xml
754757
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/ACL/ACL.xml
758+
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Bridge.php
759+
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/Bridge.xml
760+
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/BridgeMemberField.php
755761
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/LaggInterfaceField.php
756762
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/LinkAddressField.php
757763
/usr/local/opnsense/mvc/app/models/OPNsense/Interfaces/FieldTypes/NeighborField.php
@@ -915,6 +921,7 @@
915921
/usr/local/opnsense/mvc/app/views/OPNsense/IPsec/spd.volt
916922
/usr/local/opnsense/mvc/app/views/OPNsense/IPsec/tunnels.volt
917923
/usr/local/opnsense/mvc/app/views/OPNsense/IPsec/vti.volt
924+
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/bridge.volt
918925
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/gif.volt
919926
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/gre.volt
920927
/usr/local/opnsense/mvc/app/views/OPNsense/Interface/lagg.volt
@@ -1175,6 +1182,7 @@
11751182
/usr/local/opnsense/scripts/interfaces/ppp-linkup.sh
11761183
/usr/local/opnsense/scripts/interfaces/ppp-rename.sh
11771184
/usr/local/opnsense/scripts/interfaces/ppp-uptime.sh
1185+
/usr/local/opnsense/scripts/interfaces/reconfigure_bridges.php
11781186
/usr/local/opnsense/scripts/interfaces/reconfigure_gifs.php
11791187
/usr/local/opnsense/scripts/interfaces/reconfigure_gres.php
11801188
/usr/local/opnsense/scripts/interfaces/reconfigure_laggs.php
@@ -2404,8 +2412,6 @@
24042412
/usr/local/www/index.php
24052413
/usr/local/www/interfaces.php
24062414
/usr/local/www/interfaces_assign.php
2407-
/usr/local/www/interfaces_bridge.php
2408-
/usr/local/www/interfaces_bridge_edit.php
24092415
/usr/local/www/interfaces_ppps.php
24102416
/usr/local/www/interfaces_ppps_edit.php
24112417
/usr/local/www/interfaces_wireless.php
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/*
4+
* Copyright (C) 2025 Deciso B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20+
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
namespace OPNsense\Interfaces\Api;
30+
31+
use OPNsense\Base\ApiMutableModelControllerBase;
32+
use OPNsense\Base\UserException;
33+
use OPNsense\Core\Backend;
34+
use OPNsense\Core\Config;
35+
36+
/**
37+
* @package OPNsense\Interfaces
38+
*/
39+
class BridgeSettingsController extends ApiMutableModelControllerBase
40+
{
41+
protected static $internalModelName = 'bridge';
42+
protected static $internalModelClass = 'OPNsense\Interfaces\Bridge';
43+
44+
/**
45+
* search bridges
46+
* @return array search results
47+
*/
48+
public function searchItemAction()
49+
{
50+
return $this->searchBase("bridged", null, "descr");
51+
}
52+
53+
/**
54+
* Update bridge with given properties
55+
* @param string $uuid internal id
56+
* @return array save result + validation output
57+
*/
58+
public function setItemAction($uuid)
59+
{
60+
Config::getInstance()->lock();
61+
$node = $this->getModel()->getNodeByReference('bridged.' . $uuid);
62+
$overlay = null;
63+
if (!empty($node)) {
64+
// not allowed to change bridge interface name
65+
$overlay['bridgeif'] = (string)$node->bridgeif;
66+
}
67+
return $this->setBase("bridge", "bridged", $uuid, $overlay);
68+
}
69+
70+
/**
71+
* Add new bridge and set with attributes from post
72+
* @return array save result + validation output
73+
*/
74+
public function addItemAction()
75+
{
76+
Config::getInstance()->lock();
77+
$overlay = [];
78+
$ifnames = [];
79+
foreach ($this->getModel()->bridged->iterateItems() as $node) {
80+
$ifnames[] = (string)$node->bridgeif;
81+
}
82+
for ($i = 0; true; ++$i) {
83+
$gifif = sprintf('bridge%d', $i);
84+
if (!in_array($gifif, $ifnames)) {
85+
$overlay['bridgeif'] = $gifif;
86+
break;
87+
}
88+
}
89+
90+
return $this->addBase("bridge", "bridged", $overlay);
91+
}
92+
93+
/**
94+
* Retrieve bridge settings or return defaults for new one
95+
* @param $uuid item unique id
96+
* @return array bridge content
97+
*/
98+
public function getItemAction($uuid = null)
99+
{
100+
return $this->getBase("bridge", "bridged", $uuid);
101+
}
102+
103+
/**
104+
* Delete bridge by uuid
105+
* @param string $uuid internal id
106+
* @return array save status
107+
*/
108+
public function delItemAction($uuid)
109+
{
110+
Config::getInstance()->lock();
111+
$node = $this->getModel()->getNodeByReference('bridged.' . $uuid);
112+
if ($node != null) {
113+
$cfg = Config::getInstance()->object();
114+
foreach ($cfg->interfaces->children() as $key => $value) {
115+
if ((string)$value->if == (string)$node->bridgeif) {
116+
throw new \OPNsense\Base\UserException(
117+
sprintf(gettext("Cannot delete bridge. Currently in use by [%s] %s"), $key, $value),
118+
gettext("bridge in use")
119+
);
120+
}
121+
}
122+
}
123+
return $this->delBase("bridged", $uuid);
124+
}
125+
126+
/**
127+
* reconfigure bridges
128+
*/
129+
public function reconfigureAction()
130+
{
131+
if ($this->request->isPost()) {
132+
(new Backend())->configdRun("interface bridge configure");
133+
return ["status" => "ok"];
134+
} else {
135+
return ["status" => "failed"];
136+
}
137+
}
138+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* Copyright (C) 2025 Deciso B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20+
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
namespace OPNsense\Interfaces;
30+
31+
class BridgeController extends \OPNsense\Base\IndexController
32+
{
33+
public function indexAction()
34+
{
35+
$this->view->pick('OPNsense/Interface/bridge');
36+
37+
$this->view->formDialogBridge = $this->getForm("dialogBridge");
38+
$this->view->formGridBridge = $this->getFormGrid("dialogBridge");
39+
}
40+
}

0 commit comments

Comments
 (0)