treewide: use "device" option in UCI "interface" sections

netifd has been recently patched to use "device" option instead of
"ifname" as more clear & accurate.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
This commit is contained in:
Rafał Miłecki 2021-05-24 18:17:41 +02:00
parent 030804b740
commit 74be304e54
2 changed files with 76 additions and 67 deletions

View file

@ -2496,14 +2496,14 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
* *
* Alias interfaces are interfaces layering on top of another interface * Alias interfaces are interfaces layering on top of another interface
* and are denoted by a special `@interfacename` notation in the * and are denoted by a special `@interfacename` notation in the
* underlying `ifname` option. * underlying `device` option.
* *
* @returns {null|string} * @returns {null|string}
* Returns the name of the parent interface if this logical interface * Returns the name of the parent interface if this logical interface
* is an alias or `null` if it is not an alias interface. * is an alias or `null` if it is not an alias interface.
*/ */
isAlias: function() { isAlias: function() {
var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')), var ifnames = L.toArray(uci.get('network', this.sid, 'device')),
parent = null; parent = null;
for (var i = 0; i < ifnames.length; i++) for (var i = 0; i < ifnames.length; i++)
@ -2527,9 +2527,9 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
return false; return false;
var empty = true, var empty = true,
ifname = this._get('ifname'); device = this._get('device');
if (ifname != null && ifname.match(/\S+/)) if (device != null && device.match(/\S+/))
empty = false; empty = false;
if (empty == true && getWifiNetidBySid(this.sid) != null) if (empty == true && getWifiNetidBySid(this.sid) != null)
@ -2561,18 +2561,18 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
* argument was invalid, if the device was already part of the logical * argument was invalid, if the device was already part of the logical
* interface or if the logical interface is virtual. * interface or if the logical interface is virtual.
*/ */
addDevice: function(ifname) { addDevice: function(device) {
ifname = ifnameOf(ifname); device = ifnameOf(device);
if (ifname == null || this.isFloating()) if (device == null || this.isFloating())
return false; return false;
var wif = getWifiSidByIfname(ifname); var wif = getWifiSidByIfname(device);
if (wif != null) if (wif != null)
return appendValue('wireless', wif, 'network', this.sid); return appendValue('wireless', wif, 'network', this.sid);
return appendValue('network', this.sid, 'ifname', ifname); return appendValue('network', this.sid, 'device', device);
}, },
/** /**
@ -2588,20 +2588,20 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
* argument was invalid, if the device was already part of the logical * argument was invalid, if the device was already part of the logical
* interface or if the logical interface is virtual. * interface or if the logical interface is virtual.
*/ */
deleteDevice: function(ifname) { deleteDevice: function(device) {
var rv = false; var rv = false;
ifname = ifnameOf(ifname); device = ifnameOf(device);
if (ifname == null || this.isFloating()) if (device == null || this.isFloating())
return false; return false;
var wif = getWifiSidByIfname(ifname); var wif = getWifiSidByIfname(device);
if (wif != null) if (wif != null)
rv = removeValue('wireless', wif, 'network', this.sid); rv = removeValue('wireless', wif, 'network', this.sid);
if (removeValue('network', this.sid, 'ifname', ifname)) if (removeValue('network', this.sid, 'device', device))
rv = true; rv = true;
return rv; return rv;
@ -2627,7 +2627,7 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
return new Device(ifname, this); return new Device(ifname, this);
} }
else { else {
var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')); var ifnames = L.toArray(uci.get('network', this.sid, 'device'));
for (var i = 0; i < ifnames.length; i++) { for (var i = 0; i < ifnames.length; i++) {
var m = ifnames[i].match(/^([^:/]+)/); var m = ifnames[i].match(/^([^:/]+)/);
@ -2682,13 +2682,10 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
if (!this.isBridge() && !(this.isVirtual() && !this.isFloating())) if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
return null; return null;
var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')); var device = uci.get('network', this.sid, 'device');
for (var i = 0; i < ifnames.length; i++) { if (device && device.charAt(0) != '@') {
if (ifnames[i].charAt(0) == '@') var m = device.match(/^([^:/]+)/);
continue;
var m = ifnames[i].match(/^([^:/]+)/);
if (m != null) if (m != null)
rv.push(Network.prototype.instantiateDevice(m[1], this)); rv.push(Network.prototype.instantiateDevice(m[1], this));
} }
@ -2730,25 +2727,24 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
* Returns `true` when this logical interface contains the given network * Returns `true` when this logical interface contains the given network
* device or `false` if not. * device or `false` if not.
*/ */
containsDevice: function(ifname) { containsDevice: function(device) {
ifname = ifnameOf(ifname); device = ifnameOf(device);
if (ifname == null) if (device == null)
return false; return false;
else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid) == ifname) else if (this.isVirtual() && '%s-%s'.format(this.getProtocol(), this.sid) == device)
return true; return true;
else if (this.isBridge() && 'br-%s'.format(this.sid) == ifname) else if (this.isBridge() && 'br-%s'.format(this.sid) == device)
return true; return true;
var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')); var name = uci.get('network', this.sid, 'device');
if (name) {
for (var i = 0; i < ifnames.length; i++) { var m = name.match(/^([^:/]+)/);
var m = ifnames[i].match(/^([^:/]+)/); if (m != null && m[1] == device)
if (m != null && m[1] == ifname)
return true; return true;
} }
var wif = getWifiSidByIfname(ifname); var wif = getWifiSidByIfname(device);
if (wif != null) { if (wif != null) {
var networks = L.toArray(uci.get('wireless', wif, 'network')); var networks = L.toArray(uci.get('wireless', wif, 'network'));
@ -2791,19 +2787,19 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
* device and allows querying device details such as packet statistics or MTU. * device and allows querying device details such as packet statistics or MTU.
*/ */
Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ { Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
__init__: function(ifname, network) { __init__: function(device, network) {
var wif = getWifiSidByIfname(ifname); var wif = getWifiSidByIfname(device);
if (wif != null) { if (wif != null) {
var res = getWifiStateBySid(wif) || [], var res = getWifiStateBySid(wif) || [],
netid = getWifiNetidBySid(wif) || []; netid = getWifiNetidBySid(wif) || [];
this.wif = new WifiNetwork(wif, res[0], res[1], netid[0], res[2], { ifname: ifname }); this.wif = new WifiNetwork(wif, res[0], res[1], netid[0], res[2], { ifname: device });
this.ifname = this.wif.getIfname(); this.device = this.wif.getIfname();
} }
this.ifname = this.ifname || ifname; this.device = this.device || device;
this.dev = Object.assign({}, _state.netdevs[this.ifname]); this.dev = Object.assign({}, _state.netdevs[this.device]);
this.network = network; this.network = network;
}, },
@ -2826,7 +2822,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
* Returns the name of the device, e.g. `eth0` or `wlan0`. * Returns the name of the device, e.g. `eth0` or `wlan0`.
*/ */
getName: function() { getName: function() {
return (this.wif != null ? this.wif.getIfname() : this.ifname); return (this.wif != null ? this.wif.getIfname() : this.device);
}, },
/** /**
@ -2887,17 +2883,17 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
* - `ethernet` for all other device types * - `ethernet` for all other device types
*/ */
getType: function() { getType: function() {
if (this.ifname != null && this.ifname.charAt(0) == '@') if (this.device != null && this.device.charAt(0) == '@')
return 'alias'; return 'alias';
else if (this.dev.devtype == 'wlan' || this.wif != null || isWifiIfname(this.ifname)) else if (this.dev.devtype == 'wlan' || this.wif != null || isWifiIfname(this.device))
return 'wifi'; return 'wifi';
else if (this.dev.devtype == 'bridge' || _state.isBridge[this.ifname]) else if (this.dev.devtype == 'bridge' || _state.isBridge[this.device])
return 'bridge'; return 'bridge';
else if (_state.isTunnel[this.ifname]) else if (_state.isTunnel[this.device])
return 'tunnel'; return 'tunnel';
else if (this.dev.devtype == 'vlan' || this.ifname.indexOf('.') > -1) else if (this.dev.devtype == 'vlan' || this.device.indexOf('.') > -1)
return 'vlan'; return 'vlan';
else if (this.dev.devtype == 'dsa' || _state.isSwitch[this.ifname]) else if (this.dev.devtype == 'dsa' || _state.isSwitch[this.device])
return 'switch'; return 'switch';
else else
return 'ethernet'; return 'ethernet';
@ -2914,7 +2910,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
if (this.wif != null) if (this.wif != null)
return this.wif.getShortName(); return this.wif.getShortName();
return this.ifname; return this.device;
}, },
/** /**
@ -2954,11 +2950,11 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
return _('Bridge'); return _('Bridge');
case 'switch': case 'switch':
return (_state.netdevs[this.ifname] && _state.netdevs[this.ifname].devtype == 'dsa') return (_state.netdevs[this.device] && _state.netdevs[this.device].devtype == 'dsa')
? _('Switch port') : _('Ethernet Switch'); ? _('Switch port') : _('Ethernet Switch');
case 'vlan': case 'vlan':
return (_state.isSwitch[this.ifname] ? _('Switch VLAN') : _('Software VLAN')); return (_state.isSwitch[this.device] ? _('Switch VLAN') : _('Software VLAN'));
case 'tunnel': case 'tunnel':
return _('Tunnel Interface'); return _('Tunnel Interface');
@ -2977,7 +2973,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
* a Linux bridge. * a Linux bridge.
*/ */
getPorts: function() { getPorts: function() {
var br = _state.bridges[this.ifname], var br = _state.bridges[this.device],
rv = []; rv = [];
if (br == null || !Array.isArray(br.ifnames)) if (br == null || !Array.isArray(br.ifnames))
@ -2999,7 +2995,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
* device is not a Linux bridge. * device is not a Linux bridge.
*/ */
getBridgeID: function() { getBridgeID: function() {
var br = _state.bridges[this.ifname]; var br = _state.bridges[this.device];
return (br != null ? br.id : null); return (br != null ? br.id : null);
}, },
@ -3011,7 +3007,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
* enabled, else `false`. * enabled, else `false`.
*/ */
getBridgeSTP: function() { getBridgeSTP: function() {
var br = _state.bridges[this.ifname]; var br = _state.bridges[this.device];
return (br != null ? !!br.stp : false); return (br != null ? !!br.stp : false);
}, },
@ -3123,7 +3119,7 @@ Device = baseclass.extend(/** @lends LuCI.network.Device.prototype */ {
var networks = enumerateNetworks.apply(L.network); var networks = enumerateNetworks.apply(L.network);
for (var i = 0; i < networks.length; i++) for (var i = 0; i < networks.length; i++)
if (networks[i].containsDevice(this.ifname) || networks[i].getIfname() == this.ifname) if (networks[i].containsDevice(this.device) || networks[i].getIfname() == this.device)
this.networks.push(networks[i]); this.networks.push(networks[i]);
this.networks.sort(networkSort); this.networks.sort(networkSort);

View file

@ -299,7 +299,7 @@ return view.extend({
]); ]);
}, },
interfaceWithIfnameSections: function() { interfaceBridgeWithIfnameSections: function() {
return uci.sections('network', 'interface').filter(function(ns) { return uci.sections('network', 'interface').filter(function(ns) {
return ns.type == 'bridge' && !ns.ports && ns.ifname; return ns.type == 'bridge' && !ns.ports && ns.ifname;
}); });
@ -311,8 +311,14 @@ return view.extend({
}); });
}, },
interfaceWithIfnameSections: function() {
return uci.sections('network', 'interface').filter(function(ns) {
return !ns.device && ns.ifname;
});
},
handleMigration: function(ev) { handleMigration: function(ev) {
var interfaces = this.interfaceWithIfnameSections(); var interfaces = this.interfaceBridgeWithIfnameSections();
var tasks = []; var tasks = [];
interfaces.forEach(function(ns) { interfaces.forEach(function(ns) {
@ -326,7 +332,7 @@ return view.extend({
tasks.push(uci.callSet('network', ns['.name'], { tasks.push(uci.callSet('network', ns['.name'], {
'type': '', 'type': '',
'ifname': device_name 'device': device_name
})); }));
}); });
@ -337,6 +343,13 @@ return view.extend({
})); }));
}); });
this.interfaceWithIfnameSections().forEach(function(ns) {
tasks.push(uci.callSet('network', ns['.name'], {
'ifname': '',
'device': ns.ifname
}));
});
return Promise.all(tasks) return Promise.all(tasks)
.then(L.bind(ui.changes.init, ui.changes)) .then(L.bind(ui.changes.init, ui.changes))
.then(L.bind(ui.changes.apply, ui.changes)); .then(L.bind(ui.changes.apply, ui.changes));
@ -358,8 +371,9 @@ return view.extend({
var netifdVersion = (data[3] || '').match(/Version: ([^\n]+)/); var netifdVersion = (data[3] || '').match(/Version: ([^\n]+)/);
if (netifdVersion && netifdVersion[1] >= "2021-05-20" && if (netifdVersion && netifdVersion[1] >= "2021-05-20" &&
(this.interfaceWithIfnameSections().length || (this.interfaceBridgeWithIfnameSections().length ||
this.deviceWithIfnameSections().length)) this.deviceWithIfnameSections().length ||
this.interfaceWithIfnameSections().length))
return this.renderMigration(); return this.renderMigration();
var dslModemType = data[0], var dslModemType = data[0],
@ -468,9 +482,8 @@ return view.extend({
}, this); }, this);
o.write = function() {}; o.write = function() {};
o = s.taboption('general', widgets.DeviceSelect, 'ifname', _('Device')); o = s.taboption('general', widgets.DeviceSelect, 'device', _('Device'));
o.nobridges = false; o.nobridges = false;
o.noaliases = false;
o.optional = false; o.optional = false;
o.network = ifc.getName(); o.network = ifc.getName();
@ -844,7 +857,7 @@ return view.extend({
o = s.children[i]; o = s.children[i];
switch (o.option) { switch (o.option) {
case 'ifname': case 'device':
case 'proto': case 'proto':
case 'auto': case 'auto':
case '_dhcp': case '_dhcp':
@ -884,10 +897,10 @@ return view.extend({
s.handleModalCancel = function(/* ... */) { s.handleModalCancel = function(/* ... */) {
var type = uci.get('network', this.activeSection || this.addedSection, 'type'), var type = uci.get('network', this.activeSection || this.addedSection, 'type'),
ifname = (type == 'bridge') ? 'br-%s'.format(this.activeSection || this.addedSection) : null; device = (type == 'bridge') ? 'br-%s'.format(this.activeSection || this.addedSection) : null;
uci.sections('network', 'bridge-vlan', function(bvs) { uci.sections('network', 'bridge-vlan', function(bvs) {
if (ifname != null && bvs.device == ifname) if (device != null && bvs.device == device)
uci.remove('network', bvs['.name']); uci.remove('network', bvs['.name']);
}); });
@ -898,7 +911,7 @@ return view.extend({
var m2 = new form.Map('network'), var m2 = new form.Map('network'),
s2 = m2.section(form.NamedSection, '_new_'), s2 = m2.section(form.NamedSection, '_new_'),
protocols = network.getProtocols(), protocols = network.getProtocols(),
proto, name, ifname; proto, name, device;
protocols.sort(function(a, b) { protocols.sort(function(a, b) {
return a.getProtocol() > b.getProtocol(); return a.getProtocol() > b.getProtocol();
@ -928,9 +941,9 @@ return view.extend({
return true; return true;
}; };
ifname = s2.option(widgets.DeviceSelect, 'ifname', _('Device')); device = s2.option(widgets.DeviceSelect, 'device', _('Device'));
ifname.noaliases = false; device.noaliases = false;
ifname.optional = false; device.optional = false;
proto = s2.option(form.ListValue, 'proto', _('Protocol')); proto = s2.option(form.ListValue, 'proto', _('Protocol'));
proto.validate = name.validate; proto.validate = name.validate;
@ -969,7 +982,7 @@ return view.extend({
var section_id = uci.add('network', 'interface', nameval); var section_id = uci.add('network', 'interface', nameval);
protoclass.set('proto', protoval); protoclass.set('proto', protoval);
protoclass.addDevice(ifname.formvalue('_new_')); protoclass.addDevice(device.formvalue('_new_'));
m.children[0].addedSection = section_id; m.children[0].addedSection = section_id;
}).then(L.bind(m.children[0].renderMoreOptionsModal, m.children[0], nameval)); }).then(L.bind(m.children[0].renderMoreOptionsModal, m.children[0], nameval));