Skip to content

Commit

Permalink
Add chain elements for connectioncontext ipaddress and routes
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Warnicke <hagbard@gmail.com>
  • Loading branch information
edwarnicke committed Nov 18, 2020
1 parent 6ec28c5 commit d4e3feb
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 0 deletions.
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

0 comments on commit d4e3feb

Please sign in to comment.