Skip to content

Commit

Permalink
Merge pull request #21 from drozdziak1/17-log2-price-metric
Browse files Browse the repository at this point in the history
Logarithm-based price metric
  • Loading branch information
drozdziak1 authored Nov 2, 2018
2 parents fae24ac + 94bec5e commit 7d18ee0
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DEFINES = $(PLATFORM_DEFINES)

CFLAGS = -Os -Wall $(DEFINES) $(EXTRA_DEFINES)

LDLIBS = -lrt
LDLIBS = -lrt -lm

SRCS = babeld.c net.c kernel.c util.c interface.c source.c neighbour.c \
route.c xroute.c message.c resend.c configuration.c local.c \
Expand Down
19 changes: 10 additions & 9 deletions babeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ uint32_t fee = 0;

/**
* A multiplier to indicate how much the user values quality vs. price metrics;
* higher value means quality is valued more
* higher value means quality is valued more; Value is interpreted as 1000x of
* the target value, which makes the default below equal to 1.9
*/
uint16_t quality_multiplier = 1;
uint32_t metric_factor = 1900;

static int
kernel_route_notify(struct kernel_route *route, void *closure)
Expand Down Expand Up @@ -182,7 +183,7 @@ main(int argc, char **argv)

while(1) {
opt = getopt(argc, argv,
"m:p:F:h:H:i:k:A:srS:d:g:G:lwz:M:a:t:T:c:C:DL:I:V");
"m:p:F:h:H:i:k:A:srS:d:g:G:lwz:M:q:t:T:c:C:DL:I:V");
if(opt < 0)
break;

Expand Down Expand Up @@ -303,19 +304,19 @@ main(int argc, char **argv)
fee = a;
break;
}
case 'a': {
case 'q': {
char *endptr = optarg;
unsigned long a = strtoul(optarg, &endptr, 0);
unsigned long factor = strtoul(optarg, &endptr, 0);
errno = 0;

// Display help if strtoul() fails or the value won't fit
if(a > UINT16_MAX || endptr == optarg || errno) {
fprintf(stderr, "Couldn't parse the quality multiplier: %s\n",
if(factor > UINT32_MAX || endptr == optarg || errno) {
fprintf(stderr, "Couldn't parse the metric factor: %s\n",
optarg);
goto usage;
}

quality_multiplier = a;
metric_factor = factor;
break;
}
case 't':
Expand Down Expand Up @@ -906,7 +907,7 @@ main(int argc, char **argv)
" "
"[-d level] [-D] [-L logfile] [-I pidfile]\n"
" "
"[-F fee] [-a multiplier]\n"
"[-F fee] [-q multiplier]\n"
" "
"interface...\n",
BABELD_VERSION);
Expand Down
4 changes: 2 additions & 2 deletions babeld.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#define INFINITY ((unsigned short)(~0))
#define BABEL_INFINITY ((unsigned short)(~0))

#ifndef RTPROT_BABEL
#define RTPROT_BABEL 42
Expand Down Expand Up @@ -109,7 +109,7 @@ extern int kernel_socket;
extern int max_request_hopcount;

extern uint32_t fee;
extern uint16_t quality_multiplier;
extern uint32_t metric_factor;

void schedule_neighbours_check(int msecs, int override);
void schedule_interfaces_check(int msecs, int override);
Expand Down
19 changes: 10 additions & 9 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,12 @@ parse_filter(int c, gnc_t gnc, void *closure, struct filter **filter_return)
} else if(strcmp(token, "allow") == 0) {
filter->action.add_metric = 0;
} else if(strcmp(token, "deny") == 0) {
filter->action.add_metric = INFINITY;
filter->action.add_metric = BABEL_INFINITY;
} else if(strcmp(token, "metric") == 0) {
int metric;
c = getint(c, &metric, gnc, closure);
if(c < -1) goto error;
if(metric <= 0 || metric > INFINITY)
if(metric <= 0 || metric > BABEL_INFINITY)
goto error;
filter->action.add_metric = metric;
} else if(strcmp(token, "src-prefix") == 0) {
Expand All @@ -487,7 +487,7 @@ parse_filter(int c, gnc_t gnc, void *closure, struct filter **filter_return)
int table;
c = getint(c, &table, gnc, closure);
if(c < -1) goto error;
if(table <= 0 || table > INFINITY)
if(table <= 0 || table > BABEL_INFINITY)
goto error;
filter->action.table = table;
} else {
Expand Down Expand Up @@ -801,7 +801,8 @@ parse_option(int c, gnc_t gnc, void *closure, char *token)
strcmp(token, "diversity") != 0 &&
strcmp(token, "diversity-factor") != 0 &&
strcmp(token, "smoothing-half-life") != 0 &&
strcmp(token, "fee") != 0)
strcmp(token, "fee") != 0 &&
strcmp(token, "metric-factor") != 0)
goto error;
}

