HeaderInsertion.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "HeaderInsertion.h"
  34. /*All interaction through the driver are
  35. * made through this inode.
  36. */
  37. static const char* DEVICE_NAME = "/dev/ipa";
  38. #define LOG_IOCTL_RETURN_VALUE(nRetVal) \
  39. printf("%s()- %s\n", __func__, \
  40. (-1 == nRetVal) ? "Fail" : "Success");
  41. HeaderInsertion::HeaderInsertion()
  42. {
  43. m_fd = open(DEVICE_NAME, O_RDWR);
  44. if (-1 == m_fd)
  45. {
  46. printf(
  47. "Failed to open %s in HeaderInsertion test application constructor.\n",
  48. DEVICE_NAME);
  49. }
  50. }
  51. HeaderInsertion::~HeaderInsertion()
  52. {
  53. if (-1 != m_fd)
  54. {
  55. close(m_fd);
  56. }
  57. }
  58. bool HeaderInsertion::DeviceNodeIsOpened()
  59. {
  60. return (-1 != m_fd);
  61. }
  62. bool HeaderInsertion::AddHeader(struct ipa_ioc_add_hdr *pHeaderTableToAdd)
  63. {
  64. int nRetVal = 0;
  65. /*call the Driver ioctl in order to add header*/
  66. nRetVal = ioctl(m_fd, IPA_IOC_ADD_HDR, pHeaderTableToAdd);
  67. LOG_IOCTL_RETURN_VALUE(nRetVal);
  68. return (-1 != nRetVal);
  69. }
  70. bool HeaderInsertion::DeleteHeader(struct ipa_ioc_del_hdr *pHeaderTableToDelete)
  71. {
  72. int nRetVal = 0;
  73. /*call the Driver ioctl in order to remove header*/
  74. nRetVal = ioctl(m_fd, IPA_IOC_DEL_HDR , pHeaderTableToDelete);
  75. LOG_IOCTL_RETURN_VALUE(nRetVal);
  76. return (-1 != nRetVal);
  77. }
  78. bool HeaderInsertion::AddProcCtx(struct ipa_ioc_add_hdr_proc_ctx *procCtxTable)
  79. {
  80. int retval = 0;
  81. retval = ioctl(m_fd, IPA_IOC_ADD_HDR_PROC_CTX, procCtxTable);
  82. if (retval) {
  83. printf("%s(), failed adding ProcCtx rule table %p\n", __FUNCTION__, procCtxTable);
  84. return false;
  85. }
  86. printf("%s(), Added ProcCtx rule to table %p\n", __FUNCTION__, procCtxTable);
  87. return true;
  88. }
  89. bool HeaderInsertion::DeleteProcCtx(struct ipa_ioc_del_hdr_proc_ctx *procCtxTable)
  90. {
  91. int retval = 0;
  92. retval = ioctl(m_fd, IPA_IOC_DEL_HDR_PROC_CTX, procCtxTable);
  93. if (retval) {
  94. printf("%s(), failed deleting ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  95. return false;
  96. }
  97. printf("%s(), Deleted ProcCtx rule in table %p\n", __FUNCTION__, procCtxTable);
  98. return true;
  99. }
  100. bool HeaderInsertion::Commit()
  101. {
  102. int nRetVal = 0;
  103. nRetVal = ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  104. LOG_IOCTL_RETURN_VALUE(nRetVal);
  105. return true;
  106. }
  107. bool HeaderInsertion::Reset()
  108. {
  109. int nRetVal = 0;
  110. nRetVal = ioctl(m_fd, IPA_IOC_RESET_HDR);
  111. nRetVal |= ioctl(m_fd, IPA_IOC_COMMIT_HDR);
  112. LOG_IOCTL_RETURN_VALUE(nRetVal);
  113. return true;
  114. }
  115. bool HeaderInsertion::GetHeaderHandle(struct ipa_ioc_get_hdr *pHeaderStruct)
  116. {
  117. int retval = 0;
  118. if (!DeviceNodeIsOpened())
  119. return false;
  120. retval = ioctl(m_fd, IPA_IOC_GET_HDR, pHeaderStruct);
  121. if (retval) {
  122. printf(
  123. "%s(), IPA_IOC_GET_HDR ioctl failed, routingTable =0x%p, retval=0x%x.\n"
  124. , __func__,
  125. pHeaderStruct,
  126. retval);
  127. return false;
  128. }
  129. printf(
  130. "%s(), IPA_IOC_GET_HDR ioctl issued to IPA header insertion block.\n",
  131. __func__);
  132. return true;
  133. }
  134. bool HeaderInsertion::CopyHeader(struct ipa_ioc_copy_hdr *pCopyHeaderStruct)
  135. {
  136. int retval = 0;
  137. if (!DeviceNodeIsOpened())
  138. return false;
  139. retval = ioctl(m_fd, IPA_IOC_COPY_HDR, pCopyHeaderStruct);
  140. if (retval) {
  141. printf(
  142. "%s(), IPA_IOC_COPY_HDR ioctl failed, retval=0x%x.\n",
  143. __func__,
  144. retval);
  145. return false;
  146. }
  147. printf(
  148. "%s(), IPA_IOC_COPY_HDR ioctl issued to IPA header insertion block.\n",
  149. __func__);
  150. return true;
  151. }