blob: 7cc1eed2c7c40882f94aca6b4de25325d179c325 [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 NISHIMOTOdad44cb2019-02-08 09:45:40 +090066 eapolIn chan *byteMsg
67 eapolOut chan *byteMsg
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +090068 dhcpIn chan *byteMsg
69 dhcpOut chan *byteMsg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090070}
71
72type Packet struct {
73 Info *Ioinfo
74 Pkt gopacket.Packet
75}
76
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090077type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090078 IntfId uint32
79 OnuId uint32
80 Byte []byte
81}
82
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090083type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080084 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090085 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -080086 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090087}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090089func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090090 // TODO: make it decent
91 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -080092 npon := opt.npon
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090093 nonus := opt.nonus
94 s := Server{
Matteo Scandolo88e91892018-11-06 16:29:19 -080095 Olt: device.NewOlt(oltid, npon, 1),
96 Onumap: make(map[uint32][]*device.Onu),
97 Ioinfos: []*Ioinfo{},
98 gRPCAddress: opt.address,
99 gRPCPort: opt.port,
100 Vethnames: []string{},
101 IndInterval: opt.intvl,
102 Processes: []string{},
Shad Ansari704c6f22018-11-13 14:57:53 -0800103 EnableServer: nil,
Shad Ansarib744bf22019-01-17 11:36:46 -0800104 stateRepCh: make(chan stateReport, 8),
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900105 omciIn: make(chan openolt.OmciIndication, 1024),
106 omciOut: make(chan openolt.OmciMsg, 1024),
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900107 eapolIn: make(chan *byteMsg, 1024),
108 eapolOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900109 dhcpIn: make(chan *byteMsg, 1024),
110 dhcpOut: make(chan *byteMsg, 1024),
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900111 }
112
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800114 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 for intfid := nnni; intfid < npon+nnni; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900116 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900117 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900118
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 //TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900120 s.CtagMap = make(map[string]uint32)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700121 for i := 0; i < MAX_ONUS_PER_PON; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900122 oltid := s.Olt.ID
123 intfid := uint32(1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900124 sn := convB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700125 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900126 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900127 return &s
128}
129
130//Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800131func (s *Server) Start() error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900132 s.wg = &sync.WaitGroup{}
133 logger.Debug("Start() Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800134 defer func() {
Shad Ansarib744bf22019-01-17 11:36:46 -0800135 close(s.stateRepCh)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900136 logger.Debug("Start() Done")
137 }()
138 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
139 listener, gserver, err := NewGrpcServer(addressport)
140 if err != nil {
141 logger.Error("Failed to create gRPC server", err)
142 return err
143 }
144 s.gRPCserver = gserver
145 openolt.RegisterOpenoltServer(gserver, s)
146 if err := gserver.Serve(listener); err != nil {
147 logger.Error("Failed to run gRPC server", err)
148 return err
149 }
150 s.wg.Wait()
151 return nil
152}
153
154//Non-Blocking
Matteo Scandolo88e91892018-11-06 16:29:19 -0800155func (s *Server) Stop() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900156 logger.Debug("Stop() Start")
157 defer logger.Debug("Stop() Done")
158 if s.gRPCserver != nil {
159 s.gRPCserver.Stop()
160 logger.Debug("gRPCserver.Stop()")
161 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900162 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900163 return
164}
165
166// Blocking
167func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900168 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800169 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 olt.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900171 for intfid, _ := range s.Onumap {
172 for _, onu := range s.Onumap[intfid] {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900173 onu.Initialize()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900174 }
175 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900176 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 logger.Debug("Enable() Done")
178 }()
179 logger.Debug("Enable() Start")
180 s.EnableServer = sv
181 if err := s.activateOLT(*sv); err != nil {
182 return err
183 }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900184 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185
186 coreCtx := context.Background()
187 coreCtx, corecancel := context.WithCancel(coreCtx)
188 s.cancel = corecancel
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900189 if err := s.StartPktLoops(coreCtx, *sv); err != nil {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900190 return err
191 }
192 return nil
193}
194
195//Non-Blocking
196func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800197 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900198 logger.Debug("Disable() Done")
199 }()
200 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900201 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900202}
203
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900204func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
205 current := dev.GetIntState()
206 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800207 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
208 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900209 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800210 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900211 logger.Debug("ONU State updated to:%d", state)
212 } else {
213 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
214 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900215}
216
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900217func (s *Server) updateOnuIntState (intfid uint32, onuid uint32, state device.DeviceState) error {
218 onu, err := s.GetOnuByID(onuid) //TODO: IntfID should be included ?
219 if err != nil {
220 return err
221 }
222 s.updateDevIntState(onu, state)
223 return nil
224}
225
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900226func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900227 defer logger.Debug("activateOLT() Done")
228 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900229 // Activate OLT
230 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900231 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900232 return err
233 }
234 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800235 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900236
237 // OLT sends Interface Indication to Adapter
238 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800239 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900240 return err
241 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800242 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900243
244 // OLT sends Operation Indication to Adapter after activating each interface
245 //time.Sleep(IF_UP_TIME * time.Second)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900246 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800247 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900248 return err
249 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800250 logger.Info("OLT %s sent OperInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900251
252 // OLT sends ONU Discover Indication to Adapter after ONU discovery
253 for intfid, _ := range s.Onumap {
254 device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up")
255 }
256
257 for intfid, _ := range s.Onumap {
258 sendOnuDiscInd(stream, s.Onumap[intfid])
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800259 logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900260 }
261
262 // OLT Sends OnuInd after waiting all of those ONUs up
263 for {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900264 if IsAllOnuActive(s.Onumap) {
265 logger.Debug("All the Onus are Activated.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900266 break
267 }
268 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900269
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900270 for intfid, _ := range s.Onumap {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900271 sendOnuInd(stream, s.Onumap[intfid], s.IndInterval)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800272 logger.Info("OLT id:%d sent ONUInd.", olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900273 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900274 return nil
275}
276
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900277// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900278func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
279 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800280 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900281 RemoveVeths(s.Vethnames)
282 s.Vethnames = []string{}
283 s.Ioinfos = []*Ioinfo{}
284 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900285 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900286 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900287 }()
288 s.wg.Add(1)
289 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap)
290 if err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800291 logger.Error("createIoinfos failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900292 return err
293 }
294 s.Ioinfos = ioinfos
295 s.Vethnames = veths
296 logger.Debug("Created vethnames:%v", s.Vethnames)
297
298 parent := ctx
299 child, cancel := context.WithCancel(parent)
300 s.cancel = cancel
301
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900302 if err = s.runPktLoops(child, stream); err != nil {
303 logger.Error("runPktLoops failed.", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304 return err
305 }
306 return nil
307}
308
309//Non-Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900310func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900311 if s.cancel != nil {
312 cancel := s.cancel
313 cancel()
314 }
315}
316
317func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900318 ioinfos := []*Ioinfo{}
319 var err error
320 for intfid, _ := range onumap {
321 for i := 0; i < len(onumap[intfid]); i++ {
322 var handler *pcap.Handle
323 onuid := onumap[intfid][i].OnuID
324 uniup, unidw := makeUniName(oltid, intfid, onuid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900325 if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800326 logger.Error("setupVethHandler failed (onuid: %d)", onuid, err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900327 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900328 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900329 iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900330 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900331 oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900332 ioinfos = append(ioinfos, &oinfo)
333 }
334 }
335
336 var handler *pcap.Handle
337 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900338 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800339 logger.Error("setupVethHandler failed for nni", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900340 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900341 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900342
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900343 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900344 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900345 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900346 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900347 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900348}
349
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900350//Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900351func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
352 logger.Debug("runPacketPktLoops Start")
353 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900354
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900355 errchOmci := make(chan error)
356 RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900357 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900358 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900359
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900360 errchEapol := make(chan error)
361 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
362
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900363 errchDhcp := make(chan error)
364 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
365
Shad Ansarib744bf22019-01-17 11:36:46 -0800366 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900367 logger.Debug("runOMCIResponder Start")
368 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800369 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900370 case v, ok := <-errchOmci: // Wait for OmciInitialization
Shad Ansarib744bf22019-01-17 11:36:46 -0800371 if ok { //Error
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900372 logger.Error("Error happend in Omci:%s", v)
373 return v
Shad Ansarib744bf22019-01-17 11:36:46 -0800374 } else { //Close
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900375 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900376 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800377 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900378 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900379 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900380 return nil
381 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900382
Shad Ansarib744bf22019-01-17 11:36:46 -0800383 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900384 logger.Debug("runEapolResponder Start")
385 defer logger.Debug("runEapolResponder Done")
386 select {
387 case v, ok := <-errchEapol:
388 if ok { //Error
389 logger.Error("Error happend in Eapol:%s", v)
390 return v
391 }
392 case <-child.Done():
393 return nil
394 }
395 return nil
396 })
397
398 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900399 logger.Debug("runDhcpResponder Start")
400 defer logger.Debug("runDhcpResponder Done")
401 select {
402 case v, ok := <-errchDhcp:
403 if ok { //Error
404 logger.Error("Error happend in Dhcp:%s", v)
405 return v
406 }
407 case <-child.Done():
408 return nil
409 }
410 return nil
411 })
412
413 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900414 err := s.runMainPktLoop(child, stream)
415 return err
416 })
417
418 if err := eg.Wait(); err != nil {
419 logger.Error("Error happend in runPacketLoops:%s", err)
420 cancel()
421 }
422 return nil
423}
424
425func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
426 unichannel := make(chan Packet, 2048)
427 defer func() {
428 close(unichannel)
429 logger.Debug("Closed unichannel ")
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900430 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900431 }()
432 for intfid, _ := range s.Onumap {
433 for _, onu := range s.Onumap[intfid] {
434 onuid := onu.OnuID
435 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
436 if err != nil {
437 utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err)
438 return err
Shad Ansarief436272018-11-14 15:58:20 -0800439 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900440 uhandler := ioinfo.handler
441 go RecvWorker(ioinfo, uhandler, unichannel)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900442 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900443 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900444
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900445 ioinfo, err := s.IdentifyNniIoinfo("inside")
446 if err != nil {
447 return err
448 }
449 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
450 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800451 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900452 close(nnichannel)
453 }()
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900454
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900455 data := &openolt.Indication_PktInd{}
456 for {
457 select {
458 case msg := <-s.omciIn:
459 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
460 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
461 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
462 logger.Error("send omci indication failed.", err)
463 continue
464 }
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900465 case msg := <- s.eapolIn:
466 intfid := msg.IntfId
467 onuid := msg.OnuId
468 gemid, err := getGemPortID(intfid, onuid)
469 if err != nil {
470 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
471 continue
472 }
473
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900474 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900475
476 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
477 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
478 logger.Error("Fail to send EAPOL PktInd indication.", err)
479 return err
480 }
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900481 case msg := <- s.dhcpIn: //TODO: We should put omciIn, eapolIn, dhcpIn toghether
482 intfid := msg.IntfId
483 onuid := msg.OnuId
484 gemid, err := getGemPortID(intfid, onuid)
485 bytes := msg.Byte
486 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
487
488 if err != nil {
489 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
490 continue
491 }
492 /*
493 onu, err := s.GetOnuByID(onuid)
494 if err != nil {
495 logger.Error("Failed to GetOnuByID:%d", onuid)
496 continue
497 }
498 sn := convB2S(onu.SerialNumber.VendorSpecific)
499 if ctag, ok := s.CtagMap[sn]; ok == true {
500 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
501 if err != nil {
502 utils.LoggerWithOnu(onu).WithFields(log.Fields{
503 "gemId": gemid,
504 }).Error("Fail to tag C-tag")
505 } else {
506 pkt = tagpkt
507 }
508 } else {
509 utils.LoggerWithOnu(onu).WithFields(log.Fields{
510 "gemId": gemid,
511 "cTagMap": s.CtagMap,
512 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
513 }
514 */
515 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, intfid, onuid)
516 logger.Debug(pkt.Dump())
517
518 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
519 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
520 logger.Error("Fail to send DHCP PktInd indication.", err)
521 return err
522 }
523
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900524 case unipkt := <-unichannel:
525 onuid := unipkt.Info.onuid
526 onu, _ := s.GetOnuByID(onuid)
527 utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server")
528 if unipkt.Info == nil || unipkt.Info.iotype != "uni" {
529 logger.Debug("WARNING: This packet does not come from UNI ")
530 continue
531 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900532
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900533 intfid := unipkt.Info.intfid
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900534 gemid, err := getGemPortID(intfid, onuid)
535 if err != nil {
536 continue
537 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900538 pkt := unipkt.Pkt
539 layerEth := pkt.Layer(layers.LayerTypeEthernet)
540 le, _ := layerEth.(*layers.Ethernet)
541 ethtype := le.EthernetType
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900542
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900543 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900544 utils.LoggerWithOnu(onu).WithFields(log.Fields{
545 "gemId": gemid,
546 }).Info("Received upstream packet is EAPOL.")
547 } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
548 utils.LoggerWithOnu(onu).WithFields(log.Fields{
549 "gemId": gemid,
550 }).Info("Received upstream packet is DHCP.")
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900551
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900552 //C-TAG
553 sn := convB2S(onu.SerialNumber.VendorSpecific)
554 if ctag, ok := s.CtagMap[sn]; ok == true {
555 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
556 if err != nil {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900557 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900558 "gemId": gemid,
559 }).Error("Fail to tag C-tag")
560 } else {
561 pkt = tagpkt
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900562 }
563 } else {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800564 utils.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900565 "gemId": gemid,
566 "cTagMap": s.CtagMap,
567 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900568 }
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900569 } else {
570 utils.LoggerWithOnu(onu).WithFields(log.Fields{
571 "gemId": gemid,
572 }).Info("Received upstream packet is of unknow type, skipping.")
573 continue
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900574 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900575
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900576 utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet")
577 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}}
578 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
579 logger.Error("Fail to send PktInd indication.", err)
580 return err
581 }
582
583 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900584 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900585 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
586 logger.Debug("WARNING: This packet does not come from NNI ")
587 continue
588 }
589 onuid := nnipkt.Info.onuid
590 onu, _ := s.GetOnuByID(onuid)
591
592 utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
593 intfid := nnipkt.Info.intfid
594 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900595 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
596 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
597 logger.Error("Fail to send PktInd indication.", err)
598 return err
599 }
600
601 case <-ctx.Done():
602 logger.Debug("Closed nnichannel ")
603 return nil
604 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900605 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900606 return nil
607}
608
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900609func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
610 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800611 onu, _ := s.GetOnuByID(onuid)
612
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900613 if layerEth != nil {
614 pkt, _ := layerEth.(*layers.Ethernet)
615 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900616 if ethtype == layers.EthernetTypeEAPOL {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800617 utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900618 eapolPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900619 s.eapolOut <- &eapolPkt
620 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900621 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800622 utils.LoggerWithOnu(onu).WithFields(log.Fields{
623 "payload": layerDHCP.LayerPayload(),
624 "type": layerDHCP.LayerType().String(),
625 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900626 rawpkt, _, _ = PopVLAN(rawpkt)
627 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900628 logger.Debug("%s", rawpkt.Dump())
629 dhcpPkt := byteMsg{IntfId:intfid, OnuId:onuid, Byte: rawpkt.Data()}
630 s.dhcpOut <- &dhcpPkt
631 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900632 } else {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800633 utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900634 return nil
635 }
636 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
637 if err != nil {
638 return err
639 }
640 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800641 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900642 return nil
643 }
Matteo Scandoloa286c742018-11-20 08:10:04 -0800644 utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900645 return nil
646}
647
648func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900649 poppkt, _, err := PopVLAN(rawpkt)
650 poppkt, _, err = PopVLAN(poppkt)
651 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900652 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900653 return err
654 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900655 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900656 if err != nil {
657 return err
658 }
659 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900660 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900661 SendNni(handle, poppkt)
662 return nil
663}
664
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900665func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
666 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900667 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900668 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900669 return false
670 }
671 }
672 }
673 return true
674}
675
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900676func (s *Server) isAllOnuOmciActive() bool {
677 for _, onus := range s.Onumap {
678 for _, onu := range onus{
679 if onu.GetIntState() != device.ONU_OMCIACTIVE {
680 return false
681 }
682 }
683 }
684 return true
685}
686
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900687func getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900688 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
689 gemportid, err := omci.GetGemPortId(intfid, onuid)
690 if err != nil {
691 logger.Error("%s", err)
692 return 0, err
693 }
694 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900695}
696
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900697func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
698 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900699 for _, onu := range onus {
700 if device.ValidateSN(*sn, *onu.SerialNumber) {
701 return onu, nil
702 }
703 }
704 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900705 err := errors.New("No mathced SN is found ")
706 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900707 return nil, err
708}
709
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800710func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) {
711 return getOnuByID(s.Onumap, onuid)
712}
713
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900714func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) {
715 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900716 for _, onu := range onus {
717 if onu.OnuID == onuid {
718 return onu, nil
719 }
720 }
721 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900722 err := errors.New("No matched OnuID is found ")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800723 logger.WithFields(log.Fields{
724 "onumap": onumap,
725 "onuid": onuid,
726 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900727 return nil, err
728}
729
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900730func convB2S(b []byte) string {
731 s := ""
732 for _, i := range b {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700733 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900734 }
735 return s
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700736}