bwutils.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bwutils
  15. import (
  16. "yunion.io/x/pkg/util/netutils"
  17. )
  18. func GetBwValue(bw int, ip string) int {
  19. if bw > 0 {
  20. if ip == "" {
  21. bw = 1
  22. } else {
  23. ipv4, err := netutils.NewIPV4Addr(ip)
  24. if err == nil && netutils.IsExitAddress(ipv4) {
  25. bw = 1
  26. } else {
  27. bw = 10000
  28. }
  29. }
  30. }
  31. return int(bw)
  32. }
  33. func GetDownloadBwValue(bw int, ip, ifname string, bwDownloadBandwidth int) (int, error) {
  34. if len(ip) > 0 {
  35. ipv4, err := netutils.NewIPV4Addr(ip)
  36. if err != nil {
  37. return 0, err
  38. }
  39. if netutils.IsExitAddress(ipv4) && len(ifname) > 0 && bwDownloadBandwidth > 0 {
  40. bw = GetBwValue(bw, ip)
  41. if bw > bwDownloadBandwidth {
  42. return bw, nil
  43. } else {
  44. return bwDownloadBandwidth, nil
  45. }
  46. }
  47. }
  48. return 0, nil
  49. }
  50. func GetOvsBwValues(bw int, ip string) (int, int, error) {
  51. var bwOvs int
  52. bw = GetBwValue(bw, ip)
  53. if ip != "" {
  54. ipv4, err := netutils.NewIPV4Addr(ip)
  55. if err != nil {
  56. return 0, 0, err
  57. }
  58. if netutils.IsExitAddress(ipv4) {
  59. bwOvs = 1000
  60. if bwOvs > bw*15 {
  61. bwOvs = bw * 15
  62. }
  63. } else {
  64. bwOvs = bw
  65. }
  66. } else {
  67. bwOvs = bw
  68. }
  69. return bwOvs * 1000, bwOvs * 2000, nil
  70. }