/
interface_config.go
231 lines (195 loc) · 5.05 KB
/
interface_config.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2016 Zebra Project.
//
// 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 rib
import (
"fmt"
"regexp"
"github.com/coreswitch/netutil"
)
type IfcFlag uint32
const IFC_FLAG_MAX = 32
const (
IFC_FLAG_SHUTDOWN = 1 << iota
IFC_FLAG_MTU
IFC_FLAG_DESCRIPTION
)
func (f IfcFlag) SetFlag(flag IfcFlag) IfcFlag {
f |= flag
return f
}
func (f IfcFlag) UnsetFlag(flag IfcFlag) IfcFlag {
f &^= flag
return f
}
func (f IfcFlag) CheckFlag(flag IfcFlag) bool {
return (f & flag) == flag
}
type InterfaceConfig struct {
Flags IfcFlag
Name string
Description string
Shutdown bool
HwAddr []byte
Index int
L3Index int
VlanId int
Addrs [AFI_MAX]IfAddrSlice
Interface *Interface
Mtu uint32
}
var InterfaceConfigMap = map[string]*InterfaceConfig{}
func NewInterfaceConfig(ifname string) *InterfaceConfig {
return &InterfaceConfig{Name: ifname}
}
func InterfaceConfigGet(ifname string) *InterfaceConfig {
ifc := InterfaceConfigMap[ifname]
if ifc == nil {
ifc = NewInterfaceConfig(ifname)
InterfaceConfigMap[ifname] = ifc
return ifc
}
return ifc
}
func InterfaceConfigLookup(ifname string) *InterfaceConfig {
return InterfaceConfigMap[ifname]
}
func InterfaceConfigPush() {
for ifname, _ := range IfMap {
ConfigPush([]string{"interfaces", "interface", ifname})
}
}
var ShutdownSkipHook func(ifp *Interface) bool
func EsiShutdownSkipHook(ifp *Interface) bool {
r := regexp.MustCompile(`eth\d+`)
if r.MatchString(ifp.Name) {
return true
}
return false
}
func (ifc *InterfaceConfig) ShutdownSync(ifp *Interface) {
if ShutdownSkipHook != nil {
if ShutdownSkipHook(ifp) {
return
}
}
if ifc.Shutdown {
if ifp.IsUp() {
LinkSetDown(ifp)
}
} else {
if !ifp.IsUp() {
LinkSetUp(ifp)
}
}
}
func (ifc *InterfaceConfig) MtuSync(ifp *Interface) {
if ifc.Flags.CheckFlag(IFC_FLAG_MTU) {
if ifc.Mtu != ifp.Mtu {
LinkSetMtu(ifp, ifc.Mtu)
}
} else {
if ifp.Mtu != ifp.DefaultMtu {
LinkSetMtu(ifp, ifp.DefaultMtu)
}
}
}
func (ifc *InterfaceConfig) Sync(flags IfcFlag) {
ifp := IfLookupByName(ifc.Name)
if ifp == nil {
return
}
for i := 0; i < IFC_FLAG_MAX; i++ {
flag := IfcFlag(1 << uint(i))
if flags.CheckFlag(flag) {
switch flag {
case IFC_FLAG_SHUTDOWN:
ifc.ShutdownSync(ifp)
case IFC_FLAG_MTU:
ifc.MtuSync(ifp)
case IFC_FLAG_DESCRIPTION:
ifp.Description = ifc.Description
}
}
}
}
func (ifc *InterfaceConfig) ShutdownSet() {
ifc.Shutdown = true
ifc.Flags = ifc.Flags.SetFlag(IFC_FLAG_SHUTDOWN)
ifc.Sync(IFC_FLAG_SHUTDOWN)
}
func (ifc *InterfaceConfig) ShutdownUnset() {
ifc.Shutdown = false
ifc.Flags = ifc.Flags.UnsetFlag(IFC_FLAG_SHUTDOWN)
ifc.Sync(IFC_FLAG_SHUTDOWN)
}
func (ifc *InterfaceConfig) MtuSet(mtu uint32) {
ifc.Mtu = mtu
ifc.Flags = ifc.Flags.SetFlag(IFC_FLAG_MTU)
ifc.Sync(IFC_FLAG_MTU)
}
func (ifc *InterfaceConfig) MtuUnset() {
ifc.Mtu = 0
ifc.Flags = ifc.Flags.UnsetFlag(IFC_FLAG_MTU)
ifc.Sync(IFC_FLAG_MTU)
}
func (ifc *InterfaceConfig) DescriptionSet(desc string) {
ifc.Description = desc
ifc.Flags = ifc.Flags.SetFlag(IFC_FLAG_DESCRIPTION)
ifc.Sync(IFC_FLAG_DESCRIPTION)
}
func (ifc *InterfaceConfig) DescriptionUnset() {
ifc.Description = ""
ifc.Flags = ifc.Flags.UnsetFlag(IFC_FLAG_DESCRIPTION)
ifc.Sync(IFC_FLAG_DESCRIPTION)
}
func (ifc *InterfaceConfig) AddrAdd(ifName string, addr *netutil.Prefix) error {
fmt.Println("[API] AddrAdd start:", ifName, addr)
ifaddr := &IfAddr{Prefix: addr}
afi := addr.AFI()
if lookup := ifc.Addrs[afi].Lookup(addr); lookup == nil {
ifc.Addrs[afi] = append(ifc.Addrs[afi], ifaddr)
ifp := IfLookupByName(ifName)
if ifp == nil {
fmt.Println("IfcAddrAdd can't find ifp", ifName)
return fmt.Errorf("IfcAddrAdd can't find ifp %s", ifName)
}
NetlinkIpAddrAdd(ifp, addr)
if !ifp.IsUp() {
fmt.Println("IfcAddrAdd !ifp.IsUp()", ifName)
}
}
fmt.Println("[API] AddrAdd end:", ifName, addr)
return nil
}
func (ifc *InterfaceConfig) AddrDelete(ifName string, addr *netutil.Prefix) error {
afi := addr.AFI()
if afi == AFI_MAX {
return fmt.Errorf("AddrDelete unknown AFI")
}
addrs, found := ifc.Addrs[afi].Delete(addr)
if found == nil {
// XXX Can't find ifc addr
fmt.Println("IfcAddrDelete can't find address", addr)
return fmt.Errorf("IfcAddrDelete can't find address %s", addr)
}
ifc.Addrs[afi] = addrs
ifp := IfLookupByName(ifName)
if ifp == nil {
fmt.Println("IfcAddrDelete can't find ifp", ifName)
return fmt.Errorf("IfcAddrDelete can't find ifp %s", ifName)
}
NetlinkIpAddrDelete(ifp, addr)
fmt.Println("[API] AddrDelete end:", ifName, addr)
return nil
}