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.

211 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" "setup: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  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. active-backup)
  87. echo "$bonding_policy" > /sys/class/net/"$link"/bonding/mode
  88. set_driver_values primary primary_reselect fail_over_mac num_grat_arp__num_unsol_na xmit_hash_policy
  89. ;;
  90. esac
  91. # Set link monitoring (with corresponding parameters)
  92. local link_monitoring
  93. json_get_vars link_monitoring
  94. case "$link_monitoring" in
  95. arp)
  96. local arp_interval arp_ip_target arp_all_targets arp_validate
  97. json_get_vars arp_interval arp_ip_target arp_all_targets arp_validate
  98. [ -n "$arp_interval" -a "$arp_interval" != 0 ] && echo "$arp_interval" > /sys/class/net/"$link"/bonding/arp_interval
  99. IFS=' '
  100. for target in $arp_ip_target; do
  101. echo "+$target" > /sys/class/net/"$link"/bonding/arp_ip_target
  102. done
  103. [ -n "$arp_all_targets" ] && echo "$arp_all_targets" > /sys/class/net/"$link"/bonding/arp_all_targets
  104. [ -n "$arp_validate" ] && echo "$arp_validate" > /sys/class/net/"$link"/bonding/arp_validate
  105. ;;
  106. mii)
  107. local miimon downdelay updelay use_carrier
  108. json_get_vars miimon downdelay updelay use_carrier
  109. [ -n "$miimon" -a "$miimon" != 0 ] && echo "$miimon" > /sys/class/net/"$link"/bonding/miimon
  110. [ -n "$downdelay" ] && echo "$downdelay" > /sys/class/net/"$link"/bonding/downdelay
  111. [ -n "$updelay" ] && echo "$updelay" > /sys/class/net/"$link"/bonding/updelay
  112. [ -n "$use_carrier" ] && echo "$use_carrier" > /sys/class/net/"$link"/bonding/use_carrier
  113. ;;
  114. esac
  115. # Add slaves to bonding interface
  116. local slaves
  117. json_get_vars slaves
  118. for slave in $slaves; do
  119. if [ "$(cat /proc/net/dev |grep "$slave")" == "" ]; then
  120. echo "$cfg" "ERROR IN CONFIGURATION - $slave: No such device"
  121. proto_notify_error "$cfg" "ERROR IN CONFIGURATION - $slave: No such device"
  122. proto_block_restart "$cfg"
  123. return
  124. fi
  125. ifconfig "$slave" down
  126. sleep 1
  127. echo "+$slave" > /sys/class/net/"$link"/bonding/slaves
  128. ifconfig "$slave" up
  129. done
  130. [ -n "$all_slaves_active" ] && echo "$all_slaves_active" > /sys/class/net/"$link"/bonding/all_slaves_active
  131. local ipaddr netmask
  132. json_get_vars ipaddr netmask
  133. # ATTENTION
  134. #All json vars have to be read before the line below, as the
  135. # json object will be overwritten by proto_init_update
  136. # ATTENTION
  137. proto_init_update "$link" 1
  138. # For static configuration we _MUST_ have an IP address
  139. [ -z "$ipaddr" ] && {
  140. echo "$cfg" "INVALID LOCAL ADDRESS"
  141. proto_notify_error "$cfg" "INVALID_LOCAL_ADDRESS"
  142. proto_block_restart "$cfg"
  143. return
  144. }
  145. proto_add_ipv4_address "$ipaddr" "$netmask"
  146. proto_send_update "$cfg"
  147. }
  148. proto_bonding_teardown() {
  149. local cfg="$1"
  150. local link="bonding-$cfg"
  151. # Check for loaded kernel bonding driver (/sys/class/net/bonding_masters exists)
  152. [ -f "$BONDING_MASTERS" ] || {
  153. echo "$cfg" "teardown: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  154. proto_notify_error "$cfg" "teardown: bonding_masters does not exist in sysfs (kernel module not loaded?)"
  155. proto_block_restart "$cfg"
  156. return
  157. }
  158. echo "-$link" > /sys/class/net/bonding_masters
  159. logger "bonding_teardown($1): $2"
  160. }
  161. add_protocol bonding