Zebra shell Command Line Interface
Zebra CLI
The Zebra shell Command Line Interface is comprised of several components. This interface aggregates control for
the many daemons and processes that use CLIs into one shell, collecting all commands into a command tree.
Commands throughout Zebra shell, therefore, must have unique names.
Actual Zebra Command
This command shows the basic structure of a command. (ex. vtysh/user_llp.c) Complete details of the function
body are omitted for brevity.
DEFUN( setSAToPChan,
setSAToPChan_cmd,
"set satop <1-8> clock (internal|adaptive|differenial) cdv <2000-32000>",
"Set SAToP Service\n"
"SAToP option\n"
"Number of Service : 1 ~ 8\n"
"Clock option\n"
"Internal(Synchronous) Clock Mode\n"
"Adataive(Asynchronous) Clock Mode\n"
"Differential(Asynchronous) Clock Mode\n"
"CDV(Cell Delay Variation) option\n"
"CDV Size: 2000us ~ 32000us\n")
{
....
/*** function body ***/
if (!ok)
return CMD_WARNING;
return CMD_SUCCESS;
}
In the user_llp.c file, the following function installs the “set satop <1-8> clock (internal|adaptive|differenial) cdv
<2000-32000>" command:
/* CLI init functions. */
void reg_element_llp (void)
{
install_element (ENABLE_NODE, &setSAToPChan_cmd);
install_element (ENABLE_NODE, &setSAToPChanStatus_cmd);
install_element (ENABLE_NODE, &configure_tdmInterface_cmd);
install_element (ENABLE_NODE, &configure_satop_cmd);
install_element (ENABLE_NODE, &show_tdmInterface_cmd);
install_element (ENABLE_NODE, &show_satop_cmd);
install_element (ENABLE_NODE, &set_liu_loopback_cmd);
}
Skeleton Zebra shell Command
A command is defined by the DEFUN macro. The ALIAS macro may also be used. The DEFUN macro defines the
several parts of a command, and must be followed by the function body of the command. The ALIAS defines an
alternate command, and must follow the DEFUN it aliases.
Generally, commands are defined in the ANSI C language source-code files named lib/command.c. The file,
lib/command.h, contains the definition of the DEFUN and ALIAS macros. The C source files for CLI commands
include the file command.
DEFUN Macro
A DEFUN macro has four parts: function, structure, command string, and help strings. A macro instance is followed
by a function body.
DEFUN (function,
structure,
command_string,
help_string_1,
help_string_2,
help_string_3,
...)
{
function body;
return /*CMD_SUCCESS or CMD_WARNING;*/
}
Where:
function is the name and definition of the function.
structure is the data structure name, which is installed into the command tree to make the command available to
users.
command_string is the actual command string that the user types in. It is contained in quotes.
help_strings are the quoted, comma-delimited strings that provide help for each token of the command.
function body is the code that performs the work of the command.
The return value must be either CMD_SUCCESS or CMD_WARNING. Function is defined below. It has three
arguments: cli, argc and argv.
The DEFUN macro expands into this:
/* Function prototype. */
int func_name (struct cli *, int, char**);
/* Help string array. */
char *cli_name ## _help[] = {__VA_ARGS__, NULL};
/* Define CLI structure. */
struct cli_element cli_name =
{
/* Command line string. */
cmd_str,
/* Function pointer. */
func_name,
/* Help string is defined as an array. Last must be NULL. */
cli_name ## _help
};
/* Start function body at here. */ \
int func_name (struct cli *cli, int argc, char** argv)
{
function body;
return /*CMD_SUCCESS or CMD_WARNING;*/
}
Where:
The argument, cli is a pointer to the structure, struct cli, and has some important members. Typically, cli->mode
and cli->index. The cli->mode member keeps the current mode. The cli->index member keeps specific values for
the current mode. However, the use of the cli structure member is left to the implementor.
The argument, argc, contains the number of arguments in a CLI.
The argument, argv, contains a character pointer to the actual array of arguments. The use of this is the same as
a main C function. When an argument matches a non-keyword token, it is passed to the function as an argument.
When keywords match one of these special grouping tokens (), {} or [], matching keywords are also passed as
arguments.
You can specify an argument using any pre-defined available syntax. For details, refer to the Zebra shell
Command Syntax Description section as belows.
The following is an example of the OSPFv2 network command, which has two arguments:
• Argument argv[0] contains a pointer to the A.B.C.D/M argument string.
• Argument argv[1] contains a pointer to the A.B.C.D|<0-4294967295> argument string.
DEFUN (network_area,
network_area_cmd,
"network A.B.C.D/M area (A.B.C.D|<0-4294967295>)",
"Enable routing on an IP network",
"OSPF network prefix",
"Set the OSPF area ID",
"OSPF area ID in IP address format",
"OSPF area ID as a decimal value")
struct ospf *top = cli->index;
struct pal_in4_addr area_id;
struct prefix_ipv4 p;
int ret, format;
/* Get network prefix. */
CLI_GET_IPV4_PREFIX ("network prefix", p, argv[0]);
/* Get Area ID. */
CLI_GET_OSPF_AREA_ID (area_id, format, argv[1]);
{
ALIAS Macro
The ALIAS macro form is identical to the DEFUN macro, except there is no function body. Hence, this macro
follows the DEFUN macro instance for which it is an alias. Typical usage of ALIAS macro is to define another user
input string to an existing
DEFUN macro and its function.
ALIAS (function,
structure,
command_string,
help_string_1,
help_string_2,
help_string_3,
...)
Match to word (not include space or tab)
Match to the end of line (including space and tab)
IPv4 address match
IPv4 address and mask match
IPv6 adress match
IPv6 address and mask match
Time format match
BGP community value match
Zebra shell Command Syntax Description
When defining a command string, use the following tokens for special matching.
WORD
LINE
A.B.C.D
A.B.C.D/M
X:X: :X:X
X:X: :X:X/M
HH:MM:SS
AA:NN
Special grouping and repeating tokens are defined for use in a CLI or ALI macro instance:
( )
{ }
[ ]
.
?
An interface name is expanded to the FastEthernet <0-4> style. To do that, an IFNAME token is defined.
IFNAME Expanded to the predefined interface name defined in cli.h.
Grouping Tokens: parentheses ( )
Command grouping can be done by parenthesis. Grouping can be used to match multiple user input.
Grouping
Selective recursive
Infinite recursive
Simple infinite recursive
Selective keyword in infinite recursive
(in|out) means in or out.
(deny|permit) means deny or permit.
(internal|external lsa) means internal or external lsa.
The grouping can be nested like this.
(internal | external (1|2) ) means internal or external 1 or external 2
Any token can be used as a member of the group.
network A.B.C.D/M area (A.B.C.D|<0-4294967295>)
In this case, (A.B.C.D|<0-4294967295>) matches an IPv4 address or an integer range from 0 to 4294967295.
Grouping can be used to define optional parameters. When the no keyword is specified as a member of the
grouping,
it does not match with anything.
debug rip packet recv (detail| )
This matches with either debug rip packet recv or debug rip packet recv detail. Use this feature to define optional
arguments. In the function body, when an optional argument is not specified, the argument, argc, is 0.
When an optional argument is specified; the argument, argc is 1, and the argument, argv[0], contain a pointer to
the
optional string (detail).
Simple recursive: the period .
Simple recursive merely repeats the token that follows the . (period). You may input any number of these tokens.
For example, this command:
as-path prepend .<1-65535>
matches both as-path prepend 1 100 2 1000 and as-path prepend 10 3020.
Selective recursive: braces { }
Selective recursive allows the repetition of the enclosed tokens, but only one use per token per command instance;
order is not specified. For example, these tokens:
{key1|key2|key3}
matches any of these parameter collections:
key1
key1 key3
key3 key2 key1
Note that the following command string is incorrect:
key1 key1
Infinite recursive: square brackets [ ]
Infinite recursive allows the unlimited repetition of the enclosed tokens; order is not specified. For example, these
tokens:
[key1|key2|key3]
matches any of these parameter collections:
key1
key1 key1 key2
key3 key1 key2
The special token '?' may be used only with infinite recursive. It limits the immediately following token to one
occurrence in a command instance. For example,
[key1|?key2|key3]
matches any number of appearances of key1 and key3, but key2 may only appear once.
key3 key1 key2 key1
The following command string is incorrect:
key3 key1 key2 key2
IFNAME
IFNAME can be expanded to Interface name. The IFNAME expansion is done by the C preprocessor, not by the
CLI
parser. The following is an example of an IFNAME usage. CLI_IFNAME_STR is defined for interface name help
string.
DEFUN (interface,
interface_cli,
"interface "IFNAME,
"Select an interface to configure",
CLI_IFNAME_STR)
cli->mode = INTERFACE_MODE;
return CMD_SUCCESS;
{
}