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.

220 lines
6.0 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_string "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_setup() {
  55. local cfg="$1"
  56. local link="bonding-$cfg"
  57. # Check for loaded kernel bonding driver (/sys/class/net/bonding_masters exists)
  58. [ -f "$BONDING_MASTERS" ] || {
  59. echo "$cfg" "setup: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  60. proto_notify_error "$cfg" BONDING_MASTER_DOES_NOT_EXIST
  61. proto_block_restart "$cfg"
  62. return
  63. }
  64. # Add bonding interface to system
  65. echo "+$link" > "$BONDING_MASTERS"
  66. # Set bonding policy (with corresponding parameters)
  67. local bonding_policy
  68. json_get_vars bonding_policy
  69. case "$bonding_policy" in
  70. 802.3ad)
  71. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  72. set_driver_values min_links ad_actor_sys_prio ad_actor_system ad_select lacp_rate
  73. ;;
  74. balance-rr)
  75. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  76. set_driver_values packets_per_slave xmit_hash_policy
  77. ;;
  78. balance-tlb)
  79. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  80. set_driver_values primary primary_reselect lp_interval tlb_dynamic_lb resend_igmp xmit_hash_policy
  81. ;;
  82. balance-alb)
  83. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  84. set_driver_values primary primary_reselect lp_interval tlb_dynamic_lb resend_igmp xmit_hash_policy
  85. ;;
  86. balance-xor)
  87. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  88. set_driver_values xmit_hash_policy
  89. ;;
  90. broadcast)
  91. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  92. ;;
  93. active-backup)
  94. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  95. set_driver_values primary primary_reselect fail_over_mac num_grat_arp__num_unsol_na xmit_hash_policy
  96. ;;
  97. esac
  98. # Set link monitoring (with corresponding parameters)
  99. local link_monitoring
  100. json_get_vars link_monitoring
  101. case "$link_monitoring" in
  102. arp)
  103. local arp_interval arp_ip_target arp_all_targets arp_validate
  104. json_get_vars arp_interval arp_ip_target arp_all_targets arp_validate
  105. [ -n "$arp_interval" -a "$arp_interval" != 0 ] && echo "$arp_interval" > /sys/class/net/"$link"/bonding/arp_interval
  106. IFS=' '
  107. for target in $arp_ip_target; do
  108. echo "+$target" > /sys/class/net/"$link"/bonding/arp_ip_target
  109. done
  110. [ -n "$arp_all_targets" ] && echo "$arp_all_targets" > /sys/class/net/"$link"/bonding/arp_all_targets
  111. [ -n "$arp_validate" ] && echo "$arp_validate" > /sys/class/net/"$link"/bonding/arp_validate
  112. ;;
  113. mii)
  114. local miimon downdelay updelay use_carrier
  115. json_get_vars miimon downdelay updelay use_carrier
  116. [ -n "$miimon" -a "$miimon" != 0 ] && echo "$miimon" > /sys/class/net/"$link"/bonding/miimon
  117. [ -n "$downdelay" ] && echo "$downdelay" > /sys/class/net/"$link"/bonding/downdelay
  118. [ -n "$updelay" ] && echo "$updelay" > /sys/class/net/"$link"/bonding/updelay
  119. [ -n "$use_carrier" ] && echo "$use_carrier" > /sys/class/net/"$link"/bonding/use_carrier
  120. ;;
  121. esac
  122. # Add slaves to bonding interface
  123. local slaves
  124. json_get_vars slaves
  125. for slave in $slaves; do
  126. if [ "$(cat /proc/net/dev |grep "$slave")" == "" ]; then
  127. echo "$cfg" "No slave device $slave found"
  128. proto_notify_error "$cfg" NO_DEVICE
  129. proto_block_restart "$cfg"
  130. return
  131. fi
  132. ifconfig "$slave" down
  133. sleep 1
  134. echo "+$slave" > /sys/class/net/"$link"/bonding/slaves
  135. ifconfig "$slave" up
  136. done
  137. [ -n "$all_slaves_active" ] && echo "$all_slaves_active" > /sys/class/net/"$link"/bonding/all_slaves_active
  138. local ipaddr netmask
  139. json_get_vars ipaddr netmask
  140. # ATTENTION
  141. #All json vars have to be read before the line below, as the
  142. # json object will be overwritten by proto_init_update
  143. # ATTENTION
  144. proto_init_update "$link" 1
  145. # For static configuration we _MUST_ have an IP address
  146. [ -z "$ipaddr" ] && {
  147. echo "$cfg" "No local IP address defined"
  148. proto_notify_error "$cfg" INVALID_LOCAL_ADDRESS
  149. proto_block_restart "$cfg"
  150. return
  151. }
  152. proto_add_ipv4_address "$ipaddr" "$netmask"
  153. proto_send_update "$cfg"
  154. }
  155. proto_bonding_teardown() {
  156. local cfg="$1"
  157. local link="bonding-$cfg"
  158. # Check for loaded kernel bonding driver (/sys/class/net/bonding_masters exists)
  159. [ -f "$BONDING_MASTERS" ] || {
  160. echo "$cfg" "teardown: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  161. proto_notify_error "$cfg" BONDING_MASTER_DOES_NOT_EXIST
  162. proto_block_restart "$cfg"
  163. return
  164. }
  165. echo "-$link" > /sys/class/net/bonding_masters
  166. logger "bonding_teardown($1): $2"
  167. }
  168. add_protocol bonding