netmode: support to define supported modes

This commit is contained in:
Vivek Kumar Dutta 2025-05-16 18:23:43 +05:30
parent 44238e2033
commit ca49dfac97
No known key found for this signature in database
GPG key ID: 4E09F5AD8265FD4C
11 changed files with 199 additions and 37 deletions

View file

@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=netmode PKG_NAME:=netmode
PKG_VERSION:=1.0.2 PKG_VERSION:=1.1.0
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_LICENSE:=GPL-2.0-only PKG_LICENSE:=GPL-2.0-only
@ -42,7 +42,9 @@ endef
define Package/netmode/install define Package/netmode/install
$(INSTALL_DIR) $(1)/etc $(INSTALL_DIR) $(1)/etc
$(INSTALL_DIR) $(1)/lib
$(CP) ./files/etc/* $(1)/etc/ $(CP) ./files/etc/* $(1)/etc/
$(CP) ./files/lib/* $(1)/lib/
$(BBFDM_REGISTER_SERVICES) -v ${VENDOR_PREFIX} ./bbfdm_service.json $(1) $(PKG_NAME) $(BBFDM_REGISTER_SERVICES) -v ${VENDOR_PREFIX} ./bbfdm_service.json $(1) $(PKG_NAME)
$(BBFDM_INSTALL_MS_DM) -v ${VENDOR_PREFIX} ./files/datamodel.json $(1) $(PKG_NAME) $(BBFDM_INSTALL_MS_DM) -v ${VENDOR_PREFIX} ./files/datamodel.json $(1) $(PKG_NAME)
endef endef

View file

@ -6,6 +6,7 @@
"cwmp", "cwmp",
"usp" "usp"
], ],
"description": "Datamodel vendor extension to support easy switching between wan types, a reboot is required in some cases after switching the mode",
"access": false, "access": false,
"array": false, "array": false,
"dependency": "file:/etc/config/netmode", "dependency": "file:/etc/config/netmode",
@ -13,6 +14,7 @@
"type": "boolean", "type": "boolean",
"read": true, "read": true,
"write": true, "write": true,
"description": "Enable/Disable WAN switching using netmode",
"protocols": [ "protocols": [
"cwmp", "cwmp",
"usp" "usp"
@ -36,10 +38,14 @@
"type": "string", "type": "string",
"read": true, "read": true,
"write": true, "write": true,
"description": "Current applied netmode value",
"protocols": [ "protocols": [
"cwmp", "cwmp",
"usp" "usp"
], ],
"flags": [
"Reference"
],
"mapping": [ "mapping": [
{ {
"type": "uci", "type": "uci",
@ -51,32 +57,110 @@
"option": { "option": {
"name": "mode" "name": "mode"
} }
} },
"linker_obj": "Device.{BBF_VENDOR_PREFIX}NetMode.SupportedModes.[Name==@key]."
} }
] ]
}, },
"SupportedModes": { "SupportedModesNumberOfEntries": {
"type": "string", "type": "unsignedInt",
"read": true, "read": true,
"write": false, "write": false,
"description": "SupportedModes Number of entries in the current config",
"protocols": [
"usp"
],
"mapping": [
{
"type": "uci",
"uci": {
"file": "netmode",
"section": {
"type": "supported_modes"
},
"option": {
"name": "@Count"
}
}
}
]
},
"Device.{BBF_VENDOR_PREFIX}NetMode.SupportedModes.{i}.": {
"type": "object",
"protocols": [ "protocols": [
"cwmp", "cwmp",
"usp" "usp"
], ],
"access": true,
"array": true,
"description": "Object to list supported wan modes",
"mapping": [ "mapping": [
{ {
"type": "uci", "type": "uci",
"uci": { "uci": {
"file": "netmode", "file": "netmode",
"section": { "section": {
"name": "global" "type": "supported_modes"
}, },
"list": { "dmmapfile": "dmmap_netmode"
"name": "supported_modes"
}
} }
} }
] ],
"Name": {
"type": "string",
"read": true,
"write": false,
"description": "Name of the wan mode, it has to be unique and in sync with /etc/netmodes/",
"protocols": [
"usp"
],
"flags": [
"Linker",
"Unique"
],
"mapping": [
{
"type": "uci_sec",
"data": "@Parent",
"key": "name"
}
]
},
"Description": {
"type": "string",
"read": true,
"write": false,
"description": "Human readable description for this mode",
"protocols": [
"usp"
],
"mapping": [
{
"type": "uci_sec",
"data": "@Parent",
"key": "description"
}
]
},
"Args": {
"type": "string",
"read": true,
"write": true,
"description": "Input args for this mode",
"protocols": [
"usp"
],
"flags": [
"Secure"
],
"mapping": [
{
"type": "uci_sec",
"data": "@Parent",
"list": "args"
}
]
}
} }
} }
} }

View file

@ -1,3 +1,2 @@
config netmode global config netmode global
option enabled 0 option enabled 0
# option mode 'router'

View file

View file

@ -0,0 +1,21 @@
{
"mode": "routed-dhcp",
"supported_modes": [
{
"name": "routed-dhcp",
"description": "WAN with DHCP proto (Layer 3)"
},
{
"name": "routed-pppoe",
"description": "WAN with PPPoE (Layer 3)",
"args": [
"username:test",
"password:testpass123"
]
},
{
"name": "bridged",
"description": "Bridged mode (Layer 2)"
}
]
}

View file

@ -1,24 +0,0 @@
#!/bin/sh
. /lib/functions.sh
MODEDIR="/etc/netmodes"
[ -f "/etc/config/netmode" ] || exit 0
[ -d "${MODEDIR}" ] || exit 0
set_supported_modes() {
val="$(find "${MODEDIR}" -maxdepth 1 -mindepth 1 -type d -exec basename {} +)"
for mode in ${val}; do
uci add_list netmode.global.supported_modes="${mode}"
done
}
config_load netmode
config_get supported_modes global supported_modes ''
if [ -z "${supported_modes}" ]; then
set_supported_modes
fi
exit 0

View file

@ -0,0 +1,76 @@
#!/bin/sh
. /lib/functions.sh
. /usr/share/libubox/jshn.sh
COUNT=1
SUPPORTED_MODE="/etc/netmodes/supported_modes.json"
if [ ! -f "/etc/config/netmode" ]; then
exit 0
fi
if [ ! -f "${SUPPORTED_MODE}" ]; then
exit 0
fi
configure_supp_modes()
{
local obj inst name description args
obj="${1}"
inst="${2}"
if [ -z "${inst}" ]; then
return 0
fi
json_select ${inst}
json_get_var name name
json_get_var description description
json_get_values args args
if [ -d "/etc/netmodes/${name}" ]; then
uci -q set netmode.mode_${COUNT}=supported_modes
uci -q set netmode.mode_${COUNT}.name="${name}"
uci -q set netmode.mode_${COUNT}.description="${description}"
for arg in ${args}; do
uci -q add_list netmode.mode_${COUNT}.args="${arg}"
done
fi
json_select ..
COUNT="$((COUNT + 1))"
}
remove_mode()
{
uci -q delete netmode.${1}
}
cleanup_modes()
{
config_load "netmode"
config_foreach remove_mode supported_modes
}
update_modes()
{
local mode
json_init
json_load_file "${SUPPORTED_MODE}"
json_get_var mode mode ""
if [ -n "${mode}" ]; then
uci -q set netmode.global.mode="${mode}"
fi
json_for_each_item configure_supp_modes supported_modes
}
cleanup_modes
update_modes

View file

View file

@ -108,10 +108,14 @@ l2_network_config() {
uci -q commit firewall uci -q commit firewall
} }
network_mode="$(fw_printenv -n netmode 2>/dev/null)" network_mode="$(uci -q get netmode.global.mode)"
if [ -z "${network_mode}" ]; then
network_mode="$(fw_printenv -n netmode 2>/dev/null)"
fi
case "$network_mode" in case "$network_mode" in
layer2|extender) layer2|extender|bridged)
l2_network_config l2_network_config
l2_mcast_config l2_mcast_config
;; ;;