route.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. MTU int
  43. AdvMSS int
  44. }
  45. func (r Route) String() string {
  46. elems := []string{}
  47. if len(r.MultiPath) == 0 {
  48. elems = append(elems, fmt.Sprintf("Ifindex: %d", r.LinkIndex))
  49. }
  50. if r.MPLSDst != nil {
  51. elems = append(elems, fmt.Sprintf("Dst: %d", r.MPLSDst))
  52. } else {
  53. elems = append(elems, fmt.Sprintf("Dst: %s", r.Dst))
  54. }
  55. if r.NewDst != nil {
  56. elems = append(elems, fmt.Sprintf("NewDst: %s", r.NewDst))
  57. }
  58. if r.Encap != nil {
  59. elems = append(elems, fmt.Sprintf("Encap: %s", r.Encap))
  60. }
  61. elems = append(elems, fmt.Sprintf("Src: %s", r.Src))
  62. if len(r.MultiPath) > 0 {
  63. elems = append(elems, fmt.Sprintf("Gw: %s", r.MultiPath))
  64. } else {
  65. elems = append(elems, fmt.Sprintf("Gw: %s", r.Gw))
  66. }
  67. elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
  68. elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
  69. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  70. }
  71. func (r Route) Equal(x Route) bool {
  72. return r.LinkIndex == x.LinkIndex &&
  73. r.ILinkIndex == x.ILinkIndex &&
  74. r.Scope == x.Scope &&
  75. ipNetEqual(r.Dst, x.Dst) &&
  76. r.Src.Equal(x.Src) &&
  77. r.Gw.Equal(x.Gw) &&
  78. nexthopInfoSlice(r.MultiPath).Equal(x.MultiPath) &&
  79. r.Protocol == x.Protocol &&
  80. r.Priority == x.Priority &&
  81. r.Table == x.Table &&
  82. r.Type == x.Type &&
  83. r.Tos == x.Tos &&
  84. r.Flags == x.Flags &&
  85. (r.MPLSDst == x.MPLSDst || (r.MPLSDst != nil && x.MPLSDst != nil && *r.MPLSDst == *x.MPLSDst)) &&
  86. (r.NewDst == x.NewDst || (r.NewDst != nil && r.NewDst.Equal(x.NewDst))) &&
  87. (r.Encap == x.Encap || (r.Encap != nil && r.Encap.Equal(x.Encap)))
  88. }
  89. func (r *Route) SetFlag(flag NextHopFlag) {
  90. r.Flags |= int(flag)
  91. }
  92. func (r *Route) ClearFlag(flag NextHopFlag) {
  93. r.Flags &^= int(flag)
  94. }
  95. type flagString struct {
  96. f NextHopFlag
  97. s string
  98. }
  99. // RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
  100. type RouteUpdate struct {
  101. Type uint16
  102. Route
  103. }
  104. type NexthopInfo struct {
  105. LinkIndex int
  106. Hops int
  107. Gw net.IP
  108. Flags int
  109. NewDst Destination
  110. Encap Encap
  111. }
  112. func (n *NexthopInfo) String() string {
  113. elems := []string{}
  114. elems = append(elems, fmt.Sprintf("Ifindex: %d", n.LinkIndex))
  115. if n.NewDst != nil {
  116. elems = append(elems, fmt.Sprintf("NewDst: %s", n.NewDst))
  117. }
  118. if n.Encap != nil {
  119. elems = append(elems, fmt.Sprintf("Encap: %s", n.Encap))
  120. }
  121. elems = append(elems, fmt.Sprintf("Weight: %d", n.Hops+1))
  122. elems = append(elems, fmt.Sprintf("Gw: %s", n.Gw))
  123. elems = append(elems, fmt.Sprintf("Flags: %s", n.ListFlags()))
  124. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  125. }
  126. func (n NexthopInfo) Equal(x NexthopInfo) bool {
  127. return n.LinkIndex == x.LinkIndex &&
  128. n.Hops == x.Hops &&
  129. n.Gw.Equal(x.Gw) &&
  130. n.Flags == x.Flags &&
  131. (n.NewDst == x.NewDst || (n.NewDst != nil && n.NewDst.Equal(x.NewDst))) &&
  132. (n.Encap == x.Encap || (n.Encap != nil && n.Encap.Equal(x.Encap)))
  133. }
  134. type nexthopInfoSlice []*NexthopInfo
  135. func (n nexthopInfoSlice) Equal(x []*NexthopInfo) bool {
  136. if len(n) != len(x) {
  137. return false
  138. }
  139. for i := range n {
  140. if n[i] == nil || x[i] == nil {
  141. return false
  142. }
  143. if !n[i].Equal(*x[i]) {
  144. return false
  145. }
  146. }
  147. return true
  148. }
  149. // ipNetEqual returns true iff both IPNet are equal
  150. func ipNetEqual(ipn1 *net.IPNet, ipn2 *net.IPNet) bool {
  151. if ipn1 == ipn2 {
  152. return true
  153. }
  154. if ipn1 == nil || ipn2 == nil {
  155. return false
  156. }
  157. m1, _ := ipn1.Mask.Size()
  158. m2, _ := ipn2.Mask.Size()
  159. return m1 == m2 && ipn1.IP.Equal(ipn2.IP)
  160. }