2 * Copyright (C) 1998 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
21 #ifndef _ZEBRA_ROUTEMAP_H
22 #define _ZEBRA_ROUTEMAP_H
28 #include "lib/plist.h"
29 #include "lib/plist_int.h"
35 DECLARE_MTYPE(ROUTE_MAP_NAME
)
36 DECLARE_MTYPE(ROUTE_MAP_RULE
)
37 DECLARE_MTYPE(ROUTE_MAP_COMPILED
)
39 /* Route map's type. */
40 enum route_map_type
{ RMAP_PERMIT
, RMAP_DENY
, RMAP_ANY
};
48 * Route-map match or set result "Eg: match evpn vni xx"
49 * route-map match cmd always returns match/nomatch/noop
50 * match--> found a match
51 * nomatch--> didnt find a match
52 * noop--> not applicable
53 * route-map set retuns okay/error
54 * okay --> set was successful
55 * error --> set was not successful
57 enum route_map_cmd_result_t
{
59 * route-map match cmd results
65 * route-map set cmd results
82 typedef enum { RMAP_EXIT
, RMAP_GOTO
, RMAP_NEXT
} route_map_end_t
;
86 RMAP_EVENT_SET_DELETED
,
87 RMAP_EVENT_SET_REPLACED
,
88 RMAP_EVENT_MATCH_ADDED
,
89 RMAP_EVENT_MATCH_DELETED
,
90 RMAP_EVENT_MATCH_REPLACED
,
91 RMAP_EVENT_INDEX_ADDED
,
92 RMAP_EVENT_INDEX_DELETED
,
93 RMAP_EVENT_CALL_ADDED
, /* call to another routemap added */
94 RMAP_EVENT_CALL_DELETED
,
95 RMAP_EVENT_PLIST_ADDED
,
96 RMAP_EVENT_PLIST_DELETED
,
97 RMAP_EVENT_CLIST_ADDED
,
98 RMAP_EVENT_CLIST_DELETED
,
99 RMAP_EVENT_ECLIST_ADDED
,
100 RMAP_EVENT_ECLIST_DELETED
,
101 RMAP_EVENT_LLIST_ADDED
,
102 RMAP_EVENT_LLIST_DELETED
,
103 RMAP_EVENT_ASLIST_ADDED
,
104 RMAP_EVENT_ASLIST_DELETED
,
105 RMAP_EVENT_FILTER_ADDED
,
106 RMAP_EVENT_FILTER_DELETED
,
109 /* Depth limit in RMAP recursion using RMAP_CALL. */
110 #define RMAP_RECURSION_LIMIT 10
112 /* Route map rule structure for matching and setting. */
113 struct route_map_rule_cmd
{
114 /* Route map rule name (e.g. as-path, metric) */
117 /* Function for value set or match. */
118 enum route_map_cmd_result_t (*func_apply
)(void *rule
,
119 const struct prefix
*prefix
,
120 route_map_object_t type
,
123 /* Compile argument and return result as void *. */
124 void *(*func_compile
)(const char *);
126 /* Free allocated value by func_compile (). */
127 void (*func_free
)(void *);
129 /** To get the rule key after Compilation **/
130 void *(*func_get_rmap_rule_key
)(void *val
);
133 /* Route map apply error. */
134 enum rmap_compile_rets
{
135 RMAP_COMPILE_SUCCESS
,
137 /* Route map rule is missing. */
140 /* Route map rule can't compile */
145 /* Route map rule. This rule has both `match' rule and `set' rule. */
146 struct route_map_rule
{
148 const struct route_map_rule_cmd
*cmd
;
150 /* For pretty printing. */
153 /* Pre-compiled match rule. */
157 struct route_map_rule
*next
;
158 struct route_map_rule
*prev
;
161 /* Route map rule list. */
162 struct route_map_rule_list
{
163 struct route_map_rule
*head
;
164 struct route_map_rule
*tail
;
167 /* Forward struct declaration: the complete can be found later this file. */
168 struct routemap_hook_context
;
170 /* Route map index structure. */
171 struct route_map_index
{
172 struct route_map
*map
;
175 /* Preference of this route map rule. */
178 /* Route map type permit or deny. */
179 enum route_map_type type
;
181 /* Do we follow old rules, or hop forward? */
182 route_map_end_t exitpolicy
;
184 /* If we're using "GOTO", to where do we go? */
187 /* If we're using "CALL", to which route-map do ew go? */
190 /* Matching rule list. */
191 struct route_map_rule_list match_list
;
192 struct route_map_rule_list set_list
;
194 /* Make linked list. */
195 struct route_map_index
*next
;
196 struct route_map_index
*prev
;
198 /* Keep track how many times we've try to apply */
200 uint64_t applied_clear
;
202 /* List of match/sets contexts. */
203 TAILQ_HEAD(, routemap_hook_context
) rhclist
;
207 DECLARE_QOBJ_TYPE(route_map_index
)
209 /* Route map list structure. */
211 /* Name of route map. */
214 /* Route map's rule. */
215 struct route_map_index
*head
;
216 struct route_map_index
*tail
;
218 /* Make linked list. */
219 struct route_map
*next
;
220 struct route_map
*prev
;
222 /* Maintain update info */
223 bool to_be_processed
; /* True if modification isn't acted on yet */
224 bool deleted
; /* If 1, then this node will be deleted */
225 bool optimization_disabled
;
227 /* How many times have we applied this route-map */
229 uint64_t applied_clear
;
231 /* Counter to track active usage of this route-map */
234 /* Tables to maintain IPv4 and IPv6 prefixes from
235 * the prefix-list match clause.
237 struct route_table
*ipv4_prefix_table
;
238 struct route_table
*ipv6_prefix_table
;
242 DECLARE_QOBJ_TYPE(route_map
)
245 extern void route_map_init(void);
248 * This should only be called on shutdown
249 * Additionally this function sets the hooks to NULL
250 * before any processing is done.
252 extern void route_map_finish(void);
254 /* Add match statement to route map. */
255 extern enum rmap_compile_rets
route_map_add_match(struct route_map_index
*index
,
256 const char *match_name
,
257 const char *match_arg
,
258 route_map_event_t type
);
260 /* Delete specified route match rule. */
261 extern enum rmap_compile_rets
262 route_map_delete_match(struct route_map_index
*index
,
263 const char *match_name
, const char *match_arg
,
264 route_map_event_t type
);
266 extern const char *route_map_get_match_arg(struct route_map_index
*index
,
267 const char *match_name
);
269 /* Add route-map set statement to the route map. */
270 extern enum rmap_compile_rets
route_map_add_set(struct route_map_index
*index
,
271 const char *set_name
,
272 const char *set_arg
);
274 /* Delete route map set rule. */
275 extern enum rmap_compile_rets
276 route_map_delete_set(struct route_map_index
*index
,
277 const char *set_name
, const char *set_arg
);
279 /* Install rule command to the match list. */
280 extern void route_map_install_match(const struct route_map_rule_cmd
*cmd
);
283 * Install rule command to the set list.
285 * When installing a particular item, Allow a difference of handling
286 * of bad cli inputted(return NULL) -vs- this particular daemon cannot use
287 * this form of the command(return a pointer and handle it appropriately
288 * in the apply command). See 'set metric' command
289 * as it is handled in ripd/ripngd and ospfd.
291 extern void route_map_install_set(const struct route_map_rule_cmd
*cmd
);
293 /* Lookup route map by name. */
294 extern struct route_map
*route_map_lookup_by_name(const char *name
);
296 /* Simple helper to warn if route-map does not exist. */
297 struct route_map
*route_map_lookup_warn_noexist(struct vty
*vty
, const char *name
);
299 /* Apply route map to the object. */
300 extern route_map_result_t
route_map_apply(struct route_map
*map
,
301 const struct prefix
*prefix
,
302 route_map_object_t object_type
,
305 extern void route_map_add_hook(void (*func
)(const char *));
306 extern void route_map_delete_hook(void (*func
)(const char *));
309 * This is the callback for when something has changed about a
310 * route-map. The interested parties can register to receive
313 * name - Is the name of the changed route-map
315 extern void route_map_event_hook(void (*func
)(const char *name
));
316 extern int route_map_mark_updated(const char *name
);
317 extern void route_map_walk_update_list(void (*update_fn
)(char *name
));
318 extern void route_map_upd8_dependency(route_map_event_t type
, const char *arg
,
319 const char *rmap_name
);
320 extern void route_map_notify_dependencies(const char *affected_name
,
321 route_map_event_t event
);
323 route_map_notify_pentry_dependencies(const char *affected_name
,
324 struct prefix_list_entry
*pentry
,
325 route_map_event_t event
);
326 extern int generic_match_add(struct vty
*vty
, struct route_map_index
*index
,
327 const char *command
, const char *arg
,
328 route_map_event_t type
);
330 extern int generic_match_delete(struct vty
*vty
, struct route_map_index
*index
,
331 const char *command
, const char *arg
,
332 route_map_event_t type
);
333 extern int generic_set_add(struct vty
*vty
, struct route_map_index
*index
,
334 const char *command
, const char *arg
);
335 extern int generic_set_delete(struct vty
*vty
, struct route_map_index
*index
,
336 const char *command
, const char *arg
);
339 /* match interface */
340 extern void route_map_match_interface_hook(int (*func
)(
341 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
342 const char *arg
, route_map_event_t type
));
343 /* no match interface */
344 extern void route_map_no_match_interface_hook(int (*func
)(
345 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
346 const char *arg
, route_map_event_t type
));
347 /* match ip address */
348 extern void route_map_match_ip_address_hook(int (*func
)(
349 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
350 const char *arg
, route_map_event_t type
));
351 /* no match ip address */
352 extern void route_map_no_match_ip_address_hook(int (*func
)(
353 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
354 const char *arg
, route_map_event_t type
));
355 /* match ip address prefix list */
356 extern void route_map_match_ip_address_prefix_list_hook(int (*func
)(
357 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
358 const char *arg
, route_map_event_t type
));
359 /* no match ip address prefix list */
360 extern void route_map_no_match_ip_address_prefix_list_hook(int (*func
)(
361 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
362 const char *arg
, route_map_event_t type
));
363 /* match ip next hop */
364 extern void route_map_match_ip_next_hop_hook(int (*func
)(
365 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
366 const char *arg
, route_map_event_t type
));
367 /* no match ip next hop */
368 extern void route_map_no_match_ip_next_hop_hook(int (*func
)(
369 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
370 const char *arg
, route_map_event_t type
));
371 /* match ip next hop prefix list */
372 extern void route_map_match_ip_next_hop_prefix_list_hook(int (*func
)(
373 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
374 const char *arg
, route_map_event_t type
));
375 /* no match ip next hop prefix list */
376 extern void route_map_no_match_ip_next_hop_prefix_list_hook(int (*func
)(
377 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
378 const char *arg
, route_map_event_t type
));
379 /* match ip next hop type */
380 extern void route_map_match_ip_next_hop_type_hook(int (*func
)(
381 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
382 const char *arg
, route_map_event_t type
));
383 /* no match ip next hop type */
384 extern void route_map_no_match_ip_next_hop_type_hook(int (*func
)(
385 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
386 const char *arg
, route_map_event_t type
));
387 /* match ipv6 address */
388 extern void route_map_match_ipv6_address_hook(int (*func
)(
389 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
390 const char *arg
, route_map_event_t type
));
391 /* no match ipv6 address */
392 extern void route_map_no_match_ipv6_address_hook(int (*func
)(
393 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
394 const char *arg
, route_map_event_t type
));
395 /* match ipv6 address prefix list */
396 extern void route_map_match_ipv6_address_prefix_list_hook(int (*func
)(
397 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
398 const char *arg
, route_map_event_t type
));
399 /* no match ipv6 address prefix list */
400 extern void route_map_no_match_ipv6_address_prefix_list_hook(int (*func
)(
401 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
402 const char *arg
, route_map_event_t type
));
403 /* match ipv6 next-hop type */
404 extern void route_map_match_ipv6_next_hop_type_hook(int (*func
)(
405 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
406 const char *arg
, route_map_event_t type
));
407 /* no match ipv6 next-hop type */
408 extern void route_map_no_match_ipv6_next_hop_type_hook(int (*func
)(
409 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
410 const char *arg
, route_map_event_t type
));
412 extern void route_map_match_metric_hook(int (*func
)(
413 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
414 const char *arg
, route_map_event_t type
));
415 /* no match metric */
416 extern void route_map_no_match_metric_hook(int (*func
)(
417 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
418 const char *arg
, route_map_event_t type
));
420 extern void route_map_match_tag_hook(int (*func
)(
421 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
422 const char *arg
, route_map_event_t type
));
424 extern void route_map_no_match_tag_hook(int (*func
)(
425 struct vty
*vty
, struct route_map_index
*index
, const char *command
,
426 const char *arg
, route_map_event_t type
));
428 extern void route_map_set_ip_nexthop_hook(
429 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
430 const char *command
, const char *arg
));
431 /* no set ip nexthop */
432 extern void route_map_no_set_ip_nexthop_hook(
433 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
434 const char *command
, const char *arg
));
435 /* set ipv6 nexthop local */
436 extern void route_map_set_ipv6_nexthop_local_hook(
437 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
438 const char *command
, const char *arg
));
439 /* no set ipv6 nexthop local */
440 extern void route_map_no_set_ipv6_nexthop_local_hook(
441 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
442 const char *command
, const char *arg
));
444 extern void route_map_set_metric_hook(int (*func
)(struct vty
*vty
,
445 struct route_map_index
*index
,
449 extern void route_map_no_set_metric_hook(
450 int (*func
)(struct vty
*vty
, struct route_map_index
*index
,
451 const char *command
, const char *arg
));
453 extern void route_map_set_tag_hook(int (*func
)(struct vty
*vty
,
454 struct route_map_index
*index
,
458 extern void route_map_no_set_tag_hook(int (*func
)(struct vty
*vty
,
459 struct route_map_index
*index
,
463 extern void *route_map_rule_tag_compile(const char *arg
);
464 extern void route_map_rule_tag_free(void *rule
);
466 /* Increment the route-map used counter */
467 extern void route_map_counter_increment(struct route_map
*map
);
469 /* Decrement the route-map used counter */
470 extern void route_map_counter_decrement(struct route_map
*map
);
472 /* Route map hooks data structure. */
473 struct route_map_match_set_hooks
{
474 /* match interface */
475 int (*match_interface
)(struct vty
*vty
, struct route_map_index
*index
,
476 const char *command
, const char *arg
,
477 route_map_event_t type
);
479 /* no match interface */
480 int (*no_match_interface
)(struct vty
*vty
,
481 struct route_map_index
*index
,
482 const char *command
, const char *arg
,
483 route_map_event_t type
);
485 /* match ip address */
486 int (*match_ip_address
)(struct vty
*vty
, struct route_map_index
*index
,
487 const char *command
, const char *arg
,
488 route_map_event_t type
);
490 /* no match ip address */
491 int (*no_match_ip_address
)(struct vty
*vty
,
492 struct route_map_index
*index
,
493 const char *command
, const char *arg
,
494 route_map_event_t type
);
496 /* match ip address prefix list */
497 int (*match_ip_address_prefix_list
)(struct vty
*vty
,
498 struct route_map_index
*index
,
501 route_map_event_t type
);
503 /* no match ip address prefix list */
504 int (*no_match_ip_address_prefix_list
)(struct vty
*vty
,
505 struct route_map_index
*index
,
508 route_map_event_t type
);
510 /* match ip next hop */
511 int (*match_ip_next_hop
)(struct vty
*vty
, struct route_map_index
*index
,
512 const char *command
, const char *arg
,
513 route_map_event_t type
);
515 /* no match ip next hop */
516 int (*no_match_ip_next_hop
)(struct vty
*vty
,
517 struct route_map_index
*index
,
518 const char *command
, const char *arg
,
519 route_map_event_t type
);
521 /* match ip next hop prefix list */
522 int (*match_ip_next_hop_prefix_list
)(struct vty
*vty
,
523 struct route_map_index
*index
,
526 route_map_event_t type
);
528 /* no match ip next hop prefix list */
529 int (*no_match_ip_next_hop_prefix_list
)(struct vty
*vty
,
530 struct route_map_index
*index
,
533 route_map_event_t type
);
535 /* match ip next-hop type */
536 int (*match_ip_next_hop_type
)(struct vty
*vty
,
537 struct route_map_index
*index
,
540 route_map_event_t type
);
542 /* no match ip next-hop type */
543 int (*no_match_ip_next_hop_type
)(struct vty
*vty
,
544 struct route_map_index
*index
,
547 route_map_event_t type
);
549 /* match ipv6 address */
550 int (*match_ipv6_address
)(struct vty
*vty
,
551 struct route_map_index
*index
,
552 const char *command
, const char *arg
,
553 route_map_event_t type
);
555 /* no match ipv6 address */
556 int (*no_match_ipv6_address
)(struct vty
*vty
,
557 struct route_map_index
*index
,
558 const char *command
, const char *arg
,
559 route_map_event_t type
);
562 /* match ipv6 address prefix list */
563 int (*match_ipv6_address_prefix_list
)(struct vty
*vty
,
564 struct route_map_index
*index
,
567 route_map_event_t type
);
569 /* no match ipv6 address prefix list */
570 int (*no_match_ipv6_address_prefix_list
)(struct vty
*vty
,
571 struct route_map_index
*index
,
574 route_map_event_t type
);
576 /* match ipv6 next-hop type */
577 int (*match_ipv6_next_hop_type
)(struct vty
*vty
,
578 struct route_map_index
*index
,
581 route_map_event_t type
);
583 /* no match ipv6 next-hop type */
584 int (*no_match_ipv6_next_hop_type
)(struct vty
*vty
,
585 struct route_map_index
*index
,
586 const char *command
, const char *arg
,
587 route_map_event_t type
);
590 int (*match_metric
)(struct vty
*vty
, struct route_map_index
*index
,
591 const char *command
, const char *arg
,
592 route_map_event_t type
);
594 /* no match metric */
595 int (*no_match_metric
)(struct vty
*vty
, struct route_map_index
*index
,
596 const char *command
, const char *arg
,
597 route_map_event_t type
);
600 int (*match_tag
)(struct vty
*vty
, struct route_map_index
*index
,
601 const char *command
, const char *arg
,
602 route_map_event_t type
);
605 int (*no_match_tag
)(struct vty
*vty
, struct route_map_index
*index
,
606 const char *command
, const char *arg
,
607 route_map_event_t type
);
610 int (*set_ip_nexthop
)(struct vty
*vty
, struct route_map_index
*index
,
611 const char *command
, const char *arg
);
613 /* no set ip nexthop */
614 int (*no_set_ip_nexthop
)(struct vty
*vty
, struct route_map_index
*index
,
615 const char *command
, const char *arg
);
617 /* set ipv6 nexthop local */
618 int (*set_ipv6_nexthop_local
)(struct vty
*vty
,
619 struct route_map_index
*index
,
620 const char *command
, const char *arg
);
622 /* no set ipv6 nexthop local */
623 int (*no_set_ipv6_nexthop_local
)(struct vty
*vty
,
624 struct route_map_index
*index
,
625 const char *command
, const char *arg
);
628 int (*set_metric
)(struct vty
*vty
, struct route_map_index
*index
,
629 const char *command
, const char *arg
);
632 int (*no_set_metric
)(struct vty
*vty
, struct route_map_index
*index
,
633 const char *command
, const char *arg
);
636 int (*set_tag
)(struct vty
*vty
, struct route_map_index
*index
,
637 const char *command
, const char *arg
);
640 int (*no_set_tag
)(struct vty
*vty
, struct route_map_index
*index
,
641 const char *command
, const char *arg
);
644 extern struct route_map_match_set_hooks rmap_match_set_hook
;
646 /* Making route map list. */
647 struct route_map_list
{
648 struct route_map
*head
;
649 struct route_map
*tail
;
651 void (*add_hook
)(const char *);
652 void (*delete_hook
)(const char *);
653 void (*event_hook
)(const char *);
656 extern struct route_map_list route_map_master
;
658 extern struct route_map
*route_map_get(const char *name
);
659 extern void route_map_delete(struct route_map
*map
);
660 extern struct route_map_index
*route_map_index_get(struct route_map
*map
,
661 enum route_map_type type
,
663 extern void route_map_index_delete(struct route_map_index
*index
, int notify
);
665 /* routemap_northbound.c */
666 typedef int (*routemap_match_hook_fun
)(struct vty
*vty
,
667 struct route_map_index
*rmi
,
668 const char *command
, const char *arg
,
669 route_map_event_t event
);
671 typedef int (*routemap_set_hook_fun
)(struct vty
*vty
,
672 struct route_map_index
*rmi
,
673 const char *command
, const char *arg
);
675 struct routemap_hook_context
{
676 struct route_map_index
*rhc_rmi
;
677 const char *rhc_rule
;
678 route_map_event_t rhc_event
;
679 routemap_set_hook_fun rhc_shook
;
680 routemap_match_hook_fun rhc_mhook
;
681 TAILQ_ENTRY(routemap_hook_context
) rhc_entry
;
684 int lib_route_map_entry_match_destroy(enum nb_event event
,
685 const struct lyd_node
*dnode
);
686 int lib_route_map_entry_set_destroy(enum nb_event event
,
687 const struct lyd_node
*dnode
);
689 struct routemap_hook_context
*
690 routemap_hook_context_insert(struct route_map_index
*rmi
);
691 void routemap_hook_context_free(struct routemap_hook_context
*rhc
);
693 extern const struct frr_yang_module_info frr_route_map_info
;
696 extern void route_map_instance_show(struct vty
*vty
, struct lyd_node
*dnode
,
698 extern void route_map_instance_show_end(struct vty
*vty
,
699 struct lyd_node
*dnode
);
700 extern void route_map_condition_show(struct vty
*vty
, struct lyd_node
*dnode
,
702 extern void route_map_action_show(struct vty
*vty
, struct lyd_node
*dnode
,
704 extern void route_map_exit_policy_show(struct vty
*vty
, struct lyd_node
*dnode
,
706 extern void route_map_call_show(struct vty
*vty
, struct lyd_node
*dnode
,
708 extern void route_map_description_show(struct vty
*vty
,
709 struct lyd_node
*dnode
,
711 extern void route_map_cli_init(void);
717 #endif /* _ZEBRA_ROUTEMAP_H */