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