HeaderInsertion.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <cstring>
  35. #include "HeaderInsertion.h"
  36. #include "TestsUtils.h"
  37. /*All interaction through the driver are
  38. * made through this inode.
  39. */
  40. static const char* DEVICE_NAME = "/dev/ipa";
  41. #define LOG_IOCTL_RETURN_VALUE(nRetVal) \
  42. printf("%s()- %s\n", __func__, \
  43. (-1 == nRetVal) ? "Fail" : "Success");
  44. HeaderInsertion::HeaderInsertion()
  45. {
  46. m_fd = open(DEVICE_NAME, O_RDWR);
  47. if (-1 == m_fd)
  48. {
  49. printf(
  50. "Failed to open %s in HeaderInsertion test application constructor.\n",
  51. DEVICE_NAME);
  52. }
  53. }
  54. HeaderInsertion::~HeaderInsertion()
  55. {
  56. if (-1 != m_fd)
  57. {
  58. close(m_fd);
  59. }
  60. }
  61. bool HeaderInsertion::DeviceNodeIsOpened()
  62. {
  63. return (-1 != m_fd);
  64. }
  65. bool HeaderInsertion::AddHeader(struct ipa_ioc_add_hdr *pHeaderTableToAdd)
  66. {
  67. int nRetVal = 0;
  68. /*call the Driver ioctl in order to add header*/
  69. nRetVal = ioctl(m_fd, IPA_IOC_ADD_HDR, pHeaderTableToAdd);
  70. LOG_IOCTL_RETURN_VALUE(nRetVal);
  71. return (-1 != nRetVal);
  72. }
  73. bool HeaderInsertion::addHeaderHpc(const string& name, uint8_t* header, const size_t headerLen, bool isPartial, enum ipa_client_type ipaClient){
  74. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  75. return false;
  76. }
  77. int fd = open(CONFIGURATION_NODE_PATH, O_RDONLY);
  78. if (fd < 0) {
  79. cout << "failed to open " << CONFIGURATION_NODE_PATH << endl;
  80. return false;
  81. }
  82. struct ipa_ioc_add_hdr *iocH = static_cast<struct ipa_ioc_add_hdr*>(calloc(1, sizeof(*iocH) + sizeof(struct ipa_hdr_add)));
  83. if(!iocH){
  84. return false;
  85. }
  86. iocH->commit = 1;
  87. iocH->num_hdrs = 1;
  88. struct ipa_hdr_add *h = &iocH->hdr[0];
  89. strlcpy(h->name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  90. memcpy(h->hdr, header, headerLen);
  91. h->hdr_len = headerLen;
  92. h->hdr_hdl = -1;
  93. h->status = -1;
  94. h->is_partial = isPartial;
  95. cout << "h->name=" << h->name << ", h->is_partial=" << h->is_partial << endl;
  96. int result = ioctl(fd, IPA_TEST_IOC_ADD_HDR_HPC, iocH);
  97. if(result || h->status){
  98. free(iocH);
  99. close(fd);
  100. return false;
  101. }
  102. cout << "result=" << result << ", status=" << h->status << ", ipaClient=" << ipaClient << endl;
  103. struct ipa_pkt_init_ex_hdr_ofst_set lookup;
  104. lookup.ep = ipaClient;
  105. strlcpy(lookup.name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  106. result = ioctl(fd, IPA_TEST_IOC_PKT_INIT_EX_SET_HDR_OFST , &lookup);
  107. if (result) {
  108. free(iocH);
  109. close(fd);
  110. return false;
  111. }
  112. free(iocH);
  113. close(fd);
  114. return true;
  115. }
  116. bool HeaderInsertion::DeleteHeader(struct ipa_ioc_del_hdr *pHeaderTableToDelete)
  117. {
  118. int nRetVal = 0;
  119. /*call the Driver ioctl in order to remove header*/
  120. nRetVal = ioctl(m_fd, IPA_IOC_DEL_HDR , pHeaderTableToDelete);
  121. LOG_IOCTL_RETURN_VALUE(nRetVal);
  122. return (-1 != nRetVal);
  123. }
  124. bool HeaderInsertion::DeleteHeader(const string& name){
  125. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  126. return false;
  127. }
  128. int hdl = GetHeaderHandle(name);
  129. if(hdl == -1){
  130. return false;
  131. }
  132. struct ipa_ioc_del_hdr *iocD = static_cast<struct ipa_ioc_del_hdr*>(calloc(1, sizeof(*iocD) + sizeof(struct ipa_hdr_del)));
  133. if(!iocD){
  134. return false;
  135. }
  136. iocD->commit = 1;
  137. iocD->num_hdls = 1;
  138. struct ipa_hdr_del *h = &iocD->hdl[0];
  139. h->hdl = hdl;
  140. h->status = -1;
  141. cout << "h->hdl=" << h->hdl << endl;
  142. if(!DeleteHeader(iocD)){
  143. free(iocD);
  144. return false;
  145. }
  146. free(iocD);
  147. return true;
  148. }
  149. bool HeaderInsertion::AddProcCtx(struct ipa_ioc_add_hdr_proc_ctx *procCtxTable)
  150. {
  151. int retval = 0;
  152. retval = ioctl(m_fd, IPA_IOC_ADD_HDR_PROC_CTX, procCtxTable);
  153. if (retval) {
  154. printf("%s(), failed adding ProcCtx rule table %p\n", __FUNCTION__, procCtxTable);
  155. return false;
  156. }
  157. printf("%s(), Added ProcCtx rule to table %p\n", __FUNCTION__, procCtxTable);
  158. return true;
  159. }
  160. bool HeaderInsertion::DeleteProcCtx(struct ipa_ioc_del_hdr_proc_ctx *procCtxTable)
  161. {
  162. int retval = 0;
  163. retval = ioctl(m_fd, IPA_IOC_DEL_HDR_PROC_CTX, procCtxTable);
  164. if (retval) {
  165. printf("%s(), failed deleting ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  166. return false;
  167. }
  168. printf("%s(), Deleted ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  169. return true;
  170. }
  171. bool HeaderInsertion::Commit()
  172. {
  173. int nRetVal = 0;
  174. nRetVal = ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  175. LOG_IOCTL_RETURN_VALUE(nRetVal);
  176. return true;
  177. }
  178. bool HeaderInsertion::Reset()
  179. {
  180. int nRetVal = 0;
  181. nRetVal = ioctl(m_fd, IPA_IOC_RESET_HDR);
  182. nRetVal |= ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  183. LOG_IOCTL_RETURN_VALUE(nRetVal);
  184. return true;
  185. }
  186. bool HeaderInsertion::GetHeaderHandle(struct ipa_ioc_get_hdr *pHeaderStruct)
  187. {
  188. int retval = 0;
  189. if (!DeviceNodeIsOpened())
  190. return false;
  191. retval = ioctl(m_fd, IPA_IOC_GET_HDR, pHeaderStruct);
  192. if (retval) {
  193. printf(
  194. "%s(), IPA_IOC_GET_HDR ioctl failed, routingTable =0x%p, retval=0x%x.\n"
  195. , __func__,
  196. pHeaderStruct,
  197. retval);
  198. return false;
  199. }
  200. printf(
  201. "%s(), IPA_IOC_GET_HDR ioctl issued to IPA header insertion block.\n",
  202. __func__);
  203. return true;
  204. }
  205. int HeaderInsertion::GetHeaderHandle(const string& name){
  206. if(name.empty() || name.size() >= IPA_RESOURCE_NAME_MAX){
  207. return false;
  208. }
  209. struct ipa_ioc_get_hdr retHeader;
  210. memset(&retHeader, 0, sizeof(retHeader));
  211. strlcpy(retHeader.name, name.c_str(), IPA_RESOURCE_NAME_MAX);
  212. retHeader.hdl = -1;
  213. printf("retHeader.name=%s\n", retHeader.name);
  214. if(!GetHeaderHandle(&retHeader)){
  215. cout << "GetHeaderHandle(&retHeader) Failed" << endl;
  216. return -1;
  217. }
  218. cout << "retHeader.hdl=" << retHeader.hdl << endl;
  219. return retHeader.hdl;
  220. }
  221. bool HeaderInsertion::CopyHeader(struct ipa_ioc_copy_hdr *pCopyHeaderStruct)
  222. {
  223. int retval = 0;
  224. if (!DeviceNodeIsOpened())
  225. return false;
  226. retval = ioctl(m_fd, IPA_IOC_COPY_HDR, pCopyHeaderStruct);
  227. if (retval) {
  228. printf(
  229. "%s(), IPA_IOC_COPY_HDR ioctl failed, retval=0x%x.\n",
  230. __func__,
  231. retval);
  232. return false;
  233. }
  234. printf(
  235. "%s(), IPA_IOC_COPY_HDR ioctl issued to IPA header insertion block.\n",
  236. __func__);
  237. return true;
  238. }