Skip to content

Commit b674ed6

Browse files
Florian Grandelcarlescufi
authored andcommitted
net: pkt: decouple from ieee802154 internals
This change implements part two of the program laid out in the TSCH RFC, see #50336#issuecomment-1250250154 : > Consolidate IEEE 802.15.4 options in net_pkt This change improves decoupling of generic net core code from IEEE 802.15.4 internals. It also simplifies IEEE 802.15.4 attribute cloning and thereby makes it easier to maintain and less error prone (and probably even faster as individual bits are no longer copied over separately). This enables us to extend and design IEEE 802.15.4 L2 attributes inside the package in isolation from the net core code which will no longer have to be changed when introducing changes or additions to the flags. This flexibility will be built upon in later change sets to model the IEEE 802.15.4 attributes closer to the spec. The solution is inspired by Linux's sk_buff->cb attribute which addresses the same concern as the attribute introduced in this change set: https://elixir.bootlin.com/linux/v6.0.1/source/include/linux/skbuff.h#L871 As the inline comment says: The cb attribute can be made a union or even a uint8[something] in the future, if further L2s need a control block, too. Right now such full indirection would make the code overly abstract, so I chose to compromise with maintainability in mind. Care has been taken to ensure that this changes does not introduce additional padding into the net package. To maintain zero-padding, future changes to the net packet struct will have to ensure that the IEEE 802.15.4 struct is 4-byte aligned (iff the IEEE 802.15.4 struct continues with max uint32_t scalar members) which is no deviation from the previous implementation. Signed-off-by: Florian Grandel <[email protected]>
1 parent 75b55e0 commit b674ed6

File tree

5 files changed

+275
-174
lines changed

5 files changed

+275
-174
lines changed

drivers/ieee802154/ieee802154_rf2xx.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ static struct net_buf rf2xx_ack_frame = {
6262
};
6363
static struct net_pkt rf2xx_ack_pkt = {
6464
.buffer = &rf2xx_ack_frame,
65-
.ieee802154_lqi = 80,
66-
.ieee802154_rssi = -40,
65+
.cb = {
66+
.lqi = 80,
67+
.rssi = -40,
68+
}
6769
};
6870
#endif /* CONFIG_NET_L2_OPENTHREAD */
6971

