route.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  16. type Encap interface {
  17. Type() int
  18. Decode([]byte) error
  19. Encode() ([]byte, error)
  20. String() string
  21. }
  22. // Route represents a netlink route.
  23. type Route struct {
  24. LinkIndex int
  25. ILinkIndex int
  26. Scope Scope
  27. Dst *net.IPNet
  28. Src net.IP
  29. Gw net.IP
  30. MultiPath []*NexthopInfo
  31. Protocol int
  32. Priority int
  33. Table int
  34. Type int
  35. Tos int
  36. Flags int
  37. MPLSDst *int
  38. NewDst Destination
  39. Encap Encap
  40. }
  41. func (r Route) String() string {
  42. elems := []string{}
  43. if len(r.MultiPath) == 0 {
  44. elems = append(elems, fmt.Sprintf("Ifindex: %d", r.LinkIndex))
  45. }
  46. if r.MPLSDst != nil {
  47. elems = append(elems, fmt.Sprintf("Dst: %d", r.MPLSDst))
  48. } else {
  49. elems = append(elems, fmt.Sprintf("Dst: %s", r.Dst))
  50. }
  51. if r.NewDst != nil {
  52. elems = append(elems, fmt.Sprintf("NewDst: %s", r.NewDst))
  53. }
  54. if r.Encap != nil {
  55. elems = append(elems, fmt.Sprintf("Encap: %s", r.Encap))
  56. }
  57. elems = append(elems, fmt.Sprintf("Src: %s", r.Src))
  58. if len(r.MultiPath) > 0 {
  59. elems = append(elems, fmt.Sprintf("Gw: %s", r.MultiPath))
  60. } else {
  61. elems = append(elems, fmt.Sprintf("Gw: %s", r.Gw))
  62. }
  63. elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
  64. elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
  65. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  66. }
  67. func (r *Route) SetFlag(flag NextHopFlag) {
  68. r.Flags |= int(flag)
  69. }
  70. func (r *Route) ClearFlag(flag NextHopFlag) {
  71. r.Flags &^= int(flag)
  72. }
  73. type flagString struct {
  74. f NextHopFlag
  75. s string
  76. }
  77. // RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
  78. type RouteUpdate struct {
  79. Type uint16
  80. Route
  81. }
  82. type NexthopInfo struct {
  83. LinkIndex int
  84. Hops int
  85. Gw net.IP
  86. Flags int
  87. NewDst Destination
  88. Encap Encap
  89. }
  90. func (n *NexthopInfo) String() string {
  91. elems := []string{}
  92. elems = append(elems, fmt.Sprintf("Ifindex: %d", n.LinkIndex))
  93. if n.NewDst != nil {
  94. elems = append(elems, fmt.Sprintf("NewDst: %s", n.NewDst))
  95. }
  96. if n.Encap != nil {
  97. elems = append(elems, fmt.Sprintf("Encap: %s", n.Encap))
  98. }
  99. elems = append(elems, fmt.Sprintf("Weight: %d", n.Hops+1))
  100. elems = append(elems, fmt.Sprintf("Gw: %d", n.Gw))
  101. elems = append(elems, fmt.Sprintf("Flags: %s", n.ListFlags()))
  102. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  103. }