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