Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add l2xconnect chain element #10

Merged
merged 1 commit into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions pkg/networkservice/xconnect/l2xconnect/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 l2xconnect

import (
"context"

"git.fd.io/govpp.git/core"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"google.golang.org/grpc"
)

type l2XConnectServer struct {
vppConn *core.Connection
}

// NewClient returns a Client chain element that will cross connect a client and server vpp interface (if present)
func NewClient(vppConn *core.Connection) networkservice.NetworkServiceClient {
return &l2XConnectServer{
vppConn: vppConn,
}
}

func (v *l2XConnectServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

if err := addDel(ctx, v.vppConn, true); err != nil {
_, _ = v.Close(ctx, conn, opts...)
return nil, err
}
return conn, nil
}

func (v *l2XConnectServer) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
rv, err := next.Client(ctx).Close(ctx, conn, opts...)
if err != nil {
return nil, err
}
if err := addDel(ctx, v.vppConn, false); err != nil {
return nil, err
}
return rv, nil
}
71 changes: 71 additions & 0 deletions pkg/networkservice/xconnect/l2xconnect/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 l2xconnect

import (
"context"
"time"

"git.fd.io/govpp.git/api"
"github.com/edwarnicke/govpp/binapi/l2"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace"
"github.com/pkg/errors"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

func addDel(ctx context.Context, vppConn api.Connection, addDel bool) error {
clientIfIndex, ok := ifindex.Load(ctx, true)
if !ok {
return nil
}
serverIfIndex, ok := ifindex.Load(ctx, false)
if !ok {
return nil
}

now := time.Now()
if _, err := l2.NewServiceClient(vppConn).SwInterfaceSetL2Xconnect(ctx, &l2.SwInterfaceSetL2Xconnect{
RxSwIfIndex: clientIfIndex,
TxSwIfIndex: serverIfIndex,
Enable: addDel,
}); err != nil {
return errors.WithStack(err)
}
trace.Log(ctx).
WithField("RxSwIfIndex", clientIfIndex).
WithField("TxSwIfIndex", serverIfIndex).
WithField("Enable", addDel).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceSetL2Xconnect").Debug("completed")

now = time.Now()
if _, err := l2.NewServiceClient(vppConn).SwInterfaceSetL2Xconnect(ctx, &l2.SwInterfaceSetL2Xconnect{
RxSwIfIndex: serverIfIndex,
TxSwIfIndex: clientIfIndex,
Enable: addDel,
}); err != nil {
return errors.WithStack(err)
}
trace.Log(ctx).
WithField("RxSwIfIndex", serverIfIndex).
WithField("TxSwIfIndex", clientIfIndex).
WithField("Enable", addDel).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceSetL2Xconnect").Debug("completed")
return nil
}
18 changes: 18 additions & 0 deletions pkg/networkservice/xconnect/l2xconnect/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 l2xconnect provides chain elements for creating an l2 cross connect in vpp between client and server interfaces (if present)
package l2xconnect
56 changes: 56 additions & 0 deletions pkg/networkservice/xconnect/l2xconnect/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 l2xconnect

import (
"context"

"git.fd.io/govpp.git/api"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type l2XconnectServer struct {
vppConn api.Connection
}

// NewServer returns a Server chain element that will cross connect a client and server vpp interface (if present)
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
return &l2XconnectServer{
vppConn: vppConn,
}
}

func (v *l2XconnectServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if err := addDel(ctx, v.vppConn, true); err != nil {
return nil, err
}
conn, err := next.Server(ctx).Request(ctx, request)
if err != nil {
_ = addDel(ctx, v.vppConn, false)
return nil, err
}
return conn, nil
}

func (v *l2XconnectServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
if err := addDel(ctx, v.vppConn, false); err != nil {
return nil, err
}
return next.Server(ctx).Close(ctx, conn)
}