blob: 667a900adfde3a128fb4628e5c0e39f0494193d4 [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 NISHIMOTO2b694202018-12-18 07:30:55 +090034 "golang.org/x/sync/errgroup"
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090035 "reflect"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090036)
37
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090038const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090039 UNI_VETH_UP_PFX = "sim_uu"
40 UNI_VETH_DW_PFX = "sim_ud"
41 NNI_VETH_UP_PFX = "sim_nu"
42 NNI_VETH_DW_PFX = "sim_nd"
43 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090044)
45
46type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090047 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090048 Olt *device.Olt
49 Onumap map[uint32][]*device.Onu
50 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090052 gRPCAddress string
53 gRPCPort uint32
54 Vethnames []string
55 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090056 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090057 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090058 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090059 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090060 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080061 omciIn chan openolt.OmciIndication
62 omciOut chan openolt.OmciMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090063}
64
65type Packet struct {
66 Info *Ioinfo
67 Pkt gopacket.Packet
68}
69
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090070type stateReport struct {
71 device device.Device
72 current device.DeviceState
73 next device.DeviceState
74}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090075
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090076func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090077 // TODO: make it decent
78 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080079 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080 nonus := opt.nonus
81 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080082 Olt: device.NewOlt(oltid, npon, 1),
83 Onumap: make(map[uint32][]*device.Onu),
84 Ioinfos: []*Ioinfo{},
85 gRPCAddress: opt.address,
86 gRPCPort: opt.port,
87 Vethnames: []string{},
88 IndInterval: opt.intvl,
89 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080090 EnableServer: nil,
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090091 stateRepCh : make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090092 omciIn: make(chan openolt.OmciIndication, 1024),
93 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094 }
95
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090096 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080097 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090098 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090099 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900100 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900101
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900102 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900103 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700104 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900105 oltid := s.Olt.ID
106 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900107 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700108 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900109 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 return &s
111}
112
113//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800114func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900115 s.wg = &sync.WaitGroup{}
116 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800117 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900118 close(s.stateRepCh )
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 logger.Debug("Start() Done")
120 }()
121 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
122 listener, gserver, err := NewGrpcServer(addressport)
123 if err != nil {
124 logger.Error("Failed to create gRPC server", err)
125 return err
126 }
127 s.gRPCserver = gserver
128 openolt.RegisterOpenoltServer(gserver, s)
129 if err := gserver.Serve(listener); err != nil {
130 logger.Error("Failed to run gRPC server", err)
131 return err
132 }
133 s.wg.Wait()
134 return nil
135}
136
137//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800138func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900139 logger.Debug("Stop() Start")
140 defer logger.Debug("Stop() Done")
141 if s.gRPCserver != nil {
142 s.gRPCserver.Stop()
143 logger.Debug("gRPCserver.Stop()")
144 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900145 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900146 return
147}
148
149// Blocking
150func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900151 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800152 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900153 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900154 for intfid, _ := range s.Onumap {
155 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900156 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 }
158 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900159 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 logger.Debug("Enable() Done")
161 }()
162 logger.Debug("Enable() Start")
163 s.EnableServer = sv
164 if err := s.activateOLT(*sv); err != nil {
165 return err
166 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900167 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900168
169 coreCtx := context.Background()
170 coreCtx, corecancel := context.WithCancel(coreCtx)
171 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900172 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900173 return err
174 }
175 return nil
176}
177
178//Non-Blocking
179func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800180 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900181 logger.Debug("Disable() Done")
182 }()
183 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900184 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185}
186
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900187func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
188 current := dev.GetIntState()
189 dev.UpdateIntState(state)
190 s.stateRepCh <- stateReport{device: dev, current:current, next: state}
191 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){
192 logger.Debug("OLT State updated to:%d", state)
193 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}){
194 logger.Debug("ONU State updated to:%d", state)
195 } else {
196 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
197 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900198}
199
200func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900201 defer logger.Debug("activateOLT() Done")
202 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900203 // Activate OLT
204 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900205 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900206 return err
207 }
208 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800209 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900210
211 // OLT sends Interface Indication to Adapter
212 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800213 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214 return err
215 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800216 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217
218 // OLT sends Operation Indication to Adapter after activating each interface
219 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900220 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800221 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900222 return err
223 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800224 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900225
226 // OLT sends ONU Discover Indication to Adapter after ONU discovery
227 for intfid, _ := range s.Onumap {
228 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
229 }
230
231 for intfid, _ := range s.Onumap {
232 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800233 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900234 }
235
236 // OLT Sends OnuInd after waiting all of those ONUs up
237 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900238 if IsAllOnuActive(s.Onumap) {
239 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900240 break
241 }
242 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900243
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900244 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900245 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800246 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900247 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900248 return nil
249}
250
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900251// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900252func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
253 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800254 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900255 RemoveVeths(s.Vethnames)
256 s.Vethnames = []string{}
257 s.Ioinfos = []*Ioinfo{}
258 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900259 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900260 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900261 }()
262 s.wg.Add(1)
263 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
264 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800265 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900266 return err
267 }
268 s.Ioinfos = ioinfos
269 s.Vethnames = veths
270 logger.Debug("Created vethnames:%v", s.Vethnames)
271
272 parent := ctx
273 child, cancel := context.WithCancel(parent)
274 s.cancel = cancel
275
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900276 if err = s.runPktLoops(child, stream); err != nil {
277 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900278 return err
279 }
280 return nil
281}
282
283//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900284func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900285 if s.cancel != nil {
286 cancel := s.cancel
287 cancel()
288 }
289}
290
291func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900292 ioinfos := []*Ioinfo{}
293 var err error
294 for intfid, _ := range onumap {
295 for i := 0; i < len(onumap[intfid]); i++ {
296 var handler *pcap.Handle
297 onuid := onumap[intfid][i].OnuID
298 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900299 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800300 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900301 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 {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800313 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900314 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900315 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900316
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900317 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900318 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900319 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900320 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900321 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900322}
323
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900324//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900325func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
326 logger.Debug("runPacketPktLoops Start")
327 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900328
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900329 errch := make(chan error)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900330 RunOmciResponder(ctx, s.omciOut, s.omciIn, s.Onumap, errch)
331 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900332 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900333
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900334 eg.Go (func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900335 logger.Debug("runOMCIResponder Start")
336 defer logger.Debug("runOMCIResponder Done")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900337 select{
338 case v, ok := <- errch: // Wait for OmciInitialization
339 if ok { //Error
340 logger.Error("Error happend in Omci:%s", v)
341 return v
342 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900343 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900344 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900345 case <- child.Done():
346 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900347 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900348 return nil
349 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900350
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900351 eg.Go (func () error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900352 err := s.runMainPktLoop(child, stream)
353 return err
354 })
355
356 if err := eg.Wait(); err != nil {
357 logger.Error("Error happend in runPacketLoops:%s", err)
358 cancel()
359 }
360 return nil
361}
362
363func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
364 unichannel := make(chan Packet, 2048)
365 defer func() {
366 close(unichannel)
367 logger.Debug("Closed unichannel ")
368 }()
369 for intfid, _ := range s.Onumap {
370 for _, onu := range s.Onumap[intfid] {
371 onuid := onu.OnuID
372 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
373 if err != nil {
374 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
375 return err
Shad Ansarief436272018-11-14 15:58:20 -0800376 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900377 uhandler := ioinfo.handler
378 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900379 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900380 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900381
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900382 ioinfo, err := s.IdentifyNniIoinfo("inside")
383 if err != nil {
384 return err
385 }
386 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
387 go RecvWorker(ioinfo, nhandler, nnichannel)
388 defer func(){
389 close(nnichannel)
390 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900391
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900392 data := &openolt.Indication_PktInd{}
393 for {
394 select {
395 case msg := <-s.omciIn:
396 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
397 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
398 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
399 logger.Error("send omci indication failed.", err)
400 continue
401 }
402 case unipkt := <-unichannel:
403 onuid := unipkt.Info.onuid
404 onu, _ := s.GetOnuByID(onuid)
405 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
406 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
407 logger.Debug("WARNING: This packet does not come from UNI ")
408 continue
409 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900410
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900411 intfid := unipkt.Info.intfid
412 gemid, _ := getGemPortID(intfid, onuid)
413 pkt := unipkt.Pkt
414 layerEth := pkt.Layer(layers.LayerTypeEthernet)
415 le, _ := layerEth.(*layers.Ethernet)
416 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900417
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418 if ethtype == 0x888e {
419 utils.LoggerWithOnu(onu).WithFields(log.Fields{
420 "gemId": gemid,
421 }).Info("Received upstream packet is EAPOL.")
422 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
423 utils.LoggerWithOnu(onu).WithFields(log.Fields{
424 "gemId": gemid,
425 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900426
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900427 //C-TAG
428 sn := convB2S(onu.SerialNumber.VendorSpecific)
429 if ctag, ok := s.CtagMap[sn]; ok == true {
430 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
431 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900432 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900433 "gemId": gemid,
434 }).Error("Fail to tag C-tag")
435 } else {
436 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900437 }
438 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800439 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900440 "gemId": gemid,
441 "cTagMap": s.CtagMap,
442 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900443 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900444 } else {
445 utils.LoggerWithOnu(onu).WithFields(log.Fields{
446 "gemId": gemid,
447 }).Info("Received upstream packet is of unknow type, skipping.")
448 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900449 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900450
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900451 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
452 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
453 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
454 logger.Error("Fail to send PktInd indication.", err)
455 return err
456 }
457
458 case nnipkt := <-nnichannel:
459 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
460 logger.Debug("WARNING: This packet does not come from NNI ")
461 continue
462 }
463 onuid := nnipkt.Info.onuid
464 onu, _ := s.GetOnuByID(onuid)
465
466 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
467 intfid := nnipkt.Info.intfid
468 pkt := nnipkt.Pkt
469 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
470 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
471 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
472 logger.Error("Fail to send PktInd indication.", err)
473 return err
474 }
475
476 case <-ctx.Done():
477 logger.Debug("Closed nnichannel ")
478 return nil
479 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900480 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900481 return nil
482}
483
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900484func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
485 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800486 onu, _ := s.GetOnuByID(onuid)
487
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900488 if layerEth != nil {
489 pkt, _ := layerEth.(*layers.Ethernet)
490 ethtype := pkt.EthernetType
491 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800492 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900493 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800494 utils.LoggerWithOnu(onu).WithFields(log.Fields{
495 "payload": layerDHCP.LayerPayload(),
496 "type": layerDHCP.LayerType().String(),
497 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900498 rawpkt, _, _ = PopVLAN(rawpkt)
499 rawpkt, _, _ = PopVLAN(rawpkt)
500 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800501 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900502 return nil
503 }
504 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
505 if err != nil {
506 return err
507 }
508 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800509 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900510 return nil
511 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800512 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900513 return nil
514}
515
516func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900517 poppkt, _, err := PopVLAN(rawpkt)
518 poppkt, _, err = PopVLAN(poppkt)
519 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900520 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900521 return err
522 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900523 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900524 if err != nil {
525 return err
526 }
527 handle := ioinfo.handler
528 SendNni(handle, poppkt)
529 return nil
530}
531
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900532func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
533 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900534 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900535 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900536 return false
537 }
538 }
539 }
540 return true
541}
542
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900543func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansaria5c79892018-11-29 16:32:59 -0800544 key := OnuKey{intfid, onuid}
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900545 if onuState, ok := OnuOmciStateMap[key]; !ok {
Shad Ansaria5c79892018-11-29 16:32:59 -0800546 idx := uint32(0)
547 // Backwards compatible with bbsim_olt adapter
548 return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil
549 } else {
550 // FIXME - Gem Port ID is 2 bytes - fix openolt.proto
551 return uint32(onuState.gemPortId), nil
552 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900553}
554
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900555func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
556 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900557 for _, onu := range onus {
558 if device.ValidateSN(*sn, *onu.SerialNumber) {
559 return onu, nil
560 }
561 }
562 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900563 err := errors.New("No mathced SN is found ")
564 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900565 return nil, err
566}
567
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800568func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
569 return getOnuByID(s.Onumap, onuid)
570}
571
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900572func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
573 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900574 for _, onu := range onus {
575 if onu.OnuID == onuid {
576 return onu, nil
577 }
578 }
579 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900580 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800581 logger.WithFields(log.Fields{
582 "onumap": onumap,
583 "onuid": onuid,
584 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900585 return nil, err
586}
587
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900588func convB2S(b []byte) string {
589 s := ""
590 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700591 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900592 }
593 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700594}