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