-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetlink.go
130 lines (105 loc) · 3.61 KB
/
netlink.go
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
// Copyright 2022 OnMetal authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metalbond
import (
"fmt"
"net"
"sync"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
const METALBOND_RT_PROTO netlink.RouteProtocol = 254
type NetlinkClient struct {
config NetlinkClientConfig
tunDevice netlink.Link
mtx sync.Mutex
}
type NetlinkClientConfig struct {
VNITableMap map[VNI]int
LinkName string
IPv4Only bool
}
func NewNetlinkClient(config NetlinkClientConfig) (*NetlinkClient, error) {
link, err := netlink.LinkByName(config.LinkName)
if err != nil {
return nil, fmt.Errorf("Cannot find tun device '%s': %v", config.LinkName, err)
}
// TODO: Remove all routes from route tables defined in config.VNITableMap with Protocol = METALBOND_RT_PROTO
// to clean up old, stale routes installed by a prior metalbond client instance
return &NetlinkClient{
config: config,
tunDevice: link,
}, nil
}
func (c *NetlinkClient) AddRoute(vni VNI, dest Destination, hop NextHop) error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.config.IPv4Only && dest.IPVersion != IPV4 {
log.Infof("Received non-IPv4 route will not be installed in kernel route table (IPv4-only mode)")
return nil
}
table, exists := c.config.VNITableMap[vni]
if !exists {
return fmt.Errorf("No route table ID known for given VNI")
}
_, dst, err := net.ParseCIDR(dest.Prefix.String())
if err != nil {
return fmt.Errorf("cannot parse destination prefix: %v", err)
}
encap := netlink.IP6tnlEncap{
Dst: net.ParseIP(hop.TargetAddress.String()),
Src: net.ParseIP("::"), // what source ip to put here? Metalbond object, m, does not contain this info yet.
}
route := &netlink.Route{
LinkIndex: c.tunDevice.Attrs().Index,
Dst: dst,
Encap: &encap,
Table: table,
Protocol: METALBOND_RT_PROTO,
} // by default, the route is already installed into the kernel table without explicite specification
if err := netlink.RouteAdd(route); err != nil {
return fmt.Errorf("cannot add route to %s (table %d) to kernel: %v", dest, table, err)
}
return nil
}
func (c *NetlinkClient) RemoveRoute(vni VNI, dest Destination, hop NextHop) error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.config.IPv4Only && dest.IPVersion != IPV4 {
return nil
}
table, exists := c.config.VNITableMap[vni]
if !exists {
return fmt.Errorf("No route table ID known for given VNI")
}
_, dst, err := net.ParseCIDR(dest.Prefix.String())
if err != nil {
return fmt.Errorf("cannot parse destination prefix: %v", err)
}
encap := netlink.IP6tnlEncap{
Dst: net.ParseIP(hop.TargetAddress.String()),
Src: net.ParseIP("::"), // what source ip to put here? Metalbond object, m, does not contain this info yet.
}
route := &netlink.Route{
LinkIndex: c.tunDevice.Attrs().Index,
Dst: dst,
Encap: &encap,
Table: table,
Protocol: METALBOND_RT_PROTO,
} // by default, the route is already installed into the kernel table without explicite specification
if err := netlink.RouteDel(route); err != nil {
return fmt.Errorf("cannot remove route to %s (table %d) from kernel: %v", dest, table, err)
}
return nil
}