TestsUtils.cpp 50 KB

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