Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
net_offload.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
11
12#ifndef ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_
13#define ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_
14
23
24#include <zephyr/net_buf.h>
25#include <zephyr/net/net_ip.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#if defined(CONFIG_NET_OFFLOAD)
33
35
36static inline int32_t timeout_to_int32(k_timeout_t timeout)
37{
38 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
39 return 0;
40 } else if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
41 return -1;
42 } else {
43 return k_ticks_to_ms_floor32(timeout.ticks);
44 }
45}
46
48
52struct net_offload {
56 int (*get)(net_sa_family_t family,
57 enum net_sock_type type,
58 enum net_ip_protocol ip_proto,
59 struct net_context **context);
60
64 int (*bind)(struct net_context *context,
65 const struct net_sockaddr *addr,
66 net_socklen_t addrlen);
67
72 int (*listen)(struct net_context *context, int backlog);
73
78 int (*connect)(struct net_context *context,
79 const struct net_sockaddr *addr,
80 net_socklen_t addrlen,
82 int32_t timeout,
83 void *user_data);
84
89 int (*accept)(struct net_context *context,
91 int32_t timeout,
92 void *user_data);
93
97 int (*send)(struct net_pkt *pkt,
99 int32_t timeout,
100 void *user_data);
101
105 int (*sendto)(struct net_pkt *pkt,
106 const struct net_sockaddr *dst_addr,
107 net_socklen_t addrlen,
109 int32_t timeout,
110 void *user_data);
111
116 int (*recv)(struct net_context *context,
118 int32_t timeout,
119 void *user_data);
120
124 int (*put)(struct net_context *context);
125};
126
144static inline int net_offload_get(struct net_if *iface,
145 net_sa_family_t family,
146 enum net_sock_type type,
147 enum net_ip_protocol ip_proto,
148 struct net_context **context)
149{
150 struct net_offload *offload = net_if_offload(iface);
151
152 NET_ASSERT(iface);
153 NET_ASSERT(offload);
154 NET_ASSERT(offload->get);
155
156 return offload->get(family, type, ip_proto, context);
157}
158
172static inline int net_offload_bind(struct net_if *iface,
173 struct net_context *context,
174 const struct net_sockaddr *addr,
175 net_socklen_t addrlen)
176{
177 struct net_offload *offload = net_if_offload(iface);
178
179 NET_ASSERT(iface);
180 NET_ASSERT(offload);
181 NET_ASSERT(offload->bind);
182
183 return offload->bind(context, addr, addrlen);
184}
185
198static inline int net_offload_listen(struct net_if *iface,
199 struct net_context *context,
200 int backlog)
201{
202 struct net_offload *offload = net_if_offload(iface);
203
204 NET_ASSERT(iface);
205 NET_ASSERT(offload);
206 NET_ASSERT(offload->listen);
207
208 return offload->listen(context, backlog);
209}
210
240static inline int net_offload_connect(struct net_if *iface,
241 struct net_context *context,
242 const struct net_sockaddr *addr,
243 net_socklen_t addrlen,
245 k_timeout_t timeout,
246 void *user_data)
247{
248 struct net_offload *offload = net_if_offload(iface);
249
250 NET_ASSERT(iface);
251 NET_ASSERT(offload);
252 NET_ASSERT(offload->connect);
253
254 return offload->connect(
255 context, addr, addrlen, cb,
256 timeout_to_int32(timeout),
257 user_data);
258}
259
287static inline int net_offload_accept(struct net_if *iface,
288 struct net_context *context,
290 k_timeout_t timeout,
291 void *user_data)
292{
293 struct net_offload *offload = net_if_offload(iface);
294
295 NET_ASSERT(iface);
296 NET_ASSERT(offload);
297 NET_ASSERT(offload->accept);
298
299 return offload->accept(
300 context, cb,
301 timeout_to_int32(timeout),
302 user_data);
303}
304
331static inline int net_offload_send(struct net_if *iface,
332 struct net_pkt *pkt,
334 k_timeout_t timeout,
335 void *user_data)
336{
337 struct net_offload *offload = net_if_offload(iface);
338
339 NET_ASSERT(iface);
340 NET_ASSERT(offload);
341 NET_ASSERT(offload->send);
342
343 return offload->send(
344 pkt, cb,
345 timeout_to_int32(timeout),
346 user_data);
347}
348
377static inline int net_offload_sendto(struct net_if *iface,
378 struct net_pkt *pkt,
379 const struct net_sockaddr *dst_addr,
380 net_socklen_t addrlen,
382 k_timeout_t timeout,
383 void *user_data)
384{
385 struct net_offload *offload = net_if_offload(iface);
386
387 NET_ASSERT(iface);
388 NET_ASSERT(offload);
389 NET_ASSERT(offload->sendto);
390
391 return offload->sendto(
392 pkt, dst_addr, addrlen, cb,
393 timeout_to_int32(timeout),
394 user_data);
395}
396
430static inline int net_offload_recv(struct net_if *iface,
431 struct net_context *context,
433 k_timeout_t timeout,
434 void *user_data)
435{
436 struct net_offload *offload = net_if_offload(iface);
437
438 NET_ASSERT(iface);
439 NET_ASSERT(offload);
440 NET_ASSERT(offload->recv);
441
442 return offload->recv(
443 context, cb,
444 timeout_to_int32(timeout),
445 user_data);
446}
447
461static inline int net_offload_put(struct net_if *iface,
462 struct net_context *context)
463{
464 struct net_offload *offload = net_if_offload(iface);
465
466 NET_ASSERT(iface);
467 NET_ASSERT(offload);
468 NET_ASSERT(offload->put);
469
470 return offload->put(context);
471}
472
473#else
474
476
477static inline int net_offload_get(struct net_if *iface,
478 net_sa_family_t family,
479 enum net_sock_type type,
480 enum net_ip_protocol ip_proto,
481 struct net_context **context)
482{
483 return 0;
484}
485
486static inline int net_offload_bind(struct net_if *iface,
487 struct net_context *context,
488 const struct net_sockaddr *addr,
489 net_socklen_t addrlen)
490{
491 return 0;
492}
493
494static inline int net_offload_listen(struct net_if *iface,
495 struct net_context *context,
496 int backlog)
497{
498 return 0;
499}
500
501static inline int net_offload_connect(struct net_if *iface,
502 struct net_context *context,
503 const struct net_sockaddr *addr,
504 net_socklen_t addrlen,
506 k_timeout_t timeout,
507 void *user_data)
508{
509 return 0;
510}
511
512static inline int net_offload_accept(struct net_if *iface,
513 struct net_context *context,
515 k_timeout_t timeout,
516 void *user_data)
517{
518 return 0;
519}
520
521static inline int net_offload_send(struct net_if *iface,
522 struct net_pkt *pkt,
524 k_timeout_t timeout,
525 void *user_data)
526{
527 return 0;
528}
529
530static inline int net_offload_sendto(struct net_if *iface,
531 struct net_pkt *pkt,
532 const struct net_sockaddr *dst_addr,
533 net_socklen_t addrlen,
535 k_timeout_t timeout,
536 void *user_data)
537{
538 return 0;
539}
540
541static inline int net_offload_recv(struct net_if *iface,
542 struct net_context *context,
544 k_timeout_t timeout,
545 void *user_data)
546{
547 return 0;
548}
549
550static inline int net_offload_put(struct net_if *iface,
551 struct net_context *context)
552{
553 return 0;
554}
555
557
558#endif /* CONFIG_NET_OFFLOAD */
559
560#ifdef __cplusplus
561}
562#endif
563
567
568#endif /* ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_ */
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1762
#define K_NO_WAIT
Generate null timeout delay.
Definition kernel.h:1652
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition clock.h:80
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:172
unsigned short int net_sa_family_t
Socket address family type.
Definition net_ip.h:169
net_sock_type
Socket type.
Definition net_ip.h:89
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:64
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:167
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:120
void(* net_tcp_accept_cb_t)(struct net_context *new_context, struct net_sockaddr *addr, net_socklen_t addrlen, int status, void *user_data)
Accept callback.
Definition net_context.h:140
void(* net_context_recv_cb_t)(struct net_context *context, struct net_pkt *pkt, union net_ip_header *ip_hdr, union net_proto_header *proto_hdr, int status, void *user_data)
Network data receive callback.
Definition net_context.h:99
static struct net_offload * net_if_offload(struct net_if *iface)
Return the IP offload plugin.
Definition net_if.h:1183
#define k_ticks_to_ms_floor32(t)
Convert ticks to milliseconds.
Definition time_units.h:1718
Buffer management.
Network context definitions.
IPv6 and IPv4 definitions.
int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
ssize_t send(int sock, const void *buf, size_t len, int flags)
int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
int listen(int sock, int backlog)
int connect(int sock, const struct sockaddr *addr, socklen_t addrlen)
ssize_t sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
ssize_t recv(int sock, void *buf, size_t max_len, int flags)
__INT32_TYPE__ int32_t
Definition stdint.h:74
Kernel timeout type.
Definition clock.h:65
k_ticks_t ticks
Definition clock.h:66
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:207
int8_t iface
Network interface assigned to this context.
Definition net_context.h:500
Network Interface structure.
Definition net_if.h:764
Network packet.
Definition net_pkt.h:119
Generic sockaddr struct.
Definition net_ip.h:455