Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package route
import (
"fmt"
bnet "github.com/bio-routing/bio-rd/net"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
const (
ProtoBio = 45
)
// NetlinkPath represents a path learned via Netlink of a route
type NetlinkPath struct {
Dst bnet.Prefix
Src bnet.IP
NextHop bnet.IP // GW
Priority int
Protocol int
Type int
Table int
Kernel bool // True if the route is already installed in the kernel
}
func NewNlPathFromBgpPath(p *BGPPath) *NetlinkPath {
return &NetlinkPath{
Src: p.Source,
NextHop: p.NextHop,
Protocol: ProtoBio,
Kernel: false,
}
}
func NewNlPathFromRoute(r *netlink.Route, kernel bool) (*NetlinkPath, error) {
var src bnet.IP
var dst bnet.Prefix
if r.Src == nil && r.Dst == nil {
return nil, fmt.Errorf("Cannot create NlPath, since source and destination are both nil")
}
if r.Src == nil && r.Dst != nil {
dst = bnet.NewPfxFromIPNet(r.Dst)
if dst.Addr().IsIPv4() {
src = bnet.IPv4FromOctets(0, 0, 0, 0)
} else {
src = bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0)
}
}
if r.Src != nil && r.Dst == nil {
src, _ = bnet.IPFromBytes(r.Src)
if src.IsIPv4() {
dst = bnet.NewPfx(bnet.IPv4FromOctets(0, 0, 0, 0), 0)
} else {
dst = bnet.NewPfx(bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0), 0)
}
}
if r.Src != nil && r.Dst != nil {
src, _ = bnet.IPFromBytes(r.Src)
dst = bnet.NewPfxFromIPNet(r.Dst)
}
log.Warnf("IPFromBytes: %v goes to %v", r.Src, src)
log.Warnf("IPFromBytes: %v goes to %v", r.Dst, dst)
nextHop, _ := bnet.IPFromBytes(r.Gw)
return &NetlinkPath{
Dst: dst,
Src: src,
NextHop: nextHop,
Priority: r.Priority,
Protocol: r.Protocol,
Type: r.Type,
Table: r.Table,
Kernel: kernel,
}, nil
}
// Compare returns negative if s < t, 0 if paths are equal, positive if s > t
func (s *NetlinkPath) Select(t *NetlinkPath) int8 {
if !s.Dst.Equal(t.Dst) {
return 1
}
if s.NextHop.Compare(t.NextHop) > 0 {
return -1
}
if s.NextHop.Compare(t.NextHop) < 0 {
return 1
}
if s.Src.Compare(t.Src) > 0 {
return -1
}
if s.Src.Compare(t.Src) < 0 {
return 1
}
if s.Priority < t.Priority {
return -1
}
if s.Priority > t.Priority {
return 1
}
if s.Protocol < t.Protocol {
return -1
}
if s.Protocol > t.Protocol {
return 1
}
if s.Table < t.Table {
return -1
}
if s.Table > t.Table {
return 1
}
return 0
}
// ECMP determines if path s and t are equal in terms of ECMP
func (s *NetlinkPath) ECMP(t *NetlinkPath) bool {
return true
}
func (s *NetlinkPath) Copy() *NetlinkPath {
if s == nil {
return nil
}
cp := *s
return &cp
}
// get all known information about a route in a machine readable form
func (s *NetlinkPath) String() string {
ret := fmt.Sprintf("Destination: %s, ", s.Dst.String())
ret += fmt.Sprintf("Source: %s, ", s.Src.String())
ret += fmt.Sprintf("NextHop: %s, ", s.NextHop.String())
ret += fmt.Sprintf("Priority: %d, ", s.Priority)
ret += fmt.Sprintf("Type: %d, ", s.Type)
ret += fmt.Sprintf("Table: %d", s.Table)
return ret
}
// Pretty Print all known information about a route in human readable form
func (s *NetlinkPath) Print() string {
ret := fmt.Sprintf("\t\tDestination: %s\n", s.Dst.String())
ret += fmt.Sprintf("\t\tSource: %s\n", s.Src.String())
ret += fmt.Sprintf("\t\tNextHop: %s\n", s.NextHop.String())
ret += fmt.Sprintf("\t\tPriority: %d\n", s.Priority)
ret += fmt.Sprintf("\t\tType: %d\n", s.Type)
ret += fmt.Sprintf("\t\tTable: %d\n", s.Table)
return ret
}