-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If a client receives an fd for a unix file socket via grpcfd, then it is possible that vpp will be unable to open that file in its /proc/${pid}/fd/${fd} form. This is because while the receiving client process gets the rights to that open file, vpp doesn't necessarily have them. In order to resolve this issue, the receiving client proxies the memif control socket communications bilaterally. Signed-off-by: Ed Warnicke <hagbard@gmail.com>
- Loading branch information
1 parent
627b1e9
commit 99d6043
Showing
7 changed files
with
374 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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. | ||
|
||
// +build !windows | ||
|
||
package memifproxy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
memifMech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/memif" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
"github.com/pkg/errors" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
tmpDirPrefix = "memifproxy-" | ||
memifNetwork = "unixpacket" | ||
maxFDCount = 1 | ||
bufferSize = 128 | ||
) | ||
|
||
type memifProxyClient struct { | ||
initOnce sync.Once | ||
tmpDir string | ||
err error | ||
} | ||
|
||
// New - create a new memifProxy client chain element | ||
func New() networkservice.NetworkServiceClient { | ||
return &memifProxyClient{} | ||
} | ||
|
||
func (m *memifProxyClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
m.initOnce.Do(func() { | ||
m.tmpDir, m.err = ioutil.TempDir("", tmpDirPrefix) | ||
}) | ||
if m.err != nil { | ||
return nil, errors.Wrap(m.err, "failed to create memifproxy tmpDir") | ||
} | ||
conn, err := next.Client(ctx).Request(ctx, request, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mechanism := memifMech.ToMechanism(conn.GetMechanism()) | ||
if mechanism == nil { | ||
return conn, nil | ||
} | ||
// If we are already running a proxy... just keep running it | ||
if _, ok := load(ctx, true); ok { | ||
return conn, nil | ||
} | ||
listenSocketFilename := filepath.Join(m.tmpDir, fmt.Sprintf("%s.memif.socket", conn.GetId())) | ||
listener, err := newProxyListener(mechanism, listenSocketFilename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
store(ctx, metadata.IsClient(m), listener) | ||
return conn, nil | ||
} | ||
|
||
func (m *memifProxyClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
m.initOnce.Do(func() { | ||
m.tmpDir, m.err = ioutil.TempDir("", tmpDirPrefix) | ||
}) | ||
if m.err != nil { | ||
return nil, errors.Wrap(m.err, "failed to create memifproxy tmpDir") | ||
} | ||
rv, err := next.Client(ctx).Close(ctx, conn) | ||
if listener, ok := loadAndDelete(ctx, metadata.IsClient(m)); ok { | ||
_ = listener.Close() | ||
} | ||
return rv, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 memifproxy provides a NetworkServiceClient chain element to 'proxy' to the memif control socket | ||
// This is done in case the vpp instance can't open the memif socketfile | ||
package memifproxy |
55 changes: 55 additions & 0 deletions
55
pkg/networkservice/mechanisms/memif/memifproxy/metadata.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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. | ||
|
||
// +build !windows | ||
|
||
package memifproxy | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
type key struct{} | ||
|
||
// store sets the *proxyListener stored in per Connection.Id metadata. | ||
func store(ctx context.Context, isClient bool, listener *proxyListener) { | ||
metadata.Map(ctx, isClient).Store(key{}, listener) | ||
} | ||
|
||
// load returns the *proxyListener stored in per Connection.Id metadata, or nil if no | ||
// value is present. | ||
// The ok result indicates whether value was found in the per Connection.Id metadata. | ||
func load(ctx context.Context, isClient bool) (value *proxyListener, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, isClient).Load(key{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(*proxyListener) | ||
return value, ok | ||
} | ||
|
||
// loadAndDelete deletes the *proxyListener stored in per Connection.Id metadata, | ||
// returning the previous value if any. The loaded result reports whether the key was present. | ||
func loadAndDelete(ctx context.Context, isClient bool) (value *proxyListener, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, isClient).LoadAndDelete(key{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(*proxyListener) | ||
return value, ok | ||
} |
91 changes: 91 additions & 0 deletions
91
pkg/networkservice/mechanisms/memif/memifproxy/proxy_connection.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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. | ||
|
||
// +build !windows | ||
|
||
package memifproxy | ||
|
||
import ( | ||
"net" | ||
"syscall" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type proxyConnection struct { | ||
in net.Conn | ||
out net.Conn | ||
} | ||
|
||
func newProxyConnection(in, out net.Conn) (*proxyConnection, error) { | ||
p := &proxyConnection{ | ||
in: in, | ||
out: out, | ||
} | ||
if err := p.copy(in, out); err != nil { | ||
return nil, err | ||
} | ||
if err := p.copy(out, in); err != nil { | ||
return nil, err | ||
} | ||
return p, nil | ||
} | ||
|
||
func (p *proxyConnection) Close() error { | ||
inErr := p.in.Close() | ||
outErr := p.out.Close() | ||
if inErr != nil { | ||
return multierror.Append(inErr, outErr) | ||
} | ||
return outErr | ||
} | ||
|
||
func (p *proxyConnection) copy(dst, src net.Conn) error { | ||
b := make([]byte, bufferSize) | ||
unixsrc, unixSrcOK := src.(interface { | ||
ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *net.UnixAddr, err error) | ||
}) | ||
if !unixSrcOK { | ||
return errors.Errorf("%s does not implement ReadMsgUnix", src.LocalAddr()) | ||
} | ||
|
||
unixdst, unixdstOK := dst.(interface { | ||
WriteMsgUnix(b, oob []byte, addr *net.UnixAddr) (n, oobn int, err error) | ||
}) | ||
|
||
if !unixdstOK { | ||
return errors.Errorf("%s does not implement ReadMsgUnix", dst.LocalAddr()) | ||
} | ||
|
||
go func() { | ||
oob := make([]byte, syscall.CmsgSpace(4*maxFDCount)) | ||
for { | ||
var writeN, writeoob int | ||
readn, readoobn, _, _, err := unixsrc.ReadMsgUnix(b, oob) | ||
if err != nil { | ||
return | ||
} | ||
for writeN < readn { | ||
writeN, writeoob, err = unixdst.WriteMsgUnix(b[writeN:readn], oob[writeoob:readoobn], nil) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
return nil | ||
} |
Oops, something went wrong.