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 unsigned short *redist_check_instance(struct redist_proto
*red
,
92 unsigned short instance
)
94 struct listnode
*node
;
100 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
107 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
114 red
->instances
= list_new();
116 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
118 listnode_add(red
->instances
, in
);
121 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
125 id
= redist_check_instance(red
, instance
);
129 listnode_delete(red
->instances
, id
);
130 XFREE(MTYPE_REDIST_INST
, id
);
131 if (!red
->instances
->count
) {
133 list_delete_and_null(&red
->instances
);
137 /* Stop zebra client services. */
138 void zclient_stop(struct zclient
*zclient
)
144 zlog_debug("zclient stopped");
147 THREAD_OFF(zclient
->t_read
);
148 THREAD_OFF(zclient
->t_connect
);
149 THREAD_OFF(zclient
->t_write
);
152 stream_reset(zclient
->ibuf
);
153 stream_reset(zclient
->obuf
);
155 /* Empty the write buffer. */
156 buffer_reset(zclient
->wb
);
159 if (zclient
->sock
>= 0) {
160 close(zclient
->sock
);
165 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
166 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
167 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
168 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
171 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
175 vrf_bitmap_free(zclient
->default_information
);
176 zclient
->default_information
= VRF_BITMAP_NULL
;
179 void zclient_reset(struct zclient
*zclient
)
183 zclient_stop(zclient
);
185 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
187 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
190 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
195 * Connect to zebra daemon.
196 * @param zclient a pointer to zclient structure
197 * @return socket fd just to make sure that connection established
199 * @see zclient_new_notify
201 int zclient_socket_connect(struct zclient
*zclient
)
206 /* We should think about IPv6 connection. */
207 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
213 zclient
->privs
->change(ZPRIVS_RAISE
);
214 setsockopt_so_sendbuf(sock
, 1048576);
215 zclient
->privs
->change(ZPRIVS_LOWER
);
217 /* Connect to zebra. */
218 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
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
, uint16_t *size
,
299 uint8_t *marker
, uint8_t *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 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
328 STREAM_GETW(zmsg
, hdr
->length
);
329 STREAM_GETC(zmsg
, hdr
->marker
);
330 STREAM_GETC(zmsg
, hdr
->version
);
331 STREAM_GETL(zmsg
, hdr
->vrf_id
);
332 STREAM_GETW(zmsg
, hdr
->command
);
338 /* Send simple Zebra message. */
339 static int zebra_message_send(struct zclient
*zclient
, int command
,
344 /* Get zclient output buffer. */
348 /* Send very simple command only Zebra message. */
349 zclient_create_header(s
, command
, vrf_id
);
351 return zclient_send_message(zclient
);
354 static int zebra_hello_send(struct zclient
*zclient
)
358 if (zclient
->redist_default
) {
362 /* The VRF ID in the HELLO message is always 0. */
363 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
364 stream_putc(s
, zclient
->redist_default
);
365 stream_putw(s
, zclient
->instance
);
366 if (zclient
->receive_notify
)
371 stream_putw_at(s
, 0, stream_get_endp(s
));
372 return zclient_send_message(zclient
);
378 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
379 mpls_label_t label
, enum lsp_types_t ltype
)
386 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
387 stream_putl(s
, label
);
389 stream_putc(s
, ltype
);
390 stream_putw_at(s
, 0, stream_get_endp(s
));
391 zclient_send_message(zclient
);
394 /* Send register requests to zebra daemon for the information in a VRF. */
395 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
400 /* If not connected to the zebra yet. */
401 if (zclient
->sock
< 0)
405 zlog_debug("%s: send register messages for VRF %u", __func__
,
408 /* We need router-id information. */
409 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
411 /* We need interface information. */
412 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
414 /* Set unwanted redistribute route. */
415 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
416 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
419 /* Flush all redistribute request. */
420 if (vrf_id
== VRF_DEFAULT
) {
421 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
422 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
423 if (!zclient
->mi_redist
[afi
][i
].enabled
)
426 struct listnode
*node
;
429 for (ALL_LIST_ELEMENTS_RO(
430 zclient
->mi_redist
[afi
][i
]
433 if (!(i
== zclient
->redist_default
434 && *id
== zclient
->instance
))
435 zebra_redistribute_send(
436 ZEBRA_REDISTRIBUTE_ADD
,
437 zclient
, afi
, i
, *id
,
443 /* Resend all redistribute request. */
444 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
445 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
446 if (i
!= zclient
->redist_default
447 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
449 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
453 /* If default information is needed. */
454 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
455 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
459 /* Send unregister requests to zebra daemon for the information in a VRF. */
460 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
465 /* If not connected to the zebra yet. */
466 if (zclient
->sock
< 0)
470 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
473 /* We need router-id information. */
474 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
476 /* We need interface information. */
477 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
479 /* Set unwanted redistribute route. */
480 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
481 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
484 /* Flush all redistribute request. */
485 if (vrf_id
== VRF_DEFAULT
) {
486 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
487 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
488 if (!zclient
->mi_redist
[afi
][i
].enabled
)
491 struct listnode
*node
;
494 for (ALL_LIST_ELEMENTS_RO(
495 zclient
->mi_redist
[afi
][i
]
498 if (!(i
== zclient
->redist_default
499 && *id
== zclient
->instance
))
500 zebra_redistribute_send(
501 ZEBRA_REDISTRIBUTE_DELETE
,
502 zclient
, afi
, i
, *id
,
508 /* Flush all redistribute request. */
509 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
510 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
511 if (i
!= zclient
->redist_default
512 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
514 zebra_redistribute_send(
515 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
518 /* If default information is needed. */
519 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
520 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
524 /* Send request to zebra daemon to start or stop RA. */
525 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
526 struct interface
*ifp
, int enable
,
531 /* If not connected to the zebra yet. */
532 if (zclient
->sock
< 0)
535 /* Form and send message. */
540 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
542 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
544 stream_putl(s
, ifp
->ifindex
);
545 stream_putl(s
, ra_interval
);
547 stream_putw_at(s
, 0, stream_get_endp(s
));
549 zclient_send_message(zclient
);
552 /* Make connection to zebra daemon. */
553 int zclient_start(struct zclient
*zclient
)
556 zlog_info("zclient_start is called");
558 /* If already connected to the zebra. */
559 if (zclient
->sock
>= 0)
562 /* Check connect thread. */
563 if (zclient
->t_connect
)
566 if (zclient_socket_connect(zclient
) < 0) {
568 zlog_debug("zclient connection fail");
570 zclient_event(ZCLIENT_CONNECT
, zclient
);
574 if (set_nonblocking(zclient
->sock
) < 0)
575 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
578 /* Clear fail count. */
581 zlog_debug("zclient connect success with socket [%d]",
584 /* Create read thread. */
585 zclient_event(ZCLIENT_READ
, zclient
);
587 zebra_hello_send(zclient
);
589 /* Inform the successful connection. */
590 if (zclient
->zebra_connected
)
591 (*zclient
->zebra_connected
)(zclient
);
596 /* Initialize zebra client. Argument redist_default is unwanted
597 redistribute route type. */
598 void zclient_init(struct zclient
*zclient
, int redist_default
,
599 unsigned short instance
, struct zebra_privs_t
*privs
)
603 /* Set -1 to the default socket value. */
605 zclient
->privs
= privs
;
607 /* Clear redistribution flags. */
608 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
609 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
610 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
612 /* Set unwanted redistribute route. bgpd does not need BGP route
614 zclient
->redist_default
= redist_default
;
615 zclient
->instance
= instance
;
616 /* Pending: make afi(s) an arg. */
617 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
618 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
621 /* Set default-information redistribute to zero. */
622 zclient
->default_information
= vrf_bitmap_init();
625 zlog_debug("zclient_start is called");
627 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
630 /* This function is a wrapper function for calling zclient_start from
631 timer or event thread. */
632 static int zclient_connect(struct thread
*t
)
634 struct zclient
*zclient
;
636 zclient
= THREAD_ARG(t
);
637 zclient
->t_connect
= NULL
;
640 zlog_debug("zclient_connect is called");
642 return zclient_start(zclient
);
645 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
646 bool exact_match
, vrf_id_t vrf_id
)
652 zclient_create_header(s
, command
, vrf_id
);
653 stream_putc(s
, (exact_match
) ? 1 : 0);
655 stream_putw(s
, PREFIX_FAMILY(p
));
656 stream_putc(s
, p
->prefixlen
);
657 switch (PREFIX_FAMILY(p
)) {
659 stream_put_in_addr(s
, &p
->u
.prefix4
);
662 stream_put(s
, &(p
->u
.prefix6
), 16);
667 stream_putw_at(s
, 0, stream_get_endp(s
));
669 return zclient_send_message(zclient
);
673 * "xdr_encode"-like interface that allows daemon (client) to send
674 * a message to zebra server for a route that needs to be
675 * added/deleted to the kernel. Info about the route is specified
676 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
677 * the info down the zclient socket using the stream_* functions.
679 * The corresponding read ("xdr_decode") function on the server
680 * side is zread_ipv4_add()/zread_ipv4_delete().
682 * 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
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * | Length (2) | Command | Route Type |
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | ZEBRA Flags | Message Flags | Prefix length |
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 * | Destination IPv4 Prefix for route |
689 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
694 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
695 * described, as per the Nexthop count. Each nexthop described as:
698 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * | IPv4 Nexthop address or Interface Index number |
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * Alternatively, if the route is a blackhole route, then Nexthop count
704 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
707 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
708 * infrastructure was built around the traditional (32-bit "gate OR
709 * ifindex") nexthop data unit. A special encoding can be used to feed
710 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
711 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
713 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
714 * - .nexthop_num == .ifindex_num
715 * - .nexthop and .ifindex are filled with gate and ifindex parts of
716 * each compound nexthop, both in the same order
718 * zapi_ipv4_route() will produce two nexthop data units for each such
719 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
720 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
722 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
725 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
728 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
730 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
732 * XXX: No attention paid to alignment.
734 int zapi_ipv4_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
735 struct zapi_ipv4
*api
)
745 /* Some checks for labeled-unicast. The current expectation is that each
746 * nexthop is accompanied by a label in the case of labeled-unicast.
748 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
749 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
750 /* We expect prefixes installed with labels and the number to
752 * the number of nexthops.
754 assert(api
->label_num
== api
->nexthop_num
);
757 zclient_create_header(s
, cmd
, api
->vrf_id
);
759 /* Put type and nexthop. */
760 stream_putc(s
, api
->type
);
761 stream_putw(s
, api
->instance
);
762 stream_putl(s
, api
->flags
);
763 stream_putc(s
, api
->message
);
764 stream_putw(s
, api
->safi
);
766 /* Put prefix information. */
767 psize
= PSIZE(p
->prefixlen
);
768 stream_putc(s
, p
->prefixlen
);
769 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
771 /* Nexthop, ifindex, distance and metric information. */
772 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
773 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
775 for (i
= 0; i
< api
->nexthop_num
; i
++) {
776 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
777 stream_put_in_addr(s
, api
->nexthop
[i
]);
778 /* For labeled-unicast, each nexthop is followed by
780 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
781 stream_putl(s
, api
->label
[i
]);
783 for (i
= 0; i
< api
->ifindex_num
; i
++) {
784 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
785 stream_putl(s
, api
->ifindex
[i
]);
789 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
790 stream_putc(s
, api
->distance
);
791 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
792 stream_putl(s
, api
->metric
);
793 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
794 stream_putl(s
, api
->tag
);
795 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
796 stream_putl(s
, api
->mtu
);
798 /* Put length at the first point of the stream. */
799 stream_putw_at(s
, 0, stream_get_endp(s
));
801 return zclient_send_message(zclient
);
804 int zapi_ipv4_route_ipv6_nexthop(uint8_t cmd
, struct zclient
*zclient
,
805 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
815 /* Some checks for labeled-unicast. The current expectation is that each
816 * nexthop is accompanied by a label in the case of labeled-unicast.
818 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
819 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
820 /* We expect prefixes installed with labels and the number to
822 * the number of nexthops.
824 assert(api
->label_num
== api
->nexthop_num
);
827 zclient_create_header(s
, cmd
, api
->vrf_id
);
829 /* Put type and nexthop. */
830 stream_putc(s
, api
->type
);
831 stream_putw(s
, api
->instance
);
832 stream_putl(s
, api
->flags
);
833 stream_putc(s
, api
->message
);
834 stream_putw(s
, api
->safi
);
836 /* Put prefix information. */
837 psize
= PSIZE(p
->prefixlen
);
838 stream_putc(s
, p
->prefixlen
);
839 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
841 /* Nexthop, ifindex, distance and metric information. */
842 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
843 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
845 for (i
= 0; i
< api
->nexthop_num
; i
++) {
846 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
847 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
848 /* For labeled-unicast, each nexthop is followed by
850 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
851 stream_putl(s
, api
->label
[i
]);
853 for (i
= 0; i
< api
->ifindex_num
; i
++) {
854 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
855 stream_putl(s
, api
->ifindex
[i
]);
859 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
860 stream_putc(s
, api
->distance
);
861 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
862 stream_putl(s
, api
->metric
);
863 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
864 stream_putl(s
, api
->tag
);
865 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
866 stream_putl(s
, api
->mtu
);
868 /* Put length at the first point of the stream. */
869 stream_putw_at(s
, 0, stream_get_endp(s
));
871 return zclient_send_message(zclient
);
874 int zapi_ipv6_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
875 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
881 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
883 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
889 /* Some checks for labeled-unicast. The current expectation is that each
890 * nexthop is accompanied by a label in the case of labeled-unicast.
892 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
893 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
894 /* We expect prefixes installed with labels and the number to
896 * the number of nexthops.
898 assert(api
->label_num
== api
->nexthop_num
);
901 zclient_create_header(s
, cmd
, api
->vrf_id
);
903 /* Put type and nexthop. */
904 stream_putc(s
, api
->type
);
905 stream_putw(s
, api
->instance
);
906 stream_putl(s
, api
->flags
);
907 stream_putc(s
, api
->message
);
908 stream_putw(s
, api
->safi
);
910 /* Put prefix information. */
911 psize
= PSIZE(p
->prefixlen
);
912 stream_putc(s
, p
->prefixlen
);
913 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
915 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
916 psize
= PSIZE(src_p
->prefixlen
);
917 stream_putc(s
, src_p
->prefixlen
);
918 stream_write(s
, (uint8_t *)&src_p
->prefix
, psize
);
921 /* Nexthop, ifindex, distance and metric information. */
922 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
923 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
925 for (i
= 0; i
< api
->nexthop_num
; i
++) {
926 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
927 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
928 /* For labeled-unicast, each nexthop is followed by
930 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
931 stream_putl(s
, api
->label
[i
]);
933 for (i
= 0; i
< api
->ifindex_num
; i
++) {
934 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
935 stream_putl(s
, api
->ifindex
[i
]);
939 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
940 stream_putc(s
, api
->distance
);
941 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
942 stream_putl(s
, api
->metric
);
943 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
944 stream_putl(s
, api
->tag
);
945 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
946 stream_putl(s
, api
->mtu
);
948 /* Put length at the first point of the stream. */
949 stream_putw_at(s
, 0, stream_get_endp(s
));
951 return zclient_send_message(zclient
);
954 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
955 struct zapi_route
*api
)
957 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
959 return zclient_send_message(zclient
);
962 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
964 struct zapi_nexthop
*api_nh
;
969 zclient_create_header(s
, cmd
, api
->vrf_id
);
971 stream_putc(s
, api
->type
);
972 stream_putw(s
, api
->instance
);
973 stream_putl(s
, api
->flags
);
974 stream_putc(s
, api
->message
);
975 stream_putc(s
, api
->safi
);
976 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
977 stream_put(s
, &(api
->rmac
), sizeof(struct ethaddr
));
979 /* Put prefix information. */
980 stream_putc(s
, api
->prefix
.family
);
981 psize
= PSIZE(api
->prefix
.prefixlen
);
982 stream_putc(s
, api
->prefix
.prefixlen
);
983 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
985 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
986 psize
= PSIZE(api
->src_prefix
.prefixlen
);
987 stream_putc(s
, api
->src_prefix
.prefixlen
);
988 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
992 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
993 /* limit the number of nexthops if necessary */
994 if (api
->nexthop_num
> MULTIPATH_NUM
) {
995 char buf
[PREFIX2STR_BUFFER
];
997 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
999 "%s: prefix %s: can't encode %u nexthops "
1001 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1005 stream_putw(s
, api
->nexthop_num
);
1007 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1008 api_nh
= &api
->nexthops
[i
];
1010 stream_putl(s
, api_nh
->vrf_id
);
1011 stream_putc(s
, api_nh
->type
);
1012 switch (api_nh
->type
) {
1013 case NEXTHOP_TYPE_BLACKHOLE
:
1014 stream_putc(s
, api_nh
->bh_type
);
1016 case NEXTHOP_TYPE_IPV4
:
1017 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1019 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1020 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1021 stream_putl(s
, api_nh
->ifindex
);
1023 case NEXTHOP_TYPE_IFINDEX
:
1024 stream_putl(s
, api_nh
->ifindex
);
1026 case NEXTHOP_TYPE_IPV6
:
1027 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1030 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1031 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1033 stream_putl(s
, api_nh
->ifindex
);
1037 "%s: Specified Nexthop type %d does not exist",
1038 __PRETTY_FUNCTION__
, api_nh
->type
);
1042 /* MPLS labels for BGP-LU or Segment Routing */
1043 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1044 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1045 char buf
[PREFIX2STR_BUFFER
];
1046 prefix2str(&api
->prefix
, buf
,
1049 "%s: prefix %s: can't encode "
1050 "%u labels (maximum is %u)",
1057 stream_putc(s
, api_nh
->label_num
);
1058 stream_put(s
, &api_nh
->labels
[0],
1060 * sizeof(mpls_label_t
));
1066 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1067 stream_putc(s
, api
->distance
);
1068 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1069 stream_putl(s
, api
->metric
);
1070 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1071 stream_putl(s
, api
->tag
);
1072 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1073 stream_putl(s
, api
->mtu
);
1074 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1075 stream_putl(s
, api
->tableid
);
1077 /* Put length at the first point of the stream. */
1078 stream_putw_at(s
, 0, stream_get_endp(s
));
1083 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1085 struct zapi_nexthop
*api_nh
;
1088 memset(api
, 0, sizeof(*api
));
1090 /* Type, flags, message. */
1091 STREAM_GETC(s
, api
->type
);
1092 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1093 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1094 __PRETTY_FUNCTION__
, api
->type
);
1098 STREAM_GETW(s
, api
->instance
);
1099 STREAM_GETL(s
, api
->flags
);
1100 STREAM_GETC(s
, api
->message
);
1101 STREAM_GETC(s
, api
->safi
);
1102 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1103 STREAM_GET(&(api
->rmac
), s
, sizeof(struct ethaddr
));
1106 STREAM_GETC(s
, api
->prefix
.family
);
1107 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1108 switch (api
->prefix
.family
) {
1110 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1112 "%s: V4 prefixlen is %d which should not be more than 32",
1113 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1118 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1120 "%s: v6 prefixlen is %d which should not be more than 128",
1121 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1126 zlog_warn("%s: Specified family %d is not v4 or v6",
1127 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1130 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1132 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1133 api
->src_prefix
.family
= AF_INET6
;
1134 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1135 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1137 "%s: SRC Prefix prefixlen received: %d is too large",
1138 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1141 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1142 PSIZE(api
->src_prefix
.prefixlen
));
1144 if (api
->prefix
.family
!= AF_INET6
1145 || api
->src_prefix
.prefixlen
== 0) {
1147 "%s: SRC prefix specified in some manner that makes no sense",
1148 __PRETTY_FUNCTION__
);
1154 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1155 STREAM_GETW(s
, api
->nexthop_num
);
1156 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1157 zlog_warn("%s: invalid number of nexthops (%u)",
1158 __func__
, api
->nexthop_num
);
1162 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1163 api_nh
= &api
->nexthops
[i
];
1165 STREAM_GETL(s
, api_nh
->vrf_id
);
1166 STREAM_GETC(s
, api_nh
->type
);
1167 switch (api_nh
->type
) {
1168 case NEXTHOP_TYPE_BLACKHOLE
:
1169 STREAM_GETC(s
, api_nh
->bh_type
);
1171 case NEXTHOP_TYPE_IPV4
:
1172 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1175 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1176 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1178 STREAM_GETL(s
, api_nh
->ifindex
);
1180 case NEXTHOP_TYPE_IFINDEX
:
1181 STREAM_GETL(s
, api_nh
->ifindex
);
1183 case NEXTHOP_TYPE_IPV6
:
1184 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1186 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1187 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1188 STREAM_GETL(s
, api_nh
->ifindex
);
1192 "%s: Specified nexthop type %d does not exist",
1193 __PRETTY_FUNCTION__
, api_nh
->type
);
1197 /* MPLS labels for BGP-LU or Segment Routing */
1198 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1199 STREAM_GETC(s
, api_nh
->label_num
);
1201 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1203 "%s: invalid number of MPLS "
1205 __func__
, api_nh
->label_num
);
1209 STREAM_GET(&api_nh
->labels
[0], s
,
1211 * sizeof(mpls_label_t
));
1217 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1218 STREAM_GETC(s
, api
->distance
);
1219 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1220 STREAM_GETL(s
, api
->metric
);
1221 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1222 STREAM_GETL(s
, api
->tag
);
1223 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1224 STREAM_GETL(s
, api
->mtu
);
1225 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1226 STREAM_GETL(s
, api
->tableid
);
1232 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1234 enum zapi_route_notify_owner
*note
)
1238 STREAM_GET(note
, s
, sizeof(*note
));
1240 STREAM_GETC(s
, p
->family
);
1241 STREAM_GETC(s
, p
->prefixlen
);
1242 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1253 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1254 uint32_t *priority
, uint32_t *unique
,
1256 enum zapi_rule_notify_owner
*note
)
1258 uint32_t prio
, seq
, uni
;
1261 STREAM_GET(note
, s
, sizeof(*note
));
1263 STREAM_GETL(s
, seq
);
1264 STREAM_GETL(s
, prio
);
1265 STREAM_GETL(s
, uni
);
1266 STREAM_GETL(s
, ifi
);
1269 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
,
1270 seq
, prio
, uni
, ifi
);
1282 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1284 struct nexthop
*n
= nexthop_new();
1286 n
->type
= znh
->type
;
1287 n
->vrf_id
= znh
->vrf_id
;
1288 n
->ifindex
= znh
->ifindex
;
1289 n
->gate
= znh
->gate
;
1292 * This function currently handles labels
1294 if (znh
->label_num
) {
1295 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1302 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1306 memset(nhr
, 0, sizeof(*nhr
));
1308 STREAM_GETW(s
, nhr
->prefix
.family
);
1309 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1310 switch (nhr
->prefix
.family
) {
1312 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1315 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1321 STREAM_GETC(s
, nhr
->type
);
1322 STREAM_GETW(s
, nhr
->instance
);
1323 STREAM_GETC(s
, nhr
->distance
);
1324 STREAM_GETL(s
, nhr
->metric
);
1325 STREAM_GETC(s
, nhr
->nexthop_num
);
1327 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1328 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1329 switch (nhr
->nexthops
[i
].type
) {
1330 case NEXTHOP_TYPE_IPV4
:
1331 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1332 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1334 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1336 case NEXTHOP_TYPE_IFINDEX
:
1337 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1339 case NEXTHOP_TYPE_IPV6
:
1340 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1341 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1343 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1345 case NEXTHOP_TYPE_BLACKHOLE
:
1348 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1349 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1350 zlog_warn("%s: invalid number of MPLS labels (%u)",
1351 __func__
, nhr
->nexthops
[i
].label_num
);
1354 if (nhr
->nexthops
[i
].label_num
)
1355 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1356 nhr
->nexthops
[i
].label_num
1357 * sizeof(mpls_label_t
));
1366 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1367 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1368 * then set/unset redist[type] in the client handle (a struct zserv) for the
1371 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1372 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1379 zclient_create_header(s
, command
, vrf_id
);
1380 stream_putc(s
, afi
);
1381 stream_putc(s
, type
);
1382 stream_putw(s
, instance
);
1384 stream_putw_at(s
, 0, stream_get_endp(s
));
1386 return zclient_send_message(zclient
);
1389 /* Get prefix in ZServ format; family should be filled in on prefix */
1390 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1392 size_t plen
= prefix_blen(p
);
1399 stream_get(&p
->u
.prefix
, s
, plen
);
1401 p
->prefixlen
= MIN(plen
* 8, c
);
1407 /* Router-id update from zebra daemon. */
1408 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1410 /* Fetch interface address. */
1411 STREAM_GETC(s
, rid
->family
);
1413 zclient_stream_get_prefix(s
, rid
);
1419 /* Interface addition from zebra daemon. */
1421 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1422 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1424 * 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
1425 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1431 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1433 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1435 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1438 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1440 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1442 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1444 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1446 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1448 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1449 * | Link Layer Type |
1450 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1451 * | Harware Address Length |
1452 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1453 * | Hardware Address if HW lenght different from 0 |
1454 * | ... max INTERFACE_HWADDR_MAX |
1455 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1456 * | Link_params? | Whether a link-params follows: 1 or 0.
1457 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1458 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1459 * | .... (struct if_link_params). |
1460 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1463 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1466 char vrfname_tmp
[VRF_NAMSIZ
];
1467 struct vrf_data data
;
1469 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1470 /* Read interface name. */
1471 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1473 /* Lookup/create vrf by vrf_id. */
1474 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1475 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1476 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1480 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1484 /* Lookup vrf by vrf_id. */
1485 vrf
= vrf_lookup_by_id(vrf_id
);
1488 * If a routing protocol doesn't know about a
1489 * vrf that is about to be deleted. There is
1490 * no point in attempting to delete it.
1498 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1500 struct interface
*ifp
;
1501 char ifname_tmp
[INTERFACE_NAMSIZ
];
1503 /* Read interface name. */
1504 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1506 /* Lookup/create interface by name. */
1507 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1509 zebra_interface_if_set_value(s
, ifp
);
1515 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1516 * from zebra server. The format of this message is the same as
1517 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1518 * comments for zebra_interface_add_read), except that no sockaddr_dl
1519 * is sent at the tail of the message.
1521 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1523 struct interface
*ifp
;
1524 char ifname_tmp
[INTERFACE_NAMSIZ
];
1526 /* Read interface name. */
1527 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1529 /* Lookup this by interface index. */
1530 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1532 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1533 ifname_tmp
, vrf_id
);
1537 zebra_interface_if_set_value(s
, ifp
);
1542 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1548 iflp
->lp_status
= stream_getl(s
);
1549 iflp
->te_metric
= stream_getl(s
);
1550 iflp
->max_bw
= stream_getf(s
);
1551 iflp
->max_rsv_bw
= stream_getf(s
);
1552 uint32_t bwclassnum
= stream_getl(s
);
1555 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1556 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1559 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1560 " - outdated library?",
1561 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1563 iflp
->admin_grp
= stream_getl(s
);
1564 iflp
->rmt_as
= stream_getl(s
);
1565 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1567 iflp
->av_delay
= stream_getl(s
);
1568 iflp
->min_delay
= stream_getl(s
);
1569 iflp
->max_delay
= stream_getl(s
);
1570 iflp
->delay_var
= stream_getl(s
);
1572 iflp
->pkt_loss
= stream_getf(s
);
1573 iflp
->res_bw
= stream_getf(s
);
1574 iflp
->ava_bw
= stream_getf(s
);
1575 iflp
->use_bw
= stream_getf(s
);
1578 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1580 struct if_link_params
*iflp
;
1585 ifindex
= stream_getl(s
);
1587 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1590 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1595 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1598 link_params_set_value(s
, iflp
);
1603 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1605 uint8_t link_params_status
= 0;
1607 /* Read interface's index. */
1608 if_set_index(ifp
, stream_getl(s
));
1609 ifp
->status
= stream_getc(s
);
1611 /* Read interface's value. */
1612 ifp
->flags
= stream_getq(s
);
1613 ifp
->ptm_enable
= stream_getc(s
);
1614 ifp
->ptm_status
= stream_getc(s
);
1615 ifp
->metric
= stream_getl(s
);
1616 ifp
->speed
= stream_getl(s
);
1617 ifp
->mtu
= stream_getl(s
);
1618 ifp
->mtu6
= stream_getl(s
);
1619 ifp
->bandwidth
= stream_getl(s
);
1620 ifp
->ll_type
= stream_getl(s
);
1621 ifp
->hw_addr_len
= stream_getl(s
);
1622 if (ifp
->hw_addr_len
)
1623 stream_get(ifp
->hw_addr
, s
,
1624 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1626 /* Read Traffic Engineering status */
1627 link_params_status
= stream_getc(s
);
1628 /* Then, Traffic Engineering parameters if any */
1629 if (link_params_status
) {
1630 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1631 link_params_set_value(s
, iflp
);
1635 size_t zebra_interface_link_params_write(struct stream
*s
,
1636 struct interface
*ifp
)
1639 struct if_link_params
*iflp
;
1642 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1645 iflp
= ifp
->link_params
;
1648 w
+= stream_putl(s
, iflp
->lp_status
);
1650 w
+= stream_putl(s
, iflp
->te_metric
);
1651 w
+= stream_putf(s
, iflp
->max_bw
);
1652 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1654 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1655 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1656 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1658 w
+= stream_putl(s
, iflp
->admin_grp
);
1659 w
+= stream_putl(s
, iflp
->rmt_as
);
1660 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1662 w
+= stream_putl(s
, iflp
->av_delay
);
1663 w
+= stream_putl(s
, iflp
->min_delay
);
1664 w
+= stream_putl(s
, iflp
->max_delay
);
1665 w
+= stream_putl(s
, iflp
->delay_var
);
1667 w
+= stream_putf(s
, iflp
->pkt_loss
);
1668 w
+= stream_putf(s
, iflp
->res_bw
);
1669 w
+= stream_putf(s
, iflp
->ava_bw
);
1670 w
+= stream_putf(s
, iflp
->use_bw
);
1676 * format of message for address additon is:
1680 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1681 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1690 * | ifc_flags | flags for connected address
1698 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1706 static int memconstant(const void *s
, int c
, size_t n
)
1708 const uint8_t *p
= s
;
1717 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1721 struct interface
*ifp
;
1722 struct connected
*ifc
;
1723 struct prefix p
, d
, *dp
;
1727 memset(&p
, 0, sizeof(p
));
1728 memset(&d
, 0, sizeof(d
));
1730 /* Get interface index. */
1731 ifindex
= stream_getl(s
);
1734 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1736 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1737 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1743 ifc_flags
= stream_getc(s
);
1745 /* Fetch interface address. */
1746 d
.family
= p
.family
= stream_getc(s
);
1747 plen
= prefix_blen(&d
);
1749 zclient_stream_get_prefix(s
, &p
);
1751 /* Fetch destination address. */
1752 stream_get(&d
.u
.prefix
, s
, plen
);
1754 /* N.B. NULL destination pointers are encoded as all zeroes */
1755 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1757 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1758 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1760 /* N.B. NULL destination pointers are encoded as all
1762 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1765 ifc
->flags
= ifc_flags
;
1766 if (ifc
->destination
)
1767 ifc
->destination
->prefixlen
=
1768 ifc
->address
->prefixlen
;
1769 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1770 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1772 char buf
[PREFIX_STRLEN
];
1774 "warning: interface %s address %s "
1775 "with peer flag set, but no peer address!",
1776 ifp
->name
, prefix2str(ifc
->address
, buf
,
1778 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1782 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1783 ifc
= connected_delete_by_prefix(ifp
, &p
);
1790 * format of message for neighbor connected address is:
1794 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1795 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1810 * | addr_len | len of addr.
1813 struct nbr_connected
*
1814 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1816 unsigned int ifindex
;
1817 struct interface
*ifp
;
1819 struct nbr_connected
*ifc
;
1821 /* Get interface index. */
1822 ifindex
= stream_getl(s
);
1825 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1827 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1828 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1834 p
.family
= stream_getc(s
);
1835 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1836 p
.prefixlen
= stream_getc(s
);
1838 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1839 /* Currently only supporting P2P links, so any new RA source
1841 considered as the replacement of the previously learnt
1842 Link-Local address. */
1843 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1844 ifc
= nbr_connected_new();
1845 ifc
->address
= prefix_new();
1847 listnode_add(ifp
->nbr_connected
, ifc
);
1850 prefix_copy(ifc
->address
, &p
);
1852 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1854 ifc
= nbr_connected_check(ifp
, &p
);
1856 listnode_delete(ifp
->nbr_connected
, ifc
);
1862 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1864 vrf_id_t
*new_vrf_id
)
1866 unsigned int ifindex
;
1867 struct interface
*ifp
;
1870 /* Get interface index. */
1871 ifindex
= stream_getl(s
);
1873 /* Lookup interface. */
1874 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1876 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1881 /* Fetch new VRF Id. */
1882 new_id
= stream_getw(s
);
1884 *new_vrf_id
= new_id
;
1888 /* filter unwanted messages until the expected one arrives */
1889 static int zclient_read_sync_response(struct zclient
*zclient
,
1890 uint16_t expected_cmd
)
1902 cmd
= expected_cmd
+ 1;
1903 while (ret
== 0 && cmd
!= expected_cmd
) {
1907 /* wait until response arrives */
1909 FD_SET(zclient
->sock
, &readfds
);
1910 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1911 if (!FD_ISSET(zclient
->sock
, &readfds
))
1914 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1915 &version
, &vrf_id
, &cmd
);
1917 zlog_debug("%s: Response (%d bytes) received", __func__
,
1921 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1928 * Connect to label manager in a syncronous way
1930 * It first writes the request to zcient output buffer and then
1931 * immediately reads the answer from the input buffer.
1933 * @param zclient Zclient used to connect to label manager (zebra)
1934 * @result Result of response
1936 int lm_label_manager_connect(struct zclient
*zclient
)
1943 zlog_debug("Connecting to Label Manager");
1945 if (zclient
->sock
< 0)
1951 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1954 stream_putc(s
, zclient
->redist_default
);
1956 stream_putw(s
, zclient
->instance
);
1958 /* Put length at the first point of the stream. */
1959 stream_putw_at(s
, 0, stream_get_endp(s
));
1961 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1963 zlog_err("%s: can't write to zclient->sock", __func__
);
1964 close(zclient
->sock
);
1969 zlog_err("%s: zclient->sock connection closed", __func__
);
1970 close(zclient
->sock
);
1975 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1979 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1985 result
= stream_getc(s
);
1988 "%s: Label Manager connect response received, result %u",
1995 * Asynchronous label chunk request
1997 * @param zclient Zclient used to connect to label manager (zebra)
1998 * @param keep Avoid garbage collection
1999 * @param chunk_size Amount of labels requested
2000 * @result 0 on success, -1 otherwise
2002 int zclient_send_get_label_chunk(
2003 struct zclient
*zclient
,
2005 uint32_t chunk_size
)
2010 zlog_debug("Getting Label Chunk");
2012 if (zclient
->sock
< 0)
2018 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2019 stream_putc(s
, keep
);
2020 stream_putl(s
, chunk_size
);
2022 /* Put length at the first point of the stream. */
2023 stream_putw_at(s
, 0, stream_get_endp(s
));
2025 return zclient_send_message(zclient
);
2029 * Function to request a label chunk in a syncronous way
2031 * It first writes the request to zlcient output buffer and then
2032 * immediately reads the answer from the input buffer.
2034 * @param zclient Zclient used to connect to label manager (zebra)
2035 * @param keep Avoid garbage collection
2036 * @param chunk_size Amount of labels requested
2037 * @param start To write first assigned chunk label to
2038 * @param end To write last assigned chunk label to
2039 * @result 0 on success, -1 otherwise
2041 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2042 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2046 uint8_t response_keep
;
2049 zlog_debug("Getting Label Chunk");
2051 if (zclient
->sock
< 0)
2057 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2059 stream_putc(s
, keep
);
2061 stream_putl(s
, chunk_size
);
2062 /* Put length at the first point of the stream. */
2063 stream_putw_at(s
, 0, stream_get_endp(s
));
2065 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2067 zlog_err("%s: can't write to zclient->sock", __func__
);
2068 close(zclient
->sock
);
2073 zlog_err("%s: zclient->sock connection closed", __func__
);
2074 close(zclient
->sock
);
2079 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
2083 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2088 response_keep
= stream_getc(s
);
2089 /* start and end labels */
2090 *start
= stream_getl(s
);
2091 *end
= stream_getl(s
);
2093 /* not owning this response */
2094 if (keep
!= response_keep
) {
2096 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2097 __func__
, *start
, *end
, keep
, response_keep
);
2100 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2101 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2102 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
2108 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
2115 * Function to release a label chunk
2117 * @param zclient Zclient used to connect to label manager (zebra)
2118 * @param start First label of chunk
2119 * @param end Last label of chunk
2120 * @result 0 on success, -1 otherwise
2122 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2129 zlog_debug("Releasing Label Chunk");
2131 if (zclient
->sock
< 0)
2137 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2140 stream_putl(s
, start
);
2142 stream_putl(s
, end
);
2144 /* Put length at the first point of the stream. */
2145 stream_putw_at(s
, 0, stream_get_endp(s
));
2147 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2149 zlog_err("%s: can't write to zclient->sock", __func__
);
2150 close(zclient
->sock
);
2155 zlog_err("%s: zclient->sock connection closed", __func__
);
2156 close(zclient
->sock
);
2165 * Connect to table manager in a syncronous way
2167 * It first writes the request to zcient output buffer and then
2168 * immediately reads the answer from the input buffer.
2170 * @param zclient Zclient used to connect to table manager (zebra)
2171 * @result Result of response
2173 int tm_table_manager_connect(struct zclient
*zclient
)
2180 zlog_debug("Connecting to Table Manager");
2182 if (zclient
->sock
< 0)
2188 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2191 stream_putc(s
, zclient
->redist_default
);
2193 stream_putw(s
, zclient
->instance
);
2195 /* Put length at the first point of the stream. */
2196 stream_putw_at(s
, 0, stream_get_endp(s
));
2198 ret
= zclient_send_message(zclient
);
2203 zlog_debug("%s: Table manager connect request sent",
2207 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2213 STREAM_GETC(s
, result
);
2216 "%s: Table Manager connect response received, result %u",
2225 * Function to request a table chunk in a syncronous way
2227 * It first writes the request to zclient output buffer and then
2228 * immediately reads the answer from the input buffer.
2230 * @param zclient Zclient used to connect to table manager (zebra)
2231 * @param chunk_size Amount of table requested
2232 * @param start to write first assigned chunk table RT ID to
2233 * @param end To write last assigned chunk table RT ID to
2234 * @result 0 on success, -1 otherwise
2236 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2237 uint32_t *start
, uint32_t *end
)
2243 zlog_debug("Getting Table Chunk");
2245 if (zclient
->sock
< 0)
2251 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2253 stream_putl(s
, chunk_size
);
2254 /* Put length at the first point of the stream. */
2255 stream_putw_at(s
, 0, stream_get_endp(s
));
2257 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2259 zlog_err("%s: can't write to zclient->sock", __func__
);
2260 close(zclient
->sock
);
2265 zlog_err("%s: zclient->sock connection closed", __func__
);
2266 close(zclient
->sock
);
2271 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2275 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2279 /* start and end table IDs */
2280 STREAM_GETL(s
, *start
);
2281 STREAM_GETL(s
, *end
);
2284 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2291 * Function to release a table chunk
2293 * @param zclient Zclient used to connect to table manager (zebra)
2294 * @param start First label of table
2295 * @param end Last label of chunk
2296 * @result 0 on success, -1 otherwise
2298 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2304 zlog_debug("Releasing Table Chunk");
2306 if (zclient
->sock
< 0)
2312 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2315 stream_putl(s
, start
);
2317 stream_putl(s
, end
);
2319 /* Put length at the first point of the stream. */
2320 stream_putw_at(s
, 0, stream_get_endp(s
));
2322 return zclient_send_message(zclient
);
2326 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2334 zclient_create_header(s
, command
, VRF_DEFAULT
);
2335 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2336 stream_putl(s
, pw
->ifindex
);
2339 stream_putl(s
, pw
->type
);
2342 stream_putl(s
, pw
->af
);
2345 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2348 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2351 zlog_err("%s: unknown af", __func__
);
2356 stream_putl(s
, pw
->local_label
);
2357 stream_putl(s
, pw
->remote_label
);
2360 stream_putc(s
, pw
->flags
);
2362 /* Protocol specific fields */
2363 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2365 /* Put length at the first point of the stream. */
2366 stream_putw_at(s
, 0, stream_get_endp(s
));
2368 return zclient_send_message(zclient
);
2372 * Receive PW status update from Zebra and send it to LDE process.
2374 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2375 zebra_size_t length
, vrf_id_t vrf_id
,
2376 struct zapi_pw_status
*pw
)
2380 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2384 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2385 pw
->ifindex
= stream_getl(s
);
2386 pw
->status
= stream_getl(s
);
2389 /* Zebra client message read function. */
2390 static int zclient_read(struct thread
*thread
)
2393 uint16_t length
, command
;
2394 uint8_t marker
, version
;
2396 struct zclient
*zclient
;
2398 /* Get socket to zebra. */
2399 zclient
= THREAD_ARG(thread
);
2400 zclient
->t_read
= NULL
;
2402 /* Read zebra header (if we don't have it already). */
2403 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2405 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2406 ZEBRA_HEADER_SIZE
- already
))
2411 "zclient connection closed socket [%d].",
2413 return zclient_failed(zclient
);
2415 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2416 /* Try again later. */
2417 zclient_event(ZCLIENT_READ
, zclient
);
2420 already
= ZEBRA_HEADER_SIZE
;
2423 /* Reset to read from the beginning of the incoming packet. */
2424 stream_set_getp(zclient
->ibuf
, 0);
2426 /* Fetch header values. */
2427 length
= stream_getw(zclient
->ibuf
);
2428 marker
= stream_getc(zclient
->ibuf
);
2429 version
= stream_getc(zclient
->ibuf
);
2430 vrf_id
= stream_getl(zclient
->ibuf
);
2431 command
= stream_getw(zclient
->ibuf
);
2433 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2435 "%s: socket %d version mismatch, marker %d, version %d",
2436 __func__
, zclient
->sock
, marker
, version
);
2437 return zclient_failed(zclient
);
2440 if (length
< ZEBRA_HEADER_SIZE
) {
2441 zlog_err("%s: socket %d message length %u is less than %d ",
2442 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2443 return zclient_failed(zclient
);
2447 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2450 "%s: message size %u exceeds buffer size %lu, expanding...",
2452 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2453 ns
= stream_new(length
);
2454 stream_copy(ns
, zclient
->ibuf
);
2455 stream_free(zclient
->ibuf
);
2459 /* Read rest of zebra packet. */
2460 if (already
< length
) {
2462 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2468 "zclient connection closed socket [%d].",
2470 return zclient_failed(zclient
);
2472 if (nbyte
!= (ssize_t
)(length
- already
)) {
2473 /* Try again later. */
2474 zclient_event(ZCLIENT_READ
, zclient
);
2479 length
-= ZEBRA_HEADER_SIZE
;
2482 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2483 (void *)zclient
, command
, vrf_id
);
2486 case ZEBRA_ROUTER_ID_UPDATE
:
2487 if (zclient
->router_id_update
)
2488 (*zclient
->router_id_update
)(command
, zclient
, length
,
2492 zclient_vrf_add(zclient
, vrf_id
);
2494 case ZEBRA_VRF_DELETE
:
2495 zclient_vrf_delete(zclient
, vrf_id
);
2497 case ZEBRA_INTERFACE_ADD
:
2498 if (zclient
->interface_add
)
2499 (*zclient
->interface_add
)(command
, zclient
, length
,
2502 case ZEBRA_INTERFACE_DELETE
:
2503 if (zclient
->interface_delete
)
2504 (*zclient
->interface_delete
)(command
, zclient
, length
,
2507 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2508 if (zclient
->interface_address_add
)
2509 (*zclient
->interface_address_add
)(command
, zclient
,
2512 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2513 if (zclient
->interface_address_delete
)
2514 (*zclient
->interface_address_delete
)(command
, zclient
,
2517 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2518 if (zclient
->interface_bfd_dest_update
)
2519 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2522 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2523 if (zclient
->interface_nbr_address_add
)
2524 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2527 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2528 if (zclient
->interface_nbr_address_delete
)
2529 (*zclient
->interface_nbr_address_delete
)(
2530 command
, zclient
, length
, vrf_id
);
2532 case ZEBRA_INTERFACE_UP
:
2533 if (zclient
->interface_up
)
2534 (*zclient
->interface_up
)(command
, zclient
, length
,
2537 case ZEBRA_INTERFACE_DOWN
:
2538 if (zclient
->interface_down
)
2539 (*zclient
->interface_down
)(command
, zclient
, length
,
2542 case ZEBRA_INTERFACE_VRF_UPDATE
:
2543 if (zclient
->interface_vrf_update
)
2544 (*zclient
->interface_vrf_update
)(command
, zclient
,
2547 case ZEBRA_NEXTHOP_UPDATE
:
2549 zlog_debug("zclient rcvd nexthop update\n");
2550 if (zclient
->nexthop_update
)
2551 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2554 case ZEBRA_IMPORT_CHECK_UPDATE
:
2556 zlog_debug("zclient rcvd import check update\n");
2557 if (zclient
->import_check_update
)
2558 (*zclient
->import_check_update
)(command
, zclient
,
2561 case ZEBRA_BFD_DEST_REPLAY
:
2562 if (zclient
->bfd_dest_replay
)
2563 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2566 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2567 if (zclient
->redistribute_route_add
)
2568 (*zclient
->redistribute_route_add
)(command
, zclient
,
2571 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2572 if (zclient
->redistribute_route_del
)
2573 (*zclient
->redistribute_route_del
)(command
, zclient
,
2576 case ZEBRA_INTERFACE_LINK_PARAMS
:
2577 if (zclient
->interface_link_params
)
2578 (*zclient
->interface_link_params
)(command
, zclient
,
2581 case ZEBRA_FEC_UPDATE
:
2583 zlog_debug("zclient rcvd fec update\n");
2584 if (zclient
->fec_update
)
2585 (*zclient
->fec_update
)(command
, zclient
, length
);
2588 if (zclient
->local_vni_add
)
2589 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2593 if (zclient
->local_vni_del
)
2594 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2597 case ZEBRA_L3VNI_ADD
:
2598 if (zclient
->local_l3vni_add
)
2599 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2602 case ZEBRA_L3VNI_DEL
:
2603 if (zclient
->local_l3vni_del
)
2604 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2607 case ZEBRA_MACIP_ADD
:
2608 if (zclient
->local_macip_add
)
2609 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2612 case ZEBRA_MACIP_DEL
:
2613 if (zclient
->local_macip_del
)
2614 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2617 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2618 if (zclient
->local_ip_prefix_add
)
2619 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2622 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2623 if (zclient
->local_ip_prefix_del
)
2624 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2627 case ZEBRA_PW_STATUS_UPDATE
:
2628 if (zclient
->pw_status_update
)
2629 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2632 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2633 if (zclient
->route_notify_owner
)
2634 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2637 case ZEBRA_RULE_NOTIFY_OWNER
:
2638 if (zclient
->rule_notify_owner
)
2639 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2642 case ZEBRA_GET_LABEL_CHUNK
:
2643 if (zclient
->label_chunk
)
2644 (*zclient
->label_chunk
)(command
, zclient
, length
,
2651 if (zclient
->sock
< 0)
2652 /* Connection was closed during packet processing. */
2655 /* Register read thread. */
2656 stream_reset(zclient
->ibuf
);
2657 zclient_event(ZCLIENT_READ
, zclient
);
2662 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2663 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2667 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2668 if (redist_check_instance(
2669 &zclient
->mi_redist
[afi
][type
], instance
))
2671 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2674 if (!redist_check_instance(
2675 &zclient
->mi_redist
[afi
][type
], instance
))
2677 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2682 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2683 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2686 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2688 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2691 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2695 if (zclient
->sock
> 0)
2696 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2701 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2705 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2706 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2708 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2710 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2712 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2715 if (zclient
->sock
> 0)
2716 zebra_message_send(zclient
, command
, vrf_id
);
2719 static void zclient_event(enum event event
, struct zclient
*zclient
)
2722 case ZCLIENT_SCHEDULE
:
2723 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2724 &zclient
->t_connect
);
2726 case ZCLIENT_CONNECT
:
2729 "zclient connect failures: %d schedule interval is now %d",
2730 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2731 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2732 zclient
->fail
< 3 ? 10 : 60,
2733 &zclient
->t_connect
);
2736 zclient
->t_read
= NULL
;
2737 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2738 zclient
->sock
, &zclient
->t_read
);
2743 void zclient_interface_set_master(struct zclient
*client
,
2744 struct interface
*master
,
2745 struct interface
*slave
)
2752 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2754 stream_putl(s
, master
->vrf_id
);
2755 stream_putl(s
, master
->ifindex
);
2756 stream_putl(s
, slave
->vrf_id
);
2757 stream_putl(s
, slave
->ifindex
);
2759 stream_putw_at(s
, 0, stream_get_endp(s
));
2760 zclient_send_message(client
);