Zephyr Project API 3.7.0
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
22#include <zephyr/net/buf.h>
23#include <zephyr/net/net_ip.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#if defined(CONFIG_NET_OFFLOAD)
31
34static inline int32_t timeout_to_int32(k_timeout_t timeout)
35{
37 return 0;
38 } else if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
39 return -1;
40 } else {
41 return k_ticks_to_ms_floor32(timeout.ticks);
42 }
43}
44
50struct net_offload {
54 int (*get)(sa_family_t family,
55 enum net_sock_type type,
56 enum net_ip_protocol ip_proto,
57 struct net_context **context);
58
62 int (*bind)(struct net_context *context,
63 const struct sockaddr *addr,
64 socklen_t addrlen);
65
70 int (*listen)(struct net_context *context, int backlog);
71
76 int (*connect)(struct net_context *context,
77 const struct sockaddr *addr,
78 socklen_t addrlen,
81 void *user_data);
82
87 int (*accept)(struct net_context *context,
90 void *user_data);
91
95 int (*send)(struct net_pkt *pkt,
98 void *user_data);
99
103 int (*sendto)(struct net_pkt *pkt,
104 const struct sockaddr *dst_addr,
105 socklen_t addrlen,
108 void *user_data);
109
114 int (*recv)(struct net_context *context,
117 void *user_data);
118
122 int (*put)(struct net_context *context);
123};
124
142static inline int net_offload_get(struct net_if *iface,
143 sa_family_t family,
144 enum net_sock_type type,
145 enum net_ip_protocol ip_proto,
146 struct net_context **context)
147{
148 NET_ASSERT(iface);
149 NET_ASSERT(net_if_offload(iface));
150 NET_ASSERT(net_if_offload(iface)->get);
151
152 return net_if_offload(iface)->get(family, type, ip_proto, context);
153}
154
168static inline int net_offload_bind(struct net_if *iface,
169 struct net_context *context,
170 const struct sockaddr *addr,
171 socklen_t addrlen)
172{
173 NET_ASSERT(iface);
174 NET_ASSERT(net_if_offload(iface));
175 NET_ASSERT(net_if_offload(iface)->bind);
176
177 return net_if_offload(iface)->bind(context, addr, addrlen);
178}
179
192static inline int net_offload_listen(struct net_if *iface,
193 struct net_context *context,
194 int backlog)
195{
196 NET_ASSERT(iface);
197 NET_ASSERT(net_if_offload(iface));
198 NET_ASSERT(net_if_offload(iface)->listen);
199
200 return net_if_offload(iface)->listen(context, backlog);
201}
202
232static inline int net_offload_connect(struct net_if *iface,
233 struct net_context *context,
234 const struct sockaddr *addr,
235 socklen_t addrlen,
238 void *user_data)
239{
240 NET_ASSERT(iface);
241 NET_ASSERT(net_if_offload(iface));
242 NET_ASSERT(net_if_offload(iface)->connect);
243
244 return net_if_offload(iface)->connect(
245 context, addr, addrlen, cb,
246 timeout_to_int32(timeout),
247 user_data);
248}
249
277static inline int net_offload_accept(struct net_if *iface,
278 struct net_context *context,
281 void *user_data)
282{
283 NET_ASSERT(iface);
284 NET_ASSERT(net_if_offload(iface));
285 NET_ASSERT(net_if_offload(iface)->accept);
286
287 return net_if_offload(iface)->accept(
288 context, cb,
289 timeout_to_int32(timeout),
290 user_data);
291}
292
319static inline int net_offload_send(struct net_if *iface,
320 struct net_pkt *pkt,
323 void *user_data)
324{
325 NET_ASSERT(iface);
326 NET_ASSERT(net_if_offload(iface));
327 NET_ASSERT(net_if_offload(iface)->send);
328
329 return net_if_offload(iface)->send(
330 pkt, cb,
331 timeout_to_int32(timeout),
332 user_data);
333}
334
363static inline int net_offload_sendto(struct net_if *iface,
364 struct net_pkt *pkt,
365 const struct sockaddr *dst_addr,
366 socklen_t addrlen,
369 void *user_data)
370{
371 NET_ASSERT(iface);
372 NET_ASSERT(net_if_offload(iface));
373 NET_ASSERT(net_if_offload(iface)->sendto);
374
375 return net_if_offload(iface)->sendto(
376 pkt, dst_addr, addrlen, cb,
377 timeout_to_int32(timeout),
378 user_data);
379}
380
414static inline int net_offload_recv(struct net_if *iface,
415 struct net_context *context,
418 void *user_data)
419{
420 NET_ASSERT(iface);
421 NET_ASSERT(net_if_offload(iface));
422 NET_ASSERT(net_if_offload(iface)->recv);
423
424 return net_if_offload(iface)->recv(
425 context, cb,
426 timeout_to_int32(timeout),
427 user_data);
428}
429
443static inline int net_offload_put(struct net_if *iface,
444 struct net_context *context)
445{
446 NET_ASSERT(iface);
447 NET_ASSERT(net_if_offload(iface));
448 NET_ASSERT(net_if_offload(iface)->put);
449
450 return net_if_offload(iface)->put(context);
451}
452
453#else
454
457static inline int net_offload_get(struct net_if *iface,
458 sa_family_t family,
459 enum net_sock_type type,
460 enum net_ip_protocol ip_proto,
461 struct net_context **context)
462{
463 return 0;
464}
465
466static inline int net_offload_bind(struct net_if *iface,
467 struct net_context *context,
468 const struct sockaddr *addr,
469 socklen_t addrlen)
470{
471 return 0;
472}
473
474static inline int net_offload_listen(struct net_if *iface,
475 struct net_context *context,
476 int backlog)
477{
478 return 0;
479}
480
481static inline int net_offload_connect(struct net_if *iface,
482 struct net_context *context,
483 const struct sockaddr *addr,
484 socklen_t addrlen,
487 void *user_data)
488{
489 return 0;
490}
491
492static inline int net_offload_accept(struct net_if *iface,
493 struct net_context *context,
496 void *user_data)
497{
498 return 0;
499}
500
501static inline int net_offload_send(struct net_if *iface,
502 struct net_pkt *pkt,
505 void *user_data)
506{
507 return 0;
508}
509
510static inline int net_offload_sendto(struct net_if *iface,
511 struct net_pkt *pkt,
512 const struct sockaddr *dst_addr,
513 socklen_t addrlen,
516 void *user_data)
517{
518 return 0;
519}
520
521static inline int net_offload_recv(struct net_if *iface,
522 struct net_context *context,
525 void *user_data)
526{
527 return 0;
528}
529
530static inline int net_offload_put(struct net_if *iface,
531 struct net_context *context)
532{
533 return 0;
534}
535
538#endif /* CONFIG_NET_OFFLOAD */
539
540#ifdef __cplusplus
541}
542#endif
543
548#endif /* ZEPHYR_INCLUDE_NET_NET_OFFLOAD_H_ */
ZTEST_BMEM int timeout
Definition main.c:31
static int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
POSIX wrapper for zsock_bind.
Definition socket.h:891
static int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
POSIX wrapper for zsock_accept.
Definition socket.h:910
static int listen(int sock, int backlog)
POSIX wrapper for zsock_listen.
Definition socket.h:904
static ssize_t sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
POSIX wrapper for zsock_sendto.
Definition socket.h:928
static ssize_t send(int sock, const void *buf, size_t len, int flags)
POSIX wrapper for zsock_send.
Definition socket.h:916
static int connect(int sock, const struct sockaddr *addr, socklen_t addrlen)
POSIX wrapper for zsock_connect.
Definition socket.h:897
static ssize_t recv(int sock, void *buf, size_t max_len, int flags)
POSIX wrapper for zsock_recv.
Definition socket.h:922
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1363
#define K_NO_WAIT
Generate null timeout delay.
Definition kernel.h:1253
#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:164
net_sock_type
Socket type.
Definition net_ip.h:84
size_t socklen_t
Length of a socket address.
Definition net_ip.h:168
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:62
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:97
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:138
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:118
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:165
static struct net_offload * net_if_offload(struct net_if *iface)
Return the IP offload plugin.
Definition net_if.h:992
#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.
__INT32_TYPE__ int32_t
Definition stdint.h:74
Kernel timeout type.
Definition sys_clock.h:65
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:205
int8_t iface
Network interface assigned to this context.
Definition net_context.h:376
Network Interface structure.
Definition net_if.h:678
Network packet.
Definition net_pkt.h:67
Generic sockaddr struct.
Definition net_ip.h:385
static const intptr_t user_data[5]
Definition main.c:588