Skip to content

Commit

Permalink
Merge pull request #4 from donaldsharp/more_error_stuff
Browse files Browse the repository at this point in the history
More error stuff
  • Loading branch information
qlyoung authored Jun 19, 2018
2 parents e00493d + 8578990 commit 741faf9
Show file tree
Hide file tree
Showing 39 changed files with 540 additions and 187 deletions.
60 changes: 60 additions & 0 deletions babeld/babel_errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* babel_errors - code for error messages that may occur in the
* babel process
* Copyright (C) 2018 Cumulus Networks, Inc.
* Donald Sharp
*
* FRR is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* FRR is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>

#include "babel_errors.h"

static struct ferr_ref ferr_babel_err[] = {
{
.code = BABEL_ERR_MEMORY,
.title = "BABEL Memory Errors",
.description = "Babel has failed to allocate memory, the system is about to run out of memory",
.suggestion = "Find the process that is causing memory shortages and remediate that process\nRestart FRR"
},
{
.code = BABEL_ERR_PACKET,
.title = "BABEL Packet Error",
.description = "Babel has detected a packet encode/decode problem",
.suggestion = "Collect relevant log files and file an Issue"
},
{
.code = BABEL_ERR_CONFIG,
.title = "BABEL Configuration Error",
.description = "Babel has detected a configuration error of some sort",
.suggestion = "Ensure that the configuration is correct"
},
{
.code = BABEL_ERR_ROUTE,
.title = "BABEL Route Error",
.description = "Babel has detected a routing error and has an inconsistent state",
.suggestion = "Gather data for filing an Issue and then restart FRR"
},
{
.code = END_FERR,
}
};

void babel_error_init(void)
{
ferr_ref_init();

ferr_ref_add(ferr_babel_err);
}
35 changes: 35 additions & 0 deletions babeld/babel_errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* babel_errors - header for error messages that may occur in the babel process
* Copyright (C) 2018 Cumulus Networks, Inc.
* Donald Sharp
*
* FRR is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* FRR is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __BABEL_ERRORS_H__
#define __BABEL_ERRORS_H__

#include "ferr.h"
#include "babel_errors.h"

enum babel_ferr_refs {
BABEL_ERR_MEMORY = BABEL_FERR_START,
BABEL_ERR_PACKET,
BABEL_ERR_CONFIG,
BABEL_ERR_ROUTE,
};

extern void babel_error_init(void);

#endif
32 changes: 18 additions & 14 deletions babeld/babel_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ THE SOFTWARE.
#include "prefix.h"
#include "vector.h"
#include "distribute.h"
#include "lib_errors.h"

#include "babel_main.h"
#include "util.h"
Expand All @@ -39,6 +40,7 @@ THE SOFTWARE.
#include "route.h"
#include "xroute.h"
#include "babel_memory.h"
#include "babel_errors.h"

#define IS_ENABLE(ifp) (babel_enable_if_lookup(ifp->name) >= 0)

Expand Down Expand Up @@ -167,7 +169,7 @@ babel_interface_address_add (int cmd, struct zclient *client,
if (babel_ifp->ipv4 == NULL) {
babel_ifp->ipv4 = malloc(4);
if (babel_ifp->ipv4 == NULL) {
zlog_err("not einough memory");
zlog_ferr(BABEL_ERR_MEMORY, "not enough memory");
} else {
memcpy(babel_ifp->ipv4, &prefix->u.prefix4, 4);
}
Expand Down Expand Up @@ -707,7 +709,7 @@ interface_recalculate(struct interface *ifp)
tmp = babel_ifp->sendbuf;
babel_ifp->sendbuf = realloc(babel_ifp->sendbuf, babel_ifp->bufsize);
if(babel_ifp->sendbuf == NULL) {
zlog_err("Couldn't reallocate sendbuf.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't reallocate sendbuf.");
free(tmp);
babel_ifp->bufsize = 0;
return -1;
Expand All @@ -727,8 +729,9 @@ interface_recalculate(struct interface *ifp)
rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char*)&mreq, sizeof(mreq));
if(rc < 0) {
zlog_err("setsockopt(IPV6_JOIN_GROUP) on interface '%s': %s",
ifp->name, safe_strerror(errno));
zlog_ferr(LIB_ERR_SOCKET,
"setsockopt(IPV6_JOIN_GROUP) on interface '%s': %s",
ifp->name, safe_strerror(errno));
/* This is probably due to a missing link-local address,
so down this interface, and wait until the main loop
tries to up it again. */
Expand Down Expand Up @@ -790,8 +793,9 @@ interface_reset(struct interface *ifp)
rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
(char*)&mreq, sizeof(mreq));
if(rc < 0)
zlog_err("setsockopt(IPV6_LEAVE_GROUP) on interface '%s': %s",
ifp->name, safe_strerror(errno));
zlog_ferr(LIB_ERR_SOCKET,
"setsockopt(IPV6_LEAVE_GROUP) on interface '%s': %s",
ifp->name, safe_strerror(errno));
}

