2 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "lib_errors.h"
35 DEFINE_MTYPE_STATIC(LIB
, ROUTE_MAP
, "Route map")
36 DEFINE_MTYPE(LIB
, ROUTE_MAP_NAME
, "Route map name")
37 DEFINE_MTYPE_STATIC(LIB
, ROUTE_MAP_INDEX
, "Route map index")
38 DEFINE_MTYPE(LIB
, ROUTE_MAP_RULE
, "Route map rule")
39 DEFINE_MTYPE_STATIC(LIB
, ROUTE_MAP_RULE_STR
, "Route map rule str")
40 DEFINE_MTYPE(LIB
, ROUTE_MAP_COMPILED
, "Route map compiled")
41 DEFINE_MTYPE_STATIC(LIB
, ROUTE_MAP_DEP
, "Route map dependency")
42 DEFINE_MTYPE_STATIC(LIB
, ROUTE_MAP_DEP_DATA
, "Route map dependency data")
44 DEFINE_QOBJ_TYPE(route_map_index
)
45 DEFINE_QOBJ_TYPE(route_map
)
47 /* Vector for route match rules. */
48 static vector route_match_vec
;
50 /* Vector for route set rules. */
51 static vector route_set_vec
;
53 struct route_map_match_set_hooks rmap_match_set_hook
;
56 void route_map_match_interface_hook(int (*func
)(
57 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
58 const char *arg
, route_map_event_t type
))
60 rmap_match_set_hook
.match_interface
= func
;
63 /* no match interface */
64 void route_map_no_match_interface_hook(int (*func
)(
65 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
66 const char *arg
, route_map_event_t type
))
68 rmap_match_set_hook
.no_match_interface
= func
;
71 /* match ip address */
72 void route_map_match_ip_address_hook(int (*func
)(
73 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
74 const char *arg
, route_map_event_t type
))
76 rmap_match_set_hook
.match_ip_address
= func
;
79 /* no match ip address */
80 void route_map_no_match_ip_address_hook(int (*func
)(
81 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
82 const char *arg
, route_map_event_t type
))
84 rmap_match_set_hook
.no_match_ip_address
= func
;
87 /* match ip address prefix list */
88 void route_map_match_ip_address_prefix_list_hook(int (*func
)(
89 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
90 const char *arg
, route_map_event_t type
))
92 rmap_match_set_hook
.match_ip_address_prefix_list
= func
;
95 /* no match ip address prefix list */
96 void route_map_no_match_ip_address_prefix_list_hook(int (*func
)(
97 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
98 const char *arg
, route_map_event_t type
))
100 rmap_match_set_hook
.no_match_ip_address_prefix_list
= func
;
103 /* match ip next hop */
104 void route_map_match_ip_next_hop_hook(int (*func
)(
105 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
106 const char *arg
, route_map_event_t type
))
108 rmap_match_set_hook
.match_ip_next_hop
= func
;
111 /* no match ip next hop */
112 void route_map_no_match_ip_next_hop_hook(int (*func
)(
113 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
114 const char *arg
, route_map_event_t type
))
116 rmap_match_set_hook
.no_match_ip_next_hop
= func
;
119 /* match ip next hop prefix list */
120 void route_map_match_ip_next_hop_prefix_list_hook(int (*func
)(
121 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
122 const char *arg
, route_map_event_t type
))
124 rmap_match_set_hook
.match_ip_next_hop_prefix_list
= func
;
127 /* no match ip next hop prefix list */
128 void route_map_no_match_ip_next_hop_prefix_list_hook(int (*func
)(
129 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
130 const char *arg
, route_map_event_t type
))
132 rmap_match_set_hook
.no_match_ip_next_hop_prefix_list
= func
;
135 /* match ip next-hop type */
136 void route_map_match_ip_next_hop_type_hook(int (*func
)(
137 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
138 const char *arg
, route_map_event_t type
))
140 rmap_match_set_hook
.match_ip_next_hop_type
= func
;
143 /* no match ip next-hop type */
144 void route_map_no_match_ip_next_hop_type_hook(int (*func
)(
145 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
146 const char *arg
, route_map_event_t type
))
148 rmap_match_set_hook
.no_match_ip_next_hop_type
= func
;
151 /* match ipv6 address */
152 void route_map_match_ipv6_address_hook(int (*func
)(
153 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
154 const char *arg
, route_map_event_t type
))
156 rmap_match_set_hook
.match_ipv6_address
= func
;
159 /* no match ipv6 address */
160 void route_map_no_match_ipv6_address_hook(int (*func
)(
161 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
162 const char *arg
, route_map_event_t type
))
164 rmap_match_set_hook
.no_match_ipv6_address
= func
;
168 /* match ipv6 address prefix list */
169 void route_map_match_ipv6_address_prefix_list_hook(int (*func
)(
170 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
171 const char *arg
, route_map_event_t type
))
173 rmap_match_set_hook
.match_ipv6_address_prefix_list
= func
;
176 /* no match ipv6 address prefix list */
177 void route_map_no_match_ipv6_address_prefix_list_hook(int (*func
)(
178 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
179 const char *arg
, route_map_event_t type
))
181 rmap_match_set_hook
.no_match_ipv6_address_prefix_list
= func
;
184 /* match ipv6 next-hop type */
185 void route_map_match_ipv6_next_hop_type_hook(int (*func
)(
186 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
187 const char *arg
, route_map_event_t type
))
189 rmap_match_set_hook
.match_ipv6_next_hop_type
= func
;
192 /* no match ipv6 next-hop type */
193 void route_map_no_match_ipv6_next_hop_type_hook(int (*func
)(
194 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
195 const char *arg
, route_map_event_t type
))
197 rmap_match_set_hook
.no_match_ipv6_next_hop_type
= func
;
201 void route_map_match_metric_hook(int (*func
)(
202 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
203 const char *arg
, route_map_event_t type
))
205 rmap_match_set_hook
.match_metric
= func
;
208 /* no match metric */
209 void route_map_no_match_metric_hook(int (*func
)(
210 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
211 const char *arg
, route_map_event_t type
))
213 rmap_match_set_hook
.no_match_metric
= func
;
217 void route_map_match_tag_hook(int (*func
)(struct vty
*vty
,
218 struct route_map_index
*index
,
219 const char *command
, const char *arg
,
220 route_map_event_t type
))
222 rmap_match_set_hook
.match_tag
= func
;
226 void route_map_no_match_tag_hook(int (*func
)(
227 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
228 const char *arg
, route_map_event_t type
))
230 rmap_match_set_hook
.no_match_tag
= func
;
234 void route_map_set_ip_nexthop_hook(int (*func
)(struct vty
*vty
,
235 struct route_map_index
*index
,
239 rmap_match_set_hook
.set_ip_nexthop
= func
;
242 /* no set ip nexthop */
243 void route_map_no_set_ip_nexthop_hook(int (*func
)(struct vty
*vty
,
244 struct route_map_index
*index
,
248 rmap_match_set_hook
.no_set_ip_nexthop
= func
;
251 /* set ipv6 nexthop local */
252 void route_map_set_ipv6_nexthop_local_hook(
253 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
254 const char *command
, const char *arg
))
256 rmap_match_set_hook
.set_ipv6_nexthop_local
= func
;
259 /* no set ipv6 nexthop local */
260 void route_map_no_set_ipv6_nexthop_local_hook(
261 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
262 const char *command
, const char *arg
))
264 rmap_match_set_hook
.no_set_ipv6_nexthop_local
= func
;
268 void route_map_set_metric_hook(int (*func
)(struct vty
*vty
,
269 struct route_map_index
*index
,
273 rmap_match_set_hook
.set_metric
= func
;
277 void route_map_no_set_metric_hook(int (*func
)(struct vty
*vty
,
278 struct route_map_index
*index
,
282 rmap_match_set_hook
.no_set_metric
= func
;
286 void route_map_set_tag_hook(int (*func
)(struct vty
*vty
,
287 struct route_map_index
*index
,
288 const char *command
, const char *arg
))
290 rmap_match_set_hook
.set_tag
= func
;
294 void route_map_no_set_tag_hook(int (*func
)(struct vty
*vty
,
295 struct route_map_index
*index
,
299 rmap_match_set_hook
.no_set_tag
= func
;
302 int generic_match_add(struct vty
*vty
, struct route_map_index
*index
,
303 const char *command
, const char *arg
,
304 route_map_event_t type
)
306 enum rmap_compile_rets ret
;
308 ret
= route_map_add_match(index
, command
, arg
, type
);
310 case RMAP_RULE_MISSING
:
312 vty_out(vty
, "%% [%s] Can't find rule.\n",
315 zlog_warn("Can't find rule: %s", command
);
316 return CMD_WARNING_CONFIG_FAILED
;
317 case RMAP_COMPILE_ERROR
:
320 "%% [%s] Argument form is unsupported or malformed.\n",
323 zlog_warn("Argument form is unsupported or malformed: "
324 "%s %s", command
, arg
);
325 return CMD_WARNING_CONFIG_FAILED
;
326 case RMAP_COMPILE_SUCCESS
:
328 * Nothing to do here move along
336 int generic_match_delete(struct vty
*vty
, struct route_map_index
*index
,
337 const char *command
, const char *arg
,
338 route_map_event_t type
)
340 enum rmap_compile_rets ret
;
341 int retval
= CMD_SUCCESS
;
342 char *dep_name
= NULL
;
344 char *rmap_name
= NULL
;
346 if (type
!= RMAP_EVENT_MATCH_DELETED
) {
347 /* ignore the mundane, the types without any dependency */
349 if ((tmpstr
= route_map_get_match_arg(index
, command
))
352 XSTRDUP(MTYPE_ROUTE_MAP_RULE
, tmpstr
);
354 dep_name
= XSTRDUP(MTYPE_ROUTE_MAP_RULE
, arg
);
356 rmap_name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, index
->map
->name
);
359 ret
= route_map_delete_match(index
, command
, dep_name
, type
);
361 case RMAP_RULE_MISSING
:
363 vty_out(vty
, "%% [%s] Can't find rule.\n",
366 zlog_warn("Can't find rule: %s", command
);
367 retval
= CMD_WARNING_CONFIG_FAILED
;
369 case RMAP_COMPILE_ERROR
:
372 "%% [%s] Argument form is unsupported or malformed.\n",
375 zlog_warn("Argument form is unsupported or malformed: "
376 "%s %s", command
, arg
);
377 retval
= CMD_WARNING_CONFIG_FAILED
;
379 case RMAP_COMPILE_SUCCESS
:
386 XFREE(MTYPE_ROUTE_MAP_RULE
, dep_name
);
387 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap_name
);
392 int generic_set_add(struct vty
*vty
, struct route_map_index
*index
,
393 const char *command
, const char *arg
)
395 enum rmap_compile_rets ret
;
397 ret
= route_map_add_set(index
, command
, arg
);
399 case RMAP_RULE_MISSING
:
401 vty_out(vty
, "%% [%s] Can't find rule.\n", frr_protonameinst
);
403 zlog_warn("Can't find rule: %s", command
);
404 return CMD_WARNING_CONFIG_FAILED
;
405 case RMAP_COMPILE_ERROR
:
408 "%% [%s] Argument form is unsupported or malformed.\n",
411 zlog_warn("Argument form is unsupported or malformed: "
412 "%s %s", command
, arg
);
413 return CMD_WARNING_CONFIG_FAILED
;
414 case RMAP_COMPILE_SUCCESS
:
421 int generic_set_delete(struct vty
*vty
, struct route_map_index
*index
,
422 const char *command
, const char *arg
)
424 enum rmap_compile_rets ret
;
426 ret
= route_map_delete_set(index
, command
, arg
);
428 case RMAP_RULE_MISSING
:
430 vty_out(vty
, "%% [%s] Can't find rule.\n", frr_protonameinst
);
432 zlog_warn("Can't find rule: %s", command
);
433 return CMD_WARNING_CONFIG_FAILED
;
434 case RMAP_COMPILE_ERROR
:
437 "%% [%s] Argument form is unsupported or malformed.\n",
440 zlog_warn("Argument form is unsupported or malformed: "
441 "%s %s", command
, arg
);
442 return CMD_WARNING_CONFIG_FAILED
;
443 case RMAP_COMPILE_SUCCESS
:
451 /* Master list of route map. */
452 struct route_map_list route_map_master
= {NULL
, NULL
, NULL
, NULL
, NULL
};
453 struct hash
*route_map_master_hash
= NULL
;
455 static unsigned int route_map_hash_key_make(const void *p
)
457 const struct route_map
*map
= p
;
458 return string_hash_make(map
->name
);
461 static bool route_map_hash_cmp(const void *p1
, const void *p2
)
463 const struct route_map
*map1
= p1
;
464 const struct route_map
*map2
= p2
;
466 if (map1
->deleted
== map2
->deleted
) {
467 if (map1
->name
&& map2
->name
) {
468 if (!strcmp(map1
->name
, map2
->name
)) {
471 } else if (!map1
->name
&& !map2
->name
) {
479 enum route_map_upd8_type
{
484 /* all possible route-map dependency types */
485 enum route_map_dep_type
{
486 ROUTE_MAP_DEP_RMAP
= 1,
488 ROUTE_MAP_DEP_ECLIST
,
489 ROUTE_MAP_DEP_LCLIST
,
491 ROUTE_MAP_DEP_ASPATH
,
492 ROUTE_MAP_DEP_FILTER
,
496 struct route_map_dep
{
498 struct hash
*dep_rmap_hash
;
499 struct hash
*this_hash
; /* ptr to the hash structure this is part of */
502 struct route_map_dep_data
{
506 /* Count of number of sequences of this
507 * route-map that depend on the same entity.
512 /* Hashes maintaining dependency between various sublists used by route maps */
513 static struct hash
*route_map_dep_hash
[ROUTE_MAP_DEP_MAX
];
515 static unsigned int route_map_dep_hash_make_key(const void *p
);
516 static void route_map_clear_all_references(char *rmap_name
);
517 static void route_map_rule_delete(struct route_map_rule_list
*,
518 struct route_map_rule
*);
519 static bool rmap_debug
;
521 /* New route map allocation. Please note route map's name must be
523 static struct route_map
*route_map_new(const char *name
)
525 struct route_map
*new;
527 new = XCALLOC(MTYPE_ROUTE_MAP
, sizeof(struct route_map
));
528 new->name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
529 QOBJ_REG(new, route_map
);
533 /* Add new name to route_map. */
534 static struct route_map
*route_map_add(const char *name
)
536 struct route_map
*map
;
537 struct route_map_list
*list
;
539 map
= route_map_new(name
);
540 list
= &route_map_master
;
542 /* Add map to the hash */
543 hash_get(route_map_master_hash
, map
, hash_alloc_intern
);
545 /* Add new entry to the head of the list to match how it is added in the
546 * hash table. This is to ensure that if the same route-map has been
547 * created more than once and then marked for deletion (which can happen
548 * if prior deletions haven't completed as BGP hasn't yet done the
549 * route-map processing), the order of the entities is the same in both
550 * the list and the hash table. Otherwise, since there is nothing to
551 * distinguish between the two entries, the wrong entry could get freed.
552 * TODO: This needs to be re-examined to handle it better - e.g., revive
553 * a deleted entry if the route-map is created again.
556 map
->next
= list
->head
;
558 list
->head
->prev
= map
;
564 if (route_map_master
.add_hook
) {
565 (*route_map_master
.add_hook
)(name
);
566 route_map_notify_dependencies(name
, RMAP_EVENT_CALL_ADDED
);
570 zlog_debug("Add route-map %s", name
);
574 /* this is supposed to be called post processing by
575 * the delete hook function. Don't invoke delete_hook
576 * again in this routine.
578 static void route_map_free_map(struct route_map
*map
)
580 struct route_map_list
*list
;
581 struct route_map_index
*index
;
586 while ((index
= map
->head
) != NULL
)
587 route_map_index_delete(index
, 0);
590 zlog_debug("Deleting route-map %s", map
->name
);
592 list
= &route_map_master
;
597 map
->next
->prev
= map
->prev
;
599 list
->tail
= map
->prev
;
602 map
->prev
->next
= map
->next
;
604 list
->head
= map
->next
;
606 hash_release(route_map_master_hash
, map
);
607 XFREE(MTYPE_ROUTE_MAP_NAME
, map
->name
);
608 XFREE(MTYPE_ROUTE_MAP
, map
);
611 /* Route map delete from list. */
612 void route_map_delete(struct route_map
*map
)
614 struct route_map_index
*index
;
617 while ((index
= map
->head
) != NULL
)
618 route_map_index_delete(index
, 0);
623 /* Clear all dependencies */
624 route_map_clear_all_references(name
);
626 /* Execute deletion hook. */
627 if (route_map_master
.delete_hook
) {
628 (*route_map_master
.delete_hook
)(name
);
629 route_map_notify_dependencies(name
, RMAP_EVENT_CALL_DELETED
);
632 if (!map
->to_be_processed
) {
633 route_map_free_map(map
);
637 /* Lookup route map by route map name string. */
638 struct route_map
*route_map_lookup_by_name(const char *name
)
640 struct route_map
*map
;
641 struct route_map tmp_map
;
646 // map.deleted is 0 via memset
647 memset(&tmp_map
, 0, sizeof(struct route_map
));
648 tmp_map
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
649 map
= hash_lookup(route_map_master_hash
, &tmp_map
);
650 XFREE(MTYPE_ROUTE_MAP_NAME
, tmp_map
.name
);
654 /* Simple helper to warn if route-map does not exist. */
655 struct route_map
*route_map_lookup_warn_noexist(struct vty
*vty
, const char *name
)
657 struct route_map
*route_map
= route_map_lookup_by_name(name
);
660 if (vty_shell_serv(vty
))
661 vty_out(vty
, "The route-map '%s' does not exist.\n", name
);
666 int route_map_mark_updated(const char *name
)
668 struct route_map
*map
;
670 struct route_map tmp_map
;
675 map
= route_map_lookup_by_name(name
);
677 /* If we did not find the routemap with deleted=false try again
681 memset(&tmp_map
, 0, sizeof(struct route_map
));
682 tmp_map
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
683 tmp_map
.deleted
= true;
684 map
= hash_lookup(route_map_master_hash
, &tmp_map
);
685 XFREE(MTYPE_ROUTE_MAP_NAME
, tmp_map
.name
);
689 map
->to_be_processed
= true;
696 static int route_map_clear_updated(struct route_map
*map
)
701 map
->to_be_processed
= false;
703 route_map_free_map(map
);
709 /* Lookup route map. If there isn't route map create one and return
711 struct route_map
*route_map_get(const char *name
)
713 struct route_map
*map
;
715 map
= route_map_lookup_by_name(name
);
717 map
= route_map_add(name
);
722 void route_map_walk_update_list(void (*route_map_update_fn
)(char *name
))
724 struct route_map
*node
;
725 struct route_map
*nnode
= NULL
;
727 for (node
= route_map_master
.head
; node
; node
= nnode
) {
728 if (node
->to_be_processed
) {
729 /* DD: Should we add any thread yield code here */
730 route_map_update_fn(node
->name
);
732 route_map_clear_updated(node
);
738 /* Return route map's type string. */
739 static const char *route_map_type_str(enum route_map_type type
)
756 static const char *route_map_cmd_result_str(enum route_map_cmd_result_t res
)
774 static const char *route_map_result_str(route_map_result_t res
)
779 case RMAP_PERMITMATCH
:
787 static void vty_show_route_map_entry(struct vty
*vty
, struct route_map
*map
)
789 struct route_map_index
*index
;
790 struct route_map_rule
*rule
;
792 vty_out(vty
, "route-map: %s Invoked: %" PRIu64
"\n",
793 map
->name
, map
->applied
- map
->applied_clear
);
795 for (index
= map
->head
; index
; index
= index
->next
) {
796 vty_out(vty
, " %s, sequence %d Invoked %" PRIu64
"\n",
797 route_map_type_str(index
->type
), index
->pref
,
798 index
->applied
- index
->applied_clear
);
801 if (index
->description
)
802 vty_out(vty
, " Description:\n %s\n",
806 vty_out(vty
, " Match clauses:\n");
807 for (rule
= index
->match_list
.head
; rule
; rule
= rule
->next
)
808 vty_out(vty
, " %s %s\n", rule
->cmd
->str
,
811 vty_out(vty
, " Set clauses:\n");
812 for (rule
= index
->set_list
.head
; rule
; rule
= rule
->next
)
813 vty_out(vty
, " %s %s\n", rule
->cmd
->str
,
817 vty_out(vty
, " Call clause:\n");
819 vty_out(vty
, " Call %s\n", index
->nextrm
);
822 vty_out(vty
, " Action:\n");
823 if (index
->exitpolicy
== RMAP_GOTO
)
824 vty_out(vty
, " Goto %d\n", index
->nextpref
);
825 else if (index
->exitpolicy
== RMAP_NEXT
)
826 vty_out(vty
, " Continue to next entry\n");
827 else if (index
->exitpolicy
== RMAP_EXIT
)
828 vty_out(vty
, " Exit routemap\n");
832 static int sort_route_map(const void **map1
, const void **map2
)
834 const struct route_map
*m1
= *map1
;
835 const struct route_map
*m2
= *map2
;
837 return strcmp(m1
->name
, m2
->name
);
840 static int vty_show_route_map(struct vty
*vty
, const char *name
)
842 struct route_map
*map
;
844 vty_out(vty
, "%s:\n", frr_protonameinst
);
847 map
= route_map_lookup_by_name(name
);
850 vty_show_route_map_entry(vty
, map
);
853 vty_out(vty
, "%s: 'route-map %s' not found\n",
854 frr_protonameinst
, name
);
859 struct list
*maplist
= list_new();
862 for (map
= route_map_master
.head
; map
; map
= map
->next
)
863 listnode_add(maplist
, map
);
865 list_sort(maplist
, sort_route_map
);
867 for (ALL_LIST_ELEMENTS_RO(maplist
, ln
, map
))
868 vty_show_route_map_entry(vty
, map
);
870 list_delete(&maplist
);
875 /* Unused route map details */
876 static int vty_show_unused_route_map(struct vty
*vty
)
878 struct list
*maplist
= list_new();
880 struct route_map
*map
;
882 for (map
= route_map_master
.head
; map
; map
= map
->next
) {
883 /* If use_count is zero, No protocol is using this routemap.
884 * so adding to the list.
887 listnode_add(maplist
, map
);
890 if (maplist
->count
> 0) {
891 vty_out(vty
, "\n%s:\n", frr_protonameinst
);
892 list_sort(maplist
, sort_route_map
);
894 for (ALL_LIST_ELEMENTS_RO(maplist
, ln
, map
))
895 vty_show_route_map_entry(vty
, map
);
897 vty_out(vty
, "\n%s: None\n", frr_protonameinst
);
900 list_delete(&maplist
);
904 /* New route map allocation. Please note route map's name must be
906 static struct route_map_index
*route_map_index_new(void)
908 struct route_map_index
*new;
910 new = XCALLOC(MTYPE_ROUTE_MAP_INDEX
, sizeof(struct route_map_index
));
911 new->exitpolicy
= RMAP_EXIT
; /* Default to Cisco-style */
912 QOBJ_REG(new, route_map_index
);
916 /* Free route map index. */
917 void route_map_index_delete(struct route_map_index
*index
, int notify
)
919 struct route_map_rule
*rule
;
924 zlog_debug("Deleting route-map %s sequence %d",
925 index
->map
->name
, index
->pref
);
927 /* Free route match. */
928 while ((rule
= index
->match_list
.head
) != NULL
)
929 route_map_rule_delete(&index
->match_list
, rule
);
931 /* Free route set. */
932 while ((rule
= index
->set_list
.head
) != NULL
)
933 route_map_rule_delete(&index
->set_list
, rule
);
935 /* Remove index from route map list. */
937 index
->next
->prev
= index
->prev
;
939 index
->map
->tail
= index
->prev
;
942 index
->prev
->next
= index
->next
;
944 index
->map
->head
= index
->next
;
946 /* Free 'char *nextrm' if not NULL */
947 XFREE(MTYPE_ROUTE_MAP_NAME
, index
->nextrm
);
949 /* Execute event hook. */
950 if (route_map_master
.event_hook
&& notify
) {
951 (*route_map_master
.event_hook
)(index
->map
->name
);
952 route_map_notify_dependencies(index
->map
->name
,
953 RMAP_EVENT_CALL_ADDED
);
955 XFREE(MTYPE_ROUTE_MAP_INDEX
, index
);
958 /* Lookup index from route map. */
959 static struct route_map_index
*route_map_index_lookup(struct route_map
*map
,
960 enum route_map_type type
,
963 struct route_map_index
*index
;
965 for (index
= map
->head
; index
; index
= index
->next
)
966 if ((index
->type
== type
|| type
== RMAP_ANY
)
967 && index
->pref
== pref
)
972 /* Add new index to route map. */
973 static struct route_map_index
*
974 route_map_index_add(struct route_map
*map
, enum route_map_type type
, int pref
)
976 struct route_map_index
*index
;
977 struct route_map_index
*point
;
979 /* Allocate new route map inex. */
980 index
= route_map_index_new();
985 /* Compare preference. */
986 for (point
= map
->head
; point
; point
= point
->next
)
987 if (point
->pref
>= pref
)
990 if (map
->head
== NULL
) {
991 map
->head
= map
->tail
= index
;
992 } else if (point
== NULL
) {
993 index
->prev
= map
->tail
;
994 map
->tail
->next
= index
;
996 } else if (point
== map
->head
) {
997 index
->next
= map
->head
;
998 map
->head
->prev
= index
;
1001 index
->next
= point
;
1002 index
->prev
= point
->prev
;
1004 point
->prev
->next
= index
;
1005 point
->prev
= index
;
1008 /* Execute event hook. */
1009 if (route_map_master
.event_hook
) {
1010 (*route_map_master
.event_hook
)(map
->name
);
1011 route_map_notify_dependencies(map
->name
, RMAP_EVENT_CALL_ADDED
);
1015 zlog_debug("Route-map %s add sequence %d, type: %s",
1016 map
->name
, pref
, route_map_type_str(type
));
1021 /* Get route map index. */
1022 struct route_map_index
*
1023 route_map_index_get(struct route_map
*map
, enum route_map_type type
, int pref
)
1025 struct route_map_index
*index
;
1027 index
= route_map_index_lookup(map
, RMAP_ANY
, pref
);
1028 if (index
&& index
->type
!= type
) {
1029 /* Delete index from route map. */
1030 route_map_index_delete(index
, 1);
1034 index
= route_map_index_add(map
, type
, pref
);
1038 /* New route map rule */
1039 static struct route_map_rule
*route_map_rule_new(void)
1041 struct route_map_rule
*new;
1043 new = XCALLOC(MTYPE_ROUTE_MAP_RULE
, sizeof(struct route_map_rule
));
1047 /* Install rule command to the match list. */
1048 void route_map_install_match(const struct route_map_rule_cmd
*cmd
)
1050 vector_set(route_match_vec
, (void *)cmd
);
1053 /* Install rule command to the set list. */
1054 void route_map_install_set(const struct route_map_rule_cmd
*cmd
)
1056 vector_set(route_set_vec
, (void *)cmd
);
1059 /* Lookup rule command from match list. */
1060 static const struct route_map_rule_cmd
*route_map_lookup_match(const char *name
)
1063 const struct route_map_rule_cmd
*rule
;
1065 for (i
= 0; i
< vector_active(route_match_vec
); i
++)
1066 if ((rule
= vector_slot(route_match_vec
, i
)) != NULL
)
1067 if (strcmp(rule
->str
, name
) == 0)
1072 /* Lookup rule command from set list. */
1073 static const struct route_map_rule_cmd
*route_map_lookup_set(const char *name
)
1076 const struct route_map_rule_cmd
*rule
;
1078 for (i
= 0; i
< vector_active(route_set_vec
); i
++)
1079 if ((rule
= vector_slot(route_set_vec
, i
)) != NULL
)
1080 if (strcmp(rule
->str
, name
) == 0)
1085 /* Add match and set rule to rule list. */
1086 static void route_map_rule_add(struct route_map_rule_list
*list
,
1087 struct route_map_rule
*rule
)
1090 rule
->prev
= list
->tail
;
1092 list
->tail
->next
= rule
;
1098 /* Delete rule from rule list. */
1099 static void route_map_rule_delete(struct route_map_rule_list
*list
,
1100 struct route_map_rule
*rule
)
1102 if (rule
->cmd
->func_free
)
1103 (*rule
->cmd
->func_free
)(rule
->value
);
1105 XFREE(MTYPE_ROUTE_MAP_RULE_STR
, rule
->rule_str
);
1108 rule
->next
->prev
= rule
->prev
;
1110 list
->tail
= rule
->prev
;
1112 rule
->prev
->next
= rule
->next
;
1114 list
->head
= rule
->next
;
1116 XFREE(MTYPE_ROUTE_MAP_RULE
, rule
);
1119 /* strcmp wrapper function which don't crush even argument is NULL. */
1120 static int rulecmp(const char *dst
, const char *src
)
1131 return strcmp(dst
, src
);
1136 /* Use this to return the already specified argument for this match. This is
1137 * useful to get the specified argument with a route map match rule when the
1138 * rule is being deleted and the argument is not provided.
1140 const char *route_map_get_match_arg(struct route_map_index
*index
,
1141 const char *match_name
)
1143 struct route_map_rule
*rule
;
1144 const struct route_map_rule_cmd
*cmd
;
1146 /* First lookup rule for add match statement. */
1147 cmd
= route_map_lookup_match(match_name
);
1151 for (rule
= index
->match_list
.head
; rule
; rule
= rule
->next
)
1152 if (rule
->cmd
== cmd
&& rule
->rule_str
!= NULL
)
1153 return (rule
->rule_str
);
1158 static route_map_event_t
get_route_map_delete_event(route_map_event_t type
)
1161 case RMAP_EVENT_CALL_ADDED
:
1162 return RMAP_EVENT_CALL_DELETED
;
1163 case RMAP_EVENT_PLIST_ADDED
:
1164 return RMAP_EVENT_PLIST_DELETED
;
1165 case RMAP_EVENT_CLIST_ADDED
:
1166 return RMAP_EVENT_CLIST_DELETED
;
1167 case RMAP_EVENT_ECLIST_ADDED
:
1168 return RMAP_EVENT_ECLIST_DELETED
;
1169 case RMAP_EVENT_LLIST_ADDED
:
1170 return RMAP_EVENT_LLIST_DELETED
;
1171 case RMAP_EVENT_ASLIST_ADDED
:
1172 return RMAP_EVENT_ASLIST_DELETED
;
1173 case RMAP_EVENT_FILTER_ADDED
:
1174 return RMAP_EVENT_FILTER_DELETED
;
1175 case RMAP_EVENT_SET_ADDED
:
1176 case RMAP_EVENT_SET_DELETED
:
1177 case RMAP_EVENT_SET_REPLACED
:
1178 case RMAP_EVENT_MATCH_ADDED
:
1179 case RMAP_EVENT_MATCH_DELETED
:
1180 case RMAP_EVENT_MATCH_REPLACED
:
1181 case RMAP_EVENT_INDEX_ADDED
:
1182 case RMAP_EVENT_INDEX_DELETED
:
1183 case RMAP_EVENT_CALL_DELETED
:
1184 case RMAP_EVENT_PLIST_DELETED
:
1185 case RMAP_EVENT_CLIST_DELETED
:
1186 case RMAP_EVENT_ECLIST_DELETED
:
1187 case RMAP_EVENT_LLIST_DELETED
:
1188 case RMAP_EVENT_ASLIST_DELETED
:
1189 case RMAP_EVENT_FILTER_DELETED
:
1190 /* This function returns the appropriate 'deleted' event type
1191 * for every 'added' event type passed to this function.
1192 * This is done only for named entities used in the
1193 * route-map match commands.
1194 * This function is not to be invoked for any of the other event
1202 * Return to make c happy but if we get here something has gone
1203 * terribly terribly wrong, so yes this return makes no sense.
1205 return RMAP_EVENT_CALL_ADDED
;
1208 /* Add match statement to route map. */
1209 enum rmap_compile_rets
route_map_add_match(struct route_map_index
*index
,
1210 const char *match_name
,
1211 const char *match_arg
,
1212 route_map_event_t type
)
1214 struct route_map_rule
*rule
;
1215 struct route_map_rule
*next
;
1216 const struct route_map_rule_cmd
*cmd
;
1218 int8_t delete_rmap_event_type
= 0;
1219 const char *rule_key
;
1221 /* First lookup rule for add match statement. */
1222 cmd
= route_map_lookup_match(match_name
);
1224 return RMAP_RULE_MISSING
;
1226 /* Next call compile function for this match statement. */
1227 if (cmd
->func_compile
) {
1228 compile
= (*cmd
->func_compile
)(match_arg
);
1229 if (compile
== NULL
)
1230 return RMAP_COMPILE_ERROR
;
1233 /* use the compiled results if applicable */
1234 if (compile
&& cmd
->func_get_rmap_rule_key
)
1235 rule_key
= (*cmd
->func_get_rmap_rule_key
)
1238 rule_key
= match_arg
;
1240 /* If argument is completely same ignore it. */
1241 for (rule
= index
->match_list
.head
; rule
; rule
= next
) {
1243 if (rule
->cmd
== cmd
) {
1244 /* If the configured route-map match rule is exactly
1245 * the same as the existing configuration then,
1246 * ignore the duplicate configuration.
1248 if (strcmp(match_arg
, rule
->rule_str
) == 0) {
1250 (*cmd
->func_free
)(compile
);
1252 return RMAP_COMPILE_SUCCESS
;
1255 /* Remove the dependency of the route-map on the rule
1256 * that is being replaced.
1258 if (type
>= RMAP_EVENT_CALL_ADDED
) {
1259 delete_rmap_event_type
=
1260 get_route_map_delete_event(type
);
1261 route_map_upd8_dependency(
1262 delete_rmap_event_type
,
1267 route_map_rule_delete(&index
->match_list
, rule
);
1271 /* Add new route map match rule. */
1272 rule
= route_map_rule_new();
1274 rule
->value
= compile
;
1276 rule
->rule_str
= XSTRDUP(MTYPE_ROUTE_MAP_RULE_STR
, match_arg
);
1278 rule
->rule_str
= NULL
;
1280 /* Add new route match rule to linked list. */
1281 route_map_rule_add(&index
->match_list
, rule
);
1283 /* Execute event hook. */
1284 if (route_map_master
.event_hook
) {
1285 (*route_map_master
.event_hook
)(index
->map
->name
);
1286 route_map_notify_dependencies(index
->map
->name
,
1287 RMAP_EVENT_CALL_ADDED
);
1289 if (type
!= RMAP_EVENT_MATCH_ADDED
)
1290 route_map_upd8_dependency(type
, rule_key
, index
->map
->name
);
1292 return RMAP_COMPILE_SUCCESS
;
1295 /* Delete specified route match rule. */
1296 enum rmap_compile_rets
route_map_delete_match(struct route_map_index
*index
,
1297 const char *match_name
,
1298 const char *match_arg
,
1299 route_map_event_t type
)
1301 struct route_map_rule
*rule
;
1302 const struct route_map_rule_cmd
*cmd
;
1303 const char *rule_key
;
1305 cmd
= route_map_lookup_match(match_name
);
1307 return RMAP_RULE_MISSING
;
1309 for (rule
= index
->match_list
.head
; rule
; rule
= rule
->next
)
1310 if (rule
->cmd
== cmd
&& (rulecmp(rule
->rule_str
, match_arg
) == 0
1311 || match_arg
== NULL
)) {
1312 /* Execute event hook. */
1313 if (route_map_master
.event_hook
) {
1314 (*route_map_master
.event_hook
)(index
->map
->name
);
1315 route_map_notify_dependencies(
1317 RMAP_EVENT_CALL_ADDED
);
1319 if (cmd
->func_get_rmap_rule_key
)
1320 rule_key
= (*cmd
->func_get_rmap_rule_key
)
1323 rule_key
= match_arg
;
1325 if (type
!= RMAP_EVENT_MATCH_DELETED
&& rule_key
)
1326 route_map_upd8_dependency(type
, rule_key
,
1329 route_map_rule_delete(&index
->match_list
, rule
);
1330 return RMAP_COMPILE_SUCCESS
;
1332 /* Can't find matched rule. */
1333 return RMAP_RULE_MISSING
;
1336 /* Add route-map set statement to the route map. */
1337 enum rmap_compile_rets
route_map_add_set(struct route_map_index
*index
,
1338 const char *set_name
,
1339 const char *set_arg
)
1341 struct route_map_rule
*rule
;
1342 struct route_map_rule
*next
;
1343 const struct route_map_rule_cmd
*cmd
;
1346 cmd
= route_map_lookup_set(set_name
);
1348 return RMAP_RULE_MISSING
;
1350 /* Next call compile function for this match statement. */
1351 if (cmd
->func_compile
) {
1352 compile
= (*cmd
->func_compile
)(set_arg
);
1353 if (compile
== NULL
)
1354 return RMAP_COMPILE_ERROR
;
1358 /* Add by WJL. if old set command of same kind exist, delete it first
1359 to ensure only one set command of same kind exist under a
1361 for (rule
= index
->set_list
.head
; rule
; rule
= next
) {
1363 if (rule
->cmd
== cmd
)
1364 route_map_rule_delete(&index
->set_list
, rule
);
1367 /* Add new route map match rule. */
1368 rule
= route_map_rule_new();
1370 rule
->value
= compile
;
1372 rule
->rule_str
= XSTRDUP(MTYPE_ROUTE_MAP_RULE_STR
, set_arg
);
1374 rule
->rule_str
= NULL
;
1376 /* Add new route match rule to linked list. */
1377 route_map_rule_add(&index
->set_list
, rule
);
1379 /* Execute event hook. */
1380 if (route_map_master
.event_hook
) {
1381 (*route_map_master
.event_hook
)(index
->map
->name
);
1382 route_map_notify_dependencies(index
->map
->name
,
1383 RMAP_EVENT_CALL_ADDED
);
1385 return RMAP_COMPILE_SUCCESS
;
1388 /* Delete route map set rule. */
1389 enum rmap_compile_rets
route_map_delete_set(struct route_map_index
*index
,
1390 const char *set_name
,
1391 const char *set_arg
)
1393 struct route_map_rule
*rule
;
1394 const struct route_map_rule_cmd
*cmd
;
1396 cmd
= route_map_lookup_set(set_name
);
1398 return RMAP_RULE_MISSING
;
1400 for (rule
= index
->set_list
.head
; rule
; rule
= rule
->next
)
1401 if ((rule
->cmd
== cmd
) && (rulecmp(rule
->rule_str
, set_arg
) == 0
1402 || set_arg
== NULL
)) {
1403 route_map_rule_delete(&index
->set_list
, rule
);
1404 /* Execute event hook. */
1405 if (route_map_master
.event_hook
) {
1406 (*route_map_master
.event_hook
)(index
->map
->name
);
1407 route_map_notify_dependencies(
1409 RMAP_EVENT_CALL_ADDED
);
1411 return RMAP_COMPILE_SUCCESS
;
1413 /* Can't find matched rule. */
1414 return RMAP_RULE_MISSING
;
1417 static enum route_map_cmd_result_t
1418 route_map_apply_match(struct route_map_rule_list
*match_list
,
1419 const struct prefix
*prefix
, route_map_object_t type
,
1422 enum route_map_cmd_result_t ret
= RMAP_NOMATCH
;
1423 struct route_map_rule
*match
;
1424 bool is_matched
= false;
1427 /* Check all match rule and if there is no match rule, go to the
1429 if (!match_list
->head
)
1432 for (match
= match_list
->head
; match
; match
= match
->next
) {
1434 * Try each match statement. If any match does not
1435 * return RMAP_MATCH or RMAP_NOOP, return.
1436 * Otherwise continue on to next match statement.
1437 * All match statements must MATCH for
1438 * end-result to be a match.
1439 * (Exception:If match stmts result in a mix of
1440 * MATCH/NOOP, then also end-result is a match)
1441 * If all result in NOOP, end-result is NOOP.
1443 ret
= (*match
->cmd
->func_apply
)(match
->value
, prefix
,
1447 * If the consolidated result of func_apply is:
1448 * -----------------------------------------------
1449 * | MATCH | NOMATCH | NOOP | Final Result |
1450 * ------------------------------------------------
1451 * | yes | yes | yes | NOMATCH |
1452 * | no | no | yes | NOOP |
1453 * | yes | no | yes | MATCH |
1454 * | no | yes | yes | NOMATCH |
1455 * |-----------------------------------------------
1457 * Traditionally, all rules within route-map
1458 * should match for it to MATCH.
1459 * If there are noops within the route-map rules,
1460 * it follows the above matrix.
1462 * Eg: route-map rm1 permit 10
1467 * route-map rm1 permit 20
1495 /* Apply route map's each index to the object.
1497 The matrix for a route-map looks like this:
1498 (note, this includes the description for the "NEXT"
1499 and "GOTO" frobs now
1501 | Match | No Match | No op
1502 |-----------|--------------|-------
1503 permit | action | cont | cont.
1504 | | default:deny | default:permit
1505 -------------------+-----------------------
1506 | deny | cont | cont.
1507 deny | | default:deny | default:permit
1508 |-----------|--------------|--------
1511 -Apply Set statements, accept route
1512 -If Call statement is present jump to the specified route-map, if it
1513 denies the route we finish.
1514 -If NEXT is specified, goto NEXT statement
1515 -If GOTO is specified, goto the first clause where pref > nextpref
1516 -If nothing is specified, do as Cisco and finish
1518 -Route is denied by route-map.
1522 If we get no matches after we've processed all updates, then the route
1525 Some notes on the new "CALL", "NEXT" and "GOTO"
1526 call WORD - If this clause is matched, then the set statements
1527 are executed and then we jump to route-map 'WORD'. If
1528 this route-map denies the route, we finish, in other
1530 do whatever the exit policy (EXIT, NEXT or GOTO) tells.
1531 on-match next - If this clause is matched, then the set statements
1532 are executed and then we drop through to the next clause
1533 on-match goto n - If this clause is matched, then the set statments
1534 are executed and then we goto the nth clause, or the
1535 first clause greater than this. In order to ensure
1536 route-maps *always* exit, you cannot jump backwards.
1539 We need to make sure our route-map processing matches the above
1541 route_map_result_t
route_map_apply(struct route_map
*map
,
1542 const struct prefix
*prefix
,
1543 route_map_object_t type
, void *object
)
1545 static int recursion
= 0;
1546 enum route_map_cmd_result_t match_ret
= RMAP_NOMATCH
;
1547 route_map_result_t ret
= RMAP_PERMITMATCH
;
1548 struct route_map_index
*index
;
1549 struct route_map_rule
*set
;
1550 char buf
[PREFIX_STRLEN
];
1552 if (recursion
> RMAP_RECURSION_LIMIT
) {
1554 EC_LIB_RMAP_RECURSION_LIMIT
,
1555 "route-map recursion limit (%d) reached, discarding route",
1556 RMAP_RECURSION_LIMIT
);
1558 return RMAP_DENYMATCH
;
1561 if (map
== NULL
|| map
->head
== NULL
) {
1562 ret
= RMAP_DENYMATCH
;
1563 goto route_map_apply_end
;
1567 for (index
= map
->head
; index
; index
= index
->next
) {
1568 /* Apply this index. */
1570 match_ret
= route_map_apply_match(&index
->match_list
, prefix
,
1574 zlog_debug("Route-map: %s, sequence: %d, prefix: %s, result: %s",
1575 map
->name
, index
->pref
,
1576 prefix2str(prefix
, buf
, sizeof(buf
)),
1577 route_map_cmd_result_str(match_ret
));
1580 /* Now we apply the matrix from above */
1581 if (match_ret
== RMAP_NOOP
)
1583 * Do not change the return value. Retain the previous
1584 * return value. Previous values can be:
1585 * 1)permitmatch (if a nomatch was never
1586 * seen before in this route-map.)
1587 * 2)denymatch (if a nomatch was seen earlier in one
1588 * of the previous sequences)
1592 * 'cont' from matrix - continue to next route-map
1596 else if (match_ret
== RMAP_NOMATCH
) {
1599 * The return value is now changed to denymatch.
1600 * So from here on out, even if we see more noops,
1601 * we retain this return value and return this
1602 * eventually if there are no matches.
1604 ret
= RMAP_DENYMATCH
;
1607 * 'cont' from matrix - continue to next route-map
1611 } else if (match_ret
== RMAP_MATCH
) {
1612 if (index
->type
== RMAP_PERMIT
)
1615 /* Match succeeded, rmap is of type permit */
1616 ret
= RMAP_PERMITMATCH
;
1618 /* permit+match must execute sets */
1619 for (set
= index
->set_list
.head
; set
;
1622 * set cmds return RMAP_OKAY or
1623 * RMAP_ERROR. We do not care if
1624 * set succeeded or not. So, ignore
1627 (void) (*set
->cmd
->func_apply
)(
1628 set
->value
, prefix
, type
,
1631 /* Call another route-map if available */
1632 if (index
->nextrm
) {
1633 struct route_map
*nextrm
=
1634 route_map_lookup_by_name(
1637 if (nextrm
) /* Target route-map found,
1641 ret
= route_map_apply(
1642 nextrm
, prefix
, type
,
1647 /* If nextrm returned 'deny', finish. */
1648 if (ret
== RMAP_DENYMATCH
)
1649 goto route_map_apply_end
;
1652 switch (index
->exitpolicy
) {
1654 goto route_map_apply_end
;
1658 /* Find the next clause to jump to */
1659 struct route_map_index
*next
=
1661 int nextpref
= index
->nextpref
;
1663 while (next
&& next
->pref
< nextpref
) {
1668 /* No clauses match! */
1669 goto route_map_apply_end
;
1673 } else if (index
->type
== RMAP_DENY
)
1676 ret
= RMAP_DENYMATCH
;
1677 goto route_map_apply_end
;
1682 route_map_apply_end
:
1684 zlog_debug("Route-map: %s, prefix: %s, result: %s",
1685 (map
? map
->name
: "null"),
1686 prefix2str(prefix
, buf
, sizeof(buf
)),
1687 route_map_result_str(ret
));
1693 void route_map_add_hook(void (*func
)(const char *))
1695 route_map_master
.add_hook
= func
;
1698 void route_map_delete_hook(void (*func
)(const char *))
1700 route_map_master
.delete_hook
= func
;
1703 void route_map_event_hook(void (*func
)(const char *name
))
1705 route_map_master
.event_hook
= func
;
1708 /* Routines for route map dependency lists and dependency processing */
1709 static bool route_map_rmap_hash_cmp(const void *p1
, const void *p2
)
1711 return strcmp(((const struct route_map_dep_data
*)p1
)->rname
,
1712 ((const struct route_map_dep_data
*)p2
)->rname
)
1716 static bool route_map_dep_hash_cmp(const void *p1
, const void *p2
)
1719 return (strcmp(((const struct route_map_dep
*)p1
)->dep_name
,
1724 static void route_map_clear_reference(struct hash_bucket
*bucket
, void *arg
)
1726 struct route_map_dep
*dep
= bucket
->data
;
1727 struct route_map_dep_data
*dep_data
= NULL
, tmp_dep_data
;
1730 memset(&tmp_dep_data
, 0, sizeof(struct route_map_dep_data
));
1731 tmp_dep_data
.rname
= arg
;
1732 dep_data
= hash_release(dep
->dep_rmap_hash
,
1735 XFREE(MTYPE_ROUTE_MAP_NAME
, dep_data
->rname
);
1736 XFREE(MTYPE_ROUTE_MAP_DEP_DATA
, dep_data
);
1738 if (!dep
->dep_rmap_hash
->count
) {
1739 dep
= hash_release(dep
->this_hash
,
1740 (void *)dep
->dep_name
);
1741 hash_free(dep
->dep_rmap_hash
);
1742 XFREE(MTYPE_ROUTE_MAP_NAME
, dep
->dep_name
);
1743 XFREE(MTYPE_ROUTE_MAP_DEP
, dep
);
1748 static void route_map_clear_all_references(char *rmap_name
)
1752 for (i
= 1; i
< ROUTE_MAP_DEP_MAX
; i
++) {
1753 hash_iterate(route_map_dep_hash
[i
], route_map_clear_reference
,
1758 static unsigned int route_map_dep_data_hash_make_key(const void *p
)
1760 const struct route_map_dep_data
*dep_data
= p
;
1762 return string_hash_make(dep_data
->rname
);
1765 static void *route_map_dep_hash_alloc(void *p
)
1767 char *dep_name
= (char *)p
;
1768 struct route_map_dep
*dep_entry
;
1770 dep_entry
= XCALLOC(MTYPE_ROUTE_MAP_DEP
, sizeof(struct route_map_dep
));
1771 dep_entry
->dep_name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, dep_name
);
1772 dep_entry
->dep_rmap_hash
=
1773 hash_create_size(8, route_map_dep_data_hash_make_key
,
1774 route_map_rmap_hash_cmp
, "Route Map Dep Hash");
1775 dep_entry
->this_hash
= NULL
;
1780 static void *route_map_name_hash_alloc(void *p
)
1782 struct route_map_dep_data
*dep_data
= NULL
, *tmp_dep_data
= NULL
;
1784 dep_data
= XCALLOC(MTYPE_ROUTE_MAP_DEP_DATA
,
1785 sizeof(struct route_map_dep_data
));
1787 dep_data
->rname
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, tmp_dep_data
->rname
);
1791 static unsigned int route_map_dep_hash_make_key(const void *p
)
1793 return (string_hash_make((char *)p
));
1796 static void route_map_print_dependency(struct hash_bucket
*bucket
, void *data
)
1798 struct route_map_dep_data
*dep_data
= bucket
->data
;
1799 char *rmap_name
= dep_data
->rname
;
1800 char *dep_name
= data
;
1802 zlog_debug("%s: Dependency for %s: %s", __FUNCTION__
, dep_name
,
1806 static int route_map_dep_update(struct hash
*dephash
, const char *dep_name
,
1807 const char *rmap_name
, route_map_event_t type
)
1809 struct route_map_dep
*dep
= NULL
;
1810 char *dname
, *rname
;
1812 struct route_map_dep_data
*dep_data
= NULL
, *ret_dep_data
= NULL
;
1813 struct route_map_dep_data tmp_dep_data
;
1815 dname
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, dep_name
);
1816 rname
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap_name
);
1819 case RMAP_EVENT_PLIST_ADDED
:
1820 case RMAP_EVENT_CLIST_ADDED
:
1821 case RMAP_EVENT_ECLIST_ADDED
:
1822 case RMAP_EVENT_ASLIST_ADDED
:
1823 case RMAP_EVENT_LLIST_ADDED
:
1824 case RMAP_EVENT_CALL_ADDED
:
1825 case RMAP_EVENT_FILTER_ADDED
:
1827 zlog_debug("Adding dependency for filter %s in route-map %s",
1828 dep_name
, rmap_name
);
1829 dep
= (struct route_map_dep
*)hash_get(
1830 dephash
, dname
, route_map_dep_hash_alloc
);
1836 if (!dep
->this_hash
)
1837 dep
->this_hash
= dephash
;
1839 memset(&tmp_dep_data
, 0, sizeof(struct route_map_dep_data
));
1840 tmp_dep_data
.rname
= rname
;
1841 dep_data
= hash_lookup(dep
->dep_rmap_hash
, &tmp_dep_data
);
1843 dep_data
= hash_get(dep
->dep_rmap_hash
, &tmp_dep_data
,
1844 route_map_name_hash_alloc
);
1848 case RMAP_EVENT_PLIST_DELETED
:
1849 case RMAP_EVENT_CLIST_DELETED
:
1850 case RMAP_EVENT_ECLIST_DELETED
:
1851 case RMAP_EVENT_ASLIST_DELETED
:
1852 case RMAP_EVENT_LLIST_DELETED
:
1853 case RMAP_EVENT_CALL_DELETED
:
1854 case RMAP_EVENT_FILTER_DELETED
:
1856 zlog_debug("Deleting dependency for filter %s in route-map %s",
1857 dep_name
, rmap_name
);
1858 dep
= (struct route_map_dep
*)hash_get(dephash
, dname
, NULL
);
1863 memset(&tmp_dep_data
, 0, sizeof(struct route_map_dep_data
));
1864 tmp_dep_data
.rname
= rname
;
1865 dep_data
= hash_lookup(dep
->dep_rmap_hash
, &tmp_dep_data
);
1868 if (!dep_data
->refcnt
) {
1869 ret_dep_data
= hash_release(dep
->dep_rmap_hash
,
1872 XFREE(MTYPE_ROUTE_MAP_NAME
,
1873 ret_dep_data
->rname
);
1874 XFREE(MTYPE_ROUTE_MAP_DEP_DATA
, ret_dep_data
);
1878 if (!dep
->dep_rmap_hash
->count
) {
1879 dep
= hash_release(dephash
, dname
);
1880 hash_free(dep
->dep_rmap_hash
);
1881 XFREE(MTYPE_ROUTE_MAP_NAME
, dep
->dep_name
);
1882 XFREE(MTYPE_ROUTE_MAP_DEP
, dep
);
1886 case RMAP_EVENT_SET_ADDED
:
1887 case RMAP_EVENT_SET_DELETED
:
1888 case RMAP_EVENT_SET_REPLACED
:
1889 case RMAP_EVENT_MATCH_ADDED
:
1890 case RMAP_EVENT_MATCH_DELETED
:
1891 case RMAP_EVENT_MATCH_REPLACED
:
1892 case RMAP_EVENT_INDEX_ADDED
:
1893 case RMAP_EVENT_INDEX_DELETED
:
1899 hash_iterate(dep
->dep_rmap_hash
,
1900 route_map_print_dependency
, dname
);
1904 XFREE(MTYPE_ROUTE_MAP_NAME
, rname
);
1905 XFREE(MTYPE_ROUTE_MAP_NAME
, dname
);
1909 static struct hash
*route_map_get_dep_hash(route_map_event_t event
)
1911 struct hash
*upd8_hash
= NULL
;
1914 case RMAP_EVENT_PLIST_ADDED
:
1915 case RMAP_EVENT_PLIST_DELETED
:
1916 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_PLIST
];
1918 case RMAP_EVENT_CLIST_ADDED
:
1919 case RMAP_EVENT_CLIST_DELETED
:
1920 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_CLIST
];
1922 case RMAP_EVENT_ECLIST_ADDED
:
1923 case RMAP_EVENT_ECLIST_DELETED
:
1924 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_ECLIST
];
1926 case RMAP_EVENT_ASLIST_ADDED
:
1927 case RMAP_EVENT_ASLIST_DELETED
:
1928 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_ASPATH
];
1930 case RMAP_EVENT_LLIST_ADDED
:
1931 case RMAP_EVENT_LLIST_DELETED
:
1932 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_LCLIST
];
1934 case RMAP_EVENT_CALL_ADDED
:
1935 case RMAP_EVENT_CALL_DELETED
:
1936 case RMAP_EVENT_MATCH_ADDED
:
1937 case RMAP_EVENT_MATCH_DELETED
:
1938 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_RMAP
];
1940 case RMAP_EVENT_FILTER_ADDED
:
1941 case RMAP_EVENT_FILTER_DELETED
:
1942 upd8_hash
= route_map_dep_hash
[ROUTE_MAP_DEP_FILTER
];
1945 * Should we actually be ignoring these?
1946 * I am not sure but at this point in time, let
1947 * us get them into this switch and we can peel
1948 * them into the appropriate place in the future
1950 case RMAP_EVENT_SET_ADDED
:
1951 case RMAP_EVENT_SET_DELETED
:
1952 case RMAP_EVENT_SET_REPLACED
:
1953 case RMAP_EVENT_MATCH_REPLACED
:
1954 case RMAP_EVENT_INDEX_ADDED
:
1955 case RMAP_EVENT_INDEX_DELETED
:
1962 static void route_map_process_dependency(struct hash_bucket
*bucket
, void *data
)
1964 struct route_map_dep_data
*dep_data
= NULL
;
1965 char *rmap_name
= NULL
;
1967 dep_data
= bucket
->data
;
1968 rmap_name
= dep_data
->rname
;
1971 zlog_debug("Notifying %s of dependency", rmap_name
);
1972 if (route_map_master
.event_hook
)
1973 (*route_map_master
.event_hook
)(rmap_name
);
1976 void route_map_upd8_dependency(route_map_event_t type
, const char *arg
,
1977 const char *rmap_name
)
1979 struct hash
*upd8_hash
= NULL
;
1981 if ((upd8_hash
= route_map_get_dep_hash(type
))) {
1982 route_map_dep_update(upd8_hash
, arg
, rmap_name
, type
);
1984 if (type
== RMAP_EVENT_CALL_ADDED
) {
1986 if (route_map_master
.add_hook
)
1987 (*route_map_master
.add_hook
)(rmap_name
);
1988 } else if (type
== RMAP_EVENT_CALL_DELETED
) {
1990 if (route_map_master
.delete_hook
)
1991 (*route_map_master
.delete_hook
)(rmap_name
);
1996 void route_map_notify_dependencies(const char *affected_name
,
1997 route_map_event_t event
)
1999 struct route_map_dep
*dep
;
2000 struct hash
*upd8_hash
;
2006 name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, affected_name
);
2008 if ((upd8_hash
= route_map_get_dep_hash(event
)) == NULL
) {
2009 XFREE(MTYPE_ROUTE_MAP_NAME
, name
);
2013 dep
= (struct route_map_dep
*)hash_get(upd8_hash
, name
, NULL
);
2015 if (!dep
->this_hash
)
2016 dep
->this_hash
= upd8_hash
;
2019 zlog_debug("Filter %s updated", dep
->dep_name
);
2020 hash_iterate(dep
->dep_rmap_hash
, route_map_process_dependency
,
2024 XFREE(MTYPE_ROUTE_MAP_NAME
, name
);
2028 /* VTY related functions. */
2029 static void clear_route_map_helper(struct route_map
*map
)
2031 struct route_map_index
*index
;
2033 map
->applied_clear
= map
->applied
;
2034 for (index
= map
->head
; index
; index
= index
->next
)
2035 index
->applied_clear
= index
->applied
;
2038 DEFUN (rmap_clear_counters
,
2039 rmap_clear_counters_cmd
,
2040 "clear route-map counters [WORD]",
2042 "route-map information\n"
2043 "counters associated with the specified route-map\n"
2047 struct route_map
*map
;
2049 const char *name
= (argc
== 3 ) ? argv
[idx_word
]->arg
: NULL
;
2052 map
= route_map_lookup_by_name(name
);
2055 clear_route_map_helper(map
);
2057 vty_out(vty
, "%s: 'route-map %s' not found\n",
2058 frr_protonameinst
, name
);
2062 for (map
= route_map_master
.head
; map
; map
= map
->next
)
2063 clear_route_map_helper(map
);
2070 DEFUN (rmap_show_name
,
2072 "show route-map [WORD]",
2074 "route-map information\n"
2078 const char *name
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2079 return vty_show_route_map(vty
, name
);
2082 DEFUN (rmap_show_unused
,
2083 rmap_show_unused_cmd
,
2084 "show route-map-unused",
2086 "unused route-map information\n")
2088 return vty_show_unused_route_map(vty
);
2095 "Debug option set for route-maps\n")
2101 DEFUN (no_debug_rmap
,
2103 "no debug route-map",
2106 "Debug option set for route-maps\n")
2113 static struct cmd_node rmap_debug_node
= {RMAP_DEBUG_NODE
, "", 1};
2115 /* Configuration write function. */
2116 static int rmap_config_write_debug(struct vty
*vty
)
2121 vty_out(vty
, "debug route-map\n");
2128 /* Common route map rules */
2130 void *route_map_rule_tag_compile(const char *arg
)
2132 unsigned long int tmp
;
2137 tmp
= strtoul(arg
, &endptr
, 0);
2138 if (arg
[0] == '\0' || *endptr
!= '\0' || errno
|| tmp
> ROUTE_TAG_MAX
)
2141 tag
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*tag
));
2147 void route_map_rule_tag_free(void *rule
)
2149 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
2152 void route_map_finish(void)
2156 vector_free(route_match_vec
);
2157 route_match_vec
= NULL
;
2158 vector_free(route_set_vec
);
2159 route_set_vec
= NULL
;
2162 * All protocols are setting these to NULL
2163 * by default on shutdown( route_map_finish )
2164 * Why are we making them do this work?
2166 route_map_master
.add_hook
= NULL
;
2167 route_map_master
.delete_hook
= NULL
;
2168 route_map_master
.event_hook
= NULL
;
2170 /* cleanup route_map */
2171 while (route_map_master
.head
) {
2172 struct route_map
*map
= route_map_master
.head
;
2173 map
->to_be_processed
= false;
2174 route_map_delete(map
);
2177 for (i
= 1; i
< ROUTE_MAP_DEP_MAX
; i
++) {
2178 hash_free(route_map_dep_hash
[i
]);
2179 route_map_dep_hash
[i
] = NULL
;
2182 hash_free(route_map_master_hash
);
2183 route_map_master_hash
= NULL
;
2186 /* Increment the use_count counter while attaching the route map */
2187 void route_map_counter_increment(struct route_map
*map
)
2193 /* Decrement the use_count counter while detaching the route map. */
2194 void route_map_counter_decrement(struct route_map
*map
)
2197 if (map
->use_count
<= 0)
2203 /* Initialization of route map vector. */
2204 void route_map_init(void)
2208 /* Make vector for match and set. */
2209 route_match_vec
= vector_init(1);
2210 route_set_vec
= vector_init(1);
2211 route_map_master_hash
=
2212 hash_create_size(8, route_map_hash_key_make
, route_map_hash_cmp
,
2213 "Route Map Master Hash");
2215 for (i
= 1; i
< ROUTE_MAP_DEP_MAX
; i
++)
2216 route_map_dep_hash
[i
] = hash_create_size(
2217 8, route_map_dep_hash_make_key
, route_map_dep_hash_cmp
,
2218 "Route Map Dep Hash");
2222 route_map_cli_init();
2224 /* Install route map top node. */
2225 install_node(&rmap_debug_node
, rmap_config_write_debug
);
2227 /* Install route map commands. */
2228 install_element(CONFIG_NODE
, &debug_rmap_cmd
);
2229 install_element(CONFIG_NODE
, &no_debug_rmap_cmd
);
2231 /* Install show command */
2232 install_element(ENABLE_NODE
, &rmap_clear_counters_cmd
);
2234 install_element(ENABLE_NODE
, &rmap_show_name_cmd
);
2235 install_element(ENABLE_NODE
, &rmap_show_unused_cmd
);
2237 install_element(ENABLE_NODE
, &debug_rmap_cmd
);
2238 install_element(ENABLE_NODE
, &no_debug_rmap_cmd
);