blob: 6899d8682d56287aee07a0bc21cbc2fc6ef8672e [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"
Mahir Gunyel32dfd722019-08-05 16:18:06 +030026 "time"
Shad Ansarib744bf22019-01-17 11:36:46 -080027
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028 "github.com/google/gopacket"
29 "github.com/google/gopacket/layers"
30 "github.com/google/gopacket/pcap"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020031 omci "github.com/opencord/omci-sim"
Zack Williams2abf3932019-08-05 14:07:05 -070032 pb "github.com/opencord/voltha-bbsim/api"
33 "github.com/opencord/voltha-bbsim/common/logger"
34 "github.com/opencord/voltha-bbsim/device"
35 flowHandler "github.com/opencord/voltha-bbsim/flow"
36 openolt "github.com/opencord/voltha-bbsim/protos"
Matteo Scandolo88e91892018-11-06 16:29:19 -080037 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090038 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080039 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040)
41
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020042// Constants
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090043const (
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020044 NniVethNorthPfx = "nni_north"
45 NniVethSouthPfx = "nni_south"
46 MaxPonPorts = 64
47 MaxOnusPerPon = 64 // This value should be the same with the value in AdapterPlatform class
48 VendorIDLength = 4
49 SerialNumberLength = 12
50 OpenOltStart = "start"
51 OpenOltStop = "stop"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052)
53
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020054// Server structure consists of all the params required for BBsim.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090055type Server struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020056 wg *sync.WaitGroup
57 Olt *device.Olt
58 Onumap map[uint32][]*device.Onu
59 SNmap sync.Map
60 AutoONUActivate bool
61 Ioinfos []*Ioinfo
62 gRPCserver *grpc.Server
63 gRPCAddress string
64 gRPCPort uint32
65 mgmtServer *grpc.Server
66 mgmtGrpcPort uint32
67 mgmtRestPort uint32
68 Vethnames []string
Mahir Gunyel32dfd722019-08-05 16:18:06 +030069 IndInterval time.Duration
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020070 Processes []string
71 EnableServer *openolt.Openolt_EnableIndicationServer
72 CtagMap map[string]uint32
73 cancel context.CancelFunc
74 stateRepCh chan stateReport
75 omciIn chan openolt.OmciIndication
76 omciOut chan openolt.OmciMsg
77 eapolIn chan *byteMsg
78 eapolOut chan *byteMsg
79 dhcpIn chan *byteMsg
80 dhcpOut chan *byteMsg
81 FlowMap map[FlowKey]*openolt.Flow
82 alarmCh chan *openolt.Indication
83 deviceActionCh chan *pb.DeviceAction
84 serverActionCh chan string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090085}
86
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020087// Packet structure
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090088type Packet struct {
89 Info *Ioinfo
90 Pkt gopacket.Packet
91}
92
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090093type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090094 IntfId uint32
95 OnuId uint32
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070096 Byte []byte
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090097}
98
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090099type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -0800100 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900101 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -0800102 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900103}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900104
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200105// FlowKey used for FlowMap key
106type FlowKey struct {
107 FlowID uint32
108 FlowDirection string
109}
110
Anjali Thontakudi66659202019-07-03 23:14:02 +0000111//Has options (OLT id, number onu ports) from mediator
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200112// NewCore initialize OLT and ONU objects
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900113func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 // TODO: make it decent
115 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -0800116 npon := opt.npon
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200117 if npon > MaxPonPorts {
118 logger.Warn("Provided number of PON ports exceeds limit of %d", MaxPonPorts)
119 logger.Info("Setting number of PON ports to %d", MaxPonPorts)
120 npon = MaxPonPorts
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900121 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200122 nonus := opt.nonus
123 if nonus > MaxOnusPerPon {
124 logger.Warn("Provided number of ONUs per PON port exceeds limit of %d", MaxOnusPerPon)
125 logger.Info("Setting number of ONUs per PON port to %d", MaxOnusPerPon)
126 nonus = MaxOnusPerPon
127 }
128 s := Server{
129 Olt: device.NewOlt(oltid, npon, 1), // TODO nnni is to be taken from options
130 Onumap: make(map[uint32][]*device.Onu),
131 Ioinfos: []*Ioinfo{},
132 gRPCAddress: opt.address,
133 gRPCPort: opt.port,
134 Vethnames: []string{},
135 IndInterval: opt.intvl,
136 AutoONUActivate: !opt.interactiveOnuActivation,
137 Processes: []string{},
138 mgmtGrpcPort: opt.mgmtGrpcPort,
139 mgmtRestPort: opt.mgmtRestPort,
140 EnableServer: nil,
141 stateRepCh: make(chan stateReport, 8),
142 omciIn: make(chan openolt.OmciIndication, 1024),
143 omciOut: make(chan openolt.OmciMsg, 1024),
144 eapolIn: make(chan *byteMsg, 1024),
145 eapolOut: make(chan *byteMsg, 1024),
146 dhcpIn: make(chan *byteMsg, 1024),
147 dhcpOut: make(chan *byteMsg, 1024),
148 FlowMap: make(map[FlowKey]*openolt.Flow),
149 serverActionCh: make(chan string),
150 }
151 logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900152
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900153 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800154 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200155 logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber)
156 // Creating Onu Map
157 for intfid := uint32(0); intfid < npon; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900158 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900159 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900160
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200161 logger.Debug("Onu Map:")
162 for _, onus := range s.Onumap {
163 for _, onu := range onus {
164 logger.Debug("%+v", *onu)
165 }
166 }
167
168 // TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900169 s.CtagMap = make(map[string]uint32)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200170 for i := 0; i < MaxOnusPerPon; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900171 oltid := s.Olt.ID
172 intfid := uint32(1)
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900173 sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700174 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900175 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200176
177 flowHandler.InitializeFlowManager(s.Olt.ID)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900178 return &s
179}
180
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200181// Start starts the openolt gRPC server (blocking)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800182func (s *Server) Start() error {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200183 logger.Debug("Starting OpenOLT gRPC Server")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800184 defer func() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200185 logger.Debug("OpenOLT gRPC Server Stopped")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900186 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200187
188 // Start Openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900189 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
190 listener, gserver, err := NewGrpcServer(addressport)
191 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700192 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900193 return err
194 }
195 s.gRPCserver = gserver
196 openolt.RegisterOpenoltServer(gserver, s)
197 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700198 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900199 return err
200 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900201 return nil
202}
203
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200204// Stop stops the openolt gRPC servers (non-blocking).
Matteo Scandolo88e91892018-11-06 16:29:19 -0800205func (s *Server) Stop() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200206 logger.Debug("Stopping OpenOLT gRPC Server & PktLoops")
207 defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped")
208
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900209 if s.gRPCserver != nil {
210 s.gRPCserver.Stop()
211 logger.Debug("gRPCserver.Stop()")
212 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200213
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900214 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900215 return
216}
217
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200218func (s *Server) startMgmtServer(wg *sync.WaitGroup) {
219 defer logger.Debug("Management api server exited")
220
221 grpcAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
222 restAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtRestPort))
223 // Start rest gateway for BBSim server
224 go StartRestGatewayService(grpcAddressPort, restAddressPort, wg)
225 addressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
226
227 listener, apiserver, err := NewMgmtAPIServer(addressPort)
228 if err != nil {
229 logger.Error("Unable to create management api server %v", err)
230 return
231 }
232
233 s.mgmtServer = apiserver
234 pb.RegisterBBSimServiceServer(apiserver, s)
235 if e := apiserver.Serve(listener); e != nil {
236 logger.Error("Failed to run management api server %v", e)
237 return
238 }
239
240}
241
242func (s *Server) stopMgmtServer() error {
243 if s.mgmtServer != nil {
244 s.mgmtServer.GracefulStop()
245 logger.Debug("Management server stopped")
246 return nil
247 }
248 return errors.New("can not stop management server, server not created")
249}
250
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200251// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900252func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900253 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800254 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900255 olt.Initialize()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200256 // Below lines commented as we dont want to change the onu state on restart
257 // for intfid := range s.Onumap {
258 // for _, onu := range s.Onumap[intfid] {
259 // onu.Initialize()
260 // }
261 // }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900262 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900263 logger.Debug("Enable() Done")
264 }()
265 logger.Debug("Enable() Start")
266 s.EnableServer = sv
267 if err := s.activateOLT(*sv); err != nil {
268 return err
269 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700270
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900271 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900272
273 coreCtx := context.Background()
274 coreCtx, corecancel := context.WithCancel(coreCtx)
275 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700276
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200277 errorchan := make(chan error, 5)
278 go s.StartPktLoops(coreCtx, *sv, errorchan)
279
280 if s.AutoONUActivate == true {
281 // Initialize all ONUs
282 for intfid := range s.Onumap {
283 for _, onu := range s.Onumap[intfid] {
284 onu.Initialize()
285 }
286 }
287 // Activate all ONUs
288 s.activateONUs(*sv, s.Onumap)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900289 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200290
291 select {
292 case err := <-errorchan:
293 if err != nil {
294 logger.Debug("Error: %v", err)
295 return err
296 }
297 }
298
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900299 return nil
300}
301
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200302// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800304 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900305 logger.Debug("Disable() Done")
306 }()
307 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900308 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900309}
310
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900311func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
312 current := dev.GetIntState()
313 dev.UpdateIntState(state)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700314 logger.Debug("updateDevIntState called state: current %s, next %s", device.ONUState[current], device.ONUState[dev.GetIntState()])
Shad Ansarib744bf22019-01-17 11:36:46 -0800315 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
316 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700317 logger.Debug("OLT State updated to:%s", device.ONUState[state])
Shad Ansarib744bf22019-01-17 11:36:46 -0800318 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700319 logger.Debug("ONU State updated to:%s", device.ONUState[state])
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900320 } else {
321 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
322 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900323}
324
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700325func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700326
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200327 onu, err := s.GetOnuByID(onuid, intfid)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700328
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900329 if err != nil {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700330 logger.Warn("Failed to get ONU %d: %v", onuid, err)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900331 return err
332 }
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700333
334 if state == onu.GetIntState(){
335 logger.WithFields(log.Fields{
336 "toState": device.ONUState[state],
337 "currentState": device.ONUState[onu.GetIntState()],
338 }).Warn("Trying to update the state with the same state, ignoring this request")
339 return nil
340 }
341
342 logger.Debug("Transitioning ONU state from %s to %s", device.ONUState[onu.GetIntState()], device.ONUState[state])
343
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900344 s.updateDevIntState(onu, state)
345 return nil
346}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200347
348func (s *Server) activateOnu(onu *device.Onu) {
349 snKey := stringifySerialNumber(onu.SerialNumber)
350 s.SNmap.Store(snKey, onu)
351 device.UpdateOnusOpStatus(onu.IntfID, onu, "up")
352
353 err := sendOnuDiscInd(*s.EnableServer, onu)
354 if err != nil {
355 logger.Error(err.Error())
356 return
357 }
358 logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID)
359 logger.Debug("activateONUs Entry in SNmap %v", snKey)
360}
361
362func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) {
363 // Add all ONUs to SerialNumber Map
364 for intfid := range Onumap {
365 for _, onu := range Onumap[intfid] {
366 s.activateOnu(onu)
Mahir Gunyel32dfd722019-08-05 16:18:06 +0300367 if s.IndInterval > 0 {
368 time.Sleep(s.IndInterval)
369 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200370 }
371 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700372}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900373
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900374func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900375 defer logger.Debug("activateOLT() Done")
376 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900377 // Activate OLT
378 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900379 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900380 return err
381 }
382 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800383 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900384
385 // OLT sends Interface Indication to Adapter
386 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800387 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900388 return err
389 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800390 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900391
392 // OLT sends Operation Indication to Adapter after activating each interface
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900393 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800394 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900395 return err
396 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800397 logger.Info("OLT %s sent OperInd.", olt.Name)
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700398 return nil
399}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900400
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200401// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200402func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900403 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800404 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900405 RemoveVeths(s.Vethnames)
406 s.Vethnames = []string{}
407 s.Ioinfos = []*Ioinfo{}
408 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900409 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900410 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900411 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200412 s.alarmCh = make(chan *openolt.Indication, 10)
413 go startAlarmLoop(stream, s.alarmCh)
414 go s.startDeviceActionLoop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900415 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900416 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900417 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700418 logger.Error("createIoinfos failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200419 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900420 }
421 s.Ioinfos = ioinfos
422 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700423 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900424
425 parent := ctx
426 child, cancel := context.WithCancel(parent)
427 s.cancel = cancel
428
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900429 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700430 logger.Error("runPktLoops failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200431 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900432 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200433 errorchan <- nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900434}
435
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200436// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900437func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900438 if s.cancel != nil {
439 cancel := s.cancel
440 cancel()
441 }
442}
443
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900444func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900445 ioinfos := []*Ioinfo{}
446 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900447 var handler *pcap.Handle
448 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900449 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700450 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900451 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900452 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900453
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900454 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900455 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900456 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900457 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900458 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900459}
460
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200461// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900462func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
463 logger.Debug("runPacketPktLoops Start")
464 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900465
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900466 errchOmci := make(chan error)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200467 s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900468 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900469 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900470
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900471 errchEapol := make(chan error)
472 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
473
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900474 errchDhcp := make(chan error)
475 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
476
Shad Ansarib744bf22019-01-17 11:36:46 -0800477 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900478 logger.Debug("runOMCIResponder Start")
479 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800480 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900481 case v, ok := <-errchOmci: // Wait for OmciInitialization
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200482 if ok { // Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700483 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900484 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900485 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800486 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900487 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900488 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900489 return nil
490 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900491
Shad Ansarib744bf22019-01-17 11:36:46 -0800492 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900493 logger.Debug("runEapolResponder Start")
494 defer logger.Debug("runEapolResponder Done")
495 select {
496 case v, ok := <-errchEapol:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200497 if ok { // Error
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900498 logger.Error("Error happend in Eapol:%s", v)
499 return v
500 }
501 case <-child.Done():
502 return nil
503 }
504 return nil
505 })
506
507 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900508 logger.Debug("runDhcpResponder Start")
509 defer logger.Debug("runDhcpResponder Done")
510 select {
511 case v, ok := <-errchDhcp:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200512 if ok { // Error
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900513 logger.Error("Error happend in Dhcp:%s", v)
514 return v
515 }
516 case <-child.Done():
517 return nil
518 }
519 return nil
520 })
521
522 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900523 err := s.runMainPktLoop(child, stream)
524 return err
525 })
526
527 if err := eg.Wait(); err != nil {
528 logger.Error("Error happend in runPacketLoops:%s", err)
529 cancel()
530 }
531 return nil
532}
533
534func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900535 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900536 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900537 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900538 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900539 ioinfo, err := s.IdentifyNniIoinfo("inside")
540 if err != nil {
541 return err
542 }
543 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
544 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800545 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900546 close(nnichannel)
547 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900548 logger.Debug("BEFORE OLT_ACTIVE")
549 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
550 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900551 data := &openolt.Indication_PktInd{}
552 for {
553 select {
554 case msg := <-s.omciIn:
555 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
556 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
557 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700558 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900559 continue
560 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700561 case msg := <-s.eapolIn:
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900562 intfid := msg.IntfId
563 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900564 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900565 if err != nil {
566 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
567 continue
568 }
569
Zack Williamsb85f5932019-05-10 16:21:35 -0700570 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 +0900571
572 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
573 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700574 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900575 return err
576 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200577 case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900578 intfid := msg.IntfId
579 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900580 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900581 bytes := msg.Byte
582 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
583
584 if err != nil {
585 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
586 continue
587 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900588
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200589 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900590 if err != nil {
591 logger.Error("Failed to GetOnuByID:%d", onuid)
592 continue
593 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900594 sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900595 if ctag, ok := s.CtagMap[sn]; ok == true {
596 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
597 if err != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900598 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900599 "gemId": gemid,
600 }).Error("Fail to tag C-tag")
601 } else {
602 pkt = tagpkt
603 }
604 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900605 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900606 "gemId": gemid,
607 "cTagMap": s.CtagMap,
608 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
609 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900610
Zack Williamsb85f5932019-05-10 16:21:35 -0700611 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 +0900612
613 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
614 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700615 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900616 return err
617 }
618
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900619 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900620 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900621 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
622 logger.Debug("WARNING: This packet does not come from NNI ")
623 continue
624 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200625
626 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200627 intfid := nnipkt.Info.intfid
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200628 onu, _ := s.GetOnuByID(onuid, intfid)
629
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900630 device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200631
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900632 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900633 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
634 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700635 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900636 return err
637 }
638
639 case <-ctx.Done():
640 logger.Debug("Closed nnichannel ")
641 return nil
642 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900643 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900644}
645
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900646func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
647 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200648 onu, err := s.GetOnuByID(onuid, intfid)
649 if err != nil {
650 logger.Error("Failed processing onuPacketOut: %v", err)
651 return err
652 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800653
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900654 if layerEth != nil {
655 pkt, _ := layerEth.(*layers.Ethernet)
656 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900657 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900658 device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700659 eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900660 s.eapolOut <- &eapolPkt
661 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900662 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900663 device.LoggerWithOnu(onu).WithFields(log.Fields{
Matteo Scandoloa286c742018-11-20 08:10:04 -0800664 "payload": layerDHCP.LayerPayload(),
665 "type": layerDHCP.LayerType().String(),
666 }).Info("Received downstream packet is DHCP.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700667 poppkt, _, err := PopVLAN(rawpkt)
668 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700669 logger.Warn("Received untagged packet when expecting single-tagged packet.")
670 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700671 } else {
672 // check to see if the packet was double-tagged
Scott Baker07fe4dc2019-08-05 16:36:37 -0700673 poppktAgain, _, err := PopVLAN(poppkt)
Scott Baker2b4ffb72019-08-02 16:20:47 -0700674 if err == nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700675 logger.Warn("Received double-tagged packet when expecting single-tagged packet.")
676 poppkt = poppktAgain
Scott Baker2b4ffb72019-08-02 16:20:47 -0700677 }
678 }
679 logger.Debug("%s", poppkt.Dump())
680 dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900681 s.dhcpOut <- &dhcpPkt
682 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900683 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900684 device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700685 // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900686 return nil
687 }
688 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
689 if err != nil {
690 return err
691 }
692 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800693 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900694 return nil
695 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900696 device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900697 return nil
698}
699
700func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900701 poppkt, _, err := PopVLAN(rawpkt)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900702 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700703 logger.Warn("Received untagged packet when expecting double-tagged packet.")
704 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700705 } else {
706 // check to see if the packet was double-tagged
707 poppktAgain, _, err := PopVLAN(poppkt)
708 if err == nil {
709 poppkt = poppktAgain
710 } else {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700711 logger.Warn("Received single-tagged packet when expecting double-tagged packet.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700712 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900713 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900714 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900715 if err != nil {
716 return err
717 }
718 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900719 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900720 SendNni(handle, poppkt)
721 return nil
722}
723
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200724// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900725func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
726 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900727 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900728 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900729 return false
730 }
731 }
732 }
733 return true
734}
735
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900736func (s *Server) isAllOnuOmciActive() bool {
737 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700738 for _, onu := range onus {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900739 if onu.GetIntState() != device.ONU_OMCIACTIVE {
740 return false
741 }
742 }
743 }
744 return true
745}
746
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900747func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900748 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
749 gemportid, err := omci.GetGemPortId(intfid, onuid)
750 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900751 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200752 onu, err := s.GetOnuByID(onuid, intfid)
753 if err != nil {
754 logger.Error("Failed to getGemPortID: %s", err)
755 return 0, err
756 }
757 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900758 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900759 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900760}
761
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900762func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
763 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900764 for _, onu := range onus {
765 if device.ValidateSN(*sn, *onu.SerialNumber) {
766 return onu, nil
767 }
768 }
769 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200770 err := errors.New("no matching serial number found")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900771 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900772 return nil, err
773}
774
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200775// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200776func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
777 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800778}
779
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200780func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
781 for _, onu := range onumap[intfid] {
782 if onu.OnuID == onuid {
783 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900784 }
785 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200786 err := errors.New("no matching OnuID found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800787 logger.WithFields(log.Fields{
788 "onumap": onumap,
789 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200790 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800791 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900792 return nil, err
793}
794
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200795// getOnuFromSNmap method returns onu object from SNmap if found
796func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) {
797 snkey := stringifySerialNumber(serialNumber)
798
799 logger.Debug("getOnuFromSNmap received serial number %s", snkey)
800
801 if onu, exist := s.SNmap.Load(snkey); exist {
802 logger.Info("Serial number found in map")
803 return onu.(*device.Onu), true
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900804 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200805 logger.Info("Serial number not found in map")
806 return nil, false
807}
808
809func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900810 return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200811}
812
813func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
814 if len(SerialNumber) != SerialNumberLength {
815 logger.Error("Invalid serial number %s", SerialNumber)
816 return nil, errors.New("invalid serial number")
817 }
818 // First four characters are vendorId
819 vendorID := SerialNumber[:VendorIDLength]
820 vendorSpecific := SerialNumber[VendorIDLength:]
821
822 vsbyte, _ := hex.DecodeString(vendorSpecific)
823
824 // Convert to Openolt serial number
825 serialNum := new(openolt.SerialNumber)
826 serialNum.VendorId = []byte(vendorID)
827 serialNum.VendorSpecific = vsbyte
828
829 return serialNum, nil
830}
831
832// TODO move to device_onu.go
833func (s *Server) sendOnuIndicationsOnOltReboot() {
834 if AutoONUActivate == 1 {
835 // For auto activate mode, onu indications is sent in Enable()
836 return
837 }
838
839 s.SNmap.Range(
840 func(key, value interface{}) bool {
841 onu := value.(*device.Onu)
842 if onu.InternalState == device.ONU_LOS_RAISED {
843 return true
844 }
845
846 err := sendOnuDiscInd(*s.EnableServer, onu)
847 if err != nil {
848 logger.Error(err.Error())
849 }
850
851 return true
852 })
853}
854
855// StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received
856func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) {
857 for {
858 select {
859 case Req := <-s.serverActionCh:
860 logger.Debug("Request Received On serverActionCh: %+v", Req)
861 switch Req {
862 case "start":
863 logger.Debug("Server Start Request Received On ServerActionChannel")
864 go s.Start() // blocking
865 case "stop":
866 logger.Debug("Server Stop Request Received On ServerActionChannel")
867 s.Stop()
868 default:
869 logger.Error("Invalid value received in deviceActionCh")
870 }
871 }
872 }
873}
874
875// startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions
876// TODO all onu and olt related actions (like alarms) should be handled using this function
877func (s *Server) startDeviceActionLoop() {
878 logger.Debug("startDeviceActionLoop invoked")
879 s.deviceActionCh = make(chan *pb.DeviceAction, 10)
880 for {
881 logger.Debug("Action channel loop started")
882 select {
883 case Req := <-s.deviceActionCh:
884 logger.Debug("Reboot Action Type: %+v", Req.DeviceAction)
885 switch Req.DeviceType {
886 case DeviceTypeOnu:
887 value, _ := s.SNmap.Load(Req.DeviceSerialNumber)
888 onu := value.(*device.Onu)
889 if Req.DeviceAction == SoftReboot {
890 s.handleONUSoftReboot(onu.IntfID, onu.OnuID)
891 } else if Req.DeviceAction == HardReboot {
892 s.handleONUHardReboot(onu)
893 }
894 case DeviceTypeOlt:
895 logger.Debug("Reboot For OLT Received")
896 s.handleOLTReboot()
897 default:
898 logger.Error("Invalid value received in deviceActionCh")
899 }
900 }
901 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700902}