Zephyr API Documentation 4.0.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
12#ifndef ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_
13#define ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_
14
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
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
52struct net_offload {
56 int (*get)(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 sockaddr *addr,
66 socklen_t addrlen);
67
72 int (*listen)(struct net_context *context, int backlog);
73
78 int (*connect)(struct net_context *context,
79 const struct sockaddr *addr,
80 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 sockaddr *dst_addr,
107 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 sa_family_t family,
146 enum net_sock_type type,
147 enum net_ip_protocol ip_proto,
148 struct net_context **context)
149{
150 NET_ASSERT(iface);
151 NET_ASSERT(net_if_offload(iface));
152 NET_ASSERT(net_if_offload(iface)->get);
153
154 return net_if_offload(iface)->get(family, type, ip_proto, context);
155}
156
170static inline int net_offload_bind(struct net_if *iface,
171 struct net_context *context,
172 const struct sockaddr *addr,
173 socklen_t addrlen)
174{
175 NET_ASSERT(iface);
176 NET_ASSERT(net_if_offload(iface));
177 NET_ASSERT(net_if_offload(iface)->bind);
178
179 return net_if_offload(iface)->bind(context, addr, addrlen);
180}
181
194static inline int net_offload_listen(struct net_if *iface,
195 struct net_context *context,
196 int backlog)
197{
198 NET_ASSERT(iface);
199 NET_ASSERT(net_if_offload(iface));
200 NET_ASSERT(net_if_offload(iface)->listen);
201
202 return net_if_offload(iface)->listen(context, backlog);
203}
204
234static inline int net_offload_connect(struct net_if *iface,
235 struct net_context *context,
236 const struct sockaddr *addr,
237 socklen_t addrlen,
239 k_timeout_t timeout,
240 void *user_data)
241{
242 NET_ASSERT(iface);
243 NET_ASSERT(net_if_offload(iface));
244 NET_ASSERT(net_if_offload(iface)->connect);
245
246 return net_if_offload(iface)->connect(
247 context, addr, addrlen, cb,
248 timeout_to_int32(timeout),
249 user_data);
250}
251
279static inline int net_offload_accept(struct net_if *iface,
280 struct net_context *context,
282 k_timeout_t timeout,
283 void *user_data)
284{
285 NET_ASSERT(iface);
286 NET_ASSERT(net_if_offload(iface));
287 NET_ASSERT(net_if_offload(iface)->accept);
288
289 return net_if_offload(iface)->accept(
290 context, cb,
291 timeout_to_int32(timeout),
292 user_data);
293}
294
321static inline int net_offload_send(struct net_if *iface,
322 struct net_pkt *pkt,
324 k_timeout_t timeout,
325 void *user_data)
326{
327 NET_ASSERT(iface);
328 NET_ASSERT(net_if_offload(iface));
329 NET_ASSERT(net_if_offload(iface)->send);
330
331 return net_if_offload(iface)->send(
332 pkt, cb,
333 timeout_to_int32(timeout),
334 user_data);
335}
336
365static inline int net_offload_sendto(struct net_if *iface,
366 struct net_pkt *pkt,
367 const struct sockaddr *dst_addr,
368 socklen_t addrlen,
370 k_timeout_t timeout,
371 void *user_data)
372{
373 NET_ASSERT(iface);
374 NET_ASSERT(net_if_offload(iface));
375 NET_ASSERT(net_if_offload(iface)->sendto);
376
377 return net_if_offload(iface)->sendto(
378 pkt, dst_addr, addrlen, cb,
379 timeout_to_int32(timeout),
380 user_data);
381}
382
416static inline int net_offload_recv(struct net_if *iface,
417 struct net_context *context,
419 k_timeout_t timeout,
420 void *user_data)
421{
422 NET_ASSERT(iface);
423 NET_ASSERT(net_if_offload(iface));
424 NET_ASSERT(net_if_offload(iface)->recv);
425
426 return net_if_offload(iface)->recv(
427 context, cb,
428 timeout_to_int32(timeout),
429 user_data);
430}
431
445static inline int net_offload_put(struct net_if *iface,
446 struct net_context *context)
447{
448 NET_ASSERT(iface);
449 NET_ASSERT(net_if_offload(iface));
450 NET_ASSERT(net_if_offload(iface)->put);
451
452 return net_if_offload(iface)->put(context);
453}
454
455#else
456
459static inline int net_offload_get(struct net_if *iface,
460 sa_family_t family,
461 enum net_sock_type type,
462 enum net_ip_protocol ip_proto,
463 struct net_context **context)
464{
465 return 0;
466}
467
468static inline int net_offload_bind(struct net_if *iface,
469 struct net_context *context,
470 const struct sockaddr *addr,
471 socklen_t addrlen)
472{
473 return 0;
474}
475
476static inline int net_offload_listen(struct net_if *iface,
477 struct net_context *context,
478 int backlog)
479{
480 return 0;
481}
482
483static inline int net_offload_connect(struct net_if *iface,
484 struct net_context *context,
485 const struct sockaddr *addr,
486 socklen_t addrlen,
488 k_timeout_t timeout,
489 void *user_data)
490{
491 return 0;
492}
493
494static inline int net_offload_accept(struct net_if *iface,
495 struct net_context *context,
497 k_timeout_t timeout,
498 void *user_data)
499{
500 return 0;
501}
502
503static inline int net_offload_send(struct net_if *iface,
504 struct net_pkt *pkt,
506 k_timeout_t timeout,
507 void *user_data)
508{
509 return 0;
510}
511
512static inline int net_offload_sendto(struct net_if *iface,
513 struct net_pkt *pkt,
514 const struct sockaddr *dst_addr,
515 socklen_t addrlen,
517 k_timeout_t timeout,
518 void *user_data)
519{
520 return 0;
521}
522
523static inline int net_offload_recv(struct net_if *iface,
524 struct net_context *context,
526 k_timeout_t timeout,
527 void *user_data)
528{
529 return 0;
530}
531
532static inline int net_offload_put(struct net_if *iface,
533 struct net_context *context)
534{
535 return 0;
536}
537
540#endif /* CONFIG_NET_OFFLOAD */
541
542#ifdef __cplusplus
543}
544#endif
545
550#endif /* ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_ */
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1446
#define K_NO_WAIT
Generate null timeout delay.
Definition kernel.h:1336
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition sys_clock.h:80
unsigned short int sa_family_t
Socket address family type.
Definition net_ip.h:168
net_sock_type
Socket type.
Definition net_ip.h:88
size_t socklen_t
Length of a socket address.
Definition net_ip.h:172
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 sockaddr *addr, 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:1016
#define k_ticks_to_ms_floor32(t)
Convert ticks to milliseconds.
Definition time_units.h:1701
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 sys_clock.h:65
k_ticks_t ticks
Definition sys_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
void * user_data
User data associated with a context.
Definition net_context.h:214
int8_t iface
Network interface assigned to this context.
Definition net_context.h:408
Network Interface structure.
Definition net_if.h:692
Network packet.
Definition net_pkt.h:91
Generic sockaddr struct.
Definition net_ip.h:408