Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| docs:guide-developer:network-scripting [2020/12/28 00:51] – Reformat broken mutliline code segments lemoer | docs:guide-developer:network-scripting [2023/07/19 09:34] (current) – [Get WAN gateway for unmanaged default route] add vgaetera | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Network scripts ====== | ====== Network scripts ====== | ||
| - | + | netifd can (probably) bring up a wired, static ip configuration without shell scripts. | |
| - | netifd can (probably) bring up a wired, static ip configuration without shell scripts. For everything else (PPPoE or 3G) it needs //protocol handlers// implemented as sets of shell functions. | + | For everything else (PPPoE or 3G) it needs //protocol handlers// implemented as sets of shell functions. |
| ===== Writing protocol handlers ===== | ===== Writing protocol handlers ===== | ||
| - | + | Each protocol handler is a shell script in ''/ | |
| - | Each protocol handler is a shell script in ''/ | + | It is run (or maybe sourced) when netifd daemon starts. |
| + | Changes made to the scripts do not take effect until netifd restarts. | ||
| + | The name of the file usually matches '' | ||
| To be able to access the network functions, the script needs to include the necessary shell scripts at the beginning: | To be able to access the network functions, the script needs to include the necessary shell scripts at the beginning: | ||
| - | < | + | < |
| #!/bin/sh | #!/bin/sh | ||
| - | . / | + | [ -n " |
| - | . ../ | + | . / |
| - | init_proto " | + | . ../ |
| + | init_proto " | ||
| } | } | ||
| </ | </ | ||
| Line 25: | Line 28: | ||
| ==== init_config ==== | ==== init_config ==== | ||
| + | The main purpose of this function is to let netifd know which parameters does this protocol have. | ||
| + | These parameters then can be stored in ''/ | ||
| - | The main purpose of this function is to let netifd know which parameters does this protocol have. These parameters then can be stored in ''/ | + | < |
| - | + | proto_protocolname_init_config() { | |
| - | < | + | proto_config_add_string " |
| - | proto_// | + | proto_config_add_int " |
| - | proto_config_add_string "//stringparam//" | + | proto_config_add_boolean " |
| - | proto_config_add_int "//intparam//" | + | ... |
| - | proto_config_add_boolean "//boolparam//" | + | |
| - | ... | + | |
| } | } | ||
| </ | </ | ||
| ==== Setup ==== | ==== Setup ==== | ||
| - | |||
| The setup procedure implements the actual protocol specifc configuration logic and interface bringup. | The setup procedure implements the actual protocol specifc configuration logic and interface bringup. | ||
| Line 46: | Line 48: | ||
| - Interface name (only if '' | - Interface name (only if '' | ||
| - | < | + | < |
| - | proto_// | + | proto_protocolname_setup() { |
| - | local config=" | + | local config=" |
| - | # set up the interface | + | # set up the interface |
| } | } | ||
| </ | </ | ||
| Line 55: | Line 57: | ||
| This function must be implemented by any protocol backend. | This function must be implemented by any protocol backend. | ||
| - | There' | + | There' |
| + | If '' | ||
| + | It waits for '' | ||
| If '' | If '' | ||
| - | < | + | < |
| - | proto_init_update " | + | proto_protocolname_setup () { |
| - | proto_send_update " | + | ... |
| + | proto_init_update " | ||
| + | proto_send_update " | ||
| + | ... | ||
| } | } | ||
| </ | </ | ||
| - | However, this only works once. If someone called '' | + | However, this only works once. |
| + | If someone called '' | ||
| ==== Teardown ==== | ==== Teardown ==== | ||
| - | + | Protocols that need special shutdown handling, for example to kill associated daemons, may implement a stop procedure. | |
| - | Protocols that need special shutdown handling, for example to kill associated daemons, may implement | + | This procedure is called when an interface is brought down before the associated UCI state is cleared. |
| - | a stop procedure. This procedure is called when an interface is brought down before the associated UCI | + | |
| - | state is cleared. | + | |
| The function is called when we do '' | The function is called when we do '' | ||
| Line 79: | Line 85: | ||
| - iface, the network device | - iface, the network device | ||
| - | < | + | < |
| - | proto_// | + | proto_protocolname_teardown() { |
| - | local config=" | + | local config=" |
| - | local iface=" | + | local iface=" |
| - | # tear down the interface | + | # tear down the interface |
| } | } | ||
| </ | </ | ||
| Line 90: | Line 96: | ||
| ==== Coldplug ==== | ==== Coldplug ==== | ||
| - | |||
| By default, only interfaces present in ''/ | By default, only interfaces present in ''/ | ||
| Protocols which use virtual interfaces must set two variables at the beginning of init_config. | Protocols which use virtual interfaces must set two variables at the beginning of init_config. | ||
| - | < | + | < |
| - | proto_// | + | proto_protocolname_init_config() { |
| - | no_device=1 | + | no_device=1 |
| - | available=1 | + | available=1 |
| - | ... | + | ... |
| } | } | ||
| </ | </ | ||
| + | |||
| + | ==== Debugging ==== | ||
| + | * STDERR in protos is redirected to the syslog. '' | ||
| + | * As '' | ||
| + | * There is a pending patch: https:// | ||
| + | |||
| + | ==== Available proto flags ==== | ||
| + | Flags can be added to a proto handler in '' | ||
| + | The information about all loaded protocols can be obtained by calling //ubus call network get_proto_handlers// | ||
| + | |||
| + | ^ Name ^ Name in //ubus call network get_proto_handlers// | ||
| + | | no_device | no_device | The interface does not have a lower device. <color # | ||
| + | | | immediate | <color # | ||
| + | | available | init_available | Initialize the device directly as available, without the device specified by ifname being present <color # | ||
| + | | renew_handler | renew_available | Has a renew handler, which can be called. <color # | ||
| + | | teardown_on_l3_link_down | teardown_on_l3_link_down | <color # | ||
| + | | | force_link_default | <color # | ||
| + | | no_proto_task | no_task | <color # | ||
| + | |||
| + | ==== Error codes ==== | ||
| + | If errors for interfaces occur, the json object in //ifstatus interfacename// | ||
| + | If there are no errors, this attribute is not existing. | ||
| + | |||
| + | ^ CODE ^ Meaning ^ | ||
| + | | NO_DEVICE | The configured device in ifname is not found. | | ||
| + | | DEVICE_CLAIM_FAILED | One of the reasons for this is, that the device configured by ifname does not exist. Usually this would result in NO_DEVICE as the device is only claimed when it is available. However when the proto flags // | ||
| + | | SETUP_FAILED | <color # | ||
| + | |||
| + | Custom error codes can be thrown from the proto scripts aswell. | ||
| + | This is done via '' | ||
| + | |||
| + | ==== How does it work internally? ==== | ||
| + | <code bash> | ||
| + | init_proto $proto $cmd $interface $data $ifname | ||
| + | </ | ||
| + | |||
| + | This function takes all all arguments from the // | ||
| + | |||
| + | If '' | ||
| + | * A json output dump of the results of '' | ||
| + | * Other parameters than '' | ||
| + | |||
| + | If '' | ||
| + | * If '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Otherwise the script will fail with something like: | ||
| + | * '' | ||
| ===== API ===== | ===== API ===== | ||
| + | The following functions are defined in ''/ | ||
| ==== netifd functions ==== | ==== netifd functions ==== | ||
| - | |||
| === initialization functions === | === initialization functions === | ||
| - | + | * '' | |
| - | == add_protocol | + | * '' |
| - | == init_proto | + | * '' |
| - | == proto_config_add_boolean | + | * '' |
| - | == proto_config_add_int | + | * '' |
| - | == proto_config_add_string | + | |
| === notification functions === | === notification functions === | ||
| - | |||
| Note: some of these function exit immediately. | Note: some of these function exit immediately. | ||
| + | These functions are dispatched to ubus and arrive [[commit>? | ||
| - | == proto_block_restart | + | * '' |
| - | == proto_init_update | + | * '' |
| - | == proto_kill_command | + | * '' |
| - | == proto_notify_error | + | * '' |
| - | == proto_run_command | + | * '' |
| - | We can start a daemon that maintains the connection asynchronously by calling '' | + | We can start a daemon that maintains the connection asynchronously by calling '' |
| + | Netifd remembers its pid. | ||
| + | It can be killed manually by calling '' | ||
| + | Netifd can automatically kill it, when the profile stopped. | ||
| - | == proto_set_available == | + | * '' |
| - | == proto_send_update | + | |
| - | == proto_set_available | + | It seems to avoid race conditions in protos. |
| + | We can register the following type of dependencies by calling: | ||
| + | * '' | ||
| + | * '' | ||
| + | * (maybe more?) | ||
| + | |||
| + | Only if '' | ||
| + | So we need another proto to be completed, we can use this here. | ||
| + | |||
| + | From some observations I think it looks like technically the '' | ||
| + | However, currently I do not know where to find the source code, so this is only a vague idea of what happens. | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| ==== common functions ==== | ==== common functions ==== | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| - | == json_add_string | + | ===== Examples ===== |
| - | == json_dump | + | [[https:// |
| - | == json_init | + | Replace automatic WAN detection with explicit interface definition if necessary. |
| - | == json_get_var | + | |
| - | == json_get_vars | + | ==== Get LAN address ==== |
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | NET_IF=" | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_get_ipaddr NET_ADDR " | ||
| + | network_get_ipaddr6 NET_ADDR6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN interface ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN L2 device ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_physdev NET_L2D " | ||
| + | network_get_physdev NET_L2D6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN L3 device ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_device NET_L3D " | ||
| + | network_get_device NET_L3D6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent configuration | ||
| + | uci get network.wan.device | ||
| + | uci get network.wan6.device | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN address ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_ipaddr NET_ADDR " | ||
| + | network_get_ipaddr6 NET_ADDR6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan.ipaddr | ||
| + | uci get network.wan6.ip6addr | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN gateway ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_gateway NET_GW " | ||
| + | network_get_gateway6 NET_GW6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan.gateway | ||
| + | uci get network.wan6.ip6gw | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN prefix ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_prefix6 NET_PFX6 " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan6.ip6prefix | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN gateway for unmanaged default route ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | ubus call network.interface dump \ | ||
| + | | jsonfilter -e " | ||
| + | [' | ||
| + | </ | ||