Implemented automatic restart after error with all the timers needed.

This commit is contained in:
Martin Mares 2000-04-25 23:08:31 +00:00
parent 8573314326
commit 6fd766c17e
3 changed files with 74 additions and 16 deletions

View file

@ -26,6 +26,7 @@ static list bgp_list; /* List of active BGP instances */
static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established" };
static void bgp_connect(struct bgp_proto *p);
static void bgp_initiate(struct bgp_proto *p);
void
bgp_close(struct bgp_proto *p)
@ -40,7 +41,6 @@ bgp_close(struct bgp_proto *p)
rfree(bgp_linpool);
bgp_linpool = NULL;
}
/* FIXME: Automatic restart after errors? */
}
void
@ -60,6 +60,7 @@ void
bgp_close_conn(struct bgp_conn *conn)
{
struct bgp_proto *p = conn->bgp;
struct bgp_config *cf = p->cf;
DBG("BGP: Closing connection\n");
conn->packets_to_send = 0;
@ -72,14 +73,29 @@ bgp_close_conn(struct bgp_conn *conn)
sk_close(conn->sk);
conn->sk = NULL;
conn->state = BS_IDLE;
if (conn->error_flag > 1)
{
if (cf->disable_after_error)
p->p.disabled = 1;
if (p->last_connect && (bird_clock_t)(p->last_connect + cf->error_amnesia_time) < now)
p->startup_delay = 0;
if (!p->startup_delay)
p->startup_delay = cf->error_delay_time_min;
else
{
p->startup_delay *= 2;
if (p->startup_delay > cf->error_delay_time_max)
p->startup_delay = cf->error_delay_time_max;
}
}
if (conn->primary)
{
bgp_close(p);
p->conn = NULL;
if (conn->error_flag) /* FIXME: Enable automatically? */
p->p.disabled = 1;
proto_notify_state(&p->p, PS_DOWN);
}
else if (conn->error_flag > 1)
bgp_initiate(p);
}
static int
@ -175,19 +191,12 @@ bgp_keepalive_timeout(timer *t)
}
static void
bgp_setup_sk(struct bgp_proto *p, struct bgp_conn *conn, sock *s)
bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn)
{
timer *t;
s->data = conn;
s->ttl = p->cf->multihop ? : 1;
s->rbsize = BGP_RX_BUFFER_SIZE;
s->tbsize = BGP_TX_BUFFER_SIZE;
s->err_hook = bgp_sock_err;
s->tos = IP_PREC_INTERNET_CONTROL;
conn->sk = NULL;
conn->bgp = p;
conn->sk = s;
conn->packets_to_send = 0;
conn->error_flag = 0;
conn->primary = 0;
@ -203,6 +212,18 @@ bgp_setup_sk(struct bgp_proto *p, struct bgp_conn *conn, sock *s)
t->data = conn;
}
static void
bgp_setup_sk(struct bgp_proto *p, struct bgp_conn *conn, sock *s)
{
s->data = conn;
s->ttl = p->cf->multihop ? : 1;
s->rbsize = BGP_RX_BUFFER_SIZE;
s->tbsize = BGP_TX_BUFFER_SIZE;
s->err_hook = bgp_sock_err;
s->tos = IP_PREC_INTERNET_CONTROL;
conn->sk = s;
}
static void
bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing connection */
{
@ -210,6 +231,7 @@ bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing c
struct bgp_conn *conn = &p->outgoing_conn;
DBG("BGP: Connecting\n");
p->last_connect = now;
s = sk_new(p->p.pool);
s->type = SK_TCP_ACTIVE;
if (ipa_nonzero(p->cf->source_addr))
@ -218,6 +240,7 @@ bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing c
s->saddr = p->local_addr;
s->daddr = p->cf->remote_ip;
s->dport = BGP_PORT;
bgp_setup_conn(p, conn);
bgp_setup_sk(p, conn, s);
s->tx_hook = bgp_connected;
conn->state = BS_CONNECT;
@ -230,6 +253,24 @@ bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing c
bgp_start_timer(conn->connect_retry_timer, p->cf->connect_retry_time);
}
static void
bgp_initiate(struct bgp_proto *p)
{
unsigned delay;
delay = p->cf->start_delay_time;
if (p->startup_delay > delay)
delay = p->startup_delay;
if (delay)
{
DBG("BGP: Connect delayed by %d seconds\n", delay);
bgp_setup_conn(p, &p->outgoing_conn);
bgp_start_timer(p->outgoing_conn.connect_retry_timer, delay);
}
else
bgp_connect(p);
}
static int
bgp_incoming_connection(sock *sk, int dummy)
{
@ -247,6 +288,7 @@ bgp_incoming_connection(sock *sk, int dummy)
DBG("BGP: But one incoming connection already exists, how is that possible?\n");
break;
}
bgp_setup_conn(p, &p->incoming_conn);
bgp_setup_sk(p, &p->incoming_conn, sk);
bgp_send_open(&p->incoming_conn);
return 0;
@ -285,7 +327,7 @@ bgp_start_neighbor(struct bgp_proto *p)
if (!bgp_linpool)
bgp_linpool = lp_new(&root_pool, 4080);
add_tail(&bgp_list, &p->bgp_node);
bgp_connect(p);
bgp_initiate(p);
}
static void
@ -305,7 +347,6 @@ bgp_neigh_notify(neighbor *n)
bgp_graceful_close_conn(&p->outgoing_conn);
bgp_graceful_close_conn(&p->incoming_conn);
proto_notify_state(&p->p, PS_DOWN);
/* FIXME: Remember to delay protocol startup here! */
}
}
@ -340,6 +381,7 @@ bgp_start(struct proto *P)
DBG("BGP: Startup.\n");
p->outgoing_conn.state = BS_IDLE;
p->incoming_conn.state = BS_IDLE;
p->startup_delay = 0;
/*
* Before attempting to create the connection, we need to lock the
@ -415,7 +457,7 @@ bgp_error(struct bgp_conn *c, unsigned code, unsigned subcode, byte *data, int l
if (c->error_flag)
return;
bgp_log_error(c->bgp, "Error", code, subcode, data, (len > 0) ? len : -len);
c->error_flag = 1;
c->error_flag = 1 + (code != 6);
c->notify_code = code;
c->notify_subcode = subcode;
c->notify_data = data;

View file

@ -28,6 +28,11 @@ struct bgp_config {
unsigned connect_retry_time;
unsigned hold_time, initial_hold_time;
unsigned keepalive_time;
unsigned start_delay_time; /* Minimum delay between connects */
unsigned error_amnesia_time; /* Errors are forgotten after */
unsigned error_delay_time_min; /* Time to wait after an error is detected */
unsigned error_delay_time_max;
unsigned disable_after_error; /* Disable the protocol when error is detected */
};
struct bgp_conn {
@ -65,6 +70,8 @@ struct bgp_proto {
struct fib prefix_fib; /* Prefixes to be sent */
list bucket_queue; /* Queue of buckets to send */
struct bgp_bucket *withdraw_bucket; /* Withdrawn routes */
unsigned startup_delay; /* Time to delay protocol startup by due to errors */
bird_clock_t last_connect; /* Time of last connect attempt */
};
struct bgp_prefix {

View file

@ -16,6 +16,7 @@ CF_DECLS
CF_KEYWORDS(BGP, LOCAL, NEIGHBOR, AS, HOLD, TIME, CONNECT, RETRY, KEEPALIVE,
MULTIHOP, STARTUP, VIA, NEXT, HOP, SELF, DEFAULT, PATH, METRIC,
ERROR, START, DELAY, FORGET, WAIT, DISABLE, AFTER,
BGP_PATH, BGP_LOCAL_PREF, BGP_MED, BGP_ORIGIN, BGP_NEXT_HOP,
BGP_ATOMIC_AGGR, BGP_AGGREGATOR, BGP_COMMUNITY, SOURCE, ADDRESS)
@ -31,6 +32,10 @@ bgp_proto_start: proto_start BGP {
BGP_CFG->initial_hold_time = 240;
BGP_CFG->default_med = ~0; /* RFC 1771 doesn't specify this, draft-09 says ~0 */
BGP_CFG->compare_path_lengths = 1;
BGP_CFG->start_delay_time = 5;
BGP_CFG->error_amnesia_time = 300;
BGP_CFG->error_delay_time_min = 60;
BGP_CFG->error_delay_time_max = 300;
}
;
@ -56,6 +61,10 @@ bgp_proto:
| bgp_proto DEFAULT BGP_MED NUM ';' { BGP_CFG->default_med = $4; }
| bgp_proto DEFAULT BGP_LOCAL_PREF NUM ';' { BGP_CFG->default_local_pref = $4; }
| bgp_proto SOURCE ADDRESS IPA ';' { BGP_CFG->source_addr = $4; }
| bgp_proto START DELAY TIME NUM ';' { BGP_CFG->start_delay_time = $5; }
| bgp_proto ERROR FORGET TIME NUM ';' { BGP_CFG->error_amnesia_time = $5; }
| bgp_proto ERROR WAIT TIME NUM ',' NUM ';' { BGP_CFG->error_delay_time_min = $5; BGP_CFG->error_delay_time_max = $7; }
| bgp_proto DISABLE AFTER ERROR ';' { BGP_CFG->disable_after_error = 1; }
;
CF_ADDTO(dynamic_attr, BGP_PATH