This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_vppcalls.go
133 lines (119 loc) · 4.1 KB
/
watch_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2019 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 vpp1908
import (
"bytes"
"fmt"
"net"
"os"
"time"
govppapi "git.fd.io/govpp.git/api"
"github.com/pkg/errors"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp1908/dhcp"
binapi_interfaces "github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp1908/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls"
)
var InterfaceEventTimeout = time.Second
func (h *InterfaceVppHandler) WatchInterfaceEvents(events chan<- *vppcalls.InterfaceEvent) error {
notifChan := make(chan govppapi.Message, 10)
// subscribe for receiving SwInterfaceEvents notifications
vppNotifSubs, err := h.callsChannel.SubscribeNotification(notifChan, &binapi_interfaces.SwInterfaceEvent{})
if err != nil {
return errors.Errorf("failed to subscribe VPP notification (sw_interface_event): %v", err)
}
_ = vppNotifSubs
go func() {
for {
select {
case e, ok := <-notifChan:
if !ok {
h.log.Debugf("interface notification channel was closed")
return
}
ifEvent, ok := e.(*binapi_interfaces.SwInterfaceEvent)
if !ok {
continue
}
event := &vppcalls.InterfaceEvent{
SwIfIndex: ifEvent.SwIfIndex,
AdminState: ifEvent.AdminUpDown,
LinkState: ifEvent.LinkUpDown,
Deleted: ifEvent.Deleted != 0,
}
// send event in goroutine for quick processing
go func() {
select {
case events <- event:
// sent ok
case <-time.After(InterfaceEventTimeout):
h.log.Warnf("unable to deliver interface event, dropping it")
}
}()
}
}
}()
// enable interface state notifications from VPP
wantIfEventsReply := &binapi_interfaces.WantInterfaceEventsReply{}
err = h.callsChannel.SendRequest(&binapi_interfaces.WantInterfaceEvents{
PID: uint32(os.Getpid()),
EnableDisable: 1,
}).ReceiveReply(wantIfEventsReply)
if err != nil {
if err == govppapi.VPPApiError(govppapi.INVALID_REGISTRATION) {
h.log.Warnf("already registered for watch interface events: %v", err)
return nil
}
return errors.Errorf("failed to watch interface events: %v", err)
}
return nil
}
func (h *InterfaceVppHandler) WatchDHCPLeases(leasesCh chan<- *vppcalls.Lease) error {
notifChan := make(chan govppapi.Message)
// subscribe for receiving SwInterfaceEvents notifications
vppNotifSubs, err := h.callsChannel.SubscribeNotification(notifChan, &dhcp.DHCPComplEvent{})
if err != nil {
return errors.Errorf("failed to subscribe VPP notification (sw_interface_event): %v", err)
}
_ = vppNotifSubs
go func() {
for {
select {
case e := <-notifChan:
dhcpEvent, ok := e.(*dhcp.DHCPComplEvent)
if !ok {
continue
}
lease := dhcpEvent.Lease
var hostAddr, routerAddr string
if uintToBool(lease.IsIPv6) {
hostAddr = fmt.Sprintf("%s/%d", net.IP(lease.HostAddress).To16().String(), uint32(lease.MaskWidth))
routerAddr = fmt.Sprintf("%s/%d", net.IP(lease.RouterAddress).To16().String(), uint32(lease.MaskWidth))
} else {
hostAddr = fmt.Sprintf("%s/%d", net.IP(lease.HostAddress[:4]).To4().String(), uint32(lease.MaskWidth))
routerAddr = fmt.Sprintf("%s/%d", net.IP(lease.RouterAddress[:4]).To4().String(), uint32(lease.MaskWidth))
}
leasesCh <- &vppcalls.Lease{
SwIfIndex: lease.SwIfIndex,
State: lease.State,
Hostname: string(bytes.SplitN(lease.Hostname, []byte{0x00}, 2)[0]),
IsIPv6: uintToBool(lease.IsIPv6),
HostAddress: hostAddr,
RouterAddress: routerAddr,
HostMac: net.HardwareAddr(lease.HostMac).String(),
}
}
}
}()
return nil
}