TestsUtils.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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 <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <sys/ioctl.h>
  33. #include <string.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <time.h>
  37. #include <errno.h>
  38. #include "TestsUtils.h"
  39. #include "InterfaceAbstraction.h"
  40. #include "Constants.h"
  41. #include "Pipe.h"
  42. using namespace std;
  43. ///////////////////////////////////////////////////////////////////////////////
  44. extern Logger g_Logger;
  45. static uint8_t IPv4Packet[IP4_PACKET_SIZE] = {
  46. 0x45, 0x00, 0x00, 0x2e,
  47. 0x00, 0x00, 0x40, 0x00,
  48. 0xff, 0x06, 0xf5, 0xfd,// Protocol = 06 (TCP)
  49. 0xc0, 0xa8, 0x02, 0x13,// IPv4 SRC Addr 192.168.2.19
  50. 0xc0, 0xa8, 0x02, 0x68,// IPv4 DST Addr 192.168.2.104
  51. 0x04, 0x57, 0x08, 0xae,
  52. 0x00, 0x00, 0x30, 0x34,
  53. 0x00, 0x00, 0x00, 0x50,
  54. 0x50, 0xc1, 0x40, 0x00,
  55. 0xab, 0xc9, 0x00, 0x00,
  56. 0x00, 0xaa, 0xaa, 0xaa,
  57. 0xbb, 0xbb, 0xbb, 0xbb,
  58. 0xbb, 0xbb, 0xbb, 0xbb,
  59. 0xbb, 0xbb, 0xbb, 0xbb,
  60. 0xbb, 0xbb, 0xbb, 0xbb,
  61. 0xbb, 0xbb, 0xbb, 0xbb,
  62. 0xbb, 0xbb, 0xbb, 0xbb,
  63. 0xbb, 0xbb
  64. };
  65. static uint8_t IPv6Packet[] = {
  66. 0x60, 0x00, 0x00, 0x00,
  67. 0x00, 0x1c, 0x06, 0x01, // Protocol = 6 (TCP)
  68. 0xfe, 0x80, 0x00, 0x00, // source address (16B)
  69. 0x00, 0x00, 0x00, 0x00,
  70. 0xd9, 0xf9, 0xce, 0x5e,
  71. 0x02, 0xec, 0x32, 0x99,
  72. 0xff, 0x02, 0x00, 0x00, // dst address (16B)
  73. 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x0c,
  76. 0x12, 0x34, 0x12, 0x34, // port src = 0x1234 dest = 0x1234
  77. 0x00, 0x14, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00,
  79. 0x60, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, // options
  82. 0xda, 0x7a, 0xda, 0x7a // payload
  83. };
  84. static uint8_t IPv6PacketFragExtHdr[] = {
  85. 0x60, 0x00, 0x00, 0x00,
  86. 0x00, 0x0c, 0x2C, 0x01, // Next header = FRAGMENT HEADER(44)
  87. 0xfe, 0x80, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00,
  89. 0xd9, 0xf9, 0xce, 0x5e,
  90. 0x02, 0xec, 0x32, 0x99,
  91. 0xff, 0x02, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x0c,
  95. 0x06, 0x00, 0x00, 0x00, // fragment header, Protocol = 6 (TCP)
  96. 0x00, 0x00, 0x00, 0x00,
  97. 0x12, 0x34, 0x12, 0x34, // port src = 0x1234 dest = 0x1234
  98. 0x00, 0x14, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00,
  100. 0x60, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x00,
  103. 0xda, 0x7a, 0xda, 0x7a // payload
  104. };
  105. static const uint8_t Eth2IPv4Packet[] =
  106. {
  107. // ETH2 - 14 bytes
  108. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x11, // ETH2 DST
  109. 0x22, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, // ETH2 SRC
  110. 0x08, 0x00, // ETH2 TYPE IPv4 - ETH_P_IP 0x0800
  111. // IPv4
  112. 0x45, 0x00, 0x00, 0x2e,
  113. 0x00, 0x00, 0x40, 0x00,
  114. 0xff, 0x06, 0xf5, 0xfd, // Protocol = 06 (TCP)
  115. 0xc0, 0xa8, 0x02, 0x13, // IPv4 SRC Addr 192.168.2.19
  116. 0xc0, 0xa8, 0x02, 0x68, // IPv4 DST Addr 192.168.2.104
  117. 0x04, 0x57, 0x08, 0xae,
  118. 0x00, 0x00, 0x30, 0x34,
  119. 0x00, 0x00, 0x00, 0x50,
  120. 0x50, 0xc1, 0x40, 0x00,
  121. 0xab, 0xc9, 0x00, 0x00,
  122. 0x00
  123. };
  124. static const uint8_t Eth2IPv6Packet[] =
  125. {
  126. // ETH2 - 14 bytes
  127. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x11, // ETH2 DST
  128. 0x22, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, // ETH2 SRC
  129. 0x86, 0xdd, // ETH2 TYPE IPv6 - ETH_P_IPV6 x86DD
  130. // IPv6
  131. 0x60, 0x00, 0x00, 0x00,
  132. 0x02, 0x12, 0x11, 0x01,
  133. 0xfe, 0x80, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00,
  135. 0xd9, 0xf9, 0xce, 0x5e,
  136. 0x02, 0xec, 0x32, 0x99,
  137. 0xff, 0x02, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x0c
  141. };
  142. static const uint8_t WLANEth2IPv4Packet[] =
  143. {
  144. // WLAN
  145. 0xa1, 0xb2, 0xc3, 0xd4, // WLAN hdr
  146. // ETH2 - 14 bytes
  147. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x11, // ETH2 DST
  148. 0x22, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, // ETH2 SRC
  149. 0x08, 0x00, // ETH2 TYPE IPv4 - ETH_P_IP 0x0800
  150. // IPv4
  151. 0x45, 0x00, 0x00, 0x2e,
  152. 0x00, 0x00, 0x40, 0x00,
  153. 0xff, 0x06, 0xf5, 0xfd, // Protocol = 06 (TCP)
  154. 0xc0, 0xa8, 0x02, 0x13, // IPv4 SRC Addr 192.168.2.19
  155. 0xc0, 0xa8, 0x02, 0x68, // IPv4 DST Addr 192.168.2.104
  156. 0x04, 0x57, 0x08, 0xae,
  157. 0x00, 0x00, 0x30, 0x34,
  158. 0x00, 0x00, 0x00, 0x50,
  159. 0x50, 0xc1, 0x40, 0x00,
  160. 0xab, 0xc9, 0x00, 0x00,
  161. 0x00
  162. };
  163. static const uint8_t WLAN802_3IPv4Packet[] =
  164. {
  165. // WLAN
  166. 0xa1, 0xb2, 0xc3, 0xd4, // WLAN hdr
  167. // 802_3 - 26 bytes
  168. 0xa5, 0xb6, 0xc7, 0xd8, // ROME proprietary header
  169. 0xa0, 0xb1, 0xc2, 0xd3, 0xe4, 0x33, // 802_3 DST
  170. 0x44, 0xb2, 0xc3, 0xd4, 0xe5, 0xf6, // 802_3 SRC
  171. 0x00, IP4_PACKET_SIZE, // 802_3 length
  172. 0x04, 0x15, 0x26, 0x37, 0x48, 0x59, // LLC/SNAP
  173. 0x08, 0x00, // Ethrtype - 0x0800
  174. // IPv4
  175. 0x45, 0x00, 0x00, 0x2e,
  176. 0x00, 0x00, 0x40, 0x00,
  177. 0xff, 0x06, 0xf5, 0xfd, // Protocol = 06 (TCP)
  178. 0xc0, 0xa8, 0x02, 0x13, // IPv4 SRC Addr 192.168.2.19
  179. 0xc0, 0xa8, 0x02, 0x68, // IPv4 DST Addr 192.168.2.104
  180. 0x04, 0x57, 0x08, 0xae,
  181. 0x00, 0x00, 0x30, 0x34,
  182. 0x00, 0x00, 0x00, 0x50,
  183. 0x50, 0xc1, 0x40, 0x00,
  184. 0xab, 0xc9, 0x00, 0x00,
  185. 0x00
  186. };
  187. static uint8_t IPv4_TCP_No_Payload_Packet[] = {
  188. 0x45, 0x00, 0x00, 0x28, // Total length is 40
  189. 0x00, 0x00, 0x40, 0x00,
  190. 0xff, 0x06, 0xf5, 0xfd, // Protocol = 06 (TCP)
  191. 0xc0, 0xa8, 0x02, 0x13, // IPv4 SRC Addr 192.168.2.19
  192. 0xc0, 0xa8, 0x02, 0xc8, // IPv4 DST Addr 192.168.2.200
  193. 0x04, 0x57, 0x08, 0xae,
  194. 0x00, 0x00, 0x30, 0x34,
  195. 0x00, 0x00, 0x00, 0x50,
  196. 0x50, 0xc1, 0x40, 0x00,
  197. 0xab, 0xc9, 0x00, 0x00,
  198. };
  199. static uint8_t IPv6_TCP_No_Payload_Packet[] = {
  200. 0x60, 0x00, 0x00, 0x00,
  201. 0x00, 0x14, 0x06, 0x01, // Payload is 20 Bytes (TCP header only)
  202. 0xfe, 0x80, 0x00, 0x00, // SRC Addr
  203. 0x00, 0x00, 0x00, 0x00,
  204. 0xd9, 0xf9, 0xce, 0x5e,
  205. 0x02, 0xec, 0x32, 0x99,
  206. 0xff, 0x02, 0x00, 0x00, // DST Addr
  207. 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x4b,
  210. 0x04, 0x57, 0x08, 0xae, // TCP header
  211. 0x00, 0x00, 0x30, 0x34,
  212. 0x00, 0x00, 0x00, 0x50,
  213. 0x50, 0xc1, 0x40, 0x00,
  214. 0xab, 0xc9, 0x00, 0x00,
  215. };
  216. static const uint8_t Eth2IPv4Packet802_1Q_tag[] =
  217. {
  218. // ETH2 + 8021Q - 14 + 4 bytes
  219. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x11, // ETH2 DST
  220. 0x22, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, // ETH2 SRC
  221. 0x81, 0x00, 0x20, 0x03, // 802.1Q header with VLAN ID = 3
  222. 0x08, 0x00, // ETH2 TYPE IPv4 - ETH_P_IP 0x0800
  223. // IPv4
  224. 0x45, 0x00, 0x00, 0x2e,
  225. 0x00, 0x00, 0x40, 0x00,
  226. 0xff, 0x06, 0xf5, 0xfd, // Protocol = 06 (TCP)
  227. 0xc0, 0xa8, 0x02, 0x13, // IPv4 SRC Addr 192.168.2.19
  228. 0xc0, 0xa8, 0x02, 0x68, // IPv4 DST Addr 192.168.2.104
  229. 0x04, 0x57, 0x08, 0xae,
  230. 0x00, 0x00, 0x30, 0x34,
  231. 0x00, 0x00, 0x00, 0x50,
  232. 0x50, 0xc1, 0x40, 0x00,
  233. 0xab, 0xc9, 0x00, 0x00,
  234. 0x00
  235. };
  236. static struct ipa_test_config_header *current_configuration = NULL;
  237. ///////////////////////////////////////////////////////////////////////////////
  238. /**
  239. *@brief Function loads a default IPv4 / IPv6 Packet
  240. *
  241. *@param [in] eIP - Type of Packet to load (IPA_IP_v4 / IPA_IP_v6)
  242. *@param [in] pBuffer - pointer to the destination buffer
  243. *@param [in,out] nMaxSize - The size of the buffer.*Upon function return,
  244. * the total number of bytes copied will be stored in this parameter.
  245. *@return boolean indicating whether the operation completed successfully or not.
  246. *@details Function loads a default IPv4 / IPv6 packet into pBuffer.
  247. */
  248. bool LoadDefaultPacket(
  249. enum ipa_ip_type eIP,
  250. enum ipv6_ext_hdr_type extHdrType,
  251. uint8_t *pBuffer,
  252. size_t &nMaxSize)
  253. {
  254. if (IPA_IP_v4 == eIP) {
  255. if (nMaxSize < sizeof(IPv4Packet))
  256. {
  257. LOG_MSG_ERROR("Buffer is smaller than %d, no Data was copied.",sizeof(IPv4Packet));
  258. return false;
  259. }
  260. memcpy(pBuffer,IPv4Packet, sizeof(IPv4Packet));
  261. nMaxSize = sizeof(IPv4Packet);
  262. return true;
  263. } else {
  264. if (extHdrType == FRAGMENT)
  265. {
  266. if (nMaxSize < sizeof(IPv6PacketFragExtHdr))
  267. {
  268. LOG_MSG_ERROR("Buffer is smaller than %d, no Data was copied.",sizeof(IPv6PacketFragExtHdr));
  269. return false;
  270. }
  271. memcpy(pBuffer,IPv6PacketFragExtHdr, sizeof(IPv6PacketFragExtHdr));
  272. nMaxSize = sizeof(IPv6PacketFragExtHdr);
  273. }
  274. else
  275. {
  276. if (nMaxSize < sizeof(IPv6Packet))
  277. {
  278. LOG_MSG_ERROR("Buffer is smaller than %d, no Data was copied.",sizeof(IPv6Packet));
  279. return false;
  280. }
  281. memcpy(pBuffer,IPv6Packet, sizeof(IPv6Packet));
  282. nMaxSize = sizeof(IPv6Packet);
  283. }
  284. return true;
  285. }
  286. }
  287. bool LoadDefaultPacket(enum ipa_ip_type eIP, uint8_t *pBuffer, size_t &nMaxSize)
  288. {
  289. return LoadDefaultPacket(eIP, NONE, pBuffer, nMaxSize);
  290. }
  291. bool LoadNoPayloadPacket(enum ipa_ip_type eIP, uint8_t *pBuffer, size_t &nMaxSize)
  292. {
  293. if (IPA_IP_v4 == eIP) {
  294. if (nMaxSize < sizeof(IPv4_TCP_No_Payload_Packet))
  295. {
  296. LOG_MSG_ERROR("Buffer is smaller than %zu, no Data was copied.",
  297. sizeof(IPv4_TCP_No_Payload_Packet));
  298. return false;
  299. }
  300. memcpy(pBuffer, IPv4_TCP_No_Payload_Packet, sizeof(IPv4_TCP_No_Payload_Packet));
  301. nMaxSize = sizeof(IPv4_TCP_No_Payload_Packet);
  302. } else {
  303. if (nMaxSize < sizeof(IPv6_TCP_No_Payload_Packet))
  304. {
  305. LOG_MSG_ERROR("Buffer is smaller than %zu, no Data was copied.",
  306. sizeof(IPv6_TCP_No_Payload_Packet));
  307. return false;
  308. }
  309. memcpy(pBuffer, IPv6_TCP_No_Payload_Packet, sizeof(IPv6_TCP_No_Payload_Packet));
  310. nMaxSize = sizeof(IPv6_TCP_No_Payload_Packet);
  311. }
  312. return true;
  313. }
  314. bool LoadDefaultEth2Packet(
  315. enum ipa_ip_type eIP,
  316. uint8_t *pBuffer,
  317. size_t &nMaxSize)
  318. {
  319. if (IPA_IP_v4 == eIP) {
  320. if (nMaxSize < sizeof(Eth2IPv4Packet))
  321. {
  322. LOG_MSG_ERROR(
  323. "Buffer is smaller than %d, "
  324. "no data was copied.",
  325. sizeof(Eth2IPv4Packet));
  326. return false;
  327. }
  328. memcpy(pBuffer,Eth2IPv4Packet,
  329. sizeof(Eth2IPv4Packet));
  330. nMaxSize = sizeof(Eth2IPv4Packet);
  331. }
  332. else
  333. {
  334. if (nMaxSize < sizeof(Eth2IPv6Packet))
  335. {
  336. LOG_MSG_ERROR(
  337. "Buffer is smaller than %d, "
  338. "no data was copied.",
  339. sizeof(Eth2IPv6Packet));
  340. return false;
  341. }
  342. memcpy(pBuffer,Eth2IPv6Packet,
  343. sizeof(Eth2IPv6Packet));
  344. nMaxSize = sizeof(Eth2IPv6Packet);
  345. }
  346. return true;
  347. }
  348. bool LoadDefaultWLANEth2Packet(
  349. enum ipa_ip_type eIP,
  350. uint8_t *pBuffer,
  351. size_t &nMaxSize)
  352. {
  353. if (IPA_IP_v4 == eIP) {
  354. if (nMaxSize < sizeof(WLANEth2IPv4Packet))
  355. {
  356. LOG_MSG_ERROR(
  357. "Buffer is smaller than %d, "
  358. "no data was copied.",
  359. sizeof(WLANEth2IPv4Packet));
  360. return false;
  361. }
  362. memcpy(pBuffer,WLANEth2IPv4Packet,
  363. sizeof(WLANEth2IPv4Packet));
  364. nMaxSize = sizeof(WLANEth2IPv4Packet);
  365. }
  366. else
  367. {
  368. LOG_MSG_ERROR("%s isn't implemented "
  369. "for IPv6 - do it yourself :-)",
  370. __FUNCTION__);
  371. return false;
  372. }
  373. return true;
  374. }
  375. bool LoadDefaultWLAN802_32Packet(
  376. enum ipa_ip_type eIP,
  377. uint8_t *pBuffer,
  378. size_t &nMaxSize)
  379. {
  380. if (IPA_IP_v4 == eIP) {
  381. if (nMaxSize < sizeof(WLAN802_3IPv4Packet))
  382. {
  383. LOG_MSG_ERROR(
  384. "Buffer is smaller than %d, "
  385. "no data was copied.",
  386. sizeof(WLAN802_3IPv4Packet));
  387. return false;
  388. }
  389. memcpy(pBuffer,WLAN802_3IPv4Packet,
  390. sizeof(WLAN802_3IPv4Packet));
  391. nMaxSize = sizeof(WLAN802_3IPv4Packet);
  392. }
  393. else
  394. {
  395. LOG_MSG_ERROR("%s isn't implemented"
  396. " for IPv6 - do it yourself :-)",
  397. __FUNCTION__);
  398. return false;
  399. }
  400. return true;
  401. }
  402. bool LoadDefault802_1Q(
  403. enum ipa_ip_type eIP,
  404. uint8_t *pBuffer,
  405. size_t &nMaxSize)
  406. {
  407. if (IPA_IP_v4 == eIP) {
  408. if (nMaxSize < sizeof(Eth2IPv4Packet802_1Q_tag)) {
  409. LOG_MSG_ERROR(
  410. "Buffer is smaller than %d, "
  411. "no data was copied.",
  412. sizeof(Eth2IPv4Packet802_1Q_tag));
  413. return false;
  414. }
  415. memcpy(pBuffer, Eth2IPv4Packet802_1Q_tag,
  416. sizeof(Eth2IPv4Packet802_1Q_tag));
  417. nMaxSize = sizeof(Eth2IPv4Packet802_1Q_tag);
  418. } else {
  419. LOG_MSG_ERROR("%s isn't implemented"
  420. " for IPv6 - do it yourself :-)",
  421. __FUNCTION__);
  422. return false;
  423. }
  424. return true;
  425. }
  426. bool SendReceiveAndCompare(InterfaceAbstraction *pSink, uint8_t* pSendBuffer, size_t nSendBuffSize,
  427. InterfaceAbstraction *pSource, uint8_t* pExpectedBuffer, size_t nExpectedBuffSize)
  428. {
  429. LOG_MSG_STACK("Entering Function");
  430. bool bRetVal = true;
  431. uint8_t * pRxBuff = new uint8_t[2*(nExpectedBuffSize+1)];
  432. size_t nReceivedBuffSize = 0;
  433. size_t j;
  434. // Send buffer to pSink
  435. bRetVal = pSink->SendData((Byte *) pSendBuffer, nSendBuffSize);
  436. if (!bRetVal)
  437. {
  438. LOG_MSG_ERROR("SendData (pOutputBuffer=0x%p) failed",pSendBuffer);
  439. goto bail;
  440. }
  441. // Receive buffer from pSource
  442. if (NULL == pRxBuff)
  443. {
  444. LOG_MSG_ERROR("Failed to allocated pRxBuff[%d]",2*(nExpectedBuffSize+1));
  445. goto bail;
  446. }
  447. nReceivedBuffSize = pSource->ReceiveData(pRxBuff, 2*(nExpectedBuffSize+1)); // We cannot overflow here.
  448. LOG_MSG_INFO("Received %d bytes on %s.", nReceivedBuffSize, pSource->m_fromChannelName.c_str());
  449. if (0 > nReceivedBuffSize)
  450. {
  451. bRetVal = false;
  452. goto bail;
  453. }
  454. {// Logging Expected and Received buffers
  455. char aExpectedBufferStr[3*nExpectedBuffSize+1];
  456. char aRecievedBufferStr[3*nReceivedBuffSize+1];
  457. memset(aExpectedBufferStr,0,3*nExpectedBuffSize+1);
  458. memset(aRecievedBufferStr,0,3*nReceivedBuffSize+1);
  459. for(j = 0; j < nExpectedBuffSize; j++)
  460. snprintf(&aExpectedBufferStr[3*j], 3*nExpectedBuffSize+1 - (3*j + 1), " %02X", pExpectedBuffer[j]);
  461. for(j = 0; j < nReceivedBuffSize; j++)
  462. snprintf(&aRecievedBufferStr[3*j], 3*nReceivedBuffSize+1 - (3*j + 1), " %02X", pRxBuff[j]);
  463. LOG_MSG_INFO("\nExpected Value (%d)\n%s\n, Received Value(%d)\n%s\n",nExpectedBuffSize,aExpectedBufferStr,nReceivedBuffSize,aRecievedBufferStr);
  464. }
  465. //Comparing Expected and received sizes
  466. if (nExpectedBuffSize != nReceivedBuffSize)
  467. {
  468. LOG_MSG_INFO("Buffers' Size differ: expected(%d), Received(%d)",nExpectedBuffSize,nReceivedBuffSize);
  469. bRetVal = false;
  470. goto bail;
  471. }
  472. bRetVal = !memcmp((void*)pRxBuff, (void*)pExpectedBuffer, nExpectedBuffSize);
  473. LOG_MSG_INFO("Buffers %s ",bRetVal?"MATCH":"MISMATCH");
  474. LOG_MSG_INFO("Verify that pipe is Empty");
  475. nReceivedBuffSize = pSource->ReceiveData(pRxBuff, 2*(nExpectedBuffSize+1)); // We cannot overflow here.
  476. while (nReceivedBuffSize){
  477. char aRecievedBufferStr[3*nReceivedBuffSize+1];
  478. bRetVal = false;
  479. LOG_MSG_ERROR("More Data in Pipe!\nReceived %d bytes on %s.", nReceivedBuffSize, pSource->m_fromChannelName.c_str());
  480. memset(aRecievedBufferStr,0,3*nReceivedBuffSize+1);
  481. for(j = 0; j < nReceivedBuffSize; j++) {
  482. snprintf(&aRecievedBufferStr[3*j], 3*nReceivedBuffSize+1 - (3*j + 1), " %02X", pRxBuff[j]);
  483. }
  484. LOG_MSG_ERROR("\nReceived Value(%d)\n%s\n",nReceivedBuffSize,aRecievedBufferStr);
  485. nReceivedBuffSize = pSource->ReceiveData(pRxBuff, 2*(nExpectedBuffSize+1)); // We cannot overflow here.
  486. }
  487. bail:
  488. delete (pRxBuff);
  489. LOG_MSG_STACK("Leaving Function (Returning %s)",bRetVal?"True":"False");
  490. return bRetVal;
  491. }
  492. bool CreateBypassRoutingTable (RoutingDriverWrapper * pRouting,enum ipa_ip_type eIP,
  493. const char * pTableName, enum ipa_client_type eRuleDestination,
  494. uint32_t uHeaderHandle, uint32_t * pTableHdl)
  495. {
  496. bool bRetVal = true;
  497. struct ipa_ioc_add_rt_rule *pRoutingRule = NULL;
  498. struct ipa_rt_rule_add *pRoutingRuleEntry = NULL;
  499. struct ipa_ioc_get_rt_tbl sRoutingTable;
  500. LOG_MSG_STACK("Entering Function");
  501. memset(&sRoutingTable,0,sizeof(sRoutingTable));
  502. pRoutingRule = (struct ipa_ioc_add_rt_rule *)
  503. calloc(1,
  504. sizeof(struct ipa_ioc_add_rt_rule) +
  505. 1*sizeof(struct ipa_rt_rule_add)
  506. );
  507. if(!pRoutingRule) {
  508. LOG_MSG_ERROR("calloc failed to allocate pRoutingRule");
  509. bRetVal = false;
  510. goto bail;
  511. }
  512. pRoutingRule->num_rules = 1;
  513. pRoutingRule->ip = ((IPA_IP_v4 == eIP)? IPA_IP_v4 : IPA_IP_v6);
  514. pRoutingRule->commit = true;
  515. strlcpy(pRoutingRule->rt_tbl_name, pTableName, sizeof(pRoutingRule->rt_tbl_name));
  516. pRoutingRuleEntry = &(pRoutingRule->rules[0]);
  517. pRoutingRuleEntry->at_rear = 1;
  518. pRoutingRuleEntry->rule.dst = eRuleDestination;// Setting Rule's Destination Pipe
  519. pRoutingRuleEntry->rule.hdr_hdl = uHeaderHandle; // Header handle
  520. pRoutingRuleEntry->rule.attrib.attrib_mask = 0;// All Packets will get a "Hit"
  521. if (false == pRouting->AddRoutingRule(pRoutingRule))
  522. {
  523. LOG_MSG_ERROR("Routing rule addition(pRoutingRule) failed!");
  524. bRetVal = false;
  525. goto bail;
  526. }
  527. if (!pRoutingRuleEntry->rt_rule_hdl)
  528. {
  529. LOG_MSG_ERROR("pRoutingRuleEntry->rt_rule_hdl == 0, Routing rule addition(pRoutingRule) failed!");
  530. bRetVal = false;
  531. goto bail;
  532. }
  533. LOG_MSG_INFO("pRoutingRuleEntry->rt_rule_hdl == 0x%x added to Table %s",pRoutingRuleEntry->rt_rule_hdl,pTableName);
  534. sRoutingTable.ip = eIP;
  535. strlcpy(sRoutingTable.name, pTableName, sizeof(sRoutingTable.name));
  536. if (!pRouting->GetRoutingTable(&sRoutingTable)) {
  537. LOG_MSG_ERROR(
  538. "m_routing.GetRoutingTable(&sRoutingTable=0x%p) Failed.", &sRoutingTable);
  539. bRetVal = false;
  540. goto bail;
  541. }
  542. if(NULL != pTableHdl){
  543. (* pTableHdl ) = sRoutingTable.hdl;
  544. LOG_MSG_DEBUG("Table Handle =0x%x will be returned.",(*pTableHdl));
  545. }
  546. bail:
  547. Free (pRoutingRule);
  548. LOG_MSG_STACK("Leaving Function (Returning %s)",bRetVal?"True":"False");
  549. return bRetVal;
  550. }
  551. bool CreateBypassRoutingTable_v2 (RoutingDriverWrapper * pRouting,enum ipa_ip_type eIP,
  552. const char * pTableName, enum ipa_client_type eRuleDestination,
  553. uint32_t uHeaderHandle, uint32_t * pTableHdl, uint8_t uClsAggrIrqMod)
  554. {
  555. bool bRetVal = true;
  556. struct ipa_ioc_add_rt_rule_v2 *pRoutingRule = NULL;
  557. struct ipa_rt_rule_add_v2 *pRoutingRuleEntry = NULL;
  558. struct ipa_ioc_get_rt_tbl sRoutingTable;
  559. LOG_MSG_STACK("Entering Function");
  560. memset(&sRoutingTable,0,sizeof(sRoutingTable));
  561. pRoutingRule = (struct ipa_ioc_add_rt_rule_v2 *)
  562. calloc(1, sizeof(struct ipa_ioc_add_rt_rule_v2));
  563. if(!pRoutingRule) {
  564. LOG_MSG_ERROR("calloc failed to allocate pRoutingRule");
  565. bRetVal = false;
  566. goto bail;
  567. }
  568. pRoutingRule->rules = (uint64_t)calloc(1, sizeof(struct ipa_rt_rule_add_v2));
  569. if (!pRoutingRule->rules) {
  570. LOG_MSG_ERROR("calloc failed to allocate pRoutingRule->rules");
  571. bRetVal = false;
  572. goto bail;
  573. }
  574. pRoutingRule->num_rules = 1;
  575. pRoutingRule->rule_add_size = sizeof(struct ipa_rt_rule_add_v2);
  576. printf("%s(), Nadav: Adding rule_add_size\n", __FUNCTION__);
  577. pRoutingRule->ip = ((IPA_IP_v4 == eIP)? IPA_IP_v4 : IPA_IP_v6);
  578. pRoutingRule->commit = true;
  579. strlcpy(pRoutingRule->rt_tbl_name, pTableName, sizeof(pRoutingRule->rt_tbl_name));
  580. pRoutingRuleEntry = &(((struct ipa_rt_rule_add_v2 *)pRoutingRule->rules)[0]);
  581. pRoutingRuleEntry->at_rear = 1;
  582. pRoutingRuleEntry->rule.dst = eRuleDestination;// Setting Rule's Destination Pipe
  583. printf("%s(), Nadav: Destination = %d\n", __FUNCTION__, pRoutingRuleEntry->rule.dst);
  584. pRoutingRuleEntry->rule.hdr_hdl = uHeaderHandle; // Header handle
  585. pRoutingRuleEntry->rule.attrib.attrib_mask = 0;// All Packets will get a "Hit"
  586. pRoutingRuleEntry->rule.close_aggr_irq_mod = uClsAggrIrqMod;
  587. if (false == pRouting->AddRoutingRule(pRoutingRule))
  588. {
  589. LOG_MSG_ERROR("Routing rule addition(pRoutingRule) failed!");
  590. bRetVal = false;
  591. goto bail;
  592. }
  593. if (!pRoutingRuleEntry->rt_rule_hdl)
  594. {
  595. LOG_MSG_ERROR("pRoutingRuleEntry->rt_rule_hdl == 0, Routing rule addition(pRoutingRule) failed!");
  596. bRetVal = false;
  597. goto bail;
  598. }
  599. LOG_MSG_INFO("pRoutingRuleEntry->rt_rule_hdl == 0x%x added to Table %s",pRoutingRuleEntry->rt_rule_hdl,pTableName);
  600. sRoutingTable.ip = eIP;
  601. strlcpy(sRoutingTable.name, pTableName, sizeof(sRoutingTable.name));
  602. if (!pRouting->GetRoutingTable(&sRoutingTable)) {
  603. LOG_MSG_ERROR(
  604. "m_routing.GetRoutingTable(&sRoutingTable=0x%p) Failed.", &sRoutingTable);
  605. bRetVal = false;
  606. goto bail;
  607. }
  608. if(NULL != pTableHdl){
  609. (* pTableHdl ) = sRoutingTable.hdl;
  610. LOG_MSG_DEBUG("Table Handle =0x%x will be returned.",(*pTableHdl));
  611. }
  612. bail:
  613. Free (pRoutingRule);
  614. LOG_MSG_STACK("Leaving Function (Returning %s)",bRetVal?"True":"False");
  615. return bRetVal;
  616. }
  617. //Don't use these methods directly. use MACROs instead
  618. void __log_msg(enum msgType logType, const char* filename, int line, const char* function, const char* format, ... )
  619. {
  620. va_list args;
  621. switch (logType) {
  622. case ERROR:
  623. fprintf( stderr, "ERROR!");
  624. break;
  625. case DEBUG:
  626. fprintf( stderr, "DEBUG:");
  627. break;
  628. case INFO:
  629. fprintf( stderr, "INFO :");
  630. break;
  631. case STACK:
  632. fprintf( stderr, "STACK:");
  633. break;
  634. default:
  635. fprintf( stderr, "BUG!!!");
  636. break;
  637. }
  638. fprintf( stderr, " [%s:%d, %s()] ",filename,line,function);
  639. va_start( args, format );
  640. vfprintf( stderr, format, args );
  641. va_end( args );
  642. fprintf( stderr, "\n" );
  643. }
  644. bool file_exists(const char* filename)
  645. {
  646. return (access(filename, F_OK) == 0);
  647. }
  648. int ConfigureSystem(int testConfiguration, int fd)
  649. {
  650. return ConfigureSystem(testConfiguration, fd, NULL);
  651. }
  652. int ConfigureSystem(int testConfiguration, int fd, const char* params)
  653. {
  654. char testConfigurationStr[10];
  655. int ret;
  656. char *pSendBuffer;
  657. char str[10];
  658. int iter_cnt = 2000;
  659. if(params != NULL)
  660. pSendBuffer = new char[strlen(params) + 10];
  661. else
  662. pSendBuffer = new char[10];
  663. if (NULL == pSendBuffer)
  664. {
  665. LOG_MSG_ERROR("Failed to allocated pSendBuffer");
  666. return -1;
  667. }
  668. if(params != NULL)
  669. snprintf(pSendBuffer, strlen(params) + 10, "%d %s", testConfiguration, params);
  670. else
  671. snprintf(pSendBuffer, 10, "%d", testConfiguration);
  672. ret = write(fd, pSendBuffer, sizeof(pSendBuffer) );
  673. if (ret < 0) {
  674. g_Logger.AddMessage(LOG_ERROR, "%s Write operation failed.\n", __FUNCTION__);
  675. goto bail;
  676. }
  677. // Wait until the system is fully configured
  678. // Convert testConfiguration to string
  679. snprintf(testConfigurationStr, sizeof(testConfigurationStr), "%d", testConfiguration);
  680. // Read the configuration index from the device node
  681. ret = read(fd, str, sizeof(str));
  682. if (ret < 0) {
  683. g_Logger.AddMessage(LOG_ERROR, "%s Read operation failed.\n", __FUNCTION__);
  684. goto bail;
  685. }
  686. while ( strcmp(str, testConfigurationStr) ) {
  687. // Sleep for 5 msec
  688. struct timespec time;
  689. time.tv_sec = 0;
  690. time.tv_nsec = 50e6;
  691. nanosleep(&time, NULL);
  692. ret = read(fd, str, sizeof(str));
  693. if (ret < 0) {
  694. g_Logger.AddMessage(LOG_ERROR, "%s Read operation failed.\n", __FUNCTION__);
  695. goto bail;
  696. }
  697. if (!--iter_cnt) {
  698. g_Logger.AddMessage(LOG_ERROR, "%s timeout waiting for test driver.\n", __FUNCTION__);
  699. ret = -1;
  700. goto bail;
  701. }
  702. }
  703. bail:
  704. delete[] pSendBuffer;
  705. return ret;
  706. }
  707. void ConfigureScenario(int testConfiguration)
  708. {
  709. ConfigureScenario(testConfiguration, NULL);
  710. }
  711. void ConfigureScenario(int testConfiguration, const char* params)
  712. {
  713. int fd, ret;
  714. char str[10];
  715. int currentConf;
  716. // Open /dev/ipa_test device node. This will allow to configure the system
  717. // and read its current configuration.
  718. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  719. if (fd < 0) {
  720. g_Logger.AddMessage(LOG_ERROR, "%s Could not open configuration device node.\n", __FUNCTION__);
  721. exit(0);
  722. }
  723. // Read the current configuration.
  724. ret = read(fd, str, sizeof(str));
  725. if (ret < 0) {
  726. g_Logger.AddMessage(LOG_ERROR, "%s Read operation failed.\n", __FUNCTION__);
  727. return;
  728. }
  729. currentConf = atoi(str);
  730. // Do not reconfigure to the same configuration
  731. if (currentConf == testConfiguration) {
  732. g_Logger.AddMessage(LOG_DEVELOPMENT,"%s System was already configured as required(%d)\n",
  733. __FUNCTION__, currentConf);
  734. return;
  735. }
  736. /* in case the system is not "clean"*/
  737. if (-1 != currentConf) {
  738. g_Logger.AddMessage(LOG_DEVELOPMENT,"%s System has other configuration (%d) - cleanup\n", __FUNCTION__, currentConf);
  739. ret = ConfigureSystem(-1, fd);
  740. if (ret < 0) {
  741. g_Logger.AddMessage(LOG_ERROR, "%s Configure operation failed.\n",
  742. __FUNCTION__);
  743. return;
  744. }
  745. }
  746. // Start system configuration.
  747. g_Logger.AddMessage(LOG_DEVELOPMENT,"%s Setting system to the required configuration (%d)\n", __FUNCTION__, testConfiguration);
  748. ret = ConfigureSystem(testConfiguration, fd, params);
  749. if (ret < 0) {
  750. g_Logger.AddMessage(LOG_ERROR, "%s configure operation failed.\n",
  751. __FUNCTION__);
  752. return;
  753. }
  754. ret = system("mdev -s");
  755. if (ret < 0) {
  756. g_Logger.AddMessage(LOG_ERROR, "%s system(\"mdev -s\") returned %d\n",
  757. __FUNCTION__, ret);
  758. }
  759. close(fd);
  760. }//func
  761. void clean_old_stashed_config()
  762. {
  763. if (current_configuration == NULL)
  764. return;
  765. for (int i = 0 ; i < current_configuration->from_ipa_channels_num ; i++) {
  766. delete((struct test_ipa_ep_cfg*)
  767. current_configuration->from_ipa_channel_config[i]->cfg);
  768. delete((struct test_ipa_ep_cfg*)
  769. current_configuration->from_ipa_channel_config[i]);
  770. }
  771. for (int i = 0 ; i < current_configuration->to_ipa_channels_num ; i++) {
  772. delete((struct test_ipa_ep_cfg*)
  773. current_configuration->to_ipa_channel_config[i]->cfg);
  774. delete((struct test_ipa_ep_cfg*)
  775. current_configuration->to_ipa_channel_config[i]);
  776. }
  777. delete(current_configuration);
  778. current_configuration = NULL;
  779. }
  780. void stash_new_configuration(struct ipa_test_config_header *header)
  781. {
  782. clean_old_stashed_config();
  783. /*
  784. * We will start by shallow copying each level, and afterwards,
  785. * override the pointers
  786. */
  787. current_configuration = new ipa_test_config_header();
  788. *current_configuration = *header;
  789. current_configuration->from_ipa_channel_config =
  790. new ipa_channel_config*[header->from_ipa_channels_num]();
  791. current_configuration->to_ipa_channel_config =
  792. new ipa_channel_config*[header->to_ipa_channels_num]();
  793. for (int i = 0 ; i < current_configuration->from_ipa_channels_num ; i++) {
  794. current_configuration->from_ipa_channel_config[i] =
  795. new ipa_channel_config;
  796. *current_configuration->from_ipa_channel_config[i] =
  797. *header->from_ipa_channel_config[i];
  798. current_configuration->from_ipa_channel_config[i]->cfg =
  799. new test_ipa_ep_cfg();
  800. memcpy(current_configuration->from_ipa_channel_config[i]->cfg,
  801. header->from_ipa_channel_config[i]->cfg,
  802. header->from_ipa_channel_config[i]->config_size);
  803. }
  804. for (int i = 0 ; i < current_configuration->to_ipa_channels_num ; i++) {
  805. current_configuration->to_ipa_channel_config[i] =
  806. new ipa_channel_config;
  807. *current_configuration->to_ipa_channel_config[i] =
  808. *header->to_ipa_channel_config[i];
  809. current_configuration->to_ipa_channel_config[i]->cfg = new test_ipa_ep_cfg();
  810. memcpy(current_configuration->to_ipa_channel_config[i]->cfg,
  811. header->to_ipa_channel_config[i]->cfg,
  812. header->to_ipa_channel_config[i]->config_size);
  813. }
  814. }
  815. bool is_prev_configuration_generic()
  816. {
  817. int fd;
  818. char str[10] = {0};
  819. int retval;
  820. int current_conf_num;
  821. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  822. if (fd < 0) {
  823. g_Logger.AddMessage(LOG_ERROR ,"%s Could not open configuration device node.\n", __FUNCTION__);
  824. exit(0);
  825. }
  826. retval = read(fd, str, sizeof(str));
  827. close(fd);
  828. if (retval < 0) {
  829. g_Logger.AddMessage(LOG_ERROR ,"%s Read operation failed.\n", __FUNCTION__);
  830. return true;
  831. }
  832. current_conf_num = atoi(str);
  833. if (current_conf_num == GENERIC_TEST_CONFIGURATION_IDX)
  834. return true;
  835. return false;
  836. }
  837. static bool is_reconfigure_required(struct ipa_test_config_header *header)
  838. {
  839. // Is reconfiguration not required flag (current conf is cool)
  840. bool flag = true;
  841. if (is_prev_configuration_generic() == false) {
  842. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  843. "prev configuration didn't use generic configuration\n");
  844. return true;
  845. }
  846. if (current_configuration == NULL) {
  847. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  848. "no prev generic configuration found in the test app data-base\n");
  849. return true;
  850. }
  851. flag &= (header->from_ipa_channels_num ==
  852. current_configuration->from_ipa_channels_num);
  853. flag &= (header->to_ipa_channels_num ==
  854. current_configuration->to_ipa_channels_num);
  855. if (flag == false) {
  856. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  857. "not same number of pipes\n");
  858. return true;
  859. }
  860. for (int i = 0 ; i < header->from_ipa_channels_num ; i++) {
  861. flag &= (header->from_ipa_channel_config[i]->client ==
  862. current_configuration->from_ipa_channel_config[i]->client);
  863. flag &= (header->from_ipa_channel_config[i]->index ==
  864. current_configuration->from_ipa_channel_config[i]->index);
  865. flag &= !memcmp(header->from_ipa_channel_config[i]->cfg,
  866. current_configuration->from_ipa_channel_config[i]->cfg,
  867. header->from_ipa_channel_config[i]->config_size);
  868. flag &= (header->from_ipa_channel_config[i]->en_status ==
  869. current_configuration->from_ipa_channel_config[i]->en_status);
  870. }
  871. if (flag == false) {
  872. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  873. "\"from\" endpoint configuration is different from prev\n");
  874. return true;
  875. }
  876. for (int i = 0 ; i < header->to_ipa_channels_num ; i++) {
  877. flag &= (header->to_ipa_channel_config[i]->client ==
  878. current_configuration->to_ipa_channel_config[i]->client);
  879. flag &= (header->to_ipa_channel_config[i]->index ==
  880. current_configuration->to_ipa_channel_config[i]->index);
  881. flag &= !memcmp(header->to_ipa_channel_config[i]->cfg,
  882. current_configuration->to_ipa_channel_config[i]->cfg,
  883. header->to_ipa_channel_config[i]->config_size);
  884. flag &= (header->to_ipa_channel_config[i]->en_status ==
  885. current_configuration->to_ipa_channel_config[i]->en_status);
  886. }
  887. if (flag == false) {
  888. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  889. "\"to\" endpoint configuration is different from prev\n");
  890. return true;
  891. }
  892. return false;
  893. }
  894. int GenericConfigureScenario(struct ipa_test_config_header *header)
  895. {
  896. int fd;
  897. int retval;
  898. if (is_reconfigure_required(header) == false) {
  899. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  900. "No need to reconfigure, we are all good :)\n");
  901. return true;
  902. } else {
  903. g_Logger.AddMessage(LOG_DEVELOPMENT ,
  904. "Need to run configuration again\n");
  905. }
  906. g_Logger.AddMessage(LOG_DEVELOPMENT, "configuration has started, parameters:\n");
  907. g_Logger.AddMessage(LOG_DEVELOPMENT, "header->head_marker=0x%x\n", header->head_marker);
  908. g_Logger.AddMessage(LOG_DEVELOPMENT, "header->from_ipa_channels_num=%d\n", header->from_ipa_channels_num);
  909. g_Logger.AddMessage(LOG_DEVELOPMENT, "header->to_ipa_channels_num=%d\n", header->to_ipa_channels_num);
  910. g_Logger.AddMessage(LOG_DEVELOPMENT, "header->tail_marker=0x%x\n", header->tail_marker);
  911. for (int i = 0 ; i < header->from_ipa_channels_num ; i++) {
  912. g_Logger.AddMessage(LOG_DEVELOPMENT,
  913. "header->from_ipa_channel_config[%d]->head_marker=0x%x\n", i,
  914. header->from_ipa_channel_config[i]->head_marker);
  915. g_Logger.AddMessage(LOG_DEVELOPMENT,
  916. "header->from_ipa_channel_config[%d]->index=%d\n", i,
  917. header->from_ipa_channel_config[i]->index);
  918. g_Logger.AddMessage(LOG_DEVELOPMENT,
  919. "header->from_ipa_channel_config[%d]->client=%d\n", i,
  920. header->from_ipa_channel_config[i]->client);
  921. g_Logger.AddMessage(LOG_DEVELOPMENT,
  922. "header->from_ipa_channel_config[%d]->config_size=%d\n", i,
  923. header->from_ipa_channel_config[i]->config_size);
  924. g_Logger.AddMessage(LOG_DEVELOPMENT,
  925. "header->from_ipa_channel_config[%d]->en_status=%d\n", i,
  926. header->from_ipa_channel_config[i]->en_status);
  927. }
  928. for (int i = 0 ; i < header->to_ipa_channels_num ; i++) {
  929. g_Logger.AddMessage(LOG_DEVELOPMENT,
  930. "header->to_ipa_channel_config[%d]->head_marker=0x%x\n", i,
  931. header->to_ipa_channel_config[i]->head_marker);
  932. g_Logger.AddMessage(LOG_DEVELOPMENT,
  933. "header->to_ipa_channel_config[%d]->index=%d\n", i,
  934. header->to_ipa_channel_config[i]->index);
  935. g_Logger.AddMessage(LOG_DEVELOPMENT,
  936. "header->to_ipa_channel_config[%d]->client=%d\n", i,
  937. header->to_ipa_channel_config[i]->client);
  938. g_Logger.AddMessage(LOG_DEVELOPMENT,
  939. "header->to_ipa_channel_config[%d]->config_size=%d\n", i,
  940. header->to_ipa_channel_config[i]->config_size);
  941. g_Logger.AddMessage(LOG_DEVELOPMENT,
  942. "header->to_ipa_channel_config[%d]->en_status=%d\n", i,
  943. header->to_ipa_channel_config[i]->en_status);
  944. }
  945. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  946. if (fd == -1) {
  947. g_Logger.AddMessage(LOG_ERROR,
  948. "%s - open %s failed (fd=%d,errno=%s)\n",
  949. __FUNCTION__, CONFIGURATION_NODE_PATH, fd, strerror(errno));
  950. return false;
  951. }
  952. retval = ioctl(fd, IPA_TEST_IOC_CONFIGURE, header);
  953. if (retval) {
  954. g_Logger.AddMessage(LOG_ERROR, "fail to configure the system (%d)\n", retval);
  955. close(fd);
  956. return false;
  957. } else {
  958. g_Logger.AddMessage(LOG_DEVELOPMENT, "system was successfully configured\n");
  959. }
  960. retval = close(fd);
  961. if (retval) {
  962. g_Logger.AddMessage(LOG_ERROR,
  963. "%s - fail to close the fd (path=%s,retval=%d,fd=%d,errno=%s)\n",
  964. __FUNCTION__, CONFIGURATION_NODE_PATH, retval, fd, strerror(errno));
  965. return false;
  966. }
  967. g_Logger.AddMessage(LOG_DEVELOPMENT ,"stashing new configuration\n");
  968. stash_new_configuration(header);
  969. g_Logger.AddMessage(LOG_DEVELOPMENT,
  970. "Running mdev in order to create the device nodes.\n");
  971. retval = system("mdev -s");
  972. if (retval < 0) {
  973. g_Logger.AddMessage(LOG_ERROR, "%s system(\"mdev -s\") returned %d\n",
  974. __FUNCTION__, retval);
  975. }
  976. return true;
  977. }
  978. int GenericConfigureScenarioDestory(void)
  979. {
  980. int fd;
  981. int retval;
  982. g_Logger.AddMessage(LOG_DEVELOPMENT, "cleanup started\n");
  983. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  984. if (fd == -1) {
  985. g_Logger.AddMessage(LOG_ERROR,
  986. "%s - open %s failed (retval=%d,fd=%d,errno=%s)\n",
  987. __FUNCTION__, CONFIGURATION_NODE_PATH, fd, strerror(errno));
  988. return false;
  989. }
  990. retval = ioctl(fd, IPA_TEST_IOC_CLEAN);
  991. if (retval)
  992. g_Logger.AddMessage(LOG_ERROR, "fail to clean the system (%d)\n", retval);
  993. else
  994. g_Logger.AddMessage(LOG_DEVELOPMENT, "system was successfully cleaned\n");
  995. retval = close(fd);
  996. if (retval) {
  997. g_Logger.AddMessage(LOG_ERROR, "fail to close the fd - %d\n", retval);
  998. return false;
  999. }
  1000. return true;
  1001. }
  1002. bool configure_ep_ctrl(struct ipa_test_ep_ctrl *ep_ctrl)
  1003. {
  1004. int fd;
  1005. int retval = 0;
  1006. g_Logger.AddMessage(LOG_DEVELOPMENT, "ep ctrl started \n");
  1007. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  1008. if (fd == -1) {
  1009. g_Logger.AddMessage(LOG_ERROR,
  1010. "%s - open %s failed (retval=%d,fd=%d,errno=%s)\n",
  1011. __FUNCTION__, CONFIGURATION_NODE_PATH, fd, strerror(errno));
  1012. return false;
  1013. }
  1014. retval = ioctl(fd, IPA_TEST_IOC_EP_CTRL, ep_ctrl);
  1015. if (retval)
  1016. g_Logger.AddMessage(LOG_ERROR, "fail to perform ep ctrl (%d)\n", retval);
  1017. else
  1018. g_Logger.AddMessage(LOG_DEVELOPMENT, "ep ctrl was successfully executed\n");
  1019. retval = close(fd);
  1020. if (retval) {
  1021. g_Logger.AddMessage(LOG_ERROR, "fail to close the fd - %d\n", retval);
  1022. }
  1023. return true;
  1024. }
  1025. bool configure_holb(struct ipa_test_holb_config *test_holb_config)
  1026. {
  1027. int fd;
  1028. int retval = 0;
  1029. g_Logger.AddMessage(LOG_DEVELOPMENT, "holb config started \n");
  1030. if (!test_holb_config) {
  1031. g_Logger.AddMessage(LOG_ERROR, "Null pointer argument!");
  1032. return false;
  1033. }
  1034. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  1035. if (fd == -1) {
  1036. g_Logger.AddMessage(LOG_ERROR,
  1037. "%s - open %s failed (retval=%d,fd=%d,errno=%s)\n",
  1038. __FUNCTION__, CONFIGURATION_NODE_PATH, fd, strerror(errno));
  1039. return false;
  1040. }
  1041. retval = ioctl(fd, IPA_TEST_IOC_HOLB_CONFIG, test_holb_config);
  1042. if (retval)
  1043. g_Logger.AddMessage(LOG_ERROR,
  1044. "fail to perform holb config (%d)\n",
  1045. retval);
  1046. else
  1047. g_Logger.AddMessage(LOG_DEVELOPMENT,
  1048. "holb config was successfully executed\n");
  1049. retval = close(fd);
  1050. if (retval) {
  1051. g_Logger.AddMessage(LOG_ERROR, "fail to close the fd - %d\n", retval);
  1052. }
  1053. return true;
  1054. }
  1055. void prepare_channel_struct(struct ipa_channel_config *channel,
  1056. int index,
  1057. enum ipa_client_type client,
  1058. void *cfg,
  1059. size_t config_size,
  1060. bool en_status)
  1061. {
  1062. channel->head_marker = IPA_TEST_CHANNEL_CONFIG_MARKER;
  1063. channel->index = index;
  1064. channel->client = client;
  1065. channel->cfg = (char*)cfg;
  1066. channel->config_size = config_size;
  1067. channel->tail_marker = IPA_TEST_CHANNEL_CONFIG_MARKER;
  1068. channel->en_status = en_status;
  1069. }
  1070. void prepare_header_struct(struct ipa_test_config_header *header,
  1071. struct ipa_channel_config **from,
  1072. struct ipa_channel_config **to)
  1073. {
  1074. header->head_marker = IPA_TEST_CONFIG_MARKER;
  1075. header->from_ipa_channel_config = from;
  1076. header->to_ipa_channel_config = to;
  1077. header->tail_marker = IPA_TEST_CONFIG_MARKER;
  1078. }
  1079. bool CompareResultVsGolden(Byte *goldenBuffer, unsigned int goldenSize,
  1080. Byte *receivedBuffer, unsigned int receivedSize)
  1081. {
  1082. if (receivedSize != goldenSize) {
  1083. g_Logger.AddMessage(LOG_VERBOSE, "%s File sizes are different.\n", __FUNCTION__);
  1084. return false;
  1085. }
  1086. return !memcmp((void*)receivedBuffer, (void*)goldenBuffer, goldenSize);
  1087. }
  1088. bool CompareResultVsGolden_w_Status(Byte *goldenBuffer, unsigned int goldenSize,
  1089. Byte *receivedBuffer, unsigned int receivedSize)
  1090. {
  1091. size_t stts_size = sizeof(struct ipa3_hw_pkt_status);
  1092. if (TestManager::GetInstance()->GetIPAHwType() >= IPA_HW_v5_0) {
  1093. stts_size = sizeof(struct ipa3_hw_pkt_status_hw_v5_0);
  1094. }
  1095. if ((receivedSize - stts_size) != goldenSize) {
  1096. g_Logger.AddMessage(LOG_VERBOSE, "%s Buffer sizes are different.\n", __FUNCTION__);
  1097. return false;
  1098. }
  1099. printf("comparison is made considering %zu status bytes!\n", stts_size);
  1100. return !memcmp((void*)((unsigned char *)receivedBuffer +
  1101. stts_size), (void*)goldenBuffer, goldenSize);
  1102. }
  1103. Byte *LoadFileToMemory(const string &name, unsigned int *sizeLoaded)
  1104. {
  1105. FILE *file;
  1106. Byte *buffer;
  1107. size_t fileLen;
  1108. // Open file
  1109. file = fopen(name.c_str(), "rb");
  1110. if (!file) {
  1111. g_Logger.AddMessage(LOG_ERROR, "Unable to open file %s\n", name.c_str());
  1112. return NULL;
  1113. }
  1114. // Get file length
  1115. fseek(file, 0, SEEK_END);
  1116. fileLen = ftell(file);
  1117. fseek(file, 0, SEEK_SET);
  1118. // Allocate memory
  1119. buffer=(Byte*)malloc(fileLen+1);
  1120. if (!buffer) {
  1121. fprintf(stderr, "Memory error!\n");
  1122. fclose(file);
  1123. return NULL;
  1124. }
  1125. // Read file contents into buffer
  1126. *sizeLoaded = fread(buffer, 1, fileLen, file);
  1127. fclose(file);
  1128. return buffer;
  1129. }
  1130. void print_buff(void *data, size_t size)
  1131. {
  1132. uint8_t bytes_in_line = 16;
  1133. uint i, j, num_lines;
  1134. char str[1024], tmp[4];
  1135. num_lines = size / bytes_in_line;
  1136. if (size % bytes_in_line > 0)
  1137. num_lines++;
  1138. printf("Printing buffer at address 0x%p, size = %zu: \n", data, size);
  1139. for (i = 0 ; i < num_lines; i++) {
  1140. str[0] = '\0';
  1141. for (j = 0; (j < bytes_in_line) && ((i * bytes_in_line + j) < size); j++) {
  1142. snprintf(tmp, sizeof(tmp), "%02x ",
  1143. ((unsigned char*)data)[i * bytes_in_line + j]);
  1144. strlcpy(str + strlen(str), tmp, sizeof(str) - strlen(str));
  1145. }
  1146. printf("%s\n", str);
  1147. }
  1148. }
  1149. void add_buff(uint8_t *data, size_t size, uint8_t val)
  1150. {
  1151. for (int i = 0; i < static_cast<int>(size); i++)
  1152. data[i]+=val;
  1153. }
  1154. bool RegSuspendHandler(bool deferred_flag, bool reg, int DevNum)
  1155. {
  1156. int fd = 0;
  1157. int retval = 0;
  1158. struct ipa_test_reg_suspend_handler RegData;
  1159. fd = open(CONFIGURATION_NODE_PATH, O_RDWR);
  1160. if (fd == -1) {
  1161. g_Logger.AddMessage(LOG_ERROR,
  1162. "%s - open %s failed (fd=%d,errno=%s)\n",
  1163. __FUNCTION__, CONFIGURATION_NODE_PATH, fd, strerror(errno));
  1164. return false;
  1165. }
  1166. RegData.DevNum = DevNum;
  1167. RegData.reg = reg;
  1168. RegData.deferred_flag = deferred_flag;
  1169. retval = ioctl(fd, IPA_TEST_IOC_REG_SUSPEND_HNDL, &RegData);
  1170. if (retval) {
  1171. g_Logger.AddMessage(LOG_ERROR, "fail to reg suspend handler (%d)\n", retval);
  1172. close(fd);
  1173. return false;
  1174. } else {
  1175. g_Logger.AddMessage(LOG_DEVELOPMENT, "suspend handler was successfully configured\n");
  1176. }
  1177. close(fd);
  1178. return true;
  1179. }
  1180. const Byte Eth2Helper::m_ETH2_IP4_HDR[ETH_HLEN] =
  1181. {
  1182. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, // ETH2 DST
  1183. 0xA7, 0xA8, 0xA9, 0xB0, 0xB1, 0xB2, // ETH2 SRC
  1184. 0x08, 0x00, // ETH2 TYPE IPv4 - ETH_P_IP 0x0800
  1185. };
  1186. bool Eth2Helper::LoadEth2IP4Header(
  1187. uint8_t *pBuffer,
  1188. size_t bufferSize,
  1189. size_t *pLen)
  1190. {
  1191. if (bufferSize < ETH_HLEN)
  1192. {
  1193. LOG_MSG_ERROR("Buffer too small\n");
  1194. return false;
  1195. }
  1196. memcpy(pBuffer, m_ETH2_IP4_HDR, ETH_HLEN);
  1197. *pLen = ETH_HLEN;
  1198. return true;
  1199. }
  1200. bool Eth2Helper::LoadEth2IP6Header(
  1201. uint8_t *pBuffer,
  1202. size_t bufferSize,
  1203. size_t *pLen)
  1204. {
  1205. if (bufferSize < ETH_HLEN)
  1206. {
  1207. LOG_MSG_ERROR("Buffer too small\n");
  1208. return false;
  1209. }
  1210. // copy eth2 ip4 header
  1211. memcpy(pBuffer, m_ETH2_IP4_HDR, ETH_HLEN);
  1212. // change ethtype to ip6
  1213. pBuffer[ETH2_ETH_TYPE_OFFSET] = 0x86;
  1214. pBuffer[ETH2_ETH_TYPE_OFFSET+1] = 0xdd;
  1215. *pLen = ETH_HLEN;
  1216. return true;
  1217. }
  1218. bool Eth2Helper::LoadEth2IP4Packet(
  1219. uint8_t *pBuffer,
  1220. size_t bufferSize,
  1221. size_t *pLen)
  1222. {
  1223. size_t cnt = 0;
  1224. size_t len = 0;
  1225. if (!LoadEth2IP4Header(pBuffer, bufferSize, &cnt))
  1226. return false;
  1227. len = bufferSize - cnt;
  1228. if (!LoadDefaultPacket(IPA_IP_v4, pBuffer + cnt, len))
  1229. return false;
  1230. *pLen = len + cnt;
  1231. return true;
  1232. }
  1233. bool Eth2Helper::LoadEth2IP6Packet(
  1234. uint8_t *pBuffer,
  1235. size_t bufferSize,
  1236. size_t *pLen)
  1237. {
  1238. size_t cnt = 0;
  1239. size_t len = 0;
  1240. if (!LoadEth2IP6Header(pBuffer, bufferSize, &cnt))
  1241. return false;
  1242. len = bufferSize - cnt;
  1243. if (!LoadDefaultPacket(IPA_IP_v6, pBuffer + cnt, len))
  1244. return false;
  1245. *pLen = len + cnt;
  1246. return true;
  1247. }
  1248. const Byte WlanHelper::m_WLAN_HDR[WLAN_HDR_SIZE] =
  1249. {
  1250. // WLAN hdr - 4 bytes
  1251. 0x01, 0x02, 0x03, 0x04
  1252. };
  1253. bool WlanHelper::LoadWlanHeader(
  1254. uint8_t *pBuffer,
  1255. size_t bufferSize,
  1256. size_t *pLen)
  1257. {
  1258. if (bufferSize < WLAN_HDR_SIZE)
  1259. {
  1260. LOG_MSG_ERROR("Buffer too small\n");
  1261. return false;
  1262. }
  1263. memcpy(pBuffer, m_WLAN_HDR, WLAN_HDR_SIZE);
  1264. *pLen = WLAN_HDR_SIZE;
  1265. return true;
  1266. }
  1267. bool WlanHelper::LoadWlanEth2IP4Header(
  1268. uint8_t *pBuffer,
  1269. size_t bufferSize,
  1270. size_t *pLen)
  1271. {
  1272. size_t cnt = 0;
  1273. size_t len = 0;
  1274. if (!LoadWlanHeader(pBuffer, bufferSize, &cnt))
  1275. return false;
  1276. if (!Eth2Helper::LoadEth2IP4Header(
  1277. pBuffer + cnt,
  1278. bufferSize - cnt,
  1279. &len))
  1280. return false;
  1281. *pLen = len + cnt;
  1282. return true;
  1283. }
  1284. bool WlanHelper::LoadWlanEth2IP6Header(
  1285. uint8_t *pBuffer,
  1286. size_t bufferSize,
  1287. size_t *pLen)
  1288. {
  1289. size_t cnt = 0;
  1290. size_t len = 0;
  1291. if (!LoadWlanHeader(pBuffer, bufferSize, &cnt))
  1292. return false;
  1293. if (!Eth2Helper::LoadEth2IP6Header(
  1294. pBuffer + cnt,
  1295. bufferSize - cnt,
  1296. &len))
  1297. return false;
  1298. *pLen = len + cnt;
  1299. return true;
  1300. }
  1301. bool WlanHelper::LoadWlanEth2IP4Packet(
  1302. uint8_t *pBuffer,
  1303. size_t bufferSize,
  1304. size_t *pLen)
  1305. {
  1306. size_t cnt = 0;
  1307. size_t len = 0;
  1308. if (!LoadWlanHeader(pBuffer, bufferSize, &cnt))
  1309. return false;
  1310. if (!Eth2Helper::LoadEth2IP4Packet(
  1311. pBuffer + cnt,
  1312. bufferSize - cnt,
  1313. &len))
  1314. return false;
  1315. *pLen = len + cnt;
  1316. return true;
  1317. }
  1318. bool PadByLength(
  1319. uint8_t *pBuffer,
  1320. size_t bufferSize,
  1321. size_t len,
  1322. uint8_t padValue)
  1323. {
  1324. if (bufferSize < len)
  1325. {
  1326. LOG_MSG_ERROR("bufferSize < len.\n");
  1327. return false;
  1328. }
  1329. memset(pBuffer, padValue, len);
  1330. return true;
  1331. }
  1332. bool WlanHelper::LoadWlanEth2IP4PacketByLength(
  1333. uint8_t *pBuffer,
  1334. size_t bufferSize,
  1335. size_t len,
  1336. uint8_t padValue)
  1337. {
  1338. size_t cnt = 0;
  1339. if (!LoadWlanEth2IP4Packet(pBuffer, bufferSize, &cnt))
  1340. return false;
  1341. if (!PadByLength(pBuffer + cnt, bufferSize - cnt, len - cnt, padValue))
  1342. return false;
  1343. return true;
  1344. }
  1345. bool RNDISAggregationHelper::LoadRNDISHeader(
  1346. uint8_t *pBuffer,
  1347. size_t bufferSize,
  1348. uint32_t messageLength,
  1349. size_t *pLen)
  1350. {
  1351. if (bufferSize < RNDIS_HDR_SIZE)
  1352. {
  1353. LOG_MSG_ERROR("Buffer too small\n");
  1354. return false;
  1355. }
  1356. struct RndisHeader *pRndisHeader =
  1357. reinterpret_cast<struct RndisHeader*>(pBuffer);
  1358. memset(pRndisHeader, 0, sizeof(struct RndisHeader));
  1359. pRndisHeader->MessageType = 0x01;
  1360. pRndisHeader->DataOffset = 0x24;
  1361. if (messageLength > RNDIS_HDR_SIZE)
  1362. {
  1363. pRndisHeader->MessageLength = messageLength;
  1364. pRndisHeader->DataLength = messageLength - RNDIS_HDR_SIZE;
  1365. }
  1366. else
  1367. {
  1368. // This handles a case where we use the header
  1369. // in IPA headers table
  1370. // IPA needs to set these values
  1371. pRndisHeader->MessageLength = 0;
  1372. pRndisHeader->DataLength = 0;
  1373. }
  1374. *pLen = RNDIS_HDR_SIZE;
  1375. return true;
  1376. }
  1377. bool RNDISAggregationHelper::LoadRNDISEth2IP4Header(
  1378. uint8_t *pBuffer,
  1379. size_t bufferSize,
  1380. uint32_t messageLength,
  1381. size_t *pLen)
  1382. {
  1383. size_t cnt = 0;
  1384. size_t len = 0;
  1385. if (!LoadRNDISHeader(pBuffer, bufferSize, messageLength, &cnt))
  1386. return 0;
  1387. if (!Eth2Helper::LoadEth2IP4Header(pBuffer + cnt, bufferSize - cnt, &len))
  1388. return false;
  1389. *pLen = cnt + len;
  1390. return true;
  1391. }
  1392. bool RNDISAggregationHelper::LoadRNDISPacket(
  1393. enum ipa_ip_type eIP,
  1394. uint8_t *pBuffer,
  1395. size_t &nMaxSize)
  1396. {
  1397. if (nMaxSize < sizeof(struct RndisHeader))
  1398. {
  1399. LOG_MSG_ERROR("Buffer too small\n");
  1400. return false;
  1401. }
  1402. size_t nMaxSizeForDefaultPacket = nMaxSize - sizeof(struct RndisHeader);
  1403. if (!LoadEtherPacket(eIP, pBuffer + sizeof(struct RndisHeader),
  1404. nMaxSizeForDefaultPacket))
  1405. {
  1406. LOG_MSG_ERROR("LoadEtherPacket() failed\n");
  1407. return false;
  1408. }
  1409. nMaxSize = nMaxSizeForDefaultPacket + sizeof(struct RndisHeader);
  1410. struct RndisHeader *pRndisHeader = (struct RndisHeader*)pBuffer;
  1411. memset(pRndisHeader, 0, sizeof(struct RndisHeader));
  1412. pRndisHeader->MessageType = 0x01;
  1413. pRndisHeader->MessageLength = nMaxSize;
  1414. pRndisHeader->DataOffset = 0x24;
  1415. pRndisHeader->DataLength = nMaxSizeForDefaultPacket;
  1416. return true;
  1417. }
  1418. bool RNDISAggregationHelper::LoadEtherPacket(
  1419. enum ipa_ip_type eIP,
  1420. uint8_t *pBuffer,
  1421. size_t &nMaxSize)
  1422. {
  1423. if (nMaxSize < sizeof(struct ethhdr))
  1424. {
  1425. LOG_MSG_ERROR("Buffer too small\n");
  1426. return false;
  1427. }
  1428. size_t nMaxSizeForDefaultPacket = nMaxSize - sizeof(struct ethhdr);
  1429. if (!LoadDefaultPacket(eIP, pBuffer + sizeof(struct ethhdr),
  1430. nMaxSizeForDefaultPacket))
  1431. {
  1432. LOG_MSG_ERROR("LoadDefaultPacket() failed\n");
  1433. return false;
  1434. }
  1435. nMaxSize = nMaxSizeForDefaultPacket + sizeof(struct ethhdr);
  1436. struct ethhdr *pEtherHeader = (struct ethhdr*)pBuffer;
  1437. memcpy(pEtherHeader, Eth2Helper::m_ETH2_IP4_HDR, sizeof(struct ethhdr));
  1438. print_buff(pBuffer, nMaxSize);
  1439. return true;
  1440. }
  1441. bool RNDISAggregationHelper::CompareIPvsRNDISPacket(
  1442. Byte *pIPPacket,
  1443. int ipPacketSize,
  1444. Byte *pRNDISPacket,
  1445. size_t rndisPacketSize)
  1446. {
  1447. struct RndisHeader *pRndisHeader = (struct RndisHeader*)pRNDISPacket;
  1448. if (pRndisHeader->MessageType != 0x01)
  1449. {
  1450. LOG_MSG_ERROR("Wrong MessageType 0x%8x\n",
  1451. pRndisHeader->MessageType);
  1452. return false;
  1453. }
  1454. if (pRndisHeader->MessageLength != rndisPacketSize)
  1455. {
  1456. LOG_MSG_ERROR(
  1457. "Packet sizes do not match 0x%8x expected 0x%8x\n",
  1458. pRndisHeader->MessageLength, rndisPacketSize);
  1459. return false;
  1460. }
  1461. // Create Ethernet packet from the IP packet and compare it to RNDIS payload
  1462. size_t EtherPacketSize = ipPacketSize + sizeof(struct ethhdr);
  1463. Byte* pEtherPacket = (Byte *) malloc(EtherPacketSize);
  1464. if (pEtherPacket == NULL) {
  1465. LOG_MSG_ERROR("Memory allocation failure.\n");
  1466. return false;
  1467. }
  1468. memcpy(pEtherPacket, Eth2Helper::m_ETH2_IP4_HDR, sizeof(struct ethhdr));
  1469. memcpy(pEtherPacket + sizeof(struct ethhdr), pIPPacket, ipPacketSize);
  1470. if (pRndisHeader->DataLength != EtherPacketSize)
  1471. {
  1472. LOG_MSG_ERROR(
  1473. "Packet sizes do not match 0x%8x expected 0x%8x\n",
  1474. pRndisHeader->DataLength, EtherPacketSize);
  1475. Free(pEtherPacket);
  1476. return false;
  1477. }
  1478. if(!ComparePackets(
  1479. (Byte*)&pRndisHeader->DataOffset + pRndisHeader->DataOffset,
  1480. EtherPacketSize, pEtherPacket, EtherPacketSize))
  1481. {
  1482. LOG_MSG_ERROR("Packets do not match\n");
  1483. Free(pEtherPacket);
  1484. return false;
  1485. }
  1486. Free(pEtherPacket);
  1487. return true;
  1488. }
  1489. bool RNDISAggregationHelper::CompareEthervsRNDISPacket(
  1490. Byte *pIPPacket,
  1491. size_t ipPacketSize,
  1492. Byte *pRNDISPacket,
  1493. size_t rndisPacketSize)
  1494. {
  1495. struct RndisHeader *pRndisHeader = (struct RndisHeader*)pRNDISPacket;
  1496. if (pRndisHeader->MessageType != 0x01)
  1497. {
  1498. LOG_MSG_ERROR("Wrong MessageType 0x%8x\n",
  1499. pRndisHeader->MessageType);
  1500. return false;
  1501. }
  1502. if (pRndisHeader->MessageLength != rndisPacketSize)
  1503. {
  1504. LOG_MSG_ERROR(
  1505. "Packet sizes do not match 0x%8x expected 0x%8x\n",
  1506. pRndisHeader->MessageLength, rndisPacketSize);
  1507. return false;
  1508. }
  1509. if (pRndisHeader->DataLength != ipPacketSize)
  1510. {
  1511. LOG_MSG_ERROR(
  1512. "Packet sizes do not match 0x%8x expected 0x%8x\n",
  1513. pRndisHeader->DataLength, ipPacketSize);
  1514. return false;
  1515. }
  1516. return ComparePackets(
  1517. (Byte*)&pRndisHeader->DataOffset + pRndisHeader->DataOffset,
  1518. ipPacketSize, pIPPacket, ipPacketSize);
  1519. }
  1520. bool RNDISAggregationHelper::ComparePackets(
  1521. Byte *pPacket,
  1522. int packetSize,
  1523. Byte *pExpectedPacket,
  1524. int expectedPacketSize)
  1525. {
  1526. bool res = true;
  1527. if (packetSize != expectedPacketSize)
  1528. {
  1529. LOG_MSG_ERROR("Packet sizes do not match\n");
  1530. res = false;
  1531. }
  1532. for (int i = 0; i < packetSize; i++)
  1533. {
  1534. if (pPacket[i] != pExpectedPacket[i])
  1535. {
  1536. LOG_MSG_ERROR(
  1537. "Byte %d not match 0x%2x != 0x%2x\n",
  1538. i, pPacket[i], pExpectedPacket[i]);
  1539. res = false;
  1540. }
  1541. }
  1542. if (!res)
  1543. {
  1544. LOG_MSG_ERROR("Packet:\n");
  1545. print_buff(pPacket, packetSize);
  1546. LOG_MSG_ERROR("Expected Packet:\n");
  1547. print_buff(pExpectedPacket, expectedPacketSize);
  1548. }
  1549. return res;
  1550. }
  1551. #if !defined(MSM_IPA_TESTS) && !defined(USE_GLIB) && !defined(FEATURE_IPA_ANDROID)
  1552. size_t strlcpy(char* dst, const char* src, size_t size)
  1553. {
  1554. size_t i;
  1555. if (size == 0)
  1556. return strlen(src);
  1557. for (i = 0; i < (size - 1) && src[i] != '\0'; ++i)
  1558. dst[i] = src[i];
  1559. dst[i] = '\0';
  1560. return i + strlen(src + i);
  1561. }
  1562. #endif