cli: add support for 'nmcli con up ifname XXX'

Passes a NULL connection to nm_client_activate_connection() allowing
NetworkManager to pick the best available connection for the interface.
This commit is contained in:
Dan Williams
2013-09-06 10:05:47 -05:00
parent 4ec5f5c8e3
commit 42c7ea85a1
3 changed files with 57 additions and 33 deletions

View File

@@ -502,6 +502,13 @@ _nmcli_complete_COMMAND_CONNECTION()
fi fi
words=("${words[@]:2}") words=("${words[@]:2}")
;; ;;
ifname)
if [[ ${#words[@]} -eq 2 ]]; then
_nmcli_list_nl "$(_nmcli_NM_devices)"
return 0
fi
words=("${words[@]:2}")
;;
*) *)
COMMAND_CONNECTION_TYPE= COMMAND_CONNECTION_TYPE=
COMMAND_CONNECTION_ID="${words[0]}" COMMAND_CONNECTION_ID="${words[0]}"
@@ -601,12 +608,18 @@ _nmcli()
;; ;;
u|up) u|up)
if [[ ${#words[@]} -eq 3 ]]; then if [[ ${#words[@]} -eq 3 ]]; then
_nmcli_list_nl "$(printf "id\nuuid\npath\n%s" "$(_nmcli_con_id)")" _nmcli_list_nl "$(printf "ifname\nid\nuuid\npath\n%s" "$(_nmcli_con_id)")"
elif [[ ${#words[@]} -gt 3 ]]; then elif [[ ${#words[@]} -gt 3 ]]; then
local COMMAND_CONNECTION_TYPE=''
words=("${words[@]:2}") words=("${words[@]:2}")
OPTIONS=(id uuid path) OPTIONS=(ifname id uuid path)
_nmcli_complete_COMMAND_CONNECTION && return 0 _nmcli_complete_COMMAND_CONNECTION && return 0
OPTIONS=(ifname ap nsp)
if [[ "$COMMAND_CONNECTION_TYPE" = "ifname" ]]; then
OPTIONS=(ap nsp)
else
OPTIONS=(ifname ap nsp)
fi
_nmcli_complete_COMMAND_ARGS _nmcli_complete_COMMAND_ARGS
fi fi
;; ;;

View File

@@ -218,9 +218,9 @@ usage (void)
" show configured [[ id | uuid | path ] <ID>]\n\n" " show configured [[ id | uuid | path ] <ID>]\n\n"
" show active [[ id | uuid | path | apath ] <ID>]\n\n" " show active [[ id | uuid | path | apath ] <ID>]\n\n"
#if WITH_WIMAX #if WITH_WIMAX
" up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>]\n\n" " up [[ id | uuid | path ] <ID>] [ifname <ifname>] [ap <BSSID>] [nsp <name>]\n\n"
#else #else
" up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>]\n\n" " up [[ id | uuid | path ] <ID>] [ifname <ifname>] [ap <BSSID>]\n\n"
#endif #endif
" down [ id | uuid | path | apath ] <ID>\n\n" " down [ id | uuid | path | apath ] <ID>\n\n"
" add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS\n\n" " add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS\n\n"
@@ -1517,18 +1517,31 @@ nmc_activate_connection (NmCli *nmc,
GError *local = NULL; GError *local = NULL;
g_return_val_if_fail (nmc != NULL, FALSE); g_return_val_if_fail (nmc != NULL, FALSE);
g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); g_return_val_if_fail (NM_IS_CONNECTION (connection) || ifname, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (nm_connection_get_virtual_iface_name (connection)) if (connection) {
is_virtual = TRUE; if (nm_connection_get_virtual_iface_name (connection))
is_virtual = TRUE;
device_found = find_device_for_connection (nmc, connection, ifname, ap, nsp, &device, &spec_object, &local); device_found = find_device_for_connection (nmc, connection, ifname, ap, nsp, &device, &spec_object, &local);
/* Virtual connection may not have their interfaces created yet */ /* Virtual connection may not have their interfaces created yet */
if (!device_found && !is_virtual) { if (!device_found && !is_virtual) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_CON_ACTIVATION, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_CON_ACTIVATION,
"%s", local && local->message ? local->message : _("unknown error")); "%s", local && local->message ? local->message : _("unknown error"));
g_clear_error (&local); g_clear_error (&local);
return FALSE;
}
} else if (ifname) {
device = nm_client_get_device_by_iface (nmc->client, ifname);
if (!device) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_CON_ACTIVATION,
_("unknown device '%s'."), ifname);
return FALSE;
}
} else {
g_set_error_literal (error, NMCLI_ERROR, NMC_RESULT_ERROR_CON_ACTIVATION,
_("no connection and no device given."));
return FALSE; return FALSE;
} }
@@ -1554,7 +1567,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
const char *nsp = NULL; const char *nsp = NULL;
GError *error = NULL; GError *error = NULL;
const char *selector = NULL; const char *selector = NULL;
const char *name; const char *name = NULL;
char *line = NULL; char *line = NULL;
/* /*
@@ -1570,12 +1583,8 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
name = line ? line : ""; name = line ? line : "";
// TODO: enhancement: when just Enter is pressed (line is NULL), list // TODO: enhancement: when just Enter is pressed (line is NULL), list
// available connections so that the user can select one // available connections so that the user can select one
} else {
g_string_printf (nmc->return_text, _("Error: No connection specified."));
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
} }
} else { } else if (strcmp (*argv, "ifname") != 0) {
if ( strcmp (*argv, "id") == 0 if ( strcmp (*argv, "id") == 0
|| strcmp (*argv, "uuid") == 0 || strcmp (*argv, "uuid") == 0
|| strcmp (*argv, "path") == 0) { || strcmp (*argv, "path") == 0) {
@@ -1589,16 +1598,11 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
name = *argv; name = *argv;
} }
name = *argv; name = *argv;
next_arg (&argc, &argv);
} }
connection = find_connection (nmc->system_connections, selector, name); if (name)
connection = find_connection (nmc->system_connections, selector, name);
if (!connection) {
g_string_printf (nmc->return_text, _("Error: Unknown connection: %s."), name);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
goto error;
}
next_arg (&argc, &argv);
while (argc > 0) { while (argc > 0) {
if (strcmp (*argv, "ifname") == 0) { if (strcmp (*argv, "ifname") == 0) {

View File

@@ -304,14 +304,20 @@ When no command is given to the \fIconnection\fP object, the default action
is 'nmcli connection show configured'. is 'nmcli connection show configured'.
.TP .TP
.B up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>] .B up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>]
.RE
.RS
.B up ifname <ifname> [ap <BSSID>] [nsp <name>]
.RS
.br .br
Activate a connection. The connection is identified by its name, UUID or D-Bus Activate a connection. The connection is identified by its name, UUID or D-Bus
path. If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP or \fIpath\fP can be path. If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP or \fIpath\fP can be
used. When requiring a particular device to activate the connection on, the used. When requiring a particular device to activate the connection on, the
\fIifname\fP option with interface name should be given. In case of a VPN \fIifname\fP option with interface name should be given. If the <ID> is not
connection, the \fIifname\fP option specify the device of the base connection. given an \fIifname\fP is required, and NetworkManager will activate the best
The \fIap\fP option specify what particular AP should be used in case of available connection for the given \fIifname\fP. In case of a VPN connection,
a Wi\(hyFi connection. the \fIifname\fP option specifies the device of the base connection. The
\fIap\fP option specify what particular AP should be used in case of a Wi\(hyFi
connection.
.br .br
If '--wait' option is not specified, the default timeout will be 90 seconds. If '--wait' option is not specified, the default timeout will be 90 seconds.
.br .br
@@ -326,6 +332,7 @@ Available options are:
.IP \fInsp\fP 13 .IP \fInsp\fP 13
\(en NSP (Network Service Provider) which the command should connect to (for WiMAX connections) \(en NSP (Network Service Provider) which the command should connect to (for WiMAX connections)
.RE .RE
.RE
.TP .TP
.B down [ id | uuid | path | apath ] <ID> .B down [ id | uuid | path | apath ] <ID>
.br .br