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 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
41 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
43 /* Zebra client events. */
44 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
46 /* Prototype for event manager. */
47 static void zclient_event(enum event
, struct zclient
*);
49 struct sockaddr_storage zclient_addr
;
50 socklen_t zclient_addr_len
;
52 /* This file local debug flag. */
53 int zclient_debug
= 0;
55 struct zclient_options zclient_options_default
= { .receive_notify
= false };
57 /* Allocate zclient structure. */
58 struct zclient
*zclient_new_notify(struct thread_master
*master
,
59 struct zclient_options
*opt
)
61 struct zclient
*zclient
;
62 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
64 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
65 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
66 zclient
->wb
= buffer_new(0);
67 zclient
->master
= master
;
69 zclient
->receive_notify
= opt
->receive_notify
;
74 /* This function is only called when exiting, because
75 many parts of the code do not check for I/O errors, so they could
76 reference an invalid pointer if the structure was ever freed.
78 Free zclient structure. */
79 void zclient_free(struct zclient
*zclient
)
82 stream_free(zclient
->ibuf
);
84 stream_free(zclient
->obuf
);
86 buffer_free(zclient
->wb
);
88 XFREE(MTYPE_ZCLIENT
, zclient
);
91 u_short
*redist_check_instance(struct redist_proto
*red
, u_short instance
)
93 struct listnode
*node
;
99 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
106 void redist_add_instance(struct redist_proto
*red
, u_short instance
)
113 red
->instances
= list_new();
115 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(u_short
));
117 listnode_add(red
->instances
, in
);
120 void redist_del_instance(struct redist_proto
*red
, u_short instance
)
124 id
= redist_check_instance(red
, instance
);
128 listnode_delete(red
->instances
, id
);
129 XFREE(MTYPE_REDIST_INST
, id
);
130 if (!red
->instances
->count
) {
132 list_delete_and_null(&red
->instances
);
136 /* Stop zebra client services. */
137 void zclient_stop(struct zclient
*zclient
)
143 zlog_debug("zclient stopped");
146 THREAD_OFF(zclient
->t_read
);
147 THREAD_OFF(zclient
->t_connect
);
148 THREAD_OFF(zclient
->t_write
);
151 stream_reset(zclient
->ibuf
);
152 stream_reset(zclient
->obuf
);
154 /* Empty the write buffer. */
155 buffer_reset(zclient
->wb
);
158 if (zclient
->sock
>= 0) {
159 close(zclient
->sock
);
164 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
165 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
166 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
167 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
170 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
174 vrf_bitmap_free(zclient
->default_information
);
175 zclient
->default_information
= VRF_BITMAP_NULL
;
178 void zclient_reset(struct zclient
*zclient
)
182 zclient_stop(zclient
);
184 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
186 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
189 zclient_init(zclient
, zclient
->redist_default
,
190 zclient
->instance
, zclient
->privs
);
194 * Connect to zebra daemon.
195 * @param zclient a pointer to zclient structure
196 * @return socket fd just to make sure that connection established
198 * @see zclient_new_notify
200 int zclient_socket_connect(struct zclient
*zclient
)
205 /* We should think about IPv6 connection. */
206 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
212 zclient
->privs
->change(ZPRIVS_RAISE
);
213 setsockopt_so_sendbuf(sock
, 1048576);
214 zclient
->privs
->change(ZPRIVS_LOWER
);
216 /* Connect to zebra. */
217 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
,
221 zlog_warn("%s connect failure: %d(%s)",
222 __PRETTY_FUNCTION__
, errno
,
223 safe_strerror(errno
));
228 zclient
->sock
= sock
;
232 static int zclient_failed(struct zclient
*zclient
)
235 zclient_stop(zclient
);
236 zclient_event(ZCLIENT_CONNECT
, zclient
);
240 static int zclient_flush_data(struct thread
*thread
)
242 struct zclient
*zclient
= THREAD_ARG(thread
);
244 zclient
->t_write
= NULL
;
245 if (zclient
->sock
< 0)
247 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
250 "%s: buffer_flush_available failed on zclient fd %d, closing",
251 __func__
, zclient
->sock
);
252 return zclient_failed(zclient
);
255 zclient
->t_write
= NULL
;
256 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
257 zclient
->sock
, &zclient
->t_write
);
265 int zclient_send_message(struct zclient
*zclient
)
267 if (zclient
->sock
< 0)
269 switch (buffer_write(zclient
->wb
, zclient
->sock
,
270 STREAM_DATA(zclient
->obuf
),
271 stream_get_endp(zclient
->obuf
))) {
273 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
274 __func__
, zclient
->sock
);
275 return zclient_failed(zclient
);
278 THREAD_OFF(zclient
->t_write
);
281 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
282 zclient
->sock
, &zclient
->t_write
);
288 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
290 /* length placeholder, caller can update */
291 stream_putw(s
, ZEBRA_HEADER_SIZE
);
292 stream_putc(s
, ZEBRA_HEADER_MARKER
);
293 stream_putc(s
, ZSERV_VERSION
);
294 stream_putl(s
, vrf_id
);
295 stream_putw(s
, command
);
298 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
299 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
302 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
305 STREAM_GETW(s
, *size
);
306 *size
-= ZEBRA_HEADER_SIZE
;
307 STREAM_GETC(s
, *marker
);
308 STREAM_GETC(s
, *version
);
309 STREAM_GETL(s
, *vrf_id
);
310 STREAM_GETW(s
, *cmd
);
312 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
314 "%s: socket %d version mismatch, marker %d, version %d",
315 __func__
, sock
, *marker
, *version
);
319 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
326 /* Send simple Zebra message. */
327 static int zebra_message_send(struct zclient
*zclient
, int command
,
332 /* Get zclient output buffer. */
336 /* Send very simple command only Zebra message. */
337 zclient_create_header(s
, command
, vrf_id
);
339 return zclient_send_message(zclient
);
342 static int zebra_hello_send(struct zclient
*zclient
)
346 if (zclient
->redist_default
) {
350 /* The VRF ID in the HELLO message is always 0. */
351 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
352 stream_putc(s
, zclient
->redist_default
);
353 stream_putw(s
, zclient
->instance
);
354 if (zclient
->receive_notify
)
359 stream_putw_at(s
, 0, stream_get_endp(s
));
360 return zclient_send_message(zclient
);
366 /* Send register requests to zebra daemon for the information in a VRF. */
367 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
372 /* If not connected to the zebra yet. */
373 if (zclient
->sock
< 0)
377 zlog_debug("%s: send register messages for VRF %u", __func__
,
380 /* We need router-id information. */
381 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
383 /* We need interface information. */
384 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
386 /* Set unwanted redistribute route. */
387 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
388 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
391 /* Flush all redistribute request. */
392 if (vrf_id
== VRF_DEFAULT
)
393 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
394 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
395 if (zclient
->mi_redist
[afi
][i
].enabled
) {
396 struct listnode
*node
;
399 for (ALL_LIST_ELEMENTS_RO(
400 zclient
->mi_redist
[afi
][i
]
403 if (!(i
== zclient
->redist_default
404 && *id
== zclient
->instance
))
405 zebra_redistribute_send(
406 ZEBRA_REDISTRIBUTE_ADD
,
412 /* Flush all redistribute request. */
413 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
414 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
415 if (i
!= zclient
->redist_default
416 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
418 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
422 /* If default information is needed. */
423 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
424 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
428 /* Send unregister requests to zebra daemon for the information in a VRF. */
429 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
434 /* If not connected to the zebra yet. */
435 if (zclient
->sock
< 0)
439 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
442 /* We need router-id information. */
443 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
445 /* We need interface information. */
446 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
448 /* Set unwanted redistribute route. */
449 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
450 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
453 /* Flush all redistribute request. */
454 if (vrf_id
== VRF_DEFAULT
)
455 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
456 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
457 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_DELETE
,
474 /* Flush all redistribute request. */
475 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
476 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
477 if (i
!= zclient
->redist_default
478 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
480 zebra_redistribute_send(
481 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
484 /* If default information is needed. */
485 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
486 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
490 /* Send request to zebra daemon to start or stop RA. */
491 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
492 struct interface
*ifp
, int enable
,
497 /* If not connected to the zebra yet. */
498 if (zclient
->sock
< 0)
501 /* Form and send message. */
506 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
508 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
510 stream_putl(s
, ifp
->ifindex
);
511 stream_putl(s
, ra_interval
);
513 stream_putw_at(s
, 0, stream_get_endp(s
));
515 zclient_send_message(zclient
);
518 /* Make connection to zebra daemon. */
519 int zclient_start(struct zclient
*zclient
)
522 zlog_info("zclient_start is called");
524 /* If already connected to the zebra. */
525 if (zclient
->sock
>= 0)
528 /* Check connect thread. */
529 if (zclient
->t_connect
)
532 if (zclient_socket_connect(zclient
) < 0) {
534 zlog_debug("zclient connection fail");
536 zclient_event(ZCLIENT_CONNECT
, zclient
);
540 if (set_nonblocking(zclient
->sock
) < 0)
541 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
544 /* Clear fail count. */
547 zlog_debug("zclient connect success with socket [%d]",
550 /* Create read thread. */
551 zclient_event(ZCLIENT_READ
, zclient
);
553 zebra_hello_send(zclient
);
555 /* Inform the successful connection. */
556 if (zclient
->zebra_connected
)
557 (*zclient
->zebra_connected
)(zclient
);
562 /* Initialize zebra client. Argument redist_default is unwanted
563 redistribute route type. */
564 void zclient_init(struct zclient
*zclient
, int redist_default
,
565 u_short instance
, struct zebra_privs_t
*privs
)
569 /* Set -1 to the default socket value. */
571 zclient
->privs
= privs
;
573 /* Clear redistribution flags. */
574 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
575 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
576 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
578 /* Set unwanted redistribute route. bgpd does not need BGP route
580 zclient
->redist_default
= redist_default
;
581 zclient
->instance
= instance
;
582 /* Pending: make afi(s) an arg. */
583 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
584 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
587 /* Set default-information redistribute to zero. */
588 zclient
->default_information
= vrf_bitmap_init();
591 zlog_debug("zclient_start is called");
593 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
596 /* This function is a wrapper function for calling zclient_start from
597 timer or event thread. */
598 static int zclient_connect(struct thread
*t
)
600 struct zclient
*zclient
;
602 zclient
= THREAD_ARG(t
);
603 zclient
->t_connect
= NULL
;
606 zlog_debug("zclient_connect is called");
608 return zclient_start(zclient
);
612 * "xdr_encode"-like interface that allows daemon (client) to send
613 * a message to zebra server for a route that needs to be
614 * added/deleted to the kernel. Info about the route is specified
615 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
616 * the info down the zclient socket using the stream_* functions.
618 * The corresponding read ("xdr_decode") function on the server
619 * side is zread_ipv4_add()/zread_ipv4_delete().
621 * 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
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * | Length (2) | Command | Route Type |
624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625 * | ZEBRA Flags | Message Flags | Prefix length |
626 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * | Destination IPv4 Prefix for route |
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
634 * described, as per the Nexthop count. Each nexthop described as:
637 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
638 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639 * | IPv4 Nexthop address or Interface Index number |
640 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * Alternatively, if the route is a blackhole route, then Nexthop count
643 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
646 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
647 * infrastructure was built around the traditional (32-bit "gate OR
648 * ifindex") nexthop data unit. A special encoding can be used to feed
649 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
650 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
652 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
653 * - .nexthop_num == .ifindex_num
654 * - .nexthop and .ifindex are filled with gate and ifindex parts of
655 * each compound nexthop, both in the same order
657 * zapi_ipv4_route() will produce two nexthop data units for each such
658 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
659 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
661 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
664 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
667 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
669 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
671 * XXX: No attention paid to alignment.
673 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
674 struct zapi_ipv4
*api
)
684 /* Some checks for labeled-unicast. The current expectation is that each
685 * nexthop is accompanied by a label in the case of labeled-unicast.
687 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
688 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
689 /* We expect prefixes installed with labels and the number to
691 * the number of nexthops.
693 assert(api
->label_num
== api
->nexthop_num
);
696 zclient_create_header(s
, cmd
, api
->vrf_id
);
698 /* Put type and nexthop. */
699 stream_putc(s
, api
->type
);
700 stream_putw(s
, api
->instance
);
701 stream_putl(s
, api
->flags
);
702 stream_putc(s
, api
->message
);
703 stream_putw(s
, api
->safi
);
705 /* Put prefix information. */
706 psize
= PSIZE(p
->prefixlen
);
707 stream_putc(s
, p
->prefixlen
);
708 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
710 /* Nexthop, ifindex, distance and metric information. */
711 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
712 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
714 for (i
= 0; i
< api
->nexthop_num
; i
++) {
715 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
716 stream_put_in_addr(s
, api
->nexthop
[i
]);
717 /* For labeled-unicast, each nexthop is followed by
719 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
720 stream_putl(s
, api
->label
[i
]);
722 for (i
= 0; i
< api
->ifindex_num
; i
++) {
723 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
724 stream_putl(s
, api
->ifindex
[i
]);
728 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
729 stream_putc(s
, api
->distance
);
730 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
731 stream_putl(s
, api
->metric
);
732 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
733 stream_putl(s
, api
->tag
);
734 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
735 stream_putl(s
, api
->mtu
);
737 /* Put length at the first point of the stream. */
738 stream_putw_at(s
, 0, stream_get_endp(s
));
740 return zclient_send_message(zclient
);
743 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
744 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
754 /* Some checks for labeled-unicast. The current expectation is that each
755 * nexthop is accompanied by a label in the case of labeled-unicast.
757 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
758 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
759 /* We expect prefixes installed with labels and the number to
761 * the number of nexthops.
763 assert(api
->label_num
== api
->nexthop_num
);
766 zclient_create_header(s
, cmd
, api
->vrf_id
);
768 /* Put type and nexthop. */
769 stream_putc(s
, api
->type
);
770 stream_putw(s
, api
->instance
);
771 stream_putl(s
, api
->flags
);
772 stream_putc(s
, api
->message
);
773 stream_putw(s
, api
->safi
);
775 /* Put prefix information. */
776 psize
= PSIZE(p
->prefixlen
);
777 stream_putc(s
, p
->prefixlen
);
778 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
780 /* Nexthop, ifindex, distance and metric information. */
781 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
782 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
784 for (i
= 0; i
< api
->nexthop_num
; i
++) {
785 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
786 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
787 /* For labeled-unicast, each nexthop is followed by
789 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
790 stream_putl(s
, api
->label
[i
]);
792 for (i
= 0; i
< api
->ifindex_num
; i
++) {
793 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
794 stream_putl(s
, api
->ifindex
[i
]);
798 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
799 stream_putc(s
, api
->distance
);
800 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
801 stream_putl(s
, api
->metric
);
802 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
803 stream_putl(s
, api
->tag
);
804 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
805 stream_putl(s
, api
->mtu
);
807 /* Put length at the first point of the stream. */
808 stream_putw_at(s
, 0, stream_get_endp(s
));
810 return zclient_send_message(zclient
);
813 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
814 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
820 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
822 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
828 /* Some checks for labeled-unicast. The current expectation is that each
829 * nexthop is accompanied by a label in the case of labeled-unicast.
831 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
832 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
833 /* We expect prefixes installed with labels and the number to
835 * the number of nexthops.
837 assert(api
->label_num
== api
->nexthop_num
);
840 zclient_create_header(s
, cmd
, api
->vrf_id
);
842 /* Put type and nexthop. */
843 stream_putc(s
, api
->type
);
844 stream_putw(s
, api
->instance
);
845 stream_putl(s
, api
->flags
);
846 stream_putc(s
, api
->message
);
847 stream_putw(s
, api
->safi
);
849 /* Put prefix information. */
850 psize
= PSIZE(p
->prefixlen
);
851 stream_putc(s
, p
->prefixlen
);
852 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
854 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
855 psize
= PSIZE(src_p
->prefixlen
);
856 stream_putc(s
, src_p
->prefixlen
);
857 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
860 /* Nexthop, ifindex, distance and metric information. */
861 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
862 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
864 for (i
= 0; i
< api
->nexthop_num
; i
++) {
865 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
866 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
867 /* For labeled-unicast, each nexthop is followed by
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
870 stream_putl(s
, api
->label
[i
]);
872 for (i
= 0; i
< api
->ifindex_num
; i
++) {
873 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
874 stream_putl(s
, api
->ifindex
[i
]);
878 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
879 stream_putc(s
, api
->distance
);
880 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
881 stream_putl(s
, api
->metric
);
882 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
883 stream_putl(s
, api
->tag
);
884 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
885 stream_putl(s
, api
->mtu
);
887 /* Put length at the first point of the stream. */
888 stream_putw_at(s
, 0, stream_get_endp(s
));
890 return zclient_send_message(zclient
);
893 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
894 struct zapi_route
*api
)
896 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
898 return zclient_send_message(zclient
);
901 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
903 struct zapi_nexthop
*api_nh
;
908 zclient_create_header(s
, cmd
, api
->vrf_id
);
910 stream_putc(s
, api
->type
);
911 stream_putw(s
, api
->instance
);
912 stream_putl(s
, api
->flags
);
913 stream_putc(s
, api
->message
);
914 stream_putc(s
, api
->safi
);
915 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
916 stream_put(s
, &(api
->rmac
), sizeof(struct ethaddr
));
918 /* Put prefix information. */
919 stream_putc(s
, api
->prefix
.family
);
920 psize
= PSIZE(api
->prefix
.prefixlen
);
921 stream_putc(s
, api
->prefix
.prefixlen
);
922 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
924 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
925 psize
= PSIZE(api
->src_prefix
.prefixlen
);
926 stream_putc(s
, api
->src_prefix
.prefixlen
);
927 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
931 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
932 /* limit the number of nexthops if necessary */
933 if (api
->nexthop_num
> MULTIPATH_NUM
) {
934 char buf
[PREFIX2STR_BUFFER
];
936 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
938 "%s: prefix %s: can't encode %u nexthops "
940 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
944 stream_putw(s
, api
->nexthop_num
);
945 if (api
->nexthop_num
)
946 stream_putw(s
, api
->nh_vrf_id
);
948 for (i
= 0; i
< api
->nexthop_num
; i
++) {
949 api_nh
= &api
->nexthops
[i
];
951 stream_putc(s
, api_nh
->type
);
952 switch (api_nh
->type
) {
953 case NEXTHOP_TYPE_BLACKHOLE
:
954 stream_putc(s
, api_nh
->bh_type
);
956 case NEXTHOP_TYPE_IPV4
:
957 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
959 case NEXTHOP_TYPE_IPV4_IFINDEX
:
960 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
961 stream_putl(s
, api_nh
->ifindex
);
963 case NEXTHOP_TYPE_IFINDEX
:
964 stream_putl(s
, api_nh
->ifindex
);
966 case NEXTHOP_TYPE_IPV6
:
967 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
970 case NEXTHOP_TYPE_IPV6_IFINDEX
:
971 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
973 stream_putl(s
, api_nh
->ifindex
);
976 zlog_warn("%s: Specified Nexthop type %d does not exist",
977 __PRETTY_FUNCTION__
, api_nh
->type
);
981 /* MPLS labels for BGP-LU or Segment Routing */
982 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
983 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
984 char buf
[PREFIX2STR_BUFFER
];
985 prefix2str(&api
->prefix
, buf
,
988 "%s: prefix %s: can't encode "
989 "%u labels (maximum is %u)",
996 stream_putc(s
, api_nh
->label_num
);
997 stream_put(s
, &api_nh
->labels
[0],
999 * sizeof(mpls_label_t
));
1005 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1006 stream_putc(s
, api
->distance
);
1007 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1008 stream_putl(s
, api
->metric
);
1009 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1010 stream_putl(s
, api
->tag
);
1011 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1012 stream_putl(s
, api
->mtu
);
1014 /* Put length at the first point of the stream. */
1015 stream_putw_at(s
, 0, stream_get_endp(s
));
1020 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1022 struct zapi_nexthop
*api_nh
;
1025 memset(api
, 0, sizeof(*api
));
1027 /* Type, flags, message. */
1028 STREAM_GETC(s
, api
->type
);
1029 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1030 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1031 __PRETTY_FUNCTION__
, api
->type
);
1035 STREAM_GETW(s
, api
->instance
);
1036 STREAM_GETL(s
, api
->flags
);
1037 STREAM_GETC(s
, api
->message
);
1038 STREAM_GETC(s
, api
->safi
);
1039 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1040 stream_get(&(api
->rmac
), s
, sizeof(struct ethaddr
));
1043 STREAM_GETC(s
, api
->prefix
.family
);
1044 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1045 switch (api
->prefix
.family
) {
1047 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1048 zlog_warn("%s: V4 prefixlen is %d which should not be more than 32",
1049 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1054 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1055 zlog_warn("%s: v6 prefixlen is %d which should not be more than 128",
1056 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1061 zlog_warn("%s: Specified family %d is not v4 or v6",
1062 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1065 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1067 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1068 api
->src_prefix
.family
= AF_INET6
;
1069 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1070 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1071 zlog_warn("%s: SRC Prefix prefixlen received: %d is too large",
1072 __PRETTY_FUNCTION__
,
1073 api
->src_prefix
.prefixlen
);
1076 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1077 PSIZE(api
->src_prefix
.prefixlen
));
1079 if (api
->prefix
.family
!= AF_INET6
1080 || api
->src_prefix
.prefixlen
== 0) {
1081 zlog_warn("%s: SRC prefix specified in some manner that makes no sense",
1082 __PRETTY_FUNCTION__
);
1088 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1089 STREAM_GETW(s
, api
->nexthop_num
);
1090 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1091 zlog_warn("%s: invalid number of nexthops (%u)",
1092 __func__
, api
->nexthop_num
);
1096 if (api
->nexthop_num
)
1097 STREAM_GETW(s
, api
->nh_vrf_id
);
1099 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1100 api_nh
= &api
->nexthops
[i
];
1102 STREAM_GETC(s
, api_nh
->type
);
1103 switch (api_nh
->type
) {
1104 case NEXTHOP_TYPE_BLACKHOLE
:
1105 STREAM_GETC(s
, api_nh
->bh_type
);
1107 case NEXTHOP_TYPE_IPV4
:
1108 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1111 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1112 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1114 STREAM_GETL(s
, api_nh
->ifindex
);
1116 case NEXTHOP_TYPE_IFINDEX
:
1117 STREAM_GETL(s
, api_nh
->ifindex
);
1119 case NEXTHOP_TYPE_IPV6
:
1120 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1122 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1123 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1124 STREAM_GETL(s
, api_nh
->ifindex
);
1127 zlog_warn("%s: Specified nexthop type %d does not exist",
1128 __PRETTY_FUNCTION__
,
1133 /* MPLS labels for BGP-LU or Segment Routing */
1134 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1135 STREAM_GETC(s
, api_nh
->label_num
);
1137 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1139 "%s: invalid number of MPLS "
1141 __func__
, api_nh
->label_num
);
1145 STREAM_GET(&api_nh
->labels
[0], s
,
1147 * sizeof(mpls_label_t
));
1153 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1154 STREAM_GETC(s
, api
->distance
);
1155 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1156 STREAM_GETL(s
, api
->metric
);
1157 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1158 STREAM_GETL(s
, api
->tag
);
1159 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1160 STREAM_GETL(s
, api
->mtu
);
1166 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1167 enum zapi_route_notify_owner
*note
)
1169 STREAM_GET(note
, s
, sizeof(*note
));
1171 STREAM_GETC(s
, p
->family
);
1172 STREAM_GETC(s
, p
->prefixlen
);
1173 STREAM_GET(&p
->u
.prefix
, s
,
1174 PSIZE(p
->prefixlen
));
1183 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1184 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1185 * then set/unset redist[type] in the client handle (a struct zserv) for the
1188 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1189 int type
, u_short instance
, vrf_id_t vrf_id
)
1196 zclient_create_header(s
, command
, vrf_id
);
1197 stream_putc(s
, afi
);
1198 stream_putc(s
, type
);
1199 stream_putw(s
, instance
);
1201 stream_putw_at(s
, 0, stream_get_endp(s
));
1203 return zclient_send_message(zclient
);
1206 /* Get prefix in ZServ format; family should be filled in on prefix */
1207 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1209 size_t plen
= prefix_blen(p
);
1216 stream_get(&p
->u
.prefix
, s
, plen
);
1218 p
->prefixlen
= MIN(plen
* 8, c
);
1224 /* Router-id update from zebra daemon. */
1225 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1227 /* Fetch interface address. */
1228 STREAM_GETC(s
, rid
->family
);
1230 zclient_stream_get_prefix(s
, rid
);
1236 /* Interface addition from zebra daemon. */
1238 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1239 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1241 * 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
1242 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1248 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1252 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1255 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1257 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1259 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1261 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1266 * | Link Layer Type |
1267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1268 * | Harware Address Length |
1269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270 * | Hardware Address if HW lenght different from 0 |
1271 * | ... max INTERFACE_HWADDR_MAX |
1272 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1273 * | Link_params? | Whether a link-params follows: 1 or 0.
1274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1275 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1276 * | .... (struct if_link_params). |
1277 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1280 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1283 char vrfname_tmp
[VRF_NAMSIZ
];
1284 struct vrf_data data
;
1286 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1287 /* Read interface name. */
1288 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1290 /* Lookup/create vrf by vrf_id. */
1291 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1297 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1301 /* Lookup vrf by vrf_id. */
1302 vrf
= vrf_lookup_by_id(vrf_id
);
1305 * If a routing protocol doesn't know about a
1306 * vrf that is about to be deleted. There is
1307 * no point in attempting to delete it.
1315 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1317 struct interface
*ifp
;
1318 char ifname_tmp
[INTERFACE_NAMSIZ
];
1320 /* Read interface name. */
1321 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1323 /* Lookup/create interface by name. */
1324 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1326 zebra_interface_if_set_value(s
, ifp
);
1332 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1333 * from zebra server. The format of this message is the same as
1334 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1335 * comments for zebra_interface_add_read), except that no sockaddr_dl
1336 * is sent at the tail of the message.
1338 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1340 struct interface
*ifp
;
1341 char ifname_tmp
[INTERFACE_NAMSIZ
];
1343 /* Read interface name. */
1344 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1346 /* Lookup this by interface index. */
1347 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1349 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1350 ifname_tmp
, vrf_id
);
1354 zebra_interface_if_set_value(s
, ifp
);
1359 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1365 iflp
->lp_status
= stream_getl(s
);
1366 iflp
->te_metric
= stream_getl(s
);
1367 iflp
->max_bw
= stream_getf(s
);
1368 iflp
->max_rsv_bw
= stream_getf(s
);
1369 uint32_t bwclassnum
= stream_getl(s
);
1372 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1373 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1376 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1377 " - outdated library?",
1378 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1380 iflp
->admin_grp
= stream_getl(s
);
1381 iflp
->rmt_as
= stream_getl(s
);
1382 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1384 iflp
->av_delay
= stream_getl(s
);
1385 iflp
->min_delay
= stream_getl(s
);
1386 iflp
->max_delay
= stream_getl(s
);
1387 iflp
->delay_var
= stream_getl(s
);
1389 iflp
->pkt_loss
= stream_getf(s
);
1390 iflp
->res_bw
= stream_getf(s
);
1391 iflp
->ava_bw
= stream_getf(s
);
1392 iflp
->use_bw
= stream_getf(s
);
1395 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1397 struct if_link_params
*iflp
;
1402 ifindex
= stream_getl(s
);
1404 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1407 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1412 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1415 link_params_set_value(s
, iflp
);
1420 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1422 u_char link_params_status
= 0;
1424 /* Read interface's index. */
1425 if_set_index(ifp
, stream_getl(s
));
1426 ifp
->status
= stream_getc(s
);
1428 /* Read interface's value. */
1429 ifp
->flags
= stream_getq(s
);
1430 ifp
->ptm_enable
= stream_getc(s
);
1431 ifp
->ptm_status
= stream_getc(s
);
1432 ifp
->metric
= stream_getl(s
);
1433 ifp
->speed
= stream_getl(s
);
1434 ifp
->mtu
= stream_getl(s
);
1435 ifp
->mtu6
= stream_getl(s
);
1436 ifp
->bandwidth
= stream_getl(s
);
1437 ifp
->ll_type
= stream_getl(s
);
1438 ifp
->hw_addr_len
= stream_getl(s
);
1439 if (ifp
->hw_addr_len
)
1440 stream_get(ifp
->hw_addr
, s
,
1441 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1443 /* Read Traffic Engineering status */
1444 link_params_status
= stream_getc(s
);
1445 /* Then, Traffic Engineering parameters if any */
1446 if (link_params_status
) {
1447 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1448 link_params_set_value(s
, iflp
);
1452 size_t zebra_interface_link_params_write(struct stream
*s
,
1453 struct interface
*ifp
)
1456 struct if_link_params
*iflp
;
1459 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1462 iflp
= ifp
->link_params
;
1465 w
+= stream_putl(s
, iflp
->lp_status
);
1467 w
+= stream_putl(s
, iflp
->te_metric
);
1468 w
+= stream_putf(s
, iflp
->max_bw
);
1469 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1471 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1472 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1473 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1475 w
+= stream_putl(s
, iflp
->admin_grp
);
1476 w
+= stream_putl(s
, iflp
->rmt_as
);
1477 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1479 w
+= stream_putl(s
, iflp
->av_delay
);
1480 w
+= stream_putl(s
, iflp
->min_delay
);
1481 w
+= stream_putl(s
, iflp
->max_delay
);
1482 w
+= stream_putl(s
, iflp
->delay_var
);
1484 w
+= stream_putf(s
, iflp
->pkt_loss
);
1485 w
+= stream_putf(s
, iflp
->res_bw
);
1486 w
+= stream_putf(s
, iflp
->ava_bw
);
1487 w
+= stream_putf(s
, iflp
->use_bw
);
1493 * format of message for address additon is:
1497 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1498 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1507 * | ifc_flags | flags for connected address
1515 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1523 static int memconstant(const void *s
, int c
, size_t n
)
1525 const u_char
*p
= s
;
1534 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1538 struct interface
*ifp
;
1539 struct connected
*ifc
;
1540 struct prefix p
, d
, *dp
;
1544 memset(&p
, 0, sizeof(p
));
1545 memset(&d
, 0, sizeof(d
));
1547 /* Get interface index. */
1548 ifindex
= stream_getl(s
);
1551 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1553 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1554 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1560 ifc_flags
= stream_getc(s
);
1562 /* Fetch interface address. */
1563 d
.family
= p
.family
= stream_getc(s
);
1564 plen
= prefix_blen(&d
);
1566 zclient_stream_get_prefix(s
, &p
);
1568 /* Fetch destination address. */
1569 stream_get(&d
.u
.prefix
, s
, plen
);
1571 /* N.B. NULL destination pointers are encoded as all zeroes */
1572 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1574 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1575 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1577 /* N.B. NULL destination pointers are encoded as all
1579 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1582 ifc
->flags
= ifc_flags
;
1583 if (ifc
->destination
)
1584 ifc
->destination
->prefixlen
=
1585 ifc
->address
->prefixlen
;
1586 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1587 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1589 char buf
[PREFIX_STRLEN
];
1591 "warning: interface %s address %s "
1592 "with peer flag set, but no peer address!",
1593 ifp
->name
, prefix2str(ifc
->address
, buf
,
1595 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1599 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1600 ifc
= connected_delete_by_prefix(ifp
, &p
);
1607 * format of message for neighbor connected address is:
1611 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1612 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1627 * | addr_len | len of addr.
1630 struct nbr_connected
*
1631 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1633 unsigned int ifindex
;
1634 struct interface
*ifp
;
1636 struct nbr_connected
*ifc
;
1638 /* Get interface index. */
1639 ifindex
= stream_getl(s
);
1642 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1644 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1645 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1651 p
.family
= stream_getc(s
);
1652 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1653 p
.prefixlen
= stream_getc(s
);
1655 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1656 /* Currently only supporting P2P links, so any new RA source
1658 considered as the replacement of the previously learnt
1659 Link-Local address. */
1660 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1661 ifc
= nbr_connected_new();
1662 ifc
->address
= prefix_new();
1664 listnode_add(ifp
->nbr_connected
, ifc
);
1667 prefix_copy(ifc
->address
, &p
);
1669 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1671 ifc
= nbr_connected_check(ifp
, &p
);
1673 listnode_delete(ifp
->nbr_connected
, ifc
);
1679 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1681 vrf_id_t
*new_vrf_id
)
1683 unsigned int ifindex
;
1684 struct interface
*ifp
;
1687 /* Get interface index. */
1688 ifindex
= stream_getl(s
);
1690 /* Lookup interface. */
1691 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1693 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1698 /* Fetch new VRF Id. */
1699 new_id
= stream_getw(s
);
1701 *new_vrf_id
= new_id
;
1705 /* filter unwanted messages until the expected one arrives */
1706 static int zclient_read_sync_response(struct zclient
*zclient
,
1707 u_int16_t expected_cmd
)
1710 u_int16_t size
= -1;
1719 cmd
= expected_cmd
+ 1;
1720 while (ret
== 0 && cmd
!= expected_cmd
) {
1724 /* wait until response arrives */
1726 FD_SET(zclient
->sock
, &readfds
);
1727 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1728 if (!FD_ISSET(zclient
->sock
, &readfds
))
1731 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1732 &version
, &vrf_id
, &cmd
);
1734 zlog_debug("%s: Response (%d bytes) received", __func__
,
1738 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1745 * Connect to label manager in a syncronous way
1747 * It first writes the request to zcient output buffer and then
1748 * immediately reads the answer from the input buffer.
1750 * @param zclient Zclient used to connect to label manager (zebra)
1751 * @result Result of response
1753 int lm_label_manager_connect(struct zclient
*zclient
)
1760 zlog_debug("Connecting to Label Manager");
1762 if (zclient
->sock
< 0)
1768 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1771 stream_putc(s
, zclient
->redist_default
);
1773 stream_putw(s
, zclient
->instance
);
1775 /* Put length at the first point of the stream. */
1776 stream_putw_at(s
, 0, stream_get_endp(s
));
1778 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1780 zlog_err("%s: can't write to zclient->sock", __func__
);
1781 close(zclient
->sock
);
1786 zlog_err("%s: zclient->sock connection closed", __func__
);
1787 close(zclient
->sock
);
1792 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1796 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1802 result
= stream_getc(s
);
1805 "%s: Label Manager connect response received, result %u",
1812 * Function to request a label chunk in a syncronous way
1814 * It first writes the request to zlcient output buffer and then
1815 * immediately reads the answer from the input buffer.
1817 * @param zclient Zclient used to connect to label manager (zebra)
1818 * @param keep Avoid garbage collection
1819 * @param chunk_size Amount of labels requested
1820 * @param start To write first assigned chunk label to
1821 * @param end To write last assigned chunk label to
1822 * @result 0 on success, -1 otherwise
1824 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1825 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1829 u_char response_keep
;
1832 zlog_debug("Getting Label Chunk");
1834 if (zclient
->sock
< 0)
1840 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1842 stream_putc(s
, keep
);
1844 stream_putl(s
, chunk_size
);
1845 /* Put length at the first point of the stream. */
1846 stream_putw_at(s
, 0, stream_get_endp(s
));
1848 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1850 zlog_err("%s: can't write to zclient->sock", __func__
);
1851 close(zclient
->sock
);
1856 zlog_err("%s: zclient->sock connection closed", __func__
);
1857 close(zclient
->sock
);
1862 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1866 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1871 response_keep
= stream_getc(s
);
1872 /* start and end labels */
1873 *start
= stream_getl(s
);
1874 *end
= stream_getl(s
);
1876 /* not owning this response */
1877 if (keep
!= response_keep
) {
1879 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1880 __func__
, *start
, *end
, keep
, response_keep
);
1883 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1884 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1885 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1891 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1898 * Function to release a label chunk
1900 * @param zclient Zclient used to connect to label manager (zebra)
1901 * @param start First label of chunk
1902 * @param end Last label of chunk
1903 * @result 0 on success, -1 otherwise
1905 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1912 zlog_debug("Releasing Label Chunk");
1914 if (zclient
->sock
< 0)
1920 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1923 stream_putl(s
, start
);
1925 stream_putl(s
, end
);
1927 /* Put length at the first point of the stream. */
1928 stream_putw_at(s
, 0, stream_get_endp(s
));
1930 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1932 zlog_err("%s: can't write to zclient->sock", __func__
);
1933 close(zclient
->sock
);
1938 zlog_err("%s: zclient->sock connection closed", __func__
);
1939 close(zclient
->sock
);
1947 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1955 zclient_create_header(s
, command
, VRF_DEFAULT
);
1956 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1957 stream_putl(s
, pw
->ifindex
);
1960 stream_putl(s
, pw
->type
);
1963 stream_putl(s
, pw
->af
);
1966 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1969 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1972 zlog_err("%s: unknown af", __func__
);
1977 stream_putl(s
, pw
->local_label
);
1978 stream_putl(s
, pw
->remote_label
);
1981 stream_putc(s
, pw
->flags
);
1983 /* Protocol specific fields */
1984 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1986 /* Put length at the first point of the stream. */
1987 stream_putw_at(s
, 0, stream_get_endp(s
));
1989 return zclient_send_message(zclient
);
1993 * Receive PW status update from Zebra and send it to LDE process.
1995 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1996 zebra_size_t length
, vrf_id_t vrf_id
,
1997 struct zapi_pw_status
*pw
)
2001 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2005 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2006 pw
->ifindex
= stream_getl(s
);
2007 pw
->status
= stream_getl(s
);
2010 /* Zebra client message read function. */
2011 static int zclient_read(struct thread
*thread
)
2014 uint16_t length
, command
;
2015 uint8_t marker
, version
;
2017 struct zclient
*zclient
;
2019 /* Get socket to zebra. */
2020 zclient
= THREAD_ARG(thread
);
2021 zclient
->t_read
= NULL
;
2023 /* Read zebra header (if we don't have it already). */
2024 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2026 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2027 ZEBRA_HEADER_SIZE
- already
))
2032 "zclient connection closed socket [%d].",
2034 return zclient_failed(zclient
);
2036 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2037 /* Try again later. */
2038 zclient_event(ZCLIENT_READ
, zclient
);
2041 already
= ZEBRA_HEADER_SIZE
;
2044 /* Reset to read from the beginning of the incoming packet. */
2045 stream_set_getp(zclient
->ibuf
, 0);
2047 /* Fetch header values. */
2048 length
= stream_getw(zclient
->ibuf
);
2049 marker
= stream_getc(zclient
->ibuf
);
2050 version
= stream_getc(zclient
->ibuf
);
2051 vrf_id
= stream_getl(zclient
->ibuf
);
2052 command
= stream_getw(zclient
->ibuf
);
2054 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2056 "%s: socket %d version mismatch, marker %d, version %d",
2057 __func__
, zclient
->sock
, marker
, version
);
2058 return zclient_failed(zclient
);
2061 if (length
< ZEBRA_HEADER_SIZE
) {
2062 zlog_err("%s: socket %d message length %u is less than %d ",
2063 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2064 return zclient_failed(zclient
);
2068 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2071 "%s: message size %u exceeds buffer size %lu, expanding...",
2072 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
2073 ns
= stream_new(length
);
2074 stream_copy(ns
, zclient
->ibuf
);
2075 stream_free(zclient
->ibuf
);
2079 /* Read rest of zebra packet. */
2080 if (already
< length
) {
2082 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2088 "zclient connection closed socket [%d].",
2090 return zclient_failed(zclient
);
2092 if (nbyte
!= (ssize_t
)(length
- already
)) {
2093 /* Try again later. */
2094 zclient_event(ZCLIENT_READ
, zclient
);
2099 length
-= ZEBRA_HEADER_SIZE
;
2102 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2103 (void *)zclient
, command
, vrf_id
);
2106 case ZEBRA_ROUTER_ID_UPDATE
:
2107 if (zclient
->router_id_update
)
2108 (*zclient
->router_id_update
)(command
, zclient
, length
,
2112 zclient_vrf_add(zclient
, vrf_id
);
2114 case ZEBRA_VRF_DELETE
:
2115 zclient_vrf_delete(zclient
, vrf_id
);
2117 case ZEBRA_INTERFACE_ADD
:
2118 if (zclient
->interface_add
)
2119 (*zclient
->interface_add
)(command
, zclient
, length
,
2122 case ZEBRA_INTERFACE_DELETE
:
2123 if (zclient
->interface_delete
)
2124 (*zclient
->interface_delete
)(command
, zclient
, length
,
2127 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2128 if (zclient
->interface_address_add
)
2129 (*zclient
->interface_address_add
)(command
, zclient
,
2132 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2133 if (zclient
->interface_address_delete
)
2134 (*zclient
->interface_address_delete
)(command
, zclient
,
2137 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2138 if (zclient
->interface_bfd_dest_update
)
2139 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2142 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2143 if (zclient
->interface_nbr_address_add
)
2144 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2147 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2148 if (zclient
->interface_nbr_address_delete
)
2149 (*zclient
->interface_nbr_address_delete
)(
2150 command
, zclient
, length
, vrf_id
);
2152 case ZEBRA_INTERFACE_UP
:
2153 if (zclient
->interface_up
)
2154 (*zclient
->interface_up
)(command
, zclient
, length
,
2157 case ZEBRA_INTERFACE_DOWN
:
2158 if (zclient
->interface_down
)
2159 (*zclient
->interface_down
)(command
, zclient
, length
,
2162 case ZEBRA_INTERFACE_VRF_UPDATE
:
2163 if (zclient
->interface_vrf_update
)
2164 (*zclient
->interface_vrf_update
)(command
, zclient
,
2167 case ZEBRA_NEXTHOP_UPDATE
:
2169 zlog_debug("zclient rcvd nexthop update\n");
2170 if (zclient
->nexthop_update
)
2171 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2174 case ZEBRA_IMPORT_CHECK_UPDATE
:
2176 zlog_debug("zclient rcvd import check update\n");
2177 if (zclient
->import_check_update
)
2178 (*zclient
->import_check_update
)(command
, zclient
,
2181 case ZEBRA_BFD_DEST_REPLAY
:
2182 if (zclient
->bfd_dest_replay
)
2183 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2186 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2187 if (zclient
->redistribute_route_add
)
2188 (*zclient
->redistribute_route_add
)(command
, zclient
,
2191 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2192 if (zclient
->redistribute_route_del
)
2193 (*zclient
->redistribute_route_del
)(command
, zclient
,
2196 case ZEBRA_INTERFACE_LINK_PARAMS
:
2197 if (zclient
->interface_link_params
)
2198 (*zclient
->interface_link_params
)(command
, zclient
,
2201 case ZEBRA_FEC_UPDATE
:
2203 zlog_debug("zclient rcvd fec update\n");
2204 if (zclient
->fec_update
)
2205 (*zclient
->fec_update
)(command
, zclient
, length
);
2208 if (zclient
->local_vni_add
)
2209 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2213 if (zclient
->local_vni_del
)
2214 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2217 case ZEBRA_L3VNI_ADD
:
2218 if (zclient
->local_l3vni_add
)
2219 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2222 case ZEBRA_L3VNI_DEL
:
2223 if (zclient
->local_l3vni_del
)
2224 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2227 case ZEBRA_MACIP_ADD
:
2228 if (zclient
->local_macip_add
)
2229 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2232 case ZEBRA_MACIP_DEL
:
2233 if (zclient
->local_macip_del
)
2234 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2237 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2238 if (zclient
->local_ip_prefix_add
)
2239 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2242 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2243 if (zclient
->local_ip_prefix_del
)
2244 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2247 case ZEBRA_PW_STATUS_UPDATE
:
2248 if (zclient
->pw_status_update
)
2249 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2252 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2253 if (zclient
->notify_owner
)
2254 (*zclient
->notify_owner
)(command
, zclient
,
2261 if (zclient
->sock
< 0)
2262 /* Connection was closed during packet processing. */
2265 /* Register read thread. */
2266 stream_reset(zclient
->ibuf
);
2267 zclient_event(ZCLIENT_READ
, zclient
);
2272 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2273 int type
, u_short instance
, vrf_id_t vrf_id
)
2277 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2278 if (redist_check_instance(
2279 &zclient
->mi_redist
[afi
][type
], instance
))
2281 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2284 if (!redist_check_instance(
2285 &zclient
->mi_redist
[afi
][type
], instance
))
2287 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2292 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2293 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2296 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2298 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2301 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2305 if (zclient
->sock
> 0)
2306 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2311 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2315 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2316 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2318 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2320 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2322 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2325 if (zclient
->sock
> 0)
2326 zebra_message_send(zclient
, command
, vrf_id
);
2329 static void zclient_event(enum event event
, struct zclient
*zclient
)
2332 case ZCLIENT_SCHEDULE
:
2333 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2334 &zclient
->t_connect
);
2336 case ZCLIENT_CONNECT
:
2339 "zclient connect failures: %d schedule interval is now %d",
2340 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2341 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2342 zclient
->fail
< 3 ? 10 : 60,
2343 &zclient
->t_connect
);
2346 zclient
->t_read
= NULL
;
2347 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2348 zclient
->sock
, &zclient
->t_read
);
2353 void zclient_interface_set_master(struct zclient
*client
,
2354 struct interface
*master
,
2355 struct interface
*slave
)
2362 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2364 stream_putl(s
, master
->vrf_id
);
2365 stream_putl(s
, master
->ifindex
);
2366 stream_putl(s
, slave
->vrf_id
);
2367 stream_putl(s
, slave
->ifindex
);
2369 stream_putw_at(s
, 0, stream_get_endp(s
));
2370 zclient_send_message(client
);