2 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "distribute.h"
33 #include "babel_main.h"
38 #include "babel_interface.h"
39 #include "neighbour.h"
43 #include "babel_filter.h"
44 #include "babel_zebra.h"
45 #include "babel_memory.h"
47 static int babel_init_routing_process(struct thread
*thread
);
48 static void babel_get_myid(void);
49 static void babel_initial_noise(void);
50 static int babel_read_protocol (struct thread
*thread
);
51 static int babel_main_loop(struct thread
*thread
);
52 static void babel_set_timer(struct timeval
*timeout
);
53 static void babel_fill_with_next_timeout(struct timeval
*tv
);
56 /* Informations relative to the babel running daemon. */
57 static struct babel
*babel_routing_process
= NULL
;
58 static unsigned char *receive_buffer
= NULL
;
59 static int receive_buffer_size
= 0;
62 struct timeval check_neighbours_timeout
;
63 static time_t expiry_time
;
64 static time_t source_expiry_time
;
66 /* Babel node structure. */
67 static struct cmd_node cmd_babel_node
=
70 .prompt
= "%s(config-router)# ",
74 /* print current babel configuration on vty */
76 babel_config_write (struct vty
*vty
)
82 /* list enabled debug modes */
83 lines
+= debug_babel_config_write (vty
);
85 if (!babel_routing_process
)
87 vty_out (vty
, "router babel\n");
88 if (diversity_kind
!= DIVERSITY_NONE
)
90 vty_out (vty
, " babel diversity\n");
93 if (diversity_factor
!= BABEL_DEFAULT_DIVERSITY_FACTOR
)
95 vty_out (vty
, " babel diversity-factor %d\n",diversity_factor
);
98 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
100 vty_out (vty
, " babel resend-delay %u\n", resend_delay
);
103 if (smoothing_half_life
!= BABEL_DEFAULT_SMOOTHING_HALF_LIFE
)
105 vty_out (vty
, " babel smoothing-half-life %u\n",
106 smoothing_half_life
);
109 /* list enabled interfaces */
110 lines
= 1 + babel_enable_if_config_write (vty
);
111 /* list redistributed protocols */
112 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
113 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
114 if (i
!= zclient
->redist_default
&&
115 vrf_bitmap_check (zclient
->redist
[afi
][i
], VRF_DEFAULT
)) {
116 vty_out (vty
, " redistribute %s %s\n",
117 (afi
== AFI_IP
) ? "ipv4" : "ipv6",
118 zebra_route_string(i
));
124 lines
+= config_write_distribute (vty
);
131 babel_create_routing_process (void)
133 assert (babel_routing_process
== NULL
);
135 /* Allocaste Babel instance. */
136 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
138 /* Initialize timeouts */
140 expiry_time
= babel_now
.tv_sec
+ roughly(30);
141 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
143 /* Make socket for Babel protocol. */
144 protocol_socket
= babel_socket(protocol_port
);
145 if (protocol_socket
< 0) {
146 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
151 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
152 /* wait a little: zebra will announce interfaces, addresses, routes... */
153 thread_add_timer_msec(master
, babel_init_routing_process
, NULL
, 200L, &babel_routing_process
->t_update
);
157 XFREE(MTYPE_BABEL
, babel_routing_process
);
158 babel_routing_process
= NULL
;
162 /* thread reading entries form others babel daemons */
164 babel_read_protocol (struct thread
*thread
)
167 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
168 struct interface
*ifp
= NULL
;
169 struct sockaddr_in6 sin6
;
171 assert(babel_routing_process
!= NULL
);
172 assert(protocol_socket
>= 0);
174 rc
= babel_recv(protocol_socket
,
175 receive_buffer
, receive_buffer_size
,
176 (struct sockaddr
*)&sin6
, sizeof(sin6
));
178 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
179 zlog_err("recv: %s", safe_strerror(errno
));
182 FOR_ALL_INTERFACES(vrf
, ifp
) {
185 if(ifp
->ifindex
== (ifindex_t
)sin6
.sin6_scope_id
) {
186 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
194 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
198 /* Zebra will give some information, especially about interfaces. This function
199 must be call with a litte timeout wich may give zebra the time to do his job,
200 making these inits have sense. */
202 babel_init_routing_process(struct thread
*thread
)
204 myseqno
= (random() & 0xFFFF);
206 babel_load_state_file();
207 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
208 babel_initial_noise();
209 babel_main_loop(thread
);/* this function self-add to the t_update thread */
213 /* fill "myid" with an unique id (only if myid != {0}). */
217 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
218 struct interface
*ifp
= NULL
;
222 /* if we already have an id (from state file), we return. */
223 if (memcmp(myid
, zeroes
, 8) != 0) {
227 FOR_ALL_INTERFACES(vrf
, ifp
) {
228 /* ifp->ifindex is not necessarily valid at this point */
229 int ifindex
= if_nametoindex(ifp
->name
);
231 unsigned char eui
[8];
232 rc
= if_eui64(ifindex
, eui
);
235 memcpy(myid
, eui
, 8);
240 /* We failed to get a global EUI64 from the interfaces we were given.
241 Let's try to find an interface with a MAC address. */
242 for(i
= 1; i
< 256; i
++) {
243 char buf
[IF_NAMESIZE
], *ifname
;
244 unsigned char eui
[8];
245 ifname
= if_indextoname(i
, buf
);
248 rc
= if_eui64(i
, eui
);
251 memcpy(myid
, eui
, 8);
255 zlog_err("Warning: couldn't find router id -- using random value.");
257 rc
= read_random_bytes(myid
, 8);
259 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
262 /* Clear group and global bits */
266 /* Make some noise so that others notice us, and send retractions in
267 case we were restarted recently */
269 babel_initial_noise(void)
271 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
272 struct interface
*ifp
= NULL
;
274 FOR_ALL_INTERFACES(vrf
, ifp
) {
277 /* Apply jitter before we send the first message. */
278 usleep(roughly(10000));
281 send_wildcard_retraction(ifp
);
284 FOR_ALL_INTERFACES(vrf
, ifp
) {
287 usleep(roughly(10000));
290 send_wildcard_retraction(ifp
);
291 send_self_update(ifp
);
292 send_request(ifp
, NULL
, 0);
298 /* Delete all the added babel routes, make babeld only speak to zebra. */
300 babel_clean_routing_process()
303 babel_interface_close_all();
306 if (babel_routing_process
->t_read
!= NULL
) {
307 thread_cancel(babel_routing_process
->t_read
);
309 if (babel_routing_process
->t_update
!= NULL
) {
310 thread_cancel(babel_routing_process
->t_update
);
313 XFREE(MTYPE_BABEL
, babel_routing_process
);
314 babel_routing_process
= NULL
;
317 /* Function used with timeout. */
319 babel_main_loop(struct thread
*thread
)
322 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
323 struct interface
*ifp
= NULL
;
328 /* timeouts --------------------------------------------------------- */
329 /* get the next timeout */
330 babel_fill_with_next_timeout(&tv
);
331 /* if there is no timeout, we must wait. */
332 if(timeval_compare(&tv
, &babel_now
) > 0) {
333 timeval_minus(&tv
, &tv
, &babel_now
);
334 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %lld msecs",
335 (long long)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
336 /* it happens often to have less than 1 ms, it's bad. */
337 timeval_add_msec(&tv
, &tv
, 300);
338 babel_set_timer(&tv
);
344 /* update database -------------------------------------------------- */
345 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
347 msecs
= check_neighbours();
348 /* Multiply by 3/2 to allow neighbours to expire. */
349 msecs
= MAX(3 * msecs
/ 2, 10);
350 schedule_neighbours_check(msecs
, 1);
353 if(babel_now
.tv_sec
>= expiry_time
) {
356 expiry_time
= babel_now
.tv_sec
+ roughly(30);
359 if(babel_now
.tv_sec
>= source_expiry_time
) {
361 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
364 FOR_ALL_INTERFACES(vrf
, ifp
) {
365 babel_interface_nfo
*babel_ifp
= NULL
;
368 babel_ifp
= babel_get_if_nfo(ifp
);
369 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
371 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
372 send_update(ifp
, 0, NULL
, 0);
373 if(timeval_compare(&babel_now
,
374 &babel_ifp
->update_flush_timeout
) >= 0)
378 if(resend_time
.tv_sec
!= 0) {
379 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
383 if(unicast_flush_timeout
.tv_sec
!= 0) {
384 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
388 FOR_ALL_INTERFACES(vrf
, ifp
) {
389 babel_interface_nfo
*babel_ifp
= NULL
;
392 babel_ifp
= babel_get_if_nfo(ifp
);
393 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
394 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
400 assert(0); /* this line should never be reach */
404 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
406 static struct timeval curr_tv
;
407 static char buffer
[200];
408 static const char *curr_tag
= NULL
;
411 case 0: /* reset timeval */
414 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
420 case 1: /* take the min */
421 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
424 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
425 tv
->tv_usec
< curr_tv
.tv_usec
)) {
428 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
435 case 2: /* print message */
436 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
444 babel_fill_with_next_timeout(struct timeval
*tv
)
446 #if (defined NO_DEBUG)
447 #define printIfMin(a,b,c,d)
449 #define printIfMin(a,b,c,d) \
450 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
452 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
453 struct interface
*ifp
= NULL
;
455 *tv
= check_neighbours_timeout
;
456 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
457 timeval_min_sec(tv
, expiry_time
);
458 printIfMin(tv
, 1, "expiry_time", NULL
);
459 timeval_min_sec(tv
, source_expiry_time
);
460 printIfMin(tv
, 1, "source_expiry_time", NULL
);
461 timeval_min(tv
, &resend_time
);
462 printIfMin(tv
, 1, "resend_time", NULL
);
463 FOR_ALL_INTERFACES(vrf
, ifp
) {
464 babel_interface_nfo
*babel_ifp
= NULL
;
467 babel_ifp
= babel_get_if_nfo(ifp
);
468 timeval_min(tv
, &babel_ifp
->flush_timeout
);
469 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
470 timeval_min(tv
, &babel_ifp
->hello_timeout
);
471 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
472 timeval_min(tv
, &babel_ifp
->update_timeout
);
473 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
474 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
475 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
477 timeval_min(tv
, &unicast_flush_timeout
);
478 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
479 printIfMin(tv
, 2, NULL
, NULL
);
484 /* set the t_update thread of the babel routing process to be launch in
485 'timeout' (approximate at the milisecond) */
487 babel_set_timer(struct timeval
*timeout
)
489 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
490 if (babel_routing_process
->t_update
!= NULL
) {
491 thread_cancel(babel_routing_process
->t_update
);
493 thread_add_timer_msec(master
, babel_main_loop
, NULL
, msecs
, &babel_routing_process
->t_update
);
497 schedule_neighbours_check(int msecs
, int override
)
499 struct timeval timeout
;
501 timeval_add_msec(&timeout
, &babel_now
, msecs
);
503 check_neighbours_timeout
= timeout
;
505 timeval_min(&check_neighbours_timeout
, &timeout
);
509 resize_receive_buffer(int size
)
511 if(size
<= receive_buffer_size
)
514 if(receive_buffer
== NULL
) {
515 receive_buffer
= malloc(size
);
516 if(receive_buffer
== NULL
) {
517 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
520 receive_buffer_size
= size
;
523 new = realloc(receive_buffer
, size
);
525 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
528 receive_buffer
= new;
529 receive_buffer_size
= size
;
535 babel_distribute_update (struct distribute
*dist
)
537 struct interface
*ifp
;
538 babel_interface_nfo
*babel_ifp
;
545 ifp
= if_lookup_by_name (dist
->ifname
, VRF_DEFAULT
);
549 babel_ifp
= babel_get_if_nfo(ifp
);
551 for (type
= 0; type
< DISTRIBUTE_MAX
; type
++) {
552 family
= type
== DISTRIBUTE_V4_IN
|| type
== DISTRIBUTE_V4_OUT
?
554 if (dist
->list
[type
])
555 babel_ifp
->list
[type
] = access_list_lookup (family
,
558 babel_ifp
->list
[type
] = NULL
;
559 if (dist
->prefix
[type
])
560 babel_ifp
->prefix
[type
] = prefix_list_lookup (family
,
563 babel_ifp
->prefix
[type
] = NULL
;
568 babel_distribute_update_interface (struct interface
*ifp
)
570 struct distribute
*dist
;
572 dist
= distribute_lookup (ifp
->name
);
574 babel_distribute_update (dist
);
577 /* Update all interface's distribute list. */
579 babel_distribute_update_all (struct prefix_list
*notused
)
581 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
582 struct interface
*ifp
;
584 FOR_ALL_INTERFACES (vrf
, ifp
)
585 babel_distribute_update_interface (ifp
);
589 babel_distribute_update_all_wrapper (struct access_list
*notused
)
591 babel_distribute_update_all(NULL
);
596 DEFUN_NOSH (router_babel
,
599 "Enable a routing process\n"
600 "Make Babel instance command\n")
604 vty
->node
= BABEL_NODE
;
606 if (!babel_routing_process
) {
607 ret
= babel_create_routing_process ();
609 /* Notice to user we couldn't create Babel. */
611 zlog_warn ("can't create Babel");
620 DEFUN (no_router_babel
,
624 "Disable a routing process\n"
625 "Remove Babel instance command\n")
627 if(babel_routing_process
)
628 babel_clean_routing_process();
632 /* [Babel Command] */
633 DEFUN (babel_diversity
,
637 "Enable diversity-aware routing.\n")
639 diversity_kind
= DIVERSITY_CHANNEL
;
643 /* [Babel Command] */
644 DEFUN (no_babel_diversity
,
645 no_babel_diversity_cmd
,
646 "no babel diversity",
649 "Disable diversity-aware routing.\n")
651 diversity_kind
= DIVERSITY_NONE
;
655 /* [Babel Command] */
656 DEFUN (babel_diversity_factor
,
657 babel_diversity_factor_cmd
,
658 "babel diversity-factor (1-256)",
660 "Set the diversity factor.\n"
661 "Factor in units of 1/256.\n")
665 factor
= strtoul(argv
[2]->arg
, NULL
, 10);
667 diversity_factor
= factor
;
671 /* [Babel Command] */
672 DEFUN (babel_set_resend_delay
,
673 babel_set_resend_delay_cmd
,
674 "babel resend-delay (20-655340)",
676 "Time before resending a message\n"
681 interval
= strtoul(argv
[2]->arg
, NULL
, 10);
683 resend_delay
= interval
;
687 /* [Babel Command] */
688 DEFUN (babel_set_smoothing_half_life
,
689 babel_set_smoothing_half_life_cmd
,
690 "babel smoothing-half-life (0-65534)",
692 "Smoothing half-life\n"
693 "Seconds (0 to disable)\n")
697 seconds
= strtoul(argv
[2]->arg
, NULL
, 10);
699 change_smoothing_half_life(seconds
);
704 babeld_quagga_init(void)
707 install_node(&cmd_babel_node
, &babel_config_write
);
709 install_element(CONFIG_NODE
, &router_babel_cmd
);
710 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
712 install_default(BABEL_NODE
);
713 install_element(BABEL_NODE
, &babel_diversity_cmd
);
714 install_element(BABEL_NODE
, &no_babel_diversity_cmd
);
715 install_element(BABEL_NODE
, &babel_diversity_factor_cmd
);
716 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
717 install_element(BABEL_NODE
, &babel_set_smoothing_half_life_cmd
);
721 /* Access list install. */
723 access_list_add_hook (babel_distribute_update_all_wrapper
);
724 access_list_delete_hook (babel_distribute_update_all_wrapper
);
726 /* Prefix list initialize.*/
728 prefix_list_add_hook (babel_distribute_update_all
);
729 prefix_list_delete_hook (babel_distribute_update_all
);
731 /* Distribute list install. */
732 distribute_list_init (BABEL_NODE
);
733 distribute_list_add_hook (babel_distribute_update
);
734 distribute_list_delete_hook (babel_distribute_update
);
737 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
740 input_filter(const unsigned char *id
,
741 const unsigned char *prefix
, unsigned short plen
,
742 const unsigned char *neigh
, unsigned int ifindex
)
744 return babel_filter(0, prefix
, plen
, ifindex
);
748 output_filter(const unsigned char *id
, const unsigned char *prefix
,
749 unsigned short plen
, unsigned int ifindex
)
751 return babel_filter(1, prefix
, plen
, ifindex
);
754 /* There's no redistribute filter in Quagga -- the zebra daemon does its
757 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
758 unsigned int ifindex
, int proto
)