HeaderInsertion.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * Changes from Qualcomm Innovation Center are provided under the following license:
  30. *
  31. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted (subject to the limitations in the
  35. * disclaimer below) provided that the following conditions are met:
  36. *
  37. * * Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. *
  40. * * Redistributions in binary form must reproduce the above
  41. * copyright notice, this list of conditions and the following
  42. * disclaimer in the documentation and/or other materials provided
  43. * with the distribution.
  44. *
  45. * * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  46. * contributors may be used to endorse or promote products derived
  47. * from this software without specific prior written permission.
  48. *
  49. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  50. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  51. * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  52. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  53. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  54. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  55. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  57. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  58. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  59. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  60. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  61. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  62. */
  63. #include <unistd.h>
  64. #include <sys/ioctl.h>
  65. #include <fcntl.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <cstring>
  69. #include "HeaderInsertion.h"
  70. #include "TestsUtils.h"
  71. #define LOG_IOCTL_RETURN_VALUE(nRetVal) \
  72. printf("%s()- %s\n", __func__, \
  73. (-1 == nRetVal) ? "Fail" : "Success");
  74. bool HeaderInsertion::AddHeader(struct ipa_ioc_add_hdr *pHeaderTableToAdd)
  75. {
  76. int nRetVal = 0;
  77. /*call the Driver ioctl in order to add header*/
  78. nRetVal = ioctl(m_fd, IPA_IOC_ADD_HDR, pHeaderTableToAdd);
  79. LOG_IOCTL_RETURN_VALUE(nRetVal);
  80. return (-1 != nRetVal);
  81. }
  82. bool HeaderInsertion::addHeaderHpc(const string& name, uint8_t* header, const size_t headerLen, bool isPartial, enum ipa_client_type ipaClient){
  83. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  84. return false;
  85. }
  86. int fd = open(CONFIGURATION_NODE_PATH, O_RDONLY);
  87. if (fd < 0) {
  88. cout << "failed to open " << CONFIGURATION_NODE_PATH << endl;
  89. return false;
  90. }
  91. struct ipa_ioc_add_hdr *iocH = static_cast<struct ipa_ioc_add_hdr*>(calloc(1, sizeof(*iocH) + sizeof(struct ipa_hdr_add)));
  92. if(!iocH){
  93. return false;
  94. }
  95. iocH->commit = 1;
  96. iocH->num_hdrs = 1;
  97. struct ipa_hdr_add *h = &iocH->hdr[0];
  98. strlcpy(h->name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  99. memcpy(h->hdr, header, headerLen);
  100. h->hdr_len = headerLen;
  101. h->hdr_hdl = -1;
  102. h->status = -1;
  103. h->is_partial = isPartial;
  104. cout << "h->name=" << h->name << ", h->is_partial=" << h->is_partial << endl;
  105. int result = ioctl(fd, IPA_TEST_IOC_ADD_HDR_HPC, iocH);
  106. if(result || h->status){
  107. free(iocH);
  108. close(fd);
  109. return false;
  110. }
  111. cout << "result=" << result << ", status=" << h->status << ", ipaClient=" << ipaClient << endl;
  112. struct ipa_pkt_init_ex_hdr_ofst_set lookup;
  113. lookup.ep = ipaClient;
  114. strlcpy(lookup.name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  115. result = ioctl(fd, IPA_TEST_IOC_PKT_INIT_EX_SET_HDR_OFST , &lookup);
  116. if (result) {
  117. free(iocH);
  118. close(fd);
  119. return false;
  120. }
  121. free(iocH);
  122. close(fd);
  123. return true;
  124. }
  125. bool HeaderInsertion::DeleteHeader(struct ipa_ioc_del_hdr *pHeaderTableToDelete)
  126. {
  127. int nRetVal = 0;
  128. /*call the Driver ioctl in order to remove header*/
  129. nRetVal = ioctl(m_fd, IPA_IOC_DEL_HDR , pHeaderTableToDelete);
  130. LOG_IOCTL_RETURN_VALUE(nRetVal);
  131. return (-1 != nRetVal);
  132. }
  133. bool HeaderInsertion::DeleteHeader(const string& name){
  134. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  135. return false;
  136. }
  137. int hdl = GetHeaderHandle(name);
  138. if(hdl == -1){
  139. return false;
  140. }
  141. struct ipa_ioc_del_hdr *iocD = static_cast<struct ipa_ioc_del_hdr*>(calloc(1, sizeof(*iocD) + sizeof(struct ipa_hdr_del)));
  142. if(!iocD){
  143. return false;
  144. }
  145. iocD->commit = 1;
  146. iocD->num_hdls = 1;
  147. struct ipa_hdr_del *h = &iocD->hdl[0];
  148. h->hdl = hdl;
  149. h->status = -1;
  150. cout << "h->hdl=" << h->hdl << endl;
  151. if(!DeleteHeader(iocD)){
  152. free(iocD);
  153. return false;
  154. }
  155. free(iocD);
  156. return true;
  157. }
  158. bool HeaderInsertion::AddProcCtx(struct ipa_ioc_add_hdr_proc_ctx *procCtxTable)
  159. {
  160. int retval = 0;
  161. retval = ioctl(m_fd, IPA_IOC_ADD_HDR_PROC_CTX, procCtxTable);
  162. if (retval) {
  163. printf("%s(), failed adding ProcCtx rule table %p\n", __FUNCTION__, procCtxTable);
  164. return false;
  165. }
  166. printf("%s(), Added ProcCtx rule to table %p\n", __FUNCTION__, procCtxTable);
  167. return true;
  168. }
  169. bool HeaderInsertion::DeleteProcCtx(struct ipa_ioc_del_hdr_proc_ctx *procCtxTable)
  170. {
  171. int retval = 0;
  172. retval = ioctl(m_fd, IPA_IOC_DEL_HDR_PROC_CTX, procCtxTable);
  173. if (retval) {
  174. printf("%s(), failed deleting ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  175. return false;
  176. }
  177. printf("%s(), Deleted ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  178. return true;
  179. }
  180. bool HeaderInsertion::Commit()
  181. {
  182. int nRetVal = 0;
  183. nRetVal = ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  184. LOG_IOCTL_RETURN_VALUE(nRetVal);
  185. return true;
  186. }
  187. bool HeaderInsertion::Reset()
  188. {
  189. int nRetVal = 0;
  190. nRetVal = ioctl(m_fd, IPA_IOC_RESET_HDR);
  191. nRetVal |= ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  192. LOG_IOCTL_RETURN_VALUE(nRetVal);
  193. return true;
  194. }
  195. bool HeaderInsertion::GetHeaderHandle(struct ipa_ioc_get_hdr *pHeaderStruct)
  196. {
  197. int retval = 0;
  198. if (!DeviceNodeIsOpened())
  199. return false;
  200. retval = ioctl(m_fd, IPA_IOC_GET_HDR, pHeaderStruct);
  201. if (retval) {
  202. printf(
  203. "%s(), IPA_IOC_GET_HDR ioctl failed, routingTable =0x%p, retval=0x%x.\n"
  204. , __func__,
  205. pHeaderStruct,
  206. retval);
  207. return false;
  208. }
  209. printf(
  210. "%s(), IPA_IOC_GET_HDR ioctl issued to IPA header insertion block.\n",
  211. __func__);
  212. return true;
  213. }
  214. int HeaderInsertion::GetHeaderHandle(const string& name){
  215. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  216. return false;
  217. }
  218. struct ipa_ioc_get_hdr retHeader;
  219. memset(&retHeader, 0, sizeof(retHeader));
  220. strlcpy(retHeader.name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  221. retHeader.hdl = -1;
  222. printf("retHeader.name=%s\n", retHeader.name);
  223. if(!GetHeaderHandle(&retHeader)){
  224. cout << "GetHeaderHandle(&retHeader) Failed" << endl;
  225. return -1;
  226. }
  227. cout << "retHeader.hdl=" << retHeader.hdl << endl;
  228. return retHeader.hdl;
  229. }
  230. bool HeaderInsertion::CopyHeader(struct ipa_ioc_copy_hdr *pCopyHeaderStruct)
  231. {
  232. int retval = 0;
  233. if (!DeviceNodeIsOpened())
  234. return false;
  235. retval = ioctl(m_fd, IPA_IOC_COPY_HDR, pCopyHeaderStruct);
  236. if (retval) {
  237. printf(
  238. "%s(), IPA_IOC_COPY_HDR ioctl failed, retval=0x%x.\n",
  239. __func__,
  240. retval);
  241. return false;
  242. }
  243. printf(
  244. "%s(), IPA_IOC_COPY_HDR ioctl issued to IPA header insertion block.\n",
  245. __func__);
  246. return true;
  247. }