forked from FlexSNR/l3
/
dhcprelaydServer.go
344 lines (317 loc) · 10.4 KB
/
dhcprelaydServer.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
//
//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 / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
// Main entry point for DHCP_RELAY
package relayServer
import (
"asicd/asicdCommonDefs"
"asicdServices"
"dhcprelayd"
"encoding/json"
"errors"
_ "flag"
"git.apache.org/thrift.git/lib/go/thrift"
"io/ioutil"
"net"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"utils/ipcutils"
"utils/logging"
)
/******* Local API Calls. *******/
func NewDhcpRelayServer() *DhcpRelayServiceHandler {
return &DhcpRelayServiceHandler{}
}
/*
* DhcpRelaySignalHandler:
* This API will catch any os signals for DRA and if the signal is of
* SIGHUP type then it exit the process
*/
func DhcpRelaySignalHandler(sigChannel <-chan os.Signal) {
signal := <-sigChannel // receive from sigChannel and assign it to signal
switch signal {
case syscall.SIGHUP:
logger.Alert("DRA: Received SIGHUP SIGNAL")
os.Exit(0)
default:
logger.Info("DRA: Unhandled Signal : ", signal)
}
}
func DhcpRelayAgentOSSignalHandle() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
sigChannel := make(chan os.Signal, 1)
// SIGHUP is a signal sent to a process when its controlling terminal is
// closed and we need to handle that signal
signalList := []os.Signal{syscall.SIGHUP}
signal.Notify(sigChannel, signalList...)
// start a light weighted thread goroutine for signal handler
go DhcpRelaySignalHandler(sigChannel)
}
func DhcpRelayConnectToAsicd(client ClientJson) error {
var err error
asicdClient.Address = "localhost:" + strconv.Itoa(client.Port)
asicdClient.Transport, asicdClient.PtrProtocolFactory, err =
ipcutils.CreateIPCHandles(asicdClient.Address)
if asicdClient.Transport == nil ||
asicdClient.PtrProtocolFactory == nil ||
err != nil {
return err
}
asicdClient.ClientHdl =
asicdServices.NewASICDServicesClientFactory(
asicdClient.Transport,
asicdClient.PtrProtocolFactory)
asicdClient.IsConnected = true
return nil
}
/*
* ConnectToClients:
* This API will accept configFile location and from that it will
* connect to clients like asicd, etc..
*/
func DhcpRelayAgentConnectToClients(client ClientJson) error {
switch client.Name {
case "asicd":
return DhcpRelayConnectToAsicd(client)
default:
return errors.New(CLIENT_CONNECTION_NOT_REQUIRED)
}
}
/*
* InitDhcpRelayPktHandler:
* This API is used to initialize all the data structures varialbe that
* is needed by relay agent to perform its operation
*/
func InitDhcpRelayPortPktHandler() error {
// connecting to asicd
configFile := paramsDir + "/clients.json"
logger.Debug("DRA: configFile is ", configFile)
bytes, err := ioutil.ReadFile(configFile)
if err != nil {
logger.Err("DRA:Error while reading configuration file", configFile)
return err
}
var unConnectedClients []ClientJson
err = json.Unmarshal(bytes, &unConnectedClients)
if err != nil {
logger.Err("DRA: Error in Unmarshalling Json")
return err
}
logger.Debug("DRA: Connecting to Clients")
re_connect := 25
count := 0
// connect to client
for {
time.Sleep(time.Millisecond * 500)
for i := 0; i < len(unConnectedClients); i++ {
err := DhcpRelayAgentConnectToClients(
unConnectedClients[i])
if err == nil {
logger.Debug("DRA: Connected to " +
unConnectedClients[i].Name)
unConnectedClients = append(
unConnectedClients[:i],
unConnectedClients[i+1:]...)
} else if err.Error() == CLIENT_CONNECTION_NOT_REQUIRED {
unConnectedClients = append(
unConnectedClients[:i],
unConnectedClients[i+1:]...)
} else {
count++
if count == re_connect {
logger.Err("Connecting to", unConnectedClients[i].Name, "failed ", err)
count = 0
}
}
}
if len(unConnectedClients) == 0 {
break
}
}
// Initialize port parameters after client is connected
err = DhcpRelayInitPortParams()
if err != nil {
logger.Err("DRA: initializing port paramters failed")
return err
}
// OS signal channel listener thread
DhcpRelayAgentOSSignalHandle()
return nil
}
func DhcpRelayAgentInitIntfServerState(serverIp string, id int32) {
IntfId := int(id)
key := strconv.Itoa(IntfId) + "_" + serverIp
intfServerEntry := dhcprelayIntfServerStateMap[key]
intfServerEntry.IntfId = id
intfServerEntry.ServerIp = serverIp
intfServerEntry.Request = 0
intfServerEntry.Responses = 0
dhcprelayIntfServerStateMap[key] = intfServerEntry
dhcprelayIntfServerStateSlice = append(dhcprelayIntfServerStateSlice, key)
}
func DhcpRelayAgentInitIntfState(IntfId int32) {
intfEntry := dhcprelayIntfStateMap[IntfId]
intfEntry.IfIndex = IntfId
intfEntry.TotalDrops = 0
intfEntry.TotalDhcpClientRx = 0
intfEntry.TotalDhcpClientTx = 0
intfEntry.TotalDhcpServerRx = 0
intfEntry.TotalDhcpServerTx = 0
dhcprelayIntfStateMap[IntfId] = intfEntry
dhcprelayIntfStateSlice = append(dhcprelayIntfStateSlice, IntfId)
}
func DhcpRelayAgentInitGblHandling(ifNum int32, enable bool) {
//logger.Debug("DRA: Initializaing Global Info for " + strconv.Itoa(int(ifNum)))
// Created a global Entry for Interface
gblEntry := dhcprelayGblInfo[ifNum]
// Setting up default values for globalEntry
gblEntry.IpAddr = ""
gblEntry.Netmask = ""
gblEntry.IntfConfig.IfIndex = ifNum
gblEntry.IntfConfig.Enable = enable
dhcprelayGblInfo[ifNum] = gblEntry
}
func DhcpRelayAgentUpdateIntfServerIp(ifNum int32, serverIp string) {
logger.Debug("DRA: Updating Interface", ifNum, "with server ip", serverIp)
gblEntry, ok := dhcprelayGblInfo[ifNum]
if !ok {
logger.Err("No entry found in database")
return
}
gblEntry.IntfConfig.ServerIp = append(gblEntry.IntfConfig.ServerIp, serverIp)
dhcprelayGblInfo[ifNum] = gblEntry
}
func DhcpRelayAgentUpdateIntfIpAddr(ifIndexList []int32) {
logger.Debug("DRA: updating address for ", ifIndexList)
DhcpRelayAgentGetIpv4IntfList()
//@TODO: Once asicd supports Get then replace GetBulk with Get
for i := 0; i < len(ifIndexList); i++ {
obj, ok := dhcprelayIntfIpv4Map[ifIndexList[i]]
if !ok {
logger.Err("DRA: Get bulkd didn't return any info for", ifIndexList[i])
continue
}
logicalId := int32(asicdCommonDefs.GetIntfIdFromIfIndex(obj.IfIndex))
dhcprelayLogicalIntf2IfIndex[logicalId] = obj.IfIndex
gblEntry := dhcprelayGblInfo[ifIndexList[i]]
ip, ipnet, err := net.ParseCIDR(obj.IpAddr)
if err != nil {
logger.Err("DRA: Parsing ipadd and netmask failed:", err)
continue
}
gblEntry.IpAddr = ip.String()
gblEntry.Netmask = ipnet.IP.String()
dhcprelayGblInfo[ifIndexList[i]] = gblEntry
logger.Debug("DRA: Updated interface:", obj.IfIndex, " Ip address:", gblEntry.IpAddr, " netmask:", gblEntry.Netmask)
}
}
func DhcpRelayAgentInitVlanInfo(VlanName string, VlanId int32) {
logger.Debug("DRA: Vlan update message for ", VlanName, "vlan id is ", VlanId)
var linuxInterface *net.Interface
var err error
linuxInterface, err = net.InterfaceByName(VlanName)
if err != nil {
logger.Err("DRA: getting interface by name failed", err)
return
}
dhcprelayLogicalIntfId2LinuxIntId[linuxInterface.Index] = VlanId
}
func DhcpRelayGetClient(logger *logging.Writer, fileName string,
process string) (*DhcpRelayClientJson, error) {
var allClients []DhcpRelayClientJson
data, err := ioutil.ReadFile(fileName)
if err != nil {
logger.Err("Failed to open dhcpd config file", err, fileName)
return nil, err
}
json.Unmarshal(data, &allClients)
for _, client := range allClients {
if client.Name == process {
return &client, nil
}
}
return nil, errors.New("couldn't find dhcprelay port info")
}
func DhcpRelayGlobalInit(enable bool) {
if enable {
if dhcprelayRefCountMutex == nil {
dhcprelayRefCountMutex = &sync.RWMutex{}
dhcprelayEnabledIntfRefCount = 0
}
dhcprelayEnable = enable
if dhcprelayClientConn != nil {
logger.Debug("DRA: no need to create pcap as its already created")
return
} else {
DhcpRelayAgentCreateClientServerConn()
}
} else {
dhcprelayEnable = enable
}
}
func StartServer(log *logging.Writer, handler *DhcpRelayServiceHandler, params string) error {
logger = log
paramsDir = params
// Allocate Memory for Global DS
DhcpRelayAgentAllocateMemory()
// Initialize DB
err := DhcpRelayAgentInitDB()
if err != nil {
logger.Err("DRA: Init of DB failed")
} else {
DhcpRelayAgentReadDB()
}
logger.Debug("DRA: Continuining with port init")
// Initialize port information and packet handler for dhcp
go InitDhcpRelayPortPktHandler()
dhcprelayEnable = false
fileName := params + "/clients.json"
clientJson, err := DhcpRelayGetClient(logger, fileName, "dhcprelayd")
if err != nil || clientJson == nil {
return err
}
logger.Debug("Got Client info for", clientJson.Name, "port", clientJson.Port)
// create transport and protocol for server
transportFactory := thrift.NewTBufferedTransportFactory(8192)
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transport, err := thrift.NewTServerSocket("localhost:" +
strconv.Itoa(clientJson.Port))
if err != nil {
logger.Err("DRA: StartServer: NewTServerSocket failed with error:", err)
return err
}
processor := dhcprelayd.NewDHCPRELAYDServicesProcessor(handler)
server := thrift.NewTSimpleServer4(processor, transport,
transportFactory, protocolFactory)
err = server.Serve()
if err != nil {
logger.Err("DRA: Failed to start the listener, err:", err)
return err
}
return nil
}