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 c2bfe45
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/client.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 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 - returns Client chain element that sets the proper ip address on the connection in vpp
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 chain elements for setting the vpp ipaddress for a Connection from the ConnectionContext
package ipaddress
49 changes: 49 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 - returns Server chain element that sets the proper ip address in vpp
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)
}
59 changes: 59 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/routes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 - returns Client chain element that sets proper routes in vpp
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...)
}
108 changes: 108 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/routes/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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"
"net"
"time"

"git.fd.io/govpp.git/api"
"github.com/edwarnicke/govpp/binapi/fib_types"
"github.com/edwarnicke/govpp/binapi/interface_types"
"github.com/edwarnicke/govpp/binapi/ip"
"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 nil
}
from := conn.GetContext().GetIpContext().GetDstIPNet()
to := conn.GetContext().GetIpContext().GetSrcIPNet()
if isClient {
from = conn.GetContext().GetIpContext().GetSrcIPNet()
to = conn.GetContext().GetIpContext().GetDstIPNet()
}
routes := conn.GetContext().GetIpContext().GetDstRoutes()
if isClient {
routes = conn.GetContext().GetIpContext().GetSrcRoutes()
}
for _, route := range routes {
if err := routeAddDel(ctx, vppConn, swIfIndex, isAdd, route.GetPrefixIPNet(), to); err != nil {
return err
}
}
if to != nil && !to.Contains(from.IP) {
if err := routeAddDel(ctx, vppConn, swIfIndex, isAdd, to, nil); err != nil {
return err
}
}
return nil
}

func routeAddDel(ctx context.Context, vppConn api.Connection, swIfIndex interface_types.InterfaceIndex, isAdd bool, prefix, gw *net.IPNet) error {
if prefix == nil {
return errors.New("route prefix must not be nil")
}
route := route(prefix, swIfIndex, gw)
now := time.Now()
if _, err := ip.NewServiceClient(vppConn).IPRouteAddDel(ctx, &ip.IPRouteAddDel{
IsAdd: isAdd,
IsMultipath: true,
Route: route,
}); err != nil {
return errors.WithStack(err)
}
trace.Log(ctx).
WithField("swIfIndex", swIfIndex).
WithField("prefix", prefix).
WithField("isAdd", isAdd).
WithField("type", isAdd).
WithField("duration", time.Since(now)).
WithField("vppapi", "IPRouteAddDel").Debug("completed")
return nil
}

func route(dst *net.IPNet, via interface_types.InterfaceIndex, nh *net.IPNet) ip.IPRoute {
route := ip.IPRoute{
StatsIndex: 0,
Prefix: types.ToVppPrefix(dst),
NPaths: 1,
Paths: []fib_types.FibPath{
{
SwIfIndex: uint32(via),
TableID: 0,
RpfID: 0,
Weight: 1,
Type: fib_types.FIB_API_PATH_TYPE_NORMAL,
Flags: fib_types.FIB_API_PATH_FLAG_NONE,
Proto: types.IsV6toFibProto(dst.IP.To4() == nil),
},
},
}
if nh != nil {
route.Paths[0].Nh.Address = types.ToVppAddress(nh.IP).Un
}
return route
}
18 changes: 18 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/routes/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 routes provides chain elements for setting the vpp routes for a Connection from the ConnectionContext
package routes
Loading

0 comments on commit c2bfe45

Please sign in to comment.