blob: caad5ef0fbf6306fe78be3ff10e1aae9061056f3 [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
Matteo Scandolo88e91892018-11-06 16:29:19 -080023 "github.com/google/gopacket/pcap"
Zack Williams2abf3932019-08-05 14:07:05 -070024 "github.com/opencord/voltha-bbsim/common/logger"
Matteo Scandolo88e91892018-11-06 16:29:19 -080025 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090026)
27
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020028// Ioinfo represents the input/output
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029type Ioinfo struct {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090030 Name string
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020031 iotype string // nni or uni
32 ioloc string // inside or outside
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 intfid uint32
34 onuid uint32
35 handler *pcap.Handle
36}
37
38func (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 }
Zdravko Bozakov078a2712019-07-19 23:25:15 +020044 err := errors.New("no matched Ioinfo is found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080045 logger.Error("identifyUniIoinfo %s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046 return nil, err
47}
48
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020049// IdentifyNniIoinfo returns matched ioinfo
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090050func (s *Server) IdentifyNniIoinfo(ioloc string) (*Ioinfo, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 for _, ioinfo := range s.Ioinfos {
52 if ioinfo.iotype == "nni" && ioinfo.ioloc == ioloc {
53 return ioinfo, nil
54 }
55 }
Zdravko Bozakov078a2712019-07-19 23:25:15 +020056 err := errors.New("no matched Ioinfo is found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080057 logger.Error("IdentifyNniIoinfo %s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090058 return nil, err
59}
60
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020061// CreateVethPairs creates veth pairs with given names
Matteo Scandolo88e91892018-11-06 16:29:19 -080062func CreateVethPairs(veth1 string, veth2 string) (err error) {
63 err = exec.Command("ip", "link", "add", veth1, "type", "veth", "peer", "name", veth2).Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090064 if err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -080065 logger.WithFields(log.Fields{
66 "veth1": veth1,
67 "veth2": veth2,
68 }).Error("Fail to createVethPair()", err.Error())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090069 return
70 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080071 logger.Info("%s & %s was created.", veth1, veth2)
72 err = exec.Command("ip", "link", "set", veth1, "up").Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090073 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -070074 logger.Error("Fail to createVeth() veth1 up: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090075 return
76 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080077 err = exec.Command("ip", "link", "set", veth2, "up").Run()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090078 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -070079 logger.Error("Fail to createVeth() veth2 up: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090080 return
81 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080082 logger.Info("%s & %s was up.", veth1, veth2)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090083 return
84}
85
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020086// RemoveVeth deletes veth by given name
Zdravko Bozakov078a2712019-07-19 23:25:15 +020087func RemoveVeth(name string) {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090088 err := exec.Command("ip", "link", "del", name).Run()
89 if err != nil {
Matteo Scandolo88e91892018-11-06 16:29:19 -080090 logger.WithField("veth", name).Error("Fail to removeVeth()", err)
Zdravko Bozakov078a2712019-07-19 23:25:15 +020091 return
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090092 }
Matteo Scandolo88e91892018-11-06 16:29:19 -080093 logger.WithField("veth", name).Info("Veth was removed.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090094}
95
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020096// RemoveVeths deletes veth
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090097func RemoveVeths(names []string) {
98 for _, name := range names {
99 RemoveVeth(name)
100 }
Matteo Scandolo88e91892018-11-06 16:29:19 -0800101 logger.WithField("veths", names).Info("RemoveVeths(): ")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900102 return
103}