blob: 6a32da3a98125453007f42513280a6fd16699a31 [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 NISHIMOTO3b8b9c02018-10-09 09:40:01 +090034)
35
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090036const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090037 UNI_VETH_UP_PFX = "sim_uu"
38 UNI_VETH_DW_PFX = "sim_ud"
39 NNI_VETH_UP_PFX = "sim_nu"
40 NNI_VETH_DW_PFX = "sim_nd"
41 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042)
43
Shad Ansarief436272018-11-14 15:58:20 -080044type OmciIndication struct {
45 IntfId uint32
46 OnuId uint32
47 Pkt []byte
48}
49
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090050type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090051 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052 Olt *device.Olt
53 Onumap map[uint32][]*device.Onu
54 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090055 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090056 gRPCAddress string
57 gRPCPort uint32
58 Vethnames []string
59 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090060 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090061 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090062 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090063 cancel context.CancelFunc
64 state coreState
65 stateChan chan coreState
Shad Ansarief436272018-11-14 15:58:20 -080066 omciChan chan OmciIndication
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067}
68
69type Packet struct {
70 Info *Ioinfo
71 Pkt gopacket.Packet
72}
73
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090074type coreState int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090075
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090076const (
Matteo Scandolo88e91892018-11-06 16:29:19 -080077 INACTIVE = iota // OLT/ONUs are not instantiated
78 PRE_ACTIVE // Before PacketInDaemon Running
79 ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080)
81
82/* coreState
83INACTIVE -> PRE_ACTIVE -> ACTIVE
84 (ActivateOLT) (Enable)
85 <- <-
Matteo Scandolo88e91892018-11-06 16:29:19 -080086*/
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087
Matteo Scandolo88e91892018-11-06 16:29:19 -080088func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090089 // TODO: make it decent
90 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080091 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090092 nonus := opt.nonus
93 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080094 Olt: device.NewOlt(oltid, npon, 1),
95 Onumap: make(map[uint32][]*device.Onu),
96 Ioinfos: []*Ioinfo{},
97 gRPCAddress: opt.address,
98 gRPCPort: opt.port,
99 Vethnames: []string{},
100 IndInterval: opt.intvl,
101 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -0800102 EnableServer: nil,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800103 state: INACTIVE,
104 stateChan: make(chan coreState, 8),
Shad Ansarief436272018-11-14 15:58:20 -0800105 omciChan: make(chan OmciIndication, 8),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900106 }
107
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900108 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800109 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900110 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900112 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900113
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900115 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700116 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900117 oltid := s.Olt.ID
118 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700120 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900121 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 return &s
123}
124
125//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800126func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900127 s.wg = &sync.WaitGroup{}
128 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800129 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900130 close(s.stateChan)
131 logger.Debug("Start() Done")
132 }()
133 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
134 listener, gserver, err := NewGrpcServer(addressport)
135 if err != nil {
136 logger.Error("Failed to create gRPC server", err)
137 return err
138 }
139 s.gRPCserver = gserver
140 openolt.RegisterOpenoltServer(gserver, s)
141 if err := gserver.Serve(listener); err != nil {
142 logger.Error("Failed to run gRPC server", err)
143 return err
144 }
145 s.wg.Wait()
146 return nil
147}
148
149//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800150func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900151 logger.Debug("Stop() Start")
152 defer logger.Debug("Stop() Done")
153 if s.gRPCserver != nil {
154 s.gRPCserver.Stop()
155 logger.Debug("gRPCserver.Stop()")
156 }
157 s.StopPktInDaemon()
158 return
159}
160
161// Blocking
162func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800163 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900164 olt := s.Olt
165 olt.InitializeStatus()
166 for intfid, _ := range s.Onumap {
167 for _, onu := range s.Onumap[intfid] {
168 onu.InitializeStatus()
169 }
170 }
171 s.updateState(INACTIVE)
172 logger.Debug("Enable() Done")
173 }()
174 logger.Debug("Enable() Start")
175 s.EnableServer = sv
176 if err := s.activateOLT(*sv); err != nil {
177 return err
178 }
179 s.updateState(PRE_ACTIVE)
180
181 coreCtx := context.Background()
182 coreCtx, corecancel := context.WithCancel(coreCtx)
183 s.cancel = corecancel
Matteo Scandolo88e91892018-11-06 16:29:19 -0800184 if err := s.StartPktInDaemon(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185 return err
186 }
187 return nil
188}
189
190//Non-Blocking
191func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800192 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900193 logger.Debug("Disable() Done")
194 }()
195 logger.Debug("Disable() Start")
196 s.StopPktInDaemon()
197}
198
Matteo Scandolo88e91892018-11-06 16:29:19 -0800199func (s *Server) updateState(state coreState) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 s.state = state
201 s.stateChan <- state
202 logger.Debug("State updated to:%d", state)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900203}
204
205func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900206 defer logger.Debug("activateOLT() Done")
207 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900208 // Activate OLT
209 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900210 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900211 return err
212 }
213 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900214 *olt.InternalState = device.OLT_UP
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800215 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900216
217 // OLT sends Interface Indication to Adapter
218 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800219 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900220 return err
221 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800222 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900223
224 // OLT sends Operation Indication to Adapter after activating each interface
225 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900226 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900227 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800228 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900229 return err
230 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800231 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900232
233 // OLT sends ONU Discover Indication to Adapter after ONU discovery
234 for intfid, _ := range s.Onumap {
235 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
236 }
237
238 for intfid, _ := range s.Onumap {
239 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800240 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900241 }
242
243 // OLT Sends OnuInd after waiting all of those ONUs up
244 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900245 if IsAllOnuActive(s.Onumap) {
246 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 break
248 }
249 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900250
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900251 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900252 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800253 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900254 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900255 return nil
256}
257
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900258// Blocking
259func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
260 logger.Debug("StartPktInDaemon() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800261 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900262 RemoveVeths(s.Vethnames)
263 s.Vethnames = []string{}
264 s.Ioinfos = []*Ioinfo{}
265 s.wg.Done()
266 s.updateState(PRE_ACTIVE)
267 logger.Debug("StartPktInDaemon() Done")
268 }()
269 s.wg.Add(1)
270 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
271 if err != nil {
272 return err
273 }
274 s.Ioinfos = ioinfos
275 s.Vethnames = veths
276 logger.Debug("Created vethnames:%v", s.Vethnames)
277
278 parent := ctx
279 child, cancel := context.WithCancel(parent)
280 s.cancel = cancel
281
282 if err = s.runPacketInDaemon(child, stream); err != nil {
283 return err
284 }
285 return nil
286}
287
288//Non-Blocking
289func (s *Server) StopPktInDaemon() {
290 if s.cancel != nil {
291 cancel := s.cancel
292 cancel()
293 }
294}
295
296func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900297 ioinfos := []*Ioinfo{}
298 var err error
299 for intfid, _ := range onumap {
300 for i := 0; i < len(onumap[intfid]); i++ {
301 var handler *pcap.Handle
302 onuid := onumap[intfid][i].OnuID
303 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
305 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 {
317 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900318 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900319
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900320 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900321 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900322 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900323 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900324 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900325}
326
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900327//Blocking
328func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900329 logger.Debug("runPacketInDaemon Start")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900330 defer logger.Debug("runPacketInDaemon Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900331 unichannel := make(chan Packet, 2048)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900332
333 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900334 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900335 onuid := onu.OnuID
336 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
337 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800338 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900339 return err
340 }
341 uhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900342 go RecvWorker(ioinfo, uhandler, unichannel)
343 }
344 }
345
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900346 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900347 if err != nil {
348 return err
349 }
350 nhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900351 nnichannel := make(chan Packet, 32)
352 go RecvWorker(ioinfo, nhandler, nnichannel)
353
354 data := &openolt.Indication_PktInd{}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900355 s.updateState(ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900356 for {
357 select {
Shad Ansarief436272018-11-14 15:58:20 -0800358 case msg := <-s.omciChan:
359 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
360 omci := &openolt.Indication_OmciInd{OmciInd: &openolt.OmciIndication{IntfId: msg.IntfId, OnuId: msg.OnuId, Pkt: msg.Pkt}}
361 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
362 logger.Error("send omci indication failed.", err)
363 continue
364 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900365 case unipkt := <-unichannel:
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900366 logger.Debug("Received packet in grpc Server from UNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900367 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800368 logger.Debug("WARNING: This packet does not come from UNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900369 continue
370 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900371
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900372 intfid := unipkt.Info.intfid
373 onuid := unipkt.Info.onuid
374 gemid, _ := getGemPortID(intfid, onuid)
375 pkt := unipkt.Pkt
376 layerEth := pkt.Layer(layers.LayerTypeEthernet)
377 le, _ := layerEth.(*layers.Ethernet)
378 ethtype := le.EthernetType
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800379 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900380
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900381 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800382 utils.LoggerWithOnu(onu).WithFields(log.Fields{
383 "gemId": gemid,
384 }).Info("Received upstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900385 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800386 utils.LoggerWithOnu(onu).WithFields(log.Fields{
387 "gemId": gemid,
388 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900389
390 //C-TAG
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900391 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700392 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900393 tagpkt, err := PushVLAN(pkt, uint16(ctag))
394 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800395 utils.LoggerWithOnu(onu).WithFields(log.Fields{
396 "gemId": gemid,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800397 }).Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900398 } else {
399 pkt = tagpkt
400 }
401 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800402 utils.LoggerWithOnu(onu).WithFields(log.Fields{
403 "gemId": gemid,
404 "cTagMap": s.CtagMap,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800405 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900406 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900407 } else {
408 continue
409 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900410
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800411 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900412 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
413 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800414 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900415 return err
416 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900417
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900418 case nnipkt := <-nnichannel:
419 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800420 logger.Debug("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900421 continue
422 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800423 onuid := nnipkt.Info.onuid
424 onu, _ := getOnuByID(s.Onumap, onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900425
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800426 utils.LoggerWithOnu(onu).Info("Received packet in grpc Server from NNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900427 intfid := nnipkt.Info.intfid
428 pkt := nnipkt.Pkt
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800429 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900430 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
431 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800432 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900433 return err
434 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900435
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900436 case <-ctx.Done():
437 logger.Debug("PacketInDaemon thread receives close ")
438 close(unichannel)
439 logger.Debug("Closed unichannel ")
440 close(nnichannel)
441 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900442 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900443 }
444 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900445 return nil
446}
447
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900448func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
449 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800450 onu, _ := s.GetOnuByID(onuid)
451
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900452 if layerEth != nil {
453 pkt, _ := layerEth.(*layers.Ethernet)
454 ethtype := pkt.EthernetType
455 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800456 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900457 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800458 utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900459 rawpkt, _, _ = PopVLAN(rawpkt)
460 rawpkt, _, _ = PopVLAN(rawpkt)
461 } else {
462 return nil
463 }
464 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
465 if err != nil {
466 return err
467 }
468 handle := ioinfo.handler
469 SendUni(handle, rawpkt)
470 return nil
471 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900472 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900473 return nil
474}
475
476func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900477 poppkt, _, err := PopVLAN(rawpkt)
478 poppkt, _, err = PopVLAN(poppkt)
479 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900480 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900481 return err
482 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900483 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900484 if err != nil {
485 return err
486 }
487 handle := ioinfo.handler
488 SendNni(handle, poppkt)
489 return nil
490}
491
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900492func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
493 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900494 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900495 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496 return false
497 }
498 }
499 }
500 return true
501}
502
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900503func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
504 idx := uint32(0)
505 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
506 //return uint32(1032 + 8 * (vid - 1)), nil
507}
508
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900509func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
510 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900511 for _, onu := range onus {
512 if device.ValidateSN(*sn, *onu.SerialNumber) {
513 return onu, nil
514 }
515 }
516 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900517 err := errors.New("No mathced SN is found ")
518 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900519 return nil, err
520}
521
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800522func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
523 return getOnuByID(s.Onumap, onuid)
524}
525
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900526func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
527 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900528 for _, onu := range onus {
529 if onu.OnuID == onuid {
530 return onu, nil
531 }
532 }
533 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900534 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800535 logger.WithFields(log.Fields{
536 "onumap": onumap,
537 "onuid": onuid,
538 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900539 return nil, err
540}
541
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900542func convB2S(b []byte) string {
543 s := ""
544 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700545 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900546 }
547 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700548}