blob: ba0727ac618f4771e6e9eaa3545560eba5200e1b [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"
Keita NISHIMOTO7057aa62019-05-28 01:24:08 +090024 "reflect"
Shad Ansarib744bf22019-01-17 11:36:46 -080025
Keita NISHIMOTO7057aa62019-05-28 01:24:08 +090026 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"
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070030 openolt "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
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070077 Byte []byte
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090078}
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),
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700107 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 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700182
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900183 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184
185 coreCtx := context.Background()
186 coreCtx, corecancel := context.WithCancel(coreCtx)
187 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700188 go s.sendDiscovertoONUs(*sv)
189
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900190 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900191 return err
192 }
193 return nil
194}
195
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200196// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800198 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900199 logger.Debug("Disable() Done")
200 }()
201 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900202 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900203}
204
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900205func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900206 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900207 current := dev.GetIntState()
208 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800209 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
210 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900211 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800212 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900213 logger.Debug("ONU State updated to:%d", state)
214 } else {
215 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
216 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217}
218
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700219func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200220 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900221 if err != nil {
222 return err
223 }
224 s.updateDevIntState(onu, state)
225 return nil
226}
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700227func (s *Server) Activate(onu *device.Onu) error {
228 utils.LoggerWithOnu(onu).Info("sending ONUInd Onuid")
229 go sendOnuIndtoONU(*s.EnableServer, onu)
230 return nil
231}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900232
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900233func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900234 defer logger.Debug("activateOLT() Done")
235 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900236 // Activate OLT
237 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900238 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239 return err
240 }
241 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800242 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900243
244 // OLT sends Interface Indication to Adapter
245 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800246 logger.Error("Fail to sendIntfInd: %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 IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900250
251 // OLT sends Operation Indication to Adapter after activating each interface
252 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900253 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800254 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900255 return err
256 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800257 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900258
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700259 return nil
260}
261func (s *Server) sendDiscovertoONUs(stream openolt.Openolt_EnableIndicationServer) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900262 // OLT sends ONU Discover Indication to Adapter after ONU discovery
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200263 for intfid := range s.Onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900264 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
265 }
266
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200267 // Initialize all ONUs
268 for intfid := range s.Onumap {
269 for _, onu := range s.Onumap[intfid] {
270 onu.Initialize()
271 }
272 }
273
274 // Send discovery indication for all ONUs
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700275 for intfid, _ := range s.Onumap {
276 sendOnuDiscInd(stream, s.Onumap[intfid], s.IndInterval)
277 logger.Info("OLT sent ONUDiscInd for intfId:%d.", intfid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900278 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900279}
280
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200281// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900282func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
283 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800284 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900285 RemoveVeths(s.Vethnames)
286 s.Vethnames = []string{}
287 s.Ioinfos = []*Ioinfo{}
288 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900289 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900290 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900291 }()
292 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900293 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900294 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700295 logger.Error("createIoinfos failed: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900296 return err
297 }
298 s.Ioinfos = ioinfos
299 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700300 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900301
302 parent := ctx
303 child, cancel := context.WithCancel(parent)
304 s.cancel = cancel
305
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900306 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700307 logger.Error("runPktLoops failed: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900308 return err
309 }
310 return nil
311}
312
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200313// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900314func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900315 if s.cancel != nil {
316 cancel := s.cancel
317 cancel()
318 }
319}
320
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900321func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900322 ioinfos := []*Ioinfo{}
323 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900324 var handler *pcap.Handle
325 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700327 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900328 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900329 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900330
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900333 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900334 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900335 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900336}
337
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900338//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900339func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
340 logger.Debug("runPacketPktLoops Start")
341 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900342
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900343 errchOmci := make(chan error)
344 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900345 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900346 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900347
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900348 errchEapol := make(chan error)
349 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
350
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900351 errchDhcp := make(chan error)
352 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
353
Shad Ansarib744bf22019-01-17 11:36:46 -0800354 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900355 logger.Debug("runOMCIResponder Start")
356 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800357 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900358 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800359 if ok { //Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700360 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900361 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900362 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800363 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900364 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900365 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900366 return nil
367 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900368
Shad Ansarib744bf22019-01-17 11:36:46 -0800369 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900370 logger.Debug("runEapolResponder Start")
371 defer logger.Debug("runEapolResponder Done")
372 select {
373 case v, ok := <-errchEapol:
374 if ok { //Error
375 logger.Error("Error happend in Eapol:%s", v)
376 return v
377 }
378 case <-child.Done():
379 return nil
380 }
381 return nil
382 })
383
384 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900385 logger.Debug("runDhcpResponder Start")
386 defer logger.Debug("runDhcpResponder Done")
387 select {
388 case v, ok := <-errchDhcp:
389 if ok { //Error
390 logger.Error("Error happend in Dhcp:%s", v)
391 return v
392 }
393 case <-child.Done():
394 return nil
395 }
396 return nil
397 })
398
399 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900400 err := s.runMainPktLoop(child, stream)
401 return err
402 })
403
404 if err := eg.Wait(); err != nil {
405 logger.Error("Error happend in runPacketLoops:%s", err)
406 cancel()
407 }
408 return nil
409}
410
411func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900412 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900413 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900414 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900415 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900416 ioinfo, err := s.IdentifyNniIoinfo("inside")
417 if err != nil {
418 return err
419 }
420 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
421 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800422 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900423 close(nnichannel)
424 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900425 logger.Debug("BEFORE OLT_ACTIVE")
426 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
427 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900428 data := &openolt.Indication_PktInd{}
429 for {
430 select {
431 case msg := <-s.omciIn:
432 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
433 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
434 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700435 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900436 continue
437 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700438 case msg := <-s.eapolIn:
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900439 intfid := msg.IntfId
440 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900441 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900442 if err != nil {
443 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
444 continue
445 }
446
Zack Williamsb85f5932019-05-10 16:21:35 -0700447 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 +0900448
449 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
450 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700451 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900452 return err
453 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700454 case msg := <-s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900455 intfid := msg.IntfId
456 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900457 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900458 bytes := msg.Byte
459 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
460
461 if err != nil {
462 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
463 continue
464 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900465
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200466 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900467 if err != nil {
468 logger.Error("Failed to GetOnuByID:%d", onuid)
469 continue
470 }
471 sn := convB2S(onu.SerialNumber.VendorSpecific)
472 if ctag, ok := s.CtagMap[sn]; ok == true {
473 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
474 if err != nil {
475 utils.LoggerWithOnu(onu).WithFields(log.Fields{
476 "gemId": gemid,
477 }).Error("Fail to tag C-tag")
478 } else {
479 pkt = tagpkt
480 }
481 } else {
482 utils.LoggerWithOnu(onu).WithFields(log.Fields{
483 "gemId": gemid,
484 "cTagMap": s.CtagMap,
485 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
486 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900487
Zack Williamsb85f5932019-05-10 16:21:35 -0700488 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 +0900489
490 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
491 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700492 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900493 return err
494 }
495
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900496 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900497 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900498 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
499 logger.Debug("WARNING: This packet does not come from NNI ")
500 continue
501 }
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200502 intfid := nnipkt.Info.intfid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200503
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900504 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900505 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
506 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700507 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900508 return err
509 }
510
511 case <-ctx.Done():
512 logger.Debug("Closed nnichannel ")
513 return nil
514 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900515 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900516 return nil
517}
518
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900519func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
520 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200521 onu, err := s.GetOnuByID(onuid, intfid)
522 if err != nil {
523 logger.Error("Failed processing onuPacketOut: %v", err)
524 return err
525 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800526
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900527 if layerEth != nil {
528 pkt, _ := layerEth.(*layers.Ethernet)
529 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900530 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800531 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700532 eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900533 s.eapolOut <- &eapolPkt
534 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900535 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800536 utils.LoggerWithOnu(onu).WithFields(log.Fields{
537 "payload": layerDHCP.LayerPayload(),
538 "type": layerDHCP.LayerType().String(),
539 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900540 rawpkt, _, _ = PopVLAN(rawpkt)
541 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900542 logger.Debug("%s", rawpkt.Dump())
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700543 dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900544 s.dhcpOut <- &dhcpPkt
545 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900546 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800547 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900548 return nil
549 }
550 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
551 if err != nil {
552 return err
553 }
554 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800555 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900556 return nil
557 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800558 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900559 return nil
560}
561
562func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900563 poppkt, _, err := PopVLAN(rawpkt)
564 poppkt, _, err = PopVLAN(poppkt)
565 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900566 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900567 return err
568 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900569 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570 if err != nil {
571 return err
572 }
573 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900574 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900575 SendNni(handle, poppkt)
576 return nil
577}
578
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200579// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900580func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
581 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900582 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900583 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900584 return false
585 }
586 }
587 }
588 return true
589}
590
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900591func (s *Server) isAllOnuOmciActive() bool {
592 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700593 for _, onu := range onus {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900594 if onu.GetIntState() != device.ONU_OMCIACTIVE {
595 return false
596 }
597 }
598 }
599 return true
600}
601
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900602func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900603 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
604 gemportid, err := omci.GetGemPortId(intfid, onuid)
605 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900606 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200607 onu, err := s.GetOnuByID(onuid, intfid)
608 if err != nil {
609 logger.Error("Failed to getGemPortID: %s", err)
610 return 0, err
611 }
612 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900613 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900614 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900615}
616
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900617func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
618 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900619 for _, onu := range onus {
620 if device.ValidateSN(*sn, *onu.SerialNumber) {
621 return onu, nil
622 }
623 }
624 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900625 err := errors.New("No mathced SN is found ")
626 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900627 return nil, err
628}
629
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200630// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200631func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
632 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800633}
634
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200635func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
636 for _, onu := range onumap[intfid] {
637 if onu.OnuID == onuid {
638 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900639 }
640 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900641 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800642 logger.WithFields(log.Fields{
643 "onumap": onumap,
644 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200645 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800646 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900647 return nil, err
648}
649
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900650func convB2S(b []byte) string {
651 s := ""
652 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700653 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900654 }
655 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700656}