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" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 32 | api "github.com/opencord/voltha-bbsim/api" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 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" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 52 | AutoDiscoveryDelay = 5 |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 53 | ) |
| 54 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 55 | // Server structure consists of all the params required for BBsim. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 56 | type Server struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 57 | 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 Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 70 | IndInterval time.Duration |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 71 | 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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 82 | FlowMap map[device.FlowKey]*openolt.Flow |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 83 | alarmCh chan *openolt.Indication |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 84 | deviceActionCh chan *api.DeviceAction |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 85 | serverActionCh chan string |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 88 | // Packet structure |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 89 | type Packet struct { |
| 90 | Info *Ioinfo |
| 91 | Pkt gopacket.Packet |
| 92 | } |
| 93 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 94 | type byteMsg struct { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 95 | IntfID uint32 |
| 96 | OnuID uint32 |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 97 | Byte []byte |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 100 | type stateReport struct { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 101 | device device.Device |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 102 | current device.State |
| 103 | next device.State |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 104 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 105 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 106 | // NewCore initialize OLT and ONU objects |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 107 | func NewCore(opt *Option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 108 | // TODO: make it decent |
| 109 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 110 | npon := opt.npon |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 111 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 115 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 116 | 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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 142 | FlowMap: make(map[device.FlowKey]*openolt.Flow), |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 143 | serverActionCh: make(chan string), |
| 144 | } |
| 145 | logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 146 | logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber) |
| 147 | // Creating Onu Map |
| 148 | for intfid := uint32(0); intfid < npon; intfid++ { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 149 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 150 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 151 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 152 | 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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 160 | s.CtagMap = make(map[string]uint32) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 161 | for i := 0; i < MaxOnusPerPon; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 162 | oltid := s.Olt.ID |
| 163 | intfid := uint32(1) |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 164 | sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 165 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 166 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 167 | |
| 168 | flowHandler.InitializeFlowManager(s.Olt.ID) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 169 | return &s |
| 170 | } |
| 171 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 172 | // Start starts the openolt gRPC server (blocking) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 173 | func (s *Server) Start() error { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 174 | logger.Debug("Starting OpenOLT gRPC Server") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 175 | defer func() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 176 | logger.Debug("OpenOLT gRPC Server Stopped") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 177 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 178 | |
| 179 | // Start Openolt gRPC server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 180 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 181 | listener, gserver, err := NewGrpcServer(addressport) |
| 182 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 183 | logger.Error("Failed to create gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 184 | return err |
| 185 | } |
| 186 | s.gRPCserver = gserver |
| 187 | openolt.RegisterOpenoltServer(gserver, s) |
| 188 | if err := gserver.Serve(listener); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 189 | logger.Error("Failed to run gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 190 | return err |
| 191 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 192 | return nil |
| 193 | } |
| 194 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 195 | // Stop stops the openolt gRPC servers (non-blocking). |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 196 | func (s *Server) Stop() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 197 | logger.Debug("Stopping OpenOLT gRPC Server & PktLoops") |
| 198 | defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped") |
| 199 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 200 | if s.gRPCserver != nil { |
| 201 | s.gRPCserver.Stop() |
| 202 | logger.Debug("gRPCserver.Stop()") |
| 203 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 204 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 205 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 206 | return |
| 207 | } |
| 208 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 209 | func (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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 215 | go StartRestGatewayService(grpcAddressPort, restAddressPort) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 216 | 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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 225 | api.RegisterBBSimServiceServer(apiserver, s) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 226 | if e := apiserver.Serve(listener); e != nil { |
| 227 | logger.Error("Failed to run management api server %v", e) |
| 228 | return |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | |
| 233 | func (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 Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 242 | // Enable invokes methods for activation of OLT and ONU (blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 243 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 244 | olt := s.Olt |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 245 | defer func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 246 | olt.Initialize() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 247 | // 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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 253 | s.updateDevIntState(olt, device.OltInactive) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 254 | logger.Debug("Enable() Done") |
| 255 | }() |
| 256 | logger.Debug("Enable() Start") |
| 257 | s.EnableServer = sv |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 258 | flowHandler.InitializePacketInStream(*sv) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 259 | if err := s.activateOLT(*sv); err != nil { |
| 260 | return err |
| 261 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 262 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 263 | s.updateDevIntState(olt, device.OltPreactive) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 264 | coreCtx := context.Background() |
| 265 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 266 | s.cancel = corecancel |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 267 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 268 | errorchan := make(chan error, 5) |
| 269 | go s.StartPktLoops(coreCtx, *sv, errorchan) |
| 270 | |
| 271 | if s.AutoONUActivate == true { |
| 272 | // Initialize all ONUs |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 273 | // 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 Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 276 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 283 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 284 | |
| 285 | select { |
| 286 | case err := <-errorchan: |
| 287 | if err != nil { |
| 288 | logger.Debug("Error: %v", err) |
| 289 | return err |
| 290 | } |
| 291 | } |
| 292 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 293 | return nil |
| 294 | } |
| 295 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 296 | // Disable stops packet loops (non-blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 297 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 298 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 299 | logger.Debug("Disable() Done") |
| 300 | }() |
| 301 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 302 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 303 | } |
| 304 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 305 | func (s *Server) updateDevIntState(dev device.Device, state device.State) { |
| 306 | logger.Debug("updateDevIntState called state:%d", state) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 307 | current := dev.GetIntState() |
| 308 | dev.UpdateIntState(state) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 309 | 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] | 310 | s.stateRepCh <- stateReport{device: dev, current: current, next: state} |
| 311 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 312 | logger.Debug("OLT State updated to:%s", device.ONUState[state]) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 313 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 314 | logger.Debug("ONU State updated to:%s", device.ONUState[state]) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 315 | } else { |
| 316 | logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev)) |
| 317 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 318 | } |
| 319 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 320 | func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.State) error { |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 321 | onu, err := s.GetOnuByID(onuid, intfid) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 322 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 323 | if err != nil { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 324 | logger.Warn("Failed to get ONU %d: %v", onuid, err) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 325 | return err |
| 326 | } |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 327 | |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 328 | if state == onu.GetIntState() { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 329 | logger.WithFields(log.Fields{ |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 330 | "toState": device.ONUState[state], |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 331 | "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 NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 338 | s.updateDevIntState(onu, state) |
| 339 | return nil |
| 340 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 341 | |
| 342 | func (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 | |
| 356 | func (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 Gunyel | 32dfd72 | 2019-08-05 16:18:06 +0300 | [diff] [blame] | 361 | if s.IndInterval > 0 { |
| 362 | time.Sleep(s.IndInterval) |
| 363 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 364 | } |
| 365 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 366 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 367 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 368 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 369 | defer logger.Debug("activateOLT() Done") |
| 370 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 371 | // Activate OLT |
| 372 | olt := s.Olt |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 373 | olt.OperState = "up" |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 374 | if err := sendOltIndUp(stream, olt); err != nil { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 375 | olt.OperState = "down" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 376 | return err |
| 377 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 378 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 379 | |
| 380 | // OLT sends Interface Indication to Adapter |
| 381 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 382 | logger.Error("Fail to sendIntfInd: %v", err) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 383 | olt.OperState = "down" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 384 | return err |
| 385 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 386 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 387 | |
| 388 | // OLT sends Operation Indication to Adapter after activating each interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 389 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 390 | logger.Error("Fail to sendOperInd: %v", err) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 391 | olt.OperState = "down" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 392 | return err |
| 393 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 394 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 395 | return nil |
| 396 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 397 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 398 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 399 | 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] | 400 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 401 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 402 | RemoveVeths(s.Vethnames) |
| 403 | s.Vethnames = []string{} |
| 404 | s.Ioinfos = []*Ioinfo{} |
| 405 | s.wg.Done() |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 406 | s.updateDevIntState(s.Olt, device.OltPreactive) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 407 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 408 | }() |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 409 | go s.sendPortStats() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 410 | s.alarmCh = make(chan *openolt.Indication, 10) |
| 411 | go startAlarmLoop(stream, s.alarmCh) |
| 412 | go s.startDeviceActionLoop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 413 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 414 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 415 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 416 | logger.Error("createIoinfos failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 417 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 418 | } |
| 419 | s.Ioinfos = ioinfos |
| 420 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 421 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 422 | |
| 423 | parent := ctx |
| 424 | child, cancel := context.WithCancel(parent) |
| 425 | s.cancel = cancel |
| 426 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 427 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 428 | logger.Error("runPktLoops failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 429 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 430 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 431 | errorchan <- nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 432 | } |
| 433 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 434 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 435 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 436 | if s.cancel != nil { |
| 437 | cancel := s.cancel |
| 438 | cancel() |
| 439 | } |
| 440 | } |
| 441 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 442 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 443 | var ioinfos []*Ioinfo |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 444 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 445 | var handler *pcap.Handle |
| 446 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 447 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 448 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 449 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 450 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 451 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 452 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 453 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 454 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 455 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 456 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 457 | } |
| 458 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 459 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 460 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 461 | logger.Debug("runPacketPktLoops Start") |
| 462 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 463 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 464 | errchOmci := make(chan error) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 465 | s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 466 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 467 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 468 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 469 | errchEapol := make(chan error) |
| 470 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 471 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 472 | errchDhcp := make(chan error) |
| 473 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 474 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 475 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 476 | logger.Debug("runOMCIResponder Start") |
| 477 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 478 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 479 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 480 | if ok { // Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 481 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 482 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 483 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 484 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 485 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 486 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 487 | return nil |
| 488 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 489 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 490 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 491 | logger.Debug("runEapolResponder Start") |
| 492 | defer logger.Debug("runEapolResponder Done") |
| 493 | select { |
| 494 | case v, ok := <-errchEapol: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 495 | if ok { // Error |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 496 | 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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 506 | logger.Debug("runDhcpResponder Start") |
| 507 | defer logger.Debug("runDhcpResponder Done") |
| 508 | select { |
| 509 | case v, ok := <-errchDhcp: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 510 | if ok { // Error |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 511 | 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 NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 521 | 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 | |
| 532 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 533 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 534 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 535 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 536 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 537 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 543 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 544 | close(nnichannel) |
| 545 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 546 | logger.Debug("BEFORE OLT_ACTIVE") |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 547 | s.updateDevIntState(s.Olt, device.OltActive) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 548 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 549 | data := &openolt.Indication_PktInd{} |
| 550 | for { |
| 551 | select { |
| 552 | case msg := <-s.omciIn: |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 553 | 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 NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 557 | continue |
| 558 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 559 | case msg := <-s.eapolIn: |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 560 | intfid := msg.IntfID |
| 561 | onuid := msg.OnuID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 562 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 563 | if err != nil { |
| 564 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 565 | continue |
| 566 | } |
| 567 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 568 | 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] | 569 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 572 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 573 | return err |
| 574 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 575 | case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 576 | intfid := msg.IntfID |
| 577 | onuid := msg.OnuID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 578 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 579 | 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 NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 586 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 587 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 588 | if err != nil { |
| 589 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 590 | continue |
| 591 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 592 | sn := device.ConvB2S(onu.SerialNumber.VendorSpecific) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 593 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 594 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 595 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 596 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 597 | "gemId": gemid, |
| 598 | }).Error("Fail to tag C-tag") |
| 599 | } else { |
| 600 | pkt = tagpkt |
| 601 | } |
| 602 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 603 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 604 | "gemId": gemid, |
| 605 | "cTagMap": s.CtagMap, |
| 606 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 607 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 608 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 609 | 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] | 610 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 613 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 614 | return err |
| 615 | } |
| 616 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 617 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 618 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 619 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 620 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 621 | continue |
| 622 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 623 | |
| 624 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 625 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 626 | onu, _ := s.GetOnuByID(onuid, intfid) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 627 | pkt := nnipkt.Pkt |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 628 | |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 629 | device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 630 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 631 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 632 | if err = stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 633 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 634 | return err |
| 635 | } |
| 636 | |
| 637 | case <-ctx.Done(): |
| 638 | logger.Debug("Closed nnichannel ") |
| 639 | return nil |
| 640 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 641 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 642 | } |
| 643 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 644 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 645 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 646 | onu, err := s.GetOnuByID(onuid, intfid) |
| 647 | if err != nil { |
| 648 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 649 | return err |
| 650 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 651 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 652 | if layerEth != nil { |
| 653 | pkt, _ := layerEth.(*layers.Ethernet) |
| 654 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 655 | if ethtype == layers.EthernetTypeEAPOL { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 656 | device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 657 | eapolPkt := byteMsg{IntfID: intfid, OnuID: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 658 | s.eapolOut <- &eapolPkt |
| 659 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 660 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 661 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 662 | "payload": layerDHCP.LayerPayload(), |
| 663 | "type": layerDHCP.LayerType().String(), |
| 664 | }).Info("Received downstream packet is DHCP.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 665 | poppkt, _, err := PopVLAN(rawpkt) |
| 666 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 667 | logger.Warn("Received untagged packet when expecting single-tagged packet.") |
| 668 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 669 | } else { |
| 670 | // check to see if the packet was double-tagged |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 671 | poppktAgain, _, err := PopVLAN(poppkt) |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 672 | if err == nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 673 | logger.Warn("Received double-tagged packet when expecting single-tagged packet.") |
| 674 | poppkt = poppktAgain |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | logger.Debug("%s", poppkt.Dump()) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 678 | dhcpPkt := byteMsg{IntfID: intfid, OnuID: onuid, Byte: poppkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 679 | s.dhcpOut <- &dhcpPkt |
| 680 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 681 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 682 | device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 683 | // 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] | 684 | return nil |
| 685 | } |
| 686 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 687 | if err != nil { |
| 688 | return err |
| 689 | } |
| 690 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 691 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 692 | return nil |
| 693 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 694 | device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 695 | return nil |
| 696 | } |
| 697 | |
| 698 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 699 | poppkt, _, err := PopVLAN(rawpkt) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 700 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 701 | logger.Warn("Received untagged packet when expecting double-tagged packet.") |
| 702 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 703 | } 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 Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 709 | logger.Warn("Received single-tagged packet when expecting double-tagged packet.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 710 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 711 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 712 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 713 | if err != nil { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 714 | logger.Error("failed to get ioinfo") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 715 | return err |
| 716 | } |
| 717 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 718 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 719 | SendNni(handle, poppkt) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 720 | // 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 724 | return nil |
| 725 | } |
| 726 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 727 | // isAllOnuOmciActive checks for OnuOmciActive state for all the onus in the map |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 728 | func (s *Server) isAllOnuOmciActive() bool { |
| 729 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 730 | for _, onu := range onus { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 731 | if onu.GetIntState() != device.OnuOmciActive { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 732 | return false |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | return true |
| 737 | } |
| 738 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 739 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 740 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 741 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 742 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 743 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 744 | onu, err := s.GetOnuByID(onuid, intfid) |
| 745 | if err != nil { |
| 746 | logger.Error("Failed to getGemPortID: %s", err) |
| 747 | return 0, err |
| 748 | } |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 749 | for _, gemports := range onu.GemPortMap { |
| 750 | return gemports[0], nil |
| 751 | } |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 752 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 753 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 754 | } |
| 755 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 756 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 757 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 758 | for _, onu := range onus { |
| 759 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 760 | return onu, nil |
| 761 | } |
| 762 | } |
| 763 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 764 | err := errors.New("no matching serial number found") |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 765 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 766 | return nil, err |
| 767 | } |
| 768 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 769 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 770 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 771 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 774 | func 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 778 | } |
| 779 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 780 | err := errors.New("no matching OnuID found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 781 | logger.WithFields(log.Fields{ |
| 782 | "onumap": onumap, |
| 783 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 784 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 785 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 786 | return nil, err |
| 787 | } |
| 788 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 789 | // getOnuFromSNmap method returns onu object from SNmap if found |
| 790 | func (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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 798 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 799 | logger.Info("Serial number not found in map") |
| 800 | return nil, false |
| 801 | } |
| 802 | |
| 803 | func stringifySerialNumber(serialNum *openolt.SerialNumber) string { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 804 | return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | func 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 |
| 827 | func (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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 836 | if onu.InternalState == device.OnuLosRaised { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 837 | 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 |
| 850 | func (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 |
| 871 | func (s *Server) startDeviceActionLoop() { |
| 872 | logger.Debug("startDeviceActionLoop invoked") |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 873 | s.deviceActionCh = make(chan *api.DeviceAction, 10) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 874 | for { |
| 875 | logger.Debug("Action channel loop started") |
| 876 | select { |
| 877 | case Req := <-s.deviceActionCh: |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 878 | logger.Debug("Reboot Action Type: %+v", Req.Action) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 879 | switch Req.DeviceType { |
| 880 | case DeviceTypeOnu: |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 881 | value, _ := s.SNmap.Load(Req.SerialNumber) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 882 | onu := value.(*device.Onu) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 883 | if Req.Action == SoftReboot { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 884 | s.handleONUSoftReboot(onu.IntfID, onu.OnuID) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 885 | } else if Req.Action == HardReboot { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 886 | 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 Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 896 | } |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 897 | |
| 898 | func (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 | } |