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 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame^] | 201 | |
| 202 | if s.Mode == BOTH { |
| 203 | go func() { |
| 204 | defer func() { |
| 205 | log.Println("exeDHCPTest Done") |
| 206 | }() |
| 207 | info, err := s.identifyNniIoinfo("outside") |
| 208 | setup.ActivateDHCPServer(info.name, s.DhcpServerIP) |
| 209 | |
| 210 | infos, err := s.getUniIoinfos("outside") |
| 211 | if err != nil { |
| 212 | errchan <- err |
| 213 | return |
| 214 | } |
| 215 | univeths := []string{} |
| 216 | for _, info := range infos { |
| 217 | univeths = append(univeths, info.name) |
| 218 | } |
| 219 | err = s.exeDHCPTest(univeths) |
| 220 | if err != nil { |
| 221 | errchan <- err |
| 222 | return |
| 223 | } |
| 224 | }() |
| 225 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 226 | }() |
| 227 | wg.Wait() |
| 228 | tearDown(vethenv) // Grace teardown |
| 229 | log.Println("Grace shutdown down") |
| 230 | } |
| 231 | return nil |
| 232 | } |
| 233 | |
| 234 | func (s *Server) runPacketInDaemon(stream openolt.Openolt_EnableIndicationServer) error { |
| 235 | log.Println("runPacketInDaemon Start") |
| 236 | unichannel := make(chan Packet, 2048) |
| 237 | flag := false |
| 238 | |
| 239 | for intfid, _ := range s.Onumap { |
| 240 | for _, onu := range s.Onumap[intfid] { //TODO: should be updated for multiple-Interface |
| 241 | onuid := onu.OnuID |
| 242 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 243 | if err != nil { |
| 244 | log.Printf("[ERROR] Fail to identifyUniIoinfo (onuid: %d): %v\n", onuid, err) |
| 245 | return err |
| 246 | } |
| 247 | uhandler := ioinfo.handler |
| 248 | defer uhandler.Close() |
| 249 | go RecvWorker(ioinfo, uhandler, unichannel) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | nhandler := ioinfo.handler |
| 258 | defer nhandler.Close() |
| 259 | nnichannel := make(chan Packet, 32) |
| 260 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 261 | |
| 262 | data := &openolt.Indication_PktInd{} |
| 263 | for { |
| 264 | select { |
| 265 | case unipkt := <-unichannel: |
| 266 | log.Println("Received packet in grpc Server from UNI.") |
| 267 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
| 268 | log.Println("[WARNING] This packet does not come from UNI !") |
| 269 | continue |
| 270 | } |
| 271 | intfid := unipkt.Info.intfid |
| 272 | onuid := unipkt.Info.onuid |
| 273 | gemid, _ := getGemPortID(intfid, onuid) |
| 274 | pkt := unipkt.Pkt |
| 275 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 276 | le, _ := layerEth.(*layers.Ethernet) |
| 277 | ethtype := le.EthernetType |
| 278 | if ethtype == 0x888e { |
| 279 | log.Printf("Received upstream packet is EAPOL.") |
| 280 | log.Println(unipkt.Pkt.Dump()) |
| 281 | log.Println(pkt.Dump()) |
| 282 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 283 | log.Printf("Received upstream packet is DHCP.") |
| 284 | log.Println(unipkt.Pkt.Dump()) |
| 285 | log.Println(pkt.Dump()) |
| 286 | } else { |
| 287 | continue |
| 288 | } |
| 289 | log.Printf("sendPktInd intfid:%d (onuid: %d) gemid:%d\n", intfid, onuid, gemid) |
| 290 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}} |
| 291 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 292 | log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err) |
| 293 | return err |
| 294 | } |
| 295 | case nnipkt := <-nnichannel: |
| 296 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 297 | log.Println("[WARNING] This packet does not come from NNI !") |
| 298 | continue |
| 299 | } |
| 300 | log.Println("Received packet in grpc Server from NNI.") |
| 301 | intfid := nnipkt.Info.intfid |
| 302 | pkt := nnipkt.Pkt |
| 303 | log.Printf("sendPktInd intfid:%d\n", intfid) |
| 304 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 305 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 306 | log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err) |
| 307 | return err |
| 308 | } |
| 309 | case <-s.Endchan: |
| 310 | if flag == false { |
| 311 | log.Println("PacketInDaemon thread receives close !") |
| 312 | close(unichannel) |
| 313 | log.Println("Closed unichannel !") |
| 314 | close(nnichannel) |
| 315 | log.Println("Closed nnichannel !") |
| 316 | flag = true |
| 317 | return nil |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | return nil |
| 322 | } |
| 323 | |
| 324 | func (s *Server) exeAAATest(vethenv []string) error { |
| 325 | log.Println("exeAAATest Start") |
| 326 | for i := 0; i < s.AAAWait; i++ { |
| 327 | select { |
| 328 | case <-s.Endchan: |
| 329 | log.Println("exeAAATest thread receives close !") |
| 330 | return nil |
| 331 | default: |
| 332 | log.Println("exeAAATest is now sleeping....") |
| 333 | time.Sleep(time.Second) |
| 334 | } |
| 335 | } |
| 336 | err := setup.ActivateWPASups(vethenv) |
| 337 | if err != nil { |
| 338 | return err |
| 339 | } |
| 340 | return nil |
| 341 | } |
| 342 | |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame^] | 343 | func (s *Server) exeDHCPTest(vethenv []string) error { |
| 344 | log.Println("exeDHCPTest Start") |
| 345 | for i := 0; i < s.DhcpWait; i++ { |
| 346 | select { |
| 347 | case <-s.Endchan: |
| 348 | log.Println("exeDHCPTest thread receives close !") |
| 349 | return nil |
| 350 | default: |
| 351 | log.Println("exeDHCPTest is now sleeping....") |
| 352 | time.Sleep(time.Second) |
| 353 | } |
| 354 | } |
| 355 | err := setup.ActivateDHCPClients(vethenv) |
| 356 | if err != nil { |
| 357 | return err |
| 358 | } |
| 359 | return nil |
| 360 | } |
| 361 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 362 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 363 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
| 364 | if layerEth != nil { |
| 365 | pkt, _ := layerEth.(*layers.Ethernet) |
| 366 | ethtype := pkt.EthernetType |
| 367 | if ethtype == 0x888e { |
| 368 | log.Printf("Received downstream packet is EAPOL.") |
| 369 | log.Println(rawpkt.Dump()) |
| 370 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 371 | log.Printf("Received downstream packet is DHCP.") |
| 372 | log.Println(rawpkt.Dump()) |
| 373 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 374 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 375 | } else { |
| 376 | return nil |
| 377 | } |
| 378 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 379 | if err != nil { |
| 380 | return err |
| 381 | } |
| 382 | handle := ioinfo.handler |
| 383 | SendUni(handle, rawpkt) |
| 384 | return nil |
| 385 | } |
| 386 | log.Printf("[WARNING] Received packet is not supported") |
| 387 | return nil |
| 388 | } |
| 389 | |
| 390 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
| 391 | log.Println("") |
| 392 | poppkt, _, err := PopVLAN(rawpkt) |
| 393 | poppkt, _, err = PopVLAN(poppkt) |
| 394 | if err != nil { |
| 395 | log.Println(err) |
| 396 | return err |
| 397 | } |
| 398 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 399 | if err != nil { |
| 400 | return err |
| 401 | } |
| 402 | handle := ioinfo.handler |
| 403 | SendNni(handle, poppkt) |
| 404 | return nil |
| 405 | } |
| 406 | |
| 407 | func (s *Server) IsAllONUActive() bool { |
| 408 | for _, onus := range s.Onumap { |
| 409 | for _, onu := range onus { |
| 410 | if onu.InternalState != device.ONU_ACTIVATED { |
| 411 | return false |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | return true |
| 416 | } |
| 417 | |
| 418 | func getVID(onuid uint32) (uint16, error) { |
| 419 | return uint16(onuid), nil |
| 420 | } |
| 421 | |
| 422 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
| 423 | idx := uint32(0) |
| 424 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 425 | //return uint32(1032 + 8 * (vid - 1)), nil |
| 426 | } |
| 427 | |
| 428 | func (s *Server) getOnuBySN(sn *openolt.SerialNumber) (*device.Onu, error) { |
| 429 | for _, onus := range s.Onumap { |
| 430 | for _, onu := range onus { |
| 431 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 432 | return onu, nil |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | err := errors.New("No mathced SN is found !") |
| 437 | log.Println(err) |
| 438 | return nil, err |
| 439 | } |
| 440 | |
| 441 | func (s *Server) getOnuByID(onuid uint32) (*device.Onu, error) { |
| 442 | for _, onus := range s.Onumap { |
| 443 | for _, onu := range onus { |
| 444 | if onu.OnuID == onuid { |
| 445 | return onu, nil |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | err := errors.New("No matched OnuID is found !") |
| 450 | log.Println(err) |
| 451 | return nil, err |
| 452 | } |
| 453 | |
| 454 | func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) { |
| 455 | upif = setup.UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 456 | dwif = setup.UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 457 | return |
| 458 | } |
| 459 | |
| 460 | func makeNniName(oltid uint32) (upif string, dwif string) { |
| 461 | upif = setup.NNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) |
| 462 | dwif = setup.NNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) |
| 463 | return |
| 464 | } |
| 465 | |
| 466 | func tearDown(vethenv []string) error { |
| 467 | log.Println("tearDown()") |
| 468 | setup.KillAllWPASups() |
| 469 | setup.KillAllDHCPClients() |
| 470 | setup.TearVethDown(vethenv) |
| 471 | return nil |
| 472 | } |
| 473 | |
| 474 | func setupVethHandler(inveth string, outveth string, vethenv []string) (*pcap.Handle, []string, error) { |
| 475 | log.Printf("setupVethHandler: %s and %s\n", inveth, outveth) |
| 476 | err1 := setup.CreateVethPairs(inveth, outveth) |
| 477 | vethenv = append(vethenv, inveth) |
| 478 | if err1 != nil { |
| 479 | setup.RemoveVeths(vethenv) |
| 480 | return nil, vethenv, err1 |
| 481 | } |
| 482 | handler, err2 := getVethHandler(inveth) |
| 483 | if err2 != nil { |
| 484 | setup.RemoveVeths(vethenv) |
| 485 | return nil, vethenv, err2 |
| 486 | } |
| 487 | return handler, vethenv, nil |
| 488 | } |
| 489 | |
| 490 | func getVethHandler(vethname string) (*pcap.Handle, error) { |
| 491 | var ( |
| 492 | device string = vethname |
| 493 | snapshot_len int32 = 1518 |
| 494 | promiscuous bool = false |
| 495 | err error |
| 496 | timeout time.Duration = pcap.BlockForever |
| 497 | ) |
| 498 | handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout) |
| 499 | if err != nil { |
| 500 | return nil, err |
| 501 | } |
| 502 | log.Printf("Server handle created for %s\n", vethname) |
| 503 | return handle, nil |
| 504 | } |