blob: 2fc494b56a31ac0808b3f2963fa6b14fa06f8d24 [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
Shad Ansarib744bf22019-01-17 11:36:46 -080025 omci "github.com/opencord/omci-sim"
26
27 "reflect"
28
Matteo Scandolo88e91892018-11-06 16:29:19 -080029 "gerrit.opencord.org/voltha-bbsim/common/logger"
30 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "gerrit.opencord.org/voltha-bbsim/device"
32 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 "github.com/google/gopacket"
34 "github.com/google/gopacket/layers"
35 "github.com/google/gopacket/pcap"
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080038 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041const (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090042 UNI_VETH_UP_PFX = "sim_uu"
43 UNI_VETH_DW_PFX = "sim_ud"
44 NNI_VETH_UP_PFX = "sim_nu"
45 NNI_VETH_DW_PFX = "sim_nd"
46 MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047)
48
49type Server struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090050 wg *sync.WaitGroup
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 Olt *device.Olt
52 Onumap map[uint32][]*device.Onu
53 Ioinfos []*Ioinfo
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 gRPCserver *grpc.Server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055 gRPCAddress string
56 gRPCPort uint32
57 Vethnames []string
58 IndInterval int
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090059 Processes []string
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090060 EnableServer *openolt.Openolt_EnableIndicationServer
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090061 CtagMap map[string]uint32
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090062 cancel context.CancelFunc
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090063 stateRepCh chan stateReport
Shad Ansaria5c79892018-11-29 16:32:59 -080064 omciIn chan openolt.OmciIndication
65 omciOut chan openolt.OmciMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090066}
67
68type Packet struct {
69 Info *Ioinfo
70 Pkt gopacket.Packet
71}
72
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090073type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080074 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090075 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080076 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090077}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090078
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090079func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080 // TODO: make it decent
81 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080082 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090083 nonus := opt.nonus
84 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080085 Olt: device.NewOlt(oltid, npon, 1),
86 Onumap: make(map[uint32][]*device.Onu),
87 Ioinfos: []*Ioinfo{},
88 gRPCAddress: opt.address,
89 gRPCPort: opt.port,
90 Vethnames: []string{},
91 IndInterval: opt.intvl,
92 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -080093 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -080094 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090095 omciIn: make(chan openolt.OmciIndication, 1024),
96 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 }
98
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090099 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800100 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900101 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900102 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900103 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900104
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900105 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900106 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700107 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900108 oltid := s.Olt.ID
109 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900110 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700111 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900112 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 return &s
114}
115
116//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800117func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900118 s.wg = &sync.WaitGroup{}
119 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800120 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800121 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900122 logger.Debug("Start() Done")
123 }()
124 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
125 listener, gserver, err := NewGrpcServer(addressport)
126 if err != nil {
127 logger.Error("Failed to create gRPC server", err)
128 return err
129 }
130 s.gRPCserver = gserver
131 openolt.RegisterOpenoltServer(gserver, s)
132 if err := gserver.Serve(listener); err != nil {
133 logger.Error("Failed to run gRPC server", err)
134 return err
135 }
136 s.wg.Wait()
137 return nil
138}
139
140//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800141func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900142 logger.Debug("Stop() Start")
143 defer logger.Debug("Stop() Done")
144 if s.gRPCserver != nil {
145 s.gRPCserver.Stop()
146 logger.Debug("gRPCserver.Stop()")
147 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900148 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900149 return
150}
151
152// Blocking
153func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900154 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800155 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900156 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 for intfid, _ := range s.Onumap {
158 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900159 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900160 }
161 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900162 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900163 logger.Debug("Enable() Done")
164 }()
165 logger.Debug("Enable() Start")
166 s.EnableServer = sv
167 if err := s.activateOLT(*sv); err != nil {
168 return err
169 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900171
172 coreCtx := context.Background()
173 coreCtx, corecancel := context.WithCancel(coreCtx)
174 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900175 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900176 return err
177 }
178 return nil
179}
180
181//Non-Blocking
182func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800183 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184 logger.Debug("Disable() Done")
185 }()
186 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900187 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188}
189
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900190func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
191 current := dev.GetIntState()
192 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800193 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
194 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900195 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800196 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900197 logger.Debug("ONU State updated to:%d", state)
198 } else {
199 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
200 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900201}
202
203func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900204 defer logger.Debug("activateOLT() Done")
205 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900206 // Activate OLT
207 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900208 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900209 return err
210 }
211 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800212 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900213
214 // OLT sends Interface Indication to Adapter
215 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800216 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900217 return err
218 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800219 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900220
221 // OLT sends Operation Indication to Adapter after activating each interface
222 //time.Sleep(IF_UP_TIME * time.Second)
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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900255func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
256 logger.Debug("StartPktLoops () 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()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900262 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900263 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900264 }()
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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900279 if err = s.runPktLoops(child, stream); err != nil {
280 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900281 return err
282 }
283 return nil
284}
285
286//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900287func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900288 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
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900328func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
329 logger.Debug("runPacketPktLoops Start")
330 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900331
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900332 errch := make(chan error)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900333 RunOmciResponder(ctx, s.omciOut, s.omciIn, s.Onumap, errch)
334 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900335 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900336
Shad Ansarib744bf22019-01-17 11:36:46 -0800337 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900338 logger.Debug("runOMCIResponder Start")
339 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800340 select {
341 case v, ok := <-errch: // Wait for OmciInitialization
342 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900343 logger.Error("Error happend in Omci:%s", v)
344 return v
Shad Ansarib744bf22019-01-17 11:36:46 -0800345 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900346 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900347 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800348 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900349 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900350 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900351 return nil
352 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900353
Shad Ansarib744bf22019-01-17 11:36:46 -0800354 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900355 err := s.runMainPktLoop(child, stream)
356 return err
357 })
358
359 if err := eg.Wait(); err != nil {
360 logger.Error("Error happend in runPacketLoops:%s", err)
361 cancel()
362 }
363 return nil
364}
365
366func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
367 unichannel := make(chan Packet, 2048)
368 defer func() {
369 close(unichannel)
370 logger.Debug("Closed unichannel ")
371 }()
372 for intfid, _ := range s.Onumap {
373 for _, onu := range s.Onumap[intfid] {
374 onuid := onu.OnuID
375 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
376 if err != nil {
377 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
378 return err
Shad Ansarief436272018-11-14 15:58:20 -0800379 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900380 uhandler := ioinfo.handler
381 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900382 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900383 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900384
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900385 ioinfo, err := s.IdentifyNniIoinfo("inside")
386 if err != nil {
387 return err
388 }
389 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
390 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800391 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900392 close(nnichannel)
393 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900394
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900395 data := &openolt.Indication_PktInd{}
396 for {
397 select {
398 case msg := <-s.omciIn:
399 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
400 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
401 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
402 logger.Error("send omci indication failed.", err)
403 continue
404 }
405 case unipkt := <-unichannel:
406 onuid := unipkt.Info.onuid
407 onu, _ := s.GetOnuByID(onuid)
408 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
409 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
410 logger.Debug("WARNING: This packet does not come from UNI ")
411 continue
412 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900413
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900414 intfid := unipkt.Info.intfid
415 gemid, _ := getGemPortID(intfid, onuid)
416 pkt := unipkt.Pkt
417 layerEth := pkt.Layer(layers.LayerTypeEthernet)
418 le, _ := layerEth.(*layers.Ethernet)
419 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900420
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900421 if ethtype == 0x888e {
422 utils.LoggerWithOnu(onu).WithFields(log.Fields{
423 "gemId": gemid,
424 }).Info("Received upstream packet is EAPOL.")
425 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
426 utils.LoggerWithOnu(onu).WithFields(log.Fields{
427 "gemId": gemid,
428 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900429
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900430 //C-TAG
431 sn := convB2S(onu.SerialNumber.VendorSpecific)
432 if ctag, ok := s.CtagMap[sn]; ok == true {
433 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
434 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900435 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900436 "gemId": gemid,
437 }).Error("Fail to tag C-tag")
438 } else {
439 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900440 }
441 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800442 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900443 "gemId": gemid,
444 "cTagMap": s.CtagMap,
445 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900446 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900447 } else {
448 utils.LoggerWithOnu(onu).WithFields(log.Fields{
449 "gemId": gemid,
450 }).Info("Received upstream packet is of unknow type, skipping.")
451 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900452 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900453
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900454 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
455 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
456 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
457 logger.Error("Fail to send PktInd indication.", err)
458 return err
459 }
460
461 case nnipkt := <-nnichannel:
462 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
463 logger.Debug("WARNING: This packet does not come from NNI ")
464 continue
465 }
466 onuid := nnipkt.Info.onuid
467 onu, _ := s.GetOnuByID(onuid)
468
469 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
470 intfid := nnipkt.Info.intfid
471 pkt := nnipkt.Pkt
472 utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet")
473 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
474 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
475 logger.Error("Fail to send PktInd indication.", err)
476 return err
477 }
478
479 case <-ctx.Done():
480 logger.Debug("Closed nnichannel ")
481 return nil
482 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900483 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900484 return nil
485}
486
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900487func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
488 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800489 onu, _ := s.GetOnuByID(onuid)
490
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900491 if layerEth != nil {
492 pkt, _ := layerEth.(*layers.Ethernet)
493 ethtype := pkt.EthernetType
494 if ethtype == 0x888e {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800495 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900496 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800497 utils.LoggerWithOnu(onu).WithFields(log.Fields{
498 "payload": layerDHCP.LayerPayload(),
499 "type": layerDHCP.LayerType().String(),
500 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900501 rawpkt, _, _ = PopVLAN(rawpkt)
502 rawpkt, _, _ = PopVLAN(rawpkt)
503 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800504 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900505 return nil
506 }
507 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
508 if err != nil {
509 return err
510 }
511 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800512 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900513 return nil
514 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800515 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900516 return nil
517}
518
519func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900520 poppkt, _, err := PopVLAN(rawpkt)
521 poppkt, _, err = PopVLAN(poppkt)
522 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900523 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900524 return err
525 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900526 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900527 if err != nil {
528 return err
529 }
530 handle := ioinfo.handler
531 SendNni(handle, poppkt)
532 return nil
533}
534
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900535func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
536 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900537 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900538 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900539 return false
540 }
541 }
542 }
543 return true
544}
545
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900546func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Shad Ansarib744bf22019-01-17 11:36:46 -0800547 // FIXME - check for errors
548 return uint32(omci.GetGemPortId(intfid, onuid)), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900549}
550
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900551func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
552 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900553 for _, onu := range onus {
554 if device.ValidateSN(*sn, *onu.SerialNumber) {
555 return onu, nil
556 }
557 }
558 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900559 err := errors.New("No mathced SN is found ")
560 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900561 return nil, err
562}
563
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800564func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
565 return getOnuByID(s.Onumap, onuid)
566}
567
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900568func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
569 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900570 for _, onu := range onus {
571 if onu.OnuID == onuid {
572 return onu, nil
573 }
574 }
575 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900576 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800577 logger.WithFields(log.Fields{
578 "onumap": onumap,
579 "onuid": onuid,
580 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900581 return nil, err
582}
583
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900584func convB2S(b []byte) string {
585 s := ""
586 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700587 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900588 }
589 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700590}