blob: c57a3e9b6feceaa255c2f7fd28705be1dbfea940 [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
425 gemid, _ := getGemPortID(intfid, onuid)
426 pkt := unipkt.Pkt
427 layerEth := pkt.Layer(layers.LayerTypeEthernet)
428 le, _ := layerEth.(*layers.Ethernet)
429 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900430
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900431 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900432 utils.LoggerWithOnu(onu).WithFields(log.Fields{
433 "gemId": gemid,
434 }).Info("Received upstream packet is EAPOL.")
435 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
436 utils.LoggerWithOnu(onu).WithFields(log.Fields{
437 "gemId": gemid,
438 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900439
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900440 //C-TAG
441 sn := convB2S(onu.SerialNumber.VendorSpecific)
442 if ctag, ok := s.CtagMap[sn]; ok == true {
443 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
444 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900445 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900446 "gemId": gemid,
447 }).Error("Fail to tag C-tag")
448 } else {
449 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900450 }
451 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800452 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900453 "gemId": gemid,
454 "cTagMap": s.CtagMap,
455 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900456 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900457 } else {
458 utils.LoggerWithOnu(onu).WithFields(log.Fields{
459 "gemId": gemid,
460 }).Info("Received upstream packet is of unknow type, skipping.")
461 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900462 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900463
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900464 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
465 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
466 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
467 logger.Error("Fail to send PktInd indication.", err)
468 return err
469 }
470
471 case nnipkt := <-nnichannel:
472 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
473 logger.Debug("WARNING: This packet does not come from NNI ")
474 continue
475 }
476 onuid := nnipkt.Info.onuid
477 onu, _ := s.GetOnuByID(onuid)
478
479 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
480 intfid := nnipkt.Info.intfid
481 pkt := nnipkt.Pkt
482 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
483 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
484 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
485 logger.Error("Fail to send PktInd indication.", err)
486 return err
487 }
488
489 case <-ctx.Done():
490 logger.Debug("Closed nnichannel ")
491 return nil
492 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900493 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900494 return nil
495}
496
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900497func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
498 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800499 onu, _ := s.GetOnuByID(onuid)
500
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900501 if layerEth != nil {
502 pkt, _ := layerEth.(*layers.Ethernet)
503 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900504 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800505 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900506 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800507 utils.LoggerWithOnu(onu).WithFields(log.Fields{
508 "payload": layerDHCP.LayerPayload(),
509 "type": layerDHCP.LayerType().String(),
510 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900511 rawpkt, _, _ = PopVLAN(rawpkt)
512 rawpkt, _, _ = PopVLAN(rawpkt)
513 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800514 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900515 return nil
516 }
517 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
518 if err != nil {
519 return err
520 }
521 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800522 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900523 return nil
524 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800525 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900526 return nil
527}
528
529func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900530 poppkt, _, err := PopVLAN(rawpkt)
531 poppkt, _, err = PopVLAN(poppkt)
532 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900533 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900534 return err
535 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900536 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 if err != nil {
538 return err
539 }
540 handle := ioinfo.handler
541 SendNni(handle, poppkt)
542 return nil
543}
544
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900545func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
546 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900547 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900548 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900549 return false
550 }
551 }
552 }
553 return true
554}
555
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900556func (s *Server) isAllOnuOmciActive() bool {
557 for _, onus := range s.Onumap {
558 for _, onu := range onus{
559 if onu.GetIntState() != device.ONU_OMCIACTIVE {
560 return false
561 }
562 }
563 }
564 return true
565}
566
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900567func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansarib744bf22019-01-17 11:36:46 -0800568 // FIXME - check for errors
569 return uint32(omci.GetGemPortId(intfid, onuid)), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570}
571
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900572func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
573 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900574 for _, onu := range onus {
575 if device.ValidateSN(*sn, *onu.SerialNumber) {
576 return onu, nil
577 }
578 }
579 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900580 err := errors.New("No mathced SN is found ")
581 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900582 return nil, err
583}
584
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800585func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
586 return getOnuByID(s.Onumap, onuid)
587}
588
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900589func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
590 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900591 for _, onu := range onus {
592 if onu.OnuID == onuid {
593 return onu, nil
594 }
595 }
596 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900597 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800598 logger.WithFields(log.Fields{
599 "onumap": onumap,
600 "onuid": onuid,
601 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900602 return nil, err
603}
604
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900605func convB2S(b []byte) string {
606 s := ""
607 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700608 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900609 }
610 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700611}