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 #define NET_HEXDUMP_DBG(_data, _length, _str) LOG_HEXDUMP_DBG(_data, _length, _str)
65 #define NET_HEXDUMP_ERR(_data, _length, _str) LOG_HEXDUMP_ERR(_data, _length, _str)
66 #define NET_HEXDUMP_WARN(_data, _length, _str) LOG_HEXDUMP_WRN(_data, _length, _str)
67 #define NET_HEXDUMP_INFO(_data, _length, _str) LOG_HEXDUMP_INF(_data, _length, _str)
68 
69 #define NET_ASSERT(cond, ...) __ASSERT(cond, "" __VA_ARGS__)
70 
71 /* This needs to be here in order to avoid circular include dependency between
72  * net_pkt.h and net_if.h
73  */
74 #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
75 	defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
76 #if !defined(NET_PKT_DETAIL_STATS_COUNT)
77 #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
78 
79 #if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
80 #define NET_PKT_DETAIL_STATS_COUNT 4
81 #else
82 #define NET_PKT_DETAIL_STATS_COUNT 3
83 #endif /* CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
84 
85 #else
86 #define NET_PKT_DETAIL_STATS_COUNT 4
87 #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL */
88 
89 #endif /* !NET_PKT_DETAIL_STATS_COUNT */
90 #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
91 	  CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
92 
93 /** @endcond */
94 
95 struct net_buf;
96 struct net_pkt;
97 struct net_context;
98 struct net_if;
99 
100 /**
101  * @brief Net Verdict
102  */
103 enum net_verdict {
104 	/** Packet has been taken care of. */
105 	NET_OK,
106 	/** Packet has not been touched, other part should decide about its
107 	 * fate.
108 	 */
109 	NET_CONTINUE,
110 	/** Packet must be dropped. */
111 	NET_DROP,
112 };
113 
114 /**
115  * @brief Called by lower network stack or network device driver when
116  * a network packet has been received. The function will push the packet up in
117  * the network stack for further processing.
118  *
119  * @param iface Network interface where the packet was received.
120  * @param pkt Network packet data.
121  *
122  * @return 0 if ok, <0 if error.
123  */
124 int net_recv_data(struct net_if *iface, struct net_pkt *pkt);
125 
126 /**
127  * @brief Try sending data to network.
128  *
129  * @details Send data to network. This should not be used normally by
130  * applications as it requires that the network packet is properly
131  * constructed.
132  *
133  * @param pkt Network packet.
134  * @param timeout Timeout for send.
135  *
136  * @return 0 if ok, <0 if error. If <0 is returned, then the caller needs
137  * to unref the pkt in order to avoid memory leak.
138  */
139 int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout);
140 
141 /**
142  * @brief Send data to network.
143  *
144  * @details Send data to network. This should not be used normally by
145  * applications as it requires that the network packet is properly
146  * constructed. Equivalent to net_try_send_data with infinite timeout.
147  *
148  * @param pkt Network packet.
149  *
150  * @return 0 if ok, <0 if error. If <0 is returned, then the caller needs
151  * to unref the pkt in order to avoid memory leak.
152  */
net_send_data(struct net_pkt * pkt)153 static inline int net_send_data(struct net_pkt *pkt)
154 {
155 	k_timeout_t timeout = k_is_in_isr() ? K_NO_WAIT : K_FOREVER;
156 
157 	return net_try_send_data(pkt, timeout);
158 }
159 
160 /** @cond INTERNAL_HIDDEN */
161 
162 /* Some helper defines for traffic class support */
163 #if defined(CONFIG_NET_TC_TX_COUNT) && defined(CONFIG_NET_TC_RX_COUNT)
164 #define NET_TC_TX_COUNT CONFIG_NET_TC_TX_COUNT
165 #define NET_TC_RX_COUNT CONFIG_NET_TC_RX_COUNT
166 
167 #if NET_TC_TX_COUNT > NET_TC_RX_COUNT
168 #define NET_TC_COUNT NET_TC_TX_COUNT
169 #else
170 #define NET_TC_COUNT NET_TC_RX_COUNT
171 #endif
172 #else /* CONFIG_NET_TC_TX_COUNT && CONFIG_NET_TC_RX_COUNT */
173 #define NET_TC_TX_COUNT 0
174 #define NET_TC_RX_COUNT 0
175 #define NET_TC_COUNT 0
176 #endif /* CONFIG_NET_TC_TX_COUNT && CONFIG_NET_TC_RX_COUNT */
177 
178 /**
179  * @brief Registration information for a given L3 handler. Note that
180  *        the layer number (L3) just refers to something that is on top
181  *        of L2. So for example IPv6 is L3 and IPv4 is L3, but Ethernet
182  *        based LLDP, gPTP are more in the layer 2.5 but we consider them
183  *        as L3 here for simplicity.
184  */
185 struct net_l3_register {
186 	/** Store also the name of the L3 type in order to be able to
187 	 * print it later.
188 	 */
189 	const char * const name;
190 	/** What L2 layer this is for */
191 	const struct net_l2 * const l2;
192 	/** Handler function for the specified protocol type. If the handler
193 	 * has taken ownership of the pkt, it must return NET_OK. If it wants to
194 	 * continue processing at the next level (e.g. ipv4), it must return
195 	 * NET_CONTINUE. If instead something is wrong with the packet (for
196 	 * example, a multicast address that does not match the protocol type)
197 	 * it must return NET_DROP so that the statistics can be updated
198 	 * accordingly
199 	 */
200 	enum net_verdict (*handler)(struct net_if *iface,
201 				    uint16_t ptype,
202 				    struct net_pkt *pkt);
203 	/** Protocol type */
204 	uint16_t ptype;
205 };
206 
207 #define NET_L3_GET_NAME(l3_name, ptype) __net_l3_register_##l3_name##_##ptype
208 
209 #define NET_L3_REGISTER(_l2_type, _name, _ptype, _handler)		\
210 	static const STRUCT_SECTION_ITERABLE(net_l3_register,		\
211 				    NET_L3_GET_NAME(_name, _ptype)) = { \
212 		.ptype = _ptype,					\
213 		.handler = _handler,					\
214 		.name = STRINGIFY(_name),				\
215 		.l2 = _l2_type,						\
216 	};
217 
218 /* @endcond */
219 
220 /**
221  * @}
222  */
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #endif /* ZEPHYR_INCLUDE_NET_NET_CORE_H_ */
229