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" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | "strconv" |
| 23 | "sync" |
Keita NISHIMOTO | 7057aa6 | 2019-05-28 01:24:08 +0900 | [diff] [blame^] | 24 | "reflect" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 25 | |
Keita NISHIMOTO | 7057aa6 | 2019-05-28 01:24:08 +0900 | [diff] [blame^] | 26 | omci "github.com/opencord/omci-sim" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 27 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 28 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "gerrit.opencord.org/voltha-bbsim/device" |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 30 | openolt "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 31 | "github.com/google/gopacket" |
| 32 | "github.com/google/gopacket/layers" |
| 33 | "github.com/google/gopacket/pcap" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 34 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 35 | "golang.org/x/sync/errgroup" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 36 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 37 | ) |
| 38 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 39 | const ( |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 40 | NNI_VETH_NORTH_PFX = "nni_north" |
| 41 | NNI_VETH_SOUTH_PFX = "nni_south" |
| 42 | MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 43 | ) |
| 44 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 45 | // Server structure consists of all the params required for BBsim. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 46 | type Server struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 47 | wg *sync.WaitGroup |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 48 | Olt *device.Olt |
| 49 | Onumap map[uint32][]*device.Onu |
| 50 | Ioinfos []*Ioinfo |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | gRPCserver *grpc.Server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 52 | gRPCAddress string |
| 53 | gRPCPort uint32 |
| 54 | Vethnames []string |
| 55 | IndInterval int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 56 | Processes []string |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 57 | EnableServer *openolt.Openolt_EnableIndicationServer |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 58 | CtagMap map[string]uint32 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 59 | cancel context.CancelFunc |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 60 | stateRepCh chan stateReport |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 61 | omciIn chan openolt.OmciIndication |
| 62 | omciOut chan openolt.OmciMsg |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 63 | eapolIn chan *byteMsg |
| 64 | eapolOut chan *byteMsg |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 65 | dhcpIn chan *byteMsg |
| 66 | dhcpOut chan *byteMsg |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | type Packet struct { |
| 70 | Info *Ioinfo |
| 71 | Pkt gopacket.Packet |
| 72 | } |
| 73 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 74 | type byteMsg struct { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 75 | IntfId uint32 |
| 76 | OnuId uint32 |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 77 | Byte []byte |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 80 | type stateReport struct { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 81 | device device.Device |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 82 | current device.DeviceState |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 83 | next device.DeviceState |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 84 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 85 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 86 | // NewCore initialize OLT and ONU objects |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 87 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 88 | // TODO: make it decent |
| 89 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 90 | npon := opt.npon |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 91 | nonus := opt.nonus |
| 92 | s := Server{ |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 93 | Olt: device.NewOlt(oltid, npon, 1), |
| 94 | Onumap: make(map[uint32][]*device.Onu), |
| 95 | Ioinfos: []*Ioinfo{}, |
| 96 | gRPCAddress: opt.address, |
| 97 | gRPCPort: opt.port, |
| 98 | Vethnames: []string{}, |
| 99 | IndInterval: opt.intvl, |
| 100 | Processes: []string{}, |
Shad Ansari | 704c6f2 | 2018-11-13 14:57:53 -0800 | [diff] [blame] | 101 | EnableServer: nil, |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 102 | stateRepCh: make(chan stateReport, 8), |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 103 | omciIn: make(chan openolt.OmciIndication, 1024), |
| 104 | omciOut: make(chan openolt.OmciMsg, 1024), |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 105 | eapolIn: make(chan *byteMsg, 1024), |
| 106 | eapolOut: make(chan *byteMsg, 1024), |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 107 | dhcpIn: make(chan *byteMsg, 1024), |
| 108 | dhcpOut: make(chan *byteMsg, 1024), |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 111 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 112 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 113 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 115 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 116 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 117 | //TODO: To be fixed because it is hardcoded |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 118 | s.CtagMap = make(map[string]uint32) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 119 | for i := 0; i < MAX_ONUS_PER_PON; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 120 | oltid := s.Olt.ID |
| 121 | intfid := uint32(1) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 122 | sn := convB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 123 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 124 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 125 | return &s |
| 126 | } |
| 127 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 128 | // Start starts the BBSim and openolt gRPC servers (blocking) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 129 | func (s *Server) Start() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 130 | s.wg = &sync.WaitGroup{} |
| 131 | logger.Debug("Start() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 132 | defer func() { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 133 | close(s.stateRepCh) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 134 | logger.Debug("Start() Done") |
| 135 | }() |
| 136 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 137 | listener, gserver, err := NewGrpcServer(addressport) |
| 138 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 139 | logger.Error("Failed to create gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 140 | return err |
| 141 | } |
| 142 | s.gRPCserver = gserver |
| 143 | openolt.RegisterOpenoltServer(gserver, s) |
| 144 | if err := gserver.Serve(listener); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 145 | logger.Error("Failed to run gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 146 | return err |
| 147 | } |
| 148 | s.wg.Wait() |
| 149 | return nil |
| 150 | } |
| 151 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 152 | // Stop stops the BBSim and openolt gRPC servers (non-blocking). |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 153 | func (s *Server) Stop() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 154 | logger.Debug("Stop() Start") |
| 155 | defer logger.Debug("Stop() Done") |
| 156 | if s.gRPCserver != nil { |
| 157 | s.gRPCserver.Stop() |
| 158 | logger.Debug("gRPCserver.Stop()") |
| 159 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 160 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 161 | return |
| 162 | } |
| 163 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 164 | // Enable invokes methods for activation of OLT and ONU (blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 165 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 166 | olt := s.Olt |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 167 | defer func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 168 | olt.Initialize() |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 169 | for intfid := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 170 | for _, onu := range s.Onumap[intfid] { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 171 | onu.Initialize() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 172 | } |
| 173 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 174 | s.updateDevIntState(olt, device.OLT_INACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 175 | logger.Debug("Enable() Done") |
| 176 | }() |
| 177 | logger.Debug("Enable() Start") |
| 178 | s.EnableServer = sv |
| 179 | if err := s.activateOLT(*sv); err != nil { |
| 180 | return err |
| 181 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 182 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 183 | s.updateDevIntState(olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 184 | |
| 185 | coreCtx := context.Background() |
| 186 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 187 | s.cancel = corecancel |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 188 | go s.sendDiscovertoONUs(*sv) |
| 189 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 190 | if err := s.StartPktLoops(coreCtx, *sv); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 191 | return err |
| 192 | } |
| 193 | return nil |
| 194 | } |
| 195 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 196 | // Disable stops packet loops (non-blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 197 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 198 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 199 | logger.Debug("Disable() Done") |
| 200 | }() |
| 201 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 202 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 205 | func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 206 | logger.Debug("updateDevIntState called state:%d", state) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 207 | current := dev.GetIntState() |
| 208 | dev.UpdateIntState(state) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 209 | s.stateRepCh <- stateReport{device: dev, current: current, next: state} |
| 210 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 211 | logger.Debug("OLT State updated to:%d", state) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 212 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 213 | logger.Debug("ONU State updated to:%d", state) |
| 214 | } else { |
| 215 | logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev)) |
| 216 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 219 | func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error { |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 220 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 221 | if err != nil { |
| 222 | return err |
| 223 | } |
| 224 | s.updateDevIntState(onu, state) |
| 225 | return nil |
| 226 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 227 | func (s *Server) Activate(onu *device.Onu) error { |
| 228 | utils.LoggerWithOnu(onu).Info("sending ONUInd Onuid") |
| 229 | go sendOnuIndtoONU(*s.EnableServer, onu) |
| 230 | return nil |
| 231 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 232 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 233 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 234 | defer logger.Debug("activateOLT() Done") |
| 235 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 236 | // Activate OLT |
| 237 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 238 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 239 | return err |
| 240 | } |
| 241 | olt.OperState = "up" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 242 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 243 | |
| 244 | // OLT sends Interface Indication to Adapter |
| 245 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 246 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 247 | return err |
| 248 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 249 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 250 | |
| 251 | // OLT sends Operation Indication to Adapter after activating each interface |
| 252 | //time.Sleep(IF_UP_TIME * time.Second) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 253 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 254 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 255 | return err |
| 256 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 257 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 258 | |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 259 | return nil |
| 260 | } |
| 261 | func (s *Server) sendDiscovertoONUs(stream openolt.Openolt_EnableIndicationServer) { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 262 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 263 | for intfid := range s.Onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 264 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 265 | } |
| 266 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 267 | // Initialize all ONUs |
| 268 | for intfid := range s.Onumap { |
| 269 | for _, onu := range s.Onumap[intfid] { |
| 270 | onu.Initialize() |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Send discovery indication for all ONUs |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 275 | for intfid, _ := range s.Onumap { |
| 276 | sendOnuDiscInd(stream, s.Onumap[intfid], s.IndInterval) |
| 277 | logger.Info("OLT sent ONUDiscInd for intfId:%d.", intfid) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 278 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 279 | } |
| 280 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 281 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 282 | func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 283 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 284 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 285 | RemoveVeths(s.Vethnames) |
| 286 | s.Vethnames = []string{} |
| 287 | s.Ioinfos = []*Ioinfo{} |
| 288 | s.wg.Done() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 289 | s.updateDevIntState(s.Olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 290 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 291 | }() |
| 292 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 293 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 294 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 295 | logger.Error("createIoinfos failed: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 296 | return err |
| 297 | } |
| 298 | s.Ioinfos = ioinfos |
| 299 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 300 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 301 | |
| 302 | parent := ctx |
| 303 | child, cancel := context.WithCancel(parent) |
| 304 | s.cancel = cancel |
| 305 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 306 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 307 | logger.Error("runPktLoops failed: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 308 | return err |
| 309 | } |
| 310 | return nil |
| 311 | } |
| 312 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 313 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 314 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 315 | if s.cancel != nil { |
| 316 | cancel := s.cancel |
| 317 | cancel() |
| 318 | } |
| 319 | } |
| 320 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 321 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 322 | ioinfos := []*Ioinfo{} |
| 323 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 324 | var handler *pcap.Handle |
| 325 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 326 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 327 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 328 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 329 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 330 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 331 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 332 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 333 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 334 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 335 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 336 | } |
| 337 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 338 | //Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 339 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 340 | logger.Debug("runPacketPktLoops Start") |
| 341 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 342 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 343 | errchOmci := make(chan error) |
| 344 | RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 345 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 346 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 347 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 348 | errchEapol := make(chan error) |
| 349 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 350 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 351 | errchDhcp := make(chan error) |
| 352 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 353 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 354 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 355 | logger.Debug("runOMCIResponder Start") |
| 356 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 357 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 358 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 359 | if ok { //Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 360 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 361 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 362 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 363 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 364 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 365 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 366 | return nil |
| 367 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 368 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 369 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 370 | logger.Debug("runEapolResponder Start") |
| 371 | defer logger.Debug("runEapolResponder Done") |
| 372 | select { |
| 373 | case v, ok := <-errchEapol: |
| 374 | if ok { //Error |
| 375 | logger.Error("Error happend in Eapol:%s", v) |
| 376 | return v |
| 377 | } |
| 378 | case <-child.Done(): |
| 379 | return nil |
| 380 | } |
| 381 | return nil |
| 382 | }) |
| 383 | |
| 384 | eg.Go(func() error { |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 385 | logger.Debug("runDhcpResponder Start") |
| 386 | defer logger.Debug("runDhcpResponder Done") |
| 387 | select { |
| 388 | case v, ok := <-errchDhcp: |
| 389 | if ok { //Error |
| 390 | logger.Error("Error happend in Dhcp:%s", v) |
| 391 | return v |
| 392 | } |
| 393 | case <-child.Done(): |
| 394 | return nil |
| 395 | } |
| 396 | return nil |
| 397 | }) |
| 398 | |
| 399 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 400 | err := s.runMainPktLoop(child, stream) |
| 401 | return err |
| 402 | }) |
| 403 | |
| 404 | if err := eg.Wait(); err != nil { |
| 405 | logger.Error("Error happend in runPacketLoops:%s", err) |
| 406 | cancel() |
| 407 | } |
| 408 | return nil |
| 409 | } |
| 410 | |
| 411 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 412 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 413 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 414 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 415 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 416 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
| 417 | if err != nil { |
| 418 | return err |
| 419 | } |
| 420 | nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32) |
| 421 | go RecvWorker(ioinfo, nhandler, nnichannel) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 422 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 423 | close(nnichannel) |
| 424 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 425 | logger.Debug("BEFORE OLT_ACTIVE") |
| 426 | s.updateDevIntState(s.Olt, device.OLT_ACTIVE) |
| 427 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 428 | data := &openolt.Indication_PktInd{} |
| 429 | for { |
| 430 | select { |
| 431 | case msg := <-s.omciIn: |
| 432 | logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 433 | omci := &openolt.Indication_OmciInd{OmciInd: &msg} |
| 434 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 435 | logger.Error("send omci indication failed: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 436 | continue |
| 437 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 438 | case msg := <-s.eapolIn: |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 439 | intfid := msg.IntfId |
| 440 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 441 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 442 | if err != nil { |
| 443 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 444 | continue |
| 445 | } |
| 446 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 447 | 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] | 448 | |
| 449 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 450 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 451 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 452 | return err |
| 453 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 454 | case msg := <-s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 455 | intfid := msg.IntfId |
| 456 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 457 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 458 | bytes := msg.Byte |
| 459 | pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default) |
| 460 | |
| 461 | if err != nil { |
| 462 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 463 | continue |
| 464 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 465 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 466 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 467 | if err != nil { |
| 468 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 469 | continue |
| 470 | } |
| 471 | sn := convB2S(onu.SerialNumber.VendorSpecific) |
| 472 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 473 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 474 | if err != nil { |
| 475 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 476 | "gemId": gemid, |
| 477 | }).Error("Fail to tag C-tag") |
| 478 | } else { |
| 479 | pkt = tagpkt |
| 480 | } |
| 481 | } else { |
| 482 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 483 | "gemId": gemid, |
| 484 | "cTagMap": s.CtagMap, |
| 485 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 486 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 487 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 488 | 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] | 489 | |
| 490 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 491 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 492 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 493 | return err |
| 494 | } |
| 495 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 496 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 497 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 498 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 499 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 500 | continue |
| 501 | } |
| 502 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 503 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 504 | onu, err := s.GetOnuByID(onuid, intfid) |
| 505 | if err != nil { |
| 506 | logger.Error("Failed processing NNI packet: %v", err) |
| 507 | continue |
| 508 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 509 | |
| 510 | utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 511 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 512 | pkt := nnipkt.Pkt |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 513 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 514 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 515 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 516 | return err |
| 517 | } |
| 518 | |
| 519 | case <-ctx.Done(): |
| 520 | logger.Debug("Closed nnichannel ") |
| 521 | return nil |
| 522 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 523 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 524 | return nil |
| 525 | } |
| 526 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 527 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 528 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 529 | onu, err := s.GetOnuByID(onuid, intfid) |
| 530 | if err != nil { |
| 531 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 532 | return err |
| 533 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 534 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 535 | if layerEth != nil { |
| 536 | pkt, _ := layerEth.(*layers.Ethernet) |
| 537 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 538 | if ethtype == layers.EthernetTypeEAPOL { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 539 | utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 540 | eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 541 | s.eapolOut <- &eapolPkt |
| 542 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 543 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 544 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 545 | "payload": layerDHCP.LayerPayload(), |
| 546 | "type": layerDHCP.LayerType().String(), |
| 547 | }).Info("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 548 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 549 | rawpkt, _, _ = PopVLAN(rawpkt) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 550 | logger.Debug("%s", rawpkt.Dump()) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 551 | dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 552 | s.dhcpOut <- &dhcpPkt |
| 553 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 554 | } else { |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 555 | utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 556 | return nil |
| 557 | } |
| 558 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 559 | if err != nil { |
| 560 | return err |
| 561 | } |
| 562 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 563 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 564 | return nil |
| 565 | } |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 566 | utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 567 | return nil |
| 568 | } |
| 569 | |
| 570 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 571 | poppkt, _, err := PopVLAN(rawpkt) |
| 572 | poppkt, _, err = PopVLAN(poppkt) |
| 573 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 574 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 575 | return err |
| 576 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 577 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 578 | if err != nil { |
| 579 | return err |
| 580 | } |
| 581 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 582 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 583 | SendNni(handle, poppkt) |
| 584 | return nil |
| 585 | } |
| 586 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 587 | // 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] | 588 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 589 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 590 | for _, onu := range onus { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 591 | if onu.GetIntState() != device.ONU_ACTIVE { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 592 | return false |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | return true |
| 597 | } |
| 598 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 599 | func (s *Server) isAllOnuOmciActive() bool { |
| 600 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 601 | for _, onu := range onus { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 602 | if onu.GetIntState() != device.ONU_OMCIACTIVE { |
| 603 | return false |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | return true |
| 608 | } |
| 609 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 610 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 611 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 612 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 613 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 614 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 615 | onu, err := s.GetOnuByID(onuid, intfid) |
| 616 | if err != nil { |
| 617 | logger.Error("Failed to getGemPortID: %s", err) |
| 618 | return 0, err |
| 619 | } |
| 620 | gemportid = onu.GemportID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 621 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 622 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 623 | } |
| 624 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 625 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 626 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 627 | for _, onu := range onus { |
| 628 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 629 | return onu, nil |
| 630 | } |
| 631 | } |
| 632 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 633 | err := errors.New("No mathced SN is found ") |
| 634 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 635 | return nil, err |
| 636 | } |
| 637 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 638 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 639 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 640 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 643 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) { |
| 644 | for _, onu := range onumap[intfid] { |
| 645 | if onu.OnuID == onuid { |
| 646 | return onu, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 647 | } |
| 648 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 649 | err := errors.New("No matched OnuID is found ") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 650 | logger.WithFields(log.Fields{ |
| 651 | "onumap": onumap, |
| 652 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 653 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 654 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 655 | return nil, err |
| 656 | } |
| 657 | |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 658 | func convB2S(b []byte) string { |
| 659 | s := "" |
| 660 | for _, i := range b { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 661 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 662 | } |
| 663 | return s |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 664 | } |