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 | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 20 | "errors" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 21 | "gerrit.opencord.org/voltha-bbsim/device" |
| 22 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 23 | "gerrit.opencord.org/voltha-bbsim/setup" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 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 |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 54 | VethEnv []string |
| 55 | TestFlag bool |
| 56 | Processes []string |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 57 | EnableServer *openolt.Openolt_EnableIndicationServer |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | type Packet struct { |
| 61 | Info *Ioinfo |
| 62 | Pkt gopacket.Packet |
| 63 | } |
| 64 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 65 | func (s *Server)Initialize(){ |
| 66 | s.VethEnv = []string{} |
| 67 | s.Endchan = make(chan int) |
| 68 | s.TestFlag = false |
| 69 | s.Processes = []string{} |
| 70 | s.Ioinfos = []*Ioinfo{} |
| 71 | } |
| 72 | |
| 73 | func Create(oltid uint32, npon uint32, nonus uint32, aaawait int, dhcpwait int, ip string, g *grpc.Server, mode Mode, e chan int) *Server { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 74 | s := new(Server) |
| 75 | s.Olt = device.CreateOlt(oltid, npon, 1) |
| 76 | nnni := s.Olt.NumNniIntf |
| 77 | log.Printf("OLT ID: %d was retrieved.\n", s.Olt.ID) |
| 78 | s.Onumap = make(map[uint32][]*device.Onu) |
| 79 | s.AAAWait = aaawait |
| 80 | s.DhcpWait = dhcpwait |
| 81 | s.DhcpServerIP = ip |
| 82 | s.gRPCserver = g |
| 83 | s.Mode = mode |
| 84 | s.Endchan = e |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 85 | s.VethEnv = []string{} |
| 86 | s.TestFlag = false |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 87 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
| 88 | s.Onumap[intfid] = device.CreateOnus(oltid, intfid, nonus, nnni) |
| 89 | } |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 90 | s.EnableServer = new(openolt.Openolt_EnableIndicationServer) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 91 | return s |
| 92 | } |
| 93 | |
| 94 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
| 95 | // Activate OLT |
| 96 | olt := s.Olt |
| 97 | oltid := olt.ID |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 98 | wg := &sync.WaitGroup{} |
| 99 | |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 100 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 101 | return err |
| 102 | } |
| 103 | olt.OperState = "up" |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 104 | *olt.InternalState = device.OLT_UP |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 105 | log.Printf("OLT %s sent OltInd.\n", olt.Name) |
| 106 | |
| 107 | // OLT sends Interface Indication to Adapter |
| 108 | if err := sendIntfInd(stream, olt); err != nil { |
| 109 | log.Printf("[ERROR] Fail to sendIntfInd: %v\n", err) |
| 110 | return err |
| 111 | } |
| 112 | log.Printf("OLT %s sent IntfInd.\n", olt.Name) |
| 113 | |
| 114 | // OLT sends Operation Indication to Adapter after activating each interface |
| 115 | //time.Sleep(IF_UP_TIME * time.Second) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 116 | *olt.InternalState = device.PONIF_UP |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 117 | if err := sendOperInd(stream, olt); err != nil { |
| 118 | log.Printf("[ERROR] Fail to sendOperInd: %v\n", err) |
| 119 | return err |
| 120 | } |
| 121 | log.Printf("OLT %s sent OperInd.\n", olt.Name) |
| 122 | |
| 123 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
| 124 | for intfid, _ := range s.Onumap { |
| 125 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 126 | } |
| 127 | |
| 128 | for intfid, _ := range s.Onumap { |
| 129 | sendOnuDiscInd(stream, s.Onumap[intfid]) |
| 130 | log.Printf("OLT id:%d sent ONUDiscInd.\n", olt.ID) |
| 131 | } |
| 132 | |
| 133 | // OLT Sends OnuInd after waiting all of those ONUs up |
| 134 | for { |
| 135 | if s.IsAllONUActive() { |
| 136 | break |
| 137 | } |
| 138 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 139 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 140 | for intfid, _ := range s.Onumap { |
| 141 | sendOnuInd(stream, s.Onumap[intfid]) |
| 142 | log.Printf("OLT id:%d sent ONUInd.\n", olt.ID) |
| 143 | } |
| 144 | |
| 145 | if s.Mode == DEFAULT { |
| 146 | //EnableIndication's stream should be kept even after activateOLT() is finished. |
| 147 | //Otherwise, OpenOLT adapter sends EnableIndication again. |
| 148 | <-s.Endchan |
| 149 | log.Println("core server thread receives close !") |
| 150 | } else if s.Mode == AAA || s.Mode == BOTH { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 151 | s.TestFlag = true |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 152 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 153 | s.Ioinfos, s.VethEnv, err = createIoinfos(oltid, s.VethEnv, s.Onumap) |
| 154 | log.Println("s.VethEnv", s.VethEnv) |
| 155 | if err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 156 | return err |
| 157 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 158 | |
| 159 | errchan := make(chan error) |
| 160 | go func() { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 161 | <- errchan |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 162 | close(s.Endchan) |
| 163 | }() |
| 164 | |
| 165 | wg.Add(1) |
| 166 | go func() { |
| 167 | defer func() { |
| 168 | log.Println("runPacketInDaemon Done") |
| 169 | wg.Done() |
| 170 | }() |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 171 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 172 | err := s.runPacketInDaemon(stream) |
| 173 | if err != nil { |
| 174 | errchan <- err |
| 175 | return |
| 176 | } |
| 177 | }() |
| 178 | |
| 179 | wg.Add(1) |
| 180 | go func() { |
| 181 | defer func() { |
| 182 | log.Println("exeAAATest Done") |
| 183 | wg.Done() |
| 184 | }() |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 185 | |
| 186 | err = s.exeAAATest() |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 187 | if err != nil { |
| 188 | errchan <- err |
| 189 | return |
| 190 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 191 | |
| 192 | if s.Mode == BOTH { |
| 193 | go func() { |
| 194 | defer func() { |
| 195 | log.Println("exeDHCPTest Done") |
| 196 | }() |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 197 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 198 | err := s.exeDHCPTest() |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 199 | if err != nil { |
| 200 | errchan <- err |
| 201 | return |
| 202 | } |
| 203 | }() |
| 204 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 205 | }() |
| 206 | wg.Wait() |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 207 | cleanUpVeths(s.VethEnv) // Grace teardown |
| 208 | pnames := s.Processes |
| 209 | killProcesses(pnames) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 210 | log.Println("Grace shutdown down") |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 215 | func createIoinfos(oltid uint32, vethenv []string, onumap map[uint32][]*device.Onu)([]*Ioinfo, []string, error){ |
| 216 | ioinfos := []*Ioinfo{} |
| 217 | var err error |
| 218 | for intfid, _ := range onumap { |
| 219 | for i := 0; i < len(onumap[intfid]); i++ { |
| 220 | var handler *pcap.Handle |
| 221 | onuid := onumap[intfid][i].OnuID |
| 222 | uniup, unidw := makeUniName(oltid, intfid, onuid) |
| 223 | if handler, vethenv, err = setupVethHandler(uniup, unidw, vethenv); err != nil { |
| 224 | return ioinfos, vethenv, err |
| 225 | } |
| 226 | iinfo := Ioinfo{name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler} |
| 227 | ioinfos = append(ioinfos, &iinfo) |
| 228 | oinfo := Ioinfo{name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil} |
| 229 | ioinfos = append(ioinfos, &oinfo) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | var handler *pcap.Handle |
| 234 | nniup, nnidw := makeNniName(oltid) |
| 235 | if handler, vethenv, err = setupVethHandler(nniup, nnidw, vethenv); err != nil { |
| 236 | return ioinfos, vethenv, err |
| 237 | } |
| 238 | //TODO: Intfid should be modified |
| 239 | iinfo := Ioinfo{name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
| 240 | ioinfos = append(ioinfos, &iinfo) |
| 241 | oinfo := Ioinfo{name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
| 242 | ioinfos = append(ioinfos, &oinfo) |
| 243 | return ioinfos, vethenv, nil |
| 244 | } |
| 245 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 246 | func (s *Server) runPacketInDaemon(stream openolt.Openolt_EnableIndicationServer) error { |
| 247 | log.Println("runPacketInDaemon Start") |
| 248 | unichannel := make(chan Packet, 2048) |
| 249 | flag := false |
| 250 | |
| 251 | for intfid, _ := range s.Onumap { |
| 252 | for _, onu := range s.Onumap[intfid] { //TODO: should be updated for multiple-Interface |
| 253 | onuid := onu.OnuID |
| 254 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 255 | if err != nil { |
| 256 | log.Printf("[ERROR] Fail to identifyUniIoinfo (onuid: %d): %v\n", onuid, err) |
| 257 | return err |
| 258 | } |
| 259 | uhandler := ioinfo.handler |
| 260 | defer uhandler.Close() |
| 261 | go RecvWorker(ioinfo, uhandler, unichannel) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 266 | if err != nil { |
| 267 | return err |
| 268 | } |
| 269 | nhandler := ioinfo.handler |
| 270 | defer nhandler.Close() |
| 271 | nnichannel := make(chan Packet, 32) |
| 272 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 273 | |
| 274 | data := &openolt.Indication_PktInd{} |
| 275 | for { |
| 276 | select { |
| 277 | case unipkt := <-unichannel: |
| 278 | log.Println("Received packet in grpc Server from UNI.") |
| 279 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
| 280 | log.Println("[WARNING] This packet does not come from UNI !") |
| 281 | continue |
| 282 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 283 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 284 | intfid := unipkt.Info.intfid |
| 285 | onuid := unipkt.Info.onuid |
| 286 | gemid, _ := getGemPortID(intfid, onuid) |
| 287 | pkt := unipkt.Pkt |
| 288 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 289 | le, _ := layerEth.(*layers.Ethernet) |
| 290 | ethtype := le.EthernetType |
| 291 | if ethtype == 0x888e { |
| 292 | log.Printf("Received upstream packet is EAPOL.") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 293 | //log.Println(unipkt.Pkt.Dump()) |
| 294 | //log.Println(pkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 295 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 296 | log.Printf("Received upstream packet is DHCP.") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 297 | //log.Println(unipkt.Pkt.Dump()) |
| 298 | //log.Println(pkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 299 | } else { |
| 300 | continue |
| 301 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 302 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 303 | log.Printf("sendPktInd intfid:%d (onuid: %d) gemid:%d\n", intfid, onuid, gemid) |
| 304 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, 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 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 309 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 310 | case nnipkt := <-nnichannel: |
| 311 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 312 | log.Println("[WARNING] This packet does not come from NNI !") |
| 313 | continue |
| 314 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 315 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 316 | log.Println("Received packet in grpc Server from NNI.") |
| 317 | intfid := nnipkt.Info.intfid |
| 318 | pkt := nnipkt.Pkt |
| 319 | log.Printf("sendPktInd intfid:%d\n", intfid) |
| 320 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 321 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 322 | log.Printf("[ERROR] Failed to send PktInd indication. %v\n", err) |
| 323 | return err |
| 324 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 325 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 326 | case <-s.Endchan: |
| 327 | if flag == false { |
| 328 | log.Println("PacketInDaemon thread receives close !") |
| 329 | close(unichannel) |
| 330 | log.Println("Closed unichannel !") |
| 331 | close(nnichannel) |
| 332 | log.Println("Closed nnichannel !") |
| 333 | flag = true |
| 334 | return nil |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | return nil |
| 339 | } |
| 340 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 341 | func (s *Server) exeAAATest() error { |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 342 | log.Println("exeAAATest starts to sleep....") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 343 | infos, err := s.getUniIoinfos("outside") |
| 344 | if err != nil { |
| 345 | return err |
| 346 | } |
| 347 | |
| 348 | univeths := []string{} |
| 349 | for _, info := range infos { |
| 350 | univeths = append(univeths, info.name) |
| 351 | } |
| 352 | |
| 353 | for { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 354 | select { |
| 355 | case <-s.Endchan: |
| 356 | log.Println("exeAAATest thread receives close !") |
| 357 | return nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 358 | case <- time.After(time.Second * time.Duration(s.AAAWait)): |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 359 | log.Println("exeAAATest Start") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 360 | err = setup.ActivateWPASups(univeths) |
| 361 | if err != nil { |
| 362 | return err |
| 363 | } |
| 364 | s.Processes = append(s.Processes, "wpa_supplicant") |
| 365 | log.Println("s.Processes:", s.Processes) |
| 366 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 367 | } |
| 368 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 369 | return nil |
| 370 | } |
| 371 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 372 | func (s *Server) exeDHCPTest() error { |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 373 | log.Println("exeDHCPTest starts to sleep....") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 374 | info, err := s.identifyNniIoinfo("outside") |
| 375 | |
| 376 | if err != nil { |
| 377 | return err |
| 378 | } |
| 379 | |
| 380 | err = setup.ActivateDHCPServer(info.name, s.DhcpServerIP) |
| 381 | if err != nil { |
| 382 | return err |
| 383 | } |
| 384 | s.Processes = append(s.Processes, "dhcpd") |
| 385 | |
| 386 | infos, err := s.getUniIoinfos("outside") |
| 387 | if err != nil { |
| 388 | return err |
| 389 | } |
| 390 | |
| 391 | univeths := []string{} |
| 392 | for _, info := range infos { |
| 393 | univeths = append(univeths, info.name) |
| 394 | } |
| 395 | |
| 396 | for { |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 397 | select { |
| 398 | case <-s.Endchan: |
| 399 | log.Println("exeDHCPTest thread receives close !") |
| 400 | return nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 401 | case <- time.After(time.Second * time.Duration(s.DhcpWait)): |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 402 | log.Println("exeDHCPTest Start") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 403 | err = setup.ActivateDHCPClients(univeths) |
| 404 | if err != nil { |
| 405 | return err |
| 406 | } |
| 407 | s.Processes = append(s.Processes, "dhclient") |
| 408 | log.Println("s.Processes:", s.Processes) |
| 409 | return nil |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 410 | } |
| 411 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 412 | return nil |
| 413 | } |
| 414 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 415 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 416 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
| 417 | if layerEth != nil { |
| 418 | pkt, _ := layerEth.(*layers.Ethernet) |
| 419 | ethtype := pkt.EthernetType |
| 420 | if ethtype == 0x888e { |
| 421 | log.Printf("Received downstream packet is EAPOL.") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 422 | //log.Println(rawpkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 423 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 424 | log.Printf("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame^] | 425 | //log.Println(rawpkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 426 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 427 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 428 | } else { |
| 429 | return nil |
| 430 | } |
| 431 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 432 | if err != nil { |
| 433 | return err |
| 434 | } |
| 435 | handle := ioinfo.handler |
| 436 | SendUni(handle, rawpkt) |
| 437 | return nil |
| 438 | } |
| 439 | log.Printf("[WARNING] Received packet is not supported") |
| 440 | return nil |
| 441 | } |
| 442 | |
| 443 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
| 444 | log.Println("") |
| 445 | poppkt, _, err := PopVLAN(rawpkt) |
| 446 | poppkt, _, err = PopVLAN(poppkt) |
| 447 | if err != nil { |
| 448 | log.Println(err) |
| 449 | return err |
| 450 | } |
| 451 | ioinfo, err := s.identifyNniIoinfo("inside") |
| 452 | if err != nil { |
| 453 | return err |
| 454 | } |
| 455 | handle := ioinfo.handler |
| 456 | SendNni(handle, poppkt) |
| 457 | return nil |
| 458 | } |
| 459 | |
| 460 | func (s *Server) IsAllONUActive() bool { |
| 461 | for _, onus := range s.Onumap { |
| 462 | for _, onu := range onus { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 463 | if *onu.InternalState != device.ONU_ACTIVATED { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 464 | return false |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | return true |
| 469 | } |
| 470 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 471 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
| 472 | idx := uint32(0) |
| 473 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 474 | //return uint32(1032 + 8 * (vid - 1)), nil |
| 475 | } |
| 476 | |
| 477 | func (s *Server) getOnuBySN(sn *openolt.SerialNumber) (*device.Onu, error) { |
| 478 | for _, onus := range s.Onumap { |
| 479 | for _, onu := range onus { |
| 480 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 481 | return onu, nil |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | err := errors.New("No mathced SN is found !") |
| 486 | log.Println(err) |
| 487 | return nil, err |
| 488 | } |
| 489 | |
| 490 | func (s *Server) getOnuByID(onuid uint32) (*device.Onu, error) { |
| 491 | for _, onus := range s.Onumap { |
| 492 | for _, onu := range onus { |
| 493 | if onu.OnuID == onuid { |
| 494 | return onu, nil |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | err := errors.New("No matched OnuID is found !") |
| 499 | log.Println(err) |
| 500 | return nil, err |
| 501 | } |
| 502 | |
| 503 | func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) { |
| 504 | upif = setup.UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 505 | dwif = setup.UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid)) |
| 506 | return |
| 507 | } |
| 508 | |
| 509 | func makeNniName(oltid uint32) (upif string, dwif string) { |
| 510 | upif = setup.NNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) |
| 511 | dwif = setup.NNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) |
| 512 | return |
| 513 | } |
| 514 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 515 | func cleanUpVeths(vethenv []string) error { |
| 516 | if len(vethenv) > 0 { |
| 517 | log.Println("cleanUp veths !") |
| 518 | setup.TearVethDown(vethenv) |
| 519 | } |
| 520 | return nil |
| 521 | } |
| 522 | |
| 523 | func killProcesses(pnames []string) error { |
| 524 | for _, pname := range pnames { |
| 525 | setup.KillProcess(pname) |
| 526 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 527 | return nil |
| 528 | } |
| 529 | |
| 530 | func setupVethHandler(inveth string, outveth string, vethenv []string) (*pcap.Handle, []string, error) { |
| 531 | log.Printf("setupVethHandler: %s and %s\n", inveth, outveth) |
| 532 | err1 := setup.CreateVethPairs(inveth, outveth) |
| 533 | vethenv = append(vethenv, inveth) |
| 534 | if err1 != nil { |
| 535 | setup.RemoveVeths(vethenv) |
| 536 | return nil, vethenv, err1 |
| 537 | } |
| 538 | handler, err2 := getVethHandler(inveth) |
| 539 | if err2 != nil { |
| 540 | setup.RemoveVeths(vethenv) |
| 541 | return nil, vethenv, err2 |
| 542 | } |
| 543 | return handler, vethenv, nil |
| 544 | } |
| 545 | |
| 546 | func getVethHandler(vethname string) (*pcap.Handle, error) { |
| 547 | var ( |
| 548 | device string = vethname |
| 549 | snapshot_len int32 = 1518 |
| 550 | promiscuous bool = false |
| 551 | err error |
| 552 | timeout time.Duration = pcap.BlockForever |
| 553 | ) |
| 554 | handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout) |
| 555 | if err != nil { |
| 556 | return nil, err |
| 557 | } |
| 558 | log.Printf("Server handle created for %s\n", vethname) |
| 559 | return handle, nil |
| 560 | } |