device: refactor setting user-configured MTU during config commit

Instead of overwriting ip4_config_pre_commit(), add a new function
get_mtu().

This also adds a default value in case there is no user-configuration.
This will allow us later to reset a default MTU based on the device
type.
This commit is contained in:
Thomas Haller 2017-01-14 17:04:17 +01:00
parent 0210754f18
commit 6e52efe950
13 changed files with 101 additions and 173 deletions

View file

@ -383,25 +383,6 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
return ret;
}
static void
ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (self);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
/* MTU override */
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
static gboolean
enslave_slave (NMDevice *device,
NMDevice *slave,
@ -523,7 +504,7 @@ nm_device_bond_class_init (NMDeviceBondClass *klass)
parent_class->create_and_realize = create_and_realize;
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
parent_class->enslave_slave = enslave_slave;
parent_class->release_slave = release_slave;

View file

@ -1391,26 +1391,14 @@ act_stage3_ip4_config_start (NMDevice *device,
return NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->act_stage3_ip4_config_start (device, out_config, reason);
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
static guint32
get_configured_mtu (NMDevice *device, gboolean *out_is_user_config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
/* MTU only set for plain ethernet */
if (NM_DEVICE_ETHERNET_GET_PRIVATE ((NMDeviceEthernet *) device)->ppp_manager)
return;
return 0;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
g_assert (s_wired);
/* MTU override */
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
return nm_device_get_configured_mtu_for_wired (device, out_is_user_config);
}
static void
@ -1774,7 +1762,7 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass)
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->act_stage2_config = act_stage2_config;
parent_class->act_stage3_ip4_config_start = act_stage3_ip4_config_start;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = get_configured_mtu;
parent_class->deactivate = deactivate;
parent_class->spec_match_list = spec_match_list;
parent_class->update_connection = update_connection;

View file

@ -118,22 +118,22 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
return NM_ACT_STAGE_RETURN_SUCCESS;
}
static void
ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
static guint32
get_configured_mtu (NMDevice *device, gboolean *out_is_user_config)
{
NMConnection *connection;
NMSettingInfiniband *s_infiniband;
NMSettingInfiniband *setting;
guint32 mtu;
connection = nm_device_get_applied_connection (self);
g_assert (connection);
s_infiniband = nm_connection_get_setting_infiniband (connection);
g_assert (s_infiniband);
nm_assert (NM_IS_DEVICE (device));
nm_assert (out_is_user_config);
/* MTU override */
mtu = nm_setting_infiniband_get_mtu (s_infiniband);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
setting = NM_SETTING_INFINIBAND (nm_device_get_applied_setting (device, NM_TYPE_SETTING_INFINIBAND));
if (!setting)
g_return_val_if_reached (0);
mtu = nm_setting_infiniband_get_mtu (setting);
*out_is_user_config = (mtu != 0);
return mtu ?: NM_DEVICE_DEFAULT_MTU_INFINIBAND;
}
static gboolean
@ -381,7 +381,7 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass)
parent_class->update_connection = update_connection;
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = get_configured_mtu;
obj_properties[PROP_IS_PARTITION] =
g_param_spec_boolean (NM_DEVICE_INFINIBAND_IS_PARTITION, "", "",

View file

@ -744,22 +744,22 @@ create_and_realize (NMDevice *device,
return TRUE;
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
static guint32
get_configured_mtu (NMDevice *self, gboolean *out_is_user_config)
{
NMConnection *connection;
NMSettingIPTunnel *s_ip_tunnel;
NMSettingIPTunnel *setting;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_ip_tunnel = nm_connection_get_setting_ip_tunnel (connection);
g_assert (s_ip_tunnel);
nm_assert (NM_IS_DEVICE (self));
nm_assert (out_is_user_config);
/* MTU override */
mtu = nm_setting_ip_tunnel_get_mtu (s_ip_tunnel);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
setting = NM_SETTING_IP_TUNNEL (nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP_TUNNEL));
if (!setting)
g_return_val_if_reached (0);
mtu = nm_setting_ip_tunnel_get_mtu (setting);
*out_is_user_config = (mtu != 0);
return mtu ?: NM_DEVICE_DEFAULT_MTU_WIRED;
}
static NMDeviceCapabilities
@ -873,7 +873,7 @@ nm_device_ip_tunnel_class_init (NMDeviceIPTunnelClass *klass)
device_class->check_connection_compatible = check_connection_compatible;
device_class->create_and_realize = create_and_realize;
device_class->get_generic_capabilities = get_generic_capabilities;
device_class->ip4_config_pre_commit = ip4_config_pre_commit;
device_class->get_configured_mtu = get_configured_mtu;
device_class->unrealize_notify = unrealize_notify;
NM_DEVICE_CLASS_DECLARE_TYPES (klass,

View file

@ -489,24 +489,6 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
return NM_ACT_STAGE_RETURN_SUCCESS;
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
/*****************************************************************************/
static void
@ -570,7 +552,7 @@ nm_device_macvlan_class_init (NMDeviceMacvlanClass *klass)
device_class->connection_type = NM_SETTING_MACVLAN_SETTING_NAME;
device_class->create_and_realize = create_and_realize;
device_class->get_generic_capabilities = get_generic_capabilities;
device_class->ip4_config_pre_commit = ip4_config_pre_commit;
device_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
device_class->is_available = is_available;
device_class->link_changed = link_changed;
device_class->parent_changed_notify = parent_changed_notify;

View file

@ -118,6 +118,16 @@ void nm_device_ip_method_failed (NMDevice *self, int family, NMDeviceStateReason
gboolean nm_device_ipv6_sysctl_set (NMDevice *self, const char *property, const char *value);
/*****************************************************************************/
#define NM_DEVICE_DEFAULT_MTU_WIRED ((guint32) 1500)
#define NM_DEVICE_DEFAULT_MTU_WIRELESS ((guint32) 1500)
#define NM_DEVICE_DEFAULT_MTU_INFINIBAND ((guint32) 0)
guint32 nm_device_get_configured_mtu_for_wired (NMDevice *self, gboolean *out_is_user_config);
/*****************************************************************************/
#define NM_DEVICE_CLASS_DECLARE_TYPES(klass, conn_type, ...) \
NM_DEVICE_CLASS (klass)->connection_type = conn_type; \
{ \

View file

@ -313,24 +313,6 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason)
return NM_ACT_STAGE_RETURN_SUCCESS;
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
static void
unrealize_notify (NMDevice *device)
{
@ -434,7 +416,7 @@ nm_device_tun_class_init (NMDeviceTunClass *klass)
device_class->unrealize_notify = unrealize_notify;
device_class->update_connection = update_connection;
device_class->act_stage1_prepare = act_stage1_prepare;
device_class->ip4_config_pre_commit = ip4_config_pre_commit;
device_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
obj_properties[PROP_OWNER] =
g_param_spec_int64 (NM_DEVICE_TUN_OWNER, "", "",

View file

@ -572,24 +572,6 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason)
return ret;
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
/*****************************************************************************/
static void
@ -630,7 +612,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass)
parent_class->unrealize_notify = unrealize_notify;
parent_class->get_generic_capabilities = get_generic_capabilities;
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
parent_class->is_available = is_available;
parent_class->parent_changed_notify = parent_changed_notify;

View file

@ -512,24 +512,6 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason)
return NM_ACT_STAGE_RETURN_SUCCESS;
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
/*****************************************************************************/
static void
@ -622,7 +604,7 @@ nm_device_vxlan_class_init (NMDeviceVxlanClass *klass)
device_class->get_generic_capabilities = get_generic_capabilities;
device_class->update_connection = update_connection;
device_class->act_stage1_prepare = act_stage1_prepare;
device_class->ip4_config_pre_commit = ip4_config_pre_commit;
device_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
obj_properties[PROP_ID] =
g_param_spec_uint (NM_DEVICE_VXLAN_ID, "", "",

View file

@ -5182,6 +5182,16 @@ END_ADD_DEFAULT_ROUTE:
/* Allow setting MTU etc */
if (commit) {
gboolean mtu_is_user_config = FALSE;
guint32 mtu;
if (NM_DEVICE_GET_CLASS (self)->get_configured_mtu)
mtu = NM_DEVICE_GET_CLASS (self)->get_configured_mtu (self, &mtu_is_user_config);
else
mtu = 0;
if (mtu && mtu_is_user_config)
nm_ip4_config_set_mtu (composite, mtu, NM_IP_CONFIG_SOURCE_USER);
if (NM_DEVICE_GET_CLASS (self)->ip4_config_pre_commit)
NM_DEVICE_GET_CLASS (self)->ip4_config_pre_commit (self, composite);
}
@ -6589,6 +6599,35 @@ linklocal6_start (NMDevice *self)
/*****************************************************************************/
guint32
nm_device_get_configured_mtu_for_wired (NMDevice *self, gboolean *out_is_user_config)
{
NMConnection *connection;
NMSettingWired *setting;
guint32 mtu;
nm_assert (NM_IS_DEVICE (self));
nm_assert (out_is_user_config);
connection = nm_device_get_applied_connection (self);
if (!connection)
g_return_val_if_reached (0);
setting = nm_connection_get_setting_wired (connection);
if (setting) {
mtu = nm_setting_wired_get_mtu (setting);
if (mtu) {
*out_is_user_config = TRUE;
return mtu;
}
}
*out_is_user_config = FALSE;
return NM_DEVICE_DEFAULT_MTU_WIRED;
}
/*****************************************************************************/
static void
_set_mtu (NMDevice *self)
{

View file

@ -232,6 +232,8 @@ typedef struct {
NMConnection *connection,
char **specific_object);
guint32 (*get_configured_mtu) (NMDevice *self, gboolean *out_is_user_config);
/* Checks whether the connection is compatible with the device using
* only the devices type and characteristics. Does not use any live
* network information like WiFi scan lists etc.
@ -275,7 +277,6 @@ typedef struct {
NMActStageReturn (* act_stage4_ip6_config_timeout) (NMDevice *self,
NMDeviceStateReason *reason);
/* Called right before IP config is set; use for setting MTU etc */
void (* ip4_config_pre_commit) (NMDevice *self, NMIP4Config *config);
/* Async deactivating (in the DEACTIVATING phase) */

View file

@ -627,25 +627,6 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason)
NM_ACT_STAGE_RETURN_POSTPONE : NM_ACT_STAGE_RETURN_FAILURE;
}
static void
ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
{
NMConnection *connection;
NMSettingWired *s_wired;
guint32 mtu;
connection = nm_device_get_applied_connection (self);
g_assert (connection);
s_wired = nm_connection_get_setting_wired (connection);
if (s_wired) {
/* MTU override */
mtu = nm_setting_wired_get_mtu (s_wired);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
}
}
static void
deactivate (NMDevice *device)
{
@ -876,7 +857,7 @@ nm_device_team_class_init (NMDeviceTeamClass *klass)
parent_class->master_update_slave_connection = master_update_slave_connection;
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = nm_device_get_configured_mtu_for_wired;
parent_class->deactivate = deactivate;
parent_class->enslave_slave = enslave_slave;
parent_class->release_slave = release_slave;

View file

@ -2741,22 +2741,22 @@ act_stage3_ip6_config_start (NMDevice *device,
return NM_DEVICE_CLASS (nm_device_wifi_parent_class)->act_stage3_ip6_config_start (device, out_config, reason);
}
static void
ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
static guint32
get_configured_mtu (NMDevice *device, gboolean *out_is_user_config)
{
NMConnection *connection;
NMSettingWireless *s_wifi;
NMSettingWireless *setting;
guint32 mtu;
connection = nm_device_get_applied_connection (device);
g_assert (connection);
s_wifi = nm_connection_get_setting_wireless (connection);
g_assert (s_wifi);
nm_assert (NM_IS_DEVICE (device));
nm_assert (out_is_user_config);
/* MTU override */
mtu = nm_setting_wireless_get_mtu (s_wifi);
if (mtu)
nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
setting = NM_SETTING_WIRELESS (nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS));
if (!setting)
g_return_val_if_reached (0);
mtu = nm_setting_wireless_get_mtu (setting);
*out_is_user_config = (mtu != 0);
return mtu ?: NM_DEVICE_DEFAULT_MTU_WIRELESS;
}
static gboolean
@ -3215,7 +3215,7 @@ nm_device_wifi_class_init (NMDeviceWifiClass *klass)
parent_class->act_stage1_prepare = act_stage1_prepare;
parent_class->act_stage2_config = act_stage2_config;
parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
parent_class->get_configured_mtu = get_configured_mtu;
parent_class->act_stage3_ip4_config_start = act_stage3_ip4_config_start;
parent_class->act_stage3_ip6_config_start = act_stage3_ip6_config_start;
parent_class->act_stage4_ip4_config_timeout = act_stage4_ip4_config_timeout;