blob: c38419de4a4b0fb1576c5f8d282b5b28429bfb8b [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
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090083func NewCore(opt *option) *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),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900100 omciIn: make(chan openolt.OmciIndication, 1024),
101 omciOut: make(chan openolt.OmciMsg, 1024),
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
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900333
334 logger.Debug("runOMCIDaemon Start")
335 defer logger.Debug("runOMCIDaemon Done")
336 errch := make (chan error)
337 OmciRun(s.omciOut, s.omciIn, s.Onumap, errch)
338 go func(){
339 <-errch // Wait for OmciInitialization
340 s.updateState(ACTIVE)
341 }()
342
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900343 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900344 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900345 onuid := onu.OnuID
346 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
347 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800348 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900349 return err
350 }
351 uhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900352 go RecvWorker(ioinfo, uhandler, unichannel)
353 }
354 }
355
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900356 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900357 if err != nil {
358 return err
359 }
360 nhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900361 nnichannel := make(chan Packet, 32)
362 go RecvWorker(ioinfo, nhandler, nnichannel)
363
364 data := &openolt.Indication_PktInd{}
365 for {
366 select {
Shad Ansari70aafb02018-11-14 22:28:17 -0800367 case msg := <-s.omciIn:
Shad Ansarief436272018-11-14 15:58:20 -0800368 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 -0800369 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
Shad Ansarief436272018-11-14 15:58:20 -0800370 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
371 logger.Error("send omci indication failed.", err)
372 continue
373 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900374 case unipkt := <-unichannel:
Matteo Scandolo2a659142018-11-15 11:23:45 -0800375 logger.Debug("Received packet from UNI in grpc Server")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900376 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800377 logger.Debug("WARNING: This packet does not come from UNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900378 continue
379 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900380
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900381 intfid := unipkt.Info.intfid
382 onuid := unipkt.Info.onuid
383 gemid, _ := getGemPortID(intfid, onuid)
384 pkt := unipkt.Pkt
385 layerEth := pkt.Layer(layers.LayerTypeEthernet)
386 le, _ := layerEth.(*layers.Ethernet)
387 ethtype := le.EthernetType
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800388 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900389
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900390 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800391 utils.LoggerWithOnu(onu).WithFields(log.Fields{
392 "gemId": gemid,
393 }).Info("Received upstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900394 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800395 utils.LoggerWithOnu(onu).WithFields(log.Fields{
396 "gemId": gemid,
397 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900398
399 //C-TAG
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900400 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700401 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900402 tagpkt, err := PushVLAN(pkt, uint16(ctag))
403 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800404 utils.LoggerWithOnu(onu).WithFields(log.Fields{
405 "gemId": gemid,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800406 }).Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900407 } else {
408 pkt = tagpkt
409 }
410 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800411 utils.LoggerWithOnu(onu).WithFields(log.Fields{
412 "gemId": gemid,
413 "cTagMap": s.CtagMap,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800414 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900415 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900416 } else {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800417 utils.LoggerWithOnu(onu).WithFields(log.Fields{
418 "gemId": gemid,
419 }).Info("Received upstream packet is of unknow type, skipping.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900420 continue
421 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900422
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800423 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900424 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
425 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800426 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900427 return err
428 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900429
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900430 case nnipkt := <-nnichannel:
431 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800432 logger.Debug("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900433 continue
434 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800435 onuid := nnipkt.Info.onuid
Matteo Scandolo2a659142018-11-15 11:23:45 -0800436 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900437
Matteo Scandolo2a659142018-11-15 11:23:45 -0800438 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900439 intfid := nnipkt.Info.intfid
440 pkt := nnipkt.Pkt
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800441 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900442 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
443 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800444 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900445 return err
446 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900447
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900448 case <-ctx.Done():
449 logger.Debug("PacketInDaemon thread receives close ")
450 close(unichannel)
451 logger.Debug("Closed unichannel ")
452 close(nnichannel)
453 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900454 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900455 }
456 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900457 return nil
458}
459
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900460func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
461 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800462 onu, _ := s.GetOnuByID(onuid)
463
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900464 if layerEth != nil {
465 pkt, _ := layerEth.(*layers.Ethernet)
466 ethtype := pkt.EthernetType
467 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800468 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900469 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800470 utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900471 rawpkt, _, _ = PopVLAN(rawpkt)
472 rawpkt, _, _ = PopVLAN(rawpkt)
473 } else {
474 return nil
475 }
476 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
477 if err != nil {
478 return err
479 }
480 handle := ioinfo.handler
481 SendUni(handle, rawpkt)
482 return nil
483 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900484 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900485 return nil
486}
487
488func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900489 poppkt, _, err := PopVLAN(rawpkt)
490 poppkt, _, err = PopVLAN(poppkt)
491 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900492 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900493 return err
494 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900495 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496 if err != nil {
497 return err
498 }
499 handle := ioinfo.handler
500 SendNni(handle, poppkt)
501 return nil
502}
503
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900504func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
505 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900506 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900507 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900508 return false
509 }
510 }
511 }
512 return true
513}
514
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900515func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansaria5c79892018-11-29 16:32:59 -0800516 key := OnuKey{intfid, onuid}
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900517 if onuState, ok := OnuOmciStateMap[key]; !ok {
Shad Ansaria5c79892018-11-29 16:32:59 -0800518 idx := uint32(0)
519 // Backwards compatible with bbsim_olt adapter
520 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
521 } else {
522 // FIXME - Gem Port ID is 2 bytes - fix openolt.proto
523 return uint32(onuState.gemPortId), nil
524 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900525}
526
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900527func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
528 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900529 for _, onu := range onus {
530 if device.ValidateSN(*sn, *onu.SerialNumber) {
531 return onu, nil
532 }
533 }
534 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900535 err := errors.New("No mathced SN is found ")
536 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 return nil, err
538}
539
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800540func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
541 return getOnuByID(s.Onumap, onuid)
542}
543
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900544func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
545 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900546 for _, onu := range onus {
547 if onu.OnuID == onuid {
548 return onu, nil
549 }
550 }
551 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900552 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800553 logger.WithFields(log.Fields{
554 "onumap": onumap,
555 "onuid": onuid,
556 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557 return nil, err
558}
559
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900560func convB2S(b []byte) string {
561 s := ""
562 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700563 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900564 }
565 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700566}