-
Notifications
You must be signed in to change notification settings - Fork 823
dummy: Create a Dummy CNI plugin that creates a virtual interface. #743
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
--- | ||
title: dummy plugin | ||
description: "plugins/main/dummy/README.md" | ||
date: 2022-05-12 | ||
toc: true | ||
draft: true | ||
weight: 200 | ||
--- | ||
|
||
## Overview | ||
|
||
dummy is a useful feature for routing packets through the Linux kernel without transmitting. | ||
|
||
Like loopback, it is a purely virtual interface that allows packets to be routed to a designated IP address. Unlike loopback, the IP address can be arbitrary and is not restricted to the `127.0.0.0/8` range. | ||
|
||
## Example configuration | ||
|
||
```json | ||
{ | ||
"name": "mynet", | ||
"type": "dummy", | ||
"ipam": { | ||
"type": "host-local", | ||
"subnet": "10.1.2.0/24" | ||
} | ||
} | ||
``` | ||
|
||
aristaMircea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Network configuration reference | ||
|
||
* `name` (string, required): the name of the network. | ||
* `type` (string, required): "dummy". | ||
* `ipam` (dictionary, required): IPAM configuration to be used for this network. | ||
|
||
## Notes | ||
|
||
* `dummy` does not transmit packets. | ||
Therefore the container will not be able to reach any external network. | ||
This solution is designed to be used in conjunction with other CNI plugins (e.g., `bridge`) to provide an internal non-loopback address for applications to use. |
This file contains hidden or 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,300 @@ | ||
// Copyright 2022 Arista Networks | ||
aristaMircea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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 main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/vishvananda/netlink" | ||
|
||
"github.com/containernetworking/cni/pkg/skel" | ||
"github.com/containernetworking/cni/pkg/types" | ||
current "github.com/containernetworking/cni/pkg/types/100" | ||
"github.com/containernetworking/cni/pkg/version" | ||
|
||
"github.com/containernetworking/plugins/pkg/ip" | ||
"github.com/containernetworking/plugins/pkg/ipam" | ||
"github.com/containernetworking/plugins/pkg/ns" | ||
bv "github.com/containernetworking/plugins/pkg/utils/buildversion" | ||
) | ||
|
||
func parseNetConf(bytes []byte) (*types.NetConf, error) { | ||
conf := &types.NetConf{} | ||
if err := json.Unmarshal(bytes, conf); err != nil { | ||
return nil, fmt.Errorf("failed to parse network config: %v", err) | ||
} | ||
return conf, nil | ||
} | ||
|
||
func createDummy(conf *types.NetConf, ifName string, netns ns.NetNS) (*current.Interface, error) { | ||
|
||
dummy := ¤t.Interface{} | ||
|
||
dm := &netlink.Dummy{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: ifName, | ||
Namespace: netlink.NsFd(int(netns.Fd())), | ||
aristaMircea marked this conversation as resolved.
Show resolved
Hide resolved
aristaMircea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
} | ||
|
||
if err := netlink.LinkAdd(dm); err != nil { | ||
return nil, fmt.Errorf("failed to create dummy: %v", err) | ||
} | ||
dummy.Name = ifName | ||
|
||
err := netns.Do(func(_ ns.NetNS) error { | ||
// Re-fetch interface to get all properties/attributes | ||
contDummy, err := netlink.LinkByName(ifName) | ||
if err != nil { | ||
return fmt.Errorf("failed to fetch dummy%q: %v", ifName, err) | ||
} | ||
|
||
dummy.Mac = contDummy.Attrs().HardwareAddr.String() | ||
dummy.Sandbox = netns.Path() | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dummy, nil | ||
} | ||
|
||
func cmdAdd(args *skel.CmdArgs) error { | ||
conf, err := parseNetConf(args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if conf.IPAM.Type == "" { | ||
return errors.New("dummy interface requires an IPAM configuration") | ||
} | ||
|
||
netns, err := ns.GetNS(args.Netns) | ||
if err != nil { | ||
return fmt.Errorf("failed to open netns %q: %v", netns, err) | ||
} | ||
defer netns.Close() | ||
|
||
dummyInterface, err := createDummy(conf, args.IfName, netns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Delete link if err to avoid link leak in this ns | ||
defer func() { | ||
if err != nil { | ||
netns.Do(func(_ ns.NetNS) error { | ||
return ip.DelLinkByName(args.IfName) | ||
}) | ||
} | ||
}() | ||
|
||
r, err := ipam.ExecAdd(conf.IPAM.Type, args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// defer ipam deletion to avoid ip leak | ||
defer func() { | ||
if err != nil { | ||
ipam.ExecDel(conf.IPAM.Type, args.StdinData) | ||
} | ||
}() | ||
|
||
// convert IPAMResult to current Result type | ||
result, err := current.NewResultFromResult(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(result.IPs) == 0 { | ||
return errors.New("IPAM plugin returned missing IP config") | ||
} | ||
|
||
for _, ipc := range result.IPs { | ||
// all addresses apply to the container dummy interface | ||
ipc.Interface = current.Int(0) | ||
} | ||
|
||
result.Interfaces = []*current.Interface{dummyInterface} | ||
|
||
err = netns.Do(func(_ ns.NetNS) error { | ||
if err := ipam.ConfigureIface(args.IfName, result); err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return types.PrintResult(result, conf.CNIVersion) | ||
} | ||
|
||
func cmdDel(args *skel.CmdArgs) error { | ||
conf, err := parseNetConf(args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = ipam.ExecDel(conf.IPAM.Type, args.StdinData); err != nil { | ||
return err | ||
} | ||
|
||
if args.Netns == "" { | ||
return nil | ||
} | ||
|
||
err = ns.WithNetNSPath(args.Netns, func(ns.NetNS) error { | ||
err = ip.DelLinkByName(args.IfName) | ||
if err != nil && err == ip.ErrLinkNotFound { | ||
return nil | ||
} | ||
return err | ||
}) | ||
|
||
if err != nil { | ||
// if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times | ||
// so don't return an error if the device is already removed. | ||
// https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444 | ||
_, ok := err.(ns.NSPathNotExistErr) | ||
if ok { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.All, bv.BuildString("dummy")) | ||
} | ||
|
||
func cmdCheck(args *skel.CmdArgs) error { | ||
conf, err := parseNetConf(args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if conf.IPAM.Type == "" { | ||
return errors.New("dummy interface requires an IPAM configuration") | ||
} | ||
|
||
netns, err := ns.GetNS(args.Netns) | ||
if err != nil { | ||
return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) | ||
} | ||
defer netns.Close() | ||
|
||
// run the IPAM plugin and get back the config to apply | ||
err = ipam.ExecCheck(conf.IPAM.Type, args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if conf.RawPrevResult == nil { | ||
return fmt.Errorf("dummy: Required prevResult missing") | ||
} | ||
|
||
if err := version.ParsePrevResult(conf); err != nil { | ||
return err | ||
} | ||
|
||
// Convert whatever the IPAM result was into the current Result type | ||
result, err := current.NewResultFromResult(conf.PrevResult) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var contMap current.Interface | ||
// Find interfaces for name whe know, that of dummy device inside container | ||
for _, intf := range result.Interfaces { | ||
if args.IfName == intf.Name { | ||
if args.Netns == intf.Sandbox { | ||
contMap = *intf | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// The namespace must be the same as what was configured | ||
if args.Netns != contMap.Sandbox { | ||
return fmt.Errorf("Sandbox in prevResult %s doesn't match configured netns: %s", | ||
contMap.Sandbox, args.Netns) | ||
} | ||
|
||
// | ||
// Check prevResults for ips, routes and dns against values found in the container | ||
if err := netns.Do(func(_ ns.NetNS) error { | ||
|
||
// Check interface against values found in the container | ||
err := validateCniContainerInterface(contMap) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ip.ValidateExpectedInterfaceIPs(args.IfName, result.IPs) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
func validateCniContainerInterface(intf current.Interface) error { | ||
|
||
var link netlink.Link | ||
var err error | ||
|
||
if intf.Name == "" { | ||
return fmt.Errorf("Container interface name missing in prevResult: %v", intf.Name) | ||
} | ||
link, err = netlink.LinkByName(intf.Name) | ||
if err != nil { | ||
return fmt.Errorf("Container Interface name in prevResult: %s not found", intf.Name) | ||
} | ||
if intf.Sandbox == "" { | ||
return fmt.Errorf("Error: Container interface %s should not be in host namespace", link.Attrs().Name) | ||
} | ||
|
||
_, isDummy := link.(*netlink.Dummy) | ||
if !isDummy { | ||
return fmt.Errorf("Error: Container interface %s not of type dummy", link.Attrs().Name) | ||
} | ||
|
||
if intf.Mac != "" { | ||
if intf.Mac != link.Attrs().HardwareAddr.String() { | ||
return fmt.Errorf("Interface %s Mac %s doesn't match container Mac: %s", intf.Name, intf.Mac, link.Attrs().HardwareAddr) | ||
} | ||
} | ||
|
||
if link.Attrs().Flags&net.FlagUp != net.FlagUp { | ||
return fmt.Errorf("Interface %s is down", intf.Name) | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or 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,41 @@ | ||
// Copyright 2022 Arista Networks | ||
// | ||
// 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 main_test | ||
|
||
import ( | ||
"github.com/onsi/gomega/gexec" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
var pathToLoPlugin string | ||
|
||
func TestLoopback(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "plugins/main/dummy") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
var err error | ||
pathToLoPlugin, err = gexec.Build("github.com/containernetworking/plugins/plugins/main/dummy") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
gexec.CleanupBuildArtifacts() | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, this can go in, but will need to be a separate pr on https://github.com/containernetworking/cni.dev