# #
###############################################################################
-function cli_config() {
- if cli_help_requested $@; then
- cli_show_man network-config
- exit ${EXIT_OK}
- fi
+IDENT=" "
- if [ -n "${1}" ]; then
- config_set $@
- network_config_write
- else
- network_config_print
+function cli_help_requested() {
+ local argument="${1}"
+
+ if [ -n "${argument}" ]; then
+ if listmatch ${argument} help -h --help; then
+ return ${EXIT_OK}
+ fi
fi
+
+ return ${EXIT_ERROR}
+}
+
+function cli_run_help() {
+ local command="$@"
+
+ print "Run \"${command} help\" to get more information."
+ return ${EXIT_OK}
}
-function cli_device() {
+function cli_device_headline() {
local device=${1}
- local action=${2}
- shift 2
+ assert isset device
- assert device_exists ${device}
+ local long=0
+ shift
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ --long)
+ long=1
+ ;;
+ esac
+ shift
+ done
- if zone_exists ${device} || port_exists ${device}; then
- error "The device '${device}' has already been configured."
- error "You cannot do a device action."
- return ${EXIT_ERROR}
+ local type
+ if zone_exists ${device}; then
+ type="zone"
+ elif port_exists ${device}; then
+ type="port"
+ else
+ type="unknown"
fi
- case "${action}" in
- discover)
- echo "# XXX need to implement --raw here"
- cli_device_discover ${device} $@
+ local headline_prefix
+ case "${type}" in
+ zone)
+ headline_prefix="Zone ${device}"
;;
-
- show|"")
- # XXX device_show needs to be implemented
- device_show ${device}
+ port)
+ headline_prefix="Port ${device} ($(device_get_type ${device}))"
;;
*)
- cli_show_man network-device
+ headline_prefix="Device ${device} ($(device_get_type ${device}))"
;;
esac
-}
-function cli_device_discover() {
- local device=${1}
- shift
+ # Print the hook for all zones.
+ if [ "${type}" = "zone" ]; then
+ headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
+ fi
+ cli_headline 1 "${headline_prefix}"
- local device_type=$(device_get_type ${device})
- if [ "${device_type}" != "real" ]; then
- return ${EXIT_OK}
+ # Print the device status.
+ local status
+ case "$(device_get_status ${device})" in
+ ${STATUS_UP})
+ status=${MSG_DEVICE_STATUS_UP}
+ ;;
+ ${STATUS_DOWN})
+ status=${MSG_DEVICE_STATUS_DOWN}
+ ;;
+ ${STATUS_NOCARRIER})
+ status=${MSG_DEVICE_STATUS_NOCARRIER}
+ ;;
+ *)
+ status=${MSG_DEVICE_STATUS_UNKNOWN}
+ ;;
+ esac
+ cli_print_fmt1 1 "Status" "${status}"
+ if enabled long; then
+ cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
+ fi
+ if device_is_up ${device}; then
+ cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
+ fi
+ if enabled long; then
+ device_is_promisc ${device} &>/dev/null
+ cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
fi
- local raw
+ cli_space
- while [ $# -gt 0 ]; do
- case "${1}" in
- --raw)
- raw=1
- ;;
- esac
- shift
- done
+ # Print the device stats.
+ device_is_up ${device} && cli_device_stats 2 ${device}
- local up
- device_is_up ${device} && up=1
- device_set_up ${device}
+ if enabled long; then
+ # Virtual devices.
+ device_is_vlan ${device} && cli_device_vlan ${device}
- enabled raw || echo "${device}"
+ # Bonded devices.
+ device_is_bonded ${device} && cli_device_bonded ${device}
- local hook
- local out
- local ret
- for hook in $(hook_zone_get_all); do
- out=$(hook_zone_exec ${hook} discover ${device})
- ret=$?
-
- [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
-
- if enabled raw; then
- case "${ret}" in
- ${DISCOVER_OK})
- echo "${hook}: OK"
- local line
- while read line; do
- echo "${hook}: ${line}"
- done <<<"${out}"
- ;;
-
- ${DISCOVER_ERROR})
- echo "${hook}: FAILED"
- ;;
- esac
- else
- case "${ret}" in
- ${DISCOVER_OK})
- echo " ${hook} was successful."
- local line
- while read line; do
- echo " ${line}"
- done <<<"${out}"
- ;;
-
- ${DISCOVER_ERROR})
- echo " ${hook} failed."
- ;;
- esac
- fi
- done
+ # Bonding devices.
+ device_is_bonding ${device} && cli_device_bonding ${device}
+ fi
+}
- echo # New line
+function cli_device_stats() {
+ local level=${1}
+ local device=${2}
- [ "${up}" = "1" ] || device_set_down ${device}
-}
+ # This section will print statistical data from the device.
+ local packets bytes errors
-function cli_hostname() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
- fi
+ cli_headline ${level} "Statistics"
+ local format="%-10s %9d packets %6s (%d errors)"
- local hostname=${1}
+ # RX
+ packets=$(device_get_rx_packets ${device})
+ bytes=$(device_get_rx_bytes ${device})
+ errors=$(device_get_rx_errors ${device})
- if [ -n "${hostname}" ]; then
- config_hostname ${hostname}
- log INFO "Hostname was set to '${hostname}'."
- log INFO "Changes do only take affect after reboot."
- exit ${EXIT_OK}
- fi
+ cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
- echo "$(config_hostname)"
- exit ${EXIT_OK}
+ # TX
+ packets=$(device_get_tx_packets ${device})
+ bytes=$(device_get_tx_bytes ${device})
+ errors=$(device_get_tx_errors ${device})
+
+ cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
+ cli_space
}
-function cli_port() {
- if cli_help_requested $@; then
- cli_show_man network-port
- exit ${EXIT_OK}
- fi
+function cli_device_vlan() {
+ local device=${1}
- local action
- local port
+ cli_headline 2 "VLAN"
- if port_exists ${1}; then
- port=${1}
- action=${2}
- shift 2
+ cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
+ cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
+ cli_space
+}
- # Action aliases
- case "${action}" in
- start)
- action="up"
- ;;
- stop)
- action="down"
- ;;
- show)
- action="status"
- ;;
- esac
+function cli_device_bonded() {
+ local device=${1}
- case "${action}" in
- edit|up|down|status)
- port_${action} ${port} $@
- ;;
- *)
- error "Unrecognized argument: ${action}"
- exit ${EXIT_ERROR}
- ;;
- esac
- else
- action=${1}
- shift
+ cli_headline 2 "Bonding information"
- case "${action}" in
- create|destroy)
- port_${action} $@
- ;;
- *)
- error "Unrecognized argument: ${action}"
- exit ${EXIT_ERROR}
- ;;
- esac
- fi
-}
+ local master=$(bonding_slave_get_master ${port})
+ cli_print_fmt1 2 "Master" "${master}"
-function cli_zone() {
- if cli_help_requested $@; then
- cli_show_man network-zone
- exit ${EXIT_OK}
+ local mode=$(bonding_get_mode ${master})
+ if [ "${mode}" = "active-backup" ]; then
+ local active_slaves=$(bonding_get_slaves ${master} --active)
+ local active="false"
+ if list_match "${device}" ${active_slaves}; then
+ active="true"
+ fi
+ cli_print_fmt1 2 "Active slave" "$(cli_print_yesno ${active})"
fi
- local action
- local zone
+ cli_space
+}
- if zone_name_is_valid ${1}; then
- zone=${1}
- action=${2}
- shift 2
+function cli_device_bonding() {
+ local device=${1}
+ assert isset device
- # Action aliases
- case "${action}" in
- start)
- action="up"
- ;;
- stop)
- action="down"
- ;;
- show)
- action="status"
- ;;
- esac
+ cli_headline 2 "Bonding information"
- case "${action}" in
- config|down|edit|port|status|up)
- zone_${action} ${zone} $@
- ;;
- *)
- error "Unrecognized argument: ${action}"
- cli_show_man network-zone
- exit ${EXIT_ERROR}
- ;;
- esac
- else
- action=${1}
- shift
+ local mode=$(bonding_get_mode ${device})
- case "${action}" in
- create)
- zone_${action} $@
+ cli_print_fmt1 2 "Mode" "${mode}"
+ if [ "${mode}" = "802.3ad" ]; then
+ local lacp_rate=$(bonding_get_lacp_rate ${device})
+ cli_print_fmt1 2 "LACP rate" "${lacp_rate}"
+ fi
+ cli_space
+
+ local slave slave_suffix
+ local active_slaves=$(bonding_get_slaves ${device} --active)
+ for slave in $(bonding_get_slaves ${device}); do
+ # Print the device status.
+ local status
+ case "$(device_get_status ${slave})" in
+ ${STATUS_UP})
+ status=${MSG_DEVICE_STATUS_UP}
;;
- remove)
- cli_zone_remove $@
+ ${STATUS_DOWN})
+ status=${MSG_DEVICE_STATUS_DOWN}
;;
- list-hooks)
- cli_list_hooks zone $@
+ ${STATUS_NOCARRIER})
+ status=${MSG_DEVICE_STATUS_NOCARRIER}
;;
- ""|*)
- if [ -n "${action}" ]; then
- error "Unrecognized argument: '${action}'"
- echo
- fi
-
- cli_show_man network-zone
- exit ${EXIT_ERROR}
+ *)
+ status=${MSG_DEVICE_STATUS_UNKNOWN}
;;
esac
- fi
-}
-
-function cli_list_hooks() {
- local type=${1}
- shift
- if cli_help_requested $@; then
- cli_show_man network-zone
- exit ${EXIT_OK}
- fi
-
- local hook_dir=$(hook_dir ${type})
- local hook
-
- for hook in ${hook_dir}/*; do
- hook=$(basename ${hook})
- if hook_exists ${type} ${hook}; then
- echo "${hook}"
+ if list_match "${slave}" ${active_slaves}; then
+ slave_suffix="(active)"
+ else
+ slave_suffix=""
fi
- done | sort -u
+ cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
+ done
+ cli_space
}
-# Removes a zone either immediately, if it is currently down,
-# or adds a tag that the removal will be done when the zone
-# is brought down the next time.
-function cli_zone_remove() {
- if cli_help_requested $@; then
- cli_show_man network-zone
- exit ${EXIT_OK}
- fi
-
- local zone=${1}
- assert zone_exists ${zone}
+function cli_headline() {
+ local level=${1}
+ local format=${2}
+ shift 2
- if zone_is_up ${zone}; then
- echo "Zone '${zone}' is up and will be removed when it goes down the next time."
- zone_remove ${zone}
- else
- echo "Removing zone '${zone}' now..."
- zone_remove_now ${zone}
- fi
+ local ident=$(cli_ident ${level})
- exit ${EXIT_OK}
+ local out
+ printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
+ printf "${out}"
}
-function cli_start() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
- fi
-
- local zones=$(zones_get $@)
+function cli_statusline() {
+ local level=${1}
+ shift
- local zone
- for zone in ${zones}; do
- zone_start ${zone} &
- done
+ local head=${1}
+ shift
- wait # until everything is settled
+ cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
}
-function cli_stop() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
- fi
+function cli_print() {
+ local level=${1}
+ local format=${2}
+ shift 2
- local zones=$(zones_get $@)
+ local ident=$(cli_ident $(( ${level} + 1 )))
- local zone
- for zone in ${zones}; do
- zone_stop ${zone} &
- done
-
- wait # until everything is settled
+ local out
+ printf -v out "${ident}${format}\n" "$@"
+ printf "${out}"
}
-function cli_restart() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
- fi
-
- cli_stop $@
+function cli_print_fmt1() {
+ local level=${1}
+ shift
- # Give the system some time to calm down
- sleep ${TIMEOUT_RESTART}
+ local space=$(( 34 - (${level} * ${#IDENT}) ))
+ local format="%-${space}s %s"
- cli_start $@
+ cli_print ${level} "${format}" "$@"
}
-function cli_status() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
+function cli_print_bool() {
+ if [ "${1}" = "${EXIT_TRUE}" ]; then
+ echo "true"
+ else
+ echo "false"
fi
-
- # When dumping status information, the debug
- # mode clutters the console which is not what we want.
- # Logging on the console is disabled for a short time.
- local log_disable_stdout=${LOG_DISABLE_STDOUT}
- LOG_DISABLE_STDOUT="true"
-
- local zones=$(zones_get $@)
-
- local zone
- for zone in ${zones}; do
- zone_status ${zone}
- done
-
- # Reset logging.
- LOG_DISABLE_STDOUT=${log_disable_stdout}
}
-function cli_reset() {
- if cli_help_requested $@; then
- cli_show_man network
- exit ${EXIT_OK}
- fi
-
- warning_log "Will reset the whole network configuration!!!"
-
- # Force mode is disabled by default
- local force=0
-
- while [ $# -gt 0 ]; do
- case "${1}" in
- --force|-f)
- force=1
- ;;
- esac
- shift
- done
-
- # If we are not running in force mode, we ask the user if he does know
- # what he is doing.
- if ! enabled force; then
- if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
- exit ${EXIT_ERROR}
- fi
+function cli_print_yesno() {
+ if [ "${1}" = "${EXIT_TRUE}" ]; then
+ echo "yes"
+ else
+ echo "false"
fi
+}
- local zone
- for zone in $(zones_get --all); do
- zone_remove ${zone}
- done
-
- local port
- for port in $(ports_get --all); do
- port_remove ${port}
- done
-
- # Re-run the initialization functions
- init_run
+function cli_print_enabled() {
+ enabled ${1}
- exit ${EXIT_OK}
+ cli_print_bool $?
}
-function cli_help_requested() {
- local argument="${1}"
-
- if [ -n "${argument}" ]; then
- if listmatch ${argument} help -h --help; then
- return ${EXIT_OK}
- fi
- fi
+function cli_print_warning() {
+ local level=${1}
+ shift
- return ${EXIT_ERROR}
+ cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
}
-function cli_status_headline() {
- local zone=${1}
+function cli_space() {
+ printf "\n"
+}
- local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
- zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
+function cli_ident() {
+ local level=${1}
+ assert isinteger level
- echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
-}
+ local ident=""
+ while [ ${level} -gt 1 ]; do
+ ident="${ident}${IDENT}"
+ level=$(( ${level} - 1 ))
+ done
-function cli_headline() {
- echo
- echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
+ print "${ident}"
}
function cli_yesno() {
}
function cli_get_val() {
- echo "${@##*=}"
+ echo "${@#*=}"
}
function cli_usage() {