update_interface_metric(ifp);
Expand Down Expand Up @@ -1056,7 +1060,7 @@ DEFUN (show_babel_route,
}
route_stream_done(routes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
xroutes = xroute_stream();
if(xroutes) {
Expand All @@ -1068,7 +1072,7 @@ DEFUN (show_babel_route,
}
xroute_stream_done(xroutes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
return CMD_SUCCESS;
}
Expand Down Expand Up @@ -1103,7 +1107,7 @@ DEFUN (show_babel_route_prefix,
}
route_stream_done(routes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
xroutes = xroute_stream();
if(xroutes) {
Expand All @@ -1115,7 +1119,7 @@ DEFUN (show_babel_route_prefix,
}
xroute_stream_done(xroutes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
return CMD_SUCCESS;
}
Expand Down Expand Up @@ -1161,7 +1165,7 @@ DEFUN (show_babel_route_addr,
}
route_stream_done(routes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
xroutes = xroute_stream();
if(xroutes) {
Expand All @@ -1173,7 +1177,7 @@ DEFUN (show_babel_route_addr,
}
xroute_stream_done(xroutes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
return CMD_SUCCESS;
}
Expand Down Expand Up @@ -1220,7 +1224,7 @@ DEFUN (show_babel_route_addr6,
}
route_stream_done(routes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
xroutes = xroute_stream();
if(xroutes) {
Expand All @@ -1232,7 +1236,7 @@ DEFUN (show_babel_route_addr6,
}
xroute_stream_done(xroutes);
} else {
zlog_err("Couldn't allocate route stream.");
zlog_ferr(BABEL_ERR_MEMORY, "Couldn't allocate route stream.");
}
return CMD_SUCCESS;
}
Expand Down
37 changes: 24 additions & 13 deletions babeld/babel_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ THE SOFTWARE.
#include "vty.h"
#include "memory.h"
#include "libfrr.h"
#include "lib_errors.h"

#include "babel_main.h"
#include "babeld.h"
Expand All @@ -45,6 +46,7 @@ THE SOFTWARE.
#include "message.h"
#include "resend.h"
#include "babel_zebra.h"
#include "babel_errors.h"

static void babel_fail(void);
static void babel_init_random(void);
Expand Down Expand Up @@ -151,7 +153,7 @@ main(int argc, char **argv)

frr_preinit (&babeld_di, argc, argv);
frr_opt_add ("", longopts, "");

babel_init_random();

/* set the Babel's default link-local multicast address and Babel's port */
Expand Down Expand Up @@ -181,6 +183,7 @@ main(int argc, char **argv)
master = frr_init ();

/* Library inits. */
babel_error_init();
zprivs_init (&babeld_privs);
cmd_init (1);
vty_init (master);
Expand Down Expand Up @@ -228,7 +231,8 @@ babel_init_random(void)

rc = read_random_bytes(&seed, sizeof(seed));
if(rc < 0) {
zlog_err("read(random): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "read(random): %s",
safe_strerror(errno));
seed = 42;
}

Expand All @@ -248,13 +252,14 @@ babel_replace_by_null(int fd)

fd_null = open("/dev/null", O_RDONLY);
if(fd_null < 0) {
zlog_err("open(null): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "open(null): %s", safe_strerror(errno));
exit(1);
}

rc = dup2(fd_null, fd);
if(rc < 0) {
zlog_err("dup2(null, 0): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "dup2(null, 0): %s",
safe_strerror(errno));
exit(1);
}

Expand All @@ -273,10 +278,12 @@ babel_load_state_file(void)

fd = open(state_file, O_RDONLY);
if(fd < 0 && errno != ENOENT)
zlog_err("open(babel-state: %s)", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "open(babel-state: %s)",
safe_strerror(errno));
rc = unlink(state_file);
if(fd >= 0 && rc < 0) {
zlog_err("unlink(babel-state): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "unlink(babel-state): %s",
safe_strerror(errno));
/* If we couldn't unlink it, it's probably stale. */
goto fini;
}
Expand All @@ -287,15 +294,16 @@ babel_load_state_file(void)
long t;
rc = read(fd, buf, 99);
if(rc < 0) {
zlog_err("read(babel-state): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "read(babel-state): %s",
safe_strerror(errno));
} else {
buf[rc] = '\0';
rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
if(rc == 3 && s >= 0 && s <= 0xFFFF) {
unsigned char sid[8];
rc = parse_eui64(buf2, sid);
if(rc < 0) {
zlog_err("Couldn't parse babel-state.");
zlog_ferr(BABEL_ERR_CONFIG, "Couldn't parse babel-state.");
} else {
struct timeval realnow;
debugf(BABEL_DEBUG_COMMON,
Expand All @@ -305,12 +313,13 @@ babel_load_state_file(void)
if(memcmp(sid, myid, 8) == 0)
myseqno = seqno_plus(s, 1);
else
zlog_err("ID mismatch in babel-state. id=%s; old=%s",
zlog_ferr(BABEL_ERR_CONFIG,
"ID mismatch in babel-state. id=%s; old=%s",
format_eui64(myid),
format_eui64(sid));
}
} else {
zlog_err("Couldn't parse babel-state.");
zlog_ferr(BABEL_ERR_CONFIG, "Couldn't parse babel-state.");
}
}
goto fini;
Expand Down Expand Up @@ -350,7 +359,8 @@ babel_save_state_file(void)
debugf(BABEL_DEBUG_COMMON, "Save state file.");
fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if(fd < 0) {
zlog_err("creat(babel-state): %s", safe_strerror(errno));
zlog_ferr(LIB_ERR_SYSTEM_CALL, "creat(babel-state): %s",
safe_strerror(errno));
unlink(state_file);
} else {
struct timeval realnow;
Expand All @@ -360,12 +370,13 @@ babel_save_state_file(void)
format_eui64(myid), (int)myseqno,
(long)realnow.tv_sec);
if(rc < 0 || rc >= 100) {
zlog_err("write(babel-state): overflow.");
zlog_ferr(BABEL_ERR_CONFIG, "write(babel-state): overflow.");
unlink(state_file);
} else {
rc = write(fd, buf, rc);
if(rc < 0) {
zlog_err("write(babel-state): %s", safe_strerror(errno));
zlog_ferr(BABEL_ERR_CONFIG, "write(babel-state): %s",
safe_strerror(errno));
unlink(state_file);
}
fsync(fd);
Expand Down
Loading

0 comments on commit 741faf9

Please sign in to comment.