blob: f00d72d3df7ed78362ade43c76424687ef206d0a [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
44type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090045 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046 Olt *device.Olt
47 Onumap map[uint32][]*device.Onu
48 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090050 gRPCAddress string
51 gRPCPort uint32
52 Vethnames []string
53 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090054 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090055 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090056 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090057 cancel context.CancelFunc
58 state coreState
59 stateChan chan coreState
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090060}
61
62type Packet struct {
63 Info *Ioinfo
64 Pkt gopacket.Packet
65}
66
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090067type coreState int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090068
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090069const (
Matteo Scandolo88e91892018-11-06 16:29:19 -080070 INACTIVE = iota // OLT/ONUs are not instantiated
71 PRE_ACTIVE // Before PacketInDaemon Running
72 ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090073)
74
75/* coreState
76INACTIVE -> PRE_ACTIVE -> ACTIVE
77 (ActivateOLT) (Enable)
78 <- <-
Matteo Scandolo88e91892018-11-06 16:29:19 -080079*/
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080
Matteo Scandolo88e91892018-11-06 16:29:19 -080081func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090082 // TODO: make it decent
83 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080084 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090085 nonus := opt.nonus
86 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080087 Olt: device.NewOlt(oltid, npon, 1),
88 Onumap: make(map[uint32][]*device.Onu),
89 Ioinfos: []*Ioinfo{},
90 gRPCAddress: opt.address,
91 gRPCPort: opt.port,
92 Vethnames: []string{},
93 IndInterval: opt.intvl,
94 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080095 EnableServer: nil,
Matteo Scandolo88e91892018-11-06 16:29:19 -080096 state: INACTIVE,
97 stateChan: make(chan coreState, 8),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090098 }
99
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900100 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800101 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900102 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900104 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900105
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900106 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900107 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700108 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900109 oltid := s.Olt.ID
110 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700112 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900113 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114 return &s
115}
116
117//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800118func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 s.wg = &sync.WaitGroup{}
120 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800121 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 close(s.stateChan)
123 logger.Debug("Start() Done")
124 }()
125 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
126 listener, gserver, err := NewGrpcServer(addressport)
127 if err != nil {
128 logger.Error("Failed to create gRPC server", err)
129 return err
130 }
131 s.gRPCserver = gserver
132 openolt.RegisterOpenoltServer(gserver, s)
133 if err := gserver.Serve(listener); err != nil {
134 logger.Error("Failed to run gRPC server", err)
135 return err
136 }
137 s.wg.Wait()
138 return nil
139}
140
141//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800142func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900143 logger.Debug("Stop() Start")
144 defer logger.Debug("Stop() Done")
145 if s.gRPCserver != nil {
146 s.gRPCserver.Stop()
147 logger.Debug("gRPCserver.Stop()")
148 }
149 s.StopPktInDaemon()
150 return
151}
152
153// Blocking
154func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800155 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900156 olt := s.Olt
157 olt.InitializeStatus()
158 for intfid, _ := range s.Onumap {
159 for _, onu := range s.Onumap[intfid] {
160 onu.InitializeStatus()
161 }
162 }
163 s.updateState(INACTIVE)
164 logger.Debug("Enable() Done")
165 }()
166 logger.Debug("Enable() Start")
167 s.EnableServer = sv
168 if err := s.activateOLT(*sv); err != nil {
169 return err
170 }
171 s.updateState(PRE_ACTIVE)
172
173 coreCtx := context.Background()
174 coreCtx, corecancel := context.WithCancel(coreCtx)
175 s.cancel = corecancel
Matteo Scandolo88e91892018-11-06 16:29:19 -0800176 if err := s.StartPktInDaemon(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 return err
178 }
179 return nil
180}
181
182//Non-Blocking
183func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800184 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185 logger.Debug("Disable() Done")
186 }()
187 logger.Debug("Disable() Start")
188 s.StopPktInDaemon()
189}
190
Matteo Scandolo88e91892018-11-06 16:29:19 -0800191func (s *Server) updateState(state coreState) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 s.state = state
193 s.stateChan <- state
194 logger.Debug("State updated to:%d", state)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900195}
196
197func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900198 defer logger.Debug("activateOLT() Done")
199 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900200 // Activate OLT
201 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900202 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900203 return err
204 }
205 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900206 *olt.InternalState = device.OLT_UP
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800207 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900208
209 // OLT sends Interface Indication to Adapter
210 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800211 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900212 return err
213 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800214 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900215
216 // OLT sends Operation Indication to Adapter after activating each interface
217 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900218 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900219 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800220 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900221 return err
222 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800223 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900224
225 // OLT sends ONU Discover Indication to Adapter after ONU discovery
226 for intfid, _ := range s.Onumap {
227 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
228 }
229
230 for intfid, _ := range s.Onumap {
231 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800232 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900233 }
234
235 // OLT Sends OnuInd after waiting all of those ONUs up
236 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900237 if IsAllOnuActive(s.Onumap) {
238 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239 break
240 }
241 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900242
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900243 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900244 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800245 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900246 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 return nil
248}
249
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900250// Blocking
251func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
252 logger.Debug("StartPktInDaemon() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800253 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900254 RemoveVeths(s.Vethnames)
255 s.Vethnames = []string{}
256 s.Ioinfos = []*Ioinfo{}
257 s.wg.Done()
258 s.updateState(PRE_ACTIVE)
259 logger.Debug("StartPktInDaemon() Done")
260 }()
261 s.wg.Add(1)
262 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
263 if err != nil {
264 return err
265 }
266 s.Ioinfos = ioinfos
267 s.Vethnames = veths
268 logger.Debug("Created vethnames:%v", s.Vethnames)
269
270 parent := ctx
271 child, cancel := context.WithCancel(parent)
272 s.cancel = cancel
273
274 if err = s.runPacketInDaemon(child, stream); err != nil {
275 return err
276 }
277 return nil
278}
279
280//Non-Blocking
281func (s *Server) StopPktInDaemon() {
282 if s.cancel != nil {
283 cancel := s.cancel
284 cancel()
285 }
286}
287
288func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900289 ioinfos := []*Ioinfo{}
290 var err error
291 for intfid, _ := range onumap {
292 for i := 0; i < len(onumap[intfid]); i++ {
293 var handler *pcap.Handle
294 onuid := onumap[intfid][i].OnuID
295 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900296 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
297 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900298 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900299 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900300 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900301 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900302 ioinfos = append(ioinfos, &oinfo)
303 }
304 }
305
306 var handler *pcap.Handle
307 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900308 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
309 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900310 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900311
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900312 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900313 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900314 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900315 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900316 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900317}
318
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900319//Blocking
320func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900321 logger.Debug("runPacketInDaemon Start")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900322 defer logger.Debug("runPacketInDaemon Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900323 unichannel := make(chan Packet, 2048)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900324
325 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900327 onuid := onu.OnuID
328 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
329 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800330 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900331 return err
332 }
333 uhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900334 go RecvWorker(ioinfo, uhandler, unichannel)
335 }
336 }
337
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900338 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900339 if err != nil {
340 return err
341 }
342 nhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900343 nnichannel := make(chan Packet, 32)
344 go RecvWorker(ioinfo, nhandler, nnichannel)
345
346 data := &openolt.Indication_PktInd{}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900347 s.updateState(ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900348 for {
349 select {
350 case unipkt := <-unichannel:
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900351 logger.Debug("Received packet in grpc Server from UNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900352 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800353 logger.Debug("WARNING: This packet does not come from UNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900354 continue
355 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900356
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900357 intfid := unipkt.Info.intfid
358 onuid := unipkt.Info.onuid
359 gemid, _ := getGemPortID(intfid, onuid)
360 pkt := unipkt.Pkt
361 layerEth := pkt.Layer(layers.LayerTypeEthernet)
362 le, _ := layerEth.(*layers.Ethernet)
363 ethtype := le.EthernetType
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800364 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900365
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900366 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800367 utils.LoggerWithOnu(onu).WithFields(log.Fields{
368 "gemId": gemid,
369 }).Info("Received upstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900370 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800371 utils.LoggerWithOnu(onu).WithFields(log.Fields{
372 "gemId": gemid,
373 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900374
375 //C-TAG
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900376 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700377 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900378 tagpkt, err := PushVLAN(pkt, uint16(ctag))
379 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800380 utils.LoggerWithOnu(onu).WithFields(log.Fields{
381 "gemId": gemid,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800382 }).Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900383 } else {
384 pkt = tagpkt
385 }
386 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800387 utils.LoggerWithOnu(onu).WithFields(log.Fields{
388 "gemId": gemid,
389 "cTagMap": s.CtagMap,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800390 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900391 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900392 } else {
393 continue
394 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900395
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800396 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900397 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
398 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800399 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900400 return err
401 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900402
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900403 case nnipkt := <-nnichannel:
404 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800405 logger.Debug("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900406 continue
407 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800408 onuid := nnipkt.Info.onuid
409 onu, _ := getOnuByID(s.Onumap, onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900410
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800411 utils.LoggerWithOnu(onu).Info("Received packet in grpc Server from NNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900412 intfid := nnipkt.Info.intfid
413 pkt := nnipkt.Pkt
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800414 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900415 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
416 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800417 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900418 return err
419 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900420
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900421 case <-ctx.Done():
422 logger.Debug("PacketInDaemon thread receives close ")
423 close(unichannel)
424 logger.Debug("Closed unichannel ")
425 close(nnichannel)
426 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900427 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900428 }
429 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900430 return nil
431}
432
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900433func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
434 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800435 onu, _ := s.GetOnuByID(onuid)
436
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900437 if layerEth != nil {
438 pkt, _ := layerEth.(*layers.Ethernet)
439 ethtype := pkt.EthernetType
440 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800441 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900442 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800443 utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900444 rawpkt, _, _ = PopVLAN(rawpkt)
445 rawpkt, _, _ = PopVLAN(rawpkt)
446 } else {
447 return nil
448 }
449 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
450 if err != nil {
451 return err
452 }
453 handle := ioinfo.handler
454 SendUni(handle, rawpkt)
455 return nil
456 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900457 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900458 return nil
459}
460
461func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900462 poppkt, _, err := PopVLAN(rawpkt)
463 poppkt, _, err = PopVLAN(poppkt)
464 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900465 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900466 return err
467 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900468 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900469 if err != nil {
470 return err
471 }
472 handle := ioinfo.handler
473 SendNni(handle, poppkt)
474 return nil
475}
476
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900477func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
478 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900479 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900480 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900481 return false
482 }
483 }
484 }
485 return true
486}
487
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900488func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
489 idx := uint32(0)
490 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
491 //return uint32(1032 + 8 * (vid - 1)), nil
492}
493
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900494func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
495 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496 for _, onu := range onus {
497 if device.ValidateSN(*sn, *onu.SerialNumber) {
498 return onu, nil
499 }
500 }
501 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900502 err := errors.New("No mathced SN is found ")
503 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900504 return nil, err
505}
506
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800507func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
508 return getOnuByID(s.Onumap, onuid)
509}
510
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900511func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
512 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900513 for _, onu := range onus {
514 if onu.OnuID == onuid {
515 return onu, nil
516 }
517 }
518 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900519 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800520 logger.WithFields(log.Fields{
521 "onumap": onumap,
522 "onuid": onuid,
523 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900524 return nil, err
525}
526
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900527func convB2S(b []byte) string {
528 s := ""
529 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700530 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900531 }
532 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700533}