-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.go
More file actions
179 lines (139 loc) · 3.66 KB
/
Copy pathpipe.go
File metadata and controls
179 lines (139 loc) · 3.66 KB
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
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"net"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
type pipe struct {
name string
prefix *net.IPNet
sourceTable int
targetTable int
proto netlink.RouteProtocol
currentSource *netlink.Route
curentTarget *netlink.Route
mu *sync.Mutex
}
func newPipe(name string, prefix net.IPNet, sourceTable int, targetTable int, proto netlink.RouteProtocol) *pipe {
var pfx = &prefix
o, _ := pfx.Mask.Size()
if o == 0 {
// default route
pfx = nil
}
return &pipe{
name: name,
prefix: pfx,
sourceTable: sourceTable,
targetTable: targetTable,
proto: proto,
mu: &sync.Mutex{},
}
}
func (p *pipe) processUpdate(ctx context.Context, u netlink.RouteUpdate) error {
p.mu.Lock()
defer p.mu.Unlock()
if u.Table != p.sourceTable && u.Table != p.targetTable {
return nil
}
if !p.pefixMatches(u.Dst) {
return nil
}
defer recordRouteUpdateProcessed(ctx, &u, p)
if isRouteDel(&u) {
return p.processRemove(ctx, u)
}
return p.processAdd(ctx, u)
}
func (p *pipe) pefixMatches(pfx *net.IPNet) bool {
if p.prefix == nil && pfx != nil {
return false
}
if pfx == nil && p.prefix != nil {
return false
}
if pfx == p.prefix {
return true
}
return pfx.String() == p.prefix.String()
}
func (p *pipe) processAdd(ctx context.Context, u netlink.RouteUpdate) error {
if u.Table == p.sourceTable {
return p.processAddInSource(ctx, u)
}
return p.processAddInTarget(ctx, u)
}
func (p *pipe) processAddInSource(ctx context.Context, u netlink.RouteUpdate) error {
logrus.Infof("Netlink added route in source table: %v", u.Route)
p.currentSource = &u.Route
if p.curentTarget != nil && p.routeEqual(*p.curentTarget, u.Route) {
return nil
}
return p.replaceRoute(ctx, u.Route)
}
func (p *pipe) processAddInTarget(ctx context.Context, u netlink.RouteUpdate) error {
logrus.Infof("Netlink added route in target table: %v", u.Route)
p.curentTarget = &u.Route
// nothing more to be done. we only want to set routes if no route for the prefix exists or its ours
return nil
}
func (p *pipe) processRemove(ctx context.Context, u netlink.RouteUpdate) error {
if u.Table == p.sourceTable {
return p.processRemoveInSource(ctx, u)
}
return p.processRemoveInTarget(ctx, u)
}
func (p *pipe) processRemoveInSource(ctx context.Context, u netlink.RouteUpdate) error {
logrus.Infof("Netlink removed route in source table: %v", u.Route)
p.currentSource = nil
return nil
}
func (p *pipe) processRemoveInTarget(ctx context.Context, u netlink.RouteUpdate) error {
logrus.Infof("Netlink removed route in target table: %v", u.Route)
p.curentTarget = nil
if u.Protocol == p.proto {
go func() {
<-time.After(1 * time.Second)
source := p.currentSource
if source != nil && p.curentTarget == nil {
logrus.Infof("Restoring route: %v", source)
if err := p.replaceRoute(ctx, *source); err != nil {
logrus.Errorf("could not restore route: %v", err)
}
}
}()
}
return nil
}
func (p *pipe) routeEqual(r1, r2 netlink.Route) bool {
if !r1.Gw.Equal(r2.Gw) {
return false
}
if !r1.Src.Equal(r2.Src) {
return false
}
if r1.Priority != r2.Priority {
return false
}
if r1.LinkIndex != r2.LinkIndex {
return false
}
return true
}
func (p *pipe) replaceRoute(ctx context.Context, r netlink.Route) error {
logrus.Infof("Replacing route: %v", r)
new := &r
new.Protocol = p.proto
new.Table = p.targetTable
err := netlink.RouteReplace(new)
if err != nil {
recordRouteReplaceError(ctx, p)
return errors.Wrapf(err, "could not add route to table %d: %v", p.targetTable, r)
}
recordRouteReplaced(ctx, p)
return nil
}