blob: 65b9c108d261955b4e714411ae8228a607812582 [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"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020021 "encoding/hex"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070022 "errors"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020023 "reflect"
Matteo Scandolo88e91892018-11-06 16:29:19 -080024 "strconv"
25 "sync"
Shad Ansarib744bf22019-01-17 11:36:46 -080026
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020027 pb "gerrit.opencord.org/voltha-bbsim/api"
Matteo Scandolo88e91892018-11-06 16:29:19 -080028 "gerrit.opencord.org/voltha-bbsim/common/logger"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029 "gerrit.opencord.org/voltha-bbsim/device"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020030 flowHandler "gerrit.opencord.org/voltha-bbsim/flow"
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070031 openolt "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032 "github.com/google/gopacket"
33 "github.com/google/gopacket/layers"
34 "github.com/google/gopacket/pcap"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020035 omci "github.com/opencord/omci-sim"
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
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020041// Constants
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042const (
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020043 NniVethNorthPfx = "nni_north"
44 NniVethSouthPfx = "nni_south"
45 MaxPonPorts = 64
46 MaxOnusPerPon = 64 // This value should be the same with the value in AdapterPlatform class
47 VendorIDLength = 4
48 SerialNumberLength = 12
49 OpenOltStart = "start"
50 OpenOltStop = "stop"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051)
52
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020053// Server structure consists of all the params required for BBsim.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054type Server struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020055 wg *sync.WaitGroup
56 Olt *device.Olt
57 Onumap map[uint32][]*device.Onu
58 SNmap sync.Map
59 AutoONUActivate bool
60 Ioinfos []*Ioinfo
61 gRPCserver *grpc.Server
62 gRPCAddress string
63 gRPCPort uint32
64 mgmtServer *grpc.Server
65 mgmtGrpcPort uint32
66 mgmtRestPort uint32
67 Vethnames []string
68 IndInterval int
69 Processes []string
70 EnableServer *openolt.Openolt_EnableIndicationServer
71 CtagMap map[string]uint32
72 cancel context.CancelFunc
73 stateRepCh chan stateReport
74 omciIn chan openolt.OmciIndication
75 omciOut chan openolt.OmciMsg
76 eapolIn chan *byteMsg
77 eapolOut chan *byteMsg
78 dhcpIn chan *byteMsg
79 dhcpOut chan *byteMsg
80 FlowMap map[FlowKey]*openolt.Flow
81 alarmCh chan *openolt.Indication
82 deviceActionCh chan *pb.DeviceAction
83 serverActionCh chan string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090084}
85
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020086// Packet structure
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090087type Packet struct {
88 Info *Ioinfo
89 Pkt gopacket.Packet
90}
91
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090092type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090093 IntfId uint32
94 OnuId uint32
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070095 Byte []byte
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090096}
97
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090098type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080099 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900100 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -0800101 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900102}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200104// FlowKey used for FlowMap key
105type FlowKey struct {
106 FlowID uint32
107 FlowDirection string
108}
109
Anjali Thontakudi66659202019-07-03 23:14:02 +0000110//Has options (OLT id, number onu ports) from mediator
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200111// NewCore initialize OLT and ONU objects
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900112func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 // TODO: make it decent
114 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -0800115 npon := opt.npon
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200116 if npon > MaxPonPorts {
117 logger.Warn("Provided number of PON ports exceeds limit of %d", MaxPonPorts)
118 logger.Info("Setting number of PON ports to %d", MaxPonPorts)
119 npon = MaxPonPorts
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200121 nonus := opt.nonus
122 if nonus > MaxOnusPerPon {
123 logger.Warn("Provided number of ONUs per PON port exceeds limit of %d", MaxOnusPerPon)
124 logger.Info("Setting number of ONUs per PON port to %d", MaxOnusPerPon)
125 nonus = MaxOnusPerPon
126 }
127 s := Server{
128 Olt: device.NewOlt(oltid, npon, 1), // TODO nnni is to be taken from options
129 Onumap: make(map[uint32][]*device.Onu),
130 Ioinfos: []*Ioinfo{},
131 gRPCAddress: opt.address,
132 gRPCPort: opt.port,
133 Vethnames: []string{},
134 IndInterval: opt.intvl,
135 AutoONUActivate: !opt.interactiveOnuActivation,
136 Processes: []string{},
137 mgmtGrpcPort: opt.mgmtGrpcPort,
138 mgmtRestPort: opt.mgmtRestPort,
139 EnableServer: nil,
140 stateRepCh: make(chan stateReport, 8),
141 omciIn: make(chan openolt.OmciIndication, 1024),
142 omciOut: make(chan openolt.OmciMsg, 1024),
143 eapolIn: make(chan *byteMsg, 1024),
144 eapolOut: make(chan *byteMsg, 1024),
145 dhcpIn: make(chan *byteMsg, 1024),
146 dhcpOut: make(chan *byteMsg, 1024),
147 FlowMap: make(map[FlowKey]*openolt.Flow),
148 serverActionCh: make(chan string),
149 }
150 logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900151
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900152 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800153 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200154 logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber)
155 // Creating Onu Map
156 for intfid := uint32(0); intfid < npon; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900158 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900159
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200160 logger.Debug("Onu Map:")
161 for _, onus := range s.Onumap {
162 for _, onu := range onus {
163 logger.Debug("%+v", *onu)
164 }
165 }
166
167 // TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900168 s.CtagMap = make(map[string]uint32)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200169 for i := 0; i < MaxOnusPerPon; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900170 oltid := s.Olt.ID
171 intfid := uint32(1)
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900172 sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700173 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900174 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200175
176 flowHandler.InitializeFlowManager(s.Olt.ID)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 return &s
178}
179
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200180// Start starts the openolt gRPC server (blocking)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800181func (s *Server) Start() error {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200182 logger.Debug("Starting OpenOLT gRPC Server")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800183 defer func() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200184 logger.Debug("OpenOLT gRPC Server Stopped")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200186
187 // Start Openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
189 listener, gserver, err := NewGrpcServer(addressport)
190 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700191 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 return err
193 }
194 s.gRPCserver = gserver
195 openolt.RegisterOpenoltServer(gserver, s)
196 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700197 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900198 return err
199 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 return nil
201}
202
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200203// Stop stops the openolt gRPC servers (non-blocking).
Matteo Scandolo88e91892018-11-06 16:29:19 -0800204func (s *Server) Stop() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200205 logger.Debug("Stopping OpenOLT gRPC Server & PktLoops")
206 defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped")
207
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900208 if s.gRPCserver != nil {
209 s.gRPCserver.Stop()
210 logger.Debug("gRPCserver.Stop()")
211 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200212
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900213 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900214 return
215}
216
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200217func (s *Server) startMgmtServer(wg *sync.WaitGroup) {
218 defer logger.Debug("Management api server exited")
219
220 grpcAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
221 restAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtRestPort))
222 // Start rest gateway for BBSim server
223 go StartRestGatewayService(grpcAddressPort, restAddressPort, wg)
224 addressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
225
226 listener, apiserver, err := NewMgmtAPIServer(addressPort)
227 if err != nil {
228 logger.Error("Unable to create management api server %v", err)
229 return
230 }
231
232 s.mgmtServer = apiserver
233 pb.RegisterBBSimServiceServer(apiserver, s)
234 if e := apiserver.Serve(listener); e != nil {
235 logger.Error("Failed to run management api server %v", e)
236 return
237 }
238
239}
240
241func (s *Server) stopMgmtServer() error {
242 if s.mgmtServer != nil {
243 s.mgmtServer.GracefulStop()
244 logger.Debug("Management server stopped")
245 return nil
246 }
247 return errors.New("can not stop management server, server not created")
248}
249
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200250// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900251func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900252 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800253 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900254 olt.Initialize()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200255 // Below lines commented as we dont want to change the onu state on restart
256 // for intfid := range s.Onumap {
257 // for _, onu := range s.Onumap[intfid] {
258 // onu.Initialize()
259 // }
260 // }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900261 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900262 logger.Debug("Enable() Done")
263 }()
264 logger.Debug("Enable() Start")
265 s.EnableServer = sv
266 if err := s.activateOLT(*sv); err != nil {
267 return err
268 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700269
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900270 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900271
272 coreCtx := context.Background()
273 coreCtx, corecancel := context.WithCancel(coreCtx)
274 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700275
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200276 errorchan := make(chan error, 5)
277 go s.StartPktLoops(coreCtx, *sv, errorchan)
278
279 if s.AutoONUActivate == true {
280 // Initialize all ONUs
281 for intfid := range s.Onumap {
282 for _, onu := range s.Onumap[intfid] {
283 onu.Initialize()
284 }
285 }
286 // Activate all ONUs
287 s.activateONUs(*sv, s.Onumap)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900288 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200289
290 select {
291 case err := <-errorchan:
292 if err != nil {
293 logger.Debug("Error: %v", err)
294 return err
295 }
296 }
297
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900298 return nil
299}
300
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200301// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900302func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800303 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304 logger.Debug("Disable() Done")
305 }()
306 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900307 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900308}
309
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900310func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
311 current := dev.GetIntState()
312 dev.UpdateIntState(state)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700313 logger.Debug("updateDevIntState called state: current %s, next %s", device.ONUState[current], device.ONUState[dev.GetIntState()])
Shad Ansarib744bf22019-01-17 11:36:46 -0800314 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
315 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700316 logger.Debug("OLT State updated to:%s", device.ONUState[state])
Shad Ansarib744bf22019-01-17 11:36:46 -0800317 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700318 logger.Debug("ONU State updated to:%s", device.ONUState[state])
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900319 } else {
320 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
321 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900322}
323
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700324func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700325
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200326 onu, err := s.GetOnuByID(onuid, intfid)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700327
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900328 if err != nil {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700329 logger.Warn("Failed to get ONU %d: %v", onuid, err)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900330 return err
331 }
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700332
333 if state == onu.GetIntState(){
334 logger.WithFields(log.Fields{
335 "toState": device.ONUState[state],
336 "currentState": device.ONUState[onu.GetIntState()],
337 }).Warn("Trying to update the state with the same state, ignoring this request")
338 return nil
339 }
340
341 logger.Debug("Transitioning ONU state from %s to %s", device.ONUState[onu.GetIntState()], device.ONUState[state])
342
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900343 s.updateDevIntState(onu, state)
344 return nil
345}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200346
347func (s *Server) activateOnu(onu *device.Onu) {
348 snKey := stringifySerialNumber(onu.SerialNumber)
349 s.SNmap.Store(snKey, onu)
350 device.UpdateOnusOpStatus(onu.IntfID, onu, "up")
351
352 err := sendOnuDiscInd(*s.EnableServer, onu)
353 if err != nil {
354 logger.Error(err.Error())
355 return
356 }
357 logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID)
358 logger.Debug("activateONUs Entry in SNmap %v", snKey)
359}
360
361func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) {
362 // Add all ONUs to SerialNumber Map
363 for intfid := range Onumap {
364 for _, onu := range Onumap[intfid] {
365 s.activateOnu(onu)
366 }
367 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700368}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900369
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900370func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900371 defer logger.Debug("activateOLT() Done")
372 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900373 // Activate OLT
374 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900375 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900376 return err
377 }
378 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800379 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900380
381 // OLT sends Interface Indication to Adapter
382 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800383 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900384 return err
385 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800386 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900387
388 // OLT sends Operation Indication to Adapter after activating each interface
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900389 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800390 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900391 return err
392 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800393 logger.Info("OLT %s sent OperInd.", olt.Name)
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700394 return nil
395}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900396
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200397// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200398func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900399 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800400 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900401 RemoveVeths(s.Vethnames)
402 s.Vethnames = []string{}
403 s.Ioinfos = []*Ioinfo{}
404 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900405 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900406 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900407 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200408 s.alarmCh = make(chan *openolt.Indication, 10)
409 go startAlarmLoop(stream, s.alarmCh)
410 go s.startDeviceActionLoop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900411 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900412 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900413 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700414 logger.Error("createIoinfos failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200415 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900416 }
417 s.Ioinfos = ioinfos
418 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700419 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900420
421 parent := ctx
422 child, cancel := context.WithCancel(parent)
423 s.cancel = cancel
424
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900425 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700426 logger.Error("runPktLoops failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200427 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900428 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200429 errorchan <- nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900430}
431
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200432// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900433func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900434 if s.cancel != nil {
435 cancel := s.cancel
436 cancel()
437 }
438}
439
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900440func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900441 ioinfos := []*Ioinfo{}
442 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900443 var handler *pcap.Handle
444 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900445 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700446 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900447 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900448 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900449
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900450 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900451 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900452 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900453 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900454 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900455}
456
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200457// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900458func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
459 logger.Debug("runPacketPktLoops Start")
460 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900461
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900462 errchOmci := make(chan error)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200463 s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900464 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900465 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900466
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900467 errchEapol := make(chan error)
468 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
469
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900470 errchDhcp := make(chan error)
471 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
472
Shad Ansarib744bf22019-01-17 11:36:46 -0800473 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900474 logger.Debug("runOMCIResponder Start")
475 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800476 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900477 case v, ok := <-errchOmci: // Wait for OmciInitialization
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200478 if ok { // Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700479 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900480 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900481 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800482 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900483 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900484 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900485 return nil
486 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487
Shad Ansarib744bf22019-01-17 11:36:46 -0800488 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900489 logger.Debug("runEapolResponder Start")
490 defer logger.Debug("runEapolResponder Done")
491 select {
492 case v, ok := <-errchEapol:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200493 if ok { // Error
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900494 logger.Error("Error happend in Eapol:%s", v)
495 return v
496 }
497 case <-child.Done():
498 return nil
499 }
500 return nil
501 })
502
503 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900504 logger.Debug("runDhcpResponder Start")
505 defer logger.Debug("runDhcpResponder Done")
506 select {
507 case v, ok := <-errchDhcp:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200508 if ok { // Error
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900509 logger.Error("Error happend in Dhcp:%s", v)
510 return v
511 }
512 case <-child.Done():
513 return nil
514 }
515 return nil
516 })
517
518 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900519 err := s.runMainPktLoop(child, stream)
520 return err
521 })
522
523 if err := eg.Wait(); err != nil {
524 logger.Error("Error happend in runPacketLoops:%s", err)
525 cancel()
526 }
527 return nil
528}
529
530func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900531 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900532 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900533 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900534 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900535 ioinfo, err := s.IdentifyNniIoinfo("inside")
536 if err != nil {
537 return err
538 }
539 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
540 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800541 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900542 close(nnichannel)
543 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900544 logger.Debug("BEFORE OLT_ACTIVE")
545 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
546 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900547 data := &openolt.Indication_PktInd{}
548 for {
549 select {
550 case msg := <-s.omciIn:
551 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
552 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
553 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700554 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900555 continue
556 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700557 case msg := <-s.eapolIn:
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900558 intfid := msg.IntfId
559 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900560 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900561 if err != nil {
562 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
563 continue
564 }
565
Zack Williamsb85f5932019-05-10 16:21:35 -0700566 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, msg.Byte)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900567
568 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
569 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700570 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900571 return err
572 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200573 case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900574 intfid := msg.IntfId
575 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900576 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900577 bytes := msg.Byte
578 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
579
580 if err != nil {
581 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
582 continue
583 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900584
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200585 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900586 if err != nil {
587 logger.Error("Failed to GetOnuByID:%d", onuid)
588 continue
589 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900590 sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900591 if ctag, ok := s.CtagMap[sn]; ok == true {
592 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
593 if err != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900594 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900595 "gemId": gemid,
596 }).Error("Fail to tag C-tag")
597 } else {
598 pkt = tagpkt
599 }
600 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900601 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900602 "gemId": gemid,
603 "cTagMap": s.CtagMap,
604 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
605 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900606
Zack Williamsb85f5932019-05-10 16:21:35 -0700607 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, pkt.Dump())
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900608
609 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
610 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700611 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900612 return err
613 }
614
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900615 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900616 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900617 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
618 logger.Debug("WARNING: This packet does not come from NNI ")
619 continue
620 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200621
622 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200623 intfid := nnipkt.Info.intfid
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200624 onu, _ := s.GetOnuByID(onuid, intfid)
625
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900626 device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200627
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900628 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900629 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
630 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700631 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900632 return err
633 }
634
635 case <-ctx.Done():
636 logger.Debug("Closed nnichannel ")
637 return nil
638 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900639 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900640}
641
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900642func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
643 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200644 onu, err := s.GetOnuByID(onuid, intfid)
645 if err != nil {
646 logger.Error("Failed processing onuPacketOut: %v", err)
647 return err
648 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800649
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900650 if layerEth != nil {
651 pkt, _ := layerEth.(*layers.Ethernet)
652 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900653 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900654 device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700655 eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900656 s.eapolOut <- &eapolPkt
657 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900658 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900659 device.LoggerWithOnu(onu).WithFields(log.Fields{
Matteo Scandoloa286c742018-11-20 08:10:04 -0800660 "payload": layerDHCP.LayerPayload(),
661 "type": layerDHCP.LayerType().String(),
662 }).Info("Received downstream packet is DHCP.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700663 poppkt, _, err := PopVLAN(rawpkt)
664 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700665 logger.Warn("Received untagged packet when expecting single-tagged packet.")
666 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700667 } else {
668 // check to see if the packet was double-tagged
Scott Baker07fe4dc2019-08-05 16:36:37 -0700669 poppktAgain, _, err := PopVLAN(poppkt)
Scott Baker2b4ffb72019-08-02 16:20:47 -0700670 if err == nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700671 logger.Warn("Received double-tagged packet when expecting single-tagged packet.")
672 poppkt = poppktAgain
Scott Baker2b4ffb72019-08-02 16:20:47 -0700673 }
674 }
675 logger.Debug("%s", poppkt.Dump())
676 dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900677 s.dhcpOut <- &dhcpPkt
678 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900679 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900680 device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700681 // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900682 return nil
683 }
684 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
685 if err != nil {
686 return err
687 }
688 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800689 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900690 return nil
691 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900692 device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900693 return nil
694}
695
696func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900697 poppkt, _, err := PopVLAN(rawpkt)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900698 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700699 logger.Warn("Received untagged packet when expecting double-tagged packet.")
700 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700701 } else {
702 // check to see if the packet was double-tagged
703 poppktAgain, _, err := PopVLAN(poppkt)
704 if err == nil {
705 poppkt = poppktAgain
706 } else {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700707 logger.Warn("Received single-tagged packet when expecting double-tagged packet.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700708 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900709 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900710 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900711 if err != nil {
712 return err
713 }
714 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900715 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900716 SendNni(handle, poppkt)
717 return nil
718}
719
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200720// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900721func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
722 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900723 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900724 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900725 return false
726 }
727 }
728 }
729 return true
730}
731
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900732func (s *Server) isAllOnuOmciActive() bool {
733 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700734 for _, onu := range onus {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900735 if onu.GetIntState() != device.ONU_OMCIACTIVE {
736 return false
737 }
738 }
739 }
740 return true
741}
742
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900743func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900744 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
745 gemportid, err := omci.GetGemPortId(intfid, onuid)
746 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900747 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200748 onu, err := s.GetOnuByID(onuid, intfid)
749 if err != nil {
750 logger.Error("Failed to getGemPortID: %s", err)
751 return 0, err
752 }
753 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900754 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900755 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900756}
757
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900758func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
759 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900760 for _, onu := range onus {
761 if device.ValidateSN(*sn, *onu.SerialNumber) {
762 return onu, nil
763 }
764 }
765 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200766 err := errors.New("no matching serial number found")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900767 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900768 return nil, err
769}
770
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200771// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200772func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
773 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800774}
775
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200776func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
777 for _, onu := range onumap[intfid] {
778 if onu.OnuID == onuid {
779 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900780 }
781 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200782 err := errors.New("no matching OnuID found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800783 logger.WithFields(log.Fields{
784 "onumap": onumap,
785 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200786 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800787 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900788 return nil, err
789}
790
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200791// getOnuFromSNmap method returns onu object from SNmap if found
792func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) {
793 snkey := stringifySerialNumber(serialNumber)
794
795 logger.Debug("getOnuFromSNmap received serial number %s", snkey)
796
797 if onu, exist := s.SNmap.Load(snkey); exist {
798 logger.Info("Serial number found in map")
799 return onu.(*device.Onu), true
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900800 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200801 logger.Info("Serial number not found in map")
802 return nil, false
803}
804
805func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900806 return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200807}
808
809func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
810 if len(SerialNumber) != SerialNumberLength {
811 logger.Error("Invalid serial number %s", SerialNumber)
812 return nil, errors.New("invalid serial number")
813 }
814 // First four characters are vendorId
815 vendorID := SerialNumber[:VendorIDLength]
816 vendorSpecific := SerialNumber[VendorIDLength:]
817
818 vsbyte, _ := hex.DecodeString(vendorSpecific)
819
820 // Convert to Openolt serial number
821 serialNum := new(openolt.SerialNumber)
822 serialNum.VendorId = []byte(vendorID)
823 serialNum.VendorSpecific = vsbyte
824
825 return serialNum, nil
826}
827
828// TODO move to device_onu.go
829func (s *Server) sendOnuIndicationsOnOltReboot() {
830 if AutoONUActivate == 1 {
831 // For auto activate mode, onu indications is sent in Enable()
832 return
833 }
834
835 s.SNmap.Range(
836 func(key, value interface{}) bool {
837 onu := value.(*device.Onu)
838 if onu.InternalState == device.ONU_LOS_RAISED {
839 return true
840 }
841
842 err := sendOnuDiscInd(*s.EnableServer, onu)
843 if err != nil {
844 logger.Error(err.Error())
845 }
846
847 return true
848 })
849}
850
851// StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received
852func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) {
853 for {
854 select {
855 case Req := <-s.serverActionCh:
856 logger.Debug("Request Received On serverActionCh: %+v", Req)
857 switch Req {
858 case "start":
859 logger.Debug("Server Start Request Received On ServerActionChannel")
860 go s.Start() // blocking
861 case "stop":
862 logger.Debug("Server Stop Request Received On ServerActionChannel")
863 s.Stop()
864 default:
865 logger.Error("Invalid value received in deviceActionCh")
866 }
867 }
868 }
869}
870
871// startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions
872// TODO all onu and olt related actions (like alarms) should be handled using this function
873func (s *Server) startDeviceActionLoop() {
874 logger.Debug("startDeviceActionLoop invoked")
875 s.deviceActionCh = make(chan *pb.DeviceAction, 10)
876 for {
877 logger.Debug("Action channel loop started")
878 select {
879 case Req := <-s.deviceActionCh:
880 logger.Debug("Reboot Action Type: %+v", Req.DeviceAction)
881 switch Req.DeviceType {
882 case DeviceTypeOnu:
883 value, _ := s.SNmap.Load(Req.DeviceSerialNumber)
884 onu := value.(*device.Onu)
885 if Req.DeviceAction == SoftReboot {
886 s.handleONUSoftReboot(onu.IntfID, onu.OnuID)
887 } else if Req.DeviceAction == HardReboot {
888 s.handleONUHardReboot(onu)
889 }
890 case DeviceTypeOlt:
891 logger.Debug("Reboot For OLT Received")
892 s.handleOLTReboot()
893 default:
894 logger.Error("Invalid value received in deviceActionCh")
895 }
896 }
897 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700898}