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
|
/*
*------------------------------------------------------------------
* Copyright (c) 2020 Cisco and/or its affiliates.
* 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 main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/pkg/profile"
"memif"
)
func Disconnected(i *memif.Interface) error {
fmt.Println("Disconnected: ", i.GetName())
data, ok := i.GetPrivateData().(*interfaceData)
if !ok {
return fmt.Errorf("Invalid private data")
}
close(data.quitChan) // stop polling
close(data.errChan)
data.wg.Wait() // wait until polling stops, then continue disconnect
return nil
}
func Connected(i *memif.Interface) error {
fmt.Println("Connected: ", i.GetName())
data, ok := i.GetPrivateData().(*interfaceData)
if !ok {
return fmt.Errorf("Invalid private data")
}
data.errChan = make(chan error, 1)
data.quitChan = make(chan struct{}, 1)
data.wg.Add(1)
go func(errChan chan<- error, quitChan <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
// allocate packet buffer
pkt := make([]byte, 2048)
// get rx queue
rxq0, err := i.GetRxQueue(0)
if err != nil {
errChan <- err
return
}
// wait until both interfaces are connected
for !data.bri.IsConnected() {
time.Sleep(100 * time.Millisecond)
}
// get bridged interfaces tx queue
txq0, err := data.bri.GetTxQueue(0)
if err != nil {
errChan <- err
return
}
for {
select {
case <-quitChan: // channel closed
return
default:
// read packet from shared memory
pktLen, err := rxq0.ReadPacket(pkt)
if pktLen > 0 {
// FIXME: prevent packet write if interface is disconencted
// write packet to shared memory
txq0.WritePacket(pkt[:pktLen])
} else if err != nil {
errChan <- err
return
}
}
}
}(data.errChan, data.quitChan, &data.wg)
return nil
}
type interfaceData struct {
errChan chan error
quitChan chan struct{}
wg sync.WaitGroup
// bridged interface
bri *memif.Interface
}
func interractiveHelp() {
fmt.Println("help - print this help")
fmt.Println("start - start connecting loop")
fmt.Println("show - print interface details")
fmt.Println("exit - exit the application")
}
func newMemifInterface(socket *memif.Socket, id uint32, isMaster bool, name string) (*memif.Interface, *interfaceData, error) {
data := &interfaceData{}
args := &memif.Arguments{
Id: id,
IsMaster: isMaster,
ConnectedFunc: Connected,
DisconnectedFunc: Disconnected,
PrivateData: data,
Name: name,
}
i, err := socket.NewInterface(args)
if err != nil {
return nil, nil, fmt.Errorf("Failed to create interface on socket %s: %s", socket.GetFilename(), err)
}
// slave attempts to connect to control socket
// to handle control communication call socket.StartPolling()
if !i.IsMaster() {
fmt.Println(args.Name, ": Connecting to control socket...")
for !i.IsConnecting() {
err = i.RequestConnection()
if err != nil {
/* TODO: check for ECONNREFUSED errno
* if error is ECONNREFUSED it may simply mean that master
* interface is not up yet, use i.RequestConnection()
*/
return nil, nil, fmt.Errorf("Faild to connect: ", err)
}
}
}
return i, data, nil
}
func printMemifInterfaceDetails(i *memif.Interface) {
fmt.Println(i.GetName(), ":")
fmt.Println("\trole: ", memif.RoleToString(i.IsMaster()))
fmt.Println("\tid: ", i.GetId())
link := "down"
if i.IsConnected() {
link = "up"
}
fmt.Println("\tlink: ", link)
fmt.Println("\tremote: ", i.GetRemoteName())
fmt.Println("\tpeer: ", i.GetPeerName())
if i.IsConnected() {
mc := i.GetMemoryConfig()
fmt.Println("queue pairs: ", mc.NumQueuePairs)
fmt.Println("ring size: ", (1 << mc.Log2RingSize))
fmt.Println("buffer size: ", mc.PacketBufferSize)
}
}
func main() {
memifErrChan := make(chan error)
exitChan := make(chan struct{})
var i0, i1 *memif.Interface
var d0, d1 *interfaceData
cpuprof := flag.String("cpuprof", "", "cpu profiling output file")
memprof := flag.String("memprof", "", "mem profiling output file")
role := flag.String("role", "slave", "interface role")
name := flag.String("name", "gomemif", "interface name")
socketName := flag.String("socket", "", "control socket filename")
flag.Parse()
// profiling options
if *cpuprof != "" {
defer profile.Start(profile.CPUProfile, profile.ProfilePath(*cpuprof)).Stop()
}
if *memprof != "" {
defer profile.Start(profile.MemProfile, profile.ProfilePath(*memprof)).Stop()
}
// memif options
var isMaster bool
switch *role {
case "slave":
isMaster = false
case "master":
isMaster = true
default:
fmt.Println("Invalid role")
return
}
// create memif socket
socket, err := memif.NewSocket("gomemif_example", *socketName)
if err != nil {
fmt.Println("Failed to create socket: ", err)
return
}
i0, d0, err = newMemifInterface(socket, 0, isMaster, *name)
if err != nil {
fmt.Println(err)
goto exit
}
// TODO: update name
i1, d1, err = newMemifInterface(socket, 1, isMaster, *name)
if err != nil {
fmt.Println(err)
goto exit
}
// set up bridge
d0.bri = i1
d1.bri = i0
// user input goroutine
go func(exitChan chan<- struct{}) {
reader := bufio.NewReader(os.Stdin)
fmt.Println("GoMemif: Responder")
fmt.Println("-----------------------")
for {
fmt.Print("gomemif# ")
text, _ := reader.ReadString('\n')
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
switch text {
case "help":
interractiveHelp()
case "start":
// start polling for events on this socket
socket.StartPolling(memifErrChan)
case "show":
printMemifInterfaceDetails(i0)
printMemifInterfaceDetails(i1)
case "exit":
err = socket.StopPolling()
if err != nil {
fmt.Println("Failed to stop polling: ", err)
}
close(exitChan)
return
default:
fmt.Println("Unknown input")
}
}
}(exitChan)
// main loop
for {
select {
case <-exitChan:
goto exit
case err, ok := <-memifErrChan:
if ok {
fmt.Println(err)
}
case err, ok := <-d0.errChan:
if ok {
fmt.Println(err)
}
case err, ok := <-d1.errChan:
if ok {
fmt.Println(err)
}
default:
continue
}
}
exit:
socket.Delete()
close(memifErrChan)
}
|