pmlag

Poor man's link aggregation
git clone git://git.finwo.net/app/pmlag
Log | Files | Refs | README | LICENSE

commit 5f935fbe6235e8caa4da30a2a6b50bcd76b29b31
parent 43975cfe8d5da9f7074a2c8a353fb3b9412c7a77
Author: Robin Bron <robin@finwo.nl>
Date:   Wed,  8 Mar 2023 20:23:51 +0100

Replaced tidwall/btree by finwo/mindex

Diffstat:
MMakefile | 2+-
Mpackage.ini | 2+-
Mpmlag.1 | 7+++++++
Mpmlag.html | 7+++++++
Msrc/main.c | 2--
Msrc/task/bond.c | 4++--
Msrc/util/config.h | 4++--
Msrc/util/routing-table.c | 86++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Msrc/util/routing-table.h | 6++++--
9 files changed, 72 insertions(+), 48 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ SRC:= SRC+=$(wildcard src/*.c) SRC+=$(wildcard src/*/*.c) -override CFLAGS?=-Wall -g +override CFLAGS?=-Wall -g -O2 INCLUDES:= INCLUDES+=-I src diff --git a/package.ini b/package.ini @@ -1,7 +1,7 @@ [dependencies] benhoyt/inih=https://raw.githubusercontent.com/finwo/dep-repository/main/benhoyt/inih/package-edge.ini cofyc/argparse=https://raw.githubusercontent.com/finwo/dep-repository/main/cofyc/argparse/package.ini -tidwall/btree=https://raw.githubusercontent.com/finwo/dep-repository/main/tidwall/btree/package.ini +finwo/mindex=https://github.com/finwo/c-mindex/archive/refs/tags/edge.tar.gz [package] deps=lib name=finwo/pmlag diff --git a/pmlag.1 b/pmlag.1 @@ -19,6 +19,13 @@ .SH NAME .PP pmlag - Poor man\[cq]s link aggregator +.SH COMPILATION +.PP +This library makes use of dep (https://github.com/finwo/dep) to manage +it\[cq]s dependencies and exports. +.PP +To build, first run \f[V]dep install\f[R] from the root of the +repository, after which you can run \f[V]make\f[R] to build the project. .SH SYNOPSIS .PP \f[V]pmlag [options]\f[R] diff --git a/pmlag.html b/pmlag.html @@ -164,6 +164,13 @@ </header> <h1 id="name">NAME</h1> <p>pmlag - Poor man’s link aggregator</p> +<h1 id="compilation">COMPILATION</h1> +<p>This library makes use of <a +href="https://github.com/finwo/dep">dep</a> to manage it’s dependencies +and exports.</p> +<p>To build, first run <code>dep install</code> from the root of the +repository, after which you can run <code>make</code> to build the +project.</p> <h1 id="synopsis">SYNOPSIS</h1> <p><code>pmlag [options]</code></p> <h1 id="license">LICENSE</h1> diff --git a/src/main.c b/src/main.c @@ -15,12 +15,10 @@ #include <stdio.h> #include <unistd.h> -#include "tidwall/btree.h" #include "cofyc/argparse.h" #include "util/config.h" #include "task/bond.h" -/* #include "socket.h" */ static const char *const usage[] = { __NAME " [options]", diff --git a/src/task/bond.c b/src/task/bond.c @@ -7,7 +7,7 @@ #include <sys/socket.h> #include <unistd.h> -#include "tidwall/btree.h" +#include "finwo/mindex.h" #include "../util/config.h" #include "../util/routing-table.h" #include "../util/socket.h" @@ -142,7 +142,7 @@ void * task_bond_thread(void *arg) { /* // Fetch entry from routing table */ /* pthread_mutex_lock(&(bond->mtx_rt)); */ -/* rt_entry = btree_get(bond->rt, &(struct pmlag_rt_entry){ .mac = buffer }); */ +/* rt_entry = mindex_get(bond->rt, &(struct pmlag_rt_entry){ .mac = buffer }); */ /* // Broadcast on ALL interfaces if no rt entry OR broadcast packet */ /* if ((!rt_entry) || (memcmp(buffer, "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ALEN) == 0)) { */ diff --git a/src/util/config.h b/src/util/config.h @@ -4,7 +4,7 @@ #include <pthread.h> #include <stdint.h> -#include "tidwall/btree.h" +#include "finwo/mindex.h" #include "linked-list.h" @@ -38,7 +38,7 @@ struct pmlag_bond { int16_t bcidx; // big-endian broadcast index for quickly detecting dead paths pthread_t tid; // thread id where the bond interface listener recides in pthread_mutex_t mtx_rt; // lock for the routing table of the bond - struct btree *rt; // pointer to the routing table + struct mindex_t *rt; // pointer to the routing table pmlag_iface_llist *interfaces; // linked-list of interfaces contained in the bond }; diff --git a/src/util/routing-table.c b/src/util/routing-table.c @@ -4,27 +4,37 @@ #include <string.h> #include "config.h" -#include "tidwall/btree.h" +#include "finwo/mindex.h" #include "routing-table.h" -static int compare_rt_entries(const void *a, const void *b, void *udata) { +static int rt_compare(const void *a, const void *b, void *udata) { struct pmlag_rt_entry *ta = (struct pmlag_rt_entry *)a; struct pmlag_rt_entry *tb = (struct pmlag_rt_entry *)b; + + printf("\nCMP\n A = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n B = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", + ta->mac[0],ta->mac[1],ta->mac[2],ta->mac[3],ta->mac[4],ta->mac[5], + tb->mac[0],tb->mac[1],tb->mac[2],tb->mac[3],tb->mac[4],tb->mac[5] + ); + return memcmp(ta->mac, tb->mac, ETH_ALEN); } -struct btree * rt_init(void *udata) { - return btree_new(sizeof(void*), 0, compare_rt_entries, udata); +static void rt_purge(const void *item, void *udata) { + // Empty for now +} + +struct mindex_t * rt_init(void *udata) { + return mindex_init(rt_compare, rt_purge, udata); } int rt_upsert( - struct btree *rt, + struct mindex_t *rt, pthread_mutex_t *mtx, struct pmlag_iface *iface, unsigned char *mac, int16_t bcidx ) { - int isnew = 0; + /* int isnew = 0; */ struct pmlag_rt_entry *rt_entry; printf("\nUpserting RT, %d\n", bcidx); @@ -33,58 +43,58 @@ int rt_upsert( pthread_mutex_lock(mtx); // Attempt to fetch the rt entry - rt_entry = btree_get(rt, &((struct pmlag_rt_entry){ .mac = mac })); + rt_entry = mindex_get(rt, &((struct pmlag_rt_entry){ .mac = mac })); - int16_t obcidx = 0; + /* int16_t obcidx = 0; */ if (rt_entry) { - obcidx = rt_entry->bcidx; + /* obcidx = rt_entry->bcidx; */ printf(" Found, %d\n", rt_entry->bcidx); } // None given, build new one if (!rt_entry) { - rt_entry = malloc(sizeof(struct pmlag_rt_entry)); + rt_entry = calloc(1, sizeof(struct pmlag_rt_entry)); rt_entry->mac = malloc(ETH_ALEN); - rt_entry->bcidx = 0; + /* rt_entry->bcidx = 0; */ rt_entry->interfaces = NULL; memcpy(rt_entry->mac, mac, ETH_ALEN); - isnew = 1; + /* isnew = 1; */ } - printf(" Old bcidx, %d\n", rt_entry->bcidx); - if ((!isnew) && (obcidx != rt_entry->bcidx)) { - printf(" BORKED\n"); - exit(1); - } - - // Bail if - if ( - (!bcidx && rt_entry->bcidx) || // We receive a regular packet on bcidx-tracked entry - ((bcidx - rt_entry->bcidx) < 0) // Or the received bcidx is lower than known (old packet) - ) { - printf(" Bail, %d, %d\n\n", bcidx, rt_entry->bcidx); - pthread_mutex_unlock(mtx); - return 0; - } + /* printf(" Old bcidx, %d\n", rt_entry->bcidx); */ + /* if ((!isnew) && (obcidx != rt_entry->bcidx)) { */ + /* printf(" BORKED\n"); */ + /* exit(1); */ + /* } */ + + /* // Bail if */ + /* if ( */ + /* (!bcidx && rt_entry->bcidx) || // We receive a regular packet on bcidx-tracked entry */ + /* ((bcidx - rt_entry->bcidx) < 0) // Or the received bcidx is lower than known (old packet) */ + /* ) { */ + /* printf(" Bail, %d, %d\n\n", bcidx, rt_entry->bcidx); */ + /* pthread_mutex_unlock(mtx); */ + /* return 0; */ + /* } */ // Clear list of known interfaces if pmlag_iface_llist *iface_entry; - if ( - (bcidx && (rt_entry->bcidx != bcidx)) || // We got a NEW broadcast index - (!bcidx && (rt_entry->bcidx == 0)) // Or we're updating a non-pmlag remote - ) { + /* if ( */ + /* (bcidx && (rt_entry->bcidx != bcidx)) || // We got a NEW broadcast index */ + /* (!bcidx && (rt_entry->bcidx == 0)) // Or we're updating a non-pmlag remote */ + /* ) { */ // Free list 1-by-1 while(rt_entry->interfaces) { iface_entry = rt_entry->interfaces; rt_entry->interfaces = iface_entry->next; free(iface_entry); } - } + /* } */ - // Update the rt_entry's broadcast index - rt_entry->bcidx = bcidx; + /* // Update the rt_entry's broadcast index */ + /* rt_entry->bcidx = bcidx; */ - printf(" How 'bout now, %d\n", rt_entry->bcidx); + /* printf(" How 'bout now, %d\n", rt_entry->bcidx); */ // Add our iface to the entry's interface list iface_entry = malloc(sizeof(pmlag_iface_llist)); @@ -93,10 +103,10 @@ int rt_upsert( rt_entry->interfaces = iface_entry; // Ensure the entry is in the rt - btree_set(rt, rt_entry); + mindex_set(rt, rt_entry); - printf(" or now, %d\n", rt_entry->bcidx); - printf(" RT is now %ld\n\n", btree_count(rt)); + /* printf(" or now, %d\n", rt_entry->bcidx); */ + printf(" RT is now %ld\n\n", mindex_length(rt)); pthread_mutex_unlock(mtx); return 0; diff --git a/src/util/routing-table.h b/src/util/routing-table.h @@ -3,16 +3,18 @@ #include "config.h" +#include "finwo/mindex.h" + struct pmlag_rt_entry { unsigned char *mac; // mac address of the remote entity int16_t bcidx; // broadcast index last seen from the mac pmlag_iface_llist *interfaces; // list of pointers to interfaces }; -struct btree * rt_init(void *udata); +struct mindex_t * rt_init(void *udata); int rt_upsert( - struct btree *rt, + struct mindex_t *rt, pthread_mutex_t *mtx, struct pmlag_iface *iface, unsigned char *mac,