-
Notifications
You must be signed in to change notification settings - Fork 37
/
vpp.go
104 lines (91 loc) · 2.32 KB
/
vpp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Code generated by vpplink DO NOT EDIT.
// Copyright (C) 2021 Cisco Systems Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generated
import (
"context"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
govpp "go.fd.io/govpp"
vppapi "go.fd.io/govpp/api"
vppcore "go.fd.io/govpp/core"
)
const (
DefaultReplyTimeout = 1 * time.Second
InvalidSwIfIndex = ^uint32(1)
)
// Vpp is the base struct that exposes all the methods defined
// by the various wrappers.
// Depending on the available APIs, this struct will implement
// the various interfaces defined in go.fd.io/govpp/api/v1
type Vpp struct {
lock sync.Mutex
conn *vppcore.Connection
ch vppapi.Channel
socket string
log *logrus.Entry
ctx context.Context
}
func (v *Vpp) GetLog() *logrus.Entry {
return v.log
}
func (v *Vpp) GetContext() context.Context {
return v.ctx
}
func (v *Vpp) GetConnection() vppapi.Connection {
return v.conn
}
func NewVpp(socket string, logger *logrus.Entry) (*Vpp, error) {
conn, err := govpp.Connect(socket)
if err != nil {
return nil, errors.Wrapf(err, "cannot connect to VPP on socket %s", socket)
}
ch, err := conn.NewAPIChannel()
if err != nil {
return nil, errors.Wrap(err, "channel creation failed")
}
return &Vpp{
conn: conn,
ch: ch,
socket: socket,
log: logger,
ctx: context.Background(),
}, nil
}
func (v *Vpp) Reconnect() (err error) {
v.conn, err = govpp.Connect(v.socket)
if err != nil {
return errors.Wrapf(err, "cannot re-connect to VPP on socket %s", v.socket)
}
v.ch, err = v.conn.NewAPIChannel()
if err != nil {
return errors.Wrap(err, "channel re-creation failed")
}
return nil
}
func (v *Vpp) Close() error {
if v == nil {
return nil
}
if v.ch != nil {
v.ch.Close()
}
if v.conn != nil {
v.conn.Disconnect()
}
return nil
}