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 chain elements for connectioncontext ipaddress and routes #7

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
77 changes: 77 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 ipaddress

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"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type ipaddressClient struct {
vppConn api.Connection
}

// NewClient creates a NetworkServiceClient chain element to set the ip address on a vpp interface
// It sets the IP Address on the *vpp* side of an interface leaving the
// Endpoint.
// Endpoint
// +---------------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | ipaddress.NewClient()+-------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +---------------------------+
//
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
return &ipaddressClient{
vppConn: vppConn,
}
}

func (i *ipaddressClient) 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, conn, i.vppConn, metadata.IsClient(i), true); err != nil {
_, _ = i.Close(ctx, conn, opts...)
return nil, err
}
return conn, nil
}

func (i *ipaddressClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
return next.Client(ctx).Close(ctx, conn, opts...)
}
60 changes: 60 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 ipaddress

import (
"context"
"time"

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

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

func addDel(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient, isAdd bool) error {
swIfIndex, ok := ifindex.Load(ctx, isClient)
if !ok {
return errors.New("no swIfIndex available")
}
ipNet := conn.GetContext().GetIpContext().GetDstIPNet()
if isClient {
ipNet = conn.GetContext().GetIpContext().GetSrcIPNet()
}
if ipNet == nil {
return nil
}
now := time.Now()
if _, err := interfaces.NewServiceClient(vppConn).SwInterfaceAddDelAddress(ctx, &interfaces.SwInterfaceAddDelAddress{
SwIfIndex: swIfIndex,
IsAdd: isAdd,
Prefix: types.ToVppAddressWithPrefix(ipNet),
}); err != nil {
return errors.WithStack(err)
}
trace.Log(ctx).
WithField("swIfIndex", swIfIndex).
WithField("prefix", ipNet).
WithField("isAdd", isAdd).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceAddDelAddress").Debug("completed")
return nil
}
18 changes: 18 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/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 ipaddress provides networkservice chain elements to set the ip address on vpp interfaces
package ipaddress
70 changes: 70 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 ipaddress

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"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type ipaddressServer struct {
vppConn api.Connection
}

// NewServer creates a NetworkServiceServer chain element to set the ip address on a vpp interface
// It sets the IP Address on the *vpp* side of an interface plugged into the
// Endpoint.
// Endpoint
// +---------------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +-------------------+ ipaddress.NewServer() |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +---------------------------+
//
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
return &ipaddressServer{
vppConn: vppConn,
}
}

func (i *ipaddressServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if err := addDel(ctx, request.GetConnection(), i.vppConn, metadata.IsClient(i), true); err != nil {
return nil, err
}
return next.Server(ctx).Request(ctx, request)
}

func (i *ipaddressServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}
78 changes: 78 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/routes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 routes

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"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type routesClient struct {
vppConn api.Connection
}

// NewClient creates a NetworkServiceClient chain element to set routes in vpp
// Client
// +---------------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | routes.NewClient() +-------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +---------------------------+
//
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
return &routesClient{
vppConn: vppConn,
}
}

func (r *routesClient) 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, conn, r.vppConn, metadata.IsClient(r), true); err != nil {
_, _ = r.Close(ctx, conn, opts...)
return nil, err
}
return conn, nil
}

func (r *routesClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
if err := addDel(ctx, conn, r.vppConn, metadata.IsClient(r), false); err != nil {
return nil, err
}
return next.Client(ctx).Close(ctx, conn, opts...)
}
Loading