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