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_IPv4_NEXTHOP_PREFIX_LIST(condition
)) {
605 if (rmap_match_set_hook
.match_ip_next_hop_prefix_list
== NULL
)
608 rmap_match_set_hook
.no_match_ip_next_hop_prefix_list
;
609 rhc
->rhc_rule
= "ip next-hop prefix-list";
610 rhc
->rhc_event
= RMAP_EVENT_PLIST_DELETED
;
611 rv
= rmap_match_set_hook
.match_ip_next_hop_prefix_list(
612 rhc
->rhc_rmi
, "ip next-hop prefix-list", acl
,
613 RMAP_EVENT_PLIST_ADDED
,
614 args
->errmsg
, args
->errmsg_len
);
615 } else if (IS_MATCH_IPv6_ADDRESS_LIST(condition
)) {
616 if (rmap_match_set_hook
.match_ipv6_address
== NULL
)
618 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ipv6_address
;
619 rhc
->rhc_rule
= "ipv6 address";
620 rhc
->rhc_event
= RMAP_EVENT_FILTER_DELETED
;
621 rv
= rmap_match_set_hook
.match_ipv6_address(
622 rhc
->rhc_rmi
, "ipv6 address", acl
,
623 RMAP_EVENT_FILTER_ADDED
,
624 args
->errmsg
, args
->errmsg_len
);
625 } else if (IS_MATCH_IPv6_PREFIX_LIST(condition
)) {
626 if (rmap_match_set_hook
.match_ipv6_address_prefix_list
== NULL
)
629 rmap_match_set_hook
.no_match_ipv6_address_prefix_list
;
630 rhc
->rhc_rule
= "ipv6 address prefix-list";
631 rhc
->rhc_event
= RMAP_EVENT_PLIST_DELETED
;
632 rv
= rmap_match_set_hook
.match_ipv6_address_prefix_list(
633 rhc
->rhc_rmi
, "ipv6 address prefix-list", acl
,
634 RMAP_EVENT_PLIST_ADDED
,
635 args
->errmsg
, args
->errmsg_len
);
637 rv
= CMD_ERR_NO_MATCH
;
639 if (rv
!= CMD_SUCCESS
) {
640 rhc
->rhc_mhook
= NULL
;
641 return NB_ERR_INCONSISTENCY
;
647 static int lib_route_map_entry_match_condition_list_name_destroy(
648 struct nb_cb_destroy_args
*args
)
650 return lib_route_map_entry_match_destroy(args
);
654 * XPath: /frr-route-map:lib/route-map/entry/match-condition/ipv4-next-hop-type
656 static int lib_route_map_entry_match_condition_ipv4_next_hop_type_modify(
657 struct nb_cb_modify_args
*args
)
659 struct routemap_hook_context
*rhc
;
663 if (args
->event
!= NB_EV_APPLY
)
666 /* Check for hook function. */
667 if (rmap_match_set_hook
.match_ip_next_hop_type
== NULL
)
670 /* Add configuration. */
671 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
672 type
= yang_dnode_get_string(args
->dnode
, NULL
);
674 /* Set destroy information. */
675 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ip_next_hop_type
;
676 rhc
->rhc_rule
= "ip next-hop type";
677 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
679 rv
= rmap_match_set_hook
.match_ip_next_hop_type(
680 rhc
->rhc_rmi
, "ip next-hop type", type
,
681 RMAP_EVENT_MATCH_ADDED
,
682 args
->errmsg
, args
->errmsg_len
);
683 if (rv
!= CMD_SUCCESS
) {
684 rhc
->rhc_mhook
= NULL
;
685 return NB_ERR_INCONSISTENCY
;
691 static int lib_route_map_entry_match_condition_ipv4_next_hop_type_destroy(
692 struct nb_cb_destroy_args
*args
)
694 return lib_route_map_entry_match_destroy(args
);
698 * XPath: /frr-route-map:lib/route-map/entry/match-condition/ipv6-next-hop-type
700 static int lib_route_map_entry_match_condition_ipv6_next_hop_type_modify(
701 struct nb_cb_modify_args
*args
)
703 struct routemap_hook_context
*rhc
;
707 if (args
->event
!= NB_EV_APPLY
)
710 /* Check for hook function. */
711 if (rmap_match_set_hook
.match_ipv6_next_hop_type
== NULL
)
714 /* Add configuration. */
715 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
716 type
= yang_dnode_get_string(args
->dnode
, NULL
);
718 /* Set destroy information. */
719 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_ipv6_next_hop_type
;
720 rhc
->rhc_rule
= "ipv6 next-hop type";
721 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
723 rv
= rmap_match_set_hook
.match_ipv6_next_hop_type(
724 rhc
->rhc_rmi
, "ipv6 next-hop type", type
,
725 RMAP_EVENT_MATCH_ADDED
,
726 args
->errmsg
, args
->errmsg_len
);
727 if (rv
!= CMD_SUCCESS
) {
728 rhc
->rhc_mhook
= NULL
;
729 return NB_ERR_INCONSISTENCY
;
735 static int lib_route_map_entry_match_condition_ipv6_next_hop_type_destroy(
736 struct nb_cb_destroy_args
*args
)
738 return lib_route_map_entry_match_destroy(args
);
742 * XPath: /frr-route-map:lib/route-map/entry/match-condition/metric
744 static int lib_route_map_entry_match_condition_metric_modify(
745 struct nb_cb_modify_args
*args
)
747 struct routemap_hook_context
*rhc
;
751 if (args
->event
!= NB_EV_APPLY
)
754 /* Check for hook function. */
755 if (rmap_match_set_hook
.match_metric
== NULL
)
758 /* Add configuration. */
759 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
760 type
= yang_dnode_get_string(args
->dnode
, NULL
);
762 /* Set destroy information. */
763 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_metric
;
764 rhc
->rhc_rule
= "metric";
765 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
767 rv
= rmap_match_set_hook
.match_metric(rhc
->rhc_rmi
, "metric",
768 type
, RMAP_EVENT_MATCH_ADDED
,
769 args
->errmsg
, args
->errmsg_len
);
770 if (rv
!= CMD_SUCCESS
) {
771 rhc
->rhc_mhook
= NULL
;
772 return NB_ERR_INCONSISTENCY
;
778 static int lib_route_map_entry_match_condition_metric_destroy(
779 struct nb_cb_destroy_args
*args
)
781 return lib_route_map_entry_match_destroy(args
);
785 * XPath: /frr-route-map:lib/route-map/entry/match-condition/tag
788 lib_route_map_entry_match_condition_tag_modify(struct nb_cb_modify_args
*args
)
790 struct routemap_hook_context
*rhc
;
794 if (args
->event
!= NB_EV_APPLY
)
797 /* Check for hook function. */
798 if (rmap_match_set_hook
.match_tag
== NULL
)
801 /* Add configuration. */
802 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
803 tag
= yang_dnode_get_string(args
->dnode
, NULL
);
805 /* Set destroy information. */
806 rhc
->rhc_mhook
= rmap_match_set_hook
.no_match_tag
;
807 rhc
->rhc_rule
= "tag";
808 rhc
->rhc_event
= RMAP_EVENT_MATCH_DELETED
;
810 rv
= rmap_match_set_hook
.match_tag(rhc
->rhc_rmi
, "tag", tag
,
811 RMAP_EVENT_MATCH_ADDED
,
812 args
->errmsg
, args
->errmsg_len
);
813 if (rv
!= CMD_SUCCESS
) {
814 rhc
->rhc_mhook
= NULL
;
815 return NB_ERR_INCONSISTENCY
;
822 lib_route_map_entry_match_condition_tag_destroy(struct nb_cb_destroy_args
*args
)
824 return lib_route_map_entry_match_destroy(args
);
828 * XPath: /frr-route-map:lib/route-map/entry/set-action
830 static int lib_route_map_entry_set_action_create(struct nb_cb_create_args
*args
)
832 return lib_route_map_entry_match_condition_create(args
);
836 lib_route_map_entry_set_action_destroy(struct nb_cb_destroy_args
*args
)
838 struct routemap_hook_context
*rhc
;
841 if (args
->event
!= NB_EV_APPLY
)
844 rv
= lib_route_map_entry_set_destroy(args
);
845 rhc
= nb_running_unset_entry(args
->dnode
);
846 routemap_hook_context_free(rhc
);
852 * XPath: /frr-route-map:lib/route-map/entry/set-action/ipv4-address
854 static int lib_route_map_entry_set_action_ipv4_address_modify(
855 struct nb_cb_modify_args
*args
)
857 struct routemap_hook_context
*rhc
;
862 switch (args
->event
) {
865 * NOTE: validate if 'action' is 'ipv4-next-hop',
866 * currently it is not necessary because this is the
867 * only implemented action.
869 yang_dnode_get_ipv4(&ia
, args
->dnode
, NULL
);
870 if (ia
.s_addr
== INADDR_ANY
|| IPV4_CLASS_DE(ntohl(ia
.s_addr
)))
871 return NB_ERR_VALIDATION
;
880 /* Check for hook function. */
881 if (rmap_match_set_hook
.set_ip_nexthop
== NULL
)
884 /* Add configuration. */
885 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
886 address
= yang_dnode_get_string(args
->dnode
, NULL
);
888 /* Set destroy information. */
889 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_ip_nexthop
;
890 rhc
->rhc_rule
= "ip next-hop";
892 rv
= rmap_match_set_hook
.set_ip_nexthop(rhc
->rhc_rmi
, "ip next-hop",
894 args
->errmsg
, args
->errmsg_len
);
895 if (rv
!= CMD_SUCCESS
) {
896 rhc
->rhc_shook
= NULL
;
897 return NB_ERR_INCONSISTENCY
;
903 static int lib_route_map_entry_set_action_ipv4_address_destroy(
904 struct nb_cb_destroy_args
*args
)
906 return lib_route_map_entry_set_destroy(args
);
910 * XPath: /frr-route-map:lib/route-map/entry/set-action/ipv6-address
912 static int lib_route_map_entry_set_action_ipv6_address_modify(
913 struct nb_cb_modify_args
*args
)
915 struct routemap_hook_context
*rhc
;
920 switch (args
->event
) {
923 * NOTE: validate if 'action' is 'ipv6-next-hop',
924 * currently it is not necessary because this is the
925 * only implemented action. Other actions might have
926 * different validations.
928 yang_dnode_get_ipv6(&i6a
, args
->dnode
, NULL
);
929 if (!IN6_IS_ADDR_LINKLOCAL(&i6a
))
930 return NB_ERR_VALIDATION
;
939 /* Check for hook function. */
940 if (rmap_match_set_hook
.set_ipv6_nexthop_local
== NULL
)
943 /* Add configuration. */
944 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
945 address
= yang_dnode_get_string(args
->dnode
, NULL
);
947 /* Set destroy information. */
948 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_ipv6_nexthop_local
;
949 rhc
->rhc_rule
= "ipv6 next-hop local";
951 rv
= rmap_match_set_hook
.set_ipv6_nexthop_local(
952 rhc
->rhc_rmi
, "ipv6 next-hop local", address
,
953 args
->errmsg
, args
->errmsg_len
);
954 if (rv
!= CMD_SUCCESS
) {
955 rhc
->rhc_shook
= NULL
;
956 return NB_ERR_INCONSISTENCY
;
962 static int lib_route_map_entry_set_action_ipv6_address_destroy(
963 struct nb_cb_destroy_args
*args
)
965 return lib_route_map_entry_set_destroy(args
);
969 * XPath: /frr-route-map:lib/route-map/entry/set-action/value
971 static int set_action_modify(enum nb_event event
, const struct lyd_node
*dnode
,
972 union nb_resource
*resource
, const char *value
,
973 char *errmsg
, size_t errmsg_len
)
975 struct routemap_hook_context
*rhc
;
979 * NOTE: validate if 'action' is 'metric', currently it is not
980 * necessary because this is the only implemented action. Other
981 * actions might have different validations.
983 if (event
!= NB_EV_APPLY
)
986 /* Check for hook function. */
987 if (rmap_match_set_hook
.set_metric
== NULL
)
990 /* Add configuration. */
991 rhc
= nb_running_get_entry(dnode
, NULL
, true);
993 /* Set destroy information. */
994 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_metric
;
995 rhc
->rhc_rule
= "metric";
997 rv
= rmap_match_set_hook
.set_metric(rhc
->rhc_rmi
, "metric",
1001 if (rv
!= CMD_SUCCESS
) {
1002 rhc
->rhc_shook
= NULL
;
1003 return NB_ERR_INCONSISTENCY
;
1010 lib_route_map_entry_set_action_value_modify(struct nb_cb_modify_args
*args
)
1012 const char *metric
= yang_dnode_get_string(args
->dnode
, NULL
);
1014 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1015 metric
, args
->errmsg
, args
->errmsg_len
);
1019 lib_route_map_entry_set_action_value_destroy(struct nb_cb_destroy_args
*args
)
1021 return lib_route_map_entry_set_destroy(args
);
1025 * XPath: /frr-route-map:lib/route-map/entry/set-action/add-metric
1028 lib_route_map_entry_set_action_add_metric_modify(struct nb_cb_modify_args
*args
)
1030 char metric_str
[16];
1032 if (args
->event
== NB_EV_VALIDATE
1033 && yang_dnode_get_uint32(args
->dnode
, NULL
) == 0) {
1034 snprintf(args
->errmsg
, args
->errmsg_len
,
1035 "Can't add zero to metric");
1036 return NB_ERR_VALIDATION
;
1039 snprintf(metric_str
, sizeof(metric_str
), "+%s",
1040 yang_dnode_get_string(args
->dnode
, NULL
));
1041 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1043 args
->errmsg
, args
->errmsg_len
);
1046 static int lib_route_map_entry_set_action_add_metric_destroy(
1047 struct nb_cb_destroy_args
*args
)
1049 return lib_route_map_entry_set_action_value_destroy(args
);
1053 * XPath: /frr-route-map:lib/route-map/entry/set-action/subtract-metric
1055 static int lib_route_map_entry_set_action_subtract_metric_modify(
1056 struct nb_cb_modify_args
*args
)
1058 char metric_str
[16];
1060 if (args
->event
== NB_EV_VALIDATE
1061 && yang_dnode_get_uint32(args
->dnode
, NULL
) == 0) {
1062 snprintf(args
->errmsg
, args
->errmsg_len
,
1063 "Can't subtract zero from metric");
1064 return NB_ERR_VALIDATION
;
1067 snprintf(metric_str
, sizeof(metric_str
), "-%s",
1068 yang_dnode_get_string(args
->dnode
, NULL
));
1069 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1071 args
->errmsg
, args
->errmsg_len
);
1074 static int lib_route_map_entry_set_action_subtract_metric_destroy(
1075 struct nb_cb_destroy_args
*args
)
1077 return lib_route_map_entry_set_action_value_destroy(args
);
1081 * XPath: /frr-route-map:lib/route-map/entry/set-action/use-round-trip-time
1083 static int lib_route_map_entry_set_action_use_round_trip_time_modify(
1084 struct nb_cb_modify_args
*args
)
1086 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1088 args
->errmsg
, args
->errmsg_len
);
1091 static int lib_route_map_entry_set_action_use_round_trip_time_destroy(
1092 struct nb_cb_destroy_args
*args
)
1094 return lib_route_map_entry_set_action_value_destroy(args
);
1098 * XPath: /frr-route-map:lib/route-map/entry/set-action/add-round-trip-time
1100 static int lib_route_map_entry_set_action_add_round_trip_time_modify(
1101 struct nb_cb_modify_args
*args
)
1103 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1105 args
->errmsg
, args
->errmsg_len
);
1108 static int lib_route_map_entry_set_action_add_round_trip_time_destroy(
1109 struct nb_cb_destroy_args
*args
)
1111 return lib_route_map_entry_set_action_value_destroy(args
);
1115 * XPath: /frr-route-map:lib/route-map/entry/set-action/subtract-round-trip-time
1117 static int lib_route_map_entry_set_action_subtract_round_trip_time_modify(
1118 struct nb_cb_modify_args
*args
)
1120 return set_action_modify(args
->event
, args
->dnode
, args
->resource
,
1121 "-rtt", args
->errmsg
, args
->errmsg_len
);
1124 static int lib_route_map_entry_set_action_subtract_round_trip_time_destroy(
1125 struct nb_cb_destroy_args
*args
)
1127 return lib_route_map_entry_set_action_value_destroy(args
);
1131 * XPath: /frr-route-map:lib/route-map/entry/set-action/tag
1134 lib_route_map_entry_set_action_tag_modify(struct nb_cb_modify_args
*args
)
1136 struct routemap_hook_context
*rhc
;
1141 * NOTE: validate if 'action' is 'tag', currently it is not
1142 * necessary because this is the only implemented action. Other
1143 * actions might have different validations.
1145 if (args
->event
!= NB_EV_APPLY
)
1148 /* Check for hook function. */
1149 if (rmap_match_set_hook
.set_tag
== NULL
)
1152 /* Add configuration. */
1153 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
1154 tag
= yang_dnode_get_string(args
->dnode
, NULL
);
1156 /* Set destroy information. */
1157 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_tag
;
1158 rhc
->rhc_rule
= "tag";
1160 rv
= rmap_match_set_hook
.set_tag(rhc
->rhc_rmi
, "tag", tag
,
1161 args
->errmsg
, args
->errmsg_len
);
1162 if (rv
!= CMD_SUCCESS
) {
1163 rhc
->rhc_shook
= NULL
;
1164 return NB_ERR_INCONSISTENCY
;
1171 lib_route_map_entry_set_action_tag_destroy(struct nb_cb_destroy_args
*args
)
1173 return lib_route_map_entry_set_destroy(args
);
1177 * XPath: /frr-route-map:lib/route-map/entry/set-action/policy
1180 lib_route_map_entry_set_action_policy_modify(struct nb_cb_modify_args
*args
)
1182 struct routemap_hook_context
*rhc
;
1187 * NOTE: validate if 'action' is 'tag', currently it is not
1188 * necessary because this is the only implemented action. Other
1189 * actions might have different validations.
1191 if (args
->event
!= NB_EV_APPLY
)
1194 /* Check for hook function. */
1195 if (rmap_match_set_hook
.set_srte_color
== NULL
)
1198 /* Add configuration. */
1199 rhc
= nb_running_get_entry(args
->dnode
, NULL
, true);
1200 policy
= yang_dnode_get_string(args
->dnode
, NULL
);
1202 /* Set destroy information. */
1203 rhc
->rhc_shook
= rmap_match_set_hook
.no_set_tag
;
1204 rhc
->rhc_rule
= "sr-te color";
1206 rv
= rmap_match_set_hook
.set_tag(rhc
->rhc_rmi
, "sr-te color", policy
,
1207 args
->errmsg
, args
->errmsg_len
);
1208 if (rv
!= CMD_SUCCESS
) {
1209 rhc
->rhc_shook
= NULL
;
1210 return NB_ERR_INCONSISTENCY
;
1217 lib_route_map_entry_set_action_policy_destroy(struct nb_cb_destroy_args
*args
)
1219 return lib_route_map_entry_set_destroy(args
);
1222 /* clang-format off */
1223 const struct frr_yang_module_info frr_route_map_info
= {
1224 .name
= "frr-route-map",
1227 .xpath
= "/frr-route-map:lib/route-map",
1229 .create
= lib_route_map_create
,
1230 .destroy
= lib_route_map_destroy
,
1234 .xpath
= "/frr-route-map:lib/route-map/optimization-disabled",
1236 .modify
= lib_route_map_optimization_disabled_modify
,
1237 .cli_show
= route_map_optimization_disabled_show
,
1241 .xpath
= "/frr-route-map:lib/route-map/entry",
1243 .create
= lib_route_map_entry_create
,
1244 .destroy
= lib_route_map_entry_destroy
,
1245 .cli_cmp
= route_map_instance_cmp
,
1246 .cli_show
= route_map_instance_show
,
1247 .cli_show_end
= route_map_instance_show_end
,
1251 .xpath
= "/frr-route-map:lib/route-map/entry/description",
1253 .modify
= lib_route_map_entry_description_modify
,
1254 .destroy
= lib_route_map_entry_description_destroy
,
1255 .cli_show
= route_map_description_show
,
1259 .xpath
= "/frr-route-map:lib/route-map/entry/action",
1261 .modify
= lib_route_map_entry_action_modify
,
1265 .xpath
= "/frr-route-map:lib/route-map/entry/call",
1267 .modify
= lib_route_map_entry_call_modify
,
1268 .destroy
= lib_route_map_entry_call_destroy
,
1269 .cli_show
= route_map_call_show
,
1273 .xpath
= "/frr-route-map:lib/route-map/entry/exit-policy",
1275 .modify
= lib_route_map_entry_exit_policy_modify
,
1276 .cli_show
= route_map_exit_policy_show
,
1280 .xpath
= "/frr-route-map:lib/route-map/entry/goto-value",
1282 .modify
= lib_route_map_entry_goto_value_modify
,
1283 .destroy
= lib_route_map_entry_goto_value_destroy
,
1287 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition",
1289 .create
= lib_route_map_entry_match_condition_create
,
1290 .destroy
= lib_route_map_entry_match_condition_destroy
,
1291 .cli_show
= route_map_condition_show
,
1295 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/interface",
1297 .modify
= lib_route_map_entry_match_condition_interface_modify
,
1298 .destroy
= lib_route_map_entry_match_condition_interface_destroy
,
1302 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/list-name",
1304 .modify
= lib_route_map_entry_match_condition_list_name_modify
,
1305 .destroy
= lib_route_map_entry_match_condition_list_name_destroy
,
1309 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/ipv4-next-hop-type",
1311 .modify
= lib_route_map_entry_match_condition_ipv4_next_hop_type_modify
,
1312 .destroy
= lib_route_map_entry_match_condition_ipv4_next_hop_type_destroy
,
1316 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/ipv6-next-hop-type",
1318 .modify
= lib_route_map_entry_match_condition_ipv6_next_hop_type_modify
,
1319 .destroy
= lib_route_map_entry_match_condition_ipv6_next_hop_type_destroy
,
1323 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/metric",
1325 .modify
= lib_route_map_entry_match_condition_metric_modify
,
1326 .destroy
= lib_route_map_entry_match_condition_metric_destroy
,
1330 .xpath
= "/frr-route-map:lib/route-map/entry/match-condition/rmap-match-condition/tag",
1332 .modify
= lib_route_map_entry_match_condition_tag_modify
,
1333 .destroy
= lib_route_map_entry_match_condition_tag_destroy
,
1337 .xpath
= "/frr-route-map:lib/route-map/entry/set-action",
1339 .create
= lib_route_map_entry_set_action_create
,
1340 .destroy
= lib_route_map_entry_set_action_destroy
,
1341 .cli_show
= route_map_action_show
,
1345 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/ipv4-address",
1347 .modify
= lib_route_map_entry_set_action_ipv4_address_modify
,
1348 .destroy
= lib_route_map_entry_set_action_ipv4_address_destroy
,
1352 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/ipv6-address",
1354 .modify
= lib_route_map_entry_set_action_ipv6_address_modify
,
1355 .destroy
= lib_route_map_entry_set_action_ipv6_address_destroy
,
1359 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/value",
1361 .modify
= lib_route_map_entry_set_action_value_modify
,
1362 .destroy
= lib_route_map_entry_set_action_value_destroy
,
1366 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/add-metric",
1368 .modify
= lib_route_map_entry_set_action_add_metric_modify
,
1369 .destroy
= lib_route_map_entry_set_action_add_metric_destroy
,
1373 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/subtract-metric",
1375 .modify
= lib_route_map_entry_set_action_subtract_metric_modify
,
1376 .destroy
= lib_route_map_entry_set_action_subtract_metric_destroy
,
1380 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/use-round-trip-time",
1382 .modify
= lib_route_map_entry_set_action_use_round_trip_time_modify
,
1383 .destroy
= lib_route_map_entry_set_action_use_round_trip_time_destroy
,
1387 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/add-round-trip-time",
1389 .modify
= lib_route_map_entry_set_action_add_round_trip_time_modify
,
1390 .destroy
= lib_route_map_entry_set_action_add_round_trip_time_destroy
,
1394 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/subtract-round-trip-time",
1396 .modify
= lib_route_map_entry_set_action_subtract_round_trip_time_modify
,
1397 .destroy
= lib_route_map_entry_set_action_subtract_round_trip_time_destroy
,
1401 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/tag",
1403 .modify
= lib_route_map_entry_set_action_tag_modify
,
1404 .destroy
= lib_route_map_entry_set_action_tag_destroy
,
1408 .xpath
= "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/policy",
1410 .modify
= lib_route_map_entry_set_action_policy_modify
,
1411 .destroy
= lib_route_map_entry_set_action_policy_destroy
,