blob: 55166dc3a3c14780ac231c16411a8b1f5ab52657 [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{},
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090095 EnableServer: new(openolt.Openolt_EnableIndicationServer),
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
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900101 logger.Info("OLT ID: %d was retrieved.\n", 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
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900207 logger.Info("OLT %s sent OltInd.\n", 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 {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900211 logger.Error("Fail to sendIntfInd: %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900212 return err
213 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900214 logger.Info("OLT %s sent IntfInd.\n", 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 {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900220 logger.Error("Fail to sendOperInd: %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900221 return err
222 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900223 logger.Info("OLT %s sent OperInd.\n", 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])
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900232 logger.Info("OLT id:%d sent ONUDiscInd.\n", 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)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900245 logger.Info("OLT id:%d sent ONUInd.\n", 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 {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900330 logger.Error("Fail to identifyUniIoinfo (onuid: %d): %v\n", 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" {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900353 logger.Info("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 Scandolo88e91892018-11-06 16:29:19 -0800364 onu, _ := getOnuByID(s.Onumap, onuid)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900365
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900366 if ethtype == 0x888e {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800367 logger.WithFields(log.Fields{
368 "serial_number": utils.OnuToSn(onu),
369 "gemId": gemid,
370 "interfaceId": intfid,
371 "onuId": onuid,
372 }).Debug("Received upstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900373 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800374 logger.WithFields(log.Fields{
375 "serial_number": utils.OnuToSn(onu),
376 "gemId": gemid,
377 "interfaceId": intfid,
378 "onuId": onuid,
379 }).Debug("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900380
381 //C-TAG
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900382 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700383 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900384 tagpkt, err := PushVLAN(pkt, uint16(ctag))
385 if err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800386 logger.WithFields(log.Fields{
387 "serial_number": utils.OnuToSn(onu),
388 "gemId": gemid,
389 "interfaceId": intfid,
390 "onuId": onuid,
391 }).Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900392 } else {
393 pkt = tagpkt
394 }
395 } else {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800396 logger.WithFields(log.Fields{
397 "serial_number": utils.OnuToSn(onu),
398 "gemId": gemid,
399 "interfaceId": intfid,
400 "onuId": onuid,
401 "sn": sn,
402 "cTagMap": s.CtagMap,
403 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900404 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900405 } else {
406 continue
407 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900408
Matteo Scandolo88e91892018-11-06 16:29:19 -0800409 logger.WithFields(log.Fields{
410 "serial_number": utils.OnuToSn(onu),
411 "gemId": gemid,
412 "interfaceId": intfid,
413 "onuId": onuid,
414 }).Debug("sendPktInd")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900415 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, 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 NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900421 case nnipkt := <-nnichannel:
422 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900423 logger.Info("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900424 continue
425 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800426 onuid := nnipkt.Info.onuid
427 onu, _ := getOnuByID(s.Onumap, onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900428
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900429 logger.Debug("Received packet in grpc Server from NNI.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900430 intfid := nnipkt.Info.intfid
431 pkt := nnipkt.Pkt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800432 logger.WithFields(log.Fields{
433 "interfaceId": intfid,
434 "serial_number": utils.OnuToSn(onu),
435 }).Info("sendPktInd")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900436 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
437 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800438 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900439 return err
440 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900441
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900442 case <-ctx.Done():
443 logger.Debug("PacketInDaemon thread receives close ")
444 close(unichannel)
445 logger.Debug("Closed unichannel ")
446 close(nnichannel)
447 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900448 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900449 }
450 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900451 return nil
452}
453
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900454func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
455 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
456 if layerEth != nil {
457 pkt, _ := layerEth.(*layers.Ethernet)
458 ethtype := pkt.EthernetType
459 if ethtype == 0x888e {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900460 logger.Debug("Received downstream packet is EAPOL.")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800461 //logger.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900462 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900463 logger.Debug("Received downstream packet is DHCP.")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800464 //logger.Println(rawpkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900465 rawpkt, _, _ = PopVLAN(rawpkt)
466 rawpkt, _, _ = PopVLAN(rawpkt)
467 } else {
468 return nil
469 }
470 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
471 if err != nil {
472 return err
473 }
474 handle := ioinfo.handler
475 SendUni(handle, rawpkt)
476 return nil
477 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900478 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900479 return nil
480}
481
482func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900483 poppkt, _, err := PopVLAN(rawpkt)
484 poppkt, _, err = PopVLAN(poppkt)
485 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900486 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487 return err
488 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900489 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900490 if err != nil {
491 return err
492 }
493 handle := ioinfo.handler
494 SendNni(handle, poppkt)
495 return nil
496}
497
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900498func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
499 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900500 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900501 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900502 return false
503 }
504 }
505 }
506 return true
507}
508
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900509func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
510 idx := uint32(0)
511 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
512 //return uint32(1032 + 8 * (vid - 1)), nil
513}
514
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900515func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
516 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900517 for _, onu := range onus {
518 if device.ValidateSN(*sn, *onu.SerialNumber) {
519 return onu, nil
520 }
521 }
522 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900523 err := errors.New("No mathced SN is found ")
524 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900525 return nil, err
526}
527
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900528func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
529 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900530 for _, onu := range onus {
531 if onu.OnuID == onuid {
532 return onu, nil
533 }
534 }
535 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900536 err := errors.New("No matched OnuID is found ")
537 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900538 return nil, err
539}
540
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900541func convB2S(b []byte) string {
542 s := ""
543 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700544 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900545 }
546 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700547}