blob: f6d0dbb35877d92171359bde6ada4eb1a6dc3a54 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package core
18
19import (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090020 "context"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070021 "errors"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 "strconv"
23 "sync"
24
Shad Ansarib744bf22019-01-17 11:36:46 -080025 omci "github.com/opencord/omci-sim"
26
27 "reflect"
28
Matteo Scandolo88e91892018-11-06 16:29:19 -080029 "gerrit.opencord.org/voltha-bbsim/common/logger"
30 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "gerrit.opencord.org/voltha-bbsim/device"
32 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 "github.com/google/gopacket"
34 "github.com/google/gopacket/layers"
35 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080038 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090042 UNI_VETH_UP_PFX = "sim_uu"
43 UNI_VETH_DW_PFX = "sim_ud"
44 NNI_VETH_UP_PFX = "sim_nu"
45 NNI_VETH_DW_PFX = "sim_nd"
46 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047)
48
49type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090050 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 Olt *device.Olt
52 Onumap map[uint32][]*device.Onu
53 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055 gRPCAddress string
56 gRPCPort uint32
57 Vethnames []string
58 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090059 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090060 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090061 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090063 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080064 omciIn chan openolt.OmciIndication
65 omciOut chan openolt.OmciMsg
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090066 eapolIn chan *EAPByte
67 eapolOut chan *EAPPkt
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068}
69
70type Packet struct {
71 Info *Ioinfo
72 Pkt gopacket.Packet
73}
74
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090075type EAPByte struct {
76 IntfId uint32
77 OnuId uint32
78 Byte []byte
79}
80
81type EAPPkt struct {
82 IntfId uint32
83 OnuId uint32
84 Pkt gopacket.Packet
85}
86
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090087type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080088 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090089 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080090 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090091}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090092
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090093func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094 // TODO: make it decent
95 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080096 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 nonus := opt.nonus
98 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080099 Olt: device.NewOlt(oltid, npon, 1),
100 Onumap: make(map[uint32][]*device.Onu),
101 Ioinfos: []*Ioinfo{},
102 gRPCAddress: opt.address,
103 gRPCPort: opt.port,
104 Vethnames: []string{},
105 IndInterval: opt.intvl,
106 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -0800107 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -0800108 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900109 omciIn: make(chan openolt.OmciIndication, 1024),
110 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900111 eapolIn: make(chan *EAPByte, 1024),
112 eapolOut: make(chan *EAPPkt, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 }
114
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800116 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900117 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900118 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900119 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900120
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900121 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900122 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700123 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900124 oltid := s.Olt.ID
125 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900126 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700127 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900128 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900129 return &s
130}
131
132//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800133func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 s.wg = &sync.WaitGroup{}
135 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800136 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800137 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900138 logger.Debug("Start() Done")
139 }()
140 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
141 listener, gserver, err := NewGrpcServer(addressport)
142 if err != nil {
143 logger.Error("Failed to create gRPC server", err)
144 return err
145 }
146 s.gRPCserver = gserver
147 openolt.RegisterOpenoltServer(gserver, s)
148 if err := gserver.Serve(listener); err != nil {
149 logger.Error("Failed to run gRPC server", err)
150 return err
151 }
152 s.wg.Wait()
153 return nil
154}
155
156//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800157func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900158 logger.Debug("Stop() Start")
159 defer logger.Debug("Stop() Done")
160 if s.gRPCserver != nil {
161 s.gRPCserver.Stop()
162 logger.Debug("gRPCserver.Stop()")
163 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900164 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900165 return
166}
167
168// Blocking
169func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800171 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900172 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900173 for intfid, _ := range s.Onumap {
174 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900175 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900176 }
177 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900178 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900179 logger.Debug("Enable() Done")
180 }()
181 logger.Debug("Enable() Start")
182 s.EnableServer = sv
183 if err := s.activateOLT(*sv); err != nil {
184 return err
185 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900186 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900187
188 coreCtx := context.Background()
189 coreCtx, corecancel := context.WithCancel(coreCtx)
190 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900191 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 return err
193 }
194 return nil
195}
196
197//Non-Blocking
198func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800199 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 logger.Debug("Disable() Done")
201 }()
202 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900203 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900204}
205
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900206func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
207 current := dev.GetIntState()
208 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800209 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
210 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900211 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800212 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900213 logger.Debug("ONU State updated to:%d", state)
214 } else {
215 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
216 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217}
218
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900219func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
220 onu, err := s.GetOnuByID(onuid) //TODO: IntfID should be included ?
221 if err != nil {
222 return err
223 }
224 s.updateDevIntState(onu, state)
225 return nil
226}
227
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900228func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900229 defer logger.Debug("activateOLT() Done")
230 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900231 // Activate OLT
232 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900233 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900234 return err
235 }
236 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800237 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900238
239 // OLT sends Interface Indication to Adapter
240 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800241 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900242 return err
243 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800244 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900245
246 // OLT sends Operation Indication to Adapter after activating each interface
247 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900248 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800249 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900250 return err
251 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800252 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900253
254 // OLT sends ONU Discover Indication to Adapter after ONU discovery
255 for intfid, _ := range s.Onumap {
256 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
257 }
258
259 for intfid, _ := range s.Onumap {
260 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800261 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900262 }
263
264 // OLT Sends OnuInd after waiting all of those ONUs up
265 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900266 if IsAllOnuActive(s.Onumap) {
267 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900268 break
269 }
270 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900271
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900272 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900273 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800274 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900275 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900276 return nil
277}
278
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900279// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900280func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
281 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800282 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900283 RemoveVeths(s.Vethnames)
284 s.Vethnames = []string{}
285 s.Ioinfos = []*Ioinfo{}
286 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900287 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900288 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900289 }()
290 s.wg.Add(1)
291 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
292 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800293 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900294 return err
295 }
296 s.Ioinfos = ioinfos
297 s.Vethnames = veths
298 logger.Debug("Created vethnames:%v", s.Vethnames)
299
300 parent := ctx
301 child, cancel := context.WithCancel(parent)
302 s.cancel = cancel
303
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900304 if err = s.runPktLoops(child, stream); err != nil {
305 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900306 return err
307 }
308 return nil
309}
310
311//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900312func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900313 if s.cancel != nil {
314 cancel := s.cancel
315 cancel()
316 }
317}
318
319func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900320 ioinfos := []*Ioinfo{}
321 var err error
322 for intfid, _ := range onumap {
323 for i := 0; i < len(onumap[intfid]); i++ {
324 var handler *pcap.Handle
325 onuid := onumap[intfid][i].OnuID
326 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900327 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800328 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900329 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900330 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900333 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900334 ioinfos = append(ioinfos, &oinfo)
335 }
336 }
337
338 var handler *pcap.Handle
339 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900340 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800341 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900342 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900343 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900344
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900345 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900346 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900347 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900348 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900349 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900350}
351
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900352//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900353func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
354 logger.Debug("runPacketPktLoops Start")
355 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900356
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900357 errchOmci := make(chan error)
358 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900359 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900360 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900361
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900362 errchEapol := make(chan error)
363 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
364
Shad Ansarib744bf22019-01-17 11:36:46 -0800365 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900366 logger.Debug("runOMCIResponder Start")
367 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800368 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900369 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800370 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900371 logger.Error("Error happend in Omci:%s", v)
372 return v
Shad Ansarib744bf22019-01-17 11:36:46 -0800373 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900374 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900375 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800376 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900377 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900378 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900379 return nil
380 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900381
Shad Ansarib744bf22019-01-17 11:36:46 -0800382 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900383 logger.Debug("runEapolResponder Start")
384 defer logger.Debug("runEapolResponder Done")
385 select {
386 case v, ok := <-errchEapol:
387 if ok { //Error
388 logger.Error("Error happend in Eapol:%s", v)
389 return v
390 }
391 case <-child.Done():
392 return nil
393 }
394 return nil
395 })
396
397 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900398 err := s.runMainPktLoop(child, stream)
399 return err
400 })
401
402 if err := eg.Wait(); err != nil {
403 logger.Error("Error happend in runPacketLoops:%s", err)
404 cancel()
405 }
406 return nil
407}
408
409func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
410 unichannel := make(chan Packet, 2048)
411 defer func() {
412 close(unichannel)
413 logger.Debug("Closed unichannel ")
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900414 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900415 }()
416 for intfid, _ := range s.Onumap {
417 for _, onu := range s.Onumap[intfid] {
418 onuid := onu.OnuID
419 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
420 if err != nil {
421 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
422 return err
Shad Ansarief436272018-11-14 15:58:20 -0800423 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900424 uhandler := ioinfo.handler
425 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900426 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900427 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900428
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900429 ioinfo, err := s.IdentifyNniIoinfo("inside")
430 if err != nil {
431 return err
432 }
433 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
434 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800435 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900436 close(nnichannel)
437 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900438
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900439 data := &openolt.Indication_PktInd{}
440 for {
441 select {
442 case msg := <-s.omciIn:
443 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
444 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
445 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
446 logger.Error("send omci indication failed.", err)
447 continue
448 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900449 case msg := <- s.eapolIn:
450 intfid := msg.IntfId
451 onuid := msg.OnuId
452 gemid, err := getGemPortID(intfid, onuid)
453 if err != nil {
454 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
455 continue
456 }
457
458 logger.Debug("OLT %d send eapol packet out (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
459
460 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
461 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
462 logger.Error("Fail to send EAPOL PktInd indication.", err)
463 return err
464 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900465 case unipkt := <-unichannel:
466 onuid := unipkt.Info.onuid
467 onu, _ := s.GetOnuByID(onuid)
468 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
469 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
470 logger.Debug("WARNING: This packet does not come from UNI ")
471 continue
472 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900473
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900474 intfid := unipkt.Info.intfid
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900475 gemid, err := getGemPortID(intfid, onuid)
476 if err != nil {
477 continue
478 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900479 pkt := unipkt.Pkt
480 layerEth := pkt.Layer(layers.LayerTypeEthernet)
481 le, _ := layerEth.(*layers.Ethernet)
482 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900483
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900484 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900485 utils.LoggerWithOnu(onu).WithFields(log.Fields{
486 "gemId": gemid,
487 }).Info("Received upstream packet is EAPOL.")
488 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
489 utils.LoggerWithOnu(onu).WithFields(log.Fields{
490 "gemId": gemid,
491 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900492
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900493 //C-TAG
494 sn := convB2S(onu.SerialNumber.VendorSpecific)
495 if ctag, ok := s.CtagMap[sn]; ok == true {
496 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
497 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900498 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900499 "gemId": gemid,
500 }).Error("Fail to tag C-tag")
501 } else {
502 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900503 }
504 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800505 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900506 "gemId": gemid,
507 "cTagMap": s.CtagMap,
508 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900509 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900510 } else {
511 utils.LoggerWithOnu(onu).WithFields(log.Fields{
512 "gemId": gemid,
513 }).Info("Received upstream packet is of unknow type, skipping.")
514 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900515 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900516
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900517 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
518 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
519 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
520 logger.Error("Fail to send PktInd indication.", err)
521 return err
522 }
523
524 case nnipkt := <-nnichannel:
525 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
526 logger.Debug("WARNING: This packet does not come from NNI ")
527 continue
528 }
529 onuid := nnipkt.Info.onuid
530 onu, _ := s.GetOnuByID(onuid)
531
532 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
533 intfid := nnipkt.Info.intfid
534 pkt := nnipkt.Pkt
535 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
536 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
537 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
538 logger.Error("Fail to send PktInd indication.", err)
539 return err
540 }
541
542 case <-ctx.Done():
543 logger.Debug("Closed nnichannel ")
544 return nil
545 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900546 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900547 return nil
548}
549
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900550func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
551 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800552 onu, _ := s.GetOnuByID(onuid)
553
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900554 if layerEth != nil {
555 pkt, _ := layerEth.(*layers.Ethernet)
556 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900557 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800558 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900559 eapolPkt := EAPPkt{IntfId:intfid, OnuId:onuid, Pkt: rawpkt}
560 s.eapolOut <- &eapolPkt
561 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900562 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800563 utils.LoggerWithOnu(onu).WithFields(log.Fields{
564 "payload": layerDHCP.LayerPayload(),
565 "type": layerDHCP.LayerType().String(),
566 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900567 rawpkt, _, _ = PopVLAN(rawpkt)
568 rawpkt, _, _ = PopVLAN(rawpkt)
569 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800570 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900571 return nil
572 }
573 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
574 if err != nil {
575 return err
576 }
577 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800578 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900579 return nil
580 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800581 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900582 return nil
583}
584
585func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900586 poppkt, _, err := PopVLAN(rawpkt)
587 poppkt, _, err = PopVLAN(poppkt)
588 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900589 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900590 return err
591 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900592 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900593 if err != nil {
594 return err
595 }
596 handle := ioinfo.handler
597 SendNni(handle, poppkt)
598 return nil
599}
600
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900601func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
602 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900603 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900604 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900605 return false
606 }
607 }
608 }
609 return true
610}
611
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900612func (s *Server) isAllOnuOmciActive() bool {
613 for _, onus := range s.Onumap {
614 for _, onu := range onus{
615 if onu.GetIntState() != device.ONU_OMCIACTIVE {
616 return false
617 }
618 }
619 }
620 return true
621}
622
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900623func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900624 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
625 gemportid, err := omci.GetGemPortId(intfid, onuid)
626 if err != nil {
627 logger.Error("%s", err)
628 return 0, err
629 }
630 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900631}
632
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900633func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
634 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900635 for _, onu := range onus {
636 if device.ValidateSN(*sn, *onu.SerialNumber) {
637 return onu, nil
638 }
639 }
640 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900641 err := errors.New("No mathced SN is found ")
642 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900643 return nil, err
644}
645
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800646func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
647 return getOnuByID(s.Onumap, onuid)
648}
649
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900650func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
651 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900652 for _, onu := range onus {
653 if onu.OnuID == onuid {
654 return onu, nil
655 }
656 }
657 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900658 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800659 logger.WithFields(log.Fields{
660 "onumap": onumap,
661 "onuid": onuid,
662 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900663 return nil, err
664}
665
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900666func convB2S(b []byte) string {
667 s := ""
668 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700669 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900670 }
671 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700672}