Skip to content

Commit

Permalink
Merge pull request #23 from mengdemao/feature/socket
Browse files Browse the repository at this point in the history
feat(socket): 合并socket分支改动
  • Loading branch information
mengdemao committed May 28, 2024
2 parents f964b4a + 730f4fa commit 491c17c
Show file tree
Hide file tree
Showing 19 changed files with 435 additions and 354 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ WarningsAsErrors: '' # Treat all Checks from above as errors
HeaderFilterRegex: ''
FormatStyle: file
InheritParentConfig: false
AllowedIdentifiers: true

# The options below are just uncommented temporarily so that we do not change
# the public API during the hack a thon
Expand Down
2 changes: 1 addition & 1 deletion dpdk
Submodule dpdk updated 191 files
14 changes: 3 additions & 11 deletions include/lazybsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
* @version 1.0
* @date 2024-02-03
*
* @brief lazybsd根头文件
* @brief lazybsd根头文件,此文件C/CXX都是一样的
*
* @copyright Copyright (c) 2024 mengdemao
*
*/
#include <generated/config.h>
#include <generated/version.h>

#ifndef __LAZYBSD_H__
#define __LAZYBSD_H__

#ifdef __cplusplus
extern "C" {
#endif

// dpdk argc, argv, max argc: 16, member of dpdk_config
#define DPDK_CONFIG_NUM 16
#define DPDK_CONFIG_MAXLEN 256
Expand All @@ -35,8 +28,7 @@ extern "C" {
#define KNI_TYPE_KNI 0
#define KNI_TYPE_VIRTIO 1

#ifdef __cplusplus
}
#endif
#define LAZYBSD_EXIT_SUCCESS 0
#define LAZYBSD_EXIT_FAILURE 1

#endif /* __LAZYBSD_H__ */
10 changes: 10 additions & 0 deletions src/lazybsd_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @file lazybsd_api.cc
* @author mengdemao (mengdemao19951021@gmail.com)
* @brief
* @version 0.1
* @date 2024-05-22
*
* @copyright Copyright (c) 2024
*
*/
148 changes: 17 additions & 131 deletions src/lazybsd_api.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#ifndef _FSTACK_API_H
#define _FSTACK_API_H
/**
* @file lazybsd_api.h
* @author mengdemao (mengdemao19951021@gmail.com)
* @brief
* @version 0.1
* @date 2024-05-20
*
* @copyright Copyright (c) 2024
*
*/
#ifndef __LAZYBSD_API_H__
#define __LAZYBSD_API_H__

#ifdef __cplusplus
extern "C" {
Expand All @@ -19,121 +29,13 @@ struct linux_sockaddr {
char sa_data[14];
};

#define AF_INET6_LINUX 10
#define PF_INET6_LINUX AF_INET6_LINUX
#define AF_INET6_FREEBSD 28
#define PF_INET6_FREEBSD AF_INET6_FREEBSD
#define AF_INET6_LINUX 10
#define PF_INET6_LINUX AF_INET6_LINUX
#define AF_INET6_FREEBSD 28
#define PF_INET6_FREEBSD AF_INET6_FREEBSD

typedef int (*loop_func_t)(void *arg);

int lazybsd_init(int argc, char * const argv[]);

void lazybsd_run(loop_func_t loop, void *arg);

/* POSIX-LIKE api begin */

int lazybsd_fcntl(int fd, int cmd, ...);

int lazybsd_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
const void *newp, size_t newlen);

int lazybsd_ioctl(int fd, unsigned long request, ...);

/*
* While get sockfd from this API, and then need set it to non-blocking mode like this,
* Otherwise, sometimes the socket interface will not work properly, such as `lazybsd_write()`
*
* int on = 1;
* lazybsd_ioctl(sockfd, FIONBIO, &on);
*
* See also `example/main.c`
*/
int lazybsd_socket(int domain, int type, int protocol);

int lazybsd_setsockopt(int s, int level, int optname, const void *optval,
socklen_t optlen);

int lazybsd_getsockopt(int s, int level, int optname, void *optval,
socklen_t *optlen);

int lazybsd_listen(int s, int backlog);
int lazybsd_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen);
int lazybsd_accept(int s, struct linux_sockaddr *addr, socklen_t *addrlen);
int lazybsd_connect(int s, const struct linux_sockaddr *name, socklen_t namelen);
int lazybsd_close(int fd);
int lazybsd_shutdown(int s, int how);

