blob: 917ed257e2d5315d03ad88c491a95a32db61330a [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"
Shad Ansarib744bf22019-01-17 11:36:46 -080024 "reflect"
25
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020026 omci "github.com/opencord/omci-sim"
Matteo Scandolo88e91892018-11-06 16:29:19 -080027 "gerrit.opencord.org/voltha-bbsim/common/logger"
28 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029 "gerrit.opencord.org/voltha-bbsim/device"
30 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "github.com/google/gopacket"
32 "github.com/google/gopacket/layers"
33 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080034 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090035 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080036 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037)
38
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039const (
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +090040 NNI_VETH_NORTH_PFX = "nni_north"
41 NNI_VETH_SOUTH_PFX = "nni_south"
42 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090043)
44
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020045// Server structure consists of all the params required for BBsim.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090047 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090048 Olt *device.Olt
49 Onumap map[uint32][]*device.Onu
50 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090052 gRPCAddress string
53 gRPCPort uint32
54 Vethnames []string
55 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090056 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090057 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090058 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090059 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090060 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080061 omciIn chan openolt.OmciIndication
62 omciOut chan openolt.OmciMsg
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090063 eapolIn chan *byteMsg
64 eapolOut chan *byteMsg
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090065 dhcpIn chan *byteMsg
66 dhcpOut chan *byteMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067}
68
69type Packet struct {
70 Info *Ioinfo
71 Pkt gopacket.Packet
72}
73
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090074type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090075 IntfId uint32
76 OnuId uint32
77 Byte []byte
78}
79
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090080type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080081 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090082 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080083 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090084}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090085
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020086// NewCore initialize OLT and ONU objects
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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200128// Start starts the BBSim and openolt gRPC servers (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 {
Zack Williamsb85f5932019-05-10 16:21:35 -0700139 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900140 return err
141 }
142 s.gRPCserver = gserver
143 openolt.RegisterOpenoltServer(gserver, s)
144 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700145 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900146 return err
147 }
148 s.wg.Wait()
149 return nil
150}
151
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200152// Stop stops the BBSim and openolt gRPC servers (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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200164// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900165func (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()
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200169 for intfid := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900170 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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200193// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194func (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 {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200217 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900218 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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200252 for intfid := range s.Onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900253 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
254 }
255
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200256 // Initialize all ONUs
257 for intfid := range s.Onumap {
258 for _, onu := range s.Onumap[intfid] {
259 onu.Initialize()
260 }
261 }
262
263 // Send discovery indication for all ONUs
264 for intfid := range s.Onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900265 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800266 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900267 }
268
269 // OLT Sends OnuInd after waiting all of those ONUs up
270 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900271 if IsAllOnuActive(s.Onumap) {
272 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900273 break
274 }
275 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900276
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200277 for intfid := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900278 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800279 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900280 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900281 return nil
282}
283
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200284// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900285func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
286 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800287 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900288 RemoveVeths(s.Vethnames)
289 s.Vethnames = []string{}
290 s.Ioinfos = []*Ioinfo{}
291 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900292 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900293 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900294 }()
295 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900296 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900297 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700298 logger.Error("createIoinfos failed: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900299 return err
300 }
301 s.Ioinfos = ioinfos
302 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700303 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304
305 parent := ctx
306 child, cancel := context.WithCancel(parent)
307 s.cancel = cancel
308
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900309 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700310 logger.Error("runPktLoops failed: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900311 return err
312 }
313 return nil
314}
315
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200316// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900317func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900318 if s.cancel != nil {
319 cancel := s.cancel
320 cancel()
321 }
322}
323
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900324func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900325 ioinfos := []*Ioinfo{}
326 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900327 var handler *pcap.Handle
328 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900329 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700330 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900333
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900334 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900335 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900336 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900337 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900338 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900339}
340
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900341//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900342func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
343 logger.Debug("runPacketPktLoops Start")
344 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900345
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900346 errchOmci := make(chan error)
347 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900348 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900349 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900350
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900351 errchEapol := make(chan error)
352 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
353
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900354 errchDhcp := make(chan error)
355 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
356
Shad Ansarib744bf22019-01-17 11:36:46 -0800357 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900358 logger.Debug("runOMCIResponder Start")
359 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800360 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900361 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800362 if ok { //Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700363 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900364 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900365 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800366 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900367 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900368 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900369 return nil
370 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900371
Shad Ansarib744bf22019-01-17 11:36:46 -0800372 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900373 logger.Debug("runEapolResponder Start")
374 defer logger.Debug("runEapolResponder Done")
375 select {
376 case v, ok := <-errchEapol:
377 if ok { //Error
378 logger.Error("Error happend in Eapol:%s", v)
379 return v
380 }
381 case <-child.Done():
382 return nil
383 }
384 return nil
385 })
386
387 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900388 logger.Debug("runDhcpResponder Start")
389 defer logger.Debug("runDhcpResponder Done")
390 select {
391 case v, ok := <-errchDhcp:
392 if ok { //Error
393 logger.Error("Error happend in Dhcp:%s", v)
394 return v
395 }
396 case <-child.Done():
397 return nil
398 }
399 return nil
400 })
401
402 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900403 err := s.runMainPktLoop(child, stream)
404 return err
405 })
406
407 if err := eg.Wait(); err != nil {
408 logger.Error("Error happend in runPacketLoops:%s", err)
409 cancel()
410 }
411 return nil
412}
413
414func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900415 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900416 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900417 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900419 ioinfo, err := s.IdentifyNniIoinfo("inside")
420 if err != nil {
421 return err
422 }
423 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
424 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800425 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900426 close(nnichannel)
427 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900428 logger.Debug("BEFORE OLT_ACTIVE")
429 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
430 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900431 data := &openolt.Indication_PktInd{}
432 for {
433 select {
434 case msg := <-s.omciIn:
435 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
436 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
437 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700438 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900439 continue
440 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900441 case msg := <- s.eapolIn:
442 intfid := msg.IntfId
443 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900444 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900445 if err != nil {
446 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
447 continue
448 }
449
Zack Williamsb85f5932019-05-10 16:21:35 -0700450 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, msg.Byte)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900451
452 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
453 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700454 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900455 return err
456 }
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900457 case msg := <- s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether
458 intfid := msg.IntfId
459 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900460 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900461 bytes := msg.Byte
462 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
463
464 if err != nil {
465 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
466 continue
467 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900468
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200469 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900470 if err != nil {
471 logger.Error("Failed to GetOnuByID:%d", onuid)
472 continue
473 }
474 sn := convB2S(onu.SerialNumber.VendorSpecific)
475 if ctag, ok := s.CtagMap[sn]; ok == true {
476 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
477 if err != nil {
478 utils.LoggerWithOnu(onu).WithFields(log.Fields{
479 "gemId": gemid,
480 }).Error("Fail to tag C-tag")
481 } else {
482 pkt = tagpkt
483 }
484 } else {
485 utils.LoggerWithOnu(onu).WithFields(log.Fields{
486 "gemId": gemid,
487 "cTagMap": s.CtagMap,
488 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
489 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900490
Zack Williamsb85f5932019-05-10 16:21:35 -0700491 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, pkt.Dump())
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900492
493 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
494 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700495 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900496 return err
497 }
498
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900499 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900500 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900501 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
502 logger.Debug("WARNING: This packet does not come from NNI ")
503 continue
504 }
505 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200506 intfid := nnipkt.Info.intfid
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200507 onu, err := s.GetOnuByID(onuid, intfid)
508 if err != nil {
509 logger.Error("Failed processing NNI packet: %v", err)
510 continue
511 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900512
513 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200514
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900515 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900516 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
517 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700518 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900519 return err
520 }
521
522 case <-ctx.Done():
523 logger.Debug("Closed nnichannel ")
524 return nil
525 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900526 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900527 return nil
528}
529
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900530func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
531 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200532 onu, err := s.GetOnuByID(onuid, intfid)
533 if err != nil {
534 logger.Error("Failed processing onuPacketOut: %v", err)
535 return err
536 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800537
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900538 if layerEth != nil {
539 pkt, _ := layerEth.(*layers.Ethernet)
540 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900541 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800542 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900543 eapolPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900544 s.eapolOut <- &eapolPkt
545 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900546 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800547 utils.LoggerWithOnu(onu).WithFields(log.Fields{
548 "payload": layerDHCP.LayerPayload(),
549 "type": layerDHCP.LayerType().String(),
550 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900551 rawpkt, _, _ = PopVLAN(rawpkt)
552 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900553 logger.Debug("%s", rawpkt.Dump())
554 dhcpPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
555 s.dhcpOut <- &dhcpPkt
556 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800558 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900559 return nil
560 }
561 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
562 if err != nil {
563 return err
564 }
565 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800566 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900567 return nil
568 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800569 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570 return nil
571}
572
573func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900574 poppkt, _, err := PopVLAN(rawpkt)
575 poppkt, _, err = PopVLAN(poppkt)
576 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900577 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900578 return err
579 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900580 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900581 if err != nil {
582 return err
583 }
584 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900585 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900586 SendNni(handle, poppkt)
587 return nil
588}
589
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200590// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900591func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
592 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900593 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900594 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900595 return false
596 }
597 }
598 }
599 return true
600}
601
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900602func (s *Server) isAllOnuOmciActive() bool {
603 for _, onus := range s.Onumap {
604 for _, onu := range onus{
605 if onu.GetIntState() != device.ONU_OMCIACTIVE {
606 return false
607 }
608 }
609 }
610 return true
611}
612
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900613func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900614 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
615 gemportid, err := omci.GetGemPortId(intfid, onuid)
616 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900617 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200618 onu, err := s.GetOnuByID(onuid, intfid)
619 if err != nil {
620 logger.Error("Failed to getGemPortID: %s", err)
621 return 0, err
622 }
623 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900624 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900625 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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200641// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200642func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
643 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800644}
645
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200646func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
647 for _, onu := range onumap[intfid] {
648 if onu.OnuID == onuid {
649 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900650 }
651 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900652 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800653 logger.WithFields(log.Fields{
654 "onumap": onumap,
655 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200656 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800657 }).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}