Expand Down Expand Up @@ -935,12 +936,12 @@ parse_option(int c, gnc_t gnc, void *closure, char *token)
fee = f;
check_xroutes(1);

} else if (strcmp(token, "quality-multiplier") == 0) {
} else if (strcmp(token, "metric-factor") == 0) {
uint32_t f = 0;
c = getuint32_t(c, &f, gnc, closure);
if(c < -1 || f > UINT16_MAX)
if(c < -1 || f > UINT32_MAX)
goto error;
quality_multiplier = f;
metric_factor = f;
} else if(strcmp(token, "smoothing-half-life") == 0) {
int h;
c = getint(c, &h, gnc, closure);
Expand Down Expand Up @@ -1349,7 +1350,7 @@ redistribute_filter(const unsigned char *prefix, unsigned short plen,
res = do_filter(redistribute_filters, NULL, prefix, plen,
src_prefix, src_plen, NULL, ifindex, proto, result);
if(res < 0)
res = INFINITY;
res = BABEL_INFINITY;
return res;
}

Expand All @@ -1362,7 +1363,7 @@ install_filter(const unsigned char *prefix, unsigned short plen,
res = do_filter(install_filters, NULL, prefix, plen,
src_prefix, src_plen, NULL, 0, 0, result);
if(res < 0)
res = INFINITY;
res = BABEL_INFINITY;
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion disambiguation.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ kchange_route_metric(const struct babel_route *route,
unsigned refmetric, unsigned cost, unsigned add)
{
int old_metric = metric_to_kernel(route_metric(route));
int new_metric = metric_to_kernel(MIN(refmetric + cost + add, INFINITY));
int new_metric = metric_to_kernel(MIN(refmetric + cost + add, BABEL_INFINITY));
int rc;
struct babel_route *rt1 = NULL;
struct route_stream *stream = NULL;
Expand Down
25 changes: 23 additions & 2 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ local_notify_route(struct babel_route *route, int kind)
}

static void
local_notify_price_1(struct local_socket *s)
local_notify_fee_1(struct local_socket *s)
{
char buf[64];
int rc;
Expand All @@ -298,6 +298,26 @@ local_notify_price_1(struct local_socket *s)
return;
}

static void
local_notify_metric_factor_1(struct local_socket *s)
{
char buf[64];
int rc;
rc = snprintf(buf, 64, "metric factor %d\n", metric_factor);

if(rc < 0 || rc >= 64)
goto fail;

rc = write_timeout(s->fd, buf, rc);
if(rc < 0)
goto fail;
return;

fail:
shutdown(s->fd, 1);
return;
}

static void
local_notify_all_1(struct local_socket *s)
{
Expand All @@ -306,7 +326,8 @@ local_notify_all_1(struct local_socket *s)
struct xroute_stream *xroutes;
struct route_stream *routes;

local_notify_price_1(s);
local_notify_fee_1(s);
local_notify_metric_factor_1(s);

FOR_ALL_INTERFACES(ifp) {
local_notify_interface_1(s, ifp, LOCAL_ADD);
Expand Down
14 changes: 7 additions & 7 deletions message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1307,10 +1307,10 @@ really_send_update(struct interface *ifp,

add_metric = output_filter(id, prefix, plen, src_prefix,
src_plen, ifp->ifindex);
if(add_metric >= INFINITY)
if(add_metric >= BABEL_INFINITY)
return;

metric = MIN(metric + add_metric, INFINITY);
metric = MIN(metric + add_metric, BABEL_INFINITY);
/* Worst case */
ensure_space(ifp, 20 + 12 + 28 + 18 + fp_rtt_size);

Expand Down Expand Up @@ -1540,7 +1540,7 @@ flushupdates(struct interface *ifp)
route_metric(route) :
route_metric_noninterfering(route);

if(metric < INFINITY)
if(metric < BABEL_INFINITY)
satisfy_request(route->src->prefix, route->src->plen,
route->src->src_prefix,
route->src->src_plen,
Expand Down Expand Up @@ -1583,7 +1583,7 @@ flushupdates(struct interface *ifp)
after an xroute has been retracted, so send a retraction. */
really_send_update(ifp, myid, b[i].prefix, b[i].plen,
b[i].src_prefix, b[i].src_plen,
myseqno, INFINITY, INFINITY ,NULL, -1, 0, 0);
myseqno, BABEL_INFINITY, BABEL_INFINITY ,NULL, -1, 0, 0);
}
}
schedule_flush_now(ifp);
Expand Down Expand Up @@ -1663,7 +1663,7 @@ send_update(struct interface *ifp, int urgent,
/* Since flushupdates only deals with non-wildcard interfaces, we
need to do this now. */
route = find_installed_route(prefix, plen, src_prefix, src_plen);
if(route && route_metric(route) < INFINITY)
if(route && route_metric(route) < BABEL_INFINITY)
satisfy_request(prefix, plen, src_prefix, src_plen,
route->src->seqno, route->src->id, NULL);
}
Expand Down Expand Up @@ -2247,7 +2247,7 @@ handle_request(struct neighbour *neigh, const unsigned char *prefix,
return;

/* Let's try to forward this request. */
if(route && route_metric(route) < INFINITY)
if(route && route_metric(route) < BABEL_INFINITY)
successor = route->neigh;

if(!successor || successor == neigh) {
Expand All @@ -2257,7 +2257,7 @@ handle_request(struct neighbour *neigh, const unsigned char *prefix,

other_route = find_best_route(prefix, plen, src_prefix, src_plen,
0, neigh);
if(other_route && route_metric(other_route) < INFINITY)
if(other_route && route_metric(other_route) < BABEL_INFINITY)
successor = other_route->neigh;
}

Expand Down
22 changes: 11 additions & 11 deletions neighbour.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ find_neighbour(const unsigned char *address, struct interface *ifp)

neigh->hello.seqno = neigh->uhello.seqno = -1;
memcpy(neigh->address, address, 16);
neigh->txcost = INFINITY;
neigh->txcost = BABEL_INFINITY;
neigh->ihu_time = now;
neigh->hello.time = neigh->uhello.time = zero;
neigh->hello_rtt_receive_time = zero;
Expand Down Expand Up @@ -203,7 +203,7 @@ reset_txcost(struct neighbour *neigh)
if(delay >= 180000 || (neigh->hello.reach & 0xFFF0) == 0 ||
(neigh->ihu_interval > 0 &&
delay >= neigh->ihu_interval * 10 * 10)) {
neigh->txcost = INFINITY;
neigh->txcost = BABEL_INFINITY;
neigh->ihu_time = now;
return 1;
}
Expand Down Expand Up @@ -284,7 +284,7 @@ neighbour_rxcost(struct neighbour *neigh)

if(((reach & 0xFFF0) == 0 || delay >= 180000) &&
((ureach & 0xFFF0) == 0 || udelay >= 180000)) {
return INFINITY;
return BABEL_INFINITY;
} else if((neigh->ifp->flags & IF_LQ)) {
int sreach =
((reach & 0x8000) >> 2) +
Expand All @@ -295,12 +295,12 @@ neighbour_rxcost(struct neighbour *neigh)
/* cost >= interface->cost */
if(delay >= 40000)
cost = (cost * (delay - 20000) + 10000) / 20000;
return MIN(cost, INFINITY);
return MIN(cost, BABEL_INFINITY);
} else {
if(two_three(reach) || two_three(ureach))
return neigh->ifp->cost;
else
return INFINITY;
return BABEL_INFINITY;
}
}

Expand Down Expand Up @@ -333,16 +333,16 @@ neighbour_cost(struct neighbour *neigh)
unsigned a, b, cost;

if(!if_up(neigh->ifp))
return INFINITY;
return BABEL_INFINITY;

a = neighbour_txcost(neigh);

if(a >= INFINITY)
return INFINITY;
if(a >= BABEL_INFINITY)
return BABEL_INFINITY;

b = neighbour_rxcost(neigh);
if(b >= INFINITY)
return INFINITY;
if(b >= BABEL_INFINITY)
return BABEL_INFINITY;

if(!(neigh->ifp->flags & IF_LQ) || (a < 256 && b < 256)) {
cost = a;
Expand All @@ -359,7 +359,7 @@ neighbour_cost(struct neighbour *neigh)

cost += neighbour_rttcost(neigh);

return MIN(cost, INFINITY);
return MIN(cost, BABEL_INFINITY);
}

int
Expand Down
4 changes: 2 additions & 2 deletions resend.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ record_resend(int kind, const unsigned char *prefix, unsigned char plen,
if((kind == RESEND_REQUEST &&
input_filter(NULL, prefix, plen, src_prefix, src_plen, NULL,
ifindex) >=
INFINITY) ||
BABEL_INFINITY) ||
(kind == RESEND_UPDATE &&
output_filter(NULL, prefix, plen, src_prefix, src_plen, ifindex) >=
INFINITY))
BABEL_INFINITY))
return 0;

if(delay >= 0xFFFF)
Expand Down
Loading

0 comments on commit 7d18ee0

Please sign in to comment.