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
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
51 #include <stdint.h>
52 #include "./src/netapi_loc.h"
55 //#define NETAPI_DEBUG
56 #ifdef NETAPI_DEBUG
57 #define netapi_Log printf
58 #else
59 #define netapi_Log
60 #endif
62 extern NETAPI_SHM_T* pnetapiShm;
64 /* Wrapper functions around HPLIB APIs for QMSS , *utility to convert virt2phy, phy2virt */
65 #define _Osal_qmssVirtToPhy hplib_mVMVirtToPhy
67 #define _Osal_qmssPhyToVirt hplib_mVMPhyToVirt
69 //static hplib_spinLock_T netapi_util_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
71 /**
72 * @ingroup netapi_gen_functions
73 * @brief netapi_registerHeap API is used to register a heap that is created by application so that
74 * it's garbage queue can be polled automatically by @ref netapi_pollHeapGarbage.
75 *
76 * @details This API registers an application-created heap with the netapi instance
77 * so that it can add that heap's garbage queue to the garbage poll function.
78 * NOTE: netapi internal heap is automatically registered
79 * @param[in] p The NETAPI handle, @ref NETAPI_T
80 * @param[in] h Handle of pklib heap to register
81 * @retval 1 if OK, <0 on error
82 * @pre @ref netapi_init
83 */
84 static inline int netapi_registerHeap(NETAPI_T p,
85 Pktlib_HeapHandle h)
86 {
87 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
88 int i;
89 pp->spinLock.lock(&pnetapiShm->netapi_util_lock);
90 for(i=0;i<TUNE_NETAPI_MAX_HEAPS;i++)
91 {
92 if (!pp->createdHeaps[i])
93 {
94 pp->createdHeaps[i]=h;
95 pp->spinLock.unlock(&pnetapiShm->netapi_util_lock);
96 return 1;
97 }
98 }
99 pp->spinLock.unlock(&pnetapiShm->netapi_util_lock);
100 return -1;
101 }
103 /**
104 * @ingroup netapi_gen_functions
105 * @brief netapi_unregisterHeap API is used to un-register a heap that was created by application
106 * and previously registered so that it's garbage queue could be polled
107 * automatically by @ref netapi_pollHeapGarbage.
108 *
109 * @details This API un-registers an application-created heap with the netapi instance.
110 * NOTE: netapi internal heap is automatically registered
111 * @param[in] p The NETAPI handle, @ref NETAPI_T
112 * @param[in] h The pklib handle to heap
113 * @retval <0 if err, 1 if OK
114 * @pre @ref netapi_init
115 */
116 static inline int netapi_unregisterHeap(NETAPI_T p,
117 Pktlib_HeapHandle h)
118 {
119 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
120 int i;
121 pp->spinLock.lock(&pnetapiShm->netapi_util_lock);
122 for(i=0;i<TUNE_NETAPI_MAX_HEAPS;i++)
123 {
124 if (pp->createdHeaps[i] == h)
125 {
126 pp->createdHeaps[i]=NULL;
127 pp->spinLock.unlock(&pnetapiShm->netapi_util_lock);
128 return 1;
129 }
130 }
131 pp->spinLock.unlock(&pnetapiShm->netapi_util_lock);
132 return -1;
133 }
135 /**
136 * @ingroup netapi_gen_functions
137 * @brief netapi_closeHeap API is used to remove a created pktlib heap
138 *
139 * @details This API removes an application-created heap with the netapi instance
140 * NOTE: descriptors are zapped and cannot be reused]
141 * @param[in] p The NETAPI handle, @ref NETAPI_T
142 * @param[in] h : handle to pklib heap
143 * @retval <0 if err, 1 if OK
144 * @pre @ref netapi_init @ref netapi_registerHeap
145 */
146 static inline int netapi_closeHeap(NETAPI_T p,
147 Pktlib_HeapHandle h)
148 {
149 int err = 0;
151 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
152 Qmss_QueueHnd q;
153 Pktlib_garbageCollection(h);
155 Pktlib_deleteHeap(h, &err);
157 return 1;
158 }
159 /**
160 * @ingroup netapi_gen_functions
161 * @brief netapi_getCookie API is used to return a piece of application-provided opaque data that has been
162 * stored in the netapi instance.
163 *
164 * @details The application can save a pointer to opaque data in the @ref NETAPI_T instance.
165 * This APi lets this data be returned to the application.
166 * @param[in] p The NETAPI handle, @ref NETAPI_T
167 * @retval Data provided in @ref netapi_setCookie
168 * @pre @ref netapi_init, @ref netapi_setCookie
169 */
171 static inline void * netapi_getCookie(NETAPI_T p)
172 {
173 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
174 return pp->cookie;
175 }
177 /**
178 * @ingroup netapi_gen_functions
179 * @brief netapi_setCookie API is used to set a piece of application-provided opaque data t in the netapi instance.
180 *
181 * @details The applicaiton can save a pointer to opaque data in the @ref NETAPI_T instance.
182 * Pointer to a opaque data can be returned later to the application via @ref netapi_getCookie
183 * @param[in] p The NETAPI handle, @ref NETAPI_T
184 * @param[in] cookie Opaque data to be saved
185 * @retval none
186 * @pre @ref netapi_init
187 */
188 static inline void netapi_setCookie(NETAPI_T p,
189 void * cookie)
190 {
191 NETAPI_HANDLE_T *pp = (NETAPI_HANDLE_T *) p;
192 pp->cookie= cookie;
193 }
195 /**
196 * @ingroup netapi_gen_functions
197 * @brief netapi_getBufMemRemainder API is used to return the amount of free memory available for
198 * allocating buffers for additonal Pktlib heaps
199 *
200 * @details The application can use this API to determine how much free memory is
201 * available for heap buffers if it decides to create its own.
202 * @retval Amount of memory available for heap buffer storage (in bytes)
203 * @pre @ref netapi_init
204 */
205 static inline int netapi_getBufMemRemainder(void)
206 {
207 return hplib_vmGetMemPoolRemainder(0);
208 }
210 /**
211 * @ingroup netapi_gen_functions
212 * @brief netapi_getDefaultFlow: API is used to return the default NETCP flow that is to
213 * be used for incoming packets.
214 *
215 * @details The application can use this API to return the default NETCP flow that is used
216 * for received packets. A NETCP flow is a list of PacketLib Heaps that are to be
217 * used to supply free packets to the receive DMA function.
218 * @param[in] p The NETAPI handle, @ref NETAPI_T
219 * @retval NETCP_CFG_FLOW_HANDLE_T The handle to default flow
220 * @pre @ref netapi_init
221 */
222 static inline NETCP_CFG_FLOW_HANDLE_T netapi_getDefaultFlow(NETAPI_T p)
223 {
224 return NETCP_DEFAULT_FLOW;
225 }
227 /**
228 * @ingroup netapi_gen_functions
229 * @brief netapi_getDefaultRoute: API is used to return the default NETCP route handle.
230 *
231 * @details This API returns the default NETCP route created by @ref netapi_init.
232 * A netcp route consists of a NETCP flow plus a destination pktio channel
233 * @param[in] p The NETAPI handle, @ref NETAPI_T
234 * @retval The handle of the default route, NETCP_CFG_ROUTE_HANDLE_T
235 * @pre @ref netapi_init
236 */
237 static inline NETCP_CFG_ROUTE_HANDLE_T netapi_getDefaultRoute(NETAPI_T p)
238 {
239 return NETCP_DEFAULT_ROUTE;
240 }
242 /**
243 * @ingroup cfg_functions
244 * @brief netapi_netcpCfgGetPolicyUserData API to retrieve user mode data associated with
245 * Policy APPID.
246 *
247 * @details This api is used to retrieve user mode data associated with an Policy APPID
248 * @param[in] h NETAPI instance handle, @ref NETAPI_T
249 * @param[in] app_id application id whose user mode data is to be retrieved
250 * @retval void* pointer to user mode data.
251 * @pre @ref netapi_init
252 */
253 static inline void* netapi_netcpCfgGetPolicyUserData(NETAPI_T h,
254 NETCP_CFG_SA_T app_id)
255 {
256 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
257 int slot = netapi_cfgGetMatchId(app_id);
258 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_POLICY))
259 {
260 return NULL;
261 }
262 return (p->policy[slot].user_data);
263 }
265 /**
266 * @ingroup cfg_functions
267 * @brief netapi_netcpCfgGetIpSecUserData API to retrieve user mode data associated with IPSEC APPID.
268 *
269 * @details This api is used to retrieve user mode data associated with an IPSEC APPID
270 * @param[in] h NETAPI instance handle, @ref NETAPI_T
271 * @param[in] app_id application id whose user mode data is to be retrieved
272 * @retval void* pointer to user mode data.
273 * @pre @ref netapi_init
274 */
275 static inline void* netapi_netcpCfgGetIpSecUserData(NETAPI_T h,
276 NETCP_CFG_SA_T app_id)
277 {
278 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
279 int slot = netapi_cfgGetMatchId(app_id);
280 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_SA))
281 {
282 return NULL;
283 }
284 return (p->tunnel[slot].user_data);
285 }
288 /**
289 * @ingroup cfg_functions
290 * @brief netapi_netcpCfgGetIpUserData API to retrieve user mode data associated with Generic IP APPID.
291 *
292 * @details This api is used to retrieve user mode data associated with a Generic IP APPID
293 * @param[in] h NETAPI instance handle, @ref NETAPI_T
294 * @param[in] app_id application id whose user mode data is to be retrieved
295 * @retval void* pointer to user mode data.
296 * @pre @ref netapi_init
297 */
298 static inline void* netapi_netcpCfgGetIpUserData(NETAPI_T h,
299 NETCP_CFG_SA_T app_id)
300 {
301 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
302 int slot = netapi_cfgGetMatchId(app_id);
303 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_NUM_IP))
304 {
305 return NULL;
306 }
307 return (p->ips[slot].user_data);
308 }
310 /**
311 * @ingroup cfg_functions
312 * @brief netapi_netcpCfgGetClassiferUserData API to retrieve user mode data associated with Classifer APPID.
313 *
314 * @details This api is used to retrieve user mode data associated with a flassifier APPID
315 * @param[in] h NETAPI instance handle, @ref NETAPI_T
316 * @param[in] app_id application id whose user mode data is to be retrieved
317 * @retval void* pointer to user mode data.
318 * @pre @ref netapi_init
319 */
320 static inline void* netapi_netcpCfgGetClassiferUserData(NETAPI_T h,
321 NETCP_CFG_SA_T app_id)
322 {
323 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
324 int slot = netapi_cfgGetMatchId(app_id);
325 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
326 {
327 return NULL;
328 }
329 return (p->classi[slot].user_data);
330 }
333 /**
334 * @ingroup cfg_functions
335 * @brief netapi_netcpCfgGetUserData API to retrieve user mode data associated with APPID.
336 *
337 * @details This api is used to retrieve user mode data associated with an APPID
338 * @param[in] h NETAPI instance handle, @ref NETAPI_T
339 * @param[in] app_id application id whose user mode data is to be retrieved
340 * @retval void* pointer to user mode data.
341 * @pre @ref netapi_init
342 */
343 static inline void* netapi_netcpCfgGetUserData(NETAPI_T h,
344 NETCP_CFG_SA_T app_id)
345 {
346 NETCP_CFG_SA_T appIdType;
347 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
349 appIdType = app_id & 0xff000000;
350 switch(appIdType)
351 {
352 case(NETAPI_NETCP_MATCH_IPSEC):
353 return (netapi_netcpCfgGetIpSecUserData(h, app_id));
354 break;
355 case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
356 return (netapi_netcpCfgGetPolicyUserData(h, app_id));
357 break;
358 case(NETAPI_NETCP_MATCH_GENERIC_IP):
359 return (netapi_netcpCfgGetIpUserData(h, app_id));
360 break;
361 case(NETAPI_NETCP_MATCH_CLASS):
362 return (netapi_netcpCfgGetClassiferUserData(h, app_id));
363 break;
364 default:
365 return NULL;
366 break;
367 }
368 }
370 /**
371 * @ingroup cfg_functions
372 * @brief netapi_netcpCfgUpdateUserData API to update user mode data associated with APPID.
373 *
374 * @details This api is used to update user mode data associated with an APPID
375 * @param[in] h NETAPI instance handle, @ref NETAPI_T
376 * @param[in] app_id application id whose user mode data is to updated
377 * @retval void* pointer to user mode data.
378 * @pre @ref netapi_init
379 */
380 static inline netapi_RetValue netapi_netcpCfgUpdateUserData(NETAPI_T h,
381 NETCP_CFG_SA_T app_id,
382 void * user_data)
383 {
384 NETCP_CFG_SA_T appIdType;
385 NETAPI_NWAL_GLOBAL_CONTEXT_T *p = &netapi_get_global()->nwal_context;
387 int slot = netapi_cfgGetMatchId(app_id);
388 if ((slot <0 ) || (slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
389 {
390 return NETAPI_ERR_BAD_INPUT;
391 }
392 appIdType = app_id & 0xff000000;
393 switch(appIdType)
394 {
395 case(NETAPI_NETCP_MATCH_IPSEC):
396 p->tunnel[slot].user_data = user_data;
397 return NETAPI_ERR_OK;
398 break;
399 case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
400 p->policy[slot].user_data = user_data;
401 return NETAPI_ERR_OK;
402 break;
403 case(NETAPI_NETCP_MATCH_GENERIC_IP):
404 p->ips[slot].user_data = user_data;
405 return NETAPI_ERR_OK;
406 break;
407 case(NETAPI_NETCP_MATCH_CLASS):
408 p->classi[slot].user_data = user_data;
409 return NETAPI_ERR_OK;
410 break;
411 default:
412 return NETAPI_ERR_BAD_INPUT;
413 break;
414 }
415 }
417 #ifdef __cplusplus
418 }
419 #endif
422 #endif