Skip to content

Commit

Permalink
Merge pull request #39 from fedepaol/alignvrf
Browse files Browse the repository at this point in the history
Re-align with upstream VRF changes
  • Loading branch information
openshift-merge-robot committed Nov 11, 2020
2 parents 4c2abc3 + 07d6460 commit fc27124
Show file tree
Hide file tree
Showing 4 changed files with 479 additions and 8 deletions.
5 changes: 3 additions & 2 deletions plugins/meta/vrf/README.md
Expand Up @@ -4,7 +4,7 @@

This plugin creates a [VRF](https://www.kernel.org/doc/Documentation/networking/vrf.txt) in the network namespace and assigns it the interface passed in the arguments. If the VRF is already present in the namespace, it only adds the interface to it.

As a table id is mandatory, the plugin generates a new one for each different VRF that is added to the namespace.
The table id is mandatory for VRF but optional in the CNI configuration. If not specified, the plugin generates a new one for each different VRF that is added to the namespace.

It does not create any network interfaces and therefore does not bring connectivity by itself.
It is only useful when used in addition to other plugins.
Expand Down Expand Up @@ -50,4 +50,5 @@ The only configuration is the name of the VRF, as per the following example:

The following [args conventions](https://github.com/containernetworking/cni/blob/master/CONVENTIONS.md#args-in-network-config) are supported:

* `vrfname` (string, optional): The name of the VRF to be created and to be set as master of the interface
* `vrfname` (string): The name of the VRF to be created and to be set as master of the interface
* `table` (int, optional): The route table to be associated to the created VRF.
12 changes: 10 additions & 2 deletions plugins/meta/vrf/main.go
Expand Up @@ -35,10 +35,12 @@ type VRFNetConf struct {

// VRFName is the name of the vrf to add the interface to.
VRFName string `json:"vrfname"`
// Table is the optional name of the routing table set for the vrf
Table uint32 `json:"table"`
}

func main() {
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.PluginSupports("0.4.0"), bv.BuildString("vrf"))
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.PluginSupports("0.3.1", "0.4.0"), bv.BuildString("vrf"))
}

func cmdAdd(args *skel.CmdArgs) error {
Expand All @@ -54,8 +56,14 @@ func cmdAdd(args *skel.CmdArgs) error {
err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error {
vrf, err := findVRF(conf.VRFName)

// If the user set a tableid and the vrf is already in the namespace
// we check if the tableid is the same one already assigned to the vrf.
if err == nil && conf.Table != 0 && vrf.Table != conf.Table {
return fmt.Errorf("VRF %s already exist with different routing table %d", conf.VRFName, vrf.Table)
}

if _, ok := err.(netlink.LinkNotFoundError); ok {
vrf, err = createVRF(conf.VRFName)
vrf, err = createVRF(conf.VRFName, conf.Table)
}

if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions plugins/meta/vrf/vrf.go
Expand Up @@ -35,15 +35,19 @@ func findVRF(name string) (*netlink.Vrf, error) {
}

// createVRF creates a new VRF and sets it up.
func createVRF(name string) (*netlink.Vrf, error) {
func createVRF(name string, tableID uint32) (*netlink.Vrf, error) {
links, err := netlink.LinkList()
if err != nil {
return nil, fmt.Errorf("createVRF: Failed to find links %v", err)
}
tableID, err := findFreeRoutingTableID(links)
if err != nil {
return nil, err

if tableID == 0 {
tableID, err = findFreeRoutingTableID(links)
if err != nil {
return nil, err
}
}

vrf := &netlink.Vrf{
LinkAttrs: netlink.LinkAttrs{
Name: name,
Expand Down

0 comments on commit fc27124

Please sign in to comment.