back to topotato report
topotato coverage report
Current view: top level - zebra - zebra_srv6_vty.c (source / functions) Hit Total Coverage
Test: test_ospf_topo1.py::OSPFTopo1Test Lines: 20 216 9.3 %
Date: 2023-02-24 18:38:32 Functions: 1 12 8.3 %

          Line data    Source code
       1             : /*
       2             :  * Zebra SRv6 VTY functions
       3             :  * Copyright (C) 2020  Hiroki Shirokura, LINE Corporation
       4             :  *
       5             :  * This program is free software; you can redistribute it and/or modify it
       6             :  * under the terms of the GNU General Public License as published by the Free
       7             :  * Software Foundation; either version 2 of the License, or (at your option)
       8             :  * any later version.
       9             :  *
      10             :  * This program is distributed in the hope that it will be useful, but WITHOUT
      11             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             :  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      13             :  * more details.
      14             :  *
      15             :  * You should have received a copy of the GNU General Public License along
      16             :  * with this program; see the file COPYING; if not, write to the Free Software
      17             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      18             :  */
      19             : 
      20             : #include <zebra.h>
      21             : 
      22             : #include "memory.h"
      23             : #include "if.h"
      24             : #include "prefix.h"
      25             : #include "command.h"
      26             : #include "table.h"
      27             : #include "rib.h"
      28             : #include "nexthop.h"
      29             : #include "vrf.h"
      30             : #include "srv6.h"
      31             : #include "lib/json.h"
      32             : 
      33             : #include "zebra/zserv.h"
      34             : #include "zebra/zebra_router.h"
      35             : #include "zebra/zebra_vrf.h"
      36             : #include "zebra/zebra_srv6.h"
      37             : #include "zebra/zebra_srv6_vty.h"
      38             : #include "zebra/zebra_rnh.h"
      39             : #include "zebra/redistribute.h"
      40             : #include "zebra/zebra_routemap.h"
      41             : #include "zebra/zebra_dplane.h"
      42             : 
      43             : #include "zebra/zebra_srv6_vty_clippy.c"
      44             : 
      45             : static int zebra_sr_config(struct vty *vty);
      46             : 
      47             : static struct cmd_node sr_node = {
      48             :         .name = "sr",
      49             :         .node = SEGMENT_ROUTING_NODE,
      50             :         .parent_node = CONFIG_NODE,
      51             :         .prompt = "%s(config-sr)# ",
      52             :         .config_write = zebra_sr_config,
      53             : };
      54             : 
      55             : static struct cmd_node srv6_node = {
      56             :         .name = "srv6",
      57             :         .node = SRV6_NODE,
      58             :         .parent_node = SEGMENT_ROUTING_NODE,
      59             :         .prompt = "%s(config-srv6)# ",
      60             : 
      61             : };
      62             : 
      63             : static struct cmd_node srv6_locs_node = {
      64             :         .name = "srv6-locators",
      65             :         .node = SRV6_LOCS_NODE,
      66             :         .parent_node = SRV6_NODE,
      67             :         .prompt = "%s(config-srv6-locators)# ",
      68             : };
      69             : 
      70             : static struct cmd_node srv6_loc_node = {
      71             :         .name = "srv6-locator",
      72             :         .node = SRV6_LOC_NODE,
      73             :         .parent_node = SRV6_LOCS_NODE,
      74             :         .prompt = "%s(config-srv6-locator)# "
      75             : };
      76             : 
      77           0 : DEFUN (show_srv6_locator,
      78             :        show_srv6_locator_cmd,
      79             :        "show segment-routing srv6 locator [json]",
      80             :        SHOW_STR
      81             :        "Segment Routing\n"
      82             :        "Segment Routing SRv6\n"
      83             :        "Locator Information\n"
      84             :        JSON_STR)
      85             : {
      86           0 :         const bool uj = use_json(argc, argv);
      87           0 :         struct zebra_srv6 *srv6 = zebra_srv6_get_default();
      88           0 :         struct srv6_locator *locator;
      89           0 :         struct listnode *node;
      90           0 :         char str[256];
      91           0 :         int id;
      92           0 :         json_object *json = NULL;
      93           0 :         json_object *json_locators = NULL;
      94           0 :         json_object *json_locator = NULL;
      95             : 
      96           0 :         if (uj) {
      97           0 :                 json = json_object_new_object();
      98           0 :                 json_locators = json_object_new_array();
      99           0 :                 json_object_object_add(json, "locators", json_locators);
     100             : 
     101           0 :                 for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) {
     102           0 :                         json_locator = srv6_locator_json(locator);
     103           0 :                         if (!json_locator)
     104           0 :                                 continue;
     105           0 :                         json_object_array_add(json_locators, json_locator);
     106             : 
     107             :                 }
     108             : 
     109           0 :                 vty_json(vty, json);
     110             :         } else {
     111           0 :                 vty_out(vty, "Locator:\n");
     112           0 :                 vty_out(vty, "Name                 ID      Prefix                   Status\n");
     113           0 :                 vty_out(vty, "-------------------- ------- ------------------------ -------\n");
     114             : 
     115           0 :                 id = 1;
     116           0 :                 for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) {
     117           0 :                         prefix2str(&locator->prefix, str, sizeof(str));
     118           0 :                         vty_out(vty, "%-20s %7d %-24s %s\n",
     119           0 :                                 locator->name, id, str,
     120           0 :                                 locator->status_up ? "Up" : "Down");
     121           0 :                         ++id;
     122             :                 }
     123           0 :                 vty_out(vty, "\n");
     124             :         }
     125             : 
     126           0 :         return CMD_SUCCESS;
     127             : }
     128             : 
     129           0 : DEFUN (show_srv6_locator_detail,
     130             :        show_srv6_locator_detail_cmd,
     131             :        "show segment-routing srv6 locator NAME detail [json]",
     132             :        SHOW_STR
     133             :        "Segment Routing\n"
     134             :        "Segment Routing SRv6\n"
     135             :        "Locator Information\n"
     136             :        "Locator Name\n"
     137             :        "Detailed information\n"
     138             :        JSON_STR)
     139             : {
     140           0 :         const bool uj = use_json(argc, argv);
     141           0 :         struct zebra_srv6 *srv6 = zebra_srv6_get_default();
     142           0 :         struct srv6_locator *locator;
     143           0 :         struct listnode *node;
     144           0 :         char str[256];
     145           0 :         const char *locator_name = argv[4]->arg;
     146           0 :         json_object *json_locator = NULL;
     147             : 
     148           0 :         if (uj) {
     149           0 :                 locator = zebra_srv6_locator_lookup(locator_name);
     150           0 :                 if (!locator)
     151             :                         return CMD_WARNING;
     152             : 
     153           0 :                 json_locator = srv6_locator_detailed_json(locator);
     154           0 :                 vty_json(vty, json_locator);
     155           0 :                 return CMD_SUCCESS;
     156             :         }
     157             : 
     158           0 :         for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) {
     159           0 :                 struct listnode *node;
     160           0 :                 struct srv6_locator_chunk *chunk;
     161             : 
     162           0 :                 if (strcmp(locator->name, locator_name) != 0)
     163           0 :                         continue;
     164             : 
     165           0 :                 prefix2str(&locator->prefix, str, sizeof(str));
     166           0 :                 vty_out(vty, "Name: %s\n", locator->name);
     167           0 :                 vty_out(vty, "Prefix: %s\n", str);
     168           0 :                 vty_out(vty, "Block-Bit-Len: %u\n", locator->block_bits_length);
     169           0 :                 vty_out(vty, "Node-Bit-Len: %u\n", locator->node_bits_length);
     170           0 :                 vty_out(vty, "Function-Bit-Len: %u\n",
     171           0 :                         locator->function_bits_length);
     172           0 :                 vty_out(vty, "Argument-Bit-Len: %u\n",
     173           0 :                         locator->argument_bits_length);
     174             : 
     175           0 :                 if (CHECK_FLAG(locator->flags, SRV6_LOCATOR_USID))
     176           0 :                         vty_out(vty, "Behavior: uSID\n");
     177             : 
     178           0 :                 vty_out(vty, "Chunks:\n");
     179           0 :                 for (ALL_LIST_ELEMENTS_RO((struct list *)locator->chunks, node,
     180             :                                           chunk)) {
     181           0 :                         prefix2str(&chunk->prefix, str, sizeof(str));
     182           0 :                         vty_out(vty, "- prefix: %s, owner: %s\n", str,
     183           0 :                                 zebra_route_string(chunk->proto));
     184             :                 }
     185             :         }
     186             : 
     187             : 
     188             :         return CMD_SUCCESS;
     189             : }
     190             : 
     191           0 : DEFUN_NOSH (segment_routing,
     192             :             segment_routing_cmd,
     193             :             "segment-routing",
     194             :             "Segment Routing\n")
     195             : {
     196           0 :         vty->node = SEGMENT_ROUTING_NODE;
     197           0 :         return CMD_SUCCESS;
     198             : }
     199             : 
     200           0 : DEFUN_NOSH (srv6,
     201             :             srv6_cmd,
     202             :             "srv6",
     203             :             "Segment Routing SRv6\n")
     204             : {
     205           0 :         vty->node = SRV6_NODE;
     206           0 :         return CMD_SUCCESS;
     207             : }
     208             : 
     209           0 : DEFUN (no_srv6,
     210             :        no_srv6_cmd,
     211             :        "no srv6",
     212             :        NO_STR
     213             :        "Segment Routing SRv6\n")
     214             : {
     215           0 :         struct zebra_srv6 *srv6 = zebra_srv6_get_default();
     216           0 :         struct srv6_locator *locator;
     217           0 :         struct listnode *node, *nnode;
     218             : 
     219           0 :         for (ALL_LIST_ELEMENTS(srv6->locators, node, nnode, locator))
     220           0 :                 zebra_srv6_locator_delete(locator);
     221           0 :         return CMD_SUCCESS;
     222             : }
     223             : 
     224           0 : DEFUN_NOSH (srv6_locators,
     225             :             srv6_locators_cmd,
     226             :             "locators",
     227             :             "Segment Routing SRv6 locators\n")
     228             : {
     229           0 :         vty->node = SRV6_LOCS_NODE;
     230           0 :         return CMD_SUCCESS;
     231             : }
     232             : 
     233           0 : DEFUN_NOSH (srv6_locator,
     234             :             srv6_locator_cmd,
     235             :             "locator WORD",
     236             :             "Segment Routing SRv6 locator\n"
     237             :             "Specify locator-name\n")
     238             : {
     239           0 :         struct srv6_locator *locator = NULL;
     240             : 
     241           0 :         locator = zebra_srv6_locator_lookup(argv[1]->arg);
     242           0 :         if (locator) {
     243           0 :                 VTY_PUSH_CONTEXT(SRV6_LOC_NODE, locator);
     244           0 :                 locator->status_up = true;
     245           0 :                 return CMD_SUCCESS;
     246             :         }
     247             : 
     248           0 :         locator = srv6_locator_alloc(argv[1]->arg);
     249           0 :         if (!locator) {
     250           0 :                 vty_out(vty, "%% Alloc failed\n");
     251           0 :                 return CMD_WARNING_CONFIG_FAILED;
     252             :         }
     253           0 :         locator->status_up = true;
     254             : 
     255           0 :         VTY_PUSH_CONTEXT(SRV6_LOC_NODE, locator);
     256           0 :         vty->node = SRV6_LOC_NODE;
     257           0 :         return CMD_SUCCESS;
     258             : }
     259             : 
     260           0 : DEFUN (no_srv6_locator,
     261             :        no_srv6_locator_cmd,
     262             :        "no locator WORD",
     263             :        NO_STR
     264             :        "Segment Routing SRv6 locator\n"
     265             :        "Specify locator-name\n")
     266             : {
     267           0 :         struct srv6_locator *locator = zebra_srv6_locator_lookup(argv[2]->arg);
     268           0 :         if (!locator) {
     269           0 :                 vty_out(vty, "%% Can't find SRv6 locator\n");
     270           0 :                 return CMD_WARNING_CONFIG_FAILED;
     271             :         }
     272             : 
     273           0 :         zebra_srv6_locator_delete(locator);
     274           0 :         return CMD_SUCCESS;
     275             : }
     276             : 
     277           0 : DEFPY (locator_prefix,
     278             :        locator_prefix_cmd,
     279             :        "prefix X:X::X:X/M$prefix [block-len (16-64)$block_bit_len]  \
     280             :                 [node-len (16-64)$node_bit_len] [func-bits (0-64)$func_bit_len]",
     281             :        "Configure SRv6 locator prefix\n"
     282             :        "Specify SRv6 locator prefix\n"
     283             :        "Configure SRv6 locator block length in bits\n"
     284             :        "Specify SRv6 locator block length in bits\n"
     285             :        "Configure SRv6 locator node length in bits\n"
     286             :        "Specify SRv6 locator node length in bits\n"
     287             :        "Configure SRv6 locator function length in bits\n"
     288             :        "Specify SRv6 locator function length in bits\n")
     289             : {
     290           0 :         VTY_DECLVAR_CONTEXT(srv6_locator, locator);
     291           0 :         struct srv6_locator_chunk *chunk = NULL;
     292           0 :         struct listnode *node = NULL;
     293             : 
     294           0 :         locator->prefix = *prefix;
     295           0 :         func_bit_len = func_bit_len ?: ZEBRA_SRV6_FUNCTION_LENGTH;
     296             : 
     297             :         /* Resolve optional arguments */
     298           0 :         if (block_bit_len == 0 && node_bit_len == 0) {
     299           0 :                 block_bit_len =
     300           0 :                         prefix->prefixlen - ZEBRA_SRV6_LOCATOR_NODE_LENGTH;
     301           0 :                 node_bit_len = ZEBRA_SRV6_LOCATOR_NODE_LENGTH;
     302           0 :         } else if (block_bit_len == 0) {
     303           0 :                 block_bit_len = prefix->prefixlen - node_bit_len;
     304           0 :         } else if (node_bit_len == 0) {
     305           0 :                 node_bit_len = prefix->prefixlen - block_bit_len;
     306             :         } else {
     307           0 :                 if (block_bit_len + node_bit_len != prefix->prefixlen) {
     308           0 :                         vty_out(vty,
     309             :                                 "%% block-len + node-len must be equal to the selected prefix length %d\n",
     310             :                                 prefix->prefixlen);
     311           0 :                         return CMD_WARNING_CONFIG_FAILED;
     312             :                 }
     313             :         }
     314             : 
     315           0 :         if (prefix->prefixlen + func_bit_len + 0 > 128) {
     316           0 :                 vty_out(vty,
     317             :                         "%% prefix-len + function-len + arg-len (%ld) cannot be greater than 128\n",
     318             :                         prefix->prefixlen + func_bit_len + 0);
     319           0 :                 return CMD_WARNING_CONFIG_FAILED;
     320             :         }
     321             : 
     322             :         /*
     323             :          * Currently, the SID transposition algorithm implemented in bgpd
     324             :          * handles incorrectly the SRv6 locators with function length greater
     325             :          * than 20 bits. To prevent issues, we currently limit the function
     326             :          * length to 20 bits.
     327             :          * This limit will be removed when the bgpd SID transposition is fixed.
     328             :          */
     329           0 :         if (func_bit_len > 20) {
     330           0 :                 vty_out(vty,
     331             :                         "%% currently func_bit_len > 20 is not supported\n");
     332           0 :                 return CMD_WARNING_CONFIG_FAILED;
     333             :         }
     334             : 
     335           0 :         locator->block_bits_length = block_bit_len;
     336           0 :         locator->node_bits_length = node_bit_len;
     337           0 :         locator->function_bits_length = func_bit_len;
     338           0 :         locator->argument_bits_length = 0;
     339             : 
     340           0 :         if (list_isempty(locator->chunks)) {
     341           0 :                 chunk = srv6_locator_chunk_alloc();
     342           0 :                 chunk->prefix = *prefix;
     343           0 :                 chunk->proto = 0;
     344           0 :                 listnode_add(locator->chunks, chunk);
     345             :         } else {
     346           0 :                 for (ALL_LIST_ELEMENTS_RO(locator->chunks, node, chunk)) {
     347           0 :                         uint8_t zero[16] = {0};
     348             : 
     349           0 :                         if (memcmp(&chunk->prefix.prefix, zero, 16) == 0) {
     350           0 :                                 struct zserv *client;
     351           0 :                                 struct listnode *client_node;
     352             : 
     353           0 :                                 chunk->prefix = *prefix;
     354           0 :                                 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list,
     355             :                                                           client_node,
     356             :                                                           client)) {
     357           0 :                                         struct srv6_locator *tmp;
     358             : 
     359           0 :                                         if (client->proto != chunk->proto)
     360           0 :                                                 continue;
     361             : 
     362           0 :                                         srv6_manager_get_locator_chunk_call(
     363             :                                                         &tmp, client,
     364           0 :                                                         locator->name,
     365             :                                                         VRF_DEFAULT);
     366             :                                 }
     367             :                         }
     368             :                 }
     369             :         }
     370             : 
     371           0 :         zebra_srv6_locator_add(locator);
     372           0 :         return CMD_SUCCESS;
     373             : }
     374             : 
     375           0 : DEFPY (locator_behavior,
     376             :        locator_behavior_cmd,
     377             :        "[no] behavior usid",
     378             :        NO_STR
     379             :        "Configure SRv6 behavior\n"
     380             :        "Specify SRv6 behavior uSID\n")
     381             : {
     382           0 :         VTY_DECLVAR_CONTEXT(srv6_locator, locator);
     383             : 
     384           0 :         if (no && !CHECK_FLAG(locator->flags, SRV6_LOCATOR_USID))
     385             :                 /* SRv6 locator uSID flag already unset, nothing to do */
     386             :                 return CMD_SUCCESS;
     387             : 
     388           0 :         if (!no && CHECK_FLAG(locator->flags, SRV6_LOCATOR_USID))
     389             :                 /* SRv6 locator uSID flag already set, nothing to do */
     390             :                 return CMD_SUCCESS;
     391             : 
     392             :         /* Remove old locator from zclients */
     393           0 :         zebra_notify_srv6_locator_delete(locator);
     394             : 
     395             :         /* Set/Unset the SRV6_LOCATOR_USID */
     396           0 :         if (no)
     397           0 :                 UNSET_FLAG(locator->flags, SRV6_LOCATOR_USID);
     398             :         else
     399           0 :                 SET_FLAG(locator->flags, SRV6_LOCATOR_USID);
     400             : 
     401             :         /* Notify the new locator to zclients */
     402           0 :         zebra_notify_srv6_locator_add(locator);
     403             : 
     404           0 :         return CMD_SUCCESS;
     405             : }
     406             : 
     407           0 : static int zebra_sr_config(struct vty *vty)
     408             : {
     409           0 :         struct zebra_srv6 *srv6 = zebra_srv6_get_default();
     410           0 :         struct listnode *node;
     411           0 :         struct srv6_locator *locator;
     412           0 :         char str[256];
     413             : 
     414           0 :         vty_out(vty, "!\n");
     415           0 :         if (zebra_srv6_is_enable()) {
     416           0 :                 vty_out(vty, "segment-routing\n");
     417           0 :                 vty_out(vty, " srv6\n");
     418           0 :                 vty_out(vty, "  locators\n");
     419           0 :                 for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) {
     420           0 :                         inet_ntop(AF_INET6, &locator->prefix.prefix,
     421             :                                   str, sizeof(str));
     422           0 :                         vty_out(vty, "   locator %s\n", locator->name);
     423           0 :                         vty_out(vty, "    prefix %s/%u", str,
     424           0 :                                 locator->prefix.prefixlen);
     425           0 :                         if (locator->block_bits_length)
     426           0 :                                 vty_out(vty, " block-len %u",
     427             :                                         locator->block_bits_length);
     428           0 :                         if (locator->node_bits_length)
     429           0 :                                 vty_out(vty, " node-len %u",
     430             :                                         locator->node_bits_length);
     431           0 :                         if (locator->function_bits_length)
     432           0 :                                 vty_out(vty, " func-bits %u",
     433             :                                         locator->function_bits_length);
     434           0 :                         if (locator->argument_bits_length)
     435           0 :                                 vty_out(vty, " arg-len %u",
     436             :                                         locator->argument_bits_length);
     437           0 :                         vty_out(vty, "\n");
     438           0 :                         if (CHECK_FLAG(locator->flags, SRV6_LOCATOR_USID))
     439           0 :                                 vty_out(vty, "    behavior usid\n");
     440           0 :                         vty_out(vty, "   exit\n");
     441           0 :                         vty_out(vty, "   !\n");
     442             :                 }
     443           0 :                 vty_out(vty, "  exit\n");
     444           0 :                 vty_out(vty, "  !\n");
     445           0 :                 vty_out(vty, " exit\n");
     446           0 :                 vty_out(vty, " !\n");
     447           0 :                 vty_out(vty, "exit\n");
     448           0 :                 vty_out(vty, "!\n");
     449             :         }
     450           0 :         return 0;
     451             : }
     452             : 
     453           4 : void zebra_srv6_vty_init(void)
     454             : {
     455             :         /* Install nodes and its default commands */
     456           4 :         install_node(&sr_node);
     457           4 :         install_node(&srv6_node);
     458           4 :         install_node(&srv6_locs_node);
     459           4 :         install_node(&srv6_loc_node);
     460           4 :         install_default(SEGMENT_ROUTING_NODE);
     461           4 :         install_default(SRV6_NODE);
     462           4 :         install_default(SRV6_LOCS_NODE);
     463           4 :         install_default(SRV6_LOC_NODE);
     464             : 
     465             :         /* Command for change node */
     466           4 :         install_element(CONFIG_NODE, &segment_routing_cmd);
     467           4 :         install_element(SEGMENT_ROUTING_NODE, &srv6_cmd);
     468           4 :         install_element(SEGMENT_ROUTING_NODE, &no_srv6_cmd);
     469           4 :         install_element(SRV6_NODE, &srv6_locators_cmd);
     470           4 :         install_element(SRV6_LOCS_NODE, &srv6_locator_cmd);
     471           4 :         install_element(SRV6_LOCS_NODE, &no_srv6_locator_cmd);
     472             : 
     473             :         /* Command for configuration */
     474           4 :         install_element(SRV6_LOC_NODE, &locator_prefix_cmd);
     475           4 :         install_element(SRV6_LOC_NODE, &locator_behavior_cmd);
     476             : 
     477             :         /* Command for operation */
     478           4 :         install_element(VIEW_NODE, &show_srv6_locator_cmd);
     479           4 :         install_element(VIEW_NODE, &show_srv6_locator_detail_cmd);
     480           4 : }

Generated by: LCOV version v1.16-topotato