blob: 64bd6c7fed76b0a3325524f83765341296ca8504 [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"
Zdravko Bozakov078a2712019-07-19 23:25:15 +020032 api "github.com/opencord/voltha-bbsim/api"
Zack Williams2abf3932019-08-05 14:07:05 -070033 "github.com/opencord/voltha-bbsim/common/logger"
34 "github.com/opencord/voltha-bbsim/device"
35 flowHandler "github.com/opencord/voltha-bbsim/flow"
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -040036 openolt "github.com/opencord/voltha-protos/go/openolt"
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"
Zdravko Bozakov078a2712019-07-19 23:25:15 +020052 AutoDiscoveryDelay = 5
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053)
54
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020055// Server structure consists of all the params required for BBsim.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056type Server struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020057 wg *sync.WaitGroup
58 Olt *device.Olt
59 Onumap map[uint32][]*device.Onu
60 SNmap sync.Map
61 AutoONUActivate bool
62 Ioinfos []*Ioinfo
63 gRPCserver *grpc.Server
64 gRPCAddress string
65 gRPCPort uint32
66 mgmtServer *grpc.Server
67 mgmtGrpcPort uint32
68 mgmtRestPort uint32
69 Vethnames []string
Mahir Gunyel32dfd722019-08-05 16:18:06 +030070 IndInterval time.Duration
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020071 Processes []string
72 EnableServer *openolt.Openolt_EnableIndicationServer
73 CtagMap map[string]uint32
74 cancel context.CancelFunc
75 stateRepCh chan stateReport
76 omciIn chan openolt.OmciIndication
77 omciOut chan openolt.OmciMsg
78 eapolIn chan *byteMsg
79 eapolOut chan *byteMsg
80 dhcpIn chan *byteMsg
81 dhcpOut chan *byteMsg
Zdravko Bozakov078a2712019-07-19 23:25:15 +020082 FlowMap map[device.FlowKey]*openolt.Flow
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020083 alarmCh chan *openolt.Indication
Zdravko Bozakov078a2712019-07-19 23:25:15 +020084 deviceActionCh chan *api.DeviceAction
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020085 serverActionCh chan string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090086}
87
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020088// Packet structure
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090089type Packet struct {
90 Info *Ioinfo
91 Pkt gopacket.Packet
92}
93
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090094type byteMsg struct {
Zdravko Bozakov078a2712019-07-19 23:25:15 +020095 IntfID uint32
96 OnuID uint32
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070097 Byte []byte
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090098}
99
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900100type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -0800101 device device.Device
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200102 current device.State
103 next device.State
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900104}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900105
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200106// NewCore initialize OLT and ONU objects
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200107func NewCore(opt *Option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900108 // TODO: make it decent
109 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -0800110 npon := opt.npon
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200111 if npon > MaxPonPorts {
112 logger.Warn("Provided number of PON ports exceeds limit of %d", MaxPonPorts)
113 logger.Info("Setting number of PON ports to %d", MaxPonPorts)
114 npon = MaxPonPorts
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200116 nonus := opt.nonus
117 if nonus > MaxOnusPerPon {
118 logger.Warn("Provided number of ONUs per PON port exceeds limit of %d", MaxOnusPerPon)
119 logger.Info("Setting number of ONUs per PON port to %d", MaxOnusPerPon)
120 nonus = MaxOnusPerPon
121 }
122 s := Server{
123 Olt: device.NewOlt(oltid, npon, 1), // TODO nnni is to be taken from options
124 Onumap: make(map[uint32][]*device.Onu),
125 Ioinfos: []*Ioinfo{},
126 gRPCAddress: opt.address,
127 gRPCPort: opt.port,
128 Vethnames: []string{},
129 IndInterval: opt.intvl,
130 AutoONUActivate: !opt.interactiveOnuActivation,
131 Processes: []string{},
132 mgmtGrpcPort: opt.mgmtGrpcPort,
133 mgmtRestPort: opt.mgmtRestPort,
134 EnableServer: nil,
135 stateRepCh: make(chan stateReport, 8),
136 omciIn: make(chan openolt.OmciIndication, 1024),
137 omciOut: make(chan openolt.OmciMsg, 1024),
138 eapolIn: make(chan *byteMsg, 1024),
139 eapolOut: make(chan *byteMsg, 1024),
140 dhcpIn: make(chan *byteMsg, 1024),
141 dhcpOut: make(chan *byteMsg, 1024),
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200142 FlowMap: make(map[device.FlowKey]*openolt.Flow),
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200143 serverActionCh: make(chan string),
144 }
145 logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200146 logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber)
147 // Creating Onu Map
148 for intfid := uint32(0); intfid < npon; intfid++ {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200149 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900150 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900151
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200152 logger.Debug("Onu Map:")
153 for _, onus := range s.Onumap {
154 for _, onu := range onus {
155 logger.Debug("%+v", *onu)
156 }
157 }
158
159 // TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900160 s.CtagMap = make(map[string]uint32)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200161 for i := 0; i < MaxOnusPerPon; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900162 oltid := s.Olt.ID
163 intfid := uint32(1)
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900164 sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700165 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900166 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200167
168 flowHandler.InitializeFlowManager(s.Olt.ID)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900169 return &s
170}
171
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200172// Start starts the openolt gRPC server (blocking)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800173func (s *Server) Start() error {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200174 logger.Debug("Starting OpenOLT gRPC Server")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800175 defer func() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200176 logger.Debug("OpenOLT gRPC Server Stopped")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200178
179 // Start Openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900180 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
181 listener, gserver, err := NewGrpcServer(addressport)
182 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700183 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184 return err
185 }
186 s.gRPCserver = gserver
187 openolt.RegisterOpenoltServer(gserver, s)
188 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700189 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900190 return err
191 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 return nil
193}
194
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200195// Stop stops the openolt gRPC servers (non-blocking).
Matteo Scandolo88e91892018-11-06 16:29:19 -0800196func (s *Server) Stop() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200197 logger.Debug("Stopping OpenOLT gRPC Server & PktLoops")
198 defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped")
199
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 if s.gRPCserver != nil {
201 s.gRPCserver.Stop()
202 logger.Debug("gRPCserver.Stop()")
203 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200204
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900205 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900206 return
207}
208
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200209func (s *Server) startMgmtServer(wg *sync.WaitGroup) {
210 defer logger.Debug("Management api server exited")
211
212 grpcAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
213 restAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtRestPort))
214 // Start rest gateway for BBSim server
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200215 go StartRestGatewayService(grpcAddressPort, restAddressPort)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200216 addressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
217
218 listener, apiserver, err := NewMgmtAPIServer(addressPort)
219 if err != nil {
220 logger.Error("Unable to create management api server %v", err)
221 return
222 }
223
224 s.mgmtServer = apiserver
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200225 api.RegisterBBSimServiceServer(apiserver, s)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200226 if e := apiserver.Serve(listener); e != nil {
227 logger.Error("Failed to run management api server %v", e)
228 return
229 }
230
231}
232
233func (s *Server) stopMgmtServer() error {
234 if s.mgmtServer != nil {
235 s.mgmtServer.GracefulStop()
236 logger.Debug("Management server stopped")
237 return nil
238 }
239 return errors.New("can not stop management server, server not created")
240}
241
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200242// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900243func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900244 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800245 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900246 olt.Initialize()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200247 // Below lines commented as we dont want to change the onu state on restart
248 // for intfid := range s.Onumap {
249 // for _, onu := range s.Onumap[intfid] {
250 // onu.Initialize()
251 // }
252 // }
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200253 s.updateDevIntState(olt, device.OltInactive)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900254 logger.Debug("Enable() Done")
255 }()
256 logger.Debug("Enable() Start")
257 s.EnableServer = sv
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200258 flowHandler.InitializePacketInStream(*sv)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900259 if err := s.activateOLT(*sv); err != nil {
260 return err
261 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700262
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200263 s.updateDevIntState(olt, device.OltPreactive)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900264 coreCtx := context.Background()
265 coreCtx, corecancel := context.WithCancel(coreCtx)
266 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700267
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200268 errorchan := make(chan error, 5)
269 go s.StartPktLoops(coreCtx, *sv, errorchan)
270
271 if s.AutoONUActivate == true {
272 // Initialize all ONUs
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200273 // Allow some delay for OLT to become active at the VOLTHA side, before sending ONU discovery indication.
274 // Otherwise, ONU discovery indication may get processed at the VOLTHA before OLT state becomes active.
275 time.Sleep(AutoDiscoveryDelay * time.Second)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200276 for intfid := range s.Onumap {
277 for _, onu := range s.Onumap[intfid] {
278 onu.Initialize()
279 }
280 }
281 // Activate all ONUs
282 s.activateONUs(*sv, s.Onumap)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900283 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200284
285 select {
286 case err := <-errorchan:
287 if err != nil {
288 logger.Debug("Error: %v", err)
289 return err
290 }
291 }
292
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900293 return nil
294}
295
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200296// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900297func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800298 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900299 logger.Debug("Disable() Done")
300 }()
301 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900302 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303}
304
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200305func (s *Server) updateDevIntState(dev device.Device, state device.State) {
306 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900307 current := dev.GetIntState()
308 dev.UpdateIntState(state)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700309 logger.Debug("updateDevIntState called state: current %s, next %s", device.ONUState[current], device.ONUState[dev.GetIntState()])
Shad Ansarib744bf22019-01-17 11:36:46 -0800310 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
311 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700312 logger.Debug("OLT State updated to:%s", device.ONUState[state])
Shad Ansarib744bf22019-01-17 11:36:46 -0800313 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700314 logger.Debug("ONU State updated to:%s", device.ONUState[state])
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900315 } else {
316 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
317 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900318}
319
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200320func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.State) error {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200321 onu, err := s.GetOnuByID(onuid, intfid)
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700322
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900323 if err != nil {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700324 logger.Warn("Failed to get ONU %d: %v", onuid, err)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900325 return err
326 }
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700327
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -0400328 if state == onu.GetIntState() {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700329 logger.WithFields(log.Fields{
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -0400330 "toState": device.ONUState[state],
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700331 "currentState": device.ONUState[onu.GetIntState()],
332 }).Warn("Trying to update the state with the same state, ignoring this request")
333 return nil
334 }
335
336 logger.Debug("Transitioning ONU state from %s to %s", device.ONUState[onu.GetIntState()], device.ONUState[state])
337
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900338 s.updateDevIntState(onu, state)
339 return nil
340}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200341
342func (s *Server) activateOnu(onu *device.Onu) {
343 snKey := stringifySerialNumber(onu.SerialNumber)
344 s.SNmap.Store(snKey, onu)
345 device.UpdateOnusOpStatus(onu.IntfID, onu, "up")
346
347 err := sendOnuDiscInd(*s.EnableServer, onu)
348 if err != nil {
349 logger.Error(err.Error())
350 return
351 }
352 logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID)
353 logger.Debug("activateONUs Entry in SNmap %v", snKey)
354}
355
356func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) {
357 // Add all ONUs to SerialNumber Map
358 for intfid := range Onumap {
359 for _, onu := range Onumap[intfid] {
360 s.activateOnu(onu)
Mahir Gunyel32dfd722019-08-05 16:18:06 +0300361 if s.IndInterval > 0 {
362 time.Sleep(s.IndInterval)
363 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200364 }
365 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700366}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900367
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900368func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900369 defer logger.Debug("activateOLT() Done")
370 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900371 // Activate OLT
372 olt := s.Olt
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200373 olt.OperState = "up"
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900374 if err := sendOltIndUp(stream, olt); err != nil {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200375 olt.OperState = "down"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900376 return err
377 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800378 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900379
380 // OLT sends Interface Indication to Adapter
381 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800382 logger.Error("Fail to sendIntfInd: %v", err)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200383 olt.OperState = "down"
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)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200391 olt.OperState = "down"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900392 return err
393 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800394 logger.Info("OLT %s sent OperInd.", olt.Name)
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700395 return nil
396}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900397
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200398// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200399func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900400 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800401 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900402 RemoveVeths(s.Vethnames)
403 s.Vethnames = []string{}
404 s.Ioinfos = []*Ioinfo{}
405 s.wg.Done()
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200406 s.updateDevIntState(s.Olt, device.OltPreactive)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900407 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900408 }()
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200409 go s.sendPortStats()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200410 s.alarmCh = make(chan *openolt.Indication, 10)
411 go startAlarmLoop(stream, s.alarmCh)
412 go s.startDeviceActionLoop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900413 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900414 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900415 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700416 logger.Error("createIoinfos failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200417 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900418 }
419 s.Ioinfos = ioinfos
420 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700421 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900422
423 parent := ctx
424 child, cancel := context.WithCancel(parent)
425 s.cancel = cancel
426
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900427 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700428 logger.Error("runPktLoops failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200429 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900430 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200431 errorchan <- nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900432}
433
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200434// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900435func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900436 if s.cancel != nil {
437 cancel := s.cancel
438 cancel()
439 }
440}
441
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900442func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200443 var ioinfos []*Ioinfo
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900444 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900445 var handler *pcap.Handle
446 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900447 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700448 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900449 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900450 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900451
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900452 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900453 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900454 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900455 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900456 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900457}
458
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200459// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900460func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
461 logger.Debug("runPacketPktLoops Start")
462 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900463
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900464 errchOmci := make(chan error)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200465 s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900466 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900467 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900468
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900469 errchEapol := make(chan error)
470 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
471
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900472 errchDhcp := make(chan error)
473 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
474
Shad Ansarib744bf22019-01-17 11:36:46 -0800475 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900476 logger.Debug("runOMCIResponder Start")
477 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800478 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900479 case v, ok := <-errchOmci: // Wait for OmciInitialization
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200480 if ok { // Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700481 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900482 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900483 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800484 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900485 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900486 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900487 return nil
488 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900489
Shad Ansarib744bf22019-01-17 11:36:46 -0800490 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900491 logger.Debug("runEapolResponder Start")
492 defer logger.Debug("runEapolResponder Done")
493 select {
494 case v, ok := <-errchEapol:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200495 if ok { // Error
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900496 logger.Error("Error happend in Eapol:%s", v)
497 return v
498 }
499 case <-child.Done():
500 return nil
501 }
502 return nil
503 })
504
505 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900506 logger.Debug("runDhcpResponder Start")
507 defer logger.Debug("runDhcpResponder Done")
508 select {
509 case v, ok := <-errchDhcp:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200510 if ok { // Error
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900511 logger.Error("Error happend in Dhcp:%s", v)
512 return v
513 }
514 case <-child.Done():
515 return nil
516 }
517 return nil
518 })
519
520 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900521 err := s.runMainPktLoop(child, stream)
522 return err
523 })
524
525 if err := eg.Wait(); err != nil {
526 logger.Error("Error happend in runPacketLoops:%s", err)
527 cancel()
528 }
529 return nil
530}
531
532func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900533 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900534 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900535 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900536 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900537 ioinfo, err := s.IdentifyNniIoinfo("inside")
538 if err != nil {
539 return err
540 }
541 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
542 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800543 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900544 close(nnichannel)
545 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900546 logger.Debug("BEFORE OLT_ACTIVE")
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200547 s.updateDevIntState(s.Olt, device.OltActive)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900548 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900549 data := &openolt.Indication_PktInd{}
550 for {
551 select {
552 case msg := <-s.omciIn:
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200553 logger.Debug("OLT %d send omciInd indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
554 omciInd := &openolt.Indication_OmciInd{OmciInd: &msg}
555 if err := stream.Send(&openolt.Indication{Data: omciInd}); err != nil {
556 logger.Error("send omciInd indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900557 continue
558 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700559 case msg := <-s.eapolIn:
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200560 intfid := msg.IntfID
561 onuid := msg.OnuID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900562 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900563 if err != nil {
564 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
565 continue
566 }
567
Zack Williamsb85f5932019-05-10 16:21:35 -0700568 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 +0900569
570 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
571 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700572 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900573 return err
574 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200575 case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200576 intfid := msg.IntfID
577 onuid := msg.OnuID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900578 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900579 bytes := msg.Byte
580 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
581
582 if err != nil {
583 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
584 continue
585 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900586
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200587 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900588 if err != nil {
589 logger.Error("Failed to GetOnuByID:%d", onuid)
590 continue
591 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900592 sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900593 if ctag, ok := s.CtagMap[sn]; ok == true {
594 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
595 if err != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900596 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900597 "gemId": gemid,
598 }).Error("Fail to tag C-tag")
599 } else {
600 pkt = tagpkt
601 }
602 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900603 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900604 "gemId": gemid,
605 "cTagMap": s.CtagMap,
606 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
607 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900608
Zack Williamsb85f5932019-05-10 16:21:35 -0700609 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 +0900610
611 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
612 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700613 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900614 return err
615 }
616
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900617 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900618 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900619 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
620 logger.Debug("WARNING: This packet does not come from NNI ")
621 continue
622 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200623
624 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200625 intfid := nnipkt.Info.intfid
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200626 onu, _ := s.GetOnuByID(onuid, intfid)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200627 pkt := nnipkt.Pkt
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200628
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900629 device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200630
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900631 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200632 if err = stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700633 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900634 return err
635 }
636
637 case <-ctx.Done():
638 logger.Debug("Closed nnichannel ")
639 return nil
640 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900641 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900642}
643
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900644func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
645 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200646 onu, err := s.GetOnuByID(onuid, intfid)
647 if err != nil {
648 logger.Error("Failed processing onuPacketOut: %v", err)
649 return err
650 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800651
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900652 if layerEth != nil {
653 pkt, _ := layerEth.(*layers.Ethernet)
654 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900655 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900656 device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200657 eapolPkt := byteMsg{IntfID: intfid, OnuID: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900658 s.eapolOut <- &eapolPkt
659 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900660 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900661 device.LoggerWithOnu(onu).WithFields(log.Fields{
Matteo Scandoloa286c742018-11-20 08:10:04 -0800662 "payload": layerDHCP.LayerPayload(),
663 "type": layerDHCP.LayerType().String(),
664 }).Info("Received downstream packet is DHCP.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700665 poppkt, _, err := PopVLAN(rawpkt)
666 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700667 logger.Warn("Received untagged packet when expecting single-tagged packet.")
668 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700669 } else {
670 // check to see if the packet was double-tagged
Scott Baker07fe4dc2019-08-05 16:36:37 -0700671 poppktAgain, _, err := PopVLAN(poppkt)
Scott Baker2b4ffb72019-08-02 16:20:47 -0700672 if err == nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700673 logger.Warn("Received double-tagged packet when expecting single-tagged packet.")
674 poppkt = poppktAgain
Scott Baker2b4ffb72019-08-02 16:20:47 -0700675 }
676 }
677 logger.Debug("%s", poppkt.Dump())
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200678 dhcpPkt := byteMsg{IntfID: intfid, OnuID: onuid, Byte: poppkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900679 s.dhcpOut <- &dhcpPkt
680 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900681 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900682 device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700683 // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900684 return nil
685 }
686 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
687 if err != nil {
688 return err
689 }
690 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800691 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900692 return nil
693 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900694 device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900695 return nil
696}
697
698func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900699 poppkt, _, err := PopVLAN(rawpkt)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900700 if err != nil {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700701 logger.Warn("Received untagged packet when expecting double-tagged packet.")
702 poppkt = rawpkt
Scott Baker2b4ffb72019-08-02 16:20:47 -0700703 } else {
704 // check to see if the packet was double-tagged
705 poppktAgain, _, err := PopVLAN(poppkt)
706 if err == nil {
707 poppkt = poppktAgain
708 } else {
Scott Baker07fe4dc2019-08-05 16:36:37 -0700709 logger.Warn("Received single-tagged packet when expecting double-tagged packet.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700710 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900711 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900712 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900713 if err != nil {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200714 logger.Error("failed to get ioinfo")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900715 return err
716 }
717 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900718 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900719 SendNni(handle, poppkt)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200720 // Send packet to nni port
721 if err := flowHandler.PacketOut(poppkt, "nni", ioinfo.intfid); err != nil {
722 logger.Error("Error in sending packet to nni port")
723 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900724 return nil
725}
726
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200727// isAllOnuOmciActive checks for OnuOmciActive state for all the onus in the map
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900728func (s *Server) isAllOnuOmciActive() bool {
729 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700730 for _, onu := range onus {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200731 if onu.GetIntState() != device.OnuOmciActive {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900732 return false
733 }
734 }
735 }
736 return true
737}
738
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900739func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900740 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
741 gemportid, err := omci.GetGemPortId(intfid, onuid)
742 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900743 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200744 onu, err := s.GetOnuByID(onuid, intfid)
745 if err != nil {
746 logger.Error("Failed to getGemPortID: %s", err)
747 return 0, err
748 }
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200749 for _, gemports := range onu.GemPortMap {
750 return gemports[0], nil
751 }
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900752 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900753 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900754}
755
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900756func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
757 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900758 for _, onu := range onus {
759 if device.ValidateSN(*sn, *onu.SerialNumber) {
760 return onu, nil
761 }
762 }
763 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200764 err := errors.New("no matching serial number found")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900765 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900766 return nil, err
767}
768
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200769// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200770func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
771 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800772}
773
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200774func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
775 for _, onu := range onumap[intfid] {
776 if onu.OnuID == onuid {
777 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900778 }
779 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200780 err := errors.New("no matching OnuID found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800781 logger.WithFields(log.Fields{
782 "onumap": onumap,
783 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200784 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800785 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900786 return nil, err
787}
788
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200789// getOnuFromSNmap method returns onu object from SNmap if found
790func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) {
791 snkey := stringifySerialNumber(serialNumber)
792
793 logger.Debug("getOnuFromSNmap received serial number %s", snkey)
794
795 if onu, exist := s.SNmap.Load(snkey); exist {
796 logger.Info("Serial number found in map")
797 return onu.(*device.Onu), true
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900798 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200799 logger.Info("Serial number not found in map")
800 return nil, false
801}
802
803func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900804 return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200805}
806
807func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
808 if len(SerialNumber) != SerialNumberLength {
809 logger.Error("Invalid serial number %s", SerialNumber)
810 return nil, errors.New("invalid serial number")
811 }
812 // First four characters are vendorId
813 vendorID := SerialNumber[:VendorIDLength]
814 vendorSpecific := SerialNumber[VendorIDLength:]
815
816 vsbyte, _ := hex.DecodeString(vendorSpecific)
817
818 // Convert to Openolt serial number
819 serialNum := new(openolt.SerialNumber)
820 serialNum.VendorId = []byte(vendorID)
821 serialNum.VendorSpecific = vsbyte
822
823 return serialNum, nil
824}
825
826// TODO move to device_onu.go
827func (s *Server) sendOnuIndicationsOnOltReboot() {
828 if AutoONUActivate == 1 {
829 // For auto activate mode, onu indications is sent in Enable()
830 return
831 }
832
833 s.SNmap.Range(
834 func(key, value interface{}) bool {
835 onu := value.(*device.Onu)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200836 if onu.InternalState == device.OnuLosRaised {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200837 return true
838 }
839
840 err := sendOnuDiscInd(*s.EnableServer, onu)
841 if err != nil {
842 logger.Error(err.Error())
843 }
844
845 return true
846 })
847}
848
849// StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received
850func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) {
851 for {
852 select {
853 case Req := <-s.serverActionCh:
854 logger.Debug("Request Received On serverActionCh: %+v", Req)
855 switch Req {
856 case "start":
857 logger.Debug("Server Start Request Received On ServerActionChannel")
858 go s.Start() // blocking
859 case "stop":
860 logger.Debug("Server Stop Request Received On ServerActionChannel")
861 s.Stop()
862 default:
863 logger.Error("Invalid value received in deviceActionCh")
864 }
865 }
866 }
867}
868
869// startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions
870// TODO all onu and olt related actions (like alarms) should be handled using this function
871func (s *Server) startDeviceActionLoop() {
872 logger.Debug("startDeviceActionLoop invoked")
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200873 s.deviceActionCh = make(chan *api.DeviceAction, 10)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200874 for {
875 logger.Debug("Action channel loop started")
876 select {
877 case Req := <-s.deviceActionCh:
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200878 logger.Debug("Reboot Action Type: %+v", Req.Action)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200879 switch Req.DeviceType {
880 case DeviceTypeOnu:
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200881 value, _ := s.SNmap.Load(Req.SerialNumber)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200882 onu := value.(*device.Onu)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200883 if Req.Action == SoftReboot {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200884 s.handleONUSoftReboot(onu.IntfID, onu.OnuID)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200885 } else if Req.Action == HardReboot {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200886 s.handleONUHardReboot(onu)
887 }
888 case DeviceTypeOlt:
889 logger.Debug("Reboot For OLT Received")
890 s.handleOLTReboot()
891 default:
892 logger.Error("Invalid value received in deviceActionCh")
893 }
894 }
895 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700896}
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200897
898func (s *Server) sendPortStats() {
899 for {
900 for i, port := range s.Olt.NniIntfs {
901 // send nni port stats
902 logger.Debug("Sending port stats for NNI %d", port.IntfID)
903 err := sendPortStats(*s.EnableServer, &s.Olt.NniIntfs[i])
904 if err != nil {
905 logger.Error("Failed to send port stats for NNI %d", port.IntfID)
906 }
907 }
908
909 for i, port := range s.Olt.PonIntfs {
910 // send pon port stats
911 logger.Debug("Sending port stats for PON %d", port.IntfID)
912 err := sendPortStats(*s.EnableServer, &s.Olt.PonIntfs[i])
913 if err != nil {
914 logger.Error("Failed to send port stats for PON %d", port.IntfID)
915 }
916 }
917
918 time.Sleep(10 * time.Second)
919 }
920}