1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
41 #include "lib_errors.h"
46 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient");
47 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs");
49 /* Zebra client events. */
50 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
52 /* Prototype for event manager. */
53 static void zclient_event(enum event
, struct zclient
*);
55 static void zebra_interface_if_set_value(struct stream
*s
,
56 struct interface
*ifp
);
58 struct zclient_options zclient_options_default
= {.receive_notify
= false,
59 .synchronous
= false};
61 struct sockaddr_storage zclient_addr
;
62 socklen_t zclient_addr_len
;
64 /* This file local debug flag. */
65 static int zclient_debug
;
67 /* Allocate zclient structure. */
68 struct zclient
*zclient_new(struct thread_master
*master
,
69 struct zclient_options
*opt
)
71 struct zclient
*zclient
;
73 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
75 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
77 zclient
->ibuf
= stream_new(stream_size
);
78 zclient
->obuf
= stream_new(stream_size
);
79 zclient
->wb
= buffer_new(0);
80 zclient
->master
= master
;
82 zclient
->receive_notify
= opt
->receive_notify
;
83 zclient
->synchronous
= opt
->synchronous
;
88 /* This function is only called when exiting, because
89 many parts of the code do not check for I/O errors, so they could
90 reference an invalid pointer if the structure was ever freed.
92 Free zclient structure. */
93 void zclient_free(struct zclient
*zclient
)
96 stream_free(zclient
->ibuf
);
98 stream_free(zclient
->obuf
);
100 buffer_free(zclient
->wb
);
102 XFREE(MTYPE_ZCLIENT
, zclient
);
105 unsigned short *redist_check_instance(struct redist_proto
*red
,
106 unsigned short instance
)
108 struct listnode
*node
;
114 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
121 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
128 red
->instances
= list_new();
130 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
132 listnode_add(red
->instances
, in
);
135 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
139 id
= redist_check_instance(red
, instance
);
143 listnode_delete(red
->instances
, id
);
144 XFREE(MTYPE_REDIST_INST
, id
);
145 if (!red
->instances
->count
) {
147 list_delete(&red
->instances
);
151 void redist_del_all_instances(struct redist_proto
*red
)
153 struct listnode
*ln
, *nn
;
159 for (ALL_LIST_ELEMENTS(red
->instances
, ln
, nn
, id
))
160 redist_del_instance(red
, *id
);
163 /* Stop zebra client services. */
164 void zclient_stop(struct zclient
*zclient
)
170 zlog_debug("zclient %p stopped", zclient
);
173 THREAD_OFF(zclient
->t_read
);
174 THREAD_OFF(zclient
->t_connect
);
175 THREAD_OFF(zclient
->t_write
);
178 stream_reset(zclient
->ibuf
);
179 stream_reset(zclient
->obuf
);
181 /* Empty the write buffer. */
182 buffer_reset(zclient
->wb
);
185 if (zclient
->sock
>= 0) {
186 close(zclient
->sock
);
191 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
192 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
193 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
194 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
197 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
200 vrf_bitmap_free(zclient
->default_information
[afi
]);
201 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
205 void zclient_reset(struct zclient
*zclient
)
209 zclient_stop(zclient
);
211 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
213 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
216 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
221 * Connect to zebra daemon.
222 * @param zclient a pointer to zclient structure
223 * @return socket fd just to make sure that connection established
227 int zclient_socket_connect(struct zclient
*zclient
)
232 /* We should think about IPv6 connection. */
233 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
238 setsockopt_so_sendbuf(sock
, 1048576);
240 /* Connect to zebra. */
241 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
244 zlog_debug("%s connect failure: %d(%s)", __func__
,
245 errno
, safe_strerror(errno
));
250 zclient
->sock
= sock
;
254 static enum zclient_send_status
zclient_failed(struct zclient
*zclient
)
257 zclient_stop(zclient
);
258 zclient_event(ZCLIENT_CONNECT
, zclient
);
259 return ZCLIENT_SEND_FAILURE
;
262 static int zclient_flush_data(struct thread
*thread
)
264 struct zclient
*zclient
= THREAD_ARG(thread
);
266 zclient
->t_write
= NULL
;
267 if (zclient
->sock
< 0)
269 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
273 "%s: buffer_flush_available failed on zclient fd %d, closing",
274 __func__
, zclient
->sock
);
275 return zclient_failed(zclient
);
277 zclient
->t_write
= NULL
;
278 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
279 zclient
->sock
, &zclient
->t_write
);
282 if (zclient
->zebra_buffer_write_ready
)
283 (*zclient
->zebra_buffer_write_ready
)();
291 * ZCLIENT_SEND_FAILED - is a failure
292 * ZCLIENT_SEND_SUCCESS - means we sent data to zebra
293 * ZCLIENT_SEND_BUFFERED - means we are buffering
295 enum zclient_send_status
zclient_send_message(struct zclient
*zclient
)
297 if (zclient
->sock
< 0)
298 return ZCLIENT_SEND_FAILURE
;
299 switch (buffer_write(zclient
->wb
, zclient
->sock
,
300 STREAM_DATA(zclient
->obuf
),
301 stream_get_endp(zclient
->obuf
))) {
303 flog_err(EC_LIB_ZAPI_SOCKET
,
304 "%s: buffer_write failed to zclient fd %d, closing",
305 __func__
, zclient
->sock
);
306 return zclient_failed(zclient
);
308 THREAD_OFF(zclient
->t_write
);
309 return ZCLIENT_SEND_SUCCESS
;
311 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
312 zclient
->sock
, &zclient
->t_write
);
313 return ZCLIENT_SEND_BUFFERED
;
316 /* should not get here */
317 return ZCLIENT_SEND_SUCCESS
;
321 * If we add more data to this structure please ensure that
322 * struct zmsghdr in lib/zclient.h is updated as appropriate.
324 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
326 /* length placeholder, caller can update */
327 stream_putw(s
, ZEBRA_HEADER_SIZE
);
328 stream_putc(s
, ZEBRA_HEADER_MARKER
);
329 stream_putc(s
, ZSERV_VERSION
);
330 stream_putl(s
, vrf_id
);
331 stream_putw(s
, command
);
334 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
335 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
338 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
341 STREAM_GETW(s
, *size
);
342 *size
-= ZEBRA_HEADER_SIZE
;
343 STREAM_GETC(s
, *marker
);
344 STREAM_GETC(s
, *version
);
345 STREAM_GETL(s
, *vrf_id
);
346 STREAM_GETW(s
, *cmd
);
348 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
350 EC_LIB_ZAPI_MISSMATCH
,
351 "%s: socket %d version mismatch, marker %d, version %d",
352 __func__
, sock
, *marker
, *version
);
356 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
364 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
366 STREAM_GETW(zmsg
, hdr
->length
);
367 STREAM_GETC(zmsg
, hdr
->marker
);
368 STREAM_GETC(zmsg
, hdr
->version
);
369 STREAM_GETL(zmsg
, hdr
->vrf_id
);
370 STREAM_GETW(zmsg
, hdr
->command
);
376 /* Send simple Zebra message. */
377 static enum zclient_send_status
zebra_message_send(struct zclient
*zclient
,
378 int command
, vrf_id_t vrf_id
)
382 /* Get zclient output buffer. */
386 /* Send very simple command only Zebra message. */
387 zclient_create_header(s
, command
, vrf_id
);
389 return zclient_send_message(zclient
);
392 enum zclient_send_status
zclient_send_hello(struct zclient
*zclient
)
396 if (zclient
->redist_default
|| zclient
->synchronous
) {
400 /* The VRF ID in the HELLO message is always 0. */
401 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
402 stream_putc(s
, zclient
->redist_default
);
403 stream_putw(s
, zclient
->instance
);
404 stream_putl(s
, zclient
->session_id
);
405 if (zclient
->receive_notify
)
409 if (zclient
->synchronous
)
414 stream_putw_at(s
, 0, stream_get_endp(s
));
415 return zclient_send_message(zclient
);
418 return ZCLIENT_SEND_SUCCESS
;
421 enum zclient_send_status
zclient_send_vrf_label(struct zclient
*zclient
,
422 vrf_id_t vrf_id
, afi_t afi
,
424 enum lsp_types_t ltype
)
431 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
432 stream_putl(s
, label
);
434 stream_putc(s
, ltype
);
435 stream_putw_at(s
, 0, stream_get_endp(s
));
436 return zclient_send_message(zclient
);
439 enum zclient_send_status
zclient_send_localsid(struct zclient
*zclient
,
440 const struct in6_addr
*sid
, ifindex_t oif
,
441 enum seg6local_action_t action
,
442 const struct seg6local_context
*context
)
444 struct prefix_ipv6 p
= {};
445 struct zapi_route api
= {};
446 struct nexthop nh
= {};
449 p
.prefixlen
= IPV6_MAX_BITLEN
;
452 api
.vrf_id
= VRF_DEFAULT
;
453 api
.type
= ZEBRA_ROUTE_BGP
;
455 api
.safi
= SAFI_UNICAST
;
456 memcpy(&api
.prefix
, &p
, sizeof(p
));
458 if (action
== ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
459 return zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
461 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
462 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
464 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
466 SET_FLAG(nh
.flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
);
467 nexthop_add_srv6_seg6local(&nh
, action
, context
);
469 zapi_nexthop_from_nexthop(&api
.nexthops
[0], &nh
);
472 return zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
475 /* Send register requests to zebra daemon for the information in a VRF. */
476 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
481 /* If not connected to the zebra yet. */
482 if (zclient
->sock
< 0)
486 zlog_debug("%s: send register messages for VRF %u", __func__
,
489 /* We need router-id information. */
490 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_ADD
, AFI_IP
,
493 /* We need interface information. */
494 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
496 /* Set unwanted redistribute route. */
497 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
498 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
501 /* Flush all redistribute request. */
502 if (vrf_id
== VRF_DEFAULT
) {
503 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
504 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
505 if (!zclient
->mi_redist
[afi
][i
].enabled
)
508 struct listnode
*node
;
511 for (ALL_LIST_ELEMENTS_RO(
512 zclient
->mi_redist
[afi
][i
]
515 if (!(i
== zclient
->redist_default
516 && *id
== zclient
->instance
))
517 zebra_redistribute_send(
518 ZEBRA_REDISTRIBUTE_ADD
,
519 zclient
, afi
, i
, *id
,
525 /* Resend all redistribute request. */
526 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
527 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
528 if (i
!= zclient
->redist_default
529 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
531 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
535 /* If default information is needed. */
536 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
537 zebra_redistribute_default_send(
538 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
543 /* Send unregister requests to zebra daemon for the information in a VRF. */
544 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
549 /* If not connected to the zebra yet. */
550 if (zclient
->sock
< 0)
554 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
557 /* We need router-id information. */
558 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_DELETE
, AFI_IP
,
561 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
563 /* Set unwanted redistribute route. */
564 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
565 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
568 /* Flush all redistribute request. */
569 if (vrf_id
== VRF_DEFAULT
) {
570 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
571 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
572 if (!zclient
->mi_redist
[afi
][i
].enabled
)
575 struct listnode
*node
;
578 for (ALL_LIST_ELEMENTS_RO(
579 zclient
->mi_redist
[afi
][i
]
582 if (!(i
== zclient
->redist_default
583 && *id
== zclient
->instance
))
584 zebra_redistribute_send(
585 ZEBRA_REDISTRIBUTE_DELETE
,
586 zclient
, afi
, i
, *id
,
592 /* Flush all redistribute request. */
593 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
594 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
595 if (i
!= zclient
->redist_default
596 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
598 zebra_redistribute_send(
599 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
602 /* If default information is needed. */
603 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
604 zebra_redistribute_default_send(
605 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
610 enum zclient_send_status
611 zclient_send_router_id_update(struct zclient
*zclient
,
612 zebra_message_types_t type
, afi_t afi
,
615 struct stream
*s
= zclient
->obuf
;
617 zclient_create_header(s
, type
, vrf_id
);
619 stream_putw_at(s
, 0, stream_get_endp(s
));
620 return zclient_send_message(zclient
);
623 /* Send request to zebra daemon to start or stop RA. */
624 enum zclient_send_status
625 zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
626 struct interface
*ifp
, int enable
,
627 uint32_t ra_interval
)
631 /* If not connected to the zebra yet. */
632 if (zclient
->sock
< 0)
633 return ZCLIENT_SEND_FAILURE
;
635 /* Form and send message. */
640 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
642 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
644 stream_putl(s
, ifp
->ifindex
);
645 stream_putl(s
, ra_interval
);
647 stream_putw_at(s
, 0, stream_get_endp(s
));
649 return zclient_send_message(zclient
);
652 enum zclient_send_status
653 zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
654 struct interface
*ifp
, bool down
)
658 if (zclient
->sock
< 0)
659 return ZCLIENT_SEND_FAILURE
;
663 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
664 stream_putl(s
, ifp
->ifindex
);
665 stream_putc(s
, !!down
);
666 stream_putw_at(s
, 0, stream_get_endp(s
));
667 return zclient_send_message(zclient
);
670 /* Make connection to zebra daemon. */
671 int zclient_start(struct zclient
*zclient
)
674 zlog_info("zclient_start is called");
676 /* If already connected to the zebra. */
677 if (zclient
->sock
>= 0)
680 /* Check connect thread. */
681 if (zclient
->t_connect
)
684 if (zclient_socket_connect(zclient
) < 0) {
686 zlog_debug("zclient connection fail");
688 zclient_event(ZCLIENT_CONNECT
, zclient
);
692 if (set_nonblocking(zclient
->sock
) < 0)
693 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
694 __func__
, zclient
->sock
);
696 /* Clear fail count. */
699 zlog_debug("zclient connect success with socket [%d]",
702 /* Create read thread. */
703 zclient_event(ZCLIENT_READ
, zclient
);
705 zclient_send_hello(zclient
);
707 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
709 /* Inform the successful connection. */
710 if (zclient
->zebra_connected
)
711 (*zclient
->zebra_connected
)(zclient
);
716 /* Initialize zebra client. Argument redist_default is unwanted
717 redistribute route type. */
718 void zclient_init(struct zclient
*zclient
, int redist_default
,
719 unsigned short instance
, struct zebra_privs_t
*privs
)
723 /* Set -1 to the default socket value. */
725 zclient
->privs
= privs
;
727 /* Clear redistribution flags. */
728 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
729 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
730 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
732 /* Set unwanted redistribute route. bgpd does not need BGP route
734 zclient
->redist_default
= redist_default
;
735 zclient
->instance
= instance
;
736 /* Pending: make afi(s) an arg. */
737 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
738 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
741 /* Set default-information redistribute to zero. */
742 zclient
->default_information
[afi
] = vrf_bitmap_init();
746 zlog_debug("scheduling zclient connection");
748 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
751 /* This function is a wrapper function for calling zclient_start from
752 timer or event thread. */
753 static int zclient_connect(struct thread
*t
)
755 struct zclient
*zclient
;
757 zclient
= THREAD_ARG(t
);
758 zclient
->t_connect
= NULL
;
761 zlog_debug("zclient_connect is called");
763 return zclient_start(zclient
);
766 enum zclient_send_status
zclient_send_rnh(struct zclient
*zclient
, int command
,
767 const struct prefix
*p
,
769 bool resolve_via_def
, vrf_id_t vrf_id
)
775 zclient_create_header(s
, command
, vrf_id
);
776 stream_putc(s
, (connected
) ? 1 : 0);
777 stream_putc(s
, (resolve_via_def
) ? 1 : 0);
778 stream_putw(s
, SAFI_UNICAST
);
779 stream_putw(s
, PREFIX_FAMILY(p
));
780 stream_putc(s
, p
->prefixlen
);
781 switch (PREFIX_FAMILY(p
)) {
783 stream_put_in_addr(s
, &p
->u
.prefix4
);
786 stream_put(s
, &(p
->u
.prefix6
), 16);
791 stream_putw_at(s
, 0, stream_get_endp(s
));
793 return zclient_send_message(zclient
);
797 * "xdr_encode"-like interface that allows daemon (client) to send
798 * a message to zebra server for a route that needs to be
799 * added/deleted to the kernel. Info about the route is specified
800 * by the caller in a struct zapi_route. zapi_route_encode() then writes
801 * the info down the zclient socket using the stream_* functions.
803 * The corresponding read ("xdr_decode") function on the server
804 * side is zapi_route_decode().
806 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
809 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as a 4
812 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
814 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
816 * XXX: No attention paid to alignment.
818 enum zclient_send_status
819 zclient_route_send(uint8_t cmd
, struct zclient
*zclient
, struct zapi_route
*api
)
821 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
822 return ZCLIENT_SEND_FAILURE
;
823 return zclient_send_message(zclient
);
826 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
827 const struct zapi_nexthop
*next2
)
829 if (next1
->label_num
> next2
->label_num
)
832 if (next1
->label_num
< next2
->label_num
)
835 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
838 static int zapi_nexthop_srv6_cmp(const struct zapi_nexthop
*next1
,
839 const struct zapi_nexthop
*next2
)
843 ret
= memcmp(&next1
->seg6_segs
, &next2
->seg6_segs
,
844 sizeof(struct in6_addr
));
848 if (next1
->seg6local_action
> next2
->seg6local_action
)
851 if (next1
->seg6local_action
< next2
->seg6local_action
)
854 return memcmp(&next1
->seg6local_ctx
, &next2
->seg6local_ctx
,
855 sizeof(struct seg6local_context
));
858 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
859 const struct zapi_nexthop
*next2
)
863 if (next1
->vrf_id
< next2
->vrf_id
)
866 if (next1
->vrf_id
> next2
->vrf_id
)
869 if (next1
->type
< next2
->type
)
872 if (next1
->type
> next2
->type
)
875 if (next1
->weight
< next2
->weight
)
878 if (next1
->weight
> next2
->weight
)
881 switch (next1
->type
) {
882 case NEXTHOP_TYPE_IPV4
:
883 case NEXTHOP_TYPE_IPV6
:
884 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
889 case NEXTHOP_TYPE_IPV4_IFINDEX
:
890 case NEXTHOP_TYPE_IPV6_IFINDEX
:
891 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
895 /* Intentional Fall-Through */
896 case NEXTHOP_TYPE_IFINDEX
:
897 if (next1
->ifindex
< next2
->ifindex
)
900 if (next1
->ifindex
> next2
->ifindex
)
903 case NEXTHOP_TYPE_BLACKHOLE
:
904 if (next1
->bh_type
< next2
->bh_type
)
907 if (next1
->bh_type
> next2
->bh_type
)
912 if (next1
->srte_color
< next2
->srte_color
)
914 if (next1
->srte_color
> next2
->srte_color
)
917 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) ||
918 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
920 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
921 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
924 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
925 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
928 if (next1
->backup_num
> 0 || next2
->backup_num
> 0) {
930 if (next1
->backup_num
< next2
->backup_num
)
933 if (next1
->backup_num
> next2
->backup_num
)
936 ret
= memcmp(next1
->backup_idx
,
937 next2
->backup_idx
, next1
->backup_num
);
946 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
950 const struct zapi_nexthop
*next1
= item1
;
951 const struct zapi_nexthop
*next2
= item2
;
953 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
957 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
961 ret
= zapi_nexthop_srv6_cmp(next1
, next2
);
966 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
967 uint16_t nexthop_num
)
969 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
974 * Encode a single zapi nexthop
976 int zapi_nexthop_encode(struct stream
*s
, const struct zapi_nexthop
*api_nh
,
977 uint32_t api_flags
, uint32_t api_message
)
980 int nh_flags
= api_nh
->flags
;
982 stream_putl(s
, api_nh
->vrf_id
);
983 stream_putc(s
, api_nh
->type
);
985 /* If needed, set 'labelled nexthop' flag */
986 if (api_nh
->label_num
> 0) {
987 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
989 /* Validate label count */
990 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
996 /* If present, set 'weight' flag before encoding flags */
998 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
);
1000 /* Note that we're only encoding a single octet */
1001 stream_putc(s
, nh_flags
);
1003 switch (api_nh
->type
) {
1004 case NEXTHOP_TYPE_BLACKHOLE
:
1005 stream_putc(s
, api_nh
->bh_type
);
1007 case NEXTHOP_TYPE_IPV4
:
1008 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1009 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1010 stream_putl(s
, api_nh
->ifindex
);
1012 case NEXTHOP_TYPE_IFINDEX
:
1013 stream_putl(s
, api_nh
->ifindex
);
1015 case NEXTHOP_TYPE_IPV6
:
1016 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1017 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1019 stream_putl(s
, api_nh
->ifindex
);
1023 /* We only encode labels if we have >0 - we use
1024 * the per-nexthop flag above to signal that the count
1025 * is present in the payload.
1027 if (api_nh
->label_num
> 0) {
1028 stream_putc(s
, api_nh
->label_num
);
1029 stream_put(s
, &api_nh
->labels
[0],
1030 api_nh
->label_num
* sizeof(mpls_label_t
));
1034 stream_putl(s
, api_nh
->weight
);
1036 /* Router MAC for EVPN routes. */
1037 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1038 stream_put(s
, &(api_nh
->rmac
),
1039 sizeof(struct ethaddr
));
1041 /* Color for Segment Routing TE. */
1042 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1043 stream_putl(s
, api_nh
->srte_color
);
1045 /* Index of backup nexthop */
1046 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1047 /* Validate backup count */
1048 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1053 stream_putc(s
, api_nh
->backup_num
);
1054 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1055 stream_putc(s
, api_nh
->backup_idx
[i
]);
1058 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)) {
1059 stream_putl(s
, api_nh
->seg6local_action
);
1060 stream_write(s
, &api_nh
->seg6local_ctx
,
1061 sizeof(struct seg6local_context
));
1064 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_SEG6
))
1065 stream_write(s
, &api_nh
->seg6_segs
,
1066 sizeof(struct in6_addr
));
1072 int zapi_srv6_locator_chunk_encode(struct stream
*s
,
1073 const struct srv6_locator_chunk
*c
)
1075 stream_putw(s
, strlen(c
->locator_name
));
1076 stream_put(s
, c
->locator_name
, strlen(c
->locator_name
));
1077 stream_putw(s
, c
->prefix
.prefixlen
);
1078 stream_put(s
, &c
->prefix
.prefix
, sizeof(c
->prefix
.prefix
));
1079 stream_putc(s
, c
->block_bits_length
);
1080 stream_putc(s
, c
->node_bits_length
);
1081 stream_putc(s
, c
->function_bits_length
);
1082 stream_putc(s
, c
->argument_bits_length
);
1086 int zapi_srv6_locator_chunk_decode(struct stream
*s
,
1087 struct srv6_locator_chunk
*c
)
1091 c
->prefix
.family
= AF_INET6
;
1093 STREAM_GETW(s
, len
);
1094 if (len
> SRV6_LOCNAME_SIZE
)
1095 goto stream_failure
;
1097 STREAM_GET(c
->locator_name
, s
, len
);
1098 STREAM_GETW(s
, c
->prefix
.prefixlen
);
1099 STREAM_GET(&c
->prefix
.prefix
, s
, sizeof(c
->prefix
.prefix
));
1100 STREAM_GETC(s
, c
->block_bits_length
);
1101 STREAM_GETC(s
, c
->node_bits_length
);
1102 STREAM_GETC(s
, c
->function_bits_length
);
1103 STREAM_GETC(s
, c
->argument_bits_length
);
1110 int zapi_srv6_locator_encode(struct stream
*s
, const struct srv6_locator
*l
)
1112 stream_putw(s
, strlen(l
->name
));
1113 stream_put(s
, l
->name
, strlen(l
->name
));
1114 stream_putw(s
, l
->prefix
.prefixlen
);
1115 stream_put(s
, &l
->prefix
.prefix
, sizeof(l
->prefix
.prefix
));
1119 int zapi_srv6_locator_decode(struct stream
*s
, struct srv6_locator
*l
)
1123 STREAM_GETW(s
, len
);
1124 if (len
> SRV6_LOCNAME_SIZE
)
1125 goto stream_failure
;
1127 STREAM_GET(l
->name
, s
, len
);
1128 STREAM_GETW(s
, l
->prefix
.prefixlen
);
1129 STREAM_GET(&l
->prefix
.prefix
, s
, sizeof(l
->prefix
.prefix
));
1130 l
->prefix
.family
= AF_INET6
;
1137 static int zapi_nhg_encode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1141 if (cmd
!= ZEBRA_NHG_DEL
&& cmd
!= ZEBRA_NHG_ADD
) {
1142 flog_err(EC_LIB_ZAPI_ENCODE
,
1143 "%s: Specified zapi NHG command (%d) doesn't exist",
1148 if (api_nhg
->nexthop_num
>= MULTIPATH_NUM
||
1149 api_nhg
->backup_nexthop_num
>= MULTIPATH_NUM
) {
1150 flog_err(EC_LIB_ZAPI_ENCODE
,
1151 "%s: zapi NHG encode with invalid input", __func__
);
1156 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1158 stream_putw(s
, api_nhg
->proto
);
1159 stream_putl(s
, api_nhg
->id
);
1161 if (cmd
== ZEBRA_NHG_ADD
) {
1163 zapi_nexthop_group_sort(api_nhg
->nexthops
,
1164 api_nhg
->nexthop_num
);
1166 stream_putw(s
, api_nhg
->nexthop_num
);
1168 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++)
1169 zapi_nexthop_encode(s
, &api_nhg
->nexthops
[i
], 0, 0);
1171 /* Backup nexthops */
1172 stream_putw(s
, api_nhg
->backup_nexthop_num
);
1174 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++)
1175 zapi_nexthop_encode(s
, &api_nhg
->backup_nexthops
[i
], 0,
1179 stream_putw_at(s
, 0, stream_get_endp(s
));
1184 enum zclient_send_status
zclient_nhg_send(struct zclient
*zclient
, int cmd
,
1185 struct zapi_nhg
*api_nhg
)
1187 api_nhg
->proto
= zclient
->redist_default
;
1189 if (zapi_nhg_encode(zclient
->obuf
, cmd
, api_nhg
))
1192 return zclient_send_message(zclient
);
1195 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
1197 struct zapi_nexthop
*api_nh
;
1202 zclient_create_header(s
, cmd
, api
->vrf_id
);
1204 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1205 flog_err(EC_LIB_ZAPI_ENCODE
,
1206 "%s: Specified route type (%u) is not a legal value",
1207 __func__
, api
->type
);
1210 stream_putc(s
, api
->type
);
1212 stream_putw(s
, api
->instance
);
1213 stream_putl(s
, api
->flags
);
1214 stream_putl(s
, api
->message
);
1216 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1217 flog_err(EC_LIB_ZAPI_ENCODE
,
1218 "%s: Specified route SAFI (%u) is not a legal value",
1219 __func__
, api
->safi
);
1222 stream_putc(s
, api
->safi
);
1224 /* Put prefix information. */
1225 stream_putc(s
, api
->prefix
.family
);
1226 psize
= PSIZE(api
->prefix
.prefixlen
);
1227 stream_putc(s
, api
->prefix
.prefixlen
);
1228 stream_write(s
, &api
->prefix
.u
.prefix
, psize
);
1230 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1231 psize
= PSIZE(api
->src_prefix
.prefixlen
);
1232 stream_putc(s
, api
->src_prefix
.prefixlen
);
1233 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
1236 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1237 stream_putl(s
, api
->nhgid
);
1240 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1241 /* limit the number of nexthops if necessary */
1242 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1245 "%s: prefix %pFX: can't encode %u nexthops (maximum is %u)",
1246 __func__
, &api
->prefix
, api
->nexthop_num
,
1251 /* We canonicalize the nexthops by sorting them; this allows
1252 * zebra to resolve the list of nexthops to a nexthop-group
1255 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
1257 stream_putw(s
, api
->nexthop_num
);
1259 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1260 api_nh
= &api
->nexthops
[i
];
1262 /* MPLS labels for BGP-LU or Segment Routing */
1263 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1266 "%s: prefix %pFX: can't encode %u labels (maximum is %u)",
1267 __func__
, &api
->prefix
,
1268 api_nh
->label_num
, MPLS_MAX_LABELS
);
1272 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1279 /* Backup nexthops */
1280 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1281 /* limit the number of nexthops if necessary */
1282 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1285 "%s: prefix %pFX: can't encode %u backup nexthops (maximum is %u)",
1286 __func__
, &api
->prefix
, api
->backup_nexthop_num
,
1291 /* Note that we do not sort the list of backup nexthops -
1292 * this list is treated as an array and indexed by each
1293 * primary nexthop that is associated with a backup.
1296 stream_putw(s
, api
->backup_nexthop_num
);
1298 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1299 api_nh
= &api
->backup_nexthops
[i
];
1301 /* MPLS labels for BGP-LU or Segment Routing */
1302 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1305 "%s: prefix %pFX: backup: can't encode %u labels (maximum is %u)",
1306 __func__
, &api
->prefix
,
1307 api_nh
->label_num
, MPLS_MAX_LABELS
);
1311 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1319 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1320 stream_putc(s
, api
->distance
);
1321 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1322 stream_putl(s
, api
->metric
);
1323 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1324 stream_putl(s
, api
->tag
);
1325 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1326 stream_putl(s
, api
->mtu
);
1327 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1328 stream_putl(s
, api
->tableid
);
1330 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_OPAQUE
)) {
1331 if (api
->opaque
.length
> ZAPI_MESSAGE_OPAQUE_LENGTH
) {
1334 "%s: opaque length %u is greater than allowed value",
1335 __func__
, api
->opaque
.length
);
1339 stream_putw(s
, api
->opaque
.length
);
1340 stream_write(s
, api
->opaque
.data
, api
->opaque
.length
);
1342 /* Put length at the first point of the stream. */
1343 stream_putw_at(s
, 0, stream_get_endp(s
));
1349 * Decode a single zapi nexthop object
1351 int zapi_nexthop_decode(struct stream
*s
, struct zapi_nexthop
*api_nh
,
1352 uint32_t api_flags
, uint32_t api_message
)
1356 STREAM_GETL(s
, api_nh
->vrf_id
);
1357 STREAM_GETC(s
, api_nh
->type
);
1359 /* Note that we're only using a single octet of flags */
1360 STREAM_GETC(s
, api_nh
->flags
);
1362 switch (api_nh
->type
) {
1363 case NEXTHOP_TYPE_BLACKHOLE
:
1364 STREAM_GETC(s
, api_nh
->bh_type
);
1366 case NEXTHOP_TYPE_IPV4
:
1367 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1368 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1370 STREAM_GETL(s
, api_nh
->ifindex
);
1372 case NEXTHOP_TYPE_IFINDEX
:
1373 STREAM_GETL(s
, api_nh
->ifindex
);
1375 case NEXTHOP_TYPE_IPV6
:
1376 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1377 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1378 STREAM_GETL(s
, api_nh
->ifindex
);
1382 /* MPLS labels for BGP-LU or Segment Routing */
1383 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)) {
1384 STREAM_GETC(s
, api_nh
->label_num
);
1385 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1388 "%s: invalid number of MPLS labels (%u)",
1389 __func__
, api_nh
->label_num
);
1393 STREAM_GET(&api_nh
->labels
[0], s
,
1394 api_nh
->label_num
* sizeof(mpls_label_t
));
1397 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1398 STREAM_GETL(s
, api_nh
->weight
);
1400 /* Router MAC for EVPN routes. */
1401 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1402 STREAM_GET(&(api_nh
->rmac
), s
,
1403 sizeof(struct ethaddr
));
1405 /* Color for Segment Routing TE. */
1406 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1407 STREAM_GETL(s
, api_nh
->srte_color
);
1409 /* Backup nexthop index */
1410 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1411 STREAM_GETC(s
, api_nh
->backup_num
);
1413 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1416 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1417 STREAM_GETC(s
, api_nh
->backup_idx
[i
]);
1420 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)) {
1421 STREAM_GETL(s
, api_nh
->seg6local_action
);
1422 STREAM_GET(&api_nh
->seg6local_ctx
, s
,
1423 sizeof(struct seg6local_context
));
1426 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
))
1427 STREAM_GET(&api_nh
->seg6_segs
, s
,
1428 sizeof(struct in6_addr
));
1438 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1440 struct zapi_nexthop
*api_nh
;
1443 memset(api
, 0, sizeof(*api
));
1445 /* Type, flags, message. */
1446 STREAM_GETC(s
, api
->type
);
1447 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1448 flog_err(EC_LIB_ZAPI_ENCODE
,
1449 "%s: Specified route type: %d is not a legal value",
1450 __func__
, api
->type
);
1454 STREAM_GETW(s
, api
->instance
);
1455 STREAM_GETL(s
, api
->flags
);
1456 STREAM_GETL(s
, api
->message
);
1457 STREAM_GETC(s
, api
->safi
);
1458 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1459 flog_err(EC_LIB_ZAPI_ENCODE
,
1460 "%s: Specified route SAFI (%u) is not a legal value",
1461 __func__
, api
->safi
);
1466 STREAM_GETC(s
, api
->prefix
.family
);
1467 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1468 switch (api
->prefix
.family
) {
1470 if (api
->prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1473 "%s: V4 prefixlen is %d which should not be more than 32",
1474 __func__
, api
->prefix
.prefixlen
);
1479 if (api
->prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1482 "%s: v6 prefixlen is %d which should not be more than 128",
1483 __func__
, api
->prefix
.prefixlen
);
1488 flog_err(EC_LIB_ZAPI_ENCODE
,
1489 "%s: Specified family %d is not v4 or v6", __func__
,
1490 api
->prefix
.family
);
1493 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1495 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1496 api
->src_prefix
.family
= AF_INET6
;
1497 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1498 if (api
->src_prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1501 "%s: SRC Prefix prefixlen received: %d is too large",
1502 __func__
, api
->src_prefix
.prefixlen
);
1505 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1506 PSIZE(api
->src_prefix
.prefixlen
));
1508 if (api
->prefix
.family
!= AF_INET6
1509 || api
->src_prefix
.prefixlen
== 0) {
1512 "%s: SRC prefix specified in some manner that makes no sense",
1518 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1519 STREAM_GETL(s
, api
->nhgid
);
1522 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1523 STREAM_GETW(s
, api
->nexthop_num
);
1524 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1525 flog_err(EC_LIB_ZAPI_ENCODE
,
1526 "%s: invalid number of nexthops (%u)",
1527 __func__
, api
->nexthop_num
);
1531 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1532 api_nh
= &api
->nexthops
[i
];
1534 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1541 /* Backup nexthops. */
1542 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1543 STREAM_GETW(s
, api
->backup_nexthop_num
);
1544 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1545 flog_err(EC_LIB_ZAPI_ENCODE
,
1546 "%s: invalid number of backup nexthops (%u)",
1547 __func__
, api
->backup_nexthop_num
);
1551 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1552 api_nh
= &api
->backup_nexthops
[i
];
1554 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1562 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1563 STREAM_GETC(s
, api
->distance
);
1564 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1565 STREAM_GETL(s
, api
->metric
);
1566 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1567 STREAM_GETL(s
, api
->tag
);
1568 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1569 STREAM_GETL(s
, api
->mtu
);
1570 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1571 STREAM_GETL(s
, api
->tableid
);
1573 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_OPAQUE
)) {
1574 STREAM_GETW(s
, api
->opaque
.length
);
1575 if (api
->opaque
.length
> ZAPI_MESSAGE_OPAQUE_LENGTH
) {
1578 "%s: opaque length %u is greater than allowed value",
1579 __func__
, api
->opaque
.length
);
1583 STREAM_GET(api
->opaque
.data
, s
, api
->opaque
.length
);
1591 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1597 memset(&any
, 0, sizeof(any
));
1598 any
.family
= family
;
1602 stream_putc(s
, p
->family
);
1603 stream_putc(s
, p
->prefixlen
);
1604 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1607 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1610 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1613 * We are sending one item at a time at the moment
1617 stream_putl(s
, zrule
->seq
);
1618 stream_putl(s
, zrule
->priority
);
1619 stream_putl(s
, zrule
->unique
);
1621 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1622 zrule
->filter
.src_ip
.family
);
1623 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1624 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1625 zrule
->filter
.src_ip
.family
);
1626 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1627 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1629 stream_putl(s
, zrule
->action
.table
);
1630 stream_put(s
, zrule
->ifname
, INTERFACE_NAMSIZ
);
1632 /* Put length at the first point of the stream. */
1633 stream_putw_at(s
, 0, stream_get_endp(s
));
1638 bool zapi_nhg_notify_decode(struct stream
*s
, uint32_t *id
,
1639 enum zapi_nhg_notify_owner
*note
)
1643 STREAM_GET(note
, s
, sizeof(*note
));
1644 STREAM_GETL(s
, read_id
);
1654 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1656 enum zapi_route_notify_owner
*note
,
1657 afi_t
*afi
, safi_t
*safi
)
1663 STREAM_GET(note
, s
, sizeof(*note
));
1665 STREAM_GETC(s
, p
->family
);
1666 STREAM_GETC(s
, p
->prefixlen
);
1667 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1669 STREAM_GETC(s
, afi_val
);
1670 STREAM_GETC(s
, safi_val
);
1685 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1686 uint32_t *priority
, uint32_t *unique
, char *ifname
,
1687 enum zapi_rule_notify_owner
*note
)
1689 uint32_t prio
, seq
, uni
;
1691 STREAM_GET(note
, s
, sizeof(*note
));
1693 STREAM_GETL(s
, seq
);
1694 STREAM_GETL(s
, prio
);
1695 STREAM_GETL(s
, uni
);
1696 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
1699 zlog_debug("%s: %u %u %u %s", __func__
, seq
, prio
, uni
, ifname
);
1710 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1711 enum zapi_ipset_notify_owner
*note
)
1716 STREAM_GETW(s
, notew
);
1718 STREAM_GETL(s
, uni
);
1721 zlog_debug("%s: %u", __func__
, uni
);
1723 *note
= (enum zapi_ipset_notify_owner
)notew
;
1730 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1732 enum zapi_ipset_entry_notify_owner
*note
)
1737 STREAM_GETW(s
, notew
);
1739 STREAM_GETL(s
, uni
);
1741 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1744 zlog_debug("%s: %u", __func__
, uni
);
1746 *note
= (enum zapi_ipset_entry_notify_owner
)notew
;
1754 bool zapi_iptable_notify_decode(struct stream
*s
,
1756 enum zapi_iptable_notify_owner
*note
)
1761 STREAM_GETW(s
, notew
);
1763 STREAM_GETL(s
, uni
);
1766 zlog_debug("%s: %u", __func__
, uni
);
1768 *note
= (enum zapi_iptable_notify_owner
)notew
;
1776 struct nexthop
*nexthop_from_zapi_nexthop(const struct zapi_nexthop
*znh
)
1778 struct nexthop
*n
= nexthop_new();
1780 n
->type
= znh
->type
;
1781 n
->vrf_id
= znh
->vrf_id
;
1782 n
->ifindex
= znh
->ifindex
;
1783 n
->gate
= znh
->gate
;
1784 n
->srte_color
= znh
->srte_color
;
1787 * This function currently handles labels
1789 if (znh
->label_num
) {
1790 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1794 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1795 SET_FLAG(n
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1796 n
->backup_num
= znh
->backup_num
;
1797 memcpy(n
->backup_idx
, znh
->backup_idx
, n
->backup_num
);
1800 if (znh
->seg6local_action
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
1801 nexthop_add_srv6_seg6local(n
, znh
->seg6local_action
,
1802 &znh
->seg6local_ctx
);
1804 if (!sid_zero(&znh
->seg6_segs
))
1805 nexthop_add_srv6_seg6(n
, &znh
->seg6_segs
);
1811 * Convert nexthop to zapi nexthop
1813 int zapi_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1814 const struct nexthop
*nh
)
1818 memset(znh
, 0, sizeof(*znh
));
1820 znh
->type
= nh
->type
;
1821 znh
->vrf_id
= nh
->vrf_id
;
1822 znh
->weight
= nh
->weight
;
1823 znh
->ifindex
= nh
->ifindex
;
1824 znh
->gate
= nh
->gate
;
1826 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
1827 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1829 if (nh
->nh_label
&& (nh
->nh_label
->num_labels
> 0)) {
1832 if (nh
->nh_label
->num_labels
> MPLS_MAX_LABELS
)
1835 for (i
= 0; i
< nh
->nh_label
->num_labels
; i
++)
1836 znh
->labels
[i
] = nh
->nh_label
->label
[i
];
1839 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1842 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1843 if (nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1846 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1847 znh
->backup_num
= nh
->backup_num
;
1848 memcpy(znh
->backup_idx
, nh
->backup_idx
, znh
->backup_num
);
1852 if (nh
->nh_srv6
->seg6local_action
!=
1853 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1854 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
);
1855 znh
->seg6local_action
= nh
->nh_srv6
->seg6local_action
;
1856 memcpy(&znh
->seg6local_ctx
,
1857 &nh
->nh_srv6
->seg6local_ctx
,
1858 sizeof(struct seg6local_context
));
1861 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
1862 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
);
1863 memcpy(&znh
->seg6_segs
, &nh
->nh_srv6
->seg6_segs
,
1864 sizeof(struct in6_addr
));
1872 * Wrapper that converts backup nexthop
1874 int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1875 const struct nexthop
*nh
)
1879 /* Ensure that zapi flags are correct: backups don't have backups */
1880 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
1882 UNSET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1888 * Format some info about a zapi nexthop, for debug or logging.
1890 const char *zapi_nexthop2str(const struct zapi_nexthop
*znh
, char *buf
,
1893 char tmp
[INET6_ADDRSTRLEN
];
1895 switch (znh
->type
) {
1896 case NEXTHOP_TYPE_IFINDEX
:
1897 snprintf(buf
, bufsize
, "if %u", znh
->ifindex
);
1899 case NEXTHOP_TYPE_IPV4
:
1900 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1901 inet_ntop(AF_INET
, &znh
->gate
.ipv4
, tmp
, sizeof(tmp
));
1902 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
1904 case NEXTHOP_TYPE_IPV6
:
1905 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1906 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, tmp
, sizeof(tmp
));
1907 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
1909 case NEXTHOP_TYPE_BLACKHOLE
:
1910 snprintf(buf
, bufsize
, "blackhole");
1913 snprintf(buf
, bufsize
, "unknown");
1921 * Decode the nexthop-tracking update message
1923 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1927 memset(nhr
, 0, sizeof(*nhr
));
1929 STREAM_GETL(s
, nhr
->message
);
1930 STREAM_GETW(s
, nhr
->safi
);
1931 STREAM_GETW(s
, nhr
->prefix
.family
);
1932 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1933 switch (nhr
->prefix
.family
) {
1935 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1938 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1943 if (CHECK_FLAG(nhr
->message
, ZAPI_MESSAGE_SRTE
))
1944 STREAM_GETL(s
, nhr
->srte_color
);
1946 STREAM_GETC(s
, nhr
->type
);
1947 STREAM_GETW(s
, nhr
->instance
);
1948 STREAM_GETC(s
, nhr
->distance
);
1949 STREAM_GETL(s
, nhr
->metric
);
1950 STREAM_GETC(s
, nhr
->nexthop_num
);
1952 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1953 if (zapi_nexthop_decode(s
, &(nhr
->nexthops
[i
]), 0, 0) != 0)
1962 bool zapi_error_decode(struct stream
*s
, enum zebra_error_types
*error
)
1964 memset(error
, 0, sizeof(*error
));
1966 STREAM_GET(error
, s
, sizeof(*error
));
1969 zlog_debug("%s: type: %s", __func__
,
1970 zebra_error_type2str(*error
));
1978 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1979 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1980 * then set/unset redist[type] in the client handle (a struct zserv) for the
1983 enum zclient_send_status
1984 zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1985 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1992 zclient_create_header(s
, command
, vrf_id
);
1993 stream_putc(s
, afi
);
1994 stream_putc(s
, type
);
1995 stream_putw(s
, instance
);
1997 stream_putw_at(s
, 0, stream_get_endp(s
));
1999 return zclient_send_message(zclient
);
2002 enum zclient_send_status
2003 zebra_redistribute_default_send(int command
, struct zclient
*zclient
, afi_t afi
,
2011 zclient_create_header(s
, command
, vrf_id
);
2012 stream_putc(s
, afi
);
2014 stream_putw_at(s
, 0, stream_get_endp(s
));
2016 return zclient_send_message(zclient
);
2019 /* Send route notify request to zebra */
2020 int zebra_route_notify_send(int command
, struct zclient
*zclient
, bool set
)
2027 zclient_create_header(s
, command
, 0);
2028 stream_putc(s
, !!set
);
2030 stream_putw_at(s
, 0, stream_get_endp(s
));
2032 return zclient_send_message(zclient
);
2035 /* Get prefix in ZServ format; family should be filled in on prefix */
2036 static int zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
2038 size_t plen
= prefix_blen(p
);
2045 STREAM_GET(&p
->u
.prefix
, s
, plen
);
2047 p
->prefixlen
= MIN(plen
* 8, c
);
2054 /* Router-id update from zebra daemon. */
2055 int zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
2057 /* Fetch interface address. */
2058 STREAM_GETC(s
, rid
->family
);
2060 return zclient_stream_get_prefix(s
, rid
);
2066 /* Interface addition from zebra daemon. */
2068 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
2069 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
2071 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2072 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2078 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2080 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2085 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2087 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2089 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2091 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2093 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2095 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2096 * | parent ifindex |
2097 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2098 * | Link Layer Type |
2099 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2100 * | Harware Address Length |
2101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2102 * | Hardware Address if HW lenght different from 0 |
2103 * | ... max INTERFACE_HWADDR_MAX |
2104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2105 * | Link_params? | Whether a link-params follows: 1 or 0.
2106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2107 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
2108 * | .... (struct if_link_params). |
2109 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2112 static int zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
2115 char vrfname_tmp
[VRF_NAMSIZ
+ 1] = {};
2116 struct vrf_data data
;
2118 STREAM_GET(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
2119 /* Read interface name. */
2120 STREAM_GET(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
2122 if (strlen(vrfname_tmp
) == 0)
2123 goto stream_failure
;
2125 /* Lookup/create vrf by name, then vrf_id. */
2126 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
2128 /* If there's already a VRF with this name, don't create vrf */
2132 vrf
->data
.l
.table_id
= data
.l
.table_id
;
2133 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
2134 /* overwrite default vrf */
2135 if (vrf_id
== VRF_DEFAULT
)
2136 vrf_set_default_name(vrfname_tmp
, false);
2144 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
2148 /* Lookup vrf by vrf_id. */
2149 vrf
= vrf_lookup_by_id(vrf_id
);
2152 * If a routing protocol doesn't know about a
2153 * vrf that is about to be deleted. There is
2154 * no point in attempting to delete it.
2162 static int zclient_interface_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
2164 struct interface
*ifp
;
2165 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
2166 struct stream
*s
= zclient
->ibuf
;
2168 /* Read interface name. */
2169 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
2171 /* Lookup/create interface by name. */
2172 if (!vrf_get(vrf_id
, NULL
)) {
2174 "Rx'd interface add from Zebra, but VRF %u does not exist",
2179 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
2181 zebra_interface_if_set_value(s
, ifp
);
2183 if_new_via_zapi(ifp
);
2191 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
2192 * from zebra server. The format of this message is the same as
2193 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
2194 * except that no sockaddr_dl is sent at the tail of the message.
2196 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
2198 struct interface
*ifp
;
2199 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
2201 /* Read interface name. */
2202 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
2204 /* Lookup this by interface index. */
2205 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
2207 flog_err(EC_LIB_ZAPI_ENCODE
,
2208 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
2209 ifname_tmp
, vrf_id
);
2213 zebra_interface_if_set_value(s
, ifp
);
2220 static void zclient_interface_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
2222 struct interface
*ifp
;
2223 struct stream
*s
= zclient
->ibuf
;
2225 ifp
= zebra_interface_state_read(s
, vrf_id
);
2230 if_destroy_via_zapi(ifp
);
2234 static void zclient_interface_up(struct zclient
*zclient
, vrf_id_t vrf_id
)
2236 struct interface
*ifp
;
2237 struct stream
*s
= zclient
->ibuf
;
2239 ifp
= zebra_interface_state_read(s
, vrf_id
);
2244 if_up_via_zapi(ifp
);
2247 static void zclient_interface_down(struct zclient
*zclient
, vrf_id_t vrf_id
)
2249 struct interface
*ifp
;
2250 struct stream
*s
= zclient
->ibuf
;
2252 ifp
= zebra_interface_state_read(s
, vrf_id
);
2257 if_down_via_zapi(ifp
);
2260 static void zclient_handle_error(ZAPI_CALLBACK_ARGS
)
2262 enum zebra_error_types error
;
2263 struct stream
*s
= zclient
->ibuf
;
2265 zapi_error_decode(s
, &error
);
2267 if (zclient
->handle_error
)
2268 (*zclient
->handle_error
)(error
);
2271 static int link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
2277 uint32_t bwclassnum
;
2279 STREAM_GETL(s
, iflp
->lp_status
);
2280 STREAM_GETL(s
, iflp
->te_metric
);
2281 STREAM_GETF(s
, iflp
->max_bw
);
2282 STREAM_GETF(s
, iflp
->max_rsv_bw
);
2283 STREAM_GETL(s
, bwclassnum
);
2286 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
2287 STREAM_GETF(s
, iflp
->unrsv_bw
[i
]);
2290 EC_LIB_ZAPI_MISSMATCH
,
2291 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries - outdated library?",
2292 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
2294 STREAM_GETL(s
, iflp
->admin_grp
);
2295 STREAM_GETL(s
, iflp
->rmt_as
);
2296 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
2298 STREAM_GETL(s
, iflp
->av_delay
);
2299 STREAM_GETL(s
, iflp
->min_delay
);
2300 STREAM_GETL(s
, iflp
->max_delay
);
2301 STREAM_GETL(s
, iflp
->delay_var
);
2303 STREAM_GETF(s
, iflp
->pkt_loss
);
2304 STREAM_GETF(s
, iflp
->res_bw
);
2305 STREAM_GETF(s
, iflp
->ava_bw
);
2306 STREAM_GETF(s
, iflp
->use_bw
);
2313 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
2317 struct if_link_params
*iflp
;
2318 struct if_link_params iflp_copy
;
2320 bool params_changed
= false;
2322 STREAM_GETL(s
, ifindex
);
2324 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2327 flog_err(EC_LIB_ZAPI_ENCODE
,
2328 "%s: unknown ifindex %u, shouldn't happen", __func__
,
2333 if (ifp
->link_params
== NULL
)
2334 params_changed
= true;
2336 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
2339 memcpy(&iflp_copy
, iflp
, sizeof(iflp_copy
));
2341 if (link_params_set_value(s
, iflp
) != 0)
2342 goto stream_failure
;
2344 if (memcmp(&iflp_copy
, iflp
, sizeof(iflp_copy
)))
2345 params_changed
= true;
2348 *changed
= params_changed
;
2356 static void zebra_interface_if_set_value(struct stream
*s
,
2357 struct interface
*ifp
)
2359 uint8_t link_params_status
= 0;
2360 ifindex_t old_ifindex
, new_ifindex
;
2362 old_ifindex
= ifp
->oldifindex
;
2363 /* Read interface's index. */
2364 STREAM_GETL(s
, new_ifindex
);
2365 if_set_index(ifp
, new_ifindex
);
2366 STREAM_GETC(s
, ifp
->status
);
2368 /* Read interface's value. */
2369 STREAM_GETQ(s
, ifp
->flags
);
2370 STREAM_GETC(s
, ifp
->ptm_enable
);
2371 STREAM_GETC(s
, ifp
->ptm_status
);
2372 STREAM_GETL(s
, ifp
->metric
);
2373 STREAM_GETL(s
, ifp
->speed
);
2374 STREAM_GETL(s
, ifp
->mtu
);
2375 STREAM_GETL(s
, ifp
->mtu6
);
2376 STREAM_GETL(s
, ifp
->bandwidth
);
2377 STREAM_GETL(s
, ifp
->link_ifindex
);
2378 STREAM_GETL(s
, ifp
->ll_type
);
2379 STREAM_GETL(s
, ifp
->hw_addr_len
);
2380 if (ifp
->hw_addr_len
)
2381 STREAM_GET(ifp
->hw_addr
, s
,
2382 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
2384 /* Read Traffic Engineering status */
2385 link_params_status
= stream_getc(s
);
2386 /* Then, Traffic Engineering parameters if any */
2387 if (link_params_status
) {
2388 struct if_link_params
*iflp
= if_link_params_get(ifp
);
2389 link_params_set_value(s
, iflp
);
2392 nexthop_group_interface_state_change(ifp
, old_ifindex
);
2396 zlog_err("Could not parse interface values; aborting");
2397 assert(!"Failed to parse interface values");
2400 size_t zebra_interface_link_params_write(struct stream
*s
,
2401 struct interface
*ifp
)
2404 struct if_link_params
*iflp
;
2407 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
2410 iflp
= ifp
->link_params
;
2413 w
+= stream_putl(s
, iflp
->lp_status
);
2415 w
+= stream_putl(s
, iflp
->te_metric
);
2416 w
+= stream_putf(s
, iflp
->max_bw
);
2417 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
2419 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
2420 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
2421 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
2423 w
+= stream_putl(s
, iflp
->admin_grp
);
2424 w
+= stream_putl(s
, iflp
->rmt_as
);
2425 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
2427 w
+= stream_putl(s
, iflp
->av_delay
);
2428 w
+= stream_putl(s
, iflp
->min_delay
);
2429 w
+= stream_putl(s
, iflp
->max_delay
);
2430 w
+= stream_putl(s
, iflp
->delay_var
);
2432 w
+= stream_putf(s
, iflp
->pkt_loss
);
2433 w
+= stream_putf(s
, iflp
->res_bw
);
2434 w
+= stream_putf(s
, iflp
->ava_bw
);
2435 w
+= stream_putf(s
, iflp
->use_bw
);
2441 * format of message for address additon is:
2445 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
2446 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
2455 * | ifc_flags | flags for connected address
2463 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
2471 static int memconstant(const void *s
, int c
, size_t n
)
2473 const uint8_t *p
= s
;
2482 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
2486 struct interface
*ifp
;
2487 struct connected
*ifc
;
2488 struct prefix p
, d
, *dp
;
2492 memset(&p
, 0, sizeof(p
));
2493 memset(&d
, 0, sizeof(d
));
2495 /* Get interface index. */
2496 STREAM_GETL(s
, ifindex
);
2499 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2501 flog_err(EC_LIB_ZAPI_ENCODE
,
2502 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
2503 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
2509 STREAM_GETC(s
, ifc_flags
);
2511 /* Fetch interface address. */
2512 STREAM_GETC(s
, d
.family
);
2513 p
.family
= d
.family
;
2514 plen
= prefix_blen(&d
);
2516 if (zclient_stream_get_prefix(s
, &p
) != 0)
2517 goto stream_failure
;
2519 /* Fetch destination address. */
2520 STREAM_GET(&d
.u
.prefix
, s
, plen
);
2522 /* N.B. NULL destination pointers are encoded as all zeroes */
2523 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
2525 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
2526 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
2528 /* N.B. NULL destination pointers are encoded as all
2530 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
2533 ifc
->flags
= ifc_flags
;
2534 if (ifc
->destination
)
2535 ifc
->destination
->prefixlen
=
2536 ifc
->address
->prefixlen
;
2537 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
2538 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
2542 "interface %s address %pFX with peer flag set, but no peer address!",
2543 ifp
->name
, ifc
->address
);
2544 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
2548 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
2549 ifc
= connected_delete_by_prefix(ifp
, &p
);
2559 * format of message for neighbor connected address is:
2563 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
2564 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2579 * | addr_len | len of addr.
2582 struct nbr_connected
*
2583 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
2585 unsigned int ifindex
;
2586 struct interface
*ifp
;
2588 struct nbr_connected
*ifc
;
2590 /* Get interface index. */
2591 STREAM_GETL(s
, ifindex
);
2594 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2596 flog_err(EC_LIB_ZAPI_ENCODE
,
2597 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2598 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
2604 STREAM_GETC(s
, p
.family
);
2605 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
2606 STREAM_GETC(s
, p
.prefixlen
);
2608 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
2609 /* Currently only supporting P2P links, so any new RA source
2611 considered as the replacement of the previously learnt
2612 Link-Local address. */
2613 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
2614 ifc
= nbr_connected_new();
2615 ifc
->address
= prefix_new();
2617 listnode_add(ifp
->nbr_connected
, ifc
);
2620 prefix_copy(ifc
->address
, &p
);
2622 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
2624 ifc
= nbr_connected_check(ifp
, &p
);
2626 listnode_delete(ifp
->nbr_connected
, ifc
);
2635 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
2637 vrf_id_t
*new_vrf_id
)
2639 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2640 struct interface
*ifp
;
2643 /* Read interface name. */
2644 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
2646 /* Lookup interface. */
2647 ifp
= if_lookup_by_name(ifname
, vrf_id
);
2649 flog_err(EC_LIB_ZAPI_ENCODE
,
2650 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2655 /* Fetch new VRF Id. */
2656 STREAM_GETL(s
, new_id
);
2658 *new_vrf_id
= new_id
;
2665 /* filter unwanted messages until the expected one arrives */
2666 static int zclient_read_sync_response(struct zclient
*zclient
,
2667 uint16_t expected_cmd
)
2679 cmd
= expected_cmd
+ 1;
2680 while (ret
== 0 && cmd
!= expected_cmd
) {
2684 /* wait until response arrives */
2686 FD_SET(zclient
->sock
, &readfds
);
2687 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2688 if (!FD_ISSET(zclient
->sock
, &readfds
))
2691 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2692 &version
, &vrf_id
, &cmd
);
2694 zlog_debug("%s: Response (%d bytes) received", __func__
,
2698 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2706 * Connect to label manager in a syncronous way
2708 * It first writes the request to zcient output buffer and then
2709 * immediately reads the answer from the input buffer.
2711 * @param zclient Zclient used to connect to label manager (zebra)
2712 * @param async Synchronous (0) or asynchronous (1) operation
2713 * @result Result of response
2715 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2720 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2721 ZEBRA_LABEL_MANAGER_CONNECT
;
2724 zlog_debug("Connecting to Label Manager (LM)");
2726 if (zclient
->sock
< 0) {
2727 zlog_debug("%s: invalid zclient socket", __func__
);
2734 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2737 stream_putc(s
, zclient
->redist_default
);
2739 stream_putw(s
, zclient
->instance
);
2741 /* Put length at the first point of the stream. */
2742 stream_putw_at(s
, 0, stream_get_endp(s
));
2744 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2746 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2747 close(zclient
->sock
);
2752 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2753 close(zclient
->sock
);
2758 zlog_debug("LM connect request sent (%d bytes)", ret
);
2764 if (zclient_read_sync_response(zclient
, cmd
)
2770 /* read instance and proto */
2774 STREAM_GETC(s
, proto
);
2775 STREAM_GETW(s
, instance
);
2778 if (proto
!= zclient
->redist_default
)
2781 "Wrong proto (%u) in LM connect response. Should be %u",
2782 proto
, zclient
->redist_default
);
2783 if (instance
!= zclient
->instance
)
2786 "Wrong instId (%u) in LM connect response. Should be %u",
2787 instance
, zclient
->instance
);
2790 STREAM_GETC(s
, result
);
2792 zlog_debug("LM connect-response received, result %u", result
);
2801 * Function to request a srv6-locator chunk in an Asyncronous way
2803 * @param zclient Zclient used to connect to table manager (zebra)
2804 * @param locator_name Name of SRv6-locator
2805 * @result 0 on success, -1 otherwise
2807 int srv6_manager_get_locator_chunk(struct zclient
*zclient
,
2808 const char *locator_name
)
2811 const size_t len
= strlen(locator_name
);
2814 zlog_debug("Getting SRv6-Locator Chunk %s", locator_name
);
2816 if (zclient
->sock
< 0)
2822 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
,
2826 stream_putw(s
, len
);
2827 stream_put(s
, locator_name
, len
);
2829 /* Put length at the first point of the stream. */
2830 stream_putw_at(s
, 0, stream_get_endp(s
));
2832 return zclient_send_message(zclient
);
2836 * Function to release a srv6-locator chunk
2838 * @param zclient Zclient used to connect to table manager (zebra)
2839 * @param locator_name Name of SRv6-locator
2840 * @result 0 on success, -1 otherwise
2842 int srv6_manager_release_locator_chunk(struct zclient
*zclient
,
2843 const char *locator_name
)
2846 const size_t len
= strlen(locator_name
);
2849 zlog_debug("Releasing SRv6-Locator Chunk %s", locator_name
);
2851 if (zclient
->sock
< 0)
2857 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
,
2861 stream_putw(s
, len
);
2862 stream_put(s
, locator_name
, len
);
2864 /* Put length at the first point of the stream. */
2865 stream_putw_at(s
, 0, stream_get_endp(s
));
2867 return zclient_send_message(zclient
);
2871 * Asynchronous label chunk request
2873 * @param zclient Zclient used to connect to label manager (zebra)
2874 * @param keep Avoid garbage collection
2875 * @param chunk_size Amount of labels requested
2876 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2877 * @result 0 on success, -1 otherwise
2879 enum zclient_send_status
zclient_send_get_label_chunk(struct zclient
*zclient
,
2881 uint32_t chunk_size
,
2887 zlog_debug("Getting Label Chunk");
2889 if (zclient
->sock
< 0)
2890 return ZCLIENT_SEND_FAILURE
;
2895 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2897 stream_putc(s
, zclient
->redist_default
);
2899 stream_putw(s
, zclient
->instance
);
2900 stream_putc(s
, keep
);
2901 stream_putl(s
, chunk_size
);
2902 stream_putl(s
, base
);
2904 /* Put length at the first point of the stream. */
2905 stream_putw_at(s
, 0, stream_get_endp(s
));
2907 return zclient_send_message(zclient
);
2911 * Function to request a label chunk in a syncronous way
2913 * It first writes the request to zlcient output buffer and then
2914 * immediately reads the answer from the input buffer.
2916 * @param zclient Zclient used to connect to label manager (zebra)
2917 * @param keep Avoid garbage collection
2918 * @param chunk_size Amount of labels requested
2919 * @param start To write first assigned chunk label to
2920 * @param end To write last assigned chunk label to
2921 * @result 0 on success, -1 otherwise
2923 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2924 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2928 uint8_t response_keep
;
2931 zlog_debug("Getting Label Chunk");
2933 if (zclient
->sock
< 0)
2939 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2941 stream_putc(s
, zclient
->redist_default
);
2943 stream_putw(s
, zclient
->instance
);
2945 stream_putc(s
, keep
);
2947 stream_putl(s
, chunk_size
);
2948 /* requested chunk base */
2949 stream_putl(s
, base
);
2950 /* Put length at the first point of the stream. */
2951 stream_putw_at(s
, 0, stream_get_endp(s
));
2953 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2955 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2956 close(zclient
->sock
);
2961 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2962 close(zclient
->sock
);
2967 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2970 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2973 /* parse response */
2976 /* read proto and instance */
2980 STREAM_GETC(s
, proto
);
2981 STREAM_GETW(s
, instance
);
2984 if (proto
!= zclient
->redist_default
)
2985 flog_err(EC_LIB_ZAPI_ENCODE
,
2986 "Wrong proto (%u) in get chunk response. Should be %u",
2987 proto
, zclient
->redist_default
);
2988 if (instance
!= zclient
->instance
)
2989 flog_err(EC_LIB_ZAPI_ENCODE
,
2990 "Wrong instId (%u) in get chunk response Should be %u",
2991 instance
, zclient
->instance
);
2993 /* if we requested a specific chunk and it could not be allocated, the
2994 * response message will end here
2996 if (!STREAM_READABLE(s
)) {
2997 zlog_info("Unable to assign Label Chunk to %s instance %u",
2998 zebra_route_string(proto
), instance
);
3003 STREAM_GETC(s
, response_keep
);
3004 /* start and end labels */
3005 STREAM_GETL(s
, *start
);
3006 STREAM_GETL(s
, *end
);
3008 /* not owning this response */
3009 if (keep
!= response_keep
) {
3012 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
3013 *start
, *end
, keep
, response_keep
);
3016 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
3017 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
3018 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
3024 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
3034 * Function to release a label chunk
3036 * @param zclient Zclient used to connect to label manager (zebra)
3037 * @param start First label of chunk
3038 * @param end Last label of chunk
3039 * @result 0 on success, -1 otherwise
3041 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
3048 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
3050 if (zclient
->sock
< 0)
3056 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
3059 stream_putc(s
, zclient
->redist_default
);
3061 stream_putw(s
, zclient
->instance
);
3063 stream_putl(s
, start
);
3065 stream_putl(s
, end
);
3067 /* Put length at the first point of the stream. */
3068 stream_putw_at(s
, 0, stream_get_endp(s
));
3070 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
3072 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
3073 close(zclient
->sock
);
3078 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
3079 close(zclient
->sock
);
3088 * Connect to table manager in a syncronous way
3090 * It first writes the request to zcient output buffer and then
3091 * immediately reads the answer from the input buffer.
3093 * @param zclient Zclient used to connect to table manager (zebra)
3094 * @result Result of response
3096 int tm_table_manager_connect(struct zclient
*zclient
)
3103 zlog_debug("Connecting to Table Manager");
3105 if (zclient
->sock
< 0)
3106 return ZCLIENT_SEND_FAILURE
;
3111 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
3114 stream_putc(s
, zclient
->redist_default
);
3116 stream_putw(s
, zclient
->instance
);
3118 /* Put length at the first point of the stream. */
3119 stream_putw_at(s
, 0, stream_get_endp(s
));
3121 ret
= zclient_send_message(zclient
);
3122 if (ret
== ZCLIENT_SEND_FAILURE
)
3126 zlog_debug("%s: Table manager connect request sent", __func__
);
3129 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
3135 STREAM_GETC(s
, result
);
3138 "%s: Table Manager connect response received, result %u",
3147 * Function to request a table chunk in a syncronous way
3149 * It first writes the request to zclient output buffer and then
3150 * immediately reads the answer from the input buffer.
3152 * @param zclient Zclient used to connect to table manager (zebra)
3153 * @param chunk_size Amount of table requested
3154 * @param start to write first assigned chunk table RT ID to
3155 * @param end To write last assigned chunk table RT ID to
3156 * @result 0 on success, -1 otherwise
3158 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
3159 uint32_t *start
, uint32_t *end
)
3165 zlog_debug("Getting Table Chunk");
3167 if (zclient
->sock
< 0)
3173 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
3175 stream_putl(s
, chunk_size
);
3176 /* Put length at the first point of the stream. */
3177 stream_putw_at(s
, 0, stream_get_endp(s
));
3179 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
3181 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
3183 close(zclient
->sock
);
3188 flog_err(EC_LIB_ZAPI_SOCKET
,
3189 "%s: zclient->sock connection closed", __func__
);
3190 close(zclient
->sock
);
3195 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
3199 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
3203 /* start and end table IDs */
3204 STREAM_GETL(s
, *start
);
3205 STREAM_GETL(s
, *end
);
3208 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
3216 * Function to release a table chunk
3218 * @param zclient Zclient used to connect to table manager (zebra)
3219 * @param start First label of table
3220 * @param end Last label of chunk
3221 * @result 0 on success, -1 otherwise
3223 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
3229 zlog_debug("Releasing Table Chunk");
3231 if (zclient
->sock
< 0)
3237 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
3240 stream_putl(s
, start
);
3242 stream_putl(s
, end
);
3244 /* Put length at the first point of the stream. */
3245 stream_putw_at(s
, 0, stream_get_endp(s
));
3247 if (zclient_send_message(zclient
) == ZCLIENT_SEND_FAILURE
)
3253 enum zclient_send_status
zebra_send_sr_policy(struct zclient
*zclient
, int cmd
,
3254 struct zapi_sr_policy
*zp
)
3256 if (zapi_sr_policy_encode(zclient
->obuf
, cmd
, zp
) < 0)
3257 return ZCLIENT_SEND_FAILURE
;
3258 return zclient_send_message(zclient
);
3261 int zapi_sr_policy_encode(struct stream
*s
, int cmd
, struct zapi_sr_policy
*zp
)
3263 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
3267 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
3268 stream_putl(s
, zp
->color
);
3269 stream_put_ipaddr(s
, &zp
->endpoint
);
3270 stream_write(s
, &zp
->name
, SRTE_POLICY_NAME_MAX_LENGTH
);
3272 stream_putc(s
, zt
->type
);
3273 stream_putl(s
, zt
->local_label
);
3275 if (zt
->label_num
> MPLS_MAX_LABELS
) {
3276 flog_err(EC_LIB_ZAPI_ENCODE
,
3277 "%s: label %u: can't encode %u labels (maximum is %u)",
3278 __func__
, zt
->local_label
, zt
->label_num
,
3282 stream_putw(s
, zt
->label_num
);
3284 for (int i
= 0; i
< zt
->label_num
; i
++)
3285 stream_putl(s
, zt
->labels
[i
]);
3287 /* Put length at the first point of the stream. */
3288 stream_putw_at(s
, 0, stream_get_endp(s
));
3293 int zapi_sr_policy_decode(struct stream
*s
, struct zapi_sr_policy
*zp
)
3295 memset(zp
, 0, sizeof(*zp
));
3297 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
3299 STREAM_GETL(s
, zp
->color
);
3300 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3301 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3303 /* segment list of active candidate path */
3304 STREAM_GETC(s
, zt
->type
);
3305 STREAM_GETL(s
, zt
->local_label
);
3306 STREAM_GETW(s
, zt
->label_num
);
3307 if (zt
->label_num
> MPLS_MAX_LABELS
) {
3308 flog_err(EC_LIB_ZAPI_ENCODE
,
3309 "%s: label %u: can't decode %u labels (maximum is %u)",
3310 __func__
, zt
->local_label
, zt
->label_num
,
3314 for (int i
= 0; i
< zt
->label_num
; i
++)
3315 STREAM_GETL(s
, zt
->labels
[i
]);
3323 int zapi_sr_policy_notify_status_decode(struct stream
*s
,
3324 struct zapi_sr_policy
*zp
)
3326 memset(zp
, 0, sizeof(*zp
));
3328 STREAM_GETL(s
, zp
->color
);
3329 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3330 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3331 STREAM_GETL(s
, zp
->status
);
3339 enum zclient_send_status
zebra_send_mpls_labels(struct zclient
*zclient
,
3340 int cmd
, struct zapi_labels
*zl
)
3342 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
3343 return ZCLIENT_SEND_FAILURE
;
3344 return zclient_send_message(zclient
);
3347 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
3349 struct zapi_nexthop
*znh
;
3353 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
3354 stream_putc(s
, zl
->message
);
3355 stream_putc(s
, zl
->type
);
3356 stream_putl(s
, zl
->local_label
);
3358 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3359 stream_putw(s
, zl
->route
.prefix
.family
);
3360 stream_put_prefix(s
, &zl
->route
.prefix
);
3361 stream_putc(s
, zl
->route
.type
);
3362 stream_putw(s
, zl
->route
.instance
);
3365 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3368 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3369 __func__
, zl
->local_label
, zl
->nexthop_num
,
3373 stream_putw(s
, zl
->nexthop_num
);
3375 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3376 znh
= &zl
->nexthops
[i
];
3378 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3382 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3384 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3387 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3388 __func__
, zl
->local_label
, zl
->nexthop_num
,
3392 stream_putw(s
, zl
->backup_nexthop_num
);
3394 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3395 znh
= &zl
->backup_nexthops
[i
];
3397 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3403 /* Put length at the first point of the stream. */
3404 stream_putw_at(s
, 0, stream_get_endp(s
));
3409 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
3411 struct zapi_nexthop
*znh
;
3413 memset(zl
, 0, sizeof(*zl
));
3416 STREAM_GETC(s
, zl
->message
);
3417 STREAM_GETC(s
, zl
->type
);
3418 STREAM_GETL(s
, zl
->local_label
);
3420 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3423 STREAM_GETW(s
, zl
->route
.prefix
.family
);
3424 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
3426 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
3427 switch (zl
->route
.prefix
.family
) {
3429 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
3431 "%s: Specified prefix length %d is greater than a v4 address can support",
3432 __func__
, zl
->route
.prefix
.prefixlen
);
3435 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
3439 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
3441 "%s: Specified prefix length %d is greater than a v6 address can support",
3442 __func__
, zl
->route
.prefix
.prefixlen
);
3445 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
3448 flog_err(EC_LIB_ZAPI_ENCODE
,
3449 "%s: Specified family %u is not v4 or v6",
3450 __func__
, zl
->route
.prefix
.family
);
3454 STREAM_GETC(s
, zl
->route
.type
);
3455 STREAM_GETW(s
, zl
->route
.instance
);
3458 STREAM_GETW(s
, zl
->nexthop_num
);
3460 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3463 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
3464 __func__
, &zl
->route
.prefix
, zl
->nexthop_num
,
3468 zl
->nexthop_num
= MIN(MULTIPATH_NUM
, zl
->nexthop_num
);
3470 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3471 znh
= &zl
->nexthops
[i
];
3473 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3476 if (znh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3479 "%s: Prefix %pFX has a blackhole nexthop which we cannot use for a label",
3480 __func__
, &zl
->route
.prefix
);
3485 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3486 STREAM_GETW(s
, zl
->backup_nexthop_num
);
3488 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3491 "%s: Prefix %pFX has %d backup nexthops, but we can only use the first %d",
3492 __func__
, &zl
->route
.prefix
,
3493 zl
->backup_nexthop_num
, MULTIPATH_NUM
);
3496 zl
->backup_nexthop_num
= MIN(MULTIPATH_NUM
,
3497 zl
->backup_nexthop_num
);
3499 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3500 znh
= &zl
->backup_nexthops
[i
];
3502 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3505 if (znh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3508 "%s: Prefix %pFX has a backup blackhole nexthop which we cannot use for a label",
3509 __func__
, &zl
->route
.prefix
);
3520 enum zclient_send_status
zebra_send_pw(struct zclient
*zclient
, int command
,
3529 zclient_create_header(s
, command
, VRF_DEFAULT
);
3530 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
3531 stream_putl(s
, pw
->ifindex
);
3534 stream_putl(s
, pw
->type
);
3537 stream_putl(s
, pw
->af
);
3540 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
3543 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
3546 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
3547 return ZCLIENT_SEND_FAILURE
;
3551 stream_putl(s
, pw
->local_label
);
3552 stream_putl(s
, pw
->remote_label
);
3555 stream_putc(s
, pw
->flags
);
3557 /* Protocol specific fields */
3558 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
3560 /* Put length at the first point of the stream. */
3561 stream_putw_at(s
, 0, stream_get_endp(s
));
3563 return zclient_send_message(zclient
);
3567 * Receive PW status update from Zebra and send it to LDE process.
3569 int zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
3573 memset(pw
, 0, sizeof(struct zapi_pw_status
));
3577 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
3578 STREAM_GETL(s
, pw
->ifindex
);
3579 STREAM_GETL(s
, pw
->status
);
3586 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
3588 struct zclient_capabilities cap
;
3589 struct stream
*s
= zclient
->ibuf
;
3591 uint8_t mpls_enabled
;
3593 STREAM_GETL(s
, vrf_backend
);
3595 if (vrf_backend
< 0 || vrf_configure_backend(vrf_backend
)) {
3596 flog_err(EC_LIB_ZAPI_ENCODE
,
3597 "%s: Garbage VRF backend type: %d", __func__
,
3599 goto stream_failure
;
3603 memset(&cap
, 0, sizeof(cap
));
3604 STREAM_GETC(s
, mpls_enabled
);
3605 cap
.mpls_enabled
= !!mpls_enabled
;
3606 STREAM_GETL(s
, cap
.ecmp
);
3607 STREAM_GETC(s
, cap
.role
);
3609 if (zclient
->zebra_capabilities
)
3610 (*zclient
->zebra_capabilities
)(&cap
);
3616 enum zclient_send_status
zclient_send_mlag_register(struct zclient
*client
,
3624 zclient_create_header(s
, ZEBRA_MLAG_CLIENT_REGISTER
, VRF_DEFAULT
);
3625 stream_putl(s
, bit_map
);
3627 stream_putw_at(s
, 0, stream_get_endp(s
));
3628 return zclient_send_message(client
);
3631 enum zclient_send_status
zclient_send_mlag_deregister(struct zclient
*client
)
3633 return zebra_message_send(client
, ZEBRA_MLAG_CLIENT_UNREGISTER
,
3637 enum zclient_send_status
zclient_send_mlag_data(struct zclient
*client
,
3638 struct stream
*client_s
)
3645 zclient_create_header(s
, ZEBRA_MLAG_FORWARD_MSG
, VRF_DEFAULT
);
3646 stream_put(s
, client_s
->data
, client_s
->endp
);
3648 stream_putw_at(s
, 0, stream_get_endp(s
));
3649 return zclient_send_message(client
);
3652 static void zclient_mlag_process_up(ZAPI_CALLBACK_ARGS
)
3654 if (zclient
->mlag_process_up
)
3655 (*zclient
->mlag_process_up
)();
3658 static void zclient_mlag_process_down(ZAPI_CALLBACK_ARGS
)
3660 if (zclient
->mlag_process_down
)
3661 (*zclient
->mlag_process_down
)();
3664 static void zclient_mlag_handle_msg(ZAPI_CALLBACK_ARGS
)
3666 if (zclient
->mlag_handle_msg
)
3667 (*zclient
->mlag_handle_msg
)(zclient
->ibuf
, length
);
3671 * Send an OPAQUE message, contents opaque to zebra. The message header
3672 * is a message subtype.
3674 enum zclient_send_status
zclient_send_opaque(struct zclient
*zclient
,
3675 uint32_t type
, const uint8_t *data
,
3681 /* Check buffer size */
3682 if (STREAM_SIZE(zclient
->obuf
) <
3683 (ZEBRA_HEADER_SIZE
+ sizeof(type
) + datasize
))
3684 return ZCLIENT_SEND_FAILURE
;
3689 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3691 /* Send sub-type and flags */
3692 stream_putl(s
, type
);
3693 stream_putw(s
, flags
);
3695 /* Send opaque data */
3696 stream_write(s
, data
, datasize
);
3698 /* Put length into the header at the start of the stream. */
3699 stream_putw_at(s
, 0, stream_get_endp(s
));
3701 return zclient_send_message(zclient
);
3705 * Send an OPAQUE message to a specific zclient. The contents are opaque
3708 enum zclient_send_status
3709 zclient_send_opaque_unicast(struct zclient
*zclient
, uint32_t type
,
3710 uint8_t proto
, uint16_t instance
,
3711 uint32_t session_id
, const uint8_t *data
,
3717 /* Check buffer size */
3718 if (STREAM_SIZE(zclient
->obuf
) <
3719 (ZEBRA_HEADER_SIZE
+ sizeof(struct zapi_opaque_msg
) + datasize
))
3720 return ZCLIENT_SEND_FAILURE
;
3725 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3727 /* Send sub-type and flags */
3728 SET_FLAG(flags
, ZAPI_OPAQUE_FLAG_UNICAST
);
3729 stream_putl(s
, type
);
3730 stream_putw(s
, flags
);
3732 /* Send destination client info */
3733 stream_putc(s
, proto
);
3734 stream_putw(s
, instance
);
3735 stream_putl(s
, session_id
);
3737 /* Send opaque data */
3738 stream_write(s
, data
, datasize
);
3740 /* Put length into the header at the start of the stream. */
3741 stream_putw_at(s
, 0, stream_get_endp(s
));
3743 return zclient_send_message(zclient
);
3747 * Decode incoming opaque message into info struct
3749 int zclient_opaque_decode(struct stream
*s
, struct zapi_opaque_msg
*info
)
3751 memset(info
, 0, sizeof(*info
));
3753 /* Decode subtype and flags */
3754 STREAM_GETL(s
, info
->type
);
3755 STREAM_GETW(s
, info
->flags
);
3757 /* Decode unicast client info if present */
3758 if (CHECK_FLAG(info
->flags
, ZAPI_OPAQUE_FLAG_UNICAST
)) {
3759 STREAM_GETC(s
, info
->proto
);
3760 STREAM_GETW(s
, info
->instance
);
3761 STREAM_GETL(s
, info
->session_id
);
3764 info
->len
= STREAM_READABLE(s
);
3774 * Send a registration request for opaque messages with a specified subtype.
3776 enum zclient_send_status
zclient_register_opaque(struct zclient
*zclient
,
3784 zclient_create_header(s
, ZEBRA_OPAQUE_REGISTER
, VRF_DEFAULT
);
3787 stream_putl(s
, type
);
3789 /* Add zclient info */
3790 stream_putc(s
, zclient
->redist_default
);
3791 stream_putw(s
, zclient
->instance
);
3792 stream_putl(s
, zclient
->session_id
);
3794 /* Put length at the first point of the stream. */
3795 stream_putw_at(s
, 0, stream_get_endp(s
));
3797 return zclient_send_message(zclient
);
3801 * Send an un-registration request for a specified opaque subtype.
3803 enum zclient_send_status
zclient_unregister_opaque(struct zclient
*zclient
,
3811 zclient_create_header(s
, ZEBRA_OPAQUE_UNREGISTER
, VRF_DEFAULT
);
3814 stream_putl(s
, type
);
3816 /* Add zclient info */
3817 stream_putc(s
, zclient
->redist_default
);
3818 stream_putw(s
, zclient
->instance
);
3819 stream_putl(s
, zclient
->session_id
);
3821 /* Put length at the first point of the stream. */
3822 stream_putw_at(s
, 0, stream_get_endp(s
));
3824 return zclient_send_message(zclient
);
3827 /* Utility to decode opaque registration info */
3828 int zapi_opaque_reg_decode(struct stream
*s
, struct zapi_opaque_reg_info
*info
)
3830 STREAM_GETL(s
, info
->type
);
3831 STREAM_GETC(s
, info
->proto
);
3832 STREAM_GETW(s
, info
->instance
);
3833 STREAM_GETL(s
, info
->session_id
);
3842 /* Utility to decode client close notify info */
3843 int zapi_client_close_notify_decode(struct stream
*s
,
3844 struct zapi_client_close_info
*info
)
3846 memset(info
, 0, sizeof(*info
));
3848 STREAM_GETC(s
, info
->proto
);
3849 STREAM_GETW(s
, info
->instance
);
3850 STREAM_GETL(s
, info
->session_id
);
3859 /* Zebra client message read function. */
3860 static int zclient_read(struct thread
*thread
)
3863 uint16_t length
, command
;
3864 uint8_t marker
, version
;
3866 struct zclient
*zclient
;
3868 /* Get socket to zebra. */
3869 zclient
= THREAD_ARG(thread
);
3870 zclient
->t_read
= NULL
;
3872 /* Read zebra header (if we don't have it already). */
3873 already
= stream_get_endp(zclient
->ibuf
);
3874 if (already
< ZEBRA_HEADER_SIZE
) {
3876 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
3877 ZEBRA_HEADER_SIZE
- already
))
3882 "zclient connection closed socket [%d].",
3884 return zclient_failed(zclient
);
3886 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
3887 zclient_event(ZCLIENT_READ
, zclient
);
3890 already
= ZEBRA_HEADER_SIZE
;
3893 /* Reset to read from the beginning of the incoming packet. */
3894 stream_set_getp(zclient
->ibuf
, 0);
3896 /* Fetch header values. */
3897 length
= stream_getw(zclient
->ibuf
);
3898 marker
= stream_getc(zclient
->ibuf
);
3899 version
= stream_getc(zclient
->ibuf
);
3900 vrf_id
= stream_getl(zclient
->ibuf
);
3901 command
= stream_getw(zclient
->ibuf
);
3903 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
3905 EC_LIB_ZAPI_MISSMATCH
,
3906 "%s: socket %d version mismatch, marker %d, version %d",
3907 __func__
, zclient
->sock
, marker
, version
);
3908 return zclient_failed(zclient
);
3911 if (length
< ZEBRA_HEADER_SIZE
) {
3912 flog_err(EC_LIB_ZAPI_MISSMATCH
,
3913 "%s: socket %d message length %u is less than %d ",
3914 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
3915 return zclient_failed(zclient
);
3919 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
3923 "%s: message size %u exceeds buffer size %lu, expanding...",
3925 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
3926 ns
= stream_new(length
);
3927 stream_copy(ns
, zclient
->ibuf
);
3928 stream_free(zclient
->ibuf
);
3932 /* Read rest of zebra packet. */
3933 if (already
< length
) {
3935 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
3941 "zclient connection closed socket [%d].",
3943 return zclient_failed(zclient
);
3945 if (nbyte
!= (ssize_t
)(length
- already
)) {
3946 /* Try again later. */
3947 zclient_event(ZCLIENT_READ
, zclient
);
3952 length
-= ZEBRA_HEADER_SIZE
;
3955 zlog_debug("zclient %p command %s VRF %u", zclient
,
3956 zserv_command_string(command
), vrf_id
);
3959 case ZEBRA_CAPABILITIES
:
3960 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
3962 case ZEBRA_ROUTER_ID_UPDATE
:
3963 if (zclient
->router_id_update
)
3964 (*zclient
->router_id_update
)(command
, zclient
, length
,
3968 zclient_vrf_add(zclient
, vrf_id
);
3970 case ZEBRA_VRF_DELETE
:
3971 zclient_vrf_delete(zclient
, vrf_id
);
3973 case ZEBRA_INTERFACE_ADD
:
3974 zclient_interface_add(zclient
, vrf_id
);
3976 case ZEBRA_INTERFACE_DELETE
:
3977 zclient_interface_delete(zclient
, vrf_id
);
3979 case ZEBRA_INTERFACE_ADDRESS_ADD
:
3980 if (zclient
->interface_address_add
)
3981 (*zclient
->interface_address_add
)(command
, zclient
,
3984 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
3985 if (zclient
->interface_address_delete
)
3986 (*zclient
->interface_address_delete
)(command
, zclient
,
3989 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
3990 if (zclient
->interface_bfd_dest_update
)
3991 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
3994 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
3995 if (zclient
->interface_nbr_address_add
)
3996 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
3999 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
4000 if (zclient
->interface_nbr_address_delete
)
4001 (*zclient
->interface_nbr_address_delete
)(
4002 command
, zclient
, length
, vrf_id
);
4004 case ZEBRA_INTERFACE_UP
:
4005 zclient_interface_up(zclient
, vrf_id
);
4007 case ZEBRA_INTERFACE_DOWN
:
4008 zclient_interface_down(zclient
, vrf_id
);
4010 case ZEBRA_INTERFACE_VRF_UPDATE
:
4011 if (zclient
->interface_vrf_update
)
4012 (*zclient
->interface_vrf_update
)(command
, zclient
,
4015 case ZEBRA_NEXTHOP_UPDATE
:
4017 zlog_debug("zclient rcvd nexthop update");
4018 if (zclient
->nexthop_update
)
4019 (*zclient
->nexthop_update
)(command
, zclient
, length
,
4022 case ZEBRA_BFD_DEST_REPLAY
:
4023 if (zclient
->bfd_dest_replay
)
4024 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
4027 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
4028 if (zclient
->redistribute_route_add
)
4029 (*zclient
->redistribute_route_add
)(command
, zclient
,
4032 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
4033 if (zclient
->redistribute_route_del
)
4034 (*zclient
->redistribute_route_del
)(command
, zclient
,
4037 case ZEBRA_INTERFACE_LINK_PARAMS
:
4038 if (zclient
->interface_link_params
)
4039 (*zclient
->interface_link_params
)(command
, zclient
,
4042 case ZEBRA_FEC_UPDATE
:
4044 zlog_debug("zclient rcvd fec update");
4045 if (zclient
->fec_update
)
4046 (*zclient
->fec_update
)(command
, zclient
, length
);
4048 case ZEBRA_LOCAL_ES_ADD
:
4049 if (zclient
->local_es_add
)
4050 (*zclient
->local_es_add
)(command
, zclient
, length
,
4053 case ZEBRA_LOCAL_ES_DEL
:
4054 if (zclient
->local_es_del
)
4055 (*zclient
->local_es_del
)(command
, zclient
, length
,
4058 case ZEBRA_LOCAL_ES_EVI_ADD
:
4059 if (zclient
->local_es_evi_add
)
4060 (*zclient
->local_es_evi_add
)(command
, zclient
, length
,
4063 case ZEBRA_LOCAL_ES_EVI_DEL
:
4064 if (zclient
->local_es_evi_del
)
4065 (*zclient
->local_es_evi_del
)(command
, zclient
, length
,
4069 if (zclient
->local_vni_add
)
4070 (*zclient
->local_vni_add
)(command
, zclient
, length
,
4074 if (zclient
->local_vni_del
)
4075 (*zclient
->local_vni_del
)(command
, zclient
, length
,
4078 case ZEBRA_L3VNI_ADD
:
4079 if (zclient
->local_l3vni_add
)
4080 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
4083 case ZEBRA_L3VNI_DEL
:
4084 if (zclient
->local_l3vni_del
)
4085 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
4088 case ZEBRA_MACIP_ADD
:
4089 if (zclient
->local_macip_add
)
4090 (*zclient
->local_macip_add
)(command
, zclient
, length
,
4093 case ZEBRA_MACIP_DEL
:
4094 if (zclient
->local_macip_del
)
4095 (*zclient
->local_macip_del
)(command
, zclient
, length
,
4098 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
4099 if (zclient
->local_ip_prefix_add
)
4100 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
4103 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
4104 if (zclient
->local_ip_prefix_del
)
4105 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
4108 case ZEBRA_PW_STATUS_UPDATE
:
4109 if (zclient
->pw_status_update
)
4110 (*zclient
->pw_status_update
)(command
, zclient
, length
,
4113 case ZEBRA_ROUTE_NOTIFY_OWNER
:
4114 if (zclient
->route_notify_owner
)
4115 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
4118 case ZEBRA_RULE_NOTIFY_OWNER
:
4119 if (zclient
->rule_notify_owner
)
4120 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
4123 case ZEBRA_NHG_NOTIFY_OWNER
:
4124 if (zclient
->nhg_notify_owner
)
4125 (*zclient
->nhg_notify_owner
)(command
, zclient
, length
,
4128 case ZEBRA_GET_LABEL_CHUNK
:
4129 if (zclient
->label_chunk
)
4130 (*zclient
->label_chunk
)(command
, zclient
, length
,
4133 case ZEBRA_IPSET_NOTIFY_OWNER
:
4134 if (zclient
->ipset_notify_owner
)
4135 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
4138 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
4139 if (zclient
->ipset_entry_notify_owner
)
4140 (*zclient
->ipset_entry_notify_owner
)(command
,
4144 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
4145 if (zclient
->iptable_notify_owner
)
4146 (*zclient
->iptable_notify_owner
)(command
,
4150 case ZEBRA_VXLAN_SG_ADD
:
4151 if (zclient
->vxlan_sg_add
)
4152 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
4155 case ZEBRA_VXLAN_SG_DEL
:
4156 if (zclient
->vxlan_sg_del
)
4157 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
4160 case ZEBRA_MLAG_PROCESS_UP
:
4161 zclient_mlag_process_up(command
, zclient
, length
, vrf_id
);
4163 case ZEBRA_MLAG_PROCESS_DOWN
:
4164 zclient_mlag_process_down(command
, zclient
, length
, vrf_id
);
4166 case ZEBRA_MLAG_FORWARD_MSG
:
4167 zclient_mlag_handle_msg(command
, zclient
, length
, vrf_id
);
4169 case ZEBRA_SRV6_LOCATOR_ADD
:
4170 if (zclient
->srv6_locator_add
)
4171 (*zclient
->srv6_locator_add
)(command
, zclient
, length
,
4174 case ZEBRA_SRV6_LOCATOR_DELETE
:
4175 if (zclient
->srv6_locator_delete
)
4176 (*zclient
->srv6_locator_delete
)(command
, zclient
,
4179 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
:
4180 if (zclient
->process_srv6_locator_chunk
)
4181 (*zclient
->process_srv6_locator_chunk
)(command
, zclient
,
4185 zclient_handle_error(command
, zclient
, length
, vrf_id
);
4187 case ZEBRA_OPAQUE_MESSAGE
:
4188 if (zclient
->opaque_msg_handler
)
4189 (*zclient
->opaque_msg_handler
)(command
, zclient
, length
,
4192 case ZEBRA_OPAQUE_REGISTER
:
4193 if (zclient
->opaque_register_handler
)
4194 (*zclient
->opaque_register_handler
)(command
, zclient
,
4197 case ZEBRA_OPAQUE_UNREGISTER
:
4198 if (zclient
->opaque_unregister_handler
)
4199 (*zclient
->opaque_unregister_handler
)(command
, zclient
,
4202 case ZEBRA_SR_POLICY_NOTIFY_STATUS
:
4203 if (zclient
->sr_policy_notify_status
)
4204 (*zclient
->sr_policy_notify_status
)(command
, zclient
,
4207 case ZEBRA_CLIENT_CLOSE_NOTIFY
:
4208 if (zclient
->zebra_client_close_notify
)
4209 (*zclient
->zebra_client_close_notify
)(command
, zclient
,
4212 case ZEBRA_NHRP_NEIGH_ADDED
:
4213 if (zclient
->neighbor_added
)
4214 (*zclient
->neighbor_added
)(command
, zclient
, length
,
4217 case ZEBRA_NHRP_NEIGH_REMOVED
:
4218 if (zclient
->neighbor_removed
)
4219 (*zclient
->neighbor_removed
)(command
, zclient
, length
,
4222 case ZEBRA_NHRP_NEIGH_GET
:
4223 if (zclient
->neighbor_get
)
4224 (*zclient
->neighbor_get
)(command
, zclient
, length
,
4227 case ZEBRA_GRE_UPDATE
:
4228 if (zclient
->gre_update
)
4229 (*zclient
->gre_update
)(command
, zclient
,
4236 if (zclient
->sock
< 0)
4237 /* Connection was closed during packet processing. */
4240 /* Register read thread. */
4241 stream_reset(zclient
->ibuf
);
4242 zclient_event(ZCLIENT_READ
, zclient
);
4247 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
4248 int type
, unsigned short instance
, vrf_id_t vrf_id
)
4252 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
4253 if (redist_check_instance(
4254 &zclient
->mi_redist
[afi
][type
], instance
))
4256 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
4259 if (!redist_check_instance(
4260 &zclient
->mi_redist
[afi
][type
], instance
))
4262 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
4267 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
4268 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
4271 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
4273 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
4276 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
4280 if (zclient
->sock
> 0)
4281 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
4286 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
4287 afi_t afi
, vrf_id_t vrf_id
)
4290 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
4291 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
4293 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
4295 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
4298 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
4301 if (zclient
->sock
> 0)
4302 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
4305 static void zclient_event(enum event event
, struct zclient
*zclient
)
4308 case ZCLIENT_SCHEDULE
:
4309 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
4310 &zclient
->t_connect
);
4312 case ZCLIENT_CONNECT
:
4315 "zclient connect failures: %d schedule interval is now %d",
4316 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
4317 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
4318 zclient
->fail
< 3 ? 10 : 60,
4319 &zclient
->t_connect
);
4322 zclient
->t_read
= NULL
;
4323 thread_add_read(zclient
->master
, zclient_read
, zclient
,
4324 zclient
->sock
, &zclient
->t_read
);
4329 enum zclient_send_status
zclient_interface_set_master(struct zclient
*client
,
4330 struct interface
*master
,
4331 struct interface
*slave
)
4338 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
4340 stream_putl(s
, master
->vrf_id
);
4341 stream_putl(s
, master
->ifindex
);
4342 stream_putl(s
, slave
->vrf_id
);
4343 stream_putl(s
, slave
->ifindex
);
4345 stream_putw_at(s
, 0, stream_get_endp(s
));
4346 return zclient_send_message(client
);
4350 * Send capabilities message to zebra
4352 enum zclient_send_status
zclient_capabilities_send(uint32_t cmd
,
4353 struct zclient
*zclient
,
4354 struct zapi_cap
*api
)
4359 if (zclient
== NULL
)
4360 return ZCLIENT_SEND_FAILURE
;
4364 zclient_create_header(s
, cmd
, 0);
4365 stream_putl(s
, api
->cap
);
4368 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4369 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4370 stream_putl(s
, api
->stale_removal_time
);
4371 stream_putl(s
, api
->vrf_id
);
4373 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4374 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4375 stream_putl(s
, api
->afi
);
4376 stream_putl(s
, api
->safi
);
4377 stream_putl(s
, api
->vrf_id
);
4379 case ZEBRA_CLIENT_GR_DISABLE
:
4380 stream_putl(s
, api
->vrf_id
);
4384 /* Put length at the first point of the stream */
4385 stream_putw_at(s
, 0, stream_get_endp(s
));
4387 return zclient_send_message(zclient
);
4391 * Process capabilities message from zebra
4393 int32_t zapi_capabilities_decode(struct stream
*s
, struct zapi_cap
*api
)
4396 memset(api
, 0, sizeof(*api
));
4398 STREAM_GETL(s
, api
->cap
);
4400 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4401 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4402 STREAM_GETL(s
, api
->stale_removal_time
);
4403 STREAM_GETL(s
, api
->vrf_id
);
4405 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4406 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4407 STREAM_GETL(s
, api
->afi
);
4408 STREAM_GETL(s
, api
->safi
);
4409 STREAM_GETL(s
, api
->vrf_id
);
4411 case ZEBRA_CLIENT_GR_DISABLE
:
4412 STREAM_GETL(s
, api
->vrf_id
);
4419 enum zclient_send_status
4420 zclient_send_neigh_discovery_req(struct zclient
*zclient
,
4421 const struct interface
*ifp
,
4422 const struct prefix
*p
)
4429 zclient_create_header(s
, ZEBRA_NEIGH_DISCOVER
, ifp
->vrf_id
);
4430 stream_putl(s
, ifp
->ifindex
);
4432 stream_putc(s
, p
->family
);
4433 stream_putc(s
, p
->prefixlen
);
4434 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
4436 stream_putw_at(s
, 0, stream_get_endp(s
));
4437 return zclient_send_message(zclient
);
4441 * Get a starting nhg point for a routing protocol
4443 uint32_t zclient_get_nhg_start(uint32_t proto
)
4445 assert(proto
< ZEBRA_ROUTE_MAX
);
4447 return ZEBRA_NHG_PROTO_SPACING
* proto
;
4450 char *zclient_dump_route_flags(uint32_t flags
, char *buf
, size_t len
)
4453 snprintfrr(buf
, len
, "None ");
4458 buf
, len
, "%s%s%s%s%s%s%s%s%s%s",
4459 CHECK_FLAG(flags
, ZEBRA_FLAG_ALLOW_RECURSION
) ? "Recursion "
4461 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) ? "Self " : "",
4462 CHECK_FLAG(flags
, ZEBRA_FLAG_IBGP
) ? "iBGP " : "",
4463 CHECK_FLAG(flags
, ZEBRA_FLAG_SELECTED
) ? "Selected " : "",
4464 CHECK_FLAG(flags
, ZEBRA_FLAG_FIB_OVERRIDE
) ? "Override " : "",
4465 CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
) ? "Evpn " : "",
4466 CHECK_FLAG(flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) ? "RR Distance "
4468 CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
) ? "Trapped " : "",
4469 CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
) ? "Offloaded " : "",
4470 CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
) ? "Offload Failed "
4475 char *zclient_evpn_dump_macip_flags(uint8_t flags
, char *buf
, size_t len
)
4478 snprintfrr(buf
, len
, "None ");
4483 buf
, len
, "%s%s%s%s%s%s%s",
4484 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? "Sticky MAC " : "",
4485 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? "Gateway MAC " : "",
4486 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? "Router "
4488 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_OVERRIDE_FLAG
) ? "Override "
4490 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
) ? "SVI MAC " : "",
4491 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
) ? "Proxy "
4493 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
) ? "Sync " : "");
4498 static int zclient_neigh_ip_read_entry(struct stream
*s
, struct ipaddr
*add
)
4502 STREAM_GETC(s
, family
);
4503 if (family
!= AF_INET
&& family
!= AF_INET6
)
4506 STREAM_GET(&add
->ip
.addr
, s
, family2addrsize(family
));
4507 add
->ipa_type
= family
;
4513 int zclient_neigh_ip_encode(struct stream
*s
, uint16_t cmd
, union sockunion
*in
,
4514 union sockunion
*out
, struct interface
*ifp
,
4519 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
4520 stream_putc(s
, sockunion_family(in
));
4521 stream_write(s
, sockunion_get_addr(in
), sockunion_get_addrlen(in
));
4522 if (out
&& sockunion_family(out
) != AF_UNSPEC
) {
4523 stream_putc(s
, sockunion_family(out
));
4524 stream_write(s
, sockunion_get_addr(out
),
4525 sockunion_get_addrlen(out
));
4527 stream_putc(s
, AF_UNSPEC
);
4528 stream_putl(s
, ifp
->ifindex
);
4530 stream_putl(s
, ndm_state
);
4532 stream_putl(s
, ZEBRA_NEIGH_STATE_FAILED
);
4536 int zclient_neigh_ip_decode(struct stream
*s
, struct zapi_neigh_ip
*api
)
4540 ret
= zclient_neigh_ip_read_entry(s
, &api
->ip_in
);
4543 zclient_neigh_ip_read_entry(s
, &api
->ip_out
);
4545 STREAM_GETL(s
, api
->index
);
4546 STREAM_GETL(s
, api
->ndm_state
);
4552 int zclient_send_zebra_gre_request(struct zclient
*client
,
4553 struct interface
*ifp
)
4557 if (!client
|| client
->sock
< 0) {
4558 zlog_err("%s : zclient not ready", __func__
);
4563 zclient_create_header(s
,
4566 stream_putl(s
, ifp
->ifindex
);
4567 stream_putw_at(s
, 0, stream_get_endp(s
));
4568 zclient_send_message(client
);