blob: 8ebdaa0cc00a374c2d3a755cfad19277431c6975 [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
Shad Ansaria5c79892018-11-29 16:32:59 -080060 omciIn chan openolt.OmciIndication
61 omciOut chan openolt.OmciMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090062}
63
64type Packet struct {
65 Info *Ioinfo
66 Pkt gopacket.Packet
67}
68
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090069type coreState int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090070
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090071const (
Matteo Scandolo88e91892018-11-06 16:29:19 -080072 INACTIVE = iota // OLT/ONUs are not instantiated
73 PRE_ACTIVE // Before PacketInDaemon Running
74 ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090075)
76
77/* coreState
78INACTIVE -> PRE_ACTIVE -> ACTIVE
79 (ActivateOLT) (Enable)
80 <- <-
Matteo Scandolo88e91892018-11-06 16:29:19 -080081*/
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090082
Shad Ansaria5c79892018-11-29 16:32:59 -080083func NewCore(opt *option, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090084 // TODO: make it decent
85 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080086 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087 nonus := opt.nonus
88 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080089 Olt: device.NewOlt(oltid, npon, 1),
90 Onumap: make(map[uint32][]*device.Onu),
91 Ioinfos: []*Ioinfo{},
92 gRPCAddress: opt.address,
93 gRPCPort: opt.port,
94 Vethnames: []string{},
95 IndInterval: opt.intvl,
96 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080097 EnableServer: nil,
Matteo Scandolo88e91892018-11-06 16:29:19 -080098 state: INACTIVE,
99 stateChan: make(chan coreState, 8),
Shad Ansari70aafb02018-11-14 22:28:17 -0800100 omciIn: omciIn,
101 omciOut: omciOut,
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900102 }
103
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900104 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800105 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900106 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900107 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900108 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900109
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900111 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700112 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900113 oltid := s.Olt.ID
114 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700116 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900117 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900118 return &s
119}
120
121//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800122func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900123 s.wg = &sync.WaitGroup{}
124 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800125 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900126 close(s.stateChan)
127 logger.Debug("Start() Done")
128 }()
129 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
130 listener, gserver, err := NewGrpcServer(addressport)
131 if err != nil {
132 logger.Error("Failed to create gRPC server", err)
133 return err
134 }
135 s.gRPCserver = gserver
136 openolt.RegisterOpenoltServer(gserver, s)
137 if err := gserver.Serve(listener); err != nil {
138 logger.Error("Failed to run gRPC server", err)
139 return err
140 }
141 s.wg.Wait()
142 return nil
143}
144
145//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800146func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900147 logger.Debug("Stop() Start")
148 defer logger.Debug("Stop() Done")
149 if s.gRPCserver != nil {
150 s.gRPCserver.Stop()
151 logger.Debug("gRPCserver.Stop()")
152 }
153 s.StopPktInDaemon()
154 return
155}
156
157// Blocking
158func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800159 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 olt := s.Olt
161 olt.InitializeStatus()
162 for intfid, _ := range s.Onumap {
163 for _, onu := range s.Onumap[intfid] {
164 onu.InitializeStatus()
165 }
166 }
167 s.updateState(INACTIVE)
168 logger.Debug("Enable() Done")
169 }()
170 logger.Debug("Enable() Start")
171 s.EnableServer = sv
172 if err := s.activateOLT(*sv); err != nil {
173 return err
174 }
175 s.updateState(PRE_ACTIVE)
176
177 coreCtx := context.Background()
178 coreCtx, corecancel := context.WithCancel(coreCtx)
179 s.cancel = corecancel
Matteo Scandolo88e91892018-11-06 16:29:19 -0800180 if err := s.StartPktInDaemon(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900181 return err
182 }
183 return nil
184}
185
186//Non-Blocking
187func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800188 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900189 logger.Debug("Disable() Done")
190 }()
191 logger.Debug("Disable() Start")
192 s.StopPktInDaemon()
193}
194
Matteo Scandolo88e91892018-11-06 16:29:19 -0800195func (s *Server) updateState(state coreState) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900196 s.state = state
197 s.stateChan <- state
198 logger.Debug("State updated to:%d", state)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900199}
200
201func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900202 defer logger.Debug("activateOLT() Done")
203 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900204 // Activate OLT
205 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900206 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900207 return err
208 }
209 olt.OperState = "up"
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900210 *olt.InternalState = device.OLT_UP
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800211 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900212
213 // OLT sends Interface Indication to Adapter
214 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800215 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900216 return err
217 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800218 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900219
220 // OLT sends Operation Indication to Adapter after activating each interface
221 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900222 *olt.InternalState = device.PONIF_UP
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900223 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800224 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900225 return err
226 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800227 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900228
229 // OLT sends ONU Discover Indication to Adapter after ONU discovery
230 for intfid, _ := range s.Onumap {
231 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
232 }
233
234 for intfid, _ := range s.Onumap {
235 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800236 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900237 }
238
239 // OLT Sends OnuInd after waiting all of those ONUs up
240 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900241 if IsAllOnuActive(s.Onumap) {
242 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900243 break
244 }
245 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900246
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900248 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800249 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900250 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900251 return nil
252}
253
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900254// Blocking
255func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
256 logger.Debug("StartPktInDaemon() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800257 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900258 RemoveVeths(s.Vethnames)
259 s.Vethnames = []string{}
260 s.Ioinfos = []*Ioinfo{}
261 s.wg.Done()
262 s.updateState(PRE_ACTIVE)
263 logger.Debug("StartPktInDaemon() Done")
264 }()
265 s.wg.Add(1)
266 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
267 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800268 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900269 return err
270 }
271 s.Ioinfos = ioinfos
272 s.Vethnames = veths
273 logger.Debug("Created vethnames:%v", s.Vethnames)
274
275 parent := ctx
276 child, cancel := context.WithCancel(parent)
277 s.cancel = cancel
278
279 if err = s.runPacketInDaemon(child, stream); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800280 logger.Error("runPacketInDaemon failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900281 return err
282 }
283 return nil
284}
285
286//Non-Blocking
287func (s *Server) StopPktInDaemon() {
288 if s.cancel != nil {
289 cancel := s.cancel
290 cancel()
291 }
292}
293
294func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900295 ioinfos := []*Ioinfo{}
296 var err error
297 for intfid, _ := range onumap {
298 for i := 0; i < len(onumap[intfid]); i++ {
299 var handler *pcap.Handle
300 onuid := onumap[intfid][i].OnuID
301 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900302 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800303 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900305 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900306 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900307 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900308 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900309 ioinfos = append(ioinfos, &oinfo)
310 }
311 }
312
313 var handler *pcap.Handle
314 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900315 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800316 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900317 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 Ansari70aafb02018-11-14 22:28:17 -0800358 case msg := <-s.omciIn:
Shad Ansarief436272018-11-14 15:58:20 -0800359 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
Shad Ansaria5c79892018-11-29 16:32:59 -0800360 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
Shad Ansarief436272018-11-14 15:58:20 -0800361 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:
Matteo Scandolo2a659142018-11-15 11:23:45 -0800366 logger.Debug("Received packet from UNI in grpc Server")
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 {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800408 utils.LoggerWithOnu(onu).WithFields(log.Fields{
409 "gemId": gemid,
410 }).Info("Received upstream packet is of unknow type, skipping.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900411 continue
412 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900413
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800414 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
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" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800423 logger.Debug("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
Matteo Scandolo2a659142018-11-15 11:23:45 -0800427 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900428
Matteo Scandolo2a659142018-11-15 11:23:45 -0800429 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900430 intfid := nnipkt.Info.intfid
431 pkt := nnipkt.Pkt
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800432 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900433 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
434 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800435 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900436 return err
437 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900438
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900439 case <-ctx.Done():
440 logger.Debug("PacketInDaemon thread receives close ")
441 close(unichannel)
442 logger.Debug("Closed unichannel ")
443 close(nnichannel)
444 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900445 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900446 }
447 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900448 return nil
449}
450
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900451func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
452 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800453 onu, _ := s.GetOnuByID(onuid)
454
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900455 if layerEth != nil {
456 pkt, _ := layerEth.(*layers.Ethernet)
457 ethtype := pkt.EthernetType
458 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800459 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900460 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800461 utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900462 rawpkt, _, _ = PopVLAN(rawpkt)
463 rawpkt, _, _ = PopVLAN(rawpkt)
464 } else {
465 return nil
466 }
467 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
468 if err != nil {
469 return err
470 }
471 handle := ioinfo.handler
472 SendUni(handle, rawpkt)
473 return nil
474 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900475 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900476 return nil
477}
478
479func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900480 poppkt, _, err := PopVLAN(rawpkt)
481 poppkt, _, err = PopVLAN(poppkt)
482 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900483 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900484 return err
485 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900486 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487 if err != nil {
488 return err
489 }
490 handle := ioinfo.handler
491 SendNni(handle, poppkt)
492 return nil
493}
494
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900495func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
496 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900497 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900498 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900499 return false
500 }
501 }
502 }
503 return true
504}
505
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900506func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansaria5c79892018-11-29 16:32:59 -0800507 key := OnuKey{intfid, onuid}
508 if onuState, ok := Onus[key]; !ok {
509 idx := uint32(0)
510 // Backwards compatible with bbsim_olt adapter
511 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
512 } else {
513 // FIXME - Gem Port ID is 2 bytes - fix openolt.proto
514 return uint32(onuState.gemPortId), nil
515 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900516}
517
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900518func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
519 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900520 for _, onu := range onus {
521 if device.ValidateSN(*sn, *onu.SerialNumber) {
522 return onu, nil
523 }
524 }
525 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900526 err := errors.New("No mathced SN is found ")
527 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900528 return nil, err
529}
530
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800531func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
532 return getOnuByID(s.Onumap, onuid)
533}
534
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900535func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
536 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 for _, onu := range onus {
538 if onu.OnuID == onuid {
539 return onu, nil
540 }
541 }
542 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900543 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800544 logger.WithFields(log.Fields{
545 "onumap": onumap,
546 "onuid": onuid,
547 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900548 return nil, err
549}
550
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900551func convB2S(b []byte) string {
552 s := ""
553 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700554 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900555 }
556 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700557}