summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am6
-rw-r--r--lib/command.c294
-rw-r--r--lib/filter.c2
-rw-r--r--lib/heap.c517
-rw-r--r--lib/heap.h160
-rw-r--r--lib/log.c651
-rw-r--r--lib/log.h30
-rw-r--r--lib/memory.c40
-rw-r--r--lib/memory.h10
-rw-r--r--lib/memtypes.c26
-rw-r--r--lib/mqueue.c575
-rw-r--r--lib/mqueue.h141
-rw-r--r--lib/plist.c2366
-rw-r--r--lib/plist.h45
-rw-r--r--lib/prefix.c92
-rw-r--r--lib/privs.c203
-rw-r--r--lib/privs.h4
-rw-r--r--lib/qpnexus.c120
-rw-r--r--lib/qpnexus.h86
-rw-r--r--lib/qpselect.c1321
-rw-r--r--lib/qpselect.h208
-rw-r--r--lib/qpthreads.c570
-rw-r--r--lib/qpthreads.h363
-rw-r--r--lib/qtime.c194
-rw-r--r--lib/qtime.h307
-rw-r--r--lib/qtimers.c336
-rw-r--r--lib/qtimers.h144
-rw-r--r--lib/routemap.c2
-rw-r--r--lib/symtab.c1181
-rw-r--r--lib/symtab.h318
-rw-r--r--lib/vector.c1206
-rw-r--r--lib/vector.h277
-rw-r--r--lib/vty.c996
-rw-r--r--lib/vty.h33
-rw-r--r--lib/zassert.h50
35 files changed, 11080 insertions, 1794 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 315e919b..3184a9b6 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -12,7 +12,8 @@ libzebra_la_SOURCES = \
sockunion.c prefix.c thread.c if.c memory.c buffer.c table.c hash.c \
filter.c routemap.c distribute.c stream.c str.c log.c plist.c \
zclient.c sockopt.c smux.c md5.c if_rmap.c keychain.c privs.c \
- sigevent.c pqueue.c jhash.c memtypes.c workqueue.c
+ sigevent.c pqueue.c jhash.c memtypes.c workqueue.c symtab.c heap.c \
+ qtime.c qpthreads.c mqueue.c qpselect.c qtimers.c qpnexus.c
BUILT_SOURCES = memtypes.h route_types.h
@@ -27,7 +28,8 @@ pkginclude_HEADERS = \
str.h stream.h table.h thread.h vector.h version.h vty.h zebra.h \
plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h \
- workqueue.h route_types.h
+ workqueue.h route_types.h symtab.h heap.h \
+ qtime.h qpthreads.h mqueue.h qpselect.h qtimers.h qpnexus.h
EXTRA_DIST = regex.c regex-gnu.h memtypes.awk route_types.awk route_types.txt
diff --git a/lib/command.c b/lib/command.c
index 31c067a3..80b113e8 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -1,11 +1,11 @@
/*
$Id$
-
+
Command interpreter routine for virtual terminal [aka TeletYpe]
Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
This file is part of GNU Zebra.
-
+
GNU Zebra 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
@@ -93,7 +93,7 @@ static struct facility_map {
int facility;
const char *name;
size_t match;
-} syslog_facilities[] =
+} syslog_facilities[] =
{
{ LOG_KERN, "kern", 1 },
{ LOG_USER, "user", 2 },
@@ -145,7 +145,7 @@ static int
level_match(const char *s)
{
int level ;
-
+
for ( level = 0 ; zlog_priority [level] != NULL ; level ++ )
if (!strncmp (s, zlog_priority[level], 2))
return level;
@@ -160,7 +160,7 @@ print_version (const char *progname)
printf ("%s\n", QUAGGA_COPYRIGHT);
}
-
+
/* Utility function to concatenate argv argument into a single string
with inserting ' ' character between each argument. */
char *
@@ -190,30 +190,24 @@ argv_concat (const char **argv, int argc, int shift)
/* Install top node of command vector. */
void
-install_node (struct cmd_node *node,
+install_node (struct cmd_node *node,
int (*func) (struct vty *))
{
vector_set_index (cmdvec, node->node, node);
node->func = func;
- node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
+ node->cmd_vector = vector_init (0);
}
/* Compare two command's string. Used in sort_node (). */
static int
-cmp_node (const void *p, const void *q)
+cmp_node (const struct cmd_element *a, const struct cmd_element *b)
{
- const struct cmd_element *a = *(struct cmd_element * const *)p;
- const struct cmd_element *b = *(struct cmd_element * const *)q;
-
return strcmp (a->string, b->string);
}
static int
-cmp_desc (const void *p, const void *q)
+cmp_desc (const struct desc *a, const struct desc *b)
{
- const struct desc *a = *(struct desc * const *)p;
- const struct desc *b = *(struct desc * const *)q;
-
return strcmp (a->cmd, b->cmd);
}
@@ -223,24 +217,21 @@ sort_node ()
{
unsigned int i, j;
struct cmd_node *cnode;
- vector descvec;
struct cmd_element *cmd_element;
for (i = 0; i < vector_active (cmdvec); i++)
if ((cnode = vector_slot (cmdvec, i)) != NULL)
- {
+ {
vector cmd_vector = cnode->cmd_vector;
- qsort (cmd_vector->index, vector_active (cmd_vector),
- sizeof (void *), cmp_node);
+ vector_sort(cmd_vector, (vector_sort_cmp*)cmp_node) ;
for (j = 0; j < vector_active (cmd_vector); j++)
if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
&& vector_active (cmd_element->strvec))
{
- descvec = vector_slot (cmd_element->strvec,
+ vector descvec = vector_slot (cmd_element->strvec,
vector_active (cmd_element->strvec) - 1);
- qsort (descvec->index, vector_active (descvec),
- sizeof (void *), cmp_desc);
+ vector_sort(descvec, (vector_sort_cmp*)cmp_desc) ;
}
}
}
@@ -255,10 +246,10 @@ cmd_make_strvec (const char *string)
char *token;
int strlen;
vector strvec;
-
+
if (string == NULL)
return NULL;
-
+
cp = string;
/* Skip white spaces. */
@@ -273,10 +264,10 @@ cmd_make_strvec (const char *string)
return NULL;
/* Prepare return vector. */
- strvec = vector_init (VECTOR_MIN_SIZE);
+ strvec = vector_init (0);
/* Copy each command piece and set into vector. */
- while (1)
+ while (1)
{
start = cp;
while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') &&
@@ -321,7 +312,7 @@ cmd_desc_str (const char **string)
const char *cp, *start;
char *token;
int strlen;
-
+
cp = *string;
if (cp == NULL)
@@ -370,7 +361,7 @@ cmd_make_descvec (const char *string, const char *descstr)
if (cp == NULL)
return NULL;
- allvec = vector_init (VECTOR_MIN_SIZE);
+ allvec = vector_init (0);
while (1)
{
@@ -396,7 +387,7 @@ cmd_make_descvec (const char *string, const char *descstr)
}
cp++;
}
-
+
while (isspace ((int) *cp) && *cp != '\0')
cp++;
@@ -406,7 +397,7 @@ cmd_make_descvec (const char *string, const char *descstr)
cp++;
}
- if (*cp == '\0')
+ if (*cp == '\0')
return allvec;
sp = cp;
@@ -428,14 +419,14 @@ cmd_make_descvec (const char *string, const char *descstr)
{
if (multiple == 1)
{
- strvec = vector_init (VECTOR_MIN_SIZE);
+ strvec = vector_init (0);
vector_set (allvec, strvec);
}
multiple++;
}
else
{
- strvec = vector_init (VECTOR_MIN_SIZE);
+ strvec = vector_init (0);
vector_set (allvec, strvec);
}
vector_set (strvec, desc);
@@ -484,14 +475,14 @@ void
install_element (enum node_type ntype, struct cmd_element *cmd)
{
struct cmd_node *cnode;
-
+
/* cmd_init hasn't been called */
if (!cmdvec)
return;
-
+
cnode = vector_slot (cmdvec, ntype);
- if (cnode == NULL)
+ if (cnode == NULL)
{
fprintf (stderr, "Command node %d doesn't exist, please check it\n",
ntype);
@@ -506,13 +497,13 @@ install_element (enum node_type ntype, struct cmd_element *cmd)
cmd->cmdsize = cmd_cmdsize (cmd->strvec);
}
-static unsigned char itoa64[] =
+static unsigned char itoa64[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static void
to64(char *s, long v, int n)
{
- while (--n >= 0)
+ while (--n >= 0)
{
*s++ = itoa64[v&0x3f];
v >>= 6;
@@ -527,7 +518,7 @@ zencrypt (const char *passwd)
char *crypt (const char *, const char *);
gettimeofday(&tv,0);
-
+
to64(&salt[0], random(), 3);
to64(&salt[3], tv.tv_usec, 3);
salt[5] = '\0';
@@ -545,9 +536,9 @@ config_write_host (struct vty *vty)
if (host.encrypt)
{
if (host.password_encrypt)
- vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE);
+ vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE);
if (host.enable_encrypt)
- vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE);
+ vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE);
}
else
{
@@ -557,57 +548,57 @@ config_write_host (struct vty *vty)
vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE);
}
- if (zlog_default->default_lvl != LOG_DEBUG)
+ if (zlog_get_default_lvl(NULL) != LOG_DEBUG)
{
vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s",
VTY_NEWLINE);
vty_out (vty, "log trap %s%s",
- zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
+ zlog_priority[zlog_get_default_lvl(NULL)], VTY_NEWLINE);
}
- if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED))
+ if (host.logfile && (zlog_get_maxlvl(NULL, ZLOG_DEST_FILE) != ZLOG_DISABLED))
{
vty_out (vty, "log file %s", host.logfile);
- if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_FILE) != zlog_get_default_lvl(NULL))
vty_out (vty, " %s",
- zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_FILE)]);
vty_out (vty, "%s", VTY_NEWLINE);
}
- if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_STDOUT) != ZLOG_DISABLED)
{
vty_out (vty, "log stdout");
- if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_STDOUT) != zlog_get_default_lvl(NULL))
vty_out (vty, " %s",
- zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_STDOUT)]);
vty_out (vty, "%s", VTY_NEWLINE);
}
- if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_MONITOR) == ZLOG_DISABLED)
vty_out(vty,"no log monitor%s",VTY_NEWLINE);
- else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl)
+ else if (zlog_get_maxlvl(NULL, ZLOG_DEST_MONITOR) != zlog_get_default_lvl(NULL))
vty_out(vty,"log monitor %s%s",
- zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_MONITOR)],VTY_NEWLINE);
- if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_SYSLOG) != ZLOG_DISABLED)
{
vty_out (vty, "log syslog");
- if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_SYSLOG) != zlog_get_default_lvl(NULL))
vty_out (vty, " %s",
- zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_SYSLOG)]);
vty_out (vty, "%s", VTY_NEWLINE);
}
- if (zlog_default->facility != LOG_DAEMON)
+ if (zlog_get_facility(NULL) != LOG_DAEMON)
vty_out (vty, "log facility %s%s",
- facility_name(zlog_default->facility), VTY_NEWLINE);
+ facility_name(zlog_get_facility(NULL)), VTY_NEWLINE);
- if (zlog_default->record_priority == 1)
+ if (zlog_get_record_priority(NULL) == 1)
vty_out (vty, "log record-priority%s", VTY_NEWLINE);
- if (zlog_default->timestamp_precision > 0)
+ if (zlog_get_timestamp_precision(NULL) > 0)
vty_out (vty, "log timestamp precision %d%s",
- zlog_default->timestamp_precision, VTY_NEWLINE);
+ zlog_get_timestamp_precision(NULL), VTY_NEWLINE);
if (host.advanced)
vty_out (vty, "service advanced-vty%s", VTY_NEWLINE);
@@ -684,7 +675,7 @@ cmd_filter_by_symbol (char *command, char *symbol)
#endif
/* Completion match types. */
-enum match_type
+enum match_type
{
no_match,
extend_match,
@@ -695,7 +686,7 @@ enum match_type
range_match,
vararg_match,
partly_match,
- exact_match
+ exact_match
};
static enum match_type
@@ -866,7 +857,7 @@ cmd_ipv6_match (const char *str)
* ::1.2.3.4
*/
ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
-
+
if (ret == 1)
return exact_match;
@@ -1071,7 +1062,7 @@ cmd_ipv6_prefix_match (const char *str)
if (mask < 0 || mask > 128)
return no_match;
-
+
/* I don't know why mask < 13 makes command match partly.
Forgive me to make this comments. I Want to set static default route
because of lack of function to originate default in ospf6d; sorry
@@ -1162,7 +1153,7 @@ cmd_filter_by_completion (char *command, vector v, unsigned int index)
if ((desc = vector_slot (descvec, j)))
{
str = desc->cmd;
-
+
if (CMD_VARARG (str))
{
if (match_type < vararg_match)
@@ -1378,7 +1369,7 @@ is_cmd_ambiguous (char *command, vector v, int index, enum match_type type)
if ((desc = vector_slot (descvec, j)))
{
enum match_type ret;
-
+
str = desc->cmd;
switch (type)
@@ -1567,7 +1558,7 @@ desc_unique_string (vector v, const char *str)
return 0;
}
-static int
+static int
cmd_try_do_shortcut (enum node_type node, char* first_word) {
if ( first_word != NULL &&
node != AUTH_NODE &&
@@ -1602,7 +1593,7 @@ cmd_describe_command_real (vector vline, struct vty *vty, int *status)
}
else
index = vector_active (vline) - 1;
-
+
/* Make copy vector of current node's command vector. */
cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
@@ -1615,7 +1606,7 @@ cmd_describe_command_real (vector vline, struct vty *vty, int *status)
if ((command = vector_slot (vline, i)))
{
match = cmd_filter_by_completion (command, cmd_vector, i);
-
+
if (match == vararg_match)
{
struct cmd_element *cmd_element;
@@ -1634,7 +1625,7 @@ cmd_describe_command_real (vector vline, struct vty *vty, int *status)
vector_set (matchvec, desc);
}
}
-
+
vector_set (matchvec, &desc_cr);
vector_free (cmd_vector);
@@ -1734,7 +1725,7 @@ cmd_describe_command (vector vline, struct vty *vty, int *status)
shifted_vline = vector_init (vector_count(vline));
/* use memcpy? */
- for (index = 1; index < vector_active (vline); index++)
+ for (index = 1; index < vector_active (vline); index++)
{
vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
}
@@ -1836,7 +1827,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
}
*/
}
-
+
/* Prepare match vector. */
matchvec = vector_init (INIT_MATCHVEC_SIZE);
@@ -1858,7 +1849,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
for (j = 0; j < vector_active (descvec); j++)
if ((desc = vector_slot (descvec, j)))
{
- if ((string =
+ if ((string =
cmd_entry_function (vector_slot (vline, index),
desc->cmd)))
if (cmd_unique_string (matchvec, string))
@@ -1884,10 +1875,11 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
return NULL;
}
+/* XXX: TODO: stop poking around inside vector */
/* Only one matched */
if (vector_slot (matchvec, 1) == NULL)
{
- match_str = (char **) matchvec->index;
+ match_str = (char **) matchvec->VECTOR_INDEX;
vector_only_wrapper_free (matchvec);
*status = CMD_COMPLETE_FULL_MATCH;
return match_str;
@@ -1898,7 +1890,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
/* Check LCD of matched strings. */
if (vector_slot (vline, index) != NULL)
{
- lcd = cmd_lcd ((char **) matchvec->index);
+ lcd = cmd_lcd ((char **) matchvec->VECTOR_INDEX);
if (lcd)
{
@@ -1909,7 +1901,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
char *lcdstr;
lcdstr = XMALLOC (MTYPE_STRVEC, lcd + 1);
- memcpy (lcdstr, matchvec->index[0], lcd);
+ memcpy (lcdstr, matchvec->VECTOR_INDEX[0], lcd);
lcdstr[lcd] = '\0';
/* match_str = (char **) &lcdstr; */
@@ -1925,7 +1917,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
/* Make new matchvec. */
matchvec = vector_init (INIT_MATCHVEC_SIZE);
vector_set (matchvec, lcdstr);
- match_str = (char **) matchvec->index;
+ match_str = (char **) matchvec->VECTOR_INDEX;
vector_only_wrapper_free (matchvec);
*status = CMD_COMPLETE_MATCH;
@@ -1934,7 +1926,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
}
}
- match_str = (char **) matchvec->index;
+ match_str = (char **) matchvec->VECTOR_INDEX;
vector_only_wrapper_free (matchvec);
*status = CMD_COMPLETE_LIST_MATCH;
return match_str;
@@ -1957,7 +1949,7 @@ cmd_complete_command (vector vline, struct vty *vty, int *status)
shifted_vline = vector_init (vector_count(vline));
/* use memcpy? */
- for (index = 1; index < vector_active (vline); index++)
+ for (index = 1; index < vector_active (vline); index++)
{
vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
}
@@ -2030,7 +2022,7 @@ cmd_execute_command_real (vector vline, struct vty *vty,
if (match == vararg_match)
break;
-
+
ret = is_cmd_ambiguous (command, cmd_vector, index, match);
if (ret == 1)
@@ -2141,7 +2133,7 @@ cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
shifted_vline = vector_init (vector_count(vline));
/* use memcpy? */
- for (index = 1; index < vector_active (vline); index++)
+ for (index = 1; index < vector_active (vline); index++)
{
vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
}
@@ -2160,7 +2152,7 @@ cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
return saved_ret;
/* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
- while ( ret != CMD_SUCCESS && ret != CMD_WARNING
+ while ( ret != CMD_SUCCESS && ret != CMD_WARNING
&& vty->node > CONFIG_NODE )
{
try_node = node_parent(try_node);
@@ -2204,14 +2196,14 @@ cmd_execute_command_strict (vector vline, struct vty *vty,
if ((command = vector_slot (vline, index)))
{
int ret;
-
+
match = cmd_filter_by_string (vector_slot (vline, index),
cmd_vector, index);
/* If command meets '.VARARG' then finish matching. */
if (match == vararg_match)
break;
-
+
ret = is_cmd_ambiguous (command, cmd_vector, index, match);
if (ret == 1)
{
@@ -2351,7 +2343,7 @@ DEFUN (config_terminal,
}
/* Enable command */
-DEFUN (enable,
+DEFUN (enable,
config_enable_cmd,
"enable",
"Turn on privileged mode command\n")
@@ -2367,7 +2359,7 @@ DEFUN (enable,
}
/* Disable command */
-DEFUN (disable,
+DEFUN (disable,
config_disable_cmd,
"disable",
"Turn off privileged mode command\n")
@@ -2432,7 +2424,7 @@ ALIAS (config_exit,
config_quit_cmd,
"quit",
"Exit current mode and down to previous mode\n")
-
+
/* End of configuration. */
DEFUN (config_end,
config_end_cmd,
@@ -2494,7 +2486,7 @@ DEFUN (config_help,
"help",
"Description of the interactive help system\n")
{
- vty_out (vty,
+ vty_out (vty,
"Quagga VTY provides advanced help feature. When you need help,%s\
anytime at the command line please press '?'.%s\
%s\
@@ -2532,9 +2524,9 @@ DEFUN (config_list,
}
/* Write current configuration into file. */
-DEFUN (config_write_file,
+DEFUN (config_write_file,
config_write_file_cmd,
- "write file",
+ "write file",
"Write running configuration to memory, network, or terminal\n"
"Write to configuration file\n")
{
@@ -2557,7 +2549,7 @@ DEFUN (config_write_file,
/* Get filename. */
config_file = host.config;
-
+
config_file_sav =
XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
strcpy (config_file_sav, config_file);
@@ -2566,7 +2558,7 @@ DEFUN (config_write_file,
config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
sprintf (config_file_tmp, "%s.XXXXXX", config_file);
-
+
/* Open file to configuration write. */
fd = mkstemp (config_file_tmp);
if (fd < 0)
@@ -2575,10 +2567,9 @@ DEFUN (config_write_file,
VTY_NEWLINE);
goto finished;
}
-
+
/* Make vty for configuration file. */
- file_vty = vty_new ();
- file_vty->fd = fd;
+ file_vty = vty_new (fd);
file_vty->type = VTY_FILE;
/* Config file header print. */
@@ -2621,10 +2612,10 @@ DEFUN (config_write_file,
goto finished;
}
sync ();
-
+
if (chmod (config_file, CONFIGFILE_MASK) != 0)
{
- vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
+ vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
config_file, safe_strerror(errno), errno, VTY_NEWLINE);
goto finished;
}
@@ -2640,20 +2631,20 @@ finished:
return ret;
}
-ALIAS (config_write_file,
+ALIAS (config_write_file,
config_write_cmd,
- "write",
+ "write",
"Write running configuration to memory, network, or terminal\n")
-ALIAS (config_write_file,
+ALIAS (config_write_file,
config_write_memory_cmd,
- "write memory",
+ "write memory",
"Write running configuration to memory, network, or terminal\n"
"Write configuration to the file (same as write file)\n")
-ALIAS (config_write_file,
+ALIAS (config_write_file,
copy_runningconfig_startupconfig_cmd,
- "copy running-config startup-config",
+ "copy running-config startup-config",
"Copy configuration\n"
"Copy running config to... \n"
"Copy running config to startup config (same as write file)\n")
@@ -2736,7 +2727,7 @@ DEFUN (show_startup_config,
}
/* Hostname configuration */
-DEFUN (config_hostname,
+DEFUN (config_hostname,
hostname_cmd,
"hostname WORD",
"Set system's network name\n"
@@ -2750,12 +2741,12 @@ DEFUN (config_hostname,
if (host.name)
XFREE (MTYPE_HOST, host.name);
-
+
host.name = XSTRDUP (MTYPE_HOST, argv[0]);
return CMD_SUCCESS;
}
-DEFUN (config_no_hostname,
+DEFUN (config_no_hostname,
no_hostname_cmd,
"no hostname [HOSTNAME]",
NO_STR
@@ -2804,7 +2795,7 @@ DEFUN (config_password, password_cmd,
if (!isalnum ((int) *argv[0]))
{
- vty_out (vty,
+ vty_out (vty,
"Please specify string starting with alphanumeric%s", VTY_NEWLINE);
return CMD_WARNING;
}
@@ -2870,7 +2861,7 @@ DEFUN (config_enable_password, enable_password_cmd,
if (!isalnum ((int) *argv[0]))
{
- vty_out (vty,
+ vty_out (vty,
"Please specify string starting with alphanumeric%s", VTY_NEWLINE);
return CMD_WARNING;
}
@@ -2916,7 +2907,7 @@ DEFUN (no_config_enable_password, no_enable_password_cmd,
return CMD_SUCCESS;
}
-
+
DEFUN (service_password_encrypt,
service_password_encrypt_cmd,
"service password-encryption",
@@ -3068,49 +3059,51 @@ DEFUN (show_logging,
SHOW_STR
"Show current logging configuration\n")
{
- struct zlog *zl = zlog_default;
-
vty_out (vty, "Syslog logging: ");
- if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_SYSLOG) == ZLOG_DISABLED)
vty_out (vty, "disabled");
else
vty_out (vty, "level %s, facility %s, ident %s",
- zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
- facility_name(zl->facility), zl->ident);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_SYSLOG)],
+ facility_name(zlog_get_facility(NULL)), zlog_get_ident(NULL));
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "Stdout logging: ");
- if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_STDOUT) == ZLOG_DISABLED)
vty_out (vty, "disabled");
else
vty_out (vty, "level %s",
- zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_STDOUT)]);
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "Monitor logging: ");
- if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
+ if (zlog_get_maxlvl(NULL, ZLOG_DEST_MONITOR) == ZLOG_DISABLED)
vty_out (vty, "disabled");
else
vty_out (vty, "level %s",
- zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_MONITOR)]);
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "File logging: ");
- if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
- !zl->fp)
+ if ((zlog_get_maxlvl(NULL, ZLOG_DEST_FILE) == ZLOG_DISABLED) ||
+ !zlog_is_file(NULL))
vty_out (vty, "disabled");
else
- vty_out (vty, "level %s, filename %s",
- zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
- zl->filename);
+ {
+ char * filename = zlog_get_filename(NULL);
+ vty_out (vty, "level %s, filename %s",
+ zlog_priority[zlog_get_maxlvl(NULL, ZLOG_DEST_FILE)],
+ filename);
+ free(filename);
+ }
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "Protocol name: %s%s",
- zlog_proto_names[zl->protocol], VTY_NEWLINE);
+ zlog_get_proto_name(NULL), VTY_NEWLINE);
vty_out (vty, "Record priority: %s%s",
- (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
+ (zlog_get_record_priority(NULL) ? "enabled" : "disabled"), VTY_NEWLINE);
vty_out (vty, "Timestamp precision: %d%s",
- zl->timestamp_precision, VTY_NEWLINE);
+ zlog_get_timestamp_precision(NULL), VTY_NEWLINE);
return CMD_SUCCESS;
}
@@ -3121,7 +3114,7 @@ DEFUN (config_log_stdout,
"Logging control\n"
"Set stdout logging level\n")
{
- zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
+ zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_get_default_lvl(NULL));
return CMD_SUCCESS;
}
@@ -3158,7 +3151,7 @@ DEFUN (config_log_monitor,
"Logging control\n"
"Set terminal line (monitor) logging level\n")
{
- zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
+ zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_get_default_lvl(NULL));
return CMD_SUCCESS;
}
@@ -3195,19 +3188,19 @@ set_log_file(struct vty *vty, const char *fname, int loglevel)
int ret;
char *p = NULL;
const char *fullpath;
-
+
/* Path detection. */
if (! IS_DIRECTORY_SEP (*fname))
{
char cwd[MAXPATHLEN+1];
cwd[MAXPATHLEN] = '\0';
-
+
if (getcwd (cwd, MAXPATHLEN) == NULL)
{
zlog_err ("config_log_file: Unable to alloc mem!");
return CMD_WARNING;
}
-
+
if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
== NULL)
{
@@ -3246,7 +3239,7 @@ DEFUN (config_log_file,
"Logging to file\n"
"Logging filename\n")
{
- return set_log_file(vty, argv[0], zlog_default->default_lvl);
+ return set_log_file(vty, argv[0], zlog_get_default_lvl(NULL));
}
DEFUN (config_log_file_level,
@@ -3297,7 +3290,7 @@ DEFUN (config_log_syslog,
"Logging control\n"
"Set syslog logging level\n")
{
- zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
+ zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_get_default_lvl(NULL));
return CMD_SUCCESS;
}
@@ -3329,8 +3322,8 @@ DEFUN_DEPRECATED (config_log_syslog_facility,
if ((facility = facility_match(argv[0])) < 0)
return CMD_ERR_NO_MATCH;
- zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
- zlog_default->facility = facility;
+ zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_get_default_lvl(NULL));
+ zlog_set_facility(NULL, facility);
return CMD_SUCCESS;
}
@@ -3366,7 +3359,7 @@ DEFUN (config_log_facility,
if ((facility = facility_match(argv[0])) < 0)
return CMD_ERR_NO_MATCH;
- zlog_default->facility = facility;
+ zlog_set_facility(NULL, facility);
return CMD_SUCCESS;
}
@@ -3378,7 +3371,7 @@ DEFUN (no_config_log_facility,
"Reset syslog facility to default (daemon)\n"
"Syslog facility\n")
{
- zlog_default->facility = LOG_DAEMON;
+ zlog_set_facility(NULL, LOG_DAEMON);
return CMD_SUCCESS;
}
@@ -3391,14 +3384,11 @@ DEFUN_DEPRECATED (config_log_trap,
{
int new_level ;
int i;
-
+
if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
return CMD_ERR_NO_MATCH;
- zlog_default->default_lvl = new_level;
- for (i = 0; i < ZLOG_NUM_DESTS; i++)
- if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
- zlog_default->maxlvl[i] = new_level;
+ zlog_set_default_lvl_dest (NULL, new_level);
return CMD_SUCCESS;
}
@@ -3410,7 +3400,7 @@ DEFUN_DEPRECATED (no_config_log_trap,
"Permit all logging information\n"
"Logging level\n")
{
- zlog_default->default_lvl = LOG_DEBUG;
+ zlog_set_default_lvl(NULL, LOG_DEBUG);
return CMD_SUCCESS;
}
@@ -3420,7 +3410,7 @@ DEFUN (config_log_record_priority,
"Logging control\n"
"Log the priority of the message within the message\n")
{
- zlog_default->record_priority = 1 ;
+ zlog_set_record_priority(NULL, 1) ;
return CMD_SUCCESS;
}
@@ -3431,7 +3421,7 @@ DEFUN (no_config_log_record_priority,
"Logging control\n"
"Do not log the priority of the message within the message\n")
{
- zlog_default->record_priority = 0 ;
+ zlog_set_record_priority(NULL, 0) ;
return CMD_SUCCESS;
}
@@ -3443,6 +3433,8 @@ DEFUN (config_log_timestamp_precision,
"Set the timestamp precision\n"
"Number of subsecond digits\n")
{
+ int timestamp_precision;
+
if (argc != 1)
{
vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
@@ -3450,7 +3442,9 @@ DEFUN (config_log_timestamp_precision,
}
VTY_GET_INTEGER_RANGE("Timestamp Precision",
- zlog_default->timestamp_precision, argv[0], 0, 6);
+ timestamp_precision, argv[0], 0, 6);
+ zlog_set_timestamp_precision(NULL, timestamp_precision);
+
return CMD_SUCCESS;
}
@@ -3462,7 +3456,7 @@ DEFUN (no_config_log_timestamp_precision,
"Timestamp configuration\n"
"Reset the timestamp precision to the default value of 0\n")
{
- zlog_default->timestamp_precision = 0 ;
+ zlog_set_timestamp_precision(NULL, 0);
return CMD_SUCCESS;
}
@@ -3500,7 +3494,7 @@ DEFUN (no_banner_motd,
"Strings for motd\n")
{
host.motd = NULL;
- if (host.motdfile)
+ if (host.motdfile)
XFREE (MTYPE_HOST, host.motdfile);
host.motdfile = NULL;
return CMD_SUCCESS;
@@ -3540,7 +3534,7 @@ cmd_init (int terminal)
desc_cr.str = XSTRDUP(MTYPE_STRVEC, "");
/* Allocate initial top vector of commands. */
- cmdvec = vector_init (VECTOR_MIN_SIZE);
+ cmdvec = vector_init (0);
/* Default host value settings. */
host.name = NULL;
@@ -3604,7 +3598,7 @@ cmd_init (int terminal)
install_default (CONFIG_NODE);
}
-
+
install_element (CONFIG_NODE, &hostname_cmd);
install_element (CONFIG_NODE, &no_hostname_cmd);
@@ -3667,7 +3661,7 @@ cmd_terminate ()
if (cmdvec)
{
- for (i = 0; i < vector_active (cmdvec); i++)
+ for (i = 0; i < vector_active (cmdvec); i++)
if ((cmd_node = vector_slot (cmdvec, i)) != NULL)
{
cmd_node_v = cmd_node->cmd_vector;
diff --git a/lib/filter.c b/lib/filter.c
index af8d587f..57109854 100644
--- a/lib/filter.c
+++ b/lib/filter.c
@@ -1584,7 +1584,7 @@ filter_show (struct vty *vty, const char *name, afi_t afi)
/* Print the name of the protocol */
if (zlog_default)
vty_out (vty, "%s:%s",
- zlog_proto_names[zlog_default->protocol], VTY_NEWLINE);
+ zlog_get_proto_name(NULL), VTY_NEWLINE);
for (access = master->num.head; access; access = access->next)
{
diff --git a/lib/heap.c b/lib/heap.c
new file mode 100644
index 00000000..35a1b51d
--- /dev/null
+++ b/lib/heap.c
@@ -0,0 +1,517 @@
+/* Generic heap data structure -- functions.
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include "zassert.h"
+#include "heap.h"
+#include "memory.h"
+
+/* Heaps are implemented as a structure which includes a vector structure.
+ * So items in the heap are items in a vector, which are pointers to the item
+ * values.
+ *
+ * The heap structure may be statically allocated, embedded in another
+ * structure, or allocated dynamically. In any case the heap operations
+ * require the address of the heap structure -- see typedef for heap.
+ *
+ * An essential component of a heap is its comparison function. So, a heap
+ * CANNOT be used before it has been initialised and the comparison function
+ * set. (This is unlike a vector, which may be implicitly initialised empty by
+ * zeroising the vector structure.)
+ *
+ * Items may be pushed onto or popped off the heap, which is organised so that
+ * the top item has the smallest value -- according to the heap's comparison
+ * function. (For equal values, the order is undefined.)
+ *
+ * The top item in the heap may be examined, and its value may be updated.
+ * (Updating the top item is more efficient than popping and then pushing it.)
+ *
+ * Items may be deleted from the heap. Items may have their value updated
+ * while they are in the heap. Both of these operations cause the heap to be
+ * reorganised to maintain the heap's partial ordering. Note: these operations
+ * require knowledge of where in the heap the item is -- which, by default, is
+ * done by a linear scan of the heap. For large heaps, there is the option to
+ * keep a "backlink" from the item to it's heap position.
+ *
+ * Vectors may be pushed onto a heap -- copying or moving the contents.
+ *
+ * Heaps may popped to a vector -- copying or moving the contents. The
+ * resulting vector is fully sorted.
+ *
+ * ----------------------------
+ * Comparison function for heap.
+ *
+ * int heap_cmp(...** a, ...** b) ...
+ *
+ * Must return -1, 0, +1 : where -1 => a < b, 0 => a == b & +1 => a > b
+ *
+ * Heap will sort "smallest" to the top. If you want the biggest at the top,
+ * return -1 * usual comparison. Note that the effective heap ordering for
+ * equal values is, essentially, random.
+ *
+ * NB: like other comparison functions (cf qsort) the parameters are pointers
+ * to pointers to the value.
+ *
+ * NB: there should never be NULL items in the heap.
+ */
+
+/*==============================================================================
+ * Initialisation, allocation, reset etc.
+ */
+
+static heap
+heap_setup(heap h, int new_vector, vector_index size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset) ;
+
+/* Initialize heap -- allocating heap structure if required.
+ *
+ * Does not allocate the underlying vector if the heap is initialised empty.
+ *
+ * eg:
+ *
+ * ... = heap_new_init_simple(NULL, 0, (heap_cmp*)my_cmp)
+ *
+ * See: #define heap_init_new_simple(h, size, cmp)
+ * #define heap_init_new_backlinked(h, size, cmp, offset)
+ *
+ * NB: when initialising an existing heap structure it is ESSENTIAL that
+ * any previous heap and its contents have been released, because this
+ * function simply discards whatever was there before. (This function may
+ * be called to initialise a heap structure which has never been
+ * initialised.)
+ *
+ * Backlink:
+ *
+ * The heap_delete_item and heap_update_item functions need the heap
+ * position of the item. The default way of finding that is to scan the
+ * underlying heap array, looking for the address of the item.
+ *
+ * If either of these functions is done often and on large heaps, it is
+ * possible to speed this up by implementing a 'backlink'. This requires
+ * a field of type heap_backlink_t in the item structure, and it is the
+ * offset of that which must be initialised here, eg:
+ *
+ * ... = heap_new_init_backlinked(NULL, 0, (heap_cmp*)my_cmp,
+ * offset_of(struct xxx_heap_item, backlink)) ;
+ *
+ * This adds a little extra work to every change in the heap -- keeping the
+ * backlink of any moved item up to date. But avoids a linear search for
+ * every heap_delete_item or heap_update_item.
+ *
+ * Returns the heap which has been initialised.
+ */
+heap
+heap_init_new(heap h, unsigned int size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset)
+{
+ if (h == NULL)
+ h = XCALLOC(MTYPE_HEAP, sizeof(struct heap)) ;
+ else
+ memset(h, 0, sizeof(struct heap)) ;
+
+ return heap_setup(h, 1, size, cmp, with_backlink, backlink_offset) ;
+} ;
+
+/* Reinitialise heap (or create a new one, if h == NULL).
+ *
+ * Allocates heap structure if none given -- allocating vector if size != 0.
+ * Otherwise, re-initialise the heap and any vector (reusing its memory).
+ *
+ * See: #define heap_re_init_simple(h, size, cmp)
+ * #define heap_re_init_backlinked(h, size, cmp, offset)
+ *
+ * NB: when reinitialising an existing heap it is the caller's
+ * responsibility to release any item values *before* doing this.
+ *
+ * Returns the heap that has been reinitialised.
+ */
+heap
+heap_re_init(heap h, unsigned int size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset)
+{
+ if (h == NULL)
+ return heap_init_new(h, size, cmp, with_backlink, backlink_offset) ;
+ else
+ return heap_setup(h, 0, size, cmp, with_backlink, backlink_offset) ;
+} ;
+
+/* Release heap contents (underlying vector), and (if required) release the
+ * heap structure.
+ *
+ * Returns NULL if releases heap, otherwise the reset heap.
+ *
+ * If does not release the heap, it retains the comparison function and any
+ * backlink setting -- so heap can be reused without reinitialising it.
+ *
+ * NB: it is the callers responsibility to release any heap item values
+ * *before* doing this.
+ */
+heap
+heap_reset(heap h, int free_structure)
+{
+ vector_reset_keep(&h->v) ; /* vector structure is embedded in the heap */
+
+ if (free_structure)
+ XFREE(MTYPE_VECTOR, h) ; /* sets h = NULL */
+
+ return h ;
+} ;
+
+/* Common set-up for heap_init_new() & heap_reset().
+ */
+static heap
+heap_setup(heap h, int new_vector, unsigned int size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset)
+{
+ assert(cmp != NULL) ; /* or there will be tears */
+
+ h->cmp = cmp ;
+ h->state = with_backlink ? Heap_Has_Backlink : 0 ;
+ h->backlink_offset = backlink_offset ;
+
+ if (new_vector)
+ vector_init_new(&h->v, size) ;
+ else
+ vector_re_init(&h->v, size) ;
+
+ return h ;
+} ;
+
+/* Ream (another) item out of the given heap.
+ *
+ * If heap is empty, release the underlying vector, and (if required) release
+ * the heap structure.
+ *
+ * See: #define heap_ream_free(h) heap_ream(h, 1)
+ * #define heap_ream_keep(h) heap_ream(h, 0)
+ *
+ * Useful for emptying out and resetting/discarding a heap:
+ *
+ * while ((p_v = heap_ream_free(h)))
+ * ... do what's required to release the item p_v
+ *
+ * Returns NULL when heap is empty (and structure has been freed, if required).
+ *
+ * If does not release the heap, it retains the comparison function and any
+ * backlink setting -- so heap can be reused without reinitialising it.
+ *
+ * NB: once the process of reaming a heap has started: (a) MUST NOT attempt to
+ * use the heap until process completes, and (b) MUST complete the process.
+ *
+ * NB: items are reamed out in no defined order.
+ */
+p_vector_item
+heap_ream(heap h, int free_structure)
+{
+ p_vector_item p_v ;
+
+ if (h == NULL)
+ return NULL ;
+
+ if ((p_v = vector_ream_keep(&h->v)) == NULL)
+ heap_reset(h, free_structure) ;
+
+ return p_v ;
+} ;
+
+/*==============================================================================
+ * Simple Heap Operations -- see also the Inline functions.
+ */
+
+/* Pop item off the heap.
+ *
+ * Returns the popped value, which is NULL if the heap was (and still is) empty.
+ */
+p_vector_item
+heap_pop_item(heap h)
+{
+ p_vector_item p_v ;
+ p_vector_item p_x ;
+
+ p_v = vector_pop_item(&h->v) ; /* extract last item, if any */
+ if ((p_v == NULL) || (h->v.end == 0))
+ return p_v ; /* done if empty or last was also first */
+
+ p_x = h->v.p_items[0] ; /* this is what we are popping */
+
+ heap_bubble_down(h, 0, p_v) ; /* reposition what was the last item */
+ /* updating any backlink */
+ return p_x ;
+} ;
+
+/* Pop one item off the heap and promptly push another.
+ *
+ * In this combination, the pop is essentially free.
+ *
+ * Returns the popped value, which is NULL if the heap was (and still is) empty.
+ */
+p_vector_item
+heap_pop_push_item(heap h, p_vector_item p_v)
+{
+ p_vector_item p_x ;
+
+ dassert(p_v != NULL) ; /* no NULLs, thank you. */
+
+ p_x = heap_top_item(h) ; /* what we are popping */
+
+ if (p_x == NULL)
+ heap_push_item(h, p_v) ; /* for empty heap, this deals with */
+ /* extending heap etc. */
+ else
+ heap_bubble_down(h, 0, p_v) ; /* position the replacement */
+ /* setting any backlink */
+ return p_x ;
+} ;
+
+/*==============================================================================
+ * Heap Operations which use 'backlink', if implemented.
+ */
+
+/* Delete given item from the heap.
+ *
+ * See notes on backlink, above.
+ *
+ * NB: do NOT try this on items which are not in the given heap !
+ */
+void
+heap_delete_item(heap h, p_vector_item p_v)
+{
+ p_vector_item p_x ;
+ vector_index i ;
+
+ i = heap_find_item(h, p_v) ; /* index of item to be deleted */
+
+ p_x = vector_pop_item(&h->v) ; /* extract last item, if any */
+
+ if (i < h->v.end) /* if not deleting the last item... */
+ heap_bubble(h, i, p_x) ; /* ...reinsert what was last, at the delete */
+ /* position, updating any backlink */
+} ;
+
+/*==============================================================================
+ * Other Heap Operations.
+ */
+
+/* Push entire vector onto heap copying or moving items as required.
+ *
+ * Copy or move vector to end of heap's vector, then move each
+ * (non-NULL) item into heap order (discarding any NULL items).
+ *
+ * See: #define heap_push_vector_copy(h, v)
+ * #define heap_push_vector_move(h, v)
+ */
+void
+heap_push_vector(heap h, vector v, int move_vector)
+{
+ vector_index i = h->v.end ;
+ vector_index e ;
+ vector_index n = v->end ;
+ p_vector_item p_v ;
+
+ if (move_vector)
+ vector_move_append(&h->v, v) ;
+ else
+ vector_copy_append(&h->v, v) ;
+
+ e = i ; /* old end of the heap. */
+ while (n--) {
+ p_v = h->v.p_items[i++] ;
+ if (p_v != NULL)
+ heap_bubble_up(h, e++, p_v) ; /* move new item into position in heap */
+ /* setting any backlink */
+ } ;
+
+ h->v.end = e ; /* new end of heap */
+} ;
+
+/* Pop given heap to vector -- creating vector if required (v == NULL).
+ *
+ * Resulting vector is fully sorted.
+ *
+ * Moves or copies the contents of the heap.
+ *
+ * See: #define heap_pop_vector_copy(v, h)
+ * #define heap_pop_vector_move(v, h)
+ *
+ * NB: when creating new vector, will be exactly the required size.
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to release any existing items if that is required.
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to ensure the vector structure is currently valid.
+ */
+vector
+heap_pop_vector(vector v, heap h, int move_heap)
+{
+ vector_index n = h->v.end ;
+ vector_index i ;
+
+ v = vector_re_init(v, n) ; /* guarantees >= 'n' items in vector */
+ v->end = n ;
+
+ for (i = 0 ; i < n ; i++)
+ v->p_items[i] = heap_pop_item(h) ;
+
+ if (!move_heap)
+ vector_copy_here(&h->v, v) ; /* fully sorted is also heap ordered ! */
+
+ return v ;
+} ;
+
+/*==============================================================================
+ * The Heap internal mechanics.
+ */
+
+/* Returns pointer to backlink value in heap item: lvalue or rvalue */
+#define HEAP_BACKLINK(h, p_v) \
+ *(heap_backlink_t*)((char*)(p_v) + (h)->backlink_offset)
+/* Sets backlink, if required. */
+#define heap_set_backlink(h, p_v, i) \
+ if ((h)->state & Heap_Has_Backlink) HEAP_BACKLINK(h, p_v) = (i)
+
+/* Returns index of parent. */
+#define HEAP_UP(i) (((i) - 1) / 2)
+/* Returns index of left child. */
+#define HEAP_DOWN(i) (((i) * 2) + 1)
+
+/* Insert given item in the required place in heap, given that there is now
+ * a hole at the given position -- may move up or down the heap, or stay put.
+ *
+ * Bubbles up or down as required.
+ *
+ * Note that this sets the backlink on the given item.
+ */
+private void
+heap_bubble(heap h, vector_index i, p_vector_item p_v)
+{
+ /* If this is < parent, we bubble upwards. */
+ if ((i != 0) && (h->cmp(&p_v, &h->v.p_items[HEAP_UP(i)]) < 0))
+ heap_bubble_up(h, i, p_v) ;
+ /* Otherwise we try bubbling downwards. */
+ else
+ heap_bubble_down(h, i, p_v) ;
+} ;
+
+/* Insert given item in the required place in heap, given that there is now
+ * a hole at the given position -- where we know may *only* move up the heap.
+ *
+ * Note that this sets the backlink on the given item.
+ *
+ * NB: ignores anything in the heap beyond 'i' -- in particular does not use
+ * v.end at all. So this can be used to work along a vector and bring
+ * items into heap order.
+ */
+private void
+heap_bubble_up(heap h, vector_index i, p_vector_item p_v)
+{
+ p_vector_item* ha = h->v.p_items ; /* underlying array */
+ vector_index ip ; /* index of parent */
+ p_vector_item p_p ; /* pointer to parent item */
+
+ dassert(ha != NULL) ;
+
+ while (i != 0)
+ {
+ ip = HEAP_UP(i) ;
+ p_p = &ha[ip] ; /* get parent */
+
+ if (h->cmp(&p_v, &p_p) >= 0)
+ break ; /* stop when value >= parent */
+
+ ha[i] = p_p ; /* move parent down... */
+ heap_set_backlink(h, p_p, i) ; /* ...updating any backlink */
+
+ i = ip ; /* move up the heap */
+ } ;
+
+ ha[i] = p_v ; /* place in new position... */
+ heap_set_backlink(h, p_v, i) ; /* ...updating any backlink */
+} ;
+
+/* Insert given item in the required place in heap, given that there is now
+ * a hole at the given position -- where we know may *only* move down the heap.
+ *
+ * Note that this sets the backlink on the given item.
+ */
+private void
+heap_bubble_down(heap h, vector_index i, p_vector_item p_v)
+{
+ vector_index e = h->v.end ; /* end of heap */
+ vector_index ic ; /* index of child */
+ vector_index is ; /* index of sibling */
+ p_vector_item p_c ; /* pointer to child */
+ p_vector_item p_s ; /* pointer to sibling */
+
+ p_vector_item* ha = h->v.p_items ; /* underlying array */
+ dassert(ha != NULL) ;
+
+ while (1)
+ {
+ ic = HEAP_DOWN(i) ;
+ if (ic >= e)
+ break ; /* Quit if run out of heap ! */
+ p_c = &ha[ic] ; /* get left hand child */
+
+ is = ic + 1 ;
+ if (is < e) /* is there a right hand child ? */
+ {
+ p_s = &ha[is] ; /* get right hand child */
+ if (h->cmp(&p_s, &p_c) < 0)
+ {
+ ic = is ; /* select smaller sibling */
+ p_c = p_s ;
+ } ;
+ } ;
+
+ if (h->cmp(&p_v, &p_c) <= 0)
+ break ; /* stop when <= both children */
+
+ ha[i] = p_c ; /* move smaller child up */
+ heap_set_backlink(h, p_c, i) ; /* ...updating any backlink */
+
+ i = ic ; /* move down the heap */
+ } ;
+
+ ha[i] = p_v ; /* place in new position... */
+ heap_set_backlink(h, p_v, i) ; /* ...updating any backlink */
+} ;
+
+/* Find index of given item in the given heap. */
+private vector_index
+heap_find_item(heap h, p_vector_item p_v)
+{
+ vector_index i ;
+
+ if (h->state & Heap_Has_Backlink)
+ i = HEAP_BACKLINK(h, p_v) ;
+ else
+ {
+ for (i = 0 ; i < h->v.end ; ++i)
+ if (h->v.p_items[i] == p_v)
+ return i ;
+ } ;
+
+ assert((i < h->v.end) && (h->v.p_items[i] == p_v)) ;
+
+ return i ;
+} ;
diff --git a/lib/heap.h b/lib/heap.h
new file mode 100644
index 00000000..bd984398
--- /dev/null
+++ b/lib/heap.h
@@ -0,0 +1,160 @@
+/* Generic heap data structure -- header.
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *.
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_HEAP_H
+#define _ZEBRA_HEAP_H
+
+#include "vector.h"
+
+/* Macro in case there are particular compiler issues. */
+#ifndef Inline
+ #define Inline static inline
+#endif
+
+/*==============================================================================
+ * Data structures etc.
+ */
+
+typedef int heap_cmp(p_vector_item* a, p_vector_item*) ;
+
+enum heap_state {
+ Heap_Has_Backlink = 0x01, /* Set if backlink set */
+} ;
+
+typedef vector_index heap_backlink_t ;
+
+typedef struct heap* heap ;
+
+struct heap
+{
+ heap_cmp* cmp ;
+
+ enum heap_state state ;
+ unsigned int backlink_offset ;
+
+ struct vector v ;
+} ;
+
+/*==============================================================================
+ * Prototypes.
+ */
+
+extern heap heap_init_new(heap h, unsigned int size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset) ;
+#define heap_init_new_simple(h, size, cmp) \
+ heap_init_new(h, size, cmp, 0, 0)
+#define heap_init_new_backlinked(h, size, cmp, offset) \
+ heap_init_new(h, size, cmp, 1, offset)
+
+extern heap heap_re_init(heap h, unsigned int size, heap_cmp* cmp,
+ int with_backlink, unsigned int backlink_offset) ;
+#define heap_re_init_simple(h, size, cmp) \
+ heap_re_init(h, size, cmp, 0, 0)
+#define heap_re_init_backlinked(h, size, cmp, offset) \
+ heap_re_init(h, size, cmp, 1, offset)
+
+extern heap heap_reset(heap h, int free_structure) ;
+extern p_vector_item heap_ream(heap h, int free_structure) ;
+
+/* Reset heap and free the heap structure. */
+#define heap_reset_free(h) heap_reset(h, 1)
+/* Reset heap but keep the heap structure. */
+#define heap_reset_keep(h) heap_reset(h, 0)
+/* Ream out heap and free the heap structure. */
+#define heap_ream_free(h) heap_ream(h, 1)
+/* Ream out heap but keep the heap structure. */
+#define heap_ream_keep(h) heap_ream(h, 0)
+
+Inline void heap_push_item(heap h, p_vector_item p_v) ;
+extern p_vector_item heap_pop_item(heap h) ;
+extern p_vector_item heap_pop_push_item(heap h, p_vector_item p_v) ;
+Inline p_vector_item heap_top_item(heap h) ;
+Inline void heap_update_top_item(heap h) ;
+
+extern void heap_delete_item(heap h, p_vector_item p_v) ;
+Inline void heap_update_item(heap h, p_vector_item p_v) ;
+
+extern void heap_push_vector(heap h, vector v, int move_vector) ;
+#define heap_push_vector_copy(h, v) \
+ heap_push_vector(h, v, 0)
+#define heap_push_vector_move(h, v) \
+ heap_push_vector(h, v, 1)
+extern vector heap_pop_vector(vector v, heap h, int move_heap) ;
+#define heap_pop_vector_copy(v, h) \
+ heap_pop_vector(v, h, 0)
+#define heap_pop_vector_move(v, h) \
+ heap_pop_vector(v, h, 1)
+
+/*==============================================================================
+ * This are extern only for use in Inline and other friends
+ */
+
+#ifndef private
+ #define private extern
+#endif
+
+private void
+heap_bubble(heap h, vector_index i, p_vector_item p_v) ;
+
+private void
+heap_bubble_up(heap h, vector_index i, p_vector_item p_v) ;
+
+private void
+heap_bubble_down(heap h, vector_index i, p_vector_item p_v) ;
+
+private vector_index
+heap_find_item(heap h, p_vector_item p_v) ;
+
+/*==============================================================================
+ * Inline Functions
+ */
+
+/* Push given item onto the heap
+ */
+Inline void
+heap_push_item(heap h, p_vector_item p_v)
+{
+ dassert(p_v != NULL) ; /* no NULLs, thank you. */
+ heap_bubble_up(h, vector_extend_by_1(&h->v), p_v) ;
+} ;
+
+/* Get copy of top heap item (does not pop).
+ *
+ * Returns NULL if heap is empty.
+ */
+Inline p_vector_item
+heap_top_item(heap h)
+{
+ return vector_get_first_item(&h->v) ; /* if any */
+} ;
+
+/* Update heap to reflect new value of top item.
+ */
+Inline void
+heap_update_top_item(heap h)
+{
+ heap_bubble_down(h, 0, heap_top_item(h)) ;
+} ;
+
+/* Update heap to reflect new value of given item.
+ */
+Inline void
+heap_update_item(heap h, p_vector_item p_v)
+{
+ heap_bubble(h, heap_find_item(h, p_v), p_v) ;
+} ;
+
+#endif /* _ZEBRA_HEAP_H */
diff --git a/lib/log.c b/lib/log.c
index 0c2f655b..81624f58 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -34,6 +34,24 @@
#ifdef HAVE_UCONTEXT_H
#include <ucontext.h>
#endif
+#include "qpthreads.h"
+
+#ifdef NDEBUG
+#define LOCK qpt_mutex_lock(vty_mutex);
+#define UNLOCK qpt_mutex_unlock(vty_mutex);
+#else
+#define LOCK qpt_mutex_lock(vty_mutex);++vty_lock_count;
+#define UNLOCK --vty_lock_count;qpt_mutex_unlock(vty_mutex);
+#endif
+
+/* log is protected by the same mutext as vty, see comments in vty.c */
+
+/* prototypes */
+static int uzlog_reset_file (struct zlog *zl);
+static void zlog_abort (const char *mess) __attribute__ ((noreturn));
+static void vzlog (struct zlog *zl, int priority, const char *format, va_list args);
+static void uzlog_backtrace(int priority);
+static void uvzlog (struct zlog *zl, int priority, const char *format, va_list args);
static int logfile_fd = -1; /* Used in signal handler. */
@@ -66,14 +84,23 @@ const char *zlog_priority[] =
"debugging",
NULL,
};
-
-
/* For time string format. */
size_t
quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
{
+ size_t result;
+ LOCK
+ result = uquagga_timestamp(timestamp_precision, buf, buflen);
+ UNLOCK
+ return result;
+}
+
+/* unprotected version for when mutex already held */
+size_t
+uquagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
+{
static struct {
time_t last;
size_t len;
@@ -81,6 +108,8 @@ quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
} cache;
struct timeval clock;
+ size_t result = 0;
+
/* would it be sufficient to use global 'recent_time' here? I fear not... */
gettimeofday(&clock, NULL);
@@ -122,14 +151,19 @@ quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
}
while (--prec > 0);
*p = '.';
- return cache.len+1+timestamp_precision;
+ result = cache.len+1+timestamp_precision;
}
- buf[cache.len] = '\0';
- return cache.len;
+ else
+ {
+ buf[cache.len] = '\0';
+ result = cache.len;
+ }
+ } else {
+ if (buflen > 0)
+ buf[0] = '\0';
}
- if (buflen > 0)
- buf[0] = '\0';
- return 0;
+
+ return result;
}
/* Utility routine for current time printing. */
@@ -138,20 +172,30 @@ time_print(FILE *fp, struct timestamp_control *ctl)
{
if (!ctl->already_rendered)
{
- ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
+ ctl->len = uquagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
ctl->already_rendered = 1;
}
fprintf(fp, "%s ", ctl->buf);
}
-
/* va_list version of zlog. */
static void
vzlog (struct zlog *zl, int priority, const char *format, va_list args)
{
+ LOCK
+ uvzlog(zl, priority, format, args);
+ UNLOCK
+}
+
+/* va_list version of zlog. Unprotected assumes mutex already held*/
+static void
+uvzlog (struct zlog *zl, int priority, const char *format, va_list args)
+{
struct timestamp_control tsctl;
tsctl.already_rendered = 0;
+ assert(vty_lock_count);
+
/* If zlog is not specified, use default one. */
if (zl == NULL)
zl = zlog_default;
@@ -165,55 +209,58 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args)
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
fflush (stderr);
-
- /* In this case we return at here. */
- return;
}
- tsctl.precision = zl->timestamp_precision;
-
- /* Syslog output */
- if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
+ else
{
- va_list ac;
- va_copy(ac, args);
- vsyslog (priority|zlog_default->facility, format, ac);
- va_end(ac);
- }
+ tsctl.precision = zl->timestamp_precision;
- /* File output. */
- if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
- {
- va_list ac;
- time_print (zl->fp, &tsctl);
- if (zl->record_priority)
- fprintf (zl->fp, "%s: ", zlog_priority[priority]);
- fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
- va_copy(ac, args);
- vfprintf (zl->fp, format, ac);
- va_end(ac);
- fprintf (zl->fp, "\n");
- fflush (zl->fp);
- }
+ /* Syslog output */
+ if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
+ {
+ va_list ac;
+ va_copy(ac, args);
+ vsyslog (priority|zlog_default->facility, format, ac);
+ va_end(ac);
+ }
- /* stdout output. */
- if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
- {
- va_list ac;
- time_print (stdout, &tsctl);
- if (zl->record_priority)
- fprintf (stdout, "%s: ", zlog_priority[priority]);
- fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
- va_copy(ac, args);
- vfprintf (stdout, format, ac);
- va_end(ac);
- fprintf (stdout, "\n");
- fflush (stdout);
- }
+ /* File output. */
+ if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
+ {
+ va_list ac;
+ time_print (zl->fp, &tsctl);
+ if (zl->record_priority)
+ fprintf (zl->fp, "%s: ", zlog_priority[priority]);
+ fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
+ va_copy(ac, args);
+ vfprintf (zl->fp, format, ac);
+ va_end(ac);
+ fprintf (zl->fp, "\n");
+ fflush (zl->fp);
+ }
- /* Terminal monitor. */
- if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
- vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
- zlog_proto_names[zl->protocol], format, &tsctl, args);
+ /* stdout output. */
+ if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
+ {
+ va_list ac;
+ time_print (stdout, &tsctl);
+ if (zl->record_priority)
+ fprintf (stdout, "%s: ", zlog_priority[priority]);
+ fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
+ va_copy(ac, args);
+ vfprintf (stdout, format, ac);
+ va_end(ac);
+ fprintf (stdout, "\n");
+ fflush (stdout);
+ }
+
+ /* Terminal monitor. */
+ if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
+ {
+ const char *priority_name = (zl->record_priority ? zlog_priority[priority] : NULL);
+ const char *proto_name = zlog_proto_names[zl->protocol];
+ vty_log (priority_name, proto_name, format, &tsctl, args);
+ }
+ }
}
static char *
@@ -518,8 +565,16 @@ zlog_backtrace_sigsafe(int priority, void *program_counter)
void
zlog_backtrace(int priority)
{
+ LOCK
+ zlog_backtrace(priority);
+ UNLOCK
+}
+
+static void
+uzlog_backtrace(int priority)
+{
#ifndef HAVE_GLIBC_BACKTRACE
- zlog(NULL, priority, "No backtrace available on this platform.");
+ uzlog(NULL, priority, "No backtrace available on this platform.");
#else
void *array[20];
int size, i;
@@ -528,27 +583,38 @@ zlog_backtrace(int priority)
if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
((size_t)size > sizeof(array)/sizeof(array[0])))
{
- zlog_err("Cannot get backtrace, returned invalid # of frames %d "
+ uzlog(NULL, LOG_ERR, "Cannot get backtrace, returned invalid # of frames %d "
"(valid range is between 1 and %lu)",
size, (unsigned long)(sizeof(array)/sizeof(array[0])));
return;
}
- zlog(NULL, priority, "Backtrace for %d stack frames:", size);
+ uzlog(NULL, priority, "Backtrace for %d stack frames:", size);
if (!(strings = backtrace_symbols(array, size)))
{
- zlog_err("Cannot get backtrace symbols (out of memory?)");
+ uzlog(NULL, LOG_ERR, "Cannot get backtrace symbols (out of memory?)");
for (i = 0; i < size; i++)
- zlog(NULL, priority, "[bt %d] %p",i,array[i]);
+ uzlog(NULL, priority, "[bt %d] %p",i,array[i]);
}
else
{
for (i = 0; i < size; i++)
- zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
+ uzlog(NULL, priority, "[bt %d] %s",i,strings[i]);
free(strings);
}
#endif /* HAVE_GLIBC_BACKTRACE */
}
+/* unlocked version */
+void
+uzlog (struct zlog *zl, int priority, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ uvzlog (zl, priority, format, args);
+ va_end (args);
+}
+
void
zlog (struct zlog *zl, int priority, const char *format, ...)
{
@@ -605,16 +671,64 @@ PLOG_FUNC(plog_debug, LOG_DEBUG)
void
_zlog_assert_failed (const char *assertion, const char *file,
- unsigned int line, const char *function)
+ unsigned int line, const char *function)
+{
+ const static size_t buff_size = 1024;
+ char buff[buff_size];
+ snprintf(buff, buff_size,
+ "Assertion `%s' failed in file %s, line %u, function %s",
+ assertion, file, line, (function ? function : "?"));
+ zlog_abort(buff);
+}
+
+/* Abort with message */
+void
+_zlog_abort_mess (const char *mess, const char *file,
+ unsigned int line, const char *function)
+{
+ const static size_t buff_size = 1024;
+ char buff[buff_size];
+ snprintf(buff, buff_size, "%s, in file %s, line %u, function %s",
+ mess, file, line, (function ? function : "?"));
+ zlog_abort(buff);
+}
+
+/* Abort with message and errno and strerror() thereof */
+void
+_zlog_abort_errno (const char *mess, const char *file,
+ unsigned int line, const char *function)
+{
+ _zlog_abort_err(mess, errno, file, line, function);
+}
+
+/* Abort with message and given error and strerror() thereof */
+void
+_zlog_abort_err (const char *mess, int err, const char *file,
+ unsigned int line, const char *function)
+{
+ const static size_t buff_size = 1024;
+ char buff[buff_size];
+ char err_mess[buff_size];
+ strerror_r(err, err_mess, buff_size);
+ snprintf(buff, buff_size,
+ "%s, in file %s, line %u, function %s, error %d \"%s\"",
+ mess, file, line, (function ? function : "?"),
+ err, err_mess);
+ zlog_abort(buff);
+}
+
+
+static void
+zlog_abort (const char *mess)
{
/* Force fallback file logging? */
if (zlog_default && !zlog_default->fp &&
((logfile_fd = open_crashlog()) >= 0) &&
((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
- zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
- assertion,file,line,(function ? function : "?"));
- zlog_backtrace(LOG_CRIT);
+
+ uzlog(NULL, LOG_CRIT, "%s", mess);
+ uzlog_backtrace(LOG_CRIT);
abort();
}
@@ -648,22 +762,33 @@ openzlog (const char *progname, zlog_proto_t protocol,
void
closezlog (struct zlog *zl)
{
+ LOCK
+
closelog();
if (zl->fp != NULL)
fclose (zl->fp);
XFREE (MTYPE_ZLOG, zl);
+
+ UNLOCK
}
/* Called from command.c. */
void
zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
{
+ LOCK
+
if (zl == NULL)
zl = zlog_default;
- zl->maxlvl[dest] = log_level;
+ if (zl != NULL)
+ {
+ zl->maxlvl[dest] = log_level;
+ }
+
+ UNLOCK
}
int
@@ -671,46 +796,68 @@ zlog_set_file (struct zlog *zl, const char *filename, int log_level)
{
FILE *fp;
mode_t oldumask;
+ int result = 1;
+
+ LOCK
/* There is opend file. */
- zlog_reset_file (zl);
+ uzlog_reset_file (zl);
/* Set default zl. */
if (zl == NULL)
zl = zlog_default;
- /* Open file. */
- oldumask = umask (0777 & ~LOGFILE_MASK);
- fp = fopen (filename, "a");
- umask(oldumask);
- if (fp == NULL)
- return 0;
-
- /* Set flags. */
- zl->filename = strdup (filename);
- zl->maxlvl[ZLOG_DEST_FILE] = log_level;
- zl->fp = fp;
- logfile_fd = fileno(fp);
+ if (zl != NULL)
+ {
+ /* Open file. */
+ oldumask = umask (0777 & ~LOGFILE_MASK);
+ fp = fopen (filename, "a");
+ umask(oldumask);
+ if (fp == NULL)
+ result = 0;
+ else
+ {
+ /* Set flags. */
+ zl->filename = strdup (filename);
+ zl->maxlvl[ZLOG_DEST_FILE] = log_level;
+ zl->fp = fp;
+ logfile_fd = fileno(fp);
+ }
+ }
- return 1;
+ UNLOCK
+ return result;
}
/* Reset opend file. */
int
zlog_reset_file (struct zlog *zl)
{
+ int result;
+ LOCK
+ result = uzlog_reset_file(zl);
+ UNLOCK
+ return result;
+}
+
+static int
+uzlog_reset_file (struct zlog *zl)
+ {
if (zl == NULL)
zl = zlog_default;
- if (zl->fp)
- fclose (zl->fp);
- zl->fp = NULL;
- logfile_fd = -1;
- zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
-
- if (zl->filename)
- free (zl->filename);
- zl->filename = NULL;
+ if (zl != NULL)
+ {
+ if (zl->fp)
+ fclose (zl->fp);
+ zl->fp = NULL;
+ logfile_fd = -1;
+ zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
+
+ if (zl->filename)
+ free (zl->filename);
+ zl->filename = NULL;
+ }
return 1;
}
@@ -720,39 +867,318 @@ int
zlog_rotate (struct zlog *zl)
{
int level;
+ int result = 1;
+
+ LOCK
if (zl == NULL)
zl = zlog_default;
- if (zl->fp)
- fclose (zl->fp);
- zl->fp = NULL;
- logfile_fd = -1;
- level = zl->maxlvl[ZLOG_DEST_FILE];
- zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
+ if (zl != NULL)
+ {
+ if (zl->fp)
+ fclose (zl->fp);
+ zl->fp = NULL;
+ logfile_fd = -1;
+ level = zl->maxlvl[ZLOG_DEST_FILE];
+ zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
+
+ if (zl->filename)
+ {
+ mode_t oldumask;
+ int save_errno;
+
+ oldumask = umask (0777 & ~LOGFILE_MASK);
+ zl->fp = fopen (zl->filename, "a");
+ save_errno = errno;
+ umask(oldumask);
+ if (zl->fp == NULL)
+ {
+ /* can't call logging while locked */
+ char *fname = strdup(zl->filename);
+ uzlog(NULL, LOG_ERR, "Log rotate failed: cannot open file %s for append: %s",
+ fname, safe_strerror(save_errno));
+ free(fname);
+ result = -1;
+ }
+ else
+ {
+ logfile_fd = fileno(zl->fp);
+ zl->maxlvl[ZLOG_DEST_FILE] = level;
+ }
+ }
+ }
+ UNLOCK
+ return result;
+}
- if (zl->filename)
+int
+zlog_get_default_lvl (struct zlog *zl)
+{
+ int result = LOG_DEBUG;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
{
- mode_t oldumask;
- int save_errno;
+ result = zl->default_lvl;
+ }
- oldumask = umask (0777 & ~LOGFILE_MASK);
- zl->fp = fopen (zl->filename, "a");
- save_errno = errno;
- umask(oldumask);
- if (zl->fp == NULL)
- {
- zlog_err("Log rotate failed: cannot open file %s for append: %s",
- zl->filename, safe_strerror(save_errno));
- return -1;
- }
- logfile_fd = fileno(zl->fp);
- zl->maxlvl[ZLOG_DEST_FILE] = level;
+ UNLOCK
+ return result;
+}
+
+void
+zlog_set_default_lvl (struct zlog *zl, int level)
+{
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ zl->default_lvl = level;
}
- return 1;
+ UNLOCK
}
-
+
+/* Set logging level and default for all destinations */
+void
+zlog_set_default_lvl_dest (struct zlog *zl, int level)
+{
+ int i;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ zl->default_lvl = level;
+
+ for (i = 0; i < ZLOG_NUM_DESTS; i++)
+ if (zl->maxlvl[i] != ZLOG_DISABLED)
+ zl->maxlvl[i] = level;
+ }
+
+ UNLOCK
+}
+
+int
+zlog_get_maxlvl (struct zlog *zl, zlog_dest_t dest)
+{
+ int result = ZLOG_DISABLED;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = zl->maxlvl[dest];
+ }
+
+ UNLOCK
+ return result;
+}
+
+int
+zlog_get_facility (struct zlog *zl)
+{
+ int result = LOG_DAEMON;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = zl->facility;
+ }
+
+ UNLOCK
+ return result;
+}
+
+void
+zlog_set_facility (struct zlog *zl, int facility)
+{
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ zl->facility = facility;
+ }
+
+ UNLOCK
+}
+
+int
+zlog_get_record_priority (struct zlog *zl)
+{
+ int result = 0;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = zl->record_priority;
+ }
+
+ UNLOCK
+ return result;
+}
+
+void
+zlog_set_record_priority (struct zlog *zl, int record_priority)
+{
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ zl->record_priority = record_priority;
+ }
+ UNLOCK
+}
+
+int
+zlog_get_timestamp_precision (struct zlog *zl)
+{
+ int result = 0;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = zl->timestamp_precision;
+ }
+ UNLOCK
+ return result;
+}
+
+void
+zlog_set_timestamp_precision (struct zlog *zl, int timestamp_precision)
+{
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ zl->timestamp_precision = timestamp_precision;
+ }
+
+ UNLOCK
+}
+
+/* returns name of ZLOG_NONE if no zlog given and no default set */
+const char *
+zlog_get_proto_name (struct zlog *zl)
+{
+ const char * result;
+ LOCK
+ result = uzlog_get_proto_name(zl);
+ UNLOCK
+ return result;
+}
+
+/* unprotected version, assumes mutex held */
+const char *
+uzlog_get_proto_name (struct zlog *zl)
+{
+ zlog_proto_t protocol = ZLOG_NONE;
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ protocol = zl->protocol;
+ }
+
+ return zlog_proto_names[protocol];
+}
+
+/* caller must free result */
+char *
+zlog_get_filename (struct zlog *zl)
+{
+ char * result = NULL;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL && zl->filename != NULL)
+ {
+ result = strdup(zl->filename);
+ }
+
+ UNLOCK
+ return result;
+}
+
+const char *
+zlog_get_ident (struct zlog *zl)
+{
+ const char * result = NULL;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = zl->ident;
+ }
+
+ UNLOCK
+ return result;
+}
+
+/* logging to a file? */
+int
+zlog_is_file (struct zlog *zl)
+{
+ int result = 0;
+
+ LOCK
+
+ if (zl == NULL)
+ zl = zlog_default;
+
+ if (zl != NULL)
+ {
+ result = (zl->fp != NULL);
+ }
+
+ UNLOCK;
+ return result;
+}
+
/* Message lookup function. */
const char *
lookup (const struct message *mes, int key)
@@ -928,3 +1354,6 @@ proto_name2num(const char *s)
return -1;
}
#undef RTSIZE
+
+#undef LOCK
+#undef UNLOCK
diff --git a/lib/log.h b/lib/log.h
index 2dd1d313..6ab1b37c 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -93,7 +93,11 @@ struct message
const char *str;
};
-/* Default logging strucutre. */
+/* module initialization */
+extern void zlog_init_r(void);
+extern void zlog_destroy_r(void);
+
+/* Default logging structure. */
extern struct zlog *zlog_default;
/* Open zlog function */
@@ -113,6 +117,9 @@ extern void closezlog (struct zlog *zl);
/* Generic function for zlog. */
extern void zlog (struct zlog *zl, int priority, const char *format, ...)
PRINTF_ATTRIBUTE(3, 4);
+/* assumed locked version for close friends */
+extern void uzlog (struct zlog *zl, int priority, const char *format, ...)
+ PRINTF_ATTRIBUTE(3, 4);
/* Handy zlog functions. */
extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
@@ -147,6 +154,23 @@ extern int zlog_reset_file (struct zlog *zl);
/* Rotate log. */
extern int zlog_rotate (struct zlog *);
+/* getters & setters */
+extern int zlog_get_default_lvl (struct zlog *zl);
+extern void zlog_set_default_lvl (struct zlog *zl, int level);
+extern void zlog_set_default_lvl_dest (struct zlog *zl, int level);
+extern int zlog_get_maxlvl (struct zlog *zl, zlog_dest_t dest);
+extern int zlog_get_facility (struct zlog *zl);
+extern void zlog_set_facility (struct zlog *zl, int facility);
+extern int zlog_get_record_priority (struct zlog *zl);
+extern void zlog_set_record_priority (struct zlog *zl, int record_priority);
+extern int zlog_get_timestamp_precision (struct zlog *zl);
+extern void zlog_set_timestamp_precision (struct zlog *zl, int timestamp_precision);
+extern const char * zlog_get_ident (struct zlog *zl);
+extern char * zlog_get_filename (struct zlog *zl);
+extern int zlog_is_file (struct zlog *zl);
+extern const char * zlog_get_proto_name (struct zlog *zl);
+extern const char * uzlog_get_proto_name (struct zlog *zl);
+
/* For hackey massage lookup and check */
#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)")
@@ -185,7 +209,9 @@ extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
*buf will be set to '\0', and 0 will be returned. */
extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
char *buf, size_t buflen);
-
+/* unprotected version for when mutex already held */
+extern size_t uquagga_timestamp(int timestamp_precision /* # subsecond digits */,
+ char *buf, size_t buflen);
/* structure useful for avoiding repeated rendering of the same timestamp */
struct timestamp_control {
size_t len; /* length of rendered timestamp */
diff --git a/lib/memory.c b/lib/memory.c
index dc09d8a6..333f59ad 100644
--- a/lib/memory.c
+++ b/lib/memory.c
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#include <zebra.h>
@@ -32,22 +32,22 @@
static void alloc_inc (int);
static void alloc_dec (int);
static void log_memstats(int log_priority);
-
+
static const struct message mstr [] =
{
{ MTYPE_THREAD, "thread" },
{ MTYPE_THREAD_MASTER, "thread_master" },
{ MTYPE_VECTOR, "vector" },
- { MTYPE_VECTOR_INDEX, "vector_index" },
+ { MTYPE_VECTOR_BODY, "vector_index" },
{ MTYPE_IF, "interface" },
{ 0, NULL },
};
-
+
/* Fatal memory allocation error occured. */
static void __attribute__ ((noreturn))
zerror (const char *fname, int type, size_t size)
{
- zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
+ zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
fname, lookup (mstr, type), (int) size, safe_strerror(errno));
log_memstats(LOG_WARNING);
/* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
@@ -122,9 +122,9 @@ zstrdup (int type, const char *str)
alloc_inc (type);
return dup;
}
-
+
#ifdef MEMORY_LOG
-static struct
+static struct
{
const char *name;
long alloc;
@@ -187,7 +187,7 @@ mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
}
/* Important function. */
-void
+void
mtype_zfree (const char *file, int line, int type, void *ptr)
{
mstat[type].t_free++;
@@ -205,13 +205,13 @@ mtype_zstrdup (const char *file, int line, int type, const char *str)
mstat[type].c_strdup++;
memory = zstrdup (type, str);
-
+
mtype_log ("xstrdup", memory, file, line, type);
return memory;
}
#else
-static struct
+static struct
{
char *name;
long alloc;
@@ -231,7 +231,7 @@ alloc_dec (int type)
{
mstat[type].alloc--;
}
-
+
/* Looking up memory status from vty interface. */
#include "vector.h"
#include "vty.h"
@@ -329,7 +329,7 @@ show_memory_mallinfo (struct vty *vty)
{
struct mallinfo minfo = mallinfo();
char buf[MTYPE_MEMSTR_LEN];
-
+
vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
vty_out (vty, " Total heap allocated: %s%s",
mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
@@ -373,11 +373,11 @@ DEFUN (show_memory_all,
{
struct mlist *ml;
int needsep = 0;
-
+
#ifdef HAVE_MALLINFO
needsep = show_memory_mallinfo (vty);
#endif /* HAVE_MALLINFO */
-
+
for (ml = mlists; ml->list; ml++)
{
if (needsep)
@@ -516,7 +516,7 @@ memory_init (void)
install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
install_element (ENABLE_NODE, &show_memory_isis_cmd);
}
-
+
/* Stats querying from users */
/* Return a pointer to a human friendly string describing
* the byte count passed in. E.g:
@@ -529,13 +529,13 @@ const char *
mtype_memstr (char *buf, size_t len, unsigned long bytes)
{
unsigned int t, g, m, k;
-
+
/* easy cases */
if (!bytes)
return "0 bytes";
if (bytes == 1)
return "1 byte";
-
+
if (sizeof (unsigned long) >= 8)
/* Hacked to make it not warn on ILP32 machines
* Shift will always be 40 at runtime. See below too */
@@ -545,11 +545,11 @@ mtype_memstr (char *buf, size_t len, unsigned long bytes)
g = bytes >> 30;
m = bytes >> 20;
k = bytes >> 10;
-
+
if (t > 10)
{
/* The shift will always be 39 at runtime.
- * Just hacked to make it not warn on 'smaller' machines.
+ * Just hacked to make it not warn on 'smaller' machines.
* Static compiler analysis should mean no extra code
*/
if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
@@ -576,7 +576,7 @@ mtype_memstr (char *buf, size_t len, unsigned long bytes)
}
else
snprintf (buf, len, "%ld bytes", bytes);
-
+
return buf;
}
diff --git a/lib/memory.h b/lib/memory.h
index 42eb5cae..fd9f1b97 100644
--- a/lib/memory.h
+++ b/lib/memory.h
@@ -21,6 +21,8 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#ifndef _ZEBRA_MEMORY_H
#define _ZEBRA_MEMORY_H
+#include <stddef.h>
+
/* For pretty printing of memory allocate information. */
struct memory_list
{
@@ -32,11 +34,11 @@ struct mlist {
struct memory_list *list;
const char *name;
};
-
-#include "lib/memtypes.h"
extern struct mlist mlists[];
+#include "lib/memtypes.h"
+
/* #define MEMORY_LOG */
#ifdef MEMORY_LOG
#define XMALLOC(mtype, size) \
@@ -60,6 +62,8 @@ extern struct mlist mlists[];
#define XSTRDUP(mtype, str) zstrdup ((mtype), (str))
#endif /* MEMORY_LOG */
+#define SIZE(t,n) (sizeof(t) * (n))
+
/* Prototypes of memory function. */
extern void *zmalloc (int type, size_t size);
extern void *zcalloc (int type, size_t size);
@@ -69,7 +73,7 @@ extern char *zstrdup (int type, const char *str);
extern void *mtype_zmalloc (const char *file, int line, int type, size_t size);
-extern void *mtype_zcalloc (const char *file, int line, int type,
+extern void *mtype_zcalloc (const char *file, int line, int type,
size_t num, size_t size);
extern void *mtype_zrealloc (const char *file, int line, int type, void *ptr,
diff --git a/lib/memtypes.c b/lib/memtypes.c
index 05d93225..8146f21f 100644
--- a/lib/memtypes.c
+++ b/lib/memtypes.c
@@ -1,6 +1,6 @@
/*
* Memory type definitions. This file is parsed by memtypes.awk to extract
- * MTYPE_ and memory_list_.. information in order to autogenerate
+ * MTYPE_ and memory_list_.. information in order to autogenerate
* memtypes.h.
*
* The script is sensitive to the format (though not whitespace), see
@@ -16,14 +16,30 @@ struct memory_list memory_list_lib[] =
{
{ MTYPE_TMP, "Temporary memory" },
{ MTYPE_STRVEC, "String vector" },
- { MTYPE_VECTOR, "Vector" },
- { MTYPE_VECTOR_INDEX, "Vector index" },
+ { MTYPE_VECTOR, "Vector structure" },
+ { MTYPE_VECTOR_BODY, "Vector body" },
+ { MTYPE_SYMBOL_TABLE, "Symbol Table structure" },
+ { MTYPE_SYMBOL_BASES, "Symbol Table chain bases" },
+ { MTYPE_SYMBOL, "Symbol" },
+ { MTYPE_SYMBOL_REF, "Symbol Reference" },
+ { MTYPE_HEAP, "Heap structure" },
{ MTYPE_LINK_LIST, "Link List" },
{ MTYPE_LINK_NODE, "Link Node" },
{ MTYPE_THREAD, "Thread" },
{ MTYPE_THREAD_MASTER, "Thread master" },
{ MTYPE_THREAD_STATS, "Thread stats" },
- { MTYPE_THREAD_FUNCNAME, "Thread function name" },
+ { MTYPE_THREAD_FUNCNAME, "Thread function name" },
+ { MTYPE_QPT_THREAD_ATTR, "qpt thread attributes" },
+ { MTYPE_QPT_MUTEX, "qpt mutex" },
+ { MTYPE_QPT_COND, "qpt condition variable" },
+ { MTYPE_MQUEUE_QUEUE, "Mqueue queue structure" },
+ { MTYPE_MQUEUE_BLOCKS, "Mqueue message blocks" },
+ { MTYPE_MQUEUE_THREAD_SIGNAL, "Mqueue thread signal" },
+ { MTYPE_QPS_SELECTION, "qpselect selection" },
+ { MTYPE_QPS_FILE, "qpselect file" },
+ { MTYPE_QTIMER_PILE, "qtimer pile structure" },
+ { MTYPE_QTIMER, "qtimer timer" },
+ { MTYPE_QPN_NEXUS, "qtn nexus" },
{ MTYPE_VTY, "VTY" },
{ MTYPE_VTY_OUT_BUF, "VTY output buffer" },
{ MTYPE_VTY_HIST, "VTY history" },
@@ -75,7 +91,7 @@ struct memory_list memory_list_lib[] =
{ -1, NULL },
};
-struct memory_list memory_list_zebra[] =
+struct memory_list memory_list_zebra[] =
{
{ MTYPE_RTADV_PREFIX, "Router Advertisement Prefix" },
{ MTYPE_VRF, "VRF" },
diff --git a/lib/mqueue.c b/lib/mqueue.c
new file mode 100644
index 00000000..5fe892c2
--- /dev/null
+++ b/lib/mqueue.c
@@ -0,0 +1,575 @@
+/* Message Queue data structure -- functions
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <string.h>
+
+#include "memory.h"
+#include "mqueue.h"
+#include "zassert.h"
+
+/*==============================================================================
+ * These message queues are designed for inter-qpthread communication.
+ *
+ * A message queue carries messages from one or more qpthreads to one or more
+ * other qpthreads.
+ *
+ * A message queue has one ordinary priority queue and one high priority
+ * queue.
+ *
+ * There are four types of queue, depending on how qpthreads wait and how they
+ * are woken up:
+ *
+ * mqt_cond_unicast -- wait on condition variable, one waiter kicked
+ * mqt_cond_broadcast -- wait on condition variable, all waiters kicked
+ * mqt_signal_unicast -- wait for signal, one waiter kicked
+ * mqt_signal_broadcast -- wait for signal, all waiters kicked
+ *
+ * For condition variables there is a timeout mechanism so that waiters
+ * are woken up at least every now and then. The message queue maintains
+ * a timeout time and a timeout interval. The timeout time is a qtime_mono_t
+ * time -- so is monotonic.
+ *
+ * When waiting, an explicit timeout may be given, otherwise the stored timeout
+ * will be used:
+ *
+ * wait until explicit/stored timeout
+ * if times out and there is a stored interval:
+ * new stored timeout = stored timeout + stored interval
+ * if new stored timeout < time now
+ * new stored timeout = time now + stored interval
+ *
+ * Left to its own devices, this will produce a regular timeout every interval,
+ * assuming that the queue is waited on within the interval. Otherwise the
+ * "clock" will slip.
+ *
+ * There is a default timeout period. The period may be set "infinite".
+ *
+ * For waiters kicked by signal, the wait does not occur within the message
+ * queue code, but the need for a signal is recorded in the message queue.
+ *
+ * Messages take the form of a small block of information which contains:
+ *
+ * * flags -- used by the message handler
+ * * context -- identifies the context of the message (see revoke)
+ *
+ * * action -- void action(mqueue_block) message dispatch
+ * * arg_0 -- *void/uintptr_t/intptr_t ) standard arguments
+ * * arg_1 -- *void/uintptr_t/intptr_t )
+ *
+ * (see struct mqueue_block).
+ *
+ * To send a message, first allocate a message block (see mqueue_block_new),
+ * then fill in the arguments and enqueue it.
+ *
+ *
+ */
+
+/*==============================================================================
+ * Initialisation etc. for Message Queues.
+ *
+ * TODO: how to shut down a message queue... for reset/exit ?
+ */
+
+/* Initialise new Message Queue, if required (mq == NULL) allocating it.
+ *
+ * For mqt_cond_xxx type queues, sets the default timeout interval and the
+ * initial timeout time to now + that interval.
+ */
+
+mqueue_queue
+mqueue_init_new(mqueue_queue mq, enum mqueue_queue_type type)
+{
+ if (mq == NULL)
+ mq = XCALLOC(MTYPE_MQUEUE_QUEUE, sizeof(struct mqueue_queue)) ;
+ else
+ memset(mq, 0, sizeof(struct mqueue_queue)) ;
+
+ qpt_mutex_init(&mq->mutex, qpt_mutex_quagga) ;
+
+ /* head, tail and tail_priority set NULL already */
+ /* waiters set zero already */
+
+ mq->type = type ;
+ switch (type)
+ {
+ case mqt_cond_unicast:
+ case mqt_cond_broadcast:
+ qpt_cond_init(&mq->kick.cond.wait_here, qpt_cond_quagga) ;
+ if (MQUEUE_DEFAULT_INTERVAL != 0)
+ {
+ mq->kick.cond.interval = MQUEUE_DEFAULT_INTERVAL ;
+ mq->kick.cond.timeout = qt_get_monotonic()
+ + MQUEUE_DEFAULT_INTERVAL ;
+ } ;
+ break;
+
+ case mqt_signal_unicast:
+ case mqt_signal_broadcast:
+ /* head/tail pointers set NULL already */
+ break;
+
+ default:
+ zabort("Invalid mqueue queue type") ;
+ } ;
+
+ return mq ;
+} ;
+
+/* Set new timeout interval (or unset by setting <= 0)
+ *
+ * Sets the next timeout to be the time now + new interval (or never).
+ */
+void
+mqueue_set_timeout_interval(mqueue_queue mq, qtime_t interval)
+{
+ qpt_mutex_lock(&mq->mutex) ;
+
+ dassert( (mq->type == mqt_cond_unicast) ||
+ (mq->type == mqt_cond_broadcast) ) ;
+
+ mq->kick.cond.interval = interval ;
+ mq->kick.cond.timeout = (interval > 0) ? qt_get_monotonic() + interval
+ : 0 ;
+ qpt_mutex_unlock(&mq->mutex) ;
+} ;
+
+/*==============================================================================
+ * Message Block memory management.
+ *
+ * Allocates message_block structures in lots of 256. Uses first message_block
+ * in each lot to keep track of the lots.
+ *
+ * mqueue_initialise MUST be called before the first message block is allocated.
+ */
+
+static pthread_mutex_t* p_mb_mutex ; /* NULL => no mutex (yet) */
+static pthread_mutex_t mb_mutex ;
+
+#define MB_LOT_SIZE 256
+
+static mqueue_block mb_lot_list = NULL ;
+static mqueue_block mb_free_list = NULL ;
+
+static mqueue_block mqueue_block_new_lot(void) ;
+
+/* Get an empty message block
+ */
+mqueue_block
+mqueue_block_new(void)
+{
+ mqueue_block mb ;
+
+ qpt_mutex_lock(&mb_mutex) ;
+
+ mb = mb_free_list ;
+ if (mb == NULL)
+ mb = mqueue_block_new_lot() ;
+
+ mb_free_list = mb->next ;
+
+ qpt_mutex_unlock(&mb_mutex) ;
+
+ memset(mb, 0, sizeof(struct mqueue_block)) ;
+
+ return mb ;
+} ;
+
+/* Free message block when done with it.
+ */
+void
+mqueue_block_free(mqueue_block mb)
+{
+ qpt_mutex_lock(&mb_mutex) ;
+
+ mb->next = mb_free_list ;
+ mb_free_list = mb ;
+
+ qpt_mutex_unlock(&mb_mutex) ;
+} ;
+
+/* Make a new lot of empty message_block structures.
+ *
+ * NB: caller MUST hold the mb_mutex.
+ *
+ */
+static mqueue_block
+mqueue_block_new_lot(void)
+{
+ mqueue_block first, last, this ;
+
+ mqueue_block new = XCALLOC(MTYPE_MQUEUE_BLOCKS,
+ SIZE(struct mqueue_block, MB_LOT_SIZE)) ;
+ first = &new[1] ;
+ last = &new[MB_LOT_SIZE - 1] ;
+
+ new->next = mb_lot_list ; /* add to list of lots */
+ mb_lot_list = new ;
+
+ /* String all the new message_blocks together. */
+ this = last ;
+ while (this > first)
+ {
+ mqueue_block prev = this-- ;
+ this->next = prev ;
+ } ;
+ assert(this == first) ;
+
+ last->next = mb_free_list ; /* point last at old free list */
+ mb_free_list = first ; /* new blocks at head of free list */
+
+ return mb_free_list ;
+} ;
+
+/*==============================================================================
+ * Enqueue and dequeue messages.
+ */
+
+static void mqueue_kick_signal(mqueue_queue mq, int n) ;
+static void mqueue_dequeue_signal(mqueue_queue mq, mqueue_thread_signal mtsig) ;
+
+/* Enqueue message.
+ *
+ * If priority != 0, will enqueue after any previously enqueued priority
+ * messages.
+ *
+ * If there are any waiters, then we kick one or all of them.
+ *
+ * Note that we decrement or zero the waiters count here -- because if the
+ * waiter did it, they might not run before something else is enqueued.
+ * Similarly, if the kick uses a signal, the signal block is dequeued here.
+ *
+ * The waiter count is only incremented when a dequeue is attempted and the
+ * queue is empty, then:
+ *
+ * for a broadcast type message queue, the first message that arrives will
+ * kick all the waiters into action.
+ *
+ * for a signal type message queue, each message that arrives will kick one
+ * waiter.
+ */
+
+void
+mqueue_enqueue(mqueue_queue mq, mqueue_block mb, int priority)
+{
+ qpt_mutex_lock(&mq->mutex) ;
+
+ if (mq->head == NULL)
+ {
+ mb->next = NULL ;
+ mq->head = mb ;
+ mq->tail_priority = priority ? mb : NULL ;
+ mq->tail = mb ;
+ }
+ else if (priority)
+ {
+ mqueue_block after = mq->tail_priority ;
+ if (after == NULL)
+ {
+ mb->next = mq->head ;
+ mq->head = mb ;
+ }
+ else
+ {
+ mb->next = after->next ;
+ after->next = mb ;
+ }
+ mq->tail_priority = mb ;
+ }
+ else
+ {
+ dassert(mq->tail != NULL) ;
+ mb->next = NULL ;
+ mq->tail->next = mb ;
+ mq->tail = mb ;
+ } ;
+
+ if (mq->waiters != 0)
+ {
+ switch (mq->type)
+ {
+ case mqt_cond_unicast:
+ qpt_cond_signal(&mq->kick.cond.wait_here) ;
+ --mq->waiters ;
+ break ;
+
+ case mqt_cond_broadcast:
+ qpt_cond_broadcast(&mq->kick.cond.wait_here) ;
+ mq->waiters = 0 ;
+ break ;
+
+ case mqt_signal_unicast:
+ mqueue_kick_signal(mq, 1) ; /* pick off first and kick it (MUST be */
+ /* one) and decrement the waiters count */
+ break ;
+
+ case mqt_signal_broadcast:
+ mqueue_kick_signal(mq, mq->waiters) ;
+ dassert(mq->kick.signal.head == NULL) ;
+ break;
+
+ default:
+ zabort("Invalid mqueue queue type") ;
+ } ;
+ } ;
+
+ qpt_mutex_unlock(&mq->mutex) ;
+} ;
+
+/* Dequeue message.
+ *
+ * If the queue is empty and wait != 0, will wait for a message. In which
+ * case for:
+ *
+ * * mqt_cond_xxxx type message queues, will wait on the condition variable,
+ * and may timeout.
+ *
+ * If the argument is NULL, uses the already set up timeout, if there is
+ * one.
+ *
+ * If the argument is not NULL, it is a pointer to a qtime_mono_t time,
+ * to be used as the new timeout time.
+ *
+ * * mqt_signal_xxxx type message queues, will register the given signal
+ * (mtsig argument MUST be provided), and return immediately.
+ *
+ * Returns a message block if one is available. (And not otherwise.)
+ */
+
+mqueue_block
+mqueue_dequeue(mqueue_queue mq, int wait, void* arg)
+{
+ mqueue_block mb ;
+ mqueue_thread_signal last ;
+
+ mqueue_thread_signal mtsig ;
+ qtime_mono_t timeout_time ;
+
+ qpt_mutex_lock(&mq->mutex) ;
+
+ while (1)
+ {
+ mb = mq->head ;
+ if (mb != NULL)
+ break ; /* Easy if queue not empty */
+
+ if (!wait)
+ goto done ; /* Easy if not waiting ! mb == NULL */
+
+ ++mq->waiters ; /* Another waiter */
+
+ switch (mq->type)
+ {
+ case mqt_cond_unicast: /* Now wait here */
+ case mqt_cond_broadcast:
+ if ((arg == NULL) && (mq->kick.cond.interval <= 0))
+ qpt_cond_wait(&mq->kick.cond.wait_here, &mq->mutex) ;
+ else
+ {
+ timeout_time = (arg != NULL) ? *(qtime_mono_t*)arg
+ : mq->kick.cond.timeout ;
+
+ if (qpt_cond_timedwait(&mq->kick.cond.wait_here, &mq->mutex,
+ timeout_time) == 0)
+ {
+ /* Timed out -- update timeout time, if required */
+ if (mq->kick.cond.interval > 0)
+ {
+ qtime_mono_t now = qt_get_monotonic() ;
+ timeout_time = mq->kick.cond.timeout
+ + mq->kick.cond.interval ;
+ if (timeout_time < now)
+ timeout_time = now + mq->kick.cond.interval ;
+
+ mq->kick.cond.timeout = timeout_time ;
+ } ;
+
+ goto done ; /* immediate return. mb == NULL */
+ } ;
+ } ;
+ break ;
+
+ case mqt_signal_unicast: /* Register desire for signal */
+ case mqt_signal_broadcast:
+ mtsig = arg ;
+ dassert(mtsig != NULL) ;
+
+ last = mq->kick.signal.tail ;
+ if (last == NULL)
+ {
+ mq->kick.signal.head = mtsig ;
+ mtsig->prev = (void*)mq ;
+ }
+ else
+ {
+ last->next = mtsig ;
+ mtsig->prev = last ;
+ }
+ mtsig->next = NULL ;
+ mq->kick.signal.tail = mtsig ;
+
+ goto done ; /* BUT do not wait ! mb == NULL */
+
+ default:
+ zabort("Invalid mqueue queue type") ;
+ } ;
+ } ;
+
+ /* Have something to pull off the queue */
+
+ mq->head = mb->next ;
+ if (mb == mq->tail_priority)
+ mq->tail_priority = NULL ;
+
+done:
+ qpt_mutex_unlock(&mq->mutex) ;
+
+ return mb ;
+} ;
+
+/* No longer waiting for a signal.
+ *
+ * Returns true <=> signal has been kicked.
+ */
+int
+mqueue_done_waiting(mqueue_queue mq, mqueue_thread_signal mtsig)
+{
+ int kicked ;
+
+ qpt_mutex_lock(&mq->mutex) ;
+
+ dassert( (mq->type == mqt_signal_unicast) ||
+ (mq->type == mqt_signal_broadcast) ) ;
+ dassert(mtsig != NULL) ;
+
+ /* When the thread is signalled, the prev entry is set NULL and the */
+ /* waiters count is decremented. */
+ /* */
+ /* So, only need to do something here if the prev is not NULL (ie the */
+ /* mqueue_thread_signal is still on the list. */
+
+ kicked = (mtsig->prev == NULL) ;
+
+ if (!kicked)
+ mqueue_dequeue_signal(mq, mtsig) ;
+
+ qpt_mutex_unlock(&mq->mutex) ;
+
+ return kicked ;
+} ;
+
+/*==============================================================================
+ * Message queue signal handling
+ */
+
+/* Initialise a message queue signal structure (struct mqueue_thread_signal).
+ * Allocate one if required.
+ *
+ * Returns address of the structure.
+ */
+mqueue_thread_signal
+mqueue_thread_signal_init(mqueue_thread_signal mqt, qpt_thread_t thread,
+ int signum)
+{
+ if (mqt == NULL)
+ mqt = XCALLOC(MTYPE_MQUEUE_THREAD_SIGNAL,
+ sizeof(struct mqueue_thread_signal)) ;
+ else
+ memset(mqt, 0, sizeof(struct mqueue_thread_signal)) ;
+
+ /* next and prev fields set to NULL already. */
+
+ mqt->qpthread = thread ;
+ mqt->signum = signum ;
+
+ return mqt ;
+} ;
+
+/* Signal the first 'n' threads on the to be signalled list.
+ *
+ * Removes the threads from the list and reduces the waiters count.
+ *
+ * NB: sets the prev entry in the mqueue_thread_signal block to NULL, so that
+ * the thread can tell that its signal has been kicked.
+ *
+ * NB: *** MUST own the mqueue_queue mutex. ***
+ */
+static void
+mqueue_kick_signal(mqueue_queue mq, int n)
+{
+ mqueue_thread_signal mtsig ;
+
+ while (n--)
+ {
+ mqueue_dequeue_signal(mq, mtsig = mq->kick.signal.head) ;
+ qpt_thread_signal(mtsig->qpthread, mtsig->signum) ;
+ } ;
+} ;
+
+/* Remove given signal from given message queue.
+ *
+ * NB: *** MUST own the mqueue_queue mutex. ***
+ */
+static void
+mqueue_dequeue_signal(mqueue_queue mq, mqueue_thread_signal mtsig)
+{
+ mqueue_thread_signal next ;
+ mqueue_thread_signal prev ;
+
+ dassert((mq->kick.signal.head != NULL) && (mq->waiters != 0)) ;
+
+ next = mtsig->next ;
+ prev = mtsig->prev ;
+
+ if (prev == (void*)mq) /* marker for head of list */
+ {
+ dassert(mq->kick.signal.head == mtsig) ;
+ mq->kick.signal.head = next ;
+ }
+ else
+ {
+ dassert((prev != NULL) && (prev->next == mtsig)) ;
+ prev->next = next ;
+ } ;
+
+ if (next != NULL)
+ next->prev = prev ;
+
+ mtsig->next = NULL ;
+ mtsig->prev = NULL ; /* essential to show signal kicked */
+ --mq->waiters ; /* one fewer waiter */
+
+ dassert( ((mq->kick.signal.head == NULL) && (mq->waiters == 0)) ||
+ ((mq->kick.signal.head != NULL) && (mq->waiters != 0)) ) ;
+} ;
+
+/*==============================================================================
+ * Initialise Message Queue handling
+ *
+ * Must be called before any qpt_threads are started.
+ *
+ * TODO: how do we shut down message queue handling ?
+ */
+void
+mqueue_initialise(int qpthreads)
+{
+ if (qpthreads)
+ p_mb_mutex = qpt_mutex_init(&mb_mutex, qpt_mutex_quagga) ;
+} ;
diff --git a/lib/mqueue.h b/lib/mqueue.h
new file mode 100644
index 00000000..5c10911b
--- /dev/null
+++ b/lib/mqueue.h
@@ -0,0 +1,141 @@
+/* Message Queue data structure -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_MQUEUE_H
+#define _ZEBRA_MQUEUE_H
+
+#include "qpthreads.h"
+#include "qtime.h"
+
+/*==============================================================================
+ */
+
+typedef struct mqueue_block* mqueue_block ;
+
+typedef uint32_t mqueue_flags_t ;
+typedef uint32_t mqueue_context_t ;
+typedef union
+{
+ void* p ;
+ uintptr_t u ;
+ intptr_t i ;
+} mqueue_arg_t ;
+
+typedef void mqueue_action(mqueue_block) ;
+
+struct mqueue_block
+{
+ mqueue_block next ; /* single linked list -- see ... */
+
+ mqueue_action* action ; /* for message dispatch */
+
+ mqueue_flags_t flags ; /* for message handler */
+
+ mqueue_context_t context ; /* for message revoke */
+
+ mqueue_arg_t arg0 ; /* may be pointer to more data or integer */
+ mqueue_arg_t arg1 ; /* may be pointer to more data or integer */
+} ;
+
+typedef struct mqueue_thread_signal* mqueue_thread_signal ;
+
+struct mqueue_thread_signal {
+ mqueue_thread_signal next ; /* NULL => last on list */
+ mqueue_thread_signal prev ; /* NULL => NOT on list -- vital ! */
+
+ qpt_thread_t qpthread ; /* qpthread to kick */
+ int signum ; /* signal to kick with */
+} ;
+
+enum mqueue_queue_type {
+ mqt_cond_unicast, /* use qpt_cond_signal to kick the queue */
+ mqt_cond_broadcast, /* use qpt_cond_broadcast to kick the queue */
+ mqt_signal_unicast, /* use single qpt_signal to kick the queue */
+ mqt_signal_broadcast, /* use multiple qpt_signal to kick the queue */
+};
+
+#ifndef MQUEUE_DEFAULT_INTERVAL
+# define MQUEUE_DEFAULT_INTERVAL QTIME(5)
+#endif
+
+struct mqueue_queue_cond {
+ qpt_cond_t wait_here ;
+ qtime_mono_t timeout ;
+ qtime_t interval ;
+} ;
+
+struct mqueue_queue_signal {
+ mqueue_thread_signal head ; /* NULL => list is empty */
+ mqueue_thread_signal tail ;
+};
+
+typedef struct mqueue_queue* mqueue_queue ;
+
+struct mqueue_queue
+{
+ qpt_mutex_t mutex ;
+
+ mqueue_block head ; /* NULL => list is empty */
+ mqueue_block tail_priority ; /* last priority message (if any & not empty) */
+ mqueue_block tail ; /* last message (if not empty) */
+
+ enum mqueue_queue_type type ;
+
+ unsigned waiters ;
+
+ union {
+ struct mqueue_queue_cond cond ;
+ struct mqueue_queue_signal signal ;
+ } kick ;
+} ;
+
+/*==============================================================================
+ * Functions
+ */
+
+void
+mqueue_initialise(int qpthreads) ;
+
+mqueue_queue
+mqueue_init_new(mqueue_queue mq, enum mqueue_queue_type type) ;
+
+void
+mqueue_set_timeout_interval(mqueue_queue mq, qtime_t interval) ;
+
+mqueue_thread_signal
+mqueue_thread_signal_init(mqueue_thread_signal mqt, qpt_thread_t thread,
+ int signum) ;
+mqueue_block
+mqueue_block_new(void) ;
+
+void
+mqueue_block_free(mqueue_block mb) ;
+
+void
+mqueue_enqueue(mqueue_queue mq, mqueue_block mb, int priority) ;
+
+mqueue_block
+mqueue_dequeue(mqueue_queue mq, int wait, void* arg) ;
+
+int
+mqueue_done_waiting(mqueue_queue mq, mqueue_thread_signal mtsig) ;
+
+#endif /* _ZEBRA_MQUEUE_H */
diff --git a/lib/plist.c b/lib/plist.c
index 0f802a83..b01c48f6 100644
--- a/lib/plist.c
+++ b/lib/plist.c
@@ -1,6 +1,10 @@
/* Prefix list functions.
* Copyright (C) 1999 Kunihiro Ishiguro
*
+ * 24-Nov-2009 -- substantially re-cast to speed up the handling of very
+ * large prefix-lists (10,000+ entries).
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify
@@ -29,494 +33,949 @@
#include "buffer.h"
#include "stream.h"
#include "log.h"
+#include "symtab.h"
+#include "vector.h"
-/* Each prefix-list's entry. */
-struct prefix_list_entry
+/* This implements ip prefix-list functions.
+ *
+ * A prefix-list is referred to by name, where a name is an arbitrary string,
+ * case-sensitive. When showing prefix-lists the names are sorted
+ * "alphabetically", except for any digit sections, which sort numerically.
+ * Note that leading zeros are significant... "01" is not the same as "1",
+ * and sorts after it.
+*/
+
+enum prefix_flags {
+ PREFIX_ANY = 0x01, /* prefix declared as 'any' */
+ PREFIX_LE = 0x02, /* explicit 'le' */
+ PREFIX_GE = 0x04, /* explicit 'ge' */
+ PREFIX_SEQ = 0x10, /* explicit sequence number */
+} ;
+
+struct prefix_list ;
+struct prefix_list_entry ;
+
+/* Master structure of prefix_list.
+ *
+ * Each address family has it's own distinct set of prefix lists. (Including
+ * the fake AFI_ORF_PREFIX "family".)
+ *
+ * This means that a prefix_list name is local to an address family, but
+ * global wrt router instances.
+ * */
+struct prefix_master
{
- int seq;
+ struct symbol_table table ; /* table of prefix_list by name. */
- int le;
- int ge;
-
- enum prefix_list_type type;
+ int seqnum_flag ; /* ip prefix-list sequence-number state. */
- int any;
- struct prefix prefix;
+ struct prefix_list *recent ; /* the latest update. */
- unsigned long refcnt;
- unsigned long hitcnt;
+ struct prefix_list *cache_owner ;
+ /* prefix_list that owns the dup_cache */
+ struct vector dup_cache ; /* the dup_cache vector */
- struct prefix_list_entry *next;
- struct prefix_list_entry *prev;
+ void (*add_hook) (struct prefix_list *);
+ /* executed when new prefix_list is added. */
+ void (*delete_hook) (struct prefix_list *);
+ /* executed when a prefix_list is deleted. */
};
-/* List of struct prefix_list. */
-struct prefix_list_list
+/* Each prefix_list is described by one of these.*/
+struct prefix_list
{
- struct prefix_list *head;
- struct prefix_list *tail;
-};
+ struct prefix_master *master ; /* parent table: scope of this list. */
+ struct symbol* sym ; /* symbol in parent's symbol table */
-/* Master structure of prefix_list. */
-struct prefix_master
+ char *desc ; /* ip prefix-list description */
+
+ afi_t afi ; /* address family for all prefixes */
+ /* this is the *real* address family, so */
+ /* not "AFI_ORF_PREFIX" or similar. */
+
+ struct vector list ; /* the actual list of prefix matches */
+
+ int (*cmp)(struct prefix_list_entry** p_a, struct prefix_list_entry** p_b) ;
+ /* used when searching for duplicates */
+
+ int rangecount; /* XXX TODO: discover what this is for ?? */
+ /* Is not changed anywhere !! */
+} ;
+
+/* Each prefix-list's entry. */
+struct prefix_list_entry
{
- /* List of prefix_list which name is number. */
- struct prefix_list_list num;
+ int seq;
- /* List of prefix_list which name is string. */
- struct prefix_list_list str;
+ enum prefix_list_type type;
- /* Whether sequential number is used. */
- int seqnum;
+ int flags ; /* zero or more of PREFIX_ANY, PREFIX_LE and PREFIX_GE */
+ int le ; /* for exact match, set to prefix length */
+ int ge ; /* ditto */
- /* The latest update. */
- struct prefix_list *recent;
+ struct prefix prefix;
- /* Hook function which is executed when new prefix_list is added. */
- void (*add_hook) (struct prefix_list *);
+ u_int32_t mask ; /* for IPv4 -- host order. */
+ /* for last significant word of IPv6 -- network order */
+ int last ; /* for IPv4 -- not used. */
+ /* for IPv6 -- word to apply mask to. */
- /* Hook function which is executed when prefix_list is deleted. */
- void (*delete_hook) (struct prefix_list *);
+ unsigned long refcnt;
+ unsigned long hitcnt;
};
/* Static structure of IPv4 prefix_list's master. */
-static struct prefix_master prefix_master_ipv4 =
-{
- {NULL, NULL},
- {NULL, NULL},
- 1,
- NULL,
- NULL,
-};
+static struct prefix_master prefix_master_ipv4 ;
#ifdef HAVE_IPV6
/* Static structure of IPv6 prefix-list's master. */
-static struct prefix_master prefix_master_ipv6 =
-{
- {NULL, NULL},
- {NULL, NULL},
- 1,
- NULL,
- NULL,
-};
+static struct prefix_master prefix_master_ipv6 ;
#endif /* HAVE_IPV6*/
/* Static structure of BGP ORF prefix_list's master. */
-static struct prefix_master prefix_master_orf =
-{
- {NULL, NULL},
- {NULL, NULL},
- 1,
- NULL,
- NULL,
-};
-
-static struct prefix_master *
-prefix_master_get (afi_t afi)
+static struct prefix_master prefix_master_orf ;
+
+/* For real afi, the choice is strictly limited, and includes IPv6
+ * only if HAVE_IPV6 ! */
+#ifdef HAVE_IPV6
+#define assert_afi_real(a) assert(((a) == AFI_IP) || ((a) == AFI_IP6))
+#else
+#define assert_afi_real(a) assert((a) == AFI_IP)
+#endif
+
+/* Map afi to prefix_master.
+ *
+ * Note: there is no ipv6 master if not HAVE_IPV6.
+ *
+ * Returns address of prefix_master, or NULL if unknown afi.
+ */
+static inline struct prefix_master *
+prefix_master_get(afi_t afi)
{
- if (afi == AFI_IP)
- return &prefix_master_ipv4;
+ switch (afi)
+ {
+ case AFI_IP:
+ return &prefix_master_ipv4;
#ifdef HAVE_IPV6
- else if (afi == AFI_IP6)
- return &prefix_master_ipv6;
+ case AFI_IP6:
+ return &prefix_master_ipv6;
#endif /* HAVE_IPV6 */
- else if (afi == AFI_ORF_PREFIX)
- return &prefix_master_orf;
- return NULL;
-}
+ case AFI_ORF_PREFIX:
+ return &prefix_master_orf;
+ default:
+ return NULL;
+ } ;
+} ;
-/* Lookup prefix_list from list of prefix_list by name. */
-struct prefix_list *
-prefix_list_lookup (afi_t afi, const char *name)
+/* Map afi to maximum prefix length. Implied assert_afi_real(). */
+static int
+prefix_max_length(afi_t afi)
{
- struct prefix_list *plist;
- struct prefix_master *master;
+ switch (afi)
+ {
+ case AFI_IP:
+ return IPV4_MAX_BITLEN ;
+#ifdef HAVE_IPV6
+ case AFI_IP6:
+ return IPV6_MAX_BITLEN ;
+#endif
+ default:
+ assert(0) ; /* Should not get here ! */
+ return 0 ;
+ } ;
+} ;
- if (name == NULL)
- return NULL;
+/* Compare prefix list entries, taking into account:
+ *
+ * -- prefix value -- assumes is masked down correctly
+ * -- prefix length
+ * -- ge
+ * -- le
+ * -- type
+ *
+ * ... everything *except* the sequence number.
+ */
- master = prefix_master_get (afi);
- if (master == NULL)
- return NULL;
+#define PREFIX_LIST_CMP_RET(f) \
+ if ((*p_a)->f != (*p_b)->f) \
+ return ( (*p_a)->f < (*p_b)->f) ? -1 : +1
+#define PREFIX_LIST_CMP_RET_NL(f) \
+ if ((*p_a)->f != (*p_b)->f) \
+ return (ntohl((*p_a)->f) < ntohl((*p_b)->f)) ? -1 : +1
+#define PREFIX_LIST_CMP_REST \
+PREFIX_LIST_CMP_RET(prefix.prefixlen) ; \
+PREFIX_LIST_CMP_RET(ge) ; \
+PREFIX_LIST_CMP_RET(le) ; \
+PREFIX_LIST_CMP_RET(type)
- for (plist = master->num.head; plist; plist = plist->next)
- if (strcmp (plist->name, name) == 0)
- return plist;
+static int
+prefix_list_ipv4_cmp(struct prefix_list_entry** p_a,
+ struct prefix_list_entry** p_b)
+{
+ PREFIX_LIST_CMP_RET_NL(prefix.u.prefix4.s_addr) ;
+ PREFIX_LIST_CMP_REST ;
+ return 0 ;
+} ;
- for (plist = master->str.head; plist; plist = plist->next)
- if (strcmp (plist->name, name) == 0)
- return plist;
+#ifdef HAVE_IPV6 /*----------------------------------------------------*/
+static int
+prefix_list_ipv6_cmp(struct prefix_list_entry** p_a,
+ struct prefix_list_entry** p_b)
+{
+#ifdef s6_addr32
+ PREFIX_LIST_CMP_RET_NL(prefix.u.prefix6.s6_addr32[0]) ;
+ PREFIX_LIST_CMP_RET_NL(prefix.u.prefix6.s6_addr32[1]) ;
+ PREFIX_LIST_CMP_RET_NL(prefix.u.prefix6.s6_addr32[2]) ;
+ PREFIX_LIST_CMP_RET_NL(prefix.u.prefix6.s6_addr32[3]) ;
+#else
+ int c ;
+ if ((c = memcmp(&(*p_a)->prefix.u.prefix6.s6_addr,
+ &(*p_b)->prefix.u.prefix6.s6_addr, 16)) != 0)
+ return c ;
+#endif
+ PREFIX_LIST_CMP_REST ;
+ return 0 ;
+} ;
+#endif /*----------------------------------------------------*/
+
+/*==============================================================================
+ * Operations on prefix_master.
+ */
- return NULL;
-}
+static void prefix_list_delete (struct prefix_list* plist) ;
+static void prefix_dup_cache_free(struct prefix_master* pm) ;
-static struct prefix_list *
-prefix_list_new (void)
+/* Initialise given prefix_master. */
+static void
+prefix_master_init(struct prefix_master * pm)
{
- struct prefix_list *new;
+ memset(pm, 0, sizeof(struct prefix_master)) ;
- new = XCALLOC (MTYPE_PREFIX_LIST, sizeof (struct prefix_list));
- return new;
-}
+ symbol_table_init_new(&pm->table, pm, 20, 200, NULL, NULL) ;
+ pm->seqnum_flag = 1 ; /* Default is to generate sequence numbers. */
+} ;
+/* Reset given prefix_master.
+ *
+ * Frees all prefix lists and empties the symbol table. Any references to
+ * prefix lists are the responsibility of the reference owners.
+ *
+ * Resets to the default sequence numbering state.
+ *
+ * Retains current add_hook and delete_hook functions.
+ */
static void
-prefix_list_free (struct prefix_list *plist)
+prefix_master_reset(struct prefix_master * pm)
{
- XFREE (MTYPE_PREFIX_LIST, plist);
-}
+ struct prefix_list* plist ;
+ while ((plist = symbol_table_ream_keep(&(pm->table))))
+ prefix_list_delete(plist) ;
-static struct prefix_list_entry *
-prefix_list_entry_new (void)
-{
- struct prefix_list_entry *new;
+ pm->seqnum_flag = 1 ; /* Default is to generate sequence numbers. */
- new = XCALLOC (MTYPE_PREFIX_LIST_ENTRY, sizeof (struct prefix_list_entry));
- return new;
+ pm->recent = NULL ;
+ prefix_dup_cache_free(pm) ;
+} ;
+
+/* Add hook function. */
+void
+prefix_list_add_hook (void (*func)(struct prefix_list *plist))
+{
+ prefix_master_ipv4.add_hook = func;
+#ifdef HAVE_IPV6
+ prefix_master_ipv6.add_hook = func;
+#endif /* HAVE_IPV6 */
}
-static void
-prefix_list_entry_free (struct prefix_list_entry *pentry)
+/* Delete hook function. */
+void
+prefix_list_delete_hook (void (*func)(struct prefix_list *plist))
{
- XFREE (MTYPE_PREFIX_LIST_ENTRY, pentry);
+ prefix_master_ipv4.delete_hook = func;
+#ifdef HAVE_IPV6
+ prefix_master_ipv6.delete_hook = func;
+#endif /* HAVE_IPVt6 */
}
-/* Insert new prefix list to list of prefix_list. Each prefix_list
- is sorted by the name. */
-static struct prefix_list *
-prefix_list_insert (afi_t afi, const char *name)
+/*==============================================================================
+ * Prefix List Use.
+ *
+ * The prefix_list_ref type is a struct symbol_ref*, so we can operate on
+ * prefix_list_ref* arguments, keeping the stored reference values up to date.
+ */
+
+const char*
+prefix_list_get_name(struct prefix_list* plist)
{
- unsigned int i;
- long number;
- struct prefix_list *plist;
- struct prefix_list *point;
- struct prefix_list_list *list;
- struct prefix_master *master;
+ return symbol_get_name(plist->sym) ;
+} ;
- master = prefix_master_get (afi);
- if (master == NULL)
- return NULL;
+/* Set reference to prefix_list, by name.
+ * Replaces any existing reference.
+ *
+ * Returns the new value of the prefix_list_ref.
+ *
+ * NB: if reference existed, the parent and tag fields are preserved.
+ * Otherwise they are set to 0.
+ */
+prefix_list_ref
+prefix_list_set_ref(prefix_list_ref* p_ref, afi_t afi, const char* name)
+{
+ struct prefix_master* pm = prefix_master_get(afi) ;
- /* Allocate new prefix_list and copy given name. */
- plist = prefix_list_new ();
- plist->name = XSTRDUP (MTYPE_PREFIX_LIST_STR, name);
- plist->master = master;
+ if (pm == NULL)
+ return NULL ;
- /* If name is made by all digit character. We treat it as
- number. */
- for (number = 0, i = 0; i < strlen (name); i++)
- {
- if (isdigit ((int) name[i]))
- number = (number * 10) + (name[i] - '0');
- else
- break;
- }
+ return *p_ref = symbol_set_ref(*p_ref, symbol_find(&(pm->table), name)) ;
+} ;
- /* In case of name is all digit character */
- if (i == strlen (name))
- {
- plist->type = PREFIX_TYPE_NUMBER;
+/* Copy reference to prefix_list.
+ * Replaces any existing reference (by NULL if reference is NULL).
+ *
+ * Returns the new value of the prefix_list_ref.
+ *
+ * NB: if reference existed, the parent and tag fields are preserved.
+ * Otherwise they are set to 0.
+ */
+prefix_list_ref
+prefix_list_copy_ref(prefix_list_ref* p_dst, prefix_list_ref src)
+{
+ return *p_dst = symbol_set_ref(*p_dst, sym_ref_symbol(src)) ;
+} ;
- /* Set prefix_list to number list. */
- list = &master->num;
+/* Unset reference to prefix_list (does nothing if reference is NULL).
+ *
+ * Returns the new value of the prefix_list_ref -- ie NULL.
+ */
+prefix_list_ref
+prefix_list_unset_ref(prefix_list_ref* p_ref)
+{
+ return *p_ref = symbol_unset_ref(*p_ref, 1) ;
+} ;
- for (point = list->head; point; point = point->next)
- if (atol (point->name) >= number)
- break;
- }
- else
- {
- plist->type = PREFIX_TYPE_STRING;
-
- /* Set prefix_list to string list. */
- list = &master->str;
-
- /* Set point to insertion point. */
- for (point = list->head; point; point = point->next)
- if (strcmp (point->name, name) >= 0)
- break;
- }
+/* Get name of prefix_list to which given reference (if any) refers.
+ *
+ * Returns NULL if the reference is NULL.
+ */
+const char*
+prefix_list_ref_name(prefix_list_ref ref)
+{
+ return sym_ref_name(ref) ;
+} ;
- /* In case of this is the first element of master. */
- if (list->head == NULL)
- {
- list->head = list->tail = plist;
- return plist;
- }
+/* Return "identity" of prefix_list referred to by the given reference.
+ * Will be NULL if the reference is NULL.
+ *
+ * Two references to the same prefix_list will have the same "identity".
+ */
+void* prefix_list_ref_ident(prefix_list_ref ref)
+{
+ return (void*)sym_ref_symbol(ref) ;
+} ;
- /* In case of insertion is made at the tail of access_list. */
- if (point == NULL)
- {
- plist->prev = list->tail;
- list->tail->next = plist;
- list->tail = plist;
- return plist;
- }
+/* Return prefix_list referred to by the given reference.
+ * Will be NULL If the reference is NULL *OR* if the prefix_list is undefined.
+ */
+struct prefix_list* prefix_list_ref_plist(prefix_list_ref ref)
+{
+ return (struct prefix_list*)sym_ref_value(ref) ;
+} ;
- /* In case of insertion is made at the head of access_list. */
- if (point == list->head)
- {
- plist->next = list->head;
- list->head->prev = plist;
- list->head = plist;
- return plist;
- }
- /* Insertion is made at middle of the access_list. */
- plist->next = point;
- plist->prev = point->prev;
+/* Apply a prefix_list to the given prefix. */
+enum prefix_list_type
+prefix_list_apply (struct prefix_list *plist, void *object)
+{
+ struct prefix *p ;
+ int plen ;
+ struct prefix_list_entry* pe ;
+ vector_index i ;
+
+ in_addr_t ip ;
+#ifdef s6_addr32
+ u_int32_t* pp ;
+ u_int32_t* pep ;
+#else
+ unsigned char* pp ;
+ unsigned char* pep ;
+#endif
+ /* Deny if prefix_list is undefined or empty */
+ if ((plist == NULL) || (vector_end(&plist->list) == 0))
+ return PREFIX_DENY;
- if (point->prev)
- point->prev->next = plist;
- point->prev = plist;
+ p = object ;
+ plen = p->prefixlen ;
- return plist;
-}
+ /* For maximum performance we have separate loops for IPv4 and IPv6 */
+ assert_afi_real(plist->afi) ;
-static struct prefix_list *
-prefix_list_get (afi_t afi, const char *name)
-{
- struct prefix_list *plist;
+ switch (plist->afi)
+ {
+ case AFI_IP:
+ ip = p->u.prefix4.s_addr ;
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ {
+ ++pe->refcnt ;
+ if ((((pe->prefix.u.prefix4.s_addr ^ ip) & pe->mask) == 0)
+ && (plen >= pe->ge) && (plen <= pe->le))
+ {
+ ++pe->hitcnt;
+ return pe->type ;
+ } ;
+ }
+ break ;
+
+ case AFI_IP6:
+#ifdef s6_addr32
+ pp = p->u.prefix6.s6_addr32 ;
+#else
+ pp = p->u.prefix6.s6_addr ;
+#endif
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ {
+ int l = pe->last ;
+ int j ;
+ ++pe->refcnt ;
+#ifdef s6_addr32
+ pep = pe->prefix.u.prefix6.s6_addr32 ;
+#else
+ pep = pe->prefix.u.prefix6.s6_addr ;
+#endif
+ for (j = 0 ; j < l ; j++)
+ if (pep[j] != pp[j])
+ goto NEXT ;
+ if ((((pep[l] ^ pp[l]) & pe->mask) == 0)
+ && (plen >= pe->ge) && (plen <= pe->le))
+ {
+ ++pe->hitcnt;
+ return pe->type ;
+ } ;
+ NEXT: ;
+ }
+ break ;
- plist = prefix_list_lookup (afi, name);
+ default:
+ assert(0) ;
+ } ;
- if (plist == NULL)
- plist = prefix_list_insert (afi, name);
- return plist;
+ return PREFIX_DENY;
}
+/*==============================================================================
+ * Basic constructors and destructors.
+ */
-/* Delete prefix-list from prefix_list_master and free it. */
-static void
-prefix_list_delete (struct prefix_list *plist)
+/* Construct a new prefix_list and set the the associated symbol's value.
+ *
+ * Implied assert_afi_real().
+ */
+static struct prefix_list *
+prefix_list_new(struct prefix_master* pm, struct symbol* sym, afi_t afi)
{
- struct prefix_list_list *list;
- struct prefix_master *master;
- struct prefix_list_entry *pentry;
- struct prefix_list_entry *next;
+ struct prefix_list* new ;
- /* If prefix-list contain prefix_list_entry free all of it. */
- for (pentry = plist->head; pentry; pentry = next)
- {
- next = pentry->next;
- prefix_list_entry_free (pentry);
- plist->count--;
- }
-
- master = plist->master;
-
- if (plist->type == PREFIX_TYPE_NUMBER)
- list = &master->num;
- else
- list = &master->str;
+ new = XCALLOC (MTYPE_PREFIX_LIST, sizeof (struct prefix_list));
+ /* NB: implicitly sets the list empty. */
- if (plist->next)
- plist->next->prev = plist->prev;
- else
- list->tail = plist->prev;
+ new->sym = symbol_inc_ref(sym) ;
+ new->master = pm ;
- if (plist->prev)
- plist->prev->next = plist->next;
- else
- list->head = plist->next;
+ new->afi = afi ;
+ switch (afi)
+ {
+ case AFI_IP:
+ new->cmp = prefix_list_ipv4_cmp ;
+ break ;
+#ifdef HAVE_IPV6
+ case AFI_IP6:
+ new->cmp = prefix_list_ipv6_cmp ;
+ break ;
+#endif
+ default:
+ assert(0) ; /* Should not get here ! */
+ } ;
- if (plist->desc)
- XFREE (MTYPE_TMP, plist->desc);
+ symbol_set_value(sym, new) ;
- /* Make sure master's recent changed prefix-list information is
- cleared. */
- master->recent = NULL;
+ return new ;
+} ;
- if (plist->name)
- XFREE (MTYPE_PREFIX_LIST_STR, plist->name);
-
- prefix_list_free (plist);
-
- if (master->delete_hook)
- (*master->delete_hook) (NULL);
+/* Initialise prefix_list entry -- cleared to zeros. */
+static struct prefix_list_entry *
+prefix_list_entry_init(struct prefix_list_entry * pe)
+{
+ return memset(pe, 0, sizeof(struct prefix_list_entry));
}
+/* Allocate new prefix_list entry -- cleared to zeros. */
static struct prefix_list_entry *
-prefix_list_entry_make (struct prefix *prefix, enum prefix_list_type type,
- int seq, int le, int ge, int any)
+prefix_list_entry_new (void)
{
- struct prefix_list_entry *pentry;
-
- pentry = prefix_list_entry_new ();
-
- if (any)
- pentry->any = 1;
-
- prefix_copy (&pentry->prefix, prefix);
- pentry->type = type;
- pentry->seq = seq;
- pentry->le = le;
- pentry->ge = ge;
-
- return pentry;
+ return XCALLOC (MTYPE_PREFIX_LIST_ENTRY, sizeof (struct prefix_list_entry));
}
-/* Add hook function. */
-void
-prefix_list_add_hook (void (*func) (struct prefix_list *plist))
+/* Free given prefix list entry. */
+static void
+prefix_list_entry_free (struct prefix_list_entry* pe)
{
- prefix_master_ipv4.add_hook = func;
-#ifdef HAVE_IPV6
- prefix_master_ipv6.add_hook = func;
-#endif /* HAVE_IPV6 */
+ XFREE (MTYPE_PREFIX_LIST_ENTRY, pe);
}
-/* Delete hook function. */
-void
-prefix_list_delete_hook (void (*func) (struct prefix_list *plist))
+/* Set cache owned by nobody, and free the contents of the cache (if any). */
+static void
+prefix_dup_cache_free(struct prefix_master* pm)
{
- prefix_master_ipv4.delete_hook = func;
-#ifdef HAVE_IPV6
- prefix_master_ipv6.delete_hook = func;
-#endif /* HAVE_IPVt6 */
+ pm->cache_owner = NULL ;
+ vector_reset(&pm->dup_cache, 0) ;
}
-/* Calculate new sequential number. */
-static int
-prefix_new_seq_get (struct prefix_list *plist)
+/* Delete prefix_list from prefix_list_master and free it and its contents. */
+/* The prefix_list's symbol is set undefined. */
+static void
+prefix_list_delete (struct prefix_list* plist)
{
- int maxseq;
- int newseq;
- struct prefix_list_entry *pentry;
+ struct prefix_master* pm = plist->master ;
+ unsigned int i ;
+ struct prefix_list_entry* pe ;
- maxseq = newseq = 0;
+ /* Free all the prefix_list_entries, then free the vector they live in. */
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ prefix_list_entry_free(pe) ;
+ vector_reset(&plist->list, 0) ;
- for (pentry = plist->head; pentry; pentry = pentry->next)
- {
- if (maxseq < pentry->seq)
- maxseq = pentry->seq;
- }
+ /* If there is a description, release that now. */
+ if (plist->desc)
+ XFREE (MTYPE_TMP, plist->desc);
- newseq = ((maxseq / 5) * 5) + 5;
-
- return newseq;
-}
+ /* Can no longer own the dup_cache. */
+ if (pm->cache_owner == plist)
+ prefix_dup_cache_free(pm) ;
-/* Return prefix list entry which has same seq number. */
-static struct prefix_list_entry *
-prefix_seq_check (struct prefix_list *plist, int seq)
-{
- struct prefix_list_entry *pentry;
+ /* Symbol no longer has a value & drop reference. */
+ symbol_unset_value(plist->sym) ;
+ plist->sym = symbol_dec_ref(plist->sym) ;
- for (pentry = plist->head; pentry; pentry = pentry->next)
- if (pentry->seq == seq)
- return pentry;
- return NULL;
-}
+ /* Finally, release the prefix_list structure. */
+ XFREE (MTYPE_PREFIX_LIST, plist) ;
-static struct prefix_list_entry *
-prefix_list_entry_lookup (struct prefix_list *plist, struct prefix *prefix,
- enum prefix_list_type type, int seq, int le, int ge)
-{
- struct prefix_list_entry *pentry;
+ /* No longer have a recently changed prefix-list */
+ pm->recent = NULL ;
- for (pentry = plist->head; pentry; pentry = pentry->next)
- if (prefix_same (&pentry->prefix, prefix) && pentry->type == type)
- {
- if (seq >= 0 && pentry->seq != seq)
- continue;
+ /* Tell the world. */
+ if (pm->delete_hook)
+ (*pm->delete_hook) (NULL);
+} ;
- if (pentry->le != le)
- continue;
- if (pentry->ge != ge)
- continue;
+/*==============================================================================
+ * Operations on prefix_lists
+ */
- return pentry;
- }
+/* Seek prefix_list by name in give prefix master. Does NOT create. */
+static struct prefix_list *
+prefix_list_seek (struct prefix_master* pm, const char *name)
+{
+ return symbol_get_value(symbol_seek(&(pm->table), name)) ;
+} ;
- return NULL;
-}
+/* Lookup prefix_list by afi and name -- if afi is known, and name not NULL.
+ *
+ * Returns NULL if no prefix_list by that afi and name. Tolerates unknown afi
+ * and allows "fake" afi (eg. AFI_ORF_PREFIX).
+ */
+struct prefix_list *
+prefix_list_lookup (afi_t afi, const char *name)
+{
+ struct prefix_master* pm = prefix_master_get(afi) ;
-static void
-prefix_list_entry_delete (struct prefix_list *plist,
- struct prefix_list_entry *pentry,
- int update_list)
+ if ((name == NULL) || (pm == NULL))
+ return NULL;
+
+ return prefix_list_seek(pm, name) ;
+} ;
+
+/* Get prefix_list -- creating empty one if required. */
+static struct prefix_list *
+prefix_list_get (struct prefix_master* pm, const char *name, afi_t afi)
{
- if (plist == NULL || pentry == NULL)
- return;
- if (pentry->prev)
- pentry->prev->next = pentry->next;
- else
- plist->head = pentry->next;
- if (pentry->next)
- pentry->next->prev = pentry->prev;
- else
- plist->tail = pentry->prev;
+ struct symbol* sym ;
+ struct prefix_list* plist ;
- prefix_list_entry_free (pentry);
+ assert((pm != NULL) && (name != NULL)) ;
- plist->count--;
+ sym = symbol_find(&(pm->table), name) ; /* creates if required */
+ plist = symbol_get_value(sym) ;
- if (update_list)
- {
- if (plist->master->delete_hook)
- (*plist->master->delete_hook) (plist);
+ return plist ? plist : prefix_list_new(pm, sym, afi) ;
+} ;
- if (plist->head == NULL && plist->tail == NULL && plist->desc == NULL)
- prefix_list_delete (plist);
- else
- plist->master->recent = plist;
- }
-}
+/*==============================================================================
+ * Operations on prefix_list_entry
+ */
-static void
-prefix_list_entry_add (struct prefix_list *plist,
- struct prefix_list_entry *pentry)
+/* Sequence comparison function -- used in prefix_list_entry_lookup_seq */
+static int
+prefix_seq_cmp(const int** seq, const struct prefix_list_entry** pe)
{
- struct prefix_list_entry *replace;
- struct prefix_list_entry *point;
+ if (**seq != (*pe)->seq)
+ return (**seq < (*pe)->seq) ? -1 : + 1 ;
+ return 0 ;
+} ;
+
+/* Check any ge and le settings -- set defaults as required.
+ *
+ * -- sets the implied le for "any" or ge, if no explicit le set
+ *
+ * -- checks le & ge and updates as required the filter.
+ *
+ * Note that filter requires le = ge = prefix-length for an exact match.
+ *
+ * Returns: CMD_SUCCESS -- it's OK
+ * CMD_WARNING -- something amiss with the ge and/or le setting
+ *
+ * If ge: must be <= maximum prefix length and > actual prefix length
+ * else: set to prefix length
+ *
+ * If le: must be <= maximum prefix length and > actual prefix length
+ * else: if ge or any set to maximum prefix length
+ * else: set to prefix length
+ *
+ * If both ge and le: must have le > ge
+ *
+ * XXX TODO: see if we can allow explicit ge == prefix_length
+ * and explicit le == ge
+ */
+static int
+prefix_list_entry_ge_le_check(struct prefix_list_entry* pe, afi_t afi)
+{
+ int pl_max = prefix_max_length(afi) ;
+ int pl = pe->prefix.prefixlen ;
+
+ /* If we had ge, check in range, otherwise set to prefixlen. */
+ if (pe->flags & PREFIX_GE)
+ {
+ if (!(pe->ge <= pl_max) || !(pe->ge > pl))
+ return CMD_WARNING ;
+ }
+ else
+ pe->ge = pl ;
- /* Automatic asignment of seq no. */
- if (pentry->seq == -1)
- pentry->seq = prefix_new_seq_get (plist);
+ /* If we had le, check in range, otherwise set as required. */
+ if (pe->flags & PREFIX_LE)
+ {
+ if (!(pe->le <= pl_max) || !(pe->le > pl))
+ return CMD_WARNING ;
+ }
+ else
+ pe->le = (pe->flags & (PREFIX_ANY | PREFIX_GE)) ? pl_max : pl ;
- /* Is there any same seq prefix list entry? */
- replace = prefix_seq_check (plist, pentry->seq);
- if (replace)
- prefix_list_entry_delete (plist, replace, 0);
+ /* If had both ge and le, check that le > ge. */
+ if ((pe->flags & (PREFIX_GE | PREFIX_LE)) == (PREFIX_GE | PREFIX_LE))
+ if (pe->le <= pe->ge)
+ return CMD_WARNING ;
- /* Check insert point. */
- for (point = plist->head; point; point = point->next)
- if (point->seq >= pentry->seq)
- break;
+ return CMD_SUCCESS ;
+} ;
- /* In case of this is the first element of the list. */
- pentry->next = point;
+/* Lookup prefix_list_entry by its sequence number. Returns index of an entry
+ * in the prefix_list, and sets:
+ *
+ * result < 0 -- not found. index returned is of first entry in the
+ * prefix list, and this sequence number comes
+ * before it. (Or list is empty.)
+ * result == 0 -- found. index is of the entry found.
+ * result > 0 -- not found. index returned is of the entry with the largest
+ * sequence number smaller than the given one.
+ */
+static vector_index
+prefix_list_entry_lookup_seq(struct prefix_list *plist, int seq, int* result)
+{
+ return vector_bsearch(&plist->list, (vector_bsearch_cmp*)prefix_seq_cmp,
+ &seq, result) ;
+} ;
- if (point)
+/* Lookup prefix_list_entry by its contents.
+ *
+ * For large prefix_lists this uses the "dup_cache", which is an auxiliary
+ * vector, sorted by prefix value. Each prefix_master has a dup_cache,
+ * which is co-opted by the last prefix_list to use it.
+ *
+ * Returns index of an entry in the prefix_list or the dup_cache, and sets:
+ *
+ * cache -- NULL if not using dup_cache for this prefix_list.
+ * The index and result values refer to the main prefix_list.
+ *
+ * -- address of the cache (a vector).
+ * The index and result values refer to the cache. << NB
+ *
+ * result < 0 -- not found. index returned is of first entry in the
+ * prefix list, and this sequence number comes
+ * before it. (Or list is empty.)
+ * result == 0 -- found. index is of the entry found.
+ * result > 0 -- not found. index returned is of the entry with the largest
+ * sequence number smaller than the given one.
+ */
+static vector_index
+prefix_list_entry_lookup_val(struct prefix_list *plist,
+ struct prefix_list_entry* temp,
+ vector* cache,
+ int* result)
+{
+ /* See if we already have an entry like this one. */
+ if (vector_end(&plist->list) > 10)
{
- if (point->prev)
- point->prev->next = pentry;
- else
- plist->head = pentry;
-
- pentry->prev = point->prev;
- point->prev = pentry;
+ struct prefix_master* pm = plist->master ;
+
+ if (pm->cache_owner != plist)
+ {
+ /* Create new cache by copying vector. Releases any old cache. */
+ vector_copy_here(&pm->dup_cache, &plist->list) ;
+ /* Sort the result so can binary chop it. */
+ vector_sort(&pm->dup_cache, (vector_sort_cmp*)plist->cmp) ;
+ /* Now we own the cache. */
+ pm->cache_owner = plist ;
+ } ;
+
+ *cache = &pm->dup_cache ;
+ return vector_bsearch(*cache, (vector_bsearch_cmp*)plist->cmp, temp,
+ result) ;
}
else
{
- if (plist->tail)
- plist->tail->next = pentry;
- else
- plist->head = pentry;
+ struct prefix_list_entry* pe ;
+ vector_index i ;
+ *cache = NULL ; /* Not found in cache. */
+ *result = 0 ; /* Assume found ! */
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ {
+ if (plist->cmp(&pe, &temp) == 0)
+ return i ; /* Found ! */
+ } ;
+ *result = 1 ; /* Not found. */
+ return i ;
+ } ;
+} ;
+
+/* Look up prefix_list_entry looking for an exact match.
+ *
+ * If we have an explicit sequence number, then we look that up and then
+ * see if that prefix_list_entry is identical.
+ *
+ * Otherwise, look for entry whose value is identical, if any.
+ *
+ * Returns an index of a prefix_list entry and sets:
+ *
+ * -- result == 0 found -- index is of entry found
+ * -- result != 0 not found -- index is immaterial
+ *
+ * */
+static vector_index
+prefix_list_entry_lookup (struct prefix_list* plist,
+ struct prefix_list_entry* pe_seek, int* result)
+{
+ struct prefix_list_entry* pe_found ;
+ vector_index i ;
- pentry->prev = plist->tail;
- plist->tail = pentry;
+ if (pe_seek->flags & PREFIX_SEQ)
+ {
+ /* Lookup by sequence number. */
+ i = prefix_list_entry_lookup_seq(plist, pe_seek->seq, result) ;
+ if (*result == 0)
+ {
+ /* found by sequence number, now see if value matches. */
+ pe_found = vector_get_item(&plist->list, i) ;
+ *result = plist->cmp(&pe_seek, &pe_found) ;
+ } ;
+ }
+ else
+ {
+ /* Lookup by value. */
+ vector cache ;
+ i = prefix_list_entry_lookup_val(plist, pe_seek, &cache, result) ;
+ if ((*result == 0) && cache)
+ {
+ /* Found in the cache. We need it's position in prefix_list */
+ pe_found = vector_get_item(cache, i) ;
+ i = prefix_list_entry_lookup_seq(plist, pe_found->seq, result) ;
+ assert(*result == 0) ; /* MUST Find it !! */
+ } ;
}
- /* Increment count. */
- plist->count++;
+ return i ;
+} ;
+
+/* Insert prefix_list_entry or replace an existing one, if we can.
+ *
+ * May NOT insert or replace if an entry already exists with the same value,
+ * (where the value excludes the sequence number).
+ *
+ * Except that, if a sequence number is given, it is (trivially) possible to
+ * "replace" an entry with the same sequence number and the same value.
+ *
+ * Then, if no sequence number is given, one is allocated. The allocation
+ * rounds the last sequence number up to a multiple of 5 and adds 5.
+ *
+ * The prefix_list_entry is then put in the list by sequence number, replacing
+ * any existing entry.
+ *
+ * Returns: CMD_SUCCESS -- OK
+ * CMD_WARNING -- Nope, and NB: temp->seq set to sequence number
+ * of existing prefix_list_entry.
+ */
+static int
+prefix_list_entry_insert(struct prefix_list *plist,
+ struct prefix_list_entry *temp)
+{
+ struct prefix_list_entry* pe ;
+ vector cache ;
+ vector_index i, ic ;
+ int ret, retc ;
+ u_int32_t mask ;
+ int pl ;
+
+ /* See if we have an entry like this one, if we do:
+ *
+ * OK if sequence number is the same too -- nothing more to do !
+ * Fail if sequence number differs or was not set.
+ *
+ * If not found, and we own the cache, ic and retc tell us where in the
+ * cache the new entry belongs.
+ */
+ ic = prefix_list_entry_lookup_val(plist, temp, &cache, &retc) ;
+ if (retc == 0)
+ {
+ pe = vector_get_item(cache ? cache : &plist->list, ic) ;
+ if ((temp->flags & PREFIX_SEQ) && (pe->seq == temp->seq))
+ return CMD_SUCCESS ;
+ temp->seq = pe->seq ; /* capture clashing sequence number */
+ return CMD_WARNING ;
+ } ;
+
+ /* Now we need to find where to insert in the list. */
+ /* If required, we set implied sequence number, and insert at end. */
+ if (temp->flags & PREFIX_SEQ)
+ i = prefix_list_entry_lookup_seq(plist, temp->seq, &ret) ;
+ else
+ {
+ int last_seq = 0 ;
+ i = vector_end(&plist->list) ;
+ if (i != 0)
+ {
+ --i ; /* step back to last entry */
+ pe = vector_get_item(&plist->list, i) ;
+ last_seq = pe->seq ;
+ } ;
+ temp->seq = (((last_seq + 5 - 1) / 5) * 5) + 5 ;
+ ret = 1 ; /* insert after last entry (if any) */
+ } ;
+
+ /* If we found it with same sequence number, then replace entry,
+ * otherwise we need to create a new entry and insert it. i & ret show
+ * where we need to put the value in the list.
+ *
+ * If a dup cache exists, that must be kept up to date. ic & retc show
+ * where need to put the new value in the cache.
+ */
+ if (ret == 0)
+ {
+ /* We are going to replace an existing list entry. */
+ pe = vector_get_item(&plist->list, i) ; /* address of current entry */
+ /* If we have a cache, need to move the entry to it's new place */
+ if (cache)
+ {
+ /* We need to know where the old value was. */
+ vector_index io ;
+ int reto ;
+ io = vector_bsearch(cache, (vector_bsearch_cmp*)plist->cmp, pe,
+ &reto) ;
+ assert(reto == 0) ; /* MUST find it !! */
+ vector_move_item_here(cache, ic, retc, io) ;
+ } ;
+ }
+ else
+ {
+ /* We are going to insert a new list entry item. */
+ pe = prefix_list_entry_new() ;
+ vector_insert_item_here(&plist->list, i, ret, pe) ;
+ /* If we have a cache, need to insert the entry to it's place */
+ if (cache)
+ vector_insert_item_here(cache, ic, retc, pe) ;
+ } ;
+
+ /* Now we can set the value of the entry. */
+ *pe = *temp ;
+
+ /* Set mask and last ready to apply the filter. */
+ /* Note: if we don't have s6_addr32, we must handle IPv6 byte-wise ! */
+ pl = pe->prefix.prefixlen ;
+ mask = htonl((0xFFFFFFFF >> (pl & 0x1F)) ^ 0xFFFFFFFF) ;
+ switch (plist->afi)
+ {
+ case AFI_IP:
+ pe->mask = mask ;
+ break ;
+ case AFI_IP6:
+#ifdef s6_addr32
+ pe->mask = mask ;
+ pe->last = (pl == 0) ? 0 : (pl - 1) >> 5 ;
+#else
+ pe->mask = (0xFF >> (pl & 0x07)) ^ 0xFF ;
+ pe->last = (pl == 0) ? 0 : (pl - 1) >> 3 ;
+#endif
+ break ;
+ default:
+ assert(0) ;
+ } ;
/* Run hook function. */
if (plist->master->add_hook)
(*plist->master->add_hook) (plist);
plist->master->recent = plist;
+
+ return CMD_SUCCESS ;
}
+/* Delete prefix_list_entry, if we can.
+ *
+ * To delete an entry the caller must specify the exact value of an existing
+ * entry. If a sequence number is specified, that entry must exist, and its
+ * value must exactly match the given value. If no sequence number is
+ * specified, an entry must exist with exactly the given value.
+ *
+ * Returns: CMD_SUCCESS -- OK
+ * CMD_WARNING -- entry not found.
+ */
+static int
+prefix_list_entry_delete (struct prefix_list *plist,
+ struct prefix_list_entry *pe_seek)
+{
+ struct prefix_list_entry* pe ;
+ vector_index i ;
+ int ret ;
+
+ i = prefix_list_entry_lookup (plist, pe_seek, &ret) ;
+ if (ret)
+ return CMD_WARNING ;
+
+ pe = vector_delete_item(&plist->list, i) ;
+ assert(pe != NULL) ;
+
+ prefix_list_entry_free(pe) ; /* now release memory */
+
+ if (plist->master->delete_hook)
+ (*plist->master->delete_hook) (plist);
+
+ if ((vector_end(&plist->list) == 0) && (plist->desc == NULL))
+ prefix_list_delete (plist);
+ else
+ plist->master->recent = plist;
+
+ return CMD_SUCCESS ;
+} ;
+
+/*==============================================================================
+ * Common printing operations
+ */
+
/* Return string of prefix_list_type. */
static const char *
prefix_list_type_str (struct prefix_list_entry *pentry)
@@ -532,179 +991,192 @@ prefix_list_type_str (struct prefix_list_entry *pentry)
}
}
-static int
-prefix_list_entry_match (struct prefix_list_entry *pentry, struct prefix *p)
+/* Map afi to name of same: "ip" or "ipv6". Implied assert_afi_real(). */
+static const char*
+prefix_afi_name_str(afi_t afi)
{
- int ret;
+ switch (afi)
+ {
+ case AFI_IP:
+ return "ip" ;
+#ifdef HAVE_IPV6
+ case AFI_IP6:
+ return "ipv6" ;
+#endif
+ default:
+ assert(0) ; /* Should not get here ! */
+ return "?" ;
+ } ;
+} ;
- ret = prefix_match (&pentry->prefix, p);
- if (! ret)
- return 0;
-
- /* In case of le nor ge is specified, exact match is performed. */
- if (! pentry->le && ! pentry->ge)
- {
- if (pentry->prefix.prefixlen != p->prefixlen)
- return 0;
- }
- else
- {
- if (pentry->le)
- if (p->prefixlen > pentry->le)
- return 0;
-
- if (pentry->ge)
- if (p->prefixlen < pentry->ge)
- return 0;
- }
- return 1;
-}
+/* Print: "(ip|ipv6) prefix-list NAME" <post> */
+static void
+vty_prefix_list_name_print(struct vty* vty, struct prefix_list* plist,
+ const char* post)
+{
+ vty_out(vty, "%s prefix-list %s%s", prefix_afi_name_str(plist->afi),
+ (const char*)symbol_get_name(plist->sym), post) ;
+} ;
-enum prefix_list_type
-prefix_list_apply (struct prefix_list *plist, void *object)
+/* Print: "(ip|ipv6) prefix-list NAME: 99 entries" <post> */
+static void
+vty_prefix_list_name_count_print(struct vty* vty, struct prefix_list* plist,
+ const char* post)
{
- struct prefix_list_entry *pentry;
- struct prefix *p;
+ vty_prefix_list_name_print(vty, plist, "") ;
+ vty_out(vty, ": %d entries%s", vector_end(&plist->list), post);
+} ;
- p = (struct prefix *) object;
+/* Print: "(ip|ipv6) prefix-list NAME" UNDEFINED<post> */
+static void
+vty_prefix_list_undefined_print(struct vty* vty, afi_t afi, struct symbol* sym,
+ const char* post)
+{
+ vty_out(vty, "%s prefix-list %s UNDEFINED%s", prefix_afi_name_str(afi),
+ (const char*)symbol_get_name(sym), post) ;
+} ;
- if (plist == NULL)
- return PREFIX_DENY;
+/* Print: <indent>"Description: xxxx"<post>, if there is a description */
+static void
+vty_prefix_list_desc_print(struct vty* vty, struct prefix_list* plist,
+ int indent, const char* post)
+{
+ if (plist->desc)
+ vty_out (vty, "%sDescription: %s%s", VTY_SPACES(indent), plist->desc,
+ VTY_NEWLINE) ;
+}
+
+/* Size of buffer to hold either IPv4 or IPv6 string. */
+#ifndef INETX_ADDRSTRLEN
+# if INET_ADDRSTRLEN < INET6_ADDRSTRLEN
+# define INETX_ADDRSTRLEN INET6_ADDRSTRLEN
+# else
+# define INETX_ADDRSTRLEN INET_ADDRSTLEN
+# endif
+#endif
+
+/* Print value of given prefix_list_entry:
+ *
+ * "[seq 999 ](permit|deny) (any|XXXXX/999)[ ge 99][ le 99]"
+ * "[ '('hit count: 999, refcount: 999')']" <post>
+ *
+ * where: sequence number is included if "with_seq" specified
+ * ge and/or le are included if explicitly set
+ * the hit count and refcount are included if "with_stats" specified
+ */
+static void
+vty_prefix_list_value_print(struct vty* vty, struct prefix_list_entry* pe,
+ const char* post, int with_seq, int with_stats)
+{
+ if (with_seq)
+ vty_out(vty, "seq %d ", pe->seq) ;
- if (plist->count == 0)
- return PREFIX_PERMIT;
+ vty_out(vty, "%s ", prefix_list_type_str(pe)) ;
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ if (pe->flags & PREFIX_ANY)
+ vty_puts(vty, "any");
+ else
{
- pentry->refcnt++;
- if (prefix_list_entry_match (pentry, p))
- {
- pentry->hitcnt++;
- return pentry->type;
- }
- }
+ struct prefix *p = &pe->prefix ;
+ char buf[INETX_ADDRSTRLEN];
+ vty_out(vty, "%s/%d",
+ inet_ntop (p->family, &p->u.prefix, buf, INETX_ADDRSTRLEN),
+ p->prefixlen);
+ } ;
- return PREFIX_DENY;
+ if (pe->flags & PREFIX_GE)
+ vty_out(vty, " ge %d", pe->ge);
+ if (pe->flags & PREFIX_LE)
+ vty_out(vty, " le %d", pe->le);
+
+ if (with_stats)
+ vty_out (vty, " (hit count: %lu, refcount: %lu)", pe->hitcnt, pe->refcnt);
+
+ vty_puts(vty, post) ;
}
static void __attribute__ ((unused))
prefix_list_print (struct prefix_list *plist)
{
- struct prefix_list_entry *pentry;
+ struct prefix_list_entry* pe ;
+ vector_index i ;
+ struct vty* vty = NULL ;
if (plist == NULL)
return;
- printf ("ip prefix-list %s: %d entries\n", plist->name, plist->count);
+ vty_prefix_list_name_count_print(vty, plist, VTY_NEWLINE) ;
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ for (VECTOR_ITEMS(&plist->list, pe, i))
{
- if (pentry->any)
- printf ("any %s\n", prefix_list_type_str (pentry));
- else
- {
- struct prefix *p;
- char buf[BUFSIZ];
-
- p = &pentry->prefix;
-
- printf (" seq %d %s %s/%d",
- pentry->seq,
- prefix_list_type_str (pentry),
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
- if (pentry->ge)
- printf (" ge %d", pentry->ge);
- if (pentry->le)
- printf (" le %d", pentry->le);
- printf ("\n");
- }
+ vty_out_indent(vty, 2) ;
+ vty_prefix_list_value_print(vty, pe, VTY_NEWLINE, 1, 1) ;
}
}
-
-/* Retrun 1 when plist already include pentry policy. */
-static struct prefix_list_entry *
-prefix_entry_dup_check (struct prefix_list *plist,
- struct prefix_list_entry *new)
-{
- struct prefix_list_entry *pentry;
- int seq = 0;
-
- if (new->seq == -1)
- seq = prefix_new_seq_get (plist);
- else
- seq = new->seq;
-
- for (pentry = plist->head; pentry; pentry = pentry->next)
- {
- if (prefix_same (&pentry->prefix, &new->prefix)
- && pentry->type == new->type
- && pentry->le == new->le
- && pentry->ge == new->ge
- && pentry->seq != seq)
- return pentry;
- }
- return NULL;
-}
+/*==============================================================================
+ * vty prefix_list operations.
+ */
-static int
-vty_invalid_prefix_range (struct vty *vty, const char *prefix)
+/* Look up given prefix_list -- complain if not found. */
+static struct prefix_list*
+vty_prefix_list_lookup(struct vty *vty, afi_t afi, const char* name)
{
- vty_out (vty, "%% Invalid prefix range for %s, make sure: len < ge-value <= le-value%s",
- prefix, VTY_NEWLINE);
- return CMD_WARNING;
-}
+ struct prefix_list* plist = prefix_list_lookup(afi, name);
+ if (plist == NULL)
+ vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
+ return plist ;
+} ;
+/* Process parameters for (ip|ipv6) prefix-list and no (ip|ipv6) prefix-list
+ *
+ * Fills in the given prefix_list_entry structure, ready for looking up,
+ * inserting or deleting prefix_list_entry.
+ *
+ * Checks parameters for validity/legality.
+ *
+ * Returns a CMD_xxxx return code. CMD_SUCCESS => OK !
+ */
static int
-vty_prefix_list_install (struct vty *vty, afi_t afi, const char *name,
- const char *seq, const char *typestr,
- const char *prefix, const char *ge, const char *le)
+vty_prefix_list_process(struct vty *vty, struct prefix_list_entry* pe,
+ struct prefix_list* plist,
+ afi_t afi, const char *seq_str, const char *type_str,
+ const char *prefix_str,
+ const char *ge_str, const char *le_str)
{
- int ret;
- enum prefix_list_type type;
- struct prefix_list *plist;
- struct prefix_list_entry *pentry;
- struct prefix_list_entry *dup;
- struct prefix p;
- int any = 0;
- int seqnum = -1;
- int lenum = 0;
- int genum = 0;
+ int ret ;
- /* Sequential number. */
- if (seq)
- seqnum = atoi (seq);
+ assert_afi_real(afi) ; /* require real (and supported) afi */
- /* ge and le number */
- if (ge)
- genum = atoi (ge);
- if (le)
- lenum = atoi (le);
+ prefix_list_entry_init(pe) ; /* clears everything, including flags */
+
+ /* Sequence number. */
+ if (seq_str)
+ {
+ pe->flags |= PREFIX_SEQ ;
+ pe->seq = atoi(seq_str) ;
+ } ;
/* Check filter type. */
- if (strncmp ("permit", typestr, 1) == 0)
- type = PREFIX_PERMIT;
- else if (strncmp ("deny", typestr, 1) == 0)
- type = PREFIX_DENY;
+ if (strncmp ("permit", type_str, 1) == 0)
+ pe->type = PREFIX_PERMIT;
+ else if (strncmp ("deny", type_str, 1) == 0)
+ pe->type = PREFIX_DENY;
else
{
vty_out (vty, "%% prefix type must be permit or deny%s", VTY_NEWLINE);
return CMD_WARNING;
}
- /* "any" is special token for matching any IPv4 addresses. */
+ /* Watch out for "any" */
+ if (strncmp ("any", prefix_str, strlen (prefix_str)) == 0)
+ pe->flags |= PREFIX_ANY ;
+
+ /* Process the prefix. */
if (afi == AFI_IP)
{
- if (strncmp ("any", prefix, strlen (prefix)) == 0)
- {
- ret = str2prefix_ipv4 ("0.0.0.0/0", (struct prefix_ipv4 *) &p);
- genum = 0;
- lenum = IPV4_MAX_BITLEN;
- any = 1;
- }
- else
- ret = str2prefix_ipv4 (prefix, (struct prefix_ipv4 *) &p);
-
+ if (pe->flags & PREFIX_ANY)
+ prefix_str = "0.0.0.0/0" ;
+ ret = str2prefix_ipv4 (prefix_str, (struct prefix_ipv4 *)&pe->prefix);
if (ret <= 0)
{
vty_out (vty, "%% Malformed IPv4 prefix%s", VTY_NEWLINE);
@@ -714,16 +1186,9 @@ vty_prefix_list_install (struct vty *vty, afi_t afi, const char *name,
#ifdef HAVE_IPV6
else if (afi == AFI_IP6)
{
- if (strncmp ("any", prefix, strlen (prefix)) == 0)
- {
- ret = str2prefix_ipv6 ("::/0", (struct prefix_ipv6 *) &p);
- genum = 0;
- lenum = IPV6_MAX_BITLEN;
- any = 1;
- }
- else
- ret = str2prefix_ipv6 (prefix, (struct prefix_ipv6 *) &p);
-
+ if (pe->flags & PREFIX_ANY)
+ prefix_str = "::/0" ;
+ ret = str2prefix_ipv6 (prefix_str, (struct prefix_ipv6 *)&pe->prefix);
if (ret <= 0)
{
vty_out (vty, "%% Malformed IPv6 prefix%s", VTY_NEWLINE);
@@ -732,155 +1197,147 @@ vty_prefix_list_install (struct vty *vty, afi_t afi, const char *name,
}
#endif /* HAVE_IPV6 */
- /* ge and le check. */
- if (genum && genum <= p.prefixlen)
- return vty_invalid_prefix_range (vty, prefix);
+ /* ge and le number */
+ if (ge_str)
+ {
+ pe->ge = atoi (ge_str) ;
+ pe->flags |= PREFIX_GE ;
+ }
+
+ if (le_str)
+ {
+ pe->le = atoi (le_str);
+ pe->flags |= PREFIX_LE ;
+ } ;
+
+ /* Complete the entry we've constructed, and check ge and le. */
+ ret = prefix_list_entry_ge_le_check(pe, afi) ;
+
+ if (ret != CMD_SUCCESS)
+ vty_out (vty, "%% Invalid prefix range for %s, make sure: "
+ "len < ge-value <= le-value%s",
+ prefix_str, VTY_NEWLINE);
- if (lenum && lenum <= p.prefixlen)
- return vty_invalid_prefix_range (vty, prefix);
+ return ret ;
+} ;
+
+/* Install a prefix_list_entry.
+ *
+ * Deals with all of ip prefix-list and ipv6 prefix-list commands.
+ *
+ * Note:
+ *
+ */
+
+static int
+vty_prefix_list_install (struct vty *vty, afi_t afi, const char *name,
+ const char *seq_str, const char *type_str,
+ const char *prefix_str,
+ const char *ge_str, const char *le_str)
+{
+ struct prefix_master* pm ;
+ struct prefix_list *plist;
+ struct prefix_list_entry temp ;
+ int ret;
- if (lenum && genum > lenum)
- return vty_invalid_prefix_range (vty, prefix);
+ assert_afi_real(afi) ; /* UI stuff should ensure this */
+ pm = prefix_master_get(afi) ;
- if (genum && lenum == (afi == AFI_IP ? 32 : 128))
- lenum = 0;
+ /* Get prefix_list with name. Make new list if required. */
+ plist = prefix_list_get(pm, name, afi) ;
- /* Get prefix_list with name. */
- plist = prefix_list_get (afi, name);
+ /* Do the grunt work on the parameters.
+ * Completely fill in the temp prefix_list_entry structure.
+ */
+ ret = vty_prefix_list_process(vty, &temp, plist, afi, seq_str, type_str,
+ prefix_str, ge_str, le_str) ;
+ if (ret != CMD_SUCCESS)
+ return ret ;
- /* Make prefix entry. */
- pentry = prefix_list_entry_make (&p, type, seqnum, lenum, genum, any);
-
- /* Check same policy. */
- dup = prefix_entry_dup_check (plist, pentry);
+ /* Insert into the list, unless list contains an entry which is the same
+ * apart from the sequence number.
+ * If fails, sets the sequence no. in temp to the sequence number found.
+ */
+ ret = prefix_list_entry_insert(plist, &temp);
- if (dup)
+ if (ret != CMD_SUCCESS)
{
- prefix_list_entry_free (pentry);
vty_out (vty, "%% Insertion failed - prefix-list entry exists:%s",
VTY_NEWLINE);
- vty_out (vty, " seq %d %s %s", dup->seq, typestr, prefix);
- if (! any && genum)
- vty_out (vty, " ge %d", genum);
- if (! any && lenum)
- vty_out (vty, " le %d", lenum);
- vty_out (vty, "%s", VTY_NEWLINE);
- return CMD_WARNING;
+ vty_out_indent(vty, 2) ;
+ vty_prefix_list_value_print(vty, &temp, VTY_NEWLINE, 1, 0) ;
}
- /* Install new filter to the access_list. */
- prefix_list_entry_add (plist, pentry);
-
return CMD_SUCCESS;
}
+/* Remove a prefix_list_entry. */
static int
-vty_prefix_list_uninstall (struct vty *vty, afi_t afi, const char *name,
- const char *seq, const char *typestr,
- const char *prefix, const char *ge, const char *le)
+vty_prefix_list_uninstall(struct vty *vty, afi_t afi, const char *name,
+ const char *seq_str, const char *type_str,
+ const char *prefix_str,
+ const char *ge_str, const char *le_str)
{
- int ret;
- enum prefix_list_type type;
struct prefix_list *plist;
- struct prefix_list_entry *pentry;
- struct prefix p;
- int seqnum = -1;
- int lenum = 0;
- int genum = 0;
+ struct prefix_list_entry temp ;
+ int ret;
- /* Check prefix list name. */
- plist = prefix_list_lookup (afi, name);
- if (! plist)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ assert_afi_real(afi) ; /* UI should guarantee this. */
+
+ /* Seek prefix_list with name -- error if not found. */
+ plist = vty_prefix_list_lookup(vty, afi, name);
+ if (plist == NULL)
+ return CMD_WARNING ;
/* Only prefix-list name specified, delete the entire prefix-list. */
- if (seq == NULL && typestr == NULL && prefix == NULL &&
- ge == NULL && le == NULL)
+ if (seq_str == NULL && type_str == NULL && prefix_str == NULL &&
+ ge_str == NULL && le_str == NULL)
{
prefix_list_delete (plist);
return CMD_SUCCESS;
}
/* We must have, at a minimum, both the type and prefix here */
- if ((typestr == NULL) || (prefix == NULL))
+ if ((type_str == NULL) || (prefix_str == NULL))
{
vty_out (vty, "%% Both prefix and type required%s", VTY_NEWLINE);
return CMD_WARNING;
}
- /* Check sequence number. */
- if (seq)
- seqnum = atoi (seq);
+ /* Do the grunt work on the parameters.
+ * Completely fill in the temp prefix_list_entry structure.
+ */
+ ret = vty_prefix_list_process(vty, &temp, plist, afi, seq_str, type_str,
+ prefix_str, ge_str, le_str) ;
+ if (ret != CMD_SUCCESS)
+ return ret ;
- /* ge and le number */
- if (ge)
- genum = atoi (ge);
- if (le)
- lenum = atoi (le);
-
- /* Check of filter type. */
- if (strncmp ("permit", typestr, 1) == 0)
- type = PREFIX_PERMIT;
- else if (strncmp ("deny", typestr, 1) == 0)
- type = PREFIX_DENY;
- else
- {
- vty_out (vty, "%% prefix type must be permit or deny%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ /* Remove prefix_list_entry if we can. */
+ ret = prefix_list_entry_delete (plist, &temp);
- /* "any" is special token for matching any IPv4 addresses. */
- if (afi == AFI_IP)
- {
- if (strncmp ("any", prefix, strlen (prefix)) == 0)
- {
- ret = str2prefix_ipv4 ("0.0.0.0/0", (struct prefix_ipv4 *) &p);
- genum = 0;
- lenum = IPV4_MAX_BITLEN;
- }
- else
- ret = str2prefix_ipv4 (prefix, (struct prefix_ipv4 *) &p);
+ if (ret != CMD_SUCCESS)
+ vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- if (ret <= 0)
- {
- vty_out (vty, "%% Malformed IPv4 prefix%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
- }
-#ifdef HAVE_IPV6
- else if (afi == AFI_IP6)
- {
- if (strncmp ("any", prefix, strlen (prefix)) == 0)
- {
- ret = str2prefix_ipv6 ("::/0", (struct prefix_ipv6 *) &p);
- genum = 0;
- lenum = IPV6_MAX_BITLEN;
- }
- else
- ret = str2prefix_ipv6 (prefix, (struct prefix_ipv6 *) &p);
+ return ret;
+}
- if (ret <= 0)
- {
- vty_out (vty, "%% Malformed IPv6 prefix%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
- }
-#endif /* HAVE_IPV6 */
+static int
+vty_prefix_list_desc_set (struct vty *vty, afi_t afi, const char *name,
+ char* desc)
+{
+ struct prefix_master* pm ;
+ struct prefix_list *plist;
- /* Lookup prefix entry. */
- pentry = prefix_list_entry_lookup(plist, &p, type, seqnum, lenum, genum);
+ assert_afi_real(afi) ; /* UI stuff should ensure this */
+ pm = prefix_master_get(afi) ;
- if (pentry == NULL)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ /* Get prefix_list with name. Make new list if required. */
+ plist = prefix_list_get(pm, name, afi) ;
+
+ if (plist->desc)
+ XFREE (MTYPE_TMP, plist->desc) ; /* Discard any existing value */
- /* Install new filter to the access_list. */
- prefix_list_entry_delete (plist, pentry, 1);
+ plist->desc = desc ;
return CMD_SUCCESS;
}
@@ -890,21 +1347,15 @@ vty_prefix_list_desc_unset (struct vty *vty, afi_t afi, const char *name)
{
struct prefix_list *plist;
- plist = prefix_list_lookup (afi, name);
- if (! plist)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ plist = vty_prefix_list_lookup(vty, afi, name);
+ if (plist == NULL)
+ return CMD_WARNING ;
if (plist->desc)
- {
- XFREE (MTYPE_TMP, plist->desc);
- plist->desc = NULL;
- }
+ XFREE (MTYPE_TMP, plist->desc) ; /* sets plist->dec to NULL */
- if (plist->head == NULL && plist->tail == NULL && plist->desc == NULL)
- prefix_list_delete (plist);
+ if (vector_end(&plist->list) == 0)
+ prefix_list_delete(plist) ; /* delete list if all gone now */
return CMD_SUCCESS;
}
@@ -916,143 +1367,135 @@ enum display_type
detail_display,
sequential_display,
longer_display,
- first_match_display
+ first_match_display,
};
+/* Show given prefix_list */
static void
-vty_show_prefix_entry (struct vty *vty, afi_t afi, struct prefix_list *plist,
- struct prefix_master *master, enum display_type dtype,
+vty_show_prefix_entry (struct vty *vty, struct prefix_list *plist,
+ struct prefix_master* pm, enum display_type dtype,
int seqnum)
{
- struct prefix_list_entry *pentry;
-
/* Print the name of the protocol */
if (zlog_default)
- vty_out (vty, "%s: ", zlog_proto_names[zlog_default->protocol]);
-
+ vty_out (vty, "%s: ", zlog_get_proto_name(NULL));
+
if (dtype == normal_display)
{
- vty_out (vty, "ip%s prefix-list %s: %d entries%s",
- afi == AFI_IP ? "" : "v6",
- plist->name, plist->count, VTY_NEWLINE);
- if (plist->desc)
- vty_out (vty, " Description: %s%s", plist->desc, VTY_NEWLINE);
+ vty_prefix_list_name_count_print(vty, plist, VTY_NEWLINE) ;
+ vty_prefix_list_desc_print(vty, plist, 3, VTY_NEWLINE) ;
}
else if (dtype == summary_display || dtype == detail_display)
{
- vty_out (vty, "ip%s prefix-list %s:%s",
- afi == AFI_IP ? "" : "v6", plist->name, VTY_NEWLINE);
+ struct prefix_list_entry* p_f = vector_get_first_item(&plist->list) ;
+ struct prefix_list_entry* p_l = vector_get_last_item(&plist->list) ;
+
+ vty_prefix_list_name_print(vty, plist, ":") ;
+ vty_out_newline(vty) ;
- if (plist->desc)
- vty_out (vty, " Description: %s%s", plist->desc, VTY_NEWLINE);
+ vty_prefix_list_desc_print(vty, plist, 3, VTY_NEWLINE) ;
vty_out (vty, " count: %d, range entries: %d, sequences: %d - %d%s",
- plist->count, plist->rangecount,
- plist->head ? plist->head->seq : 0,
- plist->tail ? plist->tail->seq : 0,
+ vector_end(&plist->list), plist->rangecount,
+ p_f ? p_f->seq : 0,
+ p_l ? p_l->seq : 0,
VTY_NEWLINE);
- }
+ } ;
if (dtype != summary_display)
{
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ struct prefix_list_entry* pe ;
+ vector_index i ;
+ int with_seq = pm->seqnum_flag ;
+ int with_stats = (dtype == detail_display)
+ ||(dtype == sequential_display) ;
+
+ for (VECTOR_ITEMS(&plist->list, pe, i))
{
- if (dtype == sequential_display && pentry->seq != seqnum)
+ if ((dtype == sequential_display) && (pe->seq != seqnum))
continue;
-
- vty_out (vty, " ");
-
- if (master->seqnum)
- vty_out (vty, "seq %d ", pentry->seq);
- vty_out (vty, "%s ", prefix_list_type_str (pentry));
-
- if (pentry->any)
- vty_out (vty, "any");
- else
- {
- struct prefix *p = &pentry->prefix;
- char buf[BUFSIZ];
-
- vty_out (vty, "%s/%d",
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
-
- if (pentry->ge)
- vty_out (vty, " ge %d", pentry->ge);
- if (pentry->le)
- vty_out (vty, " le %d", pentry->le);
- }
-
- if (dtype == detail_display || dtype == sequential_display)
- vty_out (vty, " (hit count: %ld, refcount: %ld)",
- pentry->hitcnt, pentry->refcnt);
-
- vty_out (vty, "%s", VTY_NEWLINE);
+ vty_out_indent(vty, 3);
+ vty_prefix_list_value_print(vty, pe, VTY_NEWLINE,
+ with_seq, with_stats) ;
}
}
}
+/* Show given prefix list in given afi, or all prefix lists in given afi. */
+
static int
vty_show_prefix_list (struct vty *vty, afi_t afi, const char *name,
- const char *seq, enum display_type dtype)
+ const char *seq_str, enum display_type dtype)
{
struct prefix_list *plist;
- struct prefix_master *master;
- int seqnum = 0;
+ struct prefix_master *pm;
+ int seq = 0;
- master = prefix_master_get (afi);
- if (master == NULL)
+ pm = prefix_master_get(afi) ;
+ if (pm == NULL)
return CMD_WARNING;
- if (seq)
- seqnum = atoi (seq);
+ if (seq_str)
+ seq = atoi (seq_str);
if (name)
{
- plist = prefix_list_lookup (afi, name);
- if (! plist)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
- vty_show_prefix_entry (vty, afi, plist, master, dtype, seqnum);
+ /* Note that asking after an undefined prefix_list is an error. */
+ /* Does not mention references to an undefined prefix_list. */
+ plist = vty_prefix_list_lookup(vty, afi, name);
+ if (plist == NULL)
+ return CMD_WARNING;
+ vty_show_prefix_entry (vty, plist, pm, dtype, seq);
}
else
{
+ vector extract ;
+ vector_index i ;
+ struct symbol* sym ;
+
if (dtype == detail_display || dtype == summary_display)
{
- if (master->recent)
+ if (pm->recent)
vty_out (vty, "Prefix-list with the last deletion/insertion: %s%s",
- master->recent->name, VTY_NEWLINE);
+ (const char*)symbol_get_name(pm->recent->sym), VTY_NEWLINE) ;
}
- for (plist = master->num.head; plist; plist = plist->next)
- vty_show_prefix_entry (vty, afi, plist, master, dtype, seqnum);
+ /* Extract a vector of all prefix_list symbols, in name order. */
+ extract = symbol_table_extract(&pm->table, NULL, NULL, 0,
+ symbol_mixed_name_cmp) ;
+
+ for (VECTOR_ITEMS(extract, sym, i))
+ {
+ plist = symbol_get_value(sym) ;
+ if (plist != NULL)
+ vty_show_prefix_entry(vty, plist, pm, dtype, seq);
+ else
+ vty_prefix_list_undefined_print(vty, afi, sym, VTY_NEWLINE) ;
+ }
- for (plist = master->str.head; plist; plist = plist->next)
- vty_show_prefix_entry (vty, afi, plist, master, dtype, seqnum);
+ vector_free(extract) ; /* throw away temporary vector */
}
return CMD_SUCCESS;
}
static int
-vty_show_prefix_list_prefix (struct vty *vty, afi_t afi, const char *name,
+vty_show_prefix_list_prefix (struct vty *vty, afi_t afi, const char *name,
const char *prefix, enum display_type type)
{
struct prefix_list *plist;
- struct prefix_list_entry *pentry;
+ struct prefix_list_entry* pe ;
+ vector_index i ;
struct prefix p;
int ret;
int match;
+ int with_stats ;
- plist = prefix_list_lookup (afi, name);
- if (! plist)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ /* Error if cannot find prefix list. */
+ plist = vty_prefix_list_lookup(vty, afi, name);
+ if (plist == NULL)
+ return CMD_WARNING;
ret = str2prefix (prefix, &p);
if (ret <= 0)
@@ -1061,88 +1504,66 @@ vty_show_prefix_list_prefix (struct vty *vty, afi_t afi, const char *name,
return CMD_WARNING;
}
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ with_stats = (type == normal_display) || (type == first_match_display) ;
+
+ for (VECTOR_ITEMS(&plist->list, pe, i))
{
match = 0;
- if (type == normal_display || type == first_match_display)
- if (prefix_same (&p, &pentry->prefix))
- match = 1;
-
- if (type == longer_display)
- if (prefix_match (&p, &pentry->prefix))
- match = 1;
+ if ((type == normal_display || type == first_match_display))
+ match = prefix_same(&p, &pe->prefix) ;
+ else if (type == longer_display)
+ match = (prefix_match (&p, &pe->prefix)) ;
if (match)
{
- vty_out (vty, " seq %d %s ",
- pentry->seq,
- prefix_list_type_str (pentry));
-
- if (pentry->any)
- vty_out (vty, "any");
- else
- {
- struct prefix *p = &pentry->prefix;
- char buf[BUFSIZ];
-
- vty_out (vty, "%s/%d",
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
-
- if (pentry->ge)
- vty_out (vty, " ge %d", pentry->ge);
- if (pentry->le)
- vty_out (vty, " le %d", pentry->le);
- }
-
- if (type == normal_display || type == first_match_display)
- vty_out (vty, " (hit count: %ld, refcount: %ld)",
- pentry->hitcnt, pentry->refcnt);
-
- vty_out (vty, "%s", VTY_NEWLINE);
+ vty_out_indent(vty, 3);
+ vty_prefix_list_value_print(vty, pe, VTY_NEWLINE, 1, with_stats) ;
if (type == first_match_display)
- return CMD_SUCCESS;
+ break ;
}
}
return CMD_SUCCESS;
}
+/* Clear hit counters in all prefix_list_entries:
+ *
+ * a) in all prefix_lists -- name NULL
+ * b) in given prefix list -- prefix NULL
+ * c) that match given prefix, in given prefix_list
+ */
static int
-vty_clear_prefix_list (struct vty *vty, afi_t afi, const char *name,
+vty_clear_prefix_list (struct vty *vty, afi_t afi, const char *name,
const char *prefix)
{
- struct prefix_master *master;
+ struct prefix_master *pm;
struct prefix_list *plist;
- struct prefix_list_entry *pentry;
int ret;
struct prefix p;
+ struct prefix_list_entry* pe ;
+ vector_index i ;
- master = prefix_master_get (afi);
- if (master == NULL)
+ pm = prefix_master_get (afi);
+ if (pm == NULL)
return CMD_WARNING;
- if (name == NULL && prefix == NULL)
+ if (name == NULL)
{
- for (plist = master->num.head; plist; plist = plist->next)
- for (pentry = plist->head; pentry; pentry = pentry->next)
- pentry->hitcnt = 0;
-
- for (plist = master->str.head; plist; plist = plist->next)
- for (pentry = plist->head; pentry; pentry = pentry->next)
- pentry->hitcnt = 0;
+ struct symbol_walker walker ;
+ symbol_walk_start(&pm->table, &walker) ;
+ while ((plist = symbol_get_value(symbol_walk_next(&walker))))
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ pe->hitcnt = 0 ;
}
else
{
- plist = prefix_list_lookup (afi, name);
- if (! plist)
- {
- vty_out (vty, "%% Can't find specified prefix-list%s", VTY_NEWLINE);
- return CMD_WARNING;
- }
+ /* Error if cannot find prefix list. */
+ plist = vty_prefix_list_lookup(vty, afi, name);
+ if (plist == NULL)
+ return CMD_WARNING;
- if (prefix)
+ if (prefix != NULL)
{
ret = str2prefix (prefix, &p);
if (ret <= 0)
@@ -1152,20 +1573,13 @@ vty_clear_prefix_list (struct vty *vty, afi_t afi, const char *name,
}
}
- for (pentry = plist->head; pentry; pentry = pentry->next)
- {
- if (prefix)
- {
- if (prefix_match (&pentry->prefix, &p))
- pentry->hitcnt = 0;
- }
- else
- pentry->hitcnt = 0;
- }
- }
- return CMD_SUCCESS;
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ if ((prefix == NULL) || prefix_match(&pe->prefix, &p))
+ pe->hitcnt = 0;
+ } ;
+return CMD_SUCCESS;
}
-
+
DEFUN (ip_prefix_list,
ip_prefix_list_cmd,
"ip prefix-list WORD (deny|permit) (A.B.C.D/M|any)",
@@ -1177,7 +1591,7 @@ DEFUN (ip_prefix_list,
"IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
"Any prefix match. Same as \"0.0.0.0/0 le 32\"\n")
{
- return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL,
+ return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL,
argv[1], argv[2], NULL, NULL);
}
@@ -1193,7 +1607,7 @@ DEFUN (ip_prefix_list_ge,
"Minimum prefix length to be matched\n"
"Minimum prefix length\n")
{
- return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL, argv[1],
+ return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL, argv[1],
argv[2], argv[3], NULL);
}
@@ -1211,7 +1625,7 @@ DEFUN (ip_prefix_list_ge_le,
"Maximum prefix length to be matched\n"
"Maximum prefix length\n")
{
- return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL, argv[1],
+ return vty_prefix_list_install (vty, AFI_IP, argv[0], NULL, argv[1],
argv[2], argv[3], argv[4]);
}
@@ -1547,7 +1961,7 @@ DEFUN (ip_prefix_list_sequence_number,
PREFIX_LIST_STR
"Include/exclude sequence numbers in NVGEN\n")
{
- prefix_master_ipv4.seqnum = 1;
+ prefix_master_ipv4.seqnum_flag = 1;
return CMD_SUCCESS;
}
@@ -1559,7 +1973,7 @@ DEFUN (no_ip_prefix_list_sequence_number,
PREFIX_LIST_STR
"Include/exclude sequence numbers in NVGEN\n")
{
- prefix_master_ipv4.seqnum = 0;
+ prefix_master_ipv4.seqnum_flag = 0;
return CMD_SUCCESS;
}
@@ -1572,19 +1986,9 @@ DEFUN (ip_prefix_list_description,
"Prefix-list specific description\n"
"Up to 80 characters describing this prefix-list\n")
{
- struct prefix_list *plist;
-
- plist = prefix_list_get (AFI_IP, argv[0]);
-
- if (plist->desc)
- {
- XFREE (MTYPE_TMP, plist->desc);
- plist->desc = NULL;
- }
- plist->desc = argv_concat(argv, argc, 1);
-
- return CMD_SUCCESS;
-}
+ return vty_prefix_list_desc_set (vty, AFI_IP, argv[0],
+ argv_concat(argv, argc, 1));
+} ;
DEFUN (no_ip_prefix_list_description,
no_ip_prefix_list_description_cmd,
@@ -1759,7 +2163,7 @@ DEFUN (clear_ip_prefix_list_name_prefix,
{
return vty_clear_prefix_list (vty, AFI_IP, argv[0], argv[1]);
}
-
+
#ifdef HAVE_IPV6
DEFUN (ipv6_prefix_list,
ipv6_prefix_list_cmd,
@@ -1772,7 +2176,7 @@ DEFUN (ipv6_prefix_list,
"IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
"Any prefix match. Same as \"::0/0 le 128\"\n")
{
- return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL,
+ return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL,
argv[1], argv[2], NULL, NULL);
}
@@ -1788,7 +2192,7 @@ DEFUN (ipv6_prefix_list_ge,
"Minimum prefix length to be matched\n"
"Minimum prefix length\n")
{
- return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL, argv[1],
+ return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL, argv[1],
argv[2], argv[3], NULL);
}
@@ -1807,7 +2211,7 @@ DEFUN (ipv6_prefix_list_ge_le,
"Maximum prefix length\n")
{
- return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL, argv[1],
+ return vty_prefix_list_install (vty, AFI_IP6, argv[0], NULL, argv[1],
argv[2], argv[3], argv[4]);
}
@@ -2143,7 +2547,7 @@ DEFUN (ipv6_prefix_list_sequence_number,
PREFIX_LIST_STR
"Include/exclude sequence numbers in NVGEN\n")
{
- prefix_master_ipv6.seqnum = 1;
+ prefix_master_ipv6.seqnum_flag = 1;
return CMD_SUCCESS;
}
@@ -2155,7 +2559,7 @@ DEFUN (no_ipv6_prefix_list_sequence_number,
PREFIX_LIST_STR
"Include/exclude sequence numbers in NVGEN\n")
{
- prefix_master_ipv6.seqnum = 0;
+ prefix_master_ipv6.seqnum_flag = 0;
return CMD_SUCCESS;
}
@@ -2168,19 +2572,9 @@ DEFUN (ipv6_prefix_list_description,
"Prefix-list specific description\n"
"Up to 80 characters describing this prefix-list\n")
{
- struct prefix_list *plist;
-
- plist = prefix_list_get (AFI_IP6, argv[0]);
-
- if (plist->desc)
- {
- XFREE (MTYPE_TMP, plist->desc);
- plist->desc = NULL;
- }
- plist->desc = argv_concat(argv, argc, 1);
-
- return CMD_SUCCESS;
-}
+ return vty_prefix_list_desc_set (vty, AFI_IP6, argv[0],
+ argv_concat(argv, argc, 1));
+}
DEFUN (no_ipv6_prefix_list_description,
no_ipv6_prefix_list_description_cmd,
@@ -2355,190 +2749,138 @@ DEFUN (clear_ipv6_prefix_list_name_prefix,
return vty_clear_prefix_list (vty, AFI_IP6, argv[0], argv[1]);
}
#endif /* HAVE_IPV6 */
-
+
/* Configuration write function. */
static int
config_write_prefix_afi (afi_t afi, struct vty *vty)
{
struct prefix_list *plist;
- struct prefix_list_entry *pentry;
- struct prefix_master *master;
+ struct prefix_list_entry *pe;
+ struct prefix_master *pm;
int write = 0;
+ vector extract ;
+ vector_index i, ipe ;
+ struct symbol* sym ;
- master = prefix_master_get (afi);
- if (master == NULL)
+ pm = prefix_master_get (afi);
+ if (pm == NULL)
return 0;
- if (! master->seqnum)
+ if (! pm->seqnum_flag)
{
- vty_out (vty, "no ip%s prefix-list sequence-number%s",
+ vty_out (vty, "no ip%s prefix-list sequence-number%s",
afi == AFI_IP ? "" : "v6", VTY_NEWLINE);
vty_out (vty, "!%s", VTY_NEWLINE);
}
- for (plist = master->num.head; plist; plist = plist->next)
+ /* Extract a vector of all prefix_list symbols, in name order. */
+ extract = symbol_table_extract(&pm->table, NULL, NULL, 0,
+ symbol_mixed_name_cmp) ;
+ for (VECTOR_ITEMS(extract, sym, i))
{
- if (plist->desc)
- {
- vty_out (vty, "ip%s prefix-list %s description %s%s",
- afi == AFI_IP ? "" : "v6",
- plist->name, plist->desc, VTY_NEWLINE);
- write++;
- }
-
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ plist = symbol_get_value(sym) ;
+ if (plist)
{
- vty_out (vty, "ip%s prefix-list %s ",
- afi == AFI_IP ? "" : "v6",
- plist->name);
-
- if (master->seqnum)
- vty_out (vty, "seq %d ", pentry->seq);
-
- vty_out (vty, "%s ", prefix_list_type_str (pentry));
-
- if (pentry->any)
- vty_out (vty, "any");
- else
+ if (plist->desc)
{
- struct prefix *p = &pentry->prefix;
- char buf[BUFSIZ];
-
- vty_out (vty, "%s/%d",
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
-
- if (pentry->ge)
- vty_out (vty, " ge %d", pentry->ge);
- if (pentry->le)
- vty_out (vty, " le %d", pentry->le);
+ vty_prefix_list_name_print(vty, plist, "") ;
+ vty_out (vty, " description %s%s", plist->desc, VTY_NEWLINE) ;
+ write++ ;
}
- vty_out (vty, "%s", VTY_NEWLINE);
- write++;
- }
- /* vty_out (vty, "!%s", VTY_NEWLINE); */
- }
- for (plist = master->str.head; plist; plist = plist->next)
- {
- if (plist->desc)
- {
- vty_out (vty, "ip%s prefix-list %s description %s%s",
- afi == AFI_IP ? "" : "v6",
- plist->name, plist->desc, VTY_NEWLINE);
- write++;
+ for (VECTOR_ITEMS(&plist->list, pe, ipe))
+ {
+ vty_prefix_list_name_print(vty, plist, " ") ;
+ vty_prefix_list_value_print(vty, pe, VTY_NEWLINE,
+ pm->seqnum_flag, 0) ;
+ write++ ;
+ }
}
-
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ else
{
- vty_out (vty, "ip%s prefix-list %s ",
- afi == AFI_IP ? "" : "v6",
- plist->name);
-
- if (master->seqnum)
- vty_out (vty, "seq %d ", pentry->seq);
-
- vty_out (vty, "%s", prefix_list_type_str (pentry));
-
- if (pentry->any)
- vty_out (vty, " any");
- else
- {
- struct prefix *p = &pentry->prefix;
- char buf[BUFSIZ];
+ vty_puts(vty, "!! ") ;
+ vty_prefix_list_undefined_print(vty, afi, sym, VTY_NEWLINE) ;
+ write++ ;
+ } ;
+ } ;
- vty_out (vty, " %s/%d",
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
+ vector_free(extract) ; /* discard temporary vector */
- if (pentry->ge)
- vty_out (vty, " ge %d", pentry->ge);
- if (pentry->le)
- vty_out (vty, " le %d", pentry->le);
- }
- vty_out (vty, "%s", VTY_NEWLINE);
- write++;
- }
- }
-
return write;
}
struct stream *
-prefix_bgp_orf_entry (struct stream *s, struct prefix_list *plist,
+prefix_bgp_orf_entry (struct stream *s, prefix_list_ref ref,
u_char init_flag, u_char permit_flag, u_char deny_flag)
{
- struct prefix_list_entry *pentry;
+ struct prefix_list_entry *pe;
+ vector_index i ;
+
+ struct prefix_list *plist = prefix_list_ref_plist(ref) ;
if (! plist)
return s;
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ for (VECTOR_ITEMS(&plist->list, pe, i))
{
- u_char flag = init_flag;
- struct prefix *p = &pentry->prefix;
-
- flag |= (pentry->type == PREFIX_PERMIT ?
- permit_flag : deny_flag);
- stream_putc (s, flag);
- stream_putl (s, (u_int32_t)pentry->seq);
- stream_putc (s, (u_char)pentry->ge);
- stream_putc (s, (u_char)pentry->le);
- stream_put_prefix (s, p);
+ stream_putc (s, init_flag | (pe->type == PREFIX_PERMIT ? permit_flag
+ : deny_flag));
+ stream_putl (s, (u_int32_t)pe->seq);
+ stream_putc (s, (u_char)pe->ge);
+ stream_putc (s, (u_char)pe->le);
+ stream_put_prefix (s, &pe->prefix);
}
return s;
}
+/* Set or Unset a BGP ORF entry. */
int
prefix_bgp_orf_set (char *name, afi_t afi, struct orf_prefix *orfp,
int permit, int set)
{
- struct prefix_list *plist;
- struct prefix_list_entry *pentry;
+ struct prefix_list *plist ;
+ struct prefix_list_entry temp ;
+ int ret ;
- /* ge and le value check */
- if (orfp->ge && orfp->ge <= orfp->p.prefixlen)
- return CMD_WARNING;
- if (orfp->le && orfp->le <= orfp->p.prefixlen)
- return CMD_WARNING;
- if (orfp->le && orfp->ge > orfp->le)
- return CMD_WARNING;
+ assert_afi_real(afi) ;
- if (orfp->ge && orfp->le == (afi == AFI_IP ? 32 : 128))
- orfp->le = 0;
-
- plist = prefix_list_get (AFI_ORF_PREFIX, name);
- if (! plist)
- return CMD_WARNING;
+ /* Transfer the values from the orf_prefix */
+ prefix_list_entry_init(&temp) ;
- if (set)
+ temp.type = permit ? PREFIX_PERMIT : PREFIX_DENY ;
+ temp.prefix = orfp->p ;
+ temp.seq = orfp->seq ; /* NB: U32 and may be zero */
+ if (orfp->ge)
{
- pentry = prefix_list_entry_make (&orfp->p,
- (permit ? PREFIX_PERMIT : PREFIX_DENY),
- orfp->seq, orfp->le, orfp->ge, 0);
+ temp.flags |= PREFIX_GE ;
+ temp.ge = orfp->ge ;
+ }
+ if (orfp->le)
+ {
+ temp.flags |= PREFIX_LE ;
+ temp.le = orfp->le ;
+ }
- if (prefix_entry_dup_check (plist, pentry))
- {
- prefix_list_entry_free (pentry);
- return CMD_WARNING;
- }
+ /* Make sure ge & le are acceptable and set as required */
+ ret = prefix_list_entry_ge_le_check(&temp, afi) ;
+ if (ret != CMD_SUCCESS)
+ return ret ;
- prefix_list_entry_add (plist, pentry);
+ /* Now insert or delete */
+ if (set)
+ {
+ plist = prefix_list_get(&prefix_master_orf, name, afi);
+ return prefix_list_entry_insert(plist, &temp) ;
}
else
{
- pentry = prefix_list_entry_lookup (plist, &orfp->p,
- (permit ? PREFIX_PERMIT : PREFIX_DENY),
- orfp->seq, orfp->le, orfp->ge);
+ plist = prefix_list_seek(&prefix_master_orf, name) ;
+ if (plist == NULL)
+ return CMD_WARNING ;
- if (! pentry)
- return CMD_WARNING;
-
- prefix_list_entry_delete (plist, pentry, 1);
+ return prefix_list_entry_delete(plist, &temp) ;
}
-
- return CMD_SUCCESS;
}
void
@@ -2555,70 +2897,33 @@ prefix_bgp_orf_remove_all (char *name)
int
prefix_bgp_show_prefix_list (struct vty *vty, afi_t afi, char *name)
{
- struct prefix_list *plist;
- struct prefix_list_entry *pentry;
+ struct prefix_list *plist ;
plist = prefix_list_lookup (AFI_ORF_PREFIX, name);
if (! plist)
return 0;
- if (! vty)
- return plist->count;
-
- vty_out (vty, "ip%s prefix-list %s: %d entries%s",
- afi == AFI_IP ? "" : "v6",
- plist->name, plist->count, VTY_NEWLINE);
-
- for (pentry = plist->head; pentry; pentry = pentry->next)
+ if (vty)
{
- struct prefix *p = &pentry->prefix;
- char buf[BUFSIZ];
-
- vty_out (vty, " seq %d %s %s/%d", pentry->seq,
- prefix_list_type_str (pentry),
- inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
- p->prefixlen);
+ struct prefix_list_entry* pe ;
+ vector_index i ;
- if (pentry->ge)
- vty_out (vty, " ge %d", pentry->ge);
- if (pentry->le)
- vty_out (vty, " le %d", pentry->le);
+ vty_prefix_list_name_count_print(vty, plist, VTY_NEWLINE) ;
- vty_out (vty, "%s", VTY_NEWLINE);
+ for (VECTOR_ITEMS(&plist->list, pe, i))
+ {
+ vty_out_indent(vty, 3) ;
+ vty_prefix_list_value_print(vty, pe, VTY_NEWLINE, 1, 1) ;
+ }
}
- return plist->count;
+
+ return vector_end(&plist->list);
}
static void
prefix_list_reset_orf (void)
{
- struct prefix_list *plist;
- struct prefix_list *next;
- struct prefix_master *master;
-
- master = prefix_master_get (AFI_ORF_PREFIX);
- if (master == NULL)
- return;
-
- for (plist = master->num.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
- for (plist = master->str.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
-
- assert (master->num.head == NULL);
- assert (master->num.tail == NULL);
-
- assert (master->str.head == NULL);
- assert (master->str.tail == NULL);
-
- master->seqnum = 1;
- master->recent = NULL;
+ prefix_master_reset(&prefix_master_orf) ;
}
@@ -2639,38 +2944,14 @@ config_write_prefix_ipv4 (struct vty *vty)
static void
prefix_list_reset_ipv4 (void)
{
- struct prefix_list *plist;
- struct prefix_list *next;
- struct prefix_master *master;
-
- master = prefix_master_get (AFI_IP);
- if (master == NULL)
- return;
-
- for (plist = master->num.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
- for (plist = master->str.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
-
- assert (master->num.head == NULL);
- assert (master->num.tail == NULL);
-
- assert (master->str.head == NULL);
- assert (master->str.tail == NULL);
-
- master->seqnum = 1;
- master->recent = NULL;
+ prefix_master_reset(&prefix_master_ipv4) ;
}
static void
prefix_list_init_ipv4 (void)
{
+ prefix_master_init(&prefix_master_ipv4) ;
+
install_node (&prefix_node, config_write_prefix_ipv4);
install_element (CONFIG_NODE, &ip_prefix_list_cmd);
@@ -2734,10 +3015,10 @@ prefix_list_init_ipv4 (void)
/* Prefix-list node. */
static struct cmd_node prefix_ipv6_node =
{
- PREFIX_IPV6_NODE,
- "", /* Prefix list has no interface. */
- 1
-};
+ PREFIX_IPV6_NODE,
+ "", /* Prefix list has no interface. */
+ 1
+ };
static int
config_write_prefix_ipv6 (struct vty *vty)
@@ -2748,38 +3029,16 @@ config_write_prefix_ipv6 (struct vty *vty)
static void
prefix_list_reset_ipv6 (void)
{
- struct prefix_list *plist;
- struct prefix_list *next;
- struct prefix_master *master;
-
- master = prefix_master_get (AFI_IP6);
- if (master == NULL)
- return;
-
- for (plist = master->num.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
- for (plist = master->str.head; plist; plist = next)
- {
- next = plist->next;
- prefix_list_delete (plist);
- }
-
- assert (master->num.head == NULL);
- assert (master->num.tail == NULL);
-
- assert (master->str.head == NULL);
- assert (master->str.tail == NULL);
-
- master->seqnum = 1;
- master->recent = NULL;
-}
+#ifdef HAVE_IPV6
+ prefix_master_reset(&prefix_master_ipv6) ;
+#endif
+} ;
static void
prefix_list_init_ipv6 (void)
{
+ prefix_master_init(&prefix_master_ipv6) ;
+
install_node (&prefix_ipv6_node, config_write_prefix_ipv6);
install_element (CONFIG_NODE, &ipv6_prefix_list_cmd);
@@ -2847,6 +3106,7 @@ prefix_list_init ()
#ifdef HAVE_IPV6
prefix_list_init_ipv6 ();
#endif /* HAVE_IPV6 */
+ prefix_master_init(&prefix_master_orf) ;
}
void
diff --git a/lib/plist.h b/lib/plist.h
index fb3168a6..97dab78e 100644
--- a/lib/plist.h
+++ b/lib/plist.h
@@ -23,38 +23,21 @@
#ifndef _QUAGGA_PLIST_H
#define _QUAGGA_PLIST_H
+#include "prefix.h"
+#include "symtab.h"
+#include "vector.h"
+
#define AFI_ORF_PREFIX 65535
-enum prefix_list_type
+enum prefix_list_type
{
PREFIX_DENY,
PREFIX_PERMIT,
};
-enum prefix_name_type
-{
- PREFIX_TYPE_STRING,
- PREFIX_TYPE_NUMBER
-};
-
-struct prefix_list
-{
- char *name;
- char *desc;
-
- struct prefix_master *master;
-
- enum prefix_name_type type;
+struct prefix_list ;
- int count;
- int rangecount;
-
- struct prefix_list_entry *head;
- struct prefix_list_entry *tail;
-
- struct prefix_list *next;
- struct prefix_list *prev;
-};
+typedef struct symbol_ref* prefix_list_ref ;
struct orf_prefix
{
@@ -73,11 +56,23 @@ extern void prefix_list_delete_hook (void (*func) (struct prefix_list *));
extern struct prefix_list *prefix_list_lookup (afi_t, const char *);
extern enum prefix_list_type prefix_list_apply (struct prefix_list *, void *);
+extern const char* prefix_list_get_name(struct prefix_list* plist) ;
+
extern struct stream * prefix_bgp_orf_entry (struct stream *,
- struct prefix_list *,
+ prefix_list_ref ref,
u_char, u_char, u_char);
extern int prefix_bgp_orf_set (char *, afi_t, struct orf_prefix *, int, int);
extern void prefix_bgp_orf_remove_all (char *);
extern int prefix_bgp_show_prefix_list (struct vty *, afi_t, char *);
+extern prefix_list_ref prefix_list_set_ref(prefix_list_ref* p_ref, afi_t afi,
+ const char* name) ;
+extern prefix_list_ref prefix_list_copy_ref(prefix_list_ref* p_dst,
+ prefix_list_ref src) ;
+extern prefix_list_ref prefix_list_unset_ref(prefix_list_ref* p_ref) ;
+
+extern const char* prefix_list_ref_name(prefix_list_ref ref) ;
+extern void* prefix_list_ref_ident(prefix_list_ref ref) ;
+extern struct prefix_list* prefix_list_ref_plist(prefix_list_ref ref) ;
+
#endif /* _QUAGGA_PLIST_H */
diff --git a/lib/prefix.c b/lib/prefix.c
index 2afaa09c..6399788c 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#include <zebra.h>
@@ -27,7 +27,7 @@
#include "sockunion.h"
#include "memory.h"
#include "log.h"
-
+
/* Maskbit. */
static u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
0xf8, 0xfc, 0xfe, 0xff};
@@ -85,7 +85,7 @@ prefix_match (const struct prefix *n, const struct prefix *p)
if (shift)
if (maskbit[shift] & (np[offset] ^ pp[offset]))
return 0;
-
+
while (offset--)
if (np[offset] != pp[offset])
return 0;
@@ -118,7 +118,7 @@ prefix_copy (struct prefix *dest, const struct prefix *src)
}
}
-/*
+/*
* Return 1 if the address/netmask contained in the prefix structure
* is the same, and else return 0. For this routine, 'same' requires
* that not only the prefix length and the network part be the same,
@@ -214,7 +214,16 @@ prefix_ipv4_free (struct prefix_ipv4 *p)
prefix_free((struct prefix *)p);
}
-/* When string format is invalid return 0. */
+/* When string format is valid return 1 otherwise return 0.
+ *
+ * Some callers of this function treat non-0 as OK and 0 as invalid (which is
+ * what inet_aton() is defined to do).
+ *
+ * Some callers treat > 0 as OK and <= 0 as invalid (which is similar to what
+ * inet_pton() is defined to do).
+ *
+ * The actual returns are consistent with both usages.
+ */
int
str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
{
@@ -227,7 +236,7 @@ str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
pnt = strchr (str, '/');
/* String doesn't contail slash. */
- if (pnt == NULL)
+ if (pnt == NULL)
{
/* Convert string to prefix. */
ret = inet_aton (str, &p->prefix);
@@ -238,7 +247,7 @@ str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
p->family = AF_INET;
p->prefixlen = IPV4_MAX_BITLEN;
- return ret;
+ return 1 ;
}
else
{
@@ -257,7 +266,7 @@ str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
p->prefixlen = plen;
}
- return ret;
+ return 1 ;
}
/* Convert masklen into IP address's netmask. */
@@ -273,7 +282,7 @@ masklen2ip (int masklen, struct in_addr *netmask)
offset = masklen / 8;
bit = masklen % 8;
-
+
while (offset--)
*pnt++ = 0xff;
@@ -299,7 +308,7 @@ ip_masklen (struct in_addr netmask)
{
len+= 8;
pnt++;
- }
+ }
if (pnt < end)
{
@@ -342,7 +351,7 @@ prefix_ipv4_any (const struct prefix_ipv4 *p)
{
return (p->prefix.s_addr == 0 && p->prefixlen == 0);
}
-
+
#ifdef HAVE_IPV6
/* Allocate a new ip version 6 route */
@@ -365,7 +374,26 @@ prefix_ipv6_free (struct prefix_ipv6 *p)
prefix_free((struct prefix *)p);
}
-/* If given string is valid return pin6 else return NULL */
+/* If given string is valid IPv6 address or prefix return 1 else return 0
+ *
+ * Of inet_pton() POSIX 1003.1, 2004 says:
+ *
+ * The inet_pton() function shall return 1 if the conversion succeeds, with
+ * the address pointed to by dst in network byte order. It shall return 0 if
+ * the input is not either a valid IPv4 dotted-decimal string or a valid IPv6
+ * address string (if IPv6 supported), or return -1 with errno set to
+ * [EAFNOSUPPORT] if the af argument is unknown.
+ *
+ * Any error returned is reported as an invalid address or prefix. So best not
+ * to call this if IPv6 is not supported.
+ *
+ * Some callers treat > 0 as OK and <= 0 as invalid (which is consistent with
+ * what inet_pton() is defined to do).
+ *
+ * Some callers of this function treat non-0 as OK and 0 as invalid.
+ *
+ * The actual returns are consistent with both usages.
+ */
int
str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
{
@@ -376,14 +404,14 @@ str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
pnt = strchr (str, '/');
/* If string doesn't contain `/' treat it as host route. */
- if (pnt == NULL)
+ if (pnt == NULL)
{
ret = inet_pton (AF_INET6, str, &p->prefix);
- if (ret == 0)
+ if (ret <= 0)
return 0;
p->prefixlen = IPV6_MAX_BITLEN;
}
- else
+ else
{
int plen;
@@ -392,7 +420,7 @@ str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
*(cp + (pnt - str)) = '\0';
ret = inet_pton (AF_INET6, cp, &p->prefix);
free (cp);
- if (ret == 0)
+ if (ret <= 0)
return 0;
plen = (u_char) atoi (++pnt);
if (plen > 128)
@@ -401,7 +429,7 @@ str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
}
p->family = AF_INET6;
- return ret;
+ return 1 ;
}
/* Convert struct in6_addr netmask into integer.
@@ -412,19 +440,19 @@ ip6_masklen (struct in6_addr netmask)
int len = 0;
unsigned char val;
unsigned char *pnt;
-
+
pnt = (unsigned char *) & netmask;
- while ((*pnt == 0xff) && len < 128)
+ while ((*pnt == 0xff) && len < 128)
{
len += 8;
pnt++;
- }
-
- if (len < 128)
+ }
+
+ if (len < 128)
{
val = *pnt;
- while (val)
+ while (val)
{
len++;
val <<= 1;
@@ -572,7 +600,7 @@ sockunion2hostprefix (const union sockunion *su)
int
prefix_blen (const struct prefix *p)
{
- switch (p->family)
+ switch (p->family)
{
case AF_INET:
return IPV4_MAX_BYTELEN;
@@ -600,7 +628,7 @@ str2prefix (const char *str, struct prefix *p)
#ifdef HAVE_IPV6
/* Next we try to convert string to struct prefix_ipv6. */
ret = str2prefix_ipv6 (str, (struct prefix_ipv6 *) p);
- if (ret)
+ if (ret <= 0)
return ret;
#endif /* HAVE_IPV6 */
@@ -650,22 +678,22 @@ void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
{
u_int32_t destination;
-
+
destination = ntohl (p->prefix.s_addr);
-
+
if (p->prefixlen == IPV4_MAX_PREFIXLEN);
/* do nothing for host routes */
- else if (IN_CLASSC (destination))
+ else if (IN_CLASSC (destination))
{
p->prefixlen=24;
apply_mask_ipv4(p);
}
- else if (IN_CLASSB(destination))
+ else if (IN_CLASSB(destination))
{
p->prefixlen=16;
apply_mask_ipv4(p);
}
- else
+ else
{
p->prefixlen=8;
apply_mask_ipv4(p);
@@ -694,7 +722,7 @@ ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
(hostaddr ^ ~mask.s_addr);
}
-/* Utility function to convert ipv4 netmask to prefixes
+/* Utility function to convert ipv4 netmask to prefixes
ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
int
@@ -719,7 +747,7 @@ netmask_str2prefix_str (const char *net_str, const char *mask_str,
prefixlen = ip_masklen (mask);
}
- else
+ else
{
destination = ntohl (network.s_addr);
diff --git a/lib/privs.c b/lib/privs.c
index 69606f57..68757340 100644
--- a/lib/privs.c
+++ b/lib/privs.c
@@ -25,6 +25,10 @@
#include "log.h"
#include "privs.h"
#include "memory.h"
+#include "qpthreads.h"
+
+/* Needs to be qpthread safe */
+static qpt_mutex_t* mx = NULL;
#ifdef HAVE_CAPABILITIES
/* sort out some generic internal types for:
@@ -61,6 +65,7 @@ typedef priv_set_t *pstorage_t;
* zprivs_terminate is called and the NULL handler is installed.
*/
static zebra_privs_current_t zprivs_null_state = ZPRIVS_RAISED;
+static int raise_count = 0; /* keep raised until all pthreads have lowered */
/* internal privileges state */
static struct _zprivs_t
@@ -187,25 +192,42 @@ int
zprivs_change_caps (zebra_privs_ops_t op)
{
cap_flag_value_t cflag;
-
+ int result = 0;
+ int change = 0;
+
+ qpt_mutex_lock(mx);
+
/* should be no possibility of being called without valid caps */
assert (zprivs_state.syscaps_p && zprivs_state.caps);
if (! (zprivs_state.syscaps_p && zprivs_state.caps))
- exit (1);
-
+ {
+ qpt_mutex_unlock(mx);
+ exit (1);
+ }
+
if (op == ZPRIVS_RAISE)
- cflag = CAP_SET;
+ {
+ cflag = CAP_SET;
+ change = (raise_count++ == 0);
+ }
else if (op == ZPRIVS_LOWER)
- cflag = CAP_CLEAR;
+ {
+ cflag = CAP_CLEAR;
+ change = (--raise_count == 0);
+ }
else
- return -1;
+ {
+ result = -1;
+ }
- if ( !cap_set_flag (zprivs_state.caps, CAP_EFFECTIVE,
+ if ( change && !cap_set_flag (zprivs_state.caps, CAP_EFFECTIVE,
zprivs_state.syscaps_p->num,
zprivs_state.syscaps_p->caps,
cflag))
- return cap_set_proc (zprivs_state.caps);
- return -1;
+ result = cap_set_proc (zprivs_state.caps);
+
+ qpt_mutex_unlock(mx);
+ return result;
}
zebra_privs_current_t
@@ -213,11 +235,17 @@ zprivs_state_caps (void)
{
int i;
cap_flag_value_t val;
+ zebra_privs_current_t result = ZPRIVS_LOWERED;
+
+ qpt_mutex_lock(mx);
/* should be no possibility of being called without valid caps */
assert (zprivs_state.syscaps_p && zprivs_state.caps);
if (! (zprivs_state.syscaps_p && zprivs_state.caps))
- exit (1);
+ {
+ qpt_mutex_unlock(mx);
+ exit (1);
+ }
for (i=0; i < zprivs_state.syscaps_p->num; i++)
{
@@ -226,12 +254,18 @@ zprivs_state_caps (void)
{
zlog_warn ("zprivs_state_caps: could not cap_get_flag, %s",
safe_strerror (errno) );
- return ZPRIVS_UNKNOWN;
+ result = ZPRIVS_UNKNOWN;
+ break;
}
if (val == CAP_SET)
- return ZPRIVS_RAISED;
+ {
+ result = ZPRIVS_RAISED;
+ break;
+ }
}
- return ZPRIVS_LOWERED;
+
+ qpt_mutex_unlock(mx);
+ return result;
}
static void
@@ -376,12 +410,16 @@ zcaps2sys (zebra_capabilities_t *zcaps, int num)
int
zprivs_change_caps (zebra_privs_ops_t op)
{
+ int result = 0;
+ qpt_mutex_lock(mx);
+
/* should be no possibility of being called without valid caps */
assert (zprivs_state.syscaps_p);
if (!zprivs_state.syscaps_p)
{
fprintf (stderr, "%s: Eek, missing caps!", __func__);
+ qpt_mutex_unlock(mx);
exit (1);
}
@@ -389,49 +427,66 @@ zprivs_change_caps (zebra_privs_ops_t op)
* to lower: just clear the working effective set
*/
if (op == ZPRIVS_RAISE)
- priv_copyset (zprivs_state.syscaps_p, zprivs_state.caps);
+ {
+ if (raise_count++ == 0)
+ {
+ priv_copyset (zprivs_state.syscaps_p, zprivs_state.caps);
+ if (setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps) != 0)
+ result = -1;
+ }
+ }
else if (op == ZPRIVS_LOWER)
- priv_emptyset (zprivs_state.caps);
+ {
+ if (--raise_count == 0)
+ {
+ priv_emptyset (zprivs_state.caps);
+ if (setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps) != 0)
+ result = -1;
+ }
+ }
else
- return -1;
+ result = -1;
- if (setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps) != 0)
- return -1;
+ qpt_mutex_unlock(mx);
- return 0;
+ return result;
}
/* Retrieve current privilege state, is it RAISED or LOWERED? */
zebra_privs_current_t
zprivs_state_caps (void)
{
- zebra_privs_current_t result;
+ zebra_privs_current_t result = ZPRIVS_UNKNOWN;
pset_t *effective;
+ qpt_mutex_lock(mx);
+
if ( (effective = priv_allocset()) == NULL)
{
fprintf (stderr, "%s: failed to get priv_allocset! %s\n", __func__,
safe_strerror (errno));
- return ZPRIVS_UNKNOWN;
- }
-
- if (getppriv (PRIV_EFFECTIVE, effective))
- {
- fprintf (stderr, "%s: failed to get state! %s\n", __func__,
- safe_strerror (errno));
- result = ZPRIVS_UNKNOWN;
}
else
{
- if (priv_isemptyset (effective) == B_TRUE)
- result = ZPRIVS_LOWERED;
+
+ if (getppriv (PRIV_EFFECTIVE, effective))
+ {
+ fprintf (stderr, "%s: failed to get state! %s\n", __func__,
+ safe_strerror (errno));
+ }
else
- result = ZPRIVS_RAISED;
+ {
+ if (priv_isemptyset (effective) == B_TRUE)
+ result = ZPRIVS_LOWERED;
+ else
+ result = ZPRIVS_RAISED;
+ }
+
+ if (effective)
+ priv_freeset (effective);
}
- if (effective)
- priv_freeset (effective);
-
+ qpt_mutex_unlock(mx);
return result;
}
@@ -560,19 +615,42 @@ zprivs_caps_terminate (void)
int
zprivs_change_uid (zebra_privs_ops_t op)
{
+ int result = 0;
+
+ qpt_mutex_lock(mx);
if (op == ZPRIVS_RAISE)
- return seteuid (zprivs_state.zsuid);
+ {
+ if (raise_count++ == 0)
+ {
+ result = seteuid (zprivs_state.zsuid);
+ }
+ }
else if (op == ZPRIVS_LOWER)
- return seteuid (zprivs_state.zuid);
+ {
+ if (--raise_count == 0)
+ {
+ result = seteuid (zprivs_state.zuid);
+ }
+ }
else
- return -1;
+ {
+ result = -1;
+ }
+
+ qpt_mutex_unlock(mx);
+ return result;
}
zebra_privs_current_t
zprivs_state_uid (void)
{
- return ( (zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED : ZPRIVS_RAISED);
+ zebra_privs_current_t result;
+
+ qpt_mutex_lock(mx);
+ result = ( (zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED : ZPRIVS_RAISED);
+ qpt_mutex_unlock(mx);
+ return result;
}
int
@@ -584,7 +662,25 @@ zprivs_change_null (zebra_privs_ops_t op)
zebra_privs_current_t
zprivs_state_null (void)
{
- return zprivs_null_state;
+ int result;
+
+ qpt_mutex_lock(mx);
+ result = zprivs_null_state;
+ qpt_mutex_unlock(mx);
+ return result;
+}
+
+void
+zprivs_init_r(struct zebra_privs_t *zprivs)
+{
+ mx = qpt_mutex_init(mx, qpt_mutex_quagga);
+ zprivs_init(zprivs);
+}
+
+void
+zprivs_destroy_r(void)
+{
+ mx = qpt_mutex_destroy(mx, 1);
}
void
@@ -599,12 +695,15 @@ zprivs_init(struct zebra_privs_t *zprivs)
exit (1);
}
+ qpt_mutex_lock(mx);
+
/* NULL privs */
if (! (zprivs->user || zprivs->group
|| zprivs->cap_num_p || zprivs->cap_num_i) )
{
zprivs->change = zprivs_change_null;
zprivs->current_state = zprivs_state_null;
+ qpt_mutex_unlock(mx);
return;
}
@@ -619,6 +718,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
/* cant use log.h here as it depends on vty */
fprintf (stderr, "privs_init: could not lookup user %s\n",
zprivs->user);
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -635,6 +735,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
{
fprintf (stderr, "privs_init: could not setgroups, %s\n",
safe_strerror (errno) );
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -642,6 +743,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
{
fprintf (stderr, "privs_init: could not lookup vty group %s\n",
zprivs->vty_group);
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -656,6 +758,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
{
fprintf (stderr, "privs_init: could not lookup group %s\n",
zprivs->group);
+ qpt_mutex_unlock(mx);
exit (1);
}
/* change group now, forever. uid we do later */
@@ -663,6 +766,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
{
fprintf (stderr, "zprivs_init: could not setregid, %s\n",
safe_strerror (errno) );
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -671,7 +775,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
zprivs_caps_init (zprivs);
#else /* !HAVE_CAPABILITIES */
/* we dont have caps. we'll need to maintain rid and saved uid
- * and change euid back to saved uid (who we presume has all neccessary
+ * and change euid back to saved uid (who we presume has all necessary
* privileges) whenever we are asked to raise our privileges.
*
* This is not worth that much security wise, but all we can do.
@@ -683,6 +787,7 @@ zprivs_init(struct zebra_privs_t *zprivs)
{
fprintf (stderr, "privs_init (uid): could not setreuid, %s\n",
safe_strerror (errno));
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -690,6 +795,8 @@ zprivs_init(struct zebra_privs_t *zprivs)
zprivs->change = zprivs_change_uid;
zprivs->current_state = zprivs_state_uid;
#endif /* HAVE_CAPABILITIES */
+
+ qpt_mutex_unlock(mx);
}
void
@@ -701,6 +808,8 @@ zprivs_terminate (struct zebra_privs_t *zprivs)
exit (0);
}
+ qpt_mutex_lock(mx);
+
#ifdef HAVE_CAPABILITIES
zprivs_caps_terminate();
#else /* !HAVE_CAPABILITIES */
@@ -710,6 +819,7 @@ zprivs_terminate (struct zebra_privs_t *zprivs)
{
fprintf (stderr, "privs_terminate: could not setreuid, %s",
safe_strerror (errno) );
+ qpt_mutex_unlock(mx);
exit (1);
}
}
@@ -718,20 +828,25 @@ zprivs_terminate (struct zebra_privs_t *zprivs)
zprivs->change = zprivs_change_null;
zprivs->current_state = zprivs_state_null;
zprivs_null_state = ZPRIVS_LOWERED;
+ raise_count = 0;
+
+ qpt_mutex_unlock(mx);
return;
}
void
zprivs_get_ids(struct zprivs_ids_t *ids)
{
+ qpt_mutex_lock(mx);
- ids->uid_priv = getuid();
- (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
+ ids->uid_priv = getuid();
+ (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
: (ids->uid_normal = -1);
- (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
+ (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
: (ids->gid_normal = -1);
- (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
+ (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
: (ids->gid_vty = -1);
+ qpt_mutex_unlock(mx);
return;
}
diff --git a/lib/privs.h b/lib/privs.h
index 46d614e0..18d35d8d 100644
--- a/lib/privs.h
+++ b/lib/privs.h
@@ -81,9 +81,13 @@ struct zprivs_ids_t
};
/* initialise zebra privileges */
+extern void zprivs_init_r (struct zebra_privs_t *zprivs);
extern void zprivs_init (struct zebra_privs_t *zprivs);
+extern void zprivs_destroy_r (void);
+
/* drop all and terminate privileges */
extern void zprivs_terminate (struct zebra_privs_t *);
+
/* query for runtime uid's and gid's, eg vty needs this */
extern void zprivs_get_ids(struct zprivs_ids_t *);
diff --git a/lib/qpnexus.c b/lib/qpnexus.c
new file mode 100644
index 00000000..01985d29
--- /dev/null
+++ b/lib/qpnexus.c
@@ -0,0 +1,120 @@
+/* Quagga Pthreads support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/* This MUST come first... otherwise we don't get __USE_UNIX98, which is */
+/* essential if glibc is to allow pthread_mutexattr_settype() to be used. */
+#include "config.h"
+
+#include <signal.h>
+#include <string.h>
+
+#include "qpnexus.h"
+#include "memory.h"
+
+/* prototypes */
+static void* qpn_start(void* arg);
+
+/*==============================================================================
+ * Quagga Nexus Interface -- qpt_xxxx
+ *
+
+ */
+
+/* Initialise a nexus -- allocating it if required.
+ *
+ * Returns the qtn_nexus.
+ */
+qpn_nexus
+qpn_init_new(qpn_nexus qpn)
+{
+ if (qpn == NULL)
+ qpn = XCALLOC(MTYPE_QPN_NEXUS, sizeof(struct qpn_nexus)) ;
+ else
+ memset(qpn, 0, sizeof(struct qpn_nexus)) ;
+
+ qpn->selection = qps_selection_init_new(qpn->selection);
+ qpn->pile = qtimer_pile_init_new(qpn->pile);
+
+ /* TODO mqueue initialisation */
+
+ return qpn;
+}
+
+void
+qpn_free(qpn_nexus qpn)
+{
+ /* timers and the pile */
+ qtimer qtr;
+ while ((qtr = qtimer_pile_ream(qpn->pile, 1)))
+ {
+ qtimer_free(qtr);
+ }
+
+ /* TODO: free qtn->selection */
+
+ /* TODO: free qtn->queue */
+
+ XFREE(MTYPE_QPN_NEXUS, qpn) ;
+}
+
+/* Create and execute the qpthread */
+void
+qpn_exec(qpn_nexus qpn)
+{
+ qpn->thread_id = qpt_thread_create(qpn_start, qpn, NULL) ;
+}
+
+static void*
+qpn_start(void* arg)
+{
+ qpn_nexus qpn = arg;
+ int actions;
+
+ while (!qpn->terminate)
+ {
+ qtime_mono_t now = qt_get_monotonic();
+
+ /* process timers */
+ while (qtimer_pile_dispatch_next(qpn->pile, now))
+ {
+ }
+
+ /* block for some input, output or timeout */
+ actions = qps_pselect( qpn->selection,
+ qtimer_pile_top_time(qpn->pile, now + QTIME(MAX_PSELECT_TIMOUT)) );
+
+ /* process I/O actions */
+ while (actions)
+ {
+ actions = qps_dispatch_next(qpn->selection) ;
+ }
+
+ /* TODO process message queue */
+ }
+
+ qpn_free(qpn);
+
+ return NULL;
+}
+
+
+
+
diff --git a/lib/qpnexus.h b/lib/qpnexus.h
new file mode 100644
index 00000000..ce546edd
--- /dev/null
+++ b/lib/qpnexus.h
@@ -0,0 +1,86 @@
+/* Quagga Pthreads support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_QPNEXUS_H
+#define _ZEBRA_QPNEXUS_H
+
+#include <stdint.h>
+#include <time.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "zassert.h"
+#include "qpthreads.h"
+#include "qtimers.h"
+#include "mqueue.h"
+#include "qpselect.h"
+
+#ifndef Inline
+#define Inline static inline
+#endif
+
+/*==============================================================================
+ * Quagga Nexus Interface -- qpn_xxxx
+ *
+ * Object to hold, a qpthread, a qps_selection, a qtimer_pile, a mqueue_queue
+ * together with the thread routine to poll and dispatch their respective
+ * action routines.
+ *
+ */
+
+/* maximum time in seconds to sit in a pselect */
+#define MAX_PSELECT_TIMOUT 10
+
+/*==============================================================================
+ * Data Structures.
+ */
+
+typedef struct qpn_nexus* qpn_nexus ;
+
+struct qpn_nexus
+{
+ /* set true to terminate the thread (eventually) */
+ int terminate;
+
+ /* thread ID */
+ qpt_thread_t thread_id;
+
+ /* pselect handler */
+ qps_selection selection;
+
+ /* timer pile */
+ qtimer_pile pile;
+
+ /* message queue */
+ mqueue_queue queue;
+
+};
+
+/*==============================================================================
+ * Functions
+ */
+
+extern qpn_nexus qpn_init_new(qpn_nexus qtn);
+extern void qpn_exec(qpn_nexus qtn);
+void qpn_free(qpn_nexus qpn);
+
+#endif /* _ZEBRA_QPNEXUS_H */
diff --git a/lib/qpselect.c b/lib/qpselect.c
new file mode 100644
index 00000000..cf1df985
--- /dev/null
+++ b/lib/qpselect.c
@@ -0,0 +1,1321 @@
+/* Quagga pselect support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <signal.h>
+#include <string.h>
+
+#include "zassert.h"
+#include "qpselect.h"
+#include "qpthreads.h"
+#include "qtime.h"
+#include "memory.h"
+#include "vector.h"
+
+/*==============================================================================
+ * Quagga pselect -- qps_xxxx
+ *
+ * Here and in qpselect.h is a data structure for managing multiple file
+ * descriptors and running pselect to wait for I/O activity and to multiplex
+ * between the file descriptors.
+ *
+ * The qps_selection structure manages a collection of file descriptors which
+ * are to be waited on together in a pselect statement.
+ *
+ * NB: it is ASSUMED that a qps_selection will be private to the thread in
+ * which it is created and used.
+ *
+ * There is NO mutex handling here.
+ *
+ * This supports pselect, so supports:
+ *
+ * * waiting for file descriptors, which may each be expecting any combination
+ * of error/read/write events.
+ *
+ * Files may be added or removed from the selection. Files in the selection
+ * may then be enabled/disabled for any combination of error/read/write
+ * "mode" events.
+ *
+ * * a timeout *time*
+ *
+ * This is a qtime monotonic time at which to time out. (This is unlike
+ * pselect() itself, which takes a timeout interval.)
+ *
+ * Infinite timeouts are not supported.
+ *
+ * * an optional signal number and sigmask
+ *
+ * So that a signal may be used to interrupt a waiting pselect.
+ *
+ * For this to work there must be a signal which is generally masked, and
+ * is unmasked for the duration of the pselect.
+ *
+ * When a pselect returns there may be a number of files with events pending.
+ * The qps_dispatch_next() calls the action routine for the next event to be
+ * dealt with. Events are dispatched in the order: error, read and write, and
+ * then in file descriptor order. (So all error events in fd order, then all
+ * read events, and so on.)
+ *
+ * Note that at no time are any modes automatically disabled. So the system is
+ * level triggered. So, for example, a read event that is not dealt with will
+ * be triggered again on the next pselect -- unless the read mode is explicitly
+ * disabled for the file.
+ *
+ * Action Functions
+ * ----------------
+ *
+ * There is a separate action function for each mode. Each file has its own
+ * set of action functions -- so these may be used to implement a form of
+ * state machine for the file.
+ *
+ * When the action function is called it is passed the qps_file structure and
+ * the file_info pointer from that structure.
+ *
+ * During an action function modes may be enabled/disabled, actions changed,
+ * the file removed from the selection... there are no restrictions.
+ */
+
+static int qps_super_set_map_made = 0 ;
+
+static void qps_make_super_set_map(void) ;
+
+/*==============================================================================
+ * qps_selection handling
+ */
+
+/* Forward references */
+static qps_file qps_file_lookup_fd(qps_selection qps, int fd, qps_file insert) ;
+static void qps_file_remove(qps_selection qps, qps_file qf) ;
+static void qps_super_set_zero(fd_super_set* p_set, int n) ;
+static int qps_next_fd_pending(fd_super_set* pending, int fd, int fd_last) ;
+static void qps_selection_validate(qps_selection qps) ;
+
+/* See qps_make_super_set_map() and qps_pselect() below. */
+static short fd_byte_count[FD_SETSIZE] ; /* number of bytes for fds 0..fd */
+
+/* Initialise a selection -- allocating it if required.
+ *
+ * Returns the qps_selection.
+ */
+qps_selection
+qps_selection_init_new(qps_selection qps)
+{
+ if (!qps_super_set_map_made)
+ qps_make_super_set_map() ; /* map the fd_super_set */
+
+ if (qps == NULL)
+ qps = XCALLOC(MTYPE_QPS_SELECTION, sizeof(struct qps_selection)) ;
+ else
+ memset(qps, 0, sizeof(struct qps_selection)) ;
+
+ /* Zeroising initialises:
+ *
+ * fd_count -- no fd's yet
+ * fd_direct -- not direct lookup
+ *
+ * files -- empty vector
+ *
+ * fd_last -- unset
+ * enabled_count -- no fd's enabled in any mode
+ * enabled -- empty bit vectors
+ *
+ * tried_fd_last -- nothing tried yet
+ * tried_count -- nothing tried yet
+ * results -- empty bit vectors
+ *
+ * pend_count -- no results to dispatch
+ * pend_mnum -- unset
+ * pend_fd -- unset
+ *
+ * signum -- no signal to be enabled
+ * sigmask -- unset
+ *
+ * So nothing else to do -- see also qps_selection_re_init(), below.
+ */
+
+ return qps ;
+} ;
+
+/* Re-initialise a selection.
+ */
+static void
+qps_selection_re_init(qps_selection qps)
+{
+ memset(qps, 0, sizeof(struct qps_selection)) ;
+} ;
+
+/* Add given file to the selection, setting its fd and pointer to further
+ * file information. All modes are disabled.
+ *
+ * This initialises most of the qps_file structure, but not the actions.
+ *
+ * Adding a file using the same fd as an existing file is a FATAL error.
+ *
+ * Adding a file which is already a member a selection is a FATAL error.
+ */
+void
+qps_add_file(qps_selection qps, qps_file qf, int fd, void* file_info)
+{
+ passert(qf->selection == NULL) ;
+
+ qf->selection = qps ;
+
+ qf->file_info = file_info ;
+ qf->fd = fd ;
+
+ qf->enabled_bits = 0 ;
+
+ qps_file_lookup_fd(qps, fd, qf) ; /* Add. */
+} ;
+
+/* Remove given file from its selection, if any.
+ *
+ * It is the callers responsibility to ensure that the file is in a suitable
+ * state to be removed from the selection.
+ *
+ * When the file is removed it is disabled in all modes.
+ */
+void
+qps_remove_file(qps_file qf)
+{
+ if (qf->selection != NULL)
+ qps_file_remove(qf->selection, qf) ;
+} ;
+
+/* Ream (another) file out of the selection.
+ *
+ * If selection is empty, release the qps_selection structure, if required.
+ *
+ * See: #define qps_selection_ream_free(qps)
+ * #define qps_selection_ream_keep(qps)
+ *
+ * Useful for emptying out and discarding a selection:
+ *
+ * while ((qf = qps_selection_ream_free(qps)))
+ * ... do what's required to release the qps_file
+ *
+ * The file is removed from the selection before being returned.
+ *
+ * Returns NULL when selection is empty (and has been released, if required).
+ *
+ * If the selection is not released, it may be reused without reinitialisation.
+ *
+ * NB: once reaming has started, the selection MUST NOT be used for anything,
+ * and the process MUST be run to completion.
+ */
+qps_file
+qps_selection_ream(qps_selection qps, int free_structure)
+{
+ qps_file qf ;
+
+ qf = vector_ream_keep(&qps->files) ;
+ if (qf != NULL)
+ qps_file_remove(qps, qf) ;
+ else
+ {
+ passert(qps->fd_count == 0) ;
+
+ if (free_structure)
+ XFREE(MTYPE_QPS_SELECTION, qps) ;
+ else
+ qps_selection_re_init(qps) ;
+ } ;
+
+ return qf ;
+} ;
+
+/* Set the signal mask for the selection.
+ *
+ * This supports the unmasking of a single signal for the duration of the
+ * pselect operation.
+ *
+ * It is assumed that the set of signals generally masked by a thread is
+ * essentially static. So this function is passed that set. (So the sigmask
+ * argument must have the signum signal masked.)
+ *
+ * If the set of signals masked by the thread changes, then this function
+ * should be called again.
+ *
+ * Setting a signum == 0 turns OFF the use of the sigmask.
+ */
+void
+qps_set_signal(qps_selection qps, int signum, sigset_t sigmask)
+{
+ qps->signum = signum ;
+
+ if (signum != 0)
+ {
+ assert(sigismember(&sigmask, signum)) ;
+ sigdelset(&sigmask, signum) ;
+ qps->sigmask = sigmask ;
+ } ;
+} ;
+
+/* Execute a pselect for the given selection -- subject to the given timeout
+ * *time*.
+ *
+ * The time-out time is an "absolute" time, as measured by qt_get_monotonic().
+ *
+ * A timeout time <= the current qt_get_monotonic() is treated as a zero
+ * timeout period, and will return immediately from the pselect.
+ *
+ * There is no support for an infinite timeout.
+ *
+ * Returns: -1 => EINTR occurred -- ie a signal has gone off
+ * 0 => hit timeout -- no files are ready
+ * > 0 => there are this many files ready in one or more modes
+ *
+ * All other errors are FATAL.
+ *
+ * The qps_dispatch_next() processes the returns from pselect().
+ */
+int
+qps_pselect(qps_selection qps, qtime_mono_t timeout)
+{
+ struct timespec ts ;
+ qps_mnum_t mnum ;
+ fd_set* p_fds[qps_mnum_count] ;
+ int n ;
+
+ /* TODO: put this under a debug skip */
+ qps_selection_validate(qps) ;
+
+ /* If there is stuff still pending, tidy up by zeroising the result */
+ /* vectors. This is to make sure that when bits are copied from */
+ /* the enabled vectors, there are none from a previous run of pselect */
+ /* left hanging about. (pselect SHOULD ignore everything above the */
+ /* given count of fds -- but it does no harm to be tidy, and should */
+ /* not have to do this often.) */
+ if (qps->pend_count != 0)
+ qps_super_set_zero(qps->results, qps_mnum_count) ;
+
+ /* Prepare the argument/result bitmaps */
+ /* Capture pend_mnum and tried_count[] */
+
+ n = fd_byte_count[qps->fd_last] ; /* copy up to last sig. byte */
+
+ qps->pend_mnum = qps_mnum_count ;
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ if ((qps->tried_count[mnum] = qps->enabled_count[mnum]) != 0)
+ {
+ memcpy(&(qps->results[mnum].bytes), &(qps->enabled[mnum].bytes), n) ;
+ p_fds[mnum] = &(qps->results[mnum].fdset) ;
+ if (mnum < qps->pend_mnum)
+ qps->pend_mnum = mnum ;
+ }
+ else
+ p_fds[mnum] = NULL ;
+
+ /* Capture tried_fd_last and set initial pend_fd. */
+ qps->tried_fd_last = qps->fd_last ;
+ qps->pend_fd = 0 ;
+
+ /* Convert timeout time to interval for pselect() */
+ timeout -= qt_get_monotonic() ;
+ if (timeout < 0)
+ timeout = 0 ;
+
+ /* Finally ready for the main event */
+ n = pselect(qps->fd_last + 1, p_fds[qps_read_mnum],
+ p_fds[qps_write_mnum],
+ p_fds[qps_error_mnum],
+ qtime2timespec(&ts, timeout),
+ (qps->signum != 0) ? &qps->sigmask : NULL) ;
+
+ /* If have something, set and return the pending count. */
+ if (n > 0)
+ {
+ assert(qps->pend_mnum < qps_mnum_count) ; /* expected something */
+
+ return qps->pend_count = n ; /* set and return pending count */
+ } ;
+
+ /* Flush the results vectors -- not apparently done if n <= 0) */
+ qps_super_set_zero(qps->results, qps_mnum_count) ;
+
+ qps->pend_count = 0 ; /* nothing pending */
+
+ /* Return appropriately, if we can */
+ if ((n == 0) || (errno == EINTR))
+ return n ;
+
+ zabort_errno("Failed in pselect") ;
+} ;
+
+/* Dispatch the next errored/readable/writeable file, as returned by the
+ * most recent qps_pselect().
+ *
+ * Processes the errored files, then the readable and lastly the writeable.
+ *
+ * Processes one file per call of this function, by invoking the file's
+ * "action" routine.
+ *
+ * If a given file is ready in more than one mode, all modes will be processed,
+ * unless the action routine for one mode disables the file for other modes, or
+ * removes it from the selection.
+ *
+ * Returns the number of files left to process (after the one just processed).
+ */
+int
+qps_dispatch_next(qps_selection qps)
+{
+ int fd ;
+ qps_file qf ;
+ qps_mnum_t mnum ;
+
+ /* TODO: put this under a debug skip */
+ qps_selection_validate(qps) ;
+
+ if (qps->pend_count == 0)
+ return 0 ; /* quit immediately of nothing to do. */
+
+ fd = qps->pend_fd ;
+ mnum = qps->pend_mnum ;
+
+ dassert( (mnum >= 0) && (mnum < qps_mnum_count)
+ && (qps->tried_count[mnum] != 0)
+ && (qps->pend_count > 0) ) ;
+
+ while (1)
+ {
+ fd = qps_next_fd_pending(&(qps->results[mnum]), fd, qps->tried_fd_last) ;
+ if (fd >= 0)
+ break ; /* easy if have another fd in current mode. */
+
+ do /* step to next mode that was not empty */
+ {
+ qps->tried_count[mnum] = 0 ; /* tidy up as we go */
+ ++mnum ;
+ if (mnum >= qps_mnum_count)
+ zabort("Unexpectedly ran out of pending stuff") ;
+ } while (qps->tried_count[mnum] == 0) ;
+
+ qps->pend_mnum = mnum ; /* update mode */
+ fd = 0 ; /* back to the beginning */
+ } ;
+
+ qps->pend_count -= 1 ; /* one less pending */
+ qps->pend_fd = fd ; /* update scan */
+
+ qf = qps_file_lookup_fd(qps, fd, NULL) ;
+
+ dassert( ((qf->enabled_bits && qps_mbit(mnum)) != 0) &&
+ (qf->actions[mnum] != NULL) ) ;
+
+ qf->actions[mnum](qf, qf->file_info) ; /* dispatch the required action */
+
+ return qps->pend_count ;
+} ;
+
+/*==============================================================================
+ * qps_file structure handling
+ */
+
+/* Initialise qps_file structure -- allocating one if required.
+ *
+ * If a template is given, then the action functions are copied from there to
+ * the new structure. See above for discussion of action functions.
+ *
+ * Once initialised, the file may be added to a selection.
+ *
+ * Returns the qps_file.
+ */
+qps_file
+qps_file_init_new(qps_file qf, qps_file template)
+{
+ if (qf == NULL)
+ qf = XCALLOC(MTYPE_QPS_FILE, sizeof(struct qps_file)) ;
+ else
+ memset(qf, 0, sizeof(struct qps_file)) ;
+
+ /* Zeroising has initialised:
+ *
+ * selection -- NULL -- ditto
+ *
+ * file_info -- NULL -- is set by qps_add_file()
+ * fd -- unset -- ditto
+ *
+ * enabled_bits -- nothing enabled
+ *
+ * actions[] -- all set to NULL
+ */
+
+ if (template != NULL)
+ memcpy(qf->actions, template->actions, sizeof(qf->actions)) ;
+
+ return qf ;
+} ;
+
+/* Free dynamically allocated qps_file structure.
+ *
+ * It is the caller's responsibility to have removed it from any selection it
+ * may have been in.
+ */
+void
+qps_file_free(qps_file qf)
+{
+ assert(qf->selection == NULL) ; /* Mustn't be a selection member ! */
+
+ XFREE(MTYPE_QPS_FILE, qf) ;
+} ;
+
+/* Enable (or re-enable) file for the given mode.
+ *
+ * If the action argument is not NULL, set the action for the mode.
+ *
+ * NB: It is a FATAL error to enable a mode with a NULL action.
+ *
+ * NB: It is a FATAL error to enable modes for a file which is not in a
+ * selection.
+ */
+void
+qps_enable_mode(qps_file qf, qps_mnum_t mnum, qps_action* action)
+{
+ qps_mbit_t mbit = qps_mbit(mnum) ;
+ qps_selection qps = qf->selection ;
+
+ dassert(qps != NULL) ;
+ dassert((mnum >= 0) && (mnum <= qps_mnum_count)) ;
+
+ if (action != NULL)
+ qf->actions[mnum] = action ;
+ else
+ dassert(qf->actions[mnum] != NULL) ;
+
+ if (qf->enabled_bits & mbit)
+ dassert(FD_ISSET(qf->fd, &(qps->enabled[mnum].fdset))) ;
+ else
+ {
+ dassert( ! FD_ISSET(qf->fd, &(qps->enabled[mnum].fdset))) ;
+ FD_SET(qf->fd, &(qps->enabled[mnum].fdset)) ;
+ ++qps->enabled_count[mnum] ;
+ qf->enabled_bits |= mbit ;
+ } ;
+} ;
+
+/* Set action for given mode -- does not enable/disable.
+ *
+ * May unset an action by setting it NULL !
+ *
+ * See above for discussion of action functions.
+ *
+ * NB: it is a fatal error to unset an action for a mode which is enabled.
+ */
+void
+qps_set_action(qps_file qf, qps_mnum_t mnum, qps_action* action)
+{
+ dassert((mnum >= 0) && (mnum <= qps_mnum_count)) ;
+
+ if (action == NULL)
+ passert((qf->enabled_bits & qps_mbit(mnum)) == 0) ;
+
+ qf->actions[mnum] = action ;
+} ;
+
+/* Disable file for one or more modes.
+ *
+ * If there are any pending pending results for the modes, those are discarded.
+ *
+ * Note that this is modestly "optimised" to deal with disabling a single mode.
+ * (Much of the time only the write mode will be being disabled !)
+ *
+ * NB: it is safe to disable modes which are not enabled -- even if the file
+ * is not currently a member of a selection. (If it is not a member of a
+ * collection no modes should be enabled !)
+ */
+
+static qps_mnum_t qps_first_mnum[qps_mbit(qps_mnum_count)] =
+ {
+ -1, /* 0 -> -1 -- no bit set */
+ 0, /* 1 -> 0 -- B0 is first bit */
+ 1, /* 2 -> 1 -- B1 is first bit */
+ 1, /* 3 -> 1 -- B1 is first bit */
+ 2, /* 4 -> 2 -- B2 is first bit */
+ 2, /* 5 -> 2 -- B2 is first bit */
+ 2, /* 6 -> 2 -- B2 is first bit */
+ 2 /* 7 -> 2 -- B2 is first bit */
+ } ;
+
+CONFIRM(qps_mbit(qps_mnum_count) == 8) ;
+
+void
+qps_disable_modes(qps_file qf, qps_mbit_t mbits)
+{
+ qps_mnum_t mnum ;
+
+ qps_selection qps = qf->selection ;
+
+ dassert((mbits >= 0) && (mbits <= qps_all_mbits)) ;
+
+ mbits &= qf->enabled_bits ; /* don't bother with any not enabled */
+ qf->enabled_bits ^= mbits ; /* unset what we're about to disable */
+
+ while (mbits != 0)
+ {
+ mnum = qps_first_mnum[mbits] ;
+
+ dassert(qps->enabled_count[mnum] > 0) ;
+ dassert(FD_ISSET(qf->fd, &(qps->enabled[mnum].fdset))) ;
+
+ FD_CLR(qf->fd, &(qps->enabled[mnum].fdset)) ;
+ --qps->enabled_count[mnum] ;
+
+ if ((qps->pend_count != 0) && (qps->tried_count[mnum] != 0)
+ && (FD_ISSET(qf->fd, &(qps->results[mnum].fdset))))
+ {
+ FD_CLR(qf->fd, &(qps->results[mnum].fdset)) ;
+ --qps->pend_count ;
+ } ;
+
+ mbits ^= qps_mbit(mnum) ;
+ } ;
+} ;
+
+/*==============================================================================
+ * Handling the files vector.
+ *
+ * For small numbers of fd's, the files vector is kept as a list, in fd order.
+ * Files are found by binary chop, and added/removed by insert/delete in the
+ * list.
+ *
+ * For large numbers of fd's, the files vector is kept as an array, indexed by
+ * fd.
+ */
+
+/* Comparison function for binary chop */
+static int
+qps_fd_cmp(const int** pp_fd, const qps_file* p_qf)
+{
+ if (**pp_fd < (*p_qf)->fd)
+ return -1 ;
+ if (**pp_fd > (*p_qf)->fd)
+ return +1 ;
+ return 0 ;
+}
+
+/* Lookup/Insert file by file-descriptor.
+ *
+ * Inserts if insert argument is not NULL.
+ *
+ * Returns the file we found (if any) or the file we just inserted.
+ *
+ * NB: FATAL error to insert file with same fd as an existing one.
+ */
+static qps_file
+qps_file_lookup_fd(qps_selection qps, int fd, qps_file insert)
+{
+ qps_file qf ;
+ vector_index i ;
+ int ret ;
+
+ dassert((fd >= 0) && (fd < FD_SETSIZE)) ;
+
+ /* Look-up */
+ /* */
+ /* Set i = index for entry in files vector */
+ /* Set ret = 0 <=> i is exact index. */
+ /* < 0 <=> i is just after where entry may be inserted */
+ /* > 0 <=> i is just before where entry may be inserted */
+ if (qps->fd_direct)
+ {
+ i = fd ; /* index of entry */
+ ret = 0 ; /* how to insert, if do */
+ }
+ else
+ i = vector_bsearch(&qps->files, (vector_bsearch_cmp*)qps_fd_cmp,
+ &fd, &ret) ;
+ if (ret == 0)
+ qf = vector_get_item(&qps->files, i) ; /* NULL if not there */
+ else
+ qf = NULL ; /* not there */
+
+ /* Insert now, if required and can: keep fd_count and fd_last up to date. */
+ if (insert != NULL)
+ {
+ if (qf != NULL)
+ zabort("File with given fd already exists in qps_selection") ;
+
+ /* If required, change up to a directly addressed files vector. */
+ if (!qps->fd_direct && (qps->fd_count > 9))
+ {
+ vector tmp ;
+
+ tmp = vector_move_here(NULL, &qps->files) ;
+
+ while ((qf = vector_pop_item(tmp)) != NULL)
+ vector_set_item(&qps->files, qf->fd, qf) ;
+
+ vector_free(tmp) ;
+
+ qps->fd_direct = 1 ;
+
+ i = fd ; /* index is now the fd */
+ ret = 0 ; /* and insert there */
+ } ;
+
+ /* Now can insert accordint to i & ret */
+ vector_insert_item_here(&qps->files, i, ret, insert) ;
+
+ ++qps->fd_count ;
+ if (fd > qps->fd_last)
+ qps->fd_last = fd ;
+
+ qf = insert ; /* will return what we just inserted. */
+ } ;
+
+ /* Sanity checking. */
+ dassert( (qf == NULL) || ((qps == qf->selection) && (fd == qf->fd)) ) ;
+
+ /* Return the file we found or inserted. */
+ return qf ;
+} ;
+
+/* Remove file from selection.
+ *
+ * NB: FATAL error if file is not in the selection, or the file-descriptor
+ * is invalid (or refers to some other file !).
+ */
+static void
+qps_file_remove(qps_selection qps, qps_file qf)
+{
+ qps_file qfd ;
+ int fd_last ;
+
+ passert((qf->fd >= 0) && (qf->fd <= qps->fd_last) && (qps == qf->selection)) ;
+
+ /* Look-up and remove. */
+ if (qps->fd_direct)
+ {
+ qfd = vector_unset_item(&qps->files, qf->fd) ; /* NULL if not there */
+ fd_last = vector_end(&qps->files) - 1 ;
+ }
+ else
+ {
+ qps_file qf_last ;
+ int ret ;
+ vector_index i = vector_bsearch(&qps->files,
+ (vector_bsearch_cmp*)qps_fd_cmp,
+ &qf->fd, &ret) ;
+ if (ret == 0)
+ qfd = vector_delete_item(&qps->files, i) ;
+ else
+ qfd = NULL ;
+
+ qf_last = vector_get_last_item(&qps->files) ;
+ if (qf_last != NULL)
+ fd_last = qf_last->fd ;
+ else
+ fd_last = -1 ;
+ } ;
+
+ passert(qfd == qf) ; /* must have been there and be the expected file */
+
+ /* Keep fd_count and fd_last up to date. */
+ dassert(qps->fd_count > 0) ;
+ --qps->fd_count ;
+
+ dassert( ((qps->fd_count != 0) && (fd_last >= 0)) ||
+ ((qps->fd_count == 0) && (fd_last < 0)) ) ;
+
+ qps->fd_last = (fd_last >= 0) ? fd_last : 0 ;
+
+ /* Also, remove the from all vectors. */
+ qps_disable_modes(qf, qps_all_mbits) ;
+
+ /* Is no longer in the selection. */
+ qf->selection = NULL ;
+} ;
+
+ /*==============================================================================
+ * fd_super_set support.
+ *
+ * For large sets of file descriptors something faster than testing for all
+ * possible bits is required. The fd_super_set assumes that the fd_set is a
+ * straightforward bit-vector, and overlays a 32-bit word array and a byte
+ * array over that.
+ *
+ * Cannot tell if the underlying bit vector is arranged in bytes, or some
+ * longer words. Cannot tell if words are held big or little endian. Cannot
+ * tell if lowest numbered fd will be highest or lowest in whatever unit it's
+ * held in.
+ *
+ * So... we have maps for fd -> our word index, and fd -> byte index.
+ *
+ * we have a map for fd -> mask for bit used in its byte.
+ *
+ * We require that fds will be numbered consistently in bytes. That is,
+ * every (fd mod 8) == n will appear in the same bit in a byte, for all fd (
+ * for n = 0..7). This allows the final map, which takes a byte value and
+ * returns the lowest numbered fd in the byte, mod 8.
+ *
+ * To copy all the bytes for all descriptors 0..fd, also construct
+ * fd_byte_count[] -- which copes with the fact that on a big-endian machine
+ * it is possible that descriptor fd - 8 may be in a higher numbered byte than
+ * fd ! Using this count assumes that the underlying system really does not
+ * look at bits beyond the given maximum fd.
+ */
+
+static short fd_word_map[FD_SETSIZE] ; /* maps fd to word index */
+static short fd_byte_map[FD_SETSIZE] ; /* maps fd to byte index */
+static uint8_t fd_bit_map [FD_SETSIZE] ; /* maps fd to bit in byte */
+
+static int8_t fd_first_map[256] ; /* maps byte value to 0..7, where that */
+ /* is the lowest fd bit set in byte. */
+
+#define QPS_TESTING 0 /* true => testing */
+
+#if !QPS_TESTING
+
+/* Not testing, so map to the standard FD_SET etc. functions. */
+# define qFD_SET FD_SET
+# define qFD_CLR FD_CLR
+# define qFD_ISSET FD_ISSET
+# define qFD_ZERO FD_ZERO
+
+#else
+
+/* Set up the testing */
+
+# define QPS_TEST_WORD 4 /* Wordsize */
+# define QPS_TEST_BE 1 /* true => big-endian */
+# define QPS_TEST_B_ORD 07 /* 07 => bits 0..7, 70 => bits 7..0 */
+
+# define QPS_TEST_WORD_BITS (QPS_TEST_WORD * 8)
+# if QPS_TEST_BE
+# define QPS_BYTE(fd) ( ((fd / QPS_TEST_WORD_BITS) * QPS_TEST_WORD) \
+ + (QPS_TEST_WORD - 1) - ((fd % QPS_TEST_WORD_BITS) / 8) )
+#else
+# define QPS_BYTE(fd) ( fd / 8 )
+#endif
+
+# if QPS_TEST_B_ORD == 07
+# define QPS_BIT(fd) (0x01 << (fd & 0x7))
+# else
+# define QPS_BIT(fd) (0x80 >> (fd & 0x7))
+# endif
+
+ static void
+ qFD_SET(int fd, fd_set* set)
+ {
+ *((uint8_t*)set + QPS_BYTE(fd)) |= QPS_BIT(fd) ;
+ } ;
+
+ static void
+ qFD_CLR(int fd, fd_set* set)
+ {
+ *((uint8_t*)set + QPS_BYTE(fd)) &= ~QPS_BIT(fd) ;
+ } ;
+
+ static int
+ qFD_ISSET(int fd, fd_set* set)
+ {
+ return (*((uint8_t*)set + QPS_BYTE(fd)) & QPS_BIT(fd)) != 0 ;
+ } ;
+
+ static void
+ qFD_ZERO(fd_set* set)
+ {
+ memset(set, 0, sizeof(fd_set)) ;
+ } ;
+
+#endif
+
+/* Scan for next fd in given fd set, and clear it.
+ *
+ * Starts at the given fd, will not consider anything above fd_last.
+ *
+ * Returns next fd, or -1 if none.
+ */
+static int
+qps_next_fd_pending(fd_super_set* pending, int fd, int fd_last)
+{
+ uint8_t b ;
+
+ while (pending->words[fd_word_map[fd]] == 0) /* step past zero words */
+ {
+ fd = (fd & ~ (FD_WORD_BITS - 1)) + FD_WORD_BITS ;
+ /* step to start of next word */
+ if (fd > fd_last)
+ return -1 ; /* quit if past last */
+ } ;
+
+ fd &= ~0x0007 ; /* step back to first in byte */
+ while ((b = pending->bytes[fd_byte_map[fd]]) == 0)
+ {
+ fd += 8 ;
+ if (fd > fd_last)
+ return -1 ;
+ } ;
+
+ fd += fd_first_map[b] ;
+
+ dassert(fd <= fd_last) ;
+ dassert((b & fd_bit_map[fd]) == fd_bit_map[fd]) ;
+
+ FD_CLR(fd, &pending->fdset) ;
+
+ dassert((b ^ fd_bit_map[fd]) == pending->bytes[fd_byte_map[fd]]) ;
+
+ return fd ;
+} ;
+
+/* Make a map of the fd_super_set.
+ *
+ * The form of an fd_set is not defined. This code verifies that it is, in
+ * fact a bit vector, and hence that the fd_super_set works here !
+ *
+ * It is a FATAL error if things don't work out.
+ */
+static void
+qps_make_super_set_map(void)
+{
+ fd_super_set test ;
+ int fd, i, iw, ib ;
+
+ /* (1) check that a zeroised fd_super_set is an empty one. */
+ qps_super_set_zero(&test, 1) ;
+
+ for (fd = 0 ; fd < FD_SETSIZE ; ++fd)
+ if (qFD_ISSET(fd, &test.fdset))
+ zabort("Zeroised fd_super_set is not empty") ;
+
+ /* (2) check that zeroising the fd_set doesn't change things */
+ qFD_ZERO(&test.fdset) ;
+ for (iw = 0 ; iw < FD_SUPER_SET_WORD_SIZE ; ++iw)
+ if (test.words[iw] != 0)
+ zabort("Zeroised fd_super_set is not all zero words") ;
+
+ /* (3) check that setting one fd sets one bit, and construct the */
+ /* fd_word_map[], fd_byte_map[] and fd_bit_map[]. */
+ for (fd = 0 ; fd < FD_SETSIZE ; ++fd)
+ {
+ fd_word_t w ;
+
+ qFD_SET(fd, &test.fdset) ;
+
+ w = 0 ;
+ for (iw = 0 ; iw < FD_SUPER_SET_WORD_SIZE ; ++iw)
+ {
+ if (test.words[iw] != 0)
+ {
+ if (w != 0)
+ zabort("FD_SET set a bit in more than one word") ;
+
+ w = test.words[iw] ;
+ if ((w == 0) || ((w & (w - 1)) != 0))
+ zabort("FD_SET set more than one bit in a word") ;
+
+ fd_word_map[fd] = iw ;
+
+ ib = iw * FD_WORD_BYTES ;
+ while (test.bytes[ib] == 0)
+ {
+ ++ib ;
+ if (ib >= ((iw + 1) * FD_WORD_BYTES))
+ zabort("FD_SET set something beyond the expected bytes") ;
+ } ;
+ fd_byte_map[fd] = ib ;
+ fd_bit_map[fd] = test.bytes[ib] ;
+ } ;
+ } ;
+
+ if (w == 0)
+ zabort("FD_SET did not set any bit in any word") ;
+
+ qFD_CLR(fd, &test.fdset) ;
+
+ for (iw = 0 ; iw < FD_SUPER_SET_WORD_SIZE ; ++iw)
+ if (test.words[iw] != 0)
+ zabort("FD_CLR did not leave the fd_super_set empty") ;
+ } ;
+
+ /* (4) check the fd_byte_map. */
+ /* make sure that have 8 contiguous fd to a byte. */
+ /* make sure that have 32 contiguous fd to a word. */
+
+ for (fd = 0 ; fd < FD_SETSIZE ; fd += 8)
+ {
+ int fds ;
+ ib = fd_byte_map[fd] ;
+ iw = fd_word_map[fd] ;
+
+ /* Must share the same byte as the next 7 fds */
+ for (fds = fd + 1 ; fds < (fd + 8) ; ++fds)
+ if (fd_byte_map[fds] != ib)
+ zabort("Broken fd_byte_map -- not 8 contiguous fd's in a byte") ;
+
+ /* Must not share the same byte as any other set of 8 fd's */
+ for (fds = 0 ; fds < FD_SETSIZE ; fds += 8)
+ if ((fd_byte_map[fds] == ib) && (fds != fd))
+ zabort("Broken fd_byte_map -- fd's not in expected bytes") ;
+
+ /* Must be one of the bytes in the current word's fd's */
+ if ( (ib < (iw * FD_WORD_BYTES)) || (ib >= ((iw + 1) * FD_WORD_BYTES)) )
+ zabort("Broken fd_byte_map -- fd's not in expected words") ;
+ } ;
+
+ /* (5) check the fd_bit_map */
+ /* make sure that all fd mod 8 map to the same byte value */
+
+ for (i = 0 ; i < 8 ; ++i)
+ {
+ uint8_t b = fd_bit_map[i] ;
+ for (fd = 8 + i ; fd < FD_SETSIZE ; fd += 8)
+ if (fd_bit_map[fd] != b)
+ zabort("Broken fd_bit_map -- inconsistent bit mapping") ;
+ } ;
+
+ /* (6) construct fd_first_map, to get lowest numbered fd (mod 8) from */
+ /* a given byte value. */
+
+ for (i = 0 ; i < 256 ; ++i)
+ fd_first_map[i] = -1 ;
+
+ for (fd = 0 ; fd < 8 ; ++fd)
+ {
+ uint8_t fdb = fd_bit_map[fd] ;
+ for (i = 1 ; i < 256 ; ++i)
+ if ((fd_first_map[i] == -1) && ((i & fdb) != 0))
+ fd_first_map[i] = fd ;
+ } ;
+
+ for (i = 1 ; i < 256 ; ++i)
+ if (fd_first_map[i] == -1)
+ zabort("Broken fd_first_map -- missing bits") ;
+
+ /* (7) construct fd_byte_count[] -- number of bytes required to */
+ /* include fds 0..fd. */
+
+ i = 0 ;
+ for (fd = 0 ; fd < FD_SETSIZE ; ++fd)
+ {
+ int c = fd_byte_map[fd] + 1 ;
+
+ if (c < i)
+ c = i ; /* use largest so far. => big-endian */
+ else
+ i = c ; /* keep largest so far up to date */
+
+ fd_byte_count[fd] = c ;
+ } ;
+
+#if QPS_TESTING
+
+ /* Checking that the maps have been correctly deduced */
+
+ for (fd = 0 ; fd < FD_SETSIZE ; ++fd)
+ {
+ uint8_t b ;
+ short c ;
+
+ iw = fd / QPS_TEST_WORD_BITS ;
+ if (QPS_TEST_BE)
+ ib = ( ((fd / QPS_TEST_WORD_BITS) * QPS_TEST_WORD) +
+ (QPS_TEST_WORD - 1) - ((fd % QPS_TEST_WORD_BITS) / 8) ) ;
+ else
+ ib = ( fd / 8 ) ;
+
+ if (QPS_TEST_B_ORD == 07)
+ b = 0x01 << (fd % 8) ;
+ else
+ b = 0x80 >> (fd % 8) ;
+
+ if (QPS_TEST_BE)
+ c = (iw + 1) * QPS_TEST_WORD ;
+ else
+ c = (ib + 1) ;
+
+ if (fd_word_map[fd] != iw)
+ zabort("Broken fd_word_map") ;
+ if (fd_byte_map[fd] != ib)
+ zabort("Broken fd_byte_map") ;
+ if (fd_bit_map[fd] != b)
+ zabort("Broken fd_bit_map") ;
+ if (fd_byte_count[fd] != c)
+ zabort("Broken fd_byte_count") ;
+ } ;
+
+ for (i = 1 ; i < 256 ; ++i)
+ {
+ uint8_t b = i ;
+ fd = 0 ;
+ if (QPS_TEST_B_ORD == 07)
+ {
+ while ((b & 1) == 0)
+ {
+ b >>= 1 ;
+ ++fd ;
+ } ;
+ }
+ else
+ {
+ while ((b & 0x80) == 0)
+ {
+ b <<= 1 ;
+ ++fd ;
+ } ;
+ } ;
+
+ if (fd_first_map[i] != fd)
+ zabort("Broken fd_first_map") ;
+ } ;
+
+ zabort("OK fd mapping") ;
+#endif
+
+ /* Phew -- we're all set now */
+ qps_super_set_map_made = 1 ;
+} ;
+
+/* Zeroise 'n' contiguous fd_super_sets
+ *
+ * NB: this MUST be used in place of FD_ZERO because the fd_set may be shorter
+ * than the overlayed words/bytes vectors.
+ *
+ * NB: it is CONFIRMED elsewhere that the fd_set is no longer than the overlays.
+ */
+static void
+qps_super_set_zero(fd_super_set* p_set, int n)
+{
+ memset(p_set, 0, SIZE(fd_super_set, n)) ;
+} ;
+
+#if 0 /* Mask unused function */
+/* Copy 'n' contiguous fd_super_sets
+ */
+static void
+qps_super_set_copy(fd_super_set* p_dst, fd_super_set* p_src, int n)
+{
+ memcpy(p_dst, p_src, SIZE(fd_super_set, n)) ;
+} ;
+#endif
+
+/* Compare 'n' contiguous fd_super_sets
+ */
+static int
+qps_super_set_cmp(fd_super_set* p_a, fd_super_set* p_b, int n)
+{
+ return memcmp(p_a, p_b, SIZE(fd_super_set, n)) ;
+} ;
+
+/* Count the number of bits set in 'n' contiguous fd_super_sets.
+ */
+static int
+qps_super_set_count(fd_super_set* p_set, int n)
+{
+ fd_word_t* p ;
+ int count = 0 ;
+
+ n *= FD_SUPER_SET_WORD_SIZE ;
+ confirm(sizeof(fd_super_set) == (FD_SUPER_SET_WORD_SIZE * FD_WORD_BYTES)) ;
+
+ p = (fd_word_t*)p_set ;
+ while (n--)
+ {
+ fd_word_t w = *p++ ;
+ while (w != 0)
+ {
+ ++count ;
+ w &= (w - 1) ;
+ } ;
+ } ;
+
+ return count ;
+} ;
+
+/*==============================================================================
+ * Selection state check -- for debug purposes.
+ *
+ * Runs a check across a given selection and verifies that:
+ *
+ * 1) for !fd_direct that the files are in fd order in the vector
+ * and are unique, and there are no NULL entries.
+ * 2) for fd_direct that the file fd and the index match
+ * and the last entry is not NULL
+ * 3) that all files point at the selection
+ * 4) that the enabled modes in each file are valid
+ * 5) the number of files in the selection matches fd_count.
+ * 6) the highest numbered fd matches fd_last
+ * 7) that the enabled counts in the selection are correct
+ * 8) that the enabled modes in each file match the enabled modes in the
+ * selection
+ * 9) that no extraneous fds are set in the enabled vectors
+ *
+ * If there are no pending fds:
+ *
+ * 10) if there are no pending fds, that the results vectors are empty.
+ *
+ * If there are pending fds:
+ *
+ * 11) that pend_mnum is valid and pend_fd <= tried_fd_last.
+ *
+ * 12) that the tried_count for modes 0..pend_mnum-1 is zero,
+ * and the tried_count for pend_mnum is not.
+ *
+ * 13) that the result vectors for modes where tried count == 0 are empty.
+ *
+ * 14) that the remaining result bits are a subset of the enabled bits.
+ *
+ * 15) that no bits beyond tried_fd_last are set in the result vectors.
+ *
+ * 16) that no bits before pend_fd are set in the pemd_mnum result vector.
+ *
+ * 17) that the number of bits remaining matches pend_count.
+ */
+static void
+qps_selection_validate(qps_selection qps)
+{
+ int fd_last ;
+ int enabled_count[qps_mnum_count] ;
+ fd_full_set enabled ;
+
+ qps_file qf ;
+ int fd, n, mnum, p_mnum ;
+ vector_index i ;
+
+ /* 1..4) Run down the selection vector and check. */
+ /* Collect new enabled_count and enabled bit vectors. */
+
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ enabled_count[mnum] = 0 ;
+ qps_super_set_zero(enabled, qps_mnum_count) ;
+
+ n = 0 ;
+ fd_last = -1 ;
+ for (VECTOR_ITEMS(&qps->files, qf, i))
+ {
+ if (qf != NULL)
+ {
+ ++n ; /* Number of files */
+
+ if (qps->fd_direct)
+ {
+ if (qf->fd != (int)i) /* index and fd must match */
+ zabort("File vector index and fd mismatch") ;
+ }
+ else
+ {
+ if (qf->fd <= fd_last) /* must be unique and in order */
+ zabort("File vector not in order") ;
+ } ;
+
+ fd_last = qf->fd ; /* keep track of last fd */
+
+ if (qf->selection != qps) /* file must refer to selection */
+ zabort("File does not refer to its selection") ;
+
+ if ((qf->enabled_bits < 0) || (qf->enabled_bits > qps_all_mbits))
+ zabort("File enabled bits are invalid") ;
+
+ /* Capture enabled state of all files. */
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ if (qf->enabled_bits & qps_mbit(mnum))
+ {
+ ++enabled_count[mnum] ;
+ FD_SET(qf->fd, &enabled[mnum].fdset) ;
+ } ;
+ }
+ else
+ if (!qps->fd_direct)
+ zabort("Found NULL entry in !fd_direct files vector") ;
+ } ;
+
+ if ((n != 0) && (vector_get_last_item(&qps->files) == NULL))
+ zabort("Last entry in file vector is NULL") ;
+
+ /* 5) check that the number of files tallies. */
+ if (n != qps->fd_count)
+ zabort("Number of files in the selection does not tally") ;
+
+ /* 6) check the last fd */
+ if ( ((n == 0) && (qps->fd_last !=0)) || (fd_last != qps->fd_last) )
+ zabort("The last fd does not tally") ;
+
+ /* 7) check that the enabled counts tally. */
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ if (enabled_count[mnum] != qps->enabled_count[mnum])
+ zabort("Enabled counts do not tally") ;
+
+ /* 8..9) Check that the enabled vectors are the same as the ones just */
+ /* created by scanning the files. */
+ if (qps_super_set_cmp(enabled, qps->enabled, qps_mnum_count) != 0)
+ zabort("Enabled bit vectors do not tally") ;
+
+ /* 10) if there are no pending fds, check result vectors empty. */
+ if (qps->pend_count == 0)
+ {
+ if (qps_super_set_count(qps->results, qps_mnum_count) != 0)
+ zabort("Nothing pending, but result vectors not empty") ;
+
+ return ;
+ } ;
+
+ /* This is to stop gcc whining about signed/unsigned comparisons. */
+ p_mnum = qps->pend_mnum ;
+
+ /* 11) that pend_mnum is valid and pend_fd <= tried_fd_last. */
+ if ( (p_mnum < 0) || (p_mnum > qps_mnum_count)
+ || (qps->pend_fd < 0)
+ || (qps->pend_fd > qps->tried_fd_last) )
+ zabort("Invalid pend_mnum or pend_fd") ;
+
+ /* 12) check tried_count[] */
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ {
+ if ((mnum < p_mnum) && (qps->tried_count[mnum] != 0))
+ zabort("Non-zero tried_count for mode < pend_mnum") ;
+ if ((mnum == p_mnum) && (qps->tried_count[qps->pend_mnum] <= 0))
+ zabort("Zero tried_count for pend_mnum") ;
+ if ((mnum > p_mnum) && (qps->tried_count[mnum] < 0))
+ zabort("Invalid tried_count for mode > pend_mnum") ;
+ } ;
+
+ /* 13) check result vectors for modes where tried count == 0 */
+ n = (qps_super_set_count(qps->results, qps_mnum_count) != 0) ;
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ if ((qps->tried_count[mnum] == 0)
+ && (qps_super_set_count(&qps->results[mnum], 1) != 0))
+ zabort("Non-empty bit vector where tried count == 0") ;
+
+ /* 14) check remaining results are a subset of the enableds. */
+ /* 15) check no bit beyond tried_fd_last is set in the results. */
+ /* 16) check no bit before pend_fd is set in the pemd_mnum results. */
+ /* 17) check the number of bits remaining matches pend_count. */
+
+ n = 0 ;
+ for (mnum = 0 ; mnum < qps_mnum_count ; ++mnum)
+ if (qps->tried_count[mnum] != 0)
+ {
+ for (fd = 0 ; fd < FD_SETSIZE ; ++fd)
+ if (FD_ISSET(fd, &qps->results[mnum].fdset))
+ {
+ ++n ;
+ if (fd > qps->tried_fd_last)
+ zabort("Found pending fd beyond tried_fd_last") ;
+ if ( ! FD_ISSET(fd, &qps->results[mnum].fdset))
+ zabort("Found pending fd which is not enabled") ;
+ if ((mnum == p_mnum) && (fd < qps->pend_fd))
+ zabort("Found pending fd < current next pending") ;
+ } ;
+ } ;
+
+ if (n != qps->pend_count)
+ zabort("Non-empty bit vector where tried count == 0") ;
+} ;
diff --git a/lib/qpselect.h b/lib/qpselect.h
new file mode 100644
index 00000000..1e67d174
--- /dev/null
+++ b/lib/qpselect.h
@@ -0,0 +1,208 @@
+/* Quagga pselect support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_QPSELECT_H
+#define _ZEBRA_QPSELECT_H
+
+#include <sys/select.h>
+#include <errno.h>
+
+#include "zassert.h"
+#include "qtime.h"
+#include "vector.h"
+
+#ifndef Inline
+#define Inline static inline
+#endif
+
+/*==============================================================================
+ * Quagga pselect -- qps_xxxx
+ *
+ * Here and in qpselect.c is a data structure for managing multiple file
+ * descriptors and running pselect to wait for I/O activity and to multiplex
+ * between the file descriptors.
+ */
+
+enum qps_mnum /* "mode" numbers: error/read/write */
+{
+ qps_mnum_first = 0,
+
+ qps_error_mnum = 0,
+ qps_read_mnum = 1,
+ qps_write_mnum = 2,
+
+ qps_mnum_count = 3
+} ;
+
+typedef enum qps_mnum qps_mnum_t ;
+
+#define qps_mbit(mnum) (1 << mnum)
+
+enum qps_mbits /* "mode" bits: error/read/write */
+{
+ qps_error_mbit = qps_mbit(qps_error_mnum),
+ qps_read_mbit = qps_mbit(qps_read_mnum),
+ qps_write_mbit = qps_mbit(qps_write_mnum),
+
+ qps_all_mbits = qps_mbit(qps_mnum_count) - 1
+} ;
+
+typedef enum qps_mbits qps_mbit_t ;
+
+/* Forward references */
+typedef struct qps_selection* qps_selection ;
+typedef struct qps_file* qps_file ;
+
+/*==============================================================================
+ * fd_super_set.
+ *
+ * To speed up scanning of large fd_set's this structure overlays a 32-bit
+ * word and a byte array over the (assumed) fd_set bit vector.
+ *
+ * There is no guarantee that FD_SETSIZE is a multiple of 32 (or of 8, for
+ * that matter) -- so some care must be taken.
+ */
+
+typedef uint32_t fd_word_t ;
+
+#define FD_WORD_BITS 32
+#define FD_WORD_BYTES (FD_WORD_BITS / 8)
+
+CONFIRM(FD_WORD_BITS == (FD_WORD_BYTES * 8)) ; /* for completeness */
+
+#define FD_SUPER_SET_WORD_SIZE ((FD_SETSIZE + FD_WORD_BITS - 1) / FD_WORD_BITS)
+#define FD_SUPER_SET_BYTE_SIZE (FD_SUPER_SET_WORD_SIZE * FD_WORD_BYTES)
+
+/* Make sure that the overlay is at least as big as the fd_set ! */
+CONFIRM(FD_SUPER_SET_BYTE_SIZE >= sizeof(fd_set)) ;
+
+typedef union /* see qps_make_super_set_map() */
+{
+ fd_word_t words[FD_SUPER_SET_WORD_SIZE] ;
+ uint8_t bytes[FD_SUPER_SET_BYTE_SIZE] ;
+ fd_set fdset ;
+} fd_super_set ;
+
+/* Make sure that the fd_super_set is an exact number of fd_word_t words */
+CONFIRM(sizeof(fd_super_set) == (FD_SUPER_SET_WORD_SIZE * FD_WORD_BYTES)) ;
+
+/*==============================================================================
+ * Action function.
+ *
+ * Each file has three action functions, to be called in qps_dispatch_next()
+ * when pselect() has reported error/read/write for the file.
+ *
+ * For further discussion, see: qps_file_init.
+ */
+
+typedef void qps_action(qps_file qf, void* file_info) ;
+
+/*==============================================================================
+ * Data Structures.
+ */
+
+typedef fd_super_set fd_full_set[qps_mnum_count] ;
+
+struct qps_selection
+{
+ int fd_count ; /* number of fds we are looking after */
+ int fd_direct ; /* direct lookup in vector or not */
+
+ struct vector files ; /* mapping fd to qps_file */
+
+ int fd_last ; /* highest numbered fd we are looking after */
+ int enabled_count[qps_mnum_count] ; /* no. enabled fds in each mode */
+ fd_full_set enabled ; /* bit vectors for pselect enabled stuff */
+
+ int tried_fd_last ; /* highest numbered fd on last pselect */
+ int tried_count[qps_mnum_count] ; /* enabled_count on last pselect */
+ fd_full_set results ; /* last set of results from pselect */
+
+ int pend_count ; /* results pending (if any) */
+ qps_mnum_t pend_mnum ; /* error/read/write mode pending (if any) */
+ int pend_fd ; /* fd pending (if any) */
+
+ int signum ; /* signal that sigmask is enabling -- 0 => none */
+ sigset_t sigmask ; /* sigmask to use for duration of pselect */
+} ;
+
+struct qps_file
+{
+ qps_selection selection ;
+
+ void* file_info ;
+ int fd ;
+
+ qps_mbit_t enabled_bits ;
+
+ qps_action* actions[qps_mnum_count] ;
+} ;
+
+/*==============================================================================
+ * qps_selection handling
+ */
+
+qps_selection
+qps_selection_init_new(qps_selection qps) ;
+
+void
+qps_add_file(qps_selection qps, qps_file qf, int fd, void* file_info) ;
+
+void
+qps_remove_file(qps_file qf) ;
+
+qps_file
+qps_selection_ream(qps_selection qps, int free_structure) ;
+
+/* Ream out selection and free the selection structure. */
+#define qps_selection_ream_free(qps) qps_selection_ream(qps, 1)
+/* Ream out selection but keep the selection structure. */
+#define qps_selection_ream_keep(qps) qps_selection_ream(qps, 0)
+
+void
+qps_set_signal(qps_selection qps, int signum, sigset_t sigmask) ;
+
+int
+qps_pselect(qps_selection qps, qtime_mono_t timeout) ;
+
+int
+qps_dispatch_next(qps_selection qps) ;
+
+/*==============================================================================
+ * qps_file structure handling
+ */
+
+qps_file
+qps_file_init_new(qps_file qf, qps_file template) ;
+
+void
+qps_file_free(qps_file qf) ;
+
+void
+qps_enable_mode(qps_file qf, qps_mnum_t mnum, qps_action* action) ;
+
+void
+qps_set_action(qps_file qf, qps_mnum_t mnum, qps_action* action) ;
+
+void
+qps_disable_modes(qps_file qf, qps_mbit_t mbits) ;
+
+#endif /* _ZEBRA_QPSELECT_H */
diff --git a/lib/qpthreads.c b/lib/qpthreads.c
new file mode 100644
index 00000000..5dd0cfc4
--- /dev/null
+++ b/lib/qpthreads.c
@@ -0,0 +1,570 @@
+/* Quagga Pthreads support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/* This MUST come first... otherwise we don't get __USE_UNIX98, which is */
+/* essential if glibc is to allow pthread_mutexattr_settype() to be used. */
+#include "config.h"
+
+#include <signal.h>
+
+#include "qpthreads.h"
+#include "memory.h"
+
+/* If this is not set, will get errors later. */
+#ifndef __USE_UNIX98
+#error "_USE_UNIX98 not defined"
+#endif
+
+/*==============================================================================
+ * Quagga Pthread Interface -- qpt_xxxx
+ *
+ * Here (and in qpthreads.h) are captured all the pthreads features used in
+ * Quagga.
+ *
+ * This provides:
+ *
+ * * "wrappers" around functions which should not fail, but whose return
+ * code it is best to check... at least in a debug environment.
+ *
+ * * the possibility of a separate no pthreads build where pthread facilities
+ * are either dummied out or otherwise dealt with.
+ *
+ * * the ability to add any work-arounds which may be required if poorly
+ * conforming pthreads implementations are encountered
+ *
+ * Pthread Requirements
+ * ====================
+ *
+ * This is assuming support for 1003.1-2004 -- XOPEN Issue 6, with [THR] and
+ * [XSI] options.
+ *
+ * The [XSI] is required for pthread_mutexattr_settype(), only.
+ *
+ * If qpt_thread_attr_init() uses:
+ *
+ * pthread_attr_getinheritsched()/_setinheritshed() [TPS]
+ * pthread_attr_getscope()/_setscope() [TPS]
+ * pthread_attr_getschedpolicy()/_setschedpolicy() [TPS]
+ * pthread_attr_getschedparam()/_setschedparam() [THR]
+ *
+ * but they are only required if explicit scheduling attributes are being set.
+ * (So, could be dropped where not supported.)
+ *
+ * Amongst the things which are NOT required:
+ *
+ * pthread_attr_getguardsize()/_setguardsize() [XSI]
+ * pthread_attr_getstack()/_setstack() [TSA TSS]
+ * pthread_attr_getstackaddr()/_setstackaddr() [TSA OB]
+ * pthread_attr_getstacksize()/_setstacksize() [TSA TSS]
+ *
+ * pthread_barrier_xxx() [BAR]
+ *
+ * pthread_condattr_getpshared()/_setpshared() [TSH]
+ *
+ * pthread_mutex_getprioceiling()/_setprioceiling() [TPP]
+ * pthread_mutex_timedlock() [TMO] pro tem
+ * pthread_mutexattr_getprioceiling()/_setprioceiling() [TPP]
+ * pthread_mutexattr_getprotocol()/_setprotocol() [TPP TPI]
+ * pthread_mutexattr_getpshared()/_setpshared() [TSH]
+ *
+ * pthread_rwlock_xxx() [THR] pro tem
+ * pthread_rwlockattr_init()/_destroy() [THR] pro tem
+ * pthread_rwlockattr_getpshared()/_setpshared() [TSH]
+ *
+ * pthread_spin_xxx() [SPI]
+ *
+ * [CS] (Clock Select) is assumed if HAVE_CLOCK_MONOTONIC.
+ *
+ * In 1003.1-2008, XOPEN issue 7, [THR] and pthread_mutexattr_settype() have
+ * been moved to Base.
+ *
+ * NB: it is essential that pthread_kill() delivers the signal to the target
+ * thread only -- ie, it must be POSIX compliant. That rules out the old
+ * (2.4) LinuxThreads. For Linux, 2.6 (or greater) is required, with
+ * NPTL (these days generally included in glibc).
+ *
+ * NB: for glibc to give all the required features, either _GNU_SOURCE or
+ * _XOPEN_SOURCE must be set *before* the first #include <features.h>.
+ * _XOPEN_SOURCE=600 is sufficient.
+ *
+ * Pthread Thread Attributes -- Scheduling
+ * =======================================
+ *
+ * Pthreads defines some useful looking real-time scheduling features.
+ *
+ * One would like to be able to give I/O intensive threads an advantage over
+ * CPU bound threads.
+ *
+ * Unfortunately, conformance allows a system to have its own scheduling
+ * system -- so long as the standard ones are implemented. Further, there is
+ * no way of telling what priority values are reasonable, even in the standard
+ * scheduling policies.
+ *
+ * The approach taken here is that by default a thread will be created with
+ * the system default attributes -- which may mean inheriting the creating
+ * thread's scheduling attributes.
+ *
+ * It is also possible to construct a set of attributes, using the most
+ * obviously useful properties. It is envisaged that this may be used when a
+ * configuration file is used to set locally sensible values. The attributes
+ * supported are:
+ *
+ * * attr_detached -- whether to start detached or not
+ * * attr_inherit_sched -- whether to inherit scheduling attributes
+ * * attr_sched_scope -- scheduling scope
+ * * attr_sched_policy -- scheduling policy
+ * * attr_sched_priority -- scheduling priority
+ *
+ * See qpt_thread_attr_init, below.
+ *
+ * Not supported here are:
+ *
+ * * attr_guardsize
+ * * attr_stack
+ * * attr_stacksize
+ *
+ * Pthread Mutex Attributes -- Error Checking
+ * ==========================================
+ *
+ * Mutexes are kept simple, only attr_type is used, and that by default.
+ *
+ * POSIX defines four types of mutex:
+ *
+ * _NORMAL no ownership check -- owner will deadlock if locks mutex !
+ * -- undefined what happens if unlock
+ * mutex not owned by self !
+ * no recursive locking
+ *
+ * _ERRORCHECK checks for ownership on lock and unlock
+ * no recursive locking
+ *
+ * _RECURSIVE checks for ownership on lock and unlock
+ * counts up locks and counts down unlocks
+ *
+ * This looks useful, but goes wrong with condition variables !
+ *
+ * _DEFAULT undefined whether checks owner or not, on lock and/or unlock.
+ * no recursive locking
+ *
+ * See qpthreads.h for discussion of Quagga's standard type (QPT_MUTEX_TYPE).
+ *
+ * Other attributes are left in their default state:
+ *
+ * * attr_prioceiling -- default undefined
+ * * attr_protocol -- default undefined
+ * * attr_pshared -- defaults to _PROCESS_PRIVATE
+ *
+ * For the time being it is assumed that these are too exotic.
+ *
+ * Pthread Condition Variable Attributes
+ * =====================================
+ *
+ * Condition variables have only two attributes:
+ *
+ * * attr_clock -- which clock to use
+ * * attr_pshared -- defaults to _PROCESS_PRIVATE
+ *
+ * The use a clock other than Quagga's standard (QPT_COND_CLOCK_ID) is possible,
+ * but not recommended. (See qpthreads.h for discussion of this.)
+ *
+ * Pthread Specific Signal Handling
+ * ================================
+ *
+ * In a threaded application, need to use pthread_sigmask (not sigproc_mask).
+ * (Can use pthread_sigmask in a single threaded application.)
+ *
+ * To direct a signal at a given thread need pthread_kill. *
+ */
+
+/*==============================================================================
+ * Thread creation and attributes.
+ *
+ * Threads may be created with a given set of attributes if required.
+ *
+ * qpt_thread_attr_init() will initialise a set of attributes including the
+ * current standard scheduling attributes. It is envisaged that configuration
+ * options may be used to specify these.
+ *
+ * qpt_thread_create() creates a thread using the given attributes. If those
+ * are NULL, then the system defaults are used.
+ */
+
+/* Initialise a set of attributes -- setting the scheduling options.
+ *
+ * Options:
+ *
+ * qpt_attr_joinable -- the default if nothing specified.
+ * qpt_attr_detached -- overrides qpt_attr_joinable.
+ *
+ * qpt_attr_sched_inherit -- all scheduling attributes are to be inherited.
+ * No explicit scheduling attributes may be set.
+ *
+ * qpt_attr_sched_scope -- set explicit, given, scope.
+ * qpt_attr_sched_policy -- set explicit, given, policy
+ * qpt_attr_sched_priority -- set explicit, given, priority
+ *
+ * If none of the _sched_ options are given, then the scheduling attributes are
+ * left to whatever default values the system chooses.
+ *
+ * If the _sched_inherit option is specified, none of the other _sched_ options
+ * may be specified.
+ *
+ * If any of the explicit scheduling options are given, they are set in this
+ * order. If only some of these options are given, then the caller is
+ * assuming that the system will choose sensible defaults.
+ *
+ * The scope, policy and priority arguments are use only if the corresponding
+ * option is specified.
+ *
+ * Returns the address of the qpt_thread_attr_t structure.
+ */
+qpt_thread_attr_t*
+qpt_thread_attr_init(qpt_thread_attr_t* attr, enum qpt_attr_options opts,
+ int scope, int policy, int priority)
+{
+ int err ;
+
+ assert((opts & ~qpt_attr_known) == 0) ;
+
+ /* Initialise thread attributes structure (allocating if required.) */
+ if (attr == NULL)
+ attr = XMALLOC(MTYPE_QPT_THREAD_ATTR, sizeof(qpt_thread_attr_t)) ;
+
+ err = pthread_attr_init(attr) ;
+ if (err != 0)
+ zabort_err("pthread_attr_init failed", err) ;
+
+ /* If not qpt_attr_detached, then set joinable. */
+ err = pthread_attr_setdetachstate(attr,
+ (opts & qpt_attr_detached) ? PTHREAD_CREATE_DETACHED
+ : PTHREAD_CREATE_JOINABLE) ;
+ if (err != 0)
+ zabort_err("pthread_attr_setdetachstate failed", err) ;
+
+ /* If setting anything to do with scheduling... */
+ if (opts & qpt_attr_sched_setting)
+ {
+ /* Either we inherit or we set explicit parameters. */
+
+ err = pthread_attr_setinheritsched(attr,
+ (opts & qpt_attr_sched_inherit) ? PTHREAD_INHERIT_SCHED
+ : PTHREAD_EXPLICIT_SCHED) ;
+ if (err != 0)
+ zabort_err("pthread_attr_setinheritsched", err) ;
+
+ if (opts & qpt_attr_sched_inherit)
+ assert((opts & qpt_attr_sched_explicit) == 0) ;
+ else
+ {
+ if (opts & qpt_attr_sched_scope)
+ {
+ err = pthread_attr_setscope(attr, scope) ;
+ if (err != 0)
+ zabort_err("pthread_attr_setscope failed", err) ;
+ } ;
+ if (opts & qpt_attr_sched_policy)
+ {
+ err = pthread_attr_setschedpolicy(attr, scope) ;
+ if (err != 0)
+ zabort_err("pthread_attr_setschedpolicy failed", err) ;
+ } ;
+ if (opts & qpt_attr_sched_priority)
+ {
+ struct sched_param sparm ;
+ err = pthread_attr_getschedparam(attr, &sparm) ;
+ if (err != 0)
+ zabort_err("pthread_attr_getschedparam failed", err) ;
+ sparm.sched_priority = priority ;
+ err = pthread_attr_setschedparam(attr, &sparm) ;
+ if (err != 0)
+ zabort_err("pthread_attr_setschedparam failed", err) ;
+ } ;
+ } ;
+ } ;
+
+ /* Done -- return qpt_thread_attr_t* */
+ return attr ;
+} ;
+
+/* Create Thread with given attributes (if any).
+ *
+ * If no attributes are given (attr == NULL) the thread is created with system
+ * default attributes -- *except* that it is created joinable.
+ *
+ * Returns the qpt_thread_t "thread id".
+ */
+qpt_thread_t
+qpt_thread_create(void* (*start)(void*), void* arg, qpt_thread_attr_t* attr)
+{
+ qpt_thread_attr_t thread_attr ;
+ qpt_thread_t thread_id ;
+ int default_attr ;
+ int err ;
+
+ default_attr = (attr == NULL) ;
+ if (default_attr)
+ attr = qpt_thread_attr_init(&thread_attr, qpt_attr_joinable, 0, 0, 0) ;
+
+ err = pthread_create(&thread_id, attr, start, arg) ;
+ if (err != 0)
+ zabort_err("pthread_create failed", err) ;
+
+ if (default_attr)
+ {
+ err = pthread_attr_destroy(attr) ; /* being tidy */
+ if (err != 0)
+ zabort_err("pthread_attr_destroy failed", err) ;
+ } ;
+
+ return thread_id ;
+} ;
+
+/*==============================================================================
+ * Mutex initialise and destroy.
+ */
+
+/* Initialise Mutex (allocating if required).
+ *
+ * Options:
+ *
+ * qpt_mutex_quagga -- see qpthreads.h for discussion of this.
+ * qpt_mutex_normal -- ie PTHREAD_MUTEX_NORMAL
+ * qpt_mutex_recursive -- ie PTHREAD_MUTEX_RECURSIVE
+ * qpt_mutex_errorcheck -- ie PTHREAD_MUTEX_ERRORCHECK
+ * qpt_mutex_default -- system default
+ *
+ * Of these _recursive is the most likely alternative to _quagga... BUT do
+ * remember that such mutexes DO NOT play well with condition variables.
+ */
+qpt_mutex_t*
+qpt_mutex_init(qpt_mutex_t* mx, enum qpt_mutex_options opts)
+{
+ pthread_mutexattr_t mutex_attr ;
+ int type ;
+ int err ;
+
+ if (mx == NULL)
+ mx = XMALLOC(MTYPE_QPT_MUTEX, sizeof(qpt_mutex_t)) ;
+
+ /* Set up attributes so we can set the mutex type */
+ err = pthread_mutexattr_init(&mutex_attr);
+ if (err != 0)
+ zabort_err("pthread_mutexattr_init failed", err) ;
+
+ switch(opts)
+ {
+ case qpt_mutex_quagga:
+ type = QPT_MUTEX_TYPE ;
+ break ;
+ case qpt_mutex_normal:
+ type = PTHREAD_MUTEX_NORMAL ;
+ break ;
+ case qpt_mutex_recursive:
+ type = PTHREAD_MUTEX_RECURSIVE ;
+ break ;
+ case qpt_mutex_errorcheck:
+ type = PTHREAD_MUTEX_ERRORCHECK ;
+ break ;
+ case qpt_mutex_default:
+ type = PTHREAD_MUTEX_DEFAULT ;
+ break ;
+ default:
+ zabort("Invalid qpt_mutex option") ;
+ } ;
+
+#ifndef PTHREAD_MUTEXATTR_SETTYPE_MISSING
+ err = pthread_mutexattr_settype(&mutex_attr, type);
+ if (err != 0)
+ zabort_err("pthread_mutexattr_settype failed", err) ;
+#endif
+
+ /* Now we're ready to initialize the mutex itself */
+ err = pthread_mutex_init(mx, &mutex_attr) ;
+ if (err != 0)
+ zabort_err("pthread_mutex_init failed", err) ;
+
+ /* Be tidy with the attributes */
+ err = pthread_mutexattr_destroy(&mutex_attr) ;
+ if (err != 0)
+ zabort_err("pthread_mutexattr_destroy failed", err) ;
+
+ /* Done: return the mutex */
+ return mx ;
+} ;
+
+/* Destroy given mutex, and (if required) free it.
+ *
+ * Returns NULL.
+*/
+qpt_mutex_t*
+qpt_mutex_destroy(qpt_mutex_t* mx, int free_mutex)
+{
+ int err ;
+
+ err = pthread_mutex_destroy(mx) ;
+ if (err != 0)
+ zabort_err("pthread_mutex_destroy failed", err) ;
+
+ if (free_mutex)
+ XFREE(MTYPE_QPT_MUTEX, mx) ;
+
+ return NULL ;
+} ;
+
+/*==============================================================================
+ * Condition Variable initialise and destroy.
+ */
+
+/* Initialise Condition Variable (allocating if required).
+ *
+ * Options:
+ *
+ * qpt_cond_quagga -- use Quagga's default clock
+ * qpt_cond_realtime -- force CLOCK_REALTIME
+ * qpt_cond_monotonic -- force CLOCK_MONOTONIC (if available)
+ */
+qpt_cond_t*
+qpt_cond_init(qpt_cond_t* cv, enum qpt_cond_options opts)
+{
+ pthread_condattr_t cond_attr ;
+ int err ;
+
+ if (cv == NULL)
+ cv = XMALLOC(MTYPE_QPT_COND, sizeof(qpt_cond_t)) ;
+
+ /* Set up attributes so we can set the type */
+ err = pthread_condattr_init(&cond_attr);
+ if (err != 0)
+ zabort_err("pthread_condattr_init failed", err) ;
+
+ switch(opts)
+ {
+ case qpt_cond_quagga:
+ break ;
+ default:
+ zabort("Invalid qpt_cond option") ;
+ } ;
+
+ err = pthread_condattr_setclock(&cond_attr, QPT_COND_CLOCK_ID);
+ if (err != 0)
+ zabort_err("pthread_condattr_setclock failed", err) ;
+
+ /* Now we're ready to initialize the condition variable itself */
+ err = pthread_cond_init(cv, &cond_attr) ;
+ if (err != 0)
+ zabort_err("pthread_cond_init failed", err) ;
+
+ /* Be tidy with the attributes */
+ err = pthread_condattr_destroy(&cond_attr) ;
+ if (err != 0)
+ zabort_err("pthread_condattr_destroy failed", err) ;
+
+ /* Done: return the condition variable */
+ return cv ;
+} ;
+
+/* Destroy given mutex, and (if required) free it.
+ *
+ * Returns NULL.
+*/
+qpt_cond_t*
+qpt_cond_destroy(qpt_cond_t* cv, int free_cond)
+{
+ int err ;
+
+ err = pthread_cond_destroy(cv) ;
+ if (err != 0)
+ zabort_err("pthread_cond_destroy failed", err) ;
+
+ if (free_cond)
+ XFREE(MTYPE_QPT_COND, cv) ;
+
+ return NULL ;
+} ;
+
+/* Wait for given condition variable or time-out.
+ *
+ * Returns: wait succeeded (1 => success, 0 => timed-out).
+ *
+ * NB: timeout time is a qtime_mono_t (monotonic time).
+ *
+ * Has to check the return value, so zabort_errno if not EBUSY.
+ */
+
+int
+qpt_cond_timedwait(qpt_cond_t* cv, qpt_mutex_t* mx, qtime_mono_t timeout_time)
+{
+ struct timespec ts ;
+
+ if (QPT_COND_CLOCK_ID != CLOCK_MONOTONIC)
+ {
+ timeout_time = qt_clock_gettime(QPT_COND_CLOCK_ID)
+ + (timeout_time - qt_get_monotonic()) ;
+ } ;
+
+ int err = pthread_cond_timedwait(cv, mx, qtime2timespec(&ts, timeout_time)) ;
+ if (err == 0)
+ return 1 ; /* got condition */
+ if (err == ETIMEDOUT)
+ return 0 ; /* got time-out */
+
+ zabort_err("pthread_cond_timedwait failed", err) ;
+ /* crunch */
+} ;
+
+/*==============================================================================
+ * Signal Handling.
+ */
+
+/* Set thread signal mask
+ *
+ * Thin wrapper around pthread_sigmask.
+ *
+ * zaborts if gets any error.
+ */
+void
+qpt_thread_sigmask(int how, const sigset_t* set, sigset_t* oset)
+{
+ int err ;
+
+ if (oset != NULL)
+ sigemptyset(oset) ; /* to make absolutely sure */
+
+ err = pthread_sigmask(how, set, oset) ;
+ if (err != 0)
+ zabort_err("pthread_sigmask failed", err) ;
+} ;
+
+/* Send given thread the given signal
+ *
+ * Thin wrapper around pthread_kill.
+ *
+ * zaborts if gets any error.
+ */
+void
+qpt_thread_signal(qpt_thread_t thread, int signum)
+{
+ int err ;
+
+ err = pthread_kill(thread, signum) ;
+ if (err != 0)
+ zabort_err("pthread_kill failed", err) ;
+} ;
diff --git a/lib/qpthreads.h b/lib/qpthreads.h
new file mode 100644
index 00000000..5a442cc7
--- /dev/null
+++ b/lib/qpthreads.h
@@ -0,0 +1,363 @@
+/* Quagga Pthreads support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_QPTHREADS_H
+#define _ZEBRA_QPTHREADS_H
+
+#include <stdint.h>
+#include <time.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "zassert.h"
+#include "qtime.h"
+
+#ifndef Inline
+#define Inline static inline
+#endif
+
+/*==============================================================================
+ * Quagga Pthread Interface -- qpt_xxxx
+ *
+ * Here are captured all the pthreads features used in Quagga.
+ *
+ * This provides:
+ *
+ * * "wrappers" around functions which should not fail, but whose return
+ * code it is best to check... at least in a debug environment.
+ *
+ * * the possibility of a separate no pthreads build where pthread facilities
+ * are either dummied out or otherwise dealt with.
+ *
+ * * the ability to add any work-arounds which may be required if poorly
+ * conforming pthreads implementations are encountered
+ */
+
+#if !defined(_POSIX_THREADS) || (_POSIX_THREADS <= 0)
+#error Require _POSIX_THREADS
+#endif
+
+/*==============================================================================
+ * Data types
+ */
+
+typedef pthread_t qpt_thread_t ;
+typedef pthread_mutex_t qpt_mutex_t ;
+typedef pthread_cond_t qpt_cond_t ;
+
+typedef pthread_attr_t qpt_thread_attr_t ;
+
+/*==============================================================================
+ * Thread Creation -- see qpthreads.c for further discussion.
+ */
+
+enum qpt_attr_options
+{
+ qpt_attr_joinable = 0, /* the default for Quagga */
+
+ qpt_attr_detached = 0x0001, /* otherwise will set joinable */
+
+ qpt_attr_sched_inherit = 0x0002, /* otherwise will use default */
+
+ qpt_attr_sched_scope = 0x0004, /* otherwise inherit/default */
+ qpt_attr_sched_policy = 0x0008, /* otherwise inherit/default */
+ qpt_attr_sched_priority = 0x0010, /* otherwise inherit/default */
+} ;
+
+#define qpt_attr_sched_explicit ( qpt_attr_sched_scope \
+ | qpt_attr_sched_policy \
+ | qpt_attr_sched_priority )
+
+#define qpt_attr_sched_setting ( qpt_attr_sched_inherit \
+ | qpt_attr_sched_explicit )
+
+#define qpt_attr_known ( qpt_attr_detached | qpt_attr_sched_setting )
+
+extern qpt_thread_attr_t*
+qpt_thread_attr_init(qpt_thread_attr_t* attr, enum qpt_attr_options opts,
+ int scope, int policy, int priority) ;
+extern qpt_thread_t
+qpt_thread_create(void* (*start)(void*), void* arg, qpt_thread_attr_t* attr) ;
+
+/*==============================================================================
+ * Thread self knowledge.
+ */
+
+Inline qpt_thread_t qpt_thread_self(void)
+{
+ return pthread_self() ;
+} ;
+
+/*==============================================================================
+ * Mutex handling.
+ *
+ * Quagga's default mutex type is:
+ *
+ * * PTHREAD_MUTEX_ERRORCHECK unless NDEBUG && NDEBUG_QPTHREADS
+ * * QPT_MUTEX_TYPE_DEFAULT
+ *
+ * QPT_MUTEX_TYPE_DEFAULT may be set elsewhere. If it is not set then it is
+ * set here to be PTHREAD_MUTEX_NORMAL.
+ *
+ * NB: on the face of it PTHREAD_MUTEX_NORMAL should be the fastest. It is
+ * possible that PTHREAD_MUTEX_DEFAULT may have system specific semantics
+ * that make it faster than the standard _NORMAL. It is also possible that
+ * a given system may elect to provide a safer mutex than the _NORMAL by
+ * default.
+ *
+ * If _DEFAULT is faster than _NORMAL, then QPT_MUTEX_TYPE_DEFAULT may be
+ * used to override this choice.
+ *
+ * NB: do not (currently) support pthread_mutex_timedlock().
+ */
+
+enum qpt_mutex_options
+{
+ qpt_mutex_quagga = 0x0000, /* Quagga's default */
+ qpt_mutex_normal = 0x0001,
+ qpt_mutex_recursive = 0x0002,
+ qpt_mutex_errorcheck = 0x0003,
+ qpt_mutex_default = 0x0004, /* system default */
+} ;
+
+#ifndef QPT_MUTEX_TYPE_DEFAULT
+# define QPT_MUTEX_TYPE_DEFAULT PTHREAD_MUTEX_NORMAL
+#endif
+
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+# define QPT_MUTEX_TYPE QPT_MUTEX_TYPE_DEFAULT
+#else
+# define QPT_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
+#endif
+
+extern qpt_mutex_t*
+qpt_mutex_init(qpt_mutex_t* mx, enum qpt_mutex_options opts) ;
+
+extern qpt_mutex_t*
+qpt_mutex_destroy(qpt_mutex_t* mx, int free_mutex) ;
+
+#define qpt_mutex_destroy_keep(mx) qpt_mutex_destroy(mx, 0)
+#define qpt_mutex_destroy_free(mx) qpt_mutex_destroy(mx, 1)
+
+Inline void
+qpt_mutex_lock(qpt_mutex_t* mx) ; /* do nothing if mx == NULL */
+
+Inline int
+qpt_mutex_trylock(qpt_mutex_t* mx) ; /* do nothing if mx == NULL */
+
+Inline void
+qpt_mutex_unlock(qpt_mutex_t* mx) ; /* do nothing if mx == NULL */
+
+/*==============================================================================
+ * Condition Variable handling
+ *
+ * Quagga's clock for condition variables is QPT_COND_CLOCK_ID, which
+ * may be set elsewhere. If it is not set then it is set here to be:
+ *
+ * * CLOCK_MONOTONIC if available
+ * * CLOCK_REALTIME otherwise -- this is the standard default.
+ *
+ * QPT_COND_CLOCK_MONOTONIC is set if CLOCK_MONOTONIC is used (and must be set
+ * if QPT_COND_CLOCK_ID is set elsewhere to something that is monotonic).
+ *
+ * NB: the time-out time passed to qpt_cond_timedwait() is a qtime_mono_t
+ * time (so based on qtime's monotonic time, which is CLOCK_MONOTONIC if
+ * that is available.
+ *
+ * Otherwise, there is a conversion step from qtime_mono_t to whatever the
+ * timebase for the condition variable is.
+ *
+ * NB: static initialisation of condition variables is not supported, to avoid
+ * confusion between the standard default and Quagga's default.
+ */
+
+#ifndef QPT_COND_CLOCK_ID
+# ifdef HAVE_CLOCK_MONOTONIC
+# define QPT_COND_CLOCK_ID CLOCK_MONOTONIC
+# define QPT_COND_CLOCK_MONOTONIC 1
+# else
+# define QPT_COND_CLOCK_ID CLOCK_REALTIME
+# undef QPT_COND_CLOCK_MONOTONIC
+# endif
+#endif
+
+enum qpt_cond_options
+{
+ qpt_cond_quagga = 0x0000, /* Quagga's default */
+} ;
+
+extern qpt_cond_t*
+qpt_cond_init(qpt_cond_t* cv, enum qpt_cond_options opts) ;
+
+extern qpt_cond_t*
+qpt_cond_destroy(qpt_cond_t* cv, int free_cond) ;
+
+#define qpt_cond_destroy_keep(cv) qpt_cond_destroy(cv, 0)
+#define qpt_cond_destroy_free(cv) qpt_cond_destroy(cv, 1)
+
+Inline void
+qpt_cond_wait(qpt_cond_t* cv, qpt_mutex_t* mx) ;
+
+extern int
+qpt_cond_timedwait(qpt_cond_t* cv, qpt_mutex_t* mx, qtime_mono_t timeout_time) ;
+
+Inline void
+qpt_cond_signal(qpt_cond_t* cv) ;
+
+Inline void
+qpt_cond_broadcast(qpt_cond_t* cv) ;
+
+/*==============================================================================
+ * Mutex inline functions
+ */
+
+/* Lock given mutex.
+ *
+ * Unless both NCHECK_QPTHREADS and NDEBUG are defined, checks that the
+ * return value is valid -- zabort_errno if it isn't.
+ */
+
+Inline void
+qpt_mutex_lock(qpt_mutex_t* mx) /* do nothing if mx == NULL */
+{
+ if (mx != NULL)
+ {
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+ pthread_mutex_lock(mx) ;
+#else
+ int err = pthread_mutex_lock(mx) ;
+ if (err != 0)
+ zabort_err("pthread_mutex_lock failed", err) ;
+#endif
+ } ;
+} ;
+
+/* Try to lock given mutex.
+ *
+ * Returns: lock succeeded (1 => have locked, 0 => unable to lock).
+ *
+ * Has to check the return value, so zabort_errno if not EBUSY.
+ */
+
+Inline int
+qpt_mutex_trylock(qpt_mutex_t* mx) /* do nothing if mx == NULL */
+{
+ if (mx != NULL)
+ {
+ int err = pthread_mutex_trylock(mx) ;
+ if (err == 0)
+ return 1 ; /* success: it's locked. */
+ if (err == EBUSY)
+ return 0 ; /* unable to lock */
+
+ zabort_err("pthread_mutex_trylock failed", err) ;
+ /* crunch */
+ } ;
+} ;
+
+/* Unlock given mutex.
+ *
+ * Unless both NCHECK_QPTHREADS and NDEBUG are defined, checks that the
+ * return value is valid -- zabort_errno if it isn't.
+ */
+Inline void
+qpt_mutex_unlock(qpt_mutex_t* mx) /* do nothing if mx == NULL */
+{
+ if (mx != NULL)
+ {
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+ pthread_mutex_unlock(mx) ;
+#else
+ int err = pthread_mutex_unlock(mx) ;
+ if (err != 0)
+ zabort_err("pthread_mutex_unlock failed", err) ;
+#endif
+ } ;
+} ;
+
+/*==============================================================================
+ * Condition variable inline functions
+ */
+
+/* Wait for given condition variable.
+ *
+ * Unless both NCHECK_QPTHREADS and NDEBUG are defined, checks that the
+ * return value is valid -- zabort_errno if it isn't.
+ */
+
+Inline void
+qpt_cond_wait(qpt_cond_t* cv, qpt_mutex_t* mx)
+{
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+ pthread_cond_wait(cv, mx) ;
+#else
+ int err = pthread_cond_wait(cv, mx) ;
+ if (err != 0)
+ zabort_err("pthread_cond_wait failed", err) ;
+#endif
+} ;
+
+/* Signal given condition.
+ *
+ * Unless both NCHECK_QPTHREADS and NDEBUG are defined, checks that the
+ * return value is valid -- zabort_errno if it isn't.
+ */
+
+Inline void
+qpt_cond_signal(qpt_cond_t* cv)
+{
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+ pthread_cond_signal(cv) ;
+#else
+ int err = pthread_cond_signal(cv) ;
+ if (err != 0)
+ zabort_err("pthread_cond_signal failed", err) ;
+#endif
+} ;
+
+/* Broadcast given condition.
+ *
+ * Unless both NCHECK_QPTHREADS and NDEBUG are defined, checks that the
+ * return value is valid -- zabort_errno if it isn't.
+ */
+Inline void
+qpt_cond_broadcast(qpt_cond_t* cv)
+{
+#if defined(NDEBUG) && defined(NDEBUG_QPTHREADS)
+ pthread_cond_broadcast(cv) ;
+#else
+ int err = pthread_cond_broadcast(cv) ;
+ if (err != 0)
+ zabort_err("pthread_cond_broadcast failed", err) ;
+#endif
+} ;
+
+/*==============================================================================
+ * Signal Handling.
+ */
+void
+qpt_thread_sigmask(int how, const sigset_t* set, sigset_t* oset) ;
+
+void
+qpt_thread_signal(qpt_thread_t thread, int signum) ;
+
+#endif /* _ZEBRA_QPTHREADS_H */
diff --git a/lib/qtime.c b/lib/qtime.c
new file mode 100644
index 00000000..3ec34a72
--- /dev/null
+++ b/lib/qtime.c
@@ -0,0 +1,194 @@
+/* Quagga realtime and monotonic clock handling -- functions
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/times.h>
+#include <errno.h>
+
+#include "zassert.h"
+#include "qtime.h"
+
+/*==============================================================================
+ * This is a collection of functions and (in qtime.h) macros and inline
+ * functions which support system time and a monotonic clock.
+ *
+ * TODO: introduce mutex for crafted monotonic time, and initialisation
+ * routine for that: which can preset the various variables... but
+ * unless is guaranteed to be called, must leave the on-the-fly
+ * initialisation... could also start a watchdog at that point.
+ */
+
+/*==============================================================================
+ * Replacement for CLOCK_MONOTONIC.
+ *
+ * With thanks to Joakim Tjernlund for reminding everyone of the return value
+ * from times() !
+ *
+ * times() is defined to return a value which is the time since some fixed time
+ * before the application started (or when the application started). This time
+ * is measured in units of sysconf(_SC_CLK_TCK) ticks per second.
+ *
+ * The only tricky bit is that the value returned (of type clock_t) is a
+ * signed integer, which can overflow. It is not defined exactly how it
+ * does this... This code assumes that the system will wrap around in some
+ * obvious way. The base of the time for this clock may be when the *system*
+ * started... so when it overflows may depend on how long the system has been
+ * up... which suggests that some sensible wrap around is likely (?).
+ *
+ * The qtime_t value is in nano-seconds.
+ *
+ * The result from times() is in units of sysconf(_SC_CLK_TCK) ticks per second.
+ *
+ * If clock_t is a signed 32-bit integer, which is kept +ve, then the clock
+ * overflows/wraps round in 2^31 ticks which is:
+ *
+ * at 100 ticks/sec: > 248 days
+ * at 1,000 ticks/sec: > 24 days
+ * at 10,000 ticks/sec: > 59 hours
+ *
+ * For safety, this asserts that sysconf(_SC_CLK_TCK) <= 1,000,000 for
+ * sizeof(clock_t) > 4, but <= 1,000 for sizeof(clock_t) == 4.
+ *
+ * (It appears that 60, 100, 250 and 1,000 ticks/sec. are popular options.)
+ *
+ * If sizeof(clock_t) > 4, it is assumed large enough never to wrap around.
+ *
+ * When clock_t is a 32-bit integer must be at least ready for wrap around.
+ * There are two cases:
+ *
+ * * +ve wrap around. new < old value, and new >= 0
+ *
+ * step = (INT32_MAX - old + 1) + new
+ *
+ * * -ve wrap around. new < old value, and new < 0 (and old > 0)
+ *
+ * step = (INT32_MAX - old + 1) - (INT32_MIN - new)
+ *
+ * In any event, a step > 24 hours is taken to means that something has gone
+ * very, very badly wrong.
+ *
+ * NB: it is assumed that qt_craft_monotonic will be called often enough to
+ * ensure that the check on the step size will not be triggered !
+ *
+ * NB: it is assumed that times() does not simply stick if it overflows.
+ *
+ * TODO: Add a watchdog to monitor the behaviour of this clock ?
+ */
+
+CONFIRM((sizeof(clock_t) >= 4) && (sizeof(clock_t) <= 8)) ;
+
+#ifdef GNU_LINUX
+#define TIMES_TAKES_NULL 1
+#else
+#undef TIMES_TAKES_NULL
+#endif
+
+static uint64_t monotonic = 0 ; /* monotonic clock in _SC_CLK_TCK's */
+static int64_t last_times_sample = 0 ; /* last value returned by times() */
+
+static uint64_t step_limit = 0 ; /* for sanity check */
+
+static int64_t times_clk_tcks = 0 ; /* sysconf(_SC_CLK_TCK) */
+static qtime_t times_scale_q = 0 ; /* 10**9 / times_clk_tcks */
+static qtime_t times_scale_r = 0 ; /* 10**9 % times_clk_tcks */
+
+qtime_mono_t
+qt_craft_monotonic(void) {
+ struct tms dummy ;
+ int64_t this_times_sample ;
+ uint64_t step ;
+
+ /* Set up times_scale_q & times_scale_q if not yet done. */
+ if (times_clk_tcks == 0) /* Is zero until it's initialized */
+ {
+ lldiv_t qr ;
+ confirm(sizeof(qtime_t) <= sizeof(long long int)) ;
+
+ times_clk_tcks = sysconf(_SC_CLK_TCK) ;
+ passert((times_clk_tcks > 0) &&
+ (times_clk_tcks <= (sizeof(clock_t) > 4) ? 1000000
+ : 1000)) ;
+
+ qr = lldiv(QTIME_SECOND, times_clk_tcks) ;
+ times_scale_q = qr.quot ;
+ times_scale_r = qr.rem ;
+
+ step_limit = (uint64_t)24 * 60 * 60 * times_clk_tcks ;
+ } ;
+
+ /* No errors are defined for times(), but a return of -1 is defined */
+ /* to indicate an error condition, with errno saying what it is ! */
+ /* */
+ /* The following deals carefully with this -- cannot afford for the */
+ /* clock either to jump or to get stuck ! */
+
+#ifdef TIMES_TAKES_NULL
+ this_times_sample = times(NULL) ; /* assume this saves effort ! */
+#else
+ this_times_sample = times(&dummy) ;
+#endif
+
+ if (this_times_sample == -1) /* deal with theoretical error */
+ {
+ errno = 0 ;
+ this_times_sample = times(&dummy) ;
+ if (errno != 0)
+ zabort_errno("times() failed") ;
+ } ;
+
+ /* Calculate the step and verify sensible. */
+ /* */
+ /* Watch out for huge jumps and/or time going backwards. */
+ /* For 32-bit clock_t, look out for wrap-around. */
+
+ if ((sizeof(clock_t) > 4) || (this_times_sample > last_times_sample))
+ /* time going backwards will appear as HUGE step forwards. */
+ step = (uint64_t)(this_times_sample - last_times_sample) ;
+ else
+ {
+ if (this_times_sample > 0)
+ /* both samples +ve => +ve wrap around. */
+ step = (uint64_t)( ((int64_t)INT32_MAX - last_times_sample + 1)
+ + this_times_sample ) ;
+ else
+ /* this sample -ve and last sample +ve => -ve wrap round */
+ /* this sample -ve and last sample -ve => time gone backwards */
+ /* (which appears as a HUGE step forwards). */
+ step = (uint64_t)( ((int64_t)INT32_MAX - last_times_sample + 1)
+ - ((int64_t)INT32_MIN - this_times_sample) ) ;
+ } ;
+
+ /* TODO: better error messaging for large clock jumps. */
+ if (step > step_limit)
+ zabort("Sudden large monotonic clock jump") ;
+
+ /* Advance the monotonic clock in sysconf(_SC_CLK_TCK) units. */
+ monotonic += step ;
+
+ /* Remember what we got, for next time. */
+ last_times_sample = this_times_sample ;
+
+ /* Scale to qtime_t units. */
+ if (times_scale_r == 0)
+ return monotonic * times_scale_q ;
+ else
+ return (monotonic * times_scale_q) +
+ ((monotonic * times_scale_r) / times_clk_tcks) ;
+} ;
diff --git a/lib/qtime.h b/lib/qtime.h
new file mode 100644
index 00000000..35e1a51b
--- /dev/null
+++ b/lib/qtime.h
@@ -0,0 +1,307 @@
+/* Quagga realtime and monotonic clock handling -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_QTIME_H
+#define _ZEBRA_QTIME_H
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "zassert.h"
+#include "config.h"
+
+#ifndef Inline
+#define Inline static inline
+#endif
+
+/*==============================================================================
+ * qtime_t -- signed 64-bit integer.
+ *
+ * The various time functions work in terms of the structures:
+ *
+ * timespec -- tv_secs seconds
+ * tv_nsecs nano-seconds
+ *
+ * timeval -- tv_secs seconds
+ * tv_usecs micro-seconds
+ *
+ * Given a 64-bit integer it is much easier to do operations on a 64 bit
+ * (signed) nano-second value. That gives > 34 bits for the seconds count,
+ * and counts from zero to > 290 years.
+ */
+
+typedef int64_t qtime_t ;
+
+typedef qtime_t qtime_real_t ; /* qtime_t value, realtime time-base */
+typedef qtime_t qtime_mono_t ; /* qtime_t value, monotonic time-base */
+
+typedef qtime_t qtime_tod_t ; /* qtime_t value, timeofday time-base... */
+ /* ...just in case != CLOCK_REALTIME ! */
+
+/* A qtime_t second 123456789 -- nano-seconds */
+#define QTIME_SECOND 1000000000
+#define TIMESPEC_SECOND 1000000000
+#define TIMEVAL_SECOND 1000000
+
+/* Macro to convert time in seconds to a qtime_t */
+/* Note that the time to convert may be a float. */
+#define QTIME(s) ((qtime_t)((s) * (qtime_t)QTIME_SECOND))
+
+Inline qtime_t
+timespec2qtime(struct timespec* p_ts) ;
+
+Inline qtime_t
+timeval2qtime(struct timeval* p_tv) ;
+
+Inline struct timespec*
+qtime2timespec(struct timespec* p_ts, qtime_t qt) ;
+
+Inline struct timeval*
+qtime2timeval(struct timeval* p_tv, qtime_t qt) ;
+
+/*==============================================================================
+ * Clocks.
+ *
+ * Here is support for:
+ *
+ * * System Clock
+ *
+ * This can be read using either clock_gettime(CLOCK_REALTIME, &ts) or
+ * gettimeofday(&tv, NULL) -- which (are believed to) return the same clock,
+ * but in different units.
+ *
+ * * Monotonic Clock
+ *
+ * Using clock_gettime(CLOCK_MONOTONIC, &ts) if it is available, otherwise
+ * a manufactured equivalent using times() -- see qt_craft_monotonic().
+ */
+
+Inline qtime_real_t
+qt_get_realtime(void) ; /* clock_gettime(CLOCK_REALTIME, ...) */
+
+Inline qtime_mono_t
+qt_add_realtime(qtime_t interval) ; /* qt_get_realtime() + interval */
+
+Inline qtime_mono_t
+qt_get_monotonic(void) ; /* clock_gettime(CLOCK_MONOTONIC, ...) */
+ /* OR equivalent using times() */
+Inline qtime_mono_t
+qt_add_monotonic(qtime_t interval) ; /* qt_get_monotonic() + interval */
+
+Inline qtime_mono_t /* monotonic time from CLOCK_REALTIME */
+qt_realtime2monotonic(qtime_real_t realtime) ;
+Inline qtime_real_t /* CLOCK_REALTIME from monotonic time */
+qt_monotonic2realtime(qtime_mono_t monotonic) ;
+
+/* Function to manufacture a monotonic clock. */
+extern qtime_mono_t
+qt_craft_monotonic(void) ;
+
+/* These are provided just in case gettimeofday() != CLOCK_REALTIME */
+Inline qtime_tod_t
+qt_get_timeofday(void) ; /* gettimeofday(&tv, NULL) */
+
+Inline qtime_tod_t
+qt_add_timeofday(qtime_t interval) ; /* qt_get_timeofday() + interval */
+
+Inline qtime_mono_t /* monotonic time from timeofday */
+qt_timeofday2monotonic(qtime_tod_t timeofday) ;
+Inline qtime_tod_t /* timeofday from monotonic time */
+qt_monotonic2timeofday(qtime_mono_t monotonic) ;
+
+/*==============================================================================
+ * Inline conversion functions
+ */
+
+/* Convert timespec to qtime_t
+ *
+ * Returns qtime_t value.
+ */
+Inline qtime_t
+timespec2qtime(struct timespec* p_ts)
+{
+ return QTIME(p_ts->tv_sec) + p_ts->tv_nsec ;
+ confirm(QTIME_SECOND == TIMESPEC_SECOND) ;
+} ;
+
+/* Convert timeval to qtime_t
+ *
+ * Returns qtime_t value.
+ */
+Inline qtime_t
+timeval2qtime(struct timeval* p_tv)
+{
+ return QTIME(p_tv->tv_sec) + (p_tv->tv_usec * 1000) ;
+ confirm(QTIME_SECOND == TIMEVAL_SECOND * 1000) ;
+} ;
+
+/* Convert qtime_t to timespec
+ *
+ * Takes address of struct timespec and returns that address.
+ */
+Inline struct timespec*
+qtime2timespec(struct timespec* p_ts, qtime_t qt)
+{
+ lldiv_t imd = lldiv(qt, QTIME_SECOND) ;
+ confirm(sizeof(long long) >= sizeof(qtime_t)) ;
+
+ p_ts->tv_sec = imd.quot ;
+ p_ts->tv_nsec = imd.rem ;
+ confirm(TIMESPEC_SECOND == QTIME_SECOND) ;
+
+ return p_ts ;
+} ;
+
+/* Convert timespec to qtime_t
+ *
+ * Takes address of struct timespec and returns that address.
+ */
+Inline struct timeval*
+qtime2timeval(struct timeval* p_tv, qtime_t qt)
+{
+ lldiv_t imd = lldiv(qt, QTIME_SECOND) ;
+ confirm(sizeof(long long) >= sizeof(qtime_t)) ;
+
+ p_tv->tv_sec = imd.quot ;
+ p_tv->tv_usec = imd.rem / 1000 ;
+ confirm(TIMEVAL_SECOND * 1000 == QTIME_SECOND) ;
+
+ return p_tv ;
+} ;
+
+/*==============================================================================
+ * Inline Clock Functions.
+ */
+
+/* Read given clock & return a qtime_t value.
+ *
+ * While possibility of error is essentially theoretical, must treat it as a
+ * FATAL error -- cannot continue with broken time value !
+ */
+
+Inline qtime_t
+qt_clock_gettime(clockid_t clock_id)
+{
+ struct timespec ts ;
+
+ if (clock_gettime(clock_id, &ts) != 0)
+ zabort_errno("clock_gettime failed") ;
+
+ return timespec2qtime(&ts) ;
+} ;
+
+/* clock_gettime(CLOCK_REALTIME, ...) -- returning qtime_t value
+ *
+ * While possibility of error is essentially theoretical, must treat it as a
+ * FATAL error -- cannot continue with broken time value !
+ */
+Inline qtime_real_t
+qt_get_realtime(void)
+{
+ return qt_clock_gettime(CLOCK_REALTIME) ;
+} ;
+
+/* qt_get_realtime() + interval
+ */
+Inline qtime_real_t
+qt_add_realtime(qtime_t interval)
+{
+ return qt_get_realtime() + interval;
+} ;
+
+/* clock_gettime(CLOCK_MONOTONIC, ...) OR qt_craft_monotonic()
+ * -- returning qtime_t value
+ *
+ * While possibility of error is essentially theoretical, must treat it as a
+ * FATAL error -- cannot continue with broken time value !
+ */
+Inline qtime_mono_t
+qt_get_monotonic(void)
+{
+#ifdef HAVE_CLOCK_MONOTONIC
+ return qt_clock_gettime(CLOCK_MONOTONIC) ;
+#else
+ return qt_craft_monotonic() ;
+#endif
+} ;
+
+/* qt_get_monotonic() + interval
+ */
+Inline qtime_mono_t
+qt_add_monotonic(qtime_t interval)
+{
+ return qt_get_monotonic() + interval;
+} ;
+
+/* gettimeofday(&tv, NULL) -- returning qtime_t value
+ */
+Inline qtime_tod_t
+qt_get_timeofday(void)
+{
+ struct timeval tv ;
+ gettimeofday(&tv, NULL) ;
+ return timeval2qtime(&tv) ;
+}
+
+/* qt_get_timeofday() + interval
+ */
+Inline qtime_tod_t
+qt_add_timeofday(qtime_t interval)
+{
+ return qt_get_timeofday() + interval;
+} ;
+
+/*==============================================================================
+ * Conversion between realtime/timeofday and monotonic
+ */
+
+/* Convert a CLOCK_REALTIME time to our local monotonic time. */
+Inline qtime_mono_t
+qt_realtime2monotonic(qtime_real_t realtime)
+{
+ return qt_get_monotonic() + (realtime - qt_get_realtime()) ;
+} ;
+
+/* Convert a local monotonic time to CLOCK_REALTIME time. */
+Inline qtime_real_t
+qt_monotonic2realtime(qtime_mono_t monotonic)
+{
+ return qt_get_realtime() + (monotonic - qt_get_monotonic()) ;
+} ;
+
+/* Convert a gettimeofday() time to our local monotonic time. */
+Inline qtime_mono_t
+qt_timeofday2monotonic(qtime_tod_t timeofday)
+{
+ return qt_get_monotonic() + (timeofday - qt_get_timeofday()) ;
+} ;
+
+/* Convert a local monotonic time to gettimeofday() time. */
+Inline qtime_tod_t
+qt_monotonic2timeofday(qtime_mono_t monotonic)
+{
+ return qt_get_timeofday() + (monotonic - qt_get_monotonic()) ;
+} ;
+
+#endif /* _ZEBRA_QTIME_H */
diff --git a/lib/qtimers.c b/lib/qtimers.c
new file mode 100644
index 00000000..d4aff6ec
--- /dev/null
+++ b/lib/qtimers.c
@@ -0,0 +1,336 @@
+/* Quagga timers support -- functions
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+#include "zassert.h"
+#include "qtimers.h"
+#include "memory.h"
+#include "heap.h"
+
+/*==============================================================================
+ * Quagga Timers -- qtimer_xxxx
+ *
+ * Here and in qtimers.h is a data structure for managing multiple timers
+ * each with an action to be executed when the timer expires.
+ *
+ * The qtime_pile structure manages a "pile" of qtimer structures which are
+ * waiting for the right time to go off.
+ *
+ * NB: it is ASSUMED that a qtime_pile will be private to the thread in which
+ * it is created and used.
+ *
+ * There is NO mutex handling here.
+ *
+ * Timers are triggered by calling qtimer_dispatch_next(). This is given the
+ * current qtimer time (see below), and it dispatches the first timer whose
+ * time has come (or been passed). Dispatching a timer means calling its
+ * action function (see below). Each call of qtimer_dispatch_next() triggers
+ * at most one timer.
+ *
+ * Time Base
+ * ---------
+ *
+ * The time base for qtimers is the monotonic time provided in qtime.c/.h.
+ *
+ * Action Functions
+ * ----------------
+ *
+ * There is a separate action function for each timer.
+ *
+ * When the action function is called it is passed the qtimer structure, the
+ * timer_info pointer from that structure and the time which triggered the
+ * timer (which may, or may not, be the current qtimer time).
+ *
+ * During an action function timers may be set/unset, actions changed, and so
+ * on... there are no restrictions EXCEPT that the qtimer structure may NOT be
+ * freed.
+ */
+
+static int
+qtimer_cmp(qtimer* a, qtimer* b) /* the heap discipline */
+{
+ if ((**a).time < (**b).time)
+ return -1 ;
+ if ((**a).time > (**b).time)
+ return +1 ;
+ return 0 ;
+} ;
+
+/*==============================================================================
+ * qtimer_pile handling
+ */
+
+/* Initialise a timer pile -- allocating it if required.
+ *
+ * Returns the qtimer_pile.
+ */
+qtimer_pile
+qtimer_pile_init_new(qtimer_pile qtp)
+{
+ if (qtp == NULL)
+ qtp = XCALLOC(MTYPE_QTIMER_PILE, sizeof(struct qtimer_pile)) ;
+ else
+ memset(qtp, 0, sizeof(struct qtimer_pile)) ;
+
+ /* Zeroising has initialised:
+ *
+ * timers -- invalid heap -- need to properly initialise
+ *
+ * unset_pending -- NULL -- nothing pending
+ */
+
+ /* Eclipse flags offsetof(struct qtimer, backlink) as a syntax error :-( */
+ typedef struct qtimer qtimer_t ;
+
+ heap_init_new_backlinked(&qtp->timers, 0, (heap_cmp*)qtimer_cmp,
+ offsetof(qtimer_t, backlink)) ;
+ return qtp ;
+} ;
+
+/* Get the timer time for the first timer due to go off in the given pile.
+ *
+ * The caller must provide a maximum acceptable time. If the qtimer pile is
+ * empty, or the top entry times out after the maximum time, then the maximum
+ * is returned.
+ */
+qtime_mono_t
+qtimer_pile_top_time(qtimer_pile qtp, qtime_mono_t max_time)
+{
+ qtimer qtr = heap_top_item(&qtp->timers) ;
+
+ if ((qtr == NULL) || (qtr->time >= max_time))
+ return max_time ;
+ else
+ return qtr->time ;
+} ;
+
+/* Dispatch the next timer whose time is <= the given "upto" time.
+ *
+ * The upto time must be a qtimer time (!) -- see qtimer_time_now().
+ *
+ * The upto argument allows the caller to get a qtimer_time_now() value, and
+ * then process all timers upto that time.
+ *
+ * Returns true <=> dispatched a timer, and there may be more to do.
+ * false <=> nothing to do (and nothing done).
+ */
+int
+qtimer_pile_dispatch_next(qtimer_pile qtp, qtime_mono_t upto)
+{
+ qtimer qtr ;
+
+ qtr = heap_top_item(&qtp->timers) ;
+ if ((qtr != NULL) && (qtr->time <= upto))
+ {
+ qtr->state = qtr_state_unset_pending ;
+
+ qtr->action(qtr, qtr->timer_info, upto) ;
+
+ if (qtr->state == qtr_state_unset_pending)
+ qtimer_unset(qtr) ;
+
+ return 1 ;
+ }
+ else
+ return 0 ;
+} ;
+
+/* Ream out (another) item from qtimer_pile.
+ *
+ * If pile is empty, release the qtimer_pile structure, if required.
+ *
+ * See: #define qtimer_pile_ream_free(qtp)
+ * #define qtimer_pile_ream_keep(qtp)
+ *
+ * Useful for emptying out and discarding a pile of timers:
+ *
+ * while ((p_qtr = qtimer_pile_ream_free(qtp)))
+ * ... do what's required to release the item p_qtr
+ *
+ * Returns NULL when timer pile is empty (and has been released, if required).
+ *
+ * If the timer pile is not released, it may be reused without reinitialisation.
+ *
+ * NB: once reaming has started, the timer pile MUST NOT be used for anything,
+ * and the process MUST be run to completion.
+ */
+qtimer
+qtimer_pile_ream(qtimer_pile qtp, int free_structure)
+{
+ qtimer qtr ;
+
+ qtr = heap_ream_keep(&qtp->timers) ; /* ream, keeping the heap structure */
+ if (qtr != NULL)
+ qtr->state = qtr_state_inactive ; /* has been removed from pile */
+ else
+ if (free_structure) /* pile is empty, may now free it */
+ XFREE(MTYPE_QTIMER_PILE, qtp) ;
+
+ return qtr ;
+} ;
+
+/*==============================================================================
+ * qtimer handling
+ */
+
+/* Initialise qtimer structure -- allocating one if required.
+ *
+ * Associates qtimer with the given pile of timers, and sets up the action and
+ * the timer_info.
+ *
+ * Once initialised, the timer may be set.
+ *
+ * Returns the qtimer.
+ */
+qtimer
+qtimer_init_new(qtimer qtr, qtimer_pile qtp,
+ qtimer_action* action, void* timer_info)
+{
+ if (qtr == NULL)
+ qtr = XCALLOC(MTYPE_QTIMER, sizeof(struct qtimer)) ;
+ else
+ memset(qtr, 0, sizeof(struct qtimer)) ;
+
+ /* Zeroising has initialised:
+ *
+ * pile -- NULL -- not in any pile (yet)
+ * backlink -- unset
+ *
+ * state -- not active
+ *
+ * time -- unset
+ * action -- NULL -- no action set (yet)
+ * timer_info -- NULL -- no timer info set (yet)
+ */
+
+ confirm(qtr_state_inactive == 0) ;
+
+ qtr->pile = qtp ;
+ qtr->action = action ;
+ qtr->timer_info = timer_info ;
+
+ return qtr ;
+} ;
+
+/* Free given timer.
+ *
+ * Unsets it first if it is active.
+ *
+ * The timer MAY NOT be currently the subject of qtimer_pile_dispatch_next().
+ */
+void
+qtimer_free(qtimer qtr)
+{
+ assert(qtr->state != qtr_state_unset_pending) ;
+
+ if (qtr->state != qtr_state_inactive)
+ qtimer_unset(qtr) ;
+
+ XFREE(MTYPE_QTIMER, qtr) ;
+} ;
+
+/* Set pile in which given timer belongs.
+ *
+ * Unsets the timer if active in another pile.
+ * (Does nothing if active in the "new" pile.)
+ */
+void
+qtimer_set_pile(qtimer qtr, qtimer_pile qtp)
+{
+ if (qtr_is_active(qtr) && (qtr->pile != qtp))
+ qtimer_unset(qtr) ;
+ qtr->pile = qtp ;
+}
+
+/* Set action for given timer.
+ */
+void
+qtimer_set_action(qtimer qtr, qtimer_action* action)
+{
+ qtr->action = action ;
+} ;
+
+/* Set timer_info for given timer.
+ */
+void
+qtimer_set_info(qtimer qtr, void* timer_info)
+{
+ qtr->timer_info = timer_info ;
+} ;
+
+/* Set given timer.
+ *
+ * Setting a -ve time => qtimer_unset.
+ *
+ * Sets any given action -- if the action given is NULL, retains previously set
+ * action.
+ *
+ * If the timer is already active, sets the new time & updates pile.
+ *
+ * Otherwise, sets the time and adds to pile -- making timer active.
+ *
+ * It is an error to set a timer which has a NULL action.
+ */
+void
+qtimer_set(qtimer qtr, qtime_mono_t when, qtimer_action* action)
+{
+ qtimer_pile qtp ;
+
+ if (when < 0)
+ return qtimer_unset(qtr) ;
+
+ qtp = qtr->pile ;
+ dassert(qtp != NULL) ;
+
+ qtr->time = when ;
+
+ if (qtr_is_active(qtr))
+ heap_update_item(&qtp->timers, qtr) ; /* update in heap */
+ else
+ heap_push_item(&qtp->timers, qtr) ; /* add to heap */
+
+ qtr->state = qtr_state_active ; /* overrides any unset pending */
+
+ if (action != NULL)
+ qtr->action = action ;
+ else
+ dassert(qtr->action != NULL) ;
+} ;
+
+/* Unset given timer
+ *
+ * If the timer is active, removes from pile and sets inactive.
+ */
+void
+qtimer_unset(qtimer qtr)
+{
+ if (qtr_is_active(qtr))
+ {
+ qtimer_pile qtp = qtr->pile ;
+ dassert(qtp != NULL) ;
+
+ heap_delete_item(&qtp->timers, qtr) ;
+
+ qtr->state = qtr_state_inactive ; /* overrides any unset pending */
+ } ;
+} ;
diff --git a/lib/qtimers.h b/lib/qtimers.h
new file mode 100644
index 00000000..3a774b38
--- /dev/null
+++ b/lib/qtimers.h
@@ -0,0 +1,144 @@
+/* Quagga timers support -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_QTIMERS_H
+#define _ZEBRA_QTIMERS_H
+
+#include "zassert.h"
+#include "qtime.h"
+#include "heap.h"
+
+#ifndef Inline
+#define Inline static inline
+#endif
+
+/*==============================================================================
+ * Quagga Timers -- qtimer_xxxx
+ *
+ * Here and in qtimers.c is a data structure for managing multiple timers
+ * each with an action to be executed when the timer expires.
+ */
+
+/*==============================================================================
+ * Data Structures.
+ */
+
+typedef struct qtimer* qtimer ;
+typedef struct qtimer_pile* qtimer_pile ;
+
+typedef void (qtimer_action)(qtimer qtr, void* timer_info, qtime_mono_t when) ;
+
+enum qtimer_state {
+ qtr_state_inactive = 0,
+ qtr_state_active = 1, /* timer is active in its pile */
+ qtr_state_unset_pending = 3 /* timer is active, but unset is pending */
+} ;
+
+#define qtr_is_active(qtr) ((qtr)->state != qtr_state_inactive)
+
+typedef enum qtimer_state qtimer_state_t ;
+
+struct qtimer
+{
+ qtimer_pile pile ;
+ heap_backlink_t backlink ;
+
+ qtimer_state_t state ;
+
+ qtime_mono_t time ;
+ qtimer_action* action ;
+ void* timer_info ;
+} ;
+
+struct qtimer_pile
+{
+ struct heap timers ;
+} ;
+
+/*==============================================================================
+ * Functions
+ */
+
+qtimer_pile
+qtimer_pile_init_new(qtimer_pile qtp) ;
+
+int
+qtimer_pile_dispatch_next(qtimer_pile qtp, qtime_mono_t upto) ;
+
+qtime_mono_t
+qtimer_pile_top_time(qtimer_pile qtp, qtime_mono_t max_time) ;
+
+qtimer
+qtimer_pile_ream(qtimer_pile qtp, int free_structure) ;
+
+/* Ream out qtimer pile and free the qtimer structure. */
+#define qtimer_pile_ream_free(qtp) qtimer_pile_ream(qtp, 1)
+/* Ream out qtimer pile but keep the qtimer structure. */
+#define qtimer_pile_ream_keep(qtp) qtimer_pile_ream(qtp, 0)
+
+qtimer
+qtimer_init_new(qtimer qtr, qtimer_pile qtp,
+ qtimer_action* action, void* timer_info) ;
+void
+qtimer_set_pile(qtimer qtr, qtimer_pile qtp) ;
+
+void
+qtimer_set_action(qtimer qtr, qtimer_action* action) ;
+
+void
+qtimer_set_info(qtimer qtr, void* timer_info) ;
+
+void
+qtimer_free(qtimer qtr) ;
+
+void
+qtimer_set(qtimer qtr, qtime_mono_t when, qtimer_action* action) ;
+
+void
+qtimer_unset(qtimer qtr) ;
+
+Inline void
+qtimer_add(qtimer qtr, qtime_t interval, qtimer_action* action) ;
+
+Inline qtime_mono_t
+qtimer_get(qtimer qtr) ;
+
+/*==============================================================================
+ * Inline functions
+ */
+
+/* Set given timer to given time later than *its* current time.
+ */
+Inline void
+qtimer_add(qtimer qtr, qtime_t interval, qtimer_action* action)
+{
+ qtimer_set(qtr, qtimer_get(qtr) + interval, action);
+} ;
+
+/* Get the given timer's time.
+ */
+Inline qtime_mono_t
+qtimer_get(qtimer qtr)
+{
+ return qtr->time ;
+} ;
+
+#endif /* _ZEBRA_QTIMERS_H */
diff --git a/lib/routemap.c b/lib/routemap.c
index 4f4e6d62..2dfa5a46 100644
--- a/lib/routemap.c
+++ b/lib/routemap.c
@@ -207,7 +207,7 @@ vty_show_route_map_entry (struct vty *vty, struct route_map *map)
/* Print the name of the protocol */
if (zlog_default)
- vty_out (vty, "%s:%s", zlog_proto_names[zlog_default->protocol],
+ vty_out (vty, "%s:%s", zlog_get_proto_name(NULL),
VTY_NEWLINE);
for (index = map->head; index; index = index->next)
diff --git a/lib/symtab.c b/lib/symtab.c
new file mode 100644
index 00000000..9a833a14
--- /dev/null
+++ b/lib/symtab.c
@@ -0,0 +1,1181 @@
+/* Symbol Table data structure -- functions
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <zebra.h>
+#include <stddef.h>
+
+#include "symtab.h"
+#include "memory.h"
+
+/* A symbol table maps symbol "names" to symbol values and, for each symbol,
+ * has two ways of keeping track of references to the symbol.
+ *
+ * The symbol name can be an arbitrary collection of bytes, or a string. All
+ * names in a symbol table are unique.
+ *
+ * The symbol value is a void* -- whose contents are no concern of this code.
+ *
+ * A symbol table comprises:
+ *
+ * * symbol table structure -- containing all "red-tape"
+ * * array of chain-bases -- for the hash table
+ * * symbol entries -- each containing name and value of a symbol
+ *
+ * The symbol table structure may be statically allocated, embedded in another
+ * structure, or allocated dynamically. In any case the symbol table operations
+ * require the address of the symbol table structure -- see typedef for
+ * symbol_table.
+ *
+ * A symbol table may point to its "parent" -- which could be an enclosing
+ * structure, or any other higher level data. The symbol table code does not
+ * use or need this -- it is for the convenience of the caller.
+ *
+ * A symbol table structure which is zeroised is implicitly an empty symbol
+ * table, using the default symbol name type -- a null terminated string.
+ *
+ * Each Symbol Table requires a hash function, which takes a pointer to
+ * whatever and returns a 32-bit hash value and a canonical form of the
+ * symbol "name". When a new symbol is created the canonical form of the name
+ * is copied to the symbol entry.
+ *
+ * The array of chain-bases is dynamically allocated and will grow to maintain
+ * an approximate given maximum number of symbols per chain base. This density
+ * is set when the symbol table is initialised. The array does not
+ * automatically reduce in size.
+ *
+ * The number of chain bases is always odd. The hash function returns a 32-bit
+ * unsigned value, which is mapped to the chain bases modulo their number.
+ * Since that is odd, it is co-prime with the hash, so contributes to the hash
+ * process.
+ *
+ * Each symbol in the table has a dynamically allocated entry, which includes:
+ *
+ * * symbol value -- void*
+ * * symbol name
+ * * count of references
+ * * list of references
+ *
+ * Symbols may have the value NULL. Deleting a symbol is not the same as
+ * setting its value to NULL. If the value is set NULL and later set to
+ * something else, all references to the symbol will see the new value. If the
+ * symbol is deleted, all references will see its value set to NULL, but if a
+ * new symbol with the same name is later created, all references to the old
+ * symbol are unchanged, and continue to see the (orphan) NULL.
+ *
+ * Keeping track of references is important because it ensures that symbols
+ * can be deleted without leaving dangling references. When a symbol is
+ * deleted, it is removed from the symbol table and its value is set to NULL.
+ * If there are no references to the symbol, the symbol entry is released.
+ * If there are references to the symbol, it is preserved (as an orphan) until
+ * all references are unset. The value of an orphaned symbol should not be
+ * changed.
+ *
+ * There are two, parallel mechanisms for keeping track of references to a
+ * symbol:
+ *
+ * 1. reference count -- this is for when all that is required is a pointer
+ * to the symbol, ie:
+ *
+ * * ptr_to_symbol = symbol_inc_ref(sym) -- set reference & count up
+ * * ptr_to_symbol = symbol_dec_ref(sym) -- unset reference & count down
+ *
+ * 2. list of references -- this is for when it is useful to visit all
+ * references to a given symbol, for example when the value of the symbol
+ * is set and all users of the symbol need to adjust to the new state.
+ *
+ * A symbol reference contains a pointer to the symbol, and lives on the
+ * list of references to the symbol. When the value is set, a call-back
+ * associated with the table is called (if it is set). That call-back may
+ * walk the list of references to the symbol. Each reference contains a
+ * pointer and a pointer/long int value, which may be used to identify the
+ * reference.
+ *
+ * A symbol reference may be statically allocated, embedded in another
+ * structure, or allocated dynamically. In any case the symbol reference
+ * operations require the address of the symbol reference structure -- see
+ * typedef for symbol_ref.
+ *
+ * Symbol references are the responsibility of of the owner of the reference,
+ * who must look after setting, unsetting and (if required) releasing them.
+ *
+ * It may seem profligate to have two mechanisms. However, it is simpler than
+ * having two types of symbol, or two types of symbol table. It may also be
+ * useful to have both simple references and references for dealing with
+ * value changes. Suppose, for instance, that the value of a symbol has a
+ * pointer to the symbol -- so that the value of the symbol can refer back to
+ * its name, for example. This requires only a simple reference, even if
+ * more generally a list of references is required.
+ *
+ * A symbol table can be walked to visit all symbols, and there is support for
+ * extracting a vector of all symbols which satisfy a given criterion.
+ */
+
+static void symbol_extend_bases(symbol_table table) ;
+static void symbol_free(symbol sym) ;
+
+/* Return true iff there are no references to the given symbol */
+inline static int
+symbol_no_references(symbol sym)
+{
+ return (sym->ref_count == 0) && (sym->ref_list == NULL) ;
+} ;
+
+/* If symbol is an orphan with no references/bookmarks, free it.
+ *
+ * NB: if symbol is an orphan, then it implicitly has a NULL value, because
+ * to become an orphan it must have been deleted, which unsets the value.
+ */
+inline static void
+symbol_free_if_redundant(symbol sym)
+{
+ if ((sym->table == NULL) && symbol_no_references(sym))
+ symbol_free(sym) ;
+} ;
+
+/* Return chain base for given hash value. */
+inline static symbol*
+symbol_base(symbol_table table, u_int32_t hash)
+{
+ return &table->bases[hash % table->base_count] ;
+} ;
+
+/* Initialise a new symbol table -- allocate if required.
+ *
+ * table -- address of table to initialise. NULL -> allocate.
+ * NB: if allocated, it is the caller's responsibility to free.
+ *
+ * parent -- address of some parent or other higher level data structure.
+ * This is not used by the symbol table code and may be NULL if
+ * the caller has no use for it.
+ *
+ * bases -- number of list bases to start the symbol table at.
+ * Symbol table grows as required, but can set initial size if
+ * have some expectations and wish to avoid growth steps.
+ *
+ * density -- %-age of entries/bases. 0 => use default.
+ *
+ * hash_function
+ * -- function to fill in symbol_hash from a given "name".
+ * see: description of struct symbol_hash and default function,
+ * symbol_hash_string, below.
+ * NULL => the names in this symbol table are null terminated
+ * strings, so use symbol_hash_string.
+ *
+ * value_call_back
+ * -- function to be called when the symbol value is set or unset.
+ * (Or the symbol is about to be deleted -- which starts by
+ * unsetting it).
+ *
+ * The function is passed the new state of the symbol and its
+ * previous value.
+ *
+ * The value of the symbol is a pointer to some data. If the
+ * data changes symbol_set_value() must be called if its
+ * address changes and may be called even if its address doesn't
+ * change.
+ *
+ * In any event, value_call_back is called when symbol_set_value()
+ * is called -- except where the symbol is being set to NULL and
+ * the value is already NULL.
+ *
+ * During the call-back the symbol may be set again or unset,
+ * which will cause the call-back to be called inside itself.
+ * Also, references may be set or unset.
+ *
+ * In the call-back the reference list may be walked to signal
+ * to all holders of the reference that something has changed.
+ *
+ * NB: A completely zeroized symbol_table structure is a valid, empty
+ * symbol table. The first time it is used it will be set up to default
+ * state -- in particular: symbol names are null terminated strings (a
+ * state which *cannot* then be changed).
+ *
+ * NB: when this is used to re-initialising an existing symbol table structure,
+ * any existing chain base array, symbols and symbol references are simply
+ * discarded -- which will leak memory and is probably a mistake.
+ */
+symbol_table
+symbol_table_init_new(symbol_table table,
+ void* parent,
+ unsigned int base_count,
+ unsigned int density,
+ symbol_hash_function* hash_function,
+ symbol_call_back_function* value_call_back)
+{
+ assert(base_count <= SYMBOL_TABLE_BASES_MAX) ;
+
+ if (table == NULL)
+ table = XCALLOC(MTYPE_SYMBOL_TABLE, sizeof (struct symbol_table)) ;
+
+ table->parent = parent ;
+
+ table->bases = NULL ; /* Allocated when required */
+ table->base_count = base_count ;
+
+ table->entry_count = 0 ;
+ table->extend_thresh = density ; /* Fixed up when required */
+
+ table->hash_function = hash_function ;
+ symbol_table_set_value_call_back(table, value_call_back) ;
+
+ return table ;
+} ;
+
+/* Set "parent" of symbol table. */
+void
+symbol_table_set_parent(symbol_table table, void* parent)
+{
+ table->parent = parent ;
+} ;
+
+/* Get "parent" of symbol table. */
+void*
+symbol_table_get_parent(symbol_table table)
+{
+ return table->parent ;
+} ;
+
+/* Set the value_call_back */
+void
+symbol_table_set_value_call_back(symbol_table table,
+ symbol_call_back_function* value_call_back)
+{
+ table->value_call_back = value_call_back ;
+} ;
+
+/* Create and set new chain bases and threshold for next extension. */
+/* */
+/* Ensures that the base count is at least the minimum and is odd, */
+/* and returns the value set. */
+static unsigned int
+symbol_table_new_bases(symbol_table table,
+ unsigned int new_base_count, float density)
+{
+ if (new_base_count < SYMBOL_TABLE_BASES_MIN)
+ new_base_count = SYMBOL_TABLE_BASES_MIN ;
+ new_base_count |= 1 ;
+
+ table->bases = XCALLOC(MTYPE_SYMBOL_BASES, new_base_count * sizeof(symbol)) ;
+ table->extend_thresh = new_base_count * density ;
+ return table->base_count = new_base_count ;
+} ;
+
+/* Setup symbol table body for use.
+ *
+ * Used for "lazy" allocation of chain bases and allows symbol_lookup
+ * to operate on a completely zeroized symbol_table structure.
+ */
+static void
+symbol_table_setup(symbol_table table)
+{
+ float density ;
+
+ /* If density was set explicitly, extend_thresh entry is a %age. */
+
+ if (table->extend_thresh != 0)
+ density = (float)table->extend_thresh / (float)100 ;
+ else
+ density = (float)2 ; /* Default density */
+
+ /* Initialise the chain bases -- enforces minimum base_count and odd-ness */
+ symbol_table_new_bases(table, table->base_count, density) ;
+
+ /* make default hash_function explicit. */
+ if (table->hash_function == NULL)
+ table->hash_function = (symbol_hash_function*)symbol_hash_string ;
+} ;
+
+/* Reset symbol table.
+ *
+ * Free the symbol table body, and free the symbol table structure or reset it.
+ *
+ * Return NULL if frees symbol table structure, otherwise the address of same.
+ *
+ * NB: must only be done when the table is empty -- see assertion !
+ */
+symbol_table
+symbol_table_reset(symbol_table table, int free_structure)
+{
+ if (table== NULL)
+ return NULL ; /* allow for already freed table */
+
+ assert(table->entry_count == 0) ;
+
+ if (table->bases)
+ XFREE(MTYPE_SYMBOL_BASES, table->bases);
+
+ if (free_structure)
+ {
+ XFREE(MTYPE_VECTOR, table) ;
+ return NULL ;
+ }
+ else
+ return memset(table, 0, sizeof(struct symbol_table)) ;
+} ;
+
+/* Remove symbol from its symbol table (if any). */
+
+static void
+symbol_remove(symbol sym)
+{
+ symbol_table table ;
+ symbol* base ;
+ symbol prev ;
+
+ table = sym->table ;
+ if (table != NULL) /* Deleted symbols have no parent table. */
+ {
+ assert(table->entry_count != 0) ;
+
+ base = symbol_base(table, sym->hash) ;
+ if (*base == sym)
+ *base = sym->next ;
+ else
+ {
+ prev = *base ;
+ while (prev->next != sym)
+ prev = prev->next ;
+ prev->next = sym->next ;
+ } ;
+
+ sym->table = NULL ; /* Symbol is now an orphan. */
+ --table->entry_count ;
+ } ;
+} ;
+
+/* Free symbol, removing it from the symbol table.
+ *
+ * NB: the value and all references MUST already have been unset, because:
+ *
+ * * any value may well need to be released, and have no idea how to do
+ * that here.
+ *
+ * * similarly, references may need to be released and should not, in
+ * any case, be left dangling.
+ */
+static void
+symbol_free(symbol sym)
+{
+ assert((sym->value == NULL) && symbol_no_references(sym)) ;
+
+ symbol_remove(sym) ; /* Remove from table, if any. */
+
+ XFREE(MTYPE_SYMBOL, sym) ;
+} ;
+
+/* Ream out symbols.
+ *
+ * Delete symbols -- but do not invoke the value_call_back.
+ *
+ * When the table is (or becomes) empty, the chain bases are freed, and the
+ * structure freed or reset (depending on the free_structure argument).
+ *
+ * This is intended for use when the symbol table is being destroyed, and all
+ * references have been, or will be unset.
+ *
+ * Returns the value of the next non-NULL symbol (if any). So may be used, for
+ * example:
+ *
+ * xxxx* val ;
+ * ...
+ * while ((val = symbol_table_ream(table, free_structure)) != NULL)
+ * {
+ * ... do what's required to release the value ...
+ * }
+ *
+ * Noting that the symbol may already have been released when its value is
+ * returned. (If the symbol is required when the value is released, then the
+ * value should hold a simple reference to the symbol.)
+ *
+ * Returns NULL when the table is empty.
+ *
+ * Symbols which have one or more references when they are deleted are left as
+ * orphans, which will be freed when all their references are unset.
+ *
+ * NB: do NOT attempt to do anything else with the symbol table once reaming
+ * has started.
+ *
+ * NB: it is the caller's responsibility to unset all references and release
+ * any that need to be released -- either before or after this operation.
+ */
+void*
+symbol_table_ream(symbol_table table, int free_structure)
+{
+ void* value ;
+ symbol sym ;
+ unsigned int i ;
+
+ i = table->base_count ;
+
+ while (i--)
+ {
+ while ((sym = table->bases[i]) != NULL)
+ {
+ assert(table->entry_count != 0) ;
+
+ /* the following is effectively symbol_delete, but avoids the */
+ /* value_call_back and returns only if the value is not NULL. */
+
+ table->bases[i] = sym->next ; /* remove from table */
+ --table->entry_count ; /* count down */
+
+ sym->table = NULL ; /* orphan symbol */
+ value = sym->value ; /* pick up value. */
+ sym->value = NULL ; /* and set symbol undefined */
+
+ if (symbol_no_references(sym))
+ symbol_free(sym) ; /* not in table, no value, no references */
+
+ if (value != NULL)
+ {
+ table->base_count = i + 1 ; /* where we've got to */
+ return value ; /* <<< RETURN: caller must deal with value */
+ } ;
+ } ;
+ } ;
+
+ symbol_table_reset(table, free_structure) ;
+
+ return NULL ;
+} ;
+
+/* Look-up name in given symbol table. Add if required.
+ *
+ * Returns NULL if not found and not required to add.
+ *
+ * NB: the name argument is passed to the symbol table's hash function. That
+ * function is required to return with a 32-bit hash
+ *
+ * NB: if required to add, the caller cannot distinguish between a symbol
+ * which did not previously exist, and one which did exist but had no
+ * value and no references. Where that distinction matters, it is
+ * necessary to do an extra lookup.
+ */
+symbol
+symbol_lookup(symbol_table table, const void* name, int add)
+{
+ struct symbol* this ;
+ struct symbol** base ;
+ struct symbol_hash hash ;
+
+ assert(table != NULL) ;
+ if (table->bases == NULL)
+ symbol_table_setup(table) ; /* Lazy allocation of chain bases etc. */
+
+ table->hash_function(&hash, name) ;
+
+ base = symbol_base(table, hash.hash) ;
+ this = *base ;
+ while (this)
+ {
+ if ((this->hash == hash.hash)
+ && (this->name_len == hash.name_len)
+ && (memcmp(this->name, hash.name, this->name_len) == 0))
+ return this ;
+ this = this->next ;
+ } ;
+
+ /* Not found -- quit now if not required to add */
+ if (!add) return NULL ;
+
+ /* Adding: first, carve a new, empty symbol entry */
+ this = XCALLOC(MTYPE_SYMBOL, sizeof(struct symbol) + hash.name_copy_len) ;
+
+ this->table = table ;
+ this->value = NULL ;
+ this->ref_list = NULL ;
+ this->ref_count = 0 ;
+ this->hash = hash.hash ;
+ this->name_len = hash.name_len ;
+ memcpy(this->name, hash.name, hash.name_copy_len) ;
+
+ /* Second, if required, extend the array of list bases. We extend if */
+ /* we have a collision *and* we exceed threshold of number of entries. */
+ if ((*base != NULL) && (table->entry_count > table->extend_thresh))
+ {
+ symbol_extend_bases(table) ;
+ base = symbol_base(table, hash.hash) ;
+ } ;
+
+ /* Third, chain in the new entry, count it in and return */
+ this->next = *base ;
+ *base = this ;
+
+ ++table->entry_count ;
+
+ return this ;
+} ;
+
+/* Delete symbol.
+ *
+ * The first effect of this is to set the symbol value to NULL, which may
+ * trigger a value_call_back etc.
+ *
+ * Then the symbol is removed from the table (and the symbol becomes an orphan).
+ *
+ * Then, if there are no (remaining) references the symbol is freed. Otherwise
+ * the symbol entry remains in existence until there are no more references
+ * (at which point it will finally be destroyed).
+ *
+ * Returns the last value of the symbol -- which may itself need to be
+ * destroyed -- noting that the symbol may already have been released. (If the
+ * symbol is required when the value is released, then the value should hold a
+ * simple reference to the symbol.)
+ *
+ * NB: the effect of deleting a symbol is to leave all remaining references
+ * pointing at an NULL value, orphaned symbol.
+ *
+ * If a new symbol is created with the same name, that will be a
+ * completely different symbol -- references to the old symbol will
+ * continue to be to the vestigial NULL value.
+ *
+ * This is different from setting the symbol value to NULL and later
+ * giving it a new value.
+ *
+ * NB: orphan symbols can be deleted. The effect is to free the symbol if
+ * possible.
+ */
+void*
+symbol_delete(symbol sym)
+{
+ void* old_value = symbol_unset_value(sym) ;
+
+ if (symbol_no_references(sym))
+ symbol_free(sym) ; /* free symbol now if no references */
+ else
+ symbol_remove(sym) ; /* else just remove it from the table -- will be */
+ /* freed when all references are unset. */
+ return old_value ;
+} ;
+
+/* The hash functions provided here use CRC32 as a hash.
+ *
+ * CRC32 is not intended as a hash function, and is not a perfect one.
+ * However it is fast -- requiring a few simple operations per byte. Taken
+ * with the secondary effect of using the hash produced modulo an odd number,
+ * experience suggests this is sufficient.
+ */
+
+static u_int32_t crc_table[] ;
+
+/* Standard symbol string hash function. */
+void
+symbol_hash_string(symbol_hash p_hash, const char* string) {
+ u_int32_t h = 0 ;
+ const char* p = string ;
+
+ while (*p != 0)
+ h = crc_table[(h & 0xFF) ^ (u_int8_t)*p++] ^ (h >> 8) ;
+
+ assert((p - string) < 0xFFFF) ;
+
+ p_hash->hash = h ;
+ p_hash->name = string ;
+ p_hash->name_len = (p - string) ;
+ p_hash->name_copy_len = p_hash->name_len + 1 ;
+} ;
+
+/* Standard symbol byte vector hash function. */
+void
+symbol_hash_bytes(symbol_hash p_hash, const void* bytes, size_t len) {
+ assert(len < 0xFFFF) ;
+
+ u_int32_t h = len ; /* So strings of zeros don't CRC the same ! */
+ const u_int8_t* p = bytes ;
+ const u_int8_t* e = p + len ;
+
+ while (p < e)
+ h = crc_table[(h & 0xFF) ^ *p++] ^ (h >> 8) ;
+
+ p_hash->hash = h ;
+ p_hash->name = (const void*)bytes ;
+ p_hash->name_len = len ;
+ p_hash->name_copy_len = len ;
+} ;
+
+/* Extend the array of list bases. */
+static void
+symbol_extend_bases(symbol_table table)
+{
+ symbol this ;
+ symbol next ;
+ symbol* old_bases ;
+ symbol* new_bases ;
+ symbol* base ;
+ unsigned int new_base_count ;
+ unsigned int old_base_count ;
+
+ old_bases = table->bases ;
+ old_base_count = table->base_count ;
+
+ assert((old_bases != NULL) && (old_base_count != 0)) ;
+
+ /* TODO: should look out for overflowing base_count and requiring */
+ /* impossible amounts of memory ?! */
+
+ new_base_count = (table->base_count | 1) - 1 ; /* trim enforced odd-ness */
+
+ if (new_base_count <= SYMBOL_TABLE_BASES_DOUBLE_MAX)
+ new_base_count *= 2 ;
+ else
+ new_base_count += SYMBOL_TABLE_BASES_DOUBLE_MAX ;
+
+ new_base_count = symbol_table_new_bases(table, new_base_count,
+ (float)table->extend_thresh / table->base_count) ;
+
+ /* Rehome everything on the new chain bases. */
+ new_bases = table->bases ;
+ while (old_base_count--)
+ {
+ next = old_bases[old_base_count] ;
+ while (next != NULL)
+ {
+ this = next ;
+ next = this->next ;
+ base = &new_bases[this->hash % new_base_count] ;
+ this->next = *base ;
+ *base = this ;
+ } ;
+ } ;
+
+ /* Release the old chain bases, and we're done. */
+ XFREE(MTYPE_SYMBOL_BASES, old_bases) ;
+} ;
+
+/*==============================================================================
+ * Reference count handling.
+ *
+ * symbol_inc_ref(sym) -- declared Inline
+ * symbol_dec_ref(sym) -- declared Inline
+ */
+
+/* Zeroise the reference count.*/
+
+symbol
+symbol_zero_ref(symbol sym, int force)
+{
+ assert((sym->ref_count == 1) || force) ;
+
+ sym->ref_count = 0 ;
+ symbol_free_if_redundant(sym) ;
+
+ return NULL ;
+} ;
+
+/*==============================================================================
+ * Reference list handling.
+ *
+ * References are added at the head of the list -- which is significant when
+ * adding references during a symbol reference walk.
+ */
+
+/* Insert symbol_ref at head of symbol's list of references. */
+static inline void
+symbol_add_ref(symbol sym, symbol_ref ref)
+{
+ symbol_ref next = sym->ref_list ;
+ sym->ref_list = ref ;
+ if (next)
+ next->prev = ref ;
+ ref->next = next ;
+ ref->prev = (void*)sym ; /* marker for first on list */
+} ;
+
+/* Clip symbol_ref from symbol's list of references.
+ *
+ * If symbol_ref has already been deleted the prev pointer is NULL, and this
+ * function copes -- and does not need the symbol to be valid (sym may be NULL).
+ */
+static inline void
+symbol_del_ref(symbol sym, symbol_ref ref)
+{
+ symbol_ref prev = ref->prev ;
+ symbol_ref next = ref->next ;
+
+ if (prev != NULL)
+ {
+ if (prev == (void*)sym)
+ {
+ assert(sym->ref_list == ref) ;
+ sym->ref_list = next ;
+ }
+ else
+ prev->next = next ;
+
+ if (next != NULL)
+ next->prev = prev ;
+ } ;
+ ref->next = ref->prev = NULL ;
+} ;
+
+/*==============================================================================
+ * The value_call_back handling and symbol reference list walking.
+ *
+ * If there is one, the value_call_back function is called when the value of
+ * a symbol is set -- except when it is set NULL and is already NULL. Note
+ * that setting the same non-NULL value *does* invoke the value_call_back.
+ *
+ * The value_call_back function is passed the current state of the symbol,
+ * complete with new value, and the old value of the symbol.
+ *
+ * During the value_call_back the symbol reference list may be walked, so that
+ * users of the value may be updated.
+ *
+ * During the value_call_back the symbol may be set, unset or deleted, and
+ * references added or taken away. This may cause nested calls of the
+ * call-back. Note that each call-back holds a reference to the symbol, so if
+ * the symbol is deleted it won't be freed until the outermost call-back
+ * returns.
+ *
+ * Procedure for walking the references to a symbol:
+ *
+ * struct symbol_ref walk ;
+ * symbol sym ;
+ * symbol_ref ref ;
+ * symbol_ref_walk_start(sym, &walk) ;
+ * while ((ref = symbol_ref_walk_step(&walk)) != NULL)
+ * .... whatever
+ * symbol_ref_walk_end(&walk) ;
+ *
+ * NB: it is *essential* to call symbol_ref_walk_end() exactly once at some
+ * time after symbol_ref_walk_start.
+ *
+ * The symbol table walk uses a "bookmark" which is a special from of entry in
+ * the symbol's reference list. This mechanism:
+ *
+ * (a) prevents the symbol being freed while the reference walk is in
+ * progress -- that may happen during symbol_ref_walk_end.
+ *
+ * (b) allows for the current and other references to be set or unset.
+ *
+ * Setting a reference inserts it upstream of the bookmark -- so it will
+ * not be visited during the walk.
+ *
+ * Unsetting a reference that has yet to be visited eliminates it from
+ * the walk.
+ *
+ * Note that setting a reference to refer to the symbol it already
+ * refers to has no effect at all.
+ *
+ * (c) allows the symbol to be defined, undefined or redefined during a
+ * symbol reference walk.
+ *
+ * If that triggers another symbol reference walk, then that walk will
+ * proceed until it hits the point reached by the walk it is nested
+ * inside, and then stop.
+ *
+ * Suppose the outer walk was dealing with the value having changed from
+ * 'A' to 'B'. The inner walk will do from 'B' to the latest value 'C'
+ * for the references that have already seen 'A' to 'B'. When the outer
+ * walk resumes, it will deal with the change 'A' to 'C', unaware of the
+ * intermediate step.
+ *
+ * If that does not suit, don't fiddle with symbol values during a
+ * symbol reference walk.
+ */
+
+/* Bookmarks are symbol_ref structures, distinguished from ordinary symbol_ref
+ * structures by setting the sym field to point at the bookmark symbol_ref
+ * itself.
+ *
+ * (It would be nicer to use the parent field for this... but what is put
+ * there in ordinary symbol_ref structures is not guaranteed...)
+ */
+static inline int
+symbol_ref_is_bookmark(symbol_ref ref)
+{
+ return (void*)ref->sym == (void*)ref ;
+} ;
+
+/* Start walk of symbol references */
+void
+symbol_ref_walk_start(symbol sym, symbol_ref walk)
+{
+ symbol_init_ref(walk) ; /* keeping things tidy */
+ walk->sym = (void*)walk ; /* bookmark signature */
+ walk->parent = sym ;
+ symbol_add_ref(sym, walk) ; /* insert bookmark at head of list */
+} ;
+
+/* Step walk and return the next reference (if any). */
+symbol_ref
+symbol_ref_walk_step(symbol_ref walk)
+{
+ symbol_ref next_ref ;
+
+ assert(symbol_ref_is_bookmark(walk)) ; /* must be a bookmark ! */
+
+ /* Pick up reference following the bookmark, before deleting it. */
+ next_ref = walk->next ;
+ symbol_del_ref((symbol)walk->parent, walk) ;
+
+ /* Stop immediately if bookmark was at the end of the list or the next */
+ /* item is a bookmark (for a walk that started earlier). */
+ if ((next_ref == NULL) || symbol_ref_is_bookmark(next_ref))
+ return NULL ;
+
+ /* Now we move the bookmark from where it is now to after next_ref. */
+
+ walk->next = next_ref->next ;
+ next_ref->next = walk ;
+ walk->prev = next_ref ;
+ if (walk->next != NULL)
+ walk->next->prev = walk ;
+
+ /* Return the next real reference to be processed. */
+ return next_ref ;
+} ;
+
+/* End of symbol reference walk.
+ *
+ * NB: if the symbol is not defined and has no references or bookmarks it
+ * will now be freed.
+ */
+void
+symbol_ref_walk_end(symbol_ref walk)
+{
+ assert(symbol_ref_is_bookmark(walk)) ; /* must be a bookmark ! */
+
+ symbol_del_ref((symbol)(walk->parent), walk) ; /* make sure */
+
+ symbol_free_if_redundant((symbol)(walk->parent)) ;
+} ;
+
+/*==============================================================================
+ * Symbol Value handling.
+ */
+
+/* Set symbol value. NB: setting to NULL == symbol_unset_value.
+ * NB: setting same value as currently looks like a change.
+ * (except for setting NULL to NULL !)
+ *
+ * Invokes change call-back, if any -- except when setting to NULL and is
+ * already NULL.
+ *
+ * It is possible for the call-back to set the value again, to unset it, to
+ * change references, etc.
+ *
+ * Returns previous value -- which may require releasing.
+ */
+void*
+symbol_set_value(symbol sym, void* new_value)
+{
+ void* old_value ;
+
+ assert(sym->table != NULL) ; /* may not set value for orphan symbol ! */
+
+ old_value = sym->value ;
+ sym->value = new_value ;
+
+ /* Invoke value_call_back (if any). */
+ /* Note that the value_call_back may set/unset references and/or */
+ /* define/undefine the value. */
+ if (((sym)->table->value_call_back != NULL)
+ && ( (new_value != NULL) || (old_value != NULL) ))
+ {
+ symbol_inc_ref(sym) ; /* preserve until call-back returns */
+ sym->table->value_call_back(sym, old_value) ;
+ symbol_dec_ref(sym) ; /* may now free if has been deleted */
+ } ;
+
+ return old_value ;
+} ;
+
+/*==============================================================================
+ * Symbol Reference handling.
+ *
+ * Implementation note: the next and prev pointers in the symbol_ref structure
+ * are significant only if the sym pointer is not NULL.
+ */
+
+/* Initialise symbol reference -- allocate if required. */
+symbol_ref
+symbol_init_ref(symbol_ref ref)
+{
+ if (ref == NULL)
+ return XCALLOC(MTYPE_SYMBOL_REF, sizeof(struct symbol_ref)) ;
+ else
+ return memset(ref, 0, sizeof(struct symbol_ref)) ;
+} ;
+
+/* Set symbol reference -- allocate if required (ref == NULL).
+ *
+ * NB: does nothing if reference already set to the given symbol.
+ *
+ * NB: unsets (but does not free) reference if was not NULL (and is not
+ * same as symbol being set to) before setting new reference.
+ *
+ * NB: setting reference to NULL unsets any existing reference, but does NOT
+ * release the reference structure.
+ *
+ * NB: if reference is allocated, the parent is set NULL and the tag is set
+ * NULL/0.
+ *
+ * if reference is not allocated, the parent and tag are unchanged.
+ */
+symbol_ref
+symbol_set_ref(symbol_ref ref, struct symbol* sym)
+{
+ if (ref != NULL)
+ {
+ if (ref->sym == sym)
+ return ref ; /* Nothing more to do if already set to given value */
+ if (ref->sym != NULL)
+ symbol_unset_ref_keep(ref) ;
+ }
+ else
+ ref = symbol_init_ref(NULL) ;
+
+ ref->sym = sym ;
+ if (sym != NULL)
+ symbol_add_ref(sym, ref) ;
+
+ return ref ;
+} ;
+
+/* Unset symbol reference. Free the structure if required.
+ *
+ * NB: does nothing if address of reference is NULL.
+ *
+ * NB: if reference is not freed, the parent and tag are unchanged.
+ *
+ * NB: removing the last reference to an symbol that has been deleted causes
+ * the symbol to be freed.
+ *
+ * NB: copes if the reference is already unset, of course.
+ */
+symbol_ref
+symbol_unset_ref(symbol_ref ref, int free_ref_structure)
+{
+ if (ref == NULL) return ref ;
+
+ if (ref->sym != NULL) /* NULL => reference already unset */
+ {
+ symbol_del_ref(ref->sym, ref) ;
+ symbol_free_if_redundant(ref->sym) ;
+ ref->sym = NULL ;
+ } ;
+
+ if (free_ref_structure)
+ XFREE(MTYPE_SYMBOL_REF, ref) ; /* ref is set to NULL */
+
+ return ref ;
+} ;
+
+/*==============================================================================
+ * Walking a symbol table
+ *
+ * Simple walk: visits all entries in the table, in the order they are hashed
+ * to. Simple iterator.
+ *
+ * Extract: makes vector of pointers to selected entries, and sorts that
+ * vector as required.
+ */
+
+/* Walk the given symbol table. Usage:
+ *
+ * struct symbol_walker walker ;
+ * symbol sym ;
+ * ....
+ * symbol_walk_start(table, &walker) ;
+ * while ((sym = symbol_walk_next(&walker)))
+ * ....
+ *
+ * NB: it is possible to change the current symbol while the walk is in
+ * progress -- up to and including deleting it. Any other changes to
+ * the table must NOT be attempted.
+ */
+void
+symbol_walk_start(symbol_table table, struct symbol_walker* walker)
+{
+ walker->next = NULL ;
+ walker->base = table->bases ;
+ walker->base_count = table->base_count ;
+} ;
+
+symbol
+symbol_walk_next(struct symbol_walker* walker)
+{
+ symbol this = walker->next ;
+
+ while (this == NULL)
+ {
+ if (walker->base_count == 0)
+ return NULL ;
+ --walker->base_count ;
+ this = *(walker->base++) ;
+ } ;
+
+ walker->next = this->next ;
+ return this ;
+} ;
+
+/* Extract Symbols.
+ *
+ * Walk symbol table and select symbols to add to a new vector. Then sort the
+ * vector, if required. Takes:
+ *
+ * -- selector: NULL => select all
+ * -- p_val: pointer is passed to the select function (if any)
+ * -- most: if there is a select function, this flag hints that most of
+ * the symbols will be selected -- so it is worth preallocating
+ * a vector big enough for all symbols.
+ * -- sort: NULL => no sort (!)
+ *
+ * NB: the vector contains pointers to the selected symbols. It is the
+ * caller's responsibility to avoid deleting any symbol whose pointer
+ * in the vector they expect to rely on !
+ */
+vector
+symbol_table_extract(symbol_table table,
+ symbol_select_cmp* selector, const void* p_val, int most,
+ symbol_sort_cmp* sort)
+{
+ vector extract ;
+ symbol* base ;
+ unsigned int count ;
+ symbol sym ;
+
+ extract = vector_init_new(NULL, (most || (selector == NULL))
+ ? table->entry_count : 8) ;
+ base = table->bases ;
+
+ if (base == NULL)
+ return extract ; /* Quit if symbol table is "reset" */
+
+ count = table->base_count ;
+ while (count--)
+ {
+ sym = *base++ ;
+ while (sym != NULL)
+ {
+ if ((selector == NULL) || selector(sym, p_val))
+ vector_push_item(extract, sym) ;
+ sym = sym->next ;
+ } ;
+ } ;
+
+ if (sort != NULL)
+ vector_sort(extract, (vector_sort_cmp*)sort) ;
+
+ return extract ;
+} ;
+
+/*==============================================================================
+ * Some common comparison functions for symbol table extracts.
+ */
+
+/* Comparison function to sort names which are a mixture of digits and other
+ * characters.
+ *
+ * This comparison treats substrings of digits as numbers, so "a10" is > "a1".
+ */
+int
+symbol_mixed_name_cmp(const symbol* p_a,
+ const symbol* p_b)
+{
+ const char* a = symbol_get_name(*p_a) ;
+ const char* b = symbol_get_name(*p_b) ;
+ int la, lb ;
+
+ while (1) {
+ if (isdigit(*a) && isdigit(*b))
+ {
+ char* as ; /* Required to stop the compiler whining */
+ char* bs ;
+ unsigned long int na = strtoul(a, (char** restrict)&as, 10) ;
+ unsigned long int nb = strtoul(b, (char** restrict)&bs, 10) ;
+ if (na != nb)
+ return (na < nb) ? -1 : +1 ;
+ a = as ;
+ b = bs ;
+ }
+ else
+ {
+ if (*a != *b)
+ return (*a < *b) ? -1 : +1 ;
+ if (*a == '\0')
+ break ;
+ ++a ;
+ ++b ;
+ }
+ } ;
+
+ /* Looks like the names are equal.
+ * But may be different lengths if have number part(s) with leading zeros,
+ */
+
+ la = symbol_get_name_len(*p_a) ;
+ lb = symbol_get_name_len(*p_b) ;
+ if (la != lb)
+ return (la < lb) ? -1 : +1 ;
+ return 0 ;
+} ;
+
+/*==============================================================================
+ * Table for generating CRC-32 -- Standard (0x1_04C1_1DB7 0xEDB8_8320)
+ */
+static u_int32_t crc_table[] =
+{
+ 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
+ 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
+ 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
+ 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
+ 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
+ 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
+ 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
+ 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
+ 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
+ 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
+ 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
+ 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
+ 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
+ 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
+ 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
+ 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
+ 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
+ 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
+ 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
+ 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
+ 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
+ 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
+ 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
+ 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
+ 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
+ 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
+ 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
+ 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
+ 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
+ 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
+ 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
+ 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
+ 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
+ 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
+ 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
+ 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
+ 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
+ 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
+ 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
+ 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
+ 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
+ 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
+ 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
+} ;
diff --git a/lib/symtab.h b/lib/symtab.h
new file mode 100644
index 00000000..7b4e002c
--- /dev/null
+++ b/lib/symtab.h
@@ -0,0 +1,318 @@
+/* Symbol Table data structure -- header
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra 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.
+ *
+ * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_SYMTAB_H
+#define _ZEBRA_SYMTAB_H
+
+#include "vector.h"
+
+/* Macro in case there are particular compiler issues. */
+#ifndef Inline
+ #define Inline static inline
+#endif
+
+/* Maximum number of symbol table bases -- something has gone tragically wrong
+ * if we hit this. Assume can multiply this by 2 and get valid size_t result.
+ */
+#define SYMBOL_TABLE_BASES_MAX (1024 * 1024 * 1024)
+
+/* Minimum number of symbol table bases. */
+#define SYMBOL_TABLE_BASES_MIN 10
+/* Point at which stops doubling the symbol table size (bases) */
+#define SYMBOL_TABLE_BASES_DOUBLE_MAX 2000
+
+/* Structures defined below. */
+struct symbol_table ;
+struct symbol ;
+struct symbol_ref ;
+struct symbol_hash ;
+
+typedef struct symbol_table* symbol_table ;
+typedef struct symbol* symbol ;
+typedef struct symbol_ref* symbol_ref ;
+typedef struct symbol_hash* symbol_hash ;
+
+/* Function types used. */
+typedef void symbol_hash_function(symbol_hash hash, const void* name) ;
+typedef void symbol_call_back_function(symbol sym, void* value) ;
+typedef void symbol_destroy_function(symbol sym) ;
+
+/* Symbol Table.
+ *
+ * Don't fiddle with this directly... see access functions below.
+ */
+
+struct symbol_table
+{
+ void* parent ; /* to identify the table. */
+
+ symbol* bases ; /* ref:array of chain bases */
+ unsigned int base_count ; /* number of chain bases */
+
+ unsigned int entry_count ; /* number of entries in the table */
+ unsigned int extend_thresh ; /* when to extend the hash table */
+
+ symbol_hash_function* hash_function ; /* function to hash given "name" */
+ /* NULL => use default */
+ symbol_call_back_function* value_call_back ;
+ /* called when symbol value is set */
+} ;
+
+/* Symbol Table Entry.
+ *
+ * Don't fiddle with this directly... see access macros/functions below.
+ */
+
+struct symbol
+{
+ symbol_table table ; /* so can go from symbol to enclosing table */
+ /* NULL => orphan symbol, with NULL value. */
+
+ symbol next ; /* assume chains are short and seldom remove */
+ /* symbols -- so single pointer will do. */
+
+ void* value ; /* see: symbol_get_value(sym) etc. */
+
+ symbol_ref ref_list ; /* list of symbol_ref references */
+ unsigned ref_count ; /* count of simple references */
+
+ u_int32_t hash ; /* used in lookup and when extending bases. */
+
+ u_int16_t name_len ; /* see: symbol_get_name_len(sym) */
+ char name[] ; /* see: symbol_get_name(sym) */
+} ;
+
+/* Symbol Reference (or "bookmark").
+ *
+ * Don't fiddle with this directly... see access macros/functions below
+ */
+
+typedef union
+{
+ void* p ;
+ unsigned long u ;
+ signed long i ;
+} symbol_ref_tag_t ;
+
+struct symbol_ref
+{
+ symbol sym ; /* Address of symbol referred to (if any). */
+ /* (In "bookmark" this points to self.) */
+
+ symbol_ref next ; /* fellow references to the symbol ... */
+ symbol_ref prev ; /* ... ignore if sym is NULL. */
+
+ void* parent ; /* see: sym_ref_parent(sym_ref) etc. */
+ symbol_ref_tag_t tag ; /* see: sym_ref_tag(sym_ref) etc. */
+} ;
+
+/* Result of a hash function for a symbol name. */
+struct symbol_hash
+{
+ u_int32_t hash ; /* the hash value ! */
+ const void* name ; /* symbol name as byte vector */
+ u_int16_t name_len ; /* length in chars for comparison purposes */
+ u_int16_t name_copy_len ; /* number of chars to copy to store name. */
+} ;
+
+/* Symbol Walk Iterator */
+struct symbol_walker
+{
+ symbol next ; /* next symbol to return (if any) */
+ symbol* base ; /* next chain base to process (if any) */
+ unsigned base_count ; /* number of chain bases left to process */
+} ;
+
+/* Symbol Table Operations. */
+
+extern symbol_table
+symbol_table_init_new(symbol_table table,
+ void* parent,
+ unsigned bases,
+ unsigned density,
+ symbol_hash_function* hash_function,
+ symbol_call_back_function* value_call_back) ;
+void symbol_table_set_parent(symbol_table table, void* parent) ;
+void* symbol_table_get_parent(symbol_table table) ;
+void* symbol_table_ream(symbol_table table, int free_structure) ;
+#define symbol_table_ream_free(table) symbol_table_ream(table, 1)
+#define symbol_table_ream_keep(table) symbol_table_ream(table, 0)
+symbol_table symbol_table_reset(symbol_table table, int free_structure) ;
+#define symbol_table_reset_free(table) symbol_table_reset(table, 1)
+#define symbol_table_reset_keep(table) symbol_table_reset(table, 0)
+
+extern void symbol_hash_string(struct symbol_hash* p_hash, const char* string) ;
+extern void symbol_hash_bytes(struct symbol_hash* p_hash, const void* bytes,
+ size_t len) ;
+extern void symbol_table_set_value_call_back(symbol_table table,
+ symbol_call_back_function* value_call_back) ;
+
+extern void symbol_table_free(symbol_table) ;
+
+extern symbol symbol_lookup(symbol_table table, const void* name, int add) ;
+
+#define symbol_seek(table, name) symbol_lookup(table, name, 0)
+#define symbol_find(table, name) symbol_lookup(table, name, 1)
+
+extern void* symbol_delete(symbol sym) ;
+
+extern void* symbol_set_value(symbol sym, void* new_value) ;
+#define symbol_unset_value(sym) symbol_set_value(sym, NULL)
+
+void symbol_ref_walk_start(symbol sym, symbol_ref walk) ;
+symbol_ref symbol_ref_walk_step(symbol_ref walk) ;
+void symbol_ref_walk_end(symbol_ref walk) ;
+
+void symbol_walk_start(symbol_table table, struct symbol_walker* walker) ;
+symbol symbol_walk_next(struct symbol_walker* walker) ;
+
+typedef int symbol_select_cmp(const symbol, const void*) ;
+typedef int symbol_sort_cmp(const symbol*, const symbol*) ;
+vector symbol_table_extract(symbol_table table,
+ symbol_select_cmp* select, const void* p_value,
+ int most, symbol_sort_cmp* sort) ;
+
+extern symbol_sort_cmp symbol_mixed_name_cmp ;
+
+/* Access functions -- argument is address of symbol (may be NULL). */
+Inline void*
+symbol_get_value(const symbol sym)
+{
+ return (sym != NULL) ? sym->value : NULL ;
+} ;
+
+Inline const void*
+symbol_get_name(const symbol sym)
+{
+ return (sym != NULL) ? sym->name : NULL ;
+} ;
+
+Inline unsigned
+symbol_get_name_len(const symbol sym)
+{
+ return (sym != NULL) ? sym->name_len : 0 ;
+} ;
+
+Inline struct symbol_table*
+symbol_get_table(const symbol sym)
+{
+ return (sym != NULL) ? sym->table : NULL ;
+} ;
+
+Inline symbol
+symbol_inc_ref(symbol sym)
+{
+ ++sym->ref_count ;
+ return sym ;
+} ;
+
+extern symbol symbol_zero_ref(symbol sym, int force) ;
+
+Inline symbol
+symbol_dec_ref(symbol sym)
+{
+ if (sym->ref_count <= 1)
+ return symbol_zero_ref(sym, 0) ;
+
+ --sym->ref_count ;
+ return sym ;
+} ;
+
+extern symbol_ref
+symbol_init_ref(symbol_ref ref) ;
+
+extern symbol_ref
+symbol_set_ref(symbol_ref ref, symbol sym) ;
+
+extern symbol_ref
+symbol_unset_ref(symbol_ref ref, int free_ref_structure) ;
+
+#define symbol_unset_ref_free(ref) symbol_unset_ref(ref, 1) ;
+#define symbol_unset_ref_keep(ref) symbol_unset_ref(ref, 0) ;
+
+/* Access functions -- argument is address of symbol_ref. */
+/* These cope if address of symbol_ref is null, or reference is undefined. */
+Inline void*
+sym_ref_symbol(symbol_ref ref)
+{
+ return (ref != NULL) ? ref->sym : NULL ;
+}
+Inline void*
+sym_ref_value(symbol_ref ref)
+{
+ return symbol_get_value(sym_ref_symbol(ref)) ;
+}
+Inline const void*
+sym_ref_name(symbol_ref ref)
+{
+ return symbol_get_name(sym_ref_symbol(ref)) ;
+}
+Inline u_int16_t
+sym_ref_name_len(symbol_ref ref)
+{
+ return symbol_get_name_len(sym_ref_symbol(ref)) ;
+}
+
+Inline void*
+sym_ref_parent(symbol_ref ref)
+{
+ return (ref != NULL) ? ref->parent : NULL ;
+}
+Inline void*
+sym_ref_p_tag(symbol_ref ref)
+{
+ return (ref != NULL) ? ref->tag.p : NULL ;
+}
+Inline unsigned long int
+sym_ref_u_tag(symbol_ref ref)
+{
+ return (ref != NULL) ? ref->tag.u : 0 ;
+}
+Inline signed long int
+sym_ref_i_tag(symbol_ref ref)
+{
+ return (ref != NULL) ? ref->tag.i : 0 ;
+}
+
+/* Set properties of reference -- argument is address of symbol_ref, which is */
+/* assumed to NOT be NULL. */
+Inline void
+sym_ref_set_parent(symbol_ref ref, void* pa)
+{
+ ref->parent = pa ;
+}
+Inline void
+sym_ref_set_p_tag(symbol_ref ref, void* p_tag)
+{
+ ref->tag.p = p_tag ;
+}
+Inline void
+sym_ref_set_u_tag(symbol_ref ref, unsigned long int u_tag)
+{
+ ref->tag.u = u_tag ;
+}
+Inline void
+sym_ref_set_i_tag(symbol_ref ref, signed long int i_tag)
+{
+ ref->tag.i = i_tag ;
+}
+
+#endif /* _ZEBRA_SYMTAB_H */
diff --git a/lib/vector.c b/lib/vector.c
index 7c148628..ad2af289 100644
--- a/lib/vector.c
+++ b/lib/vector.c
@@ -1,6 +1,9 @@
-/* Generic vector interface routine
+/* Generic vector interface routine -- functions
* Copyright (C) 1997 Kunihiro Ishiguro
*
+ * 24-Nov-2009 -- extended to add a number of new operations on vectors.
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
@@ -16,7 +19,7 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#include <zebra.h>
@@ -24,87 +27,789 @@
#include "vector.h"
#include "memory.h"
-/* Initialize vector : allocate memory and return vector. */
+/* Vectors are implemented as a structure which points to an array of pointers
+ * to vector items. That array -- the body of the vector -- can change size,
+ * and therefore may move around in memory.
+ *
+ * The vector structure may be statically allocated, embedded in another
+ * structure, or allocated dynamically. In any case the vector operations
+ * require the address of the vector structure -- see typedef for vector.
+ *
+ * Vector items are accessed by index, fetching and storing pointers to
+ * those items. Can also push and pop items.
+ *
+ * A vector has a known (logical) end position. Everything beyond the end is
+ * defined to be NULL. When a vector is initialised it is set empty.
+ *
+ * At any given moment the vector body has a limit on the number of items it
+ * can accommodate (the physical end).
+ *
+ * A vector will grow to accommodate what is put into it. Adding items beyond
+ * the (logical) end moves it. Adding items beyond the (physical) limit causes
+ * the body to be extended to suit, and to leave some spare space for future
+ * expansion.
+ *
+ * While the vector is small (see VECTOR_LIMIT_DOUBLE_MAX) the body will grow by
+ * doubling in size. When it is larger, it grows to be multiples of
+ * VECTOR_LIMIT_DOUBLE_MAX.
+ *
+ * Deleting items reduces the (logical) end position, but does NOT release
+ * memory -- the (physical) limit is not changed.
+ *
+ * To release memory: vector_chop will release everything beyond the current
+ * end; vector_decant will create a new body of exactly the current size,
+ * releasing the old body; vector_discard will release everything beyond a
+ * given position.
+ *
+ * NB: you can set a vector item to be NULL. If you set a vector item beyond
+ * the current end, NULL items are inserted in the vector.
+ *
+ * NB: when setting a vector item it is the caller's responsibility to
+ * deallocate any pre-existing value of the item.
+ *
+ * NB: when deleting items it is also the caller's responsibility to deallocate
+ * any values that require it.
+ *
+ * Implementation Notes
+ *
+ * Everything beyond the (logical) end is implicitly NULL.
+ *
+ * Actual memory between (logical) end and (physical) limit is UNDEFINED. So
+ * when advancing the end some care has to be taken to ensure that any new
+ * items in the vector are either set to something or cleared to NULL.
+ *
+ * It would have been possible to ensure that everything between end and limit
+ * is cleared to NULL, but that is more work -- in particular it creates work
+ * when it is not always required.
+ */
+
+#define P_ITEMS_SIZE(n) SIZE(p_vector_item, n)
+/*==============================================================================
+ * Initialisation, allocation, reset etc.
+ */
+
+/* Initialise a brand new vector, setting it empty.
+ *
+ * Allocates vector structure if none given -- that is, if v == NULL.
+ *
+ * If size is given as zero, no body is allocated, otherwise body of exactly
+ * the required size is allocated.
+ *
+ * NB: discards any existing vector body -- so it is the caller's responsibility
+ * to release any existing body, and any items in that body.
+ */
vector
-vector_init (unsigned int size)
+vector_init_new(vector v, unsigned int size)
{
- vector v = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
+ if (v == NULL)
+ v = XCALLOC(MTYPE_VECTOR, sizeof(struct vector)) ;
+ else
+ memset(v, 0, sizeof(struct vector)) ;
- /* allocate at least one slot */
- if (size == 0)
- size = 1;
+ if (size != 0)
+ {
+ v->p_items = XMALLOC(MTYPE_VECTOR_BODY, P_ITEMS_SIZE(size)) ;
+ v->limit = size ;
+ } ;
- v->alloced = size;
- v->active = 0;
- v->index = XCALLOC (MTYPE_VECTOR_INDEX, sizeof (void *) * size);
- return v;
-}
+ return v ;
+} ;
+/* Initialize vector : allocate memory and return vector.
+ * allocates body with at least 1 entry.
+ */
+vector
+vector_init (unsigned int size)
+{
+ return vector_init_new(NULL, size ? size : 1) ; /* at least 1 entry */
+} ;
+
+/* Basic: free the given vector structure. NB: Orphans any existing body !! */
void
vector_only_wrapper_free (vector v)
{
XFREE (MTYPE_VECTOR, v);
}
+/* Basic: free the vector body. */
void
-vector_only_index_free (void *index)
+vector_only_index_free (void *body)
{
- XFREE (MTYPE_VECTOR_INDEX, index);
+ XFREE (MTYPE_VECTOR_BODY, body);
}
+/* Basic: free the vector body and the vector structure. */
void
vector_free (vector v)
{
- XFREE (MTYPE_VECTOR_INDEX, v->index);
+ XFREE (MTYPE_VECTOR_BODY, v->p_items);
XFREE (MTYPE_VECTOR, v);
}
+/* Re-Initialize vector (or create new one), setting it empty.
+ *
+ * Allocates vector structure if none given -- that is, if v == NULL.
+ *
+ * If size is given as zero, no body is allocated, but any existing body is
+ * retained. (vector_reset() will discard body.)
+ *
+ * Otherwise ensures existing body is at least the required size, or a body
+ * of exactly the required size is allocated.
+ *
+ * NB: when re-initialising an existing vector it is the caller's
+ * responsibility to release any vector item values *before* doing this.
+ * */
+vector
+vector_re_init(vector v, unsigned int size)
+{
+ if ((v == NULL) || (v->p_items == NULL))
+ return vector_init_new(v, size) ;
+
+ v->end = 0 ;
+
+ if (v->limit < size)
+ {
+ v->p_items = XREALLOC(MTYPE_VECTOR_BODY, v->p_items, P_ITEMS_SIZE(size)) ;
+ v->limit = size ;
+ } ;
+
+ return v ;
+} ;
+
+/* Free the vector body, and free the vector structure or reset it.
+ *
+ * Return NULL if releases vector, otherwise the address of the vector.
+ *
+ * NB: it is the caller's responsibility to release any vector item values
+ * *before* doing this.
+ */
+vector
+vector_reset(vector v, int free_structure)
+{
+ if (v == NULL)
+ return NULL ; /* allow for already freed vector */
+
+ if (v->p_items != NULL)
+ XFREE(MTYPE_VECTOR_BODY, v->p_items) ;
+
+ if (free_structure)
+ {
+ XFREE(MTYPE_VECTOR, v) ;
+ return NULL ;
+ }
+ else
+ return vector_init_new(v, 0) ;
+} ;
+
+/* Pop item from vector, stepping past any NULLs.
+ *
+ * If vector is empty, free the body and, if required, the vector structure.
+ *
+ * Useful for emptying out and discarding a vector:
+ *
+ * while ((p_v = vector_ream_out(v, 1)))
+ * ... do what's required to release the item p_v
+ *
+ * Returns NULL if vector was empty and has now been freed as required.
+ */
+p_vector_item
+vector_ream(vector v, int free_structure)
+{
+ p_vector_item p_v ;
+
+ if (v == NULL)
+ return NULL ;
+
+ while (v->end != 0)
+ {
+ p_v = v->p_items[--v->end] ;
+ if (p_v != NULL)
+ return p_v ; /* return non-NULL item */
+ } ;
+
+ /* vector is empty: free the body, and (if required) the vector structure. */
+ vector_reset(v, free_structure) ;
+
+ return NULL ; /* signals end */
+} ;
+
+/*==============================================================================
+ * Unset item.
+ */
+
+/* Unset item at given index (ie set it NULL).
+ *
+ * Return the old value of the item.
+ *
+ * If the item at the current (logical) end of the vector is NULL, move the
+ * end backwards until finds a non-NULL item, or the vetor becomes empty.
+ */
+extern p_vector_item
+vector_unset_item(vector v, vector_index i)
+{
+ p_vector_item was ;
+ if (i < v->end)
+ {
+ was = v->p_items[i] ;
+ v->p_items[i] = NULL ;
+ }
+ else
+ was = NULL ;
+
+ /* Now move end back past any NULLs */
+ i = v->end ;
+ while ((i > 0) && (v->p_items[i-1] == NULL))
+ --i ;
+ v->end = i ;
+
+ return was ;
+} ;
+
+/*==============================================================================
+ * Inserting and deleting items.
+ */
+
+/* Insert item in vector, before item at given position
+ * Move items and extend vector as required.
+ */
+void
+vector_insert_item(vector v, vector_index i, void* p_v)
+{
+ if ((i == v->end) && (i < v->limit))
+ ++v->end ;
+ else
+ vector_insert(v, i, 1) ;
+
+ v->p_items[i] = (p_vector_item)p_v ;
+} ;
+
+/* Insert item in vector at given position with rider:
+ *
+ * rider < 0 -- insert before the item at the given position
+ * rider == 0 -- insert at the given position -- REPLACING any existing value
+ * rider > 0 -- insert after the item at the given position
+ *
+ * NB: when an item is replaced, it is the caller's responsibility to release
+ * any memory used by the item, if required.
+ *
+ * Move items and extend vector as required.
+ */
+void
+vector_insert_item_here(vector v, vector_index i, int rider,
+ p_vector_item p_v)
+{
+ if (rider == 0)
+ return vector_set_item(v, i, p_v) ;
+
+ if (rider > 0)
+ ++i ; /* insert before next item */
+ vector_insert_item(v, i, p_v) ;
+} ;
+
+/* Delete item from vector.
+ *
+ * Move items as required. Reduces number of items in the vector (unless
+ * the item in question is beyond the end of the vector !)
+ *
+ * NB: it is the caller's responsibility to release memory used by any
+ * current value of the item, if required.
+ *
+ * NB: does NOT change the size of the vector body.
+ */
+p_vector_item
+vector_delete_item(vector v, vector_index i)
+{
+ p_vector_item p_e ;
+ if (i < v->end)
+ {
+ p_e = v->p_items[i] ; /* pick up the current value */
+ if (i != (v->end - 1))
+ vector_delete(v, i, 1) ;
+ else
+ v->end = i ;
+ return p_e ;
+ }
+ else
+ return NULL ;
+} ;
+
+/*==============================================================================
+ * Moving items within vector.
+ */
+
+/* Move item in vector from source position to destination position.
+ * Moves intervening items up or down as required.
+ * Extends vector to include the destination, if required.
+ * A source item beyond the end of the vector is implicitly NULL.
+ */
+void
+vector_move_item(vector v, vector_index i_dst, vector_index i_src)
+{
+ p_vector_item* pp_s ;
+ p_vector_item* pp_d ;
+ p_vector_item p_e ;
+
+ vector_index old_end = v->end ;
+
+ /* Worry about whether both source and destination exist. */
+ if (i_dst >= old_end)
+ {
+ vector_insert(v, i_dst, 1) ; /* ensure destination exists */
+ if (i_src >= old_end)
+ return ; /* both were beyond the end */
+ }
+ else if (i_src >= old_end)
+ {
+ i_src = old_end ; /* clamp to just beyond last */
+ vector_insert(v, i_src, 1) ; /* create empty entry */
+ } ;
+
+ if (i_dst == i_src) /* avoid work and edge case */
+ return ;
+
+ /* Both src and dst are within the vector and src != dst */
+ pp_s = &v->p_items[i_src] ; /* address of src entry */
+ pp_d = &v->p_items[i_dst] ; /* address of dst entry */
+ p_e = *pp_s ; /* pick up item to move */
+ if (i_src < i_dst)
+ memmove(pp_s, pp_s+1, P_ITEMS_SIZE(i_dst - i_src)) ;
+ else
+ memmove(pp_d+1, pp_d, P_ITEMS_SIZE(i_src - i_dst)) ;
+ *pp_d = p_e ; /* put down the item to move */
+} ;
+
+/* Move item in vector to given position with rider:
+ *
+ * rider < 0 -- move to before the item at the given position
+ * rider == 0 -- move to replace item at the given position
+ * rider > 0 -- insert after the item at the given position
+ *
+ * NB: it is the caller's responsibility to release the any existing value
+ * that will be replaced.
+ *
+ * Move items and extend vector as required.
+ */
+void
+vector_move_item_here(vector v, vector_index i_dst, int rider,
+ vector_index i_src)
+{
+ if (rider != 0)
+ {
+ if (rider > 0)
+ ++i_dst ;
+ vector_move_item(v, i_dst, i_src) ;
+ }
+ else
+ {
+ /* to replace: copy and then delete. */
+ vector_set_item(v, i_dst, vector_get_item(v, i_src)) ;
+ vector_delete_item(v, i_src) ;
+ } ;
+} ;
+
+/* Reverse vector: reverse the order of items in the vector.
+ */
+void
+vector_reverse(vector v)
+{
+ if (v != NULL)
+ vector_part_reverse(v, 0, v->end) ;
+} ;
+
+/* Reverse portion of vector.
+ */
+void
+vector_part_reverse(vector v, vector_index i, unsigned int n)
+{
+ vector_index j ;
+
+ if (v == NULL)
+ return ;
+
+ if ((i + n) > v->limit)
+ vector_extend(v, i + n) ; /* ensure portion exists */
+
+ if (n <= 1)
+ return ;
+
+ j = i + n - 1 ; /* j > i, because n > 1 */
+ do
+ {
+ p_vector_item p_i = v->p_items[i] ;
+ v->p_items[i++] = v->p_items[j] ;
+ v->p_items[j--] = p_i ;
+ } while (j > i) ;
+} ;
+
+/*==============================================================================
+ * Copying, moving and appending entire vectors.
+ */
+
+static void vector_new_limit(vector v, vector_index new_end) ;
+
+/* Shallow vector copy -- copies pointers to item values, not the values. */
+/* Creates a new vector. */
+/* NB: copies whole body, including stuff beyond (logical) end ! */
+/* TODO: is this behaviour required ? */
vector
vector_copy (vector v)
{
- unsigned int size;
- vector new = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
+ vector new = vector_init_new(NULL, v->limit) ;
- new->active = v->active;
- new->alloced = v->alloced;
+ new->end = v->end;
- size = sizeof (void *) * (v->alloced);
- new->index = XCALLOC (MTYPE_VECTOR_INDEX, size);
- memcpy (new->index, v->index, size);
+ if (v->limit > 0)
+ memcpy(new->p_items, v->p_items, P_ITEMS_SIZE(v->limit)) ;
return new;
}
-/* Check assigned index, and if it runs short double index pointer */
-void
-vector_ensure (vector v, unsigned int num)
+/* Shallow vector copy -- copies pointers to item values, not the values.
+ * Creates a new vector or re-initialises an existing one.
+ *
+ * NB: creates new vector with same limit as existing one, but copies only
+ * the known items (ie up to end, not up to limit).
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to release any existing items if that is required.
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to ensure the vector structure is currently valid.
+ *
+ * NB: do NOT try copying a vector to itself !!
+ */
+vector
+vector_copy_here(vector dst, vector src)
+{
+ assert((src != NULL) && (src != dst)) ;
+
+ dst = vector_re_init(dst, src->limit) ;
+
+ dst->end = src->end;
+
+ if (src->end > 0)
+ memcpy(dst->p_items, src->p_items, P_ITEMS_SIZE(src->end)) ;
+
+ return dst ;
+} ;
+
+/* Vector move -- moves body of vector.
+ * Creates a new vector or re-initialises an existing one.
+ * Leaves the source vector empty -- does not release the structure.
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to release any existing items if that is required.
+ *
+ * NB: if re-initialising existing vector, it is the caller's responsibility
+ * to ensure the vector structure is currently valid.
+ *
+ * NB: do NOT try moving a vector to itself !!
+ */
+vector
+vector_move_here(vector dst, vector src)
{
- if (v->alloced > num)
- return;
+ assert((src != NULL) && (src != dst)) ;
+
+ if (dst != NULL)
+ dst = vector_reset(dst, 0) ; /* Reset to deallocate any existing body */
+ else
+ dst = vector_init_new(dst, 0) ; /* Create new structure sans body. */
- v->index = XREALLOC (MTYPE_VECTOR_INDEX,
- v->index, sizeof (void *) * (v->alloced * 2));
- memset (&v->index[v->alloced], 0, sizeof (void *) * v->alloced);
- v->alloced *= 2;
-
- if (v->alloced <= num)
- vector_ensure (v, num);
+ *dst = *src ; /* Copy the vector structure */
+
+ vector_init_new(src, 0) ; /* Set empty, forgetting body */
+
+ return dst ;
}
-/* This function only returns next empty slot index. It dose not mean
+/* Shallow vector copy append -- copies pointers to item values, not the values.
+ * Appends copied pointers to the destination vector.
+ * Creates a new destination vector if required.
+ *
+ * NB: Can append to self.
+ */
+vector
+vector_copy_append(vector dst, vector src)
+{
+ vector_index new_end ;
+
+ assert(src != NULL) ;
+
+ if (dst != NULL)
+ {
+ new_end = dst->end + src->end ;
+ if (new_end > dst->limit)
+ vector_new_limit(dst, new_end) ;
+ }
+ else
+ {
+ new_end = src->end ;
+ vector_init_new(dst, new_end) ;
+ } ;
+
+ if (src->end)
+ memcpy(&dst->p_items[dst->end], src->p_items, P_ITEMS_SIZE(src->end)) ;
+
+ dst->end = new_end ; /* Done last, allows for append to self ! */
+ return dst ;
+} ;
+
+/* Vector move append -- moves pointers to item values.
+ * Appends moved pointers to the destination vector.
+ * Creates a new destination vector if required (dst == NULL).
+ * Leaves the source vector empty -- does not release the structure.
+ *
+ * NB: do NOT try moving a vector to itself !!
+ */
+vector
+vector_move_append(vector dst, vector src)
+{
+ assert((src != NULL) && (src != dst)) ;
+
+ if ((dst == NULL) || (dst->end == 0))
+ return vector_move_here(dst, src) ; /* Easy way to do it if dst empty */
+
+ vector_copy_append(dst, src) ; /* Extend dst and copy src */
+ vector_init_new(src, 0) ; /* Set empty, forgetting body */
+
+ return dst ;
+} ;
+
+/*==============================================================================
+ * Portmanteau splice/extract/replace function.
+ *
+ * All take a portion of 'src' vector and:
+ *
+ * splice:
+ *
+ * a) replace a portion of the 'dst' vector by a portion of the 'src' vector
+ * copying the replaced portion of the 'dst' vector to the 'to' vector
+ * b) either: leave 'src' unchanged -- copy
+ * or: remove the stuff copied from 'src' -- move
+ *
+ * Arguments: to_copy -- true
+ * to -- vector, or NULL => allocate new vector
+ * dst -- vector
+ * i_dst -- start of portion to replace
+ * n_dst -- length of portion to replace. 0 => insertion.
+ * src -- vector, or NULL => nothing to copy/move
+ * i_src -- start of portion to copy/move
+ * n_src -- length of portion to copy/move. 0 => nothing.
+ * src_move -- true => move, otherwise copy.
+ *
+ * Returns: the (possibly new) 'to' vector
+ *
+ * NB: 'to', 'dst' and 'src' must be distinct vectors.
+ *
+ * extract:
+ *
+ * a) copy a portion of the 'src' vector to the 'to' vector
+ * c) either: leave 'src' unchanged -- copy
+ * or: remove the stuff copied from 'src' -- move
+ *
+ * Arguments: to_copy -- true
+ * to -- vector, or NULL => allocate new vector
+ * dst -- NULL
+ * i_dst -- ignored
+ * n_dst -- ignored
+ * src -- vector, or NULL => nothing to copy/move
+ * i_src -- start of portion to copy/move
+ * n_src -- length of portion to copy/move. 0 => nothing.
+ * src_move -- true => move, otherwise copy.
+ *
+ * Returns: the (possibly new) 'to' vector
+ *
+ * NB: 'to' and 'src' must be distinct vectors.
+ *
+ * replace:
+ *
+ * a) replace a portion of the 'dst' vector by a portion of the 'src' vector
+ * b) either: leave 'src' unchanged -- copy
+ * or: remove the stuff copied from 'src' -- move
+ *
+ * Arguments: to_copy -- false
+ * to -- ignored
+ * dst -- vector
+ * i_dst -- start of portion to replace
+ * n_dst -- length of portion to replace. 0 => insertion.
+ * src -- vector, or NULL => nothing to copy/move
+ * i_src -- start of portion to copy/move
+ * n_src -- length of portion to copy/move. 0 => nothing.
+ * src_move -- true => move, otherwise copy.
+ *
+ * Returns: original 'to' argument
+ *
+ * NB: 'dst' and 'src' must be distinct vectors.
+ *
+ * All copies are shallow -- pointers to item values are copied, not the values.
+ *
+ * NB: any existing contents of the 'to' vector are discarded.
+ *
+ * NB: it is the caller's responsibility to release memory allocated to any
+ * items which are discarded in these operations.
+ *
+ * NB: for splice and replace, the resulting destination vector will be at
+ * least i_dst + n_src long. (Even if is copying actual or implied NULLs
+ * from the source.)
+ *
+ * NB: where new vectors are created, they will be of exactly the required size.
+ *
+ * NB: where an existing vector is reused, it is the caller's responsibility
+ * to ensure the vector structure is currently valid (by vector_init_new()
+ * or by ensuring it is zeroized).
+ */
+vector
+vector_sak(int to_copy, vector to,
+ vector dst, vector_index i_dst, unsigned int n_dst,
+ vector src, vector_index i_src, unsigned int n_src, int src_move)
+{
+ int dst_replace ; /* true => replace portion of 'dst' */
+
+ vector_index new_dst_end = 0 ; /* new end of dst */
+
+ unsigned int n_dst_nulls ; /* number of implicit NULLs to add */
+ unsigned int n_dst_move ; /* number of items to move up or down */
+ unsigned int n_src_real ; /* number of items to really copy */
+ unsigned int n_src_nulls ; /* number of implicit NULLs to "copy" */
+
+ assert((to == NULL) || (dst == NULL) || (to != dst)) ;
+ assert((src == NULL) || (dst == NULL) || (src != dst)) ;
+
+ /* Worry about how much we really have in the source vector. */
+
+ n_src_real = n_src ; /* assume all real */
+ n_src_nulls = 0 ; /* so no NULLs to "copy" */
+
+ if (n_src != 0)
+ {
+ if ((src == NULL) || (i_src >= src->end))
+ n_src_real = 0 ;
+ else if ((i_src + n_src) > src->end)
+ n_src_real = src->end - i_src ;
+ n_src_nulls = n_src - n_src_real ;
+ } ;
+
+ /* If no 'dst' vector, then this is an extract. */
+
+ n_dst_move = 0 ; /* assume nothing to move */
+ n_dst_nulls = 0 ; /* assume no NULLs to add */
+
+ if (dst == NULL)
+ /* For extract: set up dst, i_dst and n_dst so that can copy to the */
+ /* 'to' vector as if from 'dst'. */
+ {
+ dst_replace = 0 ; /* no replacement operation */
+ dst = src ; /* copy from here */
+ i_dst = i_src ;
+ n_dst = n_src_real ;
+ }
+ else
+ /* Reduce n_dst to the number of actual items to be replaced. */
+ /* */
+ /* Calculate the new end of 'dst'. */
+ {
+ dst_replace = 1 ; /* have replacement to do */
+ if (i_dst >= dst->end)
+ /* If i_dst is beyond the end of 'dst', then there is nothing */
+ /* to replace (so set n_dst == 0). Will be adding n_src items */
+ /* at i_dst -- so new end must be i_dst + n_src. */
+ {
+ n_dst_nulls = i_dst - dst->end ; /* fill from end to i_dst */
+ n_dst = 0 ; /* nothing to replace */
+ new_dst_end = i_dst + n_src ; /* all beyond current end */
+ }
+ else
+ /* If i_dst + n_dst is beyond the end of 'dst', reduce n_dst to */
+ /* number of items up to the end. */
+ /* Will remove n_dst items and insert n_src, so end will move */
+ /* by n_src - n_dst. */
+ {
+ if ((i_dst + n_dst) > dst->end)
+ n_dst = dst->end - i_dst ; /* what we actually replace */
+ else if (n_dst != n_src)
+ n_dst_move = dst->end - (i_dst + n_dst) ;
+ /* what we move up or down */
+
+ new_dst_end = dst->end + n_src - n_dst ;
+ /* end depends on amount added */
+ /* & amount actually replaced */
+ } ;
+ } ;
+
+ /* Copy portion of 'dst' (or of 'src') to 'to', if required. */
+ /* */
+ /* Have arranged: n_dst -- number of items to copy, all existent */
+ /* dst -- vector to copy from -- if n_dst > 0 */
+ /* i_dst -- first item to copy -- if n_dst > 0 */
+
+ if (to_copy)
+ {
+ to = vector_re_init(to, n_dst) ; /* reinitialise or create */
+ to->end = n_dst ;
+ if (n_dst > 0)
+ memcpy(to->p_items, &dst->p_items[i_dst], P_ITEMS_SIZE(n_dst)) ;
+ } ;
+
+ /* Replace portion of 'dst' by portion of 'src', if required. */
+ /* */
+ /* Have arranged: */
+ /* */
+ /* new_dst_end -- end of dst once dust settles */
+ /* n_dst_nulls -- number of NULLs to insert at dst->end to fill up */
+ /* to i_dst (when i_dst is beyond old end.) */
+ /* n_dst_move -- number of items in dst to move up or down to */
+ /* leave n_src item hole at i_dst to fill in. */
+ /* n_src_real -- number of real src items at i_src to copy to dst */
+ /* at i_dst. */
+ /* n_src_nulls -- number of nulls to add to fill to i_dst + n_src. */
+
+ if (dst_replace)
+ {
+ if (new_dst_end > dst->limit) /* extend body if required */
+ vector_new_limit(dst, new_dst_end) ;
+
+ if (n_dst_nulls > 0)
+ memset(&dst->p_items[dst->end], 0, P_ITEMS_SIZE(n_dst_nulls)) ;
+ if (n_dst_move > 0)
+ memmove(&dst->p_items[i_dst + n_dst], &dst->p_items[i_dst + n_src],
+ P_ITEMS_SIZE(n_dst_move)) ;
+ if (n_src_real > 0)
+ memcpy(&dst->p_items[i_dst], &src->p_items[i_src],
+ P_ITEMS_SIZE(n_src_real)) ;
+ if (n_src_nulls > 0)
+ memset(&dst->p_items[i_dst + n_src_real], 0,
+ P_ITEMS_SIZE(n_src_nulls)) ;
+ dst->end = new_dst_end ;
+ } ;
+
+ /* Delete portion of 'src', if required (and have 'src' !) */
+
+ if (src_move && (n_src_real != 0))
+ vector_delete(src, i_src, n_src_real) ;
+
+ /* Done -- return 'to' vector as promised. */
+
+ return to ;
+} ;
+
+/*==============================================================================
+ * Legacy Vector Operations
+ */
+
+/* This function only returns next empty slot index. It does not mean
the slot's index memory is assigned, please call vector_ensure()
- after calling this function. */
+ after calling this function.
+
+ Index returned is <= current (logical) end.
+*/
int
vector_empty_slot (vector v)
{
- unsigned int i;
-
- if (v->active == 0)
- return 0;
+ vector_index i;
- for (i = 0; i < v->active; i++)
- if (v->index[i] == 0)
- return i;
+ for (i = 0; i < v->end; i++)
+ if (v->p_items[i] == NULL)
+ break ;
return i;
}
@@ -113,77 +818,406 @@ vector_empty_slot (vector v)
int
vector_set (vector v, void *val)
{
- unsigned int i;
-
- i = vector_empty_slot (v);
- vector_ensure (v, i);
-
- v->index[i] = val;
+ vector_index i;
+ i = vector_empty_slot(v) ; /* NB: i <= v->end */
+ if (i == v->end)
+ i = vector_extend_by_1(v) ;
- if (v->active <= i)
- v->active = i + 1;
+ v->p_items[i] = val;
return i;
}
/* Set value to specified index slot. */
int
-vector_set_index (vector v, unsigned int i, void *val)
+vector_set_index (vector v, vector_index i, void *val)
{
vector_ensure (v, i);
-
- v->index[i] = val;
-
- if (v->active <= i)
- v->active = i + 1;
-
+ v->p_items[i] = val;
return i;
}
/* Look up vector. */
-void *
-vector_lookup (vector v, unsigned int i)
+p_vector_item
+vector_lookup (vector v, vector_index i)
{
- if (i >= v->active)
+ if (i >= v->end)
return NULL;
- return v->index[i];
+ return v->p_items[i];
}
/* Lookup vector, ensure it. */
-void *
-vector_lookup_ensure (vector v, unsigned int i)
+p_vector_item
+vector_lookup_ensure (vector v, vector_index i)
{
vector_ensure (v, i);
- return v->index[i];
+ return v->p_items[i];
+}
+
+/* Count the number of not empty slots. */
+vector_index
+vector_count (vector v)
+{
+ vector_index i;
+ unsigned count = 0;
+
+ for (i = 0; i < v->end; i++)
+ if (v->p_items[i] != NULL)
+ count++;
+
+ return count;
}
-/* Unset value at specified index slot. */
+/*==============================================================================
+ * Sorting and Searching vector.
+ */
+
+/* Sort the given vector.
+ *
+ * NB: the comparison function receives a pointer to the pointer to the
+ * vector item's value.
+ *
+ * NB: if there are NULL items in the vector, the comparison function MUST
+ * be ready for them.
+ */
+void
+vector_sort(vector v, vector_sort_cmp* cmp)
+{
+ if (v->end <= 1)
+ return ; /* Stop dead if 0 or 1 items */
+
+ typedef int qsort_cmp(const void*, const void*) ;
+
+ qsort(v->p_items, v->end, sizeof(p_vector_item), (qsort_cmp*)cmp) ;
+} ;
+
+/* Perform binary search on the given vector.
+ *
+ * The vector MUST be sorted in the order implied by the comparison function
+ * given. The vector need not contain unique values, but this search makes
+ * no effort to select any particular instance of a sequence of equals.
+ *
+ * Returns:
+ *
+ * result == 0: found an equal value.
+ * index returned is of first entry found which is equal to
+ * the value sought. There may be other equal values, before
+ * and/or after this one in the vector.
+ *
+ * result == +1: value not found and vector not empty.
+ * index returned is of the largest entry whose value is less
+ * than the value sought.
+ * (The value sought belongs after this point.)
+ *
+ * result == -1: value is less than everything in the vector, or the
+ * vector is empty.
+ * index returned is 0
+ *
+ * NB: The comparison function takes arguments which are:
+ *
+ * const void** pointer to pointer to value being searched for.
+ * const void** pointer to pointer to vector item to compare with
+ *
+ * The value being searched for need not be in the same form as a vector
+ * item. However, if it is then the same comparison function can be used
+ * to sort and search.
+ *
+ * NB: if there are NULL items in the vector, the comparison function MUST
+ * be ready for them.
+ */
+vector_index
+vector_bsearch(vector v, vector_bsearch_cmp* cmp, const void* p_val,
+ int* result)
+{
+ vector_index il, iv, ih ;
+ int c ;
+
+ if (v->end <= 1)
+ {
+ *result = (v->end == 0) ? -1 : cmp(&p_val, (const void**)&v->p_items[0]) ;
+ return 0 ; /* Stop dead if 0 or 1 items */
+ } ;
+
+ /* We have at least two items. */
+ il = 0 ;
+ ih = v->end - 1 ;
+
+ /* Pick off the edge cases: >= last and <= first. */
+ if ((c = cmp(&p_val, (const void**)&v->p_items[ih])) >= 0)
+ {
+ *result = c ; /* 0 => found. +1 => val > last */
+ return ih ; /* return high index. */
+ } ;
+ if ((c = cmp(&p_val, (const void**)&v->p_items[il])) <= 0)
+ {
+ *result = c ; /* 0 => found. -1 => val < first */
+ return il ; /* return low index. */
+ }
+
+ /* Now binary chop. We know that item[il] < val < item[ih] */
+ /* We also know that il < ih */
+ while (1)
+ {
+ iv = (il + ih) / 2 ;
+ if (iv == il) /* true if (ih == il+1) */
+ {
+ *result = +1 ;
+ return il ; /* return il: item[il] < val < item[il+1] */
+ } ;
+ /* We now know that il < iv < ih */
+ c = cmp(&p_val, (const void**)&v->p_items[iv]) ;
+ if (c == 0)
+ {
+ *result = 0 ;
+ return iv ; /* found !! */
+ }
+ if (c < 0)
+ ih = iv ; /* step down iv > il, so new ih > il */
+ else
+ il = iv ; /* step up iv < ih, so new il < ih */
+ } ;
+} ;
+
+/*==============================================================================
+ * Mechanics for adding/deleting items and managing the vector (logical) end
+ * and (physical) limit.
+ */
+
+/* Extract the LS bit of unsigned integer 'x'. */
+#define lsbit(x) ((x) & ((~(x)) + 1))
+/* Round 'x' up to a multiple of 'm' */
+#define multiple(x, m) ((((x) + (m) - 1) / (m)) * (m))
+
+/* Set new limit to suit new end for given vector.
+ *
+ * The new limit will be at least: VECTOR_LIMIT_MIN.
+ *
+ * While the vector is relatively small, the limit is doubled until there
+ * is at least 1/8 of the new vector free.
+ *
+ * Beyond VECTOR_LIMIT_DOUBLE_MAX, however, the limit is set to the
+ * smallest multiple of VECTOR_LIMIT_DOUBLE_MAX which gives at least
+ * VECTOR_LIMIT_SLACK_MIN free entries beyond the new end.
+ *
+ * This is an attempt to balance the cost of repeated reallocations of
+ * memory against the cost of possible wasted space at the end of the
+ * vector.
+ *
+ * NB: the new_limit depends entirely on the new end position. (Current
+ * end position is ignored.)
+ *
+ * NB: the new limit may be less than the current limit, in which case the
+ * vector body is reduced in size.
+ *
+ * Except for any size set when the vector is initialised, the vector body
+ * size will be a power of 2 or a multiple of VECTOR_LIMIT_DOUBLE_MAX.
+ * (Vectors are regular in their habits, which may help the memory allocator).
+ *
+ * TODO: what to do if calculation of new_limit overflows, or calculation
+ * of P_ITEMS_SIZE will ?
+ */
+static void
+vector_new_limit(vector v, vector_index new_end)
+{
+ vector_index old_limit = v->limit ;
+ vector_index new_limit ;
+
+ if (new_end > ((VECTOR_LIMIT_DOUBLE_MAX * 7) / 8))
+ {
+ new_limit = multiple(new_end + VECTOR_LIMIT_SLACK_MIN,
+ VECTOR_LIMIT_DOUBLE_MAX) ;
+ }
+ else
+ {
+ /* Want the new_limit to be a power of 2. */
+ /* If the old_limit was a power of 2, start from there. */
+ /* Otherwise start from a power of 2 less than new_end: either the */
+ /* minimum value or a value mid way to VECTOR_LIMIT_DOUBLE_MAX. */
+ if ( (old_limit != 0) && (old_limit == lsbit(old_limit))
+ && (new_end >= old_limit) )
+ new_limit = old_limit ;
+ else
+ new_limit = (new_end < VECTOR_LIMIT_MID) ? VECTOR_LIMIT_MIN
+ : VECTOR_LIMIT_MID ;
+ while (new_end > ((new_limit * 7) / 8))
+ new_limit *= 2 ;
+ } ;
+
+ v->p_items =
+ XREALLOC(MTYPE_VECTOR_BODY, v->p_items, P_ITEMS_SIZE(new_limit)) ;
+
+ v->limit = new_limit ;
+} ;
+
+/* Extend vector and set new (logical) end.
+ *
+ * Extends body if required.
+ * Ensures everything between old and new end is set NULL.
+ *
+ * NB: expects new end > old end, but copes with new end <= old end.
+ */
+void
+vector_extend(vector v, vector_index new_end)
+{
+ vector_index old_end = v->end ;
+
+ if (new_end > v->limit)
+ vector_new_limit(v, new_end) ;
+ v->end = new_end ;
+
+ if (new_end > old_end)
+ memset(&v->p_items[old_end], 0, P_ITEMS_SIZE(new_end - old_end)) ;
+} ;
+
+/* Insert entries into vector: insert 'n' NULL entries at location 'i'.
+ *
+ * Updates end (and limit) to be at least 'i' + 'n'.
+ * (So if 'i' < end then end becomes end + 'n', else end becomes 'i' + 'n'.)
+ */
+void
+vector_insert(vector v, vector_index i, unsigned int n)
+{
+ vector_index old_end, new_end ;
+ unsigned int n_above ;
+
+ /* If i < old end, then we are inserting n NULLs, and need
+ * to shuffle at least one item up.
+ * else we are setting new end to i + n and need to NULL
+ * fill from old end to the new end.
+ */
+ old_end = v->end ;
+ if (i < old_end)
+ {
+ if (n == 0)
+ return ; /* give up now if not inserting anything */
+ n_above = old_end - i ; /* number of items to shuffle up.. >= 1 */
+ new_end = old_end + n ;
+ }
+ else
+ {
+ n_above = 0 ; /* nothing to shuffle up. */
+ new_end = i + n ;
+ i = old_end ; /* where to zeroize from */
+ n = new_end - old_end ; /* how much to zeroize */
+ } ;
+
+ /* Now we extend the body if we need to. */
+ if (new_end > v->limit)
+ vector_new_limit(v, new_end) ;
+ v->end = new_end ;
+
+ if (n_above > 0)
+ memmove(&v->p_items[i + n], &v->p_items[i], P_ITEMS_SIZE(n_above)) ;
+
+ memset(&v->p_items[i], 0, P_ITEMS_SIZE(n)) ;
+} ;
+
+/* Delete items from vector: delete 'n' items at location 'i'.
+ *
+ * Does nothing if 'i' is beyond current end of vector or if 'n' == 0.
+ *
+ * Deletes from 'i' to end if less than 'n' items to the end.
+ *
+ * NB: does NOT change the size of the body.
+ *
+ * NB: it is the caller's responsibility to have released any memory allocated
+ * for the items that are being deleted.
+*/
void
-vector_unset (vector v, unsigned int i)
+vector_delete(vector v, vector_index i, unsigned int n)
{
- if (i >= v->alloced)
- return;
+ vector_index old_end, new_end ;
+
+ old_end = v->end ;
- v->index[i] = NULL;
+ if ((i >= old_end) || (n == 0))
+ return ;
- if (i + 1 == v->active)
+ /* If i + n < old_end, we have 1 or more items to keep and move down */
+ if ((i + n) < old_end)
{
- v->active--;
- while (i && v->index[--i] == NULL && v->active--)
- ; /* Is this ugly ? */
+ memmove(&v->p_items[i], &v->p_items[i + n],
+ P_ITEMS_SIZE(old_end - (i + n))) ;
+ new_end = old_end - n ;
}
-}
+ else
+ {
+ new_end = i ; /* We are keeping nothing above 'i' */
+ /* NB: new_end < old_end */
+ } ;
-/* Count the number of not emplty slot. */
-unsigned int
-vector_count (vector v)
+ v->end = new_end ; /* account for stuff dropped */
+} ;
+
+/* Discard entries from vector: discard entries from location 'i' onwards.
+ *
+ * Releases memory from 'i' onwards.
+ * Releases the body altogether if this sets the vector empty ('i' == 0).
+ * Sets new end of vector iff 'i' < current end.
+ *
+ * Does nothing if 'i' is beyond current limit (physical end) of vector.
+ *
+ * NB: it is the caller's responsibility to have released any memory allocated
+ * for the items that are being discarded.
+*/
+void
+vector_discard(vector v, vector_index i)
{
- unsigned int i;
- unsigned count = 0;
+ if (i >= v->limit)
+ return ;
- for (i = 0; i < v->active; i++)
- if (v->index[i] != NULL)
- count++;
+ if (i == 0)
+ vector_reset(v, 0) ; /* reset, without releasing the structure */
+ else
+ {
+ v->limit = i ;
+ if (i < v->end)
+ v->end = i ;
+ v->p_items = XREALLOC(MTYPE_VECTOR_BODY, v->p_items, P_ITEMS_SIZE(i)) ;
+ } ;
+} ;
- return count;
-}
+/* Chop vector at the current (logical) end.
+ *
+ * Releases the body altogether if the vector is currently empty.
+*/
+void
+vector_chop(vector v)
+{
+ vector_index new_limit = v->end ;
+
+ if (new_limit == 0)
+ vector_reset(v, 0) ; /* reset, without releasing the structure */
+ else if (new_limit != v->limit)
+ {
+ v->limit = new_limit ;
+ v->p_items = XREALLOC(MTYPE_VECTOR_BODY, v->p_items, P_ITEMS_SIZE(new_limit)) ;
+ } ;
+} ;
+
+/* Decant vector into a new body allocated to current logical size, and
+ * release the old body.
+ *
+ * Releases the body altogether if the vector is currently empty.
+*/
+void
+vector_decant(vector v)
+{
+ p_vector_item* p_old_body ;
+ vector_index new_limit = v->end ;
+
+ if (new_limit == 0)
+ vector_reset(v, 0) ; /* reset, without releasing the structure */
+ else
+ {
+ p_old_body = v->p_items ;
+
+ vector_init_new(v, new_limit) ; /* initialise with new body */
+
+ memcpy(v->p_items, p_old_body, P_ITEMS_SIZE(new_limit)) ;
+ /* copy the old body across */
+ v->end = new_limit ;
+
+ XFREE(MTYPE_VECTOR_BODY, p_old_body) ;
+ } ;
+} ;
diff --git a/lib/vector.h b/lib/vector.h
index 6b27fd96..2897cb51 100644
--- a/lib/vector.h
+++ b/lib/vector.h
@@ -1,7 +1,9 @@
-/*
- * Generic vector interface header.
+/* Generic vector interface header.
* Copyright (C) 1997, 98 Kunihiro Ishiguro
*
+ * 24-Nov-2009 -- extended to add a number of new operations on vectors.
+ * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
+ *
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
@@ -17,47 +19,272 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#ifndef _ZEBRA_VECTOR_H
#define _ZEBRA_VECTOR_H
-/* struct for vector */
-struct _vector
+/* Macro in case there are particular compiler issues. */
+#ifndef Inline
+ #define Inline static inline
+#endif
+
+/* types and struct for vector */
+/* */
+/* NB: an entirely zero structure represents an entirely empty vector. */
+/* */
+/* TODO: could force vector_index to be 32 bits ? */
+
+typedef void* p_vector_item ;
+typedef unsigned int vector_index ;
+
+struct vector
{
- unsigned int active; /* number of active slots */
- unsigned int alloced; /* number of allocated slot */
- void **index; /* index to data */
+ p_vector_item *p_items ; /* pointer to array of vector item pointers */
+ vector_index end ; /* number of "active" item entries */
+ vector_index limit ; /* number of allocated item entries */
};
-typedef struct _vector *vector;
+typedef struct vector *vector;
+
+/* Values that control the allocation of the vector body. */
+/* NB: these must all be powers of 2. */
+
+/* The body, when allocated, will have at least this many entries. */
+#define VECTOR_LIMIT_MIN 8
+/* When the body grows, it doubles in size, until it is this big. */
+/* After that it grows in units of this much. */
+#define VECTOR_LIMIT_DOUBLE_MAX 2048
+/* "Midway" between VECTOR_LIMIT_MIN and VECTOR_LIMIT_DOUBLE_MAX. */
+#define VECTOR_LIMIT_MID 128
+/* When growing in units of VECTOR_LIMIT_DOUBLE_MAX, this is the */
+/* minimum slack space to leave after the logical end of the vector. */
+#define VECTOR_LIMIT_SLACK_MIN ((VECTOR_LIMIT_DOUBLE_MAX) / 8)
+
+/* (Sometimes) useful macros. */
-#define VECTOR_MIN_SIZE 1
+/* Reference item at given index. */
+/* NB: does not guarantee the item is "active" (that is: within the */
+/* (logical) vector) -- but see vector_ensure. */
+/* Returns address of vector item. */
+/* See: VECTOR_ITEMS() for walking items in a vector. */
+/* See: vector_get_item(), which is preferable. */
+#define vector_slot(V,I) ((V)->p_items[(I)])
+
+/* Number of "active" item entries -- (logical) end of the vector. */
+/* Note that this differs from vector_count() as this count will */
+/* include any NULL items. */
+#define vector_active(V) ((V)->end)
+
+/* TODO: fix where this is used to poke around inside a vector */
+#define VECTOR_INDEX p_items
+
+/* To walk all items in a vector:
+ *
+ * vector_index i ;
+ * xxxxx* p_v ;
+ *
+ * for (VECTOR_ITEMS(v, p_v, i))
+ * {
+ * ... i is index of current item
+ * ... p_v is address of current item value -- may be NULL
+ * } ;
+ *
+ * ... i is number of items in vector (including any NULLs)
+ */
+#define VECTOR_ITEMS(v, p_v, i)\
+ (i) = 0 ;\
+ (i) < (v)->end ? (((p_v) = (void*)(v)->p_items[i]), 1) \
+ : (((p_v) = NULL), 0) ;\
+ ++(i)
-/* (Sometimes) usefull macros. This macro convert index expression to
- array expression. */
-/* Reference slot at given index, caller must ensure slot is active */
-#define vector_slot(V,I) ((V)->index[(I)])
-/* Number of active slots.
- * Note that this differs from vector_count() as it the count returned
- * will include any empty slots
+/*==============================================================================
+ * Prototypes.
*/
-#define vector_active(V) ((V)->active)
-/* Prototypes. */
extern vector vector_init (unsigned int size);
-extern void vector_ensure (vector v, unsigned int num);
+Inline void vector_ensure(vector v, vector_index i) ;
extern int vector_empty_slot (vector v);
extern int vector_set (vector v, void *val);
-extern int vector_set_index (vector v, unsigned int i, void *val);
-extern void vector_unset (vector v, unsigned int i);
-extern unsigned int vector_count (vector v);
+extern int vector_set_index (vector v, vector_index i, void *val);
+#define vector_unset(v, i) (void)vector_unset_item(v, i)
+extern vector_index vector_count (vector v);
extern void vector_only_wrapper_free (vector v);
extern void vector_only_index_free (void *index);
extern void vector_free (vector v);
extern vector vector_copy (vector v);
-extern void *vector_lookup (vector, unsigned int);
-extern void *vector_lookup_ensure (vector, unsigned int);
+extern void *vector_lookup (vector, vector_index);
+extern void *vector_lookup_ensure (vector, vector_index);
+
+extern vector vector_init_new(vector v, unsigned int size) ;
+extern vector vector_re_init(vector v, unsigned int size) ;
+extern vector vector_reset(vector v, int free_structure) ;
+extern p_vector_item vector_ream(vector v, int free_structure) ;
+
+/* Reset vector and free the vector structure. */
+#define vector_reset_free(v) vector_reset(v, 1)
+/* Reset vector but free the heap structure. */
+#define vector_reset_keep(v) vector_reset(v, 0)
+/* Ream out vector and free the vector structure. */
+#define vector_ream_free(v) vector_ream(v, 1)
+/* Ream out vector but keep the vector structure. */
+#define vector_ream_keep(v) vector_ream(v, 0)
+
+Inline vector_index vector_end(vector v) ;
+Inline int vector_is_empty(vector v) ;
+
+Inline p_vector_item vector_get_item(vector v, vector_index i) ;
+Inline p_vector_item vector_get_first_item(vector v) ;
+Inline p_vector_item vector_get_last_item(vector v) ;
+Inline void vector_set_item(vector v, vector_index i, p_vector_item p_v) ;
+extern p_vector_item vector_unset_item(vector v, vector_index i) ;
+
+extern void vector_insert_item(vector v, vector_index i, p_vector_item p_v) ;
+extern void vector_insert_item_here(vector v, vector_index i, int rider,
+ p_vector_item p_v) ;
+extern void vector_move_item(vector v, vector_index dst, vector_index src) ;
+extern void vector_move_item_here(vector v, vector_index dst, int rider,
+ vector_index src) ;
+extern p_vector_item vector_delete_item(vector v, vector_index i) ;
+extern void vector_reverse(vector v) ;
+extern void vector_part_reverse(vector v, vector_index i, unsigned int n) ;
+
+Inline void vector_push_item(vector v, p_vector_item p_v) ;
+Inline p_vector_item vector_pop_item(vector v) ;
+
+extern void vector_insert(vector v, vector_index i, unsigned int n) ;
+extern void vector_delete(vector v, vector_index i, unsigned int n) ;
+
+typedef int vector_bsearch_cmp(const void** pp_val, const void** item) ;
+vector_index vector_bsearch(vector v, vector_bsearch_cmp* cmp,
+ const void* p_val, int* result) ;
+typedef int vector_sort_cmp(const void** a, const void** b) ;
+void vector_sort(vector v, vector_sort_cmp* cmp) ;
+
+extern vector vector_copy_here(vector dst, vector src) ;
+extern vector vector_move_here(vector dst, vector src) ;
+extern vector vector_copy_append(vector dst, vector src) ;
+extern vector vector_move_append(vector dst, vector src) ;
+
+#define vector_copy_extract(to, src, i_src, n_src) \
+ vector_sak(1, to, NULL, 0, 0, src, i_src, n_src, 0)
+#define vector_move_extract(to, src, i_src, n_src) \
+ vector_sak(1, to, NULL, 0, 0, src, i_src, n_src, 1)
+#define vector_copy_splice(to, dst, i_dst, n_dst, src, i_src, n_src) \
+ vector_sak(1, to, dst, i_dst, n_dst, src, i_src, n_src, 0)
+#define vector_move_splice(to, dst, i_dst, n_dst, src, i_src, n_src) \
+ vector_sak(1, to, dst, i_dst, n_dst, src, i_src, n_src, 1)
+#define vector_copy_replace(dst, i_dst, n_dst, src, i_src, n_src) \
+ vector_sak(0, NULL, dst, i_dst, n_dst, src, i_src, n_src, 0)
+#define vector_move_replace(dst, i_dst, n_dst, src, i_src, n_src) \
+ vector_sak(0, NULL, dst, i_dst, n_dst, src, i_src, n_src, 1)
+
+extern vector vector_sak(int to_copy, vector to,
+ vector dst, vector_index i_dst, unsigned int n_dst,
+ vector src, vector_index i_src, unsigned int n_src, int src_move) ;
+
+extern void vector_discard(vector v, vector_index i) ;
+extern void vector_chop(vector v) ;
+extern void vector_decant(vector v) ;
+
+Inline vector_index vector_extend_by_1(vector v) ;
+extern void vector_extend(vector v, vector_index new_end) ;
+
+/*==============================================================================
+ * The inline functions:
+ */
+
+/* Extend vector by one item at the end, which is about to be set. */
+/* Returns index of new least item in the vector. */
+/* NB: if left unset, the item may be UNDEFINED. */
+Inline vector_index
+vector_extend_by_1(vector v)
+{
+ vector_index i = v->end ;
+
+ if (i < v->limit)
+ return v->end++ ; /* simple if we have room */
+
+ vector_extend(v, i + 1) ; /* the hard way */
+ return i ;
+} ;
+
+/* Ensure given index is "active". */
+/* Adjusts logical and physical end of the vector as required, filling */
+/* with NULLs upto any new logical end. */
+Inline void
+vector_ensure(vector v, vector_index i)
+{
+ if (i < v->end) /* trivial if within vector */
+ return ;
+ if ((i == v->end) && (i < v->limit)) /* simple if end and have room */
+ v->p_items[v->end++] = NULL ; /* set NULL for complete safety */
+ else
+ vector_extend(v, i + 1) ; /* do it the hard way */
+} ;
+
+/* Return index of end of vector (index of last item + 1) */
+Inline vector_index
+vector_end(vector v)
+{
+ return v->end ;
+} ;
+
+/* Returns whether vector is empty or not. */
+Inline int
+vector_is_empty(vector v)
+{
+ return (v->end == 0) ;
+} ;
+
+/* Access functions -- Inline for obvious reasons. */
+
+/* Get pointer to item. Returns NULL if accessing beyond end. */
+Inline p_vector_item
+vector_get_item(vector v, vector_index i)
+{
+ return (i < v->end) ? v->p_items[i] : NULL ;
+} ;
+
+/* Get pointer to first item. Returns NULL if vector empty. */
+Inline p_vector_item
+vector_get_first_item(vector v)
+{
+ return (v->end != 0) ? v->p_items[0] : NULL ;
+} ;
+
+/* Get pointer to last item. Returns NULL if vector empty. */
+Inline p_vector_item
+vector_get_last_item(vector v)
+{
+ return (v->end != 0) ? v->p_items[v->end - 1] : NULL ;
+} ;
+
+/* Set item value in vector. Extend vector if required. */
+/* NB: it is the caller's responsibility to release memory used by any */
+/* current value of the item, if required. */
+Inline void
+vector_set_item(vector v, vector_index i, void* p_v)
+{
+ vector_ensure(v, i) ;
+ v->p_items[i] = (p_vector_item)p_v ;
+} ;
+
+/* Push value onto vector, extending as required. */
+Inline void
+vector_push_item(vector v, void* p_v)
+{
+ vector_index i = vector_extend_by_1(v) ;
+ v->p_items[i] = (p_vector_item)p_v ;
+} ;
+
+/* Pop value from vector. Returns NULL if vector is empty. */
+/* NB: does NOT change the size of the vector body. */
+Inline p_vector_item
+vector_pop_item(vector v)
+{
+ return (v->end > 0) ? v->p_items[--v->end] : NULL ;
+} ;
#endif /* _ZEBRA_VECTOR_H */
diff --git a/lib/vty.c b/lib/vty.c
index e4818eb6..6b627a30 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#include <zebra.h>
@@ -38,9 +38,37 @@
#include "network.h"
#include <arpa/telnet.h>
+#include "qpthreads.h"
+#include "qpnexus.h"
+
+/* Needs to be qpthread safe */
+qpt_mutex_t* vty_mutex = NULL;
+#ifdef NDEBUG
+#define LOCK qpt_mutex_lock(vty_mutex);
+#define UNLOCK qpt_mutex_unlock(vty_mutex);
+#else
+int vty_lock_count = 0;
+#define LOCK qpt_mutex_lock(vty_mutex);++vty_lock_count;
+#define UNLOCK --vty_lock_count;qpt_mutex_unlock(vty_mutex);
+#endif
+
+/*
+ * To make vty qpthread safe we use a single mutex. In general external
+ * routines have explicit locks, static routines assume that they are being
+ * called with the mutex already locked. There are a few exceptions, e.g.
+ * callbacks where static routines are being invoked from outside the module.
+ *
+ * As we are not using recursive mutexes so there are a few cases where
+ * both external and static versions of a routine exist. The former for use
+ * outside, the latter for use inside the module (and lock). In these cases
+ * the internal static versions starts uty_.
+ *
+ * vty and log recurse through each other, so the same mutex is used
+ * for both, i.e. they are treated as being part of the same monitor.
+ */
/* Vty events */
-enum event
+enum event
{
VTY_SERV,
VTY_READ,
@@ -53,11 +81,27 @@ enum event
#endif /* VTYSH */
};
+/* Prototypes */
+static int uty_out (struct vty *vty, const char *format, ...);
+static int uty_vout(struct vty *vty, const char *format, va_list args);
static void vty_event (enum event, int, struct vty *);
+static void uty_hello (struct vty *vty);
+static void uty_close (struct vty *vty);
+static int uty_config_unlock (struct vty *vty);
+static int uty_shell (struct vty *vty);
+static int uty_read (struct vty *vty, int vty_sock);
+static int uty_flush (struct vty *vty, int vty_sock);
+static void vty_event_t (enum event event, int sock, struct vty *vty);
+static void vty_event_r (enum event event, int sock, struct vty *vty);
+static int uty_accept (int accept_sock);
+static int uty_timeout (struct vty *vty);
+static void vty_timeout_r (qtimer qtr, void* timer_info, qtime_t when);
+static void vty_read_r (qps_file qf, void* file_info);
+static void vty_flush_r (qps_file qf, void* file_info);
/* Extern host structure from command.c */
extern struct host host;
-
+
/* Vector which store each vty structure. */
static vector vtyvec;
@@ -71,10 +115,10 @@ static char *vty_accesslist_name = NULL;
static char *vty_ipv6_accesslist_name = NULL;
/* VTY server thread. */
-vector Vvty_serv_thread;
+static vector Vvty_serv_thread;
/* Current directory. */
-char *vty_cwd = NULL;
+static char *vty_cwd = NULL;
/* Configure lock. */
static int vty_config;
@@ -89,29 +133,60 @@ static u_char restricted_mode = 0;
/* Integrated configuration file path */
char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
-
-/* VTY standard output function. */
+/* Master of the threads. */
+static struct thread_master *master = NULL;
+static qpn_nexus master_nexus = NULL;
+
+/* VTY standard output function. vty == NULL or VTY_SHELL => stdout */
int
vty_out (struct vty *vty, const char *format, ...)
{
+ int result;
+
+ LOCK
va_list args;
+ va_start (args, format);
+ result = uty_vout(vty, format, args);
+ va_end (args);
+ UNLOCK
+ return result;
+}
+
+/* internal VTY standard output function. vty == NULL or VTY_SHELL => stdout */
+static int
+uty_out (struct vty *vty, const char *format, ...)
+{
+ int result;
+ assert(vty_lock_count);
+ va_list args;
+ va_start (args, format);
+ result = uty_vout(vty, format, args);
+ va_end (args);
+ return result;
+}
+
+/* internal VTY standard output function. vty == NULL or VTY_SHELL => stdout */
+static int
+uty_vout(struct vty *vty, const char *format, va_list args)
+{
int len = 0;
int size = 1024;
char buf[1024];
char *p = NULL;
+ va_list ac;
+
+ assert(vty_lock_count);
- if (vty_shell (vty))
+ if (uty_shell (vty))
{
- va_start (args, format);
vprintf (format, args);
- va_end (args);
}
else
{
/* Try to write to initial buffer. */
- va_start (args, format);
- len = vsnprintf (buf, sizeof buf, format, args);
- va_end (args);
+ va_copy(ac, args);
+ len = vsnprintf (buf, sizeof buf, format, ac);
+ va_end(ac);
/* Initial buffer is not enough. */
if (len < 0 || len >= size)
@@ -125,11 +200,11 @@ vty_out (struct vty *vty, const char *format, ...)
p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
if (! p)
- return -1;
+ return -1;
- va_start (args, format);
- len = vsnprintf (p, size, format, args);
- va_end (args);
+ va_copy(ac, args);
+ len = vsnprintf (p, size, format, ac);
+ va_end(ac);
if (len > -1 && len < size)
break;
@@ -151,6 +226,27 @@ vty_out (struct vty *vty, const char *format, ...)
return len;
}
+int
+vty_puts(struct vty *vty, const char* str)
+{
+ return vty_out(vty, "%s", str) ;
+}
+
+int
+vty_out_newline(struct vty *vty)
+{
+ return vty_out(vty, "%s", VTY_NEWLINE) ;
+}
+
+/* 123456789012345678901234 */
+const char* vty_spaces_string = " " ;
+
+int
+vty_out_indent(struct vty *vty, int indent)
+{
+ return vty_puts(vty, VTY_SPACES(indent)) ;
+}
+
static int
vty_log_out (struct vty *vty, const char *level, const char *proto_str,
const char *format, struct timestamp_control *ctl, va_list va)
@@ -159,9 +255,11 @@ vty_log_out (struct vty *vty, const char *level, const char *proto_str,
int len;
char buf[1024];
+ assert(vty_lock_count);
+
if (!ctl->already_rendered)
{
- ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
+ ctl->len = uquagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
ctl->already_rendered = 1;
}
if (ctl->len+1 >= sizeof(buf))
@@ -192,7 +290,7 @@ vty_log_out (struct vty *vty, const char *level, const char *proto_str,
return -1;
/* Fatal I/O error. */
vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
- zlog_warn("%s: write failed to vty client fd %d, closing: %s",
+ uzlog(NULL, LOG_WARNING, "%s: write failed to vty client fd %d, closing: %s",
__func__, vty->fd, safe_strerror(errno));
buffer_reset(vty->obuf);
/* cannot call vty_close, because a parent routine may still try
@@ -209,7 +307,7 @@ void
vty_time_print (struct vty *vty, int cr)
{
char buf [25];
-
+
if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
{
zlog (NULL, LOG_INFO, "quagga_timestamp error");
@@ -227,6 +325,15 @@ vty_time_print (struct vty *vty, int cr)
void
vty_hello (struct vty *vty)
{
+ LOCK
+ uty_hello(vty);
+ UNLOCK
+}
+
+static void
+uty_hello (struct vty *vty)
+{
+ assert(vty_lock_count);
if (host.motdfile)
{
FILE *f;
@@ -242,15 +349,15 @@ vty_hello (struct vty *vty)
for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
s--);
*s = '\0';
- vty_out (vty, "%s%s", buf, VTY_NEWLINE);
+ uty_out (vty, "%s%s", buf, VTY_NEWLINE);
}
fclose (f);
}
else
- vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
+ uty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
}
else if (host.motd)
- vty_out (vty, host.motd);
+ uty_out (vty, "%s", host.motd);
}
/* Put out prompt and wait input from user. */
@@ -260,6 +367,8 @@ vty_prompt (struct vty *vty)
struct utsname names;
const char*hostname;
+ assert(vty_lock_count);
+
if (vty->type == VTY_TERM)
{
hostname = host.name;
@@ -268,7 +377,7 @@ vty_prompt (struct vty *vty)
uname (&names);
hostname = names.nodename;
}
- vty_out (vty, cmd_prompt (vty->node), hostname);
+ uty_out (vty, cmd_prompt (vty->node), hostname);
}
}
@@ -277,7 +386,8 @@ static void
vty_will_echo (struct vty *vty)
{
unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
- vty_out (vty, "%s", cmd);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", cmd);
}
/* Make suppress Go-Ahead telnet option. */
@@ -285,7 +395,8 @@ static void
vty_will_suppress_go_ahead (struct vty *vty)
{
unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
- vty_out (vty, "%s", cmd);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", cmd);
}
/* Make don't use linemode over telnet. */
@@ -293,7 +404,8 @@ static void
vty_dont_linemode (struct vty *vty)
{
unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
- vty_out (vty, "%s", cmd);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", cmd);
}
/* Use window size. */
@@ -301,7 +413,8 @@ static void
vty_do_window_size (struct vty *vty)
{
unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
- vty_out (vty, "%s", cmd);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", cmd);
}
#if 0 /* Currently not used. */
@@ -310,21 +423,30 @@ static void
vty_dont_lflow_ahead (struct vty *vty)
{
unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
- vty_out (vty, "%s", cmd);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", cmd);
}
#endif /* 0 */
/* Allocate new vty struct. */
struct vty *
-vty_new ()
+vty_new (int fd)
{
- struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
+ struct vty *vty = XCALLOC (MTYPE_VTY, sizeof (struct vty));
- new->obuf = buffer_new(0); /* Use default buffer size. */
- new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
- new->max = VTY_BUFSIZ;
+ vty->obuf = buffer_new(0); /* Use default buffer size. */
+ vty->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
+ vty->max = VTY_BUFSIZ;
+ vty->fd = fd;
- return new;
+ if (master_nexus)
+ {
+ vty->qf = qps_file_init_new(vty->qf, NULL);
+ qps_add_file(master_nexus->selection, vty->qf, vty->fd, vty);
+ vty->qtr = qtimer_init_new(vty->qtr, master_nexus->pile, vty_timeout_r, vty);
+ }
+
+ return vty;
}
/* Authentication of vty */
@@ -336,6 +458,8 @@ vty_auth (struct vty *vty, char *buf)
int fail;
char *crypt (const char *, const char *);
+ assert(vty_lock_count);
+
switch (vty->node)
{
case AUTH_NODE:
@@ -379,14 +503,14 @@ vty_auth (struct vty *vty, char *buf)
{
if (vty->node == AUTH_NODE)
{
- vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
+ uty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
vty->status = VTY_CLOSE;
}
- else
+ else
{
/* AUTH_ENABLE_NODE */
vty->fail = 0;
- vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
+ uty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
}
}
@@ -401,6 +525,8 @@ vty_command (struct vty *vty, char *buf)
vector vline;
const char *protocolname;
+ assert(vty_lock_count);
+
/* Split readline string up into the vector */
vline = cmd_make_strvec (buf);
@@ -416,20 +542,19 @@ vty_command (struct vty *vty, char *buf)
GETRUSAGE(&before);
#endif /* CONSUMED_TIME_CHECK */
+ UNLOCK
ret = cmd_execute_command (vline, vty, NULL, 0);
+ LOCK
/* Get the name of the protocol if any */
- if (zlog_default)
- protocolname = zlog_proto_names[zlog_default->protocol];
- else
- protocolname = zlog_proto_names[ZLOG_NONE];
-
+ protocolname = uzlog_get_proto_name(NULL);
+
#ifdef CONSUMED_TIME_CHECK
GETRUSAGE(&after);
if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
CONSUMED_TIME_CHECK)
/* Warn about CPU hog that must be fixed. */
- zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
+ uzlog(NULL, LOG_WARNING, "SLOW COMMAND: command took %lums (cpu time %lums): %s",
realtime/1000, cputime/1000, buf);
}
#endif /* CONSUMED_TIME_CHECK */
@@ -439,23 +564,23 @@ vty_command (struct vty *vty, char *buf)
{
case CMD_WARNING:
if (vty->type == VTY_FILE)
- vty_out (vty, "Warning...%s", VTY_NEWLINE);
+ uty_out (vty, "Warning...%s", VTY_NEWLINE);
break;
case CMD_ERR_AMBIGUOUS:
- vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
+ uty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
break;
case CMD_ERR_NO_MATCH:
- vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
+ uty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
break;
case CMD_ERR_INCOMPLETE:
- vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
+ uty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
break;
}
cmd_free_strvec (vline);
return ret;
}
-
+
static const char telnet_backward_char = 0x08;
static const char telnet_space_char = ' ';
@@ -463,6 +588,7 @@ static const char telnet_space_char = ' ';
static void
vty_write (struct vty *vty, const char *buf, size_t nbytes)
{
+ assert(vty_lock_count);
if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
return;
@@ -474,6 +600,7 @@ vty_write (struct vty *vty, const char *buf, size_t nbytes)
static void
vty_ensure (struct vty *vty, int length)
{
+ assert(vty_lock_count);
if (vty->max <= length)
{
vty->max *= 2;
@@ -488,6 +615,8 @@ vty_self_insert (struct vty *vty, char c)
int i;
int length;
+ assert(vty_lock_count);
+
vty_ensure (vty, vty->length + 1);
length = vty->length - vty->cp;
memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
@@ -505,6 +634,7 @@ vty_self_insert (struct vty *vty, char c)
static void
vty_self_insert_overwrite (struct vty *vty, char c)
{
+ assert(vty_lock_count);
vty_ensure (vty, vty->length + 1);
vty->buf[vty->cp++] = c;
@@ -521,7 +651,11 @@ vty_self_insert_overwrite (struct vty *vty, char c)
static void
vty_insert_word_overwrite (struct vty *vty, char *str)
{
+
int len = strlen (str);
+
+ assert(vty_lock_count);
+
vty_write (vty, str, len);
strcpy (&vty->buf[vty->cp], str);
vty->cp += len;
@@ -532,6 +666,7 @@ vty_insert_word_overwrite (struct vty *vty, char *str)
static void
vty_forward_char (struct vty *vty)
{
+ assert(vty_lock_count);
if (vty->cp < vty->length)
{
vty_write (vty, &vty->buf[vty->cp], 1);
@@ -543,6 +678,7 @@ vty_forward_char (struct vty *vty)
static void
vty_backward_char (struct vty *vty)
{
+ assert(vty_lock_count);
if (vty->cp > 0)
{
vty->cp--;
@@ -554,6 +690,7 @@ vty_backward_char (struct vty *vty)
static void
vty_beginning_of_line (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp)
vty_backward_char (vty);
}
@@ -562,6 +699,7 @@ vty_beginning_of_line (struct vty *vty)
static void
vty_end_of_line (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp < vty->length)
vty_forward_char (vty);
}
@@ -576,6 +714,8 @@ vty_history_print (struct vty *vty)
{
int length;
+ assert(vty_lock_count);
+
vty_kill_line_from_beginning (vty);
/* Get previous line from history buffer */
@@ -593,6 +733,8 @@ vty_next_line (struct vty *vty)
{
int try_index;
+ assert(vty_lock_count);
+
if (vty->hp == vty->hindex)
return;
@@ -618,6 +760,8 @@ vty_previous_line (struct vty *vty)
{
int try_index;
+ assert(vty_lock_count);
+
try_index = vty->hp;
if (try_index == 0)
try_index = VTY_MAXHIST - 1;
@@ -636,6 +780,7 @@ vty_previous_line (struct vty *vty)
static void
vty_redraw_line (struct vty *vty)
{
+ assert(vty_lock_count);
vty_write (vty, vty->buf, vty->length);
vty->cp = vty->length;
}
@@ -644,9 +789,10 @@ vty_redraw_line (struct vty *vty)
static void
vty_forward_word (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
vty_forward_char (vty);
-
+
while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
vty_forward_char (vty);
}
@@ -655,6 +801,7 @@ vty_forward_word (struct vty *vty)
static void
vty_backward_pure_word (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
vty_backward_char (vty);
}
@@ -663,6 +810,7 @@ vty_backward_pure_word (struct vty *vty)
static void
vty_backward_word (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
vty_backward_char (vty);
@@ -675,7 +823,8 @@ vty_backward_word (struct vty *vty)
static void
vty_down_level (struct vty *vty)
{
- vty_out (vty, "%s", VTY_NEWLINE);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", VTY_NEWLINE);
(*config_exit_cmd.func)(NULL, vty, 0, NULL);
vty_prompt (vty);
vty->cp = 0;
@@ -685,7 +834,8 @@ vty_down_level (struct vty *vty)
static void
vty_end_config (struct vty *vty)
{
- vty_out (vty, "%s", VTY_NEWLINE);
+ assert(vty_lock_count);
+ uty_out (vty, "%s", VTY_NEWLINE);
switch (vty->node)
{
@@ -713,7 +863,7 @@ vty_end_config (struct vty *vty)
case KEYCHAIN_KEY_NODE:
case MASC_NODE:
case VTY_NODE:
- vty_config_unlock (vty);
+ uty_config_unlock (vty);
vty->node = ENABLE_NODE;
break;
default:
@@ -732,6 +882,8 @@ vty_delete_char (struct vty *vty)
int i;
int size;
+ assert(vty_lock_count);
+
if (vty->length == 0)
{
vty_down_level (vty);
@@ -746,7 +898,7 @@ vty_delete_char (struct vty *vty)
vty->length--;
memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
vty->buf[vty->length] = '\0';
-
+
if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
return;
@@ -761,6 +913,7 @@ vty_delete_char (struct vty *vty)
static void
vty_delete_backward_char (struct vty *vty)
{
+ assert(vty_lock_count);
if (vty->cp == 0)
return;
@@ -775,8 +928,10 @@ vty_kill_line (struct vty *vty)
int i;
int size;
+ assert(vty_lock_count);
+
size = vty->length - vty->cp;
-
+
if (size == 0)
return;
@@ -793,6 +948,7 @@ vty_kill_line (struct vty *vty)
static void
vty_kill_line_from_beginning (struct vty *vty)
{
+ assert(vty_lock_count);
vty_beginning_of_line (vty);
vty_kill_line (vty);
}
@@ -801,6 +957,7 @@ vty_kill_line_from_beginning (struct vty *vty)
static void
vty_forward_kill_word (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
vty_delete_char (vty);
while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
@@ -811,6 +968,7 @@ vty_forward_kill_word (struct vty *vty)
static void
vty_backward_kill_word (struct vty *vty)
{
+ assert(vty_lock_count);
while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
vty_delete_backward_char (vty);
while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
@@ -823,6 +981,8 @@ vty_transpose_chars (struct vty *vty)
{
char c1, c2;
+ assert(vty_lock_count);
+
/* If length is short or point is near by the beginning of line then
return. */
if (vty->length < 2 || vty->cp < 1)
@@ -854,11 +1014,14 @@ vty_transpose_chars (struct vty *vty)
static void
vty_complete_command (struct vty *vty)
{
+
int i;
int ret;
char **matched = NULL;
vector vline;
+ assert(vty_lock_count);
+
if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
return;
@@ -871,14 +1034,14 @@ vty_complete_command (struct vty *vty)
vector_set (vline, '\0');
matched = cmd_complete_command (vline, vty, &ret);
-
+
cmd_free_strvec (vline);
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
switch (ret)
{
case CMD_ERR_AMBIGUOUS:
- vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
+ uty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
vty_prompt (vty);
vty_redraw_line (vty);
break;
@@ -908,11 +1071,11 @@ vty_complete_command (struct vty *vty)
for (i = 0; matched[i] != NULL; i++)
{
if (i != 0 && ((i % 6) == 0))
- vty_out (vty, "%s", VTY_NEWLINE);
- vty_out (vty, "%-10s ", matched[i]);
+ uty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%-10s ", matched[i]);
XFREE (MTYPE_TMP, matched[i]);
}
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
vty_prompt (vty);
vty_redraw_line (vty);
@@ -936,11 +1099,13 @@ vty_describe_fold (struct vty *vty, int cmd_width,
const char *cmd, *p;
int pos;
+ assert(vty_lock_count);
+
cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
if (desc_width <= 0)
{
- vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
+ uty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
return;
}
@@ -957,12 +1122,12 @@ vty_describe_fold (struct vty *vty, int cmd_width,
strncpy (buf, p, pos);
buf[pos] = '\0';
- vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
+ uty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
cmd = "";
}
- vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
+ uty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
XFREE (MTYPE_TMP, buf);
}
@@ -971,12 +1136,14 @@ vty_describe_fold (struct vty *vty, int cmd_width,
static void
vty_describe_command (struct vty *vty)
{
- int ret;
+ int ret;
vector vline;
vector describe;
unsigned int i, width, desc_width;
struct desc *desc, *desc_cr = NULL;
+ assert(vty_lock_count);
+
vline = cmd_make_strvec (vty->buf);
/* In case of '> ?'. */
@@ -985,26 +1152,26 @@ vty_describe_command (struct vty *vty)
vline = vector_init (1);
vector_set (vline, '\0');
}
- else
+ else
if (isspace ((int) vty->buf[vty->length - 1]))
vector_set (vline, '\0');
describe = cmd_describe_command (vline, vty, &ret);
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
/* Ambiguous error. */
switch (ret)
{
case CMD_ERR_AMBIGUOUS:
- vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
+ uty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
goto out;
break;
case CMD_ERR_NO_MATCH:
- vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
+ uty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
goto out;
break;
- }
+ }
/* Get width of command string. */
width = 0;
@@ -1033,7 +1200,7 @@ vty_describe_command (struct vty *vty)
{
if (desc->cmd[0] == '\0')
continue;
-
+
if (strcmp (desc->cmd, command_cr) == 0)
{
desc_cr = desc;
@@ -1041,18 +1208,18 @@ vty_describe_command (struct vty *vty)
}
if (!desc->str)
- vty_out (vty, " %-s%s",
+ uty_out (vty, " %-s%s",
desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
VTY_NEWLINE);
else if (desc_width >= strlen (desc->str))
- vty_out (vty, " %-*s %s%s", width,
+ uty_out (vty, " %-*s %s%s", width,
desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
desc->str, VTY_NEWLINE);
else
vty_describe_fold (vty, width, desc_width, desc);
#if 0
- vty_out (vty, " %-*s %s%s", width
+ uty_out (vty, " %-*s %s%s", width
desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
desc->str ? desc->str : "", VTY_NEWLINE);
#endif /* 0 */
@@ -1061,11 +1228,11 @@ vty_describe_command (struct vty *vty)
if ((desc = desc_cr))
{
if (!desc->str)
- vty_out (vty, " %-s%s",
+ uty_out (vty, " %-s%s",
desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
VTY_NEWLINE);
else if (desc_width >= strlen (desc->str))
- vty_out (vty, " %-*s %s%s", width,
+ uty_out (vty, " %-*s %s%s", width,
desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
desc->str, VTY_NEWLINE);
else
@@ -1084,6 +1251,7 @@ out:
static void
vty_clear_buf (struct vty *vty)
{
+ assert(vty_lock_count);
memset (vty->buf, 0, vty->max);
}
@@ -1091,9 +1259,10 @@ vty_clear_buf (struct vty *vty)
static void
vty_stop_input (struct vty *vty)
{
+ assert(vty_lock_count);
vty->cp = vty->length = 0;
vty_clear_buf (vty);
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
switch (vty->node)
{
@@ -1116,7 +1285,7 @@ vty_stop_input (struct vty *vty)
case KEYCHAIN_KEY_NODE:
case MASC_NODE:
case VTY_NODE:
- vty_config_unlock (vty);
+ uty_config_unlock (vty);
vty->node = ENABLE_NODE;
break;
default:
@@ -1135,6 +1304,8 @@ vty_hist_add (struct vty *vty)
{
int index;
+ assert(vty_lock_count);
+
if (vty->length == 0)
return;
@@ -1170,46 +1341,48 @@ vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
#ifdef TELNET_OPTION_DEBUG
int i;
+ assert(vty_lock_count);
+
for (i = 0; i < nbytes; i++)
{
switch (buf[i])
{
case IAC:
- vty_out (vty, "IAC ");
+ uty_out (vty, "IAC ");
break;
case WILL:
- vty_out (vty, "WILL ");
+ uty_out (vty, "WILL ");
break;
case WONT:
- vty_out (vty, "WONT ");
+ uty_out (vty, "WONT ");
break;
case DO:
- vty_out (vty, "DO ");
+ uty_out (vty, "DO ");
break;
case DONT:
- vty_out (vty, "DONT ");
+ uty_out (vty, "DONT ");
break;
case SB:
- vty_out (vty, "SB ");
+ uty_out (vty, "SB ");
break;
case SE:
- vty_out (vty, "SE ");
+ uty_out (vty, "SE ");
break;
case TELOPT_ECHO:
- vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
+ uty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
break;
case TELOPT_SGA:
- vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
+ uty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
break;
case TELOPT_NAWS:
- vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
+ uty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
break;
default:
- vty_out (vty, "%x ", buf[i]);
+ uty_out (vty, "%x ", buf[i]);
break;
}
}
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
#endif /* TELNET_OPTION_DEBUG */
@@ -1220,7 +1393,7 @@ vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
vty->iac_sb_in_progress = 1;
return 0;
break;
- case SE:
+ case SE:
{
if (!vty->iac_sb_in_progress)
return 0;
@@ -1234,11 +1407,11 @@ vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
{
case TELOPT_NAWS:
if (vty->sb_len != TELNET_NAWS_SB_LEN)
- zlog_warn("RFC 1073 violation detected: telnet NAWS option "
+ uzlog(NULL, LOG_WARNING, "RFC 1073 violation detected: telnet NAWS option "
"should send %d characters, but we received %lu",
TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
- zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
+ uzlog(NULL, LOG_ERR, "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
"too small to handle the telnet NAWS option",
(u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
else
@@ -1246,7 +1419,7 @@ vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
#ifdef TELNET_OPTION_DEBUG
- vty_out(vty, "TELNET NAWS window size negotiation completed: "
+ uty_out(vty, "TELNET NAWS window size negotiation completed: "
"width %d, height %d%s",
vty->width, vty->height, VTY_NEWLINE);
#endif
@@ -1303,6 +1476,7 @@ vty_execute (struct vty *vty)
static void
vty_escape_map (unsigned char c, struct vty *vty)
{
+ assert(vty_lock_count);
switch (c)
{
case ('A'):
@@ -1329,22 +1503,51 @@ vty_escape_map (unsigned char c, struct vty *vty)
static void
vty_buffer_reset (struct vty *vty)
{
+ assert(vty_lock_count);
buffer_reset (vty->obuf);
vty_prompt (vty);
vty_redraw_line (vty);
}
-/* Read data via vty socket. */
+/* Callback: qpthreads., Read data via vty socket. */
+static void
+vty_read_r (qps_file qf, void* file_info)
+{
+ int vty_sock = qf->fd;
+ struct vty *vty = (struct vty *)file_info;
+
+ LOCK
+
+ /* is this necessary? */
+ qps_disable_modes(qf, qps_read_mbit);
+ uty_read(vty, vty_sock);
+
+ UNLOCK
+}
+
+/* Callback: threads. Read data via vty socket. */
static int
vty_read (struct thread *thread)
{
- int i;
- int nbytes;
- unsigned char buf[VTY_READ_BUFSIZ];
-
int vty_sock = THREAD_FD (thread);
struct vty *vty = THREAD_ARG (thread);
+ int result ;
+
+ LOCK
+
vty->t_read = NULL;
+ result = uty_read(vty, vty_sock);
+
+ UNLOCK
+ return result;
+}
+
+static int
+uty_read (struct vty *vty, int vty_sock)
+{
+ int i;
+ int nbytes;
+ unsigned char buf[VTY_READ_BUFSIZ];
/* Read raw data from socket */
if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
@@ -1357,14 +1560,14 @@ vty_read (struct thread *thread)
return 0;
}
vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
- zlog_warn("%s: read error on vty client fd %d, closing: %s",
+ uzlog(NULL, LOG_WARNING, "%s: read error on vty client fd %d, closing: %s",
__func__, vty->fd, safe_strerror(errno));
}
buffer_reset(vty->obuf);
vty->status = VTY_CLOSE;
}
- for (i = 0; i < nbytes; i++)
+ for (i = 0; i < nbytes; i++)
{
if (buf[i] == IAC)
{
@@ -1378,7 +1581,7 @@ vty_read (struct thread *thread)
vty->iac = 0;
}
}
-
+
if (vty->iac_sb_in_progress && !vty->iac)
{
if (vty->sb_len < sizeof(vty->sb_buf))
@@ -1396,7 +1599,7 @@ vty_read (struct thread *thread)
i += ret;
continue;
}
-
+
if (vty->status == VTY_MORE)
{
@@ -1505,7 +1708,7 @@ vty_read (struct thread *thread)
break;
case '\n':
case '\r':
- vty_out (vty, "%s", VTY_NEWLINE);
+ uty_out (vty, "%s", VTY_NEWLINE);
vty_execute (vty);
break;
case '\t':
@@ -1535,32 +1738,66 @@ vty_read (struct thread *thread)
/* Check status. */
if (vty->status == VTY_CLOSE)
- vty_close (vty);
+ uty_close (vty);
else
{
vty_event (VTY_WRITE, vty_sock, vty);
vty_event (VTY_READ, vty_sock, vty);
}
+
return 0;
}
-/* Flush buffer to the vty. */
+/* Callback: qpthreads. Flush buffer to the vty. */
+static void
+vty_flush_r (qps_file qf, void* file_info)
+{
+ int vty_sock = qf->fd;
+ struct vty *vty = (struct vty *)file_info;
+
+ LOCK
+
+ qps_disable_modes(qf, qps_write_mbit);
+
+ /* Temporary disable read thread. */
+ if ((vty->lines == 0))
+ {
+ qps_disable_modes(qf, qps_read_mbit);
+ }
+
+ uty_flush(vty, vty_sock);
+
+ UNLOCK
+}
+
+/* Callback: threads. Flush buffer to the vty. */
static int
vty_flush (struct thread *thread)
{
- int erase;
- buffer_status_t flushrc;
int vty_sock = THREAD_FD (thread);
struct vty *vty = THREAD_ARG (thread);
+ int result;
+ LOCK
vty->t_write = NULL;
- /* Tempolary disable read thread. */
+ /* Temporary disable read thread. */
if ((vty->lines == 0) && vty->t_read)
{
thread_cancel (vty->t_read);
vty->t_read = NULL;
}
+ result = uty_flush(vty, vty_sock);
+
+ UNLOCK
+ return result;
+}
+
+static int
+uty_flush (struct vty *vty, int vty_sock)
+{
+ int erase;
+ buffer_status_t flushrc;
/* Function execution continue. */
erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
@@ -1580,14 +1817,14 @@ vty_flush (struct thread *thread)
{
case BUFFER_ERROR:
vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
- zlog_warn("buffer_flush failed on vty client fd %d, closing",
+ uzlog(NULL, LOG_WARNING, "buffer_flush failed on vty client fd %d, closing",
vty->fd);
buffer_reset(vty->obuf);
- vty_close(vty);
- return 0;
+ uty_close(vty);
+ break;
case BUFFER_EMPTY:
if (vty->status == VTY_CLOSE)
- vty_close (vty);
+ uty_close (vty);
else
{
vty->status = VTY_NORMAL;
@@ -1612,9 +1849,10 @@ vty_create (int vty_sock, union sockunion *su)
{
struct vty *vty;
+ assert(vty_lock_count);
+
/* Allocate new vty structure and set up default values. */
- vty = vty_new ();
- vty->fd = vty_sock;
+ vty = vty_new (vty_sock);
vty->type = VTY_TERM;
vty->address = sockunion_su2str (su);
if (no_password_check)
@@ -1651,17 +1889,17 @@ vty_create (int vty_sock, union sockunion *su)
/* Vty is not available if password isn't set. */
if (host.password == NULL && host.password_encrypt == NULL)
{
- vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
+ uty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
vty->status = VTY_CLOSE;
- vty_close (vty);
+ uty_close (vty);
return NULL;
}
}
/* Say hello to the world. */
- vty_hello (vty);
+ uty_hello (vty);
if (! no_password_check)
- vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
+ uty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
/* Setting up terminal. */
vty_will_echo (vty);
@@ -1680,22 +1918,45 @@ vty_create (int vty_sock, union sockunion *su)
return vty;
}
-/* Accept connection from the network. */
+/* Callback: qpthreads. Accept connection from the network. */
+static void
+vty_accept_r (qps_file qf, void* file_info)
+{
+ LOCK
+
+ int accept_sock = qf->fd;
+ uty_accept(accept_sock);
+
+ UNLOCK
+}
+
+/* Callback: threads. Accept connection from the network. */
static int
vty_accept (struct thread *thread)
{
+ int result;
+
+ LOCK
+
+ int accept_sock = THREAD_FD (thread);
+ result = uty_accept(accept_sock);
+
+ UNLOCK
+ return result;
+}
+
+static int
+uty_accept (int accept_sock)
+{
int vty_sock;
struct vty *vty;
union sockunion su;
int ret;
unsigned int on;
- int accept_sock;
struct prefix *p = NULL;
struct access_list *acl = NULL;
char *bufp;
- accept_sock = THREAD_FD (thread);
-
/* We continue hearing vty socket. */
vty_event (VTY_SERV, accept_sock, NULL);
@@ -1705,7 +1966,7 @@ vty_accept (struct thread *thread)
vty_sock = sockunion_accept (accept_sock, &su);
if (vty_sock < 0)
{
- zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
+ uzlog (NULL, LOG_WARNING, "can't accept vty socket : %s", safe_strerror (errno));
return -1;
}
set_nonblocking(vty_sock);
@@ -1719,16 +1980,15 @@ vty_accept (struct thread *thread)
(access_list_apply (acl, p) == FILTER_DENY))
{
char *buf;
- zlog (NULL, LOG_INFO, "Vty connection refused from %s",
+ uzlog (NULL, LOG_INFO, "Vty connection refused from %s",
(buf = sockunion_su2str (&su)));
free (buf);
close (vty_sock);
-
+
/* continue accepting connections */
vty_event (VTY_SERV, accept_sock, NULL);
-
- prefix_free (p);
+ prefix_free (p);
return 0;
}
}
@@ -1741,28 +2001,27 @@ vty_accept (struct thread *thread)
(access_list_apply (acl, p) == FILTER_DENY))
{
char *buf;
- zlog (NULL, LOG_INFO, "Vty connection refused from %s",
+ uzlog (NULL, LOG_INFO, "Vty connection refused from %s",
(buf = sockunion_su2str (&su)));
free (buf);
close (vty_sock);
-
+
/* continue accepting connections */
vty_event (VTY_SERV, accept_sock, NULL);
-
- prefix_free (p);
+ prefix_free (p);
return 0;
}
}
#endif /* HAVE_IPV6 */
-
+
prefix_free (p);
on = 1;
- ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
+ ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
(char *) &on, sizeof (on));
if (ret < 0)
- zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
+ uzlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
safe_strerror (errno));
zlog (NULL, LOG_INFO, "Vty connection from %s",
@@ -1786,6 +2045,8 @@ vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
int sock;
char port_str[BUFSIZ];
+ assert(vty_lock_count);
+
memset (&req, 0, sizeof (struct addrinfo));
req.ai_flags = AI_PASSIVE;
req.ai_family = AF_UNSPEC;
@@ -1827,7 +2088,7 @@ vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
}
ret = listen (sock, 3);
- if (ret < 0)
+ if (ret < 0)
{
close (sock); /* Avoid sd leak. */
continue;
@@ -1841,6 +2102,7 @@ vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
}
#endif /* HAVE_IPV6 && ! NRL */
+#if defined(HAVE_IPV6) && defined(NRL) || !defined(HAVE_IPV6)
/* Make vty server socket. */
static void
vty_serv_sock_family (const char* addr, unsigned short port, int family)
@@ -1850,6 +2112,8 @@ vty_serv_sock_family (const char* addr, unsigned short port, int family)
int accept_sock;
void* naddr=NULL;
+ assert(vty_lock_count);
+
memset (&su, 0, sizeof (union sockunion));
su.sa.sa_family = family;
if(addr)
@@ -1860,18 +2124,18 @@ vty_serv_sock_family (const char* addr, unsigned short port, int family)
#ifdef HAVE_IPV6
case AF_INET6:
naddr=&su.sin6.sin6_addr;
-#endif
+#endif
}
if(naddr)
switch(inet_pton(family,addr,naddr))
{
case -1:
- zlog_err("bad address %s",addr);
+ uzlog(NULL, LOG_ERR, "bad address %s",addr);
naddr=NULL;
break;
case 0:
- zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
+ uzlog(NULL, LOG_ERR, "error translating address %s: %s",addr,safe_strerror(errno));
naddr=NULL;
}
@@ -1888,14 +2152,14 @@ vty_serv_sock_family (const char* addr, unsigned short port, int family)
ret = sockunion_bind (accept_sock, &su, port, naddr);
if (ret < 0)
{
- zlog_warn("can't bind socket");
+ uzlog(NULL, LOG_WARNING, "can't bind socket");
close (accept_sock); /* Avoid sd leak. */
return;
}
/* Listen socket under queue 3. */
ret = listen (accept_sock, 3);
- if (ret < 0)
+ if (ret < 0)
{
zlog (NULL, LOG_WARNING, "can't listen socket");
close (accept_sock); /* Avoid sd leak. */
@@ -1905,6 +2169,7 @@ vty_serv_sock_family (const char* addr, unsigned short port, int family)
/* Add vty server event. */
vty_event (VTY_SERV, accept_sock, NULL);
}
+#endif /* defined(HAVE_IPV6) && defined(NRL) || !defined(HAVE_IPV6) */
#ifdef VTYSH
/* For sockaddr_un. */
@@ -1919,7 +2184,9 @@ vty_serv_un (const char *path)
struct sockaddr_un serv;
mode_t old_mask;
struct zprivs_ids_t ids;
-
+
+ assert(vty_lock_count);
+
/* First of all, unlink existing socket */
unlink (path);
@@ -1930,7 +2197,7 @@ vty_serv_un (const char *path)
sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
- zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
+ uzlog(NULL, LOG_ERR, "Cannot create unix stream socket: %s", safe_strerror(errno));
return;
}
@@ -1947,7 +2214,7 @@ vty_serv_un (const char *path)
ret = bind (sock, (struct sockaddr *) &serv, len);
if (ret < 0)
{
- zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
+ ulog(NULL, LOG_ERR, "Cannot bind path %s: %s", path, safe_strerror(errno));
close (sock); /* Avoid sd leak. */
return;
}
@@ -1955,7 +2222,7 @@ vty_serv_un (const char *path)
ret = listen (sock, 5);
if (ret < 0)
{
- zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
+ uzlog(NULL, LOG_ERR, "listen(fd %d) failed: %s", sock, safe_strerror(errno));
close (sock); /* Avoid sd leak. */
return;
}
@@ -1963,13 +2230,13 @@ vty_serv_un (const char *path)
umask (old_mask);
zprivs_get_ids(&ids);
-
+
if (ids.gid_vty > 0)
{
/* set group of socket */
if ( chown (path, -1, ids.gid_vty) )
{
- zlog_err ("vty_serv_un: could chown socket, %s",
+ uzlog (NULL, LOG_ERR, "vty_serv_un: could chown socket, %s",
safe_strerror (errno) );
}
}
@@ -1979,16 +2246,36 @@ vty_serv_un (const char *path)
/* #define VTYSH_DEBUG 1 */
+/* Callback: qpthreads. Accept connection */
+void int
+vtysh_accept_r (qps_file qf, void* file_info)
+{
+ int accept_sock = qf->fd;
+ LOCK
+ utysh_accept (accept_sock);
+ UNLOCK
+}
+
+/* Callback: threads. Accept connection */
static int
vtysh_accept (struct thread *thread)
{
- int accept_sock;
+ int accept_sock = THREAD_FD (thread);
+ LOCK
+ result = utysh_accept (accept_sock);
+ UNLOCK
+ return result;
+}
+
+static int
+utysh_accept (int accept_sock)
+{
int sock;
int client_len;
struct sockaddr_un client;
struct vty *vty;
-
- accept_sock = THREAD_FD (thread);
+
+ assert(vty_lock_count);
vty_event (VTYSH_SERV, accept_sock, NULL);
@@ -2000,18 +2287,18 @@ vtysh_accept (struct thread *thread)
if (sock < 0)
{
- zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
+ uzlog (NULL, LOG_WARNING, "can't accept vty socket : %s", safe_strerror (errno));
return -1;
}
if (set_nonblocking(sock) < 0)
{
- zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
+ uzlog (NULL, LOG_WARNING, "vtysh_accept: could not set vty socket %d to non-blocking,"
" %s, closing", sock, safe_strerror (errno));
close (sock);
return -1;
}
-
+
#ifdef VTYSH_DEBUG
printf ("VTY shell accept\n");
#endif /* VTYSH_DEBUG */
@@ -2029,6 +2316,8 @@ vtysh_accept (struct thread *thread)
static int
vtysh_flush(struct vty *vty)
{
+ assert(vty_lock_count);
+
switch (buffer_flush_available(vty->obuf, vty->fd))
{
case BUFFER_PENDING:
@@ -2036,9 +2325,9 @@ vtysh_flush(struct vty *vty)
break;
case BUFFER_ERROR:
vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
- zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
+ uzlog(NULL, LOG_WARNING, "%s: write error to fd %d, closing", __func__, vty->fd);
buffer_reset(vty->obuf);
- vty_close(vty);
+ uty_close(vty);
return -1;
break;
case BUFFER_EMPTY:
@@ -2047,21 +2336,48 @@ vtysh_flush(struct vty *vty)
return 0;
}
+/* Callback: qpthreads., Read data via vty socket. */
+static void
+vtysh_read_r (qps_file qf, void* file_info)
+{
+ int vty_sock = qf->fd;
+ struct vty *vty = (struct vty *)file_info;
+
+ LOCK
+
+ /* is this necessary? */
+ qps_disable_modes(qf, qps_read_mbit);
+ utysh_read(vty, vty_soc);
+
+ UNLOCK
+}
+
+/* Callback: threads. Read data via vty socket. */
static int
vtysh_read (struct thread *thread)
{
+ int vty_sock = THREAD_FD (thread);
+ struct vty *vty = THREAD_ARG (thread);
+ int result;
+
+ LOCK
+
+ vty->t_read = NULL;
+ result = uty_read(vty, vty_soc);
+
+ UNLOCK
+ return result;
+}
+
+static int
+utysh_read (struct vty *vty, int sock)
+{
int ret;
- int sock;
int nbytes;
- struct vty *vty;
unsigned char buf[VTY_READ_BUFSIZ];
unsigned char *p;
u_char header[4] = {0, 0, 0, 0};
- sock = THREAD_FD (thread);
- vty = THREAD_ARG (thread);
- vty->t_read = NULL;
-
if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
{
if (nbytes < 0)
@@ -2072,11 +2388,11 @@ vtysh_read (struct thread *thread)
return 0;
}
vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
- zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
+ uzlog(NULL, LOG_WARNING, "%s: read failed on vtysh client fd %d, closing: %s",
__func__, sock, safe_strerror(errno));
}
buffer_reset(vty->obuf);
- vty_close (vty);
+ uty_close (vty);
#ifdef VTYSH_DEBUG
printf ("close vtysh\n");
#endif /* VTYSH_DEBUG */
@@ -2118,13 +2434,32 @@ vtysh_read (struct thread *thread)
return 0;
}
+/* Callback: qpthraeds. Write */
+static void
+vtysh_write_r (qps_file qf, void* file_info)
+{
+ struct vty *vty = (struct vty *)file_info;
+
+ LOCK
+
+ qps_disable_modes(qf, qps_write_mbit);
+ vtysh_flush(vty);
+
+ UNLOCK
+}
+
+//* Callback: thraeds. Write */
static int
vtysh_write (struct thread *thread)
{
struct vty *vty = THREAD_ARG (thread);
+ LOCK
+
vty->t_write = NULL;
vtysh_flush(vty);
+
+ UNLOCK
return 0;
}
@@ -2134,6 +2469,8 @@ vtysh_write (struct thread *thread)
void
vty_serv_sock (const char *addr, unsigned short port, const char *path)
{
+ LOCK
+
/* If port is set to 0, do not listen on TCP/IP at all! */
if (port)
{
@@ -2153,6 +2490,8 @@ vty_serv_sock (const char *addr, unsigned short port, const char *path)
#ifdef VTYSH
vty_serv_un (path);
#endif /* VTYSH */
+
+ UNLOCK
}
/* Close vty interface. Warning: call this only from functions that
@@ -2162,8 +2501,18 @@ vty_serv_sock (const char *addr, unsigned short port, const char *path)
void
vty_close (struct vty *vty)
{
+ LOCK
+ uty_close(vty);
+ UNLOCK
+}
+
+static void
+uty_close (struct vty *vty)
+{
int i;
+ assert(vty_lock_count);
+
/* Cancel threads.*/
if (vty->t_read)
thread_cancel (vty->t_read);
@@ -2171,6 +2520,17 @@ vty_close (struct vty *vty)
thread_cancel (vty->t_write);
if (vty->t_timeout)
thread_cancel (vty->t_timeout);
+ if (vty->qf)
+ {
+ qps_remove_file(vty->qf);
+ qps_file_free(vty->qf);
+ vty->qf = NULL;
+ }
+ if (vty->qtr)
+ {
+ qtimer_free(vty->qtr);
+ vty->qtr = NULL;
+ }
/* Flush buffer. */
buffer_flush_all (vty->obuf, vty->fd);
@@ -2196,29 +2556,48 @@ vty_close (struct vty *vty)
XFREE (MTYPE_VTY, vty->buf);
/* Check configure. */
- vty_config_unlock (vty);
+ uty_config_unlock (vty);
/* OK free vty. */
XFREE (MTYPE_VTY, vty);
}
-/* When time out occur output message then close connection. */
+/* Callback: qpthreads. When time out occur output message then close connection. */
+static void
+vty_timeout_r (qtimer qtr, void* timer_info, qtime_t when)
+{
+ struct vty *vty = (struct vty *)timer_info;
+ LOCK
+ qtimer_unset(qtr);
+ uty_timeout(vty);
+ UNLOCK
+}
+
+/* Callback: threads. When time out occur output message then close connection. */
static int
vty_timeout (struct thread *thread)
{
- struct vty *vty;
-
- vty = THREAD_ARG (thread);
+ int result;
+ struct vty *vty = THREAD_ARG (thread);
+ LOCK
vty->t_timeout = NULL;
+ result = uty_timeout(vty);
+ UNLOCK
+ return result;
+}
+
+static int
+uty_timeout (struct vty *vty)
+{
vty->v_timeout = 0;
/* Clear buffer*/
buffer_reset (vty->obuf);
- vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
+ uty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
/* Close connection. */
vty->status = VTY_CLOSE;
- vty_close (vty);
+ uty_close (vty);
return 0;
}
@@ -2230,15 +2609,16 @@ vty_read_file (FILE *confp)
int ret;
struct vty *vty;
- vty = vty_new ();
- vty->fd = 0; /* stdout */
+ vty = vty_new (0); /* stdout */
vty->type = VTY_TERM;
vty->node = CONFIG_NODE;
-
+
/* Execute configuration file */
ret = config_from_file (vty, confp);
- if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
+ LOCK
+
+ if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
{
switch (ret)
{
@@ -2249,13 +2629,14 @@ vty_read_file (FILE *confp)
fprintf (stderr, "There is no such command.\n");
break;
}
- fprintf (stderr, "Error occured during reading below line.\n%s\n",
+ fprintf (stderr, "Error occured during reading below line.\n%s\n",
vty->buf);
- vty_close (vty);
+ uty_close (vty);
exit (1);
}
- vty_close (vty);
+ uty_close (vty);
+ UNLOCK
}
static FILE *
@@ -2267,7 +2648,7 @@ vty_use_backup_config (char *fullpath)
int tmp, sav;
int c;
char buffer[512];
-
+
fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
strcpy (fullpath_sav, fullpath);
strcat (fullpath_sav, CONF_BACKUP_EXT);
@@ -2279,7 +2660,7 @@ vty_use_backup_config (char *fullpath)
fullpath_tmp = malloc (strlen (fullpath) + 8);
sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
-
+
/* Open file to configuration write. */
tmp = mkstemp (fullpath_tmp);
if (tmp < 0)
@@ -2297,13 +2678,13 @@ vty_use_backup_config (char *fullpath)
free (fullpath_tmp);
return NULL;
}
-
+
while((c = read (sav, buffer, 512)) > 0)
write (tmp, buffer, c);
-
+
close (sav);
close (tmp);
-
+
if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
{
unlink (fullpath_tmp);
@@ -2311,12 +2692,12 @@ vty_use_backup_config (char *fullpath)
free (fullpath_tmp);
return NULL;
}
-
+
if (link (fullpath_tmp, fullpath) == 0)
ret = fopen (fullpath, "r");
unlink (fullpath_tmp);
-
+
free (fullpath_sav);
free (fullpath_tmp);
return ret;
@@ -2338,7 +2719,7 @@ vty_read_config (char *config_file,
if (! IS_DIRECTORY_SEP (config_file[0]))
{
getcwd (cwd, MAXPATHLEN);
- tmp = XMALLOC (MTYPE_TMP,
+ tmp = XMALLOC (MTYPE_TMP,
strlen (cwd) + strlen (config_file) + 2);
sprintf (tmp, "%s/%s", cwd, config_file);
fullpath = tmp;
@@ -2352,13 +2733,13 @@ vty_read_config (char *config_file,
{
fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
__func__, fullpath, safe_strerror (errno));
-
+
confp = vty_use_backup_config (fullpath);
if (confp)
fprintf (stderr, "WARNING: using backup configuration file!\n");
else
{
- fprintf (stderr, "can't open configuration file [%s]\n",
+ fprintf (stderr, "can't open configuration file [%s]\n",
config_file);
exit(1);
}
@@ -2388,7 +2769,7 @@ vty_read_config (char *config_file,
{
ret = stat (integrate_default, &conf_stat);
if (ret >= 0)
- return;
+ return;
}
#endif /* VTYSH */
@@ -2397,7 +2778,7 @@ vty_read_config (char *config_file,
{
fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
__func__, config_default_dir, safe_strerror (errno));
-
+
confp = vty_use_backup_config (config_default_dir);
if (confp)
{
@@ -2410,7 +2791,7 @@ vty_read_config (char *config_file,
config_default_dir);
exit (1);
}
- }
+ }
else
fullpath = config_default_dir;
}
@@ -2420,7 +2801,7 @@ vty_read_config (char *config_file,
fclose (confp);
host_config_set (fullpath);
-
+
if (tmp)
XFREE (MTYPE_TMP, fullpath);
}
@@ -2432,19 +2813,20 @@ vty_log (const char *level, const char *proto_str,
{
unsigned int i;
struct vty *vty;
-
+
+ assert(vty_lock_count);
+
if (!vtyvec)
return;
- for (i = 0; i < vector_active (vtyvec); i++)
- if ((vty = vector_slot (vtyvec, i)) != NULL)
- if (vty->monitor)
- {
- va_list ac;
- va_copy(ac, va);
- vty_log_out (vty, level, proto_str, format, ctl, ac);
- va_end(ac);
- }
+ for (i = 0; i < vector_active (vtyvec); i++)
+ if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
+ {
+ va_list ac;
+ va_copy(ac, va);
+ vty_log_out (vty, level, proto_str, format, ctl, ac);
+ va_end(ac);
+ }
}
/* Async-signal-safe version of vty_log for fixed strings. */
@@ -2457,7 +2839,7 @@ vty_log_fixed (const char *buf, size_t len)
/* vty may not have been initialised */
if (!vtyvec)
return;
-
+
iov[0].iov_base = (void *)buf;
iov[0].iov_len = len;
iov[1].iov_base = (void *)"\r\n";
@@ -2476,17 +2858,32 @@ vty_log_fixed (const char *buf, size_t len)
int
vty_config_lock (struct vty *vty)
{
+ int result;
+ LOCK
if (vty_config == 0)
{
vty->config = 1;
vty_config = 1;
}
- return vty->config;
+ result = vty->config;
+ UNLOCK
+ return result;
}
int
vty_config_unlock (struct vty *vty)
{
+ int result;
+ LOCK
+ result = uty_config_unlock(vty);
+ UNLOCK
+ return result;
+}
+
+static int
+uty_config_unlock (struct vty *vty)
+{
+ assert(vty_lock_count);
if (vty_config == 1 && vty->config == 1)
{
vty->config = 0;
@@ -2494,15 +2891,24 @@ vty_config_unlock (struct vty *vty)
}
return vty->config;
}
-
-/* Master of the threads. */
-static struct thread_master *master;
static void
vty_event (enum event event, int sock, struct vty *vty)
{
+ if (master_nexus)
+ vty_event_r(event, sock, vty);
+ else
+ vty_event_t(event, sock, vty);
+}
+
+/* thread event setter */
+static void
+vty_event_t (enum event event, int sock, struct vty *vty)
+ {
struct thread *vty_serv_thread;
+ assert(vty_lock_count);
+
switch (event)
{
case VTY_SERV:
@@ -2528,14 +2934,14 @@ vty_event (enum event event, int sock, struct vty *vty)
{
if (vty->t_timeout)
thread_cancel (vty->t_timeout);
- vty->t_timeout =
+ vty->t_timeout =
thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
}
break;
case VTY_WRITE:
if (! vty->t_write)
vty->t_write = thread_add_write (master, vty_flush, vty, sock);
- break;
+ break;
case VTY_TIMEOUT_RESET:
if (vty->t_timeout)
{
@@ -2544,13 +2950,77 @@ vty_event (enum event event, int sock, struct vty *vty)
}
if (vty->v_timeout)
{
- vty->t_timeout =
+ vty->t_timeout =
thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
}
break;
}
}
-
+
+/* qpthreads event setter */
+static void
+vty_event_r (enum event event, int sock, struct vty *vty)
+ {
+
+ qps_file accept_file = NULL;
+
+ assert(vty_lock_count);
+
+ switch (event)
+ {
+ case VTY_SERV:
+ accept_file = vector_get_item(Vvty_serv_thread, sock);
+ if (accept_file == NULL)
+ {
+ accept_file = qps_file_init_new(accept_file, NULL);
+ qps_add_file(master_nexus->selection, accept_file, sock, NULL);
+ vector_set_index(Vvty_serv_thread, sock, accept_file);
+ }
+ qps_enable_mode(accept_file, qps_read_mnum, vty_accept_r) ;
+ break;
+#ifdef VTYSH
+ case VTYSH_SERV:
+ accept_file = vector_get_item(Vvty_serv_thread, sock);
+ if (accept_file == NULL)
+ {
+ accept_file = qps_file_init_new(accept_file, NULL);
+ qps_add_file(master, accept_file, sock, NULL);
+ vector_set_index(Vvty_serv_thread, sock, accept_file);
+ }
+ qps_enable_mode(accept_file, qps_read_mnum, vtysh_accept_r) ;
+ break;
+ case VTYSH_READ:
+ qps_enable_mode(vty->file, qps_read_mnum, vtysh_read_r) ;
+ break;
+ case VTYSH_WRITE:
+ qps_enable_mode(vty->file, qps_write_mnum, vtysh_write_r) ;
+ break;
+#endif /* VTYSH */
+ case VTY_READ:
+ qps_enable_mode(vty->qf, qps_read_mnum, vty_read_r) ;
+
+ /* Time out treatment. */
+ if (vty->v_timeout)
+ {
+ qtimer_set(vty->qtr, qt_add_monotonic(QTIME(vty->v_timeout)), NULL) ;
+ }
+ break;
+ case VTY_WRITE:
+ qps_enable_mode(vty->qf, qps_write_mnum, vty_flush_r) ;
+ break;
+ case VTY_TIMEOUT_RESET:
+ if (vty->v_timeout)
+ {
+ qtimer_set(vty->qtr, qt_add_monotonic(QTIME(vty->v_timeout)), NULL) ;
+ }
+ else
+ {
+ qtimer_unset(vty->qtr);
+ }
+ break;
+ }
+}
+
DEFUN (config_who,
config_who_cmd,
"who",
@@ -2584,6 +3054,8 @@ exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
{
unsigned long timeout = 0;
+ LOCK
+
/* min_str and sec_str are already checked by parser. So it must be
all digit string. */
if (min_str)
@@ -2598,7 +3070,7 @@ exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
vty->v_timeout = timeout;
vty_event (VTY_TIMEOUT_RESET, 0, vty);
-
+ UNLOCK
return CMD_SUCCESS;
}
@@ -2839,14 +3311,14 @@ vty_config_write (struct vty *vty)
/* exec-timeout */
if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
- vty_out (vty, " exec-timeout %ld %ld%s",
+ vty_out (vty, " exec-timeout %ld %ld%s",
vty_timeout_val / 60,
vty_timeout_val % 60, VTY_NEWLINE);
/* login */
if (no_password_check)
vty_out (vty, " no login%s", VTY_NEWLINE);
-
+
if (restricted_mode != restricted_mode_default)
{
if (restricted_mode_default)
@@ -2854,7 +3326,7 @@ vty_config_write (struct vty *vty)
else
vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
}
-
+
vty_out (vty, "!%s", VTY_NEWLINE);
return CMD_SUCCESS;
@@ -2874,22 +3346,40 @@ vty_reset ()
unsigned int i;
struct vty *vty;
struct thread *vty_serv_thread;
+ qps_file qf;
+
+ LOCK
for (i = 0; i < vector_active (vtyvec); i++)
if ((vty = vector_slot (vtyvec, i)) != NULL)
{
buffer_reset (vty->obuf);
vty->status = VTY_CLOSE;
- vty_close (vty);
+ uty_close (vty);
}
- for (i = 0; i < vector_active (Vvty_serv_thread); i++)
- if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
- {
- thread_cancel (vty_serv_thread);
- vector_slot (Vvty_serv_thread, i) = NULL;
- close (i);
- }
+ if (master_nexus)
+ {
+ for (i = 0; i < vector_active (Vvty_serv_thread); i++)
+ if ((qf = vector_slot (Vvty_serv_thread, i)) != NULL)
+ {
+ qps_remove_file(qf);
+ qps_file_free(qf);
+ vector_slot (Vvty_serv_thread, i) = NULL;
+ close (i);
+ }
+ }
+ else
+ {
+ assert(master);
+ for (i = 0; i < vector_active (Vvty_serv_thread); i++)
+ if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
+ {
+ thread_cancel (vty_serv_thread);
+ vector_slot (Vvty_serv_thread, i) = NULL;
+ close (i);
+ }
+ }
vty_timeout_val = VTY_TIMEOUT_DEFAULT;
@@ -2904,6 +3394,7 @@ vty_reset ()
XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
vty_ipv6_accesslist_name = NULL;
}
+ UNLOCK
}
static void
@@ -2933,34 +3424,69 @@ vty_get_cwd ()
int
vty_shell (struct vty *vty)
{
- return vty->type == VTY_SHELL ? 1 : 0;
+ LOCK
+ int result;
+ result = uty_shell (vty);
+ UNLOCK
+ return result;
+}
+
+static int
+uty_shell (struct vty *vty)
+{
+ return ((vty == NULL) || (vty->type == VTY_SHELL)) ? 1 : 0;
}
int
vty_shell_serv (struct vty *vty)
{
- return vty->type == VTY_SHELL_SERV ? 1 : 0;
+ LOCK
+ int result;
+ result = ((vty->type == VTY_SHELL_SERV) ? 1 : 0);
+ UNLOCK
+ return result;
}
void
vty_init_vtysh ()
{
- vtyvec = vector_init (VECTOR_MIN_SIZE);
+ LOCK
+ vtyvec = vector_init (0);
+ UNLOCK
+}
+
+/* qpthreads: Install vty's own commands like `who' command. */
+void
+vty_init_r (void)
+{
+ master_nexus = qpn_init_new(master_nexus);
+ vty_mutex = qpt_mutex_init(vty_mutex, qpt_mutex_quagga);
+ vty_init(NULL);
}
-/* Install vty's own commands like `who' command. */
+/* create and execute our thread */
+void
+vty_exec_r(void)
+{
+ if (master_nexus)
+ qpn_exec(master_nexus);
+}
+
+/* threads: Install vty's own commands like `who' command. */
void
vty_init (struct thread_master *master_thread)
{
+ LOCK
+
/* For further configuration read, preserve current directory. */
vty_save_cwd ();
- vtyvec = vector_init (VECTOR_MIN_SIZE);
+ vtyvec = vector_init (0);
master = master_thread;
/* Initilize server thread vector. */
- Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
+ Vvty_serv_thread = vector_init (0);
/* Install bgp top node. */
install_node (&vty_node, vty_config_write);
@@ -2993,11 +3519,18 @@ vty_init (struct thread_master *master_thread)
install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
#endif /* HAVE_IPV6 */
+
+ UNLOCK
}
void
vty_terminate (void)
{
+ LOCK
+
+ if (master_nexus)
+ master_nexus->terminate = 1;
+
if (vty_cwd)
XFREE (MTYPE_TMP, vty_cwd);
@@ -3007,4 +3540,11 @@ vty_terminate (void)
vector_free (vtyvec);
vector_free (Vvty_serv_thread);
}
+ UNLOCK
+
+ if (vty_mutex)
+ vty_mutex = qpt_mutex_destroy(vty_mutex, 1);
}
+
+#undef LOCK
+#undef UNLOCK
diff --git a/lib/vty.h b/lib/vty.h
index 7df04b5f..457c70d4 100644
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -23,14 +23,17 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include "thread.h"
#include "log.h"
+#include "qpthreads.h"
+#include "qpselect.h"
+#include "qtimers.h"
#define VTY_BUFSIZ 512
#define VTY_MAXHIST 20
/* VTY struct. */
-struct vty
+struct vty
{
- /* File descripter of this vty. */
+ /* File descriptor of this vty. */
int fd;
/* Is this vty connect to file or not */
@@ -39,7 +42,7 @@ struct vty
/* Node status of this vty */
int node;
- /* What address is this vty comming from. */
+ /* What address is this vty coming from. */
char *address;
/* Failure count */
@@ -112,11 +115,14 @@ struct vty
int config;
/* Read and write thread. */
+
+ qps_file qf;
struct thread *t_read;
struct thread *t_write;
/* Timeout seconds and thread. */
unsigned long v_timeout;
+ qtimer qtr;
struct thread *t_timeout;
};
@@ -124,7 +130,13 @@ struct vty
#define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
/* Small macro to determine newline is newline only or linefeed needed. */
-#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
+#define VTY_NEWLINE (((vty != NULL) && (vty->type == VTY_TERM)) ? "\r\n" : "\n")
+
+/* For indenting, mostly. */
+extern const char* vty_spaces_string ;
+#define VTY_MAX_SPACES 24
+#define VTY_SPACES(n) (vty_spaces_string + ((n) < VTY_MAX_SPACES \
+ ? VTY_MAX_SPACES - (n) : 0))
/* Default time out value */
#define VTY_TIMEOUT_DEFAULT 600
@@ -199,20 +211,29 @@ do {
/* Exported variables */
extern char integrate_default[];
+extern qpt_mutex_t* vty_mutex;
+#ifndef NDEBUG
+extern int vty_lock_count;
+#endif
/* Prototypes. */
+extern void vty_init_r (void);
+extern void vty_exec_r(void);
extern void vty_init (struct thread_master *);
extern void vty_init_vtysh (void);
extern void vty_terminate (void);
extern void vty_reset (void);
-extern struct vty *vty_new (void);
+extern struct vty *vty_new (int);
extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
+extern int vty_puts(struct vty* vty, const char* str) ;
+extern int vty_out_newline(struct vty *vty) ;
+extern int vty_out_indent(struct vty *vty, int indent) ;
extern void vty_read_config (char *, char *);
extern void vty_time_print (struct vty *, int);
extern void vty_serv_sock (const char *, unsigned short, const char *);
extern void vty_close (struct vty *);
extern char *vty_get_cwd (void);
-extern void vty_log (const char *level, const char *proto,
+extern void vty_log (const char *level, const char *proto,
const char *fmt, struct timestamp_control *, va_list);
extern int vty_config_lock (struct vty *);
extern int vty_config_unlock (struct vty *);
diff --git a/lib/zassert.h b/lib/zassert.h
index 79126760..424688b9 100644
--- a/lib/zassert.h
+++ b/lib/zassert.h
@@ -8,6 +8,18 @@
extern void _zlog_assert_failed (const char *assertion, const char *file,
unsigned int line, const char *function)
__attribute__ ((noreturn));
+extern void _zlog_abort_mess (const char *mess, const char *file,
+ unsigned int line, const char *function)
+ __attribute__ ((noreturn));
+
+extern void _zlog_abort_errno (const char *mess, const char *file,
+ unsigned int line, const char *function)
+ __attribute__ ((noreturn));
+
+extern void _zlog_abort_err (const char *mess, int err, const char *file,
+ unsigned int line, const char *function)
+ __attribute__ ((noreturn));
+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#define __ASSERT_FUNCTION __func__
@@ -21,7 +33,45 @@ extern void _zlog_assert_failed (const char *assertion, const char *file,
(_zlog_assert_failed(#EX, __FILE__, __LINE__, \
__ASSERT_FUNCTION), 0)))
+/* Implicitly *permanent* assert() -- irrespective of NDEBUG */
#undef assert
#define assert(EX) zassert(EX)
+/* Explicitly permanent assert() */
+#define passert(EX) zassert(EX)
+
+/* NDEBUG time assert() */
+#ifndef NDEBUG
+#define dassert(EX) zassert(EX)
+#else
+#define dassert(EX)
+#endif
+
+/* TODO: implement _zlog_abort() to give required messages */
+
+/* Abort with message */
+#define zabort(MS) _zlog_assert_failed(MS, __FILE__, __LINE__, \
+ __ASSERT_FUNCTION)
+
+/* Abort with message and errno and strerror() thereof */
+#define zabort_errno(MS) _zlog_abort_errno(MS, __FILE__, __LINE__, \
+ __ASSERT_FUNCTION)
+
+/* Abort with message and given error and strerror() thereof */
+#define zabort_err(MS, ERR) _zlog_abort_err(MS, ERR, __FILE__, __LINE__, \
+ __ASSERT_FUNCTION)
+
+/*==============================================================================
+ * Compile time CONFIRM gizmo
+ *
+ * Two forms: CONFIRM(e) for use at top (file) level
+ * confirm(e) for use inside compound statements
+ */
+#ifndef CONFIRM
+
+ #define CONFIRM(e) extern void CONFIRMATION(char CONFIRM[(e) ? 1 : -1]) ;
+ #define confirm(e) { CONFIRM(e) }
+
+#endif
+
#endif /* _QUAGGA_ASSERT_H */