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_outln (vty
, "router babel");
87 if (diversity_kind
!= DIVERSITY_NONE
)
89 vty_outln (vty
, " babel diversity");
92 if (diversity_factor
!= BABEL_DEFAULT_DIVERSITY_FACTOR
)
94 vty_outln (vty
, " babel diversity-factor %d",diversity_factor
);
97 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
99 vty_outln (vty
, " babel resend-delay %u", resend_delay
);
102 if (smoothing_half_life
!= BABEL_DEFAULT_SMOOTHING_HALF_LIFE
)
104 vty_outln (vty
, " babel smoothing-half-life %u",
105 smoothing_half_life
);
108 /* list enabled interfaces */
109 lines
= 1 + babel_enable_if_config_write (vty
);
110 /* list redistributed protocols */
111 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
112 if (i
!= zclient
->redist_default
&&
113 vrf_bitmap_check (zclient
->redist
[AFI_IP
][i
], VRF_DEFAULT
))
115 vty_outln (vty
, " redistribute %s", zebra_route_string(i
));
119 lines
+= config_write_distribute (vty
);
126 babel_create_routing_process (void)
128 assert (babel_routing_process
== NULL
);
130 /* Allocaste Babel instance. */
131 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
133 /* Initialize timeouts */
135 expiry_time
= babel_now
.tv_sec
+ roughly(30);
136 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
138 /* Make socket for Babel protocol. */
139 protocol_socket
= babel_socket(protocol_port
);
140 if (protocol_socket
< 0) {
141 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
146 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
147 /* wait a little: zebra will announce interfaces, addresses, routes... */
148 thread_add_timer_msec(master
, babel_init_routing_process
, NULL
, 200L, &babel_routing_process
->t_update
);
152 XFREE(MTYPE_BABEL
, babel_routing_process
);
153 babel_routing_process
= NULL
;
157 /* thread reading entries form others babel daemons */
159 babel_read_protocol (struct thread
*thread
)
162 struct interface
*ifp
= NULL
;
163 struct sockaddr_in6 sin6
;
164 struct listnode
*linklist_node
= NULL
;
166 assert(babel_routing_process
!= NULL
);
167 assert(protocol_socket
>= 0);
169 rc
= babel_recv(protocol_socket
,
170 receive_buffer
, receive_buffer_size
,
171 (struct sockaddr
*)&sin6
, sizeof(sin6
));
173 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
174 zlog_err("recv: %s", safe_strerror(errno
));
177 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
180 if(ifp
->ifindex
== (ifindex_t
)sin6
.sin6_scope_id
) {
181 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
189 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
193 /* Zebra will give some information, especially about interfaces. This function
194 must be call with a litte timeout wich may give zebra the time to do his job,
195 making these inits have sense. */
197 babel_init_routing_process(struct thread
*thread
)
199 myseqno
= (random() & 0xFFFF);
201 babel_load_state_file();
202 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
203 babel_initial_noise();
204 babel_main_loop(thread
);/* this function self-add to the t_update thread */
208 /* fill "myid" with an unique id (only if myid != {0}). */
212 struct interface
*ifp
= NULL
;
213 struct listnode
*linklist_node
= NULL
;
217 /* if we already have an id (from state file), we return. */
218 if (memcmp(myid
, zeroes
, 8) != 0) {
222 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
223 /* ifp->ifindex is not necessarily valid at this point */
224 int ifindex
= if_nametoindex(ifp
->name
);
226 unsigned char eui
[8];
227 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
230 memcpy(myid
, eui
, 8);
235 /* We failed to get a global EUI64 from the interfaces we were given.
236 Let's try to find an interface with a MAC address. */
237 for(i
= 1; i
< 256; i
++) {
238 char buf
[IF_NAMESIZE
], *ifname
;
239 unsigned char eui
[8];
240 ifname
= if_indextoname(i
, buf
);
243 rc
= if_eui64(ifname
, i
, eui
);
246 memcpy(myid
, eui
, 8);
250 zlog_err("Warning: couldn't find router id -- using random value.");
252 rc
= read_random_bytes(myid
, 8);
254 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
257 /* Clear group and global bits */
261 /* Make some noise so that others notice us, and send retractions in
262 case we were restarted recently */
264 babel_initial_noise(void)
266 struct interface
*ifp
= NULL
;
267 struct listnode
*linklist_node
= NULL
;
269 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
272 /* Apply jitter before we send the first message. */
273 usleep(roughly(10000));
276 send_wildcard_retraction(ifp
);
279 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
282 usleep(roughly(10000));
285 send_wildcard_retraction(ifp
);
286 send_self_update(ifp
);
287 send_request(ifp
, NULL
, 0);
293 /* Delete all the added babel routes, make babeld only speak to zebra. */
295 babel_clean_routing_process()
298 babel_interface_close_all();
301 if (babel_routing_process
->t_read
!= NULL
) {
302 thread_cancel(babel_routing_process
->t_read
);
304 if (babel_routing_process
->t_update
!= NULL
) {
305 thread_cancel(babel_routing_process
->t_update
);
308 XFREE(MTYPE_BABEL
, babel_routing_process
);
309 babel_routing_process
= NULL
;
312 /* Function used with timeout. */
314 babel_main_loop(struct thread
*thread
)
317 struct interface
*ifp
= NULL
;
318 struct listnode
*linklist_node
= NULL
;
323 /* timeouts --------------------------------------------------------- */
324 /* get the next timeout */
325 babel_fill_with_next_timeout(&tv
);
326 /* if there is no timeout, we must wait. */
327 if(timeval_compare(&tv
, &babel_now
) > 0) {
328 timeval_minus(&tv
, &tv
, &babel_now
);
329 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
330 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
331 /* it happens often to have less than 1 ms, it's bad. */
332 timeval_add_msec(&tv
, &tv
, 300);
333 babel_set_timer(&tv
);
339 /* update database -------------------------------------------------- */
340 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
342 msecs
= check_neighbours();
343 /* Multiply by 3/2 to allow neighbours to expire. */
344 msecs
= MAX(3 * msecs
/ 2, 10);
345 schedule_neighbours_check(msecs
, 1);
348 if(babel_now
.tv_sec
>= expiry_time
) {
351 expiry_time
= babel_now
.tv_sec
+ roughly(30);
354 if(babel_now
.tv_sec
>= source_expiry_time
) {
356 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
359 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
360 babel_interface_nfo
*babel_ifp
= NULL
;
363 babel_ifp
= babel_get_if_nfo(ifp
);
364 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
366 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
367 send_update(ifp
, 0, NULL
, 0);
368 if(timeval_compare(&babel_now
,
369 &babel_ifp
->update_flush_timeout
) >= 0)
373 if(resend_time
.tv_sec
!= 0) {
374 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
378 if(unicast_flush_timeout
.tv_sec
!= 0) {
379 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
383 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
384 babel_interface_nfo
*babel_ifp
= NULL
;
387 babel_ifp
= babel_get_if_nfo(ifp
);
388 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
389 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
395 assert(0); /* this line should never be reach */
399 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
401 static struct timeval curr_tv
;
402 static char buffer
[200];
403 static const char *curr_tag
= NULL
;
406 case 0: /* reset timeval */
409 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
415 case 1: /* take the min */
416 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
419 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
420 tv
->tv_usec
< curr_tv
.tv_usec
)) {
423 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
430 case 2: /* print message */
431 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
439 babel_fill_with_next_timeout(struct timeval
*tv
)
441 #if (defined NO_DEBUG)
442 #define printIfMin(a,b,c,d)
444 #define printIfMin(a,b,c,d) \
445 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
447 struct interface
*ifp
= NULL
;
448 struct listnode
*linklist_node
= NULL
;
450 *tv
= check_neighbours_timeout
;
451 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
452 timeval_min_sec(tv
, expiry_time
);
453 printIfMin(tv
, 1, "expiry_time", NULL
);
454 timeval_min_sec(tv
, source_expiry_time
);
455 printIfMin(tv
, 1, "source_expiry_time", NULL
);
456 timeval_min(tv
, &resend_time
);
457 printIfMin(tv
, 1, "resend_time", NULL
);
458 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
459 babel_interface_nfo
*babel_ifp
= NULL
;
462 babel_ifp
= babel_get_if_nfo(ifp
);
463 timeval_min(tv
, &babel_ifp
->flush_timeout
);
464 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
465 timeval_min(tv
, &babel_ifp
->hello_timeout
);
466 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
467 timeval_min(tv
, &babel_ifp
->update_timeout
);
468 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
469 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
470 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
472 timeval_min(tv
, &unicast_flush_timeout
);
473 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
474 printIfMin(tv
, 2, NULL
, NULL
);
479 /* set the t_update thread of the babel routing process to be launch in
480 'timeout' (approximate at the milisecond) */
482 babel_set_timer(struct timeval
*timeout
)
484 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
485 if (babel_routing_process
->t_update
!= NULL
) {
486 thread_cancel(babel_routing_process
->t_update
);
488 thread_add_timer_msec(master
, babel_main_loop
, NULL
, msecs
, &babel_routing_process
->t_update
);
492 schedule_neighbours_check(int msecs
, int override
)
494 struct timeval timeout
;
496 timeval_add_msec(&timeout
, &babel_now
, msecs
);
498 check_neighbours_timeout
= timeout
;
500 timeval_min(&check_neighbours_timeout
, &timeout
);
504 resize_receive_buffer(int size
)
506 if(size
<= receive_buffer_size
)
509 if(receive_buffer
== NULL
) {
510 receive_buffer
= malloc(size
);
511 if(receive_buffer
== NULL
) {
512 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
515 receive_buffer_size
= size
;
518 new = realloc(receive_buffer
, size
);
520 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
523 receive_buffer
= new;
524 receive_buffer_size
= size
;
530 babel_distribute_update (struct distribute
*dist
)
532 struct interface
*ifp
;
533 babel_interface_nfo
*babel_ifp
;
540 ifp
= if_lookup_by_name (dist
->ifname
, VRF_DEFAULT
);
544 babel_ifp
= babel_get_if_nfo(ifp
);
546 for (type
= 0; type
< DISTRIBUTE_MAX
; type
++) {
547 family
= type
== DISTRIBUTE_V4_IN
|| type
== DISTRIBUTE_V4_OUT
?
549 if (dist
->list
[type
])
550 babel_ifp
->list
[type
] = access_list_lookup (family
,
553 babel_ifp
->list
[type
] = NULL
;
554 if (dist
->prefix
[type
])
555 babel_ifp
->prefix
[type
] = prefix_list_lookup (family
,
558 babel_ifp
->prefix
[type
] = NULL
;
563 babel_distribute_update_interface (struct interface
*ifp
)
565 struct distribute
*dist
;
567 dist
= distribute_lookup (ifp
->name
);
569 babel_distribute_update (dist
);
572 /* Update all interface's distribute list. */
574 babel_distribute_update_all (struct prefix_list
*notused
)
576 struct interface
*ifp
;
577 struct listnode
*node
;
579 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(VRF_DEFAULT
), node
, ifp
))
580 babel_distribute_update_interface (ifp
);
584 babel_distribute_update_all_wrapper (struct access_list
*notused
)
586 babel_distribute_update_all(NULL
);
591 DEFUN_NOSH (router_babel
,
594 "Enable a routing process\n"
595 "Make Babel instance command\n")
599 vty
->node
= BABEL_NODE
;
601 if (!babel_routing_process
) {
602 ret
= babel_create_routing_process ();
604 /* Notice to user we couldn't create Babel. */
606 zlog_warn ("can't create Babel");
615 DEFUN (no_router_babel
,
619 "Disable a routing process\n"
620 "Remove Babel instance command\n")
622 if(babel_routing_process
)
623 babel_clean_routing_process();
627 /* [Babel Command] */
628 DEFUN (babel_diversity
,
632 "Enable diversity-aware routing.\n")
634 diversity_kind
= DIVERSITY_CHANNEL
;
638 /* [Babel Command] */
639 DEFUN (no_babel_diversity
,
640 no_babel_diversity_cmd
,
641 "no babel diversity",
644 "Disable diversity-aware routing.\n")
646 diversity_kind
= DIVERSITY_NONE
;
650 /* [Babel Command] */
651 DEFUN (babel_diversity_factor
,
652 babel_diversity_factor_cmd
,
653 "babel diversity-factor (1-256)",
655 "Set the diversity factor.\n"
656 "Factor in units of 1/256.\n")
660 factor
= strtoul(argv
[2]->arg
, NULL
, 10);
662 diversity_factor
= factor
;
666 /* [Babel Command] */
667 DEFUN (babel_set_resend_delay
,
668 babel_set_resend_delay_cmd
,
669 "babel resend-delay (20-655340)",
671 "Time before resending a message\n"
676 interval
= strtoul(argv
[2]->arg
, NULL
, 10);
678 resend_delay
= interval
;
682 /* [Babel Command] */
683 DEFUN (babel_set_smoothing_half_life
,
684 babel_set_smoothing_half_life_cmd
,
685 "babel smoothing-half-life (0-65534)",
687 "Smoothing half-life\n"
688 "Seconds (0 to disable)\n")
692 seconds
= strtoul(argv
[2]->arg
, NULL
, 10);
694 change_smoothing_half_life(seconds
);
699 babeld_quagga_init(void)
702 install_node(&cmd_babel_node
, &babel_config_write
);
704 install_element(CONFIG_NODE
, &router_babel_cmd
);
705 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
707 install_default(BABEL_NODE
);
708 install_element(BABEL_NODE
, &babel_diversity_cmd
);
709 install_element(BABEL_NODE
, &no_babel_diversity_cmd
);
710 install_element(BABEL_NODE
, &babel_diversity_factor_cmd
);
711 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
712 install_element(BABEL_NODE
, &babel_set_smoothing_half_life_cmd
);
716 /* Access list install. */
718 access_list_add_hook (babel_distribute_update_all_wrapper
);
719 access_list_delete_hook (babel_distribute_update_all_wrapper
);
721 /* Prefix list initialize.*/
723 prefix_list_add_hook (babel_distribute_update_all
);
724 prefix_list_delete_hook (babel_distribute_update_all
);
726 /* Distribute list install. */
727 distribute_list_init (BABEL_NODE
);
728 distribute_list_add_hook (babel_distribute_update
);
729 distribute_list_delete_hook (babel_distribute_update
);
732 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
735 input_filter(const unsigned char *id
,
736 const unsigned char *prefix
, unsigned short plen
,
737 const unsigned char *neigh
, unsigned int ifindex
)
739 return babel_filter(0, prefix
, plen
, ifindex
);
743 output_filter(const unsigned char *id
, const unsigned char *prefix
,
744 unsigned short plen
, unsigned int ifindex
)
746 return babel_filter(1, prefix
, plen
, ifindex
);
749 /* There's no redistribute filter in Quagga -- the zebra daemon does its
752 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
753 unsigned int ifindex
, int proto
)