2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-02-19 03:22:09 +00:00
|
|
|
"strings"
|
2014-09-15 00:44:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Scope is an enum representing a route scope.
|
|
|
|
type Scope uint8
|
|
|
|
|
2015-10-09 08:09:44 +00:00
|
|
|
type NextHopFlag int
|
|
|
|
|
2017-02-19 03:22:09 +00:00
|
|
|
type Destination interface {
|
|
|
|
Family() int
|
|
|
|
Decode([]byte) error
|
|
|
|
Encode() ([]byte, error)
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Encap interface {
|
|
|
|
Type() int
|
|
|
|
Decode([]byte) error
|
|
|
|
Encode() ([]byte, error)
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
// Route represents a netlink route.
|
2014-09-01 03:27:34 +00:00
|
|
|
type Route struct {
|
2015-12-08 22:35:55 +00:00
|
|
|
LinkIndex int
|
|
|
|
ILinkIndex int
|
|
|
|
Scope Scope
|
|
|
|
Dst *net.IPNet
|
|
|
|
Src net.IP
|
|
|
|
Gw net.IP
|
2016-06-15 05:02:24 +00:00
|
|
|
MultiPath []*NexthopInfo
|
2015-12-08 22:35:55 +00:00
|
|
|
Protocol int
|
|
|
|
Priority int
|
|
|
|
Table int
|
|
|
|
Type int
|
|
|
|
Tos int
|
|
|
|
Flags int
|
2017-02-19 03:22:09 +00:00
|
|
|
MPLSDst *int
|
|
|
|
NewDst Destination
|
|
|
|
Encap Encap
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Route) String() string {
|
2017-02-19 03:22:09 +00:00
|
|
|
elems := []string{}
|
|
|
|
if len(r.MultiPath) == 0 {
|
|
|
|
elems = append(elems, fmt.Sprintf("Ifindex: %d", r.LinkIndex))
|
|
|
|
}
|
|
|
|
if r.MPLSDst != nil {
|
|
|
|
elems = append(elems, fmt.Sprintf("Dst: %d", r.MPLSDst))
|
|
|
|
} else {
|
|
|
|
elems = append(elems, fmt.Sprintf("Dst: %s", r.Dst))
|
|
|
|
}
|
|
|
|
if r.NewDst != nil {
|
|
|
|
elems = append(elems, fmt.Sprintf("NewDst: %s", r.NewDst))
|
|
|
|
}
|
|
|
|
if r.Encap != nil {
|
|
|
|
elems = append(elems, fmt.Sprintf("Encap: %s", r.Encap))
|
|
|
|
}
|
|
|
|
elems = append(elems, fmt.Sprintf("Src: %s", r.Src))
|
2016-06-15 05:02:24 +00:00
|
|
|
if len(r.MultiPath) > 0 {
|
2017-02-19 03:22:09 +00:00
|
|
|
elems = append(elems, fmt.Sprintf("Gw: %s", r.MultiPath))
|
|
|
|
} else {
|
|
|
|
elems = append(elems, fmt.Sprintf("Gw: %s", r.Gw))
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
|
|
|
|
elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
|
|
|
|
return fmt.Sprintf("{%s}", strings.Join(elems, " "))
|
2015-10-09 08:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) SetFlag(flag NextHopFlag) {
|
|
|
|
r.Flags |= int(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) ClearFlag(flag NextHopFlag) {
|
|
|
|
r.Flags &^= int(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
type flagString struct {
|
|
|
|
f NextHopFlag
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:42:33 +00:00
|
|
|
// RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
|
|
|
|
type RouteUpdate struct {
|
|
|
|
Type uint16
|
|
|
|
Route
|
|
|
|
}
|
2016-06-15 05:02:24 +00:00
|
|
|
|
|
|
|
type NexthopInfo struct {
|
|
|
|
LinkIndex int
|
|
|
|
Hops int
|
|
|
|
Gw net.IP
|
2016-08-22 02:40:27 +00:00
|
|
|
Flags int
|
2017-02-19 03:22:09 +00:00
|
|
|
NewDst Destination
|
|
|
|
Encap Encap
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NexthopInfo) String() string {
|
2017-02-19 03:22:09 +00:00
|
|
|
elems := []string{}
|
|
|
|
elems = append(elems, fmt.Sprintf("Ifindex: %d", n.LinkIndex))
|
|
|
|
if n.NewDst != nil {
|
|
|
|
elems = append(elems, fmt.Sprintf("NewDst: %s", n.NewDst))
|
|
|
|
}
|
|
|
|
if n.Encap != nil {
|
|
|
|
elems = append(elems, fmt.Sprintf("Encap: %s", n.Encap))
|
|
|
|
}
|
|
|
|
elems = append(elems, fmt.Sprintf("Weight: %d", n.Hops+1))
|
2017-09-11 14:05:27 +00:00
|
|
|
elems = append(elems, fmt.Sprintf("Gw: %s", n.Gw))
|
2017-02-19 03:22:09 +00:00
|
|
|
elems = append(elems, fmt.Sprintf("Flags: %s", n.ListFlags()))
|
|
|
|
return fmt.Sprintf("{%s}", strings.Join(elems, " "))
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|