1 /******************************************************
2 * File: netapi_util.h
3 * Purpose: misc utilites
4 **************************************************************
5 * FILE: netapi_util.h
6 *
7 * DESCRIPTION: netapi utility header file for user space transport
8 * library
9 *
10 * REVISION HISTORY:
11 *
12 * Copyright (c) Texas Instruments Incorporated 2013
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 * its contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *****************************************************/
43 #ifndef __NETAPI_UTIL__H
44 #define __NETAPI_UTIL__H
46 #include <stdint.h>
47 #include "ti/runtime/netapi/src/netapi_loc.h"
50 #define NETAPI_DEBUG
51 #ifdef NETAPI_DEBUG
52 #define netapi_Log printf
53 #else
54 #define netapi_Log
55 #endif
58 /* Wrapper functions around HPLIB APIs for QMSS , *utility to convert virt2phy, phy2virt */
59 #define _Osal_qmssVirtToPhy hplib_mVMVirtToPhy
61 #define _Osal_qmssPhyToVirt hplib_mVMPhyToVirt
63 static hplib_spinLock_T netapi_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
65 /**
66 * @ingroup netapi_gen_functions
67 * @brief netapi_registerHeap API is used to register a heap that is created by application so that
68 * it's garbage queue can be polled automatically by @ref netapi_pollHeapGarbage.
69 *
70 * @details This API registers an application-created heap with the netapi instance
71 * so that it can add that heap's garbage queue to the garbage poll function.
72 * NOTE: netapi internal heap is automatically registered
73 * @param[in] p The NETAPI handle, @ref NETAPI_T
74 * @param[in] h Handle of pklib heap to register
75 * @retval 1 if OK, <0 on error
76 * @pre @ref netapi_init
77 */
78 static inline int netapi_registerHeap(NETAPI_T p,
79 Pktlib_HeapHandle h)
80 {
81 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
82 int i;
83 hplib_mSpinLockLock(&netapi_lock);
84 for(i=0;i<TUNE_NETAPI_MAX_HEAPS;i++)
85 {
86 if (!pp->createdHeaps[i])
87 {
88 pp->createdHeaps[i]=h;
89 hplib_mSpinLockUnlock(&netapi_lock);
90 return 1;
91 }
92 }
93 hplib_mSpinLockUnlock(&netapi_lock);
94 return -1;
95 }
97 /**
98 * @ingroup netapi_gen_functions
99 * @brief netapi_unregisterHeap API is used to un-register a heap that was created by application
100 * and previously registered so that it's garbage queue could be polled
101 * automatically by @ref netapi_pollHeapGarbage.
102 *
103 * @details This API un-registers an application-created heap with the netapi instance.
104 * NOTE: netapi internal heap is automatically registered
105 * @param[in] p The NETAPI handle, @ref NETAPI_T
106 * @param[in] h The pklib handle to heap
107 * @retval <0 if err, 1 if OK
108 * @pre @ref netapi_init
109 */
110 static inline int netapi_unregisterHeap(NETAPI_T p,
111 Pktlib_HeapHandle h)
112 {
113 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
114 int i;
115 hplib_mSpinLockLock(&netapi_lock);
116 for(i=0;i<TUNE_NETAPI_MAX_HEAPS;i++)
117 {
118 if (pp->createdHeaps[i] == h)
119 {
120 pp->createdHeaps[i]=NULL;
121 hplib_mSpinLockUnlock(&netapi_lock);
122 return 1;
123 }
124 }
125 hplib_mSpinLockUnlock(&netapi_lock);
126 return -1;
127 }
129 /**
130 * @ingroup netapi_gen_functions
131 * @brief netapi_closeHeap API is used to remove a created pktlib heap
132 *
133 * @details This API removes an application-created heap with the netapi instance
134 * NOTE: descriptors are zapped and cannot be reused]
135 * @param[in] p The NETAPI handle, @ref NETAPI_T
136 * @param[in] h : handle to pklib heap
137 * @retval <0 if err, 1 if OK
138 * @pre @ref netapi_init @ref netapi_registerHeap
139 */
140 static inline int netapi_closeHeap(NETAPI_T p,
141 Pktlib_HeapHandle h)
142 {
143 Qmss_QueueHnd q;
144 Pktlib_garbageCollection(h);
145 q = Pktlib_getZeroHeapQueue(h);
146 netapip_zapQ(q);
147 q= Pktlib_getInternalHeapQueue(h);
148 netapip_zapQ(q);
149 }
150 /**
151 * @ingroup netapi_gen_functions
152 * @brief netapi_getCookie API is used to return a piece of application-provided opaque data that has been
153 * stored in the netapi instance.
154 *
155 * @details The application can save a pointer to opaque data in the @ref NETAPI_T instance.
156 * This APi lets this data be returned to the application.
157 * @param[in] p The NETAPI handle, @ref NETAPI_T
158 * @retval Data provided in @ref netapi_setCookie
159 * @pre @ref netapi_init, @ref netapi_setCookie
160 */
162 static inline void * netapi_getCookie(NETAPI_T p)
163 {
164 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
165 return pp->cookie;
166 }
168 /**
169 * @ingroup netapi_gen_functions
170 * @brief netapi_setCookie API is used to set a piece of application-provided opaque data t in the netapi instance.
171 *
172 * @details The applicaiton can save a pointer to opaque data in the @ref NETAPI_T instance.
173 * Pointer to a opaque data can be returned later to the application via @ref netapi_getCookie
174 * @param[in] p The NETAPI handle, @ref NETAPI_T
175 * @param[in] cookie Opaque data to be saved
176 * @retval none
177 * @pre @ref netapi_init
178 */
179 static inline void netapi_setCookie(NETAPI_T p,
180 void * cookie)
181 {
182 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
183 pp->cookie= cookie;
184 }
186 /**
187 * @ingroup netapi_gen_functions
188 * @brief netapi_getBufMemRemainder API is used to return the amount of free memory available for
189 * allocating buffers for additonal Pktlib heaps
190 *
191 * @details The application can use this API to determine how much free memory is
192 * available for heap buffers if it decides to create its own.
193 * @retval Amount of memory available for heap buffer storage (in bytes)
194 * @pre @ref netapi_init
195 */
196 static inline int netapi_getBufMemRemainder(void)
197 {
198 return hplib_vmGetMemPoolRemainder(0);
199 }
201 /**
202 * @ingroup netapi_gen_functions
203 * @brief netapi_getDefaultFlow: API is used to return the default NETCP flow that is to
204 * be used for incoming packets.
205 *
206 * @details The application can use this API to return the default NETCP flow that is used
207 * for received packets. A NETCP flow is a list of PacketLib Heaps that are to be
208 * used to supply free packets to the receive DMA function.
209 * @param[in] p The NETAPI handle, @ref NETAPI_T
210 * @retval NETCP_CFG_FLOW_HANDLE_T The handle to default flow
211 * @pre @ref netapi_init
212 */
213 static inline NETCP_CFG_FLOW_HANDLE_T netapi_getDefaultFlow(NETAPI_T p)
214 {
215 return NETCP_DEFAULT_FLOW;
216 }
218 /**
219 * @ingroup netapi_gen_functions
220 * @brief netapi_getDefaultRoute: API is used to return the default NETCP route handle.
221 *
222 * @details This API returns the default NETCP route created by @ref netapi_init.
223 * A netcp route consists of a NETCP flow plus a destination pktio channel
224 * @param[in] p The NETAPI handle, @ref NETAPI_T
225 * @retval The handle of the default route, NETCP_CFG_ROUTE_HANDLE_T
226 * @pre @ref netapi_init
227 */
228 static inline NETCP_CFG_ROUTE_HANDLE_T netapi_getDefaultRoute(NETAPI_T p)
229 {
230 return NETCP_DEFAULT_ROUTE;
231 }
233 /**
234 * @ingroup cfg_functions
235 * @brief netapi_netcpCfgGetPolicyUserData API to retrieve user mode data associated with
236 * Policy APPID.
237 *
238 * @details This api is used to retrieve user mode data associated with an Policy APPID
239 * @param[in] h NETAPI instance handle, @ref NETAPI_T
240 * @param[in] app_id application id whose user mode data is to be retrieved
241 * @retval void* pointer to user mode data.
242 * @pre @ref netapi_init
243 */
244 static inline void* netapi_netcpCfgGetPolicyUserData(NETAPI_T h,
245 NETCP_CFG_SA_T app_id)
246 {
247 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
248 int slot = netapi_cfgGetMatchId(app_id);
249 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_POLICY))
250 {
251 return NULL;
252 }
253 return (p->policy[slot].user_data);
254 }
256 /**
257 * @ingroup cfg_functions
258 * @brief netapi_netcpCfgGetIpSecUserData API to retrieve user mode data associated with IPSEC APPID.
259 *
260 * @details This api is used to retrieve user mode data associated with an IPSEC APPID
261 * @param[in] h NETAPI instance handle, @ref NETAPI_T
262 * @param[in] app_id application id whose user mode data is to be retrieved
263 * @retval void* pointer to user mode data.
264 * @pre @ref netapi_init
265 */
266 static inline void* netapi_netcpCfgGetIpSecUserData(NETAPI_T h,
267 NETCP_CFG_SA_T app_id)
268 {
269 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
270 int slot = netapi_cfgGetMatchId(app_id);
271 netapi_Log("netapi_netcpCfgGetIpSecUserData: slot found %d\n", slot);
272 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_SA))
273 {
274 return NULL;
275 }
276 netapi_Log("netapi_netcpCfgGetIpSecUserData: user data being returned %d\n",
277 (uint32_t)(p->tunnel[slot].user_data));
278 return (p->tunnel[slot].user_data);
279 }
282 /**
283 * @ingroup cfg_functions
284 * @brief netapi_netcpCfgGetIpUserData API to retrieve user mode data associated with Generic IP APPID.
285 *
286 * @details This api is used to retrieve user mode data associated with a Generic IP APPID
287 * @param[in] h NETAPI instance handle, @ref NETAPI_T
288 * @param[in] app_id application id whose user mode data is to be retrieved
289 * @retval void* pointer to user mode data.
290 * @pre @ref netapi_init
291 */
292 static inline void* netapi_netcpCfgGetIpUserData(NETAPI_T h,
293 NETCP_CFG_SA_T app_id)
294 {
295 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
296 int slot = netapi_cfgGetMatchId(app_id);
297 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_NUM_IP))
298 {
299 return NULL;
300 }
301 return (p->ips[slot].user_data);
302 }
304 /**
305 * @ingroup cfg_functions
306 * @brief netapi_netcpCfgGetClassiferUserData API to retrieve user mode data associated with Classifer APPID.
307 *
308 * @details This api is used to retrieve user mode data associated with a flassifier APPID
309 * @param[in] h NETAPI instance handle, @ref NETAPI_T
310 * @param[in] app_id application id whose user mode data is to be retrieved
311 * @retval void* pointer to user mode data.
312 * @pre @ref netapi_init
313 */
314 static inline void* netapi_netcpCfgGetClassiferUserData(NETAPI_T h,
315 NETCP_CFG_SA_T app_id)
316 {
317 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
318 int slot = netapi_cfgGetMatchId(app_id);
319 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
320 {
321 return NULL;
322 }
323 return (p->classi[slot].user_data);
324 }
327 /**
328 * @ingroup cfg_functions
329 * @brief netapi_netcpCfgGetUserData API to retrieve user mode data associated with APPID.
330 *
331 * @details This api is used to retrieve user mode data associated with an APPID
332 * @param[in] h NETAPI instance handle, @ref NETAPI_T
333 * @param[in] app_id application id whose user mode data is to be retrieved
334 * @retval void* pointer to user mode data.
335 * @pre @ref netapi_init
336 */
337 static inline void* netapi_netcpCfgGetUserData(NETAPI_T h,
338 NETCP_CFG_SA_T app_id)
339 {
340 NETCP_CFG_SA_T appIdType;
341 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
343 appIdType = app_id & 0xff000000;
344 switch(appIdType)
345 {
346 case(NETAPI_NETCP_MATCH_IPSEC):
347 netapi_Log("netapi_netcpCfgGetUserData: ipsec appID found\n");
348 return (netapi_netcpCfgGetIpSecUserData(h, app_id));
349 break;
350 case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
351 netapi_Log("netapi_netcpCfgGetUserData: policy appID found\n");
352 return (netapi_netcpCfgGetPolicyUserData(h, app_id));
353 break;
354 case(NETAPI_NETCP_MATCH_GENERIC_IP):
355 netapi_Log("netapi_netcpCfgGetUserData: IP appID found\n");
356 return (netapi_netcpCfgGetIpUserData(h, app_id));
357 break;
358 case(NETAPI_NETCP_MATCH_CLASS):
359 netapi_Log("netapi_netcpCfgGetUserData: classifier appID found\n");
360 return (netapi_netcpCfgGetClassiferUserData(h, app_id));
361 break;
362 default:
363 return NULL;
364 break;
365 }
366 }
368 /**
369 * @ingroup cfg_functions
370 * @brief netapi_netcpCfgUpdateUserData API to update user mode data associated with APPID.
371 *
372 * @details This api is used to update user mode data associated with an APPID
373 * @param[in] h NETAPI instance handle, @ref NETAPI_T
374 * @param[in] app_id application id whose user mode data is to updated
375 * @retval void* pointer to user mode data.
376 * @pre @ref netapi_init
377 */
378 static inline netapi_RetValue netapi_netcpCfgUpdateUserData(NETAPI_T h,
379 NETCP_CFG_SA_T app_id,
380 void * user_data)
381 {
382 NETCP_CFG_SA_T appIdType;
383 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
385 int slot = netapi_cfgGetMatchId(app_id);
386 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
387 {
388 return NETAPI_ERR_BAD_INPUT;
389 }
390 appIdType = app_id & 0xff000000;
391 switch(appIdType)
392 {
393 case(NETAPI_NETCP_MATCH_IPSEC):
394 netapi_Log("netapi_netcpCfgUpdateUserData: ipsec appID found\n");
395 p->tunnel[slot].user_data = user_data;
396 return NETAPI_ERR_OK;
397 break;
398 case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
399 netapi_Log("netapi_netcpCfgUpdateUserData: policy appID found\n");
400 p->policy[slot].user_data = user_data;
401 return NETAPI_ERR_OK;
402 break;
403 case(NETAPI_NETCP_MATCH_GENERIC_IP):
404 netapi_Log("netapi_netcpCfgUpdateUserData: IP appID found\n");
405 p->ips[slot].user_data = user_data;
406 return NETAPI_ERR_OK;
407 break;
408 case(NETAPI_NETCP_MATCH_CLASS):
409 netapi_Log("netapi_netcpCfgUpdateUserData: classifier appID found\n");
410 p->classi[slot].user_data = user_data;
411 return NETAPI_ERR_OK;
412 break;
413 default:
414 return NETAPI_ERR_BAD_INPUT;
415 break;
416 }
417 }
419 #endif