Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | "strconv" |
| 23 | "sync" |
| 24 | |
| 25 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 26 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 27 | "gerrit.opencord.org/voltha-bbsim/device" |
| 28 | "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "github.com/google/gopacket" |
| 30 | "github.com/google/gopacket/layers" |
| 31 | "github.com/google/gopacket/pcap" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 32 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 33 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | ) |
| 35 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 36 | const ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 37 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 42 | ) |
| 43 | |
| 44 | type Server struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 45 | wg *sync.WaitGroup |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 46 | Olt *device.Olt |
| 47 | Onumap map[uint32][]*device.Onu |
| 48 | Ioinfos []*Ioinfo |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 49 | gRPCserver *grpc.Server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 50 | gRPCAddress string |
| 51 | gRPCPort uint32 |
| 52 | Vethnames []string |
| 53 | IndInterval int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 54 | Processes []string |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 55 | EnableServer *openolt.Openolt_EnableIndicationServer |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 56 | CtagMap map[string]uint32 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 57 | cancel context.CancelFunc |
| 58 | state coreState |
| 59 | stateChan chan coreState |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type Packet struct { |
| 63 | Info *Ioinfo |
| 64 | Pkt gopacket.Packet |
| 65 | } |
| 66 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 67 | type coreState int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 68 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 69 | const ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 70 | INACTIVE = iota // OLT/ONUs are not instantiated |
| 71 | PRE_ACTIVE // Before PacketInDaemon Running |
| 72 | ACTIVE // After PacketInDaemon Running |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 73 | ) |
| 74 | |
| 75 | /* coreState |
| 76 | INACTIVE -> PRE_ACTIVE -> ACTIVE |
| 77 | (ActivateOLT) (Enable) |
| 78 | <- <- |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 79 | */ |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 80 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 81 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 82 | // TODO: make it decent |
| 83 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 84 | npon := opt.npon |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 85 | nonus := opt.nonus |
| 86 | s := Server{ |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 87 | Olt: device.NewOlt(oltid, npon, 1), |
| 88 | Onumap: make(map[uint32][]*device.Onu), |
| 89 | Ioinfos: []*Ioinfo{}, |
| 90 | gRPCAddress: opt.address, |
| 91 | gRPCPort: opt.port, |
| 92 | Vethnames: []string{}, |
| 93 | IndInterval: opt.intvl, |
| 94 | Processes: []string{}, |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 95 | EnableServer: new(openolt.Openolt_EnableIndicationServer), |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 96 | state: INACTIVE, |
| 97 | stateChan: make(chan coreState, 8), |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 100 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 101 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 102 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 103 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 104 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 105 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 106 | //TODO: To be fixed because it is hardcoded |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 107 | s.CtagMap = make(map[string]uint32) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 108 | for i := 0; i < MAX_ONUS_PER_PON; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 109 | oltid := s.Olt.ID |
| 110 | intfid := uint32(1) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 111 | sn := convB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 112 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 113 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | return &s |
| 115 | } |
| 116 | |
| 117 | //Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 118 | func (s *Server) Start() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 119 | s.wg = &sync.WaitGroup{} |
| 120 | logger.Debug("Start() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 121 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 122 | close(s.stateChan) |
| 123 | logger.Debug("Start() Done") |
| 124 | }() |
| 125 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 126 | listener, gserver, err := NewGrpcServer(addressport) |
| 127 | if err != nil { |
| 128 | logger.Error("Failed to create gRPC server", err) |
| 129 | return err |
| 130 | } |
| 131 | s.gRPCserver = gserver |
| 132 | openolt.RegisterOpenoltServer(gserver, s) |
| 133 | if err := gserver.Serve(listener); err != nil { |
| 134 | logger.Error("Failed to run gRPC server", err) |
| 135 | return err |
| 136 | } |
| 137 | s.wg.Wait() |
| 138 | return nil |
| 139 | } |
| 140 | |
| 141 | //Non-Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 142 | func (s *Server) Stop() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 143 | logger.Debug("Stop() Start") |
| 144 | defer logger.Debug("Stop() Done") |
| 145 | if s.gRPCserver != nil { |
| 146 | s.gRPCserver.Stop() |
| 147 | logger.Debug("gRPCserver.Stop()") |
| 148 | } |
| 149 | s.StopPktInDaemon() |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | // Blocking |
| 154 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 155 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 156 | olt := s.Olt |
| 157 | olt.InitializeStatus() |
| 158 | for intfid, _ := range s.Onumap { |
| 159 | for _, onu := range s.Onumap[intfid] { |
| 160 | onu.InitializeStatus() |
| 161 | } |
| 162 | } |
| 163 | s.updateState(INACTIVE) |
| 164 | logger.Debug("Enable() Done") |
| 165 | }() |
| 166 | logger.Debug("Enable() Start") |
| 167 | s.EnableServer = sv |
| 168 | if err := s.activateOLT(*sv); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | s.updateState(PRE_ACTIVE) |
| 172 | |
| 173 | coreCtx := context.Background() |
| 174 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 175 | s.cancel = corecancel |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 176 | if err := s.StartPktInDaemon(coreCtx, *sv); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 177 | return err |
| 178 | } |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | //Non-Blocking |
| 183 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 184 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 185 | logger.Debug("Disable() Done") |
| 186 | }() |
| 187 | logger.Debug("Disable() Start") |
| 188 | s.StopPktInDaemon() |
| 189 | } |
| 190 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 191 | func (s *Server) updateState(state coreState) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 192 | s.state = state |
| 193 | s.stateChan <- state |
| 194 | logger.Debug("State updated to:%d", state) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 198 | defer logger.Debug("activateOLT() Done") |
| 199 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 200 | // Activate OLT |
| 201 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 202 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 203 | return err |
| 204 | } |
| 205 | olt.OperState = "up" |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 206 | *olt.InternalState = device.OLT_UP |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 207 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 208 | |
| 209 | // OLT sends Interface Indication to Adapter |
| 210 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 211 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 212 | return err |
| 213 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 214 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 215 | |
| 216 | // OLT sends Operation Indication to Adapter after activating each interface |
| 217 | //time.Sleep(IF_UP_TIME * time.Second) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 218 | *olt.InternalState = device.PONIF_UP |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 219 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 220 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 221 | return err |
| 222 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 223 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 224 | |
| 225 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
| 226 | for intfid, _ := range s.Onumap { |
| 227 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 228 | } |
| 229 | |
| 230 | for intfid, _ := range s.Onumap { |
| 231 | sendOnuDiscInd(stream, s.Onumap[intfid]) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 232 | logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // OLT Sends OnuInd after waiting all of those ONUs up |
| 236 | for { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 237 | if IsAllOnuActive(s.Onumap) { |
| 238 | logger.Debug("All the Onus are Activated.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 239 | break |
| 240 | } |
| 241 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 242 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 243 | for intfid, _ := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 244 | sendOnuInd(stream, s.Onumap[intfid], s.IndInterval) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 245 | logger.Info("OLT id:%d sent ONUInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 246 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 247 | return nil |
| 248 | } |
| 249 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 250 | // Blocking |
| 251 | func (s *Server) StartPktInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 252 | logger.Debug("StartPktInDaemon() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 253 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 254 | RemoveVeths(s.Vethnames) |
| 255 | s.Vethnames = []string{} |
| 256 | s.Ioinfos = []*Ioinfo{} |
| 257 | s.wg.Done() |
| 258 | s.updateState(PRE_ACTIVE) |
| 259 | logger.Debug("StartPktInDaemon() Done") |
| 260 | }() |
| 261 | s.wg.Add(1) |
| 262 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap) |
| 263 | if err != nil { |
| 264 | return err |
| 265 | } |
| 266 | s.Ioinfos = ioinfos |
| 267 | s.Vethnames = veths |
| 268 | logger.Debug("Created vethnames:%v", s.Vethnames) |
| 269 | |
| 270 | parent := ctx |
| 271 | child, cancel := context.WithCancel(parent) |
| 272 | s.cancel = cancel |
| 273 | |
| 274 | if err = s.runPacketInDaemon(child, stream); err != nil { |
| 275 | return err |
| 276 | } |
| 277 | return nil |
| 278 | } |
| 279 | |
| 280 | //Non-Blocking |
| 281 | func (s *Server) StopPktInDaemon() { |
| 282 | if s.cancel != nil { |
| 283 | cancel := s.cancel |
| 284 | cancel() |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 289 | ioinfos := []*Ioinfo{} |
| 290 | var err error |
| 291 | for intfid, _ := range onumap { |
| 292 | for i := 0; i < len(onumap[intfid]); i++ { |
| 293 | var handler *pcap.Handle |
| 294 | onuid := onumap[intfid][i].OnuID |
| 295 | uniup, unidw := makeUniName(oltid, intfid, onuid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 296 | if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil { |
| 297 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 298 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 299 | iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 300 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 301 | oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 302 | ioinfos = append(ioinfos, &oinfo) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | var handler *pcap.Handle |
| 307 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 308 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
| 309 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 310 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 311 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 312 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 313 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 314 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 315 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 316 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 319 | //Blocking |
| 320 | func (s *Server) runPacketInDaemon(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 321 | logger.Debug("runPacketInDaemon Start") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 322 | defer logger.Debug("runPacketInDaemon Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 323 | unichannel := make(chan Packet, 2048) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 324 | |
| 325 | for intfid, _ := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 326 | for _, onu := range s.Onumap[intfid] { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 327 | onuid := onu.OnuID |
| 328 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 329 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 330 | utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 331 | return err |
| 332 | } |
| 333 | uhandler := ioinfo.handler |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 334 | go RecvWorker(ioinfo, uhandler, unichannel) |
| 335 | } |
| 336 | } |
| 337 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 338 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 339 | if err != nil { |
| 340 | return err |
| 341 | } |
| 342 | nhandler := ioinfo.handler |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 343 | nnichannel := make(chan Packet, 32) |
| 344 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 345 | |
| 346 | data := &openolt.Indication_PktInd{} |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 347 | s.updateState(ACTIVE) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 348 | for { |
| 349 | select { |
| 350 | case unipkt := <-unichannel: |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 351 | logger.Debug("Received packet in grpc Server from UNI.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 352 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 353 | logger.Debug("WARNING: This packet does not come from UNI ") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 354 | continue |
| 355 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 356 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 357 | intfid := unipkt.Info.intfid |
| 358 | onuid := unipkt.Info.onuid |
| 359 | gemid, _ := getGemPortID(intfid, onuid) |
| 360 | pkt := unipkt.Pkt |
| 361 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 362 | le, _ := layerEth.(*layers.Ethernet) |
| 363 | ethtype := le.EthernetType |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 364 | onu, _ := s.GetOnuByID(onuid) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 365 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 366 | if ethtype == 0x888e { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 367 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 368 | "gemId": gemid, |
| 369 | }).Info("Received upstream packet is EAPOL.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 370 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 371 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 372 | "gemId": gemid, |
| 373 | }).Info("Received upstream packet is DHCP.") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 374 | |
| 375 | //C-TAG |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 376 | sn := convB2S(onu.SerialNumber.VendorSpecific) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 377 | if ctag, ok := s.CtagMap[sn]; ok == true { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 378 | tagpkt, err := PushVLAN(pkt, uint16(ctag)) |
| 379 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 380 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 381 | "gemId": gemid, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 382 | }).Error("Fail to tag C-tag") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 383 | } else { |
| 384 | pkt = tagpkt |
| 385 | } |
| 386 | } else { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 387 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 388 | "gemId": gemid, |
| 389 | "cTagMap": s.CtagMap, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 390 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 391 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 392 | } else { |
| 393 | continue |
| 394 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 395 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 396 | utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 397 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}} |
| 398 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 399 | logger.Error("Fail to send PktInd indication.", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 400 | return err |
| 401 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 402 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 403 | case nnipkt := <-nnichannel: |
| 404 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 405 | logger.Debug("WARNING: This packet does not come from NNI ") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 406 | continue |
| 407 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 408 | onuid := nnipkt.Info.onuid |
| 409 | onu, _ := getOnuByID(s.Onumap, onuid) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 410 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 411 | utils.LoggerWithOnu(onu).Info("Received packet in grpc Server from NNI.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 412 | intfid := nnipkt.Info.intfid |
| 413 | pkt := nnipkt.Pkt |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 414 | utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 415 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 416 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 417 | logger.Error("Fail to send PktInd indication.", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 418 | return err |
| 419 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 420 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 421 | case <-ctx.Done(): |
| 422 | logger.Debug("PacketInDaemon thread receives close ") |
| 423 | close(unichannel) |
| 424 | logger.Debug("Closed unichannel ") |
| 425 | close(nnichannel) |
| 426 | logger.Debug("Closed nnichannel ") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 427 | return nil |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 428 | } |
| 429 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 430 | return nil |
| 431 | } |
| 432 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 433 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 434 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 435 | onu, _ := s.GetOnuByID(onuid) |
| 436 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 437 | if layerEth != nil { |
| 438 | pkt, _ := layerEth.(*layers.Ethernet) |
| 439 | ethtype := pkt.EthernetType |
| 440 | if ethtype == 0x888e { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 441 | utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 442 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 443 | utils.LoggerWithOnu(onu).Info("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 444 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 445 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 446 | } else { |
| 447 | return nil |
| 448 | } |
| 449 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 450 | if err != nil { |
| 451 | return err |
| 452 | } |
| 453 | handle := ioinfo.handler |
| 454 | SendUni(handle, rawpkt) |
| 455 | return nil |
| 456 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 457 | logger.Info("WARNING: Received packet is not supported") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 458 | return nil |
| 459 | } |
| 460 | |
| 461 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 462 | poppkt, _, err := PopVLAN(rawpkt) |
| 463 | poppkt, _, err = PopVLAN(poppkt) |
| 464 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 465 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 466 | return err |
| 467 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 468 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 469 | if err != nil { |
| 470 | return err |
| 471 | } |
| 472 | handle := ioinfo.handler |
| 473 | SendNni(handle, poppkt) |
| 474 | return nil |
| 475 | } |
| 476 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 477 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 478 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 479 | for _, onu := range onus { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 480 | if onu.GetIntStatus() != device.ONU_ACTIVATED { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 481 | return false |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | return true |
| 486 | } |
| 487 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 488 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
| 489 | idx := uint32(0) |
| 490 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 491 | //return uint32(1032 + 8 * (vid - 1)), nil |
| 492 | } |
| 493 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 494 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 495 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 496 | for _, onu := range onus { |
| 497 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 498 | return onu, nil |
| 499 | } |
| 500 | } |
| 501 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 502 | err := errors.New("No mathced SN is found ") |
| 503 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 504 | return nil, err |
| 505 | } |
| 506 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 507 | func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) { |
| 508 | return getOnuByID(s.Onumap, onuid) |
| 509 | } |
| 510 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 511 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) { |
| 512 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 513 | for _, onu := range onus { |
| 514 | if onu.OnuID == onuid { |
| 515 | return onu, nil |
| 516 | } |
| 517 | } |
| 518 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 519 | err := errors.New("No matched OnuID is found ") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 520 | logger.WithFields(log.Fields{ |
| 521 | "onumap": onumap, |
| 522 | "onuid": onuid, |
| 523 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 524 | return nil, err |
| 525 | } |
| 526 | |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 527 | func convB2S(b []byte) string { |
| 528 | s := "" |
| 529 | for _, i := range b { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 530 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 531 | } |
| 532 | return s |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 533 | } |