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"
32 #include "lib_errors.h"
34 #include "babel_main.h"
39 #include "babel_interface.h"
40 #include "neighbour.h"
44 #include "babel_filter.h"
45 #include "babel_zebra.h"
46 #include "babel_errors.h"
48 DEFINE_MGROUP(BABELD
, "babeld")
49 DEFINE_MTYPE_STATIC(BABELD
, BABEL
, "Babel Structure")
51 static int babel_init_routing_process(struct thread
*thread
);
52 static void babel_get_myid(void);
53 static void babel_initial_noise(void);
54 static int babel_read_protocol (struct thread
*thread
);
55 static int babel_main_loop(struct thread
*thread
);
56 static void babel_set_timer(struct timeval
*timeout
);
57 static void babel_fill_with_next_timeout(struct timeval
*tv
);
59 babel_distribute_update (struct distribute_ctx
*ctx
, struct distribute
*dist
);
61 /* Informations relative to the babel running daemon. */
62 static struct babel
*babel_routing_process
= NULL
;
63 static unsigned char *receive_buffer
= NULL
;
64 static int receive_buffer_size
= 0;
67 struct timeval check_neighbours_timeout
;
68 static time_t expiry_time
;
69 static time_t source_expiry_time
;
71 /* Babel node structure. */
72 static struct cmd_node cmd_babel_node
=
75 .prompt
= "%s(config-router)# ",
79 /* print current babel configuration on vty */
81 babel_config_write (struct vty
*vty
)
87 /* list enabled debug modes */
88 lines
+= debug_babel_config_write (vty
);
90 if (!babel_routing_process
)
92 vty_out (vty
, "router babel\n");
93 if (diversity_kind
!= DIVERSITY_NONE
)
95 vty_out (vty
, " babel diversity\n");
98 if (diversity_factor
!= BABEL_DEFAULT_DIVERSITY_FACTOR
)
100 vty_out (vty
, " babel diversity-factor %d\n",diversity_factor
);
103 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
105 vty_out (vty
, " babel resend-delay %u\n", resend_delay
);
108 if (smoothing_half_life
!= BABEL_DEFAULT_SMOOTHING_HALF_LIFE
)
110 vty_out (vty
, " babel smoothing-half-life %u\n",
111 smoothing_half_life
);
114 /* list enabled interfaces */
115 lines
= 1 + babel_enable_if_config_write (vty
);
116 /* list redistributed protocols */
117 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
118 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
119 if (i
!= zclient
->redist_default
&&
120 vrf_bitmap_check (zclient
->redist
[afi
][i
], VRF_DEFAULT
)) {
121 vty_out (vty
, " redistribute %s %s\n",
122 (afi
== AFI_IP
) ? "ipv4" : "ipv6",
123 zebra_route_string(i
));
129 lines
+= config_write_distribute (vty
, babel_routing_process
->distribute_ctx
);
136 babel_create_routing_process (void)
138 assert (babel_routing_process
== NULL
);
140 /* Allocaste Babel instance. */
141 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
143 /* Initialize timeouts */
145 expiry_time
= babel_now
.tv_sec
+ roughly(30);
146 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
148 /* Make socket for Babel protocol. */
149 protocol_socket
= babel_socket(protocol_port
);
150 if (protocol_socket
< 0) {
151 flog_err_sys(EC_LIB_SOCKET
, "Couldn't create link local socket: %s",
152 safe_strerror(errno
));
157 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
158 /* wait a little: zebra will announce interfaces, addresses, routes... */
159 thread_add_timer_msec(master
, babel_init_routing_process
, NULL
, 200L, &babel_routing_process
->t_update
);
161 /* Distribute list install. */
162 babel_routing_process
->distribute_ctx
= distribute_list_ctx_create (vrf_lookup_by_id(VRF_DEFAULT
));
163 distribute_list_add_hook (babel_routing_process
->distribute_ctx
, babel_distribute_update
);
164 distribute_list_delete_hook (babel_routing_process
->distribute_ctx
, babel_distribute_update
);
167 XFREE(MTYPE_BABEL
, babel_routing_process
);
171 /* thread reading entries form others babel daemons */
173 babel_read_protocol (struct thread
*thread
)
176 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
177 struct interface
*ifp
= NULL
;
178 struct sockaddr_in6 sin6
;
180 assert(babel_routing_process
!= NULL
);
181 assert(protocol_socket
>= 0);
183 rc
= babel_recv(protocol_socket
,
184 receive_buffer
, receive_buffer_size
,
185 (struct sockaddr
*)&sin6
, sizeof(sin6
));
187 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
188 flog_err_sys(EC_LIB_SOCKET
, "recv: %s", safe_strerror(errno
));
191 FOR_ALL_INTERFACES(vrf
, ifp
) {
194 if(ifp
->ifindex
== (ifindex_t
)sin6
.sin6_scope_id
) {
195 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
203 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
207 /* Zebra will give some information, especially about interfaces. This function
208 must be call with a litte timeout wich may give zebra the time to do his job,
209 making these inits have sense. */
211 babel_init_routing_process(struct thread
*thread
)
213 myseqno
= (random() & 0xFFFF);
215 babel_load_state_file();
216 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
217 babel_initial_noise();
218 babel_main_loop(thread
);/* this function self-add to the t_update thread */
222 /* fill "myid" with an unique id (only if myid != {0}). */
226 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
227 struct interface
*ifp
= NULL
;
231 /* if we already have an id (from state file), we return. */
232 if (memcmp(myid
, zeroes
, 8) != 0) {
236 FOR_ALL_INTERFACES(vrf
, ifp
) {
237 /* ifp->ifindex is not necessarily valid at this point */
238 int ifindex
= if_nametoindex(ifp
->name
);
240 unsigned char eui
[8];
241 rc
= if_eui64(ifindex
, eui
);
244 memcpy(myid
, eui
, 8);
249 /* We failed to get a global EUI64 from the interfaces we were given.
250 Let's try to find an interface with a MAC address. */
251 for(i
= 1; i
< 256; i
++) {
252 char buf
[IF_NAMESIZE
], *ifname
;
253 unsigned char eui
[8];
254 ifname
= if_indextoname(i
, buf
);
257 rc
= if_eui64(i
, eui
);
260 memcpy(myid
, eui
, 8);
264 flog_err(EC_BABEL_CONFIG
,
265 "Warning: couldn't find router id -- using random value.");
267 rc
= read_random_bytes(myid
, 8);
269 flog_err(EC_BABEL_CONFIG
, "read(random): %s (cannot assign an ID)",
270 safe_strerror(errno
));
273 /* Clear group and global bits */
277 /* Make some noise so that others notice us, and send retractions in
278 case we were restarted recently */
280 babel_initial_noise(void)
282 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
283 struct interface
*ifp
= NULL
;
285 FOR_ALL_INTERFACES(vrf
, ifp
) {
288 /* Apply jitter before we send the first message. */
289 usleep(roughly(10000));
292 send_wildcard_retraction(ifp
);
295 FOR_ALL_INTERFACES(vrf
, ifp
) {
298 usleep(roughly(10000));
301 send_wildcard_retraction(ifp
);
302 send_self_update(ifp
);
303 send_request(ifp
, NULL
, 0);
309 /* Delete all the added babel routes, make babeld only speak to zebra. */
311 babel_clean_routing_process(void)
314 babel_interface_close_all();
317 if (babel_routing_process
->t_read
!= NULL
) {
318 thread_cancel(babel_routing_process
->t_read
);
320 if (babel_routing_process
->t_update
!= NULL
) {
321 thread_cancel(babel_routing_process
->t_update
);
324 distribute_list_delete(&babel_routing_process
->distribute_ctx
);
325 XFREE(MTYPE_BABEL
, babel_routing_process
);
328 /* Function used with timeout. */
330 babel_main_loop(struct thread
*thread
)
333 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
334 struct interface
*ifp
= NULL
;
339 /* timeouts --------------------------------------------------------- */
340 /* get the next timeout */
341 babel_fill_with_next_timeout(&tv
);
342 /* if there is no timeout, we must wait. */
343 if(timeval_compare(&tv
, &babel_now
) > 0) {
344 timeval_minus(&tv
, &tv
, &babel_now
);
345 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %lld msecs",
346 (long long)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
347 /* it happens often to have less than 1 ms, it's bad. */
348 timeval_add_msec(&tv
, &tv
, 300);
349 babel_set_timer(&tv
);
355 /* update database -------------------------------------------------- */
356 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
358 msecs
= check_neighbours();
359 /* Multiply by 3/2 to allow neighbours to expire. */
360 msecs
= MAX(3 * msecs
/ 2, 10);
361 schedule_neighbours_check(msecs
, 1);
364 if(babel_now
.tv_sec
>= expiry_time
) {
367 expiry_time
= babel_now
.tv_sec
+ roughly(30);
370 if(babel_now
.tv_sec
>= source_expiry_time
) {
372 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
375 FOR_ALL_INTERFACES(vrf
, ifp
) {
376 babel_interface_nfo
*babel_ifp
= NULL
;
379 babel_ifp
= babel_get_if_nfo(ifp
);
380 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
382 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
383 send_update(ifp
, 0, NULL
, 0);
384 if(timeval_compare(&babel_now
,
385 &babel_ifp
->update_flush_timeout
) >= 0)
389 if(resend_time
.tv_sec
!= 0) {
390 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
394 if(unicast_flush_timeout
.tv_sec
!= 0) {
395 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
399 FOR_ALL_INTERFACES(vrf
, ifp
) {
400 babel_interface_nfo
*babel_ifp
= NULL
;
403 babel_ifp
= babel_get_if_nfo(ifp
);
404 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
405 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
411 assert(0); /* this line should never be reach */
416 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
418 static struct timeval curr_tv
;
419 static char buffer
[200];
420 static const char *curr_tag
= NULL
;
423 case 0: /* reset timeval */
426 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
432 case 1: /* take the min */
433 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
436 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
437 tv
->tv_usec
< curr_tv
.tv_usec
)) {
440 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
447 case 2: /* print message */
448 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
456 babel_fill_with_next_timeout(struct timeval
*tv
)
458 #if (defined NO_DEBUG)
459 #define printIfMin(a,b,c,d)
461 #define printIfMin(a,b,c,d) \
462 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
464 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
465 struct interface
*ifp
= NULL
;
467 *tv
= check_neighbours_timeout
;
468 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
469 timeval_min_sec(tv
, expiry_time
);
470 printIfMin(tv
, 1, "expiry_time", NULL
);
471 timeval_min_sec(tv
, source_expiry_time
);
472 printIfMin(tv
, 1, "source_expiry_time", NULL
);
473 timeval_min(tv
, &resend_time
);
474 printIfMin(tv
, 1, "resend_time", NULL
);
475 FOR_ALL_INTERFACES(vrf
, ifp
) {
476 babel_interface_nfo
*babel_ifp
= NULL
;
479 babel_ifp
= babel_get_if_nfo(ifp
);
480 timeval_min(tv
, &babel_ifp
->flush_timeout
);
481 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
482 timeval_min(tv
, &babel_ifp
->hello_timeout
);
483 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
484 timeval_min(tv
, &babel_ifp
->update_timeout
);
485 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
486 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
487 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
489 timeval_min(tv
, &unicast_flush_timeout
);
490 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
491 printIfMin(tv
, 2, NULL
, NULL
);
496 /* set the t_update thread of the babel routing process to be launch in
497 'timeout' (approximate at the milisecond) */
499 babel_set_timer(struct timeval
*timeout
)
501 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
502 if (babel_routing_process
->t_update
!= NULL
) {
503 thread_cancel(babel_routing_process
->t_update
);
505 thread_add_timer_msec(master
, babel_main_loop
, NULL
, msecs
, &babel_routing_process
->t_update
);
509 schedule_neighbours_check(int msecs
, int override
)
511 struct timeval timeout
;
513 timeval_add_msec(&timeout
, &babel_now
, msecs
);
515 check_neighbours_timeout
= timeout
;
517 timeval_min(&check_neighbours_timeout
, &timeout
);
521 resize_receive_buffer(int size
)
523 if(size
<= receive_buffer_size
)
526 if(receive_buffer
== NULL
) {
527 receive_buffer
= malloc(size
);
528 if(receive_buffer
== NULL
) {
529 flog_err(EC_BABEL_MEMORY
, "malloc(receive_buffer): %s",
530 safe_strerror(errno
));
533 receive_buffer_size
= size
;
536 new = realloc(receive_buffer
, size
);
538 flog_err(EC_BABEL_MEMORY
, "realloc(receive_buffer): %s",
539 safe_strerror(errno
));
542 receive_buffer
= new;
543 receive_buffer_size
= size
;
549 babel_distribute_update (struct distribute_ctx
*ctx
, struct distribute
*dist
)
551 struct interface
*ifp
;
552 babel_interface_nfo
*babel_ifp
;
559 ifp
= if_lookup_by_name (dist
->ifname
, VRF_DEFAULT
);
563 babel_ifp
= babel_get_if_nfo(ifp
);
565 for (type
= 0; type
< DISTRIBUTE_MAX
; type
++) {
566 family
= type
== DISTRIBUTE_V4_IN
|| type
== DISTRIBUTE_V4_OUT
?
568 if (dist
->list
[type
])
569 babel_ifp
->list
[type
] = access_list_lookup (family
,
572 babel_ifp
->list
[type
] = NULL
;
573 if (dist
->prefix
[type
])
574 babel_ifp
->prefix
[type
] = prefix_list_lookup (family
,
577 babel_ifp
->prefix
[type
] = NULL
;
582 babel_distribute_update_interface (struct interface
*ifp
)
584 struct distribute
*dist
= NULL
;
586 if (babel_routing_process
)
587 dist
= distribute_lookup(babel_routing_process
->distribute_ctx
, ifp
->name
);
589 babel_distribute_update (babel_routing_process
->distribute_ctx
, dist
);
592 /* Update all interface's distribute list. */
594 babel_distribute_update_all (struct prefix_list
*notused
)
596 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
597 struct interface
*ifp
;
599 FOR_ALL_INTERFACES (vrf
, ifp
)
600 babel_distribute_update_interface (ifp
);
604 babel_distribute_update_all_wrapper (struct access_list
*notused
)
606 babel_distribute_update_all(NULL
);
611 DEFUN_NOSH (router_babel
,
614 "Enable a routing process\n"
615 "Make Babel instance command\n")
619 vty
->node
= BABEL_NODE
;
621 if (!babel_routing_process
) {
622 ret
= babel_create_routing_process ();
624 /* Notice to user we couldn't create Babel. */
626 zlog_warn ("can't create Babel");
635 DEFUN (no_router_babel
,
639 "Disable a routing process\n"
640 "Remove Babel instance command\n")
642 if(babel_routing_process
)
643 babel_clean_routing_process();
647 /* [Babel Command] */
648 DEFUN (babel_diversity
,
652 "Enable diversity-aware routing.\n")
654 diversity_kind
= DIVERSITY_CHANNEL
;
658 /* [Babel Command] */
659 DEFUN (no_babel_diversity
,
660 no_babel_diversity_cmd
,
661 "no babel diversity",
664 "Disable diversity-aware routing.\n")
666 diversity_kind
= DIVERSITY_NONE
;
670 /* [Babel Command] */
671 DEFUN (babel_diversity_factor
,
672 babel_diversity_factor_cmd
,
673 "babel diversity-factor (1-256)",
675 "Set the diversity factor.\n"
676 "Factor in units of 1/256.\n")
680 factor
= strtoul(argv
[2]->arg
, NULL
, 10);
682 diversity_factor
= factor
;
686 /* [Babel Command] */
687 DEFUN (babel_set_resend_delay
,
688 babel_set_resend_delay_cmd
,
689 "babel resend-delay (20-655340)",
691 "Time before resending a message\n"
696 interval
= strtoul(argv
[2]->arg
, NULL
, 10);
698 resend_delay
= interval
;
702 /* [Babel Command] */
703 DEFUN (babel_set_smoothing_half_life
,
704 babel_set_smoothing_half_life_cmd
,
705 "babel smoothing-half-life (0-65534)",
707 "Smoothing half-life\n"
708 "Seconds (0 to disable)\n")
712 seconds
= strtoul(argv
[2]->arg
, NULL
, 10);
714 change_smoothing_half_life(seconds
);
719 babeld_quagga_init(void)
722 install_node(&cmd_babel_node
, &babel_config_write
);
724 install_element(CONFIG_NODE
, &router_babel_cmd
);
725 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
727 install_default(BABEL_NODE
);
728 install_element(BABEL_NODE
, &babel_diversity_cmd
);
729 install_element(BABEL_NODE
, &no_babel_diversity_cmd
);
730 install_element(BABEL_NODE
, &babel_diversity_factor_cmd
);
731 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
732 install_element(BABEL_NODE
, &babel_set_smoothing_half_life_cmd
);
736 /* Access list install. */
738 access_list_add_hook (babel_distribute_update_all_wrapper
);
739 access_list_delete_hook (babel_distribute_update_all_wrapper
);
741 /* Prefix list initialize.*/
743 prefix_list_add_hook (babel_distribute_update_all
);
744 prefix_list_delete_hook (babel_distribute_update_all
);
746 /* Distribute list install. */
747 distribute_list_init(BABEL_NODE
);
750 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
753 input_filter(const unsigned char *id
,
754 const unsigned char *prefix
, unsigned short plen
,
755 const unsigned char *neigh
, unsigned int ifindex
)
757 return babel_filter(0, prefix
, plen
, ifindex
);
761 output_filter(const unsigned char *id
, const unsigned char *prefix
,
762 unsigned short plen
, unsigned int ifindex
)
764 return babel_filter(1, prefix
, plen
, ifindex
);
767 /* There's no redistribute filter in Quagga -- the zebra daemon does its
770 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
771 unsigned int ifindex
, int proto
)
776 struct babel
*babel_lookup(void)
778 return babel_routing_process
;