RoutingDriverWrapper.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2017,2020 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 "RoutingDriverWrapper.h"
  68. #include "TestsUtils.h"
  69. bool RoutingDriverWrapper::AddRoutingRule(struct ipa_ioc_add_rt_rule *ruleTable)
  70. {
  71. int retval = 0;
  72. if (!DeviceNodeIsOpened())
  73. return false;
  74. retval = ioctl(m_fd, IPA_IOC_ADD_RT_RULE, ruleTable);
  75. if (retval) {
  76. printf("%s(), failed adding routing rule table %p\n", __FUNCTION__, ruleTable);
  77. return false;
  78. }
  79. printf("%s(), Added routing rule table %p\n", __FUNCTION__, ruleTable);
  80. return true;
  81. }
  82. bool RoutingDriverWrapper::AddRoutingRule(struct ipa_ioc_add_rt_rule_v2 *ruleTable_v2)
  83. {
  84. int retval = 0;
  85. if (!DeviceNodeIsOpened())
  86. return false;
  87. retval = ioctl(m_fd, IPA_IOC_ADD_RT_RULE_V2, ruleTable_v2);
  88. if (retval) {
  89. printf("%s(), failed adding routing rule table %p\n", __FUNCTION__, ruleTable_v2);
  90. return false;
  91. }
  92. printf("%s(), Added routing rule table %p\n", __FUNCTION__, ruleTable_v2);
  93. return true;
  94. }
  95. bool RoutingDriverWrapper::DeleteRoutingRule(struct ipa_ioc_del_rt_rule *ruleTable)
  96. {
  97. int retval = 0;
  98. if (!DeviceNodeIsOpened())
  99. return false;
  100. retval = ioctl(m_fd, IPA_IOC_DEL_RT_RULE, ruleTable);
  101. if (retval) {
  102. printf("%s(), failed deleting routing rule table %p\n", __FUNCTION__, ruleTable);
  103. return false;
  104. }
  105. printf("%s(), Deleted routing rule table %p\n", __FUNCTION__, ruleTable);
  106. return true;
  107. }
  108. bool RoutingDriverWrapper::Commit(enum ipa_ip_type ip)
  109. {
  110. int retval = 0;
  111. if (!DeviceNodeIsOpened())
  112. return false;
  113. retval = ioctl(m_fd, IPA_IOC_COMMIT_RT, ip);
  114. if (retval) {
  115. printf("%s(), failed commiting routing rules.\n", __FUNCTION__);
  116. return false;
  117. }
  118. printf("%s(), Commited routing rules to IPA HW.\n", __FUNCTION__);
  119. return true;
  120. }
  121. bool RoutingDriverWrapper::Reset(enum ipa_ip_type ip)
  122. {
  123. int retval = 0;
  124. if (!DeviceNodeIsOpened())
  125. return false;
  126. retval = ioctl(m_fd, IPA_IOC_RESET_RT, ip);
  127. retval |= ioctl(m_fd, IPA_IOC_COMMIT_RT, ip);
  128. if (retval) {
  129. printf("%s(), failed reseting routing block.\n", __FUNCTION__);
  130. return false;
  131. }
  132. printf("%s(), Reset command issued to IPA routing block.\n", __FUNCTION__);
  133. return true;
  134. }
  135. bool RoutingDriverWrapper::GetRoutingTable(struct ipa_ioc_get_rt_tbl *routingTable)
  136. {
  137. int retval = 0;
  138. if (!DeviceNodeIsOpened())
  139. return false;
  140. retval = ioctl(m_fd, IPA_IOC_GET_RT_TBL, routingTable);
  141. if (retval) {
  142. printf("%s(), IPA_IOCTL_GET_RT_TBL ioctl failed, routingTable =0x%p, retval=0x%x.\n", __FUNCTION__, routingTable, retval);
  143. return false;
  144. }
  145. printf("%s(), IPA_IOCTL_GET_RT_TBL ioctl issued to IPA routing block.\n", __FUNCTION__);
  146. return true;
  147. }
  148. bool RoutingDriverWrapper::PutRoutingTable(uint32_t routingTableHandle)
  149. {
  150. int retval = 0;
  151. if (!DeviceNodeIsOpened())
  152. return false;
  153. retval = ioctl(m_fd, IPA_IOC_PUT_RT_TBL, routingTableHandle);
  154. if (retval) {
  155. printf("%s(), IPA_IOCTL_PUT_RT_TBL ioctl failed.\n", __FUNCTION__);
  156. return false;
  157. }
  158. printf("%s(), IPA_IOCTL_PUT_RT_TBL ioctl issued to IPA routing block.\n", __FUNCTION__);
  159. return true;
  160. }
  161. bool RoutingDriverWrapper::SetNatConntrackExcRoutingTable(uint32_t routingTableHandle, bool nat_or_conntrack)
  162. {
  163. int retval = 0;
  164. if (!DeviceNodeIsOpened())
  165. return false;
  166. if (nat_or_conntrack)
  167. retval = ioctl(m_fd, IPA_IOC_SET_NAT_EXC_RT_TBL_IDX, routingTableHandle);
  168. else
  169. retval = ioctl(m_fd, IPA_IOC_SET_CONN_TRACK_EXC_RT_TBL_IDX, routingTableHandle);
  170. if (retval) {
  171. printf("%s(), IPA_IOC_SET_CONN_TRACK_EXC_RT_TBL_IDX ioctl failed.\n", __FUNCTION__);
  172. return false;
  173. }
  174. printf("%s(), %s ioctl issued to IPA routing block.\n", __FUNCTION__,(nat_or_conntrack) ?
  175. "IPA_IOC_SET_NAT_EXC_RT_TBL_IDX" : "IPA_IOC_SET_CONN_TRACK_EXC_RT_TBL_IDX");
  176. return true;
  177. }