You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.1 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 TDT AG <development@tdt.de>
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
  7. #
  8. . /lib/functions.sh
  9. . ../netifd-proto.sh
  10. init_proto "$@"
  11. INCLUDE_ONLY=1
  12. BONDING_MASTERS="/sys/class/net/bonding_masters"
  13. set_driver_values() {
  14. local varname
  15. for varname in "$@"; do
  16. local value
  17. json_get_var value "$varname"
  18. [ -n "$value" ] && echo "$value" > /sys/class/net/"$link"/bonding/"$varname"
  19. done
  20. }
  21. proto_bonding_init_config() {
  22. no_device=1
  23. available=1
  24. proto_config_add_string "ifname"
  25. proto_config_add_string "ipaddr"
  26. proto_config_add_string "netmask"
  27. proto_config_add_string "bonding_policy"
  28. proto_config_add_string "link_monitoring"
  29. proto_config_add_array "slaves"
  30. proto_config_add_string "all_slaves_active"
  31. proto_config_add_string "min_links"
  32. proto_config_add_string "ad_actor_sys_prio"
  33. proto_config_add_string "ad_actor_system"
  34. proto_config_add_string "ad_select"
  35. proto_config_add_string "lacp_rate"
  36. proto_config_add_string "packets_per_slave"
  37. proto_config_add_string "xmit_hash_policy"
  38. proto_config_add_string "primary"
  39. proto_config_add_string "primary_reselect"
  40. proto_config_add_string "lp_interval"
  41. proto_config_add_string "tlb_dynamic_lb"
  42. proto_config_add_string "resend_igmp"
  43. proto_config_add_string "fail_over_mac"
  44. proto_config_add_string "num_grat_arp__num_unsol_na"
  45. proto_config_add_string "arp_interval"
  46. proto_config_add_string "arp_ip_target"
  47. proto_config_add_string "arp_all_targets"
  48. proto_config_add_string "arp_validate"
  49. proto_config_add_string "miimon"
  50. proto_config_add_string "downdelay"
  51. proto_config_add_string "updelay"
  52. proto_config_add_string "use_carrier"
  53. }
  54. proto_bonding_add_slave() {
  55. local slave=$1
  56. local idx=$2
  57. local cfg=$3
  58. local link=$4
  59. if [ ! -e "/sys/class/net/$slave" ]; then
  60. echo "$cfg" "No slave device $slave found"
  61. proto_notify_error "$cfg" NO_DEVICE
  62. proto_block_restart "$cfg"
  63. return
  64. fi
  65. ip link set dev "$slave" down
  66. sleep 1
  67. echo "+$slave" > /sys/class/net/"$link"/bonding/slaves
  68. ip link set dev "$slave" up
  69. }
  70. proto_bonding_setup() {
  71. local cfg="$1"
  72. local link="bonding-$cfg"
  73. # Check for loaded kernel bonding driver (/sys/class/net/bonding_masters exists)
  74. [ -f "$BONDING_MASTERS" ] || {
  75. echo "$cfg" "setup: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  76. proto_notify_error "$cfg" BONDING_MASTER_DOES_NOT_EXIST
  77. proto_block_restart "$cfg"
  78. return
  79. }
  80. # Add bonding interface to system
  81. echo "+$link" > "$BONDING_MASTERS"
  82. # Set bonding policy (with corresponding parameters)
  83. local bonding_policy
  84. json_get_vars bonding_policy
  85. case "$bonding_policy" in
  86. 802.3ad)
  87. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  88. set_driver_values min_links ad_actor_sys_prio ad_actor_system ad_select lacp_rate xmit_hash_policy
  89. ;;
  90. balance-rr)
  91. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  92. set_driver_values packets_per_slave xmit_hash_policy
  93. ;;
  94. balance-tlb)
  95. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  96. set_driver_values primary primary_reselect lp_interval tlb_dynamic_lb resend_igmp xmit_hash_policy
  97. ;;
  98. balance-alb)
  99. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  100. set_driver_values primary primary_reselect lp_interval tlb_dynamic_lb resend_igmp xmit_hash_policy
  101. ;;
  102. balance-xor)
  103. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  104. set_driver_values xmit_hash_policy
  105. ;;
  106. broadcast)
  107. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  108. ;;
  109. active-backup)
  110. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  111. set_driver_values primary primary_reselect fail_over_mac num_grat_arp__num_unsol_na xmit_hash_policy
  112. ;;
  113. esac
  114. # Set link monitoring (with corresponding parameters)
  115. local link_monitoring
  116. json_get_vars link_monitoring
  117. case "$link_monitoring" in
  118. arp)
  119. local arp_interval arp_ip_target arp_all_targets arp_validate
  120. json_get_vars arp_interval arp_ip_target arp_all_targets arp_validate
  121. [ -n "$arp_interval" -a "$arp_interval" != 0 ] && echo "$arp_interval" > /sys/class/net/"$link"/bonding/arp_interval
  122. IFS=' '
  123. for target in $arp_ip_target; do
  124. echo "+$target" > /sys/class/net/"$link"/bonding/arp_ip_target
  125. done
  126. [ -n "$arp_all_targets" ] && echo "$arp_all_targets" > /sys/class/net/"$link"/bonding/arp_all_targets
  127. [ -n "$arp_validate" ] && echo "$arp_validate" > /sys/class/net/"$link"/bonding/arp_validate
  128. ;;
  129. mii)
  130. local miimon downdelay updelay use_carrier
  131. json_get_vars miimon downdelay updelay use_carrier
  132. [ -n "$miimon" -a "$miimon" != 0 ] && echo "$miimon" > /sys/class/net/"$link"/bonding/miimon
  133. [ -n "$downdelay" ] && echo "$downdelay" > /sys/class/net/"$link"/bonding/downdelay
  134. [ -n "$updelay" ] && echo "$updelay" > /sys/class/net/"$link"/bonding/updelay
  135. [ -n "$use_carrier" ] && echo "$use_carrier" > /sys/class/net/"$link"/bonding/use_carrier
  136. ;;
  137. esac
  138. # Add slaves to bonding interface
  139. local slaves
  140. json_get_vars slaves
  141. json_for_each_item proto_bonding_add_slave slaves "$cfg" "$link"
  142. [ -n "$all_slaves_active" ] && echo "$all_slaves_active" > /sys/class/net/"$link"/bonding/all_slaves_active
  143. local ipaddr netmask
  144. json_get_vars ipaddr netmask
  145. # ATTENTION
  146. #All json vars have to be read before the line below, as the
  147. # json object will be overwritten by proto_init_update
  148. # ATTENTION
  149. proto_init_update "$link" 1
  150. # For static configuration we _MUST_ have an IP address
  151. [ -z "$ipaddr" ] && {
  152. echo "$cfg" "No local IP address defined"
  153. proto_notify_error "$cfg" INVALID_LOCAL_ADDRESS
  154. proto_block_restart "$cfg"
  155. return
  156. }
  157. proto_add_ipv4_address "$ipaddr" "$netmask"
  158. proto_send_update "$cfg"
  159. }
  160. proto_bonding_teardown() {
  161. local cfg="$1"
  162. local link="bonding-$cfg"
  163. # Check for loaded kernel bonding driver (/sys/class/net/bonding_masters exists)
  164. [ -f "$BONDING_MASTERS" ] || {
  165. echo "$cfg" "teardown: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  166. proto_notify_error "$cfg" BONDING_MASTER_DOES_NOT_EXIST
  167. proto_block_restart "$cfg"
  168. return
  169. }
  170. echo "-$link" > /sys/class/net/bonding_masters
  171. logger "bonding_teardown($1): $2"
  172. }
  173. add_protocol bonding