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 | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 20 | "errors" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 21 | "gerrit.opencord.org/voltha-bbsim/common" |
| 22 | "github.com/google/gopacket/pcap" |
| 23 | "os/exec" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type Ioinfo struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 27 | Name string |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 28 | iotype string //nni or uni |
| 29 | ioloc string //inside or outsode |
| 30 | intfid uint32 |
| 31 | onuid uint32 |
| 32 | handler *pcap.Handle |
| 33 | } |
| 34 | |
| 35 | func (s *Server) identifyUniIoinfo(ioloc string, intfid uint32, onuid uint32) (*Ioinfo, error) { |
| 36 | for _, ioinfo := range s.Ioinfos { |
| 37 | if ioinfo.iotype == "uni" && ioinfo.intfid == intfid && ioinfo.onuid == onuid && ioinfo.ioloc == ioloc { |
| 38 | return ioinfo, nil |
| 39 | } |
| 40 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 41 | err := errors.New("No matched Ioinfo is found") |
| 42 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 43 | return nil, err |
| 44 | } |
| 45 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 46 | func (s *Server) IdentifyNniIoinfo(ioloc string) (*Ioinfo, error) { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 47 | for _, ioinfo := range s.Ioinfos { |
| 48 | if ioinfo.iotype == "nni" && ioinfo.ioloc == ioloc { |
| 49 | return ioinfo, nil |
| 50 | } |
| 51 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 52 | err := errors.New("No matched Ioinfo is found") |
| 53 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 54 | return nil, err |
| 55 | } |
| 56 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 57 | func (s *Server) GetUniIoinfos(ioloc string) ([]*Ioinfo, error) { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 58 | ioinfos := []*Ioinfo{} |
| 59 | for _, ioinfo := range s.Ioinfos { |
| 60 | if ioinfo.iotype == "uni" && ioinfo.ioloc == ioloc { |
| 61 | ioinfos = append(ioinfos, ioinfo) |
| 62 | } |
| 63 | } |
| 64 | if len(ioinfos) == 0 { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 65 | err := errors.New("No matched Ioinfo is found") |
| 66 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 67 | return nil, err |
| 68 | } |
| 69 | return ioinfos, nil |
| 70 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 71 | |
| 72 | func CreateVethPairs(name1 string, name2 string) (err error) { |
| 73 | err = exec.Command("ip", "link", "add", name1, "type", "veth", "peer", "name", name2).Run() |
| 74 | if err != nil { |
| 75 | logger.Error("Fail to createVeth() for %s and %s veth creation error: %s\n", name1, name2, err.Error()) |
| 76 | return |
| 77 | } |
| 78 | logger.Info("%s & %s was created.", name1, name2) |
| 79 | err = exec.Command("ip", "link", "set", name1, "up").Run() |
| 80 | if err != nil { |
| 81 | logger.Error("Fail to createVeth() veth1 up", err) |
| 82 | return |
| 83 | } |
| 84 | err = exec.Command("ip", "link", "set", name2, "up").Run() |
| 85 | if err != nil { |
| 86 | logger.Error("Fail to createVeth() veth2 up", err) |
| 87 | return |
| 88 | } |
| 89 | logger.Info("%s & %s was up.", name1, name2) |
| 90 | return |
| 91 | } |
| 92 | |
| 93 | func RemoveVeth(name string) error { |
| 94 | err := exec.Command("ip", "link", "del", name).Run() |
| 95 | if err != nil { |
| 96 | logger.Error("Fail to removeVeth()", err) |
| 97 | } |
| 98 | logger.Info("%s was removed.", name) |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | func RemoveVeths(names []string) { |
| 103 | for _, name := range names { |
| 104 | RemoveVeth(name) |
| 105 | } |
| 106 | logger.Info("RemoveVeths() :%s\n", names) |
| 107 | return |
| 108 | } |