blob: 3e2a0754316746a4fdf4693194d2c147c915b897 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package core
18
19import (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090020 "context"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070021 "errors"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 "strconv"
23 "sync"
24
Shad Ansarib744bf22019-01-17 11:36:46 -080025 omci "github.com/opencord/omci-sim"
26
27 "reflect"
28
Matteo Scandolo88e91892018-11-06 16:29:19 -080029 "gerrit.opencord.org/voltha-bbsim/common/logger"
30 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "gerrit.opencord.org/voltha-bbsim/device"
32 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 "github.com/google/gopacket"
34 "github.com/google/gopacket/layers"
35 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080038 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090042 UNI_VETH_UP_PFX = "sim_uu"
43 UNI_VETH_DW_PFX = "sim_ud"
44 NNI_VETH_UP_PFX = "sim_nu"
45 NNI_VETH_DW_PFX = "sim_nd"
46 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047)
48
49type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090050 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 Olt *device.Olt
52 Onumap map[uint32][]*device.Onu
53 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055 gRPCAddress string
56 gRPCPort uint32
57 Vethnames []string
58 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090059 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090060 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090061 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090063 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080064 omciIn chan openolt.OmciIndication
65 omciOut chan openolt.OmciMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090066}
67
68type Packet struct {
69 Info *Ioinfo
70 Pkt gopacket.Packet
71}
72
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090073type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080074 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090075 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080076 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090077}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090078
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090079func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080 // TODO: make it decent
81 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080082 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090083 nonus := opt.nonus
84 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080085 Olt: device.NewOlt(oltid, npon, 1),
86 Onumap: make(map[uint32][]*device.Onu),
87 Ioinfos: []*Ioinfo{},
88 gRPCAddress: opt.address,
89 gRPCPort: opt.port,
90 Vethnames: []string{},
91 IndInterval: opt.intvl,
92 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080093 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -080094 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090095 omciIn: make(chan openolt.OmciIndication, 1024),
96 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 }
98
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090099 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800100 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900101 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900102 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900103 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900104
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900105 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900106 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700107 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900108 oltid := s.Olt.ID
109 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700111 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900112 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 return &s
114}
115
116//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800117func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900118 s.wg = &sync.WaitGroup{}
119 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800120 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800121 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 logger.Debug("Start() Done")
123 }()
124 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
125 listener, gserver, err := NewGrpcServer(addressport)
126 if err != nil {
127 logger.Error("Failed to create gRPC server", err)
128 return err
129 }
130 s.gRPCserver = gserver
131 openolt.RegisterOpenoltServer(gserver, s)
132 if err := gserver.Serve(listener); err != nil {
133 logger.Error("Failed to run gRPC server", err)
134 return err
135 }
136 s.wg.Wait()
137 return nil
138}
139
140//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800141func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900142 logger.Debug("Stop() Start")
143 defer logger.Debug("Stop() Done")
144 if s.gRPCserver != nil {
145 s.gRPCserver.Stop()
146 logger.Debug("gRPCserver.Stop()")
147 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900148 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900149 return
150}
151
152// Blocking
153func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900154 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800155 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900156 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 for intfid, _ := range s.Onumap {
158 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900159 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 }
161 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900162 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900163 logger.Debug("Enable() Done")
164 }()
165 logger.Debug("Enable() Start")
166 s.EnableServer = sv
167 if err := s.activateOLT(*sv); err != nil {
168 return err
169 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900171
172 coreCtx := context.Background()
173 coreCtx, corecancel := context.WithCancel(coreCtx)
174 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900175 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900176 return err
177 }
178 return nil
179}
180
181//Non-Blocking
182func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800183 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184 logger.Debug("Disable() Done")
185 }()
186 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900187 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188}
189
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900190func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
191 current := dev.GetIntState()
192 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800193 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
194 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900195 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800196 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900197 logger.Debug("ONU State updated to:%d", state)
198 } else {
199 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
200 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900201}
202
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900203func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
204 onu, err := s.GetOnuByID(onuid) //TODO: IntfID should be included ?
205 if err != nil {
206 return err
207 }
208 s.updateDevIntState(onu, state)
209 return nil
210}
211
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900212func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900213 defer logger.Debug("activateOLT() Done")
214 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900215 // Activate OLT
216 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900217 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900218 return err
219 }
220 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800221 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900222
223 // OLT sends Interface Indication to Adapter
224 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800225 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900226 return err
227 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800228 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900229
230 // OLT sends Operation Indication to Adapter after activating each interface
231 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900232 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800233 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900234 return err
235 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800236 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900237
238 // OLT sends ONU Discover Indication to Adapter after ONU discovery
239 for intfid, _ := range s.Onumap {
240 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
241 }
242
243 for intfid, _ := range s.Onumap {
244 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800245 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900246 }
247
248 // OLT Sends OnuInd after waiting all of those ONUs up
249 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900250 if IsAllOnuActive(s.Onumap) {
251 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900252 break
253 }
254 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900255
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900256 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900257 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800258 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900259 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900260 return nil
261}
262
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900263// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900264func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
265 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800266 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900267 RemoveVeths(s.Vethnames)
268 s.Vethnames = []string{}
269 s.Ioinfos = []*Ioinfo{}
270 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900271 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900272 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900273 }()
274 s.wg.Add(1)
275 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
276 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800277 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900278 return err
279 }
280 s.Ioinfos = ioinfos
281 s.Vethnames = veths
282 logger.Debug("Created vethnames:%v", s.Vethnames)
283
284 parent := ctx
285 child, cancel := context.WithCancel(parent)
286 s.cancel = cancel
287
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900288 if err = s.runPktLoops(child, stream); err != nil {
289 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900290 return err
291 }
292 return nil
293}
294
295//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900296func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900297 if s.cancel != nil {
298 cancel := s.cancel
299 cancel()
300 }
301}
302
303func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900304 ioinfos := []*Ioinfo{}
305 var err error
306 for intfid, _ := range onumap {
307 for i := 0; i < len(onumap[intfid]); i++ {
308 var handler *pcap.Handle
309 onuid := onumap[intfid][i].OnuID
310 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900311 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800312 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900313 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900314 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900315 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900316 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900317 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900318 ioinfos = append(ioinfos, &oinfo)
319 }
320 }
321
322 var handler *pcap.Handle
323 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900324 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800325 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900327 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900328
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900329 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900330 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900333 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900334}
335
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900336//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900337func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
338 logger.Debug("runPacketPktLoops Start")
339 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900340
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900341 errch := make(chan error)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900342 RunOmciResponder(ctx, s.omciOut, s.omciIn, s.Onumap, errch)
343 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900344 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900345
Shad Ansarib744bf22019-01-17 11:36:46 -0800346 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900347 logger.Debug("runOMCIResponder Start")
348 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800349 select {
350 case v, ok := <-errch: // Wait for OmciInitialization
351 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900352 logger.Error("Error happend in Omci:%s", v)
353 return v
Shad Ansarib744bf22019-01-17 11:36:46 -0800354 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900355 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900356 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800357 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900358 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900359 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900360 return nil
361 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900362
Shad Ansarib744bf22019-01-17 11:36:46 -0800363 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900364 err := s.runMainPktLoop(child, stream)
365 return err
366 })
367
368 if err := eg.Wait(); err != nil {
369 logger.Error("Error happend in runPacketLoops:%s", err)
370 cancel()
371 }
372 return nil
373}
374
375func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
376 unichannel := make(chan Packet, 2048)
377 defer func() {
378 close(unichannel)
379 logger.Debug("Closed unichannel ")
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900380 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900381 }()
382 for intfid, _ := range s.Onumap {
383 for _, onu := range s.Onumap[intfid] {
384 onuid := onu.OnuID
385 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
386 if err != nil {
387 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
388 return err
Shad Ansarief436272018-11-14 15:58:20 -0800389 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900390 uhandler := ioinfo.handler
391 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900392 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900393 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900394
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900395 ioinfo, err := s.IdentifyNniIoinfo("inside")
396 if err != nil {
397 return err
398 }
399 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
400 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800401 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900402 close(nnichannel)
403 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900404
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900405 data := &openolt.Indication_PktInd{}
406 for {
407 select {
408 case msg := <-s.omciIn:
409 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
410 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
411 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
412 logger.Error("send omci indication failed.", err)
413 continue
414 }
415 case unipkt := <-unichannel:
416 onuid := unipkt.Info.onuid
417 onu, _ := s.GetOnuByID(onuid)
418 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
419 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
420 logger.Debug("WARNING: This packet does not come from UNI ")
421 continue
422 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900423
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900424 intfid := unipkt.Info.intfid
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900425 gemid, err := getGemPortID(intfid, onuid)
426 if err != nil {
427 continue
428 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900429 pkt := unipkt.Pkt
430 layerEth := pkt.Layer(layers.LayerTypeEthernet)
431 le, _ := layerEth.(*layers.Ethernet)
432 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900433
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900434 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900435 utils.LoggerWithOnu(onu).WithFields(log.Fields{
436 "gemId": gemid,
437 }).Info("Received upstream packet is EAPOL.")
438 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
439 utils.LoggerWithOnu(onu).WithFields(log.Fields{
440 "gemId": gemid,
441 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900442
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900443 //C-TAG
444 sn := convB2S(onu.SerialNumber.VendorSpecific)
445 if ctag, ok := s.CtagMap[sn]; ok == true {
446 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
447 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900448 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900449 "gemId": gemid,
450 }).Error("Fail to tag C-tag")
451 } else {
452 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900453 }
454 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800455 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900456 "gemId": gemid,
457 "cTagMap": s.CtagMap,
458 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900459 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900460 } else {
461 utils.LoggerWithOnu(onu).WithFields(log.Fields{
462 "gemId": gemid,
463 }).Info("Received upstream packet is of unknow type, skipping.")
464 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900465 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900466
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900467 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
468 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
469 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
470 logger.Error("Fail to send PktInd indication.", err)
471 return err
472 }
473
474 case nnipkt := <-nnichannel:
475 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
476 logger.Debug("WARNING: This packet does not come from NNI ")
477 continue
478 }
479 onuid := nnipkt.Info.onuid
480 onu, _ := s.GetOnuByID(onuid)
481
482 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
483 intfid := nnipkt.Info.intfid
484 pkt := nnipkt.Pkt
485 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
486 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
487 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
488 logger.Error("Fail to send PktInd indication.", err)
489 return err
490 }
491
492 case <-ctx.Done():
493 logger.Debug("Closed nnichannel ")
494 return nil
495 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900496 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900497 return nil
498}
499
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900500func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
501 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800502 onu, _ := s.GetOnuByID(onuid)
503
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900504 if layerEth != nil {
505 pkt, _ := layerEth.(*layers.Ethernet)
506 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900507 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800508 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900509 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800510 utils.LoggerWithOnu(onu).WithFields(log.Fields{
511 "payload": layerDHCP.LayerPayload(),
512 "type": layerDHCP.LayerType().String(),
513 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900514 rawpkt, _, _ = PopVLAN(rawpkt)
515 rawpkt, _, _ = PopVLAN(rawpkt)
516 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800517 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900518 return nil
519 }
520 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
521 if err != nil {
522 return err
523 }
524 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800525 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900526 return nil
527 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800528 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900529 return nil
530}
531
532func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900533 poppkt, _, err := PopVLAN(rawpkt)
534 poppkt, _, err = PopVLAN(poppkt)
535 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900536 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 return err
538 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900539 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900540 if err != nil {
541 return err
542 }
543 handle := ioinfo.handler
544 SendNni(handle, poppkt)
545 return nil
546}
547
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900548func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
549 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900550 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900551 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900552 return false
553 }
554 }
555 }
556 return true
557}
558
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900559func (s *Server) isAllOnuOmciActive() bool {
560 for _, onus := range s.Onumap {
561 for _, onu := range onus{
562 if onu.GetIntState() != device.ONU_OMCIACTIVE {
563 return false
564 }
565 }
566 }
567 return true
568}
569
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900571 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
572 gemportid, err := omci.GetGemPortId(intfid, onuid)
573 if err != nil {
574 logger.Error("%s", err)
575 return 0, err
576 }
577 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900578}
579
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900580func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
581 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900582 for _, onu := range onus {
583 if device.ValidateSN(*sn, *onu.SerialNumber) {
584 return onu, nil
585 }
586 }
587 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900588 err := errors.New("No mathced SN is found ")
589 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900590 return nil, err
591}
592
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800593func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
594 return getOnuByID(s.Onumap, onuid)
595}
596
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900597func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
598 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900599 for _, onu := range onus {
600 if onu.OnuID == onuid {
601 return onu, nil
602 }
603 }
604 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900605 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800606 logger.WithFields(log.Fields{
607 "onumap": onumap,
608 "onuid": onuid,
609 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900610 return nil, err
611}
612
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900613func convB2S(b []byte) string {
614 s := ""
615 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700616 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900617 }
618 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700619}