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 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 28 | // Ioinfo represents the input/output |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | type Ioinfo struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 30 | Name string |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | iotype string // nni or uni |
| 32 | ioloc string // inside or outside |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 33 | intfid uint32 |
| 34 | onuid uint32 |
| 35 | handler *pcap.Handle |
| 36 | } |
| 37 | |
| 38 | func (s *Server) identifyUniIoinfo(ioloc string, intfid uint32, onuid uint32) (*Ioinfo, error) { |
| 39 | for _, ioinfo := range s.Ioinfos { |
| 40 | if ioinfo.iotype == "uni" && ioinfo.intfid == intfid && ioinfo.onuid == onuid && ioinfo.ioloc == ioloc { |
| 41 | return ioinfo, nil |
| 42 | } |
| 43 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 44 | err := errors.New("No matched Ioinfo is found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 45 | logger.Error("identifyUniIoinfo %s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 46 | return nil, err |
| 47 | } |
| 48 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 49 | // IdentifyNniIoinfo returns matched ioinfo |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 50 | func (s *Server) IdentifyNniIoinfo(ioloc string) (*Ioinfo, error) { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | for _, ioinfo := range s.Ioinfos { |
| 52 | if ioinfo.iotype == "nni" && ioinfo.ioloc == ioloc { |
| 53 | return ioinfo, nil |
| 54 | } |
| 55 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 56 | err := errors.New("No matched Ioinfo is found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 57 | logger.Error("IdentifyNniIoinfo %s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 58 | return nil, err |
| 59 | } |
| 60 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 61 | // CreateVethPairs creates veth pairs with given names |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 62 | func CreateVethPairs(veth1 string, veth2 string) (err error) { |
| 63 | 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] | 64 | if err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 65 | logger.WithFields(log.Fields{ |
| 66 | "veth1": veth1, |
| 67 | "veth2": veth2, |
| 68 | }).Error("Fail to createVethPair()", err.Error()) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 69 | return |
| 70 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 71 | logger.Info("%s & %s was created.", veth1, veth2) |
| 72 | err = exec.Command("ip", "link", "set", veth1, "up").Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 73 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 74 | logger.Error("Fail to createVeth() veth1 up: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 75 | return |
| 76 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 77 | err = exec.Command("ip", "link", "set", veth2, "up").Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 78 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 79 | logger.Error("Fail to createVeth() veth2 up: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 80 | return |
| 81 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 82 | logger.Info("%s & %s was up.", veth1, veth2) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 83 | return |
| 84 | } |
| 85 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 86 | // RemoveVeth deletes veth by given name |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 87 | func RemoveVeth(name string) error { |
| 88 | err := exec.Command("ip", "link", "del", name).Run() |
| 89 | if err != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 90 | logger.WithField("veth", name).Error("Fail to removeVeth()", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 91 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 92 | logger.WithField("veth", name).Info("Veth was removed.") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 93 | return err |
| 94 | } |
| 95 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 96 | // RemoveVeths deletes veth |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 97 | func RemoveVeths(names []string) { |
| 98 | for _, name := range names { |
| 99 | RemoveVeth(name) |
| 100 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 101 | logger.WithField("veths", names).Info("RemoveVeths(): ") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 102 | return |
| 103 | } |