pmlag

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

commit f458e825654f7e7f4fdeaa1af0c5985c34c3b55c
parent 7ec86efc4ed1c6a8e112e5afca6c5c310a34ef27
Author: Robin Bron <robin@finwo.nl>
Date:   Sun, 26 Feb 2023 01:28:34 +0100

Separate task_iface_onpacket method

Diffstat:
Msrc/task/bond.c | 8++++----
Msrc/task/iface.c | 61+++++++++++++++++++++++++------------------------------------
2 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/src/task/bond.c b/src/task/bond.c @@ -129,7 +129,7 @@ void * task_bond_thread(void *arg) { return NULL; } -void * thread_bond_old(void *arg) { +/* void * thread_bond_old(void *arg) { */ /* struct pmlag_rt_entry *rt_entry; */ /* int iface_list_len; */ /* int iface_list_sel; */ @@ -198,6 +198,6 @@ void * thread_bond_old(void *arg) { /* iface = iface->next; */ /* } */ - pthread_exit(NULL); - return NULL; -} + /* pthread_exit(NULL); */ + /* return NULL; */ +/* } */ diff --git a/src/task/iface.c b/src/task/iface.c @@ -7,6 +7,27 @@ #include "../util/config.h" #include "../util/socket.h" +int task_iface_onpacket(struct pmlag_iface *iface, unsigned char *buffer, size_t buflen) { + size_t send_len; + + // Debug: print eth header + printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x < %.2x:%.2x:%.2x:%.2x:%.2x:%.2x, %.4x (%ld)\n", + buffer[0],buffer[1],buffer[2],buffer[3],buffer[ 4],buffer[ 5], // DST + buffer[6],buffer[7],buffer[8],buffer[9],buffer[10],buffer[11], // SRC + ((unsigned int)((unsigned char)buffer[12]) << 8) + buffer[13], // PROTO + buflen + ); + + // Redirect packet to bond socket as-is + send_len = write(iface->bond->sockfd, buffer, buflen); + if (buflen != send_len) { + perror("write(bond)"); + return 1; + } + + return 0; +} + void * task_iface_thread(void *arg) { struct pmlag_iface *iface = arg; @@ -21,7 +42,7 @@ void * task_iface_thread(void *arg) { iface->ifidx = iface_idx(iface->sockfd, iface->name); // Reserve receive buffer, support 64k packets just in case - size_t buflen, send_len; + size_t buflen; unsigned char *buffer = (unsigned char *) malloc(RCVBUFSIZ); struct sockaddr saddr; int saddr_len = sizeof(saddr); @@ -36,7 +57,7 @@ void * task_iface_thread(void *arg) { while(1) { // Zero out buffer, to prevent pollution, & receive packet - memset(buffer, 0, RCVBUFSIZ); + /* memset(buffer, 0, RCVBUFSIZ); */ buflen = recvfrom(iface->sockfd, buffer, RCVBUFSIZ, 0, &saddr, (socklen_t *)&saddr_len); if (buflen < 0) { perror("recvfrom"); @@ -44,44 +65,17 @@ void * task_iface_thread(void *arg) { return NULL; } - // Debug: print eth header - printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x < %.2x:%.2x:%.2x:%.2x:%.2x:%.2x, %.4x (%ld)\n", - buffer[0],buffer[1],buffer[2],buffer[3],buffer[ 4],buffer[ 5], // DST - buffer[6],buffer[7],buffer[8],buffer[9],buffer[10],buffer[11], // SRC - ((unsigned int)((unsigned char)buffer[12]) << 8) + buffer[13], // PROTO - buflen - ); - - // Redirect packet to bond socket as-is - send_len = write(iface->bond->sockfd, buffer, buflen); - if (buflen != send_len) { - perror("write(bond)"); + // And let the more specific method handle the packet + if(task_iface_onpacket(iface, buffer, buflen)) { pthread_exit(NULL); return NULL; } - - /* sleep(1); */ } return NULL; } - -/* /1* printf("Thread started for iface: %s->%s(%d)\n", iface->bond->name, iface->name, iface->sockfd); *1/ */ - - -/* // Find bond socket iface_idx */ -/* int send_len; */ -/* uint16_t proto; */ -/* uint16_t bcidx; */ - -/* size_t rt_len; */ - -/* while(1) { */ - - - /* // Update routing table if our custom protocol is seen */ /* proto = ((uint16_t)((unsigned char)buffer[(ETH_ALEN*2)+0]) << 8) + buffer[(ETH_ALEN*2)+1]; */ /* if (proto == 0x0666) { */ @@ -97,8 +91,3 @@ void * task_iface_thread(void *arg) { /* printf("Current RT length: %ld\n", rt_len); */ -/* } */ - -/* pthread_exit(NULL); */ -/* return NULL; */ -/* } */