blob: bb02c17af3b38c73984bb07f833810ec078a8aa2 [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 (
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070020 "errors"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070021 "strconv"
22 "sync"
23 "time"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090024 "gerrit.opencord.org/voltha-bbsim/device"
25 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090026 "gerrit.opencord.org/voltha-bbsim/common"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090027 "gerrit.opencord.org/voltha-bbsim/setup"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028 "github.com/google/gopacket"
29 "github.com/google/gopacket/layers"
30 "github.com/google/gopacket/pcap"
31 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032)
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
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070053 Delay int
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 gRPCserver *grpc.Server
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090055 VethEnv []string
56 TestFlag bool
57 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090058 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090059 CtagMap map[string]uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090060}
61
62type Packet struct {
63 Info *Ioinfo
64 Pkt gopacket.Packet
65}
66
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070067func (s *Server) Initialize() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090068 s.VethEnv = []string{}
69 s.Endchan = make(chan int)
70 s.TestFlag = false
71 s.Processes = []string{}
72 s.Ioinfos = []*Ioinfo{}
73}
74
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070075func Create(oltid uint32, npon uint32, nonus uint32, aaawait int, dhcpwait int, ip string, delay int, g *grpc.Server, mode Mode, e chan int) *Server {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090076 s := new(Server)
77 s.Olt = device.CreateOlt(oltid, npon, 1)
78 nnni := s.Olt.NumNniIntf
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090079 logger.Info("OLT ID: %d was retrieved.\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090080 s.Onumap = make(map[uint32][]*device.Onu)
81 s.AAAWait = aaawait
82 s.DhcpWait = dhcpwait
83 s.DhcpServerIP = ip
84 s.gRPCserver = g
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070085 s.Delay = delay
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090086 s.Mode = mode
87 s.Endchan = e
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090088 s.VethEnv = []string{}
89 s.TestFlag = false
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090090 for intfid := nnni; intfid < npon+nnni; intfid++ {
91 s.Onumap[intfid] = device.CreateOnus(oltid, intfid, nonus, nnni)
92 }
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090093 s.EnableServer = new(openolt.Openolt_EnableIndicationServer)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090094
95 //TODO: To be fixed
96 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070097 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090098 oltid := s.Olt.ID
99 intfid := uint32(1)
100 sn := convB2S(device.CreateSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700101 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900102 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900103 return s
104}
105
106func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
107 // Activate OLT
108 olt := s.Olt
109 oltid := olt.ID
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900110 wg := &sync.WaitGroup{}
111
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900112 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 return err
114 }
115 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900116 *olt.InternalState = device.OLT_UP
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900117 logger.Info("OLT %s sent OltInd.\n", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900118
119 // OLT sends Interface Indication to Adapter
120 if err := sendIntfInd(stream, olt); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900121 logger.Error("Fail to sendIntfInd: %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900122 return err
123 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900124 logger.Info("OLT %s sent IntfInd.\n", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900125
126 // OLT sends Operation Indication to Adapter after activating each interface
127 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900128 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900129 if err := sendOperInd(stream, olt); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900130 logger.Error("Fail to sendOperInd: %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900131 return err
132 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900133 logger.Info("OLT %s sent OperInd.\n", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900134
135 // OLT sends ONU Discover Indication to Adapter after ONU discovery
136 for intfid, _ := range s.Onumap {
137 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
138 }
139
140 for intfid, _ := range s.Onumap {
141 sendOnuDiscInd(stream, s.Onumap[intfid])
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900142 logger.Info("OLT id:%d sent ONUDiscInd.\n", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900143 }
144
145 // OLT Sends OnuInd after waiting all of those ONUs up
146 for {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900147 if s.IsAllOnuActive(s.Onumap) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900148 break
149 }
150 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900151
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900152 for intfid, _ := range s.Onumap {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700153 sendOnuInd(stream, s.Onumap[intfid], s.Delay)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900154 logger.Info("OLT id:%d sent ONUInd.\n", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900155 }
156
157 if s.Mode == DEFAULT {
158 //EnableIndication's stream should be kept even after activateOLT() is finished.
159 //Otherwise, OpenOLT adapter sends EnableIndication again.
160 <-s.Endchan
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900161 logger.Debug("core server thread receives close ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900162 } else if s.Mode == AAA || s.Mode == BOTH {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900163 s.TestFlag = true
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900164 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900165 s.Ioinfos, s.VethEnv, err = createIoinfos(oltid, s.VethEnv, s.Onumap)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900166 logger.Debug("s.VethEnv:%v", s.VethEnv)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900167 if err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900168 return err
169 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900170
171 errchan := make(chan error)
172 go func() {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700173 <-errchan
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900174 close(s.Endchan)
175 }()
176
177 wg.Add(1)
178 go func() {
179 defer func() {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900180 logger.Debug("runPacketInDaemon Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900181 wg.Done()
182 }()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900183
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900184 err := s.runPacketInDaemon(stream)
185 if err != nil {
186 errchan <- err
187 return
188 }
189 }()
190
191 wg.Add(1)
192 go func() {
193 defer func() {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900194 logger.Debug("exeAAATest Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900195 wg.Done()
196 }()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900197
198 err = s.exeAAATest()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900199 if err != nil {
200 errchan <- err
201 return
202 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900203
204 if s.Mode == BOTH {
205 go func() {
206 defer func() {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900207 logger.Debug("exeDHCPTest Done")
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900208 }()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900209
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900210 err := s.exeDHCPTest()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900211 if err != nil {
212 errchan <- err
213 return
214 }
215 }()
216 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217 }()
218 wg.Wait()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900219 cleanUpVeths(s.VethEnv) // Grace teardown
220 pnames := s.Processes
221 killProcesses(pnames)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900222 logger.Debug("Grace shutdown down")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900223 }
224 return nil
225}
226
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700227func createIoinfos(oltid uint32, vethenv []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900228 ioinfos := []*Ioinfo{}
229 var err error
230 for intfid, _ := range onumap {
231 for i := 0; i < len(onumap[intfid]); i++ {
232 var handler *pcap.Handle
233 onuid := onumap[intfid][i].OnuID
234 uniup, unidw := makeUniName(oltid, intfid, onuid)
235 if handler, vethenv, err = setupVethHandler(uniup, unidw, vethenv); err != nil {
236 return ioinfos, vethenv, err
237 }
238 iinfo := Ioinfo{name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
239 ioinfos = append(ioinfos, &iinfo)
240 oinfo := Ioinfo{name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
241 ioinfos = append(ioinfos, &oinfo)
242 }
243 }
244
245 var handler *pcap.Handle
246 nniup, nnidw := makeNniName(oltid)
247 if handler, vethenv, err = setupVethHandler(nniup, nnidw, vethenv); err != nil {
248 return ioinfos, vethenv, err
249 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900250
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900251 iinfo := Ioinfo{name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
252 ioinfos = append(ioinfos, &iinfo)
253 oinfo := Ioinfo{name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
254 ioinfos = append(ioinfos, &oinfo)
255 return ioinfos, vethenv, nil
256}
257
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900258func (s *Server) runPacketInDaemon(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900259 logger.Debug("runPacketInDaemon Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900260 unichannel := make(chan Packet, 2048)
261 flag := false
262
263 for intfid, _ := range s.Onumap {
264 for _, onu := range s.Onumap[intfid] { //TODO: should be updated for multiple-Interface
265 onuid := onu.OnuID
266 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
267 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900268 logger.Error("Fail to identifyUniIoinfo (onuid: %d): %v\n", onuid, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900269 return err
270 }
271 uhandler := ioinfo.handler
272 defer uhandler.Close()
273 go RecvWorker(ioinfo, uhandler, unichannel)
274 }
275 }
276
277 ioinfo, err := s.identifyNniIoinfo("inside")
278 if err != nil {
279 return err
280 }
281 nhandler := ioinfo.handler
282 defer nhandler.Close()
283 nnichannel := make(chan Packet, 32)
284 go RecvWorker(ioinfo, nhandler, nnichannel)
285
286 data := &openolt.Indication_PktInd{}
287 for {
288 select {
289 case unipkt := <-unichannel:
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900290 logger.Debug("Received packet in grpc Server from UNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900291 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900292 logger.Info("WARNING: This packet does not come from UNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900293 continue
294 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900295
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900296 intfid := unipkt.Info.intfid
297 onuid := unipkt.Info.onuid
298 gemid, _ := getGemPortID(intfid, onuid)
299 pkt := unipkt.Pkt
300 layerEth := pkt.Layer(layers.LayerTypeEthernet)
301 le, _ := layerEth.(*layers.Ethernet)
302 ethtype := le.EthernetType
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900303
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900304 if ethtype == 0x888e {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900305 logger.Debug("Received upstream packet is EAPOL.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900306 //log.Println(unipkt.Pkt.Dump())
307 //log.Println(pkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900308 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900309 logger.Debug("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900310
311 //C-TAG
312 onu, _ := s.getOnuByID(onuid)
313 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700314 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900315 tagpkt, err := PushVLAN(pkt, uint16(ctag))
316 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900317 logger.Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900318 } else {
319 pkt = tagpkt
320 }
321 } else {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900322 logger.Error("Could not find the onuid %d (SN: %s) in CtagMap %v!\n", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900323 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900324 } else {
325 continue
326 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900327
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900328 logger.Debug("sendPktInd intfid:%d (onuid: %d) gemid:%d\n", intfid, onuid, gemid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900329 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
330 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900331 logger.Error("Fail to send PktInd indication. %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900332 return err
333 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900334
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900335 case nnipkt := <-nnichannel:
336 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900337 logger.Info("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900338 continue
339 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900340
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900341 logger.Debug("Received packet in grpc Server from NNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900342 intfid := nnipkt.Info.intfid
343 pkt := nnipkt.Pkt
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900344 logger.Info("sendPktInd intfid:%d\n", intfid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900345 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
346 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900347 logger.Error("Fail to send PktInd indication. %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900348 return err
349 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900350
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900351 case <-s.Endchan:
352 if flag == false {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900353 logger.Debug("PacketInDaemon thread receives close ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900354 close(unichannel)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900355 logger.Debug("Closed unichannel ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900356 close(nnichannel)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900357 logger.Debug("Closed nnichannel ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900358 flag = true
359 return nil
360 }
361 }
362 }
363 return nil
364}
365
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900366func (s *Server) exeAAATest() error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900367 logger.Info("exeAAATest stands by....")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900368 infos, err := s.getUniIoinfos("outside")
369 if err != nil {
370 return err
371 }
372
373 univeths := []string{}
374 for _, info := range infos {
375 univeths = append(univeths, info.name)
376 }
377
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700378 for {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900379 select {
380 case <-s.Endchan:
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900381 logger.Debug("exeAAATest thread receives close ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900382 return nil
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700383 case <-time.After(time.Second * time.Duration(s.AAAWait)):
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700384 err = setup.ActivateWPASups(univeths, s.Delay)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900385 if err != nil {
386 return err
387 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900388 logger.Info("WPA Supplicants are successfully activated ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900389 s.Processes = append(s.Processes, "wpa_supplicant")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900390 logger.Debug("Running Process:%v", s.Processes)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900391 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900392 }
393 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900394 return nil
395}
396
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900397func (s *Server) exeDHCPTest() error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900398 logger.Info("exeDHCPTest stands by....")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900399 info, err := s.identifyNniIoinfo("outside")
400
401 if err != nil {
402 return err
403 }
404
405 err = setup.ActivateDHCPServer(info.name, s.DhcpServerIP)
406 if err != nil {
407 return err
408 }
409 s.Processes = append(s.Processes, "dhcpd")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900410 logger.Debug("Running Process:%v", s.Processes)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900411
412 infos, err := s.getUniIoinfos("outside")
413 if err != nil {
414 return err
415 }
416
417 univeths := []string{}
418 for _, info := range infos {
419 univeths = append(univeths, info.name)
420 }
421
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700422 for {
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900423 select {
424 case <-s.Endchan:
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900425 logger.Debug("exeDHCPTest thread receives close ")
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900426 return nil
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700427 case <-time.After(time.Second * time.Duration(s.DhcpWait)):
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700428 err = setup.ActivateDHCPClients(univeths, s.Delay)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900429 if err != nil {
430 return err
431 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900432 logger.Info("DHCP clients are successfully activated ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900433 s.Processes = append(s.Processes, "dhclient")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900434 logger.Debug("Running Process:%v", s.Processes)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900435 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900436 }
437 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900438 return nil
439}
440
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900441func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
442 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
443 if layerEth != nil {
444 pkt, _ := layerEth.(*layers.Ethernet)
445 ethtype := pkt.EthernetType
446 if ethtype == 0x888e {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900447 logger.Debug("Received downstream packet is EAPOL.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900448 //log.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900449 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900450 logger.Debug("Received downstream packet is DHCP.")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900451 //log.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900452 rawpkt, _, _ = PopVLAN(rawpkt)
453 rawpkt, _, _ = PopVLAN(rawpkt)
454 } else {
455 return nil
456 }
457 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
458 if err != nil {
459 return err
460 }
461 handle := ioinfo.handler
462 SendUni(handle, rawpkt)
463 return nil
464 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900465 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900466 return nil
467}
468
469func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900470 poppkt, _, err := PopVLAN(rawpkt)
471 poppkt, _, err = PopVLAN(poppkt)
472 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900473 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900474 return err
475 }
476 ioinfo, err := s.identifyNniIoinfo("inside")
477 if err != nil {
478 return err
479 }
480 handle := ioinfo.handler
481 SendNni(handle, poppkt)
482 return nil
483}
484
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700485func (s *Server) IsAllOnuActive(regonus map[uint32][]*device.Onu) bool {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900486 for _, onus := range regonus {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900488 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900489 return false
490 }
491 }
492 }
493 return true
494}
495
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
497 idx := uint32(0)
498 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
499 //return uint32(1032 + 8 * (vid - 1)), nil
500}
501
502func (s *Server) getOnuBySN(sn *openolt.SerialNumber) (*device.Onu, error) {
503 for _, onus := range s.Onumap {
504 for _, onu := range onus {
505 if device.ValidateSN(*sn, *onu.SerialNumber) {
506 return onu, nil
507 }
508 }
509 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900510 err := errors.New("No mathced SN is found ")
511 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900512 return nil, err
513}
514
515func (s *Server) getOnuByID(onuid uint32) (*device.Onu, error) {
516 for _, onus := range s.Onumap {
517 for _, onu := range onus {
518 if onu.OnuID == onuid {
519 return onu, nil
520 }
521 }
522 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900523 err := errors.New("No matched OnuID is found ")
524 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900525 return nil, err
526}
527
528func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) {
529 upif = setup.UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
530 dwif = setup.UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
531 return
532}
533
534func makeNniName(oltid uint32) (upif string, dwif string) {
535 upif = setup.NNI_VETH_UP_PFX + strconv.Itoa(int(oltid))
536 dwif = setup.NNI_VETH_DW_PFX + strconv.Itoa(int(oltid))
537 return
538}
539
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900540func cleanUpVeths(vethenv []string) error {
541 if len(vethenv) > 0 {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900542 logger.Debug("cleanUpVeths called ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900543 setup.TearVethDown(vethenv)
544 }
545 return nil
546}
547
548func killProcesses(pnames []string) error {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700549 for _, pname := range pnames {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900550 setup.KillProcess(pname)
551 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900552 return nil
553}
554
555func setupVethHandler(inveth string, outveth string, vethenv []string) (*pcap.Handle, []string, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900556 logger.Debug("SetupVethHandler(%s, %s) called ", inveth, outveth)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557 err1 := setup.CreateVethPairs(inveth, outveth)
558 vethenv = append(vethenv, inveth)
559 if err1 != nil {
560 setup.RemoveVeths(vethenv)
561 return nil, vethenv, err1
562 }
563 handler, err2 := getVethHandler(inveth)
564 if err2 != nil {
565 setup.RemoveVeths(vethenv)
566 return nil, vethenv, err2
567 }
568 return handler, vethenv, nil
569}
570
571func getVethHandler(vethname string) (*pcap.Handle, error) {
572 var (
573 device string = vethname
574 snapshot_len int32 = 1518
575 promiscuous bool = false
576 err error
577 timeout time.Duration = pcap.BlockForever
578 )
579 handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
580 if err != nil {
581 return nil, err
582 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900583 logger.Debug("Server handle is created for %s\n", vethname)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900584 return handle, nil
585}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900586
587func convB2S(b []byte) string {
588 s := ""
589 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700590 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900591 }
592 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700593}