blob: 8f6528081883ee923d095d7ababdff97b35d0757 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package core
18
19import (
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090020 "errors"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090021 "gerrit.opencord.org/voltha-bbsim/common"
22 "github.com/google/gopacket/pcap"
23 "os/exec"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090024)
25
26type Ioinfo struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090027 Name string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028 iotype string //nni or uni
29 ioloc string //inside or outsode
30 intfid uint32
31 onuid uint32
32 handler *pcap.Handle
33}
34
35func (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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +090041 err := errors.New("No matched Ioinfo is found")
42 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090043 return nil, err
44}
45
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090046func (s *Server) IdentifyNniIoinfo(ioloc string) (*Ioinfo, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047 for _, ioinfo := range s.Ioinfos {
48 if ioinfo.iotype == "nni" && ioinfo.ioloc == ioloc {
49 return ioinfo, nil
50 }
51 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090052 err := errors.New("No matched Ioinfo is found")
53 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 return nil, err
55}
56
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090057func (s *Server) GetUniIoinfos(ioloc string) ([]*Ioinfo, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090058 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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +090065 err := errors.New("No matched Ioinfo is found")
66 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067 return nil, err
68 }
69 return ioinfos, nil
70}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090071
72func 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
93func 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
102func RemoveVeths(names []string) {
103 for _, name := range names {
104 RemoveVeth(name)
105 }
106 logger.Info("RemoveVeths() :%s\n", names)
107 return
108}