quagga中cli命令分析

在已有的命令视图中新增加命令


定义命令并处理

  • 定义命令,
    DEFUN(处理函数,命令名称,命令字符串,帮助信息)
#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
  DEFUN_CMD_FUNC_DECL(funcname) \
  DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
  DEFUN_CMD_FUNC_TEXT(funcname)
DEFUN (vpnv4_route_target_import,
       vpnv4_route_target_import_cmd,
       "rt-import ASN:nn",
       "Route target import\n"
       "ASN:nn, e.g., 100:1\n")
{
  return bgp_vpn_rt_import (SAFI_MPLS_VPN, vty, argv[0]);
}
处理函数声明: 
Static int bgp_vpn_rt_import (safi_t safi, struct vty *vty, const char *rt_str); 
  • 处理函数实现;
int
 bgp_vpn_rt_import (safi_t safi, struct vty *vty,
                      const char *rt_str)
{
    return CMD_SUCCESS;
}
  • 安装该命令到对应的命令视图下,
    Install_element(命令视图,命令名称)
void
bgp_mplsvpn_init (void)
{
  install_element (BGP_VPNV4_NODE, &vpnv4_network_cmd);
  install_element (BGP_VPNV4_NODE, &vpnv4_network_route_map_cmd);
  install_element (BGP_VPNV4_NODE, &vpnv4_route_target_import_cmd);
  install_element (BGP_VPNV4_NODE, &vpnv4_route_target_export_cmd);
  ...
}

show running显示命令

  • show running读取命令的位置
/* Write current configuration into the terminal. */
ALIAS (config_write_terminal,
       show_running_config_cmd,
       "show running-config",
       SHOW_STR
       "running configuration\n")

可以看到show running使用的是config_write_terminal这个函数体,它的实现在:

/* Write current configuration into the terminal. */
DEFUN (config_write_terminal,
       config_write_terminal_cmd,
       "write terminal",
       "Write running configuration to memory, network, or terminal\n"
       "Write to terminal\n")
{
  unsigned int i;
  struct cmd_node *node;

  if (vty->type == VTY_SHELL_SERV)
    {
      for (i = 0; i < vector_active (cmdvec); i++)
    if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
      {
        if ((*node->func) (vty))
          vty_out (vty, "!%s", VTY_NEWLINE);
      }
    }
  else
    {
      vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
           VTY_NEWLINE);
      vty_out (vty, "!%s", VTY_NEWLINE);

      for (i = 0; i < vector_active (cmdvec); i++)
    if ((node = vector_slot (cmdvec, i)) && node->func)
      {
        if ((*node->func) (vty))
          vty_out (vty, "!%s", VTY_NEWLINE);
      }
      vty_out (vty, "end%s",VTY_NEWLINE);
    }
  return CMD_SUCCESS;
}

执行show running时调用了node->func函数,install_node时会给node->func赋值:

/* Install top node of command vector. */
void
install_node (struct cmd_node *node, 
          int (*func) (struct vty *))
{
  vector_set_index (cmdvec, node->node, node);
  node->func = func;
  node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
}

只要func不为null,调用show running就会执行此函数。以BGP为例,install_node定义的位置如下:

void
bgp_vty_init (void)
{
  /* Install bgp top node. */
  install_node (&bgp_node, bgp_config_write);
  install_node (&bgp_ipv4_unicast_node, NULL);
  install_node (&bgp_ipv4_multicast_node, NULL);
  install_node (&bgp_ipv6_unicast_node, NULL);
  install_node (&bgp_ipv6_multicast_node, NULL);
  install_node (&bgp_vpnv4_node, NULL);
  install_node (&bgp_vpnv6_node, NULL);
  install_node (&bgp_encap_node, NULL);
  install_node (&bgp_encapv6_node, NULL);

  /* Install default VTY commands to new nodes.  */
  install_default (BGP_NODE);
  install_default (BGP_IPV4_NODE);
  ...
 }

可以看到执行的是bgp_config_write函数,可以把需要显示的命令及数值添加到函数里,以配置的bgp vpn rt为例,需要在对应的node视图下添加:

vty_out (vty, " rt-export %s", rt_export_buf);
          vty_out (vty, "%s", VTY_NEWLINE);

在quagga的基础上新增加命令视图(转)

  • 定义命令视图类型,enum node_type ;
  • 定义命令视图结构,struct cmd_node;
    Static struct cmd_node 视图名称={
    命令视图类型,
    该视图下的提示符字符串,
    };
  • 安装命令视图,install_node(),install_default();
    Install_node(视图名称,回调函数)
    Install_default(视图类型)
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值