diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/bfd.c | 3 | ||||
| -rw-r--r-- | lib/command.c | 124 | ||||
| -rw-r--r-- | lib/command.h | 1 | ||||
| -rw-r--r-- | lib/elf_py.c | 4 | ||||
| -rw-r--r-- | lib/filter.c | 257 | ||||
| -rw-r--r-- | lib/filter.h | 9 | ||||
| -rw-r--r-- | lib/filter_cli.c | 102 | ||||
| -rw-r--r-- | lib/filter_nb.c | 85 | ||||
| -rw-r--r-- | lib/frr_zmq.c | 24 | ||||
| -rw-r--r-- | lib/frrlua.c | 34 | ||||
| -rw-r--r-- | lib/frrlua.h | 6 | ||||
| -rw-r--r-- | lib/frrscript.c | 210 | ||||
| -rw-r--r-- | lib/frrscript.h | 166 | ||||
| -rw-r--r-- | lib/frrstr.c | 22 | ||||
| -rw-r--r-- | lib/frrstr.h | 20 | ||||
| -rw-r--r-- | lib/if.c | 18 | ||||
| -rw-r--r-- | lib/if.h | 3 | ||||
| -rw-r--r-- | lib/keychain.c | 1 | ||||
| -rw-r--r-- | lib/libfrr.c | 1 | ||||
| -rw-r--r-- | lib/nexthop_group.c | 1 | ||||
| -rw-r--r-- | lib/pbr.h | 6 | ||||
| -rw-r--r-- | lib/plist.c | 300 | ||||
| -rw-r--r-- | lib/plist.h | 15 | ||||
| -rw-r--r-- | lib/route_opaque.h | 8 | ||||
| -rw-r--r-- | lib/route_types.txt | 4 | ||||
| -rw-r--r-- | lib/routemap.c | 216 | ||||
| -rw-r--r-- | lib/routemap.h | 11 | ||||
| -rw-r--r-- | lib/routemap_cli.c | 39 | ||||
| -rw-r--r-- | lib/subdir.am | 19 | ||||
| -rw-r--r-- | lib/vrf.c | 74 | ||||
| -rw-r--r-- | lib/vrf.h | 4 | ||||
| -rw-r--r-- | lib/vty.c | 4 | ||||
| -rw-r--r-- | lib/zclient.c | 42 | ||||
| -rw-r--r-- | lib/zclient.h | 19 |
34 files changed, 1206 insertions, 646 deletions
@@ -366,6 +366,9 @@ int zclient_bfd_command(struct zclient *zc, struct bfd_session_arg *args) if (args->ifnamelen) stream_put(s, args->ifname, args->ifnamelen); } + + /* Send the C bit indicator. */ + stream_putc(s, args->cbit); #endif /* HAVE_BFDD */ /* Finish the message by writing the size. */ diff --git a/lib/command.c b/lib/command.c index 9dac60599c..fcaf466c65 100644 --- a/lib/command.c +++ b/lib/command.c @@ -160,6 +160,9 @@ static bool vty_check_node_for_xpath_decrement(enum node_type target_node, || node == BGP_FLOWSPECV6_NODE)) return false; + if (target_node == INTERFACE_NODE && node == LINK_PARAMS_NODE) + return false; + return true; } @@ -849,96 +852,13 @@ char **cmd_complete_command(vector vline, struct vty *vty, int *status) /* MUST eventually converge on CONFIG_NODE */ enum node_type node_parent(enum node_type node) { - enum node_type ret; + struct cmd_node *cnode; assert(node > CONFIG_NODE); - switch (node) { - case BGP_VPNV4_NODE: - case BGP_VPNV6_NODE: - case BGP_FLOWSPECV4_NODE: - case BGP_FLOWSPECV6_NODE: - case BGP_VRF_POLICY_NODE: - case BGP_VNC_DEFAULTS_NODE: - case BGP_VNC_NVE_GROUP_NODE: - case BGP_VNC_L2_GROUP_NODE: - case BGP_IPV4_NODE: - case BGP_IPV4M_NODE: - case BGP_IPV4L_NODE: - case BGP_IPV6_NODE: - case BGP_IPV6M_NODE: - case BGP_EVPN_NODE: - case BGP_IPV6L_NODE: - case BMP_NODE: - ret = BGP_NODE; - break; - case BGP_EVPN_VNI_NODE: - ret = BGP_EVPN_NODE; - break; - case KEYCHAIN_KEY_NODE: - ret = KEYCHAIN_NODE; - break; - case LINK_PARAMS_NODE: - ret = INTERFACE_NODE; - break; - case LDP_IPV4_NODE: - case LDP_IPV6_NODE: - ret = LDP_NODE; - break; - case LDP_IPV4_IFACE_NODE: - ret = LDP_IPV4_NODE; - break; - case LDP_IPV6_IFACE_NODE: - ret = LDP_IPV6_NODE; - break; - case LDP_PSEUDOWIRE_NODE: - ret = LDP_L2VPN_NODE; - break; - case BFD_PEER_NODE: - ret = BFD_NODE; - break; - case BFD_PROFILE_NODE: - ret = BFD_NODE; - break; - case SR_TRAFFIC_ENG_NODE: - ret = SEGMENT_ROUTING_NODE; - break; - case SR_SEGMENT_LIST_NODE: - ret = SR_TRAFFIC_ENG_NODE; - break; - case SR_POLICY_NODE: - ret = SR_TRAFFIC_ENG_NODE; - break; - case SR_CANDIDATE_DYN_NODE: - ret = SR_POLICY_NODE; - break; - case PCEP_NODE: - ret = SR_TRAFFIC_ENG_NODE; - break; - case PCEP_PCE_CONFIG_NODE: - ret = PCEP_NODE; - break; - case PCEP_PCE_NODE: - ret = PCEP_NODE; - break; - case PCEP_PCC_NODE: - ret = PCEP_NODE; - break; - case SRV6_NODE: - ret = SEGMENT_ROUTING_NODE; - break; - case SRV6_LOCS_NODE: - ret = SRV6_NODE; - break; - case SRV6_LOC_NODE: - ret = SRV6_LOCS_NODE; - break; - default: - ret = CONFIG_NODE; - break; - } + cnode = vector_lookup(cmdvec, node); - return ret; + return cnode->parent_node; } /* Execute command by argument vline vector. */ @@ -2419,28 +2339,30 @@ DEFUN(find, } #if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) -DEFUN(script, - script_cmd, - "script SCRIPT", - "Test command - execute a script\n" - "Script name (same as filename in /etc/frr/scripts/\n") +DEFUN(script, script_cmd, "script SCRIPT FUNCTION", + "Test command - execute a function in a script\n" + "Script name (same as filename in /etc/frr/scripts/)\n" + "Function name (in the script)\n") { struct prefix p; (void)str2prefix("1.2.3.4/24", &p); - struct frrscript *fs = frrscript_load(argv[1]->arg, NULL); + struct frrscript *fs = frrscript_new(argv[1]->arg); - if (fs == NULL) { - vty_out(vty, "Script '/etc/frr/scripts/%s.lua' not found\n", - argv[1]->arg); - } else { - int ret = frrscript_call(fs, ("p", &p)); - char buf[40]; - prefix2str(&p, buf, sizeof(buf)); - vty_out(vty, "p: %s\n", buf); - vty_out(vty, "Script result: %d\n", ret); + if (frrscript_load(fs, argv[2]->arg, NULL)) { + vty_out(vty, + "/etc/frr/scripts/%s.lua or function '%s' not found\n", + argv[1]->arg, argv[2]->arg); } + int ret = frrscript_call(fs, argv[2]->arg, ("p", &p)); + char buf[40]; + prefix2str(&p, buf, sizeof(buf)); + vty_out(vty, "p: %s\n", buf); + vty_out(vty, "Script result: %d\n", ret); + + frrscript_delete(fs); + return CMD_SUCCESS; } #endif diff --git a/lib/command.h b/lib/command.h index 2b50bc2374..c76fc1e8eb 100644 --- a/lib/command.h +++ b/lib/command.h @@ -389,6 +389,7 @@ struct cmd_node { #define SRTE_STR "SR-TE information\n" #define SRTE_COLOR_STR "SR-TE Color information\n" #define NO_STR "Negate a command or set its defaults\n" +#define IGNORED_IN_NO_STR "Ignored value in no form\n" #define REDIST_STR "Redistribute information from another routing protocol\n" #define CLEAR_STR "Reset functions\n" #define RIP_STR "RIP information\n" diff --git a/lib/elf_py.c b/lib/elf_py.c index b47aa3d795..1c306893ad 100644 --- a/lib/elf_py.c +++ b/lib/elf_py.c @@ -860,6 +860,7 @@ static PyObject *elffile_find_note(PyObject *self, PyObject *args) Py_RETURN_NONE; } +#ifdef HAVE_ELF_GETDATA_RAWCHUNK static bool elffile_virt2file(struct elffile *w, GElf_Addr virt, GElf_Addr *offs) { @@ -884,6 +885,7 @@ static bool elffile_virt2file(struct elffile *w, GElf_Addr virt, return false; } +#endif /* HAVE_ELF_GETDATA_RAWCHUNK */ static PyObject *elffile_subscript(PyObject *self, PyObject *key) { @@ -1016,6 +1018,7 @@ static PyTypeObject typeobj_elffile = { .tp_methods = methods_elffile, }; +#ifdef HAVE_ELF_GETDATA_RAWCHUNK static char *elfdata_strptr(Elf_Data *data, size_t offset) { char *p; @@ -1124,6 +1127,7 @@ static void elffile_add_dynreloc(struct elffile *w, Elf_Data *reldata, } } +#endif /* HAVE_ELF_GETDATA_RAWCHUNK */ /* primary (only, really) entry point to anything in this module */ static PyObject *elffile_load(PyTypeObject *type, PyObject *args, diff --git a/lib/filter.c b/lib/filter.c index 72a66d85ad..744ea9c480 100644 --- a/lib/filter.c +++ b/lib/filter.c @@ -30,6 +30,7 @@ #include "routemap.h" #include "libfrr.h" #include "northbound_cli.h" +#include "json.h" DEFINE_MTYPE_STATIC(LIB, ACCESS_LIST, "Access List"); DEFINE_MTYPE_STATIC(LIB, ACCESS_LIST_STR, "Access List Str"); @@ -443,71 +444,158 @@ void access_list_filter_add(struct access_list *access, host A single host address */ -static void config_write_access_zebra(struct vty *, struct filter *); -static void config_write_access_cisco(struct vty *, struct filter *); +static void config_write_access_zebra(struct vty *, struct filter *, + json_object *); +static void config_write_access_cisco(struct vty *, struct filter *, + json_object *); + +static const char *filter_type2str(struct filter *filter) +{ + if (filter->cisco) { + if (filter->u.cfilter.extended) + return "Extended"; + else + return "Standard"; + } else + return "Zebra"; +} /* show access-list command. */ -static int filter_show(struct vty *vty, const char *name, afi_t afi) +static int filter_show(struct vty *vty, const char *name, afi_t afi, + bool use_json) { struct access_list *access; struct access_master *master; struct filter *mfilter; struct filter_cisco *filter; - int write = 0; + bool first; + json_object *json = NULL; + json_object *json_proto = NULL; master = access_master_get(afi); - if (master == NULL) + if (master == NULL) { + if (use_json) + vty_out(vty, "{}\n"); return 0; + } + + if (use_json) + json = json_object_new_object(); /* Print the name of the protocol */ - vty_out(vty, "%s:\n", frr_protoname); + if (json) { + json_proto = json_object_new_object(); + json_object_object_add(json, frr_protoname, json_proto); + } else + vty_out(vty, "%s:\n", frr_protoname); for (access = master->str.head; access; access = access->next) { + json_object *json_acl = NULL; + json_object *json_rules = NULL; + if (name && strcmp(access->name, name) != 0) continue; - write = 1; + first = true; for (mfilter = access->head; mfilter; mfilter = mfilter->next) { + json_object *json_rule = NULL; + filter = &mfilter->u.cfilter; - if (write) { - vty_out(vty, "%s %s access list %s\n", - mfilter->cisco ? (filter->extended - ? "Extended" - : "Standard") - : "Zebra", - (afi == AFI_IP) - ? ("IP") - : ((afi == AFI_IP6) ? ("IPv6 ") - : ("MAC ")), - access->name); - write = 0; + if (first) { + const char *type = filter_type2str(mfilter); + + if (json) { + json_acl = json_object_new_object(); + json_object_object_add(json_proto, + access->name, + json_acl); + + json_object_string_add(json_acl, "type", + type); + json_object_string_add(json_acl, + "addressFamily", + afi2str(afi)); + json_rules = json_object_new_array(); + json_object_object_add( + json_acl, "rules", json_rules); + } else { + vty_out(vty, "%s %s access list %s\n", + type, + (afi == AFI_IP) + ? ("IP") + : ((afi == AFI_IP6) + ? ("IPv6 ") + : ("MAC ")), + access->name); + } + + first = false; } - vty_out(vty, " seq %" PRId64, mfilter->seq); - vty_out(vty, " %s%s", filter_type_str(mfilter), - mfilter->type == FILTER_DENY ? " " : ""); + if (json) { + json_rule = json_object_new_object(); + json_object_array_add(json_rules, json_rule); + + json_object_int_add(json_rule, "sequenceNumber", + mfilter->seq); + json_object_string_add( + json_rule, "filterType", + filter_type_str(mfilter)); + } else { + vty_out(vty, " seq %" PRId64, mfilter->seq); + vty_out(vty, " %s%s", filter_type_str(mfilter), + mfilter->type == FILTER_DENY ? " " + : ""); + } if (!mfilter->cisco) - config_write_access_zebra(vty, mfilter); + config_write_access_zebra(vty, mfilter, + json_rule); else if (filter->extended) - config_write_access_cisco(vty, mfilter); + config_write_access_cisco(vty, mfilter, + json_rule); else { - if (filter->addr_mask.s_addr == 0xffffffff) - vty_out(vty, " any\n"); - else { - vty_out(vty, " %pI4", &filter->addr); + if (json) { + char buf[BUFSIZ]; + + json_object_string_add( + json_rule, "address", + inet_ntop(AF_INET, + &filter->addr, buf, + sizeof(buf))); + json_object_string_add( + json_rule, "mask", + inet_ntop(AF_INET, + &filter->addr_mask, + buf, sizeof(buf))); + } else { if (filter->addr_mask.s_addr - != INADDR_ANY) - vty_out(vty, - ", wildcard bits %pI4", - &filter->addr_mask); - vty_out(vty, "\n"); + == 0xffffffff) + vty_out(vty, " any\n"); + else { + vty_out(vty, " %pI4", + &filter->addr); + if (filter->addr_mask.s_addr + != INADDR_ANY) + vty_out(vty, + ", wildcard bits %pI4", + &filter->addr_mask); + vty_out(vty, "\n"); + } } } } } + + if (json) { + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + json, JSON_C_TO_STRING_PRETTY)); + json_object_free(json); + } + return CMD_SUCCESS; } @@ -519,7 +607,7 @@ DEFUN (show_mac_access_list, "mac access lists\n" "List mac access lists\n") { - return filter_show(vty, NULL, AFI_L2VPN); + return filter_show(vty, NULL, AFI_L2VPN, false); } DEFUN (show_mac_access_list_name, @@ -530,22 +618,24 @@ DEFUN (show_mac_access_list_name, "List mac access lists\n" "mac address\n") { - return filter_show(vty, argv[3]->arg, AFI_L2VPN); + return filter_show(vty, argv[3]->arg, AFI_L2VPN, false); } DEFUN (show_ip_access_list, show_ip_access_list_cmd, - "show ip access-list", + "show ip access-list [json]", SHOW_STR IP_STR - "List IP access lists\n") + "List IP access lists\n" + JSON_STR) { - return filter_show(vty, NULL, AFI_IP); + bool uj = use_json(argc, argv); + return filter_show(vty, NULL, AFI_IP, uj); } DEFUN (show_ip_access_list_name, show_ip_access_list_name_cmd, - "show ip access-list <(1-99)|(100-199)|(1300-1999)|(2000-2699)|WORD>", + "show ip access-list <(1-99)|(100-199)|(1300-1999)|(2000-2699)|WORD> [json]", SHOW_STR IP_STR "List IP access lists\n" @@ -553,41 +643,64 @@ DEFUN (show_ip_access_list_name, "IP extended access list\n" "IP standard access list (expanded range)\n" "IP extended access list (expanded range)\n" - "IP zebra access-list\n") + "IP zebra access-list\n" + JSON_STR) { + bool uj = use_json(argc, argv); int idx_acl = 3; - return filter_show(vty, argv[idx_acl]->arg, AFI_IP); + return filter_show(vty, argv[idx_acl]->arg, AFI_IP, uj); } DEFUN (show_ipv6_access_list, show_ipv6_access_list_cmd, - "show ipv6 access-list", + "show ipv6 access-list [json]", SHOW_STR IPV6_STR - "List IPv6 access lists\n") + "List IPv6 access lists\n" + JSON_STR) { - return filter_show(vty, NULL, AFI_IP6); + bool uj = use_json(argc, argv); + return filter_show(vty, NULL, AFI_IP6, uj); } DEFUN (show_ipv6_access_list_name, show_ipv6_access_list_name_cmd, - "show ipv6 access-list WORD", + "show ipv6 access-list WORD [json]", SHOW_STR IPV6_STR "List IPv6 access lists\n" - "IPv6 zebra access-list\n") + "IPv6 zebra access-list\n" + JSON_STR) { + bool uj = use_json(argc, argv); int idx_word = 3; - return filter_show(vty, argv[idx_word]->arg, AFI_IP6); + return filter_show(vty, argv[idx_word]->arg, AFI_IP6, uj); } -static void config_write_access_cisco(struct vty *vty, struct filter *mfilter) +static void config_write_access_cisco(struct vty *vty, struct filter *mfilter, + json_object *json) { struct filter_cisco *filter; filter = &mfilter->u.cfilter; - if (filter->extended) { + if (json) { + char buf[BUFSIZ]; + + json_object_boolean_add(json, "extended", !!filter->extended); + json_object_string_add( + json, "sourceAddress", + inet_ntop(AF_INET, &filter->addr, buf, sizeof(buf))); + json_object_string_add(json, "sourceMask", + inet_ntop(AF_INET, &filter->addr_mask, + buf, sizeof(buf))); + json_object_string_add( + json, "destinationAddress", + inet_ntop(AF_INET, &filter->mask, buf, sizeof(buf))); + json_object_string_add(json, "destinationMask", + inet_ntop(AF_INET, &filter->mask_mask, + buf, sizeof(buf))); + } else { vty_out(vty, " ip"); if (filter->addr_mask.s_addr == 0xffffffff) vty_out(vty, " any"); @@ -607,19 +720,11 @@ static void config_write_access_cisco(struct vty *vty, struct filter *mfilter) vty_out(vty, " %pI4", &filter->mask_mask); } vty_out(vty, "\n"); - } else { - if (filter->addr_mask.s_addr == 0xffffffff) - vty_out(vty, " any\n"); - else { - vty_out(vty, " %pI4", &filter->addr); - if (filter->addr_mask.s_addr != INADDR_ANY) - vty_out(vty, " %pI4", &filter->addr_mask); - vty_out(vty, "\n"); - } } } -static void config_write_access_zebra(struct vty *vty, struct filter *mfilter) +static void config_write_access_zebra(struct vty *vty, struct filter *mfilter, + json_object *json) { struct filter_zebra *filter; struct prefix *p; @@ -628,21 +733,29 @@ static void config_write_access_zebra(struct vty *vty, struct filter *mfilter) filter = &mfilter->u.zfilter; p = &filter->prefix; - if (p->prefixlen == 0 && !filter->exact) - vty_out(vty, " any"); - else if (p->family == AF_INET6 || p->family == AF_INET) - vty_out(vty, " %s/%d%s", - inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), - p->prefixlen, filter->exact ? " exact-match" : ""); - else if (p->family == AF_ETHERNET) { - if (p->prefixlen == 0) + if (json) { + json_object_string_add(json, "prefix", + prefix2str(p, buf, sizeof(buf))); + json_object_boolean_add(json, "exact-match", !!filter->exact); + } else { + if (p->prefixlen == 0 && !filter->exact) vty_out(vty, " any"); - else - vty_out(vty, " %s", prefix_mac2str(&(p->u.prefix_eth), - buf, sizeof(buf))); - } + else if (p->family == AF_INET6 || p->family == AF_INET) + vty_out(vty, " %s/%d%s", + inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), + p->prefixlen, + filter->exact ? " exact-match" : ""); + else if (p->family == AF_ETHERNET) { + if (p->prefixlen == 0) + vty_out(vty, " any"); + else + vty_out(vty, " %s", + prefix_mac2str(&(p->u.prefix_eth), buf, + sizeof(buf))); + } - vty_out(vty, "\n"); + vty_out(vty, "\n"); + } } static struct cmd_node access_mac_node = { diff --git a/lib/filter.h b/lib/filter.h index 941fabd38b..d1956ec019 100644 --- a/lib/filter.h +++ b/lib/filter.h @@ -207,11 +207,10 @@ struct plist_dup_args { /** Entry action. */ const char *pda_action; -#define PDA_MAX_VALUES 4 - /** Entry XPath for value. */ - const char *pda_xpath[PDA_MAX_VALUES]; - /** Entry value to match. */ - const char *pda_value[PDA_MAX_VALUES]; + bool any; + struct prefix prefix; + int ge; + int le; /** Duplicated entry found in list? */ bool pda_found; diff --git a/lib/filter_cli.c b/lib/filter_cli.c index f030ce1b33..45c7544a3b 100644 --- a/lib/filter_cli.c +++ b/lib/filter_cli.c @@ -1196,11 +1196,9 @@ static int plist_remove_if_empty(struct vty *vty, const char *iptype, static int plist_remove(struct vty *vty, const char *iptype, const char *name, const char *seq, const char *action, - const char *prefix_str, const char *ge_str, - const char *le_str) + union prefixconstptr prefix, int ge, int le) { int64_t sseq; - int arg_idx = 0; struct plist_dup_args pda = {}; char xpath[XPATH_MAXLEN]; char xpath_entry[XPATH_MAXLEN + 32]; @@ -1225,43 +1223,13 @@ static int plist_remove(struct vty *vty, const char *iptype, const char *name, pda.pda_type = iptype; pda.pda_name = name; pda.pda_action = action; - if (prefix_str) { - if (strmatch(iptype, "ipv4")) { - pda.pda_xpath[arg_idx] = "./ipv4-prefix"; - pda.pda_value[arg_idx] = prefix_str; - arg_idx++; - if (ge_str) { - pda.pda_xpath[arg_idx] = - "./ipv4-prefix-length-greater-or-equal"; - pda.pda_value[arg_idx] = ge_str; - arg_idx++; - } - if (le_str) { - pda.pda_xpath[arg_idx] = - "./ipv4-prefix-length-lesser-or-equal"; - pda.pda_value[arg_idx] = le_str; - arg_idx++; - } - } else { - pda.pda_xpath[arg_idx] = "./ipv6-prefix"; - pda.pda_value[arg_idx] = prefix_str; - arg_idx++; - if (ge_str) { - pda.pda_xpath[arg_idx] = - "./ipv6-prefix-length-greater-or-equal"; - pda.pda_value[arg_idx] = ge_str; - arg_idx++; - } - if (le_str) { - pda.pda_xpath[arg_idx] = - "./ipv6-prefix-length-lesser-or-equal"; - pda.pda_value[arg_idx] = le_str; - arg_idx++; - } - } + if (prefix.p) { + prefix_copy(&pda.prefix, prefix); + apply_mask(&pda.prefix); + pda.ge = ge; + pda.le = le; } else { - pda.pda_xpath[0] = "./any"; - pda.pda_value[0] = ""; + pda.any = true; } if (plist_is_dup(vty->candidate_config->dnode, &pda)) @@ -1298,7 +1266,6 @@ DEFPY_YANG( "Maximum prefix length\n") { int64_t sseq; - int arg_idx = 0; struct plist_dup_args pda = {}; char xpath[XPATH_MAXLEN]; char xpath_entry[XPATH_MAXLEN + 128]; @@ -1312,24 +1279,11 @@ DEFPY_YANG( pda.pda_name = name; pda.pda_action = action; if (prefix_str) { - pda.pda_xpath[arg_idx] = "./ipv4-prefix"; - pda.pda_value[arg_idx] = prefix_str; - arg_idx++; - if (ge_str) { - pda.pda_xpath[arg_idx] = - "./ipv4-prefix-length-greater-or-equal"; - pda.pda_value[arg_idx] = ge_str; - arg_idx++; - } - if (le_str) { - pda.pda_xpath[arg_idx] = - "./ipv4-prefix-length-lesser-or-equal"; - pda.pda_value[arg_idx] = le_str; - arg_idx++; - } + prefix_copy(&pda.prefix, prefix); + pda.ge = ge; + pda.le = le; } else { - pda.pda_xpath[0] = "./any"; - pda.pda_value[0] = ""; + pda.any = true; } /* Duplicated entry without sequence, just quit. */ @@ -1408,8 +1362,8 @@ DEFPY_YANG( "Maximum prefix length to be matched\n" "Maximum prefix length\n") { - return plist_remove(vty, "ipv4", name, seq_str, action, prefix_str, - ge_str, le_str); + return plist_remove(vty, "ipv4", name, seq_str, action, + prefix_str ? prefix : NULL, ge, le); } DEFPY_YANG( @@ -1421,7 +1375,7 @@ DEFPY_YANG( PREFIX_LIST_NAME_STR ACCESS_LIST_SEQ_STR) { - return plist_remove(vty, "ipv4", name, seq_str, NULL, NULL, NULL, NULL); + return plist_remove(vty, "ipv4", name, seq_str, NULL, NULL, 0, 0); } DEFPY_YANG( @@ -1516,7 +1470,6 @@ DEFPY_YANG( "Minimum prefix length\n") { int64_t sseq; - int arg_idx = 0; struct plist_dup_args pda = {}; char xpath[XPATH_MAXLEN]; char xpath_entry[XPATH_MAXLEN + 128]; @@ -1530,24 +1483,11 @@ DEFPY_YANG( pda.pda_name = name; pda.pda_action = action; if (prefix_str) { - pda.pda_xpath[arg_idx] = "./ipv6-prefix"; - pda.pda_value[arg_idx] = prefix_str; - arg_idx++; - if (ge_str) { - pda.pda_xpath[arg_idx] = - "./ipv6-prefix-length-greater-or-equal"; - pda.pda_value[arg_idx] = ge_str; - arg_idx++; - } - if (le_str) { - pda.pda_xpath[arg_idx] = - "./ipv6-prefix-length-lesser-or-equal"; - pda.pda_value[arg_idx] = le_str; - arg_idx++; - } + prefix_copy(&pda.prefix, prefix); + pda.ge = ge; + pda.le = le; } else { - pda.pda_xpath[0] = "./any"; - pda.pda_value[0] = ""; + pda.any = true; } /* Duplicated entry without sequence, just quit. */ @@ -1626,8 +1566,8 @@ DEFPY_YANG( "Minimum prefix length to be matched\n" "Minimum prefix length\n") { - return plist_remove(vty, "ipv6", name, seq_str, action, prefix_str, - ge_str, le_str); + return plist_remove(vty, "ipv6", name, seq_str, action, + prefix_str ? prefix : NULL, ge, le); } DEFPY_YANG( @@ -1639,7 +1579,7 @@ DEFPY_YANG( PREFIX_LIST_NAME_STR ACCESS_LIST_SEQ_STR) { - return plist_remove(vty, "ipv6", name, seq_str, NULL, NULL, NULL, NULL); + return plist_remove(vty, "ipv6", name, seq_str, NULL, NULL, 0, 0); } DEFPY_YANG( diff --git a/lib/filter_nb.c b/lib/filter_nb.c index 85805ffa47..80ea7a57cb 100644 --- a/lib/filter_nb.c +++ b/lib/filter_nb.c @@ -387,10 +387,50 @@ static bool acl_zebra_is_dup(const struct lyd_node *dnode, return acl_is_dup(entry_dnode, &ada); } +static void plist_dnode_to_prefix(const struct lyd_node *dnode, bool *any, + struct prefix *p, int *ge, int *le) +{ + *any = false; + *ge = 0; + *le = 0; + + if (yang_dnode_exists(dnode, "./any")) { + *any = true; + return; + } + + switch (yang_dnode_get_enum(dnode, "../type")) { + case YPLT_IPV4: + yang_dnode_get_prefix(p, dnode, "./ipv4-prefix"); + if (yang_dnode_exists(dnode, + "./ipv4-prefix-length-greater-or-equal")) + *ge = yang_dnode_get_uint8( + dnode, "./ipv4-prefix-length-greater-or-equal"); + if (yang_dnode_exists(dnode, + "./ipv4-prefix-length-lesser-or-equal")) + *le = yang_dnode_get_uint8( + dnode, "./ipv4-prefix-length-lesser-or-equal"); + break; + case YPLT_IPV6: + yang_dnode_get_prefix(p, dnode, "./ipv6-prefix"); + if (yang_dnode_exists(dnode, + "./ipv6-prefix-length-greater-or-equal")) + *ge = yang_dnode_get_uint8( + dnode, "./ipv6-prefix-length-greater-or-equal"); + if (yang_dnode_exists(dnode, + "./ipv6-prefix-length-lesser-or-equal")) + *le = yang_dnode_get_uint8( + dnode, "./ipv6-prefix-length-lesser-or-equal"); + break; + } +} + static int _plist_is_dup(const struct lyd_node *dnode, void *arg) { struct plist_dup_args *pda = arg; - int idx; + struct prefix p; + int ge, le; + bool any; /* This entry is the caller, so skip it. */ if (pda->pda_entry_dnode @@ -400,19 +440,14 @@ static int _plist_is_dup(const struct lyd_node *dnode, void *arg) if (strcmp(yang_dnode_get_string(dnode, "action"), pda->pda_action)) return YANG_ITER_CONTINUE; - /* Check if all values match. */ - for (idx = 0; idx < PDA_MAX_VALUES; idx++) { - /* No more values. */ - if (pda->pda_xpath[idx] == NULL) - break; + plist_dnode_to_prefix(dnode, &any, &p, &ge, &le); - /* Not same type, just skip it. */ - if (!yang_dnode_exists(dnode, pda->pda_xpath[idx])) + if (pda->any) { + if (!any) return YANG_ITER_CONTINUE; - - /* Check if different value. */ - if (strcmp(yang_dnode_get_string(dnode, pda->pda_xpath[idx]), - pda->pda_value[idx])) + } else { + if (!prefix_same(&pda->prefix, &p) || pda->ge != ge + || pda->le != le) return YANG_ITER_CONTINUE; } @@ -439,17 +474,6 @@ static bool plist_is_dup_nb(const struct lyd_node *dnode) const struct lyd_node *entry_dnode = yang_dnode_get_parent(dnode, "entry"); struct plist_dup_args pda = {}; - int idx = 0, arg_idx = 0; - static const char *entries[] = { - "./ipv4-prefix", - "./ipv4-prefix-length-greater-or-equal", - "./ipv4-prefix-length-lesser-or-equal", - "./ipv6-prefix", - "./ipv6-prefix-length-greater-or-equal", - "./ipv6-prefix-length-lesser-or-equal", - "./any", - NULL - }; /* Initialize. */ pda.pda_type = yang_dnode_get_string(entry_dnode, "../type"); @@ -457,19 +481,8 @@ static bool plist_is_dup_nb(const struct lyd_node *dnode) pda.pda_action = yang_dnode_get_string(entry_dnode, "action"); pda.pda_entry_dnode = entry_dnode; - /* Load all values/XPaths. */ - while (entries[idx] != NULL) { - if (!yang_dnode_exists(entry_dnode, entries[idx])) { - idx++; - continue; - } - - pda.pda_xpath[arg_idx] = entries[idx]; - pda.pda_value[arg_idx] = - yang_dnode_get_string(entry_dnode, entries[idx]); - arg_idx++; - idx++; - } + plist_dnode_to_prefix(entry_dnode, &pda.any, &pda.prefix, &pda.ge, + &pda.le); return plist_is_dup(entry_dnode, &pda); } diff --git a/lib/frr_zmq.c b/lib/frr_zmq.c index ce52848a25..ea9c828f7c 100644 --- a/lib/frr_zmq.c +++ b/lib/frr_zmq.c @@ -17,6 +17,14 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* + * IF YOU MODIFY THIS FILE PLEASE RUN `make check` and ensure that + * the test_zmq.c unit test is still working. There are dependancies + * between the two that are extremely fragile. My understanding + * is that there is specialized ownership of the cb pointer based + * upon what is happening. Those assumptions are supposed to be + * tested in the test_zmq.c + */ #include <zebra.h> #include <zmq.h> @@ -309,8 +317,22 @@ void frrzmq_thread_cancel(struct frrzmq_cb **cb, struct cb_core *core) core->cancelled = true; thread_cancel(&core->thread); + /* + * Looking at this code one would assume that FRR + * would want a `!(*cb)->write.thread. This was + * attempted in e08165def1c62beee0e87385 but this + * change caused `make check` to stop working + * which was not noticed because our CI system + * does not build with zeromq. Put this back + * to the code as written in 2017. e08165de.. + * was introduced in 2021. So someone was ok + * with frrzmq_thread_cancel for 4 years. This will + * allow those people doing `make check` to continue + * working. In the meantime if the people using + * this code see an issue they can fix it + */ if ((*cb)->read.cancelled && !(*cb)->read.thread - && (*cb)->write.cancelled && !(*cb)->write.thread) + && (*cb)->write.cancelled && (*cb)->write.thread) XFREE(MTYPE_ZEROMQ_CB, *cb); } diff --git a/lib/frrlua.c b/lib/frrlua.c index e97e48121c..00491568f6 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -29,6 +29,8 @@ #include "log.h" #include "buffer.h" +DEFINE_MTYPE(LIB, SCRIPT_RES, "Scripting results"); + /* Lua stuff */ /* @@ -81,7 +83,7 @@ void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix) void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; } @@ -153,7 +155,8 @@ void lua_decode_interface(lua_State *L, int idx, struct interface *ifp) } void *lua_tointerface(lua_State *L, int idx) { - struct interface *ifp = XCALLOC(MTYPE_TMP, sizeof(struct interface)); + struct interface *ifp = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct interface)); lua_decode_interface(L, idx, ifp); return ifp; @@ -183,7 +186,8 @@ void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *inaddr) void *lua_toinaddr(lua_State *L, int idx) { - struct in_addr *inaddr = XCALLOC(MTYPE_TMP, sizeof(struct in_addr)); + struct in_addr *inaddr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in_addr)); lua_decode_inaddr(L, idx, inaddr); return inaddr; } @@ -213,7 +217,8 @@ void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *in6addr) void *lua_toin6addr(lua_State *L, int idx) { - struct in6_addr *in6addr = XCALLOC(MTYPE_TMP, sizeof(struct in6_addr)); + struct in6_addr *in6addr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in6_addr)); lua_decode_in6addr(L, idx, in6addr); return in6addr; } @@ -235,7 +240,9 @@ void lua_pushsockunion(lua_State *L, const union sockunion *su) void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) { lua_getfield(L, idx, "string"); - str2sockunion(lua_tostring(L, -1), su); + if (str2sockunion(lua_tostring(L, -1), su) < 0) + zlog_err("Lua hook call: Failed to decode sockunion"); + lua_pop(L, 1); /* pop the table */ lua_pop(L, 1); @@ -243,7 +250,8 @@ void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) void *lua_tosockunion(lua_State *L, int idx) { - union sockunion *su = XCALLOC(MTYPE_TMP, sizeof(union sockunion)); + union sockunion *su = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(union sockunion)); lua_decode_sockunion(L, idx, su); return su; @@ -262,7 +270,7 @@ void lua_decode_timet(lua_State *L, int idx, time_t *t) void *lua_totimet(lua_State *L, int idx) { - time_t *t = XCALLOC(MTYPE_TMP, sizeof(time_t)); + time_t *t = XCALLOC(MTYPE_SCRIPT_RES, sizeof(time_t)); lua_decode_timet(L, idx, t); return t; @@ -283,7 +291,7 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num) void *lua_tointegerp(lua_State *L, int idx) { - long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + long long *num = XCALLOC(MTYPE_SCRIPT_RES, sizeof(long long)); lua_decode_integerp(L, idx, num); return num; @@ -297,7 +305,7 @@ void lua_decode_stringp(lua_State *L, int idx, char *str) void *lua_tostringp(lua_State *L, int idx) { - char *string = XSTRDUP(MTYPE_TMP, lua_tostring(L, idx)); + char *string = XSTRDUP(MTYPE_SCRIPT_RES, lua_tostring(L, idx)); return string; } @@ -309,6 +317,14 @@ void lua_decode_noop(lua_State *L, int idx, const void *ptr) { } + +/* + * Noop decoder for int. + */ +void lua_decode_integer_noop(lua_State *L, int idx, int i) +{ +} + /* * Logging. * diff --git a/lib/frrlua.h b/lib/frrlua.h index c4de82740c..3e16c82e22 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -34,6 +34,8 @@ extern "C" { #endif +DECLARE_MTYPE(SCRIPT_RES); + /* * gcc-10 is complaining about the wrapper function * not being compatible with lua_pushstring returning @@ -162,10 +164,12 @@ void lua_decode_stringp(lua_State *L, int idx, char *str); void *lua_tostringp(lua_State *L, int idx); /* - * No-op decocder + * No-op decoders */ void lua_decode_noop(lua_State *L, int idx, const void *ptr); +void lua_decode_integer_noop(lua_State *L, int idx, int i); + /* * Retrieve an integer from table on the top of the stack. * diff --git a/lib/frrscript.c b/lib/frrscript.c index 1a9f3639dd..0e0d3c030c 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -102,67 +102,136 @@ static void codec_free(struct codec *c) } #endif -/* Generic script APIs */ +/* Lua function hash utils */ -int _frrscript_call(struct frrscript *fs) +unsigned int lua_function_hash_key(const void *data) { + const struct lua_function_state *lfs = data; - int ret = lua_pcall(fs->L, 0, 0, 0); + return string_hash_make(lfs->name); +} + +bool lua_function_hash_cmp(const void *d1, const void *d2) +{ + const struct lua_function_state *lfs1 = d1; + const struct lua_function_state *lfs2 = d2; + + return strmatch(lfs1->name, lfs2->name); +} + +void *lua_function_alloc(void *arg) +{ + struct lua_function_state *tmp = arg; + + struct lua_function_state *lfs = + XCALLOC(MTYPE_SCRIPT, sizeof(struct lua_function_state)); + lfs->name = tmp->name; + lfs->L = tmp->L; + return lfs; +} + +static void lua_function_free(struct hash_bucket *b, void *data) +{ + struct lua_function_state *lfs = (struct lua_function_state *)b->data; + lua_close(lfs->L); + XFREE(MTYPE_SCRIPT, lfs); +} + +/* internal frrscript APIs */ + +int _frrscript_call_lua(struct lua_function_state *lfs, int nargs) +{ + + int ret; + ret = lua_pcall(lfs->L, nargs, 1, 0); switch (ret) { case LUA_OK: break; case LUA_ERRRUN: - zlog_err("Script '%s' runtime error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : runtime error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRMEM: - zlog_err("Script '%s' memory error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : memory error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRERR: - zlog_err("Script '%s' error handler error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : error handler error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; case LUA_ERRGCMM: - zlog_err("Script '%s' garbage collector error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : garbage collector error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; default: - zlog_err("Script '%s' unknown error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : unknown error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; } if (ret != LUA_OK) { - lua_pop(fs->L, 1); + lua_pop(lfs->L, 1); + goto done; + } + + if (lua_gettop(lfs->L) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return only 1 result", + lfs->name); + ret = 1; goto done; } + if (lua_istable(lfs->L, 1) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return a Lua table", + lfs->name); + ret = 1; + } + done: /* LUA_OK is 0, so we can just return lua_pcall's result directly */ return ret; } -void *frrscript_get_result(struct frrscript *fs, - const struct frrscript_env *result) +void *frrscript_get_result(struct frrscript *fs, const char *function_name, + const char *name, + void *(*lua_to)(lua_State *L, int idx)) { - void *r; - struct frrscript_codec c = {.typename = result->typename}; + void *p; + struct lua_function_state *lfs; + struct lua_function_state lookup = {.name = function_name}; - struct frrscript_codec *codec = hash_lookup(codec_hash, &c); - assert(codec && "No encoder for type"); + lfs = hash_lookup(fs->lua_function_hash, &lookup); - if (!codec->decoder) { - zlog_err("No script decoder for type '%s'", result->typename); + if (lfs == NULL) + return NULL; + + /* At this point, the Lua state should have only the returned table. + * We will then search the table for the key/value we're interested in. + * Then if the value is present (i.e. non-nil), call the lua_to* + * decoder. + */ + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); + lua_getfield(lfs->L, -1, name); + if (lua_isnil(lfs->L, -1)) { + lua_pop(lfs->L, 1); + zlog_warn( + "frrscript: '%s.lua': '%s': tried to decode '%s' as result but failed", + fs->name, function_name, name); return NULL; } + p = lua_to(lfs->L, 2); - lua_getglobal(fs->L, result->name); - r = codec->decoder(fs->L, -1); - lua_pop(fs->L, 1); + /* At the end, the Lua state should be same as it was at the start + * i.e. containing soley the returned table. + */ + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); - return r; + return p; } void frrscript_register_type_codec(struct frrscript_codec *codec) @@ -183,61 +252,74 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs) frrscript_register_type_codec(&codecs[i]); } -struct frrscript *frrscript_load(const char *name, - int (*load_cb)(struct frrscript *)) +struct frrscript *frrscript_new(const char *name) { struct frrscript *fs = XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript)); fs->name = XSTRDUP(MTYPE_SCRIPT, name); - fs->L = luaL_newstate(); - frrlua_export_logging(fs->L); + fs->lua_function_hash = + hash_create(lua_function_hash_key, lua_function_hash_cmp, + "Lua function state hash"); + return fs; +} - char fname[MAXPATHLEN * 2]; - snprintf(fname, sizeof(fname), "%s/%s.lua", scriptdir, fs->name); +int frrscript_load(struct frrscript *fs, const char *function_name, + int (*load_cb)(struct frrscript *)) +{ - int ret = luaL_loadfile(fs->L, fname); + /* Set up the Lua script */ + lua_State *L = luaL_newstate(); - switch (ret) { - case LUA_OK: - break; - case LUA_ERRSYNTAX: - zlog_err("Failed loading script '%s': syntax error: %s", fname, - lua_tostring(fs->L, -1)); - break; - case LUA_ERRMEM: - zlog_err("Failed loading script '%s': out-of-memory error: %s", - fname, lua_tostring(fs->L, -1)); - break; - case LUA_ERRGCMM: - zlog_err( - "Failed loading script '%s': garbage collector error: %s", - fname, lua_tostring(fs->L, -1)); - break; - case LUA_ERRFILE: - zlog_err("Failed loading script '%s': file read error: %s", - fname, lua_tostring(fs->L, -1)); - break; - default: - zlog_err("Failed loading script '%s': unknown error: %s", fname, - lua_tostring(fs->L, -1)); - break; + frrlua_export_logging(L); + + char script_name[MAXPATHLEN]; + + if (snprintf(script_name, sizeof(script_name), "%s/%s.lua", scriptdir, + fs->name) + >= (int)sizeof(script_name)) { + zlog_err("frrscript: path to script %s/%s.lua is too long", + scriptdir, fs->name); + goto fail; } - if (ret != LUA_OK) + if (luaL_dofile(L, script_name) != 0) { + zlog_err("frrscript: failed loading script '%s.lua': error: %s", + script_name, lua_tostring(L, -1)); goto fail; + } - if (load_cb && (*load_cb)(fs) != 0) + /* To check the Lua function, we get it from the global table */ + lua_getglobal(L, function_name); + if (lua_isfunction(L, lua_gettop(L)) == 0) { + zlog_err("frrscript: loaded script '%s.lua' but %s not found", + script_name, function_name); goto fail; + } + /* Then pop the function (frrscript_call will push it when it needs it) + */ + lua_pop(L, 1); - return fs; + if (load_cb && (*load_cb)(fs) != 0) { + zlog_err( + "frrscript: '%s.lua': %s: loaded but callback returned non-zero exit code", + script_name, function_name); + goto fail; + } + + /* Add the Lua function state to frrscript */ + struct lua_function_state key = {.name = function_name, .L = L}; + + hash_get(fs->lua_function_hash, &key, lua_function_alloc); + + return 0; fail: - frrscript_unload(fs); - return NULL; + lua_close(L); + return 1; } -void frrscript_unload(struct frrscript *fs) +void frrscript_delete(struct frrscript *fs) { - lua_close(fs->L); + hash_iterate(fs->lua_function_hash, lua_function_free, NULL); XFREE(MTYPE_SCRIPT, fs->name); XFREE(MTYPE_SCRIPT, fs); } diff --git a/lib/frrscript.h b/lib/frrscript.h index 8612c602f3..c089df61fc 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -25,7 +25,7 @@ #include <lua.h> #include "frrlua.h" -#include "../bgpd/bgp_script.h" +#include "bgpd/bgp_script.h" // for peer and attr encoders/decoders #ifdef __cplusplus extern "C" { @@ -40,14 +40,30 @@ struct frrscript_codec { decoder_func decoder; }; +struct lua_function_state { + const char *name; + lua_State *L; +}; + struct frrscript { /* Script name */ char *name; - /* Lua state */ - struct lua_State *L; + /* Hash of Lua function name to Lua function state */ + struct hash *lua_function_hash; }; + +/* + * Hash related functions for lua_function_hash + */ + +void *lua_function_alloc(void *arg); + +unsigned int lua_function_hash_key(const void *data); + +bool lua_function_hash_cmp(const void *d1, const void *d2); + struct frrscript_env { /* Value type */ const char *typename; @@ -60,15 +76,24 @@ struct frrscript_env { }; /* - * Create new FRR script. + * Create new struct frrscript for a Lua script. + * This will hold the states for the Lua functions in this script. + * + * scriptname + * Name of the Lua script file, without the .lua + */ +struct frrscript *frrscript_new(const char *scriptname); + +/* + * Load a function into frrscript, run callback if any */ -struct frrscript *frrscript_load(const char *name, - int (*load_cb)(struct frrscript *)); +int frrscript_load(struct frrscript *fs, const char *function_name, + int (*load_cb)(struct frrscript *)); /* - * Destroy FRR script. + * Delete Lua function states and frrscript */ -void frrscript_unload(struct frrscript *fs); +void frrscript_delete(struct frrscript *fs); /* * Register a Lua codec for a type. @@ -97,16 +122,31 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs); */ void frrscript_init(const char *scriptdir); -#define ENCODE_ARGS(name, value) \ - do { \ - ENCODE_ARGS_WITH_STATE(L, value); \ - lua_setglobal(L, name); \ - } while (0) +/* + * This macro is mapped to every (name, value) in frrscript_call, + * so this in turn maps them onto their encoders + */ +#define ENCODE_ARGS(name, value) ENCODE_ARGS_WITH_STATE(lfs->L, (value)) +/* + * This macro is also mapped to every (name, value) in frrscript_call, but + * not every value can be mapped to its decoder - only those that appear + * in the returned table will. To find out if they appear in the returned + * table, first pop the value and check if its nil. Only call the decoder + * if non-nil. + * + * At the end, the only thing left on the stack should be the + * returned table. + */ #define DECODE_ARGS(name, value) \ do { \ - lua_getglobal(L, name); \ - DECODE_ARGS_WITH_STATE(L, value); \ + lua_getfield(lfs->L, 1, (name)); \ + if (lua_isnil(lfs->L, 2)) { \ + lua_pop(lfs->L, 1); \ + } else { \ + DECODE_ARGS_WITH_STATE(lfs->L, (value)); \ + } \ + assert(lua_gettop(lfs->L) == 1); \ } while (0) /* @@ -120,6 +160,7 @@ void frrscript_init(const char *scriptdir); */ #define ENCODE_ARGS_WITH_STATE(L, value) \ _Generic((value), \ +int : lua_pushinteger, \ long long * : lua_pushintegerp, \ struct prefix * : lua_pushprefix, \ struct interface * : lua_pushinterface, \ @@ -131,10 +172,11 @@ char * : lua_pushstring_wrapper, \ struct attr * : lua_pushattr, \ struct peer * : lua_pushpeer, \ const struct prefix * : lua_pushprefix \ -)(L, value) +)((L), (value)) #define DECODE_ARGS_WITH_STATE(L, value) \ _Generic((value), \ +int : lua_decode_integer_noop, \ long long * : lua_decode_integerp, \ struct prefix * : lua_decode_prefix, \ struct interface * : lua_decode_interface, \ @@ -146,56 +188,96 @@ char * : lua_decode_stringp, \ struct attr * : lua_decode_attr, \ struct peer * : lua_decode_noop, \ const struct prefix * : lua_decode_noop \ -)(L, -1, value) +)((L), -1, (value)) /* - * Call script. + * Call Lua function state (abstraction for a single Lua function) * - * fs - * The script to call; this is obtained from frrscript_load(). + * lfs + * The Lua function to call; this should have been loaded in by + * frrscript_load(). nargs Number of arguments the function accepts * * Returns: * 0 if the script ran successfully, nonzero otherwise. */ -int _frrscript_call(struct frrscript *fs); +int _frrscript_call_lua(struct lua_function_state *lfs, int nargs); /* - * Wrapper for call script. Maps values passed in to their encoder - * and decoder types. + * Wrapper for calling Lua function state. + * + * The Lua function name (f) to run should have already been checked by + * frrscript_load. So this wrapper will: + * 1) Find the Lua function state, which contains the Lua state + * 2) Clear the Lua state (there may be leftovers items from previous call) + * 3) Push the Lua function (f) + * 4) Map frrscript_call arguments onto their encoder and decoders, push those + * 5) Call _frrscript_call_lua (Lua execution takes place) + * 6) Write back to frrscript_call arguments using their decoders + * + * This wrapper can be called multiple times (after one frrscript_load). * * fs - * The script to call; this is obtained from frrscript_load(). + * The struct frrscript in which the Lua fuction was loaded into + * f + * Name of the Lua function. * * Returns: * 0 if the script ran successfully, nonzero otherwise. */ -#define frrscript_call(fs, ...) \ - ({ \ - lua_State *L = fs->L; \ - MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ - int ret = _frrscript_call(fs); \ - if (ret == 0) { \ - MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \ - } \ - ret; \ +#define frrscript_call(fs, f, ...) \ + ({ \ + struct lua_function_state lookup = {.name = (f)}; \ + struct lua_function_state *lfs; \ + lfs = hash_lookup((fs)->lua_function_hash, &lookup); \ + lfs == NULL ? ({ \ + zlog_err( \ + "frrscript: '%s.lua': '%s': tried to call this function but it was not loaded", \ + (fs)->name, (f)); \ + 1; \ + }) \ + : ({ \ + lua_settop(lfs->L, 0); \ + lua_getglobal(lfs->L, f); \ + MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ + _frrscript_call_lua( \ + lfs, PP_NARG(__VA_ARGS__)); \ + }) != 0 \ + ? ({ \ + zlog_err( \ + "frrscript: '%s.lua': '%s': this function called but returned non-zero exit code. No variables modified.", \ + (fs)->name, (f)); \ + 1; \ + }) \ + : ({ \ + MAP_LISTS(DECODE_ARGS, \ + ##__VA_ARGS__); \ + 0; \ + }); \ }) /* - * Get result from finished script. + * Get result from finished function * * fs * The script. This script must have been run already. - * - * result - * The result to extract from the script. - * This reuses the frrscript_env type, but only the typename and name fields - * need to be set. The value is returned directly. + * function_name + * Name of the Lua function. + * name + * Name of the result. + * This will be used as a string key to retrieve from the table that the + * Lua function returns. + * The name here should *not* appear in frrscript_call. + * lua_to + * Function pointer to a lua_to decoder function. + * This function should allocate and decode a value from the Lua state. * * Returns: - * The script result of the specified name and type, or NULL. + * A pointer to the decoded value from the Lua state, or NULL if no such + * value. */ -void *frrscript_get_result(struct frrscript *fs, - const struct frrscript_env *result); +void *frrscript_get_result(struct frrscript *fs, const char *function_name, + const char *name, + void *(*lua_to)(lua_State *L, int idx)); #ifdef __cplusplus } diff --git a/lib/frrstr.c b/lib/frrstr.c index 7ef5fffd12..1b98b224cc 100644 --- a/lib/frrstr.c +++ b/lib/frrstr.c @@ -18,9 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include "zebra.h" #include <string.h> #include <ctype.h> @@ -217,3 +215,21 @@ int all_digit(const char *str) return 0; return 1; } + + +char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num) +{ + if (bufsiz == 0) + return buff; + + char tmp[3]; + + buff[0] = '\0'; + + for (size_t i = 0; i < num; i++) { + snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); + strlcat(buff, tmp, bufsiz); + } + + return buff; +} diff --git a/lib/frrstr.h b/lib/frrstr.h index 441d7b8670..d52d6a4482 100644 --- a/lib/frrstr.h +++ b/lib/frrstr.h @@ -154,6 +154,26 @@ bool frrstr_endswith(const char *str, const char *suffix); */ int all_digit(const char *str); +/* + * Copy the hexadecimal representation of the string to a buffer. + * + * buff + * Buffer to copy result into with size of at least (2 * num) + 1. + * + * bufsiz + * Size of destination buffer. + * + * str + * String to represent as hexadecimal. + * + * num + * Number of characters to copy. + * + * Returns: + * Pointer to buffer containing resulting hexadecimal representation. + */ +char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num); + #ifdef __cplusplus } #endif @@ -1291,6 +1291,11 @@ static void cli_show_interface(struct vty *vty, struct lyd_node *dnode, vty_out(vty, "\n"); } +static void cli_show_interface_end(struct vty *vty, struct lyd_node *dnode) +{ + vty_out(vty, "exit\n"); +} + /* * XPath: /frr-interface:lib/interface/description */ @@ -1349,10 +1354,20 @@ static const struct cmd_variable_handler if_var_handlers[] = { {.tokenname = "INTERFACE", .completions = if_autocomplete}, {.completions = NULL}}; -void if_cmd_init(void) +static struct cmd_node interface_node = { + .name = "interface", + .node = INTERFACE_NODE, + .parent_node = CONFIG_NODE, + .prompt = "%s(config-if)# ", +}; + +void if_cmd_init(int (*config_write)(struct vty *)) { cmd_variable_handler_register(if_var_handlers); + interface_node.config_write = config_write; + install_node(&interface_node); + install_element(CONFIG_NODE, &interface_cmd); install_element(CONFIG_NODE, &no_interface_cmd); @@ -1642,6 +1657,7 @@ const struct frr_yang_module_info frr_interface_info = { .create = lib_interface_create, .destroy = lib_interface_destroy, .cli_show = cli_show_interface, + .cli_show_end = cli_show_interface_end, .get_next = lib_interface_get_next, .get_keys = lib_interface_get_keys, .lookup_entry = lib_interface_lookup_entry, @@ -597,7 +597,8 @@ struct if_link_params *if_link_params_get(struct interface *); void if_link_params_free(struct interface *); /* Northbound. */ -extern void if_cmd_init(void); +struct vty; +extern void if_cmd_init(int (*config_write)(struct vty *)); extern void if_zapi_callbacks(int (*create)(struct interface *ifp), int (*up)(struct interface *ifp), int (*down)(struct interface *ifp), diff --git a/lib/keychain.c b/lib/keychain.c index db5c23b1ba..02f83ef0a8 100644 --- a/lib/keychain.c +++ b/lib/keychain.c @@ -1044,6 +1044,7 @@ static int keychain_config_write(struct vty *vty) vty_out(vty, " exit\n"); } + vty_out(vty, "exit\n"); vty_out(vty, "!\n"); } diff --git a/lib/libfrr.c b/lib/libfrr.c index 97dab74d9b..d03437328b 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -418,7 +418,6 @@ static int frr_opt(int opt) switch (opt) { case 'h': frr_help_exit(0); - break; case 'v': print_version(di->progname); exit(0); diff --git a/lib/nexthop_group.c b/lib/nexthop_group.c index 4fee9bde3c..97d70189ff 100644 --- a/lib/nexthop_group.c +++ b/lib/nexthop_group.c @@ -1156,6 +1156,7 @@ static int nexthop_group_write(struct vty *vty) nexthop_group_write_nexthop_internal(vty, nh); } + vty_out(vty, "exit\n"); vty_out(vty, "!\n"); } @@ -49,7 +49,8 @@ struct pbr_filter { #define PBR_FILTER_PROTO (1 << 5) #define PBR_FILTER_SRC_PORT_RANGE (1 << 6) #define PBR_FILTER_DST_PORT_RANGE (1 << 7) -#define PBR_FILTER_DSFIELD (1 << 8) +#define PBR_FILTER_DSFIELD (1 << 8) +#define PBR_FILTER_IP_PROTOCOL (1 << 9) #define PBR_DSFIELD_DSCP (0xfc) /* Upper 6 bits of DS field: DSCP */ #define PBR_DSFIELD_ECN (0x03) /* Lower 2 bits of DS field: BCN */ @@ -67,6 +68,9 @@ struct pbr_filter { /* Filter with fwmark */ uint32_t fwmark; + + /* Filter with the ip protocol */ + uint8_t ip_proto; }; /* diff --git a/lib/plist.c b/lib/plist.c index 0ee02f8a0b..2f9f06f43b 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -750,7 +750,7 @@ static const char *prefix_list_type_str(struct prefix_list_entry *pentry) } static int prefix_list_entry_match(struct prefix_list_entry *pentry, - const struct prefix *p) + const struct prefix *p, bool address_mode) { int ret; @@ -761,6 +761,9 @@ static int prefix_list_entry_match(struct prefix_list_entry *pentry, if (!ret) return 0; + if (address_mode) + return 1; + /* In case of le nor ge is specified, exact match is performed. */ if (!pentry->le && !pentry->ge) { if (pentry->prefix.prefixlen != p->prefixlen) @@ -777,14 +780,15 @@ static int prefix_list_entry_match(struct prefix_list_entry *pentry, return 1; } -enum prefix_list_type prefix_list_apply_which_prefix( +enum prefix_list_type prefix_list_apply_ext( struct prefix_list *plist, - const struct prefix **which, - const void *object) + const struct prefix_list_entry **which, + union prefixconstptr object, + bool address_mode) { struct prefix_list_entry *pentry, *pbest = NULL; - const struct prefix *p = (const struct prefix *)object; + const struct prefix *p = object.p; const uint8_t *byte = p->u.val; size_t depth; size_t validbits = p->prefixlen; @@ -809,7 +813,7 @@ enum prefix_list_type prefix_list_apply_which_prefix( pentry = pentry->next_best) { if (pbest && pbest->seq < pentry->seq) continue; - if (prefix_list_entry_match(pentry, p)) + if (prefix_list_entry_match(pentry, p, address_mode)) pbest = pentry; } @@ -830,7 +834,7 @@ enum prefix_list_type prefix_list_apply_which_prefix( pentry = pentry->next_best) { if (pbest && pbest->seq < pentry->seq) continue; - if (prefix_list_entry_match(pentry, p)) + if (prefix_list_entry_match(pentry, p, address_mode)) pbest = pentry; } break; @@ -838,7 +842,7 @@ enum prefix_list_type prefix_list_apply_which_prefix( if (which) { if (pbest) - *which = &pbest->prefix; + *which = pbest; else *which = NULL; } @@ -878,7 +882,7 @@ static void __attribute__((unused)) prefix_list_print(struct prefix_list *plist) } } -/* Retrun 1 when plist already include pentry policy. */ +/* Return 1 when plist already include pentry policy. */ static struct prefix_list_entry * prefix_entry_dup_check(struct prefix_list *plist, struct prefix_list_entry *new) { @@ -928,102 +932,206 @@ enum display_type { first_match_display }; -static void vty_show_prefix_entry(struct vty *vty, afi_t afi, +static void vty_show_prefix_entry(struct vty *vty, json_object *json, afi_t afi, struct prefix_list *plist, struct prefix_master *master, enum display_type dtype, int seqnum) { struct prefix_list_entry *pentry; + json_object *json_pl = NULL; /* Print the name of the protocol */ - vty_out(vty, "%s: ", frr_protoname); + if (json) { + json_pl = json_object_new_object(); + json_object_object_add(json, plist->name, json_pl); + } else + vty_out(vty, "%s: ", frr_protoname); if (dtype == normal_display) { - vty_out(vty, "ip%s prefix-list %s: %d entries\n", - afi == AFI_IP ? "" : "v6", plist->name, plist->count); - if (plist->desc) - vty_out(vty, " Description: %s\n", plist->desc); + if (json) { + json_object_string_add(json_pl, "addressFamily", + afi2str(afi)); + json_object_int_add(json_pl, "entries", plist->count); + if (plist->desc) + json_object_string_add(json_pl, "description", + plist->desc); + } else { + vty_out(vty, "ip%s prefix-list %s: %d entries\n", + afi == AFI_IP ? "" : "v6", plist->name, + plist->count); + if (plist->desc) + vty_out(vty, " Description: %s\n", + plist->desc); + } } else if (dtype == summary_display || dtype == detail_display) { - vty_out(vty, "ip%s prefix-list %s:\n", - afi == AFI_IP ? "" : "v6", plist->name); - - if (plist->desc) - vty_out(vty, " Description: %s\n", plist->desc); - - vty_out(vty, - " count: %d, range entries: %d, sequences: %" PRId64 " - %" PRId64 "\n", - plist->count, plist->rangecount, - plist->head ? plist->head->seq : 0, - plist->tail ? plist->tail->seq : 0); + if (json) { + json_object_string_add(json_pl, "addressFamily", + afi2str(afi)); + if (plist->desc) + json_object_string_add(json_pl, "description", + plist->desc); + json_object_int_add(json_pl, "count", plist->count); + json_object_int_add(json_pl, "rangeEntries", + plist->rangecount); + json_object_int_add(json_pl, "sequenceStart", + plist->head ? plist->head->seq : 0); + json_object_int_add(json_pl, "sequenceEnd", + plist->tail ? plist->tail->seq : 0); + } else { + vty_out(vty, "ip%s prefix-list %s:\n", + afi == AFI_IP ? "" : "v6", plist->name); + + if (plist->desc) + vty_out(vty, " Description: %s\n", + plist->desc); + + vty_out(vty, + " count: %d, range entries: %d, sequences: %" PRId64 + " - %" PRId64 "\n", + plist->count, plist->rangecount, + plist->head ? plist->head->seq : 0, + plist->tail ? plist->tail->seq : 0); + } } if (dtype != summary_display) { + json_object *json_entries = NULL; + + if (json) { + json_entries = json_object_new_array(); + json_object_object_add(json_pl, "entries", + json_entries); + } + for (pentry = plist->head; pentry; pentry = pentry->next) { if (dtype == sequential_display && pentry->seq != seqnum) continue; - vty_out(vty, " "); + if (json) { + json_object *json_entry; + char buf[BUFSIZ]; - vty_out(vty, "seq %" PRId64 " ", pentry->seq); + json_entry = json_object_new_object(); + json_object_array_add(json_entries, json_entry); - vty_out(vty, "%s ", prefix_list_type_str(pentry)); - - if (pentry->any) - vty_out(vty, "any"); - else { - struct prefix *p = &pentry->prefix; - - vty_out(vty, "%pFX", p); + json_object_int_add(json_entry, + "sequenceNumber", + pentry->seq); + json_object_string_add( + json_entry, "type", + prefix_list_type_str(pentry)); + json_object_string_add( + json_entry, "prefix", + prefix2str(&pentry->prefix, buf, + sizeof(buf))); if (pentry->ge) - vty_out(vty, " ge %d", pentry->ge); + json_object_int_add( + json_entry, + "minimumPrefixLength", + pentry->ge); if (pentry->le) - vty_out(vty, " le %d", pentry->le); + json_object_int_add( + json_entry, + "maximumPrefixLength", + pentry->le); + + if (dtype == detail_display + || dtype == sequential_display) { + json_object_int_add(json_entry, + "hitCount", + pentry->hitcnt); + json_object_int_add(json_entry, + "referenceCount", + pentry->refcnt); + } + } else { + vty_out(vty, " "); + + vty_out(vty, "seq %" PRId64 " ", pentry->seq); + + vty_out(vty, "%s ", + prefix_list_type_str(pentry)); + + if (pentry->any) + vty_out(vty, "any"); + else { + struct prefix *p = &pentry->prefix; + + vty_out(vty, "%pFX", p); + + if (pentry->ge) + vty_out(vty, " ge %d", + pentry->ge); + if (pentry->le) + vty_out(vty, " le %d", + pentry->le); + } + + if (dtype == detail_display + || dtype == sequential_display) + vty_out(vty, + " (hit count: %ld, refcount: %ld)", + pentry->hitcnt, pentry->refcnt); + + vty_out(vty, "\n"); } - - if (dtype == detail_display - || dtype == sequential_display) - vty_out(vty, " (hit count: %ld, refcount: %ld)", - pentry->hitcnt, pentry->refcnt); - - vty_out(vty, "\n"); } } } static int vty_show_prefix_list(struct vty *vty, afi_t afi, const char *name, - const char *seq, enum display_type dtype) + const char *seq, enum display_type dtype, + bool uj) { struct prefix_list *plist; struct prefix_master *master; int64_t seqnum = 0; + json_object *json = NULL; + json_object *json_proto = NULL; master = prefix_master_get(afi, 0); if (master == NULL) return CMD_WARNING; + if (uj) { + json = json_object_new_object(); + json_proto = json_object_new_object(); + json_object_object_add(json, frr_protoname, json_proto); + } + if (seq) seqnum = (int64_t)atol(seq); if (name) { plist = prefix_list_lookup(afi, name); if (!plist) { - vty_out(vty, "%% Can't find specified prefix-list\n"); + if (!uj) + vty_out(vty, + "%% Can't find specified prefix-list\n"); return CMD_WARNING; } - vty_show_prefix_entry(vty, afi, plist, master, dtype, seqnum); + vty_show_prefix_entry(vty, json_proto, afi, plist, master, + dtype, seqnum); } else { if (dtype == detail_display || dtype == summary_display) { - if (master->recent) + if (master->recent && !uj) vty_out(vty, "Prefix-list with the last deletion/insertion: %s\n", master->recent->name); } for (plist = master->str.head; plist; plist = plist->next) - vty_show_prefix_entry(vty, afi, plist, master, dtype, - seqnum); + vty_show_prefix_entry(vty, json_proto, afi, plist, + master, dtype, seqnum); + } + + if (uj) { + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + json, JSON_C_TO_STRING_PRETTY)); + json_object_free(json); } return CMD_SUCCESS; @@ -1146,19 +1254,21 @@ static int vty_clear_prefix_list(struct vty *vty, afi_t afi, const char *name, DEFPY (show_ip_prefix_list, show_ip_prefix_list_cmd, - "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]", + "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]] [json$uj]", SHOW_STR IP_STR PREFIX_LIST_STR "Name of a prefix list\n" "sequence number of an entry\n" - "Sequence number\n") + "Sequence number\n" + JSON_STR) { enum display_type dtype = normal_display; if (dseq) dtype = sequential_display; - return vty_show_prefix_list(vty, AFI_IP, prefix_list, arg_str, dtype); + return vty_show_prefix_list(vty, AFI_IP, prefix_list, arg_str, dtype, + !!uj); } DEFPY (show_ip_prefix_list_prefix, @@ -1184,28 +1294,30 @@ DEFPY (show_ip_prefix_list_prefix, DEFPY (show_ip_prefix_list_summary, show_ip_prefix_list_summary_cmd, - "show ip prefix-list summary [WORD$prefix_list]", + "show ip prefix-list summary [WORD$prefix_list] [json$uj]", SHOW_STR IP_STR PREFIX_LIST_STR "Summary of prefix lists\n" - "Name of a prefix list\n") + "Name of a prefix list\n" + JSON_STR) { return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL, - summary_display); + summary_display, !!uj); } DEFPY (show_ip_prefix_list_detail, show_ip_prefix_list_detail_cmd, - "show ip prefix-list detail [WORD$prefix_list]", + "show ip prefix-list detail [WORD$prefix_list] [json$uj]", SHOW_STR IP_STR PREFIX_LIST_STR "Detail of prefix lists\n" - "Name of a prefix list\n") + "Name of a prefix list\n" + JSON_STR) { return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL, - detail_display); + detail_display, !!uj); } DEFPY (clear_ip_prefix_list, @@ -1222,19 +1334,21 @@ DEFPY (clear_ip_prefix_list, DEFPY (show_ipv6_prefix_list, show_ipv6_prefix_list_cmd, - "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]", + "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]] [json$uj]", SHOW_STR IPV6_STR PREFIX_LIST_STR "Name of a prefix list\n" "sequence number of an entry\n" - "Sequence number\n") + "Sequence number\n" + JSON_STR) { enum display_type dtype = normal_display; if (dseq) dtype = sequential_display; - return vty_show_prefix_list(vty, AFI_IP6, prefix_list, arg_str, dtype); + return vty_show_prefix_list(vty, AFI_IP6, prefix_list, arg_str, dtype, + !!uj); } DEFPY (show_ipv6_prefix_list_prefix, @@ -1260,28 +1374,30 @@ DEFPY (show_ipv6_prefix_list_prefix, DEFPY (show_ipv6_prefix_list_summary, show_ipv6_prefix_list_summary_cmd, - "show ipv6 prefix-list summary [WORD$prefix-list]", + "show ipv6 prefix-list summary [WORD$prefix-list] [json$uj]", SHOW_STR IPV6_STR PREFIX_LIST_STR "Summary of prefix lists\n" - "Name of a prefix list\n") + "Name of a prefix list\n" + JSON_STR) { return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL, - summary_display); + summary_display, !!uj); } DEFPY (show_ipv6_prefix_list_detail, show_ipv6_prefix_list_detail_cmd, - "show ipv6 prefix-list detail [WORD$prefix-list]", + "show ipv6 prefix-list detail [WORD$prefix-list] [json$uj]", SHOW_STR IPV6_STR PREFIX_LIST_STR "Detail of prefix lists\n" - "Name of a prefix list\n") + "Name of a prefix list\n" + JSON_STR) { return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL, - detail_display); + detail_display, !!uj); } DEFPY (clear_ipv6_prefix_list, @@ -1296,6 +1412,51 @@ DEFPY (clear_ipv6_prefix_list, return vty_clear_prefix_list(vty, AFI_IP6, prefix_list, prefix_str); } +DEFPY (debug_prefix_list_match, + debug_prefix_list_match_cmd, + "debug prefix-list WORD$prefix-list match <A.B.C.D/M|X:X::X:X/M>" + " [address-mode$addr_mode]", + DEBUG_STR + "Prefix-list test access\n" + "Name of a prefix list\n" + "Test prefix for prefix list result\n" + "Prefix to test in ip prefix-list\n" + "Prefix to test in ipv6 prefix-list\n" + "Use address matching mode (PIM RP)\n") +{ + struct prefix_list *plist; + const struct prefix_list_entry *entry = NULL; + enum prefix_list_type ret; + + plist = prefix_list_lookup(family2afi(match->family), prefix_list); + if (!plist) { + vty_out(vty, "%% no prefix list named %s for AFI %s\n", + prefix_list, afi2str(family2afi(match->family))); + return CMD_WARNING; + } + + ret = prefix_list_apply_ext(plist, &entry, match, !!addr_mode); + + vty_out(vty, "%s prefix list %s yields %s for %pFX, ", + afi2str(family2afi(match->family)), prefix_list, + ret == PREFIX_DENY ? "DENY" : "PERMIT", match); + + if (!entry) + vty_out(vty, "no match found\n"); + else { + vty_out(vty, "matching entry #%"PRId64": %pFX", entry->seq, + &entry->prefix); + if (entry->ge) + vty_out(vty, " ge %d", entry->ge); + if (entry->le) + vty_out(vty, " le %d", entry->le); + vty_out(vty, "\n"); + } + + /* allow using this in scripts for quick prefix-list member tests */ + return (ret == PREFIX_PERMIT) ? CMD_SUCCESS : CMD_WARNING; +} + struct stream *prefix_bgp_orf_entry(struct stream *s, struct prefix_list *plist, uint8_t init_flag, uint8_t permit_flag, uint8_t deny_flag) @@ -1537,6 +1698,7 @@ static void prefix_list_init_ipv6(void) install_element(VIEW_NODE, &show_ipv6_prefix_list_prefix_cmd); install_element(VIEW_NODE, &show_ipv6_prefix_list_summary_cmd); install_element(VIEW_NODE, &show_ipv6_prefix_list_detail_cmd); + install_element(VIEW_NODE, &debug_prefix_list_match_cmd); install_element(ENABLE_NODE, &clear_ipv6_prefix_list_cmd); } diff --git a/lib/plist.h b/lib/plist.h index 57eb763a68..c9507df57c 100644 --- a/lib/plist.h +++ b/lib/plist.h @@ -37,6 +37,7 @@ enum prefix_list_type { }; struct prefix_list; +struct prefix_list_entry; struct orf_prefix { uint32_t seq; @@ -63,12 +64,18 @@ extern struct prefix_list *prefix_list_lookup(afi_t, const char *); * * If no pointer is sent in, do not return anything. * If it is a empty plist return a NULL pointer. + * + * address_mode = the "prefix" being passed in is really an address, match + * regardless of prefix length (i.e. ge/le are ignored.) prefix->prefixlen + * must be /32. */ extern enum prefix_list_type -prefix_list_apply_which_prefix(struct prefix_list *plist, - const struct prefix **which, - const void *object); -#define prefix_list_apply(A, B) prefix_list_apply_which_prefix((A), NULL, (B)) +prefix_list_apply_ext(struct prefix_list *plist, + const struct prefix_list_entry **matches, + union prefixconstptr prefix, + bool address_mode); +#define prefix_list_apply(A, B) \ + prefix_list_apply_ext((A), NULL, (B), false) extern struct prefix_list *prefix_bgp_orf_lookup(afi_t, const char *); extern struct stream *prefix_bgp_orf_entry(struct stream *, diff --git a/lib/route_opaque.h b/lib/route_opaque.h index 599a0363eb..7c4e9a16e1 100644 --- a/lib/route_opaque.h +++ b/lib/route_opaque.h @@ -21,12 +21,15 @@ #ifndef FRR_ROUTE_OPAQUE_H #define FRR_ROUTE_OPAQUE_H +#include "assert.h" +#include "zclient.h" + #include "bgpd/bgp_aspath.h" #include "bgpd/bgp_community.h" #include "bgpd/bgp_lcommunity.h" struct bgp_zebra_opaque { - char aspath[ASPATH_STR_DEFAULT_LEN]; + char aspath[256]; /* Show at least 10 communities AA:BB */ char community[COMMUNITY_SIZE * 20]; @@ -35,4 +38,7 @@ struct bgp_zebra_opaque { char lcommunity[LCOMMUNITY_SIZE * 30]; }; +static_assert(sizeof(struct bgp_zebra_opaque) <= ZAPI_MESSAGE_OPAQUE_LENGTH, + "BGP opaque data shouldn't be larger than zebra's buffer"); + #endif /* FRR_ROUTE_OPAQUE_H */ diff --git a/lib/route_types.txt b/lib/route_types.txt index c48391545d..77639070c9 100644 --- a/lib/route_types.txt +++ b/lib/route_types.txt @@ -1,4 +1,4 @@ -# Canonical Zserv route types information registry for Quagga. +# Canonical Zserv route types information registry for FRR. # # Used to construct route_types.c and route_types.h # @@ -60,7 +60,7 @@ ZEBRA_ROUTE_PIM, pim, pimd, 'P', 0, 0, 0, "PIM", pimd ZEBRA_ROUTE_EIGRP, eigrp, eigrpd, 'E', 1, 0, 1, "EIGRP", eigrpd ZEBRA_ROUTE_NHRP, nhrp, nhrpd, 'N', 1, 1, 1, "NHRP", nhrpd # HSLS and OLSR both are AFI independent (so: 1, 1), however -# we want to disable for them for general Quagga distribution. +# we want to disable for them for general FRR distribution. # This at least makes it trivial for users of these protocols # to 'switch on' redist support (direct numeric entry remaining # possible). diff --git a/lib/routemap.c b/lib/routemap.c index 9dc1c7c82d..594dcf97cb 100644 --- a/lib/routemap.c +++ b/lib/routemap.c @@ -22,6 +22,7 @@ #include "linklist.h" #include "memory.h" +#include "command.h" #include "vector.h" #include "prefix.h" #include "vty.h" @@ -32,6 +33,7 @@ #include "libfrr.h" #include "lib_errors.h" #include "table.h" +#include "json.h" DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP, "Route map"); DEFINE_MTYPE(LIB, ROUTE_MAP_NAME, "Route map name"); @@ -840,50 +842,140 @@ static const char *route_map_result_str(route_map_result_t res) } /* show route-map */ -static void vty_show_route_map_entry(struct vty *vty, struct route_map *map) +static void vty_show_route_map_entry(struct vty *vty, struct route_map *map, + json_object *json) { struct route_map_index *index; struct route_map_rule *rule; - - vty_out(vty, "route-map: %s Invoked: %" PRIu64 " Optimization: %s Processed Change: %s\n", - map->name, map->applied - map->applied_clear, - map->optimization_disabled ? "disabled" : "enabled", - map->to_be_processed ? "true" : "false"); + json_object *json_rmap = NULL; + json_object *json_rules = NULL; + + if (json) { + json_rmap = json_object_new_object(); + json_object_object_add(json, map->name, json_rmap); + + json_rules = json_object_new_array(); + json_object_int_add(json_rmap, "invoked", + map->applied - map->applied_clear); + json_object_boolean_add(json_rmap, "disabledOptimization", + map->optimization_disabled); + json_object_boolean_add(json_rmap, "processedChange", + map->to_be_processed); + json_object_object_add(json_rmap, "rules", json_rules); + } else { + vty_out(vty, + "route-map: %s Invoked: %" PRIu64 + " Optimization: %s Processed Change: %s\n", + map->name, map->applied - map->applied_clear, + map->optimization_disabled ? "disabled" : "enabled", + map->to_be_processed ? "true" : "false"); + } for (index = map->head; index; index = index->next) { - vty_out(vty, " %s, sequence %d Invoked %" PRIu64 "\n", - route_map_type_str(index->type), index->pref, - index->applied - index->applied_clear); - - /* Description */ - if (index->description) - vty_out(vty, " Description:\n %s\n", - index->description); - - /* Match clauses */ - vty_out(vty, " Match clauses:\n"); - for (rule = index->match_list.head; rule; rule = rule->next) - vty_out(vty, " %s %s\n", rule->cmd->str, - rule->rule_str); - - vty_out(vty, " Set clauses:\n"); - for (rule = index->set_list.head; rule; rule = rule->next) - vty_out(vty, " %s %s\n", rule->cmd->str, - rule->rule_str); - - /* Call clause */ - vty_out(vty, " Call clause:\n"); - if (index->nextrm) - vty_out(vty, " Call %s\n", index->nextrm); - - /* Exit Policy */ - vty_out(vty, " Action:\n"); - if (index->exitpolicy == RMAP_GOTO) - vty_out(vty, " Goto %d\n", index->nextpref); - else if (index->exitpolicy == RMAP_NEXT) - vty_out(vty, " Continue to next entry\n"); - else if (index->exitpolicy == RMAP_EXIT) - vty_out(vty, " Exit routemap\n"); + if (json) { + json_object *json_rule; + json_object *json_matches; + json_object *json_sets; + char action[BUFSIZ] = {}; + + json_rule = json_object_new_object(); + json_object_array_add(json_rules, json_rule); + + json_object_int_add(json_rule, "sequenceNumber", + index->pref); + json_object_string_add(json_rule, "type", + route_map_type_str(index->type)); + json_object_int_add(json_rule, "invoked", + index->applied + - index->applied_clear); + + /* Description */ + if (index->description) + json_object_string_add(json_rule, "description", + index->description); + + /* Match clauses */ + json_matches = json_object_new_array(); + json_object_object_add(json_rule, "matchClauses", + json_matches); + for (rule = index->match_list.head; rule; + rule = rule->next) { + char buf[BUFSIZ]; + + snprintf(buf, sizeof(buf), "%s %s", + rule->cmd->str, rule->rule_str); + json_array_string_add(json_matches, buf); + } + + /* Set clauses */ + json_sets = json_object_new_array(); + json_object_object_add(json_rule, "setClauses", + json_sets); + for (rule = index->set_list.head; rule; + rule = rule->next) { + char buf[BUFSIZ]; + + snprintf(buf, sizeof(buf), "%s %s", + rule->cmd->str, rule->rule_str); + json_array_string_add(json_sets, buf); + } + + /* Call clause */ + if (index->nextrm) + json_object_string_add(json_rule, "callClause", + index->nextrm); + + /* Exit Policy */ + if (index->exitpolicy == RMAP_GOTO) + snprintf(action, sizeof(action), "Goto %d", + index->nextpref); + else if (index->exitpolicy == RMAP_NEXT) + snprintf(action, sizeof(action), + "Continue to next entry"); + else if (index->exitpolicy == RMAP_EXIT) + snprintf(action, sizeof(action), + "Exit routemap"); + if (action[0] != '\0') + json_object_string_add(json_rule, "action", + action); + } else { + vty_out(vty, " %s, sequence %d Invoked %" PRIu64 "\n", + route_map_type_str(index->type), index->pref, + index->applied - index->applied_clear); + + /* Description */ + if (index->description) + vty_out(vty, " Description:\n %s\n", + index->description); + + /* Match clauses */ + vty_out(vty, " Match clauses:\n"); + for (rule = index->match_list.head; rule; + rule = rule->next) + vty_out(vty, " %s %s\n", rule->cmd->str, + rule->rule_str); + + /* Set clauses */ + vty_out(vty, " Set clauses:\n"); + for (rule = index->set_list.head; rule; + rule = rule->next) + vty_out(vty, " %s %s\n", rule->cmd->str, + rule->rule_str); + + /* Call clause */ + vty_out(vty, " Call clause:\n"); + if (index->nextrm) + vty_out(vty, " Call %s\n", index->nextrm); + + /* Exit Policy */ + vty_out(vty, " Action:\n"); + if (index->exitpolicy == RMAP_GOTO) + vty_out(vty, " Goto %d\n", index->nextpref); + else if (index->exitpolicy == RMAP_NEXT) + vty_out(vty, " Continue to next entry\n"); + else if (index->exitpolicy == RMAP_EXIT) + vty_out(vty, " Exit routemap\n"); + } } } @@ -895,22 +987,28 @@ static int sort_route_map(const void **map1, const void **map2) return strcmp(m1->name, m2->name); } -static int vty_show_route_map(struct vty *vty, const char *name) +static int vty_show_route_map(struct vty *vty, const char *name, bool use_json) { struct route_map *map; + json_object *json = NULL; + json_object *json_proto = NULL; - vty_out(vty, "%s:\n", frr_protonameinst); + if (use_json) { + json = json_object_new_object(); + json_proto = json_object_new_object(); + json_object_object_add(json, frr_protonameinst, json_proto); + } else + vty_out(vty, "%s:\n", frr_protonameinst); if (name) { map = route_map_lookup_by_name(name); if (map) { - vty_show_route_map_entry(vty, map); + vty_show_route_map_entry(vty, map, json_proto); return CMD_SUCCESS; - } else { + } else if (!use_json) { vty_out(vty, "%s: 'route-map %s' not found\n", frr_protonameinst, name); - return CMD_SUCCESS; } } else { @@ -923,10 +1021,18 @@ static int vty_show_route_map(struct vty *vty, const char *name) list_sort(maplist, sort_route_map); for (ALL_LIST_ELEMENTS_RO(maplist, ln, map)) - vty_show_route_map_entry(vty, map); + vty_show_route_map_entry(vty, map, json_proto); list_delete(&maplist); } + + if (use_json) { + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + json, JSON_C_TO_STRING_PRETTY)); + json_object_free(json); + } + return CMD_SUCCESS; } @@ -950,7 +1056,7 @@ static int vty_show_unused_route_map(struct vty *vty) list_sort(maplist, sort_route_map); for (ALL_LIST_ELEMENTS_RO(maplist, ln, map)) - vty_show_route_map_entry(vty, map); + vty_show_route_map_entry(vty, map, NULL); } else { vty_out(vty, "\n%s: None\n", frr_protonameinst); } @@ -1325,7 +1431,7 @@ enum rmap_compile_rets route_map_add_match(struct route_map_index *index, * the same as the existing configuration then, * ignore the duplicate configuration. */ - if (strcmp(match_arg, rule->rule_str) == 0) { + if (rulecmp(match_arg, rule->rule_str) == 0) { if (cmd->func_free) (*cmd->func_free)(compile); @@ -2957,14 +3063,20 @@ DEFUN (rmap_clear_counters, DEFUN (rmap_show_name, rmap_show_name_cmd, - "show route-map [WORD]", + "show route-map [WORD] [json]", SHOW_STR "route-map information\n" - "route-map name\n") + "route-map name\n" + JSON_STR) { - int idx_word = 2; - const char *name = (argc == 3) ? argv[idx_word]->arg : NULL; - return vty_show_route_map(vty, name); + bool uj = use_json(argc, argv); + int idx = 0; + const char *name = NULL; + + if (argv_find(argv, argc, "WORD", &idx)) + name = argv[idx]->arg; + + return vty_show_route_map(vty, name, uj); } DEFUN (rmap_show_unused, diff --git a/lib/routemap.h b/lib/routemap.h index 4a40ec08b9..b356dbf52e 100644 --- a/lib/routemap.h +++ b/lib/routemap.h @@ -270,6 +270,7 @@ DECLARE_QOBJ_TYPE(route_map); /* BGP route-map match conditions */ #define IS_MATCH_LOCAL_PREF(C) \ (strmatch(C, "frr-bgp-route-map:match-local-preference")) +#define IS_MATCH_ALIAS(C) (strmatch(C, "frr-bgp-route-map:match-alias")) #define IS_MATCH_ORIGIN(C) \ (strmatch(C, "frr-bgp-route-map:match-origin")) #define IS_MATCH_RPKI(C) (strmatch(C, "frr-bgp-route-map:rpki")) @@ -349,10 +350,14 @@ DECLARE_QOBJ_TYPE(route_map); (strmatch(A, "frr-bgp-route-map:set-large-community")) #define IS_SET_COMMUNITY(A) \ (strmatch(A, "frr-bgp-route-map:set-community")) +#define IS_SET_EXTCOMMUNITY_NONE(A) \ + (strmatch(A, "frr-bgp-route-map:set-extcommunity-none")) #define IS_SET_EXTCOMMUNITY_RT(A) \ (strmatch(A, "frr-bgp-route-map:set-extcommunity-rt")) #define IS_SET_EXTCOMMUNITY_SOO(A) \ (strmatch(A, "frr-bgp-route-map:set-extcommunity-soo")) +#define IS_SET_EXTCOMMUNITY_LB(A) \ + (strmatch(A, "frr-bgp-route-map:set-extcommunity-lb")) #define IS_SET_AGGREGATOR(A) \ (strmatch(A, "frr-bgp-route-map:aggregator")) #define IS_SET_AS_PREPEND(A) \ @@ -376,6 +381,12 @@ DECLARE_QOBJ_TYPE(route_map); #define IS_SET_BGP_EVPN_GATEWAY_IP_IPV6(A) \ (strmatch(A, "frr-bgp-route-map:set-evpn-gateway-ip-ipv6")) +enum ecommunity_lb_type { + EXPLICIT_BANDWIDTH, + CUMULATIVE_BANDWIDTH, + COMPUTED_BANDWIDTH +}; + /* Prototypes. */ extern void route_map_init(void); diff --git a/lib/routemap_cli.c b/lib/routemap_cli.c index bf982cfa2b..cadad15fa7 100644 --- a/lib/routemap_cli.c +++ b/lib/routemap_cli.c @@ -124,6 +124,7 @@ void route_map_instance_show(struct vty *vty, struct lyd_node *dnode, void route_map_instance_show_end(struct vty *vty, struct lyd_node *dnode) { + vty_out(vty, "exit\n"); vty_out(vty, "!\n"); } @@ -634,6 +635,11 @@ void route_map_condition_show(struct vty *vty, struct lyd_node *dnode, yang_dnode_get_string( dnode, "./rmap-match-condition/frr-bgp-route-map:local-preference")); + } else if (IS_MATCH_ALIAS(condition)) { + vty_out(vty, " match alias %s\n", + yang_dnode_get_string( + dnode, + "./rmap-match-condition/frr-bgp-route-map:alias")); } else if (IS_MATCH_ORIGIN(condition)) { vty_out(vty, " match origin %s\n", yang_dnode_get_string( @@ -1189,6 +1195,39 @@ void route_map_action_show(struct vty *vty, struct lyd_node *dnode, yang_dnode_get_string( dnode, "./rmap-set-action/frr-bgp-route-map:extcommunity-soo")); + } else if (IS_SET_EXTCOMMUNITY_LB(action)) { + enum ecommunity_lb_type lb_type; + char str[VTY_BUFSIZ]; + uint16_t bandwidth; + + lb_type = yang_dnode_get_enum( + dnode, + "./rmap-set-action/frr-bgp-route-map:extcommunity-lb/lb-type"); + switch (lb_type) { + case EXPLICIT_BANDWIDTH: + bandwidth = yang_dnode_get_uint16( + dnode, + "./rmap-set-action/frr-bgp-route-map:extcommunity-lb/bandwidth"); + snprintf(str, sizeof(str), "%d", bandwidth); + break; + case CUMULATIVE_BANDWIDTH: + snprintf(str, sizeof(str), "%s", "cumulative"); + break; + case COMPUTED_BANDWIDTH: + snprintf(str, sizeof(str), "%s", "num-multipaths"); + } + + if (yang_dnode_get_bool( + dnode, + "./rmap-set-action/frr-bgp-route-map:extcommunity-lb/two-octet-as-specific")) + strlcat(str, " non-transitive", sizeof(str)); + + vty_out(vty, " set extcommunity bandwidth %s\n", str); + } else if (IS_SET_EXTCOMMUNITY_NONE(action)) { + if (yang_dnode_get_bool( + dnode, + "./rmap-set-action/frr-bgp-route-map:extcommunity-none")) + vty_out(vty, " set extcommunity none\n"); } else if (IS_SET_AGGREGATOR(action)) { vty_out(vty, " set aggregator as %s %s\n", yang_dnode_get_string( diff --git a/lib/subdir.am b/lib/subdir.am index 90301d800a..dab5fb9e83 100644 --- a/lib/subdir.am +++ b/lib/subdir.am @@ -2,7 +2,7 @@ # libfrr # lib_LTLIBRARIES += lib/libfrr.la -lib_libfrr_la_LDFLAGS = -version-info 0:0:0 -Xlinker -e_libfrr_version +lib_libfrr_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0 -Xlinker -e_libfrr_version lib_libfrr_la_LIBADD = $(LIBCAP) $(UNWIND_LIBS) $(LIBYANG_LIBS) $(LUA_LIB) $(UST_LIBS) $(LIBM) lib_libfrr_la_SOURCES = \ @@ -144,7 +144,6 @@ vtysh_scan += \ lib/log_vty.c \ lib/nexthop_group.c \ lib/plist.c \ - lib/resolver.c \ lib/routemap.c \ lib/routemap_cli.c \ lib/spf_backoff.c \ @@ -322,7 +321,7 @@ lib_LTLIBRARIES += lib/libfrrsnmp.la endif lib_libfrrsnmp_la_CFLAGS = $(AM_CFLAGS) $(SNMP_CFLAGS) -std=gnu11 -lib_libfrrsnmp_la_LDFLAGS = -version-info 0:0:0 +lib_libfrrsnmp_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0 lib_libfrrsnmp_la_LIBADD = $(SNMP_LIBS) lib_libfrrsnmp_la_SOURCES = \ lib/agentx.c \ @@ -335,10 +334,11 @@ lib_libfrrsnmp_la_SOURCES = \ if CARES lib_LTLIBRARIES += lib/libfrrcares.la pkginclude_HEADERS += lib/resolver.h +vtysh_scan += lib/resolver.c endif lib_libfrrcares_la_CFLAGS = $(AM_CFLAGS) $(CARES_CFLAGS) -lib_libfrrcares_la_LDFLAGS = -version-info 0:0:0 +lib_libfrrcares_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0 lib_libfrrcares_la_LIBADD = $(CARES_LIBS) lib_libfrrcares_la_SOURCES = \ lib/resolver.c \ @@ -353,7 +353,7 @@ pkginclude_HEADERS += lib/frr_zmq.h endif lib_libfrrzmq_la_CFLAGS = $(AM_CFLAGS) $(ZEROMQ_CFLAGS) -lib_libfrrzmq_la_LDFLAGS = -version-info 0:0:0 +lib_libfrrzmq_la_LDFLAGS = $(LIB_LDFLAGS) -version-info 0:0:0 lib_libfrrzmq_la_LIBADD = $(ZEROMQ_LIBS) lib_libfrrzmq_la_SOURCES = \ lib/frr_zmq.c \ @@ -367,7 +367,7 @@ module_LTLIBRARIES += lib/confd.la endif lib_confd_la_CFLAGS = $(AM_CFLAGS) $(CONFD_CFLAGS) -lib_confd_la_LDFLAGS = -avoid-version -module -shared -export-dynamic +lib_confd_la_LDFLAGS = $(MODULE_LDFLAGS) lib_confd_la_LIBADD = lib/libfrr.la $(CONFD_LIBS) lib_confd_la_SOURCES = lib/northbound_confd.c @@ -379,7 +379,7 @@ module_LTLIBRARIES += lib/sysrepo.la endif lib_sysrepo_la_CFLAGS = $(AM_CFLAGS) $(SYSREPO_CFLAGS) -lib_sysrepo_la_LDFLAGS = -avoid-version -module -shared -export-dynamic +lib_sysrepo_la_LDFLAGS = $(MODULE_LDFLAGS) lib_sysrepo_la_LIBADD = lib/libfrr.la $(SYSREPO_LIBS) lib_sysrepo_la_SOURCES = lib/northbound_sysrepo.c @@ -391,7 +391,7 @@ module_LTLIBRARIES += lib/grpc.la endif lib_grpc_la_CXXFLAGS = $(WERROR) $(GRPC_CFLAGS) -lib_grpc_la_LDFLAGS = -avoid-version -module -shared -export-dynamic +lib_grpc_la_LDFLAGS = $(MODULE_LDFLAGS) lib_grpc_la_LIBADD = lib/libfrr.la grpc/libfrrgrpc_pb.la $(GRPC_LIBS) lib_grpc_la_SOURCES = lib/northbound_grpc.cpp @@ -419,7 +419,8 @@ lib_grammar_sandbox_LDADD = \ lib_clippy_CPPFLAGS = $(CPPFLAGS_BASE) -D_GNU_SOURCE -DBUILDING_CLIPPY lib_clippy_CFLAGS = $(AC_CFLAGS) $(PYTHON_CFLAGS) lib_clippy_LDADD = $(PYTHON_LIBS) $(UST_LIBS) -lelf -lib_clippy_LDFLAGS = -export-dynamic +# no $(SAN_FLAGS) here +lib_clippy_LDFLAGS = -export-dynamic $(AC_LDFLAGS) $(AC_LDFLAGS_EXEC) lib_clippy_SOURCES = \ lib/jhash.c \ lib/clippy.c \ @@ -58,7 +58,6 @@ struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name); static int vrf_backend; static int vrf_backend_configured; -static struct zebra_privs_t *vrf_daemon_privs; static char vrf_default_name[VRF_NAMSIZ] = VRF_DEFAULT_NAME_INTERNAL; /* @@ -287,6 +286,7 @@ void vrf_delete(struct vrf *vrf) RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); vrf->vrf_id = VRF_UNKNOWN; } + vrf->ns_ctxt = NULL; return; } @@ -855,62 +855,6 @@ static struct cmd_node vrf_node = { .prompt = "%s(config-vrf)# ", }; -DEFUN_NOSH (vrf_netns, - vrf_netns_cmd, - "netns NAME", - "Attach VRF to a Namespace\n" - "The file name in " NS_RUN_DIR ", or a full pathname\n") -{ - int idx_name = 1, ret; - char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg); - - VTY_DECLVAR_CONTEXT(vrf, vrf); - - if (!pathname) - return CMD_WARNING_CONFIG_FAILED; - - frr_with_privs(vrf_daemon_privs) { - ret = vrf_netns_handler_create(vty, vrf, pathname, - NS_UNKNOWN, - NS_UNKNOWN, - NS_UNKNOWN); - } - return ret; -} - -DEFUN_NOSH (no_vrf_netns, - no_vrf_netns_cmd, - "no netns [NAME]", - NO_STR - "Detach VRF from a Namespace\n" - "The file name in " NS_RUN_DIR ", or a full pathname\n") -{ - struct ns *ns = NULL; - - VTY_DECLVAR_CONTEXT(vrf, vrf); - - if (!vrf_is_backend_netns()) { - vty_out(vty, "VRF backend is not Netns. Aborting\n"); - return CMD_WARNING_CONFIG_FAILED; - } - if (!vrf->ns_ctxt) { - vty_out(vty, "VRF %s(%u) is not configured with NetNS\n", - vrf->name, vrf->vrf_id); - return CMD_WARNING_CONFIG_FAILED; - } - - ns = (struct ns *)vrf->ns_ctxt; - - ns->vrf_ctxt = NULL; - vrf_disable(vrf); - /* vrf ID from VRF is necessary for Zebra - * so that propagate to other clients is done - */ - ns_delete(ns); - vrf->ns_ctxt = NULL; - return CMD_SUCCESS; -} - /* * Debug CLI for vrf's */ @@ -963,8 +907,7 @@ void vrf_install_commands(void) install_element(ENABLE_NODE, &no_vrf_debug_cmd); } -void vrf_cmd_init(int (*writefunc)(struct vty *vty), - struct zebra_privs_t *daemon_privs) +void vrf_cmd_init(int (*writefunc)(struct vty *vty)) { install_element(CONFIG_NODE, &vrf_cmd); install_element(CONFIG_NODE, &no_vrf_cmd); @@ -972,12 +915,6 @@ void vrf_cmd_init(int (*writefunc)(struct vty *vty), install_node(&vrf_node); install_default(VRF_NODE); install_element(VRF_NODE, &vrf_exit_cmd); - if (vrf_is_backend_netns() && ns_have_netns()) { - /* Install NS commands. */ - vrf_daemon_privs = daemon_privs; - install_element(VRF_NODE, &vrf_netns_cmd); - install_element(VRF_NODE, &no_vrf_netns_cmd); - } } void vrf_set_default_name(const char *default_name, bool force) @@ -1131,13 +1068,6 @@ int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id, return ret; } -vrf_id_t vrf_generate_id(void) -{ - static int vrf_id_local; - - return ++vrf_id_local; -} - /* ------- Northbound callbacks ------- */ /* @@ -285,8 +285,7 @@ extern int vrf_switchback_to_initial(void); /* VRF vty command initialisation */ -extern void vrf_cmd_init(int (*writefunc)(struct vty *vty), - struct zebra_privs_t *daemon_priv); +extern void vrf_cmd_init(int (*writefunc)(struct vty *vty)); /* VRF vty debugging */ @@ -324,7 +323,6 @@ extern int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf, extern void vrf_disable(struct vrf *vrf); extern int vrf_enable(struct vrf *vrf); extern void vrf_delete(struct vrf *vrf); -extern vrf_id_t vrf_generate_id(void); extern const struct frr_yang_module_info frr_vrf_info; @@ -3040,7 +3040,7 @@ DEFPY (log_commands, /* Display current configuration. */ static int vty_config_write(struct vty *vty) { - vty_out(vty, "line vty\n"); + vty_frame(vty, "line vty\n"); if (vty_accesslist_name) vty_out(vty, " access-class %s\n", vty_accesslist_name); @@ -3058,6 +3058,8 @@ static int vty_config_write(struct vty *vty) if (no_password_check) vty_out(vty, " no login\n"); + vty_endframe(vty, "exit\n"); + if (do_log_commands) vty_out(vty, "log commands\n"); diff --git a/lib/zclient.c b/lib/zclient.c index 0815e77d4e..a1e7194890 100644 --- a/lib/zclient.c +++ b/lib/zclient.c @@ -1299,7 +1299,13 @@ int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api) stream_putl(s, api->tableid); if (CHECK_FLAG(api->message, ZAPI_MESSAGE_OPAQUE)) { - assert(api->opaque.length <= ZAPI_MESSAGE_OPAQUE_LENGTH); + if (api->opaque.length > ZAPI_MESSAGE_OPAQUE_LENGTH) { + flog_err( + EC_LIB_ZAPI_ENCODE, + "%s: opaque length %u is greater than allowed value", + __func__, api->opaque.length); + return -1; + } stream_putw(s, api->opaque.length); stream_write(s, api->opaque.data, api->opaque.length); @@ -1537,7 +1543,13 @@ int zapi_route_decode(struct stream *s, struct zapi_route *api) if (CHECK_FLAG(api->message, ZAPI_MESSAGE_OPAQUE)) { STREAM_GETW(s, api->opaque.length); - assert(api->opaque.length <= ZAPI_MESSAGE_OPAQUE_LENGTH); + if (api->opaque.length > ZAPI_MESSAGE_OPAQUE_LENGTH) { + flog_err( + EC_LIB_ZAPI_ENCODE, + "%s: opaque length %u is greater than allowed value", + __func__, api->opaque.length); + return -1; + } STREAM_GET(api->opaque.data, s, api->opaque.length); } @@ -3430,6 +3442,14 @@ int zapi_labels_decode(struct stream *s, struct zapi_labels *zl) if (zapi_nexthop_decode(s, znh, 0, 0) < 0) return -1; + + if (znh->type == NEXTHOP_TYPE_BLACKHOLE) { + flog_warn( + EC_LIB_ZAPI_ENCODE, + "%s: Prefix %pFX has a blackhole nexthop which we cannot use for a label", + __func__, &zl->route.prefix); + return -1; + } } if (CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS)) { @@ -3451,6 +3471,14 @@ int zapi_labels_decode(struct stream *s, struct zapi_labels *zl) if (zapi_nexthop_decode(s, znh, 0, 0) < 0) return -1; + + if (znh->type == NEXTHOP_TYPE_BLACKHOLE) { + flog_warn( + EC_LIB_ZAPI_ENCODE, + "%s: Prefix %pFX has a backup blackhole nexthop which we cannot use for a label", + __func__, &zl->route.prefix); + return -1; + } } } @@ -4459,11 +4487,9 @@ static int zclient_neigh_ip_read_entry(struct stream *s, struct ipaddr *add) return -1; } -int zclient_neigh_ip_encode(struct stream *s, - uint16_t cmd, - union sockunion *in, - union sockunion *out, - struct interface *ifp) +int zclient_neigh_ip_encode(struct stream *s, uint16_t cmd, union sockunion *in, + union sockunion *out, struct interface *ifp, + int ndm_state) { int ret = 0; @@ -4478,7 +4504,7 @@ int zclient_neigh_ip_encode(struct stream *s, stream_putc(s, AF_UNSPEC); stream_putl(s, ifp->ifindex); if (out) - stream_putl(s, ZEBRA_NEIGH_STATE_REACHABLE); + stream_putl(s, ndm_state); else stream_putl(s, ZEBRA_NEIGH_STATE_FAILED); return ret; diff --git a/lib/zclient.h b/lib/zclient.h index a25c5800b7..71187ccae7 100644 --- a/lib/zclient.h +++ b/lib/zclient.h @@ -856,9 +856,18 @@ extern struct zclient_options zclient_options_default; * ip_in is the underlay IP, ip_out is the tunnel dest * index stands for the index of the interface * ndm state stands for the NDM value in netlink + * (see linux/neighbour.h) */ +#define ZEBRA_NEIGH_STATE_INCOMPLETE (0x01) #define ZEBRA_NEIGH_STATE_REACHABLE (0x02) -#define ZEBRA_NEIGH_STATE_FAILED (0x20) +#define ZEBRA_NEIGH_STATE_STALE (0x04) +#define ZEBRA_NEIGH_STATE_DELAY (0x08) +#define ZEBRA_NEIGH_STATE_PROBE (0x10) +#define ZEBRA_NEIGH_STATE_FAILED (0x20) +#define ZEBRA_NEIGH_STATE_NOARP (0x40) +#define ZEBRA_NEIGH_STATE_PERMANENT (0x80) +#define ZEBRA_NEIGH_STATE_NONE (0x00) + struct zapi_neigh_ip { int cmd; struct ipaddr ip_in; @@ -867,11 +876,9 @@ struct zapi_neigh_ip { uint32_t ndm_state; }; int zclient_neigh_ip_decode(struct stream *s, struct zapi_neigh_ip *api); -int zclient_neigh_ip_encode(struct stream *s, - uint16_t cmd, - union sockunion *in, - union sockunion *out, - struct interface *ifp); +int zclient_neigh_ip_encode(struct stream *s, uint16_t cmd, union sockunion *in, + union sockunion *out, struct interface *ifp, + int ndm_state); /* * We reserve the top 4 bits for l2-NHG, everything else |
