pmlag

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

commit b08118b433d37df4258dc3364bf3d842f81101f0
parent 2f6173320ae202a9f2ceb151a9c8b628dae3518d
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Tue, 10 Oct 2023 23:02:39 +0200

Open interface sockets within loop, so I can close them to re-open if broken

Diffstat:
Msrc/main.c | 86+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Msrc/util/config.c | 2++
Msrc/util/config.h | 8+++++++-
3 files changed, 62 insertions(+), 34 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -60,37 +60,10 @@ int main(int argc, const char **argv) { // Initialize epoll epfd = epoll_create1(0); - int b = 0; - int i = 0; - for( b = 0 ; b < config->bond_count ; b++ ) { - for( i = 0 ; i < config->bond[b]->iface_count ; i++ ) { - iface = config->bond[b]->iface[i]; - - // Start a socket on the interface - iface->sockfd = sockraw_open(iface->name); - if (iface->sockfd < 0) { - return 2; - } - /* printf("%s: %d\n", */ - /* iface->name, */ - /* iface->sockfd */ - /* ); */ - - epev = malloc(sizeof(struct epoll_event)); - if (!epev) return 3; - epev->events = EPOLLIN; - epev->data.ptr = iface; - if (epoll_ctl(epfd, EPOLL_CTL_ADD, iface->sockfd, epev)) { - free(epev); - close(iface->sockfd); - iface->sockfd = 0; - continue; - } - iface->epev = epev; - } - } // DEBUG: Display bonds + int b = 0; + int i = 0; for( b = 0 ; b < config->bond_count ; b++ ) { printf("%s\n", config->bond[b]->name); for( i = 0 ; i < config->bond[b]->iface_count ; i++ ) { @@ -104,18 +77,65 @@ int main(int argc, const char **argv) { int64_t ttime = millis(); int64_t tdiff = 0; while(1) { - ev_count = epoll_wait(epfd, events, 8, tdiff); - for( i=0 ; i<ev_count; i++) { - // TODO: process(events[i].data.ptr); - } + // Calculate waiting time & periodic actions tdiff = ttime - millis(); if (tdiff <= 0) { ttime += 1000; tdiff += 1000; + + // Handle sockets + for( b = 0 ; b < config->bond_count ; b++ ) { + + // (re)open bond tap + + for( i = 0 ; i < config->bond[b]->iface_count ; i++ ) { + iface = config->bond[b]->iface[i]; + + // (re)open interface socket + if (!iface->sockfd) { + iface->sockfd = sockraw_open(iface->name); + if (iface->sockfd < 0) { + perror("sockraw_open"); + iface->sockfd = 0; + continue; + } + printf("Opened socket for %s\n", iface->name); + } + + // Register it with epoll + if (!iface->epev) { + iface->epev = calloc(1, sizeof(struct epoll_event)); + if (!iface->epev) { + perror("calloc"); + iface->epev = NULL; + continue; + } + iface->epev->events = EPOLLIN; + iface->epev->data.ptr = iface; + if (epoll_ctl(epfd, EPOLL_CTL_ADD, iface->sockfd, iface->epev)) { + free(iface->epev); + close(iface->sockfd); + iface->sockfd = 0; + iface->epev = NULL; + continue; + } + printf("Registered %s with epoll\n", iface->name); + } + + } + } + // TODO: time tick method (for announce handling) printf("ttime: %ld\n", ttime); } + + // Handle incoming data + ev_count = epoll_wait(epfd, events, 8, tdiff); + for( i=0 ; i<ev_count; i++) { + // TODO: process(events[i].data.ptr); + } + } return 69; diff --git a/src/util/config.c b/src/util/config.c @@ -37,6 +37,7 @@ static int config_load_handler( config->bond[config->bond_count++] = bond = calloc(1, sizeof(struct pmlag_bond)); config->bond = realloc(config->bond, (config->bond_count + 1) * sizeof(void*)); config->bond[config->bond_count ] = NULL; + bond->type = PMLAG_ENTITY_TYPE_BOND; bond->name = strdup(section); } @@ -61,6 +62,7 @@ static int config_load_handler( bond->iface[bond->iface_count++] = iface = calloc(1, sizeof(struct pmlag_iface)); bond->iface = realloc(bond->iface, (bond->iface_count + 1) * sizeof(void*)); bond->iface[bond->iface_count ] = NULL; + iface->type = PMLAG_ENTITY_TYPE_IFACE; iface->name = strdup(value); iface->bond = bond; } diff --git a/src/util/config.h b/src/util/config.h @@ -1,12 +1,17 @@ #ifndef __PMLAG_UTIL_CONFIG_H__ #define __PMLAG_UTIL_CONFIG_H__ +#define PMLAG_ENTITY_TYPE int +#define PMLAG_ENTITY_TYPE_IFACE 1 +#define PMLAG_ENTITY_TYPE_BOND 2 + struct pmlag_iface { + PMLAG_ENTITY_TYPE type; char *name; struct pmlag_bond *bond; int sockfd; struct epoll_event *epev; - /* /1* int weight; // weight of this interface within the bond *1/ */ + /* /1* int weight; // weight of this interface within the bond *1/ */ /* int sockfd; // file descriptor for the iface raw socket */ /* int ifidx; // index of the interface within the socket */ /* pthread_t tid; // thread id where the iface listener recides in */ @@ -14,6 +19,7 @@ struct pmlag_iface { }; struct pmlag_bond { + PMLAG_ENTITY_TYPE type; char *name; struct pmlag_iface **iface; int iface_count;