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 ( |
| 20 | "gerrit.opencord.org/voltha-bbsim/device" |
| 21 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 22 | "gerrit.opencord.org/voltha-bbsim/setup" |
| 23 | "errors" |
| 24 | "github.com/google/gopacket" |
| 25 | "github.com/google/gopacket/layers" |
| 26 | "github.com/google/gopacket/pcap" |
| 27 | "google.golang.org/grpc" |
| 28 | "log" |
| 29 | "strconv" |
| 30 | "sync" |
| 31 | "time" |
| 32 | ) |
| 33 | |
| 34 | type Mode int |
| 35 | |
| 36 | const MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class |
| 37 | |
| 38 | const ( |
| 39 | DEFAULT Mode = iota |
| 40 | AAA |
| 41 | BOTH |
| 42 | ) |
| 43 | |
| 44 | type Server struct { |
| 45 | Olt *device.Olt |
| 46 | Onumap map[uint32][]*device.Onu |
| 47 | Ioinfos []*Ioinfo |
| 48 | Endchan chan int |
| 49 | Mode Mode |
| 50 | AAAWait int |
| 51 | DhcpWait int |
| 52 | DhcpServerIP string |
| 53 | gRPCserver *grpc.Server |
| 54 | } |
| 55 | |
| 56 | type Packet struct { |
| 57 | Info *Ioinfo |
| 58 | Pkt gopacket.Packet |
| 59 | } |
| 60 | |
| 61 | func CreateServer(oltid uint32, npon uint32, nonus uint32, aaawait int, dhcpwait int, ip string, g *grpc.Server, mode Mode, e chan int) *Server { |
| 62 | s := new(Server) |
| 63 | s.Olt = device.CreateOlt(oltid, npon, 1) |
| 64 | nnni := s.Olt.NumNniIntf |
| 65 | log.Printf("OLT ID: %d was retrieved.\n", s.Olt.ID) |
| 66 | s.Onumap = make(map[uint32][]*device.Onu) |
| 67 | s.AAAWait = aaawait |
| 68 | s.DhcpWait = dhcpwait |
| 69 | s.DhcpServerIP = ip |
| 70 | s.gRPCserver = g |
| 71 | s.Mode = mode |
| 72 | s.Endchan = e |
| 73 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
| 74 | s.Onumap[intfid] = device.CreateOnus(oltid, intfid, nonus, nnni) |
| 75 | } |
| 76 | return s |
| 77 | } |
| 78 | |
| 79 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
| 80 | // Activate OLT |
| 81 | olt := s.Olt |
| 82 | oltid := olt.ID |
| 83 | vethenv := []string{} |
| 84 | wg := &sync.WaitGroup{} |
| 85 | |
| 86 | if err := sendOltInd(stream, olt); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | olt.OperState = "up" |
| 90 | olt.InternalState = device.OLT_UP |
| 91 | log.Printf("OLT %s sent OltInd.\n", olt.Name) |
| 92 | |
| 93 | // OLT sends Interface Indication to Adapter |
| 94 | if err := sendIntfInd(stream, olt); err != nil { |
| 95 | log.Printf("[ERROR] Fail to sendIntfInd: %v\n", err) |
| 96 | return err |
| 97 | } |
| 98 | log.Printf("OLT %s sent IntfInd.\n", olt.Name) |
| 99 | |
| 100 | // OLT sends Operation Indication to Adapter after activating each interface |
| 101 | //time.Sleep(IF_UP_TIME * time.Second) |
| 102 | olt.InternalState = device.PONIF_UP |
| 103 | if err := sendOperInd(stream, olt); err != nil { |
| 104 | log.Printf("[ERROR] Fail to sendOperInd: %v\n", err) |
| 105 | return err |
| 106 | } |
| 107 | log.Printf("OLT %s sent OperInd.\n", olt.Name) |
| 108 | |
| 109 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
| 110 | for intfid, _ := range s.Onumap { |
| 111 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 112 | } |
| 113 | |
| 114 | for intfid, _ := range s.Onumap { |
| 115 | sendOnuDiscInd(stream, s.Onumap[intfid]) |
| 116 | log.Printf("OLT id:%d sent ONUDiscInd.\n", olt.ID) |
| 117 | } |
| 118 | |
| 119 | // OLT Sends OnuInd after waiting all of those ONUs up |
| 120 | for { |
| 121 | if s.IsAllONUActive() { |
| 122 | break |
| 123 | } |
| 124 | } |
| 125 | for intfid, _ := range s.Onumap { |
| 126 | sendOnuInd(stream, s.Onumap[intfid]) |
| 127 | log.Printf("OLT id:%d sent ONUInd.\n", olt.ID) |
| 128 | } |
| 129 | |
| 130 | if s.Mode == DEFAULT { |
| 131 | //EnableIndication's stream should be kept even after activateOLT() is finished. |
| 132 | //Otherwise, OpenOLT adapter sends EnableIndication again. |
| 133 | <-s.Endchan |
| 134 | log.Println("core server thread receives close !") |
| 135 | } else if s.Mode == AAA || s.Mode == BOTH { |
| 136 | var err error |
| 137 | s.Ioinfos = []*Ioinfo{} |
| 138 | for intfid, _ := range s.Onumap { |
| 139 | for i := 0; i < len(s.Onumap[intfid]); i++ { |
| 140 | var handler *pcap.Handle |
| 141 | onuid := s.Onumap[intfid][i].OnuID |
| 142 | uniup, unidw := makeUniName(oltid, intfid, onuid) |
| 143 | if handler, vethenv, err = setupVethHandler(uniup, unidw, vethenv); err != nil { |
| 144 | return err |
| 145 | } |
| 146 | iinfo := Ioinfo{name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler} |
| 147 | s.Ioinfos = append(s.Ioinfos, &iinfo) |
| 148 | oinfo := Ioinfo{name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil} |
| 149 | s.Ioinfos = append(s.Ioinfos, &oinfo) |
| 150 | } |
| 151 | } |
| 152 | var handler *pcap.Handle |
| 153 | nniup, nnidw := makeNniName(oltid) |
| 154 | if handler, vethenv, err = setupVethHandler(nniup, nnidw, vethenv); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | iinfo := Ioinfo{name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
| 158 | s.Ioinfos = append(s.Ioinfos, &iinfo) |
| 159 | oinfo := Ioinfo{name: nnidw, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
| 160 | s.Ioinfos = append(s.Ioinfos, &oinfo) |
| 161 | |
| 162 | errchan := make(chan error) |
| 163 | go func() { |
| 164 | <-errchan |
| 165 | close(s.Endchan) |
| 166 | }() |
| 167 | |
| 168 | wg.Add(1) |
| 169 | go func() { |
| 170 | defer func() { |
| 171 | log.Println("runPacketInDaemon Done") |
| 172 | wg.Done() |
| 173 | }() |
| 174 | err := s.runPacketInDaemon(stream) |
| 175 | if err != nil { |
| 176 | errchan <- err |
| 177 | return |
| 178 | } |
| 179 | }() |
| 180 | |
| 181 | wg.Add(1) |
| 182 | go func() { |
| 183 | defer func() { |
| 184 | log.Println("exeAAATest Done") |
| 185 | wg.Done() |
| 186 | }() |
| 187 | infos, err := s.getUniIoinfos("outside") |
| 188 | if err != nil { |
| 189 | errchan <- err |
| 190 | return |
| 191 | } |
| 192 | univeths := []string{} |
| 193 | for _, info := range infos { |
| 194 | univeths = append(univeths, info.name) |
| 195 | } |
| 196 | err = s.exeAAATest(univeths) |
| 197 | if err != nil { |
| 198 | errchan <- err |
| 199 | return |
| 200 | } |
| 201 | }() |
| 202 | wg.Wait() |
| 203 | tearDown(vethenv) // Grace teardown |
| 204 | log.Println("Grace shutdown down") |
| 205 | } |
| 206 | return nil |
| 207 | } |
| 208 | |
| 209 | func (s *Server) runPacketInDaemon(stream openolt.Openolt_EnableIndicationServer) error { |
| 210 | log.Println("runPacketInDaemon Start") |
| 211 | unichannel := make(chan Packet, 2048) |
| 212 | flag := false |
| 213 | |
| 214 | for intfid, _ := range s.Onumap { |
| 215 | for _, onu := range s.Onumap[intfid] { //TODO: should be updated for multiple-Interface |
| 216 | onuid := onu.OnuID |
| 217 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 218 | if err != nil { |
| 219 | log.Printf("[ERROR] Fail to identifyUniIoinfo (onuid: %d): %v\n", onuid, err) |
| 220 | return err |
| 221 | } |
| 222 | uhandler := ioinfo.handler |
| 223 | defer uhandler.Close() |
| 224 | go RecvWorker(ioinfo, uhandler, unichannel) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 229 | if err != nil { |
| 230 | return err |
| 231 | } |
| 232 | nhandler := ioinfo.handler |
| 233 | defer nhandler.Close() |
| 234 | nnichannel := make(chan Packet, 32) |
| 235 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 236 | |
| 237 | data := &openolt.Indication_PktInd{} |
| 238 | for { |
| 239 | select { |
| 240 | case unipkt := <-unichannel: |
| 241 | log.Println("Received packet in grpc Server from UNI.") |
| 242 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
| 243 | log.Println("[WARNING] This packet does not come from UNI !") |
| 244 | continue |
| 245 | } |
| 246 | intfid := unipkt.Info.intfid |
| 247 | onuid := unipkt.Info.onuid |
| 248 | gemid, _ := getGemPortID(intfid, onuid) |
| 249 | pkt := unipkt.Pkt |
| 250 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 251 | le, _ := layerEth.(*layers.Ethernet) |
| 252 | ethtype := le.EthernetType |
| 253 | if ethtype == 0x888e { |
| 254 | log.Printf("Received upstream packet is EAPOL.") |
| 255 | log.Println(unipkt.Pkt.Dump()) |
| 256 | log.Println(pkt.Dump()) |
| 257 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 258 | log.Printf("Received upstream packet is DHCP.") |
| 259 | log.Println(unipkt.Pkt.Dump()) |
| 260 | log.Println(pkt.Dump()) |
| 261 | } else { |
| 262 | continue |
| 263 | } |
| 264 | log.Printf("sendPktInd intfid:%d (onuid: %d) gemid:%d\n", intfid, onuid, gemid) |
| 265 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}} |
| 266 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 267 | log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err) |
| 268 | return err |
| 269 | } |
| 270 | case nnipkt := <-nnichannel: |
| 271 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 272 | log.Println("[WARNING] This packet does not come from NNI !") |
| 273 | continue |
| 274 | } |
| 275 | log.Println("Received packet in grpc Server from NNI.") |
| 276 | intfid := nnipkt.Info.intfid |
| 277 | pkt := nnipkt.Pkt |
| 278 | log.Printf("sendPktInd intfid:%d\n", intfid) |
| 279 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 280 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 281 | log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err) |
| 282 | return err |
| 283 | } |
| 284 | case <-s.Endchan: |
| 285 | if flag == false { |
| 286 | log.Println("PacketInDaemon thread receives close !") |
| 287 | close(unichannel) |
| 288 | log.Println("Closed unichannel !") |
| 289 | close(nnichannel) |
| 290 | log.Println("Closed nnichannel !") |
| 291 | flag = true |
| 292 | return nil |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | func (s *Server) exeAAATest(vethenv []string) error { |
| 300 | log.Println("exeAAATest Start") |
| 301 | for i := 0; i < s.AAAWait; i++ { |
| 302 | select { |
| 303 | case <-s.Endchan: |
| 304 | log.Println("exeAAATest thread receives close !") |
| 305 | return nil |
| 306 | default: |
| 307 | log.Println("exeAAATest is now sleeping....") |
| 308 | time.Sleep(time.Second) |
| 309 | } |
| 310 | } |
| 311 | err := setup.ActivateWPASups(vethenv) |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | return nil |
| 316 | } |
| 317 | |
| 318 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 319 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
| 320 | if layerEth != nil { |
| 321 | pkt, _ := layerEth.(*layers.Ethernet) |
| 322 | ethtype := pkt.EthernetType |
| 323 | if ethtype == 0x888e { |
| 324 | log.Printf("Received downstream packet is EAPOL.") |
| 325 | log.Println(rawpkt.Dump()) |
| 326 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 327 | log.Printf("Received downstream packet is DHCP.") |
| 328 | log.Println(rawpkt.Dump()) |
| 329 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 330 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 331 | } else { |
| 332 | return nil |
| 333 | } |
| 334 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 335 | if err != nil { |
| 336 | return err |
| 337 | } |
| 338 | handle := ioinfo.handler |
| 339 | SendUni(handle, rawpkt) |
| 340 | return nil |
| 341 | } |
| 342 | log.Printf("[WARNING] Received packet is not supported") |
| 343 | return nil |
| 344 | } |
| 345 | |
| 346 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
| 347 | log.Println("") |
| 348 | poppkt, _, err := PopVLAN(rawpkt) |
| 349 | poppkt, _, err = PopVLAN(poppkt) |
| 350 | if err != nil { |
| 351 | log.Println(err) |
| 352 | return err |
| 353 | } |
| 354 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | handle := ioinfo.handler |
| 359 | SendNni(handle, poppkt) |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | func (s *Server) IsAllONUActive() bool { |
| 364 | for _, onus := range s.Onumap { |
| 365 | for _, onu := range onus { |
| 366 | if onu.InternalState != device.ONU_ACTIVATED { |
| 367 | return false |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | return true |
| 372 | } |
| 373 | |
| 374 | func getVID(onuid uint32) (uint16, error) { |
| 375 | return uint16(onuid), nil |
| 376 | } |
| 377 | |
| 378 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
| 379 | idx := uint32(0) |
| 380 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 381 | //return uint32(1032 + 8 * (vid - 1)), nil |
| 382 | } |
| 383 | |
| 384 | func (s *Server) getOnuBySN(sn *openolt.SerialNumber) (*device.Onu, error) { |
| 385 | for _, onus := range s.Onumap { |
| 386 | for _, onu := range onus { |
| 387 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 388 | return onu, nil |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | err := errors.New("No mathced SN is found !") |
| 393 | log.Println(err) |
| 394 | return nil, err |
| 395 | } |
| 396 | |
| 397 | func (s *Server) getOnuByID(onuid uint32) (*device.Onu, error) { |
| 398 | for _, onus := range s.Onumap { |
| 399 | for _, onu := range onus { |
| 400 | if onu.OnuID == onuid { |
| 401 | return onu, nil |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | err := errors.New("No matched OnuID is found !") |
| 406 | log.Println(err) |
| 407 | return nil, err |
| 408 | } |
| 409 | |
| 410 | func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) { |
| 411 | upif = setup.UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 412 | dwif = setup.UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 413 | return |
| 414 | } |
| 415 | |
| 416 | func makeNniName(oltid uint32) (upif string, dwif string) { |
| 417 | upif = setup.NNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) |
| 418 | dwif = setup.NNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) |
| 419 | return |
| 420 | } |
| 421 | |
| 422 | func tearDown(vethenv []string) error { |
| 423 | log.Println("tearDown()") |
| 424 | setup.KillAllWPASups() |
| 425 | setup.KillAllDHCPClients() |
| 426 | setup.TearVethDown(vethenv) |
| 427 | return nil |
| 428 | } |
| 429 | |
| 430 | func setupVethHandler(inveth string, outveth string, vethenv []string) (*pcap.Handle, []string, error) { |
| 431 | log.Printf("setupVethHandler: %s and %s\n", inveth, outveth) |
| 432 | err1 := setup.CreateVethPairs(inveth, outveth) |
| 433 | vethenv = append(vethenv, inveth) |
| 434 | if err1 != nil { |
| 435 | setup.RemoveVeths(vethenv) |
| 436 | return nil, vethenv, err1 |
| 437 | } |
| 438 | handler, err2 := getVethHandler(inveth) |
| 439 | if err2 != nil { |
| 440 | setup.RemoveVeths(vethenv) |
| 441 | return nil, vethenv, err2 |
| 442 | } |
| 443 | return handler, vethenv, nil |
| 444 | } |
| 445 | |
| 446 | func getVethHandler(vethname string) (*pcap.Handle, error) { |
| 447 | var ( |
| 448 | device string = vethname |
| 449 | snapshot_len int32 = 1518 |
| 450 | promiscuous bool = false |
| 451 | err error |
| 452 | timeout time.Duration = pcap.BlockForever |
| 453 | ) |
| 454 | handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout) |
| 455 | if err != nil { |
| 456 | return nil, err |
| 457 | } |
| 458 | log.Printf("Server handle created for %s\n", vethname) |
| 459 | return handle, nil |
| 460 | } |