-
Notifications
You must be signed in to change notification settings - Fork 37
/
ip.go
171 lines (153 loc) · 4.81 KB
/
ip.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
// Copyright (C) 2019 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 vpplink
import (
"fmt"
"io"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/interface_types"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/ip"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/ip_types"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/punt"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/types"
)
func (v *VppLink) ListVRFs() (vrfs []types.VRF, err error) {
client := ip.NewServiceClient(v.GetConnection())
stream, err := client.IPTableDump(v.GetContext(), &ip.IPTableDump{})
if err != nil {
return nil, fmt.Errorf("failed to dump VRF: %w", err)
}
vrfs = make([]types.VRF, 0)
for {
response, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to dump VRF: %w", err)
}
vrfs = append(vrfs, types.VRF{
Name: response.Table.Name,
VrfID: response.Table.TableID,
IsIP6: response.Table.IsIP6,
})
}
return vrfs, nil
}
func (v *VppLink) addDelVRF(index uint32, name string, isIP6 bool, isAdd bool) error {
client := ip.NewServiceClient(v.GetConnection())
_, err := client.IPTableAddDel(v.GetContext(), &ip.IPTableAddDel{
IsAdd: isAdd,
Table: ip.IPTable{
TableID: index,
IsIP6: isIP6,
Name: name,
},
})
if err != nil {
return fmt.Errorf("IPTableAddDel failed: %w", err)
}
return nil
}
func (v *VppLink) AddVRF(index uint32, isIP6 bool, name string) error {
return v.addDelVRF(index, name, isIP6, true /*isAdd*/)
}
func (v *VppLink) DelVRF(index uint32, isIP6 bool) error {
return v.addDelVRF(index, "", isIP6, false /*isAdd*/)
}
func (v *VppLink) AllocateVRF(isIP6 bool, name string) (uint32, error) {
client := ip.NewServiceClient(v.GetConnection())
response, err := client.IPTableAllocate(v.GetContext(), &ip.IPTableAllocate{
Table: ip.IPTable{
TableID: types.InvalidID,
IsIP6: isIP6,
Name: name,
},
})
if err != nil {
return 0, fmt.Errorf("IPTableAllocate failed: %w", err)
}
return response.Table.TableID, nil
}
func (v *VppLink) PuntRedirect(punt types.IpPuntRedirect, isIP6 bool) error {
client := ip.NewServiceClient(v.GetConnection())
_, err := client.AddDelIPPuntRedirectV2(v.GetContext(), &ip.AddDelIPPuntRedirectV2{
Punt: ip.PuntRedirectV2{
RxSwIfIndex: interface_types.InterfaceIndex(punt.RxSwIfIndex),
Af: types.GetBoolIPFamily(isIP6),
Paths: types.ToFibPathList(punt.Paths, isIP6),
},
IsAdd: true,
})
if err != nil {
return fmt.Errorf("failed to set punt in VPP: %v", err)
}
return nil
}
func (v *VppLink) PuntRedirectList(swIfIndex uint32, isIP6 bool) (punts []types.IpPuntRedirect, err error) {
client := ip.NewServiceClient(v.GetConnection())
stream, err := client.IPPuntRedirectV2Dump(v.GetContext(), &ip.IPPuntRedirectV2Dump{
SwIfIndex: interface_types.InterfaceIndex(swIfIndex),
Af: types.GetBoolIPFamily(isIP6),
})
if err != nil {
return nil, fmt.Errorf("failed to dump punt redirect: %w", err)
}
punts = make([]types.IpPuntRedirect, 0)
for {
response, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to dump punt redirect: %w", err)
}
punts = append(punts, types.IpPuntRedirect{
RxSwIfIndex: uint32(response.Punt.RxSwIfIndex),
IsIP6: response.Punt.Af == ip_types.ADDRESS_IP6,
Paths: types.FromFibPathList(response.Punt.Paths),
})
}
return punts, nil
}
// PuntL4 configures L4 punt for a given address family and protocol. port = ~0 means all ports
func (v *VppLink) PuntL4(proto types.IPProto, port uint16, isIPv6 bool) error {
client := punt.NewServiceClient(v.GetConnection())
_, err := client.SetPunt(v.GetContext(), &punt.SetPunt{
IsAdd: true,
Punt: punt.Punt{
Type: punt.PUNT_API_TYPE_L4,
Punt: punt.PuntUnionL4(punt.PuntL4{
Af: types.ToVppAddressFamily(isIPv6),
Protocol: types.ToVppIPProto(proto),
Port: port,
}),
},
})
if err != nil {
return fmt.Errorf("failed to set punt in VPP: %v", err)
}
return nil
}
func (v *VppLink) PuntAllL4(isIPv6 bool) (err error) {
err = v.PuntL4(types.TCP, 0xffff, isIPv6)
if err != nil {
return err
}
err = v.PuntL4(types.UDP, 0xffff, isIPv6)
if err != nil {
return err
}
return nil
}