Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 20 | "context" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 21 | "encoding/hex" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 22 | "errors" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 23 | "reflect" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 24 | "strconv" |
| 25 | "sync" |
Mahir Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 26 | "time" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 27 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 28 | "github.com/google/gopacket" |
| 29 | "github.com/google/gopacket/layers" |
| 30 | "github.com/google/gopacket/pcap" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | omci "github.com/opencord/omci-sim" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 32 | 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" |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 36 | openolt "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 37 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 38 | "golang.org/x/sync/errgroup" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 39 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 40 | ) |
| 41 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 42 | // Constants |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 43 | const ( |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 44 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 52 | ) |
| 53 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 54 | // Server structure consists of all the params required for BBsim. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 55 | type Server struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 56 | 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 Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 69 | IndInterval time.Duration |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 70 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 87 | // Packet structure |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 88 | type Packet struct { |
| 89 | Info *Ioinfo |
| 90 | Pkt gopacket.Packet |
| 91 | } |
| 92 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 93 | type byteMsg struct { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 94 | IntfId uint32 |
| 95 | OnuId uint32 |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 96 | Byte []byte |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 99 | type stateReport struct { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 100 | device device.Device |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 101 | current device.DeviceState |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 102 | next device.DeviceState |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 103 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 104 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 105 | // FlowKey used for FlowMap key |
| 106 | type FlowKey struct { |
| 107 | FlowID uint32 |
| 108 | FlowDirection string |
| 109 | } |
| 110 | |
Anjali Thontakudi | 6665920 | 2019-07-03 23:14:02 +0000 | [diff] [blame] | 111 | //Has options (OLT id, number onu ports) from mediator |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 112 | // NewCore initialize OLT and ONU objects |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 113 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | // TODO: make it decent |
| 115 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 116 | npon := opt.npon |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 117 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 121 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 122 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 152 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 153 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 154 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 155 | logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber) |
| 156 | // Creating Onu Map |
| 157 | for intfid := uint32(0); intfid < npon; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 158 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 159 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 160 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 161 | 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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 169 | s.CtagMap = make(map[string]uint32) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 170 | for i := 0; i < MaxOnusPerPon; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 171 | oltid := s.Olt.ID |
| 172 | intfid := uint32(1) |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 173 | sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 174 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 175 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 176 | |
| 177 | flowHandler.InitializeFlowManager(s.Olt.ID) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 178 | return &s |
| 179 | } |
| 180 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 181 | // Start starts the openolt gRPC server (blocking) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 182 | func (s *Server) Start() error { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 183 | logger.Debug("Starting OpenOLT gRPC Server") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 184 | defer func() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 185 | logger.Debug("OpenOLT gRPC Server Stopped") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 186 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 187 | |
| 188 | // Start Openolt gRPC server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 189 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 190 | listener, gserver, err := NewGrpcServer(addressport) |
| 191 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 192 | logger.Error("Failed to create gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | return err |
| 194 | } |
| 195 | s.gRPCserver = gserver |
| 196 | openolt.RegisterOpenoltServer(gserver, s) |
| 197 | if err := gserver.Serve(listener); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 198 | logger.Error("Failed to run gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 199 | return err |
| 200 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 201 | return nil |
| 202 | } |
| 203 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 204 | // Stop stops the openolt gRPC servers (non-blocking). |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 205 | func (s *Server) Stop() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 206 | logger.Debug("Stopping OpenOLT gRPC Server & PktLoops") |
| 207 | defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped") |
| 208 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 209 | if s.gRPCserver != nil { |
| 210 | s.gRPCserver.Stop() |
| 211 | logger.Debug("gRPCserver.Stop()") |
| 212 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 213 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 214 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 215 | return |
| 216 | } |
| 217 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 218 | func (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 | |
| 242 | func (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 Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 251 | // Enable invokes methods for activation of OLT and ONU (blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 252 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 253 | olt := s.Olt |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 254 | defer func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 255 | olt.Initialize() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 256 | // 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 NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 262 | s.updateDevIntState(olt, device.OLT_INACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 263 | 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 Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 270 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 271 | s.updateDevIntState(olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 272 | |
| 273 | coreCtx := context.Background() |
| 274 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 275 | s.cancel = corecancel |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 276 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 277 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 289 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 290 | |
| 291 | select { |
| 292 | case err := <-errorchan: |
| 293 | if err != nil { |
| 294 | logger.Debug("Error: %v", err) |
| 295 | return err |
| 296 | } |
| 297 | } |
| 298 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 299 | return nil |
| 300 | } |
| 301 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 302 | // Disable stops packet loops (non-blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 303 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 304 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 305 | logger.Debug("Disable() Done") |
| 306 | }() |
| 307 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 308 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 309 | } |
| 310 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 311 | func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) { |
| 312 | current := dev.GetIntState() |
| 313 | dev.UpdateIntState(state) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 314 | logger.Debug("updateDevIntState called state: current %s, next %s", device.ONUState[current], device.ONUState[dev.GetIntState()]) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 315 | s.stateRepCh <- stateReport{device: dev, current: current, next: state} |
| 316 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 317 | logger.Debug("OLT State updated to:%s", device.ONUState[state]) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 318 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 319 | logger.Debug("ONU State updated to:%s", device.ONUState[state]) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 320 | } else { |
| 321 | logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev)) |
| 322 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 323 | } |
| 324 | |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 325 | func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 326 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 327 | onu, err := s.GetOnuByID(onuid, intfid) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 328 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 329 | if err != nil { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 330 | logger.Warn("Failed to get ONU %d: %v", onuid, err) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 331 | return err |
| 332 | } |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 333 | |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 334 | if state == onu.GetIntState() { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 335 | logger.WithFields(log.Fields{ |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 336 | "toState": device.ONUState[state], |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 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 NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 344 | s.updateDevIntState(onu, state) |
| 345 | return nil |
| 346 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 347 | |
| 348 | func (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 | |
| 362 | func (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 Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 367 | if s.IndInterval > 0 { |
| 368 | time.Sleep(s.IndInterval) |
| 369 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 370 | } |
| 371 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 372 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 373 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 374 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 375 | defer logger.Debug("activateOLT() Done") |
| 376 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 377 | // Activate OLT |
| 378 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 379 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 380 | return err |
| 381 | } |
| 382 | olt.OperState = "up" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 383 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 384 | |
| 385 | // OLT sends Interface Indication to Adapter |
| 386 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 387 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 388 | return err |
| 389 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 390 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 391 | |
| 392 | // OLT sends Operation Indication to Adapter after activating each interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 393 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 394 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 395 | return err |
| 396 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 397 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 398 | return nil |
| 399 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 400 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 401 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 402 | func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 403 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 404 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 405 | RemoveVeths(s.Vethnames) |
| 406 | s.Vethnames = []string{} |
| 407 | s.Ioinfos = []*Ioinfo{} |
| 408 | s.wg.Done() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 409 | s.updateDevIntState(s.Olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 410 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 411 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 412 | s.alarmCh = make(chan *openolt.Indication, 10) |
| 413 | go startAlarmLoop(stream, s.alarmCh) |
| 414 | go s.startDeviceActionLoop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 415 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 416 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 417 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 418 | logger.Error("createIoinfos failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 419 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 420 | } |
| 421 | s.Ioinfos = ioinfos |
| 422 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 423 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 424 | |
| 425 | parent := ctx |
| 426 | child, cancel := context.WithCancel(parent) |
| 427 | s.cancel = cancel |
| 428 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 429 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 430 | logger.Error("runPktLoops failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 431 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 432 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 433 | errorchan <- nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 434 | } |
| 435 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 436 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 437 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 438 | if s.cancel != nil { |
| 439 | cancel := s.cancel |
| 440 | cancel() |
| 441 | } |
| 442 | } |
| 443 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 444 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 445 | ioinfos := []*Ioinfo{} |
| 446 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 447 | var handler *pcap.Handle |
| 448 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 449 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 450 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 451 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 452 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 453 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 454 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 455 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 456 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 457 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 458 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 459 | } |
| 460 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 461 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 462 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 463 | logger.Debug("runPacketPktLoops Start") |
| 464 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 465 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 466 | errchOmci := make(chan error) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 467 | s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 468 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 469 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 470 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 471 | errchEapol := make(chan error) |
| 472 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 473 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 474 | errchDhcp := make(chan error) |
| 475 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 476 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 477 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 478 | logger.Debug("runOMCIResponder Start") |
| 479 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 480 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 481 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 482 | if ok { // Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 483 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 484 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 485 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 486 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 487 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 488 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 489 | return nil |
| 490 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 491 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 492 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 493 | logger.Debug("runEapolResponder Start") |
| 494 | defer logger.Debug("runEapolResponder Done") |
| 495 | select { |
| 496 | case v, ok := <-errchEapol: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 497 | if ok { // Error |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 498 | 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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 508 | logger.Debug("runDhcpResponder Start") |
| 509 | defer logger.Debug("runDhcpResponder Done") |
| 510 | select { |
| 511 | case v, ok := <-errchDhcp: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 512 | if ok { // Error |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 513 | 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 NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 523 | 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 | |
| 534 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 535 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 536 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 537 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 538 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 539 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 545 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 546 | close(nnichannel) |
| 547 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 548 | logger.Debug("BEFORE OLT_ACTIVE") |
| 549 | s.updateDevIntState(s.Olt, device.OLT_ACTIVE) |
| 550 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 551 | 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 558 | logger.Error("send omci indication failed: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 559 | continue |
| 560 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 561 | case msg := <-s.eapolIn: |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 562 | intfid := msg.IntfId |
| 563 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 564 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 565 | if err != nil { |
| 566 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 567 | continue |
| 568 | } |
| 569 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 570 | logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, msg.Byte) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 571 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 574 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 575 | return err |
| 576 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 577 | case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 578 | intfid := msg.IntfId |
| 579 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 580 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 581 | 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 NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 588 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 589 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 590 | if err != nil { |
| 591 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 592 | continue |
| 593 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 594 | sn := device.ConvB2S(onu.SerialNumber.VendorSpecific) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 595 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 596 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 597 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 598 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 599 | "gemId": gemid, |
| 600 | }).Error("Fail to tag C-tag") |
| 601 | } else { |
| 602 | pkt = tagpkt |
| 603 | } |
| 604 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 605 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 606 | "gemId": gemid, |
| 607 | "cTagMap": s.CtagMap, |
| 608 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 609 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 610 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 611 | logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, pkt.Dump()) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 612 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 615 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 616 | return err |
| 617 | } |
| 618 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 619 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 620 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 621 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 622 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 623 | continue |
| 624 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 625 | |
| 626 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 627 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 628 | onu, _ := s.GetOnuByID(onuid, intfid) |
| 629 | |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 630 | device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 631 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 632 | pkt := nnipkt.Pkt |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 633 | 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 635 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 636 | return err |
| 637 | } |
| 638 | |
| 639 | case <-ctx.Done(): |
| 640 | logger.Debug("Closed nnichannel ") |
| 641 | return nil |
| 642 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 643 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 644 | } |
| 645 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 646 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 647 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 648 | onu, err := s.GetOnuByID(onuid, intfid) |
| 649 | if err != nil { |
| 650 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 651 | return err |
| 652 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 653 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 654 | if layerEth != nil { |
| 655 | pkt, _ := layerEth.(*layers.Ethernet) |
| 656 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 657 | if ethtype == layers.EthernetTypeEAPOL { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 658 | device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 659 | eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 660 | s.eapolOut <- &eapolPkt |
| 661 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 662 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 663 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 664 | "payload": layerDHCP.LayerPayload(), |
| 665 | "type": layerDHCP.LayerType().String(), |
| 666 | }).Info("Received downstream packet is DHCP.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 667 | poppkt, _, err := PopVLAN(rawpkt) |
| 668 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 669 | logger.Warn("Received untagged packet when expecting single-tagged packet.") |
| 670 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 671 | } else { |
| 672 | // check to see if the packet was double-tagged |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 673 | poppktAgain, _, err := PopVLAN(poppkt) |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 674 | if err == nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 675 | logger.Warn("Received double-tagged packet when expecting single-tagged packet.") |
| 676 | poppkt = poppktAgain |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | logger.Debug("%s", poppkt.Dump()) |
| 680 | dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 681 | s.dhcpOut <- &dhcpPkt |
| 682 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 683 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 684 | device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 685 | // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 686 | return nil |
| 687 | } |
| 688 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
| 692 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 693 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 694 | return nil |
| 695 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 696 | device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 697 | return nil |
| 698 | } |
| 699 | |
| 700 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 701 | poppkt, _, err := PopVLAN(rawpkt) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 702 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 703 | logger.Warn("Received untagged packet when expecting double-tagged packet.") |
| 704 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 705 | } 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 Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 711 | logger.Warn("Received single-tagged packet when expecting double-tagged packet.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 712 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 713 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 714 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 715 | if err != nil { |
| 716 | return err |
| 717 | } |
| 718 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 719 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 720 | SendNni(handle, poppkt) |
| 721 | return nil |
| 722 | } |
| 723 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 724 | // IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 725 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 726 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 727 | for _, onu := range onus { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 728 | if onu.GetIntState() != device.ONU_ACTIVE { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 729 | return false |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | return true |
| 734 | } |
| 735 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 736 | func (s *Server) isAllOnuOmciActive() bool { |
| 737 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 738 | for _, onu := range onus { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 739 | if onu.GetIntState() != device.ONU_OMCIACTIVE { |
| 740 | return false |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | return true |
| 745 | } |
| 746 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 747 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 748 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 749 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 750 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 751 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 752 | 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 NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 758 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 759 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 760 | } |
| 761 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 762 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 763 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 764 | for _, onu := range onus { |
| 765 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 766 | return onu, nil |
| 767 | } |
| 768 | } |
| 769 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 770 | err := errors.New("no matching serial number found") |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 771 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 772 | return nil, err |
| 773 | } |
| 774 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 775 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 776 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 777 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 780 | func 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 784 | } |
| 785 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 786 | err := errors.New("no matching OnuID found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 787 | logger.WithFields(log.Fields{ |
| 788 | "onumap": onumap, |
| 789 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 790 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 791 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 792 | return nil, err |
| 793 | } |
| 794 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 795 | // getOnuFromSNmap method returns onu object from SNmap if found |
| 796 | func (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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 804 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 805 | logger.Info("Serial number not found in map") |
| 806 | return nil, false |
| 807 | } |
| 808 | |
| 809 | func stringifySerialNumber(serialNum *openolt.SerialNumber) string { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 810 | return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | func 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 |
| 833 | func (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 |
| 856 | func (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 |
| 877 | func (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 Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 902 | } |