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_notify(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_and_null(&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
],
178 vrf_bitmap_free(zclient
->default_information
);
179 zclient
->default_information
= 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
202 * @see zclient_new_notify
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_warn("%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
)) {
253 "%s: buffer_flush_available failed on zclient fd %d, closing",
254 __func__
, zclient
->sock
);
255 return zclient_failed(zclient
);
258 zclient
->t_write
= NULL
;
259 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
260 zclient
->sock
, &zclient
->t_write
);
268 int zclient_send_message(struct zclient
*zclient
)
270 if (zclient
->sock
< 0)
272 switch (buffer_write(zclient
->wb
, zclient
->sock
,
273 STREAM_DATA(zclient
->obuf
),
274 stream_get_endp(zclient
->obuf
))) {
276 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
277 __func__
, zclient
->sock
);
278 return zclient_failed(zclient
);
281 THREAD_OFF(zclient
->t_write
);
284 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
285 zclient
->sock
, &zclient
->t_write
);
291 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
293 /* length placeholder, caller can update */
294 stream_putw(s
, ZEBRA_HEADER_SIZE
);
295 stream_putc(s
, ZEBRA_HEADER_MARKER
);
296 stream_putc(s
, ZSERV_VERSION
);
297 stream_putl(s
, vrf_id
);
298 stream_putw(s
, command
);
301 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
302 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
305 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
308 STREAM_GETW(s
, *size
);
309 *size
-= ZEBRA_HEADER_SIZE
;
310 STREAM_GETC(s
, *marker
);
311 STREAM_GETC(s
, *version
);
312 STREAM_GETL(s
, *vrf_id
);
313 STREAM_GETW(s
, *cmd
);
315 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
316 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
317 "%s: socket %d version mismatch, marker %d, version %d",
318 __func__
, sock
, *marker
, *version
);
322 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
329 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
331 STREAM_GETW(zmsg
, hdr
->length
);
332 STREAM_GETC(zmsg
, hdr
->marker
);
333 STREAM_GETC(zmsg
, hdr
->version
);
334 STREAM_GETL(zmsg
, hdr
->vrf_id
);
335 STREAM_GETW(zmsg
, hdr
->command
);
341 /* Send simple Zebra message. */
342 static int zebra_message_send(struct zclient
*zclient
, int command
,
347 /* Get zclient output buffer. */
351 /* Send very simple command only Zebra message. */
352 zclient_create_header(s
, command
, vrf_id
);
354 return zclient_send_message(zclient
);
357 static int zebra_hello_send(struct zclient
*zclient
)
361 if (zclient
->redist_default
) {
365 /* The VRF ID in the HELLO message is always 0. */
366 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
367 stream_putc(s
, zclient
->redist_default
);
368 stream_putw(s
, zclient
->instance
);
369 if (zclient
->receive_notify
)
374 stream_putw_at(s
, 0, stream_get_endp(s
));
375 return zclient_send_message(zclient
);
381 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
382 mpls_label_t label
, enum lsp_types_t ltype
)
389 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
390 stream_putl(s
, label
);
392 stream_putc(s
, ltype
);
393 stream_putw_at(s
, 0, stream_get_endp(s
));
394 zclient_send_message(zclient
);
397 /* Send register requests to zebra daemon for the information in a VRF. */
398 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
403 /* If not connected to the zebra yet. */
404 if (zclient
->sock
< 0)
408 zlog_debug("%s: send register messages for VRF %u", __func__
,
411 /* We need router-id information. */
412 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
414 /* We need interface information. */
415 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
417 /* Set unwanted redistribute route. */
418 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
419 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
422 /* Flush all redistribute request. */
423 if (vrf_id
== VRF_DEFAULT
) {
424 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
425 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
426 if (!zclient
->mi_redist
[afi
][i
].enabled
)
429 struct listnode
*node
;
432 for (ALL_LIST_ELEMENTS_RO(
433 zclient
->mi_redist
[afi
][i
]
436 if (!(i
== zclient
->redist_default
437 && *id
== zclient
->instance
))
438 zebra_redistribute_send(
439 ZEBRA_REDISTRIBUTE_ADD
,
440 zclient
, afi
, i
, *id
,
446 /* Resend all redistribute request. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
448 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
449 if (i
!= zclient
->redist_default
450 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
452 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
456 /* If default information is needed. */
457 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
458 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
462 /* Send unregister requests to zebra daemon for the information in a VRF. */
463 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
468 /* If not connected to the zebra yet. */
469 if (zclient
->sock
< 0)
473 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
476 /* We need router-id information. */
477 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
479 /* We need interface information. */
480 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
482 /* Set unwanted redistribute route. */
483 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
484 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
487 /* Flush all redistribute request. */
488 if (vrf_id
== VRF_DEFAULT
) {
489 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
490 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
491 if (!zclient
->mi_redist
[afi
][i
].enabled
)
494 struct listnode
*node
;
497 for (ALL_LIST_ELEMENTS_RO(
498 zclient
->mi_redist
[afi
][i
]
501 if (!(i
== zclient
->redist_default
502 && *id
== zclient
->instance
))
503 zebra_redistribute_send(
504 ZEBRA_REDISTRIBUTE_DELETE
,
505 zclient
, afi
, i
, *id
,
511 /* Flush all redistribute request. */
512 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
513 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
514 if (i
!= zclient
->redist_default
515 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
517 zebra_redistribute_send(
518 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
521 /* If default information is needed. */
522 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
523 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
527 /* Send request to zebra daemon to start or stop RA. */
528 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
529 struct interface
*ifp
, int enable
,
534 /* If not connected to the zebra yet. */
535 if (zclient
->sock
< 0)
538 /* Form and send message. */
543 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
545 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
547 stream_putl(s
, ifp
->ifindex
);
548 stream_putl(s
, ra_interval
);
550 stream_putw_at(s
, 0, stream_get_endp(s
));
552 zclient_send_message(zclient
);
555 /* Make connection to zebra daemon. */
556 int zclient_start(struct zclient
*zclient
)
559 zlog_info("zclient_start is called");
561 /* If already connected to the zebra. */
562 if (zclient
->sock
>= 0)
565 /* Check connect thread. */
566 if (zclient
->t_connect
)
569 if (zclient_socket_connect(zclient
) < 0) {
571 zlog_debug("zclient connection fail");
573 zclient_event(ZCLIENT_CONNECT
, zclient
);
577 if (set_nonblocking(zclient
->sock
) < 0)
578 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
581 /* Clear fail count. */
584 zlog_debug("zclient connect success with socket [%d]",
587 /* Create read thread. */
588 zclient_event(ZCLIENT_READ
, zclient
);
590 zebra_hello_send(zclient
);
592 /* Inform the successful connection. */
593 if (zclient
->zebra_connected
)
594 (*zclient
->zebra_connected
)(zclient
);
599 /* Initialize zebra client. Argument redist_default is unwanted
600 redistribute route type. */
601 void zclient_init(struct zclient
*zclient
, int redist_default
,
602 unsigned short instance
, struct zebra_privs_t
*privs
)
606 /* Set -1 to the default socket value. */
608 zclient
->privs
= privs
;
610 /* Clear redistribution flags. */
611 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
612 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
613 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
615 /* Set unwanted redistribute route. bgpd does not need BGP route
617 zclient
->redist_default
= redist_default
;
618 zclient
->instance
= instance
;
619 /* Pending: make afi(s) an arg. */
620 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
621 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
624 /* Set default-information redistribute to zero. */
625 zclient
->default_information
= vrf_bitmap_init();
628 zlog_debug("zclient_start is called");
630 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
633 /* This function is a wrapper function for calling zclient_start from
634 timer or event thread. */
635 static int zclient_connect(struct thread
*t
)
637 struct zclient
*zclient
;
639 zclient
= THREAD_ARG(t
);
640 zclient
->t_connect
= NULL
;
643 zlog_debug("zclient_connect is called");
645 return zclient_start(zclient
);
648 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
649 bool exact_match
, vrf_id_t vrf_id
)
655 zclient_create_header(s
, command
, vrf_id
);
656 stream_putc(s
, (exact_match
) ? 1 : 0);
658 stream_putw(s
, PREFIX_FAMILY(p
));
659 stream_putc(s
, p
->prefixlen
);
660 switch (PREFIX_FAMILY(p
)) {
662 stream_put_in_addr(s
, &p
->u
.prefix4
);
665 stream_put(s
, &(p
->u
.prefix6
), 16);
670 stream_putw_at(s
, 0, stream_get_endp(s
));
672 return zclient_send_message(zclient
);
676 * "xdr_encode"-like interface that allows daemon (client) to send
677 * a message to zebra server for a route that needs to be
678 * added/deleted to the kernel. Info about the route is specified
679 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
680 * the info down the zclient socket using the stream_* functions.
682 * The corresponding read ("xdr_decode") function on the server
683 * side is zread_ipv4_add()/zread_ipv4_delete().
685 * 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
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | Length (2) | Command | Route Type |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | ZEBRA Flags | Message Flags | Prefix length |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | Destination IPv4 Prefix for route |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
698 * described, as per the Nexthop count. Each nexthop described as:
701 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * | IPv4 Nexthop address or Interface Index number |
704 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 * Alternatively, if the route is a blackhole route, then Nexthop count
707 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
710 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
711 * infrastructure was built around the traditional (32-bit "gate OR
712 * ifindex") nexthop data unit. A special encoding can be used to feed
713 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
714 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
716 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
717 * - .nexthop_num == .ifindex_num
718 * - .nexthop and .ifindex are filled with gate and ifindex parts of
719 * each compound nexthop, both in the same order
721 * zapi_ipv4_route() will produce two nexthop data units for each such
722 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
723 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
725 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
728 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
731 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
733 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
735 * XXX: No attention paid to alignment.
737 int zapi_ipv4_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
738 struct zapi_ipv4
*api
)
748 /* Some checks for labeled-unicast. The current expectation is that each
749 * nexthop is accompanied by a label in the case of labeled-unicast.
751 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
752 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
753 /* We expect prefixes installed with labels and the number to
755 * the number of nexthops.
757 assert(api
->label_num
== api
->nexthop_num
);
760 zclient_create_header(s
, cmd
, api
->vrf_id
);
762 /* Put type and nexthop. */
763 stream_putc(s
, api
->type
);
764 stream_putw(s
, api
->instance
);
765 stream_putl(s
, api
->flags
);
766 stream_putc(s
, api
->message
);
767 stream_putw(s
, api
->safi
);
769 /* Put prefix information. */
770 psize
= PSIZE(p
->prefixlen
);
771 stream_putc(s
, p
->prefixlen
);
772 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
774 /* Nexthop, ifindex, distance and metric information. */
775 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
776 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
778 for (i
= 0; i
< api
->nexthop_num
; i
++) {
779 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
780 stream_put_in_addr(s
, api
->nexthop
[i
]);
781 /* For labeled-unicast, each nexthop is followed by
783 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
784 stream_putl(s
, api
->label
[i
]);
786 for (i
= 0; i
< api
->ifindex_num
; i
++) {
787 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
788 stream_putl(s
, api
->ifindex
[i
]);
792 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
793 stream_putc(s
, api
->distance
);
794 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
795 stream_putl(s
, api
->metric
);
796 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
797 stream_putl(s
, api
->tag
);
798 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
799 stream_putl(s
, api
->mtu
);
801 /* Put length at the first point of the stream. */
802 stream_putw_at(s
, 0, stream_get_endp(s
));
804 return zclient_send_message(zclient
);
807 int zapi_ipv4_route_ipv6_nexthop(uint8_t cmd
, struct zclient
*zclient
,
808 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
818 /* Some checks for labeled-unicast. The current expectation is that each
819 * nexthop is accompanied by a label in the case of labeled-unicast.
821 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
822 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
823 /* We expect prefixes installed with labels and the number to
825 * the number of nexthops.
827 assert(api
->label_num
== api
->nexthop_num
);
830 zclient_create_header(s
, cmd
, api
->vrf_id
);
832 /* Put type and nexthop. */
833 stream_putc(s
, api
->type
);
834 stream_putw(s
, api
->instance
);
835 stream_putl(s
, api
->flags
);
836 stream_putc(s
, api
->message
);
837 stream_putw(s
, api
->safi
);
839 /* Put prefix information. */
840 psize
= PSIZE(p
->prefixlen
);
841 stream_putc(s
, p
->prefixlen
);
842 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
844 /* Nexthop, ifindex, distance and metric information. */
845 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
846 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
848 for (i
= 0; i
< api
->nexthop_num
; i
++) {
849 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
850 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
851 /* For labeled-unicast, each nexthop is followed by
853 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
854 stream_putl(s
, api
->label
[i
]);
856 for (i
= 0; i
< api
->ifindex_num
; i
++) {
857 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
858 stream_putl(s
, api
->ifindex
[i
]);
862 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
863 stream_putc(s
, api
->distance
);
864 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
865 stream_putl(s
, api
->metric
);
866 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
867 stream_putl(s
, api
->tag
);
868 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
869 stream_putl(s
, api
->mtu
);
871 /* Put length at the first point of the stream. */
872 stream_putw_at(s
, 0, stream_get_endp(s
));
874 return zclient_send_message(zclient
);
877 int zapi_ipv6_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
878 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
884 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
886 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
892 /* Some checks for labeled-unicast. The current expectation is that each
893 * nexthop is accompanied by a label in the case of labeled-unicast.
895 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
896 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
897 /* We expect prefixes installed with labels and the number to
899 * the number of nexthops.
901 assert(api
->label_num
== api
->nexthop_num
);
904 zclient_create_header(s
, cmd
, api
->vrf_id
);
906 /* Put type and nexthop. */
907 stream_putc(s
, api
->type
);
908 stream_putw(s
, api
->instance
);
909 stream_putl(s
, api
->flags
);
910 stream_putc(s
, api
->message
);
911 stream_putw(s
, api
->safi
);
913 /* Put prefix information. */
914 psize
= PSIZE(p
->prefixlen
);
915 stream_putc(s
, p
->prefixlen
);
916 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
918 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
919 psize
= PSIZE(src_p
->prefixlen
);
920 stream_putc(s
, src_p
->prefixlen
);
921 stream_write(s
, (uint8_t *)&src_p
->prefix
, psize
);
924 /* Nexthop, ifindex, distance and metric information. */
925 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
926 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
928 for (i
= 0; i
< api
->nexthop_num
; i
++) {
929 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
930 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
931 /* For labeled-unicast, each nexthop is followed by
933 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
934 stream_putl(s
, api
->label
[i
]);
936 for (i
= 0; i
< api
->ifindex_num
; i
++) {
937 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
938 stream_putl(s
, api
->ifindex
[i
]);
942 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
943 stream_putc(s
, api
->distance
);
944 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
945 stream_putl(s
, api
->metric
);
946 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
947 stream_putl(s
, api
->tag
);
948 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
949 stream_putl(s
, api
->mtu
);
951 /* Put length at the first point of the stream. */
952 stream_putw_at(s
, 0, stream_get_endp(s
));
954 return zclient_send_message(zclient
);
957 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
958 struct zapi_route
*api
)
960 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
962 return zclient_send_message(zclient
);
965 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
967 struct zapi_nexthop
*api_nh
;
972 zclient_create_header(s
, cmd
, api
->vrf_id
);
974 stream_putc(s
, api
->type
);
975 stream_putw(s
, api
->instance
);
976 stream_putl(s
, api
->flags
);
977 stream_putc(s
, api
->message
);
978 stream_putc(s
, api
->safi
);
980 /* Put prefix information. */
981 stream_putc(s
, api
->prefix
.family
);
982 psize
= PSIZE(api
->prefix
.prefixlen
);
983 stream_putc(s
, api
->prefix
.prefixlen
);
984 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
986 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
987 psize
= PSIZE(api
->src_prefix
.prefixlen
);
988 stream_putc(s
, api
->src_prefix
.prefixlen
);
989 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
993 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
994 /* limit the number of nexthops if necessary */
995 if (api
->nexthop_num
> MULTIPATH_NUM
) {
996 char buf
[PREFIX2STR_BUFFER
];
998 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1000 "%s: prefix %s: can't encode %u nexthops "
1002 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1006 stream_putw(s
, api
->nexthop_num
);
1008 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1009 api_nh
= &api
->nexthops
[i
];
1011 stream_putl(s
, api_nh
->vrf_id
);
1012 stream_putc(s
, api_nh
->type
);
1013 switch (api_nh
->type
) {
1014 case NEXTHOP_TYPE_BLACKHOLE
:
1015 stream_putc(s
, api_nh
->bh_type
);
1017 case NEXTHOP_TYPE_IPV4
:
1018 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1020 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1021 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1022 stream_putl(s
, api_nh
->ifindex
);
1024 case NEXTHOP_TYPE_IFINDEX
:
1025 stream_putl(s
, api_nh
->ifindex
);
1027 case NEXTHOP_TYPE_IPV6
:
1028 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1031 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1032 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1034 stream_putl(s
, api_nh
->ifindex
);
1038 "%s: Specified Nexthop type %d does not exist",
1039 __PRETTY_FUNCTION__
, api_nh
->type
);
1043 /* MPLS labels for BGP-LU or Segment Routing */
1044 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1045 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1046 char buf
[PREFIX2STR_BUFFER
];
1047 prefix2str(&api
->prefix
, buf
,
1049 flog_err(LIB_ERR_ZAPI_ENCODE
,
1050 "%s: prefix %s: can't encode "
1051 "%u labels (maximum is %u)",
1058 stream_putc(s
, api_nh
->label_num
);
1059 stream_put(s
, &api_nh
->labels
[0],
1061 * sizeof(mpls_label_t
));
1064 /* Router MAC for EVPN routes. */
1065 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1066 stream_put(s
, &(api_nh
->rmac
),
1067 sizeof(struct ethaddr
));
1072 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1073 stream_putc(s
, api
->distance
);
1074 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1075 stream_putl(s
, api
->metric
);
1076 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1077 stream_putl(s
, api
->tag
);
1078 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1079 stream_putl(s
, api
->mtu
);
1080 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1081 stream_putl(s
, api
->tableid
);
1083 /* Put length at the first point of the stream. */
1084 stream_putw_at(s
, 0, stream_get_endp(s
));
1089 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1091 struct zapi_nexthop
*api_nh
;
1094 memset(api
, 0, sizeof(*api
));
1096 /* Type, flags, message. */
1097 STREAM_GETC(s
, api
->type
);
1098 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1099 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1100 __PRETTY_FUNCTION__
, api
->type
);
1104 STREAM_GETW(s
, api
->instance
);
1105 STREAM_GETL(s
, api
->flags
);
1106 STREAM_GETC(s
, api
->message
);
1107 STREAM_GETC(s
, api
->safi
);
1110 STREAM_GETC(s
, api
->prefix
.family
);
1111 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1112 switch (api
->prefix
.family
) {
1114 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1116 "%s: V4 prefixlen is %d which should not be more than 32",
1117 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1122 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1124 "%s: v6 prefixlen is %d which should not be more than 128",
1125 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1130 zlog_warn("%s: Specified family %d is not v4 or v6",
1131 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1134 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1136 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1137 api
->src_prefix
.family
= AF_INET6
;
1138 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1139 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1141 "%s: SRC Prefix prefixlen received: %d is too large",
1142 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1145 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1146 PSIZE(api
->src_prefix
.prefixlen
));
1148 if (api
->prefix
.family
!= AF_INET6
1149 || api
->src_prefix
.prefixlen
== 0) {
1151 "%s: SRC prefix specified in some manner that makes no sense",
1152 __PRETTY_FUNCTION__
);
1158 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1159 STREAM_GETW(s
, api
->nexthop_num
);
1160 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1161 zlog_warn("%s: invalid number of nexthops (%u)",
1162 __func__
, api
->nexthop_num
);
1166 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1167 api_nh
= &api
->nexthops
[i
];
1169 STREAM_GETL(s
, api_nh
->vrf_id
);
1170 STREAM_GETC(s
, api_nh
->type
);
1171 switch (api_nh
->type
) {
1172 case NEXTHOP_TYPE_BLACKHOLE
:
1173 STREAM_GETC(s
, api_nh
->bh_type
);
1175 case NEXTHOP_TYPE_IPV4
:
1176 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1179 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1180 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1182 STREAM_GETL(s
, api_nh
->ifindex
);
1184 case NEXTHOP_TYPE_IFINDEX
:
1185 STREAM_GETL(s
, api_nh
->ifindex
);
1187 case NEXTHOP_TYPE_IPV6
:
1188 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1190 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1191 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1192 STREAM_GETL(s
, api_nh
->ifindex
);
1196 "%s: Specified nexthop type %d does not exist",
1197 __PRETTY_FUNCTION__
, api_nh
->type
);
1201 /* MPLS labels for BGP-LU or Segment Routing */
1202 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1203 STREAM_GETC(s
, api_nh
->label_num
);
1205 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1207 "%s: invalid number of MPLS "
1209 __func__
, api_nh
->label_num
);
1213 STREAM_GET(&api_nh
->labels
[0], s
,
1215 * sizeof(mpls_label_t
));
1218 /* Router MAC for EVPN routes. */
1219 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1220 stream_get(&(api_nh
->rmac
), s
,
1221 sizeof(struct ethaddr
));
1226 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1227 STREAM_GETC(s
, api
->distance
);
1228 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1229 STREAM_GETL(s
, api
->metric
);
1230 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1231 STREAM_GETL(s
, api
->tag
);
1232 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1233 STREAM_GETL(s
, api
->mtu
);
1234 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1235 STREAM_GETL(s
, api
->tableid
);
1241 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1247 memset(&any
, 0, sizeof(any
));
1248 any
.family
= family
;
1252 stream_putc(s
, p
->family
);
1253 stream_putc(s
, p
->prefixlen
);
1254 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1257 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1260 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1263 * We are sending one item at a time at the moment
1267 stream_putl(s
, zrule
->seq
);
1268 stream_putl(s
, zrule
->priority
);
1269 stream_putl(s
, zrule
->unique
);
1271 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1272 zrule
->filter
.src_ip
.family
);
1273 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1274 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1275 zrule
->filter
.src_ip
.family
);
1276 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1277 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1279 stream_putl(s
, zrule
->action
.table
);
1280 stream_putl(s
, zrule
->ifindex
);
1282 /* Put length at the first point of the stream. */
1283 stream_putw_at(s
, 0, stream_get_endp(s
));
1288 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1290 enum zapi_route_notify_owner
*note
)
1294 STREAM_GET(note
, s
, sizeof(*note
));
1296 STREAM_GETC(s
, p
->family
);
1297 STREAM_GETC(s
, p
->prefixlen
);
1298 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1309 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1310 uint32_t *priority
, uint32_t *unique
,
1312 enum zapi_rule_notify_owner
*note
)
1314 uint32_t prio
, seq
, uni
;
1317 STREAM_GET(note
, s
, sizeof(*note
));
1319 STREAM_GETL(s
, seq
);
1320 STREAM_GETL(s
, prio
);
1321 STREAM_GETL(s
, uni
);
1322 STREAM_GETL(s
, ifi
);
1325 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1338 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1339 enum zapi_ipset_notify_owner
*note
)
1343 STREAM_GET(note
, s
, sizeof(*note
));
1345 STREAM_GETL(s
, uni
);
1348 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1357 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1359 enum zapi_ipset_entry_notify_owner
*note
)
1363 STREAM_GET(note
, s
, sizeof(*note
));
1365 STREAM_GETL(s
, uni
);
1367 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1370 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1379 bool zapi_iptable_notify_decode(struct stream
*s
,
1381 enum zapi_iptable_notify_owner
*note
)
1385 STREAM_GET(note
, s
, sizeof(*note
));
1387 STREAM_GETL(s
, uni
);
1390 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1399 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1401 struct nexthop
*n
= nexthop_new();
1403 n
->type
= znh
->type
;
1404 n
->vrf_id
= znh
->vrf_id
;
1405 n
->ifindex
= znh
->ifindex
;
1406 n
->gate
= znh
->gate
;
1409 * This function currently handles labels
1411 if (znh
->label_num
) {
1412 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1419 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1423 memset(nhr
, 0, sizeof(*nhr
));
1425 STREAM_GETW(s
, nhr
->prefix
.family
);
1426 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1427 switch (nhr
->prefix
.family
) {
1429 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1432 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1438 STREAM_GETC(s
, nhr
->type
);
1439 STREAM_GETW(s
, nhr
->instance
);
1440 STREAM_GETC(s
, nhr
->distance
);
1441 STREAM_GETL(s
, nhr
->metric
);
1442 STREAM_GETC(s
, nhr
->nexthop_num
);
1444 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1445 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1446 switch (nhr
->nexthops
[i
].type
) {
1447 case NEXTHOP_TYPE_IPV4
:
1448 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1449 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1451 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1453 case NEXTHOP_TYPE_IFINDEX
:
1454 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1456 case NEXTHOP_TYPE_IPV6
:
1457 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1458 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1460 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1462 case NEXTHOP_TYPE_BLACKHOLE
:
1465 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1466 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1467 zlog_warn("%s: invalid number of MPLS labels (%u)",
1468 __func__
, nhr
->nexthops
[i
].label_num
);
1471 if (nhr
->nexthops
[i
].label_num
)
1472 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1473 nhr
->nexthops
[i
].label_num
1474 * sizeof(mpls_label_t
));
1483 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1484 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1485 * then set/unset redist[type] in the client handle (a struct zserv) for the
1488 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1489 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1496 zclient_create_header(s
, command
, vrf_id
);
1497 stream_putc(s
, afi
);
1498 stream_putc(s
, type
);
1499 stream_putw(s
, instance
);
1501 stream_putw_at(s
, 0, stream_get_endp(s
));
1503 return zclient_send_message(zclient
);
1506 /* Get prefix in ZServ format; family should be filled in on prefix */
1507 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1509 size_t plen
= prefix_blen(p
);
1516 stream_get(&p
->u
.prefix
, s
, plen
);
1518 p
->prefixlen
= MIN(plen
* 8, c
);
1524 /* Router-id update from zebra daemon. */
1525 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1527 /* Fetch interface address. */
1528 STREAM_GETC(s
, rid
->family
);
1530 zclient_stream_get_prefix(s
, rid
);
1536 /* Interface addition from zebra daemon. */
1538 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1539 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1541 * 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
1542 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1548 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1550 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1559 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1561 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1565 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1566 * | Link Layer Type |
1567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1568 * | Harware Address Length |
1569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1570 * | Hardware Address if HW lenght different from 0 |
1571 * | ... max INTERFACE_HWADDR_MAX |
1572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1573 * | Link_params? | Whether a link-params follows: 1 or 0.
1574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1575 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1576 * | .... (struct if_link_params). |
1577 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1580 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1583 char vrfname_tmp
[VRF_NAMSIZ
];
1584 struct vrf_data data
;
1586 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1587 /* Read interface name. */
1588 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1590 /* Lookup/create vrf by vrf_id. */
1591 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1592 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1593 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1597 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1601 /* Lookup vrf by vrf_id. */
1602 vrf
= vrf_lookup_by_id(vrf_id
);
1605 * If a routing protocol doesn't know about a
1606 * vrf that is about to be deleted. There is
1607 * no point in attempting to delete it.
1615 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1617 struct interface
*ifp
;
1618 char ifname_tmp
[INTERFACE_NAMSIZ
];
1620 /* Read interface name. */
1621 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1623 /* Lookup/create interface by name. */
1624 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1626 zebra_interface_if_set_value(s
, ifp
);
1632 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1633 * from zebra server. The format of this message is the same as
1634 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1635 * comments for zebra_interface_add_read), except that no sockaddr_dl
1636 * is sent at the tail of the message.
1638 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1640 struct interface
*ifp
;
1641 char ifname_tmp
[INTERFACE_NAMSIZ
];
1643 /* Read interface name. */
1644 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1646 /* Lookup this by interface index. */
1647 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1649 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1650 ifname_tmp
, vrf_id
);
1654 zebra_interface_if_set_value(s
, ifp
);
1659 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1665 iflp
->lp_status
= stream_getl(s
);
1666 iflp
->te_metric
= stream_getl(s
);
1667 iflp
->max_bw
= stream_getf(s
);
1668 iflp
->max_rsv_bw
= stream_getf(s
);
1669 uint32_t bwclassnum
= stream_getl(s
);
1672 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1673 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1675 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
1676 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1677 " - outdated library?",
1678 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1680 iflp
->admin_grp
= stream_getl(s
);
1681 iflp
->rmt_as
= stream_getl(s
);
1682 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1684 iflp
->av_delay
= stream_getl(s
);
1685 iflp
->min_delay
= stream_getl(s
);
1686 iflp
->max_delay
= stream_getl(s
);
1687 iflp
->delay_var
= stream_getl(s
);
1689 iflp
->pkt_loss
= stream_getf(s
);
1690 iflp
->res_bw
= stream_getf(s
);
1691 iflp
->ava_bw
= stream_getf(s
);
1692 iflp
->use_bw
= stream_getf(s
);
1695 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1697 struct if_link_params
*iflp
;
1702 ifindex
= stream_getl(s
);
1704 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1707 flog_err(LIB_ERR_ZAPI_ENCODE
,
1708 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1713 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1716 link_params_set_value(s
, iflp
);
1721 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1723 uint8_t link_params_status
= 0;
1724 ifindex_t old_ifindex
;
1726 old_ifindex
= ifp
->ifindex
;
1727 /* Read interface's index. */
1728 if_set_index(ifp
, stream_getl(s
));
1729 ifp
->status
= stream_getc(s
);
1731 /* Read interface's value. */
1732 ifp
->flags
= stream_getq(s
);
1733 ifp
->ptm_enable
= stream_getc(s
);
1734 ifp
->ptm_status
= stream_getc(s
);
1735 ifp
->metric
= stream_getl(s
);
1736 ifp
->speed
= stream_getl(s
);
1737 ifp
->mtu
= stream_getl(s
);
1738 ifp
->mtu6
= stream_getl(s
);
1739 ifp
->bandwidth
= stream_getl(s
);
1740 ifp
->ll_type
= stream_getl(s
);
1741 ifp
->hw_addr_len
= stream_getl(s
);
1742 if (ifp
->hw_addr_len
)
1743 stream_get(ifp
->hw_addr
, s
,
1744 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1746 /* Read Traffic Engineering status */
1747 link_params_status
= stream_getc(s
);
1748 /* Then, Traffic Engineering parameters if any */
1749 if (link_params_status
) {
1750 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1751 link_params_set_value(s
, iflp
);
1754 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1757 size_t zebra_interface_link_params_write(struct stream
*s
,
1758 struct interface
*ifp
)
1761 struct if_link_params
*iflp
;
1764 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1767 iflp
= ifp
->link_params
;
1770 w
+= stream_putl(s
, iflp
->lp_status
);
1772 w
+= stream_putl(s
, iflp
->te_metric
);
1773 w
+= stream_putf(s
, iflp
->max_bw
);
1774 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1776 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1777 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1778 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1780 w
+= stream_putl(s
, iflp
->admin_grp
);
1781 w
+= stream_putl(s
, iflp
->rmt_as
);
1782 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1784 w
+= stream_putl(s
, iflp
->av_delay
);
1785 w
+= stream_putl(s
, iflp
->min_delay
);
1786 w
+= stream_putl(s
, iflp
->max_delay
);
1787 w
+= stream_putl(s
, iflp
->delay_var
);
1789 w
+= stream_putf(s
, iflp
->pkt_loss
);
1790 w
+= stream_putf(s
, iflp
->res_bw
);
1791 w
+= stream_putf(s
, iflp
->ava_bw
);
1792 w
+= stream_putf(s
, iflp
->use_bw
);
1798 * format of message for address additon is:
1802 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1803 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1812 * | ifc_flags | flags for connected address
1820 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1828 static int memconstant(const void *s
, int c
, size_t n
)
1830 const uint8_t *p
= s
;
1839 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1843 struct interface
*ifp
;
1844 struct connected
*ifc
;
1845 struct prefix p
, d
, *dp
;
1849 memset(&p
, 0, sizeof(p
));
1850 memset(&d
, 0, sizeof(d
));
1852 /* Get interface index. */
1853 ifindex
= stream_getl(s
);
1856 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1858 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1859 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1865 ifc_flags
= stream_getc(s
);
1867 /* Fetch interface address. */
1868 d
.family
= p
.family
= stream_getc(s
);
1869 plen
= prefix_blen(&d
);
1871 zclient_stream_get_prefix(s
, &p
);
1873 /* Fetch destination address. */
1874 stream_get(&d
.u
.prefix
, s
, plen
);
1876 /* N.B. NULL destination pointers are encoded as all zeroes */
1877 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1879 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1880 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1882 /* N.B. NULL destination pointers are encoded as all
1884 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1887 ifc
->flags
= ifc_flags
;
1888 if (ifc
->destination
)
1889 ifc
->destination
->prefixlen
=
1890 ifc
->address
->prefixlen
;
1891 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1892 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1894 char buf
[PREFIX_STRLEN
];
1896 "warning: interface %s address %s "
1897 "with peer flag set, but no peer address!",
1899 prefix2str(ifc
->address
, buf
,
1901 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1905 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1906 ifc
= connected_delete_by_prefix(ifp
, &p
);
1913 * format of message for neighbor connected address is:
1917 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1918 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1933 * | addr_len | len of addr.
1936 struct nbr_connected
*
1937 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1939 unsigned int ifindex
;
1940 struct interface
*ifp
;
1942 struct nbr_connected
*ifc
;
1944 /* Get interface index. */
1945 ifindex
= stream_getl(s
);
1948 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1950 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1951 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1957 p
.family
= stream_getc(s
);
1958 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1959 p
.prefixlen
= stream_getc(s
);
1961 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1962 /* Currently only supporting P2P links, so any new RA source
1964 considered as the replacement of the previously learnt
1965 Link-Local address. */
1966 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1967 ifc
= nbr_connected_new();
1968 ifc
->address
= prefix_new();
1970 listnode_add(ifp
->nbr_connected
, ifc
);
1973 prefix_copy(ifc
->address
, &p
);
1975 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1977 ifc
= nbr_connected_check(ifp
, &p
);
1979 listnode_delete(ifp
->nbr_connected
, ifc
);
1985 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1987 vrf_id_t
*new_vrf_id
)
1989 unsigned int ifindex
;
1990 struct interface
*ifp
;
1993 /* Get interface index. */
1994 ifindex
= stream_getl(s
);
1996 /* Lookup interface. */
1997 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1999 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
2004 /* Fetch new VRF Id. */
2005 new_id
= stream_getw(s
);
2007 *new_vrf_id
= new_id
;
2011 /* filter unwanted messages until the expected one arrives */
2012 static int zclient_read_sync_response(struct zclient
*zclient
,
2013 uint16_t expected_cmd
)
2025 cmd
= expected_cmd
+ 1;
2026 while (ret
== 0 && cmd
!= expected_cmd
) {
2030 /* wait until response arrives */
2032 FD_SET(zclient
->sock
, &readfds
);
2033 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2034 if (!FD_ISSET(zclient
->sock
, &readfds
))
2037 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2038 &version
, &vrf_id
, &cmd
);
2040 zlog_debug("%s: Response (%d bytes) received", __func__
,
2044 flog_err(LIB_ERR_ZAPI_ENCODE
,
2045 "%s: Invalid Sync Message Reply", __func__
);
2052 * Connect to label manager in a syncronous way
2054 * It first writes the request to zcient output buffer and then
2055 * immediately reads the answer from the input buffer.
2057 * @param zclient Zclient used to connect to label manager (zebra)
2058 * @result Result of response
2060 int lm_label_manager_connect(struct zclient
*zclient
)
2067 zlog_debug("Connecting to Label Manager (LM)");
2069 if (zclient
->sock
< 0)
2075 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
2078 stream_putc(s
, zclient
->redist_default
);
2080 stream_putw(s
, zclient
->instance
);
2082 /* Put length at the first point of the stream. */
2083 stream_putw_at(s
, 0, stream_get_endp(s
));
2085 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2087 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
2088 close(zclient
->sock
);
2093 flog_err(LIB_ERR_ZAPI_SOCKET
, "Zclient sock closed");
2094 close(zclient
->sock
);
2099 zlog_debug("LM connect request sent (%d bytes)", ret
);
2102 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
2108 /* read instance and proto */
2109 uint8_t proto
= stream_getc(s
);
2110 uint16_t instance
= stream_getw(s
);
2113 if (proto
!= zclient
->redist_default
)
2114 flog_err(LIB_ERR_ZAPI_ENCODE
,
2115 "Wrong proto (%u) in LM connect response. Should be %u",
2116 proto
, zclient
->redist_default
);
2117 if (instance
!= zclient
->instance
)
2118 flog_err(LIB_ERR_ZAPI_ENCODE
,
2119 "Wrong instId (%u) in LM connect response. Should be %u",
2120 instance
, zclient
->instance
);
2123 result
= stream_getc(s
);
2125 zlog_debug("LM connect-response received, result %u", result
);
2131 * Asynchronous label chunk request
2133 * @param zclient Zclient used to connect to label manager (zebra)
2134 * @param keep Avoid garbage collection
2135 * @param chunk_size Amount of labels requested
2136 * @result 0 on success, -1 otherwise
2138 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2139 uint32_t chunk_size
)
2144 zlog_debug("Getting Label Chunk");
2146 if (zclient
->sock
< 0)
2152 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2154 stream_putc(s
, zclient
->redist_default
);
2156 stream_putw(s
, zclient
->instance
);
2157 stream_putc(s
, keep
);
2158 stream_putl(s
, chunk_size
);
2160 /* Put length at the first point of the stream. */
2161 stream_putw_at(s
, 0, stream_get_endp(s
));
2163 return zclient_send_message(zclient
);
2167 * Function to request a label chunk in a syncronous way
2169 * It first writes the request to zlcient output buffer and then
2170 * immediately reads the answer from the input buffer.
2172 * @param zclient Zclient used to connect to label manager (zebra)
2173 * @param keep Avoid garbage collection
2174 * @param chunk_size Amount of labels requested
2175 * @param start To write first assigned chunk label to
2176 * @param end To write last assigned chunk label to
2177 * @result 0 on success, -1 otherwise
2179 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2180 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2184 uint8_t response_keep
;
2187 zlog_debug("Getting Label Chunk");
2189 if (zclient
->sock
< 0)
2195 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2197 stream_putc(s
, zclient
->redist_default
);
2199 stream_putw(s
, zclient
->instance
);
2201 stream_putc(s
, keep
);
2203 stream_putl(s
, chunk_size
);
2204 /* Put length at the first point of the stream. */
2205 stream_putw_at(s
, 0, stream_get_endp(s
));
2207 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2209 flog_err(LIB_ERR_ZAPI_SOCKET
,
2210 "Can't write to zclient sock");
2211 close(zclient
->sock
);
2216 flog_err(LIB_ERR_ZAPI_SOCKET
,
2217 "Zclient sock closed");
2218 close(zclient
->sock
);
2223 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2226 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2229 /* parse response */
2232 /* read proto and instance */
2233 uint8_t proto
= stream_getc(s
);
2234 uint16_t instance
= stream_getw(s
);
2237 if (proto
!= zclient
->redist_default
)
2238 flog_err(LIB_ERR_ZAPI_ENCODE
,
2239 "Wrong proto (%u) in get chunk response. Should be %u",
2240 proto
, zclient
->redist_default
);
2241 if (instance
!= zclient
->instance
)
2242 flog_err(LIB_ERR_ZAPI_ENCODE
,
2243 "Wrong instId (%u) in get chunk response Should be %u",
2244 instance
, zclient
->instance
);
2247 response_keep
= stream_getc(s
);
2248 /* start and end labels */
2249 *start
= stream_getl(s
);
2250 *end
= stream_getl(s
);
2252 /* not owning this response */
2253 if (keep
!= response_keep
) {
2254 flog_err(LIB_ERR_ZAPI_ENCODE
,
2255 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2256 *start
, *end
, keep
, response_keep
);
2259 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2260 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2261 flog_err(LIB_ERR_ZAPI_ENCODE
,
2262 "Invalid Label chunk: %u - %u", *start
, *end
);
2267 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2274 * Function to release a label chunk
2276 * @param zclient Zclient used to connect to label manager (zebra)
2277 * @param start First label of chunk
2278 * @param end Last label of chunk
2279 * @result 0 on success, -1 otherwise
2281 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2288 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2290 if (zclient
->sock
< 0)
2296 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2299 stream_putc(s
, zclient
->redist_default
);
2301 stream_putw(s
, zclient
->instance
);
2303 stream_putl(s
, start
);
2305 stream_putl(s
, end
);
2307 /* Put length at the first point of the stream. */
2308 stream_putw_at(s
, 0, stream_get_endp(s
));
2310 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2312 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
2313 close(zclient
->sock
);
2318 flog_err(LIB_ERR_ZAPI_SOCKET
,
2319 "Zclient sock connection closed");
2320 close(zclient
->sock
);
2329 * Connect to table manager in a syncronous way
2331 * It first writes the request to zcient output buffer and then
2332 * immediately reads the answer from the input buffer.
2334 * @param zclient Zclient used to connect to table manager (zebra)
2335 * @result Result of response
2337 int tm_table_manager_connect(struct zclient
*zclient
)
2344 zlog_debug("Connecting to Table Manager");
2346 if (zclient
->sock
< 0)
2352 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2355 stream_putc(s
, zclient
->redist_default
);
2357 stream_putw(s
, zclient
->instance
);
2359 /* Put length at the first point of the stream. */
2360 stream_putw_at(s
, 0, stream_get_endp(s
));
2362 ret
= zclient_send_message(zclient
);
2367 zlog_debug("%s: Table manager connect request sent", __func__
);
2370 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2376 STREAM_GETC(s
, result
);
2379 "%s: Table Manager connect response received, result %u",
2388 * Function to request a table chunk in a syncronous way
2390 * It first writes the request to zclient output buffer and then
2391 * immediately reads the answer from the input buffer.
2393 * @param zclient Zclient used to connect to table manager (zebra)
2394 * @param chunk_size Amount of table requested
2395 * @param start to write first assigned chunk table RT ID to
2396 * @param end To write last assigned chunk table RT ID to
2397 * @result 0 on success, -1 otherwise
2399 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2400 uint32_t *start
, uint32_t *end
)
2406 zlog_debug("Getting Table Chunk");
2408 if (zclient
->sock
< 0)
2414 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2416 stream_putl(s
, chunk_size
);
2417 /* Put length at the first point of the stream. */
2418 stream_putw_at(s
, 0, stream_get_endp(s
));
2420 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2422 flog_err(LIB_ERR_ZAPI_SOCKET
,
2423 "%s: can't write to zclient->sock", __func__
);
2424 close(zclient
->sock
);
2429 flog_err(LIB_ERR_ZAPI_SOCKET
,
2430 "%s: zclient->sock connection closed", __func__
);
2431 close(zclient
->sock
);
2436 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2440 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2444 /* start and end table IDs */
2445 STREAM_GETL(s
, *start
);
2446 STREAM_GETL(s
, *end
);
2449 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2456 * Function to release a table chunk
2458 * @param zclient Zclient used to connect to table manager (zebra)
2459 * @param start First label of table
2460 * @param end Last label of chunk
2461 * @result 0 on success, -1 otherwise
2463 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2469 zlog_debug("Releasing Table Chunk");
2471 if (zclient
->sock
< 0)
2477 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2480 stream_putl(s
, start
);
2482 stream_putl(s
, end
);
2484 /* Put length at the first point of the stream. */
2485 stream_putw_at(s
, 0, stream_get_endp(s
));
2487 return zclient_send_message(zclient
);
2491 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2499 zclient_create_header(s
, command
, VRF_DEFAULT
);
2500 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2501 stream_putl(s
, pw
->ifindex
);
2504 stream_putl(s
, pw
->type
);
2507 stream_putl(s
, pw
->af
);
2510 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2513 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2516 flog_err(LIB_ERR_ZAPI_ENCODE
,
2517 "%s: unknown af", __func__
);
2522 stream_putl(s
, pw
->local_label
);
2523 stream_putl(s
, pw
->remote_label
);
2526 stream_putc(s
, pw
->flags
);
2528 /* Protocol specific fields */
2529 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2531 /* Put length at the first point of the stream. */
2532 stream_putw_at(s
, 0, stream_get_endp(s
));
2534 return zclient_send_message(zclient
);
2538 * Receive PW status update from Zebra and send it to LDE process.
2540 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2541 zebra_size_t length
, vrf_id_t vrf_id
,
2542 struct zapi_pw_status
*pw
)
2546 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2550 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2551 pw
->ifindex
= stream_getl(s
);
2552 pw
->status
= stream_getl(s
);
2555 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2556 zebra_size_t length
, vrf_id_t vrf_id
)
2558 struct zclient_capabilities cap
;
2559 struct stream
*s
= zclient
->ibuf
;
2560 uint8_t mpls_enabled
;
2562 memset(&cap
, 0, sizeof(cap
));
2563 STREAM_GETC(s
, mpls_enabled
);
2564 cap
.mpls_enabled
= !!mpls_enabled
;
2565 STREAM_GETL(s
, cap
.ecmp
);
2567 if (zclient
->zebra_capabilities
)
2568 (*zclient
->zebra_capabilities
)(&cap
);
2574 /* Zebra client message read function. */
2575 static int zclient_read(struct thread
*thread
)
2578 uint16_t length
, command
;
2579 uint8_t marker
, version
;
2581 struct zclient
*zclient
;
2583 /* Get socket to zebra. */
2584 zclient
= THREAD_ARG(thread
);
2585 zclient
->t_read
= NULL
;
2587 /* Read zebra header (if we don't have it already). */
2588 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2590 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2591 ZEBRA_HEADER_SIZE
- already
))
2596 "zclient connection closed socket [%d].",
2598 return zclient_failed(zclient
);
2600 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2601 /* Try again later. */
2602 zclient_event(ZCLIENT_READ
, zclient
);
2605 already
= ZEBRA_HEADER_SIZE
;
2608 /* Reset to read from the beginning of the incoming packet. */
2609 stream_set_getp(zclient
->ibuf
, 0);
2611 /* Fetch header values. */
2612 length
= stream_getw(zclient
->ibuf
);
2613 marker
= stream_getc(zclient
->ibuf
);
2614 version
= stream_getc(zclient
->ibuf
);
2615 vrf_id
= stream_getl(zclient
->ibuf
);
2616 command
= stream_getw(zclient
->ibuf
);
2618 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2619 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2620 "%s: socket %d version mismatch, marker %d, version %d",
2621 __func__
, zclient
->sock
, marker
, version
);
2622 return zclient_failed(zclient
);
2625 if (length
< ZEBRA_HEADER_SIZE
) {
2626 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2627 "%s: socket %d message length %u is less than %d ",
2628 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2629 return zclient_failed(zclient
);
2633 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2636 "%s: message size %u exceeds buffer size %lu, expanding...",
2638 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2639 ns
= stream_new(length
);
2640 stream_copy(ns
, zclient
->ibuf
);
2641 stream_free(zclient
->ibuf
);
2645 /* Read rest of zebra packet. */
2646 if (already
< length
) {
2648 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2654 "zclient connection closed socket [%d].",
2656 return zclient_failed(zclient
);
2658 if (nbyte
!= (ssize_t
)(length
- already
)) {
2659 /* Try again later. */
2660 zclient_event(ZCLIENT_READ
, zclient
);
2665 length
-= ZEBRA_HEADER_SIZE
;
2668 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2669 (void *)zclient
, command
, vrf_id
);
2672 case ZEBRA_CAPABILITIES
:
2673 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2675 case ZEBRA_ROUTER_ID_UPDATE
:
2676 if (zclient
->router_id_update
)
2677 (*zclient
->router_id_update
)(command
, zclient
, length
,
2681 zclient_vrf_add(zclient
, vrf_id
);
2683 case ZEBRA_VRF_DELETE
:
2684 zclient_vrf_delete(zclient
, vrf_id
);
2686 case ZEBRA_INTERFACE_ADD
:
2687 if (zclient
->interface_add
)
2688 (*zclient
->interface_add
)(command
, zclient
, length
,
2691 case ZEBRA_INTERFACE_DELETE
:
2692 if (zclient
->interface_delete
)
2693 (*zclient
->interface_delete
)(command
, zclient
, length
,
2696 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2697 if (zclient
->interface_address_add
)
2698 (*zclient
->interface_address_add
)(command
, zclient
,
2701 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2702 if (zclient
->interface_address_delete
)
2703 (*zclient
->interface_address_delete
)(command
, zclient
,
2706 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2707 if (zclient
->interface_bfd_dest_update
)
2708 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2711 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2712 if (zclient
->interface_nbr_address_add
)
2713 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2716 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2717 if (zclient
->interface_nbr_address_delete
)
2718 (*zclient
->interface_nbr_address_delete
)(
2719 command
, zclient
, length
, vrf_id
);
2721 case ZEBRA_INTERFACE_UP
:
2722 if (zclient
->interface_up
)
2723 (*zclient
->interface_up
)(command
, zclient
, length
,
2726 case ZEBRA_INTERFACE_DOWN
:
2727 if (zclient
->interface_down
)
2728 (*zclient
->interface_down
)(command
, zclient
, length
,
2731 case ZEBRA_INTERFACE_VRF_UPDATE
:
2732 if (zclient
->interface_vrf_update
)
2733 (*zclient
->interface_vrf_update
)(command
, zclient
,
2736 case ZEBRA_NEXTHOP_UPDATE
:
2738 zlog_debug("zclient rcvd nexthop update\n");
2739 if (zclient
->nexthop_update
)
2740 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2743 case ZEBRA_IMPORT_CHECK_UPDATE
:
2745 zlog_debug("zclient rcvd import check update\n");
2746 if (zclient
->import_check_update
)
2747 (*zclient
->import_check_update
)(command
, zclient
,
2750 case ZEBRA_BFD_DEST_REPLAY
:
2751 if (zclient
->bfd_dest_replay
)
2752 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2755 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2756 if (zclient
->redistribute_route_add
)
2757 (*zclient
->redistribute_route_add
)(command
, zclient
,
2760 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2761 if (zclient
->redistribute_route_del
)
2762 (*zclient
->redistribute_route_del
)(command
, zclient
,
2765 case ZEBRA_INTERFACE_LINK_PARAMS
:
2766 if (zclient
->interface_link_params
)
2767 (*zclient
->interface_link_params
)(command
, zclient
,
2770 case ZEBRA_FEC_UPDATE
:
2772 zlog_debug("zclient rcvd fec update\n");
2773 if (zclient
->fec_update
)
2774 (*zclient
->fec_update
)(command
, zclient
, length
);
2776 case ZEBRA_LOCAL_ES_ADD
:
2777 if (zclient
->local_es_add
)
2778 (*zclient
->local_es_add
)(command
, zclient
, length
,
2781 case ZEBRA_LOCAL_ES_DEL
:
2782 if (zclient
->local_es_del
)
2783 (*zclient
->local_es_del
)(command
, zclient
, length
,
2787 if (zclient
->local_vni_add
)
2788 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2792 if (zclient
->local_vni_del
)
2793 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2796 case ZEBRA_L3VNI_ADD
:
2797 if (zclient
->local_l3vni_add
)
2798 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2801 case ZEBRA_L3VNI_DEL
:
2802 if (zclient
->local_l3vni_del
)
2803 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2806 case ZEBRA_MACIP_ADD
:
2807 if (zclient
->local_macip_add
)
2808 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2811 case ZEBRA_MACIP_DEL
:
2812 if (zclient
->local_macip_del
)
2813 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2816 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2817 if (zclient
->local_ip_prefix_add
)
2818 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2821 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2822 if (zclient
->local_ip_prefix_del
)
2823 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2826 case ZEBRA_PW_STATUS_UPDATE
:
2827 if (zclient
->pw_status_update
)
2828 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2831 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2832 if (zclient
->route_notify_owner
)
2833 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2836 case ZEBRA_RULE_NOTIFY_OWNER
:
2837 if (zclient
->rule_notify_owner
)
2838 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2841 case ZEBRA_GET_LABEL_CHUNK
:
2842 if (zclient
->label_chunk
)
2843 (*zclient
->label_chunk
)(command
, zclient
, length
,
2846 case ZEBRA_IPSET_NOTIFY_OWNER
:
2847 if (zclient
->ipset_notify_owner
)
2848 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2851 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2852 if (zclient
->ipset_entry_notify_owner
)
2853 (*zclient
->ipset_entry_notify_owner
)(command
,
2857 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2858 if (zclient
->iptable_notify_owner
)
2859 (*zclient
->iptable_notify_owner
)(command
,
2866 if (zclient
->sock
< 0)
2867 /* Connection was closed during packet processing. */
2870 /* Register read thread. */
2871 stream_reset(zclient
->ibuf
);
2872 zclient_event(ZCLIENT_READ
, zclient
);
2877 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2878 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2882 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2883 if (redist_check_instance(
2884 &zclient
->mi_redist
[afi
][type
], instance
))
2886 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2889 if (!redist_check_instance(
2890 &zclient
->mi_redist
[afi
][type
], instance
))
2892 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2897 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2898 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2901 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2903 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2906 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2910 if (zclient
->sock
> 0)
2911 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2916 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2920 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2921 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2923 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2925 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2927 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2930 if (zclient
->sock
> 0)
2931 zebra_message_send(zclient
, command
, vrf_id
);
2934 static void zclient_event(enum event event
, struct zclient
*zclient
)
2937 case ZCLIENT_SCHEDULE
:
2938 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2939 &zclient
->t_connect
);
2941 case ZCLIENT_CONNECT
:
2944 "zclient connect failures: %d schedule interval is now %d",
2945 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2946 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2947 zclient
->fail
< 3 ? 10 : 60,
2948 &zclient
->t_connect
);
2951 zclient
->t_read
= NULL
;
2952 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2953 zclient
->sock
, &zclient
->t_read
);
2958 void zclient_interface_set_master(struct zclient
*client
,
2959 struct interface
*master
,
2960 struct interface
*slave
)
2967 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2969 stream_putl(s
, master
->vrf_id
);
2970 stream_putl(s
, master
->ifindex
);
2971 stream_putl(s
, slave
->vrf_id
);
2972 stream_putl(s
, slave
->ifindex
);
2974 stream_putw_at(s
, 0, stream_get_endp(s
));
2975 zclient_send_message(client
);