forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: sockets: Add a socket offload module
This patch enables BSD socket offload to a dedicated TCP/IP offload engine. This provides a simpler, more direct mechanism than going through NET_OFFLOAD (zsock -> net_context -> socket conversions) for those devices which provide complete TCP/IP offload at the BSD socket level, and whose use cases do not require IP routing between multiple network interfaces. To use, configure CONFIG_NET_SOCKETS_OFFLOAD=y, and register socket_offload_ops with this module. Fixes zephyrproject-rtos#3706 Signed-off-by: Gil Pitney <[email protected]>
- Loading branch information
Showing
6 changed files
with
249 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2018 Linaro Limited. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @brief Socket Offload Redirect API | ||
*/ | ||
|
||
#ifndef __SOCKET_OFFLOAD_H__ | ||
#define __SOCKET_OFFLOAD_H__ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <net/socket_offload_ops.h> | ||
|
||
extern const struct socket_offload *socket_ops; | ||
|
||
static inline int socket(int family, int type, int proto) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->socket); | ||
|
||
return socket_ops->socket(family, type, proto); | ||
} | ||
|
||
static inline int close(int sock) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->close); | ||
|
||
return socket_ops->close(sock); | ||
} | ||
|
||
static inline int accept(int sock, struct sockaddr *addr, | ||
socklen_t *addrlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->accept); | ||
|
||
return socket_ops->accept(sock, addr, addrlen); | ||
} | ||
|
||
|
||
static inline int bind(int sock, const struct sockaddr *addr, | ||
socklen_t addrlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->bind); | ||
|
||
return socket_ops->bind(sock, addr, addrlen); | ||
} | ||
|
||
static inline int listen(int sock, int backlog) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->listen); | ||
|
||
return socket_ops->listen(sock, backlog); | ||
} | ||
|
||
static inline int connect(int sock, const struct sockaddr *addr, | ||
socklen_t addrlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->connect); | ||
|
||
return socket_ops->connect(sock, addr, addrlen); | ||
} | ||
|
||
static inline int poll(struct pollfd *fds, int nfds, int timeout) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->poll); | ||
|
||
return socket_ops->poll(fds, nfds, timeout); | ||
} | ||
|
||
static inline int setsockopt(int sock, int level, int optname, | ||
const void *optval, | ||
socklen_t optlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->setsockopt); | ||
|
||
return socket_ops->setsockopt(sock, level, optname, optval, optlen); | ||
} | ||
|
||
static inline int getsockopt(int sock, int level, int optname, | ||
void *optval, socklen_t *optlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->getsockopt); | ||
|
||
return socket_ops->getsockopt(sock, level, optname, optval, optlen); | ||
} | ||
|
||
static inline ssize_t recv(int sock, void *buf, size_t max_len, | ||
int flags) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->recv); | ||
|
||
return socket_ops->recv(sock, buf, max_len, flags); | ||
} | ||
|
||
static inline ssize_t recvfrom(int sock, void *buf, | ||
short int len, | ||
short int flags, | ||
struct sockaddr *from, | ||
socklen_t *fromlen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->recvfrom); | ||
|
||
return socket_ops->recvfrom(sock, buf, len, flags, from, fromlen); | ||
} | ||
|
||
static inline ssize_t send(int sock, const void *buf, size_t len, | ||
int flags) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->send); | ||
|
||
return socket_ops->send(sock, buf, len, flags); | ||
} | ||
|
||
static inline ssize_t sendto(int sock, const void *buf, | ||
size_t len, int flags, | ||
const struct sockaddr *to, | ||
socklen_t tolen) | ||
{ | ||
__ASSERT_NO_MSG(socket_ops); | ||
__ASSERT_NO_MSG(socket_ops->sendto); | ||
|
||
return socket_ops->sendto(sock, buf, len, flags, to, tolen); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __SOCKET_OFFLOAD_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2018 Linaro Limited. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @brief Socket Offload Redirect API | ||
*/ | ||
|
||
#ifndef __SOCKET_OFFLOAD_OPS_H__ | ||
#define __SOCKET_OFFLOAD_OPS_H__ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <sys/types.h> | ||
#include <net/net_ip.h> | ||
#include <net/socket.h> /* needed for struct pollfd */ | ||
|
||
/* | ||
* It is assumed that these offload functions follow the | ||
* POSIX socket API standard for arguments, return values and setting of errno. | ||
*/ | ||
struct socket_offload { | ||
/* POSIX Socket Functions: */ | ||
int (*socket)(int family, int type, int proto); | ||
int (*close)(int sock); | ||
int (*accept)(int sock, struct sockaddr *addr, socklen_t *addrlen); | ||
int (*bind)(int sock, const struct sockaddr *addr, socklen_t addrlen); | ||
int (*listen)(int sock, int backlog); | ||
int (*connect)(int sock, const struct sockaddr *addr, | ||
socklen_t addrlen); | ||
int (*poll)(struct pollfd *fds, int nfds, int timeout); | ||
int (*setsockopt)(int sock, int level, int optname, | ||
const void *optval, socklen_t optlen); | ||
int (*getsockopt)(int sock, int level, int optname, void *optval, | ||
socklen_t *optlen); | ||
ssize_t (*recv)(int sock, void *buf, size_t max_len, int flags); | ||
ssize_t (*recvfrom)(int sock, void *buf, short int len, | ||
short int flags, struct sockaddr *from, | ||
socklen_t *fromlen); | ||
ssize_t (*send)(int sock, const void *buf, size_t len, int flags); | ||
ssize_t (*sendto)(int sock, const void *buf, size_t len, int flags, | ||
const struct sockaddr *to, socklen_t tolen); | ||
}; | ||
|
||
extern void socket_offload_register(const struct socket_offload *ops); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __SOCKET_OFFLOAD_OPS_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2018 Linaro Limited. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <net/socket_offload.h> | ||
|
||
/* Only one provider may register socket operations upon boot. */ | ||
const struct socket_offload *socket_ops; | ||
|
||
void socket_offload_register(const struct socket_offload *ops) | ||
{ | ||
__ASSERT_NO_MSG(ops); | ||
__ASSERT_NO_MSG(socket_ops == NULL); | ||
|
||
socket_ops = ops; | ||
} |