blob: 964d7042240a03459987facd420cbaa947336ed7 [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 Ansari70aafb02018-11-14 22:28:17 -080060 omciIn chan OmciMsg
61 omciOut chan 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 Ansari70aafb02018-11-14 22:28:17 -080083func NewCore(opt *option, omciOut chan OmciMsg, omciIn chan OmciMsg) *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 {
268 return err
269 }
270 s.Ioinfos = ioinfos
271 s.Vethnames = veths
272 logger.Debug("Created vethnames:%v", s.Vethnames)
273
274 parent := ctx
275 child, cancel := context.WithCancel(parent)
276 s.cancel = cancel
277
278 if err = s.runPacketInDaemon(child, stream); err != nil {
279 return err
280 }
281 return nil
282}
283
284//Non-Blocking
285func (s *Server) StopPktInDaemon() {
286 if s.cancel != nil {
287 cancel := s.cancel
288 cancel()
289 }
290}
291
292func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900293 ioinfos := []*Ioinfo{}
294 var err error
295 for intfid, _ := range onumap {
296 for i := 0; i < len(onumap[intfid]); i++ {
297 var handler *pcap.Handle
298 onuid := onumap[intfid][i].OnuID
299 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900300 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
301 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900302 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900304 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900305 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900306 ioinfos = append(ioinfos, &oinfo)
307 }
308 }
309
310 var handler *pcap.Handle
311 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900312 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
313 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900314 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900315
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900316 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900317 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900318 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900319 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900320 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900321}
322
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900323//Blocking
324func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900325 logger.Debug("runPacketInDaemon Start")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900326 defer logger.Debug("runPacketInDaemon Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900327 unichannel := make(chan Packet, 2048)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900328
329 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900330 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900331 onuid := onu.OnuID
332 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
333 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800334 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900335 return err
336 }
337 uhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900338 go RecvWorker(ioinfo, uhandler, unichannel)
339 }
340 }
341
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900342 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900343 if err != nil {
344 return err
345 }
346 nhandler := ioinfo.handler
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900347 nnichannel := make(chan Packet, 32)
348 go RecvWorker(ioinfo, nhandler, nnichannel)
349
350 data := &openolt.Indication_PktInd{}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900351 s.updateState(ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900352 for {
353 select {
Shad Ansari70aafb02018-11-14 22:28:17 -0800354 case msg := <-s.omciIn:
Shad Ansarief436272018-11-14 15:58:20 -0800355 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
356 omci := &openolt.Indication_OmciInd{OmciInd: &openolt.OmciIndication{IntfId: msg.IntfId, OnuId: msg.OnuId, Pkt: msg.Pkt}}
357 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
358 logger.Error("send omci indication failed.", err)
359 continue
360 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900361 case unipkt := <-unichannel:
Matteo Scandolo2a659142018-11-15 11:23:45 -0800362 logger.Debug("Received packet from UNI in grpc Server")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900363 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800364 logger.Debug("WARNING: This packet does not come from UNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900365 continue
366 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900367
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900368 intfid := unipkt.Info.intfid
369 onuid := unipkt.Info.onuid
370 gemid, _ := getGemPortID(intfid, onuid)
371 pkt := unipkt.Pkt
372 layerEth := pkt.Layer(layers.LayerTypeEthernet)
373 le, _ := layerEth.(*layers.Ethernet)
374 ethtype := le.EthernetType
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800375 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900376
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900377 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800378 utils.LoggerWithOnu(onu).WithFields(log.Fields{
379 "gemId": gemid,
380 }).Info("Received upstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900381 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800382 utils.LoggerWithOnu(onu).WithFields(log.Fields{
383 "gemId": gemid,
384 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900385
386 //C-TAG
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900387 sn := convB2S(onu.SerialNumber.VendorSpecific)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700388 if ctag, ok := s.CtagMap[sn]; ok == true {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900389 tagpkt, err := PushVLAN(pkt, uint16(ctag))
390 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800391 utils.LoggerWithOnu(onu).WithFields(log.Fields{
392 "gemId": gemid,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800393 }).Error("Fail to tag C-tag")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900394 } else {
395 pkt = tagpkt
396 }
397 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800398 utils.LoggerWithOnu(onu).WithFields(log.Fields{
399 "gemId": gemid,
400 "cTagMap": s.CtagMap,
Matteo Scandolo88e91892018-11-06 16:29:19 -0800401 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900402 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900403 } else {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800404 utils.LoggerWithOnu(onu).WithFields(log.Fields{
405 "gemId": gemid,
406 }).Info("Received upstream packet is of unknow type, skipping.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900407 continue
408 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900409
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800410 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900411 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
412 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800413 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900414 return err
415 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900416
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900417 case nnipkt := <-nnichannel:
418 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800419 logger.Debug("WARNING: This packet does not come from NNI ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900420 continue
421 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800422 onuid := nnipkt.Info.onuid
Matteo Scandolo2a659142018-11-15 11:23:45 -0800423 onu, _ := s.GetOnuByID(onuid)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900424
Matteo Scandolo2a659142018-11-15 11:23:45 -0800425 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900426 intfid := nnipkt.Info.intfid
427 pkt := nnipkt.Pkt
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800428 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900429 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
430 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800431 logger.Error("Fail to send PktInd indication.", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900432 return err
433 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900434
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900435 case <-ctx.Done():
436 logger.Debug("PacketInDaemon thread receives close ")
437 close(unichannel)
438 logger.Debug("Closed unichannel ")
439 close(nnichannel)
440 logger.Debug("Closed nnichannel ")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900441 return nil
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900442 }
443 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900444 return nil
445}
446
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900447func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
448 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800449 onu, _ := s.GetOnuByID(onuid)
450
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900451 if layerEth != nil {
452 pkt, _ := layerEth.(*layers.Ethernet)
453 ethtype := pkt.EthernetType
454 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800455 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900456 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800457 utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900458 rawpkt, _, _ = PopVLAN(rawpkt)
459 rawpkt, _, _ = PopVLAN(rawpkt)
460 } else {
461 return nil
462 }
463 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
464 if err != nil {
465 return err
466 }
467 handle := ioinfo.handler
468 SendUni(handle, rawpkt)
469 return nil
470 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900471 logger.Info("WARNING: Received packet is not supported")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900472 return nil
473}
474
475func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900476 poppkt, _, err := PopVLAN(rawpkt)
477 poppkt, _, err = PopVLAN(poppkt)
478 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900479 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900480 return err
481 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900482 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900483 if err != nil {
484 return err
485 }
486 handle := ioinfo.handler
487 SendNni(handle, poppkt)
488 return nil
489}
490
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900491func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
492 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900493 for _, onu := range onus {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900494 if onu.GetIntStatus() != device.ONU_ACTIVATED {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900495 return false
496 }
497 }
498 }
499 return true
500}
501
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900502func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
503 idx := uint32(0)
504 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
505 //return uint32(1032 + 8 * (vid - 1)), nil
506}
507
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900508func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
509 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900510 for _, onu := range onus {
511 if device.ValidateSN(*sn, *onu.SerialNumber) {
512 return onu, nil
513 }
514 }
515 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900516 err := errors.New("No mathced SN is found ")
517 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900518 return nil, err
519}
520
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800521func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
522 return getOnuByID(s.Onumap, onuid)
523}
524
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900525func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
526 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900527 for _, onu := range onus {
528 if onu.OnuID == onuid {
529 return onu, nil
530 }
531 }
532 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900533 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800534 logger.WithFields(log.Fields{
535 "onumap": onumap,
536 "onuid": onuid,
537 }).Error(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}