From dbbea7cad34e7ef9f24cd9f30dce6fd2aa4d6cf3 Mon Sep 17 00:00:00 2001 From: whl739 Date: Sun, 10 Jun 2018 20:37:34 +0800 Subject: [PATCH] Misc: some updates. 1.add interface `ff_dup` and `ff_dup2`. 2.add interface `ff_ioctl_freebsd`/`ff_getsockopt_freebsd`/`ff_setsockopt_freebsd` for tools. 3.initial parameter `proc-type` can be NULL, default "auto". --- .gitignore | 2 + lib/ff_api.h | 10 +++ lib/ff_api.symlist | 5 ++ lib/ff_config.c | 10 ++- lib/ff_dpdk_if.c | 6 +- lib/ff_syscall_wrapper.c | 176 ++++++++++++++++++++++++++++++++++++--- 6 files changed, 193 insertions(+), 16 deletions(-) mode change 100644 => 100755 lib/ff_api.symlist diff --git a/.gitignore b/.gitignore index 81a6553e..4474d47b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ lib/machine_include/ lib/vnode_if.h lib/vnode_if_newproto.h lib/vnode_if_typedef.h +app/nginx-1.11.10/Makefile +app/nginx-1.11.10/objs/ diff --git a/lib/ff_api.h b/lib/ff_api.h index bbd44cbb..9f518bf1 100644 --- a/lib/ff_api.h +++ b/lib/ff_api.h @@ -109,6 +109,9 @@ int ff_kevent_do_each(int kq, const struct kevent *changelist, int nchanges, int ff_gettimeofday(struct timeval *tv, struct timezone *tz); +int ff_dup(int oldfd); +int ff_dup2(int oldfd, int newfd); + /* POSIX-LIKE api end */ @@ -172,6 +175,13 @@ void ff_regist_packet_dispatcher(dispatch_func_t func); /* internal api begin */ +/* FreeBSD style calls. Used for tools. */ +int ff_ioctl_freebsd(int fd, unsigned long request, ...); +int ff_setsockopt_freebsd(int s, int level, int optname, + const void *optval, socklen_t optlen); +int ff_getsockopt_freebsd(int s, int level, int optname, + void *optval, socklen_t *optlen); + /* * Handle rtctl. * The data is a pointer to struct rt_msghdr. diff --git a/lib/ff_api.symlist b/lib/ff_api.symlist old mode 100644 new mode 100755 index 6fc14595..4c2558ea --- a/lib/ff_api.symlist +++ b/lib/ff_api.symlist @@ -45,3 +45,8 @@ ff_gettimeofday ff_fdisused ff_getmaxfd ff_ngctl +ff_ioctl_freebsd +ff_getsockopt_freebsd +ff_setsockopt_freebsd +ff_dup +ff_dup2 diff --git a/lib/ff_config.c b/lib/ff_config.c index e7adf1d2..aab8627b 100644 --- a/lib/ff_config.c +++ b/lib/ff_config.c @@ -511,9 +511,13 @@ ff_parse_args(struct ff_config *cfg, int argc, char *const argv[]) } } - if (cfg->dpdk.proc_type == NULL || - (strcmp(cfg->dpdk.proc_type, "primary") && - strcmp(cfg->dpdk.proc_type, "secondary"))) { + if (cfg->dpdk.proc_type == NULL) { + cfg->dpdk.proc_type = strdup("auto"); + } + + if (strcmp(cfg->dpdk.proc_type, "primary") && + strcmp(cfg->dpdk.proc_type, "secondary") && + strcmp(cfg->dpdk.proc_type, "auto")) { printf("invalid proc-type\n"); return -1; } diff --git a/lib/ff_dpdk_if.c b/lib/ff_dpdk_if.c index 4f517e52..ddd41fc7 100644 --- a/lib/ff_dpdk_if.c +++ b/lib/ff_dpdk_if.c @@ -1077,7 +1077,7 @@ handle_ioctl_msg(struct ff_msg *msg) goto done; } - ret = ff_ioctl(fd, msg->ioctl.cmd, msg->ioctl.data); + ret = ff_ioctl_freebsd(fd, msg->ioctl.cmd, msg->ioctl.data); ff_close(fd); @@ -1136,12 +1136,12 @@ handle_ipfw_msg(struct ff_msg *msg) switch (msg->ipfw.cmd) { case FF_IPFW_GET: - ret = ff_getsockopt(fd, msg->ipfw.level, + ret = ff_getsockopt_freebsd(fd, msg->ipfw.level, msg->ipfw.optname, msg->ipfw.optval, msg->ipfw.optlen); break; case FF_IPFW_SET: - ret = ff_setsockopt(fd, msg->ipfw.level, + ret = ff_setsockopt_freebsd(fd, msg->ipfw.level, msg->ipfw.optname, msg->ipfw.optval, *(msg->ipfw.optlen)); break; diff --git a/lib/ff_syscall_wrapper.c b/lib/ff_syscall_wrapper.c index cfc407c1..56c5ce60 100644 --- a/lib/ff_syscall_wrapper.c +++ b/lib/ff_syscall_wrapper.c @@ -52,6 +52,9 @@ #include #include +#include +#include + #include #include "ff_api.h" @@ -140,6 +143,27 @@ #define LINUX_TIOCPKT_DOSTOP 32 #define LINUX_TIOCPKT_IOCTL 64 +#define LINUX_SIOCGIFCONF 0x8912 +#define LINUX_SIOCGIFFLAGS 0x8913 +#define LINUX_SIOCSIFFLAGS 0x8914 +#define LINUX_SIOCGIFADDR 0x8915 +#define LINUX_SIOCSIFADDR 0x8916 +#define LINUX_SIOCGIFDSTADDR 0x8917 +#define LINUX_SIOCSIFDSTADDR 0x8918 +#define LINUX_SIOCGIFBRDADDR 0x8919 +#define LINUX_SIOCSIFBRDADDR 0x891a +#define LINUX_SIOCGIFNETMASK 0x891b +#define LINUX_SIOCSIFNETMASK 0x891c +#define LINUX_SIOCGIFMETRIC 0x891d +#define LINUX_SIOCSIFMETRIC 0x891e +#define LINUX_SIOCGIFMTU 0x8921 +#define LINUX_SIOCSIFMTU 0x8922 +#define LINUX_SIOCSIFNAME 0x8923 +#define LINUX_SIOCADDMULTI 0x8931 +#define LINUX_SIOCDELMULTI 0x8932 +#define LINUX_SIOCGIFINDEX 0x8933 +#define LINUX_SIOCDIFADDR 0x8936 + /* ioctl define end */ @@ -217,8 +241,48 @@ linux2freebsd_ioctl(unsigned long request) return TIOCPKT_DOSTOP; case LINUX_TIOCPKT_IOCTL: return TIOCPKT_IOCTL; + case LINUX_SIOCGIFCONF: + return SIOCGIFCONF; + case LINUX_SIOCGIFFLAGS: + return SIOCGIFFLAGS; + case LINUX_SIOCSIFFLAGS: + return SIOCSIFFLAGS; + case LINUX_SIOCGIFADDR: + return SIOCGIFADDR; + case LINUX_SIOCSIFADDR: + return SIOCSIFADDR; + case LINUX_SIOCGIFDSTADDR: + return SIOCGIFDSTADDR; + case LINUX_SIOCSIFDSTADDR: + return SIOCSIFDSTADDR; + case LINUX_SIOCGIFBRDADDR: + return SIOCGIFBRDADDR; + case LINUX_SIOCSIFBRDADDR: + return SIOCSIFBRDADDR; + case LINUX_SIOCGIFNETMASK: + return SIOCGIFNETMASK; + case LINUX_SIOCSIFNETMASK: + return SIOCSIFNETMASK; + case LINUX_SIOCGIFMETRIC: + return SIOCGIFMETRIC; + case LINUX_SIOCSIFMETRIC: + return SIOCSIFMETRIC; + case LINUX_SIOCGIFMTU: + return SIOCGIFMTU; + case LINUX_SIOCSIFMTU: + return SIOCSIFMTU; + case LINUX_SIOCSIFNAME: + return SIOCSIFNAME; + case LINUX_SIOCADDMULTI: + return SIOCADDMULTI; + case LINUX_SIOCDELMULTI: + return SIOCDELMULTI; + case LINUX_SIOCGIFINDEX: + return SIOCGIFINDEX; + case LINUX_SIOCDIFADDR: + return SIOCDIFADDR; default: - return request; + return -1; } } @@ -259,7 +323,7 @@ so_opt_convert(int optname) case LINUX_SO_PROTOCOL: return SO_PROTOCOL; default: - return optname; + return -1; } } @@ -286,7 +350,7 @@ ip_opt_convert(int optname) case LINUX_IP_DROP_MEMBERSHIP: return IP_DROP_MEMBERSHIP; default: - return optname; + return -1; } } @@ -307,7 +371,7 @@ tcp_opt_convert(int optname) case LINUX_TCP_MD5SIG: return TCP_MD5SIG; default: - return optname; + return -1; } } @@ -322,7 +386,7 @@ linux2freebsd_opt(int level, int optname) case IPPROTO_TCP: return tcp_opt_convert(optname); default: - return optname; + return -1; } } @@ -383,7 +447,24 @@ ff_getsockopt(int s, int level, int optname, void *optval, goto kern_fail; } - if ((rc = kern_getsockopt(curthread, s, level, optname, + if ((rc = kern_getsockopt(curthread, s, level, optname, + optval, UIO_USERSPACE, optlen))) + goto kern_fail; + + return (rc); + +kern_fail: + ff_os_errno(rc); + return (-1); +} + +int +ff_getsockopt_freebsd(int s, int level, int optname, + void *optval, socklen_t *optlen) +{ + int rc; + + if ((rc = kern_getsockopt(curthread, s, level, optname, optval, UIO_USERSPACE, optlen))) goto kern_fail; @@ -409,7 +490,24 @@ ff_setsockopt(int s, int level, int optname, const void *optval, goto kern_fail; } - if ((rc = kern_setsockopt(curthread, s, level, optname, + if ((rc = kern_setsockopt(curthread, s, level, optname, + __DECONST(void *, optval), UIO_USERSPACE, optlen))) + goto kern_fail; + + return (rc); + +kern_fail: + ff_os_errno(rc); + return (-1); +} + +int +ff_setsockopt_freebsd(int s, int level, int optname, + const void *optval, socklen_t optlen) +{ + int rc; + + if ((rc = kern_setsockopt(curthread, s, level, optname, __DECONST(void *, optval), UIO_USERSPACE, optlen))) goto kern_fail; @@ -447,6 +545,27 @@ kern_fail: return (-1); } +int +ff_ioctl_freebsd(int fd, unsigned long request, ...) +{ + int rc; + va_list ap; + caddr_t argp; + + va_start(ap, request); + + argp = va_arg(ap, caddr_t); + va_end(ap); + if ((rc = kern_ioctl(curthread, fd, request, argp))) + goto kern_fail; + + return (rc); + +kern_fail: + ff_os_errno(rc); + return (-1); +} + int ff_close(int fd) { @@ -814,12 +933,12 @@ ff_getpeername(int s, struct linux_sockaddr * name, freebsd2linux_sockaddr(name, pf); if(pf != NULL) - free(pf, M_SONAME); + free(pf, M_SONAME); return (rc); kern_fail: if(pf != NULL) - free(pf, M_SONAME); + free(pf, M_SONAME); ff_os_errno(rc); return (-1); } @@ -843,7 +962,7 @@ ff_getsockname(int s, struct linux_sockaddr *name, kern_fail: if(pf != NULL) - free(pf, M_SONAME); + free(pf, M_SONAME); ff_os_errno(rc); return (-1); } @@ -1037,6 +1156,43 @@ ff_gettimeofday(struct timeval *tv, struct timezone *tz) return 0; } +int +ff_dup(int oldfd) +{ + int rc; + struct dup_args da = { + .fd = oldfd, + }; + if ((rc = sys_dup(curthread, &da))) + goto kern_fail; + + rc = curthread->td_retval[0]; + + return (rc); +kern_fail: + ff_os_errno(rc); + return (-1); +} + +int +ff_dup2(int oldfd, int newfd) +{ + int rc; + struct dup2_args da = { + .from = oldfd, + .to = newfd + }; + if ((rc = sys_dup2(curthread, &da))) + goto kern_fail; + + rc = curthread->td_retval[0]; + + return (rc); +kern_fail: + ff_os_errno(rc); + return (-1); +} + int ff_route_ctl(enum FF_ROUTE_CTL req, enum FF_ROUTE_FLAG flag, struct linux_sockaddr *dst, struct linux_sockaddr *gw,