blob: 98860518a91564f76c9f07eac33ee532a4fe87a7 [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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +090074 PRE_ACTIVE // Before running MainPacketLoop
75 ACTIVE // After running MainPacketLoop
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 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900154 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900155 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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900181 if err := s.StartPktLoops(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")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900193 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900194}
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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900256func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
257 logger.Debug("StartPktLoops () 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)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900264 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900265 }()
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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900280 if err = s.runPktLoops(child, stream); err != nil {
281 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900282 return err
283 }
284 return nil
285}
286
287//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900288func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900289 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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900329func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
330 logger.Debug("runPacketPktLoops Start")
331 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900332
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900333 errch := make(chan error)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900334 RunOmciResponder(ctx, s.omciOut, s.omciIn, s.Onumap, errch)
335 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900336 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900337
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900338 eg.Go (func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900339 logger.Debug("runOMCIResponder Start")
340 defer logger.Debug("runOMCIResponder Done")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900341 select{
342 case v, ok := <- errch: // Wait for OmciInitialization
343 if ok { //Error
344 logger.Error("Error happend in Omci:%s", v)
345 return v
346 } else { //Close
347 s.updateState(ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900348 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900349 case <- child.Done():
350 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900351 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900352 return nil
353 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900354
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900355 eg.Go (func () error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900356 err := s.runMainPktLoop(child, stream)
357 return err
358 })
359
360 if err := eg.Wait(); err != nil {
361 logger.Error("Error happend in runPacketLoops:%s", err)
362 cancel()
363 }
364 return nil
365}
366
367func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
368 unichannel := make(chan Packet, 2048)
369 defer func() {
370 close(unichannel)
371 logger.Debug("Closed unichannel ")
372 }()
373 for intfid, _ := range s.Onumap {
374 for _, onu := range s.Onumap[intfid] {
375 onuid := onu.OnuID
376 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
377 if err != nil {
378 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
379 return err
Shad Ansarief436272018-11-14 15:58:20 -0800380 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900381 uhandler := ioinfo.handler
382 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900383 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900384 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900385
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900386 ioinfo, err := s.IdentifyNniIoinfo("inside")
387 if err != nil {
388 return err
389 }
390 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
391 go RecvWorker(ioinfo, nhandler, nnichannel)
392 defer func(){
393 close(nnichannel)
394 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900395
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900396 data := &openolt.Indication_PktInd{}
397 for {
398 select {
399 case msg := <-s.omciIn:
400 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
401 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
402 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
403 logger.Error("send omci indication failed.", err)
404 continue
405 }
406 case unipkt := <-unichannel:
407 onuid := unipkt.Info.onuid
408 onu, _ := s.GetOnuByID(onuid)
409 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
410 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
411 logger.Debug("WARNING: This packet does not come from UNI ")
412 continue
413 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900414
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900415 intfid := unipkt.Info.intfid
416 gemid, _ := getGemPortID(intfid, onuid)
417 pkt := unipkt.Pkt
418 layerEth := pkt.Layer(layers.LayerTypeEthernet)
419 le, _ := layerEth.(*layers.Ethernet)
420 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900421
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900422 if ethtype == 0x888e {
423 utils.LoggerWithOnu(onu).WithFields(log.Fields{
424 "gemId": gemid,
425 }).Info("Received upstream packet is EAPOL.")
426 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
427 utils.LoggerWithOnu(onu).WithFields(log.Fields{
428 "gemId": gemid,
429 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900430
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900431 //C-TAG
432 sn := convB2S(onu.SerialNumber.VendorSpecific)
433 if ctag, ok := s.CtagMap[sn]; ok == true {
434 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
435 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900436 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900437 "gemId": gemid,
438 }).Error("Fail to tag C-tag")
439 } else {
440 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900441 }
442 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800443 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900444 "gemId": gemid,
445 "cTagMap": s.CtagMap,
446 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900447 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900448 } else {
449 utils.LoggerWithOnu(onu).WithFields(log.Fields{
450 "gemId": gemid,
451 }).Info("Received upstream packet is of unknow type, skipping.")
452 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900453 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900454
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900455 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
456 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
457 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
458 logger.Error("Fail to send PktInd indication.", err)
459 return err
460 }
461
462 case nnipkt := <-nnichannel:
463 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
464 logger.Debug("WARNING: This packet does not come from NNI ")
465 continue
466 }
467 onuid := nnipkt.Info.onuid
468 onu, _ := s.GetOnuByID(onuid)
469
470 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
471 intfid := nnipkt.Info.intfid
472 pkt := nnipkt.Pkt
473 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
474 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
475 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
476 logger.Error("Fail to send PktInd indication.", err)
477 return err
478 }
479
480 case <-ctx.Done():
481 logger.Debug("Closed nnichannel ")
482 return nil
483 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900484 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900485 return nil
486}
487
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900488func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
489 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800490 onu, _ := s.GetOnuByID(onuid)
491
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900492 if layerEth != nil {
493 pkt, _ := layerEth.(*layers.Ethernet)
494 ethtype := pkt.EthernetType
495 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800496 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900497 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800498 utils.LoggerWithOnu(onu).WithFields(log.Fields{
499 "payload": layerDHCP.LayerPayload(),
500 "type": layerDHCP.LayerType().String(),
501 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900502 rawpkt, _, _ = PopVLAN(rawpkt)
503 rawpkt, _, _ = PopVLAN(rawpkt)
504 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800505 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900506 return nil
507 }
508 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
509 if err != nil {
510 return err
511 }
512 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800513 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900514 return nil
515 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800516 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900517 return nil
518}
519
520func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900521 poppkt, _, err := PopVLAN(rawpkt)
522 poppkt, _, err = PopVLAN(poppkt)
523 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900524 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900525 return err
526 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900527 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900528 if err != nil {
529 return err
530 }
531 handle := ioinfo.handler
532 SendNni(handle, poppkt)
533 return nil
534}
535
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900536func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
537 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900538 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900539 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900540 return false
541 }
542 }
543 }
544 return true
545}
546
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900547func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansaria5c79892018-11-29 16:32:59 -0800548 key := OnuKey{intfid, onuid}
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900549 if onuState, ok := OnuOmciStateMap[key]; !ok {
Shad Ansaria5c79892018-11-29 16:32:59 -0800550 idx := uint32(0)
551 // Backwards compatible with bbsim_olt adapter
552 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
553 } else {
554 // FIXME - Gem Port ID is 2 bytes - fix openolt.proto
555 return uint32(onuState.gemPortId), nil
556 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557}
558
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900559func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
560 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900561 for _, onu := range onus {
562 if device.ValidateSN(*sn, *onu.SerialNumber) {
563 return onu, nil
564 }
565 }
566 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900567 err := errors.New("No mathced SN is found ")
568 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900569 return nil, err
570}
571
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800572func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
573 return getOnuByID(s.Onumap, onuid)
574}
575
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900576func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
577 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900578 for _, onu := range onus {
579 if onu.OnuID == onuid {
580 return onu, nil
581 }
582 }
583 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900584 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800585 logger.WithFields(log.Fields{
586 "onumap": onumap,
587 "onuid": onuid,
588 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900589 return nil, err
590}
591
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900592func convB2S(b []byte) string {
593 s := ""
594 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700595 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900596 }
597 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700598}