Skip to content

Commit

Permalink
Add podman network create option for bridge mtu
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Dec 1, 2020
1 parent c71ad9a commit b1b3570
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
9 changes: 9 additions & 0 deletions cmd/podman/networks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
var (
networkCreateOptions entities.NetworkCreateOptions
labels []string
opts []string
)

func networkCreateFlags(cmd *cobra.Command) {
Expand All @@ -39,6 +40,10 @@ func networkCreateFlags(cmd *cobra.Command) {
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)

optFlagName := "opt"
flags.StringArrayVarP(&opts, optFlagName, "o", []string{}, "Set driver specific options (default [])")
_ = cmd.RegisterFlagCompletionFunc(optFlagName, completion.AutocompleteNone)

gatewayFlagName := "gateway"
flags.IPVar(&networkCreateOptions.Gateway, gatewayFlagName, nil, "IPv4 or IPv6 gateway for the subnet")
_ = cmd.RegisterFlagCompletionFunc(gatewayFlagName, completion.AutocompleteNone)
Expand Down Expand Up @@ -93,6 +98,10 @@ func networkCreate(cmd *cobra.Command, args []string) error {
if err != nil {
return errors.Wrap(err, "failed to parse labels")
}
networkCreateOptions.Options, err = parse.GetAllLabels([]string{}, opts)
if err != nil {
return errors.Wrapf(err, "unable to process options")
}
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions docs/source/markdown/podman-network-create.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ resolution.

Driver to manage the network (default "bridge"). Currently only `bridge` is supported.

#### **--opt**=*option*, **-o**

Set driver specific options.

For the `bridge` driver the following options are supported: `mtu`.
The `mtu` option sets the Maximum Transmission Unit (MTU) and takes an integer value.

#### **--gateway**

Define a gateway for the subnet. If you want to provide a gateway address, you must also provide a
Expand Down
23 changes: 22 additions & 1 deletion libpod/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"

"github.com/containernetworking/cni/pkg/version"
"github.com/containers/common/pkg/config"
Expand Down Expand Up @@ -76,6 +77,21 @@ func validateBridgeOptions(options entities.NetworkCreateOptions) error {

}

// parseMTU parses the mtu option
func parseMTU(mtu string) (int, error) {
if mtu == "" {
return 0, nil // default
}
m, err := strconv.Atoi(mtu)
if err != nil {
return 0, err
}
if m < 0 {
return 0, errors.Errorf("the value %d for mtu is less than zero", m)
}
return m, nil
}

// createBridge creates a CNI network
func createBridge(name string, options entities.NetworkCreateOptions, runtimeConfig *config.Config) (string, error) {
var (
Expand Down Expand Up @@ -149,6 +165,11 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
ipMasq = false
}

mtu, err := parseMTU(options.Options["mtu"])
if err != nil {
return "", err
}

// obtain host bridge name
bridgeDeviceName, err := GetFreeDeviceName(runtimeConfig)
if err != nil {
Expand All @@ -172,7 +193,7 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
ncList := NewNcList(name, version.Current(), options.Labels)
var plugins []CNIPlugins
// TODO need to iron out the role of isDefaultGW and IPMasq
bridge := NewHostLocalBridge(bridgeDeviceName, isGateway, false, ipMasq, ipamConfig)
bridge := NewHostLocalBridge(bridgeDeviceName, isGateway, false, ipMasq, mtu, ipamConfig)
plugins = append(plugins, bridge)
plugins = append(plugins, NewPortMapPlugin())
plugins = append(plugins, NewFirewallPlugin())
Expand Down
3 changes: 2 additions & 1 deletion libpod/network/netconflist.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func NewNcList(name, version string, labels NcLabels) NcList {
}

// NewHostLocalBridge creates a new LocalBridge for host-local
func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, ipamConf IPAMHostLocalConf) *HostLocalBridge {
func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, mtu int, ipamConf IPAMHostLocalConf) *HostLocalBridge {
hostLocalBridge := HostLocalBridge{
PluginType: "bridge",
BrName: name,
IPMasq: ipMasq,
MTU: mtu,
HairpinMode: true,
IPAM: ipamConf,
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type NetworkCreateOptions struct {
Range net.IPNet
Subnet net.IPNet
IPv6 bool
// Mapping of driver options and values.
Options map[string]string
}

// NetworkCreateReport describes a created network for the cli
Expand Down

0 comments on commit b1b3570

Please sign in to comment.