forked from named-data/YaNFD
/
main.go
260 lines (227 loc) · 8.02 KB
/
main.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
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2021 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/eric135/YaNFD/core"
"github.com/eric135/YaNFD/dispatch"
"github.com/eric135/YaNFD/face"
"github.com/eric135/YaNFD/fw"
"github.com/eric135/YaNFD/mgmt"
"github.com/eric135/YaNFD/ndn"
"github.com/eric135/YaNFD/table"
)
// Version of YaNFD.
var Version string
func main() {
// Provide metadata to other threads.
core.Version = Version
core.StartTimestamp = time.Now()
// Parse command line options
var shouldPrintVersion bool
flag.BoolVar(&shouldPrintVersion, "version", false, "Print version and exit")
var configFileName string
flag.StringVar(&configFileName, "config", "/usr/local/etc/ndn/yanfd.toml", "Configuration file location")
var disableEthernet bool
flag.BoolVar(&disableEthernet, "disable-ethernet", false, "Disable Ethernet transports")
var disableUnix bool
flag.BoolVar(&disableUnix, "disable-unix", false, "Disable Unix stream transports")
var cpuProfile string
flag.StringVar(&cpuProfile, "cpu-profile", "", "Enable CPU profiling (output to specified file)")
var memProfile string
flag.StringVar(&memProfile, "mem-profile", "", "Enable memory profiling (output to specified file)")
var blockProfile string
flag.StringVar(&blockProfile, "block-profile", "", "Enable block profiling (output to specified file")
var memoryBallastSize int
flag.IntVar(&memoryBallastSize, "memory-ballast", 0, "Enable memory ballast of specified size (in GB) to avoid frequent garbage collection")
flag.Parse()
if shouldPrintVersion {
fmt.Println("YaNFD: Yet another NDN Forwarding Daemon")
fmt.Println("Version " + core.Version)
fmt.Println("Copyright (C) 2020-2021 Eric Newberry")
fmt.Println("Released under the terms of the MIT License")
return
}
// Allocate memory ballast (if enabled)
if memoryBallastSize > 0 {
_ = make([]byte, memoryBallastSize<<30)
}
// Initialize config file
core.LoadConfig(configFileName)
core.InitializeLogger()
face.Configure()
fw.Configure()
table.Configure()
if cpuProfile != "" {
cpuProfileFile, err := os.Create(cpuProfile)
if err != nil {
core.LogFatal("Main", "Unable to open output file for CPU profile: ", err)
}
core.LogInfo("Main", "Profiling CPU - outputting to ", cpuProfile)
pprof.StartCPUProfile(cpuProfileFile)
defer cpuProfileFile.Close()
defer pprof.StopCPUProfile()
}
if memProfile != "" {
memProfileFile, err := os.Create(memProfile)
if err != nil {
core.LogFatal("Main", "Unable to open output file for memory profile: ", err)
}
core.LogInfo("Main", "Profiling memory - outputting to ", memProfile)
runtime.GC()
if err := pprof.WriteHeapProfile(memProfileFile); err != nil {
core.LogFatal("Main", "Unable to write memory profile: ", err)
}
defer memProfileFile.Close()
}
if blockProfile != "" {
core.LogInfo("Main", "Profiling blocking operations - outputting to ", blockProfile)
runtime.SetBlockProfileRate(1)
blockProfiler := pprof.Lookup("block")
// Output at end of runtime
defer func() {
blockProfileFile, err := os.Create(blockProfile)
if err != nil {
core.LogFatal("Main", "Unable to open output file for block profile: ", err)
}
if err := blockProfiler.WriteTo(blockProfileFile, 0); err != nil {
core.LogFatal("Main", "Unable to write block profile: ", err)
}
blockProfileFile.Close()
}()
}
core.LogInfo("Main", "Starting YaNFD")
// Load strategies
//core.LogInfo("Main", "Loading strategies")
//fw.LoadStrategies()
// Create null face
nullFace := face.MakeNullLinkService(face.MakeNullTransport())
face.FaceTable.Add(nullFace)
go nullFace.Run()
// Start management thread
management := mgmt.MakeMgmtThread()
go management.Run()
// Create forwarding threads
if fw.NumFwThreads < 1 || fw.NumFwThreads > fw.MaxFwThreads {
core.LogFatal("Main", "Number of forwarding threads must be in range [1, ", fw.MaxFwThreads, "]")
}
fw.Threads = make(map[int]*fw.Thread)
var fwForDispatch []dispatch.FWThread
for i := 0; i < fw.NumFwThreads; i++ {
newThread := fw.NewThread(i)
fw.Threads[i] = newThread
fwForDispatch = append(fwForDispatch, newThread)
go fw.Threads[i].Run()
}
dispatch.InitializeFWThreads(fwForDispatch)
// Perform setup operations for each network interface
ifaces, err := net.Interfaces()
multicastEthURI := ndn.DecodeURIString("ether://[" + face.EthernetMulticastAddress + "]")
if err != nil {
core.LogFatal("Main", "Unable to access network interfaces: ", err)
os.Exit(2)
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
core.LogInfo("Main", "Skipping interface ", iface.Name, " because not up")
continue
}
if !disableEthernet && iface.Flags&net.FlagMulticast != 0 {
// Create multicast Ethernet face for interface
multicastEthTransport, err := face.MakeMulticastEthernetTransport(multicastEthURI, ndn.MakeDevFaceURI(iface.Name))
if err != nil {
core.LogFatal("Main", "Unable to create MulticastEthernetTransport for ", iface.Name, ": ", err)
os.Exit(2)
}
multicastEthFace := face.MakeNDNLPLinkService(multicastEthTransport, face.MakeNDNLPLinkServiceOptions())
face.FaceTable.Add(multicastEthFace)
go multicastEthFace.Run()
core.LogInfo("Main", "Created multicast Ethernet face for ", iface.Name)
// Create Ethernet listener for interface
// TODO
}
// Create UDP listener and multicast UDP interface for every address on interface
addrs, err := iface.Addrs()
if err != nil {
core.LogFatal("Main", "Unable to access addresses on network interface ", iface.Name, ": ", err)
}
for _, addr := range addrs {
ipAddr := addr.(*net.IPNet)
ipVersion := 4
path := ipAddr.IP.String()
if ipAddr.IP.To4() == nil {
ipVersion = 6
path += "%" + iface.Name
}
if !addr.(*net.IPNet).IP.IsLoopback() {
multicastUDPTransport, err := face.MakeMulticastUDPTransport(ndn.MakeUDPFaceURI(ipVersion, path, face.UDPMulticastPort))
if err != nil {
core.LogFatal("Main", "Unable to create MulticastUDPTransport for ", path, " on ", iface.Name, ": ", err)
os.Exit(2)
}
multicastUDPFace := face.MakeNDNLPLinkService(multicastUDPTransport, face.MakeNDNLPLinkServiceOptions())
face.FaceTable.Add(multicastUDPFace)
go multicastUDPFace.Run()
core.LogInfo("Main", "Created multicast UDP face for ", path, " on ", iface.Name)
}
udpListener, err := face.MakeUDPListener(ndn.MakeUDPFaceURI(ipVersion, path, 6363))
if err != nil {
core.LogFatal("Main", "Unable to create UDP listener for ", path, " on ", iface.Name, ": ", err)
os.Exit(2)
}
go udpListener.Run()
core.LogInfo("Main", "Created UDP listener for ", path, " on ", iface.Name)
}
}
var unixListener *face.UnixStreamListener
if !disableUnix {
// Set up Unix stream listener
unixListener, err = face.MakeUnixStreamListener(ndn.MakeUnixFaceURI(face.UnixSocketPath))
if err != nil {
core.LogFatal("Main", "Unable to create Unix stream listener at ", face.UnixSocketPath, ": ", err)
os.Exit(2)
}
go unixListener.Run()
core.LogInfo("Main", "Created Unix stream listener for ", face.UnixSocketPath)
}
// Set up signal handler channel and wait for interrupt
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
receivedSig := <-sigChannel
core.LogInfo("Main", "Received signal ", receivedSig, " - exiting")
core.ShouldQuit = true
// Wait for unix socket listener to quit
if !disableUnix {
unixListener.Close()
<-unixListener.HasQuit
}
// Tell all faces to quit
for _, face := range face.FaceTable.Faces {
face.Close()
}
// Wait for all faces to quit
for _, face := range face.FaceTable.Faces {
core.LogTrace("Main", "Waiting for face ", face, " to quit")
<-face.GetHasQuit()
}
// Tell all forwarding threads to quit
for _, fw := range fw.Threads {
fw.TellToQuit()
}
// Wait for all forwarding threads to have quit
for _, fw := range fw.Threads {
<-fw.HasQuit
}
}