forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_vppcalls.go
111 lines (92 loc) · 3.12 KB
/
ip_vppcalls.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
105
106
107
108
109
110
111
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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 vppcalls
import (
"fmt"
"net"
"time"
"github.com/ligato/cn-infra/utils/addrs"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/interfaces"
)
const (
addInterfaceIP uint8 = 1
delInterfaceIP uint8 = 0
)
func (h *IfVppHandler) addDelInterfaceIP(ifIdx uint32, addr *net.IPNet, isAdd uint8) error {
defer func(t time.Time) {
h.stopwatch.TimeLog(interfaces.SwInterfaceAddDelAddress{}).LogTimeEntry(time.Since(t))
}(time.Now())
req := &interfaces.SwInterfaceAddDelAddress{
SwIfIndex: ifIdx,
IsAdd: isAdd,
}
prefix, _ := addr.Mask.Size()
req.AddressLength = byte(prefix)
isIPv6, err := addrs.IsIPv6(addr.IP.String())
if err != nil {
return err
}
if isIPv6 {
req.Address = []byte(addr.IP.To16())
req.IsIPv6 = 1
} else {
req.Address = []byte(addr.IP.To4())
req.IsIPv6 = 0
}
reply := &interfaces.SwInterfaceAddDelAddressReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
} else if reply.Retval != 0 {
return fmt.Errorf("%s returned %d", reply.GetMessageName(), reply.Retval)
}
return nil
}
// AddInterfaceIP implements interface handler.
func (h *IfVppHandler) AddInterfaceIP(ifIdx uint32, addr *net.IPNet) error {
return h.addDelInterfaceIP(ifIdx, addr, addInterfaceIP)
}
// DelInterfaceIP implements interface handler.
func (h *IfVppHandler) DelInterfaceIP(ifIdx uint32, addr *net.IPNet) error {
return h.addDelInterfaceIP(ifIdx, addr, delInterfaceIP)
}
const (
setUnnumberedIP uint8 = 1
unsetUnnumberedIP uint8 = 0
)
func (h *IfVppHandler) setUnsetUnnumberedIP(uIfIdx uint32, ifIdxWithIP uint32, isAdd uint8) error {
defer func(t time.Time) {
h.stopwatch.TimeLog(interfaces.SwInterfaceSetUnnumbered{}).LogTimeEntry(time.Since(t))
}(time.Now())
// Prepare the message.
req := &interfaces.SwInterfaceSetUnnumbered{
SwIfIndex: ifIdxWithIP,
UnnumberedSwIfIndex: uIfIdx,
IsAdd: isAdd,
}
reply := &interfaces.SwInterfaceSetUnnumberedReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
} else if reply.Retval != 0 {
return fmt.Errorf("%s returned %d", reply.GetMessageName(), reply.Retval)
}
return nil
}
// SetUnnumberedIP implements interface handler.
func (h *IfVppHandler) SetUnnumberedIP(uIfIdx uint32, ifIdxWithIP uint32) error {
return h.setUnsetUnnumberedIP(uIfIdx, ifIdxWithIP, setUnnumberedIP)
}
// UnsetUnnumberedIP implements interface handler.
func (h *IfVppHandler) UnsetUnnumberedIP(uIfIdx uint32) error {
return h.setUnsetUnnumberedIP(uIfIdx, 0, unsetUnnumberedIP)
}