static void update_connection (NMDevice *device, NMConnection *connection) { NMDeviceTeam *self = NM_DEVICE_TEAM (device); NMSettingTeam *s_team = nm_connection_get_setting_team (connection); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); if (!s_team) { s_team = (NMSettingTeam *) nm_setting_team_new (); nm_connection_add_setting (connection, (NMSetting *) s_team); } g_object_set (G_OBJECT (s_team), NM_SETTING_TEAM_CONFIG, NULL, NULL); if (priv->tdc) { const char *config = NULL; int err; err = teamdctl_config_get_raw_direct (NM_DEVICE_TEAM_GET_PRIVATE (device)->tdc, (char **)&config); if (err == 0) g_object_set (G_OBJECT (s_team), NM_SETTING_TEAM_CONFIG, config, NULL); else _LOGE (LOGD_TEAM, "failed to read teamd config (err=%d)", err); } }
static void teamd_dbus_vanished (GDBusConnection *connection, const gchar *name, gpointer user_data) { NMDevice *dev = NM_DEVICE (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (dev); g_return_if_fail (priv->teamd_dbus_watch); if (priv->teamd_timeout) { /* g_bus_watch_name will always raise an initial signal, to indicate whether the * name exists/not exists initially. Do not take this as a failure, until the * startup timeout is over. * * Note that g_bus_watch_name is guaranteed to alternate vanished/appeared signals, * so we won't hit this condition again (because the next signal is either 'appeared' * or 'timeout'). */ nm_log_dbg (LOGD_TEAM, "(%s): teamd vanished from D-Bus (ignored)", nm_device_get_iface (dev)); return; } nm_log_info (LOGD_TEAM, "(%s): teamd vanished from D-Bus", nm_device_get_iface (dev)); teamd_cleanup (dev, TRUE); }
/** * nm_device_team_get_slaves: * @device: a #NMDeviceTeam * * Gets the devices currently enslaved to @device. * * Returns: (element-type NMDevice): the #GPtrArray containing * #NMDevices that are slaves of @device. This is the internal * copy used by the device, and must not be modified. **/ const GPtrArray * nm_device_team_get_slaves (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE); return NM_DEVICE_TEAM_GET_PRIVATE (device)->slaves; }
static void update_connection (NMDevice *device, NMConnection *connection) { NMSettingTeam *s_team = nm_connection_get_setting_team (connection); const char *iface = nm_device_get_iface (device); if (!s_team) { s_team = (NMSettingTeam *) nm_setting_team_new (); nm_connection_add_setting (connection, (NMSetting *) s_team); g_object_set (G_OBJECT (s_team), NM_SETTING_TEAM_INTERFACE_NAME, iface, NULL); } g_object_set (G_OBJECT (s_team), NM_SETTING_TEAM_CONFIG, NULL, NULL); #if WITH_TEAMDCTL if (ensure_teamd_connection (device)) { const char *config = NULL; int err; err = teamdctl_config_get_raw_direct (NM_DEVICE_TEAM_GET_PRIVATE (device)->tdc, (char **)&config); if (err == 0) g_object_set (G_OBJECT (s_team), NM_SETTING_TEAM_CONFIG, config, NULL); else nm_log_err (LOGD_TEAM, "(%s): failed to read teamd config (err=%d)", iface, err); } #endif }
static void teamd_process_watch_cb (GPid pid, gint status, gpointer user_data) { NMDeviceTeam *self = NM_DEVICE_TEAM (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); NMDevice *device = NM_DEVICE (self); NMDeviceState state = nm_device_get_state (device); g_return_if_fail (priv->teamd_process_watch); _LOGD (LOGD_TEAM, "teamd died with status %d", status); priv->teamd_pid = 0; priv->teamd_process_watch = 0; /* If teamd quit within 5 seconds of starting, it's probably hosed * and will just die again, so fail the activation. */ if (priv->teamd_timeout && (state >= NM_DEVICE_STATE_PREPARE) && (state <= NM_DEVICE_STATE_ACTIVATED)) { _LOGW (LOGD_TEAM, "teamd process quit unexpectedly; failing activation"); teamd_cleanup (device, TRUE); nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED); } }
/** * nm_device_team_get_config: * @device: a #NMDeviceTeam * * Gets the current JSON configuration of the #NMDeviceTeam * * Returns: the current configuration. This is the internal string used by the * device, and must not be modified. * * Since: 1.4 **/ const char * nm_device_team_get_config (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), NULL); return nm_str_not_empty (NM_DEVICE_TEAM_GET_PRIVATE (device)->config); }
/** * nm_device_team_get_hw_address: * @device: a #NMDeviceTeam * * Gets the hardware (MAC) address of the #NMDeviceTeam * * Returns: the hardware address. This is the internal string used by the * device, and must not be modified. **/ const char * nm_device_team_get_hw_address (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), NULL); return NM_DEVICE_TEAM_GET_PRIVATE (device)->hw_address; }
static void teamd_dbus_vanished (GDBusConnection *dbus_connection, const gchar *name, gpointer user_data) { NMDeviceTeam *self = NM_DEVICE_TEAM (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); NMDevice *device = NM_DEVICE (self); NMDeviceState state = nm_device_get_state (device); g_return_if_fail (priv->teamd_dbus_watch); if (!priv->tdc) { /* g_bus_watch_name will always raise an initial signal, to indicate whether the * name exists/not exists initially. Do not take this as a failure if it hadn't * previously appeared. */ _LOGD (LOGD_TEAM, "teamd not on D-Bus (ignored)"); return; } _LOGI (LOGD_TEAM, "teamd vanished from D-Bus"); teamd_cleanup (device, TRUE); /* Attempt to respawn teamd */ if (state >= NM_DEVICE_STATE_PREPARE && state <= NM_DEVICE_STATE_ACTIVATED) { NMConnection *connection = nm_device_get_applied_connection (device); g_assert (connection); if (!teamd_start (device, nm_connection_get_setting_team (connection))) nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED); } }
/** * nm_device_team_get_carrier: * @device: a #NMDeviceTeam * * Whether the device has carrier. * * Returns: %TRUE if the device has carrier **/ gboolean nm_device_team_get_carrier (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE); return NM_DEVICE_TEAM_GET_PRIVATE (device)->carrier; }
static void teamd_cleanup (NMDevice *device, gboolean free_tdc) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device); if (priv->teamd_process_watch) { g_source_remove (priv->teamd_process_watch); priv->teamd_process_watch = 0; } if (priv->teamd_timeout) { g_source_remove (priv->teamd_timeout); priv->teamd_timeout = 0; } if (priv->teamd_pid > 0) { nm_utils_kill_child_async (priv->teamd_pid, SIGTERM, LOGD_TEAM, "teamd", 2000, NULL, NULL); priv->teamd_pid = 0; } if (priv->tdc && free_tdc) { teamdctl_disconnect (priv->tdc); teamdctl_free (priv->tdc); priv->tdc = NULL; } }
static void deactivate (NMDevice *dev) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (dev); teamd_stop (dev, priv); }
static void nm_device_team_init (NMDeviceTeam *device) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device); priv->slaves = g_ptr_array_new (); }
static gboolean enslave_slave (NMDevice *device, NMDevice *slave, NMConnection *connection, gboolean configure) { #if WITH_TEAMDCTL NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device); #endif gboolean success = TRUE, no_firmware = FALSE; const char *iface = nm_device_get_ip_iface (device); const char *slave_iface = nm_device_get_ip_iface (slave); NMSettingTeamPort *s_team_port; nm_device_master_check_slave_physical_port (device, slave, LOGD_TEAM); if (configure) { nm_device_take_down (slave, TRUE); s_team_port = nm_connection_get_setting_team_port (connection); if (s_team_port) { const char *config = nm_setting_team_port_get_config (s_team_port); if (config) { #if WITH_TEAMDCTL if (!priv->tdc) { nm_log_warn (LOGD_TEAM, "(%s): enslaved team port %s config not changed, not connected to teamd", iface, slave_iface); } else { int err; char *sanitized_config; sanitized_config = g_strdelimit (g_strdup (config), "\r\n", ' '); err = teamdctl_port_config_update_raw (priv->tdc, slave_iface, sanitized_config); g_free (sanitized_config); if (err != 0) { nm_log_err (LOGD_TEAM, "(%s): failed to update config for port %s (err=%d)", iface, slave_iface, err); return FALSE; } } #else nm_log_warn (LOGD_TEAM, "(%s): enslaved team port %s config not changed due to lack of Teamd control support", iface, slave_iface); #endif } } success = nm_platform_link_enslave (nm_device_get_ip_ifindex (device), nm_device_get_ip_ifindex (slave)); nm_device_bring_up (slave, TRUE, &no_firmware); } if (success) { nm_log_info (LOGD_TEAM, "(%s): enslaved team port %s", iface, slave_iface); g_object_notify (G_OBJECT (device), "slaves"); } return success; }
/** * nm_device_team_get_hw_address: * @device: a #NMDeviceTeam * * Gets the hardware (MAC) address of the #NMDeviceTeam * * Returns: the hardware address. This is the internal string used by the * device, and must not be modified. * * Since: 0.9.10 **/ const char * nm_device_team_get_hw_address (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), NULL); _nm_object_ensure_inited (NM_OBJECT (device)); return NM_DEVICE_TEAM_GET_PRIVATE (device)->hw_address; }
/** * nm_device_team_get_carrier: * @device: a #NMDeviceTeam * * Whether the device has carrier. * * Returns: %TRUE if the device has carrier * * Since: 0.9.10 **/ gboolean nm_device_team_get_carrier (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE); _nm_object_ensure_inited (NM_OBJECT (device)); return NM_DEVICE_TEAM_GET_PRIVATE (device)->carrier; }
/** * nm_device_team_get_slaves: * @device: a #NMDeviceTeam * * Gets the devices currently enslaved to @device. * * Returns: (element-type NMClient.Device): the #GPtrArray containing * #NMDevices that are slaves of @device. This is the internal * copy used by the device, and must not be modified. * * Since: 0.9.10 **/ const GPtrArray * nm_device_team_get_slaves (NMDeviceTeam *device) { g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE); _nm_object_ensure_inited (NM_OBJECT (device)); return handle_ptr_array_return (NM_DEVICE_TEAM_GET_PRIVATE (device)->slaves); }
static void dispose (GObject *object) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); g_clear_pointer (&priv->slaves, g_ptr_array_unref); G_OBJECT_CLASS (nm_device_team_parent_class)->dispose (object); }
static void finalize (GObject *object) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); g_free (priv->hw_address); G_OBJECT_CLASS (nm_device_team_parent_class)->finalize (object); }
static gboolean enslave_slave (NMDevice *device, NMDevice *slave, NMConnection *connection, gboolean configure) { NMDeviceTeam *self = NM_DEVICE_TEAM (device); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device); gboolean success = TRUE, no_firmware = FALSE; const char *slave_iface = nm_device_get_ip_iface (slave); NMSettingTeamPort *s_team_port; nm_device_master_check_slave_physical_port (device, slave, LOGD_TEAM); if (configure) { nm_device_take_down (slave, TRUE); s_team_port = nm_connection_get_setting_team_port (connection); if (s_team_port) { const char *config = nm_setting_team_port_get_config (s_team_port); if (config) { if (!priv->tdc) { _LOGW (LOGD_TEAM, "enslaved team port %s config not changed, not connected to teamd", slave_iface); } else { int err; char *sanitized_config; sanitized_config = g_strdelimit (g_strdup (config), "\r\n", ' '); err = teamdctl_port_config_update_raw (priv->tdc, slave_iface, sanitized_config); g_free (sanitized_config); if (err != 0) { _LOGE (LOGD_TEAM, "failed to update config for port %s (err=%d)", slave_iface, err); return FALSE; } } } } success = nm_platform_link_enslave (NM_PLATFORM_GET, nm_device_get_ip_ifindex (device), nm_device_get_ip_ifindex (slave)); nm_device_bring_up (slave, TRUE, &no_firmware); if (!success) return FALSE; _LOGI (LOGD_TEAM, "enslaved team port %s", slave_iface); } else _LOGI (LOGD_TEAM, "team port %s was enslaved", slave_iface); g_object_notify (G_OBJECT (device), NM_DEVICE_TEAM_SLAVES); return TRUE; }
static void constructed (GObject *object) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); G_OBJECT_CLASS (nm_device_team_parent_class)->constructed (object); priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_TEAM); register_properties (NM_DEVICE_TEAM (object)); }
static void teamd_timeout_remove (NMDevice *dev) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (dev); if (priv->teamd_timeout) { g_source_remove (priv->teamd_timeout); priv->teamd_timeout = 0; } }
static void teamd_dbus_appeared (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data) { NMDeviceTeam *self = NM_DEVICE_TEAM (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); NMDevice *device = NM_DEVICE (self); gboolean success; g_return_if_fail (priv->teamd_dbus_watch); _LOGI (LOGD_TEAM, "teamd appeared on D-Bus"); nm_device_queue_recheck_assume (device); /* If another teamd grabbed the bus name while our teamd was starting, * just ignore the death of our teamd and run with the existing one. */ if (priv->teamd_process_watch) { gs_unref_variant GVariant *ret = NULL; guint32 pid; ret = g_dbus_connection_call_sync (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionUnixProcessID", g_variant_new ("(s)", name_owner), NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, 2000, NULL, NULL); g_variant_get (ret, "(u)", &pid); if (pid != priv->teamd_pid) teamd_cleanup (device, FALSE); } /* Grab a teamd control handle even if we aren't going to use it * immediately. But if we are, and grabbing it failed, fail the * device activation. */ success = ensure_teamd_connection (device); if (nm_device_get_state (device) == NM_DEVICE_STATE_PREPARE) { if (success) nm_device_activate_schedule_stage2_device_config (device); else if (!nm_device_uses_assumed_connection (device)) nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED); } }
static void deactivate (NMDevice *device) { NMDeviceTeam *self = NM_DEVICE_TEAM (device); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); if (priv->teamd_pid || priv->tdc) _LOGI (LOGD_TEAM, "deactivation: stopping teamd..."); if (!priv->teamd_pid) teamd_kill (self, NULL, NULL); teamd_cleanup (device, TRUE); }
static gboolean teamd_timeout_cb (gpointer user_data) { NMDevice *dev = NM_DEVICE (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (dev); g_return_val_if_fail (priv->teamd_timeout, FALSE); nm_log_info (LOGD_TEAM, "(%s): teamd timed out.", nm_device_get_iface (dev)); teamd_cleanup (dev, TRUE); return FALSE; }
static void teamd_process_watch_cb (GPid pid, gint status, gpointer user_data) { NMDevice *dev = NM_DEVICE (user_data); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (dev); g_return_if_fail (priv->teamd_process_watch); nm_log_info (LOGD_TEAM, "(%s): teamd died", nm_device_get_iface (dev)); priv->teamd_process_watch = 0; priv->teamd_pid = 0; teamd_cleanup (dev, TRUE); }
static NMActStageReturn act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) { NMDeviceTeam *self = NM_DEVICE_TEAM (device); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS; gs_free_error GError *error = NULL; NMConnection *connection; NMSettingTeam *s_team; const char *cfg; g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE); ret = NM_DEVICE_CLASS (nm_device_team_parent_class)->act_stage1_prepare (device, reason); if (ret != NM_ACT_STAGE_RETURN_SUCCESS) return ret; connection = nm_device_get_applied_connection (device); g_assert (connection); s_team = nm_connection_get_setting_team (connection); g_assert (s_team); if (priv->tdc) { /* If the existing teamd config is the same as we're about to use, * then we can proceed. If it's not the same, and we have a PID, * kill it so we can respawn it with the right config. If we don't * have a PID, then we must fail. */ cfg = teamdctl_config_get_raw (priv->tdc); if (cfg && strcmp (cfg, nm_setting_team_get_config (s_team)) == 0) { _LOGD (LOGD_TEAM, "using existing matching teamd config"); return NM_ACT_STAGE_RETURN_SUCCESS; } if (!priv->teamd_pid) { _LOGD (LOGD_TEAM, "existing teamd config mismatch; killing existing via teamdctl"); if (!teamd_kill (self, NULL, &error)) { _LOGW (LOGD_TEAM, "existing teamd config mismatch; failed to kill existing teamd: %s", error->message); *reason = NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED; return NM_ACT_STAGE_RETURN_FAILURE; } } _LOGD (LOGD_TEAM, "existing teamd config mismatch; respawning..."); teamd_cleanup (device, TRUE); } return teamd_start (device, s_team) ? NM_ACT_STAGE_RETURN_POSTPONE : NM_ACT_STAGE_RETURN_FAILURE; }
static void dispose (GObject *object) { NMDevice *device = NM_DEVICE (object); NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); if (priv->teamd_dbus_watch) { g_bus_unwatch_name (priv->teamd_dbus_watch); priv->teamd_dbus_watch = 0; } teamd_cleanup (device, TRUE); G_OBJECT_CLASS (nm_device_team_parent_class)->dispose (object); }
static void dispose (GObject *object) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); g_clear_object (&priv->proxy); if (priv->slaves) { g_ptr_array_set_free_func (priv->slaves, g_object_unref); g_ptr_array_free (priv->slaves, TRUE); priv->slaves = NULL; } G_OBJECT_CLASS (nm_device_team_parent_class)->dispose (object); }
static void register_properties (NMDeviceTeam *device) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device); const NMPropertiesInfo property_info[] = { { NM_DEVICE_TEAM_HW_ADDRESS, &priv->hw_address }, { NM_DEVICE_TEAM_CARRIER, &priv->carrier }, { NM_DEVICE_TEAM_SLAVES, &priv->slaves, NULL, NM_TYPE_DEVICE }, { NULL }, }; _nm_object_register_properties (NM_OBJECT (device), priv->proxy, property_info); }
static void init_dbus (NMObject *object) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object); const NMPropertiesInfo property_info[] = { { NM_DEVICE_TEAM_HW_ADDRESS, &priv->hw_address }, { NM_DEVICE_TEAM_CARRIER, &priv->carrier }, { NM_DEVICE_TEAM_SLAVES, &priv->slaves, NULL, NM_TYPE_DEVICE }, { NULL }, }; NM_OBJECT_CLASS (nm_device_team_parent_class)->init_dbus (object); _nm_object_register_properties (object, NM_DBUS_INTERFACE_DEVICE_TEAM, property_info); }