1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
41 #include "lib_errors.h"
44 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
45 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
47 /* Zebra client events. */
48 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
50 /* Prototype for event manager. */
51 static void zclient_event(enum event
, struct zclient
*);
53 static void zebra_interface_if_set_value(struct stream
*s
,
54 struct interface
*ifp
);
56 struct zclient_options zclient_options_default
= {.receive_notify
= false,
57 .synchronous
= false};
59 struct sockaddr_storage zclient_addr
;
60 socklen_t zclient_addr_len
;
62 /* This file local debug flag. */
63 static int zclient_debug
;
65 /* Allocate zclient structure. */
66 struct zclient
*zclient_new(struct thread_master
*master
,
67 struct zclient_options
*opt
)
69 struct zclient
*zclient
;
71 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
73 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
75 zclient
->ibuf
= stream_new(stream_size
);
76 zclient
->obuf
= stream_new(stream_size
);
77 zclient
->wb
= buffer_new(0);
78 zclient
->master
= master
;
80 zclient
->receive_notify
= opt
->receive_notify
;
81 zclient
->synchronous
= opt
->synchronous
;
86 /* This function is only called when exiting, because
87 many parts of the code do not check for I/O errors, so they could
88 reference an invalid pointer if the structure was ever freed.
90 Free zclient structure. */
91 void zclient_free(struct zclient
*zclient
)
94 stream_free(zclient
->ibuf
);
96 stream_free(zclient
->obuf
);
98 buffer_free(zclient
->wb
);
100 XFREE(MTYPE_ZCLIENT
, zclient
);
103 unsigned short *redist_check_instance(struct redist_proto
*red
,
104 unsigned short instance
)
106 struct listnode
*node
;
112 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
119 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
126 red
->instances
= list_new();
128 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
130 listnode_add(red
->instances
, in
);
133 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
137 id
= redist_check_instance(red
, instance
);
141 listnode_delete(red
->instances
, id
);
142 XFREE(MTYPE_REDIST_INST
, id
);
143 if (!red
->instances
->count
) {
145 list_delete(&red
->instances
);
149 void redist_del_all_instances(struct redist_proto
*red
)
151 struct listnode
*ln
, *nn
;
157 for (ALL_LIST_ELEMENTS(red
->instances
, ln
, nn
, id
))
158 redist_del_instance(red
, *id
);
161 /* Stop zebra client services. */
162 void zclient_stop(struct zclient
*zclient
)
168 zlog_debug("zclient %p stopped", zclient
);
171 THREAD_OFF(zclient
->t_read
);
172 THREAD_OFF(zclient
->t_connect
);
173 THREAD_OFF(zclient
->t_write
);
176 stream_reset(zclient
->ibuf
);
177 stream_reset(zclient
->obuf
);
179 /* Empty the write buffer. */
180 buffer_reset(zclient
->wb
);
183 if (zclient
->sock
>= 0) {
184 close(zclient
->sock
);
189 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
190 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
191 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
192 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
195 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
198 vrf_bitmap_free(zclient
->default_information
[afi
]);
199 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
203 void zclient_reset(struct zclient
*zclient
)
207 zclient_stop(zclient
);
209 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
211 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
214 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
219 * Connect to zebra daemon.
220 * @param zclient a pointer to zclient structure
221 * @return socket fd just to make sure that connection established
225 int zclient_socket_connect(struct zclient
*zclient
)
230 /* We should think about IPv6 connection. */
231 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
236 setsockopt_so_sendbuf(sock
, 1048576);
238 /* Connect to zebra. */
239 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
242 zlog_debug("%s connect failure: %d(%s)", __func__
,
243 errno
, safe_strerror(errno
));
248 zclient
->sock
= sock
;
252 static int zclient_failed(struct zclient
*zclient
)
255 zclient_stop(zclient
);
256 zclient_event(ZCLIENT_CONNECT
, zclient
);
260 static int zclient_flush_data(struct thread
*thread
)
262 struct zclient
*zclient
= THREAD_ARG(thread
);
264 zclient
->t_write
= NULL
;
265 if (zclient
->sock
< 0)
267 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
271 "%s: buffer_flush_available failed on zclient fd %d, closing",
272 __func__
, zclient
->sock
);
273 return zclient_failed(zclient
);
275 zclient
->t_write
= NULL
;
276 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
277 zclient
->sock
, &zclient
->t_write
);
285 int zclient_send_message(struct zclient
*zclient
)
287 if (zclient
->sock
< 0)
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 THREAD_OFF(zclient
->t_write
);
301 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
302 zclient
->sock
, &zclient
->t_write
);
309 * If we add more data to this structure please ensure that
310 * struct zmsghdr in lib/zclient.h is updated as appropriate.
312 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
314 /* length placeholder, caller can update */
315 stream_putw(s
, ZEBRA_HEADER_SIZE
);
316 stream_putc(s
, ZEBRA_HEADER_MARKER
);
317 stream_putc(s
, ZSERV_VERSION
);
318 stream_putl(s
, vrf_id
);
319 stream_putw(s
, command
);
322 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
323 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
326 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
329 STREAM_GETW(s
, *size
);
330 *size
-= ZEBRA_HEADER_SIZE
;
331 STREAM_GETC(s
, *marker
);
332 STREAM_GETC(s
, *version
);
333 STREAM_GETL(s
, *vrf_id
);
334 STREAM_GETW(s
, *cmd
);
336 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
338 EC_LIB_ZAPI_MISSMATCH
,
339 "%s: socket %d version mismatch, marker %d, version %d",
340 __func__
, sock
, *marker
, *version
);
344 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
352 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
354 STREAM_GETW(zmsg
, hdr
->length
);
355 STREAM_GETC(zmsg
, hdr
->marker
);
356 STREAM_GETC(zmsg
, hdr
->version
);
357 STREAM_GETL(zmsg
, hdr
->vrf_id
);
358 STREAM_GETW(zmsg
, hdr
->command
);
364 /* Send simple Zebra message. */
365 static int zebra_message_send(struct zclient
*zclient
, int command
,
370 /* Get zclient output buffer. */
374 /* Send very simple command only Zebra message. */
375 zclient_create_header(s
, command
, vrf_id
);
377 return zclient_send_message(zclient
);
380 int zclient_send_hello(struct zclient
*zclient
)
384 if (zclient
->redist_default
|| zclient
->synchronous
) {
388 /* The VRF ID in the HELLO message is always 0. */
389 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
390 stream_putc(s
, zclient
->redist_default
);
391 stream_putw(s
, zclient
->instance
);
392 stream_putl(s
, zclient
->session_id
);
393 if (zclient
->receive_notify
)
397 if (zclient
->synchronous
)
402 stream_putw_at(s
, 0, stream_get_endp(s
));
403 return zclient_send_message(zclient
);
409 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
410 mpls_label_t label
, enum lsp_types_t ltype
)
417 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
418 stream_putl(s
, label
);
420 stream_putc(s
, ltype
);
421 stream_putw_at(s
, 0, stream_get_endp(s
));
422 zclient_send_message(zclient
);
425 /* Send register requests to zebra daemon for the information in a VRF. */
426 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
431 /* If not connected to the zebra yet. */
432 if (zclient
->sock
< 0)
436 zlog_debug("%s: send register messages for VRF %u", __func__
,
439 /* We need router-id information. */
440 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_ADD
, AFI_IP
,
443 /* We need interface information. */
444 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
446 /* Set unwanted redistribute route. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
448 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
451 /* Flush all redistribute request. */
452 if (vrf_id
== VRF_DEFAULT
) {
453 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
454 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
455 if (!zclient
->mi_redist
[afi
][i
].enabled
)
458 struct listnode
*node
;
461 for (ALL_LIST_ELEMENTS_RO(
462 zclient
->mi_redist
[afi
][i
]
465 if (!(i
== zclient
->redist_default
466 && *id
== zclient
->instance
))
467 zebra_redistribute_send(
468 ZEBRA_REDISTRIBUTE_ADD
,
469 zclient
, afi
, i
, *id
,
475 /* Resend all redistribute request. */
476 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
477 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
478 if (i
!= zclient
->redist_default
479 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
481 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
485 /* If default information is needed. */
486 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
487 zebra_redistribute_default_send(
488 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
493 /* Send unregister requests to zebra daemon for the information in a VRF. */
494 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
499 /* If not connected to the zebra yet. */
500 if (zclient
->sock
< 0)
504 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
507 /* We need router-id information. */
508 zclient_send_router_id_update(zclient
, ZEBRA_ROUTER_ID_DELETE
, AFI_IP
,
511 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
513 /* Set unwanted redistribute route. */
514 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
515 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
518 /* Flush all redistribute request. */
519 if (vrf_id
== VRF_DEFAULT
) {
520 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
521 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
522 if (!zclient
->mi_redist
[afi
][i
].enabled
)
525 struct listnode
*node
;
528 for (ALL_LIST_ELEMENTS_RO(
529 zclient
->mi_redist
[afi
][i
]
532 if (!(i
== zclient
->redist_default
533 && *id
== zclient
->instance
))
534 zebra_redistribute_send(
535 ZEBRA_REDISTRIBUTE_DELETE
,
536 zclient
, afi
, i
, *id
,
542 /* Flush all redistribute request. */
543 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
544 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
545 if (i
!= zclient
->redist_default
546 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
548 zebra_redistribute_send(
549 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
552 /* If default information is needed. */
553 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
554 zebra_redistribute_default_send(
555 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
560 int zclient_send_router_id_update(struct zclient
*zclient
,
561 zebra_message_types_t type
, afi_t afi
,
564 struct stream
*s
= zclient
->obuf
;
566 zclient_create_header(s
, type
, vrf_id
);
568 stream_putw_at(s
, 0, stream_get_endp(s
));
569 return zclient_send_message(zclient
);
572 /* Send request to zebra daemon to start or stop RA. */
573 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
574 struct interface
*ifp
, int enable
,
579 /* If not connected to the zebra yet. */
580 if (zclient
->sock
< 0)
583 /* Form and send message. */
588 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
590 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
592 stream_putl(s
, ifp
->ifindex
);
593 stream_putl(s
, ra_interval
);
595 stream_putw_at(s
, 0, stream_get_endp(s
));
597 zclient_send_message(zclient
);
600 int zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
601 struct interface
*ifp
, bool down
)
605 if (zclient
->sock
< 0)
610 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
611 stream_putl(s
, ifp
->ifindex
);
612 stream_putc(s
, !!down
);
613 stream_putw_at(s
, 0, stream_get_endp(s
));
614 zclient_send_message(zclient
);
619 /* Make connection to zebra daemon. */
620 int zclient_start(struct zclient
*zclient
)
623 zlog_info("zclient_start is called");
625 /* If already connected to the zebra. */
626 if (zclient
->sock
>= 0)
629 /* Check connect thread. */
630 if (zclient
->t_connect
)
633 if (zclient_socket_connect(zclient
) < 0) {
635 zlog_debug("zclient connection fail");
637 zclient_event(ZCLIENT_CONNECT
, zclient
);
641 if (set_nonblocking(zclient
->sock
) < 0)
642 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
643 __func__
, zclient
->sock
);
645 /* Clear fail count. */
648 zlog_debug("zclient connect success with socket [%d]",
651 /* Create read thread. */
652 zclient_event(ZCLIENT_READ
, zclient
);
654 zclient_send_hello(zclient
);
656 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
658 /* Inform the successful connection. */
659 if (zclient
->zebra_connected
)
660 (*zclient
->zebra_connected
)(zclient
);
665 /* Initialize zebra client. Argument redist_default is unwanted
666 redistribute route type. */
667 void zclient_init(struct zclient
*zclient
, int redist_default
,
668 unsigned short instance
, struct zebra_privs_t
*privs
)
672 /* Set -1 to the default socket value. */
674 zclient
->privs
= privs
;
676 /* Clear redistribution flags. */
677 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
678 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
679 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
681 /* Set unwanted redistribute route. bgpd does not need BGP route
683 zclient
->redist_default
= redist_default
;
684 zclient
->instance
= instance
;
685 /* Pending: make afi(s) an arg. */
686 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
687 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
690 /* Set default-information redistribute to zero. */
691 zclient
->default_information
[afi
] = vrf_bitmap_init();
695 zlog_debug("scheduling zclient connection");
697 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
700 /* This function is a wrapper function for calling zclient_start from
701 timer or event thread. */
702 static int zclient_connect(struct thread
*t
)
704 struct zclient
*zclient
;
706 zclient
= THREAD_ARG(t
);
707 zclient
->t_connect
= NULL
;
710 zlog_debug("zclient_connect is called");
712 return zclient_start(zclient
);
715 int zclient_send_rnh(struct zclient
*zclient
, int command
,
716 const struct prefix
*p
, bool exact_match
,
723 zclient_create_header(s
, command
, vrf_id
);
724 stream_putc(s
, (exact_match
) ? 1 : 0);
726 stream_putw(s
, PREFIX_FAMILY(p
));
727 stream_putc(s
, p
->prefixlen
);
728 switch (PREFIX_FAMILY(p
)) {
730 stream_put_in_addr(s
, &p
->u
.prefix4
);
733 stream_put(s
, &(p
->u
.prefix6
), 16);
738 stream_putw_at(s
, 0, stream_get_endp(s
));
740 return zclient_send_message(zclient
);
744 * "xdr_encode"-like interface that allows daemon (client) to send
745 * a message to zebra server for a route that needs to be
746 * added/deleted to the kernel. Info about the route is specified
747 * by the caller in a struct zapi_route. zapi_route_encode() then writes
748 * the info down the zclient socket using the stream_* functions.
750 * The corresponding read ("xdr_decode") function on the server
751 * side is zapi_route_decode().
753 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
754 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
755 * | Length (2) | Command | Route Type |
756 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
757 * | ZEBRA Flags | Message Flags | Prefix length |
758 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
759 * | Destination IPv4 Prefix for route |
760 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
765 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
766 * described, as per the Nexthop count. Each nexthop described as:
769 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
770 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
771 * | IPv4 Nexthop address or Interface Index number |
772 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
774 * Alternatively, if the route is a blackhole route, then Nexthop count
775 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
778 * The original struct zapi_route_*() infrastructure was built around
779 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
780 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
781 * nexthops into zapi_route_encode() using the same zapi_route structure.
782 * This is done by setting zapi_route fields as follows:
783 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
784 * - .nexthop_num == .ifindex_num
785 * - .nexthop and .ifindex are filled with gate and ifindex parts of
786 * each compound nexthop, both in the same order
788 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
791 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
794 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
796 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
798 * XXX: No attention paid to alignment.
800 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
801 struct zapi_route
*api
)
803 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
805 return zclient_send_message(zclient
);
808 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
809 const struct zapi_nexthop
*next2
)
811 if (next1
->label_num
> next2
->label_num
)
814 if (next1
->label_num
< next2
->label_num
)
817 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
820 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
821 const struct zapi_nexthop
*next2
)
825 if (next1
->vrf_id
< next2
->vrf_id
)
828 if (next1
->vrf_id
> next2
->vrf_id
)
831 if (next1
->type
< next2
->type
)
834 if (next1
->type
> next2
->type
)
837 if (next1
->weight
< next2
->weight
)
840 if (next1
->weight
> next2
->weight
)
843 switch (next1
->type
) {
844 case NEXTHOP_TYPE_IPV4
:
845 case NEXTHOP_TYPE_IPV6
:
846 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
851 case NEXTHOP_TYPE_IPV4_IFINDEX
:
852 case NEXTHOP_TYPE_IPV6_IFINDEX
:
853 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
857 /* Intentional Fall-Through */
858 case NEXTHOP_TYPE_IFINDEX
:
859 if (next1
->ifindex
< next2
->ifindex
)
862 if (next1
->ifindex
> next2
->ifindex
)
865 case NEXTHOP_TYPE_BLACKHOLE
:
866 if (next1
->bh_type
< next2
->bh_type
)
869 if (next1
->bh_type
> next2
->bh_type
)
874 if (next1
->srte_color
< next2
->srte_color
)
876 if (next1
->srte_color
> next2
->srte_color
)
879 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) ||
880 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
882 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
883 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
886 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
887 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
890 if (next1
->backup_num
> 0 || next2
->backup_num
> 0) {
892 if (next1
->backup_num
< next2
->backup_num
)
895 if (next1
->backup_num
> next2
->backup_num
)
898 ret
= memcmp(next1
->backup_idx
,
899 next2
->backup_idx
, next1
->backup_num
);
908 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
912 const struct zapi_nexthop
*next1
= item1
;
913 const struct zapi_nexthop
*next2
= item2
;
915 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
919 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
924 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
925 uint16_t nexthop_num
)
927 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
932 * Encode a single zapi nexthop
934 int zapi_nexthop_encode(struct stream
*s
, const struct zapi_nexthop
*api_nh
,
935 uint32_t api_flags
, uint32_t api_message
)
938 int nh_flags
= api_nh
->flags
;
940 stream_putl(s
, api_nh
->vrf_id
);
941 stream_putc(s
, api_nh
->type
);
943 /* If needed, set 'labelled nexthop' flag */
944 if (api_nh
->label_num
> 0) {
945 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
947 /* Validate label count */
948 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
954 /* If present, set 'weight' flag before encoding flags */
956 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
);
958 /* Note that we're only encoding a single octet */
959 stream_putc(s
, nh_flags
);
961 switch (api_nh
->type
) {
962 case NEXTHOP_TYPE_BLACKHOLE
:
963 stream_putc(s
, api_nh
->bh_type
);
965 case NEXTHOP_TYPE_IPV4
:
966 case NEXTHOP_TYPE_IPV4_IFINDEX
:
967 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
968 stream_putl(s
, api_nh
->ifindex
);
970 case NEXTHOP_TYPE_IFINDEX
:
971 stream_putl(s
, api_nh
->ifindex
);
973 case NEXTHOP_TYPE_IPV6
:
974 case NEXTHOP_TYPE_IPV6_IFINDEX
:
975 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
977 stream_putl(s
, api_nh
->ifindex
);
981 /* We only encode labels if we have >0 - we use
982 * the per-nexthop flag above to signal that the count
983 * is present in the payload.
985 if (api_nh
->label_num
> 0) {
986 stream_putc(s
, api_nh
->label_num
);
987 stream_put(s
, &api_nh
->labels
[0],
988 api_nh
->label_num
* sizeof(mpls_label_t
));
992 stream_putl(s
, api_nh
->weight
);
994 /* Router MAC for EVPN routes. */
995 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
996 stream_put(s
, &(api_nh
->rmac
),
997 sizeof(struct ethaddr
));
999 /* Color for Segment Routing TE. */
1000 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1001 stream_putl(s
, api_nh
->srte_color
);
1003 /* Index of backup nexthop */
1004 if (CHECK_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1005 /* Validate backup count */
1006 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1011 stream_putc(s
, api_nh
->backup_num
);
1012 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1013 stream_putc(s
, api_nh
->backup_idx
[i
]);
1020 int zapi_nhg_encode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1024 if (cmd
!= ZEBRA_NHG_DEL
&& cmd
!= ZEBRA_NHG_ADD
) {
1025 flog_err(EC_LIB_ZAPI_ENCODE
,
1026 "%s: Specified zapi NHG command (%d) doesn't exist\n",
1032 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1034 stream_putw(s
, api_nhg
->proto
);
1035 stream_putl(s
, api_nhg
->id
);
1037 if (cmd
== ZEBRA_NHG_ADD
) {
1039 zapi_nexthop_group_sort(api_nhg
->nexthops
,
1040 api_nhg
->nexthop_num
);
1042 stream_putw(s
, api_nhg
->nexthop_num
);
1044 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++)
1045 zapi_nexthop_encode(s
, &api_nhg
->nexthops
[i
], 0, 0);
1047 /* Backup nexthops */
1049 stream_putw(s
, api_nhg
->backup_nexthop_num
);
1051 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++)
1052 zapi_nexthop_encode(s
, &api_nhg
->backup_nexthops
[i
], 0,
1056 stream_putw_at(s
, 0, stream_get_endp(s
));
1061 int zclient_nhg_send(struct zclient
*zclient
, int cmd
, struct zapi_nhg
*api_nhg
)
1063 api_nhg
->proto
= zclient
->redist_default
;
1065 if (zapi_nhg_encode(zclient
->obuf
, cmd
, api_nhg
))
1068 return zclient_send_message(zclient
);
1071 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
1073 struct zapi_nexthop
*api_nh
;
1078 zclient_create_header(s
, cmd
, api
->vrf_id
);
1080 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1081 flog_err(EC_LIB_ZAPI_ENCODE
,
1082 "%s: Specified route type (%u) is not a legal value\n",
1083 __func__
, api
->type
);
1086 stream_putc(s
, api
->type
);
1088 stream_putw(s
, api
->instance
);
1089 stream_putl(s
, api
->flags
);
1090 stream_putl(s
, api
->message
);
1092 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1093 flog_err(EC_LIB_ZAPI_ENCODE
,
1094 "%s: Specified route SAFI (%u) is not a legal value\n",
1095 __func__
, api
->safi
);
1098 stream_putc(s
, api
->safi
);
1100 /* Put prefix information. */
1101 stream_putc(s
, api
->prefix
.family
);
1102 psize
= PSIZE(api
->prefix
.prefixlen
);
1103 stream_putc(s
, api
->prefix
.prefixlen
);
1104 stream_write(s
, &api
->prefix
.u
.prefix
, psize
);
1106 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1107 psize
= PSIZE(api
->src_prefix
.prefixlen
);
1108 stream_putc(s
, api
->src_prefix
.prefixlen
);
1109 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
1112 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1113 stream_putl(s
, api
->nhgid
);
1116 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1117 /* limit the number of nexthops if necessary */
1118 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1119 char buf
[PREFIX2STR_BUFFER
];
1121 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1124 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
1125 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1129 /* We canonicalize the nexthops by sorting them; this allows
1130 * zebra to resolve the list of nexthops to a nexthop-group
1133 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
1135 stream_putw(s
, api
->nexthop_num
);
1137 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1138 api_nh
= &api
->nexthops
[i
];
1140 /* MPLS labels for BGP-LU or Segment Routing */
1141 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1142 char buf
[PREFIX2STR_BUFFER
];
1144 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1146 flog_err(EC_LIB_ZAPI_ENCODE
,
1147 "%s: prefix %s: can't encode %u labels (maximum is %u)",
1154 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1161 /* Backup nexthops */
1162 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1163 /* limit the number of nexthops if necessary */
1164 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1165 char buf
[PREFIX2STR_BUFFER
];
1167 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1170 "%s: prefix %s: can't encode %u backup nexthops (maximum is %u)",
1171 __func__
, buf
, api
->backup_nexthop_num
,
1176 /* Note that we do not sort the list of backup nexthops -
1177 * this list is treated as an array and indexed by each
1178 * primary nexthop that is associated with a backup.
1181 stream_putw(s
, api
->backup_nexthop_num
);
1183 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1184 api_nh
= &api
->backup_nexthops
[i
];
1186 /* MPLS labels for BGP-LU or Segment Routing */
1187 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1188 char buf
[PREFIX2STR_BUFFER
];
1190 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1192 flog_err(EC_LIB_ZAPI_ENCODE
,
1193 "%s: prefix %s: backup: can't encode %u labels (maximum is %u)",
1200 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
,
1208 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1209 stream_putc(s
, api
->distance
);
1210 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1211 stream_putl(s
, api
->metric
);
1212 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1213 stream_putl(s
, api
->tag
);
1214 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1215 stream_putl(s
, api
->mtu
);
1216 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1217 stream_putl(s
, api
->tableid
);
1219 /* Put length at the first point of the stream. */
1220 stream_putw_at(s
, 0, stream_get_endp(s
));
1226 * Decode a single zapi nexthop object
1228 int zapi_nexthop_decode(struct stream
*s
, struct zapi_nexthop
*api_nh
,
1229 uint32_t api_flags
, uint32_t api_message
)
1233 STREAM_GETL(s
, api_nh
->vrf_id
);
1234 STREAM_GETC(s
, api_nh
->type
);
1236 /* Note that we're only using a single octet of flags */
1237 STREAM_GETC(s
, api_nh
->flags
);
1239 switch (api_nh
->type
) {
1240 case NEXTHOP_TYPE_BLACKHOLE
:
1241 STREAM_GETC(s
, api_nh
->bh_type
);
1243 case NEXTHOP_TYPE_IPV4
:
1244 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1245 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1247 STREAM_GETL(s
, api_nh
->ifindex
);
1249 case NEXTHOP_TYPE_IFINDEX
:
1250 STREAM_GETL(s
, api_nh
->ifindex
);
1252 case NEXTHOP_TYPE_IPV6
:
1253 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1254 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1255 STREAM_GETL(s
, api_nh
->ifindex
);
1259 /* MPLS labels for BGP-LU or Segment Routing */
1260 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)) {
1261 STREAM_GETC(s
, api_nh
->label_num
);
1262 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1265 "%s: invalid number of MPLS labels (%u)",
1266 __func__
, api_nh
->label_num
);
1270 STREAM_GET(&api_nh
->labels
[0], s
,
1271 api_nh
->label_num
* sizeof(mpls_label_t
));
1274 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1275 STREAM_GETL(s
, api_nh
->weight
);
1277 /* Router MAC for EVPN routes. */
1278 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1279 STREAM_GET(&(api_nh
->rmac
), s
,
1280 sizeof(struct ethaddr
));
1282 /* Color for Segment Routing TE. */
1283 if (CHECK_FLAG(api_message
, ZAPI_MESSAGE_SRTE
))
1284 STREAM_GETL(s
, api_nh
->srte_color
);
1286 /* Backup nexthop index */
1287 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1288 STREAM_GETC(s
, api_nh
->backup_num
);
1290 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1293 for (i
= 0; i
< api_nh
->backup_num
; i
++)
1294 STREAM_GETC(s
, api_nh
->backup_idx
[i
]);
1305 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1307 struct zapi_nexthop
*api_nh
;
1310 memset(api
, 0, sizeof(*api
));
1312 /* Type, flags, message. */
1313 STREAM_GETC(s
, api
->type
);
1314 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1315 flog_err(EC_LIB_ZAPI_ENCODE
,
1316 "%s: Specified route type: %d is not a legal value\n",
1317 __func__
, api
->type
);
1321 STREAM_GETW(s
, api
->instance
);
1322 STREAM_GETL(s
, api
->flags
);
1323 STREAM_GETL(s
, api
->message
);
1324 STREAM_GETC(s
, api
->safi
);
1325 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1326 flog_err(EC_LIB_ZAPI_ENCODE
,
1327 "%s: Specified route SAFI (%u) is not a legal value\n",
1328 __func__
, api
->safi
);
1333 STREAM_GETC(s
, api
->prefix
.family
);
1334 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1335 switch (api
->prefix
.family
) {
1337 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1340 "%s: V4 prefixlen is %d which should not be more than 32",
1341 __func__
, api
->prefix
.prefixlen
);
1346 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1349 "%s: v6 prefixlen is %d which should not be more than 128",
1350 __func__
, api
->prefix
.prefixlen
);
1355 flog_err(EC_LIB_ZAPI_ENCODE
,
1356 "%s: Specified family %d is not v4 or v6", __func__
,
1357 api
->prefix
.family
);
1360 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1362 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1363 api
->src_prefix
.family
= AF_INET6
;
1364 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1365 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1368 "%s: SRC Prefix prefixlen received: %d is too large",
1369 __func__
, api
->src_prefix
.prefixlen
);
1372 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1373 PSIZE(api
->src_prefix
.prefixlen
));
1375 if (api
->prefix
.family
!= AF_INET6
1376 || api
->src_prefix
.prefixlen
== 0) {
1379 "%s: SRC prefix specified in some manner that makes no sense",
1385 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NHG
))
1386 STREAM_GETL(s
, api
->nhgid
);
1389 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1390 STREAM_GETW(s
, api
->nexthop_num
);
1391 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1392 flog_err(EC_LIB_ZAPI_ENCODE
,
1393 "%s: invalid number of nexthops (%u)",
1394 __func__
, api
->nexthop_num
);
1398 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1399 api_nh
= &api
->nexthops
[i
];
1401 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1408 /* Backup nexthops. */
1409 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)) {
1410 STREAM_GETW(s
, api
->backup_nexthop_num
);
1411 if (api
->backup_nexthop_num
> MULTIPATH_NUM
) {
1412 flog_err(EC_LIB_ZAPI_ENCODE
,
1413 "%s: invalid number of backup nexthops (%u)",
1414 __func__
, api
->backup_nexthop_num
);
1418 for (i
= 0; i
< api
->backup_nexthop_num
; i
++) {
1419 api_nh
= &api
->backup_nexthops
[i
];
1421 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
,
1429 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1430 STREAM_GETC(s
, api
->distance
);
1431 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1432 STREAM_GETL(s
, api
->metric
);
1433 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1434 STREAM_GETL(s
, api
->tag
);
1435 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1436 STREAM_GETL(s
, api
->mtu
);
1437 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1438 STREAM_GETL(s
, api
->tableid
);
1445 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1451 memset(&any
, 0, sizeof(any
));
1452 any
.family
= family
;
1456 stream_putc(s
, p
->family
);
1457 stream_putc(s
, p
->prefixlen
);
1458 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1461 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1464 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1467 * We are sending one item at a time at the moment
1471 stream_putl(s
, zrule
->seq
);
1472 stream_putl(s
, zrule
->priority
);
1473 stream_putl(s
, zrule
->unique
);
1475 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1476 zrule
->filter
.src_ip
.family
);
1477 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1478 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1479 zrule
->filter
.src_ip
.family
);
1480 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1481 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1483 stream_putl(s
, zrule
->action
.table
);
1484 stream_put(s
, zrule
->ifname
, INTERFACE_NAMSIZ
);
1486 /* Put length at the first point of the stream. */
1487 stream_putw_at(s
, 0, stream_get_endp(s
));
1492 bool zapi_nhg_notify_decode(struct stream
*s
, uint32_t *id
,
1493 enum zapi_nhg_notify_owner
*note
)
1497 STREAM_GET(note
, s
, sizeof(*note
));
1498 STREAM_GETL(s
, read_id
);
1508 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1510 enum zapi_route_notify_owner
*note
)
1514 STREAM_GET(note
, s
, sizeof(*note
));
1516 STREAM_GETC(s
, p
->family
);
1517 STREAM_GETC(s
, p
->prefixlen
);
1518 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1529 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1530 uint32_t *priority
, uint32_t *unique
, char *ifname
,
1531 enum zapi_rule_notify_owner
*note
)
1533 uint32_t prio
, seq
, uni
;
1535 STREAM_GET(note
, s
, sizeof(*note
));
1537 STREAM_GETL(s
, seq
);
1538 STREAM_GETL(s
, prio
);
1539 STREAM_GETL(s
, uni
);
1540 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
1543 zlog_debug("%s: %u %u %u %s", __func__
, seq
, prio
, uni
, ifname
);
1554 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1555 enum zapi_ipset_notify_owner
*note
)
1559 STREAM_GET(note
, s
, sizeof(*note
));
1561 STREAM_GETL(s
, uni
);
1564 zlog_debug("%s: %u", __func__
, uni
);
1573 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1575 enum zapi_ipset_entry_notify_owner
*note
)
1579 STREAM_GET(note
, s
, sizeof(*note
));
1581 STREAM_GETL(s
, uni
);
1583 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1586 zlog_debug("%s: %u", __func__
, uni
);
1595 bool zapi_iptable_notify_decode(struct stream
*s
,
1597 enum zapi_iptable_notify_owner
*note
)
1601 STREAM_GET(note
, s
, sizeof(*note
));
1603 STREAM_GETL(s
, uni
);
1606 zlog_debug("%s: %u", __func__
, uni
);
1615 struct nexthop
*nexthop_from_zapi_nexthop(const struct zapi_nexthop
*znh
)
1617 struct nexthop
*n
= nexthop_new();
1619 n
->type
= znh
->type
;
1620 n
->vrf_id
= znh
->vrf_id
;
1621 n
->ifindex
= znh
->ifindex
;
1622 n
->gate
= znh
->gate
;
1623 n
->srte_color
= znh
->srte_color
;
1626 * This function currently handles labels
1628 if (znh
->label_num
) {
1629 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1633 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1634 SET_FLAG(n
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1635 n
->backup_num
= znh
->backup_num
;
1636 memcpy(n
->backup_idx
, znh
->backup_idx
, n
->backup_num
);
1643 * Convert nexthop to zapi nexthop
1645 int zapi_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1646 const struct nexthop
*nh
)
1650 memset(znh
, 0, sizeof(*znh
));
1652 znh
->type
= nh
->type
;
1653 znh
->vrf_id
= nh
->vrf_id
;
1654 znh
->weight
= nh
->weight
;
1655 znh
->ifindex
= nh
->ifindex
;
1656 znh
->gate
= nh
->gate
;
1658 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
1659 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1661 if (nh
->nh_label
&& (nh
->nh_label
->num_labels
> 0)) {
1664 if (nh
->nh_label
->num_labels
> MPLS_MAX_LABELS
)
1667 for (i
= 0; i
< nh
->nh_label
->num_labels
; i
++)
1668 znh
->labels
[i
] = nh
->nh_label
->label
[i
];
1671 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1674 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1675 if (nh
->backup_num
> NEXTHOP_MAX_BACKUPS
)
1678 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1679 znh
->backup_num
= nh
->backup_num
;
1680 memcpy(znh
->backup_idx
, nh
->backup_idx
, znh
->backup_num
);
1687 * Wrapper that converts backup nexthop
1689 int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1690 const struct nexthop
*nh
)
1694 /* Ensure that zapi flags are correct: backups don't have backups */
1695 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
1697 UNSET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
1703 * Format some info about a zapi nexthop, for debug or logging.
1705 const char *zapi_nexthop2str(const struct zapi_nexthop
*znh
, char *buf
,
1708 char tmp
[INET6_ADDRSTRLEN
];
1710 switch (znh
->type
) {
1711 case NEXTHOP_TYPE_IFINDEX
:
1712 snprintf(buf
, bufsize
, "if %u", znh
->ifindex
);
1714 case NEXTHOP_TYPE_IPV4
:
1715 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1716 inet_ntop(AF_INET
, &znh
->gate
.ipv4
, tmp
, sizeof(tmp
));
1717 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
1719 case NEXTHOP_TYPE_IPV6
:
1720 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1721 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, tmp
, sizeof(tmp
));
1722 snprintf(buf
, bufsize
, "%s if %u", tmp
, znh
->ifindex
);
1724 case NEXTHOP_TYPE_BLACKHOLE
:
1725 snprintf(buf
, bufsize
, "blackhole");
1728 snprintf(buf
, bufsize
, "unknown");
1736 * Decode the nexthop-tracking update message
1738 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1742 memset(nhr
, 0, sizeof(*nhr
));
1744 STREAM_GETL(s
, nhr
->message
);
1745 STREAM_GETW(s
, nhr
->prefix
.family
);
1746 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1747 switch (nhr
->prefix
.family
) {
1749 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1752 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1757 if (CHECK_FLAG(nhr
->message
, ZAPI_MESSAGE_SRTE
))
1758 STREAM_GETL(s
, nhr
->srte_color
);
1760 STREAM_GETC(s
, nhr
->type
);
1761 STREAM_GETW(s
, nhr
->instance
);
1762 STREAM_GETC(s
, nhr
->distance
);
1763 STREAM_GETL(s
, nhr
->metric
);
1764 STREAM_GETC(s
, nhr
->nexthop_num
);
1766 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1767 if (zapi_nexthop_decode(s
, &(nhr
->nexthops
[i
]), 0, 0) != 0)
1776 bool zapi_error_decode(struct stream
*s
, enum zebra_error_types
*error
)
1778 memset(error
, 0, sizeof(*error
));
1780 STREAM_GET(error
, s
, sizeof(*error
));
1783 zlog_debug("%s: type: %s", __func__
,
1784 zebra_error_type2str(*error
));
1792 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1793 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1794 * then set/unset redist[type] in the client handle (a struct zserv) for the
1797 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1798 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1805 zclient_create_header(s
, command
, vrf_id
);
1806 stream_putc(s
, afi
);
1807 stream_putc(s
, type
);
1808 stream_putw(s
, instance
);
1810 stream_putw_at(s
, 0, stream_get_endp(s
));
1812 return zclient_send_message(zclient
);
1815 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1816 afi_t afi
, vrf_id_t vrf_id
)
1823 zclient_create_header(s
, command
, vrf_id
);
1824 stream_putc(s
, afi
);
1826 stream_putw_at(s
, 0, stream_get_endp(s
));
1828 return zclient_send_message(zclient
);
1831 /* Get prefix in ZServ format; family should be filled in on prefix */
1832 static int zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1834 size_t plen
= prefix_blen(p
);
1841 STREAM_GET(&p
->u
.prefix
, s
, plen
);
1843 p
->prefixlen
= MIN(plen
* 8, c
);
1850 /* Router-id update from zebra daemon. */
1851 int zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1853 /* Fetch interface address. */
1854 STREAM_GETC(s
, rid
->family
);
1856 return zclient_stream_get_prefix(s
, rid
);
1862 /* Interface addition from zebra daemon. */
1864 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1865 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1867 * 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
1868 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1874 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1876 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1878 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1881 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1883 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1885 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1887 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1889 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1891 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1892 * | parent ifindex |
1893 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1894 * | Link Layer Type |
1895 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1896 * | Harware Address Length |
1897 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1898 * | Hardware Address if HW lenght different from 0 |
1899 * | ... max INTERFACE_HWADDR_MAX |
1900 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1901 * | Link_params? | Whether a link-params follows: 1 or 0.
1902 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1903 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1904 * | .... (struct if_link_params). |
1905 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1908 static int zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1911 char vrfname_tmp
[VRF_NAMSIZ
+ 1] = {};
1912 struct vrf_data data
;
1914 STREAM_GET(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1915 /* Read interface name. */
1916 STREAM_GET(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1918 if (strlen(vrfname_tmp
) == 0)
1919 goto stream_failure
;
1921 /* Lookup/create vrf by name, then vrf_id. */
1922 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1924 /* If there's already a VRF with this name, don't create vrf */
1928 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1929 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1930 /* overwrite default vrf */
1931 if (vrf_id
== VRF_DEFAULT
)
1932 vrf_set_default_name(vrfname_tmp
, false);
1940 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1944 /* Lookup vrf by vrf_id. */
1945 vrf
= vrf_lookup_by_id(vrf_id
);
1948 * If a routing protocol doesn't know about a
1949 * vrf that is about to be deleted. There is
1950 * no point in attempting to delete it.
1958 static int zclient_interface_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1960 struct interface
*ifp
;
1961 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
1962 struct stream
*s
= zclient
->ibuf
;
1964 /* Read interface name. */
1965 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1967 /* Lookup/create interface by name. */
1968 if (!vrf_get(vrf_id
, NULL
)) {
1970 "Rx'd interface add from Zebra, but VRF %u does not exist",
1975 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1977 zebra_interface_if_set_value(s
, ifp
);
1979 if_new_via_zapi(ifp
);
1987 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1988 * from zebra server. The format of this message is the same as
1989 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
1990 * except that no sockaddr_dl is sent at the tail of the message.
1992 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1994 struct interface
*ifp
;
1995 char ifname_tmp
[INTERFACE_NAMSIZ
+ 1] = {};
1997 /* Read interface name. */
1998 STREAM_GET(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
2000 /* Lookup this by interface index. */
2001 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
2003 flog_err(EC_LIB_ZAPI_ENCODE
,
2004 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
2005 ifname_tmp
, vrf_id
);
2009 zebra_interface_if_set_value(s
, ifp
);
2016 static void zclient_interface_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
2018 struct interface
*ifp
;
2019 struct stream
*s
= zclient
->ibuf
;
2021 ifp
= zebra_interface_state_read(s
, vrf_id
);
2026 if_destroy_via_zapi(ifp
);
2030 static void zclient_interface_up(struct zclient
*zclient
, vrf_id_t vrf_id
)
2032 struct interface
*ifp
;
2033 struct stream
*s
= zclient
->ibuf
;
2035 ifp
= zebra_interface_state_read(s
, vrf_id
);
2040 if_up_via_zapi(ifp
);
2043 static void zclient_interface_down(struct zclient
*zclient
, vrf_id_t vrf_id
)
2045 struct interface
*ifp
;
2046 struct stream
*s
= zclient
->ibuf
;
2048 ifp
= zebra_interface_state_read(s
, vrf_id
);
2053 if_down_via_zapi(ifp
);
2056 static void zclient_handle_error(ZAPI_CALLBACK_ARGS
)
2058 enum zebra_error_types error
;
2059 struct stream
*s
= zclient
->ibuf
;
2061 zapi_error_decode(s
, &error
);
2063 if (zclient
->handle_error
)
2064 (*zclient
->handle_error
)(error
);
2067 static int link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
2073 uint32_t bwclassnum
;
2075 STREAM_GETL(s
, iflp
->lp_status
);
2076 STREAM_GETL(s
, iflp
->te_metric
);
2077 STREAM_GETF(s
, iflp
->max_bw
);
2078 STREAM_GETF(s
, iflp
->max_rsv_bw
);
2079 STREAM_GETL(s
, bwclassnum
);
2082 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
2083 STREAM_GETF(s
, iflp
->unrsv_bw
[i
]);
2086 EC_LIB_ZAPI_MISSMATCH
,
2087 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries - outdated library?",
2088 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
2090 STREAM_GETL(s
, iflp
->admin_grp
);
2091 STREAM_GETL(s
, iflp
->rmt_as
);
2092 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
2094 STREAM_GETL(s
, iflp
->av_delay
);
2095 STREAM_GETL(s
, iflp
->min_delay
);
2096 STREAM_GETL(s
, iflp
->max_delay
);
2097 STREAM_GETL(s
, iflp
->delay_var
);
2099 STREAM_GETF(s
, iflp
->pkt_loss
);
2100 STREAM_GETF(s
, iflp
->res_bw
);
2101 STREAM_GETF(s
, iflp
->ava_bw
);
2102 STREAM_GETF(s
, iflp
->use_bw
);
2109 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
2112 struct if_link_params
*iflp
;
2115 STREAM_GETL(s
, ifindex
);
2117 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2120 flog_err(EC_LIB_ZAPI_ENCODE
,
2121 "%s: unknown ifindex %u, shouldn't happen", __func__
,
2126 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
2129 if (link_params_set_value(s
, iflp
) != 0)
2130 goto stream_failure
;
2138 static void zebra_interface_if_set_value(struct stream
*s
,
2139 struct interface
*ifp
)
2141 uint8_t link_params_status
= 0;
2142 ifindex_t old_ifindex
, new_ifindex
;
2144 old_ifindex
= ifp
->oldifindex
;
2145 /* Read interface's index. */
2146 STREAM_GETL(s
, new_ifindex
);
2147 if_set_index(ifp
, new_ifindex
);
2148 STREAM_GETC(s
, ifp
->status
);
2150 /* Read interface's value. */
2151 STREAM_GETQ(s
, ifp
->flags
);
2152 STREAM_GETC(s
, ifp
->ptm_enable
);
2153 STREAM_GETC(s
, ifp
->ptm_status
);
2154 STREAM_GETL(s
, ifp
->metric
);
2155 STREAM_GETL(s
, ifp
->speed
);
2156 STREAM_GETL(s
, ifp
->mtu
);
2157 STREAM_GETL(s
, ifp
->mtu6
);
2158 STREAM_GETL(s
, ifp
->bandwidth
);
2159 STREAM_GETL(s
, ifp
->link_ifindex
);
2160 STREAM_GETL(s
, ifp
->ll_type
);
2161 STREAM_GETL(s
, ifp
->hw_addr_len
);
2162 if (ifp
->hw_addr_len
)
2163 STREAM_GET(ifp
->hw_addr
, s
,
2164 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
2166 /* Read Traffic Engineering status */
2167 link_params_status
= stream_getc(s
);
2168 /* Then, Traffic Engineering parameters if any */
2169 if (link_params_status
) {
2170 struct if_link_params
*iflp
= if_link_params_get(ifp
);
2171 link_params_set_value(s
, iflp
);
2174 nexthop_group_interface_state_change(ifp
, old_ifindex
);
2178 zlog_err("Could not parse interface values; aborting");
2179 assert(!"Failed to parse interface values");
2182 size_t zebra_interface_link_params_write(struct stream
*s
,
2183 struct interface
*ifp
)
2186 struct if_link_params
*iflp
;
2189 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
2192 iflp
= ifp
->link_params
;
2195 w
+= stream_putl(s
, iflp
->lp_status
);
2197 w
+= stream_putl(s
, iflp
->te_metric
);
2198 w
+= stream_putf(s
, iflp
->max_bw
);
2199 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
2201 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
2202 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
2203 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
2205 w
+= stream_putl(s
, iflp
->admin_grp
);
2206 w
+= stream_putl(s
, iflp
->rmt_as
);
2207 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
2209 w
+= stream_putl(s
, iflp
->av_delay
);
2210 w
+= stream_putl(s
, iflp
->min_delay
);
2211 w
+= stream_putl(s
, iflp
->max_delay
);
2212 w
+= stream_putl(s
, iflp
->delay_var
);
2214 w
+= stream_putf(s
, iflp
->pkt_loss
);
2215 w
+= stream_putf(s
, iflp
->res_bw
);
2216 w
+= stream_putf(s
, iflp
->ava_bw
);
2217 w
+= stream_putf(s
, iflp
->use_bw
);
2223 * format of message for address additon is:
2227 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
2228 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
2237 * | ifc_flags | flags for connected address
2245 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
2253 static int memconstant(const void *s
, int c
, size_t n
)
2255 const uint8_t *p
= s
;
2264 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
2268 struct interface
*ifp
;
2269 struct connected
*ifc
;
2270 struct prefix p
, d
, *dp
;
2274 memset(&p
, 0, sizeof(p
));
2275 memset(&d
, 0, sizeof(d
));
2277 /* Get interface index. */
2278 STREAM_GETL(s
, ifindex
);
2281 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2283 flog_err(EC_LIB_ZAPI_ENCODE
,
2284 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
2285 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
2291 STREAM_GETC(s
, ifc_flags
);
2293 /* Fetch interface address. */
2294 STREAM_GETC(s
, d
.family
);
2295 p
.family
= d
.family
;
2296 plen
= prefix_blen(&d
);
2298 if (zclient_stream_get_prefix(s
, &p
) != 0)
2299 goto stream_failure
;
2301 /* Fetch destination address. */
2302 STREAM_GET(&d
.u
.prefix
, s
, plen
);
2304 /* N.B. NULL destination pointers are encoded as all zeroes */
2305 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
2307 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
2308 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
2310 /* N.B. NULL destination pointers are encoded as all
2312 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
2315 ifc
->flags
= ifc_flags
;
2316 if (ifc
->destination
)
2317 ifc
->destination
->prefixlen
=
2318 ifc
->address
->prefixlen
;
2319 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
2320 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
2322 char buf
[PREFIX_STRLEN
];
2325 "warning: interface %s address %s with peer flag set, but no peer address!",
2327 prefix2str(ifc
->address
, buf
,
2329 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
2333 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
2334 ifc
= connected_delete_by_prefix(ifp
, &p
);
2344 * format of message for neighbor connected address is:
2348 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
2349 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2364 * | addr_len | len of addr.
2367 struct nbr_connected
*
2368 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
2370 unsigned int ifindex
;
2371 struct interface
*ifp
;
2373 struct nbr_connected
*ifc
;
2375 /* Get interface index. */
2376 STREAM_GETL(s
, ifindex
);
2379 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2381 flog_err(EC_LIB_ZAPI_ENCODE
,
2382 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2383 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
2389 STREAM_GETC(s
, p
.family
);
2390 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
2391 STREAM_GETC(s
, p
.prefixlen
);
2393 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
2394 /* Currently only supporting P2P links, so any new RA source
2396 considered as the replacement of the previously learnt
2397 Link-Local address. */
2398 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
2399 ifc
= nbr_connected_new();
2400 ifc
->address
= prefix_new();
2402 listnode_add(ifp
->nbr_connected
, ifc
);
2405 prefix_copy(ifc
->address
, &p
);
2407 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
2409 ifc
= nbr_connected_check(ifp
, &p
);
2411 listnode_delete(ifp
->nbr_connected
, ifc
);
2420 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
2422 vrf_id_t
*new_vrf_id
)
2424 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2425 struct interface
*ifp
;
2428 /* Read interface name. */
2429 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
2431 /* Lookup interface. */
2432 ifp
= if_lookup_by_name(ifname
, vrf_id
);
2434 flog_err(EC_LIB_ZAPI_ENCODE
,
2435 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2440 /* Fetch new VRF Id. */
2441 STREAM_GETL(s
, new_id
);
2443 *new_vrf_id
= new_id
;
2450 /* filter unwanted messages until the expected one arrives */
2451 static int zclient_read_sync_response(struct zclient
*zclient
,
2452 uint16_t expected_cmd
)
2464 cmd
= expected_cmd
+ 1;
2465 while (ret
== 0 && cmd
!= expected_cmd
) {
2469 /* wait until response arrives */
2471 FD_SET(zclient
->sock
, &readfds
);
2472 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2473 if (!FD_ISSET(zclient
->sock
, &readfds
))
2476 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2477 &version
, &vrf_id
, &cmd
);
2479 zlog_debug("%s: Response (%d bytes) received", __func__
,
2483 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2491 * Connect to label manager in a syncronous way
2493 * It first writes the request to zcient output buffer and then
2494 * immediately reads the answer from the input buffer.
2496 * @param zclient Zclient used to connect to label manager (zebra)
2497 * @param async Synchronous (0) or asynchronous (1) operation
2498 * @result Result of response
2500 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2505 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2506 ZEBRA_LABEL_MANAGER_CONNECT
;
2509 zlog_debug("Connecting to Label Manager (LM)");
2511 if (zclient
->sock
< 0) {
2512 zlog_debug("%s: invalid zclient socket", __func__
);
2519 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2522 stream_putc(s
, zclient
->redist_default
);
2524 stream_putw(s
, zclient
->instance
);
2526 /* Put length at the first point of the stream. */
2527 stream_putw_at(s
, 0, stream_get_endp(s
));
2529 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2531 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2532 close(zclient
->sock
);
2537 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2538 close(zclient
->sock
);
2543 zlog_debug("LM connect request sent (%d bytes)", ret
);
2549 if (zclient_read_sync_response(zclient
, cmd
)
2555 /* read instance and proto */
2559 STREAM_GETC(s
, proto
);
2560 STREAM_GETW(s
, instance
);
2563 if (proto
!= zclient
->redist_default
)
2566 "Wrong proto (%u) in LM connect response. Should be %u",
2567 proto
, zclient
->redist_default
);
2568 if (instance
!= zclient
->instance
)
2571 "Wrong instId (%u) in LM connect response. Should be %u",
2572 instance
, zclient
->instance
);
2575 STREAM_GETC(s
, result
);
2577 zlog_debug("LM connect-response received, result %u", result
);
2586 * Asynchronous label chunk request
2588 * @param zclient Zclient used to connect to label manager (zebra)
2589 * @param keep Avoid garbage collection
2590 * @param chunk_size Amount of labels requested
2591 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2592 * @result 0 on success, -1 otherwise
2594 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2595 uint32_t chunk_size
, uint32_t base
)
2600 zlog_debug("Getting Label Chunk");
2602 if (zclient
->sock
< 0)
2608 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2610 stream_putc(s
, zclient
->redist_default
);
2612 stream_putw(s
, zclient
->instance
);
2613 stream_putc(s
, keep
);
2614 stream_putl(s
, chunk_size
);
2615 stream_putl(s
, base
);
2617 /* Put length at the first point of the stream. */
2618 stream_putw_at(s
, 0, stream_get_endp(s
));
2620 return zclient_send_message(zclient
);
2624 * Function to request a label chunk in a syncronous way
2626 * It first writes the request to zlcient output buffer and then
2627 * immediately reads the answer from the input buffer.
2629 * @param zclient Zclient used to connect to label manager (zebra)
2630 * @param keep Avoid garbage collection
2631 * @param chunk_size Amount of labels requested
2632 * @param start To write first assigned chunk label to
2633 * @param end To write last assigned chunk label to
2634 * @result 0 on success, -1 otherwise
2636 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2637 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2641 uint8_t response_keep
;
2644 zlog_debug("Getting Label Chunk");
2646 if (zclient
->sock
< 0)
2652 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2654 stream_putc(s
, zclient
->redist_default
);
2656 stream_putw(s
, zclient
->instance
);
2658 stream_putc(s
, keep
);
2660 stream_putl(s
, chunk_size
);
2661 /* requested chunk base */
2662 stream_putl(s
, base
);
2663 /* Put length at the first point of the stream. */
2664 stream_putw_at(s
, 0, stream_get_endp(s
));
2666 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2668 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2669 close(zclient
->sock
);
2674 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2675 close(zclient
->sock
);
2680 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2683 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2686 /* parse response */
2689 /* read proto and instance */
2693 STREAM_GETC(s
, proto
);
2694 STREAM_GETW(s
, instance
);
2697 if (proto
!= zclient
->redist_default
)
2698 flog_err(EC_LIB_ZAPI_ENCODE
,
2699 "Wrong proto (%u) in get chunk response. Should be %u",
2700 proto
, zclient
->redist_default
);
2701 if (instance
!= zclient
->instance
)
2702 flog_err(EC_LIB_ZAPI_ENCODE
,
2703 "Wrong instId (%u) in get chunk response Should be %u",
2704 instance
, zclient
->instance
);
2706 /* if we requested a specific chunk and it could not be allocated, the
2707 * response message will end here
2709 if (!STREAM_READABLE(s
)) {
2710 zlog_info("Unable to assign Label Chunk to %s instance %u",
2711 zebra_route_string(proto
), instance
);
2716 STREAM_GETC(s
, response_keep
);
2717 /* start and end labels */
2718 STREAM_GETL(s
, *start
);
2719 STREAM_GETL(s
, *end
);
2721 /* not owning this response */
2722 if (keep
!= response_keep
) {
2725 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2726 *start
, *end
, keep
, response_keep
);
2729 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2730 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2731 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2737 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2747 * Function to release a label chunk
2749 * @param zclient Zclient used to connect to label manager (zebra)
2750 * @param start First label of chunk
2751 * @param end Last label of chunk
2752 * @result 0 on success, -1 otherwise
2754 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2761 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2763 if (zclient
->sock
< 0)
2769 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2772 stream_putc(s
, zclient
->redist_default
);
2774 stream_putw(s
, zclient
->instance
);
2776 stream_putl(s
, start
);
2778 stream_putl(s
, end
);
2780 /* Put length at the first point of the stream. */
2781 stream_putw_at(s
, 0, stream_get_endp(s
));
2783 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2785 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2786 close(zclient
->sock
);
2791 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2792 close(zclient
->sock
);
2801 * Connect to table manager in a syncronous way
2803 * It first writes the request to zcient output buffer and then
2804 * immediately reads the answer from the input buffer.
2806 * @param zclient Zclient used to connect to table manager (zebra)
2807 * @result Result of response
2809 int tm_table_manager_connect(struct zclient
*zclient
)
2816 zlog_debug("Connecting to Table Manager");
2818 if (zclient
->sock
< 0)
2824 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2827 stream_putc(s
, zclient
->redist_default
);
2829 stream_putw(s
, zclient
->instance
);
2831 /* Put length at the first point of the stream. */
2832 stream_putw_at(s
, 0, stream_get_endp(s
));
2834 ret
= zclient_send_message(zclient
);
2839 zlog_debug("%s: Table manager connect request sent", __func__
);
2842 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2848 STREAM_GETC(s
, result
);
2851 "%s: Table Manager connect response received, result %u",
2860 * Function to request a table chunk in a syncronous way
2862 * It first writes the request to zclient output buffer and then
2863 * immediately reads the answer from the input buffer.
2865 * @param zclient Zclient used to connect to table manager (zebra)
2866 * @param chunk_size Amount of table requested
2867 * @param start to write first assigned chunk table RT ID to
2868 * @param end To write last assigned chunk table RT ID to
2869 * @result 0 on success, -1 otherwise
2871 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2872 uint32_t *start
, uint32_t *end
)
2878 zlog_debug("Getting Table Chunk");
2880 if (zclient
->sock
< 0)
2886 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2888 stream_putl(s
, chunk_size
);
2889 /* Put length at the first point of the stream. */
2890 stream_putw_at(s
, 0, stream_get_endp(s
));
2892 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2894 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2896 close(zclient
->sock
);
2901 flog_err(EC_LIB_ZAPI_SOCKET
,
2902 "%s: zclient->sock connection closed", __func__
);
2903 close(zclient
->sock
);
2908 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2912 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2916 /* start and end table IDs */
2917 STREAM_GETL(s
, *start
);
2918 STREAM_GETL(s
, *end
);
2921 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2929 * Function to release a table chunk
2931 * @param zclient Zclient used to connect to table manager (zebra)
2932 * @param start First label of table
2933 * @param end Last label of chunk
2934 * @result 0 on success, -1 otherwise
2936 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2942 zlog_debug("Releasing Table Chunk");
2944 if (zclient
->sock
< 0)
2950 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2953 stream_putl(s
, start
);
2955 stream_putl(s
, end
);
2957 /* Put length at the first point of the stream. */
2958 stream_putw_at(s
, 0, stream_get_endp(s
));
2960 return zclient_send_message(zclient
);
2963 int zebra_send_sr_policy(struct zclient
*zclient
, int cmd
,
2964 struct zapi_sr_policy
*zp
)
2966 if (zapi_sr_policy_encode(zclient
->obuf
, cmd
, zp
) < 0)
2968 return zclient_send_message(zclient
);
2971 int zapi_sr_policy_encode(struct stream
*s
, int cmd
, struct zapi_sr_policy
*zp
)
2973 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
2977 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2978 stream_putl(s
, zp
->color
);
2979 stream_put_ipaddr(s
, &zp
->endpoint
);
2980 stream_write(s
, &zp
->name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2982 stream_putc(s
, zt
->type
);
2983 stream_putl(s
, zt
->local_label
);
2985 if (zt
->label_num
> MPLS_MAX_LABELS
) {
2986 flog_err(EC_LIB_ZAPI_ENCODE
,
2987 "%s: label %u: can't encode %u labels (maximum is %u)",
2988 __func__
, zt
->local_label
, zt
->label_num
,
2992 stream_putw(s
, zt
->label_num
);
2994 for (int i
= 0; i
< zt
->label_num
; i
++)
2995 stream_putl(s
, zt
->labels
[i
]);
2997 /* Put length at the first point of the stream. */
2998 stream_putw_at(s
, 0, stream_get_endp(s
));
3003 int zapi_sr_policy_decode(struct stream
*s
, struct zapi_sr_policy
*zp
)
3005 memset(zp
, 0, sizeof(*zp
));
3007 struct zapi_srte_tunnel
*zt
= &zp
->segment_list
;
3009 STREAM_GETL(s
, zp
->color
);
3010 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3011 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3013 /* segment list of active candidate path */
3014 STREAM_GETC(s
, zt
->type
);
3015 STREAM_GETL(s
, zt
->local_label
);
3016 STREAM_GETW(s
, zt
->label_num
);
3017 if (zt
->label_num
> MPLS_MAX_LABELS
) {
3018 flog_err(EC_LIB_ZAPI_ENCODE
,
3019 "%s: label %u: can't decode %u labels (maximum is %u)",
3020 __func__
, zt
->local_label
, zt
->label_num
,
3024 for (int i
= 0; i
< zt
->label_num
; i
++)
3025 STREAM_GETL(s
, zt
->labels
[i
]);
3033 int zapi_sr_policy_notify_status_decode(struct stream
*s
,
3034 struct zapi_sr_policy
*zp
)
3036 memset(zp
, 0, sizeof(*zp
));
3038 STREAM_GETL(s
, zp
->color
);
3039 STREAM_GET_IPADDR(s
, &zp
->endpoint
);
3040 STREAM_GET(&zp
->name
, s
, SRTE_POLICY_NAME_MAX_LENGTH
);
3041 STREAM_GETL(s
, zp
->status
);
3049 int zebra_send_mpls_labels(struct zclient
*zclient
, int cmd
,
3050 struct zapi_labels
*zl
)
3052 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
3054 return zclient_send_message(zclient
);
3057 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
3059 struct zapi_nexthop
*znh
;
3063 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
3064 stream_putc(s
, zl
->message
);
3065 stream_putc(s
, zl
->type
);
3066 stream_putl(s
, zl
->local_label
);
3068 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3069 stream_putw(s
, zl
->route
.prefix
.family
);
3070 stream_put_prefix(s
, &zl
->route
.prefix
);
3071 stream_putc(s
, zl
->route
.type
);
3072 stream_putw(s
, zl
->route
.instance
);
3075 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3078 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3079 __func__
, zl
->local_label
, zl
->nexthop_num
,
3083 stream_putw(s
, zl
->nexthop_num
);
3085 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3086 znh
= &zl
->nexthops
[i
];
3088 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3092 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3094 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3097 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3098 __func__
, zl
->local_label
, zl
->nexthop_num
,
3102 stream_putw(s
, zl
->backup_nexthop_num
);
3104 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3105 znh
= &zl
->backup_nexthops
[i
];
3107 if (zapi_nexthop_encode(s
, znh
, 0, 0) < 0)
3113 /* Put length at the first point of the stream. */
3114 stream_putw_at(s
, 0, stream_get_endp(s
));
3119 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
3121 struct zapi_nexthop
*znh
;
3123 memset(zl
, 0, sizeof(*zl
));
3126 STREAM_GETC(s
, zl
->message
);
3127 STREAM_GETC(s
, zl
->type
);
3128 STREAM_GETL(s
, zl
->local_label
);
3130 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
3133 STREAM_GETW(s
, zl
->route
.prefix
.family
);
3134 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
3136 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
3137 switch (zl
->route
.prefix
.family
) {
3139 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
3141 "%s: Specified prefix length %d is greater than a v4 address can support",
3142 __func__
, zl
->route
.prefix
.prefixlen
);
3145 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
3149 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
3151 "%s: Specified prefix length %d is greater than a v6 address can support",
3152 __func__
, zl
->route
.prefix
.prefixlen
);
3155 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
3158 flog_err(EC_LIB_ZAPI_ENCODE
,
3159 "%s: Specified family %u is not v4 or v6",
3160 __func__
, zl
->route
.prefix
.family
);
3164 STREAM_GETC(s
, zl
->route
.type
);
3165 STREAM_GETW(s
, zl
->route
.instance
);
3168 STREAM_GETW(s
, zl
->nexthop_num
);
3170 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
3173 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
3174 __func__
, &zl
->route
.prefix
, zl
->nexthop_num
,
3178 zl
->nexthop_num
= MIN(MULTIPATH_NUM
, zl
->nexthop_num
);
3180 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
3181 znh
= &zl
->nexthops
[i
];
3183 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3187 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
)) {
3188 STREAM_GETW(s
, zl
->backup_nexthop_num
);
3190 if (zl
->backup_nexthop_num
> MULTIPATH_NUM
) {
3193 "%s: Prefix %pFX has %d backup nexthops, but we can only use the first %d",
3194 __func__
, &zl
->route
.prefix
,
3195 zl
->backup_nexthop_num
, MULTIPATH_NUM
);
3198 zl
->backup_nexthop_num
= MIN(MULTIPATH_NUM
,
3199 zl
->backup_nexthop_num
);
3201 for (int i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3202 znh
= &zl
->backup_nexthops
[i
];
3204 if (zapi_nexthop_decode(s
, znh
, 0, 0) < 0)
3214 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
3222 zclient_create_header(s
, command
, VRF_DEFAULT
);
3223 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
3224 stream_putl(s
, pw
->ifindex
);
3227 stream_putl(s
, pw
->type
);
3230 stream_putl(s
, pw
->af
);
3233 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
3236 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
3239 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
3244 stream_putl(s
, pw
->local_label
);
3245 stream_putl(s
, pw
->remote_label
);
3248 stream_putc(s
, pw
->flags
);
3250 /* Protocol specific fields */
3251 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
3253 /* Put length at the first point of the stream. */
3254 stream_putw_at(s
, 0, stream_get_endp(s
));
3256 return zclient_send_message(zclient
);
3260 * Receive PW status update from Zebra and send it to LDE process.
3262 int zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
3266 memset(pw
, 0, sizeof(struct zapi_pw_status
));
3270 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
3271 STREAM_GETL(s
, pw
->ifindex
);
3272 STREAM_GETL(s
, pw
->status
);
3279 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
3281 struct zclient_capabilities cap
;
3282 struct stream
*s
= zclient
->ibuf
;
3284 uint8_t mpls_enabled
;
3286 STREAM_GETL(s
, vrf_backend
);
3288 if (vrf_backend
< 0 || vrf_configure_backend(vrf_backend
)) {
3289 flog_err(EC_LIB_ZAPI_ENCODE
,
3290 "%s: Garbage VRF backend type: %d\n", __func__
,
3292 goto stream_failure
;
3296 memset(&cap
, 0, sizeof(cap
));
3297 STREAM_GETC(s
, mpls_enabled
);
3298 cap
.mpls_enabled
= !!mpls_enabled
;
3299 STREAM_GETL(s
, cap
.ecmp
);
3300 STREAM_GETC(s
, cap
.role
);
3302 if (zclient
->zebra_capabilities
)
3303 (*zclient
->zebra_capabilities
)(&cap
);
3309 void zclient_send_mlag_register(struct zclient
*client
, uint32_t bit_map
)
3316 zclient_create_header(s
, ZEBRA_MLAG_CLIENT_REGISTER
, VRF_DEFAULT
);
3317 stream_putl(s
, bit_map
);
3319 stream_putw_at(s
, 0, stream_get_endp(s
));
3320 zclient_send_message(client
);
3323 void zclient_send_mlag_deregister(struct zclient
*client
)
3325 zebra_message_send(client
, ZEBRA_MLAG_CLIENT_UNREGISTER
, VRF_DEFAULT
);
3328 void zclient_send_mlag_data(struct zclient
*client
, struct stream
*client_s
)
3335 zclient_create_header(s
, ZEBRA_MLAG_FORWARD_MSG
, VRF_DEFAULT
);
3336 stream_put(s
, client_s
->data
, client_s
->endp
);
3338 stream_putw_at(s
, 0, stream_get_endp(s
));
3339 zclient_send_message(client
);
3342 static void zclient_mlag_process_up(ZAPI_CALLBACK_ARGS
)
3344 if (zclient
->mlag_process_up
)
3345 (*zclient
->mlag_process_up
)();
3348 static void zclient_mlag_process_down(ZAPI_CALLBACK_ARGS
)
3350 if (zclient
->mlag_process_down
)
3351 (*zclient
->mlag_process_down
)();
3354 static void zclient_mlag_handle_msg(ZAPI_CALLBACK_ARGS
)
3356 if (zclient
->mlag_handle_msg
)
3357 (*zclient
->mlag_handle_msg
)(zclient
->ibuf
, length
);
3361 * Send an OPAQUE message, contents opaque to zebra. The message header
3362 * is a message subtype.
3364 int zclient_send_opaque(struct zclient
*zclient
, uint32_t type
,
3365 const uint8_t *data
, size_t datasize
)
3371 /* Check buffer size */
3372 if (STREAM_SIZE(zclient
->obuf
) <
3373 (ZEBRA_HEADER_SIZE
+ sizeof(type
) + datasize
))
3379 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3381 /* Send sub-type and flags */
3382 stream_putl(s
, type
);
3383 stream_putw(s
, flags
);
3385 /* Send opaque data */
3386 stream_write(s
, data
, datasize
);
3388 /* Put length into the header at the start of the stream. */
3389 stream_putw_at(s
, 0, stream_get_endp(s
));
3391 ret
= zclient_send_message(zclient
);
3397 * Send an OPAQUE message to a specific zclient. The contents are opaque
3400 int zclient_send_opaque_unicast(struct zclient
*zclient
, uint32_t type
,
3401 uint8_t proto
, uint16_t instance
,
3402 uint32_t session_id
, const uint8_t *data
,
3409 /* Check buffer size */
3410 if (STREAM_SIZE(zclient
->obuf
) <
3411 (ZEBRA_HEADER_SIZE
+ sizeof(struct zapi_opaque_msg
) + datasize
))
3417 zclient_create_header(s
, ZEBRA_OPAQUE_MESSAGE
, VRF_DEFAULT
);
3419 /* Send sub-type and flags */
3420 SET_FLAG(flags
, ZAPI_OPAQUE_FLAG_UNICAST
);
3421 stream_putl(s
, type
);
3422 stream_putw(s
, flags
);
3424 /* Send destination client info */
3425 stream_putc(s
, proto
);
3426 stream_putw(s
, instance
);
3427 stream_putl(s
, session_id
);
3429 /* Send opaque data */
3430 stream_write(s
, data
, datasize
);
3432 /* Put length into the header at the start of the stream. */
3433 stream_putw_at(s
, 0, stream_get_endp(s
));
3435 ret
= zclient_send_message(zclient
);
3441 * Decode incoming opaque message into info struct
3443 int zclient_opaque_decode(struct stream
*s
, struct zapi_opaque_msg
*info
)
3445 memset(info
, 0, sizeof(*info
));
3447 /* Decode subtype and flags */
3448 STREAM_GETL(s
, info
->type
);
3449 STREAM_GETW(s
, info
->flags
);
3451 /* Decode unicast client info if present */
3452 if (CHECK_FLAG(info
->flags
, ZAPI_OPAQUE_FLAG_UNICAST
)) {
3453 STREAM_GETC(s
, info
->proto
);
3454 STREAM_GETW(s
, info
->instance
);
3455 STREAM_GETL(s
, info
->session_id
);
3458 info
->len
= STREAM_READABLE(s
);
3468 * Send a registration request for opaque messages with a specified subtype.
3470 int zclient_register_opaque(struct zclient
*zclient
, uint32_t type
)
3478 zclient_create_header(s
, ZEBRA_OPAQUE_REGISTER
, VRF_DEFAULT
);
3481 stream_putl(s
, type
);
3483 /* Add zclient info */
3484 stream_putc(s
, zclient
->redist_default
);
3485 stream_putw(s
, zclient
->instance
);
3486 stream_putl(s
, zclient
->session_id
);
3488 /* Put length at the first point of the stream. */
3489 stream_putw_at(s
, 0, stream_get_endp(s
));
3491 ret
= zclient_send_message(zclient
);
3497 * Send an un-registration request for a specified opaque subtype.
3499 int zclient_unregister_opaque(struct zclient
*zclient
, uint32_t type
)
3507 zclient_create_header(s
, ZEBRA_OPAQUE_UNREGISTER
, VRF_DEFAULT
);
3510 stream_putl(s
, type
);
3512 /* Add zclient info */
3513 stream_putc(s
, zclient
->redist_default
);
3514 stream_putw(s
, zclient
->instance
);
3515 stream_putl(s
, zclient
->session_id
);
3517 /* Put length at the first point of the stream. */
3518 stream_putw_at(s
, 0, stream_get_endp(s
));
3520 ret
= zclient_send_message(zclient
);
3525 /* Utility to decode opaque registration info */
3526 int zapi_opaque_reg_decode(struct stream
*s
, struct zapi_opaque_reg_info
*info
)
3528 STREAM_GETL(s
, info
->type
);
3529 STREAM_GETC(s
, info
->proto
);
3530 STREAM_GETW(s
, info
->instance
);
3531 STREAM_GETL(s
, info
->session_id
);
3540 /* Zebra client message read function. */
3541 static int zclient_read(struct thread
*thread
)
3544 uint16_t length
, command
;
3545 uint8_t marker
, version
;
3547 struct zclient
*zclient
;
3549 /* Get socket to zebra. */
3550 zclient
= THREAD_ARG(thread
);
3551 zclient
->t_read
= NULL
;
3553 /* Read zebra header (if we don't have it already). */
3554 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
3556 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
3557 ZEBRA_HEADER_SIZE
- already
))
3562 "zclient connection closed socket [%d].",
3564 return zclient_failed(zclient
);
3566 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
3567 /* Try again later. */
3568 zclient_event(ZCLIENT_READ
, zclient
);
3571 already
= ZEBRA_HEADER_SIZE
;
3574 /* Reset to read from the beginning of the incoming packet. */
3575 stream_set_getp(zclient
->ibuf
, 0);
3577 /* Fetch header values. */
3578 length
= stream_getw(zclient
->ibuf
);
3579 marker
= stream_getc(zclient
->ibuf
);
3580 version
= stream_getc(zclient
->ibuf
);
3581 vrf_id
= stream_getl(zclient
->ibuf
);
3582 command
= stream_getw(zclient
->ibuf
);
3584 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
3586 EC_LIB_ZAPI_MISSMATCH
,
3587 "%s: socket %d version mismatch, marker %d, version %d",
3588 __func__
, zclient
->sock
, marker
, version
);
3589 return zclient_failed(zclient
);
3592 if (length
< ZEBRA_HEADER_SIZE
) {
3593 flog_err(EC_LIB_ZAPI_MISSMATCH
,
3594 "%s: socket %d message length %u is less than %d ",
3595 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
3596 return zclient_failed(zclient
);
3600 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
3604 "%s: message size %u exceeds buffer size %lu, expanding...",
3606 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
3607 ns
= stream_new(length
);
3608 stream_copy(ns
, zclient
->ibuf
);
3609 stream_free(zclient
->ibuf
);
3613 /* Read rest of zebra packet. */
3614 if (already
< length
) {
3616 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
3622 "zclient connection closed socket [%d].",
3624 return zclient_failed(zclient
);
3626 if (nbyte
!= (ssize_t
)(length
- already
)) {
3627 /* Try again later. */
3628 zclient_event(ZCLIENT_READ
, zclient
);
3633 length
-= ZEBRA_HEADER_SIZE
;
3636 zlog_debug("zclient 0x%p command %s VRF %u",
3637 (void *)zclient
, zserv_command_string(command
),
3641 case ZEBRA_CAPABILITIES
:
3642 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
3644 case ZEBRA_ROUTER_ID_UPDATE
:
3645 if (zclient
->router_id_update
)
3646 (*zclient
->router_id_update
)(command
, zclient
, length
,
3650 zclient_vrf_add(zclient
, vrf_id
);
3652 case ZEBRA_VRF_DELETE
:
3653 zclient_vrf_delete(zclient
, vrf_id
);
3655 case ZEBRA_INTERFACE_ADD
:
3656 zclient_interface_add(zclient
, vrf_id
);
3658 case ZEBRA_INTERFACE_DELETE
:
3659 zclient_interface_delete(zclient
, vrf_id
);
3661 case ZEBRA_INTERFACE_ADDRESS_ADD
:
3662 if (zclient
->interface_address_add
)
3663 (*zclient
->interface_address_add
)(command
, zclient
,
3666 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
3667 if (zclient
->interface_address_delete
)
3668 (*zclient
->interface_address_delete
)(command
, zclient
,
3671 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
3672 if (zclient
->interface_bfd_dest_update
)
3673 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
3676 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
3677 if (zclient
->interface_nbr_address_add
)
3678 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
3681 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
3682 if (zclient
->interface_nbr_address_delete
)
3683 (*zclient
->interface_nbr_address_delete
)(
3684 command
, zclient
, length
, vrf_id
);
3686 case ZEBRA_INTERFACE_UP
:
3687 zclient_interface_up(zclient
, vrf_id
);
3689 case ZEBRA_INTERFACE_DOWN
:
3690 zclient_interface_down(zclient
, vrf_id
);
3692 case ZEBRA_INTERFACE_VRF_UPDATE
:
3693 if (zclient
->interface_vrf_update
)
3694 (*zclient
->interface_vrf_update
)(command
, zclient
,
3697 case ZEBRA_NEXTHOP_UPDATE
:
3699 zlog_debug("zclient rcvd nexthop update");
3700 if (zclient
->nexthop_update
)
3701 (*zclient
->nexthop_update
)(command
, zclient
, length
,
3704 case ZEBRA_IMPORT_CHECK_UPDATE
:
3706 zlog_debug("zclient rcvd import check update");
3707 if (zclient
->import_check_update
)
3708 (*zclient
->import_check_update
)(command
, zclient
,
3711 case ZEBRA_BFD_DEST_REPLAY
:
3712 if (zclient
->bfd_dest_replay
)
3713 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
3716 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
3717 if (zclient
->redistribute_route_add
)
3718 (*zclient
->redistribute_route_add
)(command
, zclient
,
3721 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
3722 if (zclient
->redistribute_route_del
)
3723 (*zclient
->redistribute_route_del
)(command
, zclient
,
3726 case ZEBRA_INTERFACE_LINK_PARAMS
:
3727 if (zclient
->interface_link_params
)
3728 (*zclient
->interface_link_params
)(command
, zclient
,
3731 case ZEBRA_FEC_UPDATE
:
3733 zlog_debug("zclient rcvd fec update");
3734 if (zclient
->fec_update
)
3735 (*zclient
->fec_update
)(command
, zclient
, length
);
3737 case ZEBRA_LOCAL_ES_ADD
:
3738 if (zclient
->local_es_add
)
3739 (*zclient
->local_es_add
)(command
, zclient
, length
,
3742 case ZEBRA_LOCAL_ES_DEL
:
3743 if (zclient
->local_es_del
)
3744 (*zclient
->local_es_del
)(command
, zclient
, length
,
3747 case ZEBRA_LOCAL_ES_EVI_ADD
:
3748 if (zclient
->local_es_evi_add
)
3749 (*zclient
->local_es_evi_add
)(command
, zclient
, length
,
3752 case ZEBRA_LOCAL_ES_EVI_DEL
:
3753 if (zclient
->local_es_evi_del
)
3754 (*zclient
->local_es_evi_del
)(command
, zclient
, length
,
3758 if (zclient
->local_vni_add
)
3759 (*zclient
->local_vni_add
)(command
, zclient
, length
,
3763 if (zclient
->local_vni_del
)
3764 (*zclient
->local_vni_del
)(command
, zclient
, length
,
3767 case ZEBRA_L3VNI_ADD
:
3768 if (zclient
->local_l3vni_add
)
3769 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
3772 case ZEBRA_L3VNI_DEL
:
3773 if (zclient
->local_l3vni_del
)
3774 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
3777 case ZEBRA_MACIP_ADD
:
3778 if (zclient
->local_macip_add
)
3779 (*zclient
->local_macip_add
)(command
, zclient
, length
,
3782 case ZEBRA_MACIP_DEL
:
3783 if (zclient
->local_macip_del
)
3784 (*zclient
->local_macip_del
)(command
, zclient
, length
,
3787 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
3788 if (zclient
->local_ip_prefix_add
)
3789 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
3792 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
3793 if (zclient
->local_ip_prefix_del
)
3794 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
3797 case ZEBRA_PW_STATUS_UPDATE
:
3798 if (zclient
->pw_status_update
)
3799 (*zclient
->pw_status_update
)(command
, zclient
, length
,
3802 case ZEBRA_ROUTE_NOTIFY_OWNER
:
3803 if (zclient
->route_notify_owner
)
3804 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
3807 case ZEBRA_RULE_NOTIFY_OWNER
:
3808 if (zclient
->rule_notify_owner
)
3809 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
3812 case ZEBRA_NHG_NOTIFY_OWNER
:
3813 if (zclient
->nhg_notify_owner
)
3814 (*zclient
->nhg_notify_owner
)(command
, zclient
, length
,
3817 case ZEBRA_GET_LABEL_CHUNK
:
3818 if (zclient
->label_chunk
)
3819 (*zclient
->label_chunk
)(command
, zclient
, length
,
3822 case ZEBRA_IPSET_NOTIFY_OWNER
:
3823 if (zclient
->ipset_notify_owner
)
3824 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
3827 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
3828 if (zclient
->ipset_entry_notify_owner
)
3829 (*zclient
->ipset_entry_notify_owner
)(command
,
3833 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
3834 if (zclient
->iptable_notify_owner
)
3835 (*zclient
->iptable_notify_owner
)(command
,
3839 case ZEBRA_VXLAN_SG_ADD
:
3840 if (zclient
->vxlan_sg_add
)
3841 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
3844 case ZEBRA_VXLAN_SG_DEL
:
3845 if (zclient
->vxlan_sg_del
)
3846 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
3849 case ZEBRA_MLAG_PROCESS_UP
:
3850 zclient_mlag_process_up(command
, zclient
, length
, vrf_id
);
3852 case ZEBRA_MLAG_PROCESS_DOWN
:
3853 zclient_mlag_process_down(command
, zclient
, length
, vrf_id
);
3855 case ZEBRA_MLAG_FORWARD_MSG
:
3856 zclient_mlag_handle_msg(command
, zclient
, length
, vrf_id
);
3859 zclient_handle_error(command
, zclient
, length
, vrf_id
);
3861 case ZEBRA_OPAQUE_MESSAGE
:
3862 if (zclient
->opaque_msg_handler
)
3863 (*zclient
->opaque_msg_handler
)(command
, zclient
, length
,
3866 case ZEBRA_OPAQUE_REGISTER
:
3867 if (zclient
->opaque_register_handler
)
3868 (*zclient
->opaque_register_handler
)(command
, zclient
,
3871 case ZEBRA_OPAQUE_UNREGISTER
:
3872 if (zclient
->opaque_unregister_handler
)
3873 (*zclient
->opaque_unregister_handler
)(command
, zclient
,
3876 case ZEBRA_SR_POLICY_NOTIFY_STATUS
:
3877 if (zclient
->sr_policy_notify_status
)
3878 (*zclient
->sr_policy_notify_status
)(command
, zclient
,
3884 if (zclient
->sock
< 0)
3885 /* Connection was closed during packet processing. */
3888 /* Register read thread. */
3889 stream_reset(zclient
->ibuf
);
3890 zclient_event(ZCLIENT_READ
, zclient
);
3895 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
3896 int type
, unsigned short instance
, vrf_id_t vrf_id
)
3900 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3901 if (redist_check_instance(
3902 &zclient
->mi_redist
[afi
][type
], instance
))
3904 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3907 if (!redist_check_instance(
3908 &zclient
->mi_redist
[afi
][type
], instance
))
3910 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3915 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3916 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
3919 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3921 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
3924 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3928 if (zclient
->sock
> 0)
3929 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
3934 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
3935 afi_t afi
, vrf_id_t vrf_id
)
3938 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
3939 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
3941 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
3943 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
3946 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
3949 if (zclient
->sock
> 0)
3950 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
3953 static void zclient_event(enum event event
, struct zclient
*zclient
)
3956 case ZCLIENT_SCHEDULE
:
3957 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
3958 &zclient
->t_connect
);
3960 case ZCLIENT_CONNECT
:
3963 "zclient connect failures: %d schedule interval is now %d",
3964 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
3965 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
3966 zclient
->fail
< 3 ? 10 : 60,
3967 &zclient
->t_connect
);
3970 zclient
->t_read
= NULL
;
3971 thread_add_read(zclient
->master
, zclient_read
, zclient
,
3972 zclient
->sock
, &zclient
->t_read
);
3977 void zclient_interface_set_master(struct zclient
*client
,
3978 struct interface
*master
,
3979 struct interface
*slave
)
3986 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
3988 stream_putl(s
, master
->vrf_id
);
3989 stream_putl(s
, master
->ifindex
);
3990 stream_putl(s
, slave
->vrf_id
);
3991 stream_putl(s
, slave
->ifindex
);
3993 stream_putw_at(s
, 0, stream_get_endp(s
));
3994 zclient_send_message(client
);
3998 * Send capabilities message to zebra
4000 int32_t zclient_capabilities_send(uint32_t cmd
, struct zclient
*zclient
,
4001 struct zapi_cap
*api
)
4006 if (zclient
== NULL
)
4011 zclient_create_header(s
, cmd
, 0);
4012 stream_putl(s
, api
->cap
);
4015 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4016 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4017 stream_putl(s
, api
->stale_removal_time
);
4018 stream_putl(s
, api
->vrf_id
);
4020 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4021 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4022 stream_putl(s
, api
->afi
);
4023 stream_putl(s
, api
->safi
);
4024 stream_putl(s
, api
->vrf_id
);
4026 case ZEBRA_CLIENT_GR_DISABLE
:
4027 stream_putl(s
, api
->vrf_id
);
4031 /* Put length at the first point of the stream */
4032 stream_putw_at(s
, 0, stream_get_endp(s
));
4034 return zclient_send_message(zclient
);
4038 * Process capabilities message from zebra
4040 int32_t zapi_capabilities_decode(struct stream
*s
, struct zapi_cap
*api
)
4043 memset(api
, 0, sizeof(*api
));
4045 STREAM_GETL(s
, api
->cap
);
4047 case ZEBRA_CLIENT_GR_CAPABILITIES
:
4048 case ZEBRA_CLIENT_RIB_STALE_TIME
:
4049 STREAM_GETL(s
, api
->stale_removal_time
);
4050 STREAM_GETL(s
, api
->vrf_id
);
4052 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
4053 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
4054 STREAM_GETL(s
, api
->afi
);
4055 STREAM_GETL(s
, api
->safi
);
4056 STREAM_GETL(s
, api
->vrf_id
);
4058 case ZEBRA_CLIENT_GR_DISABLE
:
4059 STREAM_GETL(s
, api
->vrf_id
);
4066 int zclient_send_neigh_discovery_req(struct zclient
*zclient
,
4067 const struct interface
*ifp
,
4068 const struct prefix
*p
)
4075 zclient_create_header(s
, ZEBRA_NEIGH_DISCOVER
, ifp
->vrf_id
);
4076 stream_putl(s
, ifp
->ifindex
);
4078 stream_putc(s
, p
->family
);
4079 stream_putc(s
, p
->prefixlen
);
4080 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
4082 stream_putw_at(s
, 0, stream_get_endp(s
));
4083 return zclient_send_message(zclient
);
4087 * Get a starting nhg point for a routing protocol
4089 uint32_t zclient_get_nhg_start(uint32_t proto
)
4091 assert(proto
< ZEBRA_ROUTE_MAX
);
4093 return ZEBRA_NHG_PROTO_SPACING
* proto
;