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