openwrt之network配置文件

湛财
2023-12-01

config network,主要负责网络接口的配置
openwrt 19.07.04

  • netowrk 配置文件
root@OpenWrt:/# cat /etc/config/network

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdfd:8a0a:d67d::/48'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0.1'
        option proto 'static'
        option ipaddr '192.168.1.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

config device 'lan_eth0_1_dev'
        option name 'eth0.1'
        option macaddr 'ff:ff:ff:ff:ff:ff'

config interface 'wan'
        option ifname 'eth0.2'
        option metric '1'
        option proto 'dhcp'

config device 'wan_eth0_2_dev'
        option name 'eth0.2'
        option macaddr 'ff:ff:ff:ff:ff:ff'

config interface 'wan6'
        option ifname 'eth0.2'
        option proto 'dhcpv6'

config interface 'wan_4g'
        option ifname 'eth1'
        option metric '3'
        option proto 'dhcp'

config interface 'wan_4g6'
        option ifname 'eth1'
        option proto 'dhcpv6'

config switch
        option name 'switch0'
        option reset '1'
        option enable_vlan '1'

config switch_vlan
        option device 'switch0'
        option vlan '1'
        option ports '0 6t'

config switch_vlan
        option device 'switch0'
        option vlan '2'
        option ports '4 6t'

  • 该配置自动生成,由02_network进行修改
		# 配置switch,一个lan,一个wan
		ucidef_add_switch "switch0" \
			"0:lan" "4:wan" "6@eth0"

		# 增加4g wan
		ucidef_set_interface "wan_4g" ifname "eth1" protocol "${2:-dhcp}" 

		# metric设置跃点,网口优先,,跃点小,优先级高,,需要同步修改config_generate,增加metric
		ucidef_set_interface "wan_4g" metric 3
		ucidef_set_interface "wan" metric 1
  • 02_network,并不直接生成/etc/config/network,而是生成一个board.json配置文件,其中network节点,就是有02_network添加
root@OpenWrt:/# cat /etc/board.json
{
        "model": {
                "id": "linkits7688",
                "name": "MediaTek LinkIt Smart 7688"
        },
        "led": {
                "led_wifi": {
                        "name": "wifi",
                        "sysfs": "green:wifi",
                        "type": "netdev",
                        "device": "wlan0",
                        "mode": "link tx rx"
                }
        },
        "switch": {
                "switch0": {
                        "enable": true,
                        "reset": true,
                        "ports": [
                                {
                                        "num": 0,
                                        "role": "lan"
                                },
                                {
                                        "num": 4,
                                        "role": "wan"
                                },
                                {
                                        "num": 6,
                                        "device": "eth0",
                                        "need_tag": false,
                                        "want_untag": false
                                }
                        ],
                        "roles": [
                                {
                                        "role": "lan",
                                        "ports": "0 6t",
                                        "device": "eth0.1"
                                },
                                {
                                        "role": "wan",
                                        "ports": "4 6t",
                                        "device": "eth0.2"
                                }
                        ]
                }
        },
        "network": {
                "lan": {
                        "ifname": "eth0.1",
                        "protocol": "static",
                        "macaddr": "ff:ff:ff:ff:ff:ff"
                },
                "wan": {
                        "ifname": "eth0.2",
                        "protocol": "dhcp",
                        "metric": "1",
                        "macaddr": "ff:ff:ff:ff:ff:ff"
                },
                "wan_4g": {
                        "ifname": "eth1",
                        "protocol": "dhcp",
                        "metric": "3"
                }
        }
}
root@OpenWrt:/#

  • 再由config_generate脚本,生成/etc/config/network、system等配置文件。
  • 默认没有metric选项,如下修改,增加metric
generate_network() {
	# eric++,metric
	local ifname macaddr protocol type ipaddr netmask metric

	json_select network
		json_select "$1"
			json_get_vars ifname macaddr protocol ipaddr netmask metric
		json_select ..
	json_select ..

	[ -n "$ifname" ] || return

	# force bridge for multi-interface devices (and lan)
	case "$1:$ifname" in
		*\ * | lan:*) type="bridge" ;;
	esac

	uci -q batch <<-EOF
		delete network.$1
		set network.$1='interface'
		set network.$1.type='$type'
		set network.$1.ifname='$ifname'
		set network.$1.proto='none'
		set network.$1.metric='$metric'
	EOF
  • 以上是openwrt network配置文件自动生成的逻辑,当然也可以在自定义启动脚本中调用uci API自行修改!

 类似资料: