2 *------------------------------------------------------------------
3 * Copyright (c) 2020 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
28 "github.com/pkg/profile"
32 func Disconnected(i *memif.Interface) error {
33 fmt.Println("Disconnected: ", i.GetName())
35 data, ok := i.GetPrivateData().(*interfaceData)
37 return fmt.Errorf("Invalid private data")
39 close(data.quitChan) // stop polling
41 data.wg.Wait() // wait until polling stops, then continue disconnect
46 func Connected(i *memif.Interface) error {
47 fmt.Println("Connected: ", i.GetName())
49 data, ok := i.GetPrivateData().(*interfaceData)
51 return fmt.Errorf("Invalid private data")
53 data.errChan = make(chan error, 1)
54 data.quitChan = make(chan struct{}, 1)
57 go func(errChan chan<- error, quitChan <-chan struct{}, wg *sync.WaitGroup) {
59 // allocate packet buffer
60 pkt := make([]byte, 2048)
62 rxq0, err := i.GetRxQueue(0)
68 txq0, err := i.GetTxQueue(0)
75 case <-quitChan: // channel closed
78 // read packet from shared memory
79 pktLen, err := rxq0.ReadPacket(pkt)
81 // write packet to shared memory
82 txq0.WritePacket(pkt[:pktLen])
83 } else if err != nil {
89 }(data.errChan, data.quitChan, &data.wg)
94 type interfaceData struct {
96 quitChan chan struct{}
100 func interractiveHelp() {
101 fmt.Println("help - print this help")
102 fmt.Println("start - start connecting loop")
103 fmt.Println("show - print interface details")
104 fmt.Println("exit - exit the application")
108 cpuprof := flag.String("cpuprof", "", "cpu profiling output file")
109 memprof := flag.String("memprof", "", "mem profiling output file")
110 role := flag.String("role", "slave", "interface role")
111 name := flag.String("name", "gomemif", "interface name")
112 socketName := flag.String("socket", "", "control socket filename")
117 defer profile.Start(profile.CPUProfile, profile.ProfilePath(*cpuprof)).Stop()
120 defer profile.Start(profile.MemProfile, profile.ProfilePath(*memprof)).Stop()
123 memifErrChan := make(chan error)
124 exitChan := make(chan struct{})
133 fmt.Println("Invalid role")
137 fmt.Println("GoMemif: Responder")
138 fmt.Println("-----------------------")
140 socket, err := memif.NewSocket("gomemif_example", *socketName)
142 fmt.Println("Failed to create socket: ", err)
146 data := interfaceData{}
147 args := &memif.Arguments{
149 ConnectedFunc: Connected,
150 DisconnectedFunc: Disconnected,
155 i, err := socket.NewInterface(args)
157 fmt.Println("Failed to create interface on socket %s: %s", socket.GetFilename(), err)
161 // slave attempts to connect to control socket
162 // to handle control communication call socket.StartPolling()
164 fmt.Println(args.Name, ": Connecting to control socket...")
165 for !i.IsConnecting() {
166 err = i.RequestConnection()
168 /* TODO: check for ECONNREFUSED errno
169 * if error is ECONNREFUSED it may simply mean that master
170 * interface is not up yet, use i.RequestConnection()
172 fmt.Println("Faild to connect: ", err)
178 go func(exitChan chan<- struct{}) {
179 reader := bufio.NewReader(os.Stdin)
181 fmt.Print("gomemif# ")
182 text, _ := reader.ReadString('\n')
183 // convert CRLF to LF
184 text = strings.Replace(text, "\n", "", -1)
189 // start polling for events on this socket
190 socket.StartPolling(memifErrChan)
192 fmt.Println("remote: ", i.GetRemoteName())
193 fmt.Println("peer: ", i.GetPeerName())
195 err = socket.StopPolling()
197 fmt.Println("Failed to stop polling: ", err)
202 fmt.Println("Unknown input")
211 case err, ok := <-memifErrChan:
215 case err, ok := <-data.errChan: