2 * Route map northbound implementation.
4 * Copyright (C) 2019 Network Device Education Foundation, Inc. ("NetDEF")
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "lib/command.h"
27 #include "lib/northbound.h"
28 #include "lib/routemap.h"
31 * Auxiliary functions to avoid code duplication:
33 * lib_route_map_entry_set_destroy: unset `set` commands.
34 * lib_route_map_entry_match_destroy: unset `match` commands.
36 int lib_route_map_entry_match_destroy(struct nb_cb_destroy_args
*args
)
38 struct routemap_hook_context
*rhc
;
41 if (args
->event
!= NB_EV_APPLY
)
44 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
45 if (rhc
->rhc_mhook
== NULL
)
48 rv
= rhc
->rhc_mhook(rhc
->rhc_rmi
, rhc
->rhc_rule
, NULL
,
50 args
->errmsg
, args
->errmsg_len
);
51 if (rv
!= CMD_SUCCESS
)
52 return NB_ERR_INCONSISTENCY
;
57 int lib_route_map_entry_set_destroy(struct nb_cb_destroy_args
*args
)
59 struct routemap_hook_context
*rhc
;
62 if (args
->event
!= NB_EV_APPLY
)
65 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
66 if (rhc
->rhc_shook
== NULL
)
69 rv
= rhc
->rhc_shook(rhc
->rhc_rmi
, rhc
->rhc_rule
, NULL
,
70 args
->errmsg
, args
->errmsg_len
);
71 if (rv
!= CMD_SUCCESS
)
72 return NB_ERR_INCONSISTENCY
;
78 * Auxiliary hook context list manipulation functions.
80 struct routemap_hook_context
*
81 routemap_hook_context_insert(struct route_map_index
*rmi
)
83 struct routemap_hook_context
*rhc
;
85 rhc
= XCALLOC(MTYPE_TMP
, sizeof(*rhc
));
87 TAILQ_INSERT_TAIL(&rmi
->rhclist
, rhc
, rhc_entry
);
92 void routemap_hook_context_free(struct routemap_hook_context
*rhc
)
94 struct route_map_index
*rmi
= rhc
->rhc_rmi
;
96 TAILQ_REMOVE(&rmi
->rhclist
, rhc
, rhc_entry
);
97 XFREE(MTYPE_TMP
, rhc
);
101 * XPath: /frr-route-map:lib/route-map
103 static int lib_route_map_create(struct nb_cb_create_args
*args
)
105 struct route_map
*rm
;
108 switch (args
->event
) {
115 rm_name
= yang_dnode_get_string(args
->dnode
, "./name");
116 rm
= route_map_get(rm_name
);
117 nb_running_set_entry(args
->dnode
, rm
);
124 static int lib_route_map_destroy(struct nb_cb_destroy_args
*args
)
126 struct route_map
*rm
;
128 switch (args
->event
) {
135 rm
= nb_running_unset_entry(args
->dnode
);
136 route_map_delete(rm
);
144 * XPath: /frr-route-map:lib/route-map/optimization-disabled
147 lib_route_map_optimization_disabled_modify(struct nb_cb_modify_args
*args
)
149 struct route_map
*rm
;
150 bool disabled
= yang_dnode_get_bool(args
->dnode
, NULL
);
152 switch (args
->event
) {
159 rm
= nb_running_get_entry(args
->dnode
, NULL
, true);
160 rm
->optimization_disabled
= disabled
;
168 * XPath: /frr-route-map:lib/route-map/entry
170 static int lib_route_map_entry_create(struct nb_cb_create_args
*args
)
172 struct route_map_index
*rmi
;
173 struct route_map
*rm
;
177 switch (args
->event
) {
184 sequence
= yang_dnode_get_uint16(args
->dnode
, "./sequence");
185 action
= yang_dnode_get_enum(args
->dnode
, "./action") == 0
188 rm
= nb_running_get_entry(args
->dnode
, NULL
, true);
189 rmi
= route_map_index_get(rm
, action
, sequence
);
190 nb_running_set_entry(args
->dnode
, rmi
);
197 static int lib_route_map_entry_destroy(struct nb_cb_destroy_args
*args
)
199 struct route_map_index
*rmi
;
201 switch (args
->event
) {
208 rmi
= nb_running_unset_entry(args
->dnode
);
209 route_map_index_delete(rmi
, 1);
217 * XPath: /frr-route-map:lib/route-map/entry/description
220 lib_route_map_entry_description_modify(struct nb_cb_modify_args
*args
)
222 struct route_map_index
*rmi
;
223 const char *description
;
225 switch (args
->event
) {
230 description
= yang_dnode_get_string(args
->dnode
, NULL
);
231 args
->resource
->ptr
= XSTRDUP(MTYPE_TMP
, description
);
232 if (args
->resource
->ptr
== NULL
)
233 return NB_ERR_RESOURCE
;
236 XFREE(MTYPE_TMP
, args
->resource
->ptr
);
239 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
240 XFREE(MTYPE_TMP
, rmi
->description
);
241 rmi
->description
= args
->resource
->ptr
;
249 lib_route_map_entry_description_destroy(struct nb_cb_destroy_args
*args
)
251 struct route_map_index
*rmi
;
253 switch (args
->event
) {
260 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
261 XFREE(MTYPE_TMP
, rmi
->description
);
269 * XPath: /frr-route-map:lib/route-map/entry/action
271 static int lib_route_map_entry_action_modify(struct nb_cb_modify_args
*args
)
273 struct route_map_index
*rmi
;
274 struct route_map
*map
;
276 switch (args
->event
) {
283 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
284 rmi
->type
= yang_dnode_get_enum(args
->dnode
, NULL
);
287 /* Execute event hook. */
288 if (route_map_master
.event_hook
) {
289 (*route_map_master
.event_hook
)(map
->name
);
290 route_map_notify_dependencies(map
->name
,
291 RMAP_EVENT_CALL_ADDED
);
301 * XPath: /frr-route-map:lib/route-map/entry/call
303 static int lib_route_map_entry_call_modify(struct nb_cb_modify_args
*args
)
305 struct route_map_index
*rmi
;
306 const char *rm_name
, *rmn_name
;
308 switch (args
->event
) {
310 rm_name
= yang_dnode_get_string(args
->dnode
, "../../name");
311 rmn_name
= yang_dnode_get_string(args
->dnode
, NULL
);
312 /* Don't allow to jump to the same route map instance. */
313 if (strcmp(rm_name
, rmn_name
) == 0)
314 return NB_ERR_VALIDATION
;
316 /* TODO: detect circular route map sequences. */
319 rmn_name
= yang_dnode_get_string(args
->dnode
, NULL
);
320 args
->resource
->ptr
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmn_name
);
323 XFREE(MTYPE_ROUTE_MAP_NAME
, args
->resource
->ptr
);
326 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
328 route_map_upd8_dependency(RMAP_EVENT_CALL_DELETED
,
329 rmi
->nextrm
, rmi
->map
->name
);
330 XFREE(MTYPE_ROUTE_MAP_NAME
, rmi
->nextrm
);
332 rmi
->nextrm
= args
->resource
->ptr
;
333 route_map_upd8_dependency(RMAP_EVENT_CALL_ADDED
, rmi
->nextrm
,
341 static int lib_route_map_entry_call_destroy(struct nb_cb_destroy_args
*args
)
343 struct route_map_index
*rmi
;
345 switch (args
->event
) {
352 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
353 route_map_upd8_dependency(RMAP_EVENT_CALL_DELETED
, rmi
->nextrm
,
355 XFREE(MTYPE_ROUTE_MAP_NAME
, rmi
->nextrm
);
364 * XPath: /frr-route-map:lib/route-map/entry/exit-policy
367 lib_route_map_entry_exit_policy_modify(struct nb_cb_modify_args
*args
)
369 struct route_map_index
*rmi
;
373 switch (args
->event
) {
375 policy
= yang_dnode_get_enum(args
->dnode
, NULL
);
377 case 0: /* permit-or-deny */
383 yang_dnode_get_enum(args
->dnode
, "../action");
384 if (rm_action
== 1 /* deny */) {
386 * On deny it is not possible to 'goto'
389 return NB_ERR_VALIDATION
;
398 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
399 policy
= yang_dnode_get_enum(args
->dnode
, NULL
);
402 case 0: /* permit-or-deny */
403 rmi
->exitpolicy
= RMAP_EXIT
;
406 rmi
->exitpolicy
= RMAP_NEXT
;
409 rmi
->exitpolicy
= RMAP_GOTO
;
419 * XPath: /frr-route-map:lib/route-map/entry/goto-value
421 static int lib_route_map_entry_goto_value_modify(struct nb_cb_modify_args
*args
)
423 struct route_map_index
*rmi
;
427 switch (args
->event
) {
429 rmi_index
= yang_dnode_get_uint16(args
->dnode
, "../sequence");
430 rmi_next
= yang_dnode_get_uint16(args
->dnode
, NULL
);
431 if (rmi_next
<= rmi_index
) {
432 /* Can't jump backwards on a route map. */
433 return NB_ERR_VALIDATION
;
441 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
442 rmi
->nextpref
= yang_dnode_get_uint16(args
->dnode
, NULL
);
450 lib_route_map_entry_goto_value_destroy(struct nb_cb_destroy_args
*args
)
452 struct route_map_index
*rmi
;
454 switch (args
->event
) {
461 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
470 * XPath: /frr-route-map:lib/route-map/entry/match-condition
473 lib_route_map_entry_match_condition_create(struct nb_cb_create_args
*args
)
475 struct routemap_hook_context
*rhc
;
476 struct route_map_index
*rmi
;
478 switch (args
->event
) {
485 rmi
= nb_running_get_entry(args
->dnode
, NULL
, true);
486 rhc
= routemap_hook_context_insert(rmi
);
487 nb_running_set_entry(args
->dnode
, rhc
);
495 lib_route_map_entry_match_condition_destroy(struct nb_cb_destroy_args
*args
)
497 struct routemap_hook_context
*rhc
;
500 if (args
->event
!= NB_EV_APPLY
)
503 rv
= lib_route_map_entry_match_destroy(args
);
504 rhc
= nb_running_unset_entry(args
->dnode
);
505 routemap_hook_context_free(rhc
);
511 * XPath: /frr-route-map:lib/route-map/entry/match-condition/interface
513 static int lib_route_map_entry_match_condition_interface_modify(
514 struct nb_cb_modify_args
*args
)
516 struct routemap_hook_context
*rhc
;
520 if (args
->event
!= NB_EV_APPLY
)
523 /* Check for hook function. */
524 if (rmap_match_set_hook
.match_interface
== NULL
)
527 /* Add configuration. */
528 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
529 ifname
= yang_dnode_get_string(args
->dnode
, NULL
);
531 /* Set destroy information. */
532 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_interface
;
533 rhc
->rhc_rule
= "interface";
534 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
536 rv
= rmap_match_set_hook
.match_interface(rhc
->rhc_rmi
,
538 RMAP_EVENT_MATCH_ADDED
,
539 args
->errmsg
, args
->errmsg_len
);
540 if (rv
!= CMD_SUCCESS
) {
541 rhc
->rhc_mhook
= NULL
;
542 return NB_ERR_INCONSISTENCY
;
548 static int lib_route_map_entry_match_condition_interface_destroy(
549 struct nb_cb_destroy_args
*args
)
551 return lib_route_map_entry_match_destroy(args
);
555 * XPath: /frr-route-map:lib/route-map/entry/match-condition/list-name
557 static int lib_route_map_entry_match_condition_list_name_modify(
558 struct nb_cb_modify_args
*args
)
560 struct routemap_hook_context
*rhc
;
562 const char *condition
;
565 if (args
->event
!= NB_EV_APPLY
)
568 /* Check for hook installation, otherwise we can just stop. */
569 acl
= yang_dnode_get_string(args
->dnode
, NULL
);
570 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
571 condition
= yang_dnode_get_string(args
->dnode
, "../../condition");
573 if (IS_MATCH_IPv4_ADDRESS_LIST(condition
)) {
574 if (rmap_match_set_hook
.match_ip_address
== NULL
)
576 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ip_address
;
577 rhc
->rhc_rule
= "ip address";
578 rhc
->rhc_event
= RMAP_EVENT_FILTER_DELETED
;
579 rv
= rmap_match_set_hook
.match_ip_address(
580 rhc
->rhc_rmi
, "ip address", acl
,
581 RMAP_EVENT_FILTER_ADDED
,
582 args
->errmsg
, args
->errmsg_len
);
583 } else if (IS_MATCH_IPv4_PREFIX_LIST(condition
)) {
584 if (rmap_match_set_hook
.match_ip_address_prefix_list
== NULL
)
587 rmap_match_set_hook
.no_match_ip_address_prefix_list
;
588 rhc
->rhc_rule
= "ip address prefix-list";
589 rhc
->rhc_event
= RMAP_EVENT_PLIST_DELETED
;
590 rv
= rmap_match_set_hook
.match_ip_address_prefix_list(
591 rhc
->rhc_rmi
, "ip address prefix-list", acl
,
592 RMAP_EVENT_PLIST_ADDED
,
593 args
->errmsg
, args
->errmsg_len
);
594 } else if (IS_MATCH_IPv4_NEXTHOP_LIST(condition
)) {
595 if (rmap_match_set_hook
.match_ip_next_hop
== NULL
)
597 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ip_next_hop
;
598 rhc
->rhc_rule
= "ip next-hop";
599 rhc
->rhc_event
= RMAP_EVENT_FILTER_DELETED
;
600 rv
= rmap_match_set_hook
.match_ip_next_hop(
601 rhc
->rhc_rmi
, "ip next-hop", acl
,
602 RMAP_EVENT_FILTER_ADDED
,
603 args
->errmsg
, args
->errmsg_len
);
604 } else if (IS_MATCH_IPv6_NEXTHOP_LIST(condition
)) {
605 if (rmap_match_set_hook
.match_ipv6_next_hop
== NULL
)
607 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ipv6_next_hop
;
608 rhc
->rhc_rule
= "ipv6 next-hop";
609 rhc
->rhc_event
= RMAP_EVENT_FILTER_DELETED
;
610 rv
= rmap_match_set_hook
.match_ipv6_next_hop(
611 rhc
->rhc_rmi
, "ipv6 next-hop", acl
,
612 RMAP_EVENT_FILTER_ADDED
, args
->errmsg
,
614 } else if (IS_MATCH_IPv4_NEXTHOP_PREFIX_LIST(condition
)) {
615 if (rmap_match_set_hook
.match_ip_next_hop_prefix_list
== NULL
)
618 rmap_match_set_hook
.no_match_ip_next_hop_prefix_list
;
619 rhc
->rhc_rule
= "ip next-hop prefix-list";
620 rhc
->rhc_event
= RMAP_EVENT_PLIST_DELETED
;
621 rv
= rmap_match_set_hook
.match_ip_next_hop_prefix_list(
622 rhc
->rhc_rmi
, "ip next-hop prefix-list", acl
,
623 RMAP_EVENT_PLIST_ADDED
,
624 args
->errmsg
, args
->errmsg_len
);
625 } else if (IS_MATCH_IPv6_ADDRESS_LIST(condition
)) {
626 if (rmap_match_set_hook
.match_ipv6_address
== NULL
)
628 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ipv6_address
;
629 rhc
->rhc_rule
= "ipv6 address";
630 rhc
->rhc_event
= RMAP_EVENT_FILTER_DELETED
;
631 rv
= rmap_match_set_hook
.match_ipv6_address(
632 rhc
->rhc_rmi
, "ipv6 address", acl
,
633 RMAP_EVENT_FILTER_ADDED
,
634 args
->errmsg
, args
->errmsg_len
);
635 } else if (IS_MATCH_IPv6_PREFIX_LIST(condition
)) {
636 if (rmap_match_set_hook
.match_ipv6_address_prefix_list
== NULL
)
639 rmap_match_set_hook
.no_match_ipv6_address_prefix_list
;
640 rhc
->rhc_rule
= "ipv6 address prefix-list";
641 rhc
->rhc_event
= RMAP_EVENT_PLIST_DELETED
;
642 rv
= rmap_match_set_hook
.match_ipv6_address_prefix_list(
643 rhc
->rhc_rmi
, "ipv6 address prefix-list", acl
,
644 RMAP_EVENT_PLIST_ADDED
,
645 args
->errmsg
, args
->errmsg_len
);
647 rv
= CMD_ERR_NO_MATCH
;
649 if (rv
!= CMD_SUCCESS
) {
650 rhc
->rhc_mhook
= NULL
;
651 return NB_ERR_INCONSISTENCY
;
657 static int lib_route_map_entry_match_condition_list_name_destroy(
658 struct nb_cb_destroy_args
*args
)
660 return lib_route_map_entry_match_destroy(args
);
664 * XPath: /frr-route-map:lib/route-map/entry/match-condition/ipv4-next-hop-type
666 static int lib_route_map_entry_match_condition_ipv4_next_hop_type_modify(
667 struct nb_cb_modify_args
*args
)
669 struct routemap_hook_context
*rhc
;
673 if (args
->event
!= NB_EV_APPLY
)
676 /* Check for hook function. */
677 if (rmap_match_set_hook
.match_ip_next_hop_type
== NULL
)
680 /* Add configuration. */
681 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
682 type
= yang_dnode_get_string(args
->dnode
, NULL
);
684 /* Set destroy information. */
685 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ip_next_hop_type
;
686 rhc
->rhc_rule
= "ip next-hop type";
687 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
689 rv
= rmap_match_set_hook
.match_ip_next_hop_type(
690 rhc
->rhc_rmi
, "ip next-hop type", type
,
691 RMAP_EVENT_MATCH_ADDED
,
692 args
->errmsg
, args
->errmsg_len
);
693 if (rv
!= CMD_SUCCESS
) {
694 rhc
->rhc_mhook
= NULL
;
695 return NB_ERR_INCONSISTENCY
;
701 static int lib_route_map_entry_match_condition_ipv4_next_hop_type_destroy(
702 struct nb_cb_destroy_args
*args
)
704 return lib_route_map_entry_match_destroy(args
);
708 * XPath: /frr-route-map:lib/route-map/entry/match-condition/ipv6-next-hop-type
710 static int lib_route_map_entry_match_condition_ipv6_next_hop_type_modify(
711 struct nb_cb_modify_args
*args
)
713 struct routemap_hook_context
*rhc
;
717 if (args
->event
!= NB_EV_APPLY
)
720 /* Check for hook function. */
721 if (rmap_match_set_hook
.match_ipv6_next_hop_type
== NULL
)
724 /* Add configuration. */
725 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
726 type
= yang_dnode_get_string(args
->dnode
, NULL
);
728 /* Set destroy information. */
729 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ipv6_next_hop_type
;
730 rhc
->rhc_rule
= "ipv6 next-hop type";
731 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
733 rv
= rmap_match_set_hook
.match_ipv6_next_hop_type(
734 rhc
->rhc_rmi
, "ipv6 next-hop type", type
,
735 RMAP_EVENT_MATCH_ADDED
,
736 args
->errmsg
, args
->errmsg_len
);
737 if (rv
!= CMD_SUCCESS
) {
738 rhc
->rhc_mhook
= NULL
;
739 return NB_ERR_INCONSISTENCY
;
745 static int lib_route_map_entry_match_condition_ipv6_next_hop_type_destroy(
746 struct nb_cb_destroy_args
*args
)
748 return lib_route_map_entry_match_destroy(args
);
752 * XPath: /frr-route-map:lib/route-map/entry/match-condition/metric
754 static int lib_route_map_entry_match_condition_metric_modify(
755 struct nb_cb_modify_args
*args
)
757 struct routemap_hook_context
*rhc
;
761 if (args
->event
!= NB_EV_APPLY
)
764 /* Check for hook function. */
765 if (rmap_match_set_hook
.match_metric
== NULL
)
768 /* Add configuration. */
769 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
770 type
= yang_dnode_get_string(args
->dnode
, NULL
);
772 /* Set destroy information. */
773 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_metric
;
774 rhc
->rhc_rule
= "metric";
775 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
777 rv
= rmap_match_set_hook
.match_metric(rhc
->rhc_rmi
, "metric",
778 type
, RMAP_EVENT_MATCH_ADDED
,
779 args
->errmsg
, args
->errmsg_len
);
780 if (rv
!= CMD_SUCCESS
) {
781 rhc
->rhc_mhook
= NULL
;
782 return NB_ERR_INCONSISTENCY
;
788 static int lib_route_map_entry_match_condition_metric_destroy(
789 struct nb_cb_destroy_args
*args
)
791 return lib_route_map_entry_match_destroy(args
);
795 * XPath: /frr-route-map:lib/route-map/entry/match-condition/tag
798 lib_route_map_entry_match_condition_tag_modify(struct nb_cb_modify_args
*args
)
800 struct routemap_hook_context
*rhc
;
804 if (args
->event
!= NB_EV_APPLY
)
807 /* Check for hook function. */
808 if (rmap_match_set_hook
.match_tag
== NULL
)
811 /* Add configuration. */
812 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
813 tag
= yang_dnode_get_string(args
->dnode
, NULL
);
815 /* Set destroy information. */
816 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_tag
;
817 rhc
->rhc_rule
= "tag";
818 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
820 rv
= rmap_match_set_hook
.match_tag(rhc
->rhc_rmi
, "tag", tag
,
821 RMAP_EVENT_MATCH_ADDED
,
822 args
->errmsg
, args
->errmsg_len
);
823 if (rv
!= CMD_SUCCESS
) {
824 rhc
->rhc_mhook
= NULL
;
825 return NB_ERR_INCONSISTENCY
;
832 lib_route_map_entry_match_condition_tag_destroy(struct nb_cb_destroy_args
*args
)
834 return lib_route_map_entry_match_destroy(args
);
838 * XPath: /frr-route-map:lib/route-map/entry/set-action
840 static int lib_route_map_entry_set_action_create(struct nb_cb_create_args
*args
)
842 return lib_route_map_entry_match_condition_create(args
);
846 lib_route_map_entry_set_action_destroy(struct nb_cb_destroy_args
*args
)
848 struct routemap_hook_context
*rhc
;
851 if (args
->event
!= NB_EV_APPLY
)
854 rv
= lib_route_map_entry_set_destroy(args
);
855 rhc
= nb_running_unset_entry(args
->dnode
);
856 routemap_hook_context_free(rhc
);
862 * XPath: /frr-route-map:lib/route-map/entry/set-action/ipv4-address
864 static int lib_route_map_entry_set_action_ipv4_address_modify(
865 struct nb_cb_modify_args
*args
)
867 struct routemap_hook_context
*rhc
;
872 switch (args
->event
) {
875 * NOTE: validate if 'action' is 'ipv4-next-hop',
876 * currently it is not necessary because this is the
877 * only implemented action.
879 yang_dnode_get_ipv4(&ia
, args
->dnode
, NULL
);
880 if (ia
.s_addr
== INADDR_ANY
|| IPV4_CLASS_DE(ntohl(ia
.s_addr
)))
881 return NB_ERR_VALIDATION
;
890 /* Check for hook function. */
891 if (rmap_match_set_hook
.set_ip_nexthop
== NULL
)
894 /* Add configuration. */
895 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
896 address
= yang_dnode_get_string(args
->dnode
, NULL
);
898 /* Set destroy information. */
899 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_ip_nexthop
;
900 rhc
->rhc_rule
= "ip next-hop";
902 rv
= rmap_match_set_hook
.set_ip_nexthop(rhc
->rhc_rmi
, "ip next-hop",
904 args
->errmsg
, args
->errmsg_len
);
905 if (rv
!= CMD_SUCCESS
) {
906 rhc
->rhc_shook
= NULL
;
907 return NB_ERR_INCONSISTENCY
;
913 static int lib_route_map_entry_set_action_ipv4_address_destroy(
914 struct nb_cb_destroy_args
*args
)
916 return lib_route_map_entry_set_destroy(args
);
920 * XPath: /frr-route-map:lib/route-map/entry/set-action/ipv6-address
922 static int lib_route_map_entry_set_action_ipv6_address_modify(
923 struct nb_cb_modify_args
*args
)
925 struct routemap_hook_context
*rhc
;
930 switch (args
->event
) {
933 * NOTE: validate if 'action' is 'ipv6-next-hop',
934 * currently it is not necessary because this is the
935 * only implemented action. Other actions might have
936 * different validations.
938 yang_dnode_get_ipv6(&i6a
, args
->dnode
, NULL
);
939 if (!IN6_IS_ADDR_LINKLOCAL(&i6a
))
940 return NB_ERR_VALIDATION
;
949 /* Check for hook function. */
950 if (rmap_match_set_hook
.set_ipv6_nexthop_local
== NULL
)
953 /* Add configuration. */
954 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
955 address
= yang_dnode_get_string(args
->dnode
, NULL
);
957 /* Set destroy information. */
958 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_ipv6_nexthop_local
;
959 rhc
->rhc_rule
= "ipv6 next-hop local";
961 rv
= rmap_match_set_hook
.set_ipv6_nexthop_local(
962 rhc
->rhc_rmi
, "ipv6 next-hop local", address
,
963 args
->errmsg
, args
->errmsg_len
);
964 if (rv
!= CMD_SUCCESS
) {
965 rhc
->rhc_shook
= NULL
;
966 return NB_ERR_INCONSISTENCY
;
972 static int lib_route_map_entry_set_action_ipv6_address_destroy(
973 struct nb_cb_destroy_args
*args
)
975 return lib_route_map_entry_set_destroy(args
);
979 * XPath: /frr-route-map:lib/route-map/entry/set-action/value
981 static int set_action_modify(enum nb_event event
, const struct lyd_node
*dnode
,
982 union nb_resource
*resource
, const char *value
,
983 char *errmsg
, size_t errmsg_len
)
985 struct routemap_hook_context
*rhc
;
989 * NOTE: validate if 'action' is 'metric', currently it is not
990 * necessary because this is the only implemented action. Other
991 * actions might have different validations.
993 if (event
!= NB_EV_APPLY
)
996 /* Check for hook function. */
997 if (rmap_match_set_hook
.set_metric
== NULL
)
1000 /* Add configuration. */
1001 rhc
= nb_running_get_entry(dnode
, NULL
, true);
1003 /* Set destroy information. */
1004 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_metric
;
1005 rhc
->rhc_rule
= "metric";
1007 rv
= rmap_match_set_hook
.set_metric(rhc
->rhc_rmi
, "metric",
1011 if (rv
!= CMD_SUCCESS
) {
1012 rhc
->rhc_shook
= NULL
;
1013 return NB_ERR_INCONSISTENCY
;
1020 lib_route_map_entry_set_action_value_modify(struct nb_cb_modify_args
*args
)
1022 const char *metric
= yang_dnode_get_string(args
->dnode
, NULL
);
1024 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1025 metric
, args
->errmsg
, args
->errmsg_len
);
1029 lib_route_map_entry_set_action_value_destroy(struct nb_cb_destroy_args
*args
)
1031 return lib_route_map_entry_set_destroy(args
);
1035 * XPath: /frr-route-map:lib/route-map/entry/set-action/add-metric
1038 lib_route_map_entry_set_action_add_metric_modify(struct nb_cb_modify_args
*args
)
1040 char metric_str
[16];
1042 if (args
->event
== NB_EV_VALIDATE
1043 && yang_dnode_get_uint32(args
->dnode
, NULL
) == 0) {
1044 snprintf(args
->errmsg
, args
->errmsg_len
,
1045 "Can't add zero to metric");
1046 return NB_ERR_VALIDATION
;
1049 snprintf(metric_str
, sizeof(metric_str
), "+%s",
1050 yang_dnode_get_string(args
->dnode
, NULL
));
1051 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1053 args
->errmsg
, args
->errmsg_len
);
1056 static int lib_route_map_entry_set_action_add_metric_destroy(
1057 struct nb_cb_destroy_args
*args
)
1059 return lib_route_map_entry_set_action_value_destroy(args
);
1063 * XPath: /frr-route-map:lib/route-map/entry/set-action/subtract-metric
1065 static int lib_route_map_entry_set_action_subtract_metric_modify(
1066 struct nb_cb_modify_args
*args
)
1068 char metric_str
[16];
1070 if (args
->event
== NB_EV_VALIDATE
1071 && yang_dnode_get_uint32(args
->dnode
, NULL
) == 0) {
1072 snprintf(args
->errmsg
, args
->errmsg_len
,
1073 "Can't subtract zero from metric");
1074 return NB_ERR_VALIDATION
;
1077 snprintf(metric_str
, sizeof(metric_str
), "-%s",
1078 yang_dnode_get_string(args
->dnode
, NULL
));
1079 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1081 args
->errmsg
, args
->errmsg_len
);
1084 static int lib_route_map_entry_set_action_subtract_metric_destroy(
1085 struct nb_cb_destroy_args
*args
)
1087 return lib_route_map_entry_set_action_value_destroy(args
);
1091 * XPath: /frr-route-map:lib/route-map/entry/set-action/use-round-trip-time
1093 static int lib_route_map_entry_set_action_use_round_trip_time_modify(
1094 struct nb_cb_modify_args
*args
)
1096 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1098 args
->errmsg
, args
->errmsg_len
);
1101 static int lib_route_map_entry_set_action_use_round_trip_time_destroy(
1102 struct nb_cb_destroy_args
*args
)
1104 return lib_route_map_entry_set_action_value_destroy(args
);
1108 * XPath: /frr-route-map:lib/route-map/entry/set-action/add-round-trip-time
1110 static int lib_route_map_entry_set_action_add_round_trip_time_modify(
1111 struct nb_cb_modify_args
*args
)
1113 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1115 args
->errmsg
, args
->errmsg_len
);
1118 static int lib_route_map_entry_set_action_add_round_trip_time_destroy(
1119 struct nb_cb_destroy_args
*args
)
1121 return lib_route_map_entry_set_action_value_destroy(args
);
1125 * XPath: /frr-route-map:lib/route-map/entry/set-action/subtract-round-trip-time
1127 static int lib_route_map_entry_set_action_subtract_round_trip_time_modify(
1128 struct nb_cb_modify_args
*args
)
1130 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1131 "-rtt", args
->errmsg
, args
->errmsg_len
);
1134 static int lib_route_map_entry_set_action_subtract_round_trip_time_destroy(
1135 struct nb_cb_destroy_args
*args
)
1137 return lib_route_map_entry_set_action_value_destroy(args
);
1141 * XPath: /frr-route-map:lib/route-map/entry/set-action/tag
1144 lib_route_map_entry_set_action_tag_modify(struct nb_cb_modify_args
*args
)
1146 struct routemap_hook_context
*rhc
;
1151 * NOTE: validate if 'action' is 'tag', currently it is not
1152 * necessary because this is the only implemented action. Other
1153 * actions might have different validations.
1155 if (args
->event
!= NB_EV_APPLY
)
1158 /* Check for hook function. */
1159 if (rmap_match_set_hook
.set_tag
== NULL
)
1162 /* Add configuration. */
1163 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
1164 tag
= yang_dnode_get_string(args
->dnode
, NULL
);
1166 /* Set destroy information. */
1167 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_tag
;
1168 rhc
->rhc_rule
= "tag";
1170 rv
= rmap_match_set_hook
.set_tag(rhc
->rhc_rmi
, "tag", tag
,
1171 args
->errmsg
, args
->errmsg_len
);
1172 if (rv
!= CMD_SUCCESS
) {
1173 rhc
->rhc_shook
= NULL
;
1174 return NB_ERR_INCONSISTENCY
;
1181 lib_route_map_entry_set_action_tag_destroy(struct nb_cb_destroy_args
*args
)
1183 return lib_route_map_entry_set_destroy(args
);
1187 * XPath: /frr-route-map:lib/route-map/entry/set-action/policy
1190 lib_route_map_entry_set_action_policy_modify(struct nb_cb_modify_args
*args
)
1192 struct routemap_hook_context
*rhc
;
1197 * NOTE: validate if 'action' is 'tag', currently it is not
1198 * necessary because this is the only implemented action. Other
1199 * actions might have different validations.
1201 if (args
->event
!= NB_EV_APPLY
)
1204 /* Check for hook function. */
1205 if (rmap_match_set_hook
.set_srte_color
== NULL
)
1208 /* Add configuration. */
1209 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
1210 policy
= yang_dnode_get_string(args
->dnode
, NULL
);
1212 /* Set destroy information. */
1213 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_tag
;
1214 rhc
->rhc_rule
= "sr-te color";
1216 rv
= rmap_match_set_hook
.set_tag(rhc
->rhc_rmi
, "sr-te color", policy
,
1217 args
->errmsg
, args
->errmsg_len
);
1218 if (rv
!= CMD_SUCCESS
) {
1219 rhc
->rhc_shook
= NULL
;
1220 return NB_ERR_INCONSISTENCY
;
1227 lib_route_map_entry_set_action_policy_destroy(struct nb_cb_destroy_args
*args
)
1229 return lib_route_map_entry_set_destroy(args
);
1232 /* clang-format off */
1233 const struct frr_yang_module_info frr_route_map_info
= {
1234 .name
= "frr-route-map",
1237 .xpath
= "/frr-route-map:lib/route-map",
1239 .create
= lib_route_map_create
,
1240 .destroy
= lib_route_map_destroy
,
1244 .xpath
= "/frr-route-map:lib/route-map/optimization-disabled",
1246 .modify
= lib_route_map_optimization_disabled_modify
,
1247 .cli_show
= route_map_optimization_disabled_show
,
1251 .xpath
= "/frr-route-map:lib/route-map/entry",
1253 .create
= lib_route_map_entry_create
,
1254 .destroy
= lib_route_map_entry_destroy
,
1255 .cli_cmp
= route_map_instance_cmp
,
1256 .cli_show
= route_map_instance_show
,
1257 .cli_show_end
= route_map_instance_show_end
,
1261 .xpath
= "/frr-route-map:lib/route-map/entry/description",
1263 .modify
= lib_route_map_entry_description_modify
,
1264 .destroy
= lib_route_map_entry_description_destroy
,
1265 .cli_show
= route_map_description_show
,
1269 .xpath
= "/frr-route-map:lib/route-map/entry/action",
1271 .modify
= lib_route_map_entry_action_modify
,
1275 .xpath
= "/frr-route-map:lib/route-map/entry/call",
1277 .modify
= lib_route_map_entry_call_modify
,
1278 .destroy
= lib_route_map_entry_call_destroy
,
1279 .cli_show
= route_map_call_show
,
1283 .xpath
= "/frr-route-map:lib/route-map/entry/exit-policy",
1285 .modify
= lib_route_map_entry_exit_policy_modify
,
1286 .cli_show
= route_map_exit_policy_show
,
1290 .xpath
= "/frr-route-map:lib/route-map/entry/goto-value",
1292 .modify
= lib_route_map_entry_goto_value_modify
,
1293 .destroy
= lib_route_map_entry_goto_value_destroy
,
1297 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition",
1299 .create
= lib_route_map_entry_match_condition_create
,
1300 .destroy
= lib_route_map_entry_match_condition_destroy
,
1301 .cli_show
= route_map_condition_show
,
1305 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/interface",
1307 .modify
= lib_route_map_entry_match_condition_interface_modify
,
1308 .destroy
= lib_route_map_entry_match_condition_interface_destroy
,
1312 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/list-name",
1314 .modify
= lib_route_map_entry_match_condition_list_name_modify
,
1315 .destroy
= lib_route_map_entry_match_condition_list_name_destroy
,
1319 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/ipv4-next-hop-type",
1321 .modify
= lib_route_map_entry_match_condition_ipv4_next_hop_type_modify
,
1322 .destroy
= lib_route_map_entry_match_condition_ipv4_next_hop_type_destroy
,
1326 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/ipv6-next-hop-type",
1328 .modify
= lib_route_map_entry_match_condition_ipv6_next_hop_type_modify
,
1329 .destroy
= lib_route_map_entry_match_condition_ipv6_next_hop_type_destroy
,
1333 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/metric",
1335 .modify
= lib_route_map_entry_match_condition_metric_modify
,
1336 .destroy
= lib_route_map_entry_match_condition_metric_destroy
,
1340 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/tag",
1342 .modify
= lib_route_map_entry_match_condition_tag_modify
,
1343 .destroy
= lib_route_map_entry_match_condition_tag_destroy
,
1347 .xpath
= "/frr-route-map:lib/route-map/entry/set-action",
1349 .create
= lib_route_map_entry_set_action_create
,
1350 .destroy
= lib_route_map_entry_set_action_destroy
,
1351 .cli_show
= route_map_action_show
,
1355 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/ipv4-address",
1357 .modify
= lib_route_map_entry_set_action_ipv4_address_modify
,
1358 .destroy
= lib_route_map_entry_set_action_ipv4_address_destroy
,
1362 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/ipv6-address",
1364 .modify
= lib_route_map_entry_set_action_ipv6_address_modify
,
1365 .destroy
= lib_route_map_entry_set_action_ipv6_address_destroy
,
1369 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/value",
1371 .modify
= lib_route_map_entry_set_action_value_modify
,
1372 .destroy
= lib_route_map_entry_set_action_value_destroy
,
1376 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/add-metric",
1378 .modify
= lib_route_map_entry_set_action_add_metric_modify
,
1379 .destroy
= lib_route_map_entry_set_action_add_metric_destroy
,
1383 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/subtract-metric",
1385 .modify
= lib_route_map_entry_set_action_subtract_metric_modify
,
1386 .destroy
= lib_route_map_entry_set_action_subtract_metric_destroy
,
1390 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/use-round-trip-time",
1392 .modify
= lib_route_map_entry_set_action_use_round_trip_time_modify
,
1393 .destroy
= lib_route_map_entry_set_action_use_round_trip_time_destroy
,
1397 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/add-round-trip-time",
1399 .modify
= lib_route_map_entry_set_action_add_round_trip_time_modify
,
1400 .destroy
= lib_route_map_entry_set_action_add_round_trip_time_destroy
,
1404 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/subtract-round-trip-time",
1406 .modify
= lib_route_map_entry_set_action_subtract_round_trip_time_modify
,
1407 .destroy
= lib_route_map_entry_set_action_subtract_round_trip_time_destroy
,
1411 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/tag",
1413 .modify
= lib_route_map_entry_set_action_tag_modify
,
1414 .destroy
= lib_route_map_entry_set_action_tag_destroy
,
1418 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/policy",
1420 .modify
= lib_route_map_entry_set_action_policy_modify
,
1421 .destroy
= lib_route_map_entry_set_action_policy_destroy
,