Network Traffic Offloading

Network Offloading

Overview

The network offloading API provides hooks that a device vendor can use to provide an alternate implementation for an IP stack. This means that the actual network connection creation, data transfer, etc., is done in the vendor HAL instead of the Zephyr network stack.

API Reference

group net_offload

Network offloading interface.

Functions

static int net_offload_get(struct net_if *iface, sa_family_t family, enum net_sock_type type, enum net_ip_protocol ip_proto, struct net_context **context)

Get a network socket/context from the offloaded IP stack.

Network socket is used to define the connection 5-tuple (protocol, remote address, remote port, source address and source port). This is similar as BSD socket() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • family: IP address family (AF_INET or AF_INET6)
  • type: Type of the socket, SOCK_STREAM or SOCK_DGRAM
  • ip_proto: IP protocol, IPPROTO_UDP or IPPROTO_TCP
  • context: The allocated context is returned to the caller.

static int net_offload_bind(struct net_if *iface, struct net_context *context, const struct sockaddr *addr, socklen_t addrlen)

Assign a socket a local address.

This is similar as BSD bind() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The context to be assigned.
  • addr: Address to assigned.
  • addrlen: Length of the address.

static int net_offload_listen(struct net_if *iface, struct net_context *context, int backlog)

Mark the context as a listening one.

This is similar as BSD listen() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The context to use.
  • backlog: The size of the pending connections backlog.

static int net_offload_connect(struct net_if *iface, struct net_context *context, const struct sockaddr *addr, socklen_t addrlen, net_context_connect_cb_t cb, s32_t timeout, void *user_data)

Create a network connection.

The net_context_connect function creates a network connection to the host specified by addr. After the connection is established, the user-supplied callback (cb) is executed. cb is called even if the timeout was set to K_FOREVER. cb is not called if the timeout expires. For datagram sockets (SOCK_DGRAM), this function only sets the peer address. This function is similar to the BSD connect() function.

Return
0 on success.
Return
-EINVAL if an invalid parameter is passed as an argument.
Return
-ENOTSUP if the operation is not supported or implemented.
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The network context.
  • addr: The peer address to connect to.
  • addrlen: Peer address length.
  • cb: Callback function. Set to NULL if not required.
  • timeout: The timeout value for the connection. Possible values:
    • K_NO_WAIT: this function will return immediately,
    • K_FOREVER: this function will block until the connection is established,
    • >0: this function will wait the specified ms.
  • user_data: Data passed to the callback function.

static int net_offload_accept(struct net_if *iface, struct net_context *context, net_tcp_accept_cb_t cb, s32_t timeout, void *user_data)

Accept a network connection attempt.

Accept a connection being established. This function will return immediately if the timeout is set to K_NO_WAIT. In this case the context will call the supplied callback when ever there is a connection established to this context. This is “a register

handler and forget” type of call (async). If the timeout is set to K_FOREVER, the function will wait until the connection is established. Timeout value > 0, will wait as many ms. After the connection is established a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return in this case. The callback is not called if the timeout expires. This is similar as BSD

accept() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The context to use.
  • cb: Caller-supplied callback function.
  • timeout: Timeout for the connection. Possible values are K_FOREVER, K_NO_WAIT, >0.
  • user_data: Caller-supplied user data.

static int net_offload_send(struct net_if *iface, struct net_pkt *pkt, net_context_send_cb_t cb, s32_t timeout, void *user_data)

Send a network packet to a peer.

This function can be used to send network data to a peer connection. This function will return immediately if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function will wait until the network packet is sent. Timeout value > 0 will wait as many ms. After the network packet is sent, a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return in this case. The callback is not called if the timeout expires. For context of type SOCK_DGRAM, the destination address must have been set by the call to net_context_connect(). This is similar as BSD send() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • pkt: The network packet to send.
  • cb: Caller-supplied callback function.
  • timeout: Timeout for the connection. Possible values are K_FOREVER, K_NO_WAIT, >0.
  • user_data: Caller-supplied user data.

static int net_offload_sendto(struct net_if *iface, struct net_pkt *pkt, const struct sockaddr *dst_addr, socklen_t addrlen, net_context_send_cb_t cb, s32_t timeout, void *user_data)

Send a network packet to a peer specified by address.

This function can be used to send network data to a peer specified by address. This variant can only be used for datagram connections of type SOCK_DGRAM. This function will return immediately if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function will wait until the network packet is sent. Timeout value > 0 will wait as many ms. After the network packet is sent, a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return. The callback is not called if the timeout expires. This is similar as BSD sendto() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • pkt: The network packet to send.
  • dst_addr: Destination address. This will override the address already set in network packet.
  • addrlen: Length of the address.
  • cb: Caller-supplied callback function.
  • timeout: Timeout for the connection. Possible values are K_FOREVER, K_NO_WAIT, >0.
  • user_data: Caller-supplied user data.

static int net_offload_recv(struct net_if *iface, struct net_context *context, net_context_recv_cb_t cb, s32_t timeout, void *user_data)

Receive network data from a peer specified by context.

This function can be used to register a callback function that is called by the network stack when network data has been received for this context. As this function registers a callback, then there is no need to call this function multiple times if timeout is set to K_NO_WAIT. If callback function or user data changes, then the function can be called multiple times to register new values. This function will return immediately if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function will wait until the network packet is received. Timeout value > 0 will wait as many ms. After the network packet is received, a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return in this case. The callback is not called if the timeout expires. The timeout functionality can be compiled out if synchronous behavior is not needed. The sync call logic requires some memory that can be saved if only async way of call is used. If CONFIG_NET_CONTEXT_SYNC_RECV is not set, then the timeout parameter value is ignored. This is similar as BSD recv() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The network context to use.
  • cb: Caller-supplied callback function.
  • timeout: Caller-supplied timeout. Possible values are K_FOREVER, K_NO_WAIT, >0.
  • user_data: Caller-supplied user data.

static int net_offload_put(struct net_if *iface, struct net_context *context)

Free/close a network context.

This releases the context. It is not possible to send or receive data via this context after this call. This is similar as BSD shutdown() function.

Return
0 if ok, < 0 if error
Parameters
  • iface: Network interface where the offloaded IP stack can be reached.
  • context: The context to be closed.

struct net_offload
#include <net_offload.h>

For return parameters and return values of the elements in this struct, see similarly named functions in net_context.h

Socket Offloading

Overview

In addition to the network offloading API, Zephyr allows offloading of networking functionality at the socket API level. With this approach, vendors who provide an alternate implementation of the networking stack, exposing socket API for their networking devices, can easily integrate it with Zephyr.

See drivers/wifi/simplelink/simplelink_sockets.c for a sample implementation on how to integrate network offloading at socket level.

API Reference

group socket_offload

Socket Offload Redirect API.

Functions

void socket_offload_register(const struct socket_offload *ops)

Register an offloaded socket API interface.

Parameters
  • ops: A pointer to the offloaded socket API interface.

struct socket_offload
#include <socket_offload_ops.h>

An offloaded Socket API interface.

It is assumed that these offload functions follow the POSIX socket API standard for arguments, return values and setting of errno.