Skip to content

Commit

Permalink
Merge pull request #28 from mengdemao/feature/socket
Browse files Browse the repository at this point in the history
feat(socket): fix lazybsd lazybsd_socket
  • Loading branch information
mengdemao authored Jun 12, 2024
2 parents d5a43aa + d36caa0 commit 09d2410
Show file tree
Hide file tree
Showing 21 changed files with 1,770 additions and 236 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ target_include_directories(${PROJECT_NAME}
include_directories(${CMAKE_SOURCE_DIR}/include/generated)
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(${CMAKE_SOURCE_DIR}/bsd/lib)

# 添加子文件夹
add_subdirectory(${CMAKE_SOURCE_DIR}/bsd ${CMAKE_BINARY_DIR}/bsd)
Expand Down
10 changes: 8 additions & 2 deletions bsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ set(BSD_SOURCE
${BSD_SOURCE_DIR}/sys/vm/uma_core.c

${CMAKE_CURRENT_SOURCE_DIR}/lib/lazybsd_veth.c
${CMAKE_CURRENT_SOURCE_DIR}/lib/lazybsd_socket.c
${CMAKE_CURRENT_SOURCE_DIR}/lib/lazybsd_kevent.c
)

add_library(freebsd STATIC ${BSD_SOURCE})
Expand All @@ -181,14 +183,14 @@ target_compile_options(freebsd PRIVATE
$<$<COMPILE_LANGUAGE:C>:-Wmissing-prototypes>
$<$<COMPILE_LANGUAGE:C>:-Wno-pointer-sign>
$<$<COMPILE_LANGUAGE:CXX>:-fno-permissive>
$<$<COMPILE_LANGUAGE:C>:-nostdinc>
-Wpointer-arith
-Wno-inline
-Wno-strict-aliasing
-Wno-maybe-uninitialized
-Wmissing-include-dirs
-Wno-unused-but-set-variable
-Wno-address-of-packed-member
-nostdinc
-finline-limit=8000
-fdiagnostics-show-option
-fno-builtin
Expand All @@ -213,10 +215,14 @@ target_include_directories(freebsd PRIVATE
${BSD_SOURCE_DIR}/sys/
${BSD_SOURCE_DIR}/sys/amd64/include/
${BSD_SOURCE_DIR}/sys/contrib/ck/include
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/opt
)

include_directories(${CMAKE_SOURCE_DIR}/bsd/src/freebsd/sys)
include_directories(${CMAKE_SOURCE_DIR}/bsd/src/freebsd/sys/sys)
include_directories(${CMAKE_BINARY_DIR}/bsd/include)
include_directories(${CMAKE_SOURCE_DIR}/bsd/lib)

# 生成文件
add_custom_target(freebsd_generate
COMMAND bash ${CMAKE_SOURCE_DIR}/script/freebsd_generate.sh ${BSD_SOURCE_DIR} >> /dev/null
Expand Down
143 changes: 143 additions & 0 deletions bsd/lib/lazybsd_kevent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* @file lazybsd_kevent.c
* @author Meng Demao (mengdemao19951021@163.com)
* @brief
* @version 0.1
* @date 2024-06-12
*
* @copyright Copyright (c) 2024
*
*/
#include <sys/param.h>
#include <sys/limits.h>
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/syscallsubr.h>
#include <sys/module.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socketvar.h>
#include <sys/kernel.h>
#include <sys/refcount.h>
#include <sys/sysctl.h>
#include <sys/pcpu.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ttycom.h>
#include <sys/filio.h>
#include <sys/sysproto.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <net/route.h>
#include <net/route/route_ctl.h>

#include "lazybsd_errno.h"
#include "lazybsd_kevent.h"

int lazybsd_kqueue(void)
{
int rc;
if ((rc = kern_kqueue(curthread, 0, NULL)))
goto kern_fail;

rc = curthread->td_retval[0];
return (rc);

kern_fail:
lazybsd_os_errno(rc);
return (-1);
}

struct sys_kevent_args {
int fd;
const struct kevent *changelist;
int nchanges;
void *eventlist;
int nevents;
const struct timespec *timeout;
void (*do_each)(void **, struct kevent *);
};

static int
kevent_copyout(void *arg, struct kevent *kevp, int count)
{
int i;
struct kevent *ke;
struct sys_kevent_args *uap;

uap = (struct sys_kevent_args *)arg;

if (!uap->do_each) {
bcopy(kevp, uap->eventlist, count * sizeof *kevp);
uap->eventlist = (void *)((struct kevent *)(uap->eventlist) + count);

} else {
for (ke = kevp, i = 0; i < count; i++, ke++) {
uap->do_each(&(uap->eventlist), ke);
}
}

return (0);
}

/*
* Copy 'count' items from the list pointed to by uap->changelist.
*/
static int
kevent_copyin(void *arg, struct kevent *kevp, int count)
{
struct sys_kevent_args *uap;

uap = (struct sys_kevent_args *)arg;
bcopy(uap->changelist, kevp, count * sizeof *kevp);

uap->changelist += count;

return (0);
}

int lazybsd_kevent_do_each(int kq, const struct kevent *changelist, int nchanges,
void *eventlist, int nevents, const struct timespec *timeout,
void (*do_each)(void **, struct kevent *))
{
int rc;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;

struct sys_kevent_args ska = {
kq,
changelist,
nchanges,
eventlist,
nevents,
&ts,
do_each
};

struct kevent_copyops k_ops = {
&ska,
kevent_copyout,
kevent_copyin
};

if ((rc = kern_kevent(curthread, kq, nchanges, nevents, &k_ops,
&ts)))
goto kern_fail;

rc = curthread->td_retval[0];
return (rc);
kern_fail:
lazybsd_os_errno(rc);
return (-1);
}

int
lazybsd_kevent(int kq, const struct kevent *changelist, int nchanges,
struct kevent *eventlist, int nevents, const struct timespec *timeout)
{
return lazybsd_kevent_do_each(kq, changelist, nchanges, eventlist, nevents, timeout, NULL);
}
25 changes: 25 additions & 0 deletions bsd/lib/lazybsd_kevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file lazybsd_kevent.h
* @author Meng Demao (mengdemao19951021@163.com)
* @brief
* @version 0.1
* @date 2024-06-12
*
* @copyright Copyright (c) 2024
*
*/
#include <sys/types.h>
#include <sys/event.h>
#ifndef __LAZYBSD_KEVENT_H__
#define __LAZYBSD_KEVENT_H__

int lazybsd_kqueue(void);

int lazybsd_kevent(int kq, const struct kevent *changelist, int nchanges,
struct kevent *eventlist, int nevents, const struct timespec *timeout);

int lazybsd_kevent_do_each(int kq, const struct kevent *changelist, int nchanges,
void *eventlist, int nevents, const struct timespec *timeout,
void (*do_each)(void **, struct kevent *));

#endif /* __LAZYBSD_KEVENT_H__ */
Loading

0 comments on commit 09d2410

Please sign in to comment.