blob: e56d4b6a006f04843cfe430b35bfe997d3d5848f [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 "os/exec"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022
23 "gerrit.opencord.org/voltha-bbsim/common/logger"
24 "github.com/google/gopacket/pcap"
25 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090026)
27
28type Ioinfo struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090029 Name string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090030 iotype string //nni or uni
31 ioloc string //inside or outsode
32 intfid uint32
33 onuid uint32
34 handler *pcap.Handle
35}
36
37func (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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +090043 err := errors.New("No matched Ioinfo is found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080044 logger.Error("identifyUniIoinfo %s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090045 return nil, err
46}
47
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090048func (s *Server) IdentifyNniIoinfo(ioloc string) (*Ioinfo, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 for _, ioinfo := range s.Ioinfos {
50 if ioinfo.iotype == "nni" && ioinfo.ioloc == ioloc {
51 return ioinfo, nil
52 }
53 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090054 err := errors.New("No matched Ioinfo is found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080055 logger.Error("IdentifyNniIoinfo %s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056 return nil, err
57}
58
Matteo Scandolo88e91892018-11-06 16:29:19 -080059func CreateVethPairs(veth1 string, veth2 string) (err error) {
60 err = exec.Command("ip", "link", "add", veth1, "type", "veth", "peer", "name", veth2).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090061 if err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -080062 logger.WithFields(log.Fields{
63 "veth1": veth1,
64 "veth2": veth2,
65 }).Error("Fail to createVethPair()", err.Error())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090066 return
67 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080068 logger.Info("%s & %s was created.", veth1, veth2)
69 err = exec.Command("ip", "link", "set", veth1, "up").Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090070 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -070071 logger.Error("Fail to createVeth() veth1 up: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090072 return
73 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080074 err = exec.Command("ip", "link", "set", veth2, "up").Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090075 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -070076 logger.Error("Fail to createVeth() veth2 up: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090077 return
78 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080079 logger.Info("%s & %s was up.", veth1, veth2)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080 return
81}
82
83func RemoveVeth(name string) error {
84 err := exec.Command("ip", "link", "del", name).Run()
85 if err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -080086 logger.WithField("veth", name).Error("Fail to removeVeth()", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090087 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080088 logger.WithField("veth", name).Info("Veth was removed.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090089 return err
90}
91
92func RemoveVeths(names []string) {
93 for _, name := range names {
94 RemoveVeth(name)
95 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080096 logger.WithField("veths", names).Info("RemoveVeths(): ")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097 return
98}