diff --git a/interface-definitions/interfaces-bonding.xml b/interface-definitions/interfaces-bonding.xml index 88dbab6ab..7279e5993 100644 --- a/interface-definitions/interfaces-bonding.xml +++ b/interface-definitions/interfaces-bonding.xml @@ -1,673 +1,693 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="interfaces"> <children> <tagNode name="bonding" owner="${vyos_conf_scripts_dir}/interface-bonding.py"> <properties> <help>Bonding interface name</help> <priority>315</priority> <constraint> <regex>bond[0-9]+$</regex> </constraint> <constraintErrorMessage>Bonding interface must be named bondN</constraintErrorMessage> <valueHelp> <format>bondN</format> <description>Bonding interface name</description> </valueHelp> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <node name="arp-monitor"> <properties> <help>ARP link monitoring parameters</help> </properties> <children> <leafNode name="interval"> <properties> <help>ARP link monitoring interval</help> <valueHelp> <format>0-4294967295</format> <description>Specifies the ARP link monitoring frequency in milliseconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4294967295"/> </constraint> </properties> </leafNode> <leafNode name="target"> <properties> <help>IP address used for ARP monitoring</help> <valueHelp> <format>ipv4</format> <description>Network Time Protocol (NTP) IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> </children> </node> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> <help>DHCP client host name (overrides the system host name)</help> </properties> </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> + </properties> + </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="hash-policy"> <properties> <help>Bonding transmit hash policy</help> <completionHelp> <list>layer2 layer2+3 layer3+4</list> </completionHelp> <valueHelp> <format>layer2</format> <description>use MAC addresses to generate the hash (802.3ad, default)</description> </valueHelp> <valueHelp> <format>layer2+3</format> <description>combine MAC address and IP address to make hash</description> </valueHelp> <valueHelp> <format>layer3+4</format> <description>combine IP address and port to make hash</description> </valueHelp> <constraint> <regex>(layer2\\+3|layer3\\+4|layer2)</regex> </constraint> <constraintErrorMessage>hash-policy must be layer2 layer2+3 or layer3+4</constraintErrorMessage> </properties> </leafNode> <node name="ip"> <children> <leafNode name="arp-cache-timeout"> <properties> <help>ARP cache entry timeout in seconds</help> <valueHelp> <format>1-86400</format> <description>ARP cache entry timout in seconds (default 30)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-86400"/> </constraint> <constraintErrorMessage>ARP cache entry timeout must be between 1 and 86400 seconds</constraintErrorMessage> </properties> </leafNode> <leafNode name="enable-proxy-arp"> <properties> <help>Enable proxy-arp on this interface</help> <valueless/> </properties> </leafNode> <leafNode name="proxy-arp-pvlan"> <properties> <help>Enable private VLAN proxy ARP on this interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mode"> <properties> <help>Bonding mode</help> <completionHelp> <list>802.3ad active-backup broadcast round-robin transmit-load-balance adaptive-load-balance xor-hash</list> </completionHelp> <valueHelp> <format>802.3ad</format> <description>IEEE 802.3ad Dynamic link aggregation (Default)</description> </valueHelp> <valueHelp> <format>active-backup</format> <description>Fault tolerant: only one slave in the bond is active</description> </valueHelp> <valueHelp> <format>broadcast</format> <description>Fault tolerant: transmits everything on all slave interfaces</description> </valueHelp> <valueHelp> <format>round-robin</format> <description>Load balance: transmit packets in sequential order</description> </valueHelp> <valueHelp> <format>transmit-load-balance</format> <description>Load balance: adapts based on transmit load and speed</description> </valueHelp> <valueHelp> <format>adaptive-load-balance</format> <description>Load balance: adapts based on transmit and receive plus ARP</description> </valueHelp> <valueHelp> <format>xor-hash</format> <description>Distribute based on MAC address</description> </valueHelp> <constraint> <regex>(802.3ad|active-backup|broadcast|round-robin|transmit-load-balance|adaptive-load-balance|xor-hash)</regex> </constraint> <constraintErrorMessage>mode must be 802.3ad, active-backup, broadcast, round-robin, transmit-load-balance, adaptive-load-balance, or xor</constraintErrorMessage> </properties> </leafNode> <node name="member"> <properties> <help>Bridge member interfaces</help> </properties> <children> <leafNode name="interface"> <properties> <help>Member interface name</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces.py --bondable</script> </completionHelp> <multi/> </properties> </leafNode> </children> </node> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> <leafNode name="primary"> <properties> <help>Primary device interface</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces.py --bondable</script> </completionHelp> </properties> </leafNode> <tagNode name="vif-s"> <properties> <help>QinQ TAG-S Virtual Local Area Network (VLAN) ID</help> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> <help>DHCP client host name (overrides the system host name)</help> </properties> </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> + </properties> + </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="ethertype"> <properties> <help>Set Ethertype</help> <completionHelp> <list>0x88A8 0x8100</list> </completionHelp> <valueHelp> <format>0x88A8</format> <description>802.1ad</description> </valueHelp> <valueHelp> <format>0x8100</format> <description>802.1q</description> </valueHelp> <constraint> <regex>(0x88A8|0x8100)</regex> </constraint> <constraintErrorMessage>Ethertype must be 0x88A8 or 0x8100</constraintErrorMessage> </properties> </leafNode> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> <tagNode name="vif-c"> <properties> <help>QinQ TAG-C Virtual Local Area Network (VLAN) ID</help> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> <help>DHCP client host name (overrides the system host name)</help> </properties> </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> + </properties> + </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <tagNode name="vif"> <properties> <help>Virtual Local Area Network (VLAN) ID</help> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> <help>DHCP client host name (overrides the system host name)</help> </properties> </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> + </properties> + </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </tagNode> </children> </node> </interfaceDefinition> diff --git a/interface-definitions/interfaces-bridge.xml b/interface-definitions/interfaces-bridge.xml index 4b82972dc..c026c29b3 100644 --- a/interface-definitions/interfaces-bridge.xml +++ b/interface-definitions/interfaces-bridge.xml @@ -1,269 +1,274 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="interfaces"> <children> <tagNode name="bridge" owner="${vyos_conf_scripts_dir}/interface-bridge.py"> <properties> <help>Bridge interface name</help> <priority>470</priority> <constraint> <regex>^br[0-9]+$</regex> </constraint> <constraintErrorMessage>Bridge interface must be named brN</constraintErrorMessage> <valueHelp> <format>brN</format> <description>Bridge interface name</description> </valueHelp> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="aging"> <properties> <help>MAC address aging interval</help> <valueHelp> <format>0</format> <description>Disable MAC address learning (always flood)</description> </valueHelp> <valueHelp> <format>10-1000000</format> <description>MAC address aging time in seconds (default: 300)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-0 --range 10-1000000"/> </constraint> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> <help>DHCP client host name (overrides the system host name)</help> </properties> </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> + </properties> + </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="forwarding-delay"> <properties> <help>Forwarding delay</help> <valueHelp> <format>0-200</format> <description>Spanning Tree Protocol forwarding delay in seconds (default 15)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-200"/> </constraint> <constraintErrorMessage>Forwarding delay must be between 0 and 200 seconds</constraintErrorMessage> </properties> </leafNode> <leafNode name="hello-time"> <properties> <help>Hello packet advertisment interval</help> <valueHelp> <format>1-10</format> <description>Spanning Tree Protocol hello advertisement interval in seconds (default 2)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-10"/> </constraint> <constraintErrorMessage>Bridge Hello interval must be between 1 and 10 seconds</constraintErrorMessage> </properties> </leafNode> <node name="igmp"> <properties> <help>Internet Group Management Protocol (IGMP) settings</help> </properties> <children> <leafNode name="querier"> <properties> <help>Enable IGMP querier</help> <valueless/> </properties> </leafNode> </children> </node> <node name="ip"> <children> <leafNode name="arp-cache-timeout"> <properties> <help>ARP cache entry timeout in seconds</help> <valueHelp> <format>1-86400</format> <description>ARP cache entry timout in seconds (default 30)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-86400"/> </constraint> <constraintErrorMessage>ARP cache entry timeout must be between 1 and 86400 seconds</constraintErrorMessage> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="max-age"> <properties> <help>Interval at which neighbor bridges are removed</help> <valueHelp> <format>1-40</format> <description>Bridge maximum aging time in seconds (default 20)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-40"/> </constraint> <constraintErrorMessage>Bridge max aging value must be between 1 and 40 seconds</constraintErrorMessage> </properties> </leafNode> <node name="member"> <properties> <help>Bridge member interfaces</help> </properties> <children> <tagNode name="interface"> <properties> <help>Member interface name</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces.py --bridgeable</script> </completionHelp> </properties> <children> <leafNode name="cost"> <properties> <help>Bridge port cost</help> <valueHelp> <format>1-65535</format> <description>Path cost value for Spanning Tree Protocol</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> <constraintErrorMessage>Path cost value must be between 1 and 65535</constraintErrorMessage> </properties> </leafNode> <leafNode name="priority"> <properties> <help>Bridge port priority</help> <valueHelp> <format>0-63</format> <description>Bridge port priority</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-63"/> </constraint> <constraintErrorMessage>Port priority value must be between 0 and 63</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </node> <leafNode name="priority"> <properties> <help>Priority for this bridge</help> <valueHelp> <format>0-65535</format> <description>Bridge priority (default 32768)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-65535"/> </constraint> <constraintErrorMessage>Bridge priority must be between 0 and 65535 (multiples of 4096)</constraintErrorMessage> </properties> </leafNode> <leafNode name="stp"> <properties> <help>Enable spanning tree protocol</help> <valueless/> </properties> </leafNode> </children> </tagNode> </children> </node> </interfaceDefinition> diff --git a/interface-definitions/interfaces-ethernet.xml b/interface-definitions/interfaces-ethernet.xml index e4a56b216..96d2fda10 100644 --- a/interface-definitions/interfaces-ethernet.xml +++ b/interface-definitions/interfaces-ethernet.xml @@ -1,884 +1,904 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="interfaces"> <children> <tagNode name="ethernet" owner="${vyos_conf_scripts_dir}/interface-ethernet.py"> <properties> <help>Ethernet interface name</help> <priority>318</priority> <constraint> <regex>((eth|lan)[0-9]+|(eno|ens|enp|enx).+)$</regex> </constraint> <constraintErrorMessage>Invalid Ethernet interface name</constraintErrorMessage> <valueHelp> <format>ethN</format> <description>Ethernet interface name</description> </valueHelp> <valueHelp> <format>en[ospx]N</format> <description>Ethernet interface name</description> </valueHelp> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> - <help>DHCP client host name (overrides the system host name)</help> + <help>DHCP client host name (overrides system host name)</help> + </properties> + </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> </properties> </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-flow-control"> <properties> <help>Disable Ethernet flow control (pause frames)</help> <valueless/> </properties> </leafNode> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="duplex"> <properties> <help>Duplex mode</help> <completionHelp> <list>auto half full</list> </completionHelp> <valueHelp> <format>auto</format> <description>Auto negotiation (default)</description> </valueHelp> <valueHelp> <format>half</format> <description>Half duplex</description> </valueHelp> <valueHelp> <format>full</format> <description>Full duplex</description> </valueHelp> <constraint> <regex>(auto|half|full)</regex> </constraint> <constraintErrorMessage>duplex must be auto, half or full</constraintErrorMessage> </properties> </leafNode> <leafNode name="hw-id"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <node name="ip"> <children> <leafNode name="arp-cache-timeout"> <properties> <help>ARP cache entry timeout in seconds</help> <valueHelp> <format>1-86400</format> <description>ARP cache entry timout in seconds (default 30)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-86400"/> </constraint> <constraintErrorMessage>ARP cache entry timeout must be between 1 and 86400 seconds</constraintErrorMessage> </properties> </leafNode> <leafNode name="enable-proxy-arp"> <properties> <help>Enable proxy-arp on this interface</help> <valueless/> </properties> </leafNode> <leafNode name="proxy-arp-pvlan"> <properties> <help>Enable private VLAN proxy ARP on this interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> <node name="offload-options"> <properties> <help>Configurable offload options</help> </properties> <children> <leafNode name="generic-receive"> <properties> <help>Configure GRO (generic receive offload)</help> <completionHelp> <list>on off</list> </completionHelp> <valueHelp> <format>on</format> <description>Enable GRO (generic receive offload)</description> </valueHelp> <valueHelp> <format>off</format> <description>Disable GRO (generic receive offload)</description> </valueHelp> <constraint> <regex>(on|off)</regex> </constraint> <constraintErrorMessage>Must be either 'on' or 'off'</constraintErrorMessage> </properties> </leafNode> <leafNode name="generic-segmentation"> <properties> <help>Configure GSO (generic segmentation offload)</help> <completionHelp> <list>on off</list> </completionHelp> <valueHelp> <format>on</format> <description>Enable GSO (generic segmentation offload)</description> </valueHelp> <valueHelp> <format>off</format> <description>Disable GSO (generic segmentation offload)</description> </valueHelp> <constraint> <regex>(on|off)</regex> </constraint> <constraintErrorMessage>Must be either 'on' or 'off'</constraintErrorMessage> </properties> </leafNode> <leafNode name="scatter-gather"> <properties> <help>Configure scatter-gather option</help> <completionHelp> <list>on off</list> </completionHelp> <valueHelp> <format>on</format> <description>Enable scatter-gather</description> </valueHelp> <valueHelp> <format>off</format> <description>Disable scatter-gather</description> </valueHelp> <constraint> <regex>(on|off)</regex> </constraint> <constraintErrorMessage>Must be either 'on' or 'off'</constraintErrorMessage> </properties> </leafNode> <leafNode name="tcp-segmentation"> <properties> <help>Configure TSO (TCP segmentation offloading)</help> <completionHelp> <list>on off</list> </completionHelp> <valueHelp> <format>on</format> <description>Enable TSO (TCP segmentation offloading)</description> </valueHelp> <valueHelp> <format>off</format> <description>Disable TSO (TCP segmentation offloading)</description> </valueHelp> <constraint> <regex>(on|off)</regex> </constraint> <constraintErrorMessage>Must be either 'on' or 'off'</constraintErrorMessage> </properties> </leafNode> <leafNode name="udp-fragmentation"> <properties> <help>Configure UDP fragmentation offloading</help> <completionHelp> <list>on off</list> </completionHelp> <valueHelp> <format>on</format> <description>Enable UDP fragmentation offloading</description> </valueHelp> <valueHelp> <format>off</format> <description>Disable UDP fragmentation offloading</description> </valueHelp> <constraint> <regex>(on|off)</regex> </constraint> <constraintErrorMessage>Must be either 'on' or 'off'</constraintErrorMessage> </properties> </leafNode> </children> </node> <leafNode name="smp-affinity"> <properties> <help>CPU interrupt affinity mask</help> <completionHelp> <list>auto 10 100 1000 2500 5000 10000</list> </completionHelp> <valueHelp> <format>auto</format> <description>Auto negotiation (default)</description> </valueHelp> <valueHelp> <format>hex</format> <description>Bitmask representing CPUs that this NIC will interrupt</description> </valueHelp> <valueHelp> <format>hex,hex</format> <description>Bitmasks representing CPUs for interrupt and receive processing</description> </valueHelp> <constraint> <regex>(auto)</regex> <regex>[0-9a-f]+(|,[0-9a-f]+)$</regex> </constraint> <constraintErrorMessage>IRQ affinity mask must be hex value or auto</constraintErrorMessage> </properties> </leafNode> <leafNode name="speed"> <properties> <help>Link speed</help> <completionHelp> <list>auto 10 100 1000 2500 5000 10000 25000 40000 50000 100000</list> </completionHelp> <valueHelp> <format>auto</format> <description>Auto negotiation (default)</description> </valueHelp> <valueHelp> <format>10</format> <description>10 Mbit/sec</description> </valueHelp> <valueHelp> <format>100</format> <description>100 Mbit/sec</description> </valueHelp> <valueHelp> <format>1000</format> <description>1 Gbit/sec</description> </valueHelp> <valueHelp> <format>2500</format> <description>2.5 Gbit/sec</description> </valueHelp> <valueHelp> <format>5000</format> <description>5 Gbit/sec</description> </valueHelp> <valueHelp> <format>10000</format> <description>10 Gbit/sec</description> </valueHelp> <valueHelp> <format>25000</format> <description>25 Gbit/sec</description> </valueHelp> <valueHelp> <format>40000</format> <description>40 Gbit/sec</description> </valueHelp> <valueHelp> <format>50000</format> <description>50 Gbit/sec</description> </valueHelp> <valueHelp> <format>100000</format> <description>100 Gbit/sec</description> </valueHelp> <constraint> <regex>(auto|10|100|1000|2500|5000|10000|25000|40000|50000|100000)</regex> </constraint> <constraintErrorMessage>Speed must be auto, 10, 100, 1000, 2500, 5000, 10000, 25000, 40000, 50000 or 100000</constraintErrorMessage> </properties> </leafNode> <tagNode name="vif-s"> <properties> <help>QinQ TAG-S Virtual Local Area Network (VLAN) ID</help> <valueHelp> <format>0-4094</format> <description>QinQ TAG-S Virtual Local Area Network (VLAN) ID</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> - <help>DHCP client host name (overrides the system host name)</help> + <help>DHCP client host name (overrides system host name)</help> + </properties> + </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> </properties> </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="ethertype"> <properties> <help>Set Ethertype</help> <completionHelp> <list>0x88A8 0x8100</list> </completionHelp> <valueHelp> <format>0x88A8</format> <description>802.1ad</description> </valueHelp> <valueHelp> <format>0x8100</format> <description>802.1q</description> </valueHelp> <constraint> <regex>(0x88A8|0x8100)</regex> </constraint> <constraintErrorMessage>Ethertype must be 0x88A8 or 0x8100</constraintErrorMessage> </properties> </leafNode> <node name="ip"> <children> <leafNode name="enable-proxy-arp"> <properties> <help>Enable proxy-arp on this interface</help> <valueless/> </properties> </leafNode> <leafNode name="proxy-arp-pvlan"> <properties> <help>Enable private VLAN proxy ARP on this interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> <tagNode name="vif-c"> <properties> <help>QinQ TAG-C Virtual Local Area Network (VLAN) ID</help> <valueHelp> <format>0-4094</format> <description>QinQ TAG-C Virtual Local Area Network (VLAN) ID</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> - <help>DHCP client host name (overrides the system host name)</help> + <help>DHCP client host name (overrides system host name)</help> + </properties> + </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> </properties> </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <node name="ip"> <children> <leafNode name="enable-proxy-arp"> <properties> <help>Enable proxy-arp on this interface</help> <valueless/> </properties> </leafNode> <leafNode name="proxy-arp-pvlan"> <properties> <help>Enable private VLAN proxy ARP on this interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <tagNode name="vif"> <properties> <help>Virtual Local Area Network (VLAN) ID</help> <valueHelp> <format>0-4094</format> <description>Virtual Local Area Network (VLAN) ID</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4094"/> </constraint> <constraintErrorMessage>VLAN ID must be between 0 and 4094</constraintErrorMessage> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <completionHelp> <list>dhcp dhcpv6</list> </completionHelp> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 address and prefix length</description> </valueHelp> <valueHelp> <format>dhcp</format> <description>Dynamic Host Configuration Protocol</description> </valueHelp> <valueHelp> <format>dhcpv6</format> <description>Dynamic Host Configuration Protocol for IPv6</description> </valueHelp> <constraint> <validator name="ip-cidr"/> <regex>(dhcp|dhcpv6)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="description"> <properties> <help>Interface description</help> <constraint> <regex>^.{1,256}$</regex> </constraint> <constraintErrorMessage>Interface description too long (limit 256 characters)</constraintErrorMessage> </properties> </leafNode> <node name="dhcp-options"> <properties> <help>DHCP options</help> </properties> <children> <leafNode name="client-id"> <properties> <help>DHCP client identifier</help> </properties> </leafNode> <leafNode name="host-name"> <properties> - <help>DHCP client host name (overrides the system host name)</help> + <help>DHCP client host name (overrides system host name)</help> + </properties> + </leafNode> + <leafNode name="vendor-class-id"> + <properties> + <help>DHCP client vendor type</help> </properties> </leafNode> </children> </node> <node name="dhcpv6-options"> <properties> <help>DHCPv6 options</help> <priority>319</priority> </properties> <children> <leafNode name="parameters-only"> <properties> <help>Acquire only config parameters, no address</help> <valueless/> </properties> </leafNode> <leafNode name="temporary"> <properties> <help>IPv6 "temporary" address</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="disable-link-detect"> <properties> <help>Ignore link state changes</help> <valueless/> </properties> </leafNode> <leafNode name="disable"> <properties> <help>Disable this bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="egress-qos"> <properties> <help>VLAN egress QoS</help> <completionHelp> <script>echo Format for qos mapping \"0:1 1:6 7:6\"</script> </completionHelp> <constraint> <regex>[:0-7 ]+$</regex> </constraint> <constraintErrorMessage>QoS mapping should be in the format of \"0:7 2:3\" with numbers 0-9</constraintErrorMessage> </properties> </leafNode> <leafNode name="ingress-qos"> <properties> <help>VLAN ingress QoS</help> <completionHelp> <script>echo Format for qos mapping \"0:1 1:6 7:6\"</script> </completionHelp> <constraint> <regex>[:0-7 ]+$</regex> </constraint> <constraintErrorMessage>QoS mapping should be in the format of \"0:7 2:3\" with numbers 0-9</constraintErrorMessage> </properties> </leafNode> <node name="ip"> <children> <leafNode name="arp-cache-timeout"> <properties> <help>ARP cache entry timeout in seconds</help> <valueHelp> <format>1-86400</format> <description>ARP cache entry timout in seconds (default 30)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-86400"/> </constraint> <constraintErrorMessage>ARP cache entry timeout must be between 1 and 86400 seconds</constraintErrorMessage> </properties> </leafNode> <leafNode name="enable-proxy-arp"> <properties> <help>Enable proxy-arp on this interface</help> <valueless/> </properties> </leafNode> <leafNode name="proxy-arp-pvlan"> <properties> <help>Enable private VLAN proxy ARP on this interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="mac"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>h:h:h:h:h:h</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="mtu"> <properties> <help>Maximum Transmission Unit (MTU)</help> <valueHelp> <format>68-9000</format> <description>Maximum Transmission Unit</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 68-9000"/> </constraint> <constraintErrorMessage>MTU must be between 68 and 9000</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </tagNode> </children> </node> </interfaceDefinition> diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index 1c9cf6897..1022b88de 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -1,216 +1,221 @@ # Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. """ A library for retrieving value dicts from VyOS configs in a declarative fashion. """ from vyos import ConfigError def retrieve_config(path_hash, base_path, config): """ Retrieves a VyOS config as a dict according to a declarative description The description dict, passed in the first argument, must follow this format: ``field_name : <path, type, [inner_options_dict]>``. Supported types are: ``str`` (for normal nodes), ``list`` (returns a list of strings, for multi nodes), ``bool`` (returns True if valueless node exists), ``dict`` (for tag nodes, returns a dict indexed by node names, according to description in the third item of the tuple). Args: path_hash (dict): Declarative description of the config to retrieve base_path (list): A base path to prepend to all option paths config (vyos.config.Config): A VyOS config object Returns: dict: config dict """ config_hash = {} for k in path_hash: if type(path_hash[k]) != tuple: raise ValueError("In field {0}: expected a tuple, got a value {1}".format(k, str(path_hash[k]))) if len(path_hash[k]) < 2: raise ValueError("In field {0}: field description must be a tuple of at least two items, path (list) and type".format(k)) path = path_hash[k][0] if type(path) != list: raise ValueError("In field {0}: path must be a list, not a {1}".format(k, type(path))) typ = path_hash[k][1] if type(typ) != type: raise ValueError("In field {0}: type must be a type, not a {1}".format(k, type(typ))) path = base_path + path path_str = " ".join(path) if typ == str: config_hash[k] = config.return_value(path_str) elif typ == list: config_hash[k] = config.return_values(path_str) elif typ == bool: config_hash[k] = config.exists(path_str) elif typ == dict: try: inner_hash = path_hash[k][2] except IndexError: raise ValueError("The type of the \'{0}\' field is dict, but inner options hash is missing from the tuple".format(k)) config_hash[k] = {} nodes = config.list_nodes(path_str) for node in nodes: config_hash[k][node] = retrieve_config(inner_hash, path + [node], config) return config_hash def list_diff(first, second): """ Diff two dictionaries and return only unique items """ second = set(second) return [item for item in first if item not in second] def get_ethertype(ethertype_val): if ethertype_val == '0x88A8': return '802.1ad' elif ethertype_val == '0x8100': return '802.1q' else: raise ConfigError('invalid ethertype "{}"'.format(ethertype_val)) def vlan_to_dict(conf): """ Common used function which will extract VLAN related information from config and represent the result as Python dictionary. Function call's itself recursively if a vif-s/vif-c pair is detected. """ vlan = { 'id': conf.get_level().split()[-1], # get the '100' in 'interfaces bonding bond0 vif-s 100' 'address': [], 'address_remove': [], 'description': '', 'dhcp_client_id': '', 'dhcp_hostname': '', + 'dhcp_vendor_class_id': '', 'dhcpv6_prm_only': False, 'dhcpv6_temporary': False, 'disable': False, 'disable_link_detect': 1, 'egress_qos': '', 'egress_qos_changed': False, 'ingress_qos': '', 'ingress_qos_changed': False, 'mac': '', 'mtu': 1500 } # retrieve configured interface addresses if conf.exists('address'): vlan['address'] = conf.return_values('address') # Determine interface addresses (currently effective) - to determine which # address is no longer valid and needs to be removed from the bond eff_addr = conf.return_effective_values('address') act_addr = conf.return_values('address') vlan['address_remove'] = list_diff(eff_addr, act_addr) # retrieve interface description if conf.exists('description'): vlan['description'] = conf.return_value('description') # get DHCP client identifier if conf.exists('dhcp-options client-id'): vlan['dhcp_client_id'] = conf.return_value('dhcp-options client-id') # DHCP client host name (overrides the system host name) if conf.exists('dhcp-options host-name'): vlan['dhcp_hostname'] = conf.return_value('dhcp-options host-name') + # DHCP client vendor identifier + if conf.exists('dhcp-options vendor-class-id'): + vlan['dhcp_vendor_class_id'] = conf.return_value('dhcp-options vendor-class-id') + # DHCPv6 only acquire config parameters, no address if conf.exists('dhcpv6-options parameters-only'): vlan['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only') # DHCPv6 temporary IPv6 address if conf.exists('dhcpv6-options temporary'): vlan['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary') # ignore link state changes if conf.exists('disable-link-detect'): vlan['disable_link_detect'] = 2 # disable VLAN interface if conf.exists('disable'): vlan['disable'] = True # Media Access Control (MAC) address if conf.exists('mac'): vlan['mac'] = conf.return_value('mac') # Maximum Transmission Unit (MTU) if conf.exists('mtu'): vlan['mtu'] = int(conf.return_value('mtu')) # VLAN egress QoS if conf.exists('egress-qos'): vlan['egress_qos'] = conf.return_value('egress-qos') # egress changes QoS require VLAN interface recreation if vlan['egress_qos'] != conf.return_effective_value('egress-qos'): vlan['egress_qos_changed'] = True # VLAN ingress QoS if conf.exists('ingress-qos'): vlan['ingress_qos'] = conf.return_value('ingress-qos') # ingress changes QoS require VLAN interface recreation if vlan['ingress_qos'] != conf.return_effective_value('ingress-qos'): vlan['ingress_qos_changed'] = True # ethertype is mandatory on vif-s nodes and only exists here! # check if this is a vif-s node at all: if conf.get_level().split()[-2] == 'vif-s': vlan['vif_c'] = [] vlan['vif_c_remove'] = [] # ethertype uses a default of 0x88A8 tmp = '0x88A8' if conf.exists('ethertype'): tmp = conf.return_value('ethertype') vlan['ethertype'] = get_ethertype(tmp) # get vif-c interfaces (currently effective) - to determine which vif-c # interface is no longer present and needs to be removed eff_intf = conf.list_effective_nodes('vif-c') act_intf = conf.list_nodes('vif-c') vlan['vif_c_remove'] = list_diff(eff_intf, act_intf) # check if there is a Q-in-Q vlan customer interface # and call this function recursively if conf.exists('vif-c'): cfg_level = conf.get_level() # add new key (vif-c) to dictionary for vif in conf.list_nodes('vif-c'): # set config level to vif interface conf.set_level(cfg_level + ' vif-c ' + vif) vlan['vif_c'].append(vlan_to_dict(conf)) return vlan diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py index 0f80f4cea..23e66c089 100644 --- a/python/vyos/ifconfig.py +++ b/python/vyos/ifconfig.py @@ -1,1484 +1,1488 @@ # Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. import os import re import jinja2 from vyos.validate import * from ipaddress import IPv4Network, IPv6Address from netifaces import ifaddresses, AF_INET, AF_INET6 from subprocess import Popen, PIPE, STDOUT from time import sleep dhclient_base = r'/var/lib/dhcp/dhclient_' dhcp_cfg = """ # generated by ifconfig.py option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; timeout 60; retry 300; interface "{{ intf }}" { send host-name "{{ hostname }}"; {% if client_id -%} send dhcp-client-identifier "{{ client_id }}"; {% endif -%} + {% if vendor_class_id -%} + send vendor-class-identifier "{{ vendor_class_id }}"; + {% endif -%} request subnet-mask, broadcast-address, routers, domain-name-servers, rfc3442-classless-static-routes, domain-name, interface-mtu; require subnet-mask; } """ dhcpv6_cfg = """ # generated by ifconfig.py interface "{{ intf }}" { request routers, domain-name-servers, domain-name; } """ class Interface: def __init__(self, ifname, type=None): """ This is the base interface class which supports basic IP/MAC address operations as well as DHCP(v6). Other interface which represent e.g. and ethernet bridge are implemented as derived classes adding all additional functionality. DEBUG: This class has embedded debugging (print) which can be enabled by creating the following file: vyos@vyos# touch /tmp/vyos.ifconfig.debug Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') """ self._ifname = str(ifname) if not os.path.exists('/sys/class/net/{}'.format(ifname)) and not type: raise Exception('interface "{}" not found'.format(self._ifname)) if not os.path.exists('/sys/class/net/{}'.format(self._ifname)): cmd = 'ip link add dev {} type {}'.format(self._ifname, type) self._cmd(cmd) # per interface DHCP config files self._dhcp_cfg_file = dhclient_base + self._ifname + '.conf' self._dhcp_pid_file = dhclient_base + self._ifname + '.pid' self._dhcp_lease_file = dhclient_base + self._ifname + '.leases' # per interface DHCPv6 config files self._dhcpv6_cfg_file = dhclient_base + self._ifname + '.v6conf' self._dhcpv6_pid_file = dhclient_base + self._ifname + '.v6pid' self._dhcpv6_lease_file = dhclient_base + self._ifname + '.v6leases' # DHCP options self._dhcp_options = { 'intf' : self._ifname, 'hostname' : '', 'client_id' : '', + 'vendor_class_id' : '' } def _debug_msg(self, msg): if os.path.isfile('/tmp/vyos.ifconfig.debug'): print('DEBUG/{:<6} {}'.format(self._ifname, msg)) def _cmd(self, command): p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True) tmp = p.communicate()[0].strip() self._debug_msg("cmd '{}'".format(command)) if tmp.decode(): self._debug_msg("returned:\n{}".format(tmp.decode())) # do we need some error checking code here? return tmp def _read_sysfs(self, filename): """ Provide a single primitive w/ error checking for reading from sysfs. """ value = None with open(filename, 'r') as f: value = f.read().rstrip('\n') self._debug_msg("read '{}' < '{}'".format(value, filename)) return value def _write_sysfs(self, filename, value): """ Provide a single primitive w/ error checking for writing to sysfs. """ self._debug_msg("write '{}' > '{}'".format(value, filename)) with open(filename, 'w') as f: f.write(str(value)) return None def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # stop DHCP(v6) if running self._del_dhcp() self._del_dhcpv6() # remove all assigned IP addresses from interface - this is a bit redundant # as the kernel will remove all addresses on interface deletion, but we # can not delete ALL interfaces, see below for addr in self.get_addr(): self.del_addr(addr) # Ethernet interfaces can not be removed if type(self) == type(EthernetIf(self._ifname)): return # NOTE (Improvement): # after interface removal no other commands should be allowed # to be called and instead should raise an Exception: cmd = 'ip link del dev {}'.format(self._ifname) return self._cmd(cmd) def get_mtu(self): """ Get/set interface mtu in bytes. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_mtu() '1500' """ return self._read_sysfs('/sys/class/net/{}/mtu' .format(self._ifname)) def set_mtu(self, mtu): """ Get/set interface mtu in bytes. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_mtu(1400) >>> Interface('eth0').get_mtu() '1400' """ if mtu < 68 or mtu > 9000: raise ValueError('Invalid MTU size: "{}"'.format(mru)) return self._write_sysfs('/sys/class/net/{}/mtu' .format(self._ifname), mtu) def set_mac(self, mac): """ Set interface MAC (Media Access Contrl) address to given value. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_mac('00:50:ab:cd:ef:01') """ # on interface removal (ethernet) an empty string is passed - ignore it if not mac: return None # a mac address consits out of 6 octets octets = len(mac.split(':')) if octets != 6: raise ValueError('wrong number of MAC octets: {} '.format(octets)) # validate against the first mac address byte if it's a multicast # address if int(mac.split(':')[0], 16) & 1: raise ValueError('{} is a multicast MAC address'.format(mac)) # overall mac address is not allowed to be 00:00:00:00:00:00 if sum(int(i, 16) for i in mac.split(':')) == 0: raise ValueError('00:00:00:00:00:00 is not a valid MAC address') # check for VRRP mac address if mac.split(':')[0] == '0' and addr.split(':')[1] == '0' and mac.split(':')[2] == '94' and mac.split(':')[3] == '0' and mac.split(':')[4] == '1': raise ValueError('{} is a VRRP MAC address'.format(mac)) # Assemble command executed on system. Unfortunately there is no way # of altering the MAC address via sysfs cmd = 'ip link set dev {} address {}'.format(self._ifname, mac) return self._cmd(cmd) def set_arp_cache_tmo(self, tmo): """ Set ARP cache timeout value in seconds. Internal Kernel representation is in milliseconds. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_arp_cache_tmo(40) """ return self._write_sysfs('/proc/sys/net/ipv4/neigh/{0}/base_reachable_time_ms' .format(self._ifname), (int(tmo) * 1000)) def set_link_detect(self, link_filter): """ Configure kernel response in packets received on interfaces that are 'down' 0 - Allow packets to be received for the address on this interface even if interface is disabled or no carrier. 1 - Ignore packets received if interface associated with the incoming address is down. 2 - Ignore packets received if interface associated with the incoming address is down or has no carrier. Default value is 0. Note that some distributions enable it in startup scripts. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_link_detect(1) """ if int(link_filter) >= 0 and int(link_filter) <= 2: return self._write_sysfs('/proc/sys/net/ipv4/conf/{0}/link_filter' .format(self._ifname), link_filter) else: raise ValueError("Value out of range") def set_alias(self, ifalias=None): """ Set interface alias name used by e.g. SNMP Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_alias('VyOS upstream interface') to clear alias e.g. delete it use: >>> Interface('eth0').set_ifalias('') """ if not ifalias: # clear interface alias ifalias = '\0' self._write_sysfs('/sys/class/net/{}/ifalias' .format(self._ifname), ifalias) def get_state(self): """ Enable (up) / Disable (down) an interface Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_state() 'up' """ return self._read_sysfs('/sys/class/net/{}/operstate' .format(self._ifname)) def set_state(self, state): """ Enable (up) / Disable (down) an interface Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_state('down') >>> Interface('eth0').get_state() 'down' """ if state not in ['up', 'down']: raise ValueError('state must be "up" or "down"') # Assemble command executed on system. Unfortunately there is no way # to up/down an interface via sysfs cmd = 'ip link set dev {} {}'.format(self._ifname, state) tmp = self._cmd(cmd) # better safe then sorry - wait until the interface is really up # but only for a given period of time to avoid potential deadlocks! cnt = 0 while self.get_state() != state: cnt += 1 if cnt == 50: print('Interface {} could not be brought up in time ...'.format(self._ifname)) break # sleep 250ms sleep(0.250) return tmp def set_proxy_arp(self, enable): """ Set per interface proxy ARP configuration Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_proxy_arp(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/proc/sys/net/ipv4/conf/{}/proxy_arp' .format(self._ifname), enable) else: raise ValueError("Value out of range") def set_proxy_arp_pvlan(self, enable): """ Private VLAN proxy arp. Basically allow proxy arp replies back to the same interface (from which the ARP request/solicitation was received). This is done to support (ethernet) switch features, like RFC 3069, where the individual ports are NOT allowed to communicate with each other, but they are allowed to talk to the upstream router. As described in RFC 3069, it is possible to allow these hosts to communicate through the upstream router by proxy_arp'ing. Don't need to be used together with proxy_arp. This technology is known by different names: In RFC 3069 it is called VLAN Aggregation. Cisco and Allied Telesyn call it Private VLAN. Hewlett-Packard call it Source-Port filtering or port-isolation. Ericsson call it MAC-Forced Forwarding (RFC Draft). Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_proxy_arp_pvlan(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/proc/sys/net/ipv4/conf/{}/proxy_arp_pvlan' .format(self._ifname), enable) else: raise ValueError("Value out of range") def get_addr(self): """ Retrieve assigned IPv4 and IPv6 addresses from given interface. This is done using the netifaces and ipaddress python modules. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_addrs() ['172.16.33.30/24', 'fe80::20c:29ff:fe11:a174/64'] """ ipv4 = [] ipv6 = [] if AF_INET in ifaddresses(self._ifname).keys(): for v4_addr in ifaddresses(self._ifname)[AF_INET]: # we need to manually assemble a list of IPv4 address/prefix prefix = '/' + \ str(IPv4Network('0.0.0.0/' + v4_addr['netmask']).prefixlen) ipv4.append(v4_addr['addr'] + prefix) if AF_INET6 in ifaddresses(self._ifname).keys(): for v6_addr in ifaddresses(self._ifname)[AF_INET6]: # Note that currently expanded netmasks are not supported. That means # 2001:db00::0/24 is a valid argument while 2001:db00::0/ffff:ff00:: not. # see https://docs.python.org/3/library/ipaddress.html bits = bin( int(v6_addr['netmask'].replace(':', ''), 16)).count('1') prefix = '/' + str(bits) # we alsoneed to remove the interface suffix on link local # addresses v6_addr['addr'] = v6_addr['addr'].split('%')[0] ipv6.append(v6_addr['addr'] + prefix) return ipv4 + ipv6 def add_addr(self, addr): """ Add IP(v6) address to interface. Address is only added if it is not already assigned to that interface. addr: can be an IPv4 address, IPv6 address, dhcp or dhcpv6! IPv4: add IPv4 address to interface IPv6: add IPv6 address to interface dhcp: start dhclient (IPv4) on interface dhcpv6: start dhclient (IPv6) on interface Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.add_addr('192.0.2.1/24') >>> j.add_addr('2001:db8::ffff/64') >>> j.get_addr() ['192.0.2.1/24', '2001:db8::ffff/64'] """ if addr == 'dhcp': self._set_dhcp() elif addr == 'dhcpv6': self._set_dhcpv6() else: if not is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr add "{}" dev "{}"'.format(addr, self._ifname) return self._cmd(cmd) def del_addr(self, addr): """ Delete IP(v6) address to interface. Address is only added if it is assigned to that interface. addr: can be an IPv4 address, IPv6 address, dhcp or dhcpv6! IPv4: delete IPv4 address from interface IPv6: delete IPv6 address from interface dhcp: stop dhclient (IPv4) on interface dhcpv6: stop dhclient (IPv6) on interface Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.add_addr('2001:db8::ffff/64') >>> j.add_addr('192.0.2.1/24') >>> j.get_addr() ['192.0.2.1/24', '2001:db8::ffff/64'] >>> j.del_addr('192.0.2.1/24') >>> j.get_addr() ['2001:db8::ffff/64'] """ if addr == 'dhcp': self._del_dhcp() elif addr == 'dhcpv6': self._del_dhcpv6() else: if is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr del "{}" dev "{}"'.format(addr, self._ifname) return self._cmd(cmd) def get_dhcp_options(self): """ Return dictionary with supported DHCP options. Dictionary should be altered and send back via set_dhcp_options() so those options are applied when DHCP is run. """ return self._dhcp_options def set_dhcp_options(self, options): """ Store new DHCP options used by next run of DHCP client. """ self._dhcp_options = options # replace dhcpv4/v6 with systemd.networkd? def _set_dhcp(self): """ Configure interface as DHCP client. The dhclient binary is automatically started in background! Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.set_dhcp() """ dhcp = self.get_dhcp_options() if not dhcp['hostname']: # read configured system hostname. # maybe change to vyos hostd client ??? with open('/etc/hostname', 'r') as f: dhcp['hostname'] = f.read().rstrip('\n') # render DHCP configuration tmpl = jinja2.Template(dhcp_cfg) dhcp_text = tmpl.render(dhcp) with open(self._dhcp_cfg_file, 'w') as f: f.write(dhcp_text) if self.get_state() == 'up': cmd = 'start-stop-daemon --start --quiet --pidfile ' + \ self._dhcp_pid_file cmd += ' --exec /sbin/dhclient --' # now pass arguments to dhclient binary cmd += ' -4 -nw -cf {} -pf {} -lf {} {}'.format( self._dhcp_cfg_file, self._dhcp_pid_file, self._dhcp_lease_file, self._ifname) return self._cmd(cmd) def _del_dhcp(self): """ De-configure interface as DHCP clinet. All auto generated files like pid, config and lease will be removed. Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.del_dhcp() """ pid = 0 if os.path.isfile(self._dhcp_pid_file): with open(self._dhcp_pid_file, 'r') as f: pid = int(f.read()) else: self._debug_msg('No DHCP client PID found') return None # stop dhclient, we need to call dhclient and tell it should release the # aquired IP address. tcpdump tells me: # 172.16.35.103.68 > 172.16.35.254.67: [bad udp cksum 0xa0cb -> 0xb943!] BOOTP/DHCP, Request from 00:50:56:9d:11:df, length 300, xid 0x620e6946, Flags [none] (0x0000) # Client-IP 172.16.35.103 # Client-Ethernet-Address 00:50:56:9d:11:df # Vendor-rfc1048 Extensions # Magic Cookie 0x63825363 # DHCP-Message Option 53, length 1: Release # Server-ID Option 54, length 4: 172.16.35.254 # Hostname Option 12, length 10: "vyos" # cmd = '/sbin/dhclient -cf {} -pf {} -lf {} -r {}'.format( self._dhcp_cfg_file, self._dhcp_pid_file, self._dhcp_lease_file, self._ifname) self._cmd(cmd) # cleanup old config file if os.path.isfile(self._dhcp_cfg_file): os.remove(self._dhcp_cfg_file) # cleanup old pid file if os.path.isfile(self._dhcp_pid_file): os.remove(self._dhcp_pid_file) # cleanup old lease file if os.path.isfile(self._dhcp_lease_file): os.remove(self._dhcp_lease_file) def _set_dhcpv6(self): """ Configure interface as DHCPv6 client. The dhclient binary is automatically started in background! Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.set_dhcpv6() """ dhcpv6 = { 'intf': self._ifname } # render DHCP configuration tmpl = jinja2.Template(dhcpv6_cfg) dhcpv6_text = tmpl.render(dhcpv6) with open(self._dhcpv6_cfg_file, 'w') as f: f.write(dhcpv6_text) if self.get_state() == 'up': # https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1447715 # # wee need to wait for IPv6 DAD to finish once and interface is added # this suxx :-( sleep(5) # no longer accept router announcements on this interface cmd = 'sysctl -q -w net.ipv6.conf.{}.accept_ra=0'.format(self._ifname) self._cmd(cmd) # assemble command-line to start DHCPv6 client (dhclient) cmd = 'start-stop-daemon --start --quiet --pidfile ' + \ self._dhcpv6_pid_file cmd += ' --exec /sbin/dhclient --' # now pass arguments to dhclient binary cmd += ' -6 -nw -cf {} -pf {} -lf {} {}'.format( self._dhcpv6_cfg_file, self._dhcpv6_pid_file, self._dhcpv6_lease_file, self._ifname) return self._cmd(cmd) def _del_dhcpv6(self): """ De-configure interface as DHCPv6 clinet. All auto generated files like pid, config and lease will be removed. Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.del_dhcpv6() """ pid = 0 if os.path.isfile(self._dhcpv6_pid_file): with open(self._dhcpv6_pid_file, 'r') as f: pid = int(f.read()) else: self._debug_msg('No DHCPv6 client PID found') return None # stop dhclient cmd = 'start-stop-daemon --stop --quiet --pidfile {}'.format(self._dhcpv6_pid_file) self._cmd(cmd) # accept router announcements on this interface cmd = 'sysctl -q -w net.ipv6.conf.{}.accept_ra=1'.format(self._ifname) self._cmd(cmd) # cleanup old config file if os.path.isfile(self._dhcpv6_cfg_file): os.remove(self._dhcpv6_cfg_file) # cleanup old pid file if os.path.isfile(self._dhcpv6_pid_file): os.remove(self._dhcpv6_pid_file) # cleanup old lease file if os.path.isfile(self._dhcpv6_lease_file): os.remove(self._dhcpv6_lease_file) class LoopbackIf(Interface): """ The loopback device is a special, virtual network interface that your router uses to communicate with itself. """ def __init__(self, ifname): super().__init__(ifname, type='loopback') def remove(self): """ Loopback interface can not be deleted from operating system. We can only remove all assigned IP addresses. Example: >>> from vyos.ifconfig import Interface >>> i = LoopbackIf('lo').remove() """ # remove all assigned IP addresses from interface for addr in self.get_addr(): self.del_addr(addr) # question: do we also delerte the loopback address? 127.0.0.1/8 class DummyIf(Interface): """ A dummy interface is entirely virtual like, for example, the loopback interface. The purpose of a dummy interface is to provide a device to route packets through without actually transmitting them. """ def __init__(self, ifname): super().__init__(ifname, type='dummy') class STPIf(Interface): """ A spanning-tree capable interface. This applies only to bridge port member interfaces! """ def __init__(self, ifname): super().__init__(ifname) def set_path_cost(self, cost): """ Set interface path cost, only relevant for STP enabled interfaces Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_path_cost(4) """ if not os.path.isfile('/sys/class/net/{}/brport/path_cost' .format(self._ifname)): raise TypeError('{} is not a bridge port member'.format(self._ifname)) return self._write_sysfs('/sys/class/net/{}/brport/path_cost' .format(self._ifname), cost) def set_path_priority(self, priority): """ Set interface path priority, only relevant for STP enabled interfaces Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_path_priority(4) """ if not os.path.isfile('/sys/class/net/{}/brport/priority' .format(self._ifname)): raise TypeError('{} is not a bridge port member'.format(self._ifname)) return self._write_sysfs('/sys/class/net/{}/brport/priority' .format(self._ifname), priority) class BridgeIf(Interface): """ A bridge is a way to connect two Ethernet segments together in a protocol independent way. Packets are forwarded based on Ethernet address, rather than IP address (like a router). Since forwarding is done at Layer 2, all protocols can go transparently through a bridge. The Linux bridge code implements a subset of the ANSI/IEEE 802.1d standard. """ def __init__(self, ifname): super().__init__(ifname, type='bridge') def set_ageing_time(self, time): """ Set bridge interface MAC address aging time in seconds. Internal kernel representation is in centiseconds. Kernel default is 300 seconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').ageing_time(2) """ time = int(time) * 100 return self._write_sysfs('/sys/class/net/{}/bridge/ageing_time' .format(self._ifname), time) def set_forward_delay(self, time): """ Set bridge forwarding delay in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').forward_delay(15) """ return self._write_sysfs('/sys/class/net/{}/bridge/forward_delay' .format(self._ifname), (int(time) * 100)) def set_hello_time(self, time): """ Set bridge hello time in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_hello_time(2) """ return self._write_sysfs('/sys/class/net/{}/bridge/hello_time' .format(self._ifname), (int(time) * 100)) def set_max_age(self, time): """ Set bridge max message age in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').set_max_age(30) """ return self._write_sysfs('/sys/class/net/{}/bridge/max_age' .format(self._ifname), (int(time) * 100)) def set_priority(self, priority): """ Set bridge max aging time in seconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_priority(8192) """ return self._write_sysfs('/sys/class/net/{}/bridge/priority' .format(self._ifname), priority) def set_stp(self, state): """ Set bridge STP (Spanning Tree) state. 0 -> STP disabled, 1 -> STP enabled Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_stp(1) """ if int(state) >= 0 and int(state) <= 1: return self._write_sysfs('/sys/class/net/{}/bridge/stp_state' .format(self._ifname), state) else: raise ValueError("Value out of range") def set_multicast_querier(self, enable): """ Sets whether the bridge actively runs a multicast querier or not. When a bridge receives a 'multicast host membership' query from another network host, that host is tracked based on the time that the query was received plus the multicast query interval time. Use enable=1 to enable or enable=0 to disable Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').set_multicast_querier(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/sys/class/net/{}/bridge/multicast_querier' .format(self._ifname), enable) else: raise ValueError("Value out of range") def add_port(self, interface): """ Add physical interface to bridge (member port) Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').add_port('eth0') >>> BridgeIf('br0').add_port('eth1') """ cmd = 'ip link set dev {} master {}'.format(interface, self._ifname) return self._cmd(cmd) def del_port(self, interface): """ Remove member port from bridge instance. Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').del_port('eth1') """ cmd = 'ip link set dev {} nomaster'.format(interface) return self._cmd(cmd) class VLANIf(Interface): """ This class handels the creation and removal of a VLAN interface. It serves as base class for BondIf and EthernetIf. """ def __init__(self, ifname, type=None): super().__init__(ifname, type) def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # Do we have sub interfaces (VLANs)? We apply a regex matching # subinterfaces (indicated by a .) of a parent interface. # # As interfaces need to be deleted "in order" starting from Q-in-Q # we delete them first. vlan_ifs = [f for f in os.listdir(r'/sys/class/net') \ if re.match(self._ifname + r'(?:\.\d+)(?:\.\d+)', f)] for vlan in vlan_ifs: Interface(vlan).remove() # After deleting all Q-in-Q interfaces delete other VLAN interfaces # which probably acted as parent to Q-in-Q or have been regular 802.1q # interface. vlan_ifs = [f for f in os.listdir(r'/sys/class/net') \ if re.match(self._ifname + r'(?:\.\d+)', f)] for vlan in vlan_ifs: Interface(vlan).remove() # All subinterfaces are now removed, continue on the physical interface super().remove() def add_vlan(self, vlan_id, ethertype='', ingress_qos='', egress_qos=''): """ A virtual LAN (VLAN) is any broadcast domain that is partitioned and isolated in a computer network at the data link layer (OSI layer 2). Use this function to create a new VLAN interface on a given physical interface. This function creates both 802.1q and 802.1ad (Q-in-Q) interfaces. Proto parameter is used to indicate VLAN type. A new object of type VLANIf is returned once the interface has been created. @param ethertype: If specified, create 802.1ad or 802.1q Q-in-Q VLAN interface @param ingress_qos: Defines a mapping of VLAN header prio field to the Linux internal packet priority on incoming frames. @param ingress_qos: Defines a mapping of Linux internal packet priority to VLAN header prio field but for outgoing frames. Example: >>> from vyos.ifconfig import VLANIf >>> i = VLANIf('eth0') >>> i.add_vlan(10) """ vlan_ifname = self._ifname + '.' + str(vlan_id) if not os.path.exists('/sys/class/net/{}'.format(vlan_ifname)): self._vlan_id = int(vlan_id) if ethertype: self._ethertype = ethertype ethertype = 'proto {}'.format(ethertype) # Optional ingress QOS mapping opt_i = '' if ingress_qos: opt_i = 'ingress-qos-map ' + ingress_qos # Optional egress QOS mapping opt_e = '' if egress_qos: opt_e = 'egress-qos-map ' + egress_qos # create interface in the system cmd = 'ip link add link {intf} name {intf}.{vlan} type vlan {proto} id {vlan} {opt_e} {opt_i}' \ .format(intf=self._ifname, vlan=self._vlan_id, proto=ethertype, opt_e=opt_e, opt_i=opt_i) self._cmd(cmd) # return new object mapping to the newly created interface # we can now work on this object for e.g. IP address setting # or interface description and so on return VLANIf(vlan_ifname) def del_vlan(self, vlan_id): """ Remove VLAN interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import VLANIf >>> i = VLANIf('eth0.10') >>> i.del_vlan() """ vlan_ifname = self._ifname + '.' + str(vlan_id) VLANIf(vlan_ifname).remove() class EthernetIf(VLANIf): """ Abstraction of a Linux Ethernet Interface """ def __init__(self, ifname): super().__init__(ifname) def get_driver_name(self): """ Return the driver name used by NIC. Some NICs don't support all features e.g. changing link-speed, duplex Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.get_driver_name() 'vmxnet3' """ link = os.readlink('/sys/class/net/{}/device/driver/module'.format(self._ifname)) return os.path.basename(link) def set_flow_control(self, enable): """ Changes the pause parameters of the specified Ethernet device. @param enable: true -> enable pause frames, false -> disable pause frames Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_flow_control(True) """ if enable not in ['on', 'off']: raise ValueError("Value out of range") if self.get_driver_name() in ['vmxnet3', 'virtio_net']: self._debug_msg('{} driver does not support changing flow control settings!' .format(self.get_driver_name())) return # Assemble command executed on system. Unfortunately there is no way # to change this setting via sysfs cmd = '/sbin/ethtool --pause {0} autoneg {1} tx {1} rx {1}'.format( self._ifname, enable) try: # An exception will be thrown if the settings are not changed return self._cmd(cmd) except CalledProcessError: pass def set_speed_duplex(self, speed, duplex): """ Set link speed in Mbit/s and duplex. @speed can be any link speed in MBit/s, e.g. 10, 100, 1000 auto @duplex can be half, full, auto Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_speed_duplex('auto', 'auto') """ if speed not in ['auto', '10', '100', '1000', '2500', '5000', '10000', '25000', '40000', '50000', '100000', '400000']: raise ValueError("Value out of range (speed)") if duplex not in ['auto', 'full', 'half']: raise ValueError("Value out of range (duplex)") if self.get_driver_name() in ['vmxnet3', 'virtio_net']: self._debug_msg('{} driver does not support changing speed/duplex settings!' .format(self.get_driver_name())) return cmd = '/sbin/ethtool -s {}'.format(self._ifname) if speed == 'auto' or duplex == 'auto': cmd += ' autoneg on' else: cmd += ' speed {} duplex {} autoneg off'.format(speed, duplex) return self._cmd(cmd) def set_gro(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_gro('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} gro {}'.format(self._ifname, state) return self._cmd(cmd) def set_gso(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_gso('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} gso {}'.format(self._ifname, state) return self._cmd(cmd) def set_sg(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_sg('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} sg {}'.format(self._ifname, state) return self._cmd(cmd) def set_tso(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_tso('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} tso {}'.format(self._ifname, state) return self._cmd(cmd) def set_ufo(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_udp_offload('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} ufo {}'.format(self._ifname, state) return self._cmd(cmd) class BondIf(VLANIf): """ The Linux bonding driver provides a method for aggregating multiple network interfaces into a single logical "bonded" interface. The behavior of the bonded interfaces depends upon the mode; generally speaking, modes provide either hot standby or load balancing services. Additionally, link integrity monitoring may be performed. """ def __init__(self, ifname): super().__init__(ifname, type='bond') def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # when a bond member gets deleted, all members are placed in A/D state # even when they are enabled inside CLI. This will make the config # and system look async. slave_list = [] for s in self.get_slaves(): slave = { 'ifname' : s, 'state': Interface(s).get_state() } slave_list.append(slave) # remove bond master which places members in disabled state super().remove() # replicate previous interface state before bond destruction back to # physical interface for slave in slave_list: i = Interface(slave['ifname']) i.set_state(slave['state']) def set_hash_policy(self, mode): """ Selects the transmit hash policy to use for slave selection in balance-xor, 802.3ad, and tlb modes. Possible values are: layer2, layer2+3, layer3+4, encap2+3, encap3+4. The default value is layer2 Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_hash_policy('layer2+3') """ if not mode in ['layer2', 'layer2+3', 'layer3+4', 'encap2+3', 'encap3+4']: raise ValueError("Value out of range") return self._write_sysfs('/sys/class/net/{}/bonding/xmit_hash_policy' .format(self._ifname), mode) def set_arp_interval(self, interval): """ Specifies the ARP link monitoring frequency in milliseconds. The ARP monitor works by periodically checking the slave devices to determine whether they have sent or received traffic recently (the precise criteria depends upon the bonding mode, and the state of the slave). Regular traffic is generated via ARP probes issued for the addresses specified by the arp_ip_target option. If ARP monitoring is used in an etherchannel compatible mode (modes 0 and 2), the switch should be configured in a mode that evenly distributes packets across all links. If the switch is configured to distribute the packets in an XOR fashion, all replies from the ARP targets will be received on the same link which could cause the other team members to fail. value of 0 disables ARP monitoring. The default value is 0. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_arp_interval('100') """ if int(interval) == 0: """ Specifies the MII link monitoring frequency in milliseconds. This determines how often the link state of each slave is inspected for link failures. A value of zero disables MII link monitoring. A value of 100 is a good starting point. """ return self._write_sysfs('/sys/class/net/{}/bonding/miimon' .format(self._ifname), interval) else: return self._write_sysfs('/sys/class/net/{}/bonding/arp_interval' .format(self._ifname), interval) def get_arp_ip_target(self): """ Specifies the IP addresses to use as ARP monitoring peers when arp_interval is > 0. These are the targets of the ARP request sent to determine the health of the link to the targets. Specify these values in ddd.ddd.ddd.ddd format. Multiple IP addresses must be separated by a comma. At least one IP address must be given for ARP monitoring to function. The maximum number of targets that can be specified is 16. The default value is no IP addresses. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').get_arp_ip_target() '192.0.2.1' """ return self._read_sysfs('/sys/class/net/{}/bonding/arp_ip_target' .format(self._ifname)) def set_arp_ip_target(self, target): """ Specifies the IP addresses to use as ARP monitoring peers when arp_interval is > 0. These are the targets of the ARP request sent to determine the health of the link to the targets. Specify these values in ddd.ddd.ddd.ddd format. Multiple IP addresses must be separated by a comma. At least one IP address must be given for ARP monitoring to function. The maximum number of targets that can be specified is 16. The default value is no IP addresses. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_arp_ip_target('192.0.2.1') >>> BondIf('bond0').get_arp_ip_target() '192.0.2.1' """ return self._write_sysfs('/sys/class/net/{}/bonding/arp_ip_target' .format(self._ifname), target) def add_port(self, interface): """ Enslave physical interface to bond. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').add_port('eth0') >>> BondIf('bond0').add_port('eth1') """ # An interface can only be added to a bond if it is in 'down' state. If # interface is in 'up' state, the following Kernel error will be thrown: # bond0: eth1 is up - this may be due to an out of date ifenslave. Interface(interface).set_state('down') return self._write_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname), '+' + interface) def del_port(self, interface): """ Remove physical port from bond Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').del_port('eth1') """ return self._write_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname), '-' + interface) def get_slaves(self): """ Return a list with all configured slave interfaces on this bond. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').get_slaves() ['eth1', 'eth2'] """ slaves = self._read_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname)) return list(map(str, slaves.split())) def set_primary(self, interface): """ A string (eth0, eth2, etc) specifying which slave is the primary device. The specified device will always be the active slave while it is available. Only when the primary is off-line will alternate devices be used. This is useful when one slave is preferred over another, e.g., when one slave has higher throughput than another. The primary option is only valid for active-backup, balance-tlb and balance-alb mode. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_primary('eth2') """ if not interface: # reset primary interface interface = '\0' return self._write_sysfs('/sys/class/net/{}/bonding/primary' .format(self._ifname), interface) def set_mode(self, mode): """ Specifies one of the bonding policies. The default is balance-rr (round robin). Possible values are: balance-rr, active-backup, balance-xor, broadcast, 802.3ad, balance-tlb, balance-alb NOTE: the bonding mode can not be changed when the bond itself has slaves Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_mode('802.3ad') """ if not mode in [ 'balance-rr', 'active-backup', 'balance-xor', 'broadcast', '802.3ad', 'balance-tlb', 'balance-alb']: raise ValueError("Value out of range") return self._write_sysfs('/sys/class/net/{}/bonding/mode' .format(self._ifname), mode) class WireGuardIf(Interface): """ Wireguard interface class, contains a comnfig dictionary since wireguard VPN is being comnfigured via the wg command rather than writing the config into a file. Otherwise if a pre-shared key is used (symetric enryption key), it would we exposed within multiple files. Currently it's only within the config.boot if the config was saved. Example: >>> from vyos.ifconfig import WireGuardIf as wg_if >>> wg_intfc = wg_if("wg01") >>> print (wg_intfc.wg_config) {'private-key': None, 'keepalive': 0, 'endpoint': None, 'port': 0, 'allowed-ips': [], 'pubkey': None, 'fwmark': 0, 'psk': '/dev/null'} >>> wg_intfc.wg_config['keepalive'] = 100 >>> print (wg_intfc.wg_config) {'private-key': None, 'keepalive': 100, 'endpoint': None, 'port': 0, 'allowed-ips': [], 'pubkey': None, 'fwmark': 0, 'psk': '/dev/null'} """ def __init__(self, ifname): super().__init__(ifname, type='wireguard') self.config = { 'port': 0, 'private-key': None, 'pubkey': None, 'psk': '/dev/null', 'allowed-ips': [], 'fwmark': 0x00, 'endpoint': None, 'keepalive': 0 } def update(self): if not self.config['private-key']: raise ValueError("private key required") else: # fmask permission check? pass cmd = "wg set {} ".format(self._ifname) cmd += "listen-port {} ".format(self.config['port']) cmd += "fwmark {} ".format(str(self.config['fwmark'])) cmd += "private-key {} ".format(self.config['private-key']) cmd += "peer {} ".format(self.config['pubkey']) cmd += " preshared-key {} ".format(self.config['psk']) cmd += " allowed-ips " for aip in self.config['allowed-ips']: if aip != self.config['allowed-ips'][-1]: cmd += aip + "," else: cmd += aip if self.config['endpoint']: cmd += " endpoint {}".format(self.config['endpoint']) cmd += " persistent-keepalive {}".format(self.config['keepalive']) self._cmd(cmd) # remove psk since it isn't required anymore and is saved in the cli # config only !! if self.config['psk'] != '/dev/null': if os.path.exists(self.config['psk']): os.remove(self.config['psk']) def remove_peer(self, peerkey): """ Remove a peer of an interface, peers are identified by their public key. Giving it a readable name is a vyos feature, to remove a peer the pubkey and the interface is needed, to remove the entry. """ cmd = "wg set {0} peer {1} remove".format( self._ifname, str(peerkey)) return self._cmd(cmd) class VXLANIf(Interface, ): """ The VXLAN protocol is a tunnelling protocol designed to solve the problem of limited VLAN IDs (4096) in IEEE 802.1q. With VXLAN the size of the identifier is expanded to 24 bits (16777216). VXLAN is described by IETF RFC 7348, and has been implemented by a number of vendors. The protocol runs over UDP using a single destination port. This document describes the Linux kernel tunnel device, there is also a separate implementation of VXLAN for Openvswitch. Unlike most tunnels, a VXLAN is a 1 to N network, not just point to point. A VXLAN device can learn the IP address of the other endpoint either dynamically in a manner similar to a learning bridge, or make use of statically-configured forwarding entries. For more information please refer to: https://www.kernel.org/doc/Documentation/networking/vxlan.txt """ def __init__(self, ifname, config=''): if config: self._ifname = ifname if not os.path.exists('/sys/class/net/{}'.format(self._ifname)): # we assume that by default a multicast interface is created group = 'group {}'.format(config['group']) # if remote host is specified we ignore the multicast address if config['remote']: group = 'remote {}'.format(config['remote']) # an underlay device is not always specified dev = '' if config['dev']: dev = 'dev {}'.format(config['dev']) cmd = 'ip link add {intf} type vxlan id {vni} {grp_rem} {dev} dstport {port}' \ .format(intf=self._ifname, vni=config['vni'], grp_rem=group, dev=dev, port=config['port']) self._cmd(cmd) super().__init__(ifname, type='vxlan') @staticmethod def get_config(): """ VXLAN interfaces require a configuration when they are added using iproute2. This static method will provide the configuration dictionary used by this class. Example: >> dict = VXLANIf().get_config() """ config = { 'vni': 0, 'dev': '', 'group': '', 'port': 8472, # The Linux implementation of VXLAN pre-dates # the IANA's selection of a standard destination port 'remote': '' } return config diff --git a/src/conf_mode/interface-bonding.py b/src/conf_mode/interface-bonding.py index 7caf9e0e8..49d2a05d4 100755 --- a/src/conf_mode/interface-bonding.py +++ b/src/conf_mode/interface-bonding.py @@ -1,465 +1,473 @@ #!/usr/bin/env python3 # # Copyright (C) 2019 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os from copy import deepcopy from sys import exit from netifaces import interfaces from vyos.ifconfig import BondIf, VLANIf from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config from vyos import ConfigError default_config_data = { 'address': [], 'address_remove': [], 'arp_mon_intvl': 0, 'arp_mon_tgt': [], 'description': '', 'deleted': False, 'dhcp_client_id': '', 'dhcp_hostname': '', + 'dhcp_vendor_class_id': '', 'dhcpv6_prm_only': False, 'dhcpv6_temporary': False, 'disable': False, 'disable_link_detect': 1, 'hash_policy': 'layer2', 'ip_arp_cache_tmo': 30, 'ip_proxy_arp': 0, 'ip_proxy_arp_pvlan': 0, 'intf': '', 'mac': '', 'mode': '802.3ad', 'member': [], 'mtu': 1500, 'primary': '', 'vif_s': [], 'vif_s_remove': [], 'vif': [], 'vif_remove': [] } def get_bond_mode(mode): if mode == 'round-robin': return 'balance-rr' elif mode == 'active-backup': return 'active-backup' elif mode == 'xor-hash': return 'balance-xor' elif mode == 'broadcast': return 'broadcast' elif mode == '802.3ad': return '802.3ad' elif mode == 'transmit-load-balance': return 'balance-tlb' elif mode == 'adaptive-load-balance': return 'balance-alb' else: raise ConfigError('invalid bond mode "{}"'.format(mode)) def apply_vlan_config(vlan, config): """ Generic function to apply a VLAN configuration from a dictionary to a VLAN interface """ if type(vlan) != type(VLANIf("lo")): raise TypeError() # update interface description used e.g. within SNMP vlan.set_alias(config['description']) # ignore link state changes vlan.set_link_detect(config['disable_link_detect']) # Maximum Transmission Unit (MTU) vlan.set_mtu(config['mtu']) # Change VLAN interface MAC address if config['mac']: vlan.set_mac(config['mac']) # enable/disable VLAN interface if config['disable']: vlan.set_state('down') else: vlan.set_state('up') # Configure interface address(es) # - not longer required addresses get removed first # - newly addresses will be added second for addr in config['address_remove']: vlan.del_addr(addr) for addr in config['address']: vlan.add_addr(addr) def get_config(): # initialize kernel module if not loaded if not os.path.isfile('/sys/class/net/bonding_masters'): import syslog syslog.syslog(syslog.LOG_NOTICE, "loading bonding kernel module") if os.system('modprobe bonding max_bonds=0 miimon=250') != 0: syslog.syslog(syslog.LOG_NOTICE, "failed loading bonding kernel module") raise ConfigError("failed loading bonding kernel module") bond = deepcopy(default_config_data) conf = Config() # determine tagNode instance try: bond['intf'] = os.environ['VYOS_TAGNODE_VALUE'] except KeyError as E: print("Interface not specified") # check if bond has been removed cfg_base = 'interfaces bonding ' + bond['intf'] if not conf.exists(cfg_base): bond['deleted'] = True return bond # set new configuration level conf.set_level(cfg_base) # retrieve configured interface addresses if conf.exists('address'): bond['address'] = conf.return_values('address') # get interface addresses (currently effective) - to determine which # address is no longer valid and needs to be removed eff_addr = conf.return_effective_values('address') bond['address_remove'] = list_diff(eff_addr, bond['address']) # ARP link monitoring frequency in milliseconds if conf.exists('arp-monitor interval'): bond['arp_mon_intvl'] = int(conf.return_value('arp-monitor interval')) # IP address to use for ARP monitoring if conf.exists('arp-monitor target'): bond['arp_mon_tgt'] = conf.return_values('arp-monitor target') # retrieve interface description if conf.exists('description'): bond['description'] = conf.return_value('description') # get DHCP client identifier if conf.exists('dhcp-options client-id'): bond['dhcp_client_id'] = conf.return_value('dhcp-options client-id') # DHCP client host name (overrides the system host name) if conf.exists('dhcp-options host-name'): bond['dhcp_hostname'] = conf.return_value('dhcp-options host-name') + # DHCP client vendor identifier + if conf.exists('dhcp-options vendor-class-id'): + bond['dhcp_vendor_class_id'] = conf.return_value('dhcp-options vendor-class-id') + # DHCPv6 only acquire config parameters, no address if conf.exists('dhcpv6-options parameters-only'): bond['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only') # DHCPv6 temporary IPv6 address if conf.exists('dhcpv6-options temporary'): bond['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary') # ignore link state changes if conf.exists('disable-link-detect'): bond['disable_link_detect'] = 2 # disable bond interface if conf.exists('disable'): bond['disable'] = True # Bonding transmit hash policy if conf.exists('hash-policy'): bond['hash_policy'] = conf.return_value('hash-policy') # ARP cache entry timeout in seconds if conf.exists('ip arp-cache-timeout'): bond['ip_arp_cache_tmo'] = int(conf.return_value('ip arp-cache-timeout')) # Enable proxy-arp on this interface if conf.exists('ip enable-proxy-arp'): bond['ip_proxy_arp'] = 1 # Enable private VLAN proxy ARP on this interface if conf.exists('ip proxy-arp-pvlan'): bond['ip_proxy_arp_pvlan'] = 1 # Media Access Control (MAC) address if conf.exists('mac'): bond['mac'] = conf.return_value('mac') # Bonding mode if conf.exists('mode'): bond['mode'] = get_bond_mode(conf.return_value('mode')) # Maximum Transmission Unit (MTU) if conf.exists('mtu'): bond['mtu'] = int(conf.return_value('mtu')) # determine bond member interfaces (currently configured) if conf.exists('member interface'): bond['member'] = conf.return_values('member interface') # Primary device interface if conf.exists('primary'): bond['primary'] = conf.return_value('primary') # re-set configuration level and retrieve vif-s interfaces conf.set_level(cfg_base) # get vif-s interfaces (currently effective) - to determine which vif-s # interface is no longer present and needs to be removed eff_intf = conf.list_effective_nodes('vif-s') act_intf = conf.list_nodes('vif-s') bond['vif_s_remove'] = list_diff(eff_intf, act_intf) if conf.exists('vif-s'): for vif_s in conf.list_nodes('vif-s'): # set config level to vif-s interface conf.set_level(cfg_base + ' vif-s ' + vif_s) bond['vif_s'].append(vlan_to_dict(conf)) # re-set configuration level and retrieve vif-s interfaces conf.set_level(cfg_base) # Determine vif interfaces (currently effective) - to determine which # vif interface is no longer present and needs to be removed eff_intf = conf.list_effective_nodes('vif') act_intf = conf.list_nodes('vif') bond['vif_remove'] = list_diff(eff_intf, act_intf) if conf.exists('vif'): for vif in conf.list_nodes('vif'): # set config level to vif interface conf.set_level(cfg_base + ' vif ' + vif) bond['vif'].append(vlan_to_dict(conf)) return bond def verify(bond): if len (bond['arp_mon_tgt']) > 16: raise ConfigError('The maximum number of targets that can be specified is 16') if bond['primary']: if bond['mode'] not in ['active-backup', 'balance-tlb', 'balance-alb']: raise ConfigError('Mode dependency failed, primary not supported ' \ 'in this mode.'.format()) if bond['primary'] not in bond['member']: raise ConfigError('Interface "{}" is not part of the bond' \ .format(bond['primary'])) for vif_s in bond['vif_s']: for vif in bond['vif']: if vif['id'] == vif_s['id']: raise ConfigError('Can not use identical ID on vif and vif-s interface') conf = Config() for intf in bond['member']: # a bonding member interface is only allowed to be assigned to one bond! all_bonds = conf.list_nodes('interfaces bonding') # We do not need to check our own bond all_bonds.remove(bond['intf']) for tmp in all_bonds: if conf.exists('interfaces bonding ' + tmp + ' member interface ' + intf): raise ConfigError('can not enslave interface {} which already ' \ 'belongs to {}'.format(intf, tmp)) # can not add interfaces with an assigned address to a bond if conf.exists('interfaces ethernet ' + intf + ' address'): raise ConfigError('can not enslave interface {} which has an address ' \ 'assigned'.format(intf)) # bond members are not allowed to be bridge members, too for tmp in conf.list_nodes('interfaces bridge'): if conf.exists('interfaces bridge ' + tmp + ' member interface ' + intf): raise ConfigError('can not enslave interface {} which belongs to ' \ 'bridge {}'.format(intf, tmp)) # bond members are not allowed to be vrrp members, too for tmp in conf.list_nodes('high-availability vrrp group'): if conf.exists('high-availability vrrp group ' + tmp + ' interface ' + intf): raise ConfigError('can not enslave interface {} which belongs to ' \ 'VRRP group {}'.format(intf, tmp)) # bond members are not allowed to be underlaying psuedo-ethernet devices for tmp in conf.list_nodes('interfaces pseudo-ethernet'): if conf.exists('interfaces pseudo-ethernet ' + tmp + ' link ' + intf): raise ConfigError('can not enslave interface {} which belongs to ' \ 'pseudo-ethernet {}'.format(intf, tmp)) # bond members are not allowed to be underlaying vxlan devices for tmp in conf.list_nodes('interfaces vxlan'): if conf.exists('interfaces vxlan ' + tmp + ' link ' + intf): raise ConfigError('can not enslave interface {} which belongs to ' \ 'vxlan {}'.format(intf, tmp)) if bond['primary']: if bond['primary'] not in bond['member']: raise ConfigError('primary interface must be a member interface of {}' \ .format(bond['intf'])) if bond['mode'] not in ['active-backup', 'balance-tlb', 'balance-alb']: raise ConfigError('primary interface only works for mode active-backup, ' \ 'transmit-load-balance or adaptive-load-balance') if bond['arp_mon_intvl'] > 0: if bond['mode'] in ['802.3ad', 'balance-tlb', 'balance-alb']: raise ConfigError('ARP link monitoring does not work for mode 802.3ad, ' \ 'transmit-load-balance or adaptive-load-balance') return None def generate(bond): return None def apply(bond): b = BondIf(bond['intf']) if bond['deleted']: # delete interface b.remove() else: # Some parameters can not be changed when the bond is up. # Always disable the bond prior changing anything b.set_state('down') # The bonding mode can not be changed when there are interfaces enslaved # to this bond, thus we will free all interfaces from the bond first! for intf in b.get_slaves(): b.del_port(intf) # ARP link monitoring frequency, reset miimon when arp-montior is inactive # this is done inside BondIf automatically b.set_arp_interval(bond['arp_mon_intvl']) # ARP monitor targets need to be synchronized between sysfs and CLI. # Unfortunately an address can't be send twice to sysfs as this will # result in the following exception: OSError: [Errno 22] Invalid argument. # # We remove ALL adresses prior adding new ones, this will remove addresses # added manually by the user too - but as we are limited to 16 adresses # from the kernel side this looks valid to me. We won't run into an error # when a user added manual adresses which would result in having more # then 16 adresses in total. arp_tgt_addr = list(map(str, b.get_arp_ip_target().split())) for addr in arp_tgt_addr: b.set_arp_ip_target('-' + addr) # Add configured ARP target addresses for addr in bond['arp_mon_tgt']: b.set_arp_ip_target('+' + addr) # update interface description used e.g. within SNMP b.set_alias(bond['description']) # get DHCP config dictionary and update values opt = b.get_dhcp_options() if bond['dhcp_client_id']: opt['client_id'] = bond['dhcp_client_id'] if bond['dhcp_hostname']: opt['hostname'] = bond['dhcp_hostname'] + if bond['dhcp_vendor_class_id']: + opt['vendor_class_id'] = bond['dhcp_vendor_class_id'] + # store DHCP config dictionary - used later on when addresses # are requested b.set_dhcp_options(opt) # ignore link state changes b.set_link_detect(bond['disable_link_detect']) # Bonding transmit hash policy b.set_hash_policy(bond['hash_policy']) # configure ARP cache timeout in milliseconds b.set_arp_cache_tmo(bond['ip_arp_cache_tmo']) # Enable proxy-arp on this interface b.set_proxy_arp(bond['ip_proxy_arp']) # Enable private VLAN proxy ARP on this interface b.set_proxy_arp_pvlan(bond['ip_proxy_arp_pvlan']) # Change interface MAC address if bond['mac']: b.set_mac(bond['mac']) # Bonding policy b.set_mode(bond['mode']) # Maximum Transmission Unit (MTU) b.set_mtu(bond['mtu']) # Primary device interface if bond['primary']: b.set_primary(bond['primary']) # Add (enslave) interfaces to bond for intf in bond['member']: b.add_port(intf) # As the bond interface is always disabled first when changing # parameters we will only re-enable the interface if it is not # administratively disabled if not bond['disable']: b.set_state('up') # Configure interface address(es) # - not longer required addresses get removed first # - newly addresses will be added second for addr in bond['address_remove']: b.del_addr(addr) for addr in bond['address']: b.add_addr(addr) # remove no longer required service VLAN interfaces (vif-s) for vif_s in bond['vif_s_remove']: b.del_vlan(vif_s) # create service VLAN interfaces (vif-s) for vif_s in bond['vif_s']: s_vlan = b.add_vlan(vif_s['id'], ethertype=vif_s['ethertype']) apply_vlan_config(s_vlan, vif_s) # remove no longer required client VLAN interfaces (vif-c) # on lower service VLAN interface for vif_c in vif_s['vif_c_remove']: s_vlan.del_vlan(vif_c) # create client VLAN interfaces (vif-c) # on lower service VLAN interface for vif_c in vif_s['vif_c']: c_vlan = s_vlan.add_vlan(vif_c['id']) apply_vlan_config(c_vlan, vif_c) # remove no longer required VLAN interfaces (vif) for vif in bond['vif_remove']: b.del_vlan(vif) # create VLAN interfaces (vif) for vif in bond['vif']: vlan = b.add_vlan(vif['id']) apply_vlan_config(vlan, vif) return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1) diff --git a/src/conf_mode/interface-bridge.py b/src/conf_mode/interface-bridge.py index b2755547c..57ac98444 100755 --- a/src/conf_mode/interface-bridge.py +++ b/src/conf_mode/interface-bridge.py @@ -1,285 +1,293 @@ #!/usr/bin/env python3 # # Copyright (C) 2019 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os from copy import deepcopy from sys import exit from netifaces import interfaces from vyos.ifconfig import BridgeIf, STPIf from vyos.configdict import list_diff from vyos.config import Config from vyos import ConfigError default_config_data = { 'address': [], 'address_remove': [], 'aging': 300, 'arp_cache_tmo': 30, 'description': '', 'deleted': False, 'dhcp_client_id': '', 'dhcp_hostname': '', + 'dhcp_vendor_class_id': '', 'dhcpv6_prm_only': False, 'dhcpv6_temporary': False, 'disable': False, 'disable_link_detect': 1, 'forwarding_delay': 14, 'hello_time': 2, 'igmp_querier': 0, 'intf': '', 'mac' : '', 'max_age': 20, 'member': [], 'member_remove': [], 'priority': 32768, 'stp': 0 } def get_config(): bridge = deepcopy(default_config_data) conf = Config() # determine tagNode instance try: bridge['intf'] = os.environ['VYOS_TAGNODE_VALUE'] except KeyError as E: print("Interface not specified") # Check if bridge has been removed if not conf.exists('interfaces bridge ' + bridge['intf']): bridge['deleted'] = True return bridge # set new configuration level conf.set_level('interfaces bridge ' + bridge['intf']) # retrieve configured interface addresses if conf.exists('address'): bridge['address'] = conf.return_values('address') # Determine interface addresses (currently effective) - to determine which # address is no longer valid and needs to be removed eff_addr = conf.return_effective_values('address') bridge['address_remove'] = list_diff(eff_addr, bridge['address']) # retrieve aging - how long addresses are retained if conf.exists('aging'): bridge['aging'] = int(conf.return_value('aging')) # retrieve interface description if conf.exists('description'): bridge['description'] = conf.return_value('description') # get DHCP client identifier if conf.exists('dhcp-options client-id'): bridge['dhcp_client_id'] = conf.return_value('dhcp-options client-id') # DHCP client host name (overrides the system host name) if conf.exists('dhcp-options host-name'): bridge['dhcp_hostname'] = conf.return_value('dhcp-options host-name') + # DHCP client vendor identifier + if conf.exists('dhcp-options vendor-class-id'): + bridge['dhcp_vendor_class_id'] = conf.return_value('dhcp-options vendor-class-id') + # DHCPv6 only acquire config parameters, no address if conf.exists('dhcpv6-options parameters-only'): bridge['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only') # DHCPv6 temporary IPv6 address if conf.exists('dhcpv6-options temporary'): bridge['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary') # Disable this bridge interface if conf.exists('disable'): bridge['disable'] = True # Ignore link state changes if conf.exists('disable-link-detect'): bridge['disable_link_detect'] = 2 # Forwarding delay if conf.exists('forwarding-delay'): bridge['forwarding_delay'] = int(conf.return_value('forwarding-delay')) # Hello packet advertisment interval if conf.exists('hello-time'): bridge['hello_time'] = int(conf.return_value('hello-time')) # Enable Internet Group Management Protocol (IGMP) querier if conf.exists('igmp querier'): bridge['igmp_querier'] = 1 # ARP cache entry timeout in seconds if conf.exists('ip arp-cache-timeout'): bridge['arp_cache_tmo'] = int(conf.return_value('ip arp-cache-timeout')) # Media Access Control (MAC) address if conf.exists('mac'): bridge['mac'] = conf.return_value('mac') # Interval at which neighbor bridges are removed if conf.exists('max-age'): bridge['max_age'] = int(conf.return_value('max-age')) # Determine bridge member interface (currently configured) for intf in conf.list_nodes('member interface'): # cost and priority initialized with linux defaults # by reading /sys/devices/virtual/net/br0/brif/eth2/{path_cost,priority} # after adding interface to bridge after reboot iface = { 'name': intf, 'cost': 100, 'priority': 32 } if conf.exists('member interface {} cost'.format(intf)): iface['cost'] = int(conf.return_value('member interface {} cost'.format(intf))) if conf.exists('member interface {} priority'.format(intf)): iface['priority'] = int(conf.return_value('member interface {} priority'.format(intf))) bridge['member'].append(iface) # Determine bridge member interface (currently effective) - to determine which # interfaces is no longer assigend to the bridge and thus can be removed eff_intf = conf.list_effective_nodes('member interface') act_intf = conf.list_nodes('member interface') bridge['member_remove'] = list_diff(eff_intf, act_intf) # Priority for this bridge if conf.exists('priority'): bridge['priority'] = int(conf.return_value('priority')) # Enable spanning tree protocol if conf.exists('stp'): bridge['stp'] = 1 return bridge def verify(bridge): conf = Config() for br in conf.list_nodes('interfaces bridge'): # it makes no sense to verify ourself in this case if br == bridge['intf']: continue for intf in bridge['member']: tmp = conf.list_nodes('interfaces bridge {} member interface'.format(br)) if intf['name'] in tmp: raise ConfigError('Interface "{}" belongs to bridge "{}" and can not be enslaved.'.format(intf['name'], bridge['intf'])) # the interface must exist prior adding it to a bridge for intf in bridge['member']: if intf['name'] not in interfaces(): raise ConfigError('Can not add non existing interface "{}" to bridge "{}"'.format(intf['name'], bridge['intf'])) # bridge members are not allowed to be bond members, too for intf in bridge['member']: for bond in conf.list_nodes('interfaces bonding'): if conf.exists('interfaces bonding ' + bond + ' member interface'): if intf['name'] in conf.return_values('interfaces bonding ' + bond + ' member interface'): raise ConfigError('Interface {} belongs to bond {}, can not add it to {}'.format(intf['name'], bond, bridge['intf'])) return None def generate(bridge): return None def apply(bridge): br = BridgeIf(bridge['intf']) if bridge['deleted']: # delete interface br.remove() else: # enable interface br.set_state('up') # set ageing time br.set_ageing_time(bridge['aging']) # set bridge forward delay br.set_forward_delay(bridge['forwarding_delay']) # set hello time br.set_hello_time(bridge['hello_time']) # set max message age br.set_max_age(bridge['max_age']) # set bridge priority br.set_priority(bridge['priority']) # turn stp on/off br.set_stp(bridge['stp']) # enable or disable IGMP querier br.set_multicast_querier(bridge['igmp_querier']) # update interface description used e.g. within SNMP br.set_alias(bridge['description']) # get DHCP config dictionary and update values opt = br.get_dhcp_options() if bridge['dhcp_client_id']: opt['client_id'] = bridge['dhcp_client_id'] if bridge['dhcp_hostname']: opt['hostname'] = bridge['dhcp_hostname'] + if bridge['dhcp_vendor_class_id']: + opt['vendor_class_id'] = bridge['dhcp_vendor_class_id'] + # store DHCP config dictionary - used later on when addresses # are requested br.set_dhcp_options(opt) # Change interface MAC address if bridge['mac']: br.set_mac(bridge['mac']) # remove interface from bridge for intf in bridge['member_remove']: br.del_port( intf['name'] ) # add interfaces to bridge for member in bridge['member']: br.add_port(member['name']) # up/down interface if bridge['disable']: br.set_state('down') # Configure interface address(es) # - not longer required addresses get removed first # - newly addresses will be added second for addr in bridge['address_remove']: br.del_addr(addr) for addr in bridge['address']: br.add_addr(addr) # configure additional bridge member options for member in bridge['member']: i = STPIf(member['name']) # configure ARP cache timeout i.set_arp_cache_tmo(bridge['arp_cache_tmo']) # ignore link state changes i.set_link_detect(bridge['disable_link_detect']) # set bridge port path cost i.set_path_cost(member['cost']) # set bridge port path priority i.set_path_priority(member['priority']) return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1) diff --git a/src/conf_mode/interface-ethernet.py b/src/conf_mode/interface-ethernet.py index 5f9b4d8b0..3cdc03ce5 100755 --- a/src/conf_mode/interface-ethernet.py +++ b/src/conf_mode/interface-ethernet.py @@ -1,404 +1,415 @@ #!/usr/bin/env python3 # # Copyright (C) 2019 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os from copy import deepcopy from sys import exit from vyos.ifconfig import EthernetIf, VLANIf from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config from vyos import ConfigError default_config_data = { 'address': [], 'address_remove': [], 'description': '', 'deleted': False, 'dhcp_client_id': '', 'dhcp_hostname': '', + 'dhcp_vendor_class_id': '', 'dhcpv6_prm_only': False, 'dhcpv6_temporary': False, 'disable': False, 'disable_link_detect': 1, 'duplex': 'auto', 'flow_control': 'on', 'hw_id': '', 'ip_arp_cache_tmo': 30, 'ip_proxy_arp': 0, 'ip_proxy_arp_pvlan': 0, 'intf': '', 'mac': '', 'mtu': 1500, 'offload_gro': 'off', 'offload_gso': 'off', 'offload_sg': 'off', 'offload_tso': 'off', 'offload_ufo': 'off', 'speed': 'auto', 'vif_s': [], 'vif_s_remove': [], 'vif': [], 'vif_remove': [] } def apply_vlan_config(vlan, config): """ Generic function to apply a VLAN configuration from a dictionary to a VLAN interface """ if type(vlan) != type(VLANIf("lo")): raise TypeError() # get DHCP config dictionary and update values opt = vlan.get_dhcp_options() if config['dhcp_client_id']: opt['client_id'] = config['dhcp_client_id'] if config['dhcp_hostname']: opt['hostname'] = config['dhcp_hostname'] + if config['dhcp_vendor_class_id']: + opt['vendor_class_id'] = config['dhcp_vendor_class_id'] + # store DHCP config dictionary - used later on when addresses # are requested vlan.set_dhcp_options(opt) # update interface description used e.g. within SNMP vlan.set_alias(config['description']) # ignore link state changes vlan.set_link_detect(config['disable_link_detect']) # Maximum Transmission Unit (MTU) vlan.set_mtu(config['mtu']) # Change VLAN interface MAC address if config['mac']: vlan.set_mac(config['mac']) # enable/disable VLAN interface if config['disable']: vlan.set_state('down') else: vlan.set_state('up') # Configure interface address(es) # - not longer required addresses get removed first # - newly addresses will be added second for addr in config['address_remove']: vlan.del_addr(addr) for addr in config['address']: vlan.add_addr(addr) def get_config(): eth = deepcopy(default_config_data) conf = Config() # determine tagNode instance try: eth['intf'] = os.environ['VYOS_TAGNODE_VALUE'] except KeyError as E: print("Interface not specified") # check if ethernet interface has been removed cfg_base = 'interfaces ethernet ' + eth['intf'] if not conf.exists(cfg_base): eth['deleted'] = True # we can not bail out early as ethernet interface can not be removed # Kernel will complain with: RTNETLINK answers: Operation not supported. # Thus we need to remove individual settings return eth # set new configuration level conf.set_level(cfg_base) # retrieve configured interface addresses if conf.exists('address'): eth['address'] = conf.return_values('address') # get interface addresses (currently effective) - to determine which # address is no longer valid and needs to be removed eff_addr = conf.return_effective_values('address') eth['address_remove'] = list_diff(eff_addr, eth['address']) # retrieve interface description if conf.exists('description'): eth['description'] = conf.return_value('description') # get DHCP client identifier if conf.exists('dhcp-options client-id'): eth['dhcp_client_id'] = conf.return_value('dhcp-options client-id') # DHCP client host name (overrides the system host name) if conf.exists('dhcp-options host-name'): eth['dhcp_hostname'] = conf.return_value('dhcp-options host-name') + # DHCP client vendor identifier + if conf.exists('dhcp-options vendor-class-id'): + eth['dhcp_vendor_class_id'] = conf.return_value('dhcp-options vendor-class-id') + # DHCPv6 only acquire config parameters, no address if conf.exists('dhcpv6-options parameters-only'): eth['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only') # DHCPv6 temporary IPv6 address if conf.exists('dhcpv6-options temporary'): eth['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary') # ignore link state changes if conf.exists('disable-link-detect'): eth['disable_link_detect'] = 2 # disable ethernet flow control (pause frames) if conf.exists('disable-flow-control'): eth['flow_control'] = 'off' # retrieve real hardware address if conf.exists('hw-id'): eth['hw_id'] = conf.return_value('hw-id') # disable interface if conf.exists('disable'): eth['disable'] = True # interface duplex if conf.exists('duplex'): eth['duplex'] = conf.return_value('duplex') # ARP cache entry timeout in seconds if conf.exists('ip arp-cache-timeout'): eth['ip_arp_cache_tmo'] = int(conf.return_value('ip arp-cache-timeout')) # Enable proxy-arp on this interface if conf.exists('ip enable-proxy-arp'): eth['ip_proxy_arp'] = 1 # Enable private VLAN proxy ARP on this interface if conf.exists('ip proxy-arp-pvlan'): eth['ip_proxy_arp_pvlan'] = 1 # Media Access Control (MAC) address if conf.exists('mac'): eth['mac'] = conf.return_value('mac') # Maximum Transmission Unit (MTU) if conf.exists('mtu'): eth['mtu'] = int(conf.return_value('mtu')) # GRO (generic receive offload) if conf.exists('offload-options generic-receive'): eth['offload_gro'] = conf.return_value('offload-options generic-receive') # GSO (generic segmentation offload) if conf.exists('offload-options generic-segmentation'): eth['offload_gso'] = conf.return_value('offload-options generic-segmentation') # scatter-gather option if conf.exists('offload-options scatter-gather'): eth['offload_sg'] = conf.return_value('offload-options scatter-gather') # TSO (TCP segmentation offloading) if conf.exists('offload-options tcp-segmentation'): eth['offload_tso'] = conf.return_value('offload-options tcp-segmentation') # UDP fragmentation offloading if conf.exists('offload-options udp-fragmentation'): eth['offload_ufo'] = conf.return_value('offload-options udp-fragmentation') # interface speed if conf.exists('speed'): eth['speed'] = conf.return_value('speed') # re-set configuration level and retrieve vif-s interfaces conf.set_level(cfg_base) # get vif-s interfaces (currently effective) - to determine which vif-s # interface is no longer present and needs to be removed eff_intf = conf.list_effective_nodes('vif-s') act_intf = conf.list_nodes('vif-s') eth['vif_s_remove'] = list_diff(eff_intf, act_intf) if conf.exists('vif-s'): for vif_s in conf.list_nodes('vif-s'): # set config level to vif-s interface conf.set_level(cfg_base + ' vif-s ' + vif_s) eth['vif_s'].append(vlan_to_dict(conf)) # re-set configuration level and retrieve vif-s interfaces conf.set_level(cfg_base) # Determine vif interfaces (currently effective) - to determine which # vif interface is no longer present and needs to be removed eff_intf = conf.list_effective_nodes('vif') act_intf = conf.list_nodes('vif') eth['vif_remove'] = list_diff(eff_intf, act_intf) if conf.exists('vif'): for vif in conf.list_nodes('vif'): # set config level to vif interface conf.set_level(cfg_base + ' vif ' + vif) eth['vif'].append(vlan_to_dict(conf)) return eth def verify(eth): if eth['deleted']: return None if eth['speed'] == 'auto': if eth['duplex'] != 'auto': raise ConfigError('If speed is hardcoded, duplex must be hardcoded, too') if eth['duplex'] == 'auto': if eth['speed'] != 'auto': raise ConfigError('If duplex is hardcoded, speed must be hardcoded, too') conf = Config() # some options can not be changed when interface is enslaved to a bond for bond in conf.list_nodes('interfaces bonding'): if conf.exists('interfaces bonding ' + bond + ' member interface'): bond_member = conf.return_values('interfaces bonding ' + bond + ' member interface') if eth['intf'] in bond_member: if eth['address']: raise ConfigError('Can not assign address to interface {} which is a member of {}').format(eth['intf'], bond) return None def generate(eth): return None def apply(eth): e = EthernetIf(eth['intf']) if eth['deleted']: # delete interface e.remove() else: # update interface description used e.g. within SNMP e.set_alias(eth['description']) # get DHCP config dictionary and update values opt = e.get_dhcp_options() if eth['dhcp_client_id']: opt['client_id'] = eth['dhcp_client_id'] if eth['dhcp_hostname']: opt['hostname'] = eth['dhcp_hostname'] + if eth['dhcp_vendor_class_id']: + opt['vendor_class_id'] = eth['dhcp_vendor_class_id'] + # store DHCP config dictionary - used later on when addresses # are requested e.set_dhcp_options(opt) # ignore link state changes e.set_link_detect(eth['disable_link_detect']) # disable ethernet flow control (pause frames) e.set_flow_control(eth['flow_control']) # configure ARP cache timeout in milliseconds e.set_arp_cache_tmo(eth['ip_arp_cache_tmo']) # Enable proxy-arp on this interface e.set_proxy_arp(eth['ip_proxy_arp']) # Enable private VLAN proxy ARP on this interface e.set_proxy_arp_pvlan(eth['ip_proxy_arp_pvlan']) # Change interface MAC address - re-set to real hardware address (hw-id) # if custom mac is removed if eth['mac']: e.set_mac(eth['mac']) else: e.set_mac(eth['hw_id']) # Maximum Transmission Unit (MTU) e.set_mtu(eth['mtu']) # GRO (generic receive offload) e.set_gro(eth['offload_gro']) # GSO (generic segmentation offload) e.set_gso(eth['offload_gso']) # scatter-gather option e.set_sg(eth['offload_sg']) # TSO (TCP segmentation offloading) e.set_tso(eth['offload_tso']) # UDP fragmentation offloading e.set_ufo(eth['offload_ufo']) # Set physical interface speed and duplex e.set_speed_duplex(eth['speed'], eth['duplex']) # Enable/Disable interface if eth['disable']: e.set_state('down') else: e.set_state('up') # Configure interface address(es) # - not longer required addresses get removed first # - newly addresses will be added second for addr in eth['address_remove']: e.del_addr(addr) for addr in eth['address']: e.add_addr(addr) # remove no longer required service VLAN interfaces (vif-s) for vif_s in eth['vif_s_remove']: e.del_vlan(vif_s) # create service VLAN interfaces (vif-s) for vif_s in eth['vif_s']: s_vlan = e.add_vlan(vif_s['id'], ethertype=vif_s['ethertype']) apply_vlan_config(s_vlan, vif_s) # remove no longer required client VLAN interfaces (vif-c) # on lower service VLAN interface for vif_c in vif_s['vif_c_remove']: s_vlan.del_vlan(vif_c) # create client VLAN interfaces (vif-c) # on lower service VLAN interface for vif_c in vif_s['vif_c']: c_vlan = s_vlan.add_vlan(vif_c['id']) apply_vlan_config(c_vlan, vif_c) # remove no longer required VLAN interfaces (vif) for vif in eth['vif_remove']: e.del_vlan(vif) # create VLAN interfaces (vif) for vif in eth['vif']: # QoS priority mapping can only be set during interface creation # so we delete the interface first if required. if vif['egress_qos_changed'] or vif['ingress_qos_changed']: try: # on system bootup the above condition is true but the interface # does not exists, which throws an exception, but that's legal e.del_vlan(vif['id']) except: pass vlan = e.add_vlan(vif['id'], ingress_qos=vif['ingress_qos'], egress_qos=vif['egress_qos']) apply_vlan_config(vlan, vif) return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1)