int lazybsd_getpeername(int s, struct linux_sockaddr *name,
socklen_t *namelen);
int lazybsd_getsockname(int s, struct linux_sockaddr *name,
socklen_t *namelen);

ssize_t lazybsd_read(int d, void *buf, size_t nbytes);
ssize_t lazybsd_readv(int fd, const struct iovec *iov, int iovcnt);


/*
* Write data to the socket sendspace buf.
*
* Note:
* The `fd` parameter need set non-blocking mode in advance if F-Stack's APP.
* Otherwise if the `nbytes` parameter is greater than
* `net.inet.tcp.sendspace + net.inet.tcp.sendbuf_inc`,
* the API will return -1, but not the length that has been sent.
*
* You also can modify the value of `net.inet.tcp.sendspace`(default 16384 bytes)
* and `net.inet.tcp.sendbuf_inc`(default 16384 bytes) with `config.ini`.
* But it should be noted that not all parameters can take effect, such as 32768 and 32768.
* `lazybsd_sysctl` can see there values while APP is running.
*/
ssize_t lazybsd_write(int fd, const void *buf, size_t nbytes);
ssize_t lazybsd_writev(int fd, const struct iovec *iov, int iovcnt);

ssize_t lazybsd_send(int s, const void *buf, size_t len, int flags);
ssize_t lazybsd_sendto(int s, const void *buf, size_t len, int flags,
const struct linux_sockaddr *to, socklen_t tolen);
ssize_t lazybsd_sendmsg(int s, const struct msghdr *msg, int flags);

ssize_t lazybsd_recv(int s, void *buf, size_t len, int flags);
ssize_t lazybsd_recvfrom(int s, void *buf, size_t len, int flags,
struct linux_sockaddr *from, socklen_t *fromlen);
ssize_t lazybsd_recvmsg(int s, struct msghdr *msg, int flags);

int lazybsd_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);

int lazybsd_poll(struct pollfd fds[], nfds_t nfds, int timeout);

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 *));

#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 31)
int lazybsd_gettimeofday(struct timeval *tv, void *tz);
#else
int lazybsd_gettimeofday(struct timeval *tv, struct timezone *tz);
#endif

int lazybsd_dup(int oldfd);
int lazybsd_dup2(int oldfd, int newfd);

/* POSIX-LIKE api end */


/* Tests if fd is used by F-Stack */
extern int lazybsd_fdisused(int fd);

extern int lazybsd_getmaxfd(void);

/*
* Get traffic for QoS or other via API.
* The size of buffer must >= siezof(struct lazybsd_traffic_args), now is 32 bytes.
*/
void lazybsd_get_traffic(void *buffer);

