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.

148 lines
2.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2013 Julius Schulz-Zander <julius@net.t-labs.tu-berlin.de>
  3. # Copyright (C) 2014-2017 OpenWrt.org
  4. # Copyright (C) 2018 Yousong Zhou <yszhou4tech@gmail.com>
  5. # Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
  6. . /lib/functions/procd.sh
  7. START=15
  8. ovs_ctl="/usr/share/openvswitch/scripts/ovs-ctl"; [ -x "$ovs_ctl" ] || ovs_ctl=:
  9. ovn_ctl="/usr/share/ovn/scripts/ovn-ctl"; [ -x "$ovn_ctl" ] || ovn_ctl=:
  10. extra_command "status" "Get status information"
  11. service_triggers() {
  12. procd_add_reload_trigger openvswitch
  13. }
  14. init_triggers() {
  15. procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
  16. procd_close_service set
  17. }
  18. start() {
  19. init_triggers
  20. ovs_action start "$@"
  21. }
  22. reload() {
  23. start
  24. }
  25. running() {
  26. return 0
  27. }
  28. stop() {
  29. procd_kill "$(basename ${basescript:-$initscript})"
  30. ovs_action stop "$@"
  31. }
  32. restart() {
  33. init_triggers
  34. ovs_action restart "$@"
  35. }
  36. status() {
  37. ovs_action status "$@"
  38. }
  39. ovs_action_cfgs=
  40. ovs_action() {
  41. local action="$1"; shift
  42. local cfgtype
  43. ovs_action_cfgs="$*"
  44. config_load openvswitch
  45. for cfgtype in ovs ovn_northd ovn_controller; do
  46. config_foreach "ovs_xx" "$cfgtype" "$action" "$cfgtype"
  47. done
  48. config_foreach ovs_bridge_init "ovs_bridge"
  49. }
  50. ovs_xx() {
  51. local cfg="$1"
  52. local action="$2"
  53. local cfgtype="$3"
  54. local disabled
  55. if [ -n "$ovs_action_cfgs" ] && ! list_contains "ovs_action_cfgs" "$cfg"; then
  56. return
  57. fi
  58. case "$action" in
  59. status|stop) ;;
  60. *)
  61. config_get_bool disabled "$cfg" disabled 0
  62. [ "$disabled" == "0" ] || return
  63. ;;
  64. esac
  65. case "$cfgtype" in
  66. ovs)
  67. "$ovs_ctl" "$action" \
  68. --system-id=random
  69. ;;
  70. ovn_*)
  71. "$ovn_ctl" "${action}_${cfgtype#ovn_}"
  72. ;;
  73. esac
  74. }
  75. ovs_bridge_parse_port() {
  76. case "$1" in
  77. *:*)
  78. port="${1%%:*}"
  79. type="${1#*:}"
  80. ;;
  81. *)
  82. port="$1"
  83. type=""
  84. ;;
  85. esac
  86. }
  87. ovs_bridge_port_add() {
  88. [ -n "$1" ] || return
  89. ovs_bridge_parse_port "$1"
  90. cur_type="$(ovs-vsctl get interface "$port" type 2>/dev/null)"
  91. [ "$?" = 0 ] && {
  92. [ "$type" = "$cur_type" ] || ovs-vsctl del-port "$port"
  93. }
  94. ovs-vsctl --may-exist add-port "$name" "$port" ${type:+ -- set interface "$port" type="$type"}
  95. __port_list="$__port_list ${port} "
  96. }
  97. ovs_bridge_port_cleanup() {
  98. for port in `ovs-vsctl list-ports "$name"`; do
  99. case "$__port_list" in
  100. *" $port "*);;
  101. *) ovs-vsctl del-port "$port";;
  102. esac
  103. done
  104. }
  105. ovs_bridge_init() {
  106. local cfg="$1"
  107. local disabled
  108. local name
  109. local controller
  110. config_get_bool disabled "$cfg" disabled 0
  111. [ "$disabled" == "0" ] || return
  112. config_get name "$cfg" name $cfg
  113. ovs-vsctl --may-exist add-br "$name"
  114. config_list_foreach "$cfg" "ports" ovs_bridge_port_add
  115. config_get_bool drop "$cfg" "drop_unknown_ports" 0
  116. [ "$drop" == 1 ] && ovs_bridge_port_cleanup
  117. config_get controller "$cfg" controller
  118. [ -n "$controller" ] && \
  119. ovs-vsctl set-controller "$name" "$controller"
  120. }