1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Zebra's client library.
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 * Copyright (C) 2005 Andrew J. Schorr
26 #include "nexthop_group.h"
27 #include "lib_errors.h"
32 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient");
33 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs");
35 /* Zebra client events. */
36 enum zclient_event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
38 /* Prototype for event manager. */
39 static void zclient_event(enum zclient_event
, struct zclient
*);
41 static void zebra_interface_if_set_value(struct stream
*s
,
42 struct interface
*ifp
);
44 struct zclient_options zclient_options_default
= {.receive_notify
= false,
45 .synchronous
= false};
47 struct sockaddr_storage zclient_addr
;
48 socklen_t zclient_addr_len
;
50 /* This file local debug flag. */
51 static int zclient_debug
;
53 /* Allocate zclient structure. */
54 struct zclient
*zclient_new(struct event_loop
*master
,
55 struct zclient_options
*opt
,
56 zclient_handler
*const *handlers
, size_t n_handlers
)
58 struct zclient
*zclient
;
60 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
62 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
64 zclient
->ibuf
= stream_new(stream_size
);
65 zclient
->obuf
= stream_new(stream_size
);
66 zclient
->wb
= buffer_new(0);
67 zclient
->master
= master
;
69 zclient
->handlers
= handlers
;
70 zclient
->n_handlers
= n_handlers
;
72 zclient
->receive_notify
= opt
->receive_notify
;
73 zclient
->synchronous
= opt
->synchronous
;
78 /* This function is only called when exiting, because
79 many parts of the code do not check for I/O errors, so they could
80 reference an invalid pointer if the structure was ever freed.
82 Free zclient structure. */
83 void zclient_free(struct zclient
*zclient
)
86 stream_free(zclient
->ibuf
);
88 stream_free(zclient
->obuf
);
90 buffer_free(zclient
->wb
);
92 XFREE(MTYPE_ZCLIENT
, zclient
);
95 unsigned short *redist_check_instance(struct redist_proto
*red
,
96 unsigned short instance
)
98 struct listnode
*node
;
104 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
111 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
118 red
->instances
= list_new();
120 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
122 listnode_add(red
->instances
, in
);
125 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
129 id
= redist_check_instance(red
, instance
);
133 listnode_delete(red
->instances
, id
);
134 XFREE(MTYPE_REDIST_INST
, id
);
135 if (!red
->instances
->count
) {
137 list_delete(&red
->instances
);
141 void redist_del_all_instances(struct redist_proto
*red
)
143 struct listnode
*ln
, *nn
;
149 for (ALL_LIST_ELEMENTS(red
->instances
, ln
, nn
, id
))
150 redist_del_instance(red
, *id
);
153 /* Stop zebra client services. */
154 void zclient_stop(struct zclient
*zclient
)
160 zlog_debug("zclient %p stopped", zclient
);
163 EVENT_OFF(zclient
->t_read
);
164 EVENT_OFF(zclient
->t_connect
);
165 EVENT_OFF(zclient
->t_write
);
168 stream_reset(zclient
->ibuf
);
169 stream_reset(zclient
->obuf
);
171 /* Empty the write buffer. */
172 buffer_reset(zclient
->wb
);
175 if (zclient
->sock
>= 0) {
176 close(zclient
->sock
);
181 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
182 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
183 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
184 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
187 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
190 vrf_bitmap_free(zclient
->default_information
[afi
]);
191 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
195 void zclient_reset(struct zclient
*zclient
)
199 zclient_stop(zclient
);
201 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
203 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
206 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
211 * Connect to zebra daemon.
212 * @param zclient a pointer to zclient structure
213 * @return socket fd just to make sure that connection established
217 int zclient_socket_connect(struct zclient
*zclient
)
222 /* We should think about IPv6 connection. */
223 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
228 setsockopt_so_sendbuf(sock
, 1048576);
230 /* Connect to zebra. */
231 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
234 zlog_debug("%s connect failure: %d(%s)", __func__
,
235 errno
, safe_strerror(errno
));
240 zclient
->sock
= sock
;
244 static enum zclient_send_status
zclient_failed(struct zclient
*zclient
)
247 zclient_stop(zclient
);
248 zclient_event(ZCLIENT_CONNECT
, zclient
);
249 return ZCLIENT_SEND_FAILURE
;
252 static void zclient_flush_data(struct event
*thread
)
254 struct zclient
*zclient
= EVENT_ARG(thread
);
256 zclient
->t_write
= NULL
;
257 if (zclient
->sock
< 0)
259 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
263 "%s: buffer_flush_available failed on zclient fd %d, closing",
264 __func__
, zclient
->sock
);
265 zclient_failed(zclient
);
268 zclient
->t_write
= NULL
;
269 event_add_write(zclient
->master
, zclient_flush_data
, zclient
,
270 zclient
->sock
, &zclient
->t_write
);
273 if (zclient
->zebra_buffer_write_ready
)
274 (*zclient
->zebra_buffer_write_ready
)();
281 * ZCLIENT_SEND_FAILED - is a failure
282 * ZCLIENT_SEND_SUCCESS - means we sent data to zebra
283 * ZCLIENT_SEND_BUFFERED - means we are buffering
285 enum zclient_send_status
zclient_send_message(struct zclient
*zclient
)
287 if (zclient
->sock
< 0)
288 return ZCLIENT_SEND_FAILURE
;
289 switch (buffer_write(zclient
->wb
, zclient
->sock
,
290 STREAM_DATA(zclient
->obuf
),
291 stream_get_endp(zclient
->obuf
))) {
293 flog_err(EC_LIB_ZAPI_SOCKET
,
294 "%s: buffer_write failed to zclient fd %d, closing",
295 __func__
, zclient
->sock
);
296 return zclient_failed(zclient
);
298 EVENT_OFF(zclient
->t_write
);
299 return ZCLIENT_SEND_SUCCESS
;
301 event_add_write(zclient
->master
, zclient_flush_data
, zclient
,
302 zclient
->sock
, &zclient
->t_write
);
303 return ZCLIENT_SEND_BUFFERED
;
306 /* should not get here */
307 return ZCLIENT_SEND_SUCCESS
;
311 * If we add more data to this structure please ensure that
312 * struct zmsghdr in lib/zclient.h is updated as appropriate.
314 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
316 /* length placeholder, caller can update */
317 stream_putw(s
, ZEBRA_HEADER_SIZE
);
318 stream_putc(s
, ZEBRA_HEADER_MARKER
);
319 stream_putc(s
, ZSERV_VERSION
);
320 stream_putl(s
, vrf_id
);
321 stream_putw(s
, command
);
324 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
325 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
328 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
331 STREAM_GETW(s
, *size
);
332 *size
-= ZEBRA_HEADER_SIZE
;
333 STREAM_GETC(s
, *marker
);
334 STREAM_GETC(s
, *version
);
335 STREAM_GETL(s
, *vrf_id
);
336 STREAM_GETW(s
, *cmd
);
338 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
340 EC_LIB_ZAPI_MISSMATCH
,
341 "%s: socket %d version mismatch, marker %d, version %d",
342 __func__
, sock
, *marker
, *version
);
346 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
354 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
356 STREAM_GETW(zmsg
, hdr
->length
);
357 STREAM_GETC(zmsg
, hdr
->marker
);
358 STREAM_GETC(zmsg
, hdr
->version
);
359 STREAM_GETL(zmsg
, hdr
->vrf_id
);
360 STREAM_GETW(zmsg
, hdr
->command
);
366 /* Send simple Zebra message. */
367 static enum zclient_send_status
zebra_message_send(struct zclient
*zclient
,
368 int command
, vrf_id_t vrf_id
)
372 /* Get zclient output buffer. */
376 /* Send very simple command only Zebra message. */
377 zclient_create_header(s
, command
, vrf_id
);
379 return zclient_send_message(zclient
);
382 enum zclient_send_status
zclient_send_hello(struct zclient
*zclient
)
386 if (zclient
->redist_default
|| zclient
->synchronous
) {
390 /* The VRF ID in the HELLO message is always 0. */
391 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
392 stream_putc(s
, zclient
->redist_default
);
393 stream_putw(s
, zclient
->instance
);
394 stream_putl(s
, zclient
->session_id
);
395 if (zclient
->receive_notify
)
399 if (zclient
->synchronous
)
404 stream_putw_at(s
, 0, stream_get_endp(s
));
405 return zclient_send_message(zclient
);
408 return ZCLIENT_SEND_SUCCESS
;
411 enum zclient_send_status
zclient_send_vrf_label(struct zclient
*zclient
,
412 vrf_id_t vrf_id
, afi_t afi
,
414 enum lsp_types_t ltype
)
421 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
422 stream_putl(s
, label
);
424 stream_putc(s
, ltype
);
425 stream_putw_at(s
, 0, stream_get_endp(s
));
426 return zclient_send_message(zclient
);
429 enum zclient_send_status
zclient_send_localsid(struct zclient
*zclient
,
430 const struct in6_addr
*sid
, ifindex_t oif
,
431 enum seg6local_action_t action
,
432 const struct seg6local_context
*context
)
434 struct prefix_ipv6 p
= {};
435 struct zapi_route api
= {};
436 struct zapi_nexthop
*znh
;
439 p
.prefixlen
= IPV6_MAX_BITLEN
;
442 api
.vrf_id
= VRF_DEFAULT
;
443 api
.type
= zclient
->redist_default
;
445 api
.safi
= SAFI_UNICAST
;
446 memcpy(&api
.prefix
, &p
, sizeof(p
));
448 if (action
== ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
449 return zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
451 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
452 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
454 znh
= &api
.nexthops
[0];
456 memset(znh
, 0, sizeof(*znh
));
458 znh
->type
= NEXTHOP_TYPE_IFINDEX
;
460 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
);
461 znh
->seg6local_action
= action
;
462 memcpy(&znh
->seg6local_ctx
, context
, sizeof(struct seg6local_context
));
466 return zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
469 /* Send register requests to zebra daemon for the information in a VRF. */
470 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
475 /* If not connected to the zebra yet. */
476 if (zclient
->sock
< 0)
480 zlog_debug("%s: send register messages for VRF %u", __func__
,
483 /* We need router-id information. */
484 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_ADD
, AFI_IP
,
487 /* We need interface information. */
488 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
490 /* Set unwanted redistribute route. */
491 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
492 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
495 /* Flush all redistribute request. */
496 if (vrf_id
== VRF_DEFAULT
) {
497 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
498 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
499 if (!zclient
->mi_redist
[afi
][i
].enabled
)
502 struct listnode
*node
;
505 for (ALL_LIST_ELEMENTS_RO(
506 zclient
->mi_redist
[afi
][i
]
509 if (!(i
== zclient
->redist_default
510 && *id
== zclient
->instance
))
511 zebra_redistribute_send(
512 ZEBRA_REDISTRIBUTE_ADD
,
513 zclient
, afi
, i
, *id
,
519 /* Resend all redistribute request. */
520 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
521 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
522 if (i
!= zclient
->redist_default
523 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
525 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
529 /* If default information is needed. */
530 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
531 zebra_redistribute_default_send(
532 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
537 /* Send unregister requests to zebra daemon for the information in a VRF. */
538 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
543 /* If not connected to the zebra yet. */
544 if (zclient
->sock
< 0)
548 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
551 /* We need router-id information. */
552 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_DELETE
, AFI_IP
,
555 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
557 /* Set unwanted redistribute route. */
558 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
559 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
562 /* Flush all redistribute request. */
563 if (vrf_id
== VRF_DEFAULT
) {
564 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
565 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
566 if (!zclient
->mi_redist
[afi
][i
].enabled
)
569 struct listnode
*node
;
572 for (ALL_LIST_ELEMENTS_RO(
573 zclient
->mi_redist
[afi
][i
]
576 if (!(i
== zclient
->redist_default
577 && *id
== zclient
->instance
))
578 zebra_redistribute_send(
579 ZEBRA_REDISTRIBUTE_DELETE
,
580 zclient
, afi
, i
, *id
,
586 /* Flush all redistribute request. */
587 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
588 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
589 if (i
!= zclient
->redist_default
590 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
592 zebra_redistribute_send(
593 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
596 /* If default information is needed. */
597 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
598 zebra_redistribute_default_send(
599 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
604 enum zclient_send_status
605 zclient_send_router_id_update(struct zclient
*zclient
,
606 zebra_message_types_t type
, afi_t afi
,
609 struct stream
*s
= zclient
->obuf
;
611 zclient_create_header(s
, type
, vrf_id
);
613 stream_putw_at(s
, 0, stream_get_endp(s
));
614 return zclient_send_message(zclient
);
617 /* Send request to zebra daemon to start or stop RA. */
618 enum zclient_send_status
619 zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
620 struct interface
*ifp
, int enable
,
621 uint32_t ra_interval
)
625 /* If not connected to the zebra yet. */
626 if (zclient
->sock
< 0)
627 return ZCLIENT_SEND_FAILURE
;
629 /* Form and send message. */
634 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
636 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
638 stream_putl(s
, ifp
->ifindex
);
639 stream_putl(s
, ra_interval
);
641 stream_putw_at(s
, 0, stream_get_endp(s
));
643 return zclient_send_message(zclient
);
646 enum zclient_send_status
647 zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
648 struct interface
*ifp
, bool down
)
652 if (zclient
->sock
< 0)
653 return ZCLIENT_SEND_FAILURE
;
657 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
658 stream_putl(s
, ifp
->ifindex
);
659 stream_putc(s
, !!down
);
660 stream_putw_at(s
, 0, stream_get_endp(s
));
661 return zclient_send_message(zclient
);
664 /* Make connection to zebra daemon. */
665 int zclient_start(struct zclient
*zclient
)
668 zlog_info("zclient_start is called");
670 /* If already connected to the zebra. */
671 if (zclient
->sock
>= 0)
674 /* Check connect thread. */
675 if (zclient
->t_connect
)
678 if (zclient_socket_connect(zclient
) < 0) {
680 zlog_debug("zclient connection fail");
682 zclient_event(ZCLIENT_CONNECT
, zclient
);
686 if (set_nonblocking(zclient
->sock
) < 0)
687 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
688 __func__
, zclient
->sock
);
690 /* Clear fail count. */
693 zlog_debug("zclient connect success with socket [%d]",
696 /* Create read thread. */
697 zclient_event(ZCLIENT_READ
, zclient
);
699 zclient_send_hello(zclient
);
701 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
703 /* Inform the successful connection. */
704 if (zclient
->zebra_connected
)
705 (*zclient
->zebra_connected
)(zclient
);
710 /* Initialize zebra client. Argument redist_default is unwanted
711 redistribute route type. */
712 void zclient_init(struct zclient
*zclient
, int redist_default
,
713 unsigned short instance
, struct zebra_privs_t
*privs
)
717 /* Set -1 to the default socket value. */
719 zclient
->privs
= privs
;
721 /* Clear redistribution flags. */
722 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
723 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
724 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
726 /* Set unwanted redistribute route. bgpd does not need BGP route
728 zclient
->redist_default
= redist_default
;
729 zclient
->instance
= instance
;
730 /* Pending: make afi(s) an arg. */
731 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
732 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
735 /* Set default-information redistribute to zero. */
736 zclient
->default_information
[afi
] = vrf_bitmap_init();
740 zlog_debug("scheduling zclient connection");
742 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
745 /* This function is a wrapper function for calling zclient_start from
746 timer or event thread. */
747 static void zclient_connect(struct event
*t
)
749 struct zclient
*zclient
;
751 zclient
= EVENT_ARG(t
);
752 zclient
->t_connect
= NULL
;
755 zlog_debug("zclient_connect is called");
757 zclient_start(zclient
);
760 enum zclient_send_status
zclient_send_rnh(struct zclient
*zclient
, int command
,
761 const struct prefix
*p
, safi_t safi
,
762 bool connected
, bool resolve_via_def
,
769 zclient_create_header(s
, command
, vrf_id
);
770 stream_putc(s
, (connected
) ? 1 : 0);
771 stream_putc(s
, (resolve_via_def
) ? 1 : 0);
772 stream_putw(s
, safi
);
773 stream_putw(s
, PREFIX_FAMILY(p
));
774 stream_putc(s
, p
->prefixlen
);
775 switch (PREFIX_FAMILY(p
)) {
777 stream_put_in_addr(s
, &p
->u
.prefix4
);
780 stream_put(s
, &(p
->u
.prefix6
), 16);
785 stream_putw_at(s
, 0, stream_get_endp(s
));
787 return zclient_send_message(zclient
);
791 * "xdr_encode"-like interface that allows daemon (client) to send
792 * a message to zebra server for a route that needs to be
793 * added/deleted to the kernel. Info about the route is specified
794 * by the caller in a struct zapi_route. zapi_route_encode() then writes
795 * the info down the zclient socket using the stream_* functions.
797 * The corresponding read ("xdr_decode") function on the server
798 * side is zapi_route_decode().
800 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
803 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as a 4
806 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
808 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
810 * XXX: No attention paid to alignment.
812 enum zclient_send_status
813 zclient_route_send(uint8_t cmd
, struct zclient
*zclient
, struct zapi_route
*api
)
815 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
816 return ZCLIENT_SEND_FAILURE
;
817 return zclient_send_message(zclient
);
820 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
821 const struct zapi_nexthop
*next2
)
823 if (next1
->label_num
> next2
->label_num
)
826 if (next1
->label_num
< next2
->label_num
)
829 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
832 static int zapi_nexthop_srv6_cmp(const struct zapi_nexthop
*next1
,
833 const struct zapi_nexthop
*next2
)
837 ret
= memcmp(&next1
->seg6_segs
, &next2
->seg6_segs
,
838 sizeof(struct in6_addr
));
842 if (next1
->seg6local_action
> next2
->seg6local_action
)
845 if (next1
->seg6local_action
< next2
->seg6local_action
)
848 return memcmp(&next1
->seg6local_ctx
, &next2
->seg6local_ctx
,
849 sizeof(struct seg6local_context
));
852 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
853 const struct zapi_nexthop
*next2
)
857 if (next1
->vrf_id
< next2
->vrf_id
)
860 if (next1
->vrf_id
> next2
->vrf_id
)
863 if (next1
->type
< next2
->type
)
866 if (next1
->type
> next2
->type
)
869 if (next1
->weight
< next2
->weight
)
872 if (next1
->weight
> next2
->weight
)
875 switch (next1
->type
) {
876 case NEXTHOP_TYPE_IPV4
:
877 case NEXTHOP_TYPE_IPV6
:
878 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
883 case NEXTHOP_TYPE_IPV4_IFINDEX
:
884 case NEXTHOP_TYPE_IPV6_IFINDEX
:
885 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
889 /* Intentional Fall-Through */
890 case NEXTHOP_TYPE_IFINDEX
:
891 if (next1
->ifindex
< next2
->ifindex
)
894 if (next1
->ifindex
> next2
->ifindex
)
897 case NEXTHOP_TYPE_BLACKHOLE
:
898 if (next1
->bh_type
< next2
->bh_type
)
901 if (next1
->bh_type
> next2
->bh_type
)
906 if (next1
->srte_color
< next2
->srte_color
)
908 if (next1
->srte_color
> next2
->srte_color
)
911 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) ||
912 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
914 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
915 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
918 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
919 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
922 if (next1
->backup_num
> 0 || next2
->backup_num
> 0) {
924 if (next1
->backup_num
< next2
->backup_num
)
927 if (next1
->backup_num
> next2
->backup_num
)
930 ret
= memcmp(next1
->backup_idx
,
931 next2
->backup_idx
, next1
->backup_num
);
940 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
944 const struct zapi_nexthop
*next1
= item1
;
945 const struct zapi_nexthop
*next2
= item2
;
947 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
951 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
955 ret
= zapi_nexthop_srv6_cmp(next1
, next2
);
960 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
961 uint16_t nexthop_num
)
963 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
968 * Encode a single zapi nexthop
970 int zapi_nexthop_encode(struct stream
*s
, const struct zapi_nexthop
*api_nh
,
971 uint32_t api_flags
, uint32_t api_message
)
974 int nh_flags
= api_nh
->flags
;
976 stream_putl(s
, api_nh
->vrf_id
);
977 stream_putc(s
, api_nh
->type
);
979 /* If needed, set 'labelled nexthop' flag */
980 if (api_nh
->label_num
> 0) {
981 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
983 /* Validate label count */
984 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
990 /* If present, set 'weight' flag before encoding flags */
992 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
);
994 /* Note that we're only encoding a single octet */
995 stream_putc(s
, nh_flags
);
997 switch (api_nh
->type
) {
998 case NEXTHOP_TYPE_BLACKHOLE
:
999 stream_putc(s
, api_nh
->bh_type
);
1001 case NEXTHOP_TYPE_IPV4
:
1002 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1003 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1004 stream_putl(s
, api_nh
->ifindex
);
1006 case NEXTHOP_TYPE_IFINDEX
:
1007 stream_putl(s
, api_nh
->ifindex
);
1009 case NEXTHOP_TYPE_IPV6
:
1010 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1011 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1013 stream_putl(s
, api_nh
->ifindex
);
1017 /* We only encode labels if we have >0 - we use
1018 * the per-nexthop flag above to signal that the count
1019 * is present in the payload.
1021 if (api_nh
->label_num
> 0) {
1022 stream_putc(s
, api_nh
->label_num
);
1023 stream_putc(s
, api_nh
->label_type
);
1024 stream_put(s
, &api_nh
->labels
[0],
1025 api_nh
->label_num
* sizeof(mpls_label_t
));
1029 stream_putl(s
, api_nh
->weight
);
1031 /* Router MAC for EVPN routes. */
1032 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_EVPN
))
1033 stream_put(s
, &(api_nh
->rmac
),
1034 sizeof(struct ethaddr
));
1036 /* Color for Segment Routing TE. */
1037 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1038 stream_putl(s
, api_nh
->srte_color
);
1040 /* Index of backup nexthop */
1041 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1042 /* Validate backup count */
1043 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1048 stream_putc(s
, api_nh
->backup_num
);
1049 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1050 stream_putc(s
, api_nh
->backup_idx
[i
]);
1053 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)) {
1054 stream_putl(s
, api_nh
->seg6local_action
);
1055 stream_write(s
, &api_nh
->seg6local_ctx
,
1056 sizeof(struct seg6local_context
));
1059 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_SEG6
))
1060 stream_write(s
, &api_nh
->seg6_segs
,
1061 sizeof(struct in6_addr
));
1067 int zapi_srv6_locator_chunk_encode(struct stream
*s
,
1068 const struct srv6_locator_chunk
*c
)
1070 stream_putw(s
, strlen(c
->locator_name
));
1071 stream_put(s
, c
->locator_name
, strlen(c
->locator_name
));
1072 stream_putw(s
, c
->prefix
.prefixlen
);
1073 stream_put(s
, &c
->prefix
.prefix
, sizeof(c
->prefix
.prefix
));
1074 stream_putc(s
, c
->block_bits_length
);
1075 stream_putc(s
, c
->node_bits_length
);
1076 stream_putc(s
, c
->function_bits_length
);
1077 stream_putc(s
, c
->argument_bits_length
);
1078 stream_putc(s
, c
->flags
);
1082 int zapi_srv6_locator_chunk_decode(struct stream
*s
,
1083 struct srv6_locator_chunk
*c
)
1087 c
->prefix
.family
= AF_INET6
;
1089 STREAM_GETW(s
, len
);
1090 if (len
> SRV6_LOCNAME_SIZE
)
1091 goto stream_failure
;
1093 STREAM_GET(c
->locator_name
, s
, len
);
1094 STREAM_GETW(s
, c
->prefix
.prefixlen
);
1095 STREAM_GET(&c
->prefix
.prefix
, s
, sizeof(c
->prefix
.prefix
));
1096 STREAM_GETC(s
, c
->block_bits_length
);
1097 STREAM_GETC(s
, c
->node_bits_length
);
1098 STREAM_GETC(s
, c
->function_bits_length
);
1099 STREAM_GETC(s
, c
->argument_bits_length
);
1100 STREAM_GETC(s
, c
->flags
);
1107 int zapi_srv6_locator_encode(struct stream
*s
, const struct srv6_locator
*l
)
1109 stream_putw(s
, strlen(l
->name
));
1110 stream_put(s
, l
->name
, strlen(l
->name
));
1111 stream_putw(s
, l
->prefix
.prefixlen
);
1112 stream_put(s
, &l
->prefix
.prefix
, sizeof(l
->prefix
.prefix
));
1116 int zapi_srv6_locator_decode(struct stream
*s
, struct srv6_locator
*l
)
1120 STREAM_GETW(s
, len
);
1121 if (len
> SRV6_LOCNAME_SIZE
)
1122 goto stream_failure
;
1124 STREAM_GET(l
->name
, s
, len
);
1125 STREAM_GETW(s
, l
->prefix
.prefixlen
);
1126 STREAM_GET(&l
->prefix
.prefix
, s
, sizeof(l
->prefix
.prefix
));
1127 l
->prefix
.family
= AF_INET6
;
1134 static int zapi_nhg_encode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1138 if (cmd
!= ZEBRA_NHG_DEL
&& cmd
!= ZEBRA_NHG_ADD
) {
1139 flog_err(EC_LIB_ZAPI_ENCODE
,
1140 "%s: Specified zapi NHG command (%d) doesn't exist",
1145 if (api_nhg
->nexthop_num
>= MULTIPATH_NUM
||
1146 api_nhg
->backup_nexthop_num
>= MULTIPATH_NUM
) {
1147 flog_err(EC_LIB_ZAPI_ENCODE
,
1148 "%s: zapi NHG encode with invalid input", __func__
);
1153 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1155 stream_putw(s
, api_nhg
->proto
);
1156 stream_putl(s
, api_nhg
->id
);
1158 stream_putw(s
, api_nhg
->resilience
.buckets
);
1159 stream_putl(s
, api_nhg
->resilience
.idle_timer
);
1160 stream_putl(s
, api_nhg
->resilience
.unbalanced_timer
);
1162 if (cmd
== ZEBRA_NHG_ADD
) {
1164 zapi_nexthop_group_sort(api_nhg
->nexthops
,
1165 api_nhg
->nexthop_num
);
1167 stream_putw(s
, api_nhg
->nexthop_num
);
1169 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++)
1170 zapi_nexthop_encode(s
, &api_nhg
->nexthops
[i
], 0, 0);
1172 /* Backup nexthops */
1173 stream_putw(s
, api_nhg
->backup_nexthop_num
);
1175 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++)
1176 zapi_nexthop_encode(s
, &api_nhg
->backup_nexthops
[i
], 0,
1180 stream_putw_at(s
, 0, stream_get_endp(s
));
1185 enum zclient_send_status
zclient_nhg_send(struct zclient
*zclient
, int cmd
,
1186 struct zapi_nhg
*api_nhg
)
1188 api_nhg
->proto
= zclient
->redist_default
;
1190 if (zapi_nhg_encode(zclient
->obuf
, cmd
, api_nhg
))
1193 return zclient_send_message(zclient
);
1196 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
1198 struct zapi_nexthop
*api_nh
;
1203 zclient_create_header(s
, cmd
, api
->vrf_id
);
1205 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1206 flog_err(EC_LIB_ZAPI_ENCODE
,
1207 "%s: Specified route type (%u) is not a legal value",
1208 __func__
, api
->type
);
1211 stream_putc(s
, api
->type
);
1213 stream_putw(s
, api
->instance
);
1214 stream_putl(s
, api
->flags
);
1215 stream_putl(s
, api
->message
);
1217 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1218 flog_err(EC_LIB_ZAPI_ENCODE
,
1219 "%s: Specified route SAFI (%u) is not a legal value",
1220 __func__
, api
->safi
);
1223 stream_putc(s
, api
->safi
);
1225 /* Put prefix information. */
1226 stream_putc(s
, api
->prefix
.family
);
1227 psize
= PSIZE(api
->prefix
.prefixlen
);
1228 stream_putc(s
, api
->prefix
.prefixlen
);
1229 stream_write(s
, &api
->prefix
.u
.prefix
, psize
);
1231 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1232 psize
= PSIZE(api
->src_prefix
.prefixlen
);
1233 stream_putc(s
, api
->src_prefix
.prefixlen
);
1234 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
1237 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1238 stream_putl(s
, api
->nhgid
);
1241 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1242 /* limit the number of nexthops if necessary */
1243 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1246 "%s: prefix %pFX: can't encode %u nexthops (maximum is %u)",
1247 __func__
, &api
->prefix
, api
->nexthop_num
,
1252 /* We canonicalize the nexthops by sorting them; this allows
1253 * zebra to resolve the list of nexthops to a nexthop-group
1256 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
1258 stream_putw(s
, api
->nexthop_num
);
1260 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1261 api_nh
= &api
->nexthops
[i
];
1263 /* MPLS labels for BGP-LU or Segment Routing */
1264 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1267 "%s: prefix %pFX: can't encode %u labels (maximum is %u)",
1268 __func__
, &api
->prefix
,
1269 api_nh
->label_num
, MPLS_MAX_LABELS
);
1273 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1280 /* Backup nexthops */
1281 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1282 /* limit the number of nexthops if necessary */
1283 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1286 "%s: prefix %pFX: can't encode %u backup nexthops (maximum is %u)",
1287 __func__
, &api
->prefix
, api
->backup_nexthop_num
,
1292 /* Note that we do not sort the list of backup nexthops -
1293 * this list is treated as an array and indexed by each
1294 * primary nexthop that is associated with a backup.
1297 stream_putw(s
, api
->backup_nexthop_num
);
1299 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1300 api_nh
= &api
->backup_nexthops
[i
];
1302 /* MPLS labels for BGP-LU or Segment Routing */
1303 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1306 "%s: prefix %pFX: backup: can't encode %u labels (maximum is %u)",
1307 __func__
, &api
->prefix
,
1308 api_nh
->label_num
, MPLS_MAX_LABELS
);
1312 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1320 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1321 stream_putc(s
, api
->distance
);
1322 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1323 stream_putl(s
, api
->metric
);
1324 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1325 stream_putl(s
, api
->tag
);
1326 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1327 stream_putl(s
, api
->mtu
);
1328 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1329 stream_putl(s
, api
->tableid
);
1331 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_OPAQUE
)) {
1332 if (api
->opaque
.length
> ZAPI_MESSAGE_OPAQUE_LENGTH
) {
1335 "%s: opaque length %u is greater than allowed value",
1336 __func__
, api
->opaque
.length
);
1340 stream_putw(s
, api
->opaque
.length
);
1341 stream_write(s
, api
->opaque
.data
, api
->opaque
.length
);
1343 /* Put length at the first point of the stream. */
1344 stream_putw_at(s
, 0, stream_get_endp(s
));
1350 * Decode a single zapi nexthop object
1352 int zapi_nexthop_decode(struct stream
*s
, struct zapi_nexthop
*api_nh
,
1353 uint32_t api_flags
, uint32_t api_message
)
1357 STREAM_GETL(s
, api_nh
->vrf_id
);
1358 STREAM_GETC(s
, api_nh
->type
);
1360 /* Note that we're only using a single octet of flags */
1361 STREAM_GETC(s
, api_nh
->flags
);
1363 switch (api_nh
->type
) {
1364 case NEXTHOP_TYPE_BLACKHOLE
:
1365 STREAM_GETC(s
, api_nh
->bh_type
);
1367 case NEXTHOP_TYPE_IPV4
:
1368 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1369 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1371 STREAM_GETL(s
, api_nh
->ifindex
);
1373 case NEXTHOP_TYPE_IFINDEX
:
1374 STREAM_GETL(s
, api_nh
->ifindex
);
1376 case NEXTHOP_TYPE_IPV6
:
1377 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1378 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1379 STREAM_GETL(s
, api_nh
->ifindex
);
1383 /* MPLS labels for BGP-LU or Segment Routing */
1384 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)) {
1385 STREAM_GETC(s
, api_nh
->label_num
);
1386 STREAM_GETC(s
, api_nh
->label_type
);
1387 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1390 "%s: invalid number of MPLS labels (%u)",
1391 __func__
, api_nh
->label_num
);
1395 STREAM_GET(&api_nh
->labels
[0], s
,
1396 api_nh
->label_num
* sizeof(mpls_label_t
));
1399 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1400 STREAM_GETL(s
, api_nh
->weight
);
1402 /* Router MAC for EVPN routes. */
1403 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
))
1404 STREAM_GET(&(api_nh
->rmac
), s
,
1405 sizeof(struct ethaddr
));
1407 /* Color for Segment Routing TE. */
1408 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1409 STREAM_GETL(s
, api_nh
->srte_color
);
1411 /* Backup nexthop index */
1412 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1413 STREAM_GETC(s
, api_nh
->backup_num
);
1415 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1418 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1419 STREAM_GETC(s
, api_nh
->backup_idx
[i
]);
1422 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)) {
1423 STREAM_GETL(s
, api_nh
->seg6local_action
);
1424 STREAM_GET(&api_nh
->seg6local_ctx
, s
,
1425 sizeof(struct seg6local_context
));
1428 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
))
1429 STREAM_GET(&api_nh
->seg6_segs
, s
,
1430 sizeof(struct in6_addr
));
1440 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1442 struct zapi_nexthop
*api_nh
;
1445 memset(api
, 0, sizeof(*api
));
1447 /* Type, flags, message. */
1448 STREAM_GETC(s
, api
->type
);
1449 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1450 flog_err(EC_LIB_ZAPI_ENCODE
,
1451 "%s: Specified route type: %d is not a legal value",
1452 __func__
, api
->type
);
1456 STREAM_GETW(s
, api
->instance
);
1457 STREAM_GETL(s
, api
->flags
);
1458 STREAM_GETL(s
, api
->message
);
1459 STREAM_GETC(s
, api
->safi
);
1460 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1461 flog_err(EC_LIB_ZAPI_ENCODE
,
1462 "%s: Specified route SAFI (%u) is not a legal value",
1463 __func__
, api
->safi
);
1468 STREAM_GETC(s
, api
->prefix
.family
);
1469 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1470 switch (api
->prefix
.family
) {
1472 if (api
->prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1475 "%s: V4 prefixlen is %d which should not be more than 32",
1476 __func__
, api
->prefix
.prefixlen
);
1481 if (api
->prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1484 "%s: v6 prefixlen is %d which should not be more than 128",
1485 __func__
, api
->prefix
.prefixlen
);
1490 flog_err(EC_LIB_ZAPI_ENCODE
,
1491 "%s: Specified family %d is not v4 or v6", __func__
,
1492 api
->prefix
.family
);
1495 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1497 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1498 api
->src_prefix
.family
= AF_INET6
;
1499 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1500 if (api
->src_prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1503 "%s: SRC Prefix prefixlen received: %d is too large",
1504 __func__
, api
->src_prefix
.prefixlen
);
1507 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1508 PSIZE(api
->src_prefix
.prefixlen
));
1510 if (api
->prefix
.family
!= AF_INET6
1511 || api
->src_prefix
.prefixlen
== 0) {
1514 "%s: SRC prefix specified in some manner that makes no sense",
1520 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1521 STREAM_GETL(s
, api
->nhgid
);
1524 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1525 STREAM_GETW(s
, api
->nexthop_num
);
1526 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1527 flog_err(EC_LIB_ZAPI_ENCODE
,
1528 "%s: invalid number of nexthops (%u)",
1529 __func__
, api
->nexthop_num
);
1533 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1534 api_nh
= &api
->nexthops
[i
];
1536 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1543 /* Backup nexthops. */
1544 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1545 STREAM_GETW(s
, api
->backup_nexthop_num
);
1546 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1547 flog_err(EC_LIB_ZAPI_ENCODE
,
1548 "%s: invalid number of backup nexthops (%u)",
1549 __func__
, api
->backup_nexthop_num
);
1553 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1554 api_nh
= &api
->backup_nexthops
[i
];
1556 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1564 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1565 STREAM_GETC(s
, api
->distance
);
1566 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1567 STREAM_GETL(s
, api
->metric
);
1568 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1569 STREAM_GETL(s
, api
->tag
);
1570 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1571 STREAM_GETL(s
, api
->mtu
);
1572 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1573 STREAM_GETL(s
, api
->tableid
);
1575 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_OPAQUE
)) {
1576 STREAM_GETW(s
, api
->opaque
.length
);
1577 if (api
->opaque
.length
> ZAPI_MESSAGE_OPAQUE_LENGTH
) {
1580 "%s: opaque length %u is greater than allowed value",
1581 __func__
, api
->opaque
.length
);
1585 STREAM_GET(api
->opaque
.data
, s
, api
->opaque
.length
);
1593 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1599 memset(&any
, 0, sizeof(any
));
1600 any
.family
= family
;
1604 stream_putc(s
, p
->family
);
1605 stream_putc(s
, p
->prefixlen
);
1606 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1609 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1612 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1615 * We are sending one item at a time at the moment
1619 stream_putl(s
, zrule
->seq
);
1620 stream_putl(s
, zrule
->priority
);
1621 stream_putl(s
, zrule
->unique
);
1623 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1624 zrule
->filter
.src_ip
.family
);
1625 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1626 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1627 zrule
->filter
.src_ip
.family
);
1628 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1629 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1631 stream_putl(s
, zrule
->action
.table
);
1632 stream_put(s
, zrule
->ifname
, INTERFACE_NAMSIZ
);
1634 /* Put length at the first point of the stream. */
1635 stream_putw_at(s
, 0, stream_get_endp(s
));
1640 int zapi_tc_qdisc_encode(uint8_t cmd
, struct stream
*s
, struct tc_qdisc
*qdisc
)
1643 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1648 stream_putl(s
, qdisc
->ifindex
);
1649 stream_putl(s
, qdisc
->kind
);
1651 stream_putw_at(s
, 0, stream_get_endp(s
));
1656 int zapi_tc_class_encode(uint8_t cmd
, struct stream
*s
, struct tc_class
*class)
1659 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1663 stream_putl(s
, class->ifindex
);
1664 stream_putl(s
, class->handle
);
1665 stream_putl(s
, class->kind
);
1667 switch (class->kind
) {
1669 stream_putq(s
, class->u
.htb
.rate
);
1670 stream_putq(s
, class->u
.htb
.ceil
);
1672 case TC_QDISC_UNSPEC
:
1673 case TC_QDISC_NOQUEUE
:
1674 /* not implemented */
1677 stream_putw_at(s
, 0, stream_get_endp(s
));
1682 int zapi_tc_filter_encode(uint8_t cmd
, struct stream
*s
,
1683 struct tc_filter
*filter
)
1686 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1690 stream_putl(s
, filter
->ifindex
);
1691 stream_putl(s
, filter
->handle
);
1692 stream_putl(s
, filter
->priority
);
1693 stream_putl(s
, filter
->protocol
);
1694 stream_putl(s
, filter
->kind
);
1696 switch (filter
->kind
) {
1697 case TC_FILTER_FLOWER
:
1698 stream_putl(s
, filter
->u
.flower
.filter_bm
);
1699 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_IP_PROTOCOL
)
1700 stream_putc(s
, filter
->u
.flower
.ip_proto
);
1701 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_SRC_IP
)
1702 zapi_encode_prefix(s
, &filter
->u
.flower
.src_ip
,
1703 filter
->u
.flower
.src_ip
.family
);
1704 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_SRC_PORT
) {
1705 stream_putw(s
, filter
->u
.flower
.src_port_min
);
1706 stream_putw(s
, filter
->u
.flower
.src_port_max
);
1708 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_DST_IP
)
1709 zapi_encode_prefix(s
, &filter
->u
.flower
.dst_ip
,
1710 filter
->u
.flower
.dst_ip
.family
);
1711 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_DST_PORT
) {
1712 stream_putw(s
, filter
->u
.flower
.dst_port_min
);
1713 stream_putw(s
, filter
->u
.flower
.dst_port_max
);
1715 if (filter
->u
.flower
.filter_bm
& TC_FLOWER_DSFIELD
) {
1716 stream_putc(s
, filter
->u
.flower
.dsfield
);
1717 stream_putc(s
, filter
->u
.flower
.dsfield_mask
);
1719 stream_putl(s
, filter
->u
.flower
.classid
);
1721 case TC_FILTER_UNSPEC
:
1723 case TC_FILTER_FLOW
:
1725 /* not implemented */
1729 stream_putw_at(s
, 0, stream_get_endp(s
));
1734 bool zapi_nhg_notify_decode(struct stream
*s
, uint32_t *id
,
1735 enum zapi_nhg_notify_owner
*note
)
1739 STREAM_GET(note
, s
, sizeof(*note
));
1740 STREAM_GETL(s
, read_id
);
1750 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1752 enum zapi_route_notify_owner
*note
,
1753 afi_t
*afi
, safi_t
*safi
)
1759 STREAM_GET(note
, s
, sizeof(*note
));
1761 STREAM_GETC(s
, p
->family
);
1762 STREAM_GETC(s
, p
->prefixlen
);
1763 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1765 STREAM_GETC(s
, afi_val
);
1766 STREAM_GETC(s
, safi_val
);
1781 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1782 uint32_t *priority
, uint32_t *unique
, char *ifname
,
1783 enum zapi_rule_notify_owner
*note
)
1785 uint32_t prio
, seq
, uni
;
1787 STREAM_GET(note
, s
, sizeof(*note
));
1789 STREAM_GETL(s
, seq
);
1790 STREAM_GETL(s
, prio
);
1791 STREAM_GETL(s
, uni
);
1792 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
1795 zlog_debug("%s: %u %u %u %s", __func__
, seq
, prio
, uni
, ifname
);
1806 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1807 enum zapi_ipset_notify_owner
*note
)
1812 STREAM_GETW(s
, notew
);
1814 STREAM_GETL(s
, uni
);
1817 zlog_debug("%s: %u", __func__
, uni
);
1819 *note
= (enum zapi_ipset_notify_owner
)notew
;
1826 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1828 enum zapi_ipset_entry_notify_owner
*note
)
1833 STREAM_GETW(s
, notew
);
1835 STREAM_GETL(s
, uni
);
1837 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1840 zlog_debug("%s: %u", __func__
, uni
);
1842 *note
= (enum zapi_ipset_entry_notify_owner
)notew
;
1850 bool zapi_iptable_notify_decode(struct stream
*s
,
1852 enum zapi_iptable_notify_owner
*note
)
1857 STREAM_GETW(s
, notew
);
1859 STREAM_GETL(s
, uni
);
1862 zlog_debug("%s: %u", __func__
, uni
);
1864 *note
= (enum zapi_iptable_notify_owner
)notew
;
1872 struct nexthop
*nexthop_from_zapi_nexthop(const struct zapi_nexthop
*znh
)
1874 struct nexthop
*n
= nexthop_new();
1876 n
->type
= znh
->type
;
1877 n
->vrf_id
= znh
->vrf_id
;
1878 n
->ifindex
= znh
->ifindex
;
1879 n
->gate
= znh
->gate
;
1880 n
->srte_color
= znh
->srte_color
;
1883 * This function currently handles labels
1885 if (znh
->label_num
) {
1886 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1890 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1891 SET_FLAG(n
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1892 n
->backup_num
= znh
->backup_num
;
1893 memcpy(n
->backup_idx
, znh
->backup_idx
, n
->backup_num
);
1896 if (znh
->seg6local_action
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
1897 nexthop_add_srv6_seg6local(n
, znh
->seg6local_action
,
1898 &znh
->seg6local_ctx
);
1900 if (!sid_zero(&znh
->seg6_segs
))
1901 nexthop_add_srv6_seg6(n
, &znh
->seg6_segs
);
1907 * Convert nexthop to zapi nexthop
1909 int zapi_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1910 const struct nexthop
*nh
)
1914 memset(znh
, 0, sizeof(*znh
));
1916 znh
->type
= nh
->type
;
1917 znh
->vrf_id
= nh
->vrf_id
;
1918 znh
->weight
= nh
->weight
;
1919 znh
->ifindex
= nh
->ifindex
;
1920 znh
->gate
= nh
->gate
;
1922 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
1923 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1925 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_EVPN
))
1926 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1928 if (nh
->nh_label
&& (nh
->nh_label
->num_labels
> 0)) {
1931 if (nh
->nh_label
->num_labels
> MPLS_MAX_LABELS
)
1934 for (i
= 0; i
< nh
->nh_label
->num_labels
; i
++)
1935 znh
->labels
[i
] = nh
->nh_label
->label
[i
];
1938 znh
->label_type
= nh
->nh_label_type
;
1939 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1942 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1943 if (nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1946 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1947 znh
->backup_num
= nh
->backup_num
;
1948 memcpy(znh
->backup_idx
, nh
->backup_idx
, znh
->backup_num
);
1952 if (nh
->nh_srv6
->seg6local_action
!=
1953 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1954 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
);
1955 znh
->seg6local_action
= nh
->nh_srv6
->seg6local_action
;
1956 memcpy(&znh
->seg6local_ctx
,
1957 &nh
->nh_srv6
->seg6local_ctx
,
1958 sizeof(struct seg6local_context
));
1961 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
1962 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
);
1963 memcpy(&znh
->seg6_segs
, &nh
->nh_srv6
->seg6_segs
,
1964 sizeof(struct in6_addr
));
1972 * Wrapper that converts backup nexthop
1974 int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1975 const struct nexthop
*nh
)
1979 /* Ensure that zapi flags are correct: backups don't have backups */
1980 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
1982 UNSET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1988 * Format some info about a zapi nexthop, for debug or logging.
1990 const char *zapi_nexthop2str(const struct zapi_nexthop
*znh
, char *buf
,
1993 char tmp
[INET6_ADDRSTRLEN
];
1995 switch (znh
->type
) {
1996 case NEXTHOP_TYPE_IFINDEX
:
1997 snprintf(buf
, bufsize
, "if %u", znh
->ifindex
);
1999 case NEXTHOP_TYPE_IPV4
:
2000 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2001 inet_ntop(AF_INET
, &znh
->gate
.ipv4
, tmp
, sizeof(tmp
));
2002 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
2004 case NEXTHOP_TYPE_IPV6
:
2005 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2006 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, tmp
, sizeof(tmp
));
2007 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
2009 case NEXTHOP_TYPE_BLACKHOLE
:
2010 snprintf(buf
, bufsize
, "blackhole");
2013 snprintf(buf
, bufsize
, "unknown");
2021 * Decode the nexthop-tracking update message
2023 bool zapi_nexthop_update_decode(struct stream
*s
, struct prefix
*match
,
2024 struct zapi_route
*nhr
)
2028 memset(nhr
, 0, sizeof(*nhr
));
2030 STREAM_GETL(s
, nhr
->message
);
2031 STREAM_GETW(s
, nhr
->safi
);
2032 STREAM_GETW(s
, match
->family
);
2033 STREAM_GETC(s
, match
->prefixlen
);
2035 * What we got told to match against
2037 switch (match
->family
) {
2039 STREAM_GET(&match
->u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2042 STREAM_GET(&match
->u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
2046 * What we matched against
2048 STREAM_GETW(s
, nhr
->prefix
.family
);
2049 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
2050 switch (nhr
->prefix
.family
) {
2052 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2055 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
2060 if (CHECK_FLAG(nhr
->message
, ZAPI_MESSAGE_SRTE
))
2061 STREAM_GETL(s
, nhr
->srte_color
);
2063 STREAM_GETC(s
, nhr
->type
);
2064 STREAM_GETW(s
, nhr
->instance
);
2065 STREAM_GETC(s
, nhr
->distance
);
2066 STREAM_GETL(s
, nhr
->metric
);
2067 STREAM_GETC(s
, nhr
->nexthop_num
);
2069 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
2070 if (zapi_nexthop_decode(s
, &(nhr
->nexthops
[i
]), 0, 0) != 0)
2079 bool zapi_error_decode(struct stream
*s
, enum zebra_error_types
*error
)
2081 memset(error
, 0, sizeof(*error
));
2083 STREAM_GET(error
, s
, sizeof(*error
));
2086 zlog_debug("%s: type: %s", __func__
,
2087 zebra_error_type2str(*error
));
2095 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
2096 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
2097 * then set/unset redist[type] in the client handle (a struct zserv) for the
2100 enum zclient_send_status
2101 zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
2102 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2109 zclient_create_header(s
, command
, vrf_id
);
2110 stream_putc(s
, afi
);
2111 stream_putc(s
, type
);
2112 stream_putw(s
, instance
);
2114 stream_putw_at(s
, 0, stream_get_endp(s
));
2116 return zclient_send_message(zclient
);
2119 enum zclient_send_status
2120 zebra_redistribute_default_send(int command
, struct zclient
*zclient
, afi_t afi
,
2128 zclient_create_header(s
, command
, vrf_id
);
2129 stream_putc(s
, afi
);
2131 stream_putw_at(s
, 0, stream_get_endp(s
));
2133 return zclient_send_message(zclient
);
2136 /* Send route notify request to zebra */
2137 int zebra_route_notify_send(int command
, struct zclient
*zclient
, bool set
)
2144 zclient_create_header(s
, command
, 0);
2145 stream_putc(s
, !!set
);
2147 stream_putw_at(s
, 0, stream_get_endp(s
));
2149 return zclient_send_message(zclient
);
2152 /* Get prefix in ZServ format; family should be filled in on prefix */
2153 static int zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
2155 size_t plen
= prefix_blen(p
);
2162 STREAM_GET(&p
->u
.prefix
, s
, plen
);
2164 p
->prefixlen
= MIN(plen
* 8, c
);
2171 /* Router-id update from zebra daemon. */
2172 int zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
2174 /* Fetch interface address. */
2175 STREAM_GETC(s
, rid
->family
);
2177 return zclient_stream_get_prefix(s
, rid
);
2183 /* Interface addition from zebra daemon. */
2185 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
2186 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
2188 * 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
2189 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2195 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2202 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2206 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2208 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2210 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2212 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2213 * | parent ifindex |
2214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2215 * | Link Layer Type |
2216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2217 * | Harware Address Length |
2218 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2219 * | Hardware Address if HW length different from 0 |
2220 * | ... max INTERFACE_HWADDR_MAX |
2221 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2222 * | Link_params? | Whether a link-params follows: 1 or 0.
2223 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2224 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
2225 * | .... (struct if_link_params). |
2226 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2229 static int zclient_vrf_add(ZAPI_CALLBACK_ARGS
)
2232 char vrfname_tmp
[VRF_NAMSIZ
+ 1] = {};
2233 struct vrf_data data
;
2235 STREAM_GET(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
2236 /* Read interface name. */
2237 STREAM_GET(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
2239 if (strlen(vrfname_tmp
) == 0)
2240 goto stream_failure
;
2242 /* Lookup/create vrf by name, then vrf_id. */
2243 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
2245 /* If there's already a VRF with this name, don't create vrf */
2249 vrf
->data
.l
.table_id
= data
.l
.table_id
;
2250 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
2258 static int zclient_vrf_delete(ZAPI_CALLBACK_ARGS
)
2262 /* Lookup vrf by vrf_id. */
2263 vrf
= vrf_lookup_by_id(vrf_id
);
2266 * If a routing protocol doesn't know about a
2267 * vrf that is about to be deleted. There is
2268 * no point in attempting to delete it.
2277 static int zclient_interface_add(ZAPI_CALLBACK_ARGS
)
2279 struct interface
*ifp
;
2280 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
2281 struct stream
*s
= zclient
->ibuf
;
2284 /* Read interface name. */
2285 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
2287 /* Lookup/create interface by name. */
2288 vrf
= vrf_lookup_by_id(vrf_id
);
2291 "Rx'd interface add from Zebra, but VRF %u does not exist",
2296 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, vrf
->name
);
2298 zebra_interface_if_set_value(s
, ifp
);
2300 if_new_via_zapi(ifp
);
2308 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
2309 * from zebra server. The format of this message is the same as
2310 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
2311 * except that no sockaddr_dl is sent at the tail of the message.
2313 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
2315 struct interface
*ifp
;
2316 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
2318 /* Read interface name. */
2319 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
2321 /* Lookup this by interface index. */
2322 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
2324 flog_err(EC_LIB_ZAPI_ENCODE
,
2325 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
2326 ifname_tmp
, vrf_id
);
2330 zebra_interface_if_set_value(s
, ifp
);
2337 static int zclient_interface_delete(ZAPI_CALLBACK_ARGS
)
2339 struct interface
*ifp
;
2340 struct stream
*s
= zclient
->ibuf
;
2342 ifp
= zebra_interface_state_read(s
, vrf_id
);
2347 if_destroy_via_zapi(ifp
);
2351 static int zclient_interface_up(ZAPI_CALLBACK_ARGS
)
2353 struct interface
*ifp
;
2354 struct stream
*s
= zclient
->ibuf
;
2356 ifp
= zebra_interface_state_read(s
, vrf_id
);
2361 if_up_via_zapi(ifp
);
2365 static int zclient_interface_down(ZAPI_CALLBACK_ARGS
)
2367 struct interface
*ifp
;
2368 struct stream
*s
= zclient
->ibuf
;
2370 ifp
= zebra_interface_state_read(s
, vrf_id
);
2375 if_down_via_zapi(ifp
);
2379 static int zclient_handle_error(ZAPI_CALLBACK_ARGS
)
2381 enum zebra_error_types error
;
2382 struct stream
*s
= zclient
->ibuf
;
2384 zapi_error_decode(s
, &error
);
2386 if (zclient
->handle_error
)
2387 (*zclient
->handle_error
)(error
);
2391 static int link_params_set_value(struct stream
*s
, struct interface
*ifp
)
2393 uint8_t link_params_enabled
, nb_ext_adm_grp
;
2394 struct if_link_params
*iflp
;
2395 uint32_t bwclassnum
, bitmap_data
;
2397 iflp
= if_link_params_get(ifp
);
2400 iflp
= if_link_params_init(ifp
);
2402 STREAM_GETC(s
, link_params_enabled
);
2403 if (!link_params_enabled
) {
2404 if_link_params_free(ifp
);
2408 STREAM_GETL(s
, iflp
->lp_status
);
2409 STREAM_GETL(s
, iflp
->te_metric
);
2410 STREAM_GETF(s
, iflp
->max_bw
);
2411 STREAM_GETF(s
, iflp
->max_rsv_bw
);
2412 STREAM_GETL(s
, bwclassnum
);
2415 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
2416 STREAM_GETF(s
, iflp
->unrsv_bw
[i
]);
2419 EC_LIB_ZAPI_MISSMATCH
,
2420 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries - outdated library?",
2421 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
2423 STREAM_GETL(s
, iflp
->admin_grp
);
2425 /* Extended Administrative Group */
2426 admin_group_clear(&iflp
->ext_admin_grp
);
2427 STREAM_GETC(s
, nb_ext_adm_grp
);
2428 for (size_t i
= 0; i
< nb_ext_adm_grp
; i
++) {
2429 STREAM_GETL(s
, bitmap_data
);
2430 admin_group_bulk_set(&iflp
->ext_admin_grp
, bitmap_data
, i
);
2433 STREAM_GETL(s
, iflp
->rmt_as
);
2434 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
2436 STREAM_GETL(s
, iflp
->av_delay
);
2437 STREAM_GETL(s
, iflp
->min_delay
);
2438 STREAM_GETL(s
, iflp
->max_delay
);
2439 STREAM_GETL(s
, iflp
->delay_var
);
2441 STREAM_GETF(s
, iflp
->pkt_loss
);
2442 STREAM_GETF(s
, iflp
->res_bw
);
2443 STREAM_GETF(s
, iflp
->ava_bw
);
2444 STREAM_GETF(s
, iflp
->use_bw
);
2451 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
2455 struct if_link_params
*iflp
;
2456 struct if_link_params iflp_prev
= {0};
2458 bool iflp_prev_set
= false;
2460 STREAM_GETL(s
, ifindex
);
2462 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2465 flog_err(EC_LIB_ZAPI_ENCODE
,
2466 "%s: unknown ifindex %u, shouldn't happen", __func__
,
2471 iflp
= if_link_params_get(ifp
);
2474 iflp_prev_set
= true;
2475 admin_group_init(&iflp_prev
.ext_admin_grp
);
2476 if_link_params_copy(&iflp_prev
, iflp
);
2479 /* read the link_params from stream
2480 * Free ifp->link_params if the stream has no params
2481 * to means that link-params are not enabled on links.
2483 if (link_params_set_value(s
, ifp
) != 0)
2484 goto stream_failure
;
2486 if (changed
!= NULL
) {
2487 iflp
= if_link_params_get(ifp
);
2489 if (iflp_prev_set
&& iflp
) {
2490 if (if_link_params_cmp(&iflp_prev
, iflp
))
2494 } else if (!iflp_prev_set
&& !iflp
)
2501 admin_group_term(&iflp_prev
.ext_admin_grp
);
2507 admin_group_term(&iflp_prev
.ext_admin_grp
);
2511 static void zebra_interface_if_set_value(struct stream
*s
,
2512 struct interface
*ifp
)
2514 uint8_t link_params_status
= 0;
2515 ifindex_t old_ifindex
, new_ifindex
;
2517 old_ifindex
= ifp
->oldifindex
;
2518 /* Read interface's index. */
2519 STREAM_GETL(s
, new_ifindex
);
2520 if_set_index(ifp
, new_ifindex
);
2521 STREAM_GETC(s
, ifp
->status
);
2523 /* Read interface's value. */
2524 STREAM_GETQ(s
, ifp
->flags
);
2525 STREAM_GETC(s
, ifp
->ptm_enable
);
2526 STREAM_GETC(s
, ifp
->ptm_status
);
2527 STREAM_GETL(s
, ifp
->metric
);
2528 STREAM_GETL(s
, ifp
->speed
);
2529 STREAM_GETL(s
, ifp
->mtu
);
2530 STREAM_GETL(s
, ifp
->mtu6
);
2531 STREAM_GETL(s
, ifp
->bandwidth
);
2532 STREAM_GETL(s
, ifp
->link_ifindex
);
2533 STREAM_GETL(s
, ifp
->ll_type
);
2534 STREAM_GETL(s
, ifp
->hw_addr_len
);
2535 if (ifp
->hw_addr_len
)
2536 STREAM_GET(ifp
->hw_addr
, s
,
2537 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
2539 /* Read Traffic Engineering status */
2540 link_params_status
= stream_getc(s
);
2541 /* Then, Traffic Engineering parameters if any */
2542 if (link_params_status
)
2543 link_params_set_value(s
, ifp
);
2545 nexthop_group_interface_state_change(ifp
, old_ifindex
);
2549 zlog_err("Could not parse interface values; aborting");
2550 assert(!"Failed to parse interface values");
2553 size_t zebra_interface_link_params_write(struct stream
*s
,
2554 struct interface
*ifp
)
2556 size_t w
, nb_ext_adm_grp
;
2557 struct if_link_params
*iflp
;
2561 if (s
== NULL
|| ifp
== NULL
)
2564 iflp
= ifp
->link_params
;
2567 /* encode if link_params is enabled */
2569 w
+= stream_putc(s
, true);
2571 w
+= stream_putc(s
, false);
2575 w
+= stream_putl(s
, iflp
->lp_status
);
2577 w
+= stream_putl(s
, iflp
->te_metric
);
2578 w
+= stream_putf(s
, iflp
->max_bw
);
2579 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
2581 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
2582 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
2583 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
2585 w
+= stream_putl(s
, iflp
->admin_grp
);
2587 /* Extended Administrative Group */
2588 nb_ext_adm_grp
= admin_group_nb_words(&iflp
->ext_admin_grp
);
2589 w
+= stream_putc(s
, nb_ext_adm_grp
);
2590 for (size_t i
= 0; i
< nb_ext_adm_grp
; i
++)
2591 stream_putl(s
, admin_group_get_offset(&iflp
->ext_admin_grp
, i
));
2593 w
+= stream_putl(s
, iflp
->rmt_as
);
2594 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
2596 w
+= stream_putl(s
, iflp
->av_delay
);
2597 w
+= stream_putl(s
, iflp
->min_delay
);
2598 w
+= stream_putl(s
, iflp
->max_delay
);
2599 w
+= stream_putl(s
, iflp
->delay_var
);
2601 w
+= stream_putf(s
, iflp
->pkt_loss
);
2602 w
+= stream_putf(s
, iflp
->res_bw
);
2603 w
+= stream_putf(s
, iflp
->ava_bw
);
2604 w
+= stream_putf(s
, iflp
->use_bw
);
2610 * format of message for address addition is:
2614 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
2615 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
2624 * | ifc_flags | flags for connected address
2632 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
2640 static int memconstant(const void *s
, int c
, size_t n
)
2642 const uint8_t *p
= s
;
2651 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
2655 struct interface
*ifp
;
2656 struct connected
*ifc
;
2657 struct prefix p
, d
, *dp
;
2661 memset(&p
, 0, sizeof(p
));
2662 memset(&d
, 0, sizeof(d
));
2664 /* Get interface index. */
2665 STREAM_GETL(s
, ifindex
);
2668 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2670 flog_err(EC_LIB_ZAPI_ENCODE
,
2671 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
2672 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
2678 STREAM_GETC(s
, ifc_flags
);
2680 /* Fetch interface address. */
2681 STREAM_GETC(s
, d
.family
);
2682 p
.family
= d
.family
;
2683 plen
= prefix_blen(&d
);
2685 if (zclient_stream_get_prefix(s
, &p
) != 0)
2686 goto stream_failure
;
2688 /* Fetch destination address. */
2689 STREAM_GET(&d
.u
.prefix
, s
, plen
);
2691 /* N.B. NULL destination pointers are encoded as all zeroes */
2692 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
2694 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
2695 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
2697 /* N.B. NULL destination pointers are encoded as all
2699 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
2702 ifc
->flags
= ifc_flags
;
2703 if (ifc
->destination
)
2704 ifc
->destination
->prefixlen
=
2705 ifc
->address
->prefixlen
;
2706 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
2707 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
2711 "interface %s address %pFX with peer flag set, but no peer address!",
2712 ifp
->name
, ifc
->address
);
2713 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
2717 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
2718 ifc
= connected_delete_by_prefix(ifp
, &p
);
2728 * format of message for neighbor connected address is:
2732 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
2733 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2748 * | addr_len | len of addr.
2751 struct nbr_connected
*
2752 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
2754 unsigned int ifindex
;
2755 struct interface
*ifp
;
2757 struct nbr_connected
*ifc
;
2759 /* Get interface index. */
2760 STREAM_GETL(s
, ifindex
);
2763 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2765 flog_err(EC_LIB_ZAPI_ENCODE
,
2766 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2767 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
2773 STREAM_GETC(s
, p
.family
);
2774 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
2775 STREAM_GETC(s
, p
.prefixlen
);
2777 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
2778 /* Currently only supporting P2P links, so any new RA source
2780 considered as the replacement of the previously learnt
2781 Link-Local address. */
2782 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
2783 ifc
= nbr_connected_new();
2784 ifc
->address
= prefix_new();
2786 listnode_add(ifp
->nbr_connected
, ifc
);
2789 prefix_copy(ifc
->address
, &p
);
2791 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
2793 ifc
= nbr_connected_check(ifp
, &p
);
2795 listnode_delete(ifp
->nbr_connected
, ifc
);
2804 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
2806 vrf_id_t
*new_vrf_id
)
2808 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2809 struct interface
*ifp
;
2812 /* Read interface name. */
2813 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
2815 /* Lookup interface. */
2816 ifp
= if_lookup_by_name(ifname
, vrf_id
);
2818 flog_err(EC_LIB_ZAPI_ENCODE
,
2819 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2824 /* Fetch new VRF Id. */
2825 STREAM_GETL(s
, new_id
);
2827 *new_vrf_id
= new_id
;
2834 /* filter unwanted messages until the expected one arrives */
2835 static int zclient_read_sync_response(struct zclient
*zclient
,
2836 uint16_t expected_cmd
)
2848 cmd
= expected_cmd
+ 1;
2849 while (ret
== 0 && cmd
!= expected_cmd
) {
2853 /* wait until response arrives */
2855 FD_SET(zclient
->sock
, &readfds
);
2856 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2857 if (!FD_ISSET(zclient
->sock
, &readfds
))
2860 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2861 &version
, &vrf_id
, &cmd
);
2863 zlog_debug("%s: Response (%d bytes) received", __func__
,
2867 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2875 * Connect to label manager in a synchronous way
2877 * It first writes the request to zclient output buffer and then
2878 * immediately reads the answer from the input buffer.
2880 * @param zclient Zclient used to connect to label manager (zebra)
2881 * @param async Synchronous (0) or asynchronous (1) operation
2882 * @result Result of response
2884 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2889 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2890 ZEBRA_LABEL_MANAGER_CONNECT
;
2893 zlog_debug("Connecting to Label Manager (LM)");
2895 if (zclient
->sock
< 0) {
2896 zlog_debug("%s: invalid zclient socket", __func__
);
2903 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2906 stream_putc(s
, zclient
->redist_default
);
2908 stream_putw(s
, zclient
->instance
);
2910 /* Put length at the first point of the stream. */
2911 stream_putw_at(s
, 0, stream_get_endp(s
));
2913 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2915 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2916 close(zclient
->sock
);
2921 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2922 close(zclient
->sock
);
2927 zlog_debug("LM connect request sent (%d bytes)", ret
);
2933 if (zclient_read_sync_response(zclient
, cmd
)
2939 /* read instance and proto */
2943 STREAM_GETC(s
, proto
);
2944 STREAM_GETW(s
, instance
);
2947 if (proto
!= zclient
->redist_default
)
2950 "Wrong proto (%u) in LM connect response. Should be %u",
2951 proto
, zclient
->redist_default
);
2952 if (instance
!= zclient
->instance
)
2955 "Wrong instId (%u) in LM connect response. Should be %u",
2956 instance
, zclient
->instance
);
2959 STREAM_GETC(s
, result
);
2961 zlog_debug("LM connect-response received, result %u", result
);
2970 * Function to request a srv6-locator chunk in an asynchronous way
2972 * @param zclient Zclient used to connect to table manager (zebra)
2973 * @param locator_name Name of SRv6-locator
2974 * @result 0 on success, -1 otherwise
2976 int srv6_manager_get_locator_chunk(struct zclient
*zclient
,
2977 const char *locator_name
)
2980 const size_t len
= strlen(locator_name
);
2983 zlog_debug("Getting SRv6-Locator Chunk %s", locator_name
);
2985 if (zclient
->sock
< 0)
2991 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
,
2995 stream_putw(s
, len
);
2996 stream_put(s
, locator_name
, len
);
2998 /* Put length at the first point of the stream. */
2999 stream_putw_at(s
, 0, stream_get_endp(s
));
3001 return zclient_send_message(zclient
);
3005 * Function to release a srv6-locator chunk
3007 * @param zclient Zclient used to connect to table manager (zebra)
3008 * @param locator_name Name of SRv6-locator
3009 * @result 0 on success, -1 otherwise
3011 int srv6_manager_release_locator_chunk(struct zclient
*zclient
,
3012 const char *locator_name
)
3015 const size_t len
= strlen(locator_name
);
3018 zlog_debug("Releasing SRv6-Locator Chunk %s", locator_name
);
3020 if (zclient
->sock
< 0)
3026 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
,
3030 stream_putw(s
, len
);
3031 stream_put(s
, locator_name
, len
);
3033 /* Put length at the first point of the stream. */
3034 stream_putw_at(s
, 0, stream_get_endp(s
));
3036 return zclient_send_message(zclient
);
3040 * Asynchronous label chunk request
3042 * @param zclient Zclient used to connect to label manager (zebra)
3043 * @param keep Avoid garbage collection
3044 * @param chunk_size Amount of labels requested
3045 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
3046 * @result 0 on success, -1 otherwise
3048 enum zclient_send_status
zclient_send_get_label_chunk(struct zclient
*zclient
,
3050 uint32_t chunk_size
,
3056 zlog_debug("Getting Label Chunk");
3058 if (zclient
->sock
< 0)
3059 return ZCLIENT_SEND_FAILURE
;
3064 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
3066 stream_putc(s
, zclient
->redist_default
);
3068 stream_putw(s
, zclient
->instance
);
3069 stream_putc(s
, keep
);
3070 stream_putl(s
, chunk_size
);
3071 stream_putl(s
, base
);
3073 /* Put length at the first point of the stream. */
3074 stream_putw_at(s
, 0, stream_get_endp(s
));
3076 return zclient_send_message(zclient
);
3080 * Function to request a label chunk in a synchronous way
3082 * It first writes the request to zclient output buffer and then
3083 * immediately reads the answer from the input buffer.
3085 * @param zclient Zclient used to connect to label manager (zebra)
3086 * @param keep Avoid garbage collection
3087 * @param chunk_size Amount of labels requested
3088 * @param start To write first assigned chunk label to
3089 * @param end To write last assigned chunk label to
3090 * @result 0 on success, -1 otherwise
3092 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
3093 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
3097 uint8_t response_keep
;
3100 zlog_debug("Getting Label Chunk");
3102 if (zclient
->sock
< 0)
3108 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
3110 stream_putc(s
, zclient
->redist_default
);
3112 stream_putw(s
, zclient
->instance
);
3114 stream_putc(s
, keep
);
3116 stream_putl(s
, chunk_size
);
3117 /* requested chunk base */
3118 stream_putl(s
, base
);
3119 /* Put length at the first point of the stream. */
3120 stream_putw_at(s
, 0, stream_get_endp(s
));
3122 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
3124 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
3125 close(zclient
->sock
);
3130 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
3131 close(zclient
->sock
);
3136 zlog_debug("Label chunk request (%d bytes) sent", ret
);
3139 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
3142 /* parse response */
3145 /* read proto and instance */
3149 STREAM_GETC(s
, proto
);
3150 STREAM_GETW(s
, instance
);
3153 if (proto
!= zclient
->redist_default
)
3154 flog_err(EC_LIB_ZAPI_ENCODE
,
3155 "Wrong proto (%u) in get chunk response. Should be %u",
3156 proto
, zclient
->redist_default
);
3157 if (instance
!= zclient
->instance
)
3158 flog_err(EC_LIB_ZAPI_ENCODE
,
3159 "Wrong instId (%u) in get chunk response Should be %u",
3160 instance
, zclient
->instance
);
3162 /* if we requested a specific chunk and it could not be allocated, the
3163 * response message will end here
3165 if (!STREAM_READABLE(s
)) {
3166 zlog_info("Unable to assign Label Chunk to %s instance %u",
3167 zebra_route_string(proto
), instance
);
3172 STREAM_GETC(s
, response_keep
);
3173 /* start and end labels */
3174 STREAM_GETL(s
, *start
);
3175 STREAM_GETL(s
, *end
);
3177 /* not owning this response */
3178 if (keep
!= response_keep
) {
3181 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
3182 *start
, *end
, keep
, response_keep
);
3185 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
3186 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
3187 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
3193 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
3203 * Function to release a label chunk
3205 * @param zclient Zclient used to connect to label manager (zebra)
3206 * @param start First label of chunk
3207 * @param end Last label of chunk
3208 * @result 0 on success, -1 otherwise
3210 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
3217 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
3219 if (zclient
->sock
< 0)
3225 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
3228 stream_putc(s
, zclient
->redist_default
);
3230 stream_putw(s
, zclient
->instance
);
3232 stream_putl(s
, start
);
3234 stream_putl(s
, end
);
3236 /* Put length at the first point of the stream. */
3237 stream_putw_at(s
, 0, stream_get_endp(s
));
3239 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
3241 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
3242 close(zclient
->sock
);
3247 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
3248 close(zclient
->sock
);
3257 * Connect to table manager in a synchronous way
3259 * It first writes the request to zclient output buffer and then
3260 * immediately reads the answer from the input buffer.
3262 * @param zclient Zclient used to connect to table manager (zebra)
3263 * @result Result of response
3265 int tm_table_manager_connect(struct zclient
*zclient
)
3272 zlog_debug("Connecting to Table Manager");
3274 if (zclient
->sock
< 0)
3275 return ZCLIENT_SEND_FAILURE
;
3280 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
3283 stream_putc(s
, zclient
->redist_default
);
3285 stream_putw(s
, zclient
->instance
);
3287 /* Put length at the first point of the stream. */
3288 stream_putw_at(s
, 0, stream_get_endp(s
));
3290 ret
= zclient_send_message(zclient
);
3291 if (ret
== ZCLIENT_SEND_FAILURE
)
3295 zlog_debug("%s: Table manager connect request sent", __func__
);
3298 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
3304 STREAM_GETC(s
, result
);
3307 "%s: Table Manager connect response received, result %u",
3316 * Function to request a table chunk in a synchronous way
3318 * It first writes the request to zclient output buffer and then
3319 * immediately reads the answer from the input buffer.
3321 * @param zclient Zclient used to connect to table manager (zebra)
3322 * @param chunk_size Amount of table requested
3323 * @param start to write first assigned chunk table RT ID to
3324 * @param end To write last assigned chunk table RT ID to
3325 * @result 0 on success, -1 otherwise
3327 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
3328 uint32_t *start
, uint32_t *end
)
3334 zlog_debug("Getting Table Chunk");
3336 if (zclient
->sock
< 0)
3342 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
3344 stream_putl(s
, chunk_size
);
3345 /* Put length at the first point of the stream. */
3346 stream_putw_at(s
, 0, stream_get_endp(s
));
3348 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
3350 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
3352 close(zclient
->sock
);
3357 flog_err(EC_LIB_ZAPI_SOCKET
,
3358 "%s: zclient->sock connection closed", __func__
);
3359 close(zclient
->sock
);
3364 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
3368 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
3372 /* start and end table IDs */
3373 STREAM_GETL(s
, *start
);
3374 STREAM_GETL(s
, *end
);
3377 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
3385 * Function to release a table chunk
3387 * @param zclient Zclient used to connect to table manager (zebra)
3388 * @param start First label of table
3389 * @param end Last label of chunk
3390 * @result 0 on success, -1 otherwise
3392 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
3398 zlog_debug("Releasing Table Chunk");
3400 if (zclient
->sock
< 0)
3406 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
3409 stream_putl(s
, start
);
3411 stream_putl(s
, end
);
3413 /* Put length at the first point of the stream. */
3414 stream_putw_at(s
, 0, stream_get_endp(s
));
3416 if (zclient_send_message(zclient
) == ZCLIENT_SEND_FAILURE
)
3422 enum zclient_send_status
zebra_send_sr_policy(struct zclient
*zclient
, int cmd
,
3423 struct zapi_sr_policy
*zp
)
3425 if (zapi_sr_policy_encode(zclient
->obuf
, cmd
, zp
) < 0)
3426 return ZCLIENT_SEND_FAILURE
;
3427 return zclient_send_message(zclient
);
3430 int zapi_sr_policy_encode(struct stream
*s
, int cmd
, struct zapi_sr_policy
*zp
)
3432 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
3436 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
3437 stream_putl(s
, zp
->color
);
3438 stream_put_ipaddr(s
, &zp
->endpoint
);
3439 stream_write(s
, &zp
->name
, SRTE_POLICY_NAME_MAX_LENGTH
);
3441 stream_putc(s
, zt
->type
);
3442 stream_putl(s
, zt
->local_label
);
3444 if (zt
->label_num
> MPLS_MAX_LABELS
) {
3445 flog_err(EC_LIB_ZAPI_ENCODE
,
3446 "%s: label %u: can't encode %u labels (maximum is %u)",
3447 __func__
, zt
->local_label
, zt
->label_num
,
3451 stream_putw(s
, zt
->label_num
);
3453 for (int i
= 0; i
< zt
->label_num
; i
++)
3454 stream_putl(s
, zt
->labels
[i
]);
3456 /* Put length at the first point of the stream. */
3457 stream_putw_at(s
, 0, stream_get_endp(s
));
3462 int zapi_sr_policy_decode(struct stream
*s
, struct zapi_sr_policy
*zp
)
3464 memset(zp
, 0, sizeof(*zp
));
3466 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
3468 STREAM_GETL(s
, zp
->color
);
3469 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3470 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3472 /* segment list of active candidate path */
3473 STREAM_GETC(s
, zt
->type
);
3474 STREAM_GETL(s
, zt
->local_label
);
3475 STREAM_GETW(s
, zt
->label_num
);
3476 if (zt
->label_num
> MPLS_MAX_LABELS
) {
3477 flog_err(EC_LIB_ZAPI_ENCODE
,
3478 "%s: label %u: can't decode %u labels (maximum is %u)",
3479 __func__
, zt
->local_label
, zt
->label_num
,
3483 for (int i
= 0; i
< zt
->label_num
; i
++)
3484 STREAM_GETL(s
, zt
->labels
[i
]);
3492 int zapi_sr_policy_notify_status_decode(struct stream
*s
,
3493 struct zapi_sr_policy
*zp
)
3495 memset(zp
, 0, sizeof(*zp
));
3497 STREAM_GETL(s
, zp
->color
);
3498 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3499 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3500 STREAM_GETL(s
, zp
->status
);
3508 enum zclient_send_status
zebra_send_mpls_labels(struct zclient
*zclient
,
3509 int cmd
, struct zapi_labels
*zl
)
3511 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
3512 return ZCLIENT_SEND_FAILURE
;
3513 return zclient_send_message(zclient
);
3516 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
3518 struct zapi_nexthop
*znh
;
3522 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
3523 stream_putc(s
, zl
->message
);
3524 stream_putc(s
, zl
->type
);
3525 stream_putl(s
, zl
->local_label
);
3527 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3528 stream_putw(s
, zl
->route
.prefix
.family
);
3529 stream_put_prefix(s
, &zl
->route
.prefix
);
3530 stream_putc(s
, zl
->route
.type
);
3531 stream_putw(s
, zl
->route
.instance
);
3534 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3537 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3538 __func__
, zl
->local_label
, zl
->nexthop_num
,
3542 stream_putw(s
, zl
->nexthop_num
);
3544 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3545 znh
= &zl
->nexthops
[i
];
3547 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3551 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3553 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3556 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3557 __func__
, zl
->local_label
, zl
->nexthop_num
,
3561 stream_putw(s
, zl
->backup_nexthop_num
);
3563 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3564 znh
= &zl
->backup_nexthops
[i
];
3566 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3572 /* Put length at the first point of the stream. */
3573 stream_putw_at(s
, 0, stream_get_endp(s
));
3578 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
3580 struct zapi_nexthop
*znh
;
3582 memset(zl
, 0, sizeof(*zl
));
3585 STREAM_GETC(s
, zl
->message
);
3586 STREAM_GETC(s
, zl
->type
);
3587 STREAM_GETL(s
, zl
->local_label
);
3589 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3592 STREAM_GETW(s
, zl
->route
.prefix
.family
);
3593 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
3595 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
3596 switch (zl
->route
.prefix
.family
) {
3598 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
3600 "%s: Specified prefix length %d is greater than a v4 address can support",
3601 __func__
, zl
->route
.prefix
.prefixlen
);
3604 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
3608 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
3610 "%s: Specified prefix length %d is greater than a v6 address can support",
3611 __func__
, zl
->route
.prefix
.prefixlen
);
3614 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
3617 flog_err(EC_LIB_ZAPI_ENCODE
,
3618 "%s: Specified family %u is not v4 or v6",
3619 __func__
, zl
->route
.prefix
.family
);
3623 STREAM_GETC(s
, zl
->route
.type
);
3624 STREAM_GETW(s
, zl
->route
.instance
);
3627 STREAM_GETW(s
, zl
->nexthop_num
);
3629 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3632 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
3633 __func__
, &zl
->route
.prefix
, zl
->nexthop_num
,
3637 zl
->nexthop_num
= MIN(MULTIPATH_NUM
, zl
->nexthop_num
);
3639 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3640 znh
= &zl
->nexthops
[i
];
3642 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3645 if (znh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3648 "%s: Prefix %pFX has a blackhole nexthop which we cannot use for a label",
3649 __func__
, &zl
->route
.prefix
);
3654 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3655 STREAM_GETW(s
, zl
->backup_nexthop_num
);
3657 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3660 "%s: Prefix %pFX has %d backup nexthops, but we can only use the first %d",
3661 __func__
, &zl
->route
.prefix
,
3662 zl
->backup_nexthop_num
, MULTIPATH_NUM
);
3665 zl
->backup_nexthop_num
= MIN(MULTIPATH_NUM
,
3666 zl
->backup_nexthop_num
);
3668 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3669 znh
= &zl
->backup_nexthops
[i
];
3671 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3674 if (znh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3677 "%s: Prefix %pFX has a backup blackhole nexthop which we cannot use for a label",
3678 __func__
, &zl
->route
.prefix
);
3689 enum zclient_send_status
zebra_send_pw(struct zclient
*zclient
, int command
,
3698 zclient_create_header(s
, command
, VRF_DEFAULT
);
3699 stream_write(s
, pw
->ifname
, INTERFACE_NAMSIZ
);
3700 stream_putl(s
, pw
->ifindex
);
3703 stream_putl(s
, pw
->type
);
3706 stream_putl(s
, pw
->af
);
3709 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
3712 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
3715 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
3716 return ZCLIENT_SEND_FAILURE
;
3720 stream_putl(s
, pw
->local_label
);
3721 stream_putl(s
, pw
->remote_label
);
3724 stream_putc(s
, pw
->flags
);
3726 /* Protocol specific fields */
3727 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
3729 /* Put length at the first point of the stream. */
3730 stream_putw_at(s
, 0, stream_get_endp(s
));
3732 return zclient_send_message(zclient
);
3736 * Receive PW status update from Zebra and send it to LDE process.
3738 int zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
3742 memset(pw
, 0, sizeof(struct zapi_pw_status
));
3746 stream_get(pw
->ifname
, s
, INTERFACE_NAMSIZ
);
3747 STREAM_GETL(s
, pw
->ifindex
);
3748 STREAM_GETL(s
, pw
->status
);
3755 static int zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
3757 struct zclient_capabilities cap
;
3758 struct stream
*s
= zclient
->ibuf
;
3760 uint8_t mpls_enabled
;
3762 STREAM_GETL(s
, vrf_backend
);
3764 if (vrf_backend
< 0 || vrf_configure_backend(vrf_backend
)) {
3765 flog_err(EC_LIB_ZAPI_ENCODE
,
3766 "%s: Garbage VRF backend type: %d", __func__
,
3768 goto stream_failure
;
3772 memset(&cap
, 0, sizeof(cap
));
3773 STREAM_GETC(s
, mpls_enabled
);
3774 cap
.mpls_enabled
= !!mpls_enabled
;
3775 STREAM_GETL(s
, cap
.ecmp
);
3776 STREAM_GETC(s
, cap
.role
);
3778 if (zclient
->zebra_capabilities
)
3779 (*zclient
->zebra_capabilities
)(&cap
);
3785 enum zclient_send_status
zclient_send_mlag_register(struct zclient
*client
,
3793 zclient_create_header(s
, ZEBRA_MLAG_CLIENT_REGISTER
, VRF_DEFAULT
);
3794 stream_putl(s
, bit_map
);
3796 stream_putw_at(s
, 0, stream_get_endp(s
));
3797 return zclient_send_message(client
);
3800 enum zclient_send_status
zclient_send_mlag_deregister(struct zclient
*client
)
3802 return zebra_message_send(client
, ZEBRA_MLAG_CLIENT_UNREGISTER
,
3806 enum zclient_send_status
zclient_send_mlag_data(struct zclient
*client
,
3807 struct stream
*client_s
)
3814 zclient_create_header(s
, ZEBRA_MLAG_FORWARD_MSG
, VRF_DEFAULT
);
3815 stream_put(s
, client_s
->data
, client_s
->endp
);
3817 stream_putw_at(s
, 0, stream_get_endp(s
));
3818 return zclient_send_message(client
);
3822 * Send an OPAQUE message, contents opaque to zebra. The message header
3823 * is a message subtype.
3825 enum zclient_send_status
zclient_send_opaque(struct zclient
*zclient
,
3826 uint32_t type
, const uint8_t *data
,
3832 /* Check buffer size */
3833 if (STREAM_SIZE(zclient
->obuf
) <
3834 (ZEBRA_HEADER_SIZE
+ sizeof(type
) + datasize
))
3835 return ZCLIENT_SEND_FAILURE
;
3840 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3842 /* Send sub-type and flags */
3843 stream_putl(s
, type
);
3844 stream_putw(s
, flags
);
3846 /* Send opaque data */
3847 stream_write(s
, data
, datasize
);
3849 /* Put length into the header at the start of the stream. */
3850 stream_putw_at(s
, 0, stream_get_endp(s
));
3852 return zclient_send_message(zclient
);
3856 * Send an OPAQUE message to a specific zclient. The contents are opaque
3859 enum zclient_send_status
3860 zclient_send_opaque_unicast(struct zclient
*zclient
, uint32_t type
,
3861 uint8_t proto
, uint16_t instance
,
3862 uint32_t session_id
, const uint8_t *data
,
3868 /* Check buffer size */
3869 if (STREAM_SIZE(zclient
->obuf
) <
3870 (ZEBRA_HEADER_SIZE
+ sizeof(struct zapi_opaque_msg
) + datasize
))
3871 return ZCLIENT_SEND_FAILURE
;
3876 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3878 /* Send sub-type and flags */
3879 SET_FLAG(flags
, ZAPI_OPAQUE_FLAG_UNICAST
);
3880 stream_putl(s
, type
);
3881 stream_putw(s
, flags
);
3883 /* Send destination client info */
3884 stream_putc(s
, proto
);
3885 stream_putw(s
, instance
);
3886 stream_putl(s
, session_id
);
3888 /* Send opaque data */
3889 stream_write(s
, data
, datasize
);
3891 /* Put length into the header at the start of the stream. */
3892 stream_putw_at(s
, 0, stream_get_endp(s
));
3894 return zclient_send_message(zclient
);
3898 * Decode incoming opaque message into info struct
3900 int zclient_opaque_decode(struct stream
*s
, struct zapi_opaque_msg
*info
)
3902 memset(info
, 0, sizeof(*info
));
3904 /* Decode subtype and flags */
3905 STREAM_GETL(s
, info
->type
);
3906 STREAM_GETW(s
, info
->flags
);
3908 /* Decode unicast client info if present */
3909 if (CHECK_FLAG(info
->flags
, ZAPI_OPAQUE_FLAG_UNICAST
)) {
3910 STREAM_GETC(s
, info
->proto
);
3911 STREAM_GETW(s
, info
->instance
);
3912 STREAM_GETL(s
, info
->session_id
);
3915 info
->len
= STREAM_READABLE(s
);
3925 * Send a registration request for opaque messages with a specified subtype.
3927 enum zclient_send_status
zclient_register_opaque(struct zclient
*zclient
,
3935 zclient_create_header(s
, ZEBRA_OPAQUE_REGISTER
, VRF_DEFAULT
);
3938 stream_putl(s
, type
);
3940 /* Add zclient info */
3941 stream_putc(s
, zclient
->redist_default
);
3942 stream_putw(s
, zclient
->instance
);
3943 stream_putl(s
, zclient
->session_id
);
3945 /* Put length at the first point of the stream. */
3946 stream_putw_at(s
, 0, stream_get_endp(s
));
3948 return zclient_send_message(zclient
);
3952 * Send an un-registration request for a specified opaque subtype.
3954 enum zclient_send_status
zclient_unregister_opaque(struct zclient
*zclient
,
3962 zclient_create_header(s
, ZEBRA_OPAQUE_UNREGISTER
, VRF_DEFAULT
);
3965 stream_putl(s
, type
);
3967 /* Add zclient info */
3968 stream_putc(s
, zclient
->redist_default
);
3969 stream_putw(s
, zclient
->instance
);
3970 stream_putl(s
, zclient
->session_id
);
3972 /* Put length at the first point of the stream. */
3973 stream_putw_at(s
, 0, stream_get_endp(s
));
3975 return zclient_send_message(zclient
);
3978 /* Utility to decode opaque registration info */
3979 int zapi_opaque_reg_decode(struct stream
*s
, struct zapi_opaque_reg_info
*info
)
3981 STREAM_GETL(s
, info
->type
);
3982 STREAM_GETC(s
, info
->proto
);
3983 STREAM_GETW(s
, info
->instance
);
3984 STREAM_GETL(s
, info
->session_id
);
3993 /* Utility to decode client close notify info */
3994 int zapi_client_close_notify_decode(struct stream
*s
,
3995 struct zapi_client_close_info
*info
)
3997 memset(info
, 0, sizeof(*info
));
3999 STREAM_GETC(s
, info
->proto
);
4000 STREAM_GETW(s
, info
->instance
);
4001 STREAM_GETL(s
, info
->session_id
);
4010 static zclient_handler
*const lib_handlers
[] = {
4012 [ZEBRA_CAPABILITIES
] = zclient_capability_decode
,
4013 [ZEBRA_ERROR
] = zclient_handle_error
,
4015 /* VRF & interface code is shared in lib */
4016 [ZEBRA_VRF_ADD
] = zclient_vrf_add
,
4017 [ZEBRA_VRF_DELETE
] = zclient_vrf_delete
,
4018 [ZEBRA_INTERFACE_ADD
] = zclient_interface_add
,
4019 [ZEBRA_INTERFACE_DELETE
] = zclient_interface_delete
,
4020 [ZEBRA_INTERFACE_UP
] = zclient_interface_up
,
4021 [ZEBRA_INTERFACE_DOWN
] = zclient_interface_down
,
4024 [ZEBRA_BFD_DEST_REPLAY
] = zclient_bfd_session_replay
,
4025 [ZEBRA_INTERFACE_BFD_DEST_UPDATE
] = zclient_bfd_session_update
,
4028 /* Zebra client message read function. */
4029 static void zclient_read(struct event
*thread
)
4032 uint16_t length
, command
;
4033 uint8_t marker
, version
;
4035 struct zclient
*zclient
;
4037 /* Get socket to zebra. */
4038 zclient
= EVENT_ARG(thread
);
4039 zclient
->t_read
= NULL
;
4041 /* Read zebra header (if we don't have it already). */
4042 already
= stream_get_endp(zclient
->ibuf
);
4043 if (already
< ZEBRA_HEADER_SIZE
) {
4045 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
4046 ZEBRA_HEADER_SIZE
- already
))
4051 "zclient connection closed socket [%d].",
4053 zclient_failed(zclient
);
4056 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
4057 zclient_event(ZCLIENT_READ
, zclient
);
4060 already
= ZEBRA_HEADER_SIZE
;
4063 /* Reset to read from the beginning of the incoming packet. */
4064 stream_set_getp(zclient
->ibuf
, 0);
4066 /* Fetch header values. */
4067 length
= stream_getw(zclient
->ibuf
);
4068 marker
= stream_getc(zclient
->ibuf
);
4069 version
= stream_getc(zclient
->ibuf
);
4070 vrf_id
= stream_getl(zclient
->ibuf
);
4071 command
= stream_getw(zclient
->ibuf
);
4073 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
4075 EC_LIB_ZAPI_MISSMATCH
,
4076 "%s: socket %d version mismatch, marker %d, version %d",
4077 __func__
, zclient
->sock
, marker
, version
);
4078 zclient_failed(zclient
);
4082 if (length
< ZEBRA_HEADER_SIZE
) {
4083 flog_err(EC_LIB_ZAPI_MISSMATCH
,
4084 "%s: socket %d message length %u is less than %d ",
4085 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
4086 zclient_failed(zclient
);
4091 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
4095 "%s: message size %u exceeds buffer size %lu, expanding...",
4097 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
4098 ns
= stream_new(length
);
4099 stream_copy(ns
, zclient
->ibuf
);
4100 stream_free(zclient
->ibuf
);
4104 /* Read rest of zebra packet. */
4105 if (already
< length
) {
4107 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
4113 "zclient connection closed socket [%d].",
4115 zclient_failed(zclient
);
4118 if (nbyte
!= (ssize_t
)(length
- already
)) {
4119 /* Try again later. */
4120 zclient_event(ZCLIENT_READ
, zclient
);
4125 length
-= ZEBRA_HEADER_SIZE
;
4128 zlog_debug("zclient %p command %s VRF %u", zclient
,
4129 zserv_command_string(command
), vrf_id
);
4131 if (command
< array_size(lib_handlers
) && lib_handlers
[command
])
4132 lib_handlers
[command
](command
, zclient
, length
, vrf_id
);
4133 if (command
< zclient
->n_handlers
&& zclient
->handlers
[command
])
4134 zclient
->handlers
[command
](command
, zclient
, length
, vrf_id
);
4136 if (zclient
->sock
< 0)
4137 /* Connection was closed during packet processing. */
4140 /* Register read thread. */
4141 stream_reset(zclient
->ibuf
);
4142 zclient_event(ZCLIENT_READ
, zclient
);
4145 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
4146 int type
, unsigned short instance
, vrf_id_t vrf_id
)
4150 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
4151 if (redist_check_instance(
4152 &zclient
->mi_redist
[afi
][type
], instance
))
4154 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
4157 if (!redist_check_instance(
4158 &zclient
->mi_redist
[afi
][type
], instance
))
4160 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
4165 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
4166 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
4169 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
4171 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
4174 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
4178 if (zclient
->sock
> 0)
4179 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
4184 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
4185 afi_t afi
, vrf_id_t vrf_id
)
4188 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
4189 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
4191 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
4193 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
4196 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
4199 if (zclient
->sock
> 0)
4200 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
4203 static void zclient_event(enum zclient_event event
, struct zclient
*zclient
)
4206 case ZCLIENT_SCHEDULE
:
4207 event_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
4208 &zclient
->t_connect
);
4210 case ZCLIENT_CONNECT
:
4213 "zclient connect failures: %d schedule interval is now %d",
4214 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
4215 event_add_timer(zclient
->master
, zclient_connect
, zclient
,
4216 zclient
->fail
< 3 ? 10 : 60,
4217 &zclient
->t_connect
);
4220 zclient
->t_read
= NULL
;
4221 event_add_read(zclient
->master
, zclient_read
, zclient
,
4222 zclient
->sock
, &zclient
->t_read
);
4227 enum zclient_send_status
zclient_interface_set_master(struct zclient
*client
,
4228 struct interface
*master
,
4229 struct interface
*slave
)
4236 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
,
4237 master
->vrf
->vrf_id
);
4239 stream_putl(s
, master
->vrf
->vrf_id
);
4240 stream_putl(s
, master
->ifindex
);
4241 stream_putl(s
, slave
->vrf
->vrf_id
);
4242 stream_putl(s
, slave
->ifindex
);
4244 stream_putw_at(s
, 0, stream_get_endp(s
));
4245 return zclient_send_message(client
);
4249 * Send capabilities message to zebra
4251 enum zclient_send_status
zclient_capabilities_send(uint32_t cmd
,
4252 struct zclient
*zclient
,
4253 struct zapi_cap
*api
)
4258 if (zclient
== NULL
)
4259 return ZCLIENT_SEND_FAILURE
;
4263 zclient_create_header(s
, cmd
, 0);
4264 stream_putl(s
, api
->cap
);
4267 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4268 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4269 stream_putl(s
, api
->stale_removal_time
);
4270 stream_putl(s
, api
->vrf_id
);
4272 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4273 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4274 stream_putl(s
, api
->afi
);
4275 stream_putl(s
, api
->safi
);
4276 stream_putl(s
, api
->vrf_id
);
4278 case ZEBRA_CLIENT_GR_DISABLE
:
4279 stream_putl(s
, api
->vrf_id
);
4283 /* Put length at the first point of the stream */
4284 stream_putw_at(s
, 0, stream_get_endp(s
));
4286 return zclient_send_message(zclient
);
4290 * Process capabilities message from zebra
4292 int32_t zapi_capabilities_decode(struct stream
*s
, struct zapi_cap
*api
)
4295 memset(api
, 0, sizeof(*api
));
4297 STREAM_GETL(s
, api
->cap
);
4299 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4300 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4301 STREAM_GETL(s
, api
->stale_removal_time
);
4302 STREAM_GETL(s
, api
->vrf_id
);
4304 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4305 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4306 STREAM_GETL(s
, api
->afi
);
4307 STREAM_GETL(s
, api
->safi
);
4308 STREAM_GETL(s
, api
->vrf_id
);
4310 case ZEBRA_CLIENT_GR_DISABLE
:
4311 STREAM_GETL(s
, api
->vrf_id
);
4318 enum zclient_send_status
4319 zclient_send_neigh_discovery_req(struct zclient
*zclient
,
4320 const struct interface
*ifp
,
4321 const struct prefix
*p
)
4328 zclient_create_header(s
, ZEBRA_NEIGH_DISCOVER
, ifp
->vrf
->vrf_id
);
4329 stream_putl(s
, ifp
->ifindex
);
4331 stream_putc(s
, p
->family
);
4332 stream_putc(s
, p
->prefixlen
);
4333 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
4335 stream_putw_at(s
, 0, stream_get_endp(s
));
4336 return zclient_send_message(zclient
);
4340 * Get a starting nhg point for a routing protocol
4342 uint32_t zclient_get_nhg_start(uint32_t proto
)
4344 assert(proto
< ZEBRA_ROUTE_MAX
);
4346 return ZEBRA_NHG_PROTO_SPACING
* proto
;
4349 char *zclient_dump_route_flags(uint32_t flags
, char *buf
, size_t len
)
4352 snprintfrr(buf
, len
, "None ");
4357 buf
, len
, "%s%s%s%s%s%s%s%s%s%s",
4358 CHECK_FLAG(flags
, ZEBRA_FLAG_ALLOW_RECURSION
) ? "Recursion "
4360 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) ? "Self " : "",
4361 CHECK_FLAG(flags
, ZEBRA_FLAG_IBGP
) ? "iBGP " : "",
4362 CHECK_FLAG(flags
, ZEBRA_FLAG_SELECTED
) ? "Selected " : "",
4363 CHECK_FLAG(flags
, ZEBRA_FLAG_FIB_OVERRIDE
) ? "Override " : "",
4364 CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
) ? "Evpn " : "",
4365 CHECK_FLAG(flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) ? "RR Distance "
4367 CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
) ? "Trapped " : "",
4368 CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
) ? "Offloaded " : "",
4369 CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
) ? "Offload Failed "
4374 char *zclient_evpn_dump_macip_flags(uint8_t flags
, char *buf
, size_t len
)
4377 snprintfrr(buf
, len
, "None ");
4382 buf
, len
, "%s%s%s%s%s%s%s",
4383 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? "Sticky MAC " : "",
4384 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? "Gateway MAC " : "",
4385 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? "Router "
4387 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_OVERRIDE_FLAG
) ? "Override "
4389 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
) ? "SVI MAC " : "",
4390 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
) ? "Proxy "
4392 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
) ? "Sync " : "");
4397 static int zclient_neigh_ip_read_entry(struct stream
*s
, struct ipaddr
*add
)
4401 STREAM_GETC(s
, family
);
4402 if (family
!= AF_INET
&& family
!= AF_INET6
)
4405 STREAM_GET(&add
->ip
.addr
, s
, family2addrsize(family
));
4406 add
->ipa_type
= family
;
4412 int zclient_neigh_ip_encode(struct stream
*s
, uint16_t cmd
, union sockunion
*in
,
4413 union sockunion
*out
, struct interface
*ifp
,
4418 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
4419 stream_putc(s
, sockunion_family(in
));
4420 stream_write(s
, sockunion_get_addr(in
), sockunion_get_addrlen(in
));
4421 if (out
&& sockunion_family(out
) != AF_UNSPEC
) {
4422 stream_putc(s
, sockunion_family(out
));
4423 stream_write(s
, sockunion_get_addr(out
),
4424 sockunion_get_addrlen(out
));
4426 stream_putc(s
, AF_UNSPEC
);
4427 stream_putl(s
, ifp
->ifindex
);
4429 stream_putl(s
, ndm_state
);
4431 stream_putl(s
, ZEBRA_NEIGH_STATE_FAILED
);
4435 int zclient_neigh_ip_decode(struct stream
*s
, struct zapi_neigh_ip
*api
)
4439 ret
= zclient_neigh_ip_read_entry(s
, &api
->ip_in
);
4442 zclient_neigh_ip_read_entry(s
, &api
->ip_out
);
4444 STREAM_GETL(s
, api
->index
);
4445 STREAM_GETL(s
, api
->ndm_state
);
4451 int zclient_send_zebra_gre_request(struct zclient
*client
,
4452 struct interface
*ifp
)
4456 if (!client
|| client
->sock
< 0) {
4457 zlog_err("%s : zclient not ready", __func__
);
4462 zclient_create_header(s
, ZEBRA_GRE_GET
, ifp
->vrf
->vrf_id
);
4463 stream_putl(s
, ifp
->ifindex
);
4464 stream_putw_at(s
, 0, stream_get_endp(s
));
4465 zclient_send_message(client
);