RoutingDriverWrapper.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include "RoutingDriverWrapper.h"
  34. #include "TestsUtils.h"
  35. const char* RoutingDriverWrapper::DEVICE_NAME = "/dev/ipa";
  36. RoutingDriverWrapper::RoutingDriverWrapper()
  37. {
  38. m_fd = open(DEVICE_NAME, O_RDWR);
  39. if (0 == m_fd) {
  40. printf("Failed opening %s.\n", DEVICE_NAME);
  41. }
  42. }
  43. RoutingDriverWrapper::~RoutingDriverWrapper()
  44. {
  45. close(m_fd);
  46. }
  47. bool RoutingDriverWrapper::DeviceNodeIsOpened()
  48. {
  49. int res = fcntl(m_fd, F_GETFL);
  50. if (m_fd > 0 && res >=0)
  51. return true;
  52. else
  53. return false;
  54. }
  55. bool RoutingDriverWrapper::AddRoutingRule(struct ipa_ioc_add_rt_rule *ruleTable)
  56. {
  57. int retval = 0;
  58. if (!DeviceNodeIsOpened())
  59. return false;
  60. retval = ioctl(m_fd, IPA_IOC_ADD_RT_RULE, ruleTable);
  61. if (retval) {
  62. printf("%s(), failed adding routing rule table %p\n", __FUNCTION__, ruleTable);
  63. return false;
  64. }
  65. printf("%s(), Added routing rule table %p\n", __FUNCTION__, ruleTable);
  66. return true;
  67. }
  68. bool RoutingDriverWrapper::AddRoutingRule(struct ipa_ioc_add_rt_rule_v2 *ruleTable_v2)
  69. {
  70. int retval = 0;
  71. if (!DeviceNodeIsOpened())
  72. return false;
  73. retval = ioctl(m_fd, IPA_IOC_ADD_RT_RULE_V2, ruleTable_v2);
  74. if (retval) {
  75. printf("%s(), failed adding routing rule table %p\n", __FUNCTION__, ruleTable_v2);
  76. return false;
  77. }
  78. printf("%s(), Added routing rule table %p\n", __FUNCTION__, ruleTable_v2);
  79. return true;
  80. }
  81. bool RoutingDriverWrapper::DeleteRoutingRule(struct ipa_ioc_del_rt_rule *ruleTable)
  82. {
  83. int retval = 0;
  84. if (!DeviceNodeIsOpened())
  85. return false;
  86. retval = ioctl(m_fd, IPA_IOC_DEL_RT_RULE, ruleTable);
  87. if (retval) {
  88. printf("%s(), failed deleting routing rule table %p\n", __FUNCTION__, ruleTable);
  89. return false;
  90. }
  91. printf("%s(), Deleted routing rule table %p\n", __FUNCTION__, ruleTable);
  92. return true;
  93. }
  94. bool RoutingDriverWrapper::Commit(enum ipa_ip_type ip)
  95. {
  96. int retval = 0;
  97. if (!DeviceNodeIsOpened())
  98. return false;
  99. retval = ioctl(m_fd, IPA_IOC_COMMIT_RT, ip);
  100. if (retval) {
  101. printf("%s(), failed commiting routing rules.\n", __FUNCTION__);
  102. return false;
  103. }
  104. printf("%s(), Commited routing rules to IPA HW.\n", __FUNCTION__);
  105. return true;
  106. }
  107. bool RoutingDriverWrapper::Reset(enum ipa_ip_type ip)
  108. {
  109. int retval = 0;
  110. if (!DeviceNodeIsOpened())
  111. return false;
  112. retval = ioctl(m_fd, IPA_IOC_RESET_RT, ip);
  113. retval |= ioctl(m_fd, IPA_IOC_COMMIT_RT, ip);
  114. if (retval) {
  115. printf("%s(), failed reseting routing block.\n", __FUNCTION__);
  116. return false;
  117. }
  118. printf("%s(), Reset command issued to IPA routing block.\n", __FUNCTION__);
  119. return true;
  120. }
  121. bool RoutingDriverWrapper::GetRoutingTable(struct ipa_ioc_get_rt_tbl *routingTable)
  122. {
  123. int retval = 0;
  124. if (!DeviceNodeIsOpened())
  125. return false;
  126. retval = ioctl(m_fd, IPA_IOC_GET_RT_TBL, routingTable);
  127. if (retval) {
  128. printf("%s(), IPA_IOCTL_GET_RT_TBL ioctl failed, routingTable =0x%p, retval=0x%x.\n", __FUNCTION__, routingTable, retval);
  129. return false;
  130. }
  131. printf("%s(), IPA_IOCTL_GET_RT_TBL ioctl issued to IPA routing block.\n", __FUNCTION__);
  132. return true;
  133. }
  134. bool RoutingDriverWrapper::PutRoutingTable(uint32_t routingTableHandle)
  135. {
  136. int retval = 0;
  137. if (!DeviceNodeIsOpened())
  138. return false;
  139. retval = ioctl(m_fd, IPA_IOC_PUT_RT_TBL, routingTableHandle);
  140. if (retval) {
  141. printf("%s(), IPA_IOCTL_PUT_RT_TBL ioctl failed.\n", __FUNCTION__);
  142. return false;
  143. }
  144. printf("%s(), IPA_IOCTL_PUT_RT_TBL ioctl issued to IPA routing block.\n", __FUNCTION__);
  145. return true;
  146. }