-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (102 loc) · 3.7 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
package main
import (
"os"
_ "shila/config"
"shila/core/connection"
"shila/core/router"
"shila/core/shila"
"shila/kernelSide"
"shila/log"
"shila/networkSide"
"shila/shutdown"
"shila/workingSide"
)
const (
SuccessCode = 0
ErrorCode = 1
)
func main() {
os.Exit(realMain())
}
func realMain() int {
defer log.Info.Println("Shutdown complete.")
// TODO: Return if not run as root.
// TODO: Encourage user to run separate namespace for ingress and egress.
var err error
log.Init() // Initialize logging functionality
shutdown.Init() // Initialize termination functionality
log.Verbose.Println("Setup started...")
// This channel is used by the kernel and the network side to announce new packet channels, either to
// the ingress or the egress working side. (traffic channel publications)
trafficChannelPubs := shila.PacketChannelPubChannels{
Ingress: make(shila.PacketChannelPubChannel),
Egress: make(shila.PacketChannelPubChannel),
}
endpointIssues := shila.EndpointIssuePubChannels{
Ingress: make(shila.EndpointIssuePubChannel),
Egress: make(shila.EndpointIssuePubChannel),
}
// Create and setup the kernelSide side
kernelSide := kernelSide.New(trafficChannelPubs)
if err = kernelSide.Setup(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to setup kernel side.").Error())
return ErrorCode
}
log.Verbose.Println("Kernel side setup successfully.")
defer kernelSide.CleanUp()
// Create and setup the network side
networkSide := networkSide.New(trafficChannelPubs, endpointIssues)
if err = networkSide.Setup(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to setup network side.").Error())
return ErrorCode
}
log.Verbose.Println("Network side setup successfully.")
defer networkSide.CleanUp()
// Setup the ingress working side
// The ingress working side handles all traffic which was initiated by the network side.
workingSideIngress := workingSide.New(connection.NewMapping(kernelSide, networkSide, router.New()),
trafficChannelPubs.Ingress, endpointIssues.Ingress, workingSide.Ingress)
if err := workingSideIngress.Setup(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to setup ingress working side.").Error())
return ErrorCode
}
defer workingSideIngress.CleanUp()
// Setup the egress working side
// The egress working side handles all the traffic which was initiated by the client side.
workingSideEgress := workingSide.New(connection.NewMapping(kernelSide, networkSide, router.New()),
trafficChannelPubs.Egress, endpointIssues.Egress, workingSide.Egress)
if err := workingSideEgress.Setup(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to setup egress working side.").Error())
return ErrorCode
}
defer workingSideEgress.CleanUp()
log.Verbose.Println("Working sides setup successfully.")
log.Verbose.Println("Setup done, starting machinery..")
if err = workingSideIngress.Start(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to start ingress working side.").Error())
return ErrorCode
}
if err = workingSideEgress.Start(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to start egress working side.").Error())
return ErrorCode
}
if err = networkSide.Start(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to start network side.").Error())
return ErrorCode
}
if err = kernelSide.Start(); err != nil {
log.Error.Print(shila.PrependError(err, "Unable to start the kernel side.").Error())
return ErrorCode
}
log.Info.Println("Shila up and running.")
returnCode := waitForTeardown()
return returnCode
}
func waitForTeardown() int {
select {
case <-shutdown.OrderlyChan():
return SuccessCode
case <-shutdown.FatalChan():
return ErrorCode
}
}