forked from FlexSNR/l2
/
helper.go
364 lines (335 loc) · 9.75 KB
/
helper.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
//Copyright [2016] [SnapRoute Inc]
//
//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.
//
// _______ __ __________ ___ _______.____ __ ____ __ .___________. ______ __ __
// | ____|| | | ____\ \ / / / |\ \ / \ / / | | | | / || | | |
// | |__ | | | |__ \ V / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
package server
import (
"errors"
"fmt"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"l2/lldp/config"
"l2/lldp/packet"
"l2/lldp/utils"
"net"
"strconv"
"strings"
"sync"
"time"
)
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}
/* Init l2 port information for global runtime information
*/
func (intf *LLDPGlobalInfo) InitRuntimeInfo(portConf *config.PortInfo) {
intf.Port = *portConf
intf.RxInfo = packet.RxInit()
intf.TxInfo = packet.TxInit(LLDP_DEFAULT_TX_INTERVAL, LLDP_DEFAULT_TX_HOLD_MULTIPLIER)
intf.RxKill = make(chan bool)
intf.TxDone = make(chan bool)
intf.RxLock = &sync.RWMutex{}
}
/* De-Init l2 port information
*/
func (intf *LLDPGlobalInfo) DeInitRuntimeInfo() {
intf.StopCacheTimer()
intf.DeletePcapHandler()
intf.FreeDynamicMemory()
}
/* Delete l2 port pcap handler
*/
func (intf *LLDPGlobalInfo) DeletePcapHandler() {
if intf.PcapHandle != nil {
// Send go routine kill signal right away before even we do anything else
if intf.RxInfo.RxRunning {
intf.RxKill <- true
<-intf.RxKill
}
// @FIXME: some bug in close handling that causes 5 mins delay
intf.PcapHandle.Close()
intf.PcapHandle = nil
}
}
/* Based on configuration we will enable disable lldp per port
*/
func (intf *LLDPGlobalInfo) Enable() {
intf.enable = true
}
/* Based on configuration we will enable disable lldp per port
*/
func (intf *LLDPGlobalInfo) Disable() {
intf.enable = false
}
/* Check LLDP is disabled or not
*/
func (intf *LLDPGlobalInfo) isDisabled() bool {
return !intf.enable
}
/* Check LLDP is enabled or not
*/
func (intf *LLDPGlobalInfo) isEnabled() bool {
return intf.enable
}
/* Stop RX cache timer
*/
func (intf *LLDPGlobalInfo) StopCacheTimer() {
intf.RxLock.Lock()
defer intf.RxLock.Unlock()
if intf.RxInfo.ClearCacheTimer == nil {
return
}
intf.RxInfo.ClearCacheTimer.Stop()
}
/* Return back all the memory which was allocated using new
*/
func (intf *LLDPGlobalInfo) FreeDynamicMemory() {
intf.RxLock.Lock()
defer intf.RxLock.Unlock()
intf.RxInfo.RxFrame = nil
intf.RxInfo.RxLinkInfo = nil
}
/* Create Pcap Handler
*/
func (intf *LLDPGlobalInfo) CreatePcapHandler(lldpSnapshotLen int32, lldpPromiscuous bool, lldpTimeout time.Duration) error {
debug.Logger.Debug("Creating Pcap for port:", intf.Port.Name, "ifIndex:", intf.Port.IfIndex)
pcapHdl, err := pcap.OpenLive(intf.Port.Name, lldpSnapshotLen, lldpPromiscuous, lldpTimeout)
if err != nil {
debug.Logger.Err(fmt.Sprintln("Creating Pcap Handler failed for", intf.Port.Name, "Error:", err))
return errors.New("Creating Pcap Failed")
}
err = pcapHdl.SetBPFFilter(LLDP_BPF_FILTER)
if err != nil {
debug.Logger.Err(fmt.Sprintln("setting filter:", LLDP_BPF_FILTER, "for", intf.Port.Name,
"failed with error:", err))
return errors.New("Setting BPF Filter Failed")
}
intf.PcapHandle = pcapHdl
debug.Logger.Debug("Pcap Created for port:", intf.Port.Name, "ifIndex:", intf.Port.IfIndex)
return nil
}
/* Get Chassis Id info
* Based on SubType Return the string, mac address then form string using
* net package
*/
func (intf *LLDPGlobalInfo) GetChassisIdInfo() string {
retVal := ""
switch intf.RxInfo.RxFrame.ChassisID.Subtype {
case layers.LLDPChassisIDSubTypeReserved:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubTypeChassisComp:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubtypeIfaceAlias:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubTypePortComp:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubTypeMACAddr:
var mac net.HardwareAddr
mac = intf.RxInfo.RxFrame.ChassisID.ID
return mac.String()
case layers.LLDPChassisIDSubTypeNetworkAddr:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubtypeIfaceName:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPChassisIDSubTypeLocal:
debug.Logger.Debug("Need to handle this case")
default:
return retVal
}
return retVal
}
/* Get Port Id info
* Based on SubType Return the string, mac address then form string using
* net package
*/
func (intf *LLDPGlobalInfo) GetPortIdInfo() string {
retVal := ""
switch intf.RxInfo.RxFrame.PortID.Subtype {
case layers.LLDPPortIDSubtypeReserved:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPPortIDSubtypeIfaceAlias:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPPortIDSubtypePortComp:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPPortIDSubtypeMACAddr:
var mac net.HardwareAddr
mac = intf.RxInfo.RxFrame.ChassisID.ID
return mac.String()
case layers.LLDPPortIDSubtypeNetworkAddr:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPPortIDSubtypeIfaceName:
return string(intf.RxInfo.RxFrame.PortID.ID)
case layers.LLDPPortIDSubtypeAgentCircuitID:
debug.Logger.Debug("Need to handle this case")
case layers.LLDPPortIDSubtypeLocal:
debug.Logger.Debug("Need to handle this case")
default:
return retVal
}
return retVal
}
/* Get System Capability info
* Based on booleans value Return the string, which states what system capabilities are enabled
*/
func (intf *LLDPGlobalInfo) GetSystemCap() string {
retVal := ""
systemCap := intf.RxInfo.RxLinkInfo.SysCapabilities.SystemCap
if systemCap.Other {
retVal += "Other, "
}
if systemCap.Repeater {
retVal += "Repeater, "
}
if systemCap.Bridge {
retVal += "Bridge, "
}
if systemCap.WLANAP {
retVal += "WlanAP, "
}
if systemCap.Router {
retVal += "Router, "
}
if systemCap.Phone {
retVal += "Phone, "
}
if systemCap.DocSis {
retVal += "DocSis, "
}
if systemCap.StationOnly {
retVal += "StationOnly, "
}
if systemCap.CVLAN {
retVal += "CVlan, "
}
if systemCap.SVLAN {
retVal += "SVlan, "
}
if systemCap.TMPR {
retVal += "TMPR, "
}
return strings.TrimSuffix(retVal, ", ")
}
/* Get Enabled Capability info
* Based on booleans value Return the string, which states what enabled capabilities are enabled
*/
func (intf *LLDPGlobalInfo) GetEnabledCap() string {
retVal := ""
enabledCap := intf.RxInfo.RxLinkInfo.SysCapabilities.EnabledCap
if enabledCap.Other {
retVal += "Other, "
}
if enabledCap.Repeater {
retVal += "Repeater, "
}
if enabledCap.Bridge {
retVal += "Bridge, "
}
if enabledCap.WLANAP {
retVal += "WlanAP, "
}
if enabledCap.Router {
retVal += "Router, "
}
if enabledCap.Phone {
retVal += "Phone, "
}
if enabledCap.DocSis {
retVal += "DocSis, "
}
if enabledCap.StationOnly {
retVal += "StationOnly, "
}
if enabledCap.CVLAN {
retVal += "CVlan, "
}
if enabledCap.SVLAN {
retVal += "SVlan, "
}
if enabledCap.TMPR {
retVal += "TMPR, "
}
return strings.TrimSuffix(retVal, ", ")
}
/* Get Peer Host Name information
*
*/
func (intf *LLDPGlobalInfo) GetPeerHostName() string {
return intf.RxInfo.RxLinkInfo.SysName
}
/* Get Peer Host Name information
*
*/
func (intf *LLDPGlobalInfo) GetSystemDescription() string {
return intf.RxInfo.RxLinkInfo.SysDescription
}
/* dump received lldp frame and other TX information
*/
func (intf LLDPGlobalInfo) DumpFrame() {
intf.RxLock.RLock()
defer intf.RxLock.RUnlock()
debug.Logger.Debug("L2 Port:", intf.Port.IfIndex, "Port IfIndex:", intf.Port.IfIndex)
debug.Logger.Debug("SrcMAC:", intf.RxInfo.SrcMAC.String(), "DstMAC:", intf.RxInfo.DstMAC.String())
debug.Logger.Debug("ChassisID info is", intf.RxInfo.RxFrame.ChassisID)
debug.Logger.Debug("PortID info is", intf.RxInfo.RxFrame.PortID)
debug.Logger.Debug("TTL info is", intf.RxInfo.RxFrame.TTL)
debug.Logger.Debug("Optional Values is", intf.RxInfo.RxLinkInfo)
}
/* Api used to get entry.. This is mainly used by LLDP Server API Layer when it get config from
* North Bound Plugin...
*/
func (svr *LLDPServer) EntryExist(intfRef string) (int32, bool) {
// first check whether the input is all numbers
if ifIndex, err := strconv.Atoi(intfRef); err == nil {
_, exists := svr.lldpGblInfo[int32(ifIndex)]
if exists {
return int32(ifIndex), exists
}
} else {
// this is proper interface reference lets check the xRef
ifIndex, exists := svr.lldpIntfRef2IfIndexMap[intfRef]
if exists {
return ifIndex, exists
}
}
return -1, false
}
/* Api to update system cache on next send frame
*/
func (svr *LLDPServer) UpdateCache(sysInfo *config.SystemInfo) {
svr.SysInfo = sysInfo
debug.Logger.Debug("Updated system information:", *svr.SysInfo)
for _, ifIndex := range svr.lldpUpIntfStateSlice {
intf, exists := svr.lldpGblInfo[ifIndex]
if !exists {
continue
}
intf.TxInfo.SetCache(false)
}
}