Page Menu
Home
ClusterLabs Projects
Search
Configure Global Search
Log In
Files
F5519422
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
25 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/knet.c b/knet.c
index 529821c2..d34a418c 100644
--- a/knet.c
+++ b/knet.c
@@ -1,192 +1,214 @@
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
+#include <net/ethernet.h>
#include "knet.h"
#include "logging.h"
#include "utils.h"
+
+/* make this configurable */
+uint8_t knet_hwvend[2] = { 0x16, 0x07 };
+
+
int knet_open(char *dev, size_t dev_size)
{
struct ifreq ifr;
int fd, err;
if (dev_size < IFNAMSIZ) {
errno = EINVAL;
return -1;
}
if (strlen(dev) > IFNAMSIZ) {
errno = E2BIG;
return -1;
}
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
errno = ENOENT;
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int knet_set_hwid(char *dev, uint32_t nodeid)
{
struct ifreq ifr;
int sockfd, ret;
uint32_t machwid;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
return sockfd;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ret = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
if (ret != 0) goto exit_clean;
- ifr.ifr_hwaddr.sa_data[0] = 0x16;
- ifr.ifr_hwaddr.sa_data[1] = 0x07;
-
machwid = htonl(nodeid);
- memmove(ifr.ifr_hwaddr.sa_data + 2, &machwid, ETH_ALEN - 2);
+
+ memmove(ifr.ifr_hwaddr.sa_data, knet_hwvend, sizeof(knet_hwvend));
+ memmove(ifr.ifr_hwaddr.sa_data + sizeof(knet_hwvend),
+ &machwid, ETH_ALEN - sizeof(knet_hwvend));
ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
exit_clean:
close(sockfd);
return ret;
}
+uint32_t knet_hwtoid(void *packet)
+{
+ uint32_t nodeid;
+ struct ether_header *eth_h = packet;
+
+ if (memcmp(eth_h->ether_dhost, knet_hwvend, sizeof(knet_hwvend)) != 0)
+ return 0;
+
+ memmove(&nodeid, eth_h->ether_dhost + sizeof(knet_hwvend),
+ ETH_ALEN - sizeof(knet_hwvend));
+
+ nodeid = ntohl(nodeid);
+
+ return nodeid;
+}
+
int knet_get_mtu(char *dev)
{
struct ifreq ifr;
int sockfd, err;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
return sockfd;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
err = ioctl(sockfd, SIOCGIFMTU, (void *)&ifr);
close(sockfd);
if (err < 0)
return err;
return ifr.ifr_mtu;
}
int knet_close(int fd)
{
return close(fd);
}
int knet_read(int fd, char *buf, int len)
{
return do_read(fd, buf, len);
}
int knet_write(int fd, char *buf, int len)
{
return do_write(fd, buf, len);
}
/*
* TODO:
* - knet_up + add_ip - add one function to run /sbin/ip
* - use fork + dup2 + exec?? to redirect stderr
* - correctly handle broadcast address (only for ipv4)
*/
/*
* Bring tun interface up and set mtu. If mtu is 0, system default is used.
*/
int knet_up(const char *dev_name, int mtu)
{
char cmd[512];
int res;
snprintf(cmd, sizeof(cmd), "%s link set %s up", IPROUTE_CMD, dev_name);
if (mtu != 0) {
snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), " mtu %d", mtu);
}
log_printf(LOGSYS_LEVEL_DEBUG, "Spawning %s\n", cmd);
res = system(cmd);
if (res == -1 || res == 127) {
log_printf(LOGSYS_LEVEL_INFO, "Unable to spawn shell\n");
return -1;
}
if (!WIFEXITED(res)) {
log_printf(LOGSYS_LEVEL_INFO, "Shell not exited properly\n");
return -1;
}
if (WIFEXITED(res) && WEXITSTATUS(res) != 0) {
log_printf(LOGSYS_LEVEL_INFO, "Shell return code %d is not 0\n", WEXITSTATUS(res));
return -1;
}
return 0;
}
/*
* Add IP to tun interface.
*/
int knet_add_ip(const char *dev_name, const char *ip)
{
char cmd[512];
int res;
snprintf(cmd, sizeof(cmd), "%s addr add %s dev %s", IPROUTE_CMD, ip, dev_name);
log_printf(LOGSYS_LEVEL_DEBUG, "Spawning %s\n", cmd);
res = system(cmd);
if (res == -1 || res == 127) {
log_printf(LOGSYS_LEVEL_INFO, "Unable to spawn shell\n");
return -1;
}
if (!WIFEXITED(res)) {
log_printf(LOGSYS_LEVEL_INFO, "Shell not exited properly\n");
return -1;
}
if (WIFEXITED(res) && WEXITSTATUS(res) != 0) {
log_printf(LOGSYS_LEVEL_INFO, "Shell return code %d is not 0\n", WEXITSTATUS(res));
return -1;
}
return 0;
}
diff --git a/knet.h b/knet.h
index 93610f09..3e1937d8 100644
--- a/knet.h
+++ b/knet.h
@@ -1,20 +1,24 @@
#ifndef __KNET_H__
#define __KNET_H__
#include <stdlib.h>
+#include <stdint.h>
/*
* TODO: Make this configurable
*/
#define IPROUTE_CMD "/sbin/ip"
int knet_open(char *dev, size_t dev_size);
int knet_set_hwid(char *dev, uint32_t nodeid);
+uint32_t knet_hwtoid(void *packet);
int knet_get_mtu(char *dev);
int knet_close(int fd);
int knet_read(int fd, char *buf, int len);
int knet_write(int fd, char *buf, int len);
extern int knet_up(const char *dev_name, int mtu);
extern int knet_add_ip(const char *dev_name, const char *ip);
+extern uint8_t knet_hwvend[2];
+
#endif
diff --git a/main.c b/main.c
index a03117fe..b09aa483 100644
--- a/main.c
+++ b/main.c
@@ -1,672 +1,672 @@
#include "config.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>
#include <netinet/ether.h>
#include <netinet/if_ether.h>
#include <endian.h>
#include <byteswap.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define swap16(x) bswap_16(x)
#define swap32(x) bswap_32(x)
#define swap64(x) bswap_64(x)
#endif
#if __BYTE_ORDER == _BIG_ENDIAN
#define swap16(x) (x)
#define swap32(x) (x)
#define swap64(x) (x)
#endif
#include "conf.h"
#include "logging.h"
#include "nodes.h"
#include "controlt.h"
#include "netsocket.h"
#include "utils.h"
#include "knet.h"
#include "controlt_comm.h"
#define LOCKFILE_NAME RUNDIR PACKAGE ".pid"
#define OPTION_STRING "hdfVc:"
#define DEFAULT_NET_NAME "kronosnet%d"
#define TX_KNET_DATASIZE 131072
#define TX_KNET_SIZE (TX_KNET_DATASIZE + sizeof(struct knet_header))
int daemonize = 1;
int debug = 0;
int daemon_quit = 0;
char *conffile = NULL;
int statistics = 0;
int rerouting = 0;
int net_sock;
int eth_fd;
char localnet[16]; /* match IFNAMSIZ from linux/if.h */
static pthread_t eth_thread;
static pthread_t hb_thread;
struct node *mainconf;
uint32_t our_nodeid;
static void print_usage(void)
{
printf("Usage:\n\n");
printf(PACKAGE " [options]\n\n");
printf("Options:\n\n");
printf(" -c <file> Use config file (default "CONFFILE")\n");
printf(" -f Do not fork in background\n");
printf(" -d Enable debugging output\n");
printf(" -h This help\n");
printf(" -V Print program version information\n");
return;
}
static void read_arguments(int argc, char **argv)
{
int cont = 1;
int optchar;
while (cont) {
optchar = getopt(argc, argv, OPTION_STRING);
switch (optchar) {
case 'c':
conffile = strdup(optarg);
break;
case 'd':
debug = 1;
break;
case 'f':
daemonize = 0;
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
break;
case 'V':
printf(PACKAGE " " PACKAGE_VERSION " (built " __DATE__
" " __TIME__ ")\n");
exit(EXIT_SUCCESS);
break;
case EOF:
cont = 0;
break;
default:
fprintf(stderr, "unknown option: %c\n", optchar);
print_usage();
exit(EXIT_FAILURE);
break;
}
}
}
static void set_oom_adj(int val)
{
FILE *fp;
fp = fopen("/proc/self/oom_adj", "w");
if (!fp)
return;
fprintf(fp, "%i", val);
fclose(fp);
}
static void set_scheduler(void)
{
struct sched_param sched_param;
int err;
err = sched_get_priority_max(SCHED_RR);
if (err != -1) {
sched_param.sched_priority = err;
err = sched_setscheduler(0, SCHED_RR, &sched_param);
if (err == -1)
log_printf(LOGSYS_LEVEL_WARNING,
"could not set SCHED_RR priority %d err %d",
sched_param.sched_priority, errno);
} else {
log_printf(LOGSYS_LEVEL_WARNING,
"could not get maximum scheduler priority err %d",
errno);
}
}
static void remove_lockfile(void)
{
unlink(LOCKFILE_NAME);
}
static int create_lockfile(const char *lockfile)
{
int fd, value;
size_t bufferlen;
ssize_t write_out;
struct flock lock;
char buffer[50];
if ((fd = open(lockfile, O_CREAT | O_WRONLY,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) < 0) {
fprintf(stderr, "Cannot open lockfile [%s], error was [%s]\n",
lockfile, strerror(errno));
return -1;
}
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
retry_fcntl:
if (fcntl(fd, F_SETLK, &lock) < 0) {
switch (errno) {
case EINTR:
goto retry_fcntl;
break;
case EACCES:
case EAGAIN:
fprintf(stderr, "Cannot lock lockfile [%s], error was [%s]\n",
lockfile, strerror(errno));
break;
default:
fprintf(stderr, "process is already running\n");
}
goto fail_close;
}
if (ftruncate(fd, 0) < 0) {
fprintf(stderr, "Cannot truncate pidfile [%s], error was [%s]\n",
lockfile, strerror(errno));
goto fail_close_unlink;
}
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer)-1, "%u\n", getpid());
bufferlen = strlen(buffer);
write_out = write(fd, buffer, bufferlen);
if ((write_out < 0) || (write_out == 0 && errno)) {
fprintf(stderr, "Cannot write pid to pidfile [%s], error was [%s]\n",
lockfile, strerror(errno));
goto fail_close_unlink;
}
if ((write_out == 0) || (write_out < bufferlen)) {
fprintf(stderr, "Cannot write pid to pidfile [%s], shortwrite of"
"[%zu] bytes, expected [%zu]\n",
lockfile, write_out, bufferlen);
goto fail_close_unlink;
}
if ((value = fcntl(fd, F_GETFD, 0)) < 0) {
fprintf(stderr, "Cannot get close-on-exec flag from pidfile [%s], "
"error was [%s]\n", lockfile, strerror(errno));
goto fail_close_unlink;
}
value |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, value) < 0) {
fprintf(stderr, "Cannot set close-on-exec flag from pidfile [%s], "
"error was [%s]\n", lockfile, strerror(errno));
goto fail_close_unlink;
}
return 0;
fail_close_unlink:
if (unlink(lockfile))
fprintf(stderr, "Unable to unlink %s\n", lockfile);
fail_close:
if (close(fd))
fprintf(stderr, "Unable to close %s file descriptor\n", lockfile);
return -1;
}
static void sigterm_handler(int sig)
{
daemon_quit = 1;
}
static void sigpipe_handler(int sig)
{
return;
}
static void dispatch_buffer(struct node *next, uint32_t nodeid, struct knet_header *read_buf, ssize_t read_len)
{
while (next) {
struct conn *conn;
if ((nodeid) && (next->nodeid != nodeid)) {
log_printf(LOGSYS_LEVEL_DEBUG, "Requested nodeid: %u current: %u\n", nodeid, next->nodeid);
goto next;
}
conn = next->conn;
while (conn) {
if (conn->fd) {
if (do_write(conn->fd, read_buf, read_len) < 0) {
/* need to handle errors properly */
log_printf(LOGSYS_LEVEL_DEBUG, "Unable to dispatch buf: %s\n", strerror(errno));
}
}
conn = conn->next;
}
next:
next = next->next;
}
}
static void *heartbeat_thread(void *arg)
{
struct knet_header knet_h;
memset(&knet_h, 0, sizeof(struct knet_header));
knet_h.magic = KNETD_MAGIC;
knet_h.src_nodeid = our_nodeid;
knet_h.seq_num = 0;
knet_h.pckt_type = KNETD_PKCT_TYPE_PING;
knet_h.compress = KNETD_COMPRESS_OFF;
knet_h.encryption = KNETD_ENCRYPTION_OFF;
for (;;) {
sleep(100);
knet_h.seq_num++;
//dispatch_buffer(mainconf, 1, (char *)&knet_h, sizeof(struct knet_header));
}
return NULL;
}
#define IEEE_802_3_MAX_LEN 1500
#define ETHERTYPE_UNK 0x0000
static void decode_pckt(struct knet_header *buf)
{
struct ether_header *ether = (struct ether_header *)buf;
char *data = (char *)buf;
uint16_t ether_type;
uint16_t lenght;
struct ether_addr *src_mac = (struct ether_addr *)ether->ether_shost;
struct ether_addr *dst_mac = (struct ether_addr *)ether->ether_dhost;
ether_type = swap16(ether->ether_type);
log_printf(LOGSYS_LEVEL_DEBUG, "START DECODING\n");
log_printf(LOGSYS_LEVEL_DEBUG, "ether type: %x\n", ether_type);
if (ether_type <= IEEE_802_3_MAX_LEN && ether_type != ETHERTYPE_UNK) {
lenght = ether_type;
if (data[14] == 0xff && data[15] == 0xff)
log_printf(LOGSYS_LEVEL_DEBUG, "ETHERNET_802_3 ?\n");
else
log_printf(LOGSYS_LEVEL_DEBUG, "ETHERNET_802_2 ?\n");
} else {
log_printf(LOGSYS_LEVEL_DEBUG, "ETHERNET_II \n");
}
switch(ether_type) {
case ETHERTYPE_IP:
log_printf(LOGSYS_LEVEL_DEBUG, "ether type: IP\n");
break;
case ETHERTYPE_ARP:
log_printf(LOGSYS_LEVEL_DEBUG, "ether type: ARP\n");
break;
case ETHERTYPE_IPV6:
log_printf(LOGSYS_LEVEL_DEBUG, "ether type: IP6\n");
break;
default:
log_printf(LOGSYS_LEVEL_DEBUG, "ether type: unknown\n");
break;
}
log_printf(LOGSYS_LEVEL_DEBUG, "src mac: %s ", ether_ntoa(src_mac));
log_printf(LOGSYS_LEVEL_DEBUG, "dst mac: %s\n", ether_ntoa(dst_mac));
log_printf(LOGSYS_LEVEL_DEBUG, "END DECODING\n");
}
static void *eth_to_knet_thread(void *arg)
{
fd_set rfds;
int se_result;
ssize_t read_len = 0;
struct timeval tv;
struct knet_header *knet_h = alloca(TX_KNET_SIZE);
uint32_t dst_nodeid = 0;
/* we need to prepare the header only once for now */
memset(knet_h, 0, sizeof(struct knet_header));
knet_h->magic = KNETD_MAGIC;
knet_h->src_nodeid = our_nodeid;
knet_h->seq_num = 0;
knet_h->pckt_type = KNETD_PKCT_TYPE_DATA;
knet_h->compress = KNETD_COMPRESS_OFF;
knet_h->encryption = KNETD_ENCRYPTION_OFF;
do {
FD_ZERO (&rfds);
FD_SET (eth_fd, &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
se_result = select((eth_fd + 1), &rfds, 0, 0, &tv);
if (se_result == -1) {
log_printf(LOGSYS_LEVEL_CRIT, "Unable to select in eth thread: %s\n", strerror(errno));
daemon_quit = 1;
}
if (se_result == 0)
continue;
if (FD_ISSET(eth_fd, &rfds)) {
read_len = read(eth_fd, knet_h + 1, TX_KNET_DATASIZE);
if (read_len > 0) {
- // dst_nodeid = packet_to_nodeid(knet_h + 1);
+ // dst_nodeid = knet_hwtoid(knet_h + 1);
decode_pckt(knet_h + 1);
knet_h->seq_num++;
dispatch_buffer(mainconf, dst_nodeid, knet_h, read_len + sizeof(struct knet_header));
} else if (read_len < 0) {
log_printf(LOGSYS_LEVEL_INFO, "Error reading from localnet error: %s\n", strerror(errno));
} else
log_printf(LOGSYS_LEVEL_DEBUG, "Read 0?\n");
}
} while (se_result >= 0 && !daemon_quit);
return NULL;
}
/* unused for now, just a test */
/*
static void knet_send_synack(struct node *next, uint32_t nodeid, uint32_t type)
{
struct knet_header knet_h;
memset(&knet_h, 0, sizeof(struct knet_header));
knet_h.magic = KNETD_MAGIC;
knet_h.src_nodeid = our_nodeid;
knet_h.seq_num = tx_knet_h->seq_num;
knet_h.pckt_type = (type == KNETD_PKCT_TYPE_SYN) ? KNETD_PKCT_TYPE_SYN : KNETD_PKCT_TYPE_ACK;
knet_h.compress = KNETD_COMPRESS_OFF;
knet_h.encryption = KNETD_ENCRYPTION_OFF;
dispatch_buffer(next, nodeid, &knet_h, sizeof(struct knet_header));
}
*/
static void loop(void) {
int se_result;
fd_set rfds;
struct timeval tv;
ssize_t read_len = 0;
int rv;
uint32_t peer_nodeid;
struct knet_header *knet_h = alloca(TX_KNET_SIZE);
struct node *peer;
do {
FD_ZERO (&rfds);
FD_SET (net_sock, &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
se_result = select((net_sock + 1), &rfds, 0, 0, &tv);
if (daemon_quit)
goto out;
if (se_result == -1) {
log_printf(LOGSYS_LEVEL_CRIT, "Unable to select: %s\n", strerror(errno));
goto out;
}
if (se_result == 0)
continue;
if (FD_ISSET(net_sock, &rfds)) {
read_len = read(net_sock, knet_h, TX_KNET_SIZE);
if (read_len > 0) {
//log_printf(LOGSYS_LEVEL_DEBUG, "Magic: %u\nnodeid: %u\nseq_num: %u\npckt_type: %i\ncompress: %i\nencryption: %i\npadding: %i\n", knet_h->magic, knet_h->nodeid, knet_h->seq_num, knet_h->pckt_type, knet_h->compress, knet_h->encryption, knet_h->padding);
if (knet_h->magic != KNETD_MAGIC) {
log_printf(LOGSYS_LEVEL_DEBUG, "no magic? print peer info for fun and profit\n");
continue;
}
if (knet_h->src_nodeid == our_nodeid) {
log_printf(LOGSYS_LEVEL_DEBUG, "Are we really sending pckts to our selves?\n");
continue;
}
/* optimize this to do faster lookups */
peer = mainconf;
while (peer) {
if (peer->nodeid == knet_h->src_nodeid)
break;
peer = peer->next;
}
switch(knet_h->pckt_type) {
/*
case KNETD_PKCT_TYPE_SYN:
knet_send_synack(mainconf, peer->nodeid, KNETD_PKCT_TYPE_ACK);
case KNETD_PKCT_TYPE_ACK:
log_printf(LOGSYS_LEVEL_DEBUG, "syn/ack request from: %u\n", knet_h->src_nodeid);
peer->seq_num = knet_h->seq_num;
peer->status = NODE_STATUS_ONLINE;
memset(peer->circular_buffer, 0, CBUFFER_SIZE);
break;
*/
case KNETD_PKCT_TYPE_DATA:
if (should_deliver(peer, knet_h->seq_num) > 0) {
//log_printf(LOGSYS_LEVEL_DEBUG, "Act pkct from node %s[%u]: %u\n", peer->nodename, peer->nodeid, knet_h->seq_num);
rv = do_write(eth_fd, knet_h + 1, read_len - sizeof(struct knet_header));
if (rv < 0)
log_printf(LOGSYS_LEVEL_INFO, "Error writing to eth_fd: %s\n", strerror(errno));
else
has_been_delivered(peer, knet_h->seq_num);
} //else
// log_printf(LOGSYS_LEVEL_DEBUG, "Discarding duplicated package from node %s[%u]: %u\n", peer->nodename, peer->nodeid, knet_h->seq_num);
break;
case KNETD_PKCT_TYPE_PING:
log_printf(LOGSYS_LEVEL_DEBUG, "Got a PING request %u\n", knet_h->src_nodeid);
peer_nodeid = knet_h->src_nodeid;
/* reply */
knet_h->pckt_type = KNETD_PKCT_TYPE_PONG;
knet_h->src_nodeid = our_nodeid;
dispatch_buffer(mainconf, peer_nodeid, knet_h, read_len);
break;
case KNETD_PKCT_TYPE_PONG:
log_printf(LOGSYS_LEVEL_DEBUG, "Got a PONG reply\n");
/* need to correlate this with a PING */
break;
default:
log_printf(LOGSYS_LEVEL_INFO, "Error: received unknown packet type on network socket\n");
break;
}
} else if (read_len < 0) {
log_printf(LOGSYS_LEVEL_INFO, "Error reading from KNET error %d: %s\n", net_sock, strerror(errno));
} else
log_printf(LOGSYS_LEVEL_DEBUG, "Read 0?\n");
}
out:
if (se_result <0 || daemon_quit)
log_printf(LOGSYS_LEVEL_DEBUG, "End of mail loop\n");
} while (se_result >= 0 && !daemon_quit);
}
int main(int argc, char **argv)
{
confdb_handle_t confdb_handle = 0;
int rv;
int eth_thread_started = 1, hb_thread_started = 1;
if (create_lockfile(LOCKFILE_NAME) < 0)
exit(EXIT_FAILURE);
atexit(remove_lockfile);
read_arguments(argc, argv);
strncpy(localnet, DEFAULT_NET_NAME, sizeof(DEFAULT_NET_NAME));
if (!conffile)
conffile = strdup(CONFFILE);
confdb_handle = readconf(conffile);
if (confdb_handle == 0)
exit(EXIT_FAILURE);
if (configure_logging(confdb_handle) < 0) {
fprintf(stderr, "Unable to initialize logging subsystem\n");
exit(EXIT_FAILURE);
}
log_printf(LOGSYS_LEVEL_INFO, PACKAGE " version " VERSION "\n");
if (daemonize) {
if (daemon(0, 0) < 0) {
perror("Unable to daemonize");
exit(EXIT_FAILURE);
}
}
signal(SIGTERM, sigterm_handler);
signal(SIGPIPE, sigpipe_handler);
parse_global_config(confdb_handle);
mainconf = parse_nodes_config(confdb_handle);
if (process_local_node_config_preup(mainconf, localnet) != 0) {
log_printf(LOGSYS_LEVEL_INFO, "Unable to process local node config\n");
goto out;
}
if (statistics)
log_printf(LOGSYS_LEVEL_DEBUG, "statistics collector enabled\n");
if (rerouting)
log_printf(LOGSYS_LEVEL_DEBUG, "rerouting engine enabled\n");
log_printf(LOGSYS_LEVEL_DEBUG, "Adjust OOM to -16\n");
set_oom_adj(-16);
log_printf(LOGSYS_LEVEL_DEBUG, "Set RR scheduler\n");
set_scheduler();
/* do stuff here, should we */
log_printf(LOGSYS_LEVEL_DEBUG, "Starting daemon control thread\n");
if (start_control_thread() < 0)
goto out;
log_printf(LOGSYS_LEVEL_DEBUG, "Initializing local ethernet\n");
eth_fd = knet_open(localnet, 16);
if (eth_fd < 0) {
log_printf(LOGSYS_LEVEL_INFO, "Unable to inizialize local tap device: %s\n",
strerror(errno));
goto out;
}
log_printf(LOGSYS_LEVEL_INFO, "Using local net device %s\n", localnet);
log_printf(LOGSYS_LEVEL_DEBUG, "Setting %s device mac address\n", localnet);
knet_set_hwid(localnet, our_nodeid);
if (process_local_node_config_postup(mainconf, localnet) != 0) {
log_printf(LOGSYS_LEVEL_INFO, "Unable to process post up config\n");
goto out;
}
log_printf(LOGSYS_LEVEL_DEBUG, "Initializing local ethernet delivery thread\n");
rv = pthread_create(ð_thread, NULL, eth_to_knet_thread, NULL);
if (rv < 0) {
eth_thread_started = 0;
log_printf(LOGSYS_LEVEL_INFO, "Unable to inizialize local RX thread. error: %s\n",
strerror(errno));
goto out;
}
log_printf(LOGSYS_LEVEL_DEBUG, "Opening sockets to other nodes\n");
connect_to_nodes(mainconf);
log_printf(LOGSYS_LEVEL_DEBUG, "Here we need to configure the ethernet ip/pre/post/stuff\n");
log_printf(LOGSYS_LEVEL_DEBUG, "Starting network socket listener\n");
net_sock = setup_net_listener();
if (net_sock < 0)
goto out;
log_printf(LOGSYS_LEVEL_DEBUG, "Starting heartbeat thread\n");
rv = pthread_create(&hb_thread, NULL, heartbeat_thread, NULL);
if (rv < 0) {
hb_thread_started = 0;
log_printf(LOGSYS_LEVEL_INFO, "Unable to inizialize heartbeat thread. error: %s\n",
strerror(errno));
goto out;
}
/*
log_printf(LOGSYS_LEVEL_DEBUG, "Sending syn packet\n");
knet_send_synack(mainconf, 0, KNETD_PKCT_TYPE_SYN);
*/
log_printf(LOGSYS_LEVEL_DEBUG, "Entering main loop\n");
loop();
out:
if (hb_thread_started > 0)
pthread_cancel(hb_thread);
if (eth_thread_started > 0)
pthread_cancel(eth_thread);
disconnect_from_nodes(mainconf);
if (eth_fd >= 0)
close(eth_fd);
if (net_sock >= 0)
close(net_sock);
stop_control_thread();
free_nodes_config(mainconf);
free(conffile);
freeconf(confdb_handle);
close_logging();
return 0;
}
diff --git a/packet.c b/packet.c
deleted file mode 100644
index f4e1a0a9..00000000
--- a/packet.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include "config.h"
-
-#include <stdio.h>
-#include <arpa/inet.h>
-
-#include "packet.h"
-#include "netsocket.h"
-
-
-static uint8_t ipv4_bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-static uint8_t ipv6_bcast[] = { 0x33, 0x33 };
-
-
-inline uint32_t packet_to_nodeid(void *packet, struct node *node)
-{
- struct ether_header *eth_h = packet;
-
- if (memcmp(ipv4_bcast, eth_h->ether_dhost, sizeof(ipv4_bcast)) == 0)
- return 0;
-
- if (memcmp(ipv6_bcast, eth_h->ether_dhost, sizeof(ipv6_bcast)) == 0)
- return 0;
-
- while (node) {
- if (memcmp(node->hwaddress, eth_h->ether_dhost, ETH_ALEN) == 0)
- return node->nodeid;
- node = node->next;
- }
-
- return 0;
-}
-
diff --git a/packet.h b/packet.h
deleted file mode 100644
index 2e6fa266..00000000
--- a/packet.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef __PACKET_H__
-#define __PACKET_H__
-
-#include "nodes.h"
-
-uint32_t packet_to_nodeid(void *packet, struct node *node);
-
-#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 28287839..91fbf696 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,12 +1,17 @@
+MAINTAINERCLEANFILES = Makefile.in
+
VALGRIND = valgrind -q --error-exitcode=127 --leak-check=full
check-valgrind: $(check_PROGRAMS)
$(MAKE) TESTS_ENVIRONMENT="$(VALGRIND)" check
check_PROGRAMS = check_packet
TESTS = $(check_PROGRAMS)
check_packet_SOURCES = \
check_packet.c \
- ../packet.c
+ ../knet.c \
+ ../utils.c
+check_packet_LDFLAGS = \
+ $(logsys_LIBS)
diff --git a/tests/check_packet.c b/tests/check_packet.c
index a6b19f2b..dcf64a10 100644
--- a/tests/check_packet.c
+++ b/tests/check_packet.c
@@ -1,60 +1,59 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include <error.h>
-#include "nodes.h"
-#include "packet.h"
+#include "knet.h"
char eth_frame[] = {
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* ether_dhost */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ether_dhost */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ether_shost */
0x08, 0x00, /* ether_type */
};
char bcast4_mac[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ether_dhost */
};
char bcast6_mac[] = {
0x33, 0x33, /* ether_dhost */
};
int main(int argc, char *argv[])
{
uint32_t nid;
- struct node n;
- memset(&n, 0, sizeof(struct node));
- n.nodeid = 1;
- memmove(n.hwaddress, eth_frame, sizeof(n.hwaddress));
+ memmove(eth_frame, knet_hwvend, sizeof(knet_hwvend));
- nid = packet_to_nodeid(eth_frame, &n);
- if (nid != n.nodeid)
+ eth_frame[5] = 0x01;
+
+ nid = knet_hwtoid(eth_frame);
+ if (nid != 0x01)
error(EXIT_FAILURE, -EINVAL, "eth_frame1 failed");
- memset(eth_frame, 0x00, ETH_ALEN);
+ eth_frame[5] = 0x02;
- nid = packet_to_nodeid(eth_frame, &n);
- if (nid != 0)
+ nid = knet_hwtoid(eth_frame);
+ if (nid != 0x02)
error(EXIT_FAILURE, -EINVAL, "eth_frame2 failed");
memmove(eth_frame, bcast4_mac, sizeof(bcast4_mac));
- nid = packet_to_nodeid(eth_frame, &n);
+ nid = knet_hwtoid(eth_frame);
if (nid != 0)
error(EXIT_FAILURE, -EINVAL, "bcast4_mac failed");
memmove(eth_frame, bcast6_mac, sizeof(bcast6_mac));
- nid = packet_to_nodeid(eth_frame, &n);
+ nid = knet_hwtoid(eth_frame);
if (nid != 0)
error(EXIT_FAILURE, -EINVAL, "bcast6_mac failed");
return 0;
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Sep 5, 9:29 AM (12 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2308692
Default Alt Text
(25 KB)
Attached To
Mode
rK kronosnet
Attached
Detach File
Event Timeline
Log In to Comment