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"
43 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
44 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
46 /* Zebra client events. */
47 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
49 /* Prototype for event manager. */
50 static void zclient_event(enum event
, struct zclient
*);
52 struct sockaddr_storage zclient_addr
;
53 socklen_t zclient_addr_len
;
55 /* This file local debug flag. */
56 int zclient_debug
= 0;
58 struct zclient_options zclient_options_default
= {.receive_notify
= false};
60 /* Allocate zclient structure. */
61 struct zclient
*zclient_new(struct thread_master
*master
,
62 struct zclient_options
*opt
)
64 struct zclient
*zclient
;
65 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
67 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
68 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
69 zclient
->wb
= buffer_new(0);
70 zclient
->master
= master
;
72 zclient
->receive_notify
= opt
->receive_notify
;
77 /* This function is only called when exiting, because
78 many parts of the code do not check for I/O errors, so they could
79 reference an invalid pointer if the structure was ever freed.
81 Free zclient structure. */
82 void zclient_free(struct zclient
*zclient
)
85 stream_free(zclient
->ibuf
);
87 stream_free(zclient
->obuf
);
89 buffer_free(zclient
->wb
);
91 XFREE(MTYPE_ZCLIENT
, zclient
);
94 unsigned short *redist_check_instance(struct redist_proto
*red
,
95 unsigned short instance
)
97 struct listnode
*node
;
103 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
110 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
117 red
->instances
= list_new();
119 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
121 listnode_add(red
->instances
, in
);
124 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
128 id
= redist_check_instance(red
, instance
);
132 listnode_delete(red
->instances
, id
);
133 XFREE(MTYPE_REDIST_INST
, id
);
134 if (!red
->instances
->count
) {
136 list_delete(&red
->instances
);
140 /* Stop zebra client services. */
141 void zclient_stop(struct zclient
*zclient
)
147 zlog_debug("zclient stopped");
150 THREAD_OFF(zclient
->t_read
);
151 THREAD_OFF(zclient
->t_connect
);
152 THREAD_OFF(zclient
->t_write
);
155 stream_reset(zclient
->ibuf
);
156 stream_reset(zclient
->obuf
);
158 /* Empty the write buffer. */
159 buffer_reset(zclient
->wb
);
162 if (zclient
->sock
>= 0) {
163 close(zclient
->sock
);
168 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
169 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
170 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
171 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
174 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
177 vrf_bitmap_free(zclient
->default_information
[afi
]);
178 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
182 void zclient_reset(struct zclient
*zclient
)
186 zclient_stop(zclient
);
188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
190 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
193 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
198 * Connect to zebra daemon.
199 * @param zclient a pointer to zclient structure
200 * @return socket fd just to make sure that connection established
204 int zclient_socket_connect(struct zclient
*zclient
)
209 /* We should think about IPv6 connection. */
210 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
216 frr_elevate_privs(zclient
->privs
) {
217 setsockopt_so_sendbuf(sock
, 1048576);
220 /* Connect to zebra. */
221 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
224 zlog_debug("%s connect failure: %d(%s)",
225 __PRETTY_FUNCTION__
, errno
,
226 safe_strerror(errno
));
231 zclient
->sock
= sock
;
235 static int zclient_failed(struct zclient
*zclient
)
238 zclient_stop(zclient
);
239 zclient_event(ZCLIENT_CONNECT
, zclient
);
243 static int zclient_flush_data(struct thread
*thread
)
245 struct zclient
*zclient
= THREAD_ARG(thread
);
247 zclient
->t_write
= NULL
;
248 if (zclient
->sock
< 0)
250 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
254 "%s: buffer_flush_available failed on zclient fd %d, closing",
255 __func__
, zclient
->sock
);
256 return zclient_failed(zclient
);
259 zclient
->t_write
= NULL
;
260 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
261 zclient
->sock
, &zclient
->t_write
);
269 int zclient_send_message(struct zclient
*zclient
)
271 if (zclient
->sock
< 0)
273 switch (buffer_write(zclient
->wb
, zclient
->sock
,
274 STREAM_DATA(zclient
->obuf
),
275 stream_get_endp(zclient
->obuf
))) {
277 flog_err(EC_LIB_ZAPI_SOCKET
,
278 "%s: buffer_write failed to zclient fd %d, closing",
279 __func__
, zclient
->sock
);
280 return zclient_failed(zclient
);
283 THREAD_OFF(zclient
->t_write
);
286 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
287 zclient
->sock
, &zclient
->t_write
);
293 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
295 /* length placeholder, caller can update */
296 stream_putw(s
, ZEBRA_HEADER_SIZE
);
297 stream_putc(s
, ZEBRA_HEADER_MARKER
);
298 stream_putc(s
, ZSERV_VERSION
);
299 stream_putl(s
, vrf_id
);
300 stream_putw(s
, command
);
303 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
304 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
307 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
310 STREAM_GETW(s
, *size
);
311 *size
-= ZEBRA_HEADER_SIZE
;
312 STREAM_GETC(s
, *marker
);
313 STREAM_GETC(s
, *version
);
314 STREAM_GETL(s
, *vrf_id
);
315 STREAM_GETW(s
, *cmd
);
317 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
319 EC_LIB_ZAPI_MISSMATCH
,
320 "%s: socket %d version mismatch, marker %d, version %d",
321 __func__
, sock
, *marker
, *version
);
325 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
332 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
334 STREAM_GETW(zmsg
, hdr
->length
);
335 STREAM_GETC(zmsg
, hdr
->marker
);
336 STREAM_GETC(zmsg
, hdr
->version
);
337 STREAM_GETL(zmsg
, hdr
->vrf_id
);
338 STREAM_GETW(zmsg
, hdr
->command
);
344 /* Send simple Zebra message. */
345 static int zebra_message_send(struct zclient
*zclient
, int command
,
350 /* Get zclient output buffer. */
354 /* Send very simple command only Zebra message. */
355 zclient_create_header(s
, command
, vrf_id
);
357 return zclient_send_message(zclient
);
360 static int zebra_hello_send(struct zclient
*zclient
)
364 if (zclient
->redist_default
) {
368 /* The VRF ID in the HELLO message is always 0. */
369 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
370 stream_putc(s
, zclient
->redist_default
);
371 stream_putw(s
, zclient
->instance
);
372 if (zclient
->receive_notify
)
377 stream_putw_at(s
, 0, stream_get_endp(s
));
378 return zclient_send_message(zclient
);
384 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
385 mpls_label_t label
, enum lsp_types_t ltype
)
392 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
393 stream_putl(s
, label
);
395 stream_putc(s
, ltype
);
396 stream_putw_at(s
, 0, stream_get_endp(s
));
397 zclient_send_message(zclient
);
400 /* Send register requests to zebra daemon for the information in a VRF. */
401 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
406 /* If not connected to the zebra yet. */
407 if (zclient
->sock
< 0)
411 zlog_debug("%s: send register messages for VRF %u", __func__
,
414 /* We need router-id information. */
415 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
417 /* We need interface information. */
418 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
420 /* Set unwanted redistribute route. */
421 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
422 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
425 /* Flush all redistribute request. */
426 if (vrf_id
== VRF_DEFAULT
) {
427 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
428 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
429 if (!zclient
->mi_redist
[afi
][i
].enabled
)
432 struct listnode
*node
;
435 for (ALL_LIST_ELEMENTS_RO(
436 zclient
->mi_redist
[afi
][i
]
439 if (!(i
== zclient
->redist_default
440 && *id
== zclient
->instance
))
441 zebra_redistribute_send(
442 ZEBRA_REDISTRIBUTE_ADD
,
443 zclient
, afi
, i
, *id
,
449 /* Resend all redistribute request. */
450 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
451 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
452 if (i
!= zclient
->redist_default
453 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
455 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
459 /* If default information is needed. */
460 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
461 zebra_redistribute_default_send(
462 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
467 /* Send unregister requests to zebra daemon for the information in a VRF. */
468 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
473 /* If not connected to the zebra yet. */
474 if (zclient
->sock
< 0)
478 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
481 /* We need router-id information. */
482 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
484 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
486 /* Set unwanted redistribute route. */
487 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
488 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
491 /* Flush all redistribute request. */
492 if (vrf_id
== VRF_DEFAULT
) {
493 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
494 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
495 if (!zclient
->mi_redist
[afi
][i
].enabled
)
498 struct listnode
*node
;
501 for (ALL_LIST_ELEMENTS_RO(
502 zclient
->mi_redist
[afi
][i
]
505 if (!(i
== zclient
->redist_default
506 && *id
== zclient
->instance
))
507 zebra_redistribute_send(
508 ZEBRA_REDISTRIBUTE_DELETE
,
509 zclient
, afi
, i
, *id
,
515 /* Flush all redistribute request. */
516 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
517 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
518 if (i
!= zclient
->redist_default
519 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
521 zebra_redistribute_send(
522 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
525 /* If default information is needed. */
526 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
527 zebra_redistribute_default_send(
528 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
533 /* Send request to zebra daemon to start or stop RA. */
534 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
535 struct interface
*ifp
, int enable
,
540 /* If not connected to the zebra yet. */
541 if (zclient
->sock
< 0)
544 /* Form and send message. */
549 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
551 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
553 stream_putl(s
, ifp
->ifindex
);
554 stream_putl(s
, ra_interval
);
556 stream_putw_at(s
, 0, stream_get_endp(s
));
558 zclient_send_message(zclient
);
561 /* Make connection to zebra daemon. */
562 int zclient_start(struct zclient
*zclient
)
565 zlog_info("zclient_start is called");
567 /* If already connected to the zebra. */
568 if (zclient
->sock
>= 0)
571 /* Check connect thread. */
572 if (zclient
->t_connect
)
575 if (zclient_socket_connect(zclient
) < 0) {
577 zlog_debug("zclient connection fail");
579 zclient_event(ZCLIENT_CONNECT
, zclient
);
583 if (set_nonblocking(zclient
->sock
) < 0)
584 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
585 __func__
, zclient
->sock
);
587 /* Clear fail count. */
590 zlog_debug("zclient connect success with socket [%d]",
593 /* Create read thread. */
594 zclient_event(ZCLIENT_READ
, zclient
);
596 zebra_hello_send(zclient
);
598 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
600 /* Inform the successful connection. */
601 if (zclient
->zebra_connected
)
602 (*zclient
->zebra_connected
)(zclient
);
607 /* Initialize zebra client. Argument redist_default is unwanted
608 redistribute route type. */
609 void zclient_init(struct zclient
*zclient
, int redist_default
,
610 unsigned short instance
, struct zebra_privs_t
*privs
)
614 /* Set -1 to the default socket value. */
616 zclient
->privs
= privs
;
618 /* Clear redistribution flags. */
619 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
620 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
621 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
623 /* Set unwanted redistribute route. bgpd does not need BGP route
625 zclient
->redist_default
= redist_default
;
626 zclient
->instance
= instance
;
627 /* Pending: make afi(s) an arg. */
628 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
629 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
632 /* Set default-information redistribute to zero. */
633 zclient
->default_information
[afi
] = vrf_bitmap_init();
637 zlog_debug("scheduling zclient connection");
639 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
642 /* This function is a wrapper function for calling zclient_start from
643 timer or event thread. */
644 static int zclient_connect(struct thread
*t
)
646 struct zclient
*zclient
;
648 zclient
= THREAD_ARG(t
);
649 zclient
->t_connect
= NULL
;
652 zlog_debug("zclient_connect is called");
654 return zclient_start(zclient
);
657 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
658 bool exact_match
, vrf_id_t vrf_id
)
664 zclient_create_header(s
, command
, vrf_id
);
665 stream_putc(s
, (exact_match
) ? 1 : 0);
667 stream_putw(s
, PREFIX_FAMILY(p
));
668 stream_putc(s
, p
->prefixlen
);
669 switch (PREFIX_FAMILY(p
)) {
671 stream_put_in_addr(s
, &p
->u
.prefix4
);
674 stream_put(s
, &(p
->u
.prefix6
), 16);
679 stream_putw_at(s
, 0, stream_get_endp(s
));
681 return zclient_send_message(zclient
);
685 * "xdr_encode"-like interface that allows daemon (client) to send
686 * a message to zebra server for a route that needs to be
687 * added/deleted to the kernel. Info about the route is specified
688 * by the caller in a struct zapi_route. zapi_route_encode() then writes
689 * the info down the zclient socket using the stream_* functions.
691 * The corresponding read ("xdr_decode") function on the server
692 * side is zapi_route_decode().
694 * 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
695 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
696 * | Length (2) | Command | Route Type |
697 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
698 * | ZEBRA Flags | Message Flags | Prefix length |
699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * | Destination IPv4 Prefix for route |
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
707 * described, as per the Nexthop count. Each nexthop described as:
710 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
711 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
712 * | IPv4 Nexthop address or Interface Index number |
713 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
715 * Alternatively, if the route is a blackhole route, then Nexthop count
716 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
719 * The original struct zapi_route_*() infrastructure was built around
720 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
721 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
722 * nexthops into zapi_route_encode() using the same zapi_route structure.
723 * This is done by setting zapi_route fields as follows:
724 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
725 * - .nexthop_num == .ifindex_num
726 * - .nexthop and .ifindex are filled with gate and ifindex parts of
727 * each compound nexthop, both in the same order
729 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
732 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
735 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
737 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
739 * XXX: No attention paid to alignment.
741 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
742 struct zapi_route
*api
)
744 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
746 return zclient_send_message(zclient
);
749 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
751 struct zapi_nexthop
*api_nh
;
756 zclient_create_header(s
, cmd
, api
->vrf_id
);
758 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
759 flog_err(EC_LIB_ZAPI_ENCODE
,
760 "%s: Specified route type (%u) is not a legal value\n",
761 __PRETTY_FUNCTION__
, api
->type
);
764 stream_putc(s
, api
->type
);
766 stream_putw(s
, api
->instance
);
767 stream_putl(s
, api
->flags
);
768 stream_putc(s
, api
->message
);
770 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
771 flog_err(EC_LIB_ZAPI_ENCODE
,
772 "%s: Specified route SAFI (%u) is not a legal value\n",
773 __PRETTY_FUNCTION__
, api
->safi
);
776 stream_putc(s
, api
->safi
);
778 /* Put prefix information. */
779 stream_putc(s
, api
->prefix
.family
);
780 psize
= PSIZE(api
->prefix
.prefixlen
);
781 stream_putc(s
, api
->prefix
.prefixlen
);
782 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
784 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
785 psize
= PSIZE(api
->src_prefix
.prefixlen
);
786 stream_putc(s
, api
->src_prefix
.prefixlen
);
787 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
791 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
792 /* limit the number of nexthops if necessary */
793 if (api
->nexthop_num
> MULTIPATH_NUM
) {
794 char buf
[PREFIX2STR_BUFFER
];
796 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
799 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
800 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
804 stream_putw(s
, api
->nexthop_num
);
806 for (i
= 0; i
< api
->nexthop_num
; i
++) {
807 api_nh
= &api
->nexthops
[i
];
809 stream_putl(s
, api_nh
->vrf_id
);
810 stream_putc(s
, api_nh
->type
);
811 stream_putc(s
, api_nh
->onlink
);
812 switch (api_nh
->type
) {
813 case NEXTHOP_TYPE_BLACKHOLE
:
814 stream_putc(s
, api_nh
->bh_type
);
816 case NEXTHOP_TYPE_IPV4
:
817 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
819 case NEXTHOP_TYPE_IPV4_IFINDEX
:
820 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
821 stream_putl(s
, api_nh
->ifindex
);
823 case NEXTHOP_TYPE_IFINDEX
:
824 stream_putl(s
, api_nh
->ifindex
);
826 case NEXTHOP_TYPE_IPV6
:
827 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
830 case NEXTHOP_TYPE_IPV6_IFINDEX
:
831 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
833 stream_putl(s
, api_nh
->ifindex
);
837 /* MPLS labels for BGP-LU or Segment Routing */
838 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
839 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
840 char buf
[PREFIX2STR_BUFFER
];
841 prefix2str(&api
->prefix
, buf
,
843 flog_err(EC_LIB_ZAPI_ENCODE
,
844 "%s: prefix %s: can't encode "
845 "%u labels (maximum is %u)",
852 stream_putc(s
, api_nh
->label_num
);
853 stream_put(s
, &api_nh
->labels
[0],
855 * sizeof(mpls_label_t
));
858 /* Router MAC for EVPN routes. */
859 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
860 stream_put(s
, &(api_nh
->rmac
),
861 sizeof(struct ethaddr
));
866 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
867 stream_putc(s
, api
->distance
);
868 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
869 stream_putl(s
, api
->metric
);
870 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
871 stream_putl(s
, api
->tag
);
872 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
873 stream_putl(s
, api
->mtu
);
874 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
875 stream_putl(s
, api
->tableid
);
877 /* Put length at the first point of the stream. */
878 stream_putw_at(s
, 0, stream_get_endp(s
));
883 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
885 struct zapi_nexthop
*api_nh
;
888 memset(api
, 0, sizeof(*api
));
890 /* Type, flags, message. */
891 STREAM_GETC(s
, api
->type
);
892 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
893 flog_err(EC_LIB_ZAPI_ENCODE
,
894 "%s: Specified route type: %d is not a legal value\n",
895 __PRETTY_FUNCTION__
, api
->type
);
899 STREAM_GETW(s
, api
->instance
);
900 STREAM_GETL(s
, api
->flags
);
901 STREAM_GETC(s
, api
->message
);
902 STREAM_GETC(s
, api
->safi
);
903 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
904 flog_err(EC_LIB_ZAPI_ENCODE
,
905 "%s: Specified route SAFI (%u) is not a legal value\n",
906 __PRETTY_FUNCTION__
, api
->safi
);
911 STREAM_GETC(s
, api
->prefix
.family
);
912 STREAM_GETC(s
, api
->prefix
.prefixlen
);
913 switch (api
->prefix
.family
) {
915 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
918 "%s: V4 prefixlen is %d which should not be more than 32",
919 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
924 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
927 "%s: v6 prefixlen is %d which should not be more than 128",
928 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
933 flog_err(EC_LIB_ZAPI_ENCODE
,
934 "%s: Specified family %d is not v4 or v6",
935 __PRETTY_FUNCTION__
, api
->prefix
.family
);
938 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
940 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
941 api
->src_prefix
.family
= AF_INET6
;
942 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
943 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
946 "%s: SRC Prefix prefixlen received: %d is too large",
947 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
950 STREAM_GET(&api
->src_prefix
.prefix
, s
,
951 PSIZE(api
->src_prefix
.prefixlen
));
953 if (api
->prefix
.family
!= AF_INET6
954 || api
->src_prefix
.prefixlen
== 0) {
957 "%s: SRC prefix specified in some manner that makes no sense",
958 __PRETTY_FUNCTION__
);
964 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
965 STREAM_GETW(s
, api
->nexthop_num
);
966 if (api
->nexthop_num
> MULTIPATH_NUM
) {
967 flog_err(EC_LIB_ZAPI_ENCODE
,
968 "%s: invalid number of nexthops (%u)",
969 __func__
, api
->nexthop_num
);
973 for (i
= 0; i
< api
->nexthop_num
; i
++) {
974 api_nh
= &api
->nexthops
[i
];
976 STREAM_GETL(s
, api_nh
->vrf_id
);
977 STREAM_GETC(s
, api_nh
->type
);
978 STREAM_GETC(s
, api_nh
->onlink
);
979 switch (api_nh
->type
) {
980 case NEXTHOP_TYPE_BLACKHOLE
:
981 STREAM_GETC(s
, api_nh
->bh_type
);
983 case NEXTHOP_TYPE_IPV4
:
984 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
987 case NEXTHOP_TYPE_IPV4_IFINDEX
:
988 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
990 STREAM_GETL(s
, api_nh
->ifindex
);
992 case NEXTHOP_TYPE_IFINDEX
:
993 STREAM_GETL(s
, api_nh
->ifindex
);
995 case NEXTHOP_TYPE_IPV6
:
996 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
998 case NEXTHOP_TYPE_IPV6_IFINDEX
:
999 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1000 STREAM_GETL(s
, api_nh
->ifindex
);
1004 /* MPLS labels for BGP-LU or Segment Routing */
1005 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1006 STREAM_GETC(s
, api_nh
->label_num
);
1008 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1011 "%s: invalid number of MPLS labels (%u)",
1012 __func__
, api_nh
->label_num
);
1016 STREAM_GET(&api_nh
->labels
[0], s
,
1018 * sizeof(mpls_label_t
));
1021 /* Router MAC for EVPN routes. */
1022 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1023 stream_get(&(api_nh
->rmac
), s
,
1024 sizeof(struct ethaddr
));
1029 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1030 STREAM_GETC(s
, api
->distance
);
1031 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1032 STREAM_GETL(s
, api
->metric
);
1033 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1034 STREAM_GETL(s
, api
->tag
);
1035 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1036 STREAM_GETL(s
, api
->mtu
);
1037 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1038 STREAM_GETL(s
, api
->tableid
);
1044 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1050 memset(&any
, 0, sizeof(any
));
1051 any
.family
= family
;
1055 stream_putc(s
, p
->family
);
1056 stream_putc(s
, p
->prefixlen
);
1057 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1060 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1063 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1066 * We are sending one item at a time at the moment
1070 stream_putl(s
, zrule
->seq
);
1071 stream_putl(s
, zrule
->priority
);
1072 stream_putl(s
, zrule
->unique
);
1074 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1075 zrule
->filter
.src_ip
.family
);
1076 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1077 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1078 zrule
->filter
.src_ip
.family
);
1079 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1080 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1082 stream_putl(s
, zrule
->action
.table
);
1083 stream_putl(s
, zrule
->ifindex
);
1085 /* Put length at the first point of the stream. */
1086 stream_putw_at(s
, 0, stream_get_endp(s
));
1091 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1093 enum zapi_route_notify_owner
*note
)
1097 STREAM_GET(note
, s
, sizeof(*note
));
1099 STREAM_GETC(s
, p
->family
);
1100 STREAM_GETC(s
, p
->prefixlen
);
1101 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1112 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1113 uint32_t *priority
, uint32_t *unique
,
1115 enum zapi_rule_notify_owner
*note
)
1117 uint32_t prio
, seq
, uni
;
1120 STREAM_GET(note
, s
, sizeof(*note
));
1122 STREAM_GETL(s
, seq
);
1123 STREAM_GETL(s
, prio
);
1124 STREAM_GETL(s
, uni
);
1125 STREAM_GETL(s
, ifi
);
1128 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1141 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1142 enum zapi_ipset_notify_owner
*note
)
1146 STREAM_GET(note
, s
, sizeof(*note
));
1148 STREAM_GETL(s
, uni
);
1151 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1160 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1162 enum zapi_ipset_entry_notify_owner
*note
)
1166 STREAM_GET(note
, s
, sizeof(*note
));
1168 STREAM_GETL(s
, uni
);
1170 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1173 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1182 bool zapi_iptable_notify_decode(struct stream
*s
,
1184 enum zapi_iptable_notify_owner
*note
)
1188 STREAM_GET(note
, s
, sizeof(*note
));
1190 STREAM_GETL(s
, uni
);
1193 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1202 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1204 struct nexthop
*n
= nexthop_new();
1206 n
->type
= znh
->type
;
1207 n
->vrf_id
= znh
->vrf_id
;
1208 n
->ifindex
= znh
->ifindex
;
1209 n
->gate
= znh
->gate
;
1212 * This function currently handles labels
1214 if (znh
->label_num
) {
1215 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1222 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1226 memset(nhr
, 0, sizeof(*nhr
));
1228 STREAM_GETW(s
, nhr
->prefix
.family
);
1229 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1230 switch (nhr
->prefix
.family
) {
1232 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1235 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1241 STREAM_GETC(s
, nhr
->type
);
1242 STREAM_GETW(s
, nhr
->instance
);
1243 STREAM_GETC(s
, nhr
->distance
);
1244 STREAM_GETL(s
, nhr
->metric
);
1245 STREAM_GETC(s
, nhr
->nexthop_num
);
1247 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1248 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1249 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1250 switch (nhr
->nexthops
[i
].type
) {
1251 case NEXTHOP_TYPE_IPV4
:
1252 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1253 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1255 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1257 case NEXTHOP_TYPE_IFINDEX
:
1258 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1260 case NEXTHOP_TYPE_IPV6
:
1261 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1262 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1264 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1266 case NEXTHOP_TYPE_BLACKHOLE
:
1269 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1270 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1271 flog_err(EC_LIB_ZAPI_ENCODE
,
1272 "%s: invalid number of MPLS labels (%u)",
1273 __func__
, nhr
->nexthops
[i
].label_num
);
1276 if (nhr
->nexthops
[i
].label_num
)
1277 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1278 nhr
->nexthops
[i
].label_num
1279 * sizeof(mpls_label_t
));
1288 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1289 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1290 * then set/unset redist[type] in the client handle (a struct zserv) for the
1293 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1294 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1301 zclient_create_header(s
, command
, vrf_id
);
1302 stream_putc(s
, afi
);
1303 stream_putc(s
, type
);
1304 stream_putw(s
, instance
);
1306 stream_putw_at(s
, 0, stream_get_endp(s
));
1308 return zclient_send_message(zclient
);
1311 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1312 afi_t afi
, vrf_id_t vrf_id
)
1319 zclient_create_header(s
, command
, vrf_id
);
1320 stream_putc(s
, afi
);
1322 stream_putw_at(s
, 0, stream_get_endp(s
));
1324 return zclient_send_message(zclient
);
1327 /* Get prefix in ZServ format; family should be filled in on prefix */
1328 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1330 size_t plen
= prefix_blen(p
);
1337 stream_get(&p
->u
.prefix
, s
, plen
);
1339 p
->prefixlen
= MIN(plen
* 8, c
);
1345 /* Router-id update from zebra daemon. */
1346 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1348 /* Fetch interface address. */
1349 STREAM_GETC(s
, rid
->family
);
1351 zclient_stream_get_prefix(s
, rid
);
1357 /* Interface addition from zebra daemon. */
1359 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1360 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1362 * 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
1363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1369 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1387 * | Link Layer Type |
1388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1389 * | Harware Address Length |
1390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 * | Hardware Address if HW lenght different from 0 |
1392 * | ... max INTERFACE_HWADDR_MAX |
1393 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1394 * | Link_params? | Whether a link-params follows: 1 or 0.
1395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1396 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1397 * | .... (struct if_link_params). |
1398 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1401 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1404 char vrfname_tmp
[VRF_NAMSIZ
];
1405 struct vrf_data data
;
1407 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1408 /* Read interface name. */
1409 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1411 /* Lookup/create vrf by vrf_id. */
1412 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1413 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1414 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1415 /* overwrite default vrf */
1416 if (vrf_id
== VRF_DEFAULT
)
1417 vrf_set_default_name(vrfname_tmp
, false);
1421 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1425 /* Lookup vrf by vrf_id. */
1426 vrf
= vrf_lookup_by_id(vrf_id
);
1429 * If a routing protocol doesn't know about a
1430 * vrf that is about to be deleted. There is
1431 * no point in attempting to delete it.
1439 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1441 struct interface
*ifp
;
1442 char ifname_tmp
[INTERFACE_NAMSIZ
];
1444 /* Read interface name. */
1445 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1447 /* Lookup/create interface by name. */
1448 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1450 zebra_interface_if_set_value(s
, ifp
);
1456 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1457 * from zebra server. The format of this message is the same as
1458 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1459 * comments for zebra_interface_add_read), except that no sockaddr_dl
1460 * is sent at the tail of the message.
1462 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1464 struct interface
*ifp
;
1465 char ifname_tmp
[INTERFACE_NAMSIZ
];
1467 /* Read interface name. */
1468 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1470 /* Lookup this by interface index. */
1471 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1473 flog_err(EC_LIB_ZAPI_ENCODE
,
1474 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1475 ifname_tmp
, vrf_id
);
1479 zebra_interface_if_set_value(s
, ifp
);
1484 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1490 iflp
->lp_status
= stream_getl(s
);
1491 iflp
->te_metric
= stream_getl(s
);
1492 iflp
->max_bw
= stream_getf(s
);
1493 iflp
->max_rsv_bw
= stream_getf(s
);
1494 uint32_t bwclassnum
= stream_getl(s
);
1497 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1498 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1501 EC_LIB_ZAPI_MISSMATCH
,
1502 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1503 " - outdated library?",
1504 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1506 iflp
->admin_grp
= stream_getl(s
);
1507 iflp
->rmt_as
= stream_getl(s
);
1508 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1510 iflp
->av_delay
= stream_getl(s
);
1511 iflp
->min_delay
= stream_getl(s
);
1512 iflp
->max_delay
= stream_getl(s
);
1513 iflp
->delay_var
= stream_getl(s
);
1515 iflp
->pkt_loss
= stream_getf(s
);
1516 iflp
->res_bw
= stream_getf(s
);
1517 iflp
->ava_bw
= stream_getf(s
);
1518 iflp
->use_bw
= stream_getf(s
);
1521 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1524 struct if_link_params
*iflp
;
1529 ifindex
= stream_getl(s
);
1531 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1534 flog_err(EC_LIB_ZAPI_ENCODE
,
1535 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1540 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1543 link_params_set_value(s
, iflp
);
1548 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1550 uint8_t link_params_status
= 0;
1551 ifindex_t old_ifindex
;
1553 old_ifindex
= ifp
->ifindex
;
1554 /* Read interface's index. */
1555 if_set_index(ifp
, stream_getl(s
));
1556 ifp
->status
= stream_getc(s
);
1558 /* Read interface's value. */
1559 ifp
->flags
= stream_getq(s
);
1560 ifp
->ptm_enable
= stream_getc(s
);
1561 ifp
->ptm_status
= stream_getc(s
);
1562 ifp
->metric
= stream_getl(s
);
1563 ifp
->speed
= stream_getl(s
);
1564 ifp
->mtu
= stream_getl(s
);
1565 ifp
->mtu6
= stream_getl(s
);
1566 ifp
->bandwidth
= stream_getl(s
);
1567 ifp
->ll_type
= stream_getl(s
);
1568 ifp
->hw_addr_len
= stream_getl(s
);
1569 if (ifp
->hw_addr_len
)
1570 stream_get(ifp
->hw_addr
, s
,
1571 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1573 /* Read Traffic Engineering status */
1574 link_params_status
= stream_getc(s
);
1575 /* Then, Traffic Engineering parameters if any */
1576 if (link_params_status
) {
1577 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1578 link_params_set_value(s
, iflp
);
1581 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1584 size_t zebra_interface_link_params_write(struct stream
*s
,
1585 struct interface
*ifp
)
1588 struct if_link_params
*iflp
;
1591 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1594 iflp
= ifp
->link_params
;
1597 w
+= stream_putl(s
, iflp
->lp_status
);
1599 w
+= stream_putl(s
, iflp
->te_metric
);
1600 w
+= stream_putf(s
, iflp
->max_bw
);
1601 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1603 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1604 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1605 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1607 w
+= stream_putl(s
, iflp
->admin_grp
);
1608 w
+= stream_putl(s
, iflp
->rmt_as
);
1609 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1611 w
+= stream_putl(s
, iflp
->av_delay
);
1612 w
+= stream_putl(s
, iflp
->min_delay
);
1613 w
+= stream_putl(s
, iflp
->max_delay
);
1614 w
+= stream_putl(s
, iflp
->delay_var
);
1616 w
+= stream_putf(s
, iflp
->pkt_loss
);
1617 w
+= stream_putf(s
, iflp
->res_bw
);
1618 w
+= stream_putf(s
, iflp
->ava_bw
);
1619 w
+= stream_putf(s
, iflp
->use_bw
);
1625 * format of message for address additon is:
1629 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1630 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1639 * | ifc_flags | flags for connected address
1647 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1655 static int memconstant(const void *s
, int c
, size_t n
)
1657 const uint8_t *p
= s
;
1666 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1670 struct interface
*ifp
;
1671 struct connected
*ifc
;
1672 struct prefix p
, d
, *dp
;
1676 memset(&p
, 0, sizeof(p
));
1677 memset(&d
, 0, sizeof(d
));
1679 /* Get interface index. */
1680 ifindex
= stream_getl(s
);
1683 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1685 flog_err(EC_LIB_ZAPI_ENCODE
,
1686 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1687 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1693 ifc_flags
= stream_getc(s
);
1695 /* Fetch interface address. */
1696 d
.family
= p
.family
= stream_getc(s
);
1697 plen
= prefix_blen(&d
);
1699 zclient_stream_get_prefix(s
, &p
);
1701 /* Fetch destination address. */
1702 stream_get(&d
.u
.prefix
, s
, plen
);
1704 /* N.B. NULL destination pointers are encoded as all zeroes */
1705 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1707 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1708 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1710 /* N.B. NULL destination pointers are encoded as all
1712 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1715 ifc
->flags
= ifc_flags
;
1716 if (ifc
->destination
)
1717 ifc
->destination
->prefixlen
=
1718 ifc
->address
->prefixlen
;
1719 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1720 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1722 char buf
[PREFIX_STRLEN
];
1725 "warning: interface %s address %s with peer flag set, but no peer address!",
1727 prefix2str(ifc
->address
, buf
,
1729 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1733 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1734 ifc
= connected_delete_by_prefix(ifp
, &p
);
1741 * format of message for neighbor connected address is:
1745 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1746 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1761 * | addr_len | len of addr.
1764 struct nbr_connected
*
1765 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1767 unsigned int ifindex
;
1768 struct interface
*ifp
;
1770 struct nbr_connected
*ifc
;
1772 /* Get interface index. */
1773 ifindex
= stream_getl(s
);
1776 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1778 flog_err(EC_LIB_ZAPI_ENCODE
,
1779 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1780 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1786 p
.family
= stream_getc(s
);
1787 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1788 p
.prefixlen
= stream_getc(s
);
1790 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1791 /* Currently only supporting P2P links, so any new RA source
1793 considered as the replacement of the previously learnt
1794 Link-Local address. */
1795 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1796 ifc
= nbr_connected_new();
1797 ifc
->address
= prefix_new();
1799 listnode_add(ifp
->nbr_connected
, ifc
);
1802 prefix_copy(ifc
->address
, &p
);
1804 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1806 ifc
= nbr_connected_check(ifp
, &p
);
1808 listnode_delete(ifp
->nbr_connected
, ifc
);
1814 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1816 vrf_id_t
*new_vrf_id
)
1818 char ifname
[INTERFACE_NAMSIZ
];
1819 struct interface
*ifp
;
1822 /* Read interface name. */
1823 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
1825 /* Lookup interface. */
1826 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1828 flog_err(EC_LIB_ZAPI_ENCODE
,
1829 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1834 /* Fetch new VRF Id. */
1835 new_id
= stream_getw(s
);
1837 *new_vrf_id
= new_id
;
1841 /* filter unwanted messages until the expected one arrives */
1842 static int zclient_read_sync_response(struct zclient
*zclient
,
1843 uint16_t expected_cmd
)
1855 cmd
= expected_cmd
+ 1;
1856 while (ret
== 0 && cmd
!= expected_cmd
) {
1860 /* wait until response arrives */
1862 FD_SET(zclient
->sock
, &readfds
);
1863 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1864 if (!FD_ISSET(zclient
->sock
, &readfds
))
1867 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1868 &version
, &vrf_id
, &cmd
);
1870 zlog_debug("%s: Response (%d bytes) received", __func__
,
1874 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
1882 * Connect to label manager in a syncronous way
1884 * It first writes the request to zcient output buffer and then
1885 * immediately reads the answer from the input buffer.
1887 * @param zclient Zclient used to connect to label manager (zebra)
1888 * @param async Synchronous (0) or asynchronous (1) operation
1889 * @result Result of response
1891 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
1896 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
1897 ZEBRA_LABEL_MANAGER_CONNECT
;
1900 zlog_debug("Connecting to Label Manager (LM)");
1902 if (zclient
->sock
< 0) {
1903 zlog_debug("%s: invalid zclient socket", __func__
);
1910 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1913 stream_putc(s
, zclient
->redist_default
);
1915 stream_putw(s
, zclient
->instance
);
1917 /* Put length at the first point of the stream. */
1918 stream_putw_at(s
, 0, stream_get_endp(s
));
1920 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1922 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
1923 close(zclient
->sock
);
1928 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
1929 close(zclient
->sock
);
1934 zlog_debug("LM connect request sent (%d bytes)", ret
);
1940 if (zclient_read_sync_response(zclient
, cmd
)
1946 /* read instance and proto */
1947 uint8_t proto
= stream_getc(s
);
1948 uint16_t instance
= stream_getw(s
);
1951 if (proto
!= zclient
->redist_default
)
1954 "Wrong proto (%u) in LM connect response. Should be %u",
1955 proto
, zclient
->redist_default
);
1956 if (instance
!= zclient
->instance
)
1959 "Wrong instId (%u) in LM connect response. Should be %u",
1960 instance
, zclient
->instance
);
1963 result
= stream_getc(s
);
1965 zlog_debug("LM connect-response received, result %u", result
);
1971 * Asynchronous label chunk request
1973 * @param zclient Zclient used to connect to label manager (zebra)
1974 * @param keep Avoid garbage collection
1975 * @param chunk_size Amount of labels requested
1976 * @result 0 on success, -1 otherwise
1978 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1979 uint32_t chunk_size
)
1984 zlog_debug("Getting Label Chunk");
1986 if (zclient
->sock
< 0)
1992 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1994 stream_putc(s
, zclient
->redist_default
);
1996 stream_putw(s
, zclient
->instance
);
1997 stream_putc(s
, keep
);
1998 stream_putl(s
, chunk_size
);
2000 /* Put length at the first point of the stream. */
2001 stream_putw_at(s
, 0, stream_get_endp(s
));
2003 return zclient_send_message(zclient
);
2007 * Function to request a label chunk in a syncronous way
2009 * It first writes the request to zlcient output buffer and then
2010 * immediately reads the answer from the input buffer.
2012 * @param zclient Zclient used to connect to label manager (zebra)
2013 * @param keep Avoid garbage collection
2014 * @param chunk_size Amount of labels requested
2015 * @param start To write first assigned chunk label to
2016 * @param end To write last assigned chunk label to
2017 * @result 0 on success, -1 otherwise
2019 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2020 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2024 uint8_t response_keep
;
2027 zlog_debug("Getting Label Chunk");
2029 if (zclient
->sock
< 0)
2035 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2037 stream_putc(s
, zclient
->redist_default
);
2039 stream_putw(s
, zclient
->instance
);
2041 stream_putc(s
, keep
);
2043 stream_putl(s
, chunk_size
);
2044 /* Put length at the first point of the stream. */
2045 stream_putw_at(s
, 0, stream_get_endp(s
));
2047 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2049 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2050 close(zclient
->sock
);
2055 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2056 close(zclient
->sock
);
2061 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2064 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2067 /* parse response */
2070 /* read proto and instance */
2071 uint8_t proto
= stream_getc(s
);
2072 uint16_t instance
= stream_getw(s
);
2075 if (proto
!= zclient
->redist_default
)
2076 flog_err(EC_LIB_ZAPI_ENCODE
,
2077 "Wrong proto (%u) in get chunk response. Should be %u",
2078 proto
, zclient
->redist_default
);
2079 if (instance
!= zclient
->instance
)
2080 flog_err(EC_LIB_ZAPI_ENCODE
,
2081 "Wrong instId (%u) in get chunk response Should be %u",
2082 instance
, zclient
->instance
);
2085 response_keep
= stream_getc(s
);
2086 /* start and end labels */
2087 *start
= stream_getl(s
);
2088 *end
= stream_getl(s
);
2090 /* not owning this response */
2091 if (keep
!= response_keep
) {
2094 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2095 *start
, *end
, keep
, response_keep
);
2098 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2099 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2100 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2106 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2113 * Function to release a label chunk
2115 * @param zclient Zclient used to connect to label manager (zebra)
2116 * @param start First label of chunk
2117 * @param end Last label of chunk
2118 * @result 0 on success, -1 otherwise
2120 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2127 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2129 if (zclient
->sock
< 0)
2135 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2138 stream_putc(s
, zclient
->redist_default
);
2140 stream_putw(s
, zclient
->instance
);
2142 stream_putl(s
, start
);
2144 stream_putl(s
, end
);
2146 /* Put length at the first point of the stream. */
2147 stream_putw_at(s
, 0, stream_get_endp(s
));
2149 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2151 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2152 close(zclient
->sock
);
2157 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2158 close(zclient
->sock
);
2167 * Connect to table manager in a syncronous way
2169 * It first writes the request to zcient output buffer and then
2170 * immediately reads the answer from the input buffer.
2172 * @param zclient Zclient used to connect to table manager (zebra)
2173 * @result Result of response
2175 int tm_table_manager_connect(struct zclient
*zclient
)
2182 zlog_debug("Connecting to Table Manager");
2184 if (zclient
->sock
< 0)
2190 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2193 stream_putc(s
, zclient
->redist_default
);
2195 stream_putw(s
, zclient
->instance
);
2197 /* Put length at the first point of the stream. */
2198 stream_putw_at(s
, 0, stream_get_endp(s
));
2200 ret
= zclient_send_message(zclient
);
2205 zlog_debug("%s: Table manager connect request sent", __func__
);
2208 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2214 STREAM_GETC(s
, result
);
2217 "%s: Table Manager connect response received, result %u",
2226 * Function to request a table chunk in a syncronous way
2228 * It first writes the request to zclient output buffer and then
2229 * immediately reads the answer from the input buffer.
2231 * @param zclient Zclient used to connect to table manager (zebra)
2232 * @param chunk_size Amount of table requested
2233 * @param start to write first assigned chunk table RT ID to
2234 * @param end To write last assigned chunk table RT ID to
2235 * @result 0 on success, -1 otherwise
2237 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2238 uint32_t *start
, uint32_t *end
)
2244 zlog_debug("Getting Table Chunk");
2246 if (zclient
->sock
< 0)
2252 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2254 stream_putl(s
, chunk_size
);
2255 /* Put length at the first point of the stream. */
2256 stream_putw_at(s
, 0, stream_get_endp(s
));
2258 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2260 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2262 close(zclient
->sock
);
2267 flog_err(EC_LIB_ZAPI_SOCKET
,
2268 "%s: zclient->sock connection closed", __func__
);
2269 close(zclient
->sock
);
2274 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2278 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2282 /* start and end table IDs */
2283 STREAM_GETL(s
, *start
);
2284 STREAM_GETL(s
, *end
);
2287 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2294 * Function to release a table chunk
2296 * @param zclient Zclient used to connect to table manager (zebra)
2297 * @param start First label of table
2298 * @param end Last label of chunk
2299 * @result 0 on success, -1 otherwise
2301 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2307 zlog_debug("Releasing Table Chunk");
2309 if (zclient
->sock
< 0)
2315 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2318 stream_putl(s
, start
);
2320 stream_putl(s
, end
);
2322 /* Put length at the first point of the stream. */
2323 stream_putw_at(s
, 0, stream_get_endp(s
));
2325 return zclient_send_message(zclient
);
2329 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2337 zclient_create_header(s
, command
, VRF_DEFAULT
);
2338 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2339 stream_putl(s
, pw
->ifindex
);
2342 stream_putl(s
, pw
->type
);
2345 stream_putl(s
, pw
->af
);
2348 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2351 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2354 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2359 stream_putl(s
, pw
->local_label
);
2360 stream_putl(s
, pw
->remote_label
);
2363 stream_putc(s
, pw
->flags
);
2365 /* Protocol specific fields */
2366 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2368 /* Put length at the first point of the stream. */
2369 stream_putw_at(s
, 0, stream_get_endp(s
));
2371 return zclient_send_message(zclient
);
2375 * Receive PW status update from Zebra and send it to LDE process.
2377 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2378 zebra_size_t length
, vrf_id_t vrf_id
,
2379 struct zapi_pw_status
*pw
)
2383 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2387 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2388 pw
->ifindex
= stream_getl(s
);
2389 pw
->status
= stream_getl(s
);
2392 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2393 zebra_size_t length
, vrf_id_t vrf_id
)
2395 struct zclient_capabilities cap
;
2396 struct stream
*s
= zclient
->ibuf
;
2398 uint8_t mpls_enabled
;
2400 STREAM_GETL(s
, vrf_backend
);
2401 vrf_configure_backend(vrf_backend
);
2403 memset(&cap
, 0, sizeof(cap
));
2404 STREAM_GETC(s
, mpls_enabled
);
2405 cap
.mpls_enabled
= !!mpls_enabled
;
2406 STREAM_GETL(s
, cap
.ecmp
);
2407 STREAM_GETC(s
, cap
.role
);
2409 if (zclient
->zebra_capabilities
)
2410 (*zclient
->zebra_capabilities
)(&cap
);
2416 /* Zebra client message read function. */
2417 static int zclient_read(struct thread
*thread
)
2420 uint16_t length
, command
;
2421 uint8_t marker
, version
;
2423 struct zclient
*zclient
;
2425 /* Get socket to zebra. */
2426 zclient
= THREAD_ARG(thread
);
2427 zclient
->t_read
= NULL
;
2429 /* Read zebra header (if we don't have it already). */
2430 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2432 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2433 ZEBRA_HEADER_SIZE
- already
))
2438 "zclient connection closed socket [%d].",
2440 return zclient_failed(zclient
);
2442 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2443 /* Try again later. */
2444 zclient_event(ZCLIENT_READ
, zclient
);
2447 already
= ZEBRA_HEADER_SIZE
;
2450 /* Reset to read from the beginning of the incoming packet. */
2451 stream_set_getp(zclient
->ibuf
, 0);
2453 /* Fetch header values. */
2454 length
= stream_getw(zclient
->ibuf
);
2455 marker
= stream_getc(zclient
->ibuf
);
2456 version
= stream_getc(zclient
->ibuf
);
2457 vrf_id
= stream_getl(zclient
->ibuf
);
2458 command
= stream_getw(zclient
->ibuf
);
2460 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2462 EC_LIB_ZAPI_MISSMATCH
,
2463 "%s: socket %d version mismatch, marker %d, version %d",
2464 __func__
, zclient
->sock
, marker
, version
);
2465 return zclient_failed(zclient
);
2468 if (length
< ZEBRA_HEADER_SIZE
) {
2469 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2470 "%s: socket %d message length %u is less than %d ",
2471 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2472 return zclient_failed(zclient
);
2476 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2480 "%s: message size %u exceeds buffer size %lu, expanding...",
2482 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2483 ns
= stream_new(length
);
2484 stream_copy(ns
, zclient
->ibuf
);
2485 stream_free(zclient
->ibuf
);
2489 /* Read rest of zebra packet. */
2490 if (already
< length
) {
2492 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2498 "zclient connection closed socket [%d].",
2500 return zclient_failed(zclient
);
2502 if (nbyte
!= (ssize_t
)(length
- already
)) {
2503 /* Try again later. */
2504 zclient_event(ZCLIENT_READ
, zclient
);
2509 length
-= ZEBRA_HEADER_SIZE
;
2512 zlog_debug("zclient 0x%p command %s VRF %u",
2513 (void *)zclient
, zserv_command_string(command
),
2517 case ZEBRA_CAPABILITIES
:
2518 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2520 case ZEBRA_ROUTER_ID_UPDATE
:
2521 if (zclient
->router_id_update
)
2522 (*zclient
->router_id_update
)(command
, zclient
, length
,
2526 zclient_vrf_add(zclient
, vrf_id
);
2528 case ZEBRA_VRF_DELETE
:
2529 zclient_vrf_delete(zclient
, vrf_id
);
2531 case ZEBRA_INTERFACE_ADD
:
2532 if (zclient
->interface_add
)
2533 (*zclient
->interface_add
)(command
, zclient
, length
,
2536 case ZEBRA_INTERFACE_DELETE
:
2537 if (zclient
->interface_delete
)
2538 (*zclient
->interface_delete
)(command
, zclient
, length
,
2541 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2542 if (zclient
->interface_address_add
)
2543 (*zclient
->interface_address_add
)(command
, zclient
,
2546 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2547 if (zclient
->interface_address_delete
)
2548 (*zclient
->interface_address_delete
)(command
, zclient
,
2551 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2552 if (zclient
->interface_bfd_dest_update
)
2553 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2556 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2557 if (zclient
->interface_nbr_address_add
)
2558 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2561 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2562 if (zclient
->interface_nbr_address_delete
)
2563 (*zclient
->interface_nbr_address_delete
)(
2564 command
, zclient
, length
, vrf_id
);
2566 case ZEBRA_INTERFACE_UP
:
2567 if (zclient
->interface_up
)
2568 (*zclient
->interface_up
)(command
, zclient
, length
,
2571 case ZEBRA_INTERFACE_DOWN
:
2572 if (zclient
->interface_down
)
2573 (*zclient
->interface_down
)(command
, zclient
, length
,
2576 case ZEBRA_INTERFACE_VRF_UPDATE
:
2577 if (zclient
->interface_vrf_update
)
2578 (*zclient
->interface_vrf_update
)(command
, zclient
,
2581 case ZEBRA_NEXTHOP_UPDATE
:
2583 zlog_debug("zclient rcvd nexthop update");
2584 if (zclient
->nexthop_update
)
2585 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2588 case ZEBRA_IMPORT_CHECK_UPDATE
:
2590 zlog_debug("zclient rcvd import check update");
2591 if (zclient
->import_check_update
)
2592 (*zclient
->import_check_update
)(command
, zclient
,
2595 case ZEBRA_BFD_DEST_REPLAY
:
2596 if (zclient
->bfd_dest_replay
)
2597 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2600 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2601 if (zclient
->redistribute_route_add
)
2602 (*zclient
->redistribute_route_add
)(command
, zclient
,
2605 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2606 if (zclient
->redistribute_route_del
)
2607 (*zclient
->redistribute_route_del
)(command
, zclient
,
2610 case ZEBRA_INTERFACE_LINK_PARAMS
:
2611 if (zclient
->interface_link_params
)
2612 (*zclient
->interface_link_params
)(command
, zclient
,
2615 case ZEBRA_FEC_UPDATE
:
2617 zlog_debug("zclient rcvd fec update");
2618 if (zclient
->fec_update
)
2619 (*zclient
->fec_update
)(command
, zclient
, length
);
2621 case ZEBRA_LOCAL_ES_ADD
:
2622 if (zclient
->local_es_add
)
2623 (*zclient
->local_es_add
)(command
, zclient
, length
,
2626 case ZEBRA_LOCAL_ES_DEL
:
2627 if (zclient
->local_es_del
)
2628 (*zclient
->local_es_del
)(command
, zclient
, length
,
2632 if (zclient
->local_vni_add
)
2633 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2637 if (zclient
->local_vni_del
)
2638 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2641 case ZEBRA_L3VNI_ADD
:
2642 if (zclient
->local_l3vni_add
)
2643 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2646 case ZEBRA_L3VNI_DEL
:
2647 if (zclient
->local_l3vni_del
)
2648 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2651 case ZEBRA_MACIP_ADD
:
2652 if (zclient
->local_macip_add
)
2653 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2656 case ZEBRA_MACIP_DEL
:
2657 if (zclient
->local_macip_del
)
2658 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2661 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2662 if (zclient
->local_ip_prefix_add
)
2663 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2666 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2667 if (zclient
->local_ip_prefix_del
)
2668 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2671 case ZEBRA_PW_STATUS_UPDATE
:
2672 if (zclient
->pw_status_update
)
2673 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2676 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2677 if (zclient
->route_notify_owner
)
2678 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2681 case ZEBRA_RULE_NOTIFY_OWNER
:
2682 if (zclient
->rule_notify_owner
)
2683 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2686 case ZEBRA_GET_LABEL_CHUNK
:
2687 if (zclient
->label_chunk
)
2688 (*zclient
->label_chunk
)(command
, zclient
, length
,
2691 case ZEBRA_IPSET_NOTIFY_OWNER
:
2692 if (zclient
->ipset_notify_owner
)
2693 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2696 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2697 if (zclient
->ipset_entry_notify_owner
)
2698 (*zclient
->ipset_entry_notify_owner
)(command
,
2702 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2703 if (zclient
->iptable_notify_owner
)
2704 (*zclient
->iptable_notify_owner
)(command
,
2711 if (zclient
->sock
< 0)
2712 /* Connection was closed during packet processing. */
2715 /* Register read thread. */
2716 stream_reset(zclient
->ibuf
);
2717 zclient_event(ZCLIENT_READ
, zclient
);
2722 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2723 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2727 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2728 if (redist_check_instance(
2729 &zclient
->mi_redist
[afi
][type
], instance
))
2731 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2734 if (!redist_check_instance(
2735 &zclient
->mi_redist
[afi
][type
], instance
))
2737 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2742 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2743 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2746 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2748 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2751 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2755 if (zclient
->sock
> 0)
2756 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2761 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2762 afi_t afi
, vrf_id_t vrf_id
)
2765 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2766 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
2768 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
2770 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
2773 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
2776 if (zclient
->sock
> 0)
2777 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
2780 static void zclient_event(enum event event
, struct zclient
*zclient
)
2783 case ZCLIENT_SCHEDULE
:
2784 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2785 &zclient
->t_connect
);
2787 case ZCLIENT_CONNECT
:
2790 "zclient connect failures: %d schedule interval is now %d",
2791 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2792 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2793 zclient
->fail
< 3 ? 10 : 60,
2794 &zclient
->t_connect
);
2797 zclient
->t_read
= NULL
;
2798 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2799 zclient
->sock
, &zclient
->t_read
);
2804 void zclient_interface_set_master(struct zclient
*client
,
2805 struct interface
*master
,
2806 struct interface
*slave
)
2813 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2815 stream_putl(s
, master
->vrf_id
);
2816 stream_putl(s
, master
->ifindex
);
2817 stream_putl(s
, slave
->vrf_id
);
2818 stream_putl(s
, slave
->ifindex
);
2820 stream_putw_at(s
, 0, stream_get_endp(s
));
2821 zclient_send_message(client
);