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