1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef __OAL_LITEOS_NETBUFF_H__
20 #define __OAL_LITEOS_NETBUFF_H__
21
22 #ifndef __KERNEL__
23 #include <endian.h>
24 #endif
25 #include "hdf_netbuf.h"
26 #include "oal_err_wifi.h"
27 #include "oal_util.h"
28
29 #define oal_init_netbuf_stru()
30 #define oal_malloc_netbuf_stru(size)
31 #define oal_free_netbuf_stru(p)
32
33 #define oal_host2net_short(_x) htons(_x)
34 #define oal_net2host_short(_x) ntohs(_x)
35 #define oal_host2net_long(_x) htonl(_x)
36 #define oal_net2host_long(_x) ntohl(_x)
37 #define oal_high_half_byte(a) (((a) & 0xF0) >> 4)
38 #define oal_low_half_byte(a) ((a) & 0x0F)
39
40 typedef uint32_t oal_gfp_enum_uint8;
41 typedef NetBuf oal_netbuf_stru;
42 typedef NetBufQueue oal_netbuf_head_stru;
43
44 typedef struct oal_netbuf_stru_tag {
45 struct oal_netbuf_stru_tag *next;
46 uint8_t mem_state_flag; /* 内存块状态 */
47 uint8_t subpool_id : 4; /* 记录所属子池id */
48 uint8_t is_high_priority : 1;
49 uint8_t bit_resv : 3;
50 uint16_t us_index;
51 } oal_dev_netbuf_stru;
52
53 typedef struct oal_ip_header {
54 #if (_PRE_LITTLE_CPU_ENDIAN == _PRE_CPU_ENDIAN) /* LITTLE_ENDIAN */
55 uint8_t us_ihl: 4,
56 version_ihl: 4;
57 #else
58 uint8_t version_ihl: 4,
59 us_ihl: 4;
60 #endif
61 uint8_t tos;
62 uint16_t us_tot_len;
63 uint16_t us_id;
64 uint16_t us_frag_off;
65 uint8_t ttl;
66 uint8_t protocol;
67 uint16_t us_check;
68 uint32_t saddr;
69 uint32_t daddr;
70 /* The options start here */
71 } oal_ip_header_stru;
72
73 typedef struct oal_tcp_header {
74 uint16_t us_sport;
75 uint16_t us_dport;
76 uint32_t seqnum;
77 uint32_t acknum;
78 uint8_t offset;
79 uint8_t flags;
80 uint16_t us_window;
81 uint16_t us_check;
82 uint16_t us_urgent;
83 } oal_tcp_header_stru;
84
85 #ifdef _PRE_WLAN_FEATURE_FLOWCTL
86 hi_void oal_netbuf_get_txtid(oal_netbuf_stru *netbuf, uint8_t *puc_tos);
87 #endif
88 hi_u8 oal_netbuf_is_tcp_ack(oal_ip_header_stru *ip_hdr);
89 hi_u8 oal_netbuf_is_icmp(const oal_ip_header_stru *ip_hdr);
90
91
92 #define oal_netbuf_list_num(q) NetBufQueueSize(q)
93 #define oal_netbuf_list_len(q) NetBufQueueSize(q)
94 #define oal_netbuf_list_empty(q) NetBufQueueIsEmpty(q)
95 #define oal_free_netbuf_list(q) NetBufQueueClear(q)
96
97 #define oal_netbuf_queue_num(q) NetBufQueueSize(q)
98 #define OAL_NETBUF_QUEUE_TAIL(q, nb) NetBufQueueEnqueue(q, nb)
99 #define OAL_NETBUF_QUEUE_HEAD_INIT(q) NetBufQueueInit(q)
100 #define OAL_NETBUF_DEQUEUE(q) NetBufQueueDequeue(q)
101 #define oal_netbuf_head_init(q) NetBufQueueInit(q)
102 #define oal_netbuf_peek(q) NetBufQueueAtHead(q)
103 #define oal_netbuf_tail(q) NetBufQueueAtTail(q)
104 #define oal_netbuf_get_buf_num(q) NetBufQueueSize(q)
105
106 #define oal_dev_alloc_skb(size) NetBufAlloc(size)
107 #define oal_netbuf_free(nb) NetBufFree(nb)
108 #define oal_netbuf_expand_head(nb, head, tail, mask) NetBufResizeRoom(nb, head, tail)
109 #define oal_netbuf_realloc_tailroom(nb, tail) (NetBufResizeRoom(nb, 0, tail) == 0 ? nb : NULL)
110 #define oal_netbuf_concat(nb, cnb) NetBufConcat(nb, cnb)
111
112 #define oal_netbuf_list_head_init(q) NetBufQueueInit(q)
113 #define oal_netbuf_add_to_list_tail(nb, q) NetBufQueueEnqueue(q, nb)
114 #define oal_netbuf_addlist(q, nb) NetBufQueueEnqueueHead(q, nb)
115 #define oal_netbuf_delist(q) NetBufQueueDequeue(q)
116 #define oal_netbuf_delist_tail(q) NetBufQueueDequeueTail(q)
117 #define oal_netbuf_list_tail(q, nb) NetBufQueueEnqueue(q, nb)
118 #define oal_netbuf_list_purge(q) NetBufQueueClear(q)
119
120
121 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
122
123 #ifdef _PRE_LWIP_ZERO_COPY
124 #define PBUF_ZERO_COPY_RESERVE 36
125 oal_netbuf_stru *oal_pbuf_netbuf_alloc(uint32_t len);
126 #endif
127
128 #define ETH_P_CONTROL 0x0016 /* Card specific control frames */
129
130 #define L1_CACHE_BYTES (1 << 5)
131 #define SKB_DATA_ALIGN(X) (((X) + (L1_CACHE_BYTES - 1)) & ~(L1_CACHE_BYTES - 1))
132
133 #ifndef NET_SKB_PAD
134 #define NET_SKB_PAD 64 // max(32, L1_CACHE_BYTES)
135 #endif
136
137 #define oal_netbuf_next(nb) (nb->dlist.next == NULL ? NULL : CONTAINER_OF(nb->dlist.next, NetBuf, dlist))
138 #define oal_netbuf_prev(nb) (nb->dlist.prev == NULL ? NULL : CONTAINER_OF(nb->dlist.prev, NetBuf, dlist))
set_oal_netbuf_next(NetBuf * nb,NetBuf * new)139 static inline void set_oal_netbuf_next(NetBuf *nb, NetBuf *new)
140 {
141 (nb)->dlist.next = (new == NULL ? NULL : &new->dlist);
142 }
143
set_oal_netbuf_prev(NetBuf * nb,NetBuf * new)144 static inline void set_oal_netbuf_prev(NetBuf *nb, NetBuf *new)
145 {
146 (nb)->dlist.prev = (new == NULL ? NULL : &new->dlist);
147 }
148
149 #define oal_netbuf_head_next(q) ((q)->dlist.next)
150 #define oal_netbuf_head_prev(q) ((q)->dlist.prev)
151
152 #define oal_netbuf_set_protocol(nb, p) // ((nb)->protocol)
153 #define oal_netbuf_priority(nb) 0 // ((nb)->rsv[oal_netbuf_cb_size()-1])
154 #define oal_netbuf_data(nb) NetBufGetAddress(nb, E_DATA_BUF)
155 #define oal_netbuf_header(nb) NetBufGetAddress(nb, E_DATA_BUF)
156 #define oal_netbuf_payload(nb) NetBufGetAddress(nb, E_DATA_BUF)
157 #define oal_netbuf_cb(nb) ((nb)->rsv)
158 #define oal_netbuf_cb_size() (sizeof(((NetBuf*)0)->rsv))
159 #define oal_netbuf_len(nb) NetBufGetDataLen(nb)
160 #define OAL_NETBUF_TAIL(nb) NetBufGetAddress(nb, E_TAIL_BUF)
161
162 #define oal_netbuf_put(nb, len) NetBufPush(nb, E_DATA_BUF, len)
163 #define oal_netbuf_push(nb, len) NetBufPop(nb, E_HEAD_BUF, len)
164 #define oal_netbuf_pull(nb, len) NetBufPush(nb, E_HEAD_BUF, len)
165 #define oal_netbuf_reserve(nb, l) do { \
166 NetBufPop(nb, E_TAIL_BUF, l); \
167 NetBufPop(nb, E_DATA_BUF, l); \
168 } while(0)
169 #define oal_netbuf_get(nb) (nb)
170 #define oal_netbuf_unshare(nb) (nb)
171 #define oal_netbuf_headroom(nb) NetBufGetRoom(nb, E_HEAD_BUF)
172 #define oal_netbuf_tailroom(nb) NetBufGetRoom(nb, E_TAIL_BUF)
173
174 #define oal_netbuf_trim(nb, l) if (nb->dataLen > l && nb->bufs[E_DATA_BUF].len > l) { \
175 NetBufPush(nb, E_TAIL_BUF, l); \
176 }
177 #define oal_netbuf_set_len(nb,l) if (nb->dataLen > l) { \
178 NetBufPush(nb, E_TAIL_BUF, nb->dataLen - l); \
179 } else { \
180 NetBufPush(nb, E_DATA_BUF, (l - nb->dataLen)); \
181 }
182
183 #define oal_netbuf_list_next(nb) oal_netbuf_next(nb)
184 #define oal_netbuf_init(nb, len) oal_netbuf_set_len(nb, len)
185
186 #define oal_netbuf_splice_init(add, q) NetBufQueueConcat(q, add)
187 #define oal_netbuf_copy_queue_mapping(to, from)
188 #define oal_skb_set_queue_mapping(nb, mapping) nb->qmap = mapping
189
190 #define oal_skb_queue_walk_safe(q, nb, tmp) DLIST_FOR_EACH_ENTRY_SAFE(nb, tmp, &q->dlist, NetBuf, dlist)
191 #define oal_netbuf_append(q, nb, prev) do { \
192 DListInsertHead(&nb->dlist, &prev->dlist); \
193 (q)->size++; \
194 } while(0)
195
196 typedef struct pbuf oal_lwip_buf;
197
198 #elif (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
199
200 #define ETH_P_CONTROL 0x0016 /* Card specific control frames */
201
202 #define oal_netbuf_next(nb) ((nb)->next)
203 #define oal_netbuf_prev(nb) ((nb)->prev)
204 #define set_oal_netbuf_next(nb, new) ((nb)->next = new)
205 #define set_oal_netbuf_prev(nb, new) ((nb)->prev = new)
206
207 #define oal_netbuf_head_next(q) ((q)->next)
208 #define oal_netbuf_head_prev(q) ((q)->prev)
209
210 #define oal_netbuf_set_protocol(nb, p) ((nb)->protocol = p)
211 #define oal_netbuf_priority(nb) ((nb)->priority)
212 #define oal_netbuf_data(nb) ((nb)->data)
213 #define oal_netbuf_header(nb) ((nb)->data)
214 #define oal_netbuf_payload(nb) ((nb)->data)
215 #define oal_netbuf_cb(nb) (((nb)->cb))
216 #define oal_netbuf_cb_size() (sizeof(((NetBuf*)0)->cb))
217 #define oal_netbuf_len(nb) ((nb)->len)
218 #define OAL_NETBUF_TAIL(nb) skb_tail_pointer(nb)
219
220
221 #define oal_netbuf_put(nb, len) skb_put(nb, len)
222 #define oal_netbuf_push(nb, len) skb_push(nb, len)
223 #define oal_netbuf_pull(nb, len) skb_pull(nb, len)
224 #define oal_netbuf_reserve(nb, l) skb_reserve(nb,l)
225 #define oal_netbuf_get(nb) skb_get(nb)
226 #define oal_netbuf_unshare(nb, pri) skb_unshare(nb, pri)
227 #define oal_netbuf_headroom(nb) skb_headroom(nb)
228 #define oal_netbuf_tailroom(nb) skb_tailroom(nb)
229 #define oal_netbuf_trim(nb, l) skb_trim(nb, (nb->len - l))
230 #define oal_netbuf_set_len(nb, l) if (nb->len > l) { \
231 skb_trim(nb, l); \
232 } else { \
233 skb_put(nb, (l - nb->len)); \
234 }
235
236 #define oal_netbuf_list_next(nb) oal_netbuf_next(nb)
237 #define oal_netbuf_init(nb, len) oal_netbuf_set_len(nb,len) \
238 nb->protocol = ETH_P_CONTROL;
239
240 #define oal_netbuf_splice_init(from, to) skb_queue_splice_init(from, to)
241 #define oal_netbuf_copy_queue_mapping(to, from) skb_copy_queue_mapping(to, from)
242 #define oal_skb_set_queue_mapping(nb, mapping) skb_set_queue_mapping(nb, mapping)
243 #define oal_skb_queue_walk_safe(q, nb, tmp) skb_queue_walk_safe(q, nb, tmp)
244 #define oal_netbuf_append(q, nb, prev) __skb_queue_after(q, prev, nb)
245
246 #endif
247
oal_netbuf_get_appointed_netbuf(NetBuf * nb,uint8_t num,NetBuf ** expect_netbuf)248 static inline uint32_t oal_netbuf_get_appointed_netbuf(NetBuf *nb, uint8_t num, NetBuf **expect_netbuf)
249 {
250 hi_u8 buf_num;
251
252 if (oal_unlikely((nb == HI_NULL) || (expect_netbuf == HI_NULL))) {
253 return HI_ERR_CODE_PTR_NULL;
254 }
255
256 *expect_netbuf = HI_NULL;
257
258 for (buf_num = 0; buf_num < num; buf_num++) {
259 *expect_netbuf = oal_netbuf_next(nb);
260
261 if (*expect_netbuf == HI_NULL) {
262 break;
263 }
264
265 nb = *expect_netbuf;
266 }
267
268 return HI_SUCCESS;
269 }
270
oal_netbuf_copydata(const oal_netbuf_stru * netbuf,hi_u32 offset,hi_void * dst,hi_u32 dst_len,hi_u32 len)271 static inline hi_u32 oal_netbuf_copydata(const oal_netbuf_stru *netbuf, hi_u32 offset, hi_void *dst, hi_u32 dst_len,
272 hi_u32 len)
273 {
274 if (memcpy_s(dst, dst_len, oal_netbuf_data(netbuf) + offset, len) != EOK) {
275 return HI_FAIL;
276 }
277
278 return HI_SUCCESS;
279 }
280
oal_netbuf_alloc(uint32_t size,uint32_t reserve,uint32_t align)281 static inline NetBuf *oal_netbuf_alloc(uint32_t size, uint32_t reserve, uint32_t align)
282 {
283 NetBuf *nb = NULL;
284
285 (void)align;
286 nb = NetBufAlloc(size + reserve + NET_SKB_PAD);
287 if (nb != NULL) {
288 oal_netbuf_reserve(nb, reserve + NET_SKB_PAD);
289 }
290
291 return nb;
292 }
293
oal_netbuf_splice_sync(oal_netbuf_head_stru * to,oal_netbuf_head_stru * from)294 static inline void oal_netbuf_splice_sync(oal_netbuf_head_stru *to, oal_netbuf_head_stru *from)
295 {
296 NetBuf *nb = NULL;
297
298 while (1) {
299 nb = NetBufQueueDequeueTail(from);
300 if (nb == NULL)
301 break;
302 NetBufQueueEnqueueHead(to, nb);
303 }
304 }
305
306 #endif
307