From ce401448d62f65956ed57b077af2b315f59efe94 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Thu, 28 Jul 2016 21:15:17 +0200 Subject: lib: Portability to FreeBSD Various portability fixes for FreeBSD. POSIX requires shm file names to start with a "/" to be portable. lseek(2) can be undefined on POSIX shm, replaced with ftruncate(2). IRMd check on existing lockfile more portable. FreeBSD 11.0 is preferred as it natively supports robust mutexes. Full working LLC implementation pending. --- include/ouroboros/config.h.in | 10 +++-- src/ipcpd/shim-eth-llc/main.c | 79 +++++++++++++++++++++++++++++++++++--- src/irmd/main.c | 46 +++++++++++----------- src/lib/lockfile.c | 4 +- src/lib/shm_ap_rbuff.c | 2 +- src/lib/shm_du_map.c | 4 +- src/tools/cbr/cbr_server.c | 5 +++ src/tools/irm/irm_ipcp_bootstrap.c | 4 +- 8 files changed, 117 insertions(+), 37 deletions(-) diff --git a/include/ouroboros/config.h.in b/include/ouroboros/config.h.in index 7bb4cee0..3a6dd3c8 100644 --- a/include/ouroboros/config.h.in +++ b/include/ouroboros/config.h.in @@ -27,6 +27,10 @@ #define PROJECT_VERSION "@PACKAGE_VERSION@" #define INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" #define _POSIX_C_SOURCE 200809L +#define __XSI_VISIBLE 500 +#ifdef __FreeBSD__ +#define __BSD_VISIBLE 1 +#endif #define IPCP_SHIM_UDP_EXEC "@IPCP_SHIM_UDP_TARGET@" #define IPCP_SHIM_ETH_LLC_EXEC "@IPCP_SHIM_ETH_LLC_TARGET@" #define IPCP_NORMAL_EXEC "@IPCP_NORMAL_TARGET@" @@ -34,13 +38,13 @@ #define AP_MAX_FLOWS 256 #define SHM_DU_BUFF_BLOCK_SIZE sysconf(_SC_PAGESIZE) #define SHM_DU_MAP_MULTI_BLOCK -#define SHM_DU_MAP_FILENAME "ouroboros.shm" -#define LOCKFILE_NAME "ouroboros.lockfile" +#define SHM_DU_MAP_FILENAME "/ouroboros.shm" +#define LOCKFILE_NAME "/ouroboros.lockfile" #define SHM_BLOCKS_IN_MAP (1 << 14) #define SHM_DU_TIMEOUT_MICROS 15000 #define DU_BUFF_HEADSPACE 128 #define DU_BUFF_TAILSPACE 0 -#define SHM_AP_RBUFF_PREFIX "ouroboros.rbuff." +#define SHM_AP_RBUFF_PREFIX "/ouroboros.rbuff." #define SHM_RBUFF_SIZE (1 << 14) #define IRMD_MAX_FLOWS 4096 #define IRMD_THREADPOOL_SIZE 3 diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index c2309272..3fd2188a 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -53,8 +53,17 @@ #include #include #include + +#ifdef __linux__ #include #include +#endif + +#ifdef __FreeBSD__ +#include +#include +#endif + #include #include @@ -89,7 +98,11 @@ struct eth_llc_ipcp_data { /* Keep ipcp_data first for polymorphism. */ struct ipcp_data ipcp_data; +#ifdef __FreeBSD__ + struct sockaddr_dl device; +#else struct sockaddr_ll device; +#endif int s_fd; struct bmp * indices; @@ -285,7 +298,11 @@ static int eth_llc_ipcp_send_frame(uint8_t dst_addr[MAC_SIZE], uint8_t * frame; #else uint8_t frame[SHIM_ETH_LLC_MAX_SDU_SIZE]; - struct sockaddr_ll device; +#ifdef __FreeBSD__ + struct sockaddr_dl device; +#else + struct sockaddr_ll device; +#endif #endif if (payload == NULL) { @@ -321,7 +338,11 @@ static int eth_llc_ipcp_send_frame(uint8_t dst_addr[MAC_SIZE], memcpy(frame, dst_addr, MAC_SIZE * sizeof(uint8_t)); frame_len += MAC_SIZE; memcpy(frame + frame_len, +#ifdef __FreeBSD__ + LLADDR(&shim_data(_ipcp)->device), +#else shim_data(_ipcp)->device.sll_addr, +#endif MAC_SIZE * sizeof(uint8_t)); frame_len += MAC_SIZE; memcpy(frame + frame_len, &length, 2 * sizeof(uint8_t)); @@ -671,8 +692,11 @@ static void * eth_llc_ipcp_sdu_reader(void * o) #endif for (i = 0; i < MAC_SIZE; i++) dst_mac[i] = buf[i]; - +#ifdef __FreeBSD__ + if (memcmp(LLADDR(&shim_data(_ipcp)->device), +#else if (memcmp(shim_data(_ipcp)->device.sll_addr, +#endif dst_mac, MAC_SIZE) && memcmp(br_addr, dst_mac, MAC_SIZE)) { @@ -868,7 +892,13 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) int fd = -1; struct ifreq ifr; int index; +#ifdef __FreeBSD__ + struct ifaddrs * ifaddr; + struct ifaddrs * ifa; + struct sockaddr_dl device; +#else struct sockaddr_ll device; +#endif #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) struct tpacket_req req; @@ -897,21 +927,58 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) memcpy(ifr.ifr_name, conf->if_name, strlen(conf->if_name)); +#ifdef __FreeBSD__ + if (getifaddrs(&ifaddr) < 0) { + close(fd); + LOG_ERR("Could not get interfaces."); + return -1; + } + + for (ifa = ifaddr, index = 0; ifa != NULL; ifa = ifa->ifa_next, ++index) { + if (strcmp(ifa->ifa_name, conf->if_name)) + continue; + LOG_DBGF("Interface %s found.", conf->if_name); + + memcpy(&ifr.ifr_addr, ifa->ifa_addr, sizeof(*ifa->ifa_addr)); + break; + } + + if (ifa == NULL) { + LOG_ERR("Interface not found."); + freeifaddrs(ifaddr); + return -1; + } + + freeifaddrs(ifaddr); + +#else if (ioctl(fd, SIOCGIFHWADDR, &ifr)) { close(fd); LOG_ERR("Failed to ioctl: %s.", strerror(errno)); return -1; } - close(fd); - index = if_nametoindex(conf->if_name); if (index == 0) { LOG_ERR("Failed to retrieve interface index."); return -1; } +#endif + + close(fd); memset(&(device), 0, sizeof(device)); +#ifdef __FreeBSD__ + device.sdl_index = index; + device.sdl_family = AF_LINK; + memcpy(LLADDR(&device), + ifr.ifr_addr.sa_data, + MAC_SIZE * sizeof (uint8_t)); + device.sdl_alen = MAC_SIZE; + /* TODO: replace socket calls with bpf for BSD */ + LOG_MISSING; + fd = socket(AF_LINK, SOCK_RAW, 0); +#else device.sll_ifindex = index; device.sll_family = AF_PACKET; memcpy(device.sll_addr, @@ -921,6 +988,7 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) device.sll_protocol = htons(ETH_P_ALL); fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_802_2)); +#endif if (fd < 0) { LOG_ERR("Failed to create socket: %s.", strerror(errno)); return -1; @@ -953,8 +1021,7 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) } #endif - if (bind(fd,(struct sockaddr *) &device, - sizeof(struct sockaddr_ll))) { + if (bind(fd,(struct sockaddr *) &device, sizeof(device))) { LOG_ERR("Failed to bind socket to interface"); close(fd); return -1; diff --git a/src/irmd/main.c b/src/irmd/main.c index b071ff1c..1c731788 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1679,28 +1679,6 @@ static struct irm * irm_create() irmd->state = IRMD_NULL; - if (access("/dev/shm/" LOCKFILE_NAME, F_OK) != -1) { - struct lockfile * lf = lockfile_open(); - if (lf == NULL) { - LOG_ERR("Failed to open existing lockfile."); - free(irmd); - return NULL; - } - - if (kill(lockfile_owner(lf), 0) < 0) { - LOG_INFO("IRMd didn't properly shut down last time."); - shm_du_map_destroy(shm_du_map_open()); - LOG_INFO("Stale resources cleaned"); - lockfile_destroy(lf); - } else { - LOG_INFO("IRMd already running (%d), exiting.", - lockfile_owner(lf)); - lockfile_close(lf); - free(irmd); - return NULL; - } - } - if (pthread_rwlock_init(&irmd->state_lock, NULL)) { LOG_ERR("Failed to initialize rwlock."); free(irmd); @@ -1757,6 +1735,28 @@ static struct irm * irm_create() } if ((irmd->lf = lockfile_create()) == NULL) { + if ((irmd->lf = lockfile_open()) == NULL) { + LOG_ERR("Lockfile error."); + irm_destroy(); + return NULL; + } + + if (kill(lockfile_owner(irmd->lf), 0) < 0) { + LOG_INFO("IRMd didn't properly shut down last time."); + shm_du_map_destroy(shm_du_map_open()); + LOG_INFO("Stale resources cleaned"); + lockfile_destroy(irmd->lf); + irmd->lf = lockfile_create(); + } else { + LOG_INFO("IRMd already running (%d), exiting.", + lockfile_owner(irmd->lf)); + lockfile_close(irmd->lf); + free(irmd); + return NULL; + } + } + + if (irmd->lf == NULL) { irm_destroy(); return NULL; } @@ -1768,6 +1768,8 @@ static struct irm * irm_create() irmd->state = IRMD_RUNNING; + LOG_INFO("IRMd started..."); + return irmd; } diff --git a/src/lib/lockfile.c b/src/lib/lockfile.c index a93d5f17..75ee2085 100644 --- a/src/lib/lockfile.c +++ b/src/lib/lockfile.c @@ -60,7 +60,7 @@ struct lockfile * lockfile_create() { return NULL; } - if (lseek(lf->fd, LF_SIZE - 1, SEEK_SET) < 0) { + if (ftruncate(lf->fd, LF_SIZE - 1) < 0) { LOG_DBGF("Failed to extend lockfile."); free(lf); return NULL; @@ -99,7 +99,7 @@ struct lockfile * lockfile_open() { return NULL; lf->fd = shm_open(LOCKFILE_NAME, O_RDWR, 0666); - if (lf->fd == -1) { + if (lf->fd < 0) { LOG_DBGF("Could not open lock file."); free(lf); return NULL; diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c index 618c4c19..605e5678 100644 --- a/src/lib/shm_ap_rbuff.c +++ b/src/lib/shm_ap_rbuff.c @@ -90,7 +90,7 @@ struct shm_ap_rbuff * shm_ap_rbuff_create() return NULL; } - if (lseek(shm_fd, SHM_RBUFF_FILE_SIZE - 1, SEEK_SET) < 0) { + if (ftruncate(shm_fd, SHM_RBUFF_FILE_SIZE - 1) < 0) { LOG_DBGF("Failed to extend ringbuffer."); free(rb); return NULL; diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 9d2d4f6e..6dc4a1bd 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -161,7 +161,7 @@ struct shm_du_map * shm_du_map_create() return NULL; } - if (lseek(shm_fd, SHM_FILE_SIZE - 1, SEEK_SET) < 0) { + if (ftruncate(shm_fd, SHM_FILE_SIZE - 1) < 0) { LOG_DBGF("Failed to extend shared memory map."); free(dum); return NULL; @@ -235,7 +235,7 @@ struct shm_du_map * shm_du_map_open() } shm_fd = shm_open(SHM_DU_MAP_FILENAME, O_RDWR, 0666); - if (shm_fd == -1) { + if (shm_fd < 0) { LOG_DBGF("Failed opening shared memory."); return NULL; } diff --git a/src/tools/cbr/cbr_server.c b/src/tools/cbr/cbr_server.c index 3a1d8d5c..9c6ac7f1 100644 --- a/src/tools/cbr/cbr_server.c +++ b/src/tools/cbr/cbr_server.c @@ -22,6 +22,11 @@ */ #include + +#ifdef __FreeBSD__ +#define __XSI_VISIBLE +#endif + #include #include #include diff --git a/src/tools/irm/irm_ipcp_bootstrap.c b/src/tools/irm/irm_ipcp_bootstrap.c index c2b696e4..94b7f81e 100644 --- a/src/tools/irm/irm_ipcp_bootstrap.c +++ b/src/tools/irm/irm_ipcp_bootstrap.c @@ -24,7 +24,9 @@ #include #include #include - +#ifdef __FreeBSD__ +#include +#endif #include #include -- cgit v1.2.3