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 | |
| 25 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 26 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 27 | "gerrit.opencord.org/voltha-bbsim/device" |
| 28 | "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "github.com/google/gopacket" |
| 30 | "github.com/google/gopacket/layers" |
| 31 | "github.com/google/gopacket/pcap" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 32 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 33 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | ) |
| 35 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 36 | const ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 37 | UNI_VETH_UP_PFX = "sim_uu" |
| 38 | UNI_VETH_DW_PFX = "sim_ud" |
| 39 | NNI_VETH_UP_PFX = "sim_nu" |
| 40 | NNI_VETH_DW_PFX = "sim_nd" |
| 41 | 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] | 42 | ) |
| 43 | |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame^] | 44 | type OmciIndication struct { |
| 45 | IntfId uint32 |
| 46 | OnuId uint32 |
| 47 | Pkt []byte |
| 48 | } |
| 49 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 50 | type Server struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 51 | wg *sync.WaitGroup |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 52 | Olt *device.Olt |
| 53 | Onumap map[uint32][]*device.Onu |
| 54 | Ioinfos []*Ioinfo |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 55 | gRPCserver *grpc.Server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 56 | gRPCAddress string |
| 57 | gRPCPort uint32 |
| 58 | Vethnames []string |
| 59 | IndInterval int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 60 | Processes []string |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 61 | EnableServer *openolt.Openolt_EnableIndicationServer |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 62 | CtagMap map[string]uint32 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 63 | cancel context.CancelFunc |
| 64 | state coreState |
| 65 | stateChan chan coreState |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame^] | 66 | omciChan chan OmciIndication |
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 | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 74 | type coreState int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 75 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 76 | const ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 77 | INACTIVE = iota // OLT/ONUs are not instantiated |
| 78 | PRE_ACTIVE // Before PacketInDaemon Running |
| 79 | ACTIVE // After PacketInDaemon Running |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 80 | ) |
| 81 | |
| 82 | /* coreState |
| 83 | INACTIVE -> PRE_ACTIVE -> ACTIVE |
| 84 | (ActivateOLT) (Enable) |
| 85 | <- <- |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 86 | */ |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 87 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 88 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 89 | // TODO: make it decent |
| 90 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 91 | npon := opt.npon |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 92 | nonus := opt.nonus |
| 93 | s := Server{ |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 94 | Olt: device.NewOlt(oltid, npon, 1), |
| 95 | Onumap: make(map[uint32][]*device.Onu), |
| 96 | Ioinfos: []*Ioinfo{}, |
| 97 | gRPCAddress: opt.address, |
| 98 | gRPCPort: opt.port, |
| 99 | Vethnames: []string{}, |
| 100 | IndInterval: opt.intvl, |
| 101 | Processes: []string{}, |
Shad Ansari | 704c6f2 | 2018-11-13 14:57:53 -0800 | [diff] [blame] | 102 | EnableServer: nil, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 103 | state: INACTIVE, |
| 104 | stateChan: make(chan coreState, 8), |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame^] | 105 | omciChan: make(chan OmciIndication, 8), |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 108 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 109 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 110 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 111 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 112 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 113 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | //TODO: To be fixed because it is hardcoded |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 115 | s.CtagMap = make(map[string]uint32) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 116 | for i := 0; i < MAX_ONUS_PER_PON; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 117 | oltid := s.Olt.ID |
| 118 | intfid := uint32(1) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 119 | sn := convB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 120 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 121 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 122 | return &s |
| 123 | } |
| 124 | |
| 125 | //Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 126 | func (s *Server) Start() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 127 | s.wg = &sync.WaitGroup{} |
| 128 | logger.Debug("Start() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 129 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 130 | close(s.stateChan) |
| 131 | logger.Debug("Start() Done") |
| 132 | }() |
| 133 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 134 | listener, gserver, err := NewGrpcServer(addressport) |
| 135 | if err != nil { |
| 136 | logger.Error("Failed to create gRPC server", err) |
| 137 | return err |
| 138 | } |
| 139 | s.gRPCserver = gserver |
| 140 | openolt.RegisterOpenoltServer(gserver, s) |
| 141 | if err := gserver.Serve(listener); err != nil { |
| 142 | logger.Error("Failed to run gRPC server", err) |
| 143 | return err |
| 144 | } |
| 145 | s.wg.Wait() |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | //Non-Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 150 | func (s *Server) Stop() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 151 | logger.Debug("Stop() Start") |
| 152 | defer logger.Debug("Stop() Done") |
| 153 | if s.gRPCserver != nil { |
| 154 | s.gRPCserver.Stop() |
| 155 | logger.Debug("gRPCserver.Stop()") |
| 156 | } |
| 157 | s.StopPktInDaemon() |
| 158 | return |
| 159 | } |
| 160 | |
| 161 | // Blocking |
| 162 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 163 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 164 | olt := s.Olt |
| 165 | olt.InitializeStatus() |
| 166 | for intfid, _ := range s.Onumap { |
| 167 | for _, onu := range s.Onumap[intfid] { |
| 168 | onu.InitializeStatus() |
| 169 | } |
| 170 | } |
| 171 | s.updateState(INACTIVE) |
| 172 | logger.Debug("Enable() Done") |
| 173 | }() |
| 174 | logger.Debug("Enable() Start") |
| 175 | s.EnableServer = sv |
| 176 | if err := s.activateOLT(*sv); err != nil { |
| 177 | return err |
| 178 | } |
| 179 | s.updateState(PRE_ACTIVE) |
| 180 | |
| 181 | coreCtx := context.Background() |
| 182 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 183 | s.cancel = corecancel |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 184 | if err := s.StartPktInDaemon(coreCtx, *sv); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 185 | return err |
| 186 | } |
| 187 | return nil |
| 188 | } |
| 189 | |
| 190 | //Non-Blocking |
| 191 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 192 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | logger.Debug("Disable() Done") |
| 194 | }() |
| 195 | logger.Debug("Disable() Start") |
| 196 | s.StopPktInDaemon() |
| 197 | } |
| 198 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 199 | func (s *Server) updateState(state coreState) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 200 | s.state = state |
| 201 | s.stateChan <- state |
| 202 | logger.Debug("State updated to:%d", state) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 206 | defer logger.Debug("activateOLT() Done") |
| 207 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 208 | // Activate OLT |
| 209 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 210 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 211 | return err |
| 212 | } |
| 213 | olt.OperState = "up" |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 214 | *olt.InternalState = device.OLT_UP |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 215 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 216 | |
| 217 | // OLT sends Interface Indication to Adapter |
| 218 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 219 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 220 | return err |
| 221 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 222 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 223 | |
| 224 | // OLT sends Operation Indication to Adapter after activating each interface |
| 225 | //time.Sleep(IF_UP_TIME * time.Second) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 226 | *olt.InternalState = device.PONIF_UP |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 227 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 228 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 229 | return err |
| 230 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 231 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 232 | |
| 233 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
| 234 | for intfid, _ := range s.Onumap { |
| 235 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 236 | } |
| 237 | |
| 238 | for intfid, _ := range s.Onumap { |
| 239 | sendOnuDiscInd(stream, s.Onumap[intfid]) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 240 | logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // OLT Sends OnuInd after waiting all of those ONUs up |
| 244 | for { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 245 | if IsAllOnuActive(s.Onumap) { |
| 246 | logger.Debug("All the Onus are Activated.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 247 | break |
| 248 | } |
| 249 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 250 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 251 | for intfid, _ := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 252 | sendOnuInd(stream, s.Onumap[intfid], s.IndInterval) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 253 | logger.Info("OLT id:%d sent ONUInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 254 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 255 | return nil |
| 256 | } |
| 257 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 258 | // Blocking |
| 259 | func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 260 | logger.Debug("StartPktInDaemon() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 261 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 262 | RemoveVeths(s.Vethnames) |
| 263 | s.Vethnames = []string{} |
| 264 | s.Ioinfos = []*Ioinfo{} |
| 265 | s.wg.Done() |
| 266 | s.updateState(PRE_ACTIVE) |
| 267 | logger.Debug("StartPktInDaemon() Done") |
| 268 | }() |
| 269 | s.wg.Add(1) |
| 270 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap) |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | s.Ioinfos = ioinfos |
| 275 | s.Vethnames = veths |
| 276 | logger.Debug("Created vethnames:%v", s.Vethnames) |
| 277 | |
| 278 | parent := ctx |
| 279 | child, cancel := context.WithCancel(parent) |
| 280 | s.cancel = cancel |
| 281 | |
| 282 | if err = s.runPacketInDaemon(child, stream); err != nil { |
| 283 | return err |
| 284 | } |
| 285 | return nil |
| 286 | } |
| 287 | |
| 288 | //Non-Blocking |
| 289 | func (s *Server) StopPktInDaemon() { |
| 290 | if s.cancel != nil { |
| 291 | cancel := s.cancel |
| 292 | cancel() |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | 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] | 297 | ioinfos := []*Ioinfo{} |
| 298 | var err error |
| 299 | for intfid, _ := range onumap { |
| 300 | for i := 0; i < len(onumap[intfid]); i++ { |
| 301 | var handler *pcap.Handle |
| 302 | onuid := onumap[intfid][i].OnuID |
| 303 | uniup, unidw := makeUniName(oltid, intfid, onuid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 304 | if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil { |
| 305 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 306 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 307 | 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] | 308 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 309 | 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] | 310 | ioinfos = append(ioinfos, &oinfo) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | var handler *pcap.Handle |
| 315 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 316 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
| 317 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 318 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 319 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 320 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 321 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 322 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 323 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 324 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 327 | //Blocking |
| 328 | func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 329 | logger.Debug("runPacketInDaemon Start") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 330 | defer logger.Debug("runPacketInDaemon Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 331 | unichannel := make(chan Packet, 2048) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 332 | |
| 333 | for intfid, _ := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 334 | for _, onu := range s.Onumap[intfid] { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 335 | onuid := onu.OnuID |
| 336 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 337 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 338 | utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 339 | return err |
| 340 | } |
| 341 | uhandler := ioinfo.handler |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 342 | go RecvWorker(ioinfo, uhandler, unichannel) |
| 343 | } |
| 344 | } |
| 345 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 346 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 347 | if err != nil { |
| 348 | return err |
| 349 | } |
| 350 | nhandler := ioinfo.handler |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 351 | nnichannel := make(chan Packet, 32) |
| 352 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 353 | |
| 354 | data := &openolt.Indication_PktInd{} |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 355 | s.updateState(ACTIVE) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 356 | for { |
| 357 | select { |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame^] | 358 | case msg := <-s.omciChan: |
| 359 | logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 360 | omci := &openolt.Indication_OmciInd{OmciInd: &openolt.OmciIndication{IntfId: msg.IntfId, OnuId: msg.OnuId, Pkt: msg.Pkt}} |
| 361 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
| 362 | logger.Error("send omci indication failed.", err) |
| 363 | continue |
| 364 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 365 | case unipkt := <-unichannel: |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 366 | logger.Debug("Received packet in grpc Server from UNI.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 367 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 368 | logger.Debug("WARNING: This packet does not come from UNI ") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 369 | continue |
| 370 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 371 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 372 | intfid := unipkt.Info.intfid |
| 373 | onuid := unipkt.Info.onuid |
| 374 | gemid, _ := getGemPortID(intfid, onuid) |
| 375 | pkt := unipkt.Pkt |
| 376 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 377 | le, _ := layerEth.(*layers.Ethernet) |
| 378 | ethtype := le.EthernetType |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 379 | onu, _ := s.GetOnuByID(onuid) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 380 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 381 | if ethtype == 0x888e { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 382 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 383 | "gemId": gemid, |
| 384 | }).Info("Received upstream packet is EAPOL.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 385 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 386 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 387 | "gemId": gemid, |
| 388 | }).Info("Received upstream packet is DHCP.") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 389 | |
| 390 | //C-TAG |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 391 | sn := convB2S(onu.SerialNumber.VendorSpecific) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 392 | if ctag, ok := s.CtagMap[sn]; ok == true { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 393 | tagpkt, err := PushVLAN(pkt, uint16(ctag)) |
| 394 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 395 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 396 | "gemId": gemid, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 397 | }).Error("Fail to tag C-tag") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 398 | } else { |
| 399 | pkt = tagpkt |
| 400 | } |
| 401 | } else { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 402 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 403 | "gemId": gemid, |
| 404 | "cTagMap": s.CtagMap, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 405 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 406 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 407 | } else { |
| 408 | continue |
| 409 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 410 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 411 | utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 412 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}} |
| 413 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 414 | logger.Error("Fail to send PktInd indication.", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 415 | return err |
| 416 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 417 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 418 | case nnipkt := <-nnichannel: |
| 419 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 420 | logger.Debug("WARNING: This packet does not come from NNI ") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 421 | continue |
| 422 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 423 | onuid := nnipkt.Info.onuid |
| 424 | onu, _ := getOnuByID(s.Onumap, onuid) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 425 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 426 | utils.LoggerWithOnu(onu).Info("Received packet in grpc Server from NNI.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 427 | intfid := nnipkt.Info.intfid |
| 428 | pkt := nnipkt.Pkt |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 429 | utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 430 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 431 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 432 | logger.Error("Fail to send PktInd indication.", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 433 | return err |
| 434 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 435 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 436 | case <-ctx.Done(): |
| 437 | logger.Debug("PacketInDaemon thread receives close ") |
| 438 | close(unichannel) |
| 439 | logger.Debug("Closed unichannel ") |
| 440 | close(nnichannel) |
| 441 | logger.Debug("Closed nnichannel ") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 442 | return nil |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 443 | } |
| 444 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 445 | return nil |
| 446 | } |
| 447 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 448 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 449 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 450 | onu, _ := s.GetOnuByID(onuid) |
| 451 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 452 | if layerEth != nil { |
| 453 | pkt, _ := layerEth.(*layers.Ethernet) |
| 454 | ethtype := pkt.EthernetType |
| 455 | if ethtype == 0x888e { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 456 | utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 457 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 458 | utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 459 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 460 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 461 | } else { |
| 462 | return nil |
| 463 | } |
| 464 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 465 | if err != nil { |
| 466 | return err |
| 467 | } |
| 468 | handle := ioinfo.handler |
| 469 | SendUni(handle, rawpkt) |
| 470 | return nil |
| 471 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 472 | logger.Info("WARNING: Received packet is not supported") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 473 | return nil |
| 474 | } |
| 475 | |
| 476 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 477 | poppkt, _, err := PopVLAN(rawpkt) |
| 478 | poppkt, _, err = PopVLAN(poppkt) |
| 479 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 480 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 481 | return err |
| 482 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 483 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 484 | if err != nil { |
| 485 | return err |
| 486 | } |
| 487 | handle := ioinfo.handler |
| 488 | SendNni(handle, poppkt) |
| 489 | return nil |
| 490 | } |
| 491 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 492 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 493 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 494 | for _, onu := range onus { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 495 | if onu.GetIntStatus() != device.ONU_ACTIVATED { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 496 | return false |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | return true |
| 501 | } |
| 502 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 503 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
| 504 | idx := uint32(0) |
| 505 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 506 | //return uint32(1032 + 8 * (vid - 1)), nil |
| 507 | } |
| 508 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 509 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 510 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 511 | for _, onu := range onus { |
| 512 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 513 | return onu, nil |
| 514 | } |
| 515 | } |
| 516 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 517 | err := errors.New("No mathced SN is found ") |
| 518 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 519 | return nil, err |
| 520 | } |
| 521 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 522 | func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) { |
| 523 | return getOnuByID(s.Onumap, onuid) |
| 524 | } |
| 525 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 526 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) { |
| 527 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 528 | for _, onu := range onus { |
| 529 | if onu.OnuID == onuid { |
| 530 | return onu, nil |
| 531 | } |
| 532 | } |
| 533 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 534 | err := errors.New("No matched OnuID is found ") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 535 | logger.WithFields(log.Fields{ |
| 536 | "onumap": onumap, |
| 537 | "onuid": onuid, |
| 538 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 539 | return nil, err |
| 540 | } |
| 541 | |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 542 | func convB2S(b []byte) string { |
| 543 | s := "" |
| 544 | for _, i := range b { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 545 | 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] | 546 | } |
| 547 | return s |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 548 | } |