/* route api begin */
enum LAZYBSD_ROUTE_CTL {
LAZYBSD_ROUTE_ADD,
Expand All @@ -146,17 +48,6 @@ enum LAZYBSD_ROUTE_FLAG {
LAZYBSD_RTF_GATEWAY,
};

/*
* On success, 0 is returned.
* On error, -1 is returned, and errno is set appropriately.
*/
int lazybsd_route_ctl(enum LAZYBSD_ROUTE_CTL req, enum LAZYBSD_ROUTE_FLAG flag,
struct linux_sockaddr *dst, struct linux_sockaddr *gw,
struct linux_sockaddr *netmask);

/* route api end */


/* dispatch api begin */
#define LAZYBSD_DISPATCH_ERROR (-1)
#define LAZYBSD_DISPATCH_RESPONSE (-2)
Expand Down Expand Up @@ -185,11 +76,6 @@ int lazybsd_route_ctl(enum LAZYBSD_ROUTE_CTL req, enum LAZYBSD_ROUTE_FLAG flag,
typedef int (*dispatch_func_t)(void *data, uint16_t *len,
uint16_t queue_id, uint16_t nb_queues);

/* regist a packet dispath function */
void lazybsd_regist_packet_dispatcher(dispatch_func_t func);

/* dispatch api end */

/* pcb lddr api begin */
/*
* pcb lddr callback function.
Expand Down Expand Up @@ -311,5 +197,5 @@ int lazybsd_zc_mbuf_read(struct lazybsd_zc_mbuf *m, const char *data, int len);
#ifdef __cplusplus
}
#endif
#endif
#endif // __LAZYBSD_API_H__

50 changes: 49 additions & 1 deletion src/lazybsd_api.hh
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
#include <lazybsd_api.h>
/**
* @file lazybsd_api.hh
* @author mengdemao (mengdemao19951021@gmail.com)
* @brief
* @version 0.1
* @date 2024-05-20
*
* @copyright Copyright (c) 2024
*
*/
#include "lazybsd_api.h"

#ifndef __LAZYBSD_API_HXX__
#define __LAZYBSD_API_HXX__

int lazybsd_init(int argc, char * const argv[]);

void lazybsd_run(loop_func_t loop, void *arg);

/* Tests if fd is used by F-Stack */
extern int lazybsd_fdisused(int fd);

extern int lazybsd_getmaxfd(void);

/*
* Get traffic for QoS or other via API.
* The size of buffer must >= siezof(struct lazybsd_traffic_args), now is 32 bytes.
*/
void lazybsd_get_traffic(void *buffer);

/*
* On success, 0 is returned.
* On error, -1 is returned, and errno is set appropriately.
*/
int lazybsd_route_ctl(enum LAZYBSD_ROUTE_CTL req, enum LAZYBSD_ROUTE_FLAG flag,
struct linux_sockaddr *dst, struct linux_sockaddr *gw,
struct linux_sockaddr *netmask);

/* regist a packet dispath function */
void lazybsd_regist_packet_dispatcher(dispatch_func_t func);

int lazybsd_fcntl(int fd, int cmd, ...);

int lazybsd_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
const void *newp, size_t newlen);

int lazybsd_ioctl(int fd, unsigned long request, ...);

#endif // __LAZYBSD_API_HXX__
8 changes: 5 additions & 3 deletions src/lazybsd_args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
* @copyright Copyright (c) 2024 mengdemao
*
*/
#include <boost/program_options.hpp>
#include <config.h>
#include <cstdio>
#include <cstddef>
#include <cstdint>
#include <config.h>
#include <fmt/core.h>
#include <gtest/gtest.h>
#include <iostream>
#include <lazybsd_args.h>
#include <lazybsd_version.h>

#include <boost/program_options.hpp>

using namespace boost;
using namespace std;

Expand Down
5 changes: 3 additions & 2 deletions src/lazybsd_dpdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <lazybsd_host.hh>
#include <lazybsd_dpdk.hh>
#include <lazybsd_veth.hh>
#include <lazybsd_socket.hh>

#include <iostream>
#include <string>
Expand Down Expand Up @@ -1644,7 +1645,7 @@ handle_ioctl_msg(struct lazybsd_msg *msg)
fd = lazybsd_socket(AF_INET6, SOCK_DGRAM, 0);
} else
#endif
fd = lazybsd_socket(AF_INET, SOCK_DGRAM, 0);
fd = lazybsd::net::lazybsd_socket(AF_INET, SOCK_DGRAM, 0);

if (fd < 0) {
ret = -1;
Expand All @@ -1653,7 +1654,7 @@ handle_ioctl_msg(struct lazybsd_msg *msg)

ret = lazybsd_ioctl_freebsd(fd, msg->ioctl.cmd, msg->ioctl.data);

lazybsd_close(fd);
lazybsd::net::lazybsd_close(fd);

done:
if (ret < 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/lazybsd_epoll.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @file lazybsd_epoll.hh
* @author mengdemao (mengdemao19951021@gmail.com)
* @brief
* @version 0.1
* @date 2024-05-28
*
* @copyright Copyright (c) 2024
*
*/
Loading

0 comments on commit 491c17c

Please sign in to comment.