base-files: rename ethernet devs on known boards
Some platforms lack an established way to name netdevs; for example, on x86, PCIe-based ethernet interfaces will be named starting from eth0 in the order they are probed. This is a problem for many devices supported explicitly by OpenWrt which have hard-wired, standalone or on-CPU NICs not supported by DSA (which is usually used to rename the ports based on their ostensible function). To fix this, add a mapping between ethernet device name and sysfs device path to board.json; this allows us to configure ethernet device names we know about for a given board so that they correspond to external labeling. Signed-off-by: Martin Kennedy <hurricos@gmail.com>
This commit is contained in:
		 Martin Kennedy
					Martin Kennedy
				
			
				
					committed by
					
						 Paul Spooren
						Paul Spooren
					
				
			
			
				
	
			
			
			 Paul Spooren
						Paul Spooren
					
				
			
						parent
						
							fee31628c4
						
					
				
				
					commit
					b688bf83f9
				
			| @@ -114,6 +114,14 @@ ucidef_set_network_device_mac() { | |||||||
| 	json_select .. | 	json_select .. | ||||||
| } | } | ||||||
|  |  | ||||||
|  | ucidef_set_network_device_path() { | ||||||
|  | 	json_select_object "network_device" | ||||||
|  | 	json_select_object "$1" | ||||||
|  | 	json_add_string path "$2" | ||||||
|  | 	json_select .. | ||||||
|  | 	json_select .. | ||||||
|  | } | ||||||
|  |  | ||||||
| _ucidef_add_switch_port() { | _ucidef_add_switch_port() { | ||||||
| 	# inherited: $num $device $need_tag $want_untag $role $index $prev_role | 	# inherited: $num $device $need_tag $want_untag $role $index $prev_role | ||||||
| 	# inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 | 	# inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 | ||||||
|   | |||||||
| @@ -63,6 +63,20 @@ preinit_config_switch() { | |||||||
| 	json_select .. | 	json_select .. | ||||||
| } | } | ||||||
|  |  | ||||||
|  | preinit_config_port() { | ||||||
|  | 	local original | ||||||
|  |  | ||||||
|  | 	local netdev="$1" | ||||||
|  | 	local path="$2" | ||||||
|  |  | ||||||
|  | 	[ -d "/sys/devices/$path/net" ] || return | ||||||
|  | 	original="$(ls "/sys/devices/$path/net" | head -1)" | ||||||
|  |  | ||||||
|  | 	[ "$netdev" = "$original" ] && return | ||||||
|  |  | ||||||
|  | 	ip link set "$original" name "$netdev" | ||||||
|  | } | ||||||
|  |  | ||||||
| preinit_config_board() { | preinit_config_board() { | ||||||
| 	/bin/board_detect /tmp/board.json | 	/bin/board_detect /tmp/board.json | ||||||
|  |  | ||||||
| @@ -73,6 +87,35 @@ preinit_config_board() { | |||||||
| 	json_init | 	json_init | ||||||
| 	json_load "$(cat /tmp/board.json)" | 	json_load "$(cat /tmp/board.json)" | ||||||
|  |  | ||||||
|  | 	# Find the current highest eth* | ||||||
|  | 	max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1) | ||||||
|  | 	# Find and move netdevs using eth*s we are configuring | ||||||
|  | 	json_get_keys keys "network_device" | ||||||
|  | 	for netdev in $keys; do | ||||||
|  | 		json_select "network_device" | ||||||
|  | 			json_select "$netdev" | ||||||
|  | 				json_get_vars path path | ||||||
|  | 				next_eth="$(echo "$netdev" | grep 'eth[0-9]*' | tr -dc '[0-9]')" | ||||||
|  | 				[ "$next_eth" -gt "$max_eth" ] && max_eth=$next_eth | ||||||
|  | 				if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then | ||||||
|  | 					ip link set "$netdev" down | ||||||
|  | 					ip link set "$netdev" name eth$((++max_eth)) | ||||||
|  | 				fi | ||||||
|  | 			json_select .. | ||||||
|  | 		json_select .. | ||||||
|  | 	done | ||||||
|  |  | ||||||
|  | 	# Move interfaces by path to their netdev name | ||||||
|  | 	json_get_keys keys "network_device" | ||||||
|  | 	for netdev in $keys; do | ||||||
|  | 		json_select "network_device" | ||||||
|  | 			json_select "$netdev" | ||||||
|  | 				json_get_vars path path | ||||||
|  | 				[ -n "$path" ] && preinit_config_port "$netdev" "$path" | ||||||
|  | 			json_select .. | ||||||
|  | 		json_select .. | ||||||
|  | 	done | ||||||
|  |  | ||||||
| 	json_select network | 	json_select network | ||||||
| 		json_select "lan" | 		json_select "lan" | ||||||
| 			json_get_vars device | 			json_get_vars device | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user