blob: 610e88bedbfc875078d025fc863b07678e201800 [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
25 "gerrit.opencord.org/voltha-bbsim/common/logger"
26 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090027 "gerrit.opencord.org/voltha-bbsim/device"
28 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029 "github.com/google/gopacket"
30 "github.com/google/gopacket/layers"
31 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080032 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 "google.golang.org/grpc"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090034 "golang.org/x/sync/errgroup"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090035)
36
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090038 UNI_VETH_UP_PFX = "sim_uu"
39 UNI_VETH_DW_PFX = "sim_ud"
40 NNI_VETH_UP_PFX = "sim_nu"
41 NNI_VETH_DW_PFX = "sim_nd"
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
45type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090046 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047 Olt *device.Olt
48 Onumap map[uint32][]*device.Onu
49 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090050 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090051 gRPCAddress string
52 gRPCPort uint32
53 Vethnames []string
54 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090055 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090056 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090057 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090058 cancel context.CancelFunc
59 state coreState
60 stateChan chan coreState
Shad Ansaria5c79892018-11-29 16:32:59 -080061 omciIn chan openolt.OmciIndication
62 omciOut chan openolt.OmciMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090063}
64
65type Packet struct {
66 Info *Ioinfo
67 Pkt gopacket.Packet
68}
69
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090070type coreState int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090071
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090072const (
Matteo Scandolo88e91892018-11-06 16:29:19 -080073 INACTIVE = iota // OLT/ONUs are not instantiated
74 PRE_ACTIVE // Before PacketInDaemon Running
75 ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090076)
77
78/* coreState
79INACTIVE -> PRE_ACTIVE -> ACTIVE
80 (ActivateOLT) (Enable)
81 <- <-
Matteo Scandolo88e91892018-11-06 16:29:19 -080082*/
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090083
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090084func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090085 // TODO: make it decent
86 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080087 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088 nonus := opt.nonus
89 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080090 Olt: device.NewOlt(oltid, npon, 1),
91 Onumap: make(map[uint32][]*device.Onu),
92 Ioinfos: []*Ioinfo{},
93 gRPCAddress: opt.address,
94 gRPCPort: opt.port,
95 Vethnames: []string{},
96 IndInterval: opt.intvl,
97 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080098 EnableServer: nil,
Matteo Scandolo88e91892018-11-06 16:29:19 -080099 state: INACTIVE,
100 stateChan: make(chan coreState, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900101 omciIn: make(chan openolt.OmciIndication, 1024),
102 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103 }
104
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900105 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800106 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900107 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900108 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900109 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900110
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900112 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700113 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900114 oltid := s.Olt.ID
115 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900116 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700117 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900118 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 return &s
120}
121
122//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800123func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900124 s.wg = &sync.WaitGroup{}
125 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800126 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900127 close(s.stateChan)
128 logger.Debug("Start() Done")
129 }()
130 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
131 listener, gserver, err := NewGrpcServer(addressport)
132 if err != nil {
133 logger.Error("Failed to create gRPC server", err)
134 return err
135 }
136 s.gRPCserver = gserver
137 openolt.RegisterOpenoltServer(gserver, s)
138 if err := gserver.Serve(listener); err != nil {
139 logger.Error("Failed to run gRPC server", err)
140 return err
141 }
142 s.wg.Wait()
143 return nil
144}
145
146//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800147func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900148 logger.Debug("Stop() Start")
149 defer logger.Debug("Stop() Done")
150 if s.gRPCserver != nil {
151 s.gRPCserver.Stop()
152 logger.Debug("gRPCserver.Stop()")
153 }
154 s.StopPktInDaemon()
155 return
156}
157
158// Blocking
159func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800160 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900161 olt := s.Olt
162 olt.InitializeStatus()
163 for intfid, _ := range s.Onumap {
164 for _, onu := range s.Onumap[intfid] {
165 onu.InitializeStatus()
166 }
167 }
168 s.updateState(INACTIVE)
169 logger.Debug("Enable() Done")
170 }()
171 logger.Debug("Enable() Start")
172 s.EnableServer = sv
173 if err := s.activateOLT(*sv); err != nil {
174 return err
175 }
176 s.updateState(PRE_ACTIVE)
177
178 coreCtx := context.Background()
179 coreCtx, corecancel := context.WithCancel(coreCtx)
180 s.cancel = corecancel
Matteo Scandolo88e91892018-11-06 16:29:19 -0800181 if err := s.StartPktInDaemon(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900182 return err
183 }
184 return nil
185}
186
187//Non-Blocking
188func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800189 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900190 logger.Debug("Disable() Done")
191 }()
192 logger.Debug("Disable() Start")
193 s.StopPktInDaemon()
194}
195
Matteo Scandolo88e91892018-11-06 16:29:19 -0800196func (s *Server) updateState(state coreState) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 s.state = state
198 s.stateChan <- state
199 logger.Debug("State updated to:%d", state)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900200}
201
202func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900203 defer logger.Debug("activateOLT() Done")
204 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900205 // Activate OLT
206 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900207 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900208 return err
209 }
210 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900211 *olt.InternalState = device.OLT_UP
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800212 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900213
214 // OLT sends Interface Indication to Adapter
215 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800216 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217 return err
218 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800219 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900220
221 // OLT sends Operation Indication to Adapter after activating each interface
222 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900223 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900224 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800225 logger.Error("Fail to sendOperInd: %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 OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900229
230 // OLT sends ONU Discover Indication to Adapter after ONU discovery
231 for intfid, _ := range s.Onumap {
232 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
233 }
234
235 for intfid, _ := range s.Onumap {
236 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800237 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900238 }
239
240 // OLT Sends OnuInd after waiting all of those ONUs up
241 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900242 if IsAllOnuActive(s.Onumap) {
243 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900244 break
245 }
246 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900247
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900248 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900249 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800250 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900251 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900252 return nil
253}
254
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900255// Blocking
256func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
257 logger.Debug("StartPktInDaemon() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800258 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900259 RemoveVeths(s.Vethnames)
260 s.Vethnames = []string{}
261 s.Ioinfos = []*Ioinfo{}
262 s.wg.Done()
263 s.updateState(PRE_ACTIVE)
264 logger.Debug("StartPktInDaemon() Done")
265 }()
266 s.wg.Add(1)
267 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
268 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800269 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900270 return err
271 }
272 s.Ioinfos = ioinfos
273 s.Vethnames = veths
274 logger.Debug("Created vethnames:%v", s.Vethnames)
275
276 parent := ctx
277 child, cancel := context.WithCancel(parent)
278 s.cancel = cancel
279
280 if err = s.runPacketInDaemon(child, stream); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800281 logger.Error("runPacketInDaemon failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900282 return err
283 }
284 return nil
285}
286
287//Non-Blocking
288func (s *Server) StopPktInDaemon() {
289 if s.cancel != nil {
290 cancel := s.cancel
291 cancel()
292 }
293}
294
295func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900296 ioinfos := []*Ioinfo{}
297 var err error
298 for intfid, _ := range onumap {
299 for i := 0; i < len(onumap[intfid]); i++ {
300 var handler *pcap.Handle
301 onuid := onumap[intfid][i].OnuID
302 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800304 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900305 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900306 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900307 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900308 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900309 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900310 ioinfos = append(ioinfos, &oinfo)
311 }
312 }
313
314 var handler *pcap.Handle
315 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900316 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800317 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900318 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900319 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900320
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900321 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900322 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900323 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900324 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900325 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900326}
327
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900328//Blocking
329func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900330 logger.Debug("runPacketInDaemon Start")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 defer logger.Debug("runPacketInDaemon Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900332
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900333 errch := make(chan error)
334 OmciRun(ctx, s.omciOut, s.omciIn, s.Onumap, errch)
335 parent := ctx
336 eg, child := errgroup.WithContext(parent)
337 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900338
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900339 eg.Go (func() error {
340 logger.Debug("runOMCIDaemon Start")
341 defer logger.Debug("runOMCIDaemon Done")
342 select{
343 case v, ok := <- errch: // Wait for OmciInitialization
344 if ok { //Error
345 logger.Error("Error happend in Omci:%s", v)
346 return v
347 } else { //Close
348 s.updateState(ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900349 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900350 case <- child.Done():
351 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900352 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900353 return nil
354 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900355
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900356 eg.Go (func () error {
357 unichannel := make(chan Packet, 2048)
358 defer func() {
359 close(unichannel)
360 logger.Debug("Closed unichannel ")
361 }()
362 for intfid, _ := range s.Onumap {
363 for _, onu := range s.Onumap[intfid] {
364 onuid := onu.OnuID
365 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
366 if err != nil {
367 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
368 return err
369 }
370 uhandler := ioinfo.handler
371 go RecvWorker(ioinfo, uhandler, unichannel)
Shad Ansarief436272018-11-14 15:58:20 -0800372 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900373 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900374
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900375 ioinfo, err := s.IdentifyNniIoinfo("inside")
376 if err != nil {
377 return err
378 }
379 nhandler := ioinfo.handler
380 nnichannel := make(chan Packet, 32)
381 go RecvWorker(ioinfo, nhandler, nnichannel)
382 defer func(){
383 logger.Debug("PacketInDaemon thread receives close ")
384 close(nnichannel)
385 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900386
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900387 data := &openolt.Indication_PktInd{}
388 for {
389 select {
390 case msg := <-s.omciIn:
391 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
392 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
393 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
394 logger.Error("send omci indication failed.", err)
395 continue
396 }
397 case unipkt := <-unichannel:
398 onuid := unipkt.Info.onuid
399 onu, _ := s.GetOnuByID(onuid)
400 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
401 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
402 logger.Debug("WARNING: This packet does not come from UNI ")
403 continue
404 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900405
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900406 intfid := unipkt.Info.intfid
407 gemid, _ := getGemPortID(intfid, onuid)
408 pkt := unipkt.Pkt
409 layerEth := pkt.Layer(layers.LayerTypeEthernet)
410 le, _ := layerEth.(*layers.Ethernet)
411 ethtype := le.EthernetType
412
413 if ethtype == 0x888e {
414 utils.LoggerWithOnu(onu).WithFields(log.Fields{
415 "gemId": gemid,
416 }).Info("Received upstream packet is EAPOL.")
417 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
418 utils.LoggerWithOnu(onu).WithFields(log.Fields{
419 "gemId": gemid,
420 }).Info("Received upstream packet is DHCP.")
421
422 //C-TAG
423 sn := convB2S(onu.SerialNumber.VendorSpecific)
424 if ctag, ok := s.CtagMap[sn]; ok == true {
425 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
426 if err != nil {
427 utils.LoggerWithOnu(onu).WithFields(log.Fields{
428 "gemId": gemid,
429 }).Error("Fail to tag C-tag")
430 } else {
431 pkt = tagpkt
432 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900433 } else {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900434 utils.LoggerWithOnu(onu).WithFields(log.Fields{
435 "gemId": gemid,
436 "cTagMap": s.CtagMap,
437 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900438 }
439 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800440 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900441 "gemId": gemid,
442 }).Info("Received upstream packet is of unknow type, skipping.")
443 continue
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900444 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900445
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900446 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
447 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
448 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
449 logger.Error("Fail to send PktInd indication.", err)
450 return err
451 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900452
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900453 case nnipkt := <-nnichannel:
454 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
455 logger.Debug("WARNING: This packet does not come from NNI ")
456 continue
457 }
458 onuid := nnipkt.Info.onuid
459 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900460
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900461 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
462 intfid := nnipkt.Info.intfid
463 pkt := nnipkt.Pkt
464 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
465 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, 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 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900470
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900471 case <-child.Done():
472 logger.Debug("Closed nnichannel ")
473 return nil
474 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900475 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900476 return nil
477 })
478
479 logger.Debug("Wait here")
480 if err := eg.Wait(); err != nil {
481 logger.Error("Error happend in runPacketInDaemon:%s", err)
482 cancel()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900483 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900484 return nil
485}
486
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
488 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800489 onu, _ := s.GetOnuByID(onuid)
490
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900491 if layerEth != nil {
492 pkt, _ := layerEth.(*layers.Ethernet)
493 ethtype := pkt.EthernetType
494 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800495 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800497 utils.LoggerWithOnu(onu).WithFields(log.Fields{
498 "payload": layerDHCP.LayerPayload(),
499 "type": layerDHCP.LayerType().String(),
500 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900501 rawpkt, _, _ = PopVLAN(rawpkt)
502 rawpkt, _, _ = PopVLAN(rawpkt)
503 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800504 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900505 return nil
506 }
507 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
508 if err != nil {
509 return err
510 }
511 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800512 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900513 return nil
514 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800515 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900516 return nil
517}
518
519func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900520 poppkt, _, err := PopVLAN(rawpkt)
521 poppkt, _, err = PopVLAN(poppkt)
522 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900523 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900524 return err
525 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900526 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900527 if err != nil {
528 return err
529 }
530 handle := ioinfo.handler
531 SendNni(handle, poppkt)
532 return nil
533}
534
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900535func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
536 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900538 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900539 return false
540 }
541 }
542 }
543 return true
544}
545
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900546func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansaria5c79892018-11-29 16:32:59 -0800547 key := OnuKey{intfid, onuid}
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900548 if onuState, ok := OnuOmciStateMap[key]; !ok {
Shad Ansaria5c79892018-11-29 16:32:59 -0800549 idx := uint32(0)
550 // Backwards compatible with bbsim_olt adapter
551 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
552 } else {
553 // FIXME - Gem Port ID is 2 bytes - fix openolt.proto
554 return uint32(onuState.gemPortId), nil
555 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900556}
557
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900558func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
559 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900560 for _, onu := range onus {
561 if device.ValidateSN(*sn, *onu.SerialNumber) {
562 return onu, nil
563 }
564 }
565 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900566 err := errors.New("No mathced SN is found ")
567 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900568 return nil, err
569}
570
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800571func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
572 return getOnuByID(s.Onumap, onuid)
573}
574
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900575func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
576 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900577 for _, onu := range onus {
578 if onu.OnuID == onuid {
579 return onu, nil
580 }
581 }
582 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900583 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800584 logger.WithFields(log.Fields{
585 "onumap": onumap,
586 "onuid": onuid,
587 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900588 return nil, err
589}
590
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900591func convB2S(b []byte) string {
592 s := ""
593 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700594 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900595 }
596 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700597}