-
Notifications
You must be signed in to change notification settings - Fork 17
/
factory.go
123 lines (105 loc) · 2.84 KB
/
factory.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
// -*- coding: utf-8 -*-
// Copyright (C) 2018 Nippon Telegraph and Telephone Corporation.
//
// 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 main
import (
"fmt"
"strconv"
"strings"
"sync/atomic"
"github.com/osrg/gobgp/packet/bgp"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
const (
TunnelIfNameDelim = "_"
TunnelCounterMin = 4096
TunnelTypeIp4 = "ipip"
TunnelTypeIp6 = "ip6tnl"
)
//
// TunnelFactory is factory for TunnelTable.
//
type TunnelFactory struct {
counter uint32
device string
prefix string
}
func NewTunnelFactory(device string) *TunnelFactory {
return &TunnelFactory{
counter: TunnelCounterMin,
device: device,
prefix: device + TunnelIfNameDelim,
}
}
func (t *TunnelFactory) NextCounter() uint32 {
return atomic.AddUint32(&t.counter, 1)
}
func (t *TunnelFactory) NewIfName(counter uint32) string {
return fmt.Sprintf("%s%x", t.prefix, counter)
}
func (t *TunnelFactory) ParseIfName(ifname string) (string, uint32, bool) {
if ok := strings.HasPrefix(ifname, t.prefix); !ok {
log.Debugf("TunnelFactory.ParseIfName No prefix %s", ifname)
return ifname, 0, false
}
subfix := ifname[len(t.prefix):]
v, err := strconv.ParseUint(subfix, 16, 32)
if err != nil {
log.Debugf("TunnelFactory.ParseIfName Parse failed. %s", ifname)
return ifname, 0, false
}
log.Debugf("TunnelFactory.ParseIfName %s %d", t.device, v)
return t.device, uint32(v), true
}
func (t *TunnelFactory) LinkList(f func(netlink.Link, uint32)) error {
links, err := netlink.LinkList()
if err != nil {
log.Errorf("TunnelFactory.LinkList error. %s", err)
return err
}
for _, link := range links {
ifname := link.Attrs().Name
if _, id, ok := t.ParseIfName(ifname); ok {
linkType := link.Type()
if linkType == TunnelTypeIp4 || linkType == TunnelTypeIp6 {
f(link, id)
}
}
}
return nil
}
func (t *TunnelFactory) RouteList(link netlink.Link, f func(uint16, *netlink.Route)) error {
routes, err := netlink.RouteList(link, unix.AF_INET)
if err != nil {
return nil
}
for _, route := range routes {
if route.Dst.IP.IsGlobalUnicast() {
f(bgp.AFI_IP, &route)
}
}
routes, err = netlink.RouteList(link, unix.AF_INET6)
if err != nil {
return nil
}
for _, route := range routes {
if route.Dst.IP.IsGlobalUnicast() {
f(bgp.AFI_IP6, &route)
}
}
return nil
}