Skip to content

Commit

Permalink
drivers/macvlan: Add support for optional runtime hint
Browse files Browse the repository at this point in the history
docker supports alternate OCI runtimes including virtual machine
based runtimes. In certian cases network plugins can optionally
choose to support creation of  virtual machine friendly interfaces
using optional network options.

This is illustrated here with the hint being used by the macvlan
driver to create a macvtap interface vs a macvlan interface when
the runtime is known to be a VM based runtime.

docker run --runtime=cor -it --net=pub_net --network "name=pub_net,runtime=namespace" alpine sh

This is currently based off of
moby/moby#27638

However this will be implemented as per the proposal
moby/moby#31964

Signed-off-by: Manohar Castelino <manohar.r.castelino@intel.com>
  • Loading branch information
mcastelino committed Mar 22, 2017
1 parent 1fa6812 commit fedca67
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions drivers/macvlan/macvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type endpoint struct {
srcName string
dbIndex uint64
dbExists bool
runtime string
}

type network struct {
Expand Down
13 changes: 13 additions & 0 deletions drivers/macvlan/macvlan_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
return err
}
}

// disallow portmapping -p
if opt, ok := epOptions[netlabel.PortMap]; ok {
if _, ok := opt.([]types.PortBinding); ok {
Expand All @@ -57,6 +58,18 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
}
}

// Setup the runtime to default to namespace, override if specified
ep.runtime = "namespace"
if opt, ok := epOptions["runtime"]; ok {
if runtime, ok := opt.(string); ok {
if runtime != "namespace" && runtime != "vm" {
logrus.Warnf("driver does not support [%s] runtime", runtime)
} else {
ep.runtime = runtime
}
}
}

if err := d.storeUpdate(ep); err != nil {
return fmt.Errorf("failed to save macvlan endpoint %s to store: %v", ep.id[0:7], err)
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/macvlan/macvlan_joinleave.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
return fmt.Errorf("error generating an interface name: %s", err)
}
// create the netlink macvlan interface
vethName, err := createMacVlan(containerIfName, n.config.Parent, n.config.MacvlanMode)
vethName, err := createMacVlan(containerIfName, n.config.Parent, n.config.MacvlanMode, endpoint.runtime)
if err != nil {
return err
}
Expand Down
50 changes: 35 additions & 15 deletions drivers/macvlan/macvlan_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const (
macvlanMajorVer = 9 // minimum macvlan major kernel support
)

// Create the macvlan slave specifying the source name
func createMacVlan(containerIfName, parent, macvlanMode string) (string, error) {
// Create the macvlan slave specifying the source name and the runtime type
func createMacVlan(containerIfName, parent, macvlanMode, runtime string) (string, error) {
// Set the macvlan mode. Default is bridge mode
mode, err := setMacVlanMode(macvlanMode)
if err != nil {
Expand All @@ -32,20 +32,40 @@ func createMacVlan(containerIfName, parent, macvlanMode string) (string, error)
if err != nil {
return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", macvlanType, parent, err)
}
// Create a macvlan link
macvlan := &netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
Name: containerIfName,
ParentIndex: parentLink.Attrs().Index,
},
Mode: mode,
}
if err := ns.NlHandle().LinkAdd(macvlan); err != nil {
// If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
}

return macvlan.Attrs().Name, nil
if runtime == "vm" {
// Create a macvtap link
macvtap := &netlink.Macvtap{
Macvlan: netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
Name: containerIfName,
ParentIndex: parentLink.Attrs().Index,
},
Mode: mode,
},
}

if err := ns.NlHandle().LinkAdd(macvtap); err != nil {
return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
}

return macvtap.Attrs().Name, nil
} else {
// Create a macvlan link
macvlan := &netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
Name: containerIfName,
ParentIndex: parentLink.Attrs().Index,
},
Mode: mode,
}
if err := ns.NlHandle().LinkAdd(macvlan); err != nil {
// If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
}

return macvlan.Attrs().Name, nil
}
}

// setMacVlanMode setter for one of the four macvlan port types
Expand Down
2 changes: 2 additions & 0 deletions drivers/macvlan/macvlan_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (ep *endpoint) MarshalJSON() ([]byte, error) {
epMap["id"] = ep.id
epMap["nid"] = ep.nid
epMap["SrcName"] = ep.srcName
epMap["runtime"] = ep.runtime
if len(ep.mac) != 0 {
epMap["MacAddress"] = ep.mac.String()
}
Expand Down Expand Up @@ -295,6 +296,7 @@ func (ep *endpoint) UnmarshalJSON(b []byte) error {
ep.id = epMap["id"].(string)
ep.nid = epMap["nid"].(string)
ep.srcName = epMap["SrcName"].(string)
ep.runtime = epMap["runtime"].(string)

return nil
}
Expand Down

0 comments on commit fedca67

Please sign in to comment.