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
)
81 /* list enabled debug modes */
82 lines
+= debug_babel_config_write (vty
);
84 if (!babel_routing_process
)
86 vty_out (vty
, "router babel%s", VTY_NEWLINE
);
87 if (diversity_kind
!= DIVERSITY_NONE
)
89 vty_out (vty
, " babel diversity%s", VTY_NEWLINE
);
92 if (diversity_factor
!= BABEL_DEFAULT_DIVERSITY_FACTOR
)
94 vty_out (vty
, " babel diversity-factor %d%s", diversity_factor
,
98 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
100 vty_out (vty
, " babel resend-delay %u%s", resend_delay
, VTY_NEWLINE
);
103 if (smoothing_half_life
!= BABEL_DEFAULT_SMOOTHING_HALF_LIFE
)
105 vty_out (vty
, " babel smoothing-half-life %u%s",
106 smoothing_half_life
, VTY_NEWLINE
);
109 /* list enabled interfaces */
110 lines
= 1 + babel_enable_if_config_write (vty
);
111 /* list redistributed protocols */
112 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
113 if (i
!= zclient
->redist_default
&&
114 vrf_bitmap_check (zclient
->redist
[AFI_IP
][i
], VRF_DEFAULT
))
116 vty_out (vty
, " redistribute %s%s", zebra_route_string (i
), VTY_NEWLINE
);
120 lines
+= config_write_distribute (vty
);
127 babel_create_routing_process (void)
129 assert (babel_routing_process
== NULL
);
131 /* Allocaste Babel instance. */
132 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
134 /* Initialize timeouts */
136 expiry_time
= babel_now
.tv_sec
+ roughly(30);
137 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
139 /* Make socket for Babel protocol. */
140 protocol_socket
= babel_socket(protocol_port
);
141 if (protocol_socket
< 0) {
142 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
147 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
148 /* wait a little: zebra will announce interfaces, addresses, routes... */
149 thread_add_timer_msec(master
, babel_init_routing_process
, NULL
, 200L, &babel_routing_process
->t_update
);
153 XFREE(MTYPE_BABEL
, babel_routing_process
);
154 babel_routing_process
= NULL
;
158 /* thread reading entries form others babel daemons */
160 babel_read_protocol (struct thread
*thread
)
163 struct interface
*ifp
= NULL
;
164 struct sockaddr_in6 sin6
;
165 struct listnode
*linklist_node
= NULL
;
167 assert(babel_routing_process
!= NULL
);
168 assert(protocol_socket
>= 0);
170 rc
= babel_recv(protocol_socket
,
171 receive_buffer
, receive_buffer_size
,
172 (struct sockaddr
*)&sin6
, sizeof(sin6
));
174 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
175 zlog_err("recv: %s", safe_strerror(errno
));
178 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
181 if(ifp
->ifindex
== (ifindex_t
)sin6
.sin6_scope_id
) {
182 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
190 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
194 /* Zebra will give some information, especially about interfaces. This function
195 must be call with a litte timeout wich may give zebra the time to do his job,
196 making these inits have sense. */
198 babel_init_routing_process(struct thread
*thread
)
200 myseqno
= (random() & 0xFFFF);
202 babel_load_state_file();
203 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
204 babel_initial_noise();
205 babel_main_loop(thread
);/* this function self-add to the t_update thread */
209 /* fill "myid" with an unique id (only if myid != {0}). */
213 struct interface
*ifp
= NULL
;
214 struct listnode
*linklist_node
= NULL
;
218 /* if we already have an id (from state file), we return. */
219 if (memcmp(myid
, zeroes
, 8) != 0) {
223 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
224 /* ifp->ifindex is not necessarily valid at this point */
225 int ifindex
= if_nametoindex(ifp
->name
);
227 unsigned char eui
[8];
228 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
231 memcpy(myid
, eui
, 8);
236 /* We failed to get a global EUI64 from the interfaces we were given.
237 Let's try to find an interface with a MAC address. */
238 for(i
= 1; i
< 256; i
++) {
239 char buf
[IF_NAMESIZE
], *ifname
;
240 unsigned char eui
[8];
241 ifname
= if_indextoname(i
, buf
);
244 rc
= if_eui64(ifname
, i
, eui
);
247 memcpy(myid
, eui
, 8);
251 zlog_err("Warning: couldn't find router id -- using random value.");
253 rc
= read_random_bytes(myid
, 8);
255 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
258 /* Clear group and global bits */
262 /* Make some noise so that others notice us, and send retractions in
263 case we were restarted recently */
265 babel_initial_noise(void)
267 struct interface
*ifp
= NULL
;
268 struct listnode
*linklist_node
= NULL
;
270 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
273 /* Apply jitter before we send the first message. */
274 usleep(roughly(10000));
277 send_wildcard_retraction(ifp
);
280 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
283 usleep(roughly(10000));
286 send_wildcard_retraction(ifp
);
287 send_self_update(ifp
);
288 send_request(ifp
, NULL
, 0);
294 /* Delete all the added babel routes, make babeld only speak to zebra. */
296 babel_clean_routing_process()
299 babel_interface_close_all();
302 if (babel_routing_process
->t_read
!= NULL
) {
303 thread_cancel(babel_routing_process
->t_read
);
305 if (babel_routing_process
->t_update
!= NULL
) {
306 thread_cancel(babel_routing_process
->t_update
);
309 XFREE(MTYPE_BABEL
, babel_routing_process
);
310 babel_routing_process
= NULL
;
313 /* Function used with timeout. */
315 babel_main_loop(struct thread
*thread
)
318 struct interface
*ifp
= NULL
;
319 struct listnode
*linklist_node
= NULL
;
324 /* timeouts --------------------------------------------------------- */
325 /* get the next timeout */
326 babel_fill_with_next_timeout(&tv
);
327 /* if there is no timeout, we must wait. */
328 if(timeval_compare(&tv
, &babel_now
) > 0) {
329 timeval_minus(&tv
, &tv
, &babel_now
);
330 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
331 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
332 /* it happens often to have less than 1 ms, it's bad. */
333 timeval_add_msec(&tv
, &tv
, 300);
334 babel_set_timer(&tv
);
340 /* update database -------------------------------------------------- */
341 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
343 msecs
= check_neighbours();
344 /* Multiply by 3/2 to allow neighbours to expire. */
345 msecs
= MAX(3 * msecs
/ 2, 10);
346 schedule_neighbours_check(msecs
, 1);
349 if(babel_now
.tv_sec
>= expiry_time
) {
352 expiry_time
= babel_now
.tv_sec
+ roughly(30);
355 if(babel_now
.tv_sec
>= source_expiry_time
) {
357 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
360 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
361 babel_interface_nfo
*babel_ifp
= NULL
;
364 babel_ifp
= babel_get_if_nfo(ifp
);
365 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
367 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
368 send_update(ifp
, 0, NULL
, 0);
369 if(timeval_compare(&babel_now
,
370 &babel_ifp
->update_flush_timeout
) >= 0)
374 if(resend_time
.tv_sec
!= 0) {
375 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
379 if(unicast_flush_timeout
.tv_sec
!= 0) {
380 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
384 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
385 babel_interface_nfo
*babel_ifp
= NULL
;
388 babel_ifp
= babel_get_if_nfo(ifp
);
389 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
390 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
396 assert(0); /* this line should never be reach */
400 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
402 static struct timeval curr_tv
;
403 static char buffer
[200];
404 static const char *curr_tag
= NULL
;
407 case 0: /* reset timeval */
410 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
416 case 1: /* take the min */
417 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
420 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
421 tv
->tv_usec
< curr_tv
.tv_usec
)) {
424 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
431 case 2: /* print message */
432 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
440 babel_fill_with_next_timeout(struct timeval
*tv
)
442 #if (defined NO_DEBUG)
443 #define printIfMin(a,b,c,d)
445 #define printIfMin(a,b,c,d) \
446 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
448 struct interface
*ifp
= NULL
;
449 struct listnode
*linklist_node
= NULL
;
451 *tv
= check_neighbours_timeout
;
452 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
453 timeval_min_sec(tv
, expiry_time
);
454 printIfMin(tv
, 1, "expiry_time", NULL
);
455 timeval_min_sec(tv
, source_expiry_time
);
456 printIfMin(tv
, 1, "source_expiry_time", NULL
);
457 timeval_min(tv
, &resend_time
);
458 printIfMin(tv
, 1, "resend_time", NULL
);
459 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
460 babel_interface_nfo
*babel_ifp
= NULL
;
463 babel_ifp
= babel_get_if_nfo(ifp
);
464 timeval_min(tv
, &babel_ifp
->flush_timeout
);
465 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
466 timeval_min(tv
, &babel_ifp
->hello_timeout
);
467 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
468 timeval_min(tv
, &babel_ifp
->update_timeout
);
469 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
470 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
471 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
473 timeval_min(tv
, &unicast_flush_timeout
);
474 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
475 printIfMin(tv
, 2, NULL
, NULL
);
480 /* set the t_update thread of the babel routing process to be launch in
481 'timeout' (approximate at the milisecond) */
483 babel_set_timer(struct timeval
*timeout
)
485 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
486 if (babel_routing_process
->t_update
!= NULL
) {
487 thread_cancel(babel_routing_process
->t_update
);
489 thread_add_timer_msec(master
, babel_main_loop
, NULL
, msecs
, &babel_routing_process
->t_update
);
493 schedule_neighbours_check(int msecs
, int override
)
495 struct timeval timeout
;
497 timeval_add_msec(&timeout
, &babel_now
, msecs
);
499 check_neighbours_timeout
= timeout
;
501 timeval_min(&check_neighbours_timeout
, &timeout
);
505 resize_receive_buffer(int size
)
507 if(size
<= receive_buffer_size
)
510 if(receive_buffer
== NULL
) {
511 receive_buffer
= malloc(size
);
512 if(receive_buffer
== NULL
) {
513 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
516 receive_buffer_size
= size
;
519 new = realloc(receive_buffer
, size
);
521 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
524 receive_buffer
= new;
525 receive_buffer_size
= size
;
531 babel_distribute_update (struct distribute
*dist
)
533 struct interface
*ifp
;
534 babel_interface_nfo
*babel_ifp
;
541 ifp
= if_lookup_by_name (dist
->ifname
, VRF_DEFAULT
);
545 babel_ifp
= babel_get_if_nfo(ifp
);
547 for (type
= 0; type
< DISTRIBUTE_MAX
; type
++) {
548 family
= type
== DISTRIBUTE_V4_IN
|| type
== DISTRIBUTE_V4_OUT
?
550 if (dist
->list
[type
])
551 babel_ifp
->list
[type
] = access_list_lookup (family
,
554 babel_ifp
->list
[type
] = NULL
;
555 if (dist
->prefix
[type
])
556 babel_ifp
->prefix
[type
] = prefix_list_lookup (family
,
559 babel_ifp
->prefix
[type
] = NULL
;
564 babel_distribute_update_interface (struct interface
*ifp
)
566 struct distribute
*dist
;
568 dist
= distribute_lookup (ifp
->name
);
570 babel_distribute_update (dist
);
573 /* Update all interface's distribute list. */
575 babel_distribute_update_all (struct prefix_list
*notused
)
577 struct interface
*ifp
;
578 struct listnode
*node
;
580 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(VRF_DEFAULT
), node
, ifp
))
581 babel_distribute_update_interface (ifp
);
585 babel_distribute_update_all_wrapper (struct access_list
*notused
)
587 babel_distribute_update_all(NULL
);
592 DEFUN_NOSH (router_babel
,
595 "Enable a routing process\n"
596 "Make Babel instance command\n")
600 vty
->node
= BABEL_NODE
;
602 if (!babel_routing_process
) {
603 ret
= babel_create_routing_process ();
605 /* Notice to user we couldn't create Babel. */
607 zlog_warn ("can't create Babel");
616 DEFUN (no_router_babel
,
620 "Disable a routing process\n"
621 "Remove Babel instance command\n"
624 if(babel_routing_process
)
625 babel_clean_routing_process();
629 /* [Babel Command] */
630 DEFUN (babel_diversity
,
634 "Enable diversity-aware routing.\n")
636 diversity_kind
= DIVERSITY_CHANNEL
;
640 /* [Babel Command] */
641 DEFUN (no_babel_diversity
,
642 no_babel_diversity_cmd
,
643 "no babel diversity",
646 "Disable diversity-aware routing.\n")
648 diversity_kind
= DIVERSITY_NONE
;
652 /* [Babel Command] */
653 DEFUN (babel_diversity_factor
,
654 babel_diversity_factor_cmd
,
655 "babel diversity-factor (1-256)",
657 "Set the diversity factor.\n"
658 "Factor in units of 1/256.\n")
662 VTY_GET_INTEGER_RANGE("factor", factor
, argv
[2]->arg
, 1, 256);
664 diversity_factor
= factor
;
668 /* [Babel Command] */
669 DEFUN (babel_set_resend_delay
,
670 babel_set_resend_delay_cmd
,
671 "babel resend-delay (20-655340)",
673 "Time before resending a message\n"
678 VTY_GET_INTEGER_RANGE("milliseconds", interval
, argv
[2]->arg
, 20, 10 * 0xFFFE);
680 resend_delay
= interval
;
684 /* [Babel Command] */
685 DEFUN (babel_set_smoothing_half_life
,
686 babel_set_smoothing_half_life_cmd
,
687 "babel smoothing-half-life (0-65534)",
689 "Smoothing half-life\n"
690 "Seconds (0 to disable)\n")
694 VTY_GET_INTEGER_RANGE("seconds", seconds
, argv
[2]->arg
, 0, 0xFFFE);
696 change_smoothing_half_life(seconds
);
701 babeld_quagga_init(void)
704 install_node(&cmd_babel_node
, &babel_config_write
);
706 install_element(CONFIG_NODE
, &router_babel_cmd
);
707 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
709 install_default(BABEL_NODE
);
710 install_element(BABEL_NODE
, &babel_diversity_cmd
);
711 install_element(BABEL_NODE
, &no_babel_diversity_cmd
);
712 install_element(BABEL_NODE
, &babel_diversity_factor_cmd
);
713 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
714 install_element(BABEL_NODE
, &babel_set_smoothing_half_life_cmd
);
718 /* Access list install. */
720 access_list_add_hook (babel_distribute_update_all_wrapper
);
721 access_list_delete_hook (babel_distribute_update_all_wrapper
);
723 /* Prefix list initialize.*/
725 prefix_list_add_hook (babel_distribute_update_all
);
726 prefix_list_delete_hook (babel_distribute_update_all
);
728 /* Distribute list install. */
729 distribute_list_init (BABEL_NODE
);
730 distribute_list_add_hook (babel_distribute_update
);
731 distribute_list_delete_hook (babel_distribute_update
);
734 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
737 input_filter(const unsigned char *id
,
738 const unsigned char *prefix
, unsigned short plen
,
739 const unsigned char *neigh
, unsigned int ifindex
)
741 return babel_filter(0, prefix
, plen
, ifindex
);
745 output_filter(const unsigned char *id
, const unsigned char *prefix
,
746 unsigned short plen
, unsigned int ifindex
)
748 return babel_filter(1, prefix
, plen
, ifindex
);
751 /* There's no redistribute filter in Quagga -- the zebra daemon does its
754 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
755 unsigned int ifindex
, int proto
)