Line data Source code
1 1 : /** @file
2 : * @brief Network core definitions
3 : *
4 : * Definitions for networking support.
5 : */
6 :
7 : /*
8 : * Copyright (c) 2015 Intel Corporation
9 : *
10 : * SPDX-License-Identifier: Apache-2.0
11 : */
12 :
13 : #ifndef ZEPHYR_INCLUDE_NET_NET_CORE_H_
14 : #define ZEPHYR_INCLUDE_NET_NET_CORE_H_
15 :
16 : #include <stdbool.h>
17 : #include <string.h>
18 :
19 : #include <zephyr/logging/log.h>
20 : #include <zephyr/sys/__assert.h>
21 : #include <zephyr/kernel.h>
22 :
23 : #include <zephyr/net/net_timeout.h>
24 : #include <zephyr/net/net_linkaddr.h>
25 :
26 : #ifdef __cplusplus
27 : extern "C" {
28 : #endif
29 :
30 : /**
31 : * @brief Networking
32 : * @defgroup networking Networking
33 : * @since 1.0
34 : * @version 1.0.0
35 : * @ingroup connectivity
36 : * @{
37 : * @}
38 : */
39 :
40 : /**
41 : * @brief Network core library
42 : * @defgroup net_core Network Core Library
43 : * @since 1.0
44 : * @version 1.0.0
45 : * @ingroup networking
46 : * @{
47 : */
48 :
49 : /** @cond INTERNAL_HIDDEN */
50 :
51 : /* Network subsystem logging helpers */
52 : #ifdef CONFIG_THREAD_NAME
53 : #define NET_DBG(fmt, ...) LOG_DBG("(%s): " fmt, \
54 : k_thread_name_get(k_current_get()), \
55 : ##__VA_ARGS__)
56 : #else
57 : #define NET_DBG(fmt, ...) LOG_DBG("(%p): " fmt, k_current_get(), \
58 : ##__VA_ARGS__)
59 : #endif /* CONFIG_THREAD_NAME */
60 : #define NET_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
61 : #define NET_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
62 : #define NET_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
63 :
64 : /* Rate-limited network logging macros */
65 : #define NET_ERR_RATELIMIT(fmt, ...) LOG_ERR_RATELIMIT(fmt, ##__VA_ARGS__)
66 : #define NET_WARN_RATELIMIT(fmt, ...) LOG_WRN_RATELIMIT(fmt, ##__VA_ARGS__)
67 : #define NET_INFO_RATELIMIT(fmt, ...) LOG_INF_RATELIMIT(fmt, ##__VA_ARGS__)
68 : #define NET_DBG_RATELIMIT(fmt, ...) LOG_DBG_RATELIMIT(fmt, ##__VA_ARGS__)
69 :
70 : #define NET_HEXDUMP_DBG(_data, _length, _str) LOG_HEXDUMP_DBG(_data, _length, _str)
71 : #define NET_HEXDUMP_ERR(_data, _length, _str) LOG_HEXDUMP_ERR(_data, _length, _str)
72 : #define NET_HEXDUMP_WARN(_data, _length, _str) LOG_HEXDUMP_WRN(_data, _length, _str)
73 : #define NET_HEXDUMP_INFO(_data, _length, _str) LOG_HEXDUMP_INF(_data, _length, _str)
74 :
75 : #define NET_ASSERT(cond, ...) __ASSERT(cond, "" __VA_ARGS__)
76 :
77 : /* This needs to be here in order to avoid circular include dependency between
78 : * net_pkt.h and net_if.h
79 : */
80 : #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
81 : defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
82 : #if !defined(NET_PKT_DETAIL_STATS_COUNT)
83 : #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
84 :
85 : #if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
86 : #define NET_PKT_DETAIL_STATS_COUNT 4
87 : #else
88 : #define NET_PKT_DETAIL_STATS_COUNT 3
89 : #endif /* CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
90 :
91 : #else
92 : #define NET_PKT_DETAIL_STATS_COUNT 4
93 : #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL */
94 :
95 : #endif /* !NET_PKT_DETAIL_STATS_COUNT */
96 : #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
97 : CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
98 :
99 : /** @endcond */
100 :
101 : struct net_buf;
102 : struct net_pkt;
103 : struct net_context;
104 : struct net_if;
105 :
106 : /**
107 : * @brief Net Verdict
108 : */
109 1 : enum net_verdict {
110 : /** Packet has been taken care of. */
111 : NET_OK,
112 : /** Packet has not been touched, other part should decide about its
113 : * fate.
114 : */
115 : NET_CONTINUE,
116 : /** Packet must be dropped. */
117 : NET_DROP,
118 : };
119 :
120 : /**
121 : * @brief Called by lower network stack or network device driver when
122 : * a network packet has been received. The function will push the packet up in
123 : * the network stack for further processing.
124 : *
125 : * @param iface Network interface where the packet was received.
126 : * @param pkt Network packet data.
127 : *
128 : * @return 0 if ok, <0 if error.
129 : */
130 1 : int net_recv_data(struct net_if *iface, struct net_pkt *pkt);
131 :
132 : /**
133 : * @brief Try sending data to network.
134 : *
135 : * @details Send data to network. This should not be used normally by
136 : * applications as it requires that the network packet is properly
137 : * constructed.
138 : *
139 : * @param pkt Network packet.
140 : * @param timeout Timeout for send.
141 : *
142 : * @return 0 if ok, <0 if error. If <0 is returned, then the caller needs
143 : * to unref the pkt in order to avoid memory leak.
144 : */
145 1 : int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout);
146 :
147 : /**
148 : * @brief Send data to network.
149 : *
150 : * @details Send data to network. This should not be used normally by
151 : * applications as it requires that the network packet is properly
152 : * constructed. Equivalent to net_try_send_data with infinite timeout.
153 : *
154 : * @param pkt Network packet.
155 : *
156 : * @return 0 if ok, <0 if error. If <0 is returned, then the caller needs
157 : * to unref the pkt in order to avoid memory leak.
158 : */
159 1 : static inline int net_send_data(struct net_pkt *pkt)
160 : {
161 : k_timeout_t timeout = k_is_in_isr() ? K_NO_WAIT : K_FOREVER;
162 :
163 : return net_try_send_data(pkt, timeout);
164 : }
165 :
166 : /** @cond INTERNAL_HIDDEN */
167 :
168 : /* Some helper defines for traffic class support */
169 : #if defined(CONFIG_NET_TC_TX_COUNT) && defined(CONFIG_NET_TC_RX_COUNT)
170 : #define NET_TC_TX_COUNT CONFIG_NET_TC_TX_COUNT
171 : #define NET_TC_RX_COUNT CONFIG_NET_TC_RX_COUNT
172 :
173 : #if NET_TC_TX_COUNT > NET_TC_RX_COUNT
174 : #define NET_TC_COUNT NET_TC_TX_COUNT
175 : #else
176 : #define NET_TC_COUNT NET_TC_RX_COUNT
177 : #endif
178 : #else /* CONFIG_NET_TC_TX_COUNT && CONFIG_NET_TC_RX_COUNT */
179 : #define NET_TC_TX_COUNT 0
180 : #define NET_TC_RX_COUNT 0
181 : #define NET_TC_COUNT 0
182 : #endif /* CONFIG_NET_TC_TX_COUNT && CONFIG_NET_TC_RX_COUNT */
183 :
184 : /**
185 : * @brief Registration information for a given L3 handler. Note that
186 : * the layer number (L3) just refers to something that is on top
187 : * of L2. So for example IPv6 is L3 and IPv4 is L3, but Ethernet
188 : * based LLDP, gPTP are more in the layer 2.5 but we consider them
189 : * as L3 here for simplicity.
190 : */
191 : struct net_l3_register {
192 : /** Store also the name of the L3 type in order to be able to
193 : * print it later.
194 : */
195 : const char * const name;
196 : /** What L2 layer this is for */
197 : const struct net_l2 * const l2;
198 : /** Handler function for the specified protocol type. If the handler
199 : * has taken ownership of the pkt, it must return NET_OK. If it wants to
200 : * continue processing at the next level (e.g. ipv4), it must return
201 : * NET_CONTINUE. If instead something is wrong with the packet (for
202 : * example, a multicast address that does not match the protocol type)
203 : * it must return NET_DROP so that the statistics can be updated
204 : * accordingly
205 : */
206 : enum net_verdict (*handler)(struct net_if *iface,
207 : uint16_t ptype,
208 : struct net_pkt *pkt);
209 : /** Protocol type */
210 : uint16_t ptype;
211 : };
212 :
213 : #define NET_L3_GET_NAME(l3_name, ptype) __net_l3_register_##l3_name##_##ptype
214 :
215 : #define NET_L3_REGISTER(_l2_type, _name, _ptype, _handler) \
216 : static const STRUCT_SECTION_ITERABLE(net_l3_register, \
217 : NET_L3_GET_NAME(_name, _ptype)) = { \
218 : .ptype = _ptype, \
219 : .handler = _handler, \
220 : .name = STRINGIFY(_name), \
221 : .l2 = _l2_type, \
222 : };
223 :
224 : /* @endcond */
225 :
226 : /**
227 : * @}
228 : */
229 :
230 : #ifdef __cplusplus
231 : }
232 : #endif
233 :
234 : #endif /* ZEPHYR_INCLUDE_NET_NET_CORE_H_ */
|