-
Notifications
You must be signed in to change notification settings - Fork 127
/
punt_to_host.go
181 lines (153 loc) · 5.48 KB
/
punt_to_host.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2018 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 descriptor
import (
"errors"
"github.com/ligato/cn-infra/logging"
punt "github.com/ligato/vpp-agent/api/models/vpp/punt"
"github.com/ligato/vpp-agent/pkg/models"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/vpp/puntplugin/descriptor/adapter"
"github.com/ligato/vpp-agent/plugins/vpp/puntplugin/vppcalls"
)
const (
// PuntToHostDescriptorName is the name of the descriptor for the VPP punt to host/socket
PuntToHostDescriptorName = "vpp-punt-to-host"
)
// A list of non-retriable errors:
var (
// ErrPuntWithoutL3Protocol is returned when VPP punt has undefined L3 protocol.
ErrPuntWithoutL3Protocol = errors.New("VPP punt defined without L3 protocol")
// ErrPuntWithoutL4Protocol is returned when VPP punt has undefined L4 protocol.
ErrPuntWithoutL4Protocol = errors.New("VPP punt defined without L4 protocol")
// ErrPuntWithoutPort is returned when VPP punt has undefined port.
ErrPuntWithoutPort = errors.New("VPP punt defined without port")
)
// PuntToHostDescriptor teaches KVScheduler how to configure VPP punt to host or unix domain socket.
type PuntToHostDescriptor struct {
RegisterSocketFn func(register bool, toHost *punt.ToHost, socketPath string)
// dependencies
log logging.Logger
puntHandler vppcalls.PuntVppAPI
}
// NewPuntToHostDescriptor creates a new instance of the punt to host descriptor.
func NewPuntToHostDescriptor(puntHandler vppcalls.PuntVppAPI, log logging.LoggerFactory) *PuntToHostDescriptor {
return &PuntToHostDescriptor{
log: log.NewLogger("punt-to-host-descriptor"),
puntHandler: puntHandler,
}
}
// GetDescriptor returns descriptor suitable for registration (via adapter) with
// the KVScheduler.
func (d *PuntToHostDescriptor) GetDescriptor() *adapter.PuntToHostDescriptor {
return &adapter.PuntToHostDescriptor{
Name: PuntToHostDescriptorName,
NBKeyPrefix: punt.ModelToHost.KeyPrefix(),
ValueTypeName: punt.ModelToHost.ProtoName(),
KeySelector: punt.ModelToHost.IsKeyValid,
KeyLabel: punt.ModelToHost.StripKeyPrefix,
ValueComparator: d.EquivalentPuntToHost,
Validate: d.Validate,
Create: d.Create,
Delete: d.Delete,
Retrieve: d.Retrieve,
}
}
// EquivalentPuntToHost is case-insensitive comparison function for punt.ToHost.
func (d *PuntToHostDescriptor) EquivalentPuntToHost(key string, oldPunt, newPunt *punt.ToHost) bool {
if oldPunt.L3Protocol != newPunt.L3Protocol ||
oldPunt.L4Protocol != newPunt.L4Protocol ||
oldPunt.Port != newPunt.Port {
return false
}
return true
}
// Validate validates VPP punt configuration.
func (d *PuntToHostDescriptor) Validate(key string, puntCfg *punt.ToHost) error {
// validate L3 protocol
switch puntCfg.L3Protocol {
case punt.L3Protocol_IPv4:
case punt.L3Protocol_IPv6:
case punt.L3Protocol_ALL:
default:
return kvs.NewInvalidValueError(ErrPuntWithoutL3Protocol, "l3_protocol")
}
// validate L4 protocol
switch puntCfg.L4Protocol {
case punt.L4Protocol_TCP:
case punt.L4Protocol_UDP:
default:
return kvs.NewInvalidValueError(ErrPuntWithoutL4Protocol, "l4_protocol")
}
if puntCfg.Port == 0 {
return kvs.NewInvalidValueError(ErrPuntWithoutPort, "port")
}
return nil
}
// Create adds new punt to host entry or registers new punt to unix domain socket.
func (d *PuntToHostDescriptor) Create(key string, punt *punt.ToHost) (interface{}, error) {
// add punt to host
if punt.SocketPath == "" {
if err := d.puntHandler.AddPunt(punt); err != nil {
d.log.Error(err)
return nil, err
}
return nil, nil
}
// register punt to socket
pathname, err := d.puntHandler.RegisterPuntSocket(punt)
if err != nil {
d.log.Error(err)
return nil, err
}
if d.RegisterSocketFn != nil {
d.RegisterSocketFn(true, punt, pathname)
}
return nil, nil
}
// Delete removes VPP punt configuration.
func (d *PuntToHostDescriptor) Delete(key string, punt *punt.ToHost, metadata interface{}) error {
if punt.SocketPath == "" {
if err := d.puntHandler.DeletePunt(punt); err != nil {
d.log.Error(err)
return err
}
}
// deregister punt to socket
if err := d.puntHandler.DeregisterPuntSocket(punt); err != nil {
d.log.Error(err)
return err
}
if d.RegisterSocketFn != nil {
d.RegisterSocketFn(false, punt, "")
}
return nil
}
// Retrieve returns all configured VPP punt to host entries.
func (d *PuntToHostDescriptor) Retrieve(correlate []adapter.PuntToHostKVWithMetadata) (retrieved []adapter.PuntToHostKVWithMetadata, err error) {
// TODO dump for punt and punt socket register missing in api
d.log.Info("Dump punt/socket register is not supported by the VPP")
socks, err := d.puntHandler.DumpRegisteredPuntSockets()
if err != nil {
return nil, err
}
for _, punt := range socks {
retrieved = append(retrieved, adapter.PuntToHostKVWithMetadata{
Key: models.Key(punt.PuntData),
Value: punt.PuntData,
Origin: kvs.FromNB,
})
}
return retrieved, nil
}