blob: 50a2224ccc820fe4d90d85dd0240f9d977b0e804 [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 NISHIMOTOdd9f6732019-02-09 09:41:31 +090042 NNI_VETH_NORTH_PFX = "nni_north"
43 NNI_VETH_SOUTH_PFX = "nni_south"
44 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090045)
46
47type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090048 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 Olt *device.Olt
50 Onumap map[uint32][]*device.Onu
51 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090053 gRPCAddress string
54 gRPCPort uint32
55 Vethnames []string
56 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090057 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090058 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090059 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090060 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090061 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080062 omciIn chan openolt.OmciIndication
63 omciOut chan openolt.OmciMsg
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090064 eapolIn chan *byteMsg
65 eapolOut chan *byteMsg
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090066 dhcpIn chan *byteMsg
67 dhcpOut 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 NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900107 dhcpIn: make(chan *byteMsg, 1024),
108 dhcpOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900109 }
110
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900111 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800112 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900116
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900117 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900118 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700119 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900120 oltid := s.Olt.ID
121 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700123 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900124 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900125 return &s
126}
127
128//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800129func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900130 s.wg = &sync.WaitGroup{}
131 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800132 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800133 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900134 logger.Debug("Start() Done")
135 }()
136 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
137 listener, gserver, err := NewGrpcServer(addressport)
138 if err != nil {
139 logger.Error("Failed to create gRPC server", err)
140 return err
141 }
142 s.gRPCserver = gserver
143 openolt.RegisterOpenoltServer(gserver, s)
144 if err := gserver.Serve(listener); err != nil {
145 logger.Error("Failed to run gRPC server", err)
146 return err
147 }
148 s.wg.Wait()
149 return nil
150}
151
152//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800153func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900154 logger.Debug("Stop() Start")
155 defer logger.Debug("Stop() Done")
156 if s.gRPCserver != nil {
157 s.gRPCserver.Stop()
158 logger.Debug("gRPCserver.Stop()")
159 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900160 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900161 return
162}
163
164// Blocking
165func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900166 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800167 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900168 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900169 for intfid, _ := range s.Onumap {
170 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900171 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900172 }
173 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900174 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900175 logger.Debug("Enable() Done")
176 }()
177 logger.Debug("Enable() Start")
178 s.EnableServer = sv
179 if err := s.activateOLT(*sv); err != nil {
180 return err
181 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900182 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900183
184 coreCtx := context.Background()
185 coreCtx, corecancel := context.WithCancel(coreCtx)
186 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900187 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188 return err
189 }
190 return nil
191}
192
193//Non-Blocking
194func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800195 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900196 logger.Debug("Disable() Done")
197 }()
198 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900199 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200}
201
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900202func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900203 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900204 current := dev.GetIntState()
205 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800206 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
207 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900208 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800209 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900210 logger.Debug("ONU State updated to:%d", state)
211 } else {
212 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
213 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214}
215
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900216func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
217 onu, err := s.GetOnuByID(onuid) //TODO: IntfID should be included ?
218 if err != nil {
219 return err
220 }
221 s.updateDevIntState(onu, state)
222 return nil
223}
224
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900225func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900226 defer logger.Debug("activateOLT() Done")
227 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900228 // Activate OLT
229 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900230 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900231 return err
232 }
233 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800234 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900235
236 // OLT sends Interface Indication to Adapter
237 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800238 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239 return err
240 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800241 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900242
243 // OLT sends Operation Indication to Adapter after activating each interface
244 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900245 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800246 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 return err
248 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800249 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900250
251 // OLT sends ONU Discover Indication to Adapter after ONU discovery
252 for intfid, _ := range s.Onumap {
253 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
254 }
255
256 for intfid, _ := range s.Onumap {
257 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800258 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900259 }
260
261 // OLT Sends OnuInd after waiting all of those ONUs up
262 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900263 if IsAllOnuActive(s.Onumap) {
264 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900265 break
266 }
267 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900268
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900269 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900270 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800271 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900272 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900273 return nil
274}
275
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900276// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900277func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
278 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800279 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900280 RemoveVeths(s.Vethnames)
281 s.Vethnames = []string{}
282 s.Ioinfos = []*Ioinfo{}
283 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900284 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900285 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900286 }()
287 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900288 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900289 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800290 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900291 return err
292 }
293 s.Ioinfos = ioinfos
294 s.Vethnames = veths
295 logger.Debug("Created vethnames:%v", s.Vethnames)
296
297 parent := ctx
298 child, cancel := context.WithCancel(parent)
299 s.cancel = cancel
300
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900301 if err = s.runPktLoops(child, stream); err != nil {
302 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303 return err
304 }
305 return nil
306}
307
308//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900309func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900310 if s.cancel != nil {
311 cancel := s.cancel
312 cancel()
313 }
314}
315
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900316func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900317 ioinfos := []*Ioinfo{}
318 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900319 var handler *pcap.Handle
320 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900321 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800322 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900323 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900324 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900325
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900327 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900328 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900329 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900330 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900331}
332
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900333//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900334func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
335 logger.Debug("runPacketPktLoops Start")
336 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900337
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900338 errchOmci := make(chan error)
339 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900340 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900341 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900342
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900343 errchEapol := make(chan error)
344 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
345
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900346 errchDhcp := make(chan error)
347 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
348
Shad Ansarib744bf22019-01-17 11:36:46 -0800349 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900350 logger.Debug("runOMCIResponder Start")
351 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800352 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900353 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800354 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900355 logger.Error("Error happend in Omci:%s", v)
356 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900357 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800358 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900359 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900360 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900361 return nil
362 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900363
Shad Ansarib744bf22019-01-17 11:36:46 -0800364 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900365 logger.Debug("runEapolResponder Start")
366 defer logger.Debug("runEapolResponder Done")
367 select {
368 case v, ok := <-errchEapol:
369 if ok { //Error
370 logger.Error("Error happend in Eapol:%s", v)
371 return v
372 }
373 case <-child.Done():
374 return nil
375 }
376 return nil
377 })
378
379 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900380 logger.Debug("runDhcpResponder Start")
381 defer logger.Debug("runDhcpResponder Done")
382 select {
383 case v, ok := <-errchDhcp:
384 if ok { //Error
385 logger.Error("Error happend in Dhcp:%s", v)
386 return v
387 }
388 case <-child.Done():
389 return nil
390 }
391 return nil
392 })
393
394 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900395 err := s.runMainPktLoop(child, stream)
396 return err
397 })
398
399 if err := eg.Wait(); err != nil {
400 logger.Error("Error happend in runPacketLoops:%s", err)
401 cancel()
402 }
403 return nil
404}
405
406func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900407 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900408 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900409 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900410 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900411 ioinfo, err := s.IdentifyNniIoinfo("inside")
412 if err != nil {
413 return err
414 }
415 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
416 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800417 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418 close(nnichannel)
419 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900420 logger.Debug("BEFORE OLT_ACTIVE")
421 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
422 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900423 data := &openolt.Indication_PktInd{}
424 for {
425 select {
426 case msg := <-s.omciIn:
427 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
428 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
429 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
430 logger.Error("send omci indication failed.", err)
431 continue
432 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900433 case msg := <- s.eapolIn:
434 intfid := msg.IntfId
435 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900436 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900437 if err != nil {
438 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
439 continue
440 }
441
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900442 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 +0900443
444 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
445 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
446 logger.Error("Fail to send EAPOL PktInd indication.", err)
447 return err
448 }
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900449 case msg := <- s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether
450 intfid := msg.IntfId
451 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900452 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900453 bytes := msg.Byte
454 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
455
456 if err != nil {
457 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
458 continue
459 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900460
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900461 onu, err := s.GetOnuByID(onuid)
462 if err != nil {
463 logger.Error("Failed to GetOnuByID:%d", onuid)
464 continue
465 }
466 sn := convB2S(onu.SerialNumber.VendorSpecific)
467 if ctag, ok := s.CtagMap[sn]; ok == true {
468 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
469 if err != nil {
470 utils.LoggerWithOnu(onu).WithFields(log.Fields{
471 "gemId": gemid,
472 }).Error("Fail to tag C-tag")
473 } else {
474 pkt = tagpkt
475 }
476 } else {
477 utils.LoggerWithOnu(onu).WithFields(log.Fields{
478 "gemId": gemid,
479 "cTagMap": s.CtagMap,
480 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
481 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900482
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900483 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
484 logger.Debug(pkt.Dump())
485
486 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
487 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
488 logger.Error("Fail to send DHCP PktInd indication.", err)
489 return err
490 }
491
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900492 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900493 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900494 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
495 logger.Debug("WARNING: This packet does not come from NNI ")
496 continue
497 }
498 onuid := nnipkt.Info.onuid
499 onu, _ := s.GetOnuByID(onuid)
500
501 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
502 intfid := nnipkt.Info.intfid
503 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900504 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
505 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
506 logger.Error("Fail to send PktInd indication.", err)
507 return err
508 }
509
510 case <-ctx.Done():
511 logger.Debug("Closed nnichannel ")
512 return nil
513 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900514 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900515 return nil
516}
517
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900518func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
519 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800520 onu, _ := s.GetOnuByID(onuid)
521
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900522 if layerEth != nil {
523 pkt, _ := layerEth.(*layers.Ethernet)
524 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900525 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800526 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900527 eapolPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900528 s.eapolOut <- &eapolPkt
529 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900530 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800531 utils.LoggerWithOnu(onu).WithFields(log.Fields{
532 "payload": layerDHCP.LayerPayload(),
533 "type": layerDHCP.LayerType().String(),
534 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900535 rawpkt, _, _ = PopVLAN(rawpkt)
536 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900537 logger.Debug("%s", rawpkt.Dump())
538 dhcpPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
539 s.dhcpOut <- &dhcpPkt
540 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900541 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800542 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900543 return nil
544 }
545 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
546 if err != nil {
547 return err
548 }
549 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800550 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900551 return nil
552 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800553 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900554 return nil
555}
556
557func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900558 poppkt, _, err := PopVLAN(rawpkt)
559 poppkt, _, err = PopVLAN(poppkt)
560 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900561 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900562 return err
563 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900564 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900565 if err != nil {
566 return err
567 }
568 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900569 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570 SendNni(handle, poppkt)
571 return nil
572}
573
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900574func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
575 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900576 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900577 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900578 return false
579 }
580 }
581 }
582 return true
583}
584
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900585func (s *Server) isAllOnuOmciActive() bool {
586 for _, onus := range s.Onumap {
587 for _, onu := range onus{
588 if onu.GetIntState() != device.ONU_OMCIACTIVE {
589 return false
590 }
591 }
592 }
593 return true
594}
595
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900596func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900597 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
598 gemportid, err := omci.GetGemPortId(intfid, onuid)
599 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900600 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
601 }
602 onu, err := s.GetOnuByID(onuid)
603 if err != nil {
604 logger.Error("Failed to getGemPortID: %s", err)
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900605 return 0, err
606 }
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900607 gemportid = onu.GemportID
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900608 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900609}
610
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900611func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
612 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900613 for _, onu := range onus {
614 if device.ValidateSN(*sn, *onu.SerialNumber) {
615 return onu, nil
616 }
617 }
618 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900619 err := errors.New("No mathced SN is found ")
620 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900621 return nil, err
622}
623
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800624func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
625 return getOnuByID(s.Onumap, onuid)
626}
627
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900628func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
629 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900630 for _, onu := range onus {
631 if onu.OnuID == onuid {
632 return onu, nil
633 }
634 }
635 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900636 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800637 logger.WithFields(log.Fields{
638 "onumap": onumap,
639 "onuid": onuid,
640 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900641 return nil, err
642}
643
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900644func convB2S(b []byte) string {
645 s := ""
646 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700647 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900648 }
649 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700650}