route.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. )
  7. // Scope is an enum representing a route scope.
  8. type Scope uint8
  9. type NextHopFlag int
  10. type Destination interface {
  11. Family() int
  12. Decode([]byte) error
  13. Encode() ([]byte, error)
  14. String() string
  15. Equal(Destination) bool
  16. }
  17. type Encap interface {
  18. Type() int
  19. Decode([]byte) error
  20. Encode() ([]byte, error)
  21. String() string
  22. Equal(Encap) bool
  23. }
  24. // Route represents a netlink route.
  25. type Route struct {
  26. LinkIndex int
  27. ILinkIndex int
  28. Scope Scope
  29. Dst *net.IPNet
  30. Src net.IP
  31. Gw net.IP
  32. MultiPath []*NexthopInfo
  33. Protocol int
  34. Priority int
  35. Table int
  36. Type int
  37. Tos int
  38. Flags int
  39. MPLSDst *int
  40. NewDst Destination
  41. Encap Encap
  42. }
  43. func (r Route) String() string {
  44. elems := []string{}
  45. if len(r.MultiPath) == 0 {
  46. elems = append(elems, fmt.Sprintf("Ifindex: %d", r.LinkIndex))
  47. }
  48. if r.MPLSDst != nil {
  49. elems = append(elems, fmt.Sprintf("Dst: %d", r.MPLSDst))
  50. } else {
  51. elems = append(elems, fmt.Sprintf("Dst: %s", r.Dst))
  52. }
  53. if r.NewDst != nil {
  54. elems = append(elems, fmt.Sprintf("NewDst: %s", r.NewDst))
  55. }
  56. if r.Encap != nil {
  57. elems = append(elems, fmt.Sprintf("Encap: %s", r.Encap))
  58. }
  59. elems = append(elems, fmt.Sprintf("Src: %s", r.Src))
  60. if len(r.MultiPath) > 0 {
  61. elems = append(elems, fmt.Sprintf("Gw: %s", r.MultiPath))
  62. } else {
  63. elems = append(elems, fmt.Sprintf("Gw: %s", r.Gw))
  64. }
  65. elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
  66. elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
  67. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  68. }
  69. func (r Route) Equal(x Route) bool {
  70. return r.LinkIndex == x.LinkIndex &&
  71. r.ILinkIndex == x.ILinkIndex &&
  72. r.Scope == x.Scope &&
  73. ipNetEqual(r.Dst, x.Dst) &&
  74. r.Src.Equal(x.Src) &&
  75. r.Gw.Equal(x.Gw) &&
  76. nexthopInfoSlice(r.MultiPath).Equal(x.MultiPath) &&
  77. r.Protocol == x.Protocol &&
  78. r.Priority == x.Priority &&
  79. r.Table == x.Table &&
  80. r.Type == x.Type &&
  81. r.Tos == x.Tos &&
  82. r.Flags == x.Flags &&
  83. (r.MPLSDst == x.MPLSDst || (r.MPLSDst != nil && x.MPLSDst != nil && *r.MPLSDst == *x.MPLSDst)) &&
  84. (r.NewDst == x.NewDst || (r.NewDst != nil && r.NewDst.Equal(x.NewDst))) &&
  85. (r.Encap == x.Encap || (r.Encap != nil && r.Encap.Equal(x.Encap)))
  86. }
  87. func (r *Route) SetFlag(flag NextHopFlag) {
  88. r.Flags |= int(flag)
  89. }
  90. func (r *Route) ClearFlag(flag NextHopFlag) {
  91. r.Flags &^= int(flag)
  92. }
  93. type flagString struct {
  94. f NextHopFlag
  95. s string
  96. }
  97. // RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
  98. type RouteUpdate struct {
  99. Type uint16
  100. Route
  101. }
  102. type NexthopInfo struct {
  103. LinkIndex int
  104. Hops int
  105. Gw net.IP
  106. Flags int
  107. NewDst Destination
  108. Encap Encap
  109. }
  110. func (n *NexthopInfo) String() string {
  111. elems := []string{}
  112. elems = append(elems, fmt.Sprintf("Ifindex: %d", n.LinkIndex))
  113. if n.NewDst != nil {
  114. elems = append(elems, fmt.Sprintf("NewDst: %s", n.NewDst))
  115. }
  116. if n.Encap != nil {
  117. elems = append(elems, fmt.Sprintf("Encap: %s", n.Encap))
  118. }
  119. elems = append(elems, fmt.Sprintf("Weight: %d", n.Hops+1))
  120. elems = append(elems, fmt.Sprintf("Gw: %s", n.Gw))
  121. elems = append(elems, fmt.Sprintf("Flags: %s", n.ListFlags()))
  122. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  123. }
  124. func (n NexthopInfo) Equal(x NexthopInfo) bool {
  125. return n.LinkIndex == x.LinkIndex &&
  126. n.Hops == x.Hops &&
  127. n.Gw.Equal(x.Gw) &&
  128. n.Flags == x.Flags &&
  129. (n.NewDst == x.NewDst || (n.NewDst != nil && n.NewDst.Equal(x.NewDst))) &&
  130. (n.Encap == x.Encap || (n.Encap != nil && n.Encap.Equal(x.Encap)))
  131. }
  132. type nexthopInfoSlice []*NexthopInfo
  133. func (n nexthopInfoSlice) Equal(x []*NexthopInfo) bool {
  134. if len(n) != len(x) {
  135. return false
  136. }
  137. for i := range n {
  138. if n[i] == nil || x[i] == nil {
  139. return false
  140. }
  141. if !n[i].Equal(*x[i]) {
  142. return false
  143. }
  144. }
  145. return true
  146. }
  147. // ipNetEqual returns true iff both IPNet are equal
  148. func ipNetEqual(ipn1 *net.IPNet, ipn2 *net.IPNet) bool {
  149. if ipn1 == ipn2 {
  150. return true
  151. }
  152. if ipn1 == nil || ipn2 == nil {
  153. return false
  154. }
  155. m1, _ := ipn1.Mask.Size()
  156. m2, _ := ipn2.Mask.Size()
  157. return m1 == m2 && ipn1.IP.Equal(ipn2.IP)
  158. }