blob: 0f6b8f4c9da290b41bfbcc144a060a9d1b90496b [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 NISHIMOTOdad44cb2019-02-08 09:45:40 +090066 eapolIn chan *byteMsg
67 eapolOut chan *byteMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068}
69
70type Packet struct {
71 Info *Ioinfo
72 Pkt gopacket.Packet
73}
74
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090075type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090076 IntfId uint32
77 OnuId uint32
78 Byte []byte
79}
80
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090081type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080082 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090083 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080084 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090085}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090086
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090087func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088 // TODO: make it decent
89 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080090 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090091 nonus := opt.nonus
92 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080093 Olt: device.NewOlt(oltid, npon, 1),
94 Onumap: make(map[uint32][]*device.Onu),
95 Ioinfos: []*Ioinfo{},
96 gRPCAddress: opt.address,
97 gRPCPort: opt.port,
98 Vethnames: []string{},
99 IndInterval: opt.intvl,
100 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -0800101 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -0800102 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900103 omciIn: make(chan openolt.OmciIndication, 1024),
104 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900105 eapolIn: make(chan *byteMsg, 1024),
106 eapolOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900107 }
108
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900109 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800110 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900111 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900112 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900114
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900116 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700117 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900118 oltid := s.Olt.ID
119 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700121 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900122 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900123 return &s
124}
125
126//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800127func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900128 s.wg = &sync.WaitGroup{}
129 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800130 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800131 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900132 logger.Debug("Start() Done")
133 }()
134 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
135 listener, gserver, err := NewGrpcServer(addressport)
136 if err != nil {
137 logger.Error("Failed to create gRPC server", err)
138 return err
139 }
140 s.gRPCserver = gserver
141 openolt.RegisterOpenoltServer(gserver, s)
142 if err := gserver.Serve(listener); err != nil {
143 logger.Error("Failed to run gRPC server", err)
144 return err
145 }
146 s.wg.Wait()
147 return nil
148}
149
150//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800151func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900152 logger.Debug("Stop() Start")
153 defer logger.Debug("Stop() Done")
154 if s.gRPCserver != nil {
155 s.gRPCserver.Stop()
156 logger.Debug("gRPCserver.Stop()")
157 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900158 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900159 return
160}
161
162// Blocking
163func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900164 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800165 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900166 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900167 for intfid, _ := range s.Onumap {
168 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900169 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900170 }
171 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900172 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900173 logger.Debug("Enable() Done")
174 }()
175 logger.Debug("Enable() Start")
176 s.EnableServer = sv
177 if err := s.activateOLT(*sv); err != nil {
178 return err
179 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900180 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900181
182 coreCtx := context.Background()
183 coreCtx, corecancel := context.WithCancel(coreCtx)
184 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900185 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900186 return err
187 }
188 return nil
189}
190
191//Non-Blocking
192func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800193 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194 logger.Debug("Disable() Done")
195 }()
196 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900197 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900198}
199
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900200func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
201 current := dev.GetIntState()
202 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800203 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
204 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900205 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800206 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900207 logger.Debug("ONU State updated to:%d", state)
208 } else {
209 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
210 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900211}
212
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900213func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
214 onu, err := s.GetOnuByID(onuid) //TODO: IntfID should be included ?
215 if err != nil {
216 return err
217 }
218 s.updateDevIntState(onu, state)
219 return nil
220}
221
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900222func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900223 defer logger.Debug("activateOLT() Done")
224 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900225 // Activate OLT
226 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900227 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900228 return err
229 }
230 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800231 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900232
233 // OLT sends Interface Indication to Adapter
234 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800235 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900236 return err
237 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800238 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239
240 // OLT sends Operation Indication to Adapter after activating each interface
241 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900242 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800243 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900244 return err
245 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800246 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247
248 // OLT sends ONU Discover Indication to Adapter after ONU discovery
249 for intfid, _ := range s.Onumap {
250 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
251 }
252
253 for intfid, _ := range s.Onumap {
254 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800255 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900256 }
257
258 // OLT Sends OnuInd after waiting all of those ONUs up
259 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900260 if IsAllOnuActive(s.Onumap) {
261 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900262 break
263 }
264 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900265
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900266 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900267 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800268 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900269 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900270 return nil
271}
272
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900273// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900274func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
275 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800276 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900277 RemoveVeths(s.Vethnames)
278 s.Vethnames = []string{}
279 s.Ioinfos = []*Ioinfo{}
280 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900281 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900282 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900283 }()
284 s.wg.Add(1)
285 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
286 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800287 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900288 return err
289 }
290 s.Ioinfos = ioinfos
291 s.Vethnames = veths
292 logger.Debug("Created vethnames:%v", s.Vethnames)
293
294 parent := ctx
295 child, cancel := context.WithCancel(parent)
296 s.cancel = cancel
297
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900298 if err = s.runPktLoops(child, stream); err != nil {
299 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900300 return err
301 }
302 return nil
303}
304
305//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900306func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900307 if s.cancel != nil {
308 cancel := s.cancel
309 cancel()
310 }
311}
312
313func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900314 ioinfos := []*Ioinfo{}
315 var err error
316 for intfid, _ := range onumap {
317 for i := 0; i < len(onumap[intfid]); i++ {
318 var handler *pcap.Handle
319 onuid := onumap[intfid][i].OnuID
320 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900321 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800322 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900323 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900324 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900325 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900326 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900327 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900328 ioinfos = append(ioinfos, &oinfo)
329 }
330 }
331
332 var handler *pcap.Handle
333 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900334 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800335 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900336 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900337 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900338
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900339 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900340 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900341 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900342 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900343 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900344}
345
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900346//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900347func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
348 logger.Debug("runPacketPktLoops Start")
349 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900350
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900351 errchOmci := make(chan error)
352 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900353 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900354 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900355
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900356 errchEapol := make(chan error)
357 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
358
Shad Ansarib744bf22019-01-17 11:36:46 -0800359 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900360 logger.Debug("runOMCIResponder Start")
361 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800362 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900363 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800364 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900365 logger.Error("Error happend in Omci:%s", v)
366 return v
Shad Ansarib744bf22019-01-17 11:36:46 -0800367 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900368 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900369 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800370 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900371 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900372 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900373 return nil
374 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900375
Shad Ansarib744bf22019-01-17 11:36:46 -0800376 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900377 logger.Debug("runEapolResponder Start")
378 defer logger.Debug("runEapolResponder Done")
379 select {
380 case v, ok := <-errchEapol:
381 if ok { //Error
382 logger.Error("Error happend in Eapol:%s", v)
383 return v
384 }
385 case <-child.Done():
386 return nil
387 }
388 return nil
389 })
390
391 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900392 err := s.runMainPktLoop(child, stream)
393 return err
394 })
395
396 if err := eg.Wait(); err != nil {
397 logger.Error("Error happend in runPacketLoops:%s", err)
398 cancel()
399 }
400 return nil
401}
402
403func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
404 unichannel := make(chan Packet, 2048)
405 defer func() {
406 close(unichannel)
407 logger.Debug("Closed unichannel ")
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900408 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900409 }()
410 for intfid, _ := range s.Onumap {
411 for _, onu := range s.Onumap[intfid] {
412 onuid := onu.OnuID
413 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
414 if err != nil {
415 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
416 return err
Shad Ansarief436272018-11-14 15:58:20 -0800417 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418 uhandler := ioinfo.handler
419 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900420 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900421 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900422
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900423 ioinfo, err := s.IdentifyNniIoinfo("inside")
424 if err != nil {
425 return err
426 }
427 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
428 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800429 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900430 close(nnichannel)
431 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900432
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900433 data := &openolt.Indication_PktInd{}
434 for {
435 select {
436 case msg := <-s.omciIn:
437 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
438 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
439 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
440 logger.Error("send omci indication failed.", err)
441 continue
442 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900443 case msg := <- s.eapolIn:
444 intfid := msg.IntfId
445 onuid := msg.OnuId
446 gemid, err := getGemPortID(intfid, onuid)
447 if err != nil {
448 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
449 continue
450 }
451
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900452 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900453
454 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
455 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
456 logger.Error("Fail to send EAPOL PktInd indication.", err)
457 return err
458 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900459 case unipkt := <-unichannel:
460 onuid := unipkt.Info.onuid
461 onu, _ := s.GetOnuByID(onuid)
462 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
463 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
464 logger.Debug("WARNING: This packet does not come from UNI ")
465 continue
466 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900467
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900468 intfid := unipkt.Info.intfid
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900469 gemid, err := getGemPortID(intfid, onuid)
470 if err != nil {
471 continue
472 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900473 pkt := unipkt.Pkt
474 layerEth := pkt.Layer(layers.LayerTypeEthernet)
475 le, _ := layerEth.(*layers.Ethernet)
476 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900477
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900478 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900479 utils.LoggerWithOnu(onu).WithFields(log.Fields{
480 "gemId": gemid,
481 }).Info("Received upstream packet is EAPOL.")
482 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
483 utils.LoggerWithOnu(onu).WithFields(log.Fields{
484 "gemId": gemid,
485 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900486
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900487 //C-TAG
488 sn := convB2S(onu.SerialNumber.VendorSpecific)
489 if ctag, ok := s.CtagMap[sn]; ok == true {
490 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
491 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900492 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900493 "gemId": gemid,
494 }).Error("Fail to tag C-tag")
495 } else {
496 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900497 }
498 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800499 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900500 "gemId": gemid,
501 "cTagMap": s.CtagMap,
502 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900503 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900504 } else {
505 utils.LoggerWithOnu(onu).WithFields(log.Fields{
506 "gemId": gemid,
507 }).Info("Received upstream packet is of unknow type, skipping.")
508 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900509 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900510
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900511 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
512 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
513 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
514 logger.Error("Fail to send PktInd indication.", err)
515 return err
516 }
517
518 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900519 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900520 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
521 logger.Debug("WARNING: This packet does not come from NNI ")
522 continue
523 }
524 onuid := nnipkt.Info.onuid
525 onu, _ := s.GetOnuByID(onuid)
526
527 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
528 intfid := nnipkt.Info.intfid
529 pkt := nnipkt.Pkt
530 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
531 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
532 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
533 logger.Error("Fail to send PktInd indication.", err)
534 return err
535 }
536
537 case <-ctx.Done():
538 logger.Debug("Closed nnichannel ")
539 return nil
540 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900541 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900542 return nil
543}
544
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900545func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
546 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800547 onu, _ := s.GetOnuByID(onuid)
548
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900549 if layerEth != nil {
550 pkt, _ := layerEth.(*layers.Ethernet)
551 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900552 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800553 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900554 eapolPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900555 s.eapolOut <- &eapolPkt
556 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800558 utils.LoggerWithOnu(onu).WithFields(log.Fields{
559 "payload": layerDHCP.LayerPayload(),
560 "type": layerDHCP.LayerType().String(),
561 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900562 rawpkt, _, _ = PopVLAN(rawpkt)
563 rawpkt, _, _ = PopVLAN(rawpkt)
564 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800565 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900566 return nil
567 }
568 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
569 if err != nil {
570 return err
571 }
572 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800573 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900574 return nil
575 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800576 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900577 return nil
578}
579
580func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900581 poppkt, _, err := PopVLAN(rawpkt)
582 poppkt, _, err = PopVLAN(poppkt)
583 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900584 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900585 return err
586 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900587 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900588 if err != nil {
589 return err
590 }
591 handle := ioinfo.handler
592 SendNni(handle, poppkt)
593 return nil
594}
595
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900596func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
597 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900598 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900599 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900600 return false
601 }
602 }
603 }
604 return true
605}
606
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900607func (s *Server) isAllOnuOmciActive() bool {
608 for _, onus := range s.Onumap {
609 for _, onu := range onus{
610 if onu.GetIntState() != device.ONU_OMCIACTIVE {
611 return false
612 }
613 }
614 }
615 return true
616}
617
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900618func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900619 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
620 gemportid, err := omci.GetGemPortId(intfid, onuid)
621 if err != nil {
622 logger.Error("%s", err)
623 return 0, err
624 }
625 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900626}
627
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900628func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
629 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900630 for _, onu := range onus {
631 if device.ValidateSN(*sn, *onu.SerialNumber) {
632 return onu, nil
633 }
634 }
635 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900636 err := errors.New("No mathced SN is found ")
637 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900638 return nil, err
639}
640
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800641func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
642 return getOnuByID(s.Onumap, onuid)
643}
644
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900645func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
646 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900647 for _, onu := range onus {
648 if onu.OnuID == onuid {
649 return onu, nil
650 }
651 }
652 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900653 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800654 logger.WithFields(log.Fields{
655 "onumap": onumap,
656 "onuid": onuid,
657 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900658 return nil, err
659}
660
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900661func convB2S(b []byte) string {
662 s := ""
663 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700664 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900665 }
666 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700667}