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