blob: ce55024630586a92be615c997eda0d4fcbffce31 [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 (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090020 "context"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070021 "errors"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 "strconv"
23 "sync"
24
Shad Ansarib744bf22019-01-17 11:36:46 -080025 omci "github.com/opencord/omci-sim"
26
27 "reflect"
28
Matteo Scandolo88e91892018-11-06 16:29:19 -080029 "gerrit.opencord.org/voltha-bbsim/common/logger"
30 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "gerrit.opencord.org/voltha-bbsim/device"
32 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 "github.com/google/gopacket"
34 "github.com/google/gopacket/layers"
35 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080038 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041const (
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090042 NNI_VETH_NORTH_PFX = "nni_north"
43 NNI_VETH_SOUTH_PFX = "nni_south"
44 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090045)
46
47type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090048 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 Olt *device.Olt
50 Onumap map[uint32][]*device.Onu
51 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090053 gRPCAddress string
54 gRPCPort uint32
55 Vethnames []string
56 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090057 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 NISHIMOTO9708e042018-10-27 09:24:44 +090060 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090061 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080062 omciIn chan openolt.OmciIndication
63 omciOut chan openolt.OmciMsg
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090064 eapolIn chan *byteMsg
65 eapolOut chan *byteMsg
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090066 dhcpIn chan *byteMsg
67 dhcpOut chan *byteMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068}
69
70type Packet struct {
71 Info *Ioinfo
72 Pkt gopacket.Packet
73}
74
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090075type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090076 IntfId uint32
77 OnuId uint32
78 Byte []byte
79}
80
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090081type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080082 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090083 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080084 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090085}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090086
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090087func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088 // TODO: make it decent
89 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080090 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090091 nonus := opt.nonus
92 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080093 Olt: device.NewOlt(oltid, npon, 1),
94 Onumap: make(map[uint32][]*device.Onu),
95 Ioinfos: []*Ioinfo{},
96 gRPCAddress: opt.address,
97 gRPCPort: opt.port,
98 Vethnames: []string{},
99 IndInterval: opt.intvl,
100 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -0800101 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -0800102 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900103 omciIn: make(chan openolt.OmciIndication, 1024),
104 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900105 eapolIn: make(chan *byteMsg, 1024),
106 eapolOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900107 dhcpIn: make(chan *byteMsg, 1024),
108 dhcpOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900109 }
110
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900111 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800112 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900116
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900117 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900118 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700119 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900120 oltid := s.Olt.ID
121 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700123 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900124 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900125 return &s
126}
127
128//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800129func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900130 s.wg = &sync.WaitGroup{}
131 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800132 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800133 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 logger.Debug("Start() Done")
135 }()
136 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
137 listener, gserver, err := NewGrpcServer(addressport)
138 if err != nil {
139 logger.Error("Failed to create gRPC server", err)
140 return err
141 }
142 s.gRPCserver = gserver
143 openolt.RegisterOpenoltServer(gserver, s)
144 if err := gserver.Serve(listener); err != nil {
145 logger.Error("Failed to run gRPC server", err)
146 return err
147 }
148 s.wg.Wait()
149 return nil
150}
151
152//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800153func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900154 logger.Debug("Stop() Start")
155 defer logger.Debug("Stop() Done")
156 if s.gRPCserver != nil {
157 s.gRPCserver.Stop()
158 logger.Debug("gRPCserver.Stop()")
159 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900160 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900161 return
162}
163
164// Blocking
165func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900166 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800167 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900168 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900169 for intfid, _ := range s.Onumap {
170 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900171 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900172 }
173 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900174 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900175 logger.Debug("Enable() Done")
176 }()
177 logger.Debug("Enable() Start")
178 s.EnableServer = sv
179 if err := s.activateOLT(*sv); err != nil {
180 return err
181 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900182 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900183
184 coreCtx := context.Background()
185 coreCtx, corecancel := context.WithCancel(coreCtx)
186 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900187 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188 return err
189 }
190 return nil
191}
192
193//Non-Blocking
194func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800195 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900196 logger.Debug("Disable() Done")
197 }()
198 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900199 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200}
201
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900202func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900203 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900204 current := dev.GetIntState()
205 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800206 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
207 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900208 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800209 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900210 logger.Debug("ONU State updated to:%d", state)
211 } else {
212 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
213 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214}
215
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900216func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200217 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900218 if err != nil {
219 return err
220 }
221 s.updateDevIntState(onu, state)
222 return nil
223}
224
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900225func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900226 defer logger.Debug("activateOLT() Done")
227 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900228 // Activate OLT
229 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900230 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900231 return err
232 }
233 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800234 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900235
236 // OLT sends Interface Indication to Adapter
237 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800238 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239 return err
240 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800241 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900242
243 // OLT sends Operation Indication to Adapter after activating each interface
244 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900245 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800246 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 return err
248 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800249 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900250
251 // OLT sends ONU Discover Indication to Adapter after ONU discovery
252 for intfid, _ := range s.Onumap {
253 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
254 }
255
256 for intfid, _ := range s.Onumap {
257 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800258 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900259 }
260
261 // OLT Sends OnuInd after waiting all of those ONUs up
262 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900263 if IsAllOnuActive(s.Onumap) {
264 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900265 break
266 }
267 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900268
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900269 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900270 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800271 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900272 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900273 return nil
274}
275
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900276// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900277func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
278 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800279 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900280 RemoveVeths(s.Vethnames)
281 s.Vethnames = []string{}
282 s.Ioinfos = []*Ioinfo{}
283 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900284 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900285 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900286 }()
287 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900288 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900289 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800290 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900291 return err
292 }
293 s.Ioinfos = ioinfos
294 s.Vethnames = veths
295 logger.Debug("Created vethnames:%v", s.Vethnames)
296
297 parent := ctx
298 child, cancel := context.WithCancel(parent)
299 s.cancel = cancel
300
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900301 if err = s.runPktLoops(child, stream); err != nil {
302 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303 return err
304 }
305 return nil
306}
307
308//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900309func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900310 if s.cancel != nil {
311 cancel := s.cancel
312 cancel()
313 }
314}
315
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900316func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900317 ioinfos := []*Ioinfo{}
318 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900319 var handler *pcap.Handle
320 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900321 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800322 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900323 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900324 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900325
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900327 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900328 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900329 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900330 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900331}
332
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900333//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900334func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
335 logger.Debug("runPacketPktLoops Start")
336 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900337
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900338 errchOmci := make(chan error)
339 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900340 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900341 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900342
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900343 errchEapol := make(chan error)
344 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
345
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900346 errchDhcp := make(chan error)
347 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
348
Shad Ansarib744bf22019-01-17 11:36:46 -0800349 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900350 logger.Debug("runOMCIResponder Start")
351 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800352 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900353 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800354 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900355 logger.Error("Error happend in Omci:%s", v)
356 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900357 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800358 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900359 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900360 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900361 return nil
362 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900363
Shad Ansarib744bf22019-01-17 11:36:46 -0800364 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900365 logger.Debug("runEapolResponder Start")
366 defer logger.Debug("runEapolResponder Done")
367 select {
368 case v, ok := <-errchEapol:
369 if ok { //Error
370 logger.Error("Error happend in Eapol:%s", v)
371 return v
372 }
373 case <-child.Done():
374 return nil
375 }
376 return nil
377 })
378
379 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900380 logger.Debug("runDhcpResponder Start")
381 defer logger.Debug("runDhcpResponder Done")
382 select {
383 case v, ok := <-errchDhcp:
384 if ok { //Error
385 logger.Error("Error happend in Dhcp:%s", v)
386 return v
387 }
388 case <-child.Done():
389 return nil
390 }
391 return nil
392 })
393
394 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900395 err := s.runMainPktLoop(child, stream)
396 return err
397 })
398
399 if err := eg.Wait(); err != nil {
400 logger.Error("Error happend in runPacketLoops:%s", err)
401 cancel()
402 }
403 return nil
404}
405
406func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900407 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900408 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900409 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900410 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900411 ioinfo, err := s.IdentifyNniIoinfo("inside")
412 if err != nil {
413 return err
414 }
415 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
416 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800417 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418 close(nnichannel)
419 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900420 logger.Debug("BEFORE OLT_ACTIVE")
421 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
422 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900423 data := &openolt.Indication_PktInd{}
424 for {
425 select {
426 case msg := <-s.omciIn:
427 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
428 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
429 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
430 logger.Error("send omci indication failed.", err)
431 continue
432 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900433 case msg := <- s.eapolIn:
434 intfid := msg.IntfId
435 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900436 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900437 if err != nil {
438 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
439 continue
440 }
441
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900442 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900443
444 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
445 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
446 logger.Error("Fail to send EAPOL PktInd indication.", err)
447 return err
448 }
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900449 case msg := <- s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether
450 intfid := msg.IntfId
451 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900452 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900453 bytes := msg.Byte
454 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
455
456 if err != nil {
457 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
458 continue
459 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900460
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200461 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900462 if err != nil {
463 logger.Error("Failed to GetOnuByID:%d", onuid)
464 continue
465 }
466 sn := convB2S(onu.SerialNumber.VendorSpecific)
467 if ctag, ok := s.CtagMap[sn]; ok == true {
468 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
469 if err != nil {
470 utils.LoggerWithOnu(onu).WithFields(log.Fields{
471 "gemId": gemid,
472 }).Error("Fail to tag C-tag")
473 } else {
474 pkt = tagpkt
475 }
476 } else {
477 utils.LoggerWithOnu(onu).WithFields(log.Fields{
478 "gemId": gemid,
479 "cTagMap": s.CtagMap,
480 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
481 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900482
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900483 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
484 logger.Debug(pkt.Dump())
485
486 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
487 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
488 logger.Error("Fail to send DHCP PktInd indication.", err)
489 return err
490 }
491
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900492 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900493 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900494 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
495 logger.Debug("WARNING: This packet does not come from NNI ")
496 continue
497 }
498 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200499 intfid := nnipkt.Info.intfid
500 onu, _ := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900501
502 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200503
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900504 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900505 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
506 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
507 logger.Error("Fail to send PktInd indication.", err)
508 return err
509 }
510
511 case <-ctx.Done():
512 logger.Debug("Closed nnichannel ")
513 return nil
514 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900515 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900516 return nil
517}
518
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900519func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
520 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200521 onu, _ := s.GetOnuByID(onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800522
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900523 if layerEth != nil {
524 pkt, _ := layerEth.(*layers.Ethernet)
525 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900526 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800527 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900528 eapolPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900529 s.eapolOut <- &eapolPkt
530 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900531 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800532 utils.LoggerWithOnu(onu).WithFields(log.Fields{
533 "payload": layerDHCP.LayerPayload(),
534 "type": layerDHCP.LayerType().String(),
535 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900536 rawpkt, _, _ = PopVLAN(rawpkt)
537 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900538 logger.Debug("%s", rawpkt.Dump())
539 dhcpPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
540 s.dhcpOut <- &dhcpPkt
541 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900542 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800543 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900544 return nil
545 }
546 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
547 if err != nil {
548 return err
549 }
550 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800551 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900552 return nil
553 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800554 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900555 return nil
556}
557
558func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900559 poppkt, _, err := PopVLAN(rawpkt)
560 poppkt, _, err = PopVLAN(poppkt)
561 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900562 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900563 return err
564 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900565 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900566 if err != nil {
567 return err
568 }
569 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900570 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900571 SendNni(handle, poppkt)
572 return nil
573}
574
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900575func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
576 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900577 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900578 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900579 return false
580 }
581 }
582 }
583 return true
584}
585
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900586func (s *Server) isAllOnuOmciActive() bool {
587 for _, onus := range s.Onumap {
588 for _, onu := range onus{
589 if onu.GetIntState() != device.ONU_OMCIACTIVE {
590 return false
591 }
592 }
593 }
594 return true
595}
596
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900597func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900598 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
599 gemportid, err := omci.GetGemPortId(intfid, onuid)
600 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900601 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
602 }
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200603 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900604 if err != nil {
605 logger.Error("Failed to getGemPortID: %s", err)
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900606 return 0, err
607 }
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900608 gemportid = onu.GemportID
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900609 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900610}
611
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900612func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
613 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900614 for _, onu := range onus {
615 if device.ValidateSN(*sn, *onu.SerialNumber) {
616 return onu, nil
617 }
618 }
619 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900620 err := errors.New("No mathced SN is found ")
621 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900622 return nil, err
623}
624
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200625func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
626 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800627}
628
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200629func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
630 for _, onu := range onumap[intfid] {
631 if onu.OnuID == onuid {
632 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900633 }
634 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900635 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800636 logger.WithFields(log.Fields{
637 "onumap": onumap,
638 "onuid": onuid,
639 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900640 return nil, err
641}
642
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900643func convB2S(b []byte) string {
644 s := ""
645 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700646 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900647 }
648 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700649}