route.go 4.1 KB

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