include/zephyr/net/ieee802154_pkt.h

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation.
3+
* Copyright (c) 2022 Florian Grandel.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
/**
9+
* @file
10+
* @brief Packet data common to all IEEE 802.15.4 L2 layers
11+
*/
12+
13+
#ifndef ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_
14+
#define ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_
15+
16+
#include <string.h>
17+
18+
#include <zephyr/types.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/** @cond ignore */
25+
26+
#ifndef NET_PKT_HAS_CONTROL_BLOCK
27+
#define NET_PKT_HAS_CONTROL_BLOCK
28+
#endif
29+
30+
struct net_pkt_cb_ieee802154 {
31+
#if defined(CONFIG_IEEE802154_2015)
32+
uint32_t ack_fc; /* Frame counter set in the ACK */
33+
uint8_t ack_keyid; /* Key index set in the ACK */
34+
#endif
35+
union {
36+
/* RX packets */
37+
struct {
38+
uint8_t lqi; /* Link Quality Indicator */
39+
uint8_t rssi; /* Received Signal Strength Indication */
40+
};
41+
#if defined(CONFIG_IEEE802154_SELECTIVE_TXPOWER)
42+
/* TX packets */
43+
struct {
44+
int8_t txpwr; /* TX power in dBm. */
45+
};
46+
#endif /* CONFIG_IEEE802154_SELECTIVE_TXPOWER */
47+
};
48+
49+
/* Flags */
50+
uint8_t arb : 1; /* ACK Request Bit is set in the frame */
51+
uint8_t ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
52+
uint8_t frame_secured : 1; /* Frame is authenticated and
53+
* encrypted according to its
54+
* Auxiliary Security Header
55+
*/
56+
uint8_t mac_hdr_rdy : 1; /* Indicates if frame's MAC header
57+
* is ready to be transmitted or if
58+
* it requires further modifications,
59+
* e.g. Frame Counter injection.
60+
*/
61+
#if defined(CONFIG_IEEE802154_2015)
62+
uint8_t fv2015 : 1; /* Frame version is IEEE 802.15.4-2015 or later */
63+
uint8_t ack_seb : 1; /* Security Enabled Bit was set in the ACK */
64+
#endif
65+
};
66+
67+
struct net_pkt;
68+
static inline void *net_pkt_cb(struct net_pkt *pkt);
69+
70+
static inline struct net_pkt_cb_ieee802154 *net_pkt_cb_ieee802154(struct net_pkt *pkt)
71+
{
72+
return net_pkt_cb(pkt);
73+
};
74+
75+
static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
76+
{
77+
return net_pkt_cb_ieee802154(pkt)->lqi;
78+
}
79+
80+
static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt, uint8_t lqi)
81+
{
82+
net_pkt_cb_ieee802154(pkt)->lqi = lqi;
83+
}
84+
85+
static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
86+
{
87+
return net_pkt_cb_ieee802154(pkt)->rssi;
88+
}
89+
90+
static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt, uint8_t rssi)
91+
{
92+
net_pkt_cb_ieee802154(pkt)->rssi = rssi;
93+
}
94+
95+
#if defined(CONFIG_IEEE802154_SELECTIVE_TXPOWER)
96+
static inline int8_t net_pkt_ieee802154_txpwr(struct net_pkt *pkt)
97+
{
98+
return net_pkt_cb_ieee802154(pkt)->txpwr;
99+
}
100+
101+
static inline void net_pkt_set_ieee802154_txpwr(struct net_pkt *pkt, int8_t txpwr)
102+
{
103+
net_pkt_cb_ieee802154(pkt)->txpwr = txpwr;
104+
}
105+
#endif /* CONFIG_IEEE802154_SELECTIVE_TXPOWER */
106+
107+
static inline bool net_pkt_ieee802154_arb(struct net_pkt *pkt)
108+
{
109+
return net_pkt_cb_ieee802154(pkt)->arb;
110+
}
111+
112+
static inline void net_pkt_set_ieee802154_arb(struct net_pkt *pkt, bool arb)
113+
{
114+
net_pkt_cb_ieee802154(pkt)->arb = arb;
115+
}
116+
117+
static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
118+
{
119+
return net_pkt_cb_ieee802154(pkt)->ack_fpb;
120+
}
121+
122+
static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt, bool fpb)
123+
{
124+
net_pkt_cb_ieee802154(pkt)->ack_fpb = fpb;
125+
}
126+
127+
static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
128+
{
129+
return net_pkt_cb_ieee802154(pkt)->frame_secured;
130+
}
131+
132+
static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt, bool secured)
133+
{
134+
net_pkt_cb_ieee802154(pkt)->frame_secured = secured;
135+
}
136+
137+
static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
138+
{
139+
return net_pkt_cb_ieee802154(pkt)->mac_hdr_rdy;
140+
}
141+
142+
static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt, bool rdy)
143+
{
144+
net_pkt_cb_ieee802154(pkt)->mac_hdr_rdy = rdy;
145+
}
146+
147+
#if defined(CONFIG_IEEE802154_2015)
148+
static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
149+
{
150+
return net_pkt_cb_ieee802154(pkt)->ack_fc;
151+
}
152+
153+
static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt, uint32_t fc)
154+
{
155+
net_pkt_cb_ieee802154(pkt)->ack_fc = fc;
156+
}
157+
158+
static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
159+
{
160+
return net_pkt_cb_ieee802154(pkt)->ack_keyid;
161+
}
162+
163+
static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt, uint8_t keyid)
164+
{
165+
net_pkt_cb_ieee802154(pkt)->ack_keyid = keyid;
166+
}
167+
168+
static inline bool net_pkt_ieee802154_fv2015(struct net_pkt *pkt)
169+
{
170+
return net_pkt_cb_ieee802154(pkt)->fv2015;
171+
}
172+
173+
static inline void net_pkt_set_ieee802154_fv2015(struct net_pkt *pkt, bool fv2015)
174+
{
175+
net_pkt_cb_ieee802154(pkt)->fv2015 = fv2015;
176+
}
177+
178+
static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
179+
{
180+
return net_pkt_cb_ieee802154(pkt)->ack_seb;
181+
}
182+
183+
static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
184+
{
185+
net_pkt_cb_ieee802154(pkt)->ack_seb = seb;
186+
}
187+
#endif /* CONFIG_IEEE802154_2015 */
188+
189+
/** @endcond */
190+
191+
#ifdef __cplusplus
192+
}
193+
#endif
194+
195+
#endif /* ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_ */

0 commit comments

Comments
 (0)