blob: 8b52e491497385268d352619e108cf818a8a4621 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
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 */
16
17package core
18
19import (
20 "gerrit.opencord.org/voltha-bbsim/device"
21 "gerrit.opencord.org/voltha-bbsim/protos"
22 "gerrit.opencord.org/voltha-bbsim/setup"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090023 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
25 "github.com/google/gopacket/pcap"
26 "google.golang.org/grpc"
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090027 "errors"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028 "log"
29 "strconv"
30 "sync"
31 "time"
32)
33
34type Mode int
35
36const MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
37
38const (
39 DEFAULT Mode = iota
40 AAA
41 BOTH
42)
43
44type Server struct {
45 Olt *device.Olt
46 Onumap map[uint32][]*device.Onu
47 Ioinfos []*Ioinfo
48 Endchan chan int
49 Mode Mode
50 AAAWait int
51 DhcpWait int
52 DhcpServerIP string
53 gRPCserver *grpc.Server
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090054 VethEnv []string
55 TestFlag bool
56 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090057 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090058 CtagMap map[string]uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090059}
60
61type Packet struct {
62 Info *Ioinfo
63 Pkt gopacket.Packet
64}
65
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090066func (s *Server)Initialize(){
67 s.VethEnv = []string{}
68 s.Endchan = make(chan int)
69 s.TestFlag = false
70 s.Processes = []string{}
71 s.Ioinfos = []*Ioinfo{}
72}
73
74func Create(oltid uint32, npon uint32, nonus uint32, aaawait int, dhcpwait int, ip string, g *grpc.Server, mode Mode, e chan int) *Server {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090075 s := new(Server)
76 s.Olt = device.CreateOlt(oltid, npon, 1)
77 nnni := s.Olt.NumNniIntf
78 log.Printf("OLT ID: %d was retrieved.\n", s.Olt.ID)
79 s.Onumap = make(map[uint32][]*device.Onu)
80 s.AAAWait = aaawait
81 s.DhcpWait = dhcpwait
82 s.DhcpServerIP = ip
83 s.gRPCserver = g
84 s.Mode = mode
85 s.Endchan = e
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090086 s.VethEnv = []string{}
87 s.TestFlag = false
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090088 for intfid := nnni; intfid < npon+nnni; intfid++ {
89 s.Onumap[intfid] = device.CreateOnus(oltid, intfid, nonus, nnni)
90 }
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090091 s.EnableServer = new(openolt.Openolt_EnableIndicationServer)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090092
93 //TODO: To be fixed
94 s.CtagMap = make(map[string]uint32)
95 for i := 0; i < MAX_ONUS_PER_PON; i ++ {
96 oltid := s.Olt.ID
97 intfid := uint32(1)
98 sn := convB2S(device.CreateSN(oltid, intfid, uint32(i)))
99 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
100 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900101 return s
102}
103
104func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
105 // Activate OLT
106 olt := s.Olt
107 oltid := olt.ID
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900108 wg := &sync.WaitGroup{}
109
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900110 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900111 return err
112 }
113 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900114 *olt.InternalState = device.OLT_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 log.Printf("OLT %s sent OltInd.\n", olt.Name)
116
117 // OLT sends Interface Indication to Adapter
118 if err := sendIntfInd(stream, olt); err != nil {
119 log.Printf("[ERROR] Fail to sendIntfInd: %v\n", err)
120 return err
121 }
122 log.Printf("OLT %s sent IntfInd.\n", olt.Name)
123
124 // OLT sends Operation Indication to Adapter after activating each interface
125 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900126 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900127 if err := sendOperInd(stream, olt); err != nil {
128 log.Printf("[ERROR] Fail to sendOperInd: %v\n", err)
129 return err
130 }
131 log.Printf("OLT %s sent OperInd.\n", olt.Name)
132
133 // OLT sends ONU Discover Indication to Adapter after ONU discovery
134 for intfid, _ := range s.Onumap {
135 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
136 }
137
138 for intfid, _ := range s.Onumap {
139 sendOnuDiscInd(stream, s.Onumap[intfid])
140 log.Printf("OLT id:%d sent ONUDiscInd.\n", olt.ID)
141 }
142
143 // OLT Sends OnuInd after waiting all of those ONUs up
144 for {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900145 if s.IsAllOnuActive(s.Onumap) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900146 break
147 }
148 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900149
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900150 for intfid, _ := range s.Onumap {
151 sendOnuInd(stream, s.Onumap[intfid])
152 log.Printf("OLT id:%d sent ONUInd.\n", olt.ID)
153 }
154
155 if s.Mode == DEFAULT {
156 //EnableIndication's stream should be kept even after activateOLT() is finished.
157 //Otherwise, OpenOLT adapter sends EnableIndication again.
158 <-s.Endchan
159 log.Println("core server thread receives close !")
160 } else if s.Mode == AAA || s.Mode == BOTH {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900161 s.TestFlag = true
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900162 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900163 s.Ioinfos, s.VethEnv, err = createIoinfos(oltid, s.VethEnv, s.Onumap)
164 log.Println("s.VethEnv", s.VethEnv)
165 if err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900166 return err
167 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900168
169 errchan := make(chan error)
170 go func() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900171 <- errchan
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900172 close(s.Endchan)
173 }()
174
175 wg.Add(1)
176 go func() {
177 defer func() {
178 log.Println("runPacketInDaemon Done")
179 wg.Done()
180 }()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900181
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900182 err := s.runPacketInDaemon(stream)
183 if err != nil {
184 errchan <- err
185 return
186 }
187 }()
188
189 wg.Add(1)
190 go func() {
191 defer func() {
192 log.Println("exeAAATest Done")
193 wg.Done()
194 }()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900195
196 err = s.exeAAATest()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900197 if err != nil {
198 errchan <- err
199 return
200 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900201
202 if s.Mode == BOTH {
203 go func() {
204 defer func() {
205 log.Println("exeDHCPTest Done")
206 }()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900207
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900208 err := s.exeDHCPTest()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900209 if err != nil {
210 errchan <- err
211 return
212 }
213 }()
214 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900215 }()
216 wg.Wait()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900217 cleanUpVeths(s.VethEnv) // Grace teardown
218 pnames := s.Processes
219 killProcesses(pnames)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900220 log.Println("Grace shutdown down")
221 }
222 return nil
223}
224
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900225func createIoinfos(oltid uint32, vethenv []string, onumap map[uint32][]*device.Onu)([]*Ioinfo, []string, error){
226 ioinfos := []*Ioinfo{}
227 var err error
228 for intfid, _ := range onumap {
229 for i := 0; i < len(onumap[intfid]); i++ {
230 var handler *pcap.Handle
231 onuid := onumap[intfid][i].OnuID
232 uniup, unidw := makeUniName(oltid, intfid, onuid)
233 if handler, vethenv, err = setupVethHandler(uniup, unidw, vethenv); err != nil {
234 return ioinfos, vethenv, err
235 }
236 iinfo := Ioinfo{name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
237 ioinfos = append(ioinfos, &iinfo)
238 oinfo := Ioinfo{name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
239 ioinfos = append(ioinfos, &oinfo)
240 }
241 }
242
243 var handler *pcap.Handle
244 nniup, nnidw := makeNniName(oltid)
245 if handler, vethenv, err = setupVethHandler(nniup, nnidw, vethenv); err != nil {
246 return ioinfos, vethenv, err
247 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900248
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900249 iinfo := Ioinfo{name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
250 ioinfos = append(ioinfos, &iinfo)
251 oinfo := Ioinfo{name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
252 ioinfos = append(ioinfos, &oinfo)
253 return ioinfos, vethenv, nil
254}
255
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900256func (s *Server) runPacketInDaemon(stream openolt.Openolt_EnableIndicationServer) error {
257 log.Println("runPacketInDaemon Start")
258 unichannel := make(chan Packet, 2048)
259 flag := false
260
261 for intfid, _ := range s.Onumap {
262 for _, onu := range s.Onumap[intfid] { //TODO: should be updated for multiple-Interface
263 onuid := onu.OnuID
264 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
265 if err != nil {
266 log.Printf("[ERROR] Fail to identifyUniIoinfo (onuid: %d): %v\n", onuid, err)
267 return err
268 }
269 uhandler := ioinfo.handler
270 defer uhandler.Close()
271 go RecvWorker(ioinfo, uhandler, unichannel)
272 }
273 }
274
275 ioinfo, err := s.identifyNniIoinfo("inside")
276 if err != nil {
277 return err
278 }
279 nhandler := ioinfo.handler
280 defer nhandler.Close()
281 nnichannel := make(chan Packet, 32)
282 go RecvWorker(ioinfo, nhandler, nnichannel)
283
284 data := &openolt.Indication_PktInd{}
285 for {
286 select {
287 case unipkt := <-unichannel:
288 log.Println("Received packet in grpc Server from UNI.")
289 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
290 log.Println("[WARNING] This packet does not come from UNI !")
291 continue
292 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900293
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900294 intfid := unipkt.Info.intfid
295 onuid := unipkt.Info.onuid
296 gemid, _ := getGemPortID(intfid, onuid)
297 pkt := unipkt.Pkt
298 layerEth := pkt.Layer(layers.LayerTypeEthernet)
299 le, _ := layerEth.(*layers.Ethernet)
300 ethtype := le.EthernetType
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900301
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900302 if ethtype == 0x888e {
303 log.Printf("Received upstream packet is EAPOL.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900304 //log.Println(unipkt.Pkt.Dump())
305 //log.Println(pkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900306 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
307 log.Printf("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900308
309 //C-TAG
310 onu, _ := s.getOnuByID(onuid)
311 sn := convB2S(onu.SerialNumber.VendorSpecific)
312 if ctag, ok := s.CtagMap[sn]; ok == true{
313 tagpkt, err := PushVLAN(pkt, uint16(ctag))
314 if err != nil {
315 log.Println("Error happend in C-tag tagging")
316 } else {
317 pkt = tagpkt
318 }
319 } else {
320 log.Printf("Could not find the onuid %d (SN: %s) in CtagMap %v!\n", onuid, sn, s.CtagMap)
321 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900322 } else {
323 continue
324 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900325
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900326 log.Printf("sendPktInd intfid:%d (onuid: %d) gemid:%d\n", intfid, onuid, gemid)
327 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
328 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
329 log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err)
330 return err
331 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900333 case nnipkt := <-nnichannel:
334 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
335 log.Println("[WARNING] This packet does not come from NNI !")
336 continue
337 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900338
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900339 log.Println("Received packet in grpc Server from NNI.")
340 intfid := nnipkt.Info.intfid
341 pkt := nnipkt.Pkt
342 log.Printf("sendPktInd intfid:%d\n", intfid)
343 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
344 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
345 log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err)
346 return err
347 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900348
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900349 case <-s.Endchan:
350 if flag == false {
351 log.Println("PacketInDaemon thread receives close !")
352 close(unichannel)
353 log.Println("Closed unichannel !")
354 close(nnichannel)
355 log.Println("Closed nnichannel !")
356 flag = true
357 return nil
358 }
359 }
360 }
361 return nil
362}
363
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900364func (s *Server) exeAAATest() error {
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900365 log.Println("exeAAATest starts to sleep....")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900366 infos, err := s.getUniIoinfos("outside")
367 if err != nil {
368 return err
369 }
370
371 univeths := []string{}
372 for _, info := range infos {
373 univeths = append(univeths, info.name)
374 }
375
376 for {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900377 select {
378 case <-s.Endchan:
379 log.Println("exeAAATest thread receives close !")
380 return nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900381 case <- time.After(time.Second * time.Duration(s.AAAWait)):
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900382 log.Println("exeAAATest Start")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900383 err = setup.ActivateWPASups(univeths)
384 if err != nil {
385 return err
386 }
387 s.Processes = append(s.Processes, "wpa_supplicant")
388 log.Println("s.Processes:", s.Processes)
389 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900390 }
391 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900392 return nil
393}
394
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900395func (s *Server) exeDHCPTest() error {
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900396 log.Println("exeDHCPTest starts to sleep....")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900397 info, err := s.identifyNniIoinfo("outside")
398
399 if err != nil {
400 return err
401 }
402
403 err = setup.ActivateDHCPServer(info.name, s.DhcpServerIP)
404 if err != nil {
405 return err
406 }
407 s.Processes = append(s.Processes, "dhcpd")
408
409 infos, err := s.getUniIoinfos("outside")
410 if err != nil {
411 return err
412 }
413
414 univeths := []string{}
415 for _, info := range infos {
416 univeths = append(univeths, info.name)
417 }
418
419 for {
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900420 select {
421 case <-s.Endchan:
422 log.Println("exeDHCPTest thread receives close !")
423 return nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900424 case <- time.After(time.Second * time.Duration(s.DhcpWait)):
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900425 log.Println("exeDHCPTest Start")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900426 err = setup.ActivateDHCPClients(univeths)
427 if err != nil {
428 return err
429 }
430 s.Processes = append(s.Processes, "dhclient")
431 log.Println("s.Processes:", s.Processes)
432 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900433 }
434 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900435 return nil
436}
437
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900438func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
439 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
440 if layerEth != nil {
441 pkt, _ := layerEth.(*layers.Ethernet)
442 ethtype := pkt.EthernetType
443 if ethtype == 0x888e {
444 log.Printf("Received downstream packet is EAPOL.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900445 //log.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900446 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
447 log.Printf("Received downstream packet is DHCP.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900448 //log.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900449 rawpkt, _, _ = PopVLAN(rawpkt)
450 rawpkt, _, _ = PopVLAN(rawpkt)
451 } else {
452 return nil
453 }
454 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
455 if err != nil {
456 return err
457 }
458 handle := ioinfo.handler
459 SendUni(handle, rawpkt)
460 return nil
461 }
462 log.Printf("[WARNING] Received packet is not supported")
463 return nil
464}
465
466func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900467 poppkt, _, err := PopVLAN(rawpkt)
468 poppkt, _, err = PopVLAN(poppkt)
469 if err != nil {
470 log.Println(err)
471 return err
472 }
473 ioinfo, err := s.identifyNniIoinfo("inside")
474 if err != nil {
475 return err
476 }
477 handle := ioinfo.handler
478 SendNni(handle, poppkt)
479 return nil
480}
481
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900482func (s *Server)IsAllOnuActive(regonus map[uint32][]*device.Onu) bool {
483 for _, onus := range regonus {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900484 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900485 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900486 return false
487 }
488 }
489 }
490 return true
491}
492
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900493func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
494 idx := uint32(0)
495 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
496 //return uint32(1032 + 8 * (vid - 1)), nil
497}
498
499func (s *Server) getOnuBySN(sn *openolt.SerialNumber) (*device.Onu, error) {
500 for _, onus := range s.Onumap {
501 for _, onu := range onus {
502 if device.ValidateSN(*sn, *onu.SerialNumber) {
503 return onu, nil
504 }
505 }
506 }
507 err := errors.New("No mathced SN is found !")
508 log.Println(err)
509 return nil, err
510}
511
512func (s *Server) getOnuByID(onuid uint32) (*device.Onu, error) {
513 for _, onus := range s.Onumap {
514 for _, onu := range onus {
515 if onu.OnuID == onuid {
516 return onu, nil
517 }
518 }
519 }
520 err := errors.New("No matched OnuID is found !")
521 log.Println(err)
522 return nil, err
523}
524
525func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) {
526 upif = setup.UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
527 dwif = setup.UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
528 return
529}
530
531func makeNniName(oltid uint32) (upif string, dwif string) {
532 upif = setup.NNI_VETH_UP_PFX + strconv.Itoa(int(oltid))
533 dwif = setup.NNI_VETH_DW_PFX + strconv.Itoa(int(oltid))
534 return
535}
536
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900537func cleanUpVeths(vethenv []string) error {
538 if len(vethenv) > 0 {
539 log.Println("cleanUp veths !")
540 setup.TearVethDown(vethenv)
541 }
542 return nil
543}
544
545func killProcesses(pnames []string) error {
546 for _, pname := range pnames {
547 setup.KillProcess(pname)
548 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900549 return nil
550}
551
552func setupVethHandler(inveth string, outveth string, vethenv []string) (*pcap.Handle, []string, error) {
553 log.Printf("setupVethHandler: %s and %s\n", inveth, outveth)
554 err1 := setup.CreateVethPairs(inveth, outveth)
555 vethenv = append(vethenv, inveth)
556 if err1 != nil {
557 setup.RemoveVeths(vethenv)
558 return nil, vethenv, err1
559 }
560 handler, err2 := getVethHandler(inveth)
561 if err2 != nil {
562 setup.RemoveVeths(vethenv)
563 return nil, vethenv, err2
564 }
565 return handler, vethenv, nil
566}
567
568func getVethHandler(vethname string) (*pcap.Handle, error) {
569 var (
570 device string = vethname
571 snapshot_len int32 = 1518
572 promiscuous bool = false
573 err error
574 timeout time.Duration = pcap.BlockForever
575 )
576 handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
577 if err != nil {
578 return nil, err
579 }
580 log.Printf("Server handle created for %s\n", vethname)
581 return handle, nil
582}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900583
584func convB2S(b []byte) string {
585 s := ""
586 for _, i := range b {
587 s = s + strconv. FormatInt(int64(i/16), 16) + strconv. FormatInt(int64(i%16), 16)
588 }
589 return s
590}