Skip to content

Commit f0d1aec

Browse files
committed
[PoC] Restore vpp interfaces
Signed-off-by: Artem Glazychev <[email protected]>
1 parent e09c347 commit f0d1aec

File tree

28 files changed

+898
-50
lines changed

28 files changed

+898
-50
lines changed

pkg/networkservice/chains/forwarder/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type forwarderOptions struct {
3636
authorizeServer networkservice.NetworkServiceServer
3737
clientURL *url.URL
3838
dialTimeout time.Duration
39+
dumpCleanTimeou time.Duration
3940
domain2Device map[string]string
4041
statsOpts []stats.Option
4142
cleanupOpts []cleanup.Option
@@ -77,6 +78,13 @@ func WithDialTimeout(dialTimeout time.Duration) Option {
7778
}
7879
}
7980

81+
// WithDumpCleanupTimeout sets
82+
func WithDumpCleanupTimeout(timeout time.Duration) Option {
83+
return func(o *forwarderOptions) {
84+
o.dumpCleanTimeou = timeout
85+
}
86+
}
87+
8088
// WithVlanDomain2Device sets vlan option
8189
func WithVlanDomain2Device(domain2Device map[string]string) Option {
8290
return func(o *forwarderOptions) {

pkg/networkservice/chains/forwarder/server.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"net/url"
2727
"time"
2828

29+
"github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
30+
2931
"git.fd.io/govpp.git/api"
3032
"github.com/google/uuid"
3133

@@ -81,11 +83,18 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, vppConn
8183
authorizeServer: authorize.NewServer(authorize.Any()),
8284
clientURL: &url.URL{Scheme: "unix", Host: "connect.to.socket"},
8385
dialTimeout: time.Millisecond * 200,
86+
dumpCleanTimeou: time.Minute,
8487
domain2Device: make(map[string]string),
8588
}
8689
for _, opt := range options {
8790
opt(opts)
8891
}
92+
93+
dumpOption := &dumptool.DumpOption{
94+
PodName: opts.name,
95+
Timeout: opts.dumpCleanTimeou,
96+
}
97+
8998
nseClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, opts.clientURL,
9099
registryclient.WithNSEAdditionalFunctionality(
91100
registryrecvfd.NewNetworkServiceEndpointRegistryClient(),
@@ -111,9 +120,10 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, vppConn
111120
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
112121
memif.MECHANISM: memif.NewServer(ctx, vppConn,
113122
memif.WithDirectMemif(),
114-
memif.WithChangeNetNS()),
115-
kernel.MECHANISM: kernel.NewServer(vppConn),
116-
vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP, opts.vxlanOpts...),
123+
memif.WithChangeNetNS(),
124+
memif.WithDump(dumpOption)),
125+
kernel.MECHANISM: kernel.NewServer(vppConn, kernel.WithDump(dumpOption)),
126+
vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP, append(opts.vxlanOpts, vxlan.WithDump(dumpOption))...),
117127
wireguard.MECHANISM: wireguard.NewServer(vppConn, tunnelIP),
118128
}),
119129
pinhole.NewServer(vppConn),
@@ -134,9 +144,10 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, vppConn
134144
// mechanisms
135145
memif.NewClient(vppConn,
136146
memif.WithChangeNetNS(),
147+
memif.WithDump(dumpOption),
137148
),
138-
kernel.NewClient(vppConn),
139-
vxlan.NewClient(vppConn, tunnelIP, opts.vxlanOpts...),
149+
kernel.NewClient(vppConn, kernel.WithDump(dumpOption)),
150+
vxlan.NewClient(vppConn, tunnelIP, append(opts.vxlanOpts, vxlan.WithDump(dumpOption))...),
140151
wireguard.NewClient(vppConn, tunnelIP),
141152
vlan.NewClient(vppConn, opts.domain2Device),
142153
filtermechanisms.NewClient(),

pkg/networkservice/mechanisms/kernel/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
1+
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -30,9 +30,14 @@ import (
3030
)
3131

3232
// NewClient - returns a new Client chain element implementing the kernel mechanism with vpp
33-
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
33+
func NewClient(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceClient {
34+
o := &options{}
35+
for _, opt := range opts {
36+
opt(o)
37+
}
38+
3439
if _, err := os.Stat(vnetFilename); err == nil {
35-
return kerneltap.NewClient(vppConn)
40+
return kerneltap.NewClient(vppConn, kerneltap.WithDump(o.dumpOpt))
3641
}
3742
return kernelvethpair.NewClient(vppConn)
3843
}

pkg/networkservice/mechanisms/kernel/kerneltap/client.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
1+
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/networkservicemesh/api/pkg/api/networkservice"
3030
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
31+
"github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
3132
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
3233
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
3334
"github.com/networkservicemesh/sdk/pkg/tools/log"
@@ -36,12 +37,30 @@ import (
3637

3738
type kernelTapClient struct {
3839
vppConn api.Connection
40+
dumpMap *dumptool.Map
3941
}
4042

4143
// NewClient - return a new Client chain element implementing the kernel mechanism with vpp using tapv2
42-
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
44+
func NewClient(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceClient {
45+
o := &options{}
46+
for _, opt := range opts {
47+
opt(o)
48+
}
49+
50+
ctx := context.Background()
51+
dumpMap := dumptool.NewMap(ctx, 0)
52+
if o.dumpOpt != nil {
53+
var err error
54+
dumpMap, err = dump(ctx, vppConn, o.dumpOpt.PodName, o.dumpOpt.Timeout, true)
55+
if err != nil {
56+
log.FromContext(ctx).Errorf("failed to Dump: %v", err)
57+
/* TODO: set empty dumpMap here? */
58+
}
59+
}
60+
4361
return &kernelTapClient{
4462
vppConn: vppConn,
63+
dumpMap: dumpMap,
4564
}
4665
}
4766

@@ -60,7 +79,7 @@ func (k *kernelTapClient) Request(ctx context.Context, request *networkservice.N
6079
return nil, err
6180
}
6281

63-
if err := create(ctx, conn, k.vppConn, metadata.IsClient(k)); err != nil {
82+
if err := create(ctx, conn, k.vppConn, k.dumpMap, metadata.IsClient(k)); err != nil {
6483
closeCtx, cancelClose := postponeCtxFunc()
6584
defer cancelClose()
6685

@@ -75,7 +94,7 @@ func (k *kernelTapClient) Request(ctx context.Context, request *networkservice.N
7594
}
7695

7796
func (k *kernelTapClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
78-
err := del(ctx, conn, k.vppConn, metadata.IsClient(k))
97+
err := del(ctx, conn, k.vppConn, k.dumpMap, metadata.IsClient(k))
7998
if err != nil {
8099
log.FromContext(ctx).Error(err)
81100
}

pkg/networkservice/mechanisms/kernel/kerneltap/common.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"context"
2323
"time"
2424

25+
"github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
26+
2527
"git.fd.io/govpp.git/api"
2628
interfaces "github.com/edwarnicke/govpp/binapi/interface"
2729
"github.com/edwarnicke/govpp/binapi/interface_types"
@@ -38,8 +40,11 @@ import (
3840
"github.com/networkservicemesh/sdk-vpp/pkg/tools/mechutils"
3941
)
4042

41-
func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient bool) error {
43+
func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, dumpMap *dumptool.Map, isClient bool) error {
4244
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil {
45+
if val, loaded := dumpMap.LoadAndDelete(conn.GetId()); loaded {
46+
ifindex.Store(ctx, isClient, val.(interface_types.InterfaceIndex))
47+
}
4348
// Construct the netlink handle for the target namespace for this kernel interface
4449
handle, err := kernellink.GetNetlinkHandle(mechanism.GetNetNSURL())
4550
if err != nil {
@@ -53,7 +58,7 @@ func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Co
5358
}
5459
}
5560
// Delete the kernel interface if there is one in the target namespace
56-
_ = del(ctx, conn, vppConn, isClient)
61+
_ = del(ctx, conn, vppConn, dumpMap, isClient)
5762

5863
nsFilename, err := mechutils.ToNSFilename(mechanism)
5964
if err != nil {
@@ -141,8 +146,11 @@ func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Co
141146
return nil
142147
}
143148

144-
func del(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient bool) error {
149+
func del(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, dumpMap *dumptool.Map, isClient bool) error {
145150
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil {
151+
if val, loaded := dumpMap.LoadAndDelete(conn.GetId()); loaded {
152+
ifindex.Store(ctx, isClient, val.(interface_types.InterfaceIndex))
153+
}
146154
swIfIndex, ok := ifindex.LoadAndDelete(ctx, isClient)
147155
if !ok {
148156
return nil
@@ -162,3 +170,21 @@ func del(ctx context.Context, conn *networkservice.Connection, vppConn api.Conne
162170
}
163171
return nil
164172
}
173+
174+
func dump(ctx context.Context, vppConn api.Connection, podName string, timeout time.Duration, isClient bool) (*dumptool.Map, error) {
175+
return dumptool.DumpInterfaces(ctx, vppConn, podName, timeout, isClient,
176+
/* Function on dump */
177+
func(details *interfaces.SwInterfaceDetails) (interface{}, error) {
178+
if details.InterfaceDevType == dumptool.DevTypeTap {
179+
return details.SwIfIndex, nil
180+
}
181+
return nil, errors.New("Doesn't match the tap interface")
182+
},
183+
/* Function on delete */
184+
func(ifindex interface{}) error {
185+
_, err := tapv2.NewServiceClient(vppConn).TapDeleteV2(ctx, &tapv2.TapDeleteV2{
186+
SwIfIndex: ifindex.(interface_types.InterfaceIndex),
187+
})
188+
return err
189+
})
190+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022 Cisco and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
// +build linux
18+
19+
package kerneltap
20+
21+
import "github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
22+
23+
type options struct {
24+
dumpOpt *dumptool.DumpOption
25+
}
26+
27+
// Option is an option pattern for kernel
28+
type Option func(o *options)
29+
30+
// WithDump - sets dump parameters
31+
func WithDump(dump *dumptool.DumpOption) Option {
32+
return func(o *options) {
33+
o.dumpOpt = dump
34+
}
35+
}

pkg/networkservice/mechanisms/kernel/kerneltap/server.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
1+
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -23,6 +23,7 @@ import (
2323

2424
"git.fd.io/govpp.git/api"
2525
"github.com/golang/protobuf/ptypes/empty"
26+
"github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
2627
"github.com/pkg/errors"
2728

2829
"github.com/networkservicemesh/api/pkg/api/networkservice"
@@ -34,12 +35,30 @@ import (
3435

3536
type kernelTapServer struct {
3637
vppConn api.Connection
38+
dumpMap *dumptool.Map
3739
}
3840

3941
// NewServer - return a new Server chain element implementing the kernel mechanism with vpp using tapv2
40-
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
42+
func NewServer(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceServer {
43+
o := &options{}
44+
for _, opt := range opts {
45+
opt(o)
46+
}
47+
48+
ctx := context.Background()
49+
dumpMap := dumptool.NewMap(ctx, 0)
50+
if o.dumpOpt != nil {
51+
var err error
52+
dumpMap, err = dump(ctx, vppConn, o.dumpOpt.PodName, o.dumpOpt.Timeout, false)
53+
if err != nil {
54+
log.FromContext(ctx).Errorf("failed to Dump: %v", err)
55+
/* TODO: set empty dumpMap here? */
56+
}
57+
}
58+
4159
return &kernelTapServer{
4260
vppConn: vppConn,
61+
dumpMap: dumpMap,
4362
}
4463
}
4564

@@ -51,7 +70,7 @@ func (k *kernelTapServer) Request(ctx context.Context, request *networkservice.N
5170
return nil, err
5271
}
5372

54-
if err := create(ctx, conn, k.vppConn, metadata.IsClient(k)); err != nil {
73+
if err := create(ctx, conn, k.vppConn, k.dumpMap, metadata.IsClient(k)); err != nil {
5574
closeCtx, cancelClose := postponeCtxFunc()
5675
defer cancelClose()
5776

@@ -66,7 +85,7 @@ func (k *kernelTapServer) Request(ctx context.Context, request *networkservice.N
6685
}
6786

6887
func (k *kernelTapServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
69-
err := del(ctx, conn, k.vppConn, metadata.IsClient(k))
88+
err := del(ctx, conn, k.vppConn, k.dumpMap, metadata.IsClient(k))
7089
if err != nil {
7190
log.FromContext(ctx).Error(err)
7291
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022 Cisco and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
// +build linux
18+
19+
package kernel
20+
21+
import "github.com/networkservicemesh/sdk-vpp/pkg/tools/dumptool"
22+
23+
type options struct {
24+
dumpOpt *dumptool.DumpOption
25+
}
26+
27+
// Option is an option pattern for kernel
28+
type Option func(o *options)
29+
30+
// WithDump - sets dump parameters
31+
func WithDump(dump *dumptool.DumpOption) Option {
32+
return func(o *options) {
33+
o.dumpOpt = dump
34+
}
35+
}

pkg/networkservice/mechanisms/kernel/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
1+
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -30,9 +30,14 @@ import (
3030
)
3131

3232
// NewServer return a NetworkServiceServer chain element that correctly handles the kernel Mechanism
33-
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
33+
func NewServer(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceServer {
34+
o := &options{}
35+
for _, opt := range opts {
36+
opt(o)
37+
}
38+
3439
if _, err := os.Stat(vnetFilename); err == nil {
35-
return kerneltap.NewServer(vppConn)
40+
return kerneltap.NewServer(vppConn, kerneltap.WithDump(o.dumpOpt))
3641
}
3742
return kernelvethpair.NewServer(vppConn)
3843
}

0 commit comments

Comments
 (0)