tls_bench.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /* tls_bench.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. Example gcc build statement
  23. gcc -lwolfssl -lpthread -o tls_bench tls_bench.c
  24. ./tls_bench
  25. Or
  26. #include <examples/benchmark/tls_bench.h>
  27. bench_tls(args);
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #ifndef WOLFSSL_USER_SETTINGS
  33. #include <wolfssl/options.h>
  34. #endif
  35. #include <wolfssl/wolfcrypt/settings.h>
  36. #include <wolfssl/ssl.h>
  37. #include <wolfssl/wolfcrypt/hash.h> /* WC_MAX_DIGEST_SIZE */
  38. #include <wolfssl/test.h>
  39. #include <wolfssl/wolfio.h>
  40. #include <examples/benchmark/tls_bench.h>
  41. /* force certificate test buffers to be included via headers */
  42. #undef USE_CERT_BUFFERS_2048
  43. #define USE_CERT_BUFFERS_2048
  44. #undef USE_CERT_BUFFERS_256
  45. #define USE_CERT_BUFFERS_256
  46. #include <wolfssl/certs_test.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. #include <sys/time.h>
  52. #include <errno.h>
  53. /* For testing no pthread support */
  54. #if 0
  55. #undef HAVE_PTHREAD
  56. #endif
  57. /* PTHREAD requires server and client enabled */
  58. #if defined(HAVE_PTHREAD) && (defined(NO_WOLFSSL_CLIENT) || defined(NO_WOLFSSL_SERVER))
  59. #undef HAVE_PTHREAD
  60. #endif
  61. #ifdef HAVE_PTHREAD
  62. #include <pthread.h>
  63. #endif
  64. #if 0
  65. #define BENCH_USE_NONBLOCK
  66. #endif
  67. /* Defaults for configuration parameters */
  68. #define BENCH_DEFAULT_HOST "localhost"
  69. #define BENCH_DEFAULT_PORT 11112
  70. #define NUM_THREAD_PAIRS 1 /* Thread pairs of server/client */
  71. #ifndef BENCH_RUNTIME_SEC
  72. #ifdef BENCH_EMBEDDED
  73. #define BENCH_RUNTIME_SEC 15
  74. #else
  75. #define BENCH_RUNTIME_SEC 1
  76. #endif
  77. #endif
  78. /* TLS packet size */
  79. #ifndef TEST_PACKET_SIZE
  80. #ifdef BENCH_EMBEDDED
  81. #define TEST_PACKET_SIZE (2 * 1024)
  82. #else
  83. #define TEST_PACKET_SIZE (16 * 1024)
  84. #endif
  85. #endif
  86. /* Total bytes to benchmark per connection */
  87. #ifndef TEST_MAX_SIZE
  88. #ifdef BENCH_EMBEDDED
  89. #define TEST_MAX_SIZE (16 * 1024)
  90. #else
  91. #define TEST_MAX_SIZE (128 * 1024)
  92. #endif
  93. #endif
  94. #ifdef WOLFSSL_DTLS
  95. #ifdef BENCH_EMBEDDED
  96. /* WOLFSSL_MAX_MTU in internal.h */
  97. #define TEST_DTLS_PACKET_SIZE (1500)
  98. #else
  99. /* MAX_UDP_SIZE in interna.h */
  100. #define TEST_DTLS_PACKET_SIZE (8092)
  101. #endif
  102. #endif
  103. /* In memory transfer buffer maximum size */
  104. /* Must be large enough to handle max TLS packet size plus max TLS header MAX_MSG_EXTRA */
  105. #define MEM_BUFFER_SZ (TEST_PACKET_SIZE + 38 + WC_MAX_DIGEST_SIZE)
  106. #define SHOW_VERBOSE 0 /* Default output is tab delimited format */
  107. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  108. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  109. /* shutdown message - nice signal to server, we are done */
  110. static const char* kShutdown = "shutdown";
  111. #ifndef NO_WOLFSSL_CLIENT
  112. PEDANTIC_EXTENSION static const char* kTestStr =
  113. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  114. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  115. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  116. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  117. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  118. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  119. "\n"
  120. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  121. "four loko you probably haven't heard of them high life. Messenger bag\n"
  122. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  123. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  124. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  125. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  126. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  127. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  128. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  129. "four loko whatever street art yr farm-to-table.\n"
  130. "\n"
  131. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  132. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  133. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  134. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  135. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  136. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  137. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  138. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  139. "locavore.\n"
  140. "\n"
  141. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  142. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  143. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  144. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  145. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  146. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  147. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  148. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  149. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  150. "neutra PBR selvage.\n"
  151. "\n"
  152. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  153. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  154. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  155. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  156. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  157. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  158. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  159. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  160. "mollit magna, sriracha sartorial helvetica.\n"
  161. "\n"
  162. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  163. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  164. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  165. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  166. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  167. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  168. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  169. "\n"
  170. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  171. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  172. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  173. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  174. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  175. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  176. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  177. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  178. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  179. "\n"
  180. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  181. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  182. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  183. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  184. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  185. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  186. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  187. "bag dolor terry richardson sapiente.\n";
  188. #endif
  189. #if !defined(NO_DH)
  190. #define MIN_DHKEY_BITS 1024
  191. #if !defined(NO_WOLFSSL_SERVER)
  192. /* dh2048 p */
  193. static const unsigned char dhp[] =
  194. {
  195. 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13, 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30,
  196. 0xd5, 0xf5, 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a, 0xc6, 0x25, 0x27,
  197. 0x1c, 0xe8, 0xd2, 0x96, 0x53, 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  198. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1, 0x8a, 0xc5, 0xbe, 0x72, 0x5c,
  199. 0xa7, 0xe7, 0x91, 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66, 0x48, 0xc7,
  200. 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9, 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46,
  201. 0x3e, 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d, 0x9f, 0x2b, 0x96, 0x17,
  202. 0x39, 0x6d, 0x30, 0x8d, 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75, 0xe6,
  203. 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa, 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda,
  204. 0x76, 0x93, 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c, 0xe5, 0xb7, 0xc8,
  205. 0x09, 0xae, 0x6f, 0x50, 0x35, 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  206. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76, 0xcc, 0x16, 0x30, 0xdb, 0x0c,
  207. 0xfc, 0xc5, 0x62, 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0, 0x36, 0xf6,
  208. 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40, 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3,
  209. 0x1a, 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4, 0x48, 0x4e, 0x22, 0x73,
  210. 0x6f, 0xc3, 0x5f, 0xd4, 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90, 0xab,
  211. 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58, 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf,
  212. 0x68, 0x1f, 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6, 0xb6, 0x06, 0x65,
  213. 0xf7, 0xa6, 0xdc, 0x00, 0x67, 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  214. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f, 0x93
  215. };
  216. /* dh2048 g */
  217. static const unsigned char dhg[] =
  218. {
  219. 0x02,
  220. };
  221. #endif /* !NO_WOLFSSL_SERVER */
  222. #endif /* !NO_DH */
  223. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  224. struct group_info {
  225. word16 group;
  226. const char *name;
  227. };
  228. static struct group_info groups[] = {
  229. { WOLFSSL_ECC_SECP160K1, "ECC_SECP160K1" },
  230. { WOLFSSL_ECC_SECP160R1, "ECC_SECP160R1" },
  231. { WOLFSSL_ECC_SECP160R2, "ECC_SECP160R2" },
  232. { WOLFSSL_ECC_SECP192K1, "ECC_SECP192K1" },
  233. { WOLFSSL_ECC_SECP192R1, "ECC_SECP192R1" },
  234. { WOLFSSL_ECC_SECP224K1, "ECC_SECP224K1" },
  235. { WOLFSSL_ECC_SECP224R1, "ECC_SECP224R1" },
  236. { WOLFSSL_ECC_SECP256K1, "ECC_SECP256K1" },
  237. { WOLFSSL_ECC_SECP256R1, "ECC_SECP256R1" },
  238. { WOLFSSL_ECC_SECP384R1, "ECC_SECP384R1" },
  239. { WOLFSSL_ECC_SECP521R1, "ECC_SECP521R1" },
  240. { WOLFSSL_ECC_BRAINPOOLP256R1, "ECC_BRAINPOOLP256R1" },
  241. { WOLFSSL_ECC_BRAINPOOLP384R1, "ECC_BRAINPOOLP384R1" },
  242. { WOLFSSL_ECC_BRAINPOOLP512R1, "ECC_BRAINPOOLP512R1" },
  243. { WOLFSSL_ECC_X25519, "ECC_X25519" },
  244. { WOLFSSL_ECC_X448, "ECC_X448" },
  245. { WOLFSSL_FFDHE_2048, "FFDHE_2048" },
  246. { WOLFSSL_FFDHE_3072, "FFDHE_3072" },
  247. { WOLFSSL_FFDHE_4096, "FFDHE_4096" },
  248. { WOLFSSL_FFDHE_6144, "FFDHE_6144" },
  249. { WOLFSSL_FFDHE_8192, "FFDHE_8192" },
  250. #ifdef HAVE_PQC
  251. { WOLFSSL_KYBER_LEVEL1, "KYBER_LEVEL1" },
  252. { WOLFSSL_KYBER_LEVEL3, "KYBER_LEVEL3" },
  253. { WOLFSSL_KYBER_LEVEL5, "KYBER_LEVEL5" },
  254. { WOLFSSL_P256_KYBER_LEVEL1, "P256_KYBER_LEVEL1" },
  255. { WOLFSSL_P384_KYBER_LEVEL3, "P384_KYBER_LEVEL3" },
  256. { WOLFSSL_P521_KYBER_LEVEL5, "P521_KYBER_LEVEL5" },
  257. #endif
  258. { 0, NULL }
  259. };
  260. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  261. #ifdef HAVE_PTHREAD
  262. typedef struct {
  263. unsigned char buf[MEM_BUFFER_SZ];
  264. int write_bytes;
  265. int write_idx;
  266. int read_bytes;
  267. int read_idx;
  268. pthread_t tid;
  269. pthread_mutex_t mutex;
  270. pthread_cond_t cond;
  271. int done;
  272. } memBuf_t;
  273. #endif
  274. typedef struct {
  275. double connTime;
  276. double rxTime;
  277. double txTime;
  278. int connCount;
  279. int rxTotal;
  280. int txTotal;
  281. } stats_t;
  282. typedef struct {
  283. int shutdown;
  284. int sockFd;
  285. int ret;
  286. } side_t;
  287. typedef struct {
  288. const char* cipher;
  289. word16 group;
  290. const char* host;
  291. word32 port;
  292. int packetSize; /* The data payload size in the packet */
  293. int maxSize;
  294. int runTimeSec;
  295. int showPeerInfo;
  296. int showVerbose;
  297. #ifndef NO_WOLFSSL_SERVER
  298. int listenFd;
  299. #endif
  300. #ifdef WOLFSSL_DTLS
  301. int doDTLS;
  302. struct sockaddr_in serverAddr;
  303. struct sockaddr_in clientAddr;
  304. #ifdef HAVE_PTHREAD
  305. int serverReady;
  306. int clientOrserverOnly;
  307. pthread_mutex_t dtls_mutex;
  308. pthread_cond_t dtls_cond;
  309. #endif
  310. #endif
  311. side_t client;
  312. side_t server;
  313. #ifdef HAVE_PTHREAD
  314. int useLocalMem;
  315. /* client messages to server in memory */
  316. memBuf_t to_server;
  317. /* server messages to client in memory */
  318. memBuf_t to_client;
  319. /* Indicates that the server is ready for connection */
  320. int serverListening;
  321. #endif
  322. /* server */
  323. stats_t server_stats;
  324. /* client */
  325. stats_t client_stats;
  326. } info_t;
  327. /* Global vars for argument parsing */
  328. int myoptind = 0;
  329. char* myoptarg = NULL;
  330. #ifdef WOLFSSL_DTLS
  331. int DoneHandShake = 0;
  332. #endif
  333. static double gettime_secs(int reset)
  334. {
  335. struct timeval tv;
  336. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  337. (void)reset;
  338. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  339. }
  340. #ifdef HAVE_PTHREAD
  341. /* server send callback */
  342. static int ServerMemSend(info_t* info, char* buf, int sz)
  343. {
  344. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_client.mutex));
  345. #ifndef BENCH_USE_NONBLOCK
  346. /* check for overflow */
  347. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  348. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
  349. fprintf(stderr, "ServerMemSend overflow\n");
  350. return -1;
  351. }
  352. #else
  353. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  354. sz = MEM_BUFFER_SZ - info->to_client.write_idx;
  355. }
  356. #endif
  357. XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
  358. info->to_client.write_idx += sz;
  359. info->to_client.write_bytes += sz;
  360. PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_client.cond));
  361. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
  362. #ifdef BENCH_USE_NONBLOCK
  363. if (sz == 0) {
  364. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  365. }
  366. #endif
  367. return sz;
  368. }
  369. /* server recv callback */
  370. static int ServerMemRecv(info_t* info, char* buf, int sz)
  371. {
  372. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_server.mutex));
  373. #ifndef BENCH_USE_NONBLOCK
  374. while (info->to_server.write_idx - info->to_server.read_idx < sz &&
  375. !info->to_client.done) {
  376. PTHREAD_CHECK_RET(pthread_cond_wait(&info->to_server.cond,
  377. &info->to_server.mutex));
  378. }
  379. #else
  380. if (info->to_server.write_idx - info->to_server.read_idx < sz) {
  381. sz = info->to_server.write_idx - info->to_server.read_idx;
  382. }
  383. #endif
  384. XMEMCPY(buf, &info->to_server.buf[info->to_server.read_idx], sz);
  385. info->to_server.read_idx += sz;
  386. info->to_server.read_bytes += sz;
  387. /* if the rx has caught up with pending then reset buffer positions */
  388. if (info->to_server.read_bytes == info->to_server.write_bytes) {
  389. info->to_server.read_bytes = info->to_server.read_idx = 0;
  390. info->to_server.write_bytes = info->to_server.write_idx = 0;
  391. }
  392. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
  393. if (info->to_client.done != 0) {
  394. return -1;
  395. }
  396. #ifdef BENCH_USE_NONBLOCK
  397. if (sz == 0) {
  398. return WOLFSSL_CBIO_ERR_WANT_READ;
  399. }
  400. #endif
  401. return sz;
  402. }
  403. /* client send callback */
  404. static int ClientMemSend(info_t* info, char* buf, int sz)
  405. {
  406. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_server.mutex));
  407. #ifndef BENCH_USE_NONBLOCK
  408. /* check for overflow */
  409. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  410. fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
  411. info->to_server.write_idx, sz, MEM_BUFFER_SZ);
  412. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
  413. return -1;
  414. }
  415. #else
  416. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  417. sz = MEM_BUFFER_SZ - info->to_server.write_idx;
  418. }
  419. #endif
  420. XMEMCPY(&info->to_server.buf[info->to_server.write_idx], buf, sz);
  421. info->to_server.write_idx += sz;
  422. info->to_server.write_bytes += sz;
  423. PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_server.cond));
  424. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
  425. #ifdef BENCH_USE_NONBLOCK
  426. if (sz == 0) {
  427. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  428. }
  429. #endif
  430. return sz;
  431. }
  432. /* client recv callback */
  433. static int ClientMemRecv(info_t* info, char* buf, int sz)
  434. {
  435. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_client.mutex));
  436. #ifndef BENCH_USE_NONBLOCK
  437. while (info->to_client.write_idx - info->to_client.read_idx < sz &&
  438. !info->to_server.done) {
  439. PTHREAD_CHECK_RET(pthread_cond_wait(&info->to_client.cond,
  440. &info->to_client.mutex));
  441. }
  442. #else
  443. if (info->to_client.write_idx - info->to_client.read_idx < sz) {
  444. sz = info->to_client.write_idx - info->to_client.read_idx;
  445. }
  446. #endif
  447. XMEMCPY(buf, &info->to_client.buf[info->to_client.read_idx], sz);
  448. info->to_client.read_idx += sz;
  449. info->to_client.read_bytes += sz;
  450. /* if the rx has caught up with pending then reset buffer positions */
  451. if (info->to_client.read_bytes == info->to_client.write_bytes) {
  452. info->to_client.read_bytes = info->to_client.read_idx = 0;
  453. info->to_client.write_bytes = info->to_client.write_idx = 0;
  454. }
  455. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
  456. if (info->to_server.done != 0) {
  457. return -1;
  458. }
  459. #ifdef BENCH_USE_NONBLOCK
  460. if (sz == 0) {
  461. return WOLFSSL_CBIO_ERR_WANT_READ;
  462. }
  463. #endif
  464. return sz;
  465. }
  466. #endif /* HAVE_PTHREAD */
  467. static int SocketRecv(int sockFd, char* buf, int sz)
  468. {
  469. int recvd = (int)recv(sockFd, buf, sz, 0);
  470. if (recvd == -1) {
  471. switch (errno) {
  472. #if EAGAIN != SOCKET_EWOULDBLOCK
  473. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  474. #endif
  475. case SOCKET_EWOULDBLOCK:
  476. return WOLFSSL_CBIO_ERR_WANT_READ;
  477. case SOCKET_ECONNRESET:
  478. return WOLFSSL_CBIO_ERR_CONN_RST;
  479. case SOCKET_EINTR:
  480. return WOLFSSL_CBIO_ERR_ISR;
  481. case SOCKET_ECONNREFUSED: /* DTLS case */
  482. return WOLFSSL_CBIO_ERR_WANT_READ;
  483. case SOCKET_ECONNABORTED:
  484. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  485. default:
  486. return WOLFSSL_CBIO_ERR_GENERAL;
  487. }
  488. }
  489. else if (recvd == 0) {
  490. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  491. }
  492. return recvd;
  493. }
  494. static int SocketSend(int sockFd, char* buf, int sz)
  495. {
  496. int sent = (int)send(sockFd, buf, sz, 0);
  497. if (sent == -1) {
  498. switch (errno) {
  499. #if EAGAIN != SOCKET_EWOULDBLOCK
  500. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  501. #endif
  502. case SOCKET_EWOULDBLOCK:
  503. return WOLFSSL_CBIO_ERR_WANT_READ;
  504. case SOCKET_ECONNRESET:
  505. return WOLFSSL_CBIO_ERR_CONN_RST;
  506. case SOCKET_EINTR:
  507. return WOLFSSL_CBIO_ERR_ISR;
  508. case SOCKET_EPIPE:
  509. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  510. default:
  511. return WOLFSSL_CBIO_ERR_GENERAL;
  512. }
  513. }
  514. else if (sent == 0) {
  515. return 0;
  516. }
  517. return sent;
  518. }
  519. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  520. static int ReceiveFrom(WOLFSSL *ssl, int sd, char *buf, int sz)
  521. {
  522. int recvd;
  523. int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
  524. struct sockaddr peer;
  525. socklen_t peerSz = 0;
  526. if (DoneHandShake) {
  527. dtls_timeout = 0;
  528. }
  529. if (!wolfSSL_get_using_nonblock(ssl)) {
  530. struct timeval timeout;
  531. XMEMSET(&timeout, 0, sizeof(timeout));
  532. timeout.tv_sec = dtls_timeout;
  533. if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
  534. sizeof(timeout)) != 0) {
  535. fprintf(stderr, "setsockopt rcvtimeo failed\n");
  536. }
  537. }
  538. recvd = (int)recvfrom(sd, buf, sz, 0, (SOCKADDR*)&peer, &peerSz);
  539. if (recvd < 0) {
  540. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  541. if (wolfSSL_dtls_get_using_nonblock(ssl)) {
  542. return WOLFSSL_CBIO_ERR_WANT_READ;
  543. }
  544. else {
  545. return WOLFSSL_CBIO_ERR_TIMEOUT;
  546. }
  547. }
  548. else if (errno == SOCKET_ECONNRESET) {
  549. return WOLFSSL_CBIO_ERR_CONN_RST;
  550. }
  551. else if (errno == SOCKET_EINTR) {
  552. return WOLFSSL_CBIO_ERR_ISR;
  553. }
  554. else if (errno == SOCKET_ECONNREFUSED) {
  555. return WOLFSSL_CBIO_ERR_WANT_READ;
  556. }
  557. else {
  558. return WOLFSSL_CBIO_ERR_GENERAL;
  559. }
  560. }
  561. else {
  562. if (recvd == 0) {
  563. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  564. }
  565. }
  566. return recvd;
  567. }
  568. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  569. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  570. static int SendTo(int sd, char *buf, int sz, const struct sockaddr *peer,
  571. socklen_t peerSz)
  572. {
  573. int sent;
  574. sent = (int)sendto(sd, buf, sz, 0, peer, peerSz);
  575. if (sent < 0) {
  576. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  577. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  578. }
  579. else if (errno == SOCKET_ECONNRESET) {
  580. return WOLFSSL_CBIO_ERR_CONN_RST;
  581. }
  582. else if (errno == SOCKET_EINTR) {
  583. return WOLFSSL_CBIO_ERR_ISR;
  584. }
  585. else if (errno == SOCKET_EPIPE) {
  586. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  587. }
  588. else {
  589. return WOLFSSL_CBIO_ERR_GENERAL;
  590. }
  591. }
  592. return sent;
  593. }
  594. static int myDoneHsCb(WOLFSSL* ssl, void* user_ctx)
  595. {
  596. (void) ssl;
  597. (void) user_ctx;
  598. DoneHandShake = 1;
  599. return 1;
  600. }
  601. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_CLIENT */
  602. #ifndef NO_WOLFSSL_SERVER
  603. static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  604. {
  605. info_t* info = (info_t*)ctx;
  606. (void)ssl;
  607. #ifdef HAVE_PTHREAD
  608. if (info->useLocalMem) {
  609. return ServerMemSend(info, buf, sz);
  610. }
  611. #endif
  612. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  613. if (info->doDTLS) {
  614. return SendTo(info->server.sockFd, buf, sz,
  615. (const struct sockaddr*)&info->clientAddr, sizeof(info->clientAddr));
  616. }
  617. else
  618. #endif
  619. {
  620. return SocketSend(info->server.sockFd, buf, sz);
  621. }
  622. }
  623. static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  624. {
  625. info_t* info = (info_t*)ctx;
  626. (void)ssl;
  627. #ifdef HAVE_PTHREAD
  628. if (info->useLocalMem) {
  629. return ServerMemRecv(info, buf, sz);
  630. }
  631. #endif
  632. #ifdef WOLFSSL_DTLS
  633. if (info->doDTLS) {
  634. return ReceiveFrom(ssl, info->server.sockFd, buf, sz);
  635. }
  636. else
  637. #endif
  638. {
  639. return SocketRecv(info->server.sockFd, buf, sz);
  640. }
  641. }
  642. #endif /* !NO_WOLFSSL_SERVER */
  643. #ifndef NO_WOLFSSL_CLIENT
  644. static int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  645. {
  646. info_t* info = (info_t*)ctx;
  647. (void)ssl;
  648. #ifdef HAVE_PTHREAD
  649. if (info->useLocalMem) {
  650. return ClientMemSend(info, buf, sz);
  651. }
  652. #endif
  653. #ifdef WOLFSSL_DTLS
  654. if (info->doDTLS) {
  655. return SendTo(info->client.sockFd, buf, sz,
  656. (const struct sockaddr*)&info->serverAddr, sizeof(info->serverAddr));
  657. }
  658. else
  659. #endif
  660. {
  661. return SocketSend(info->client.sockFd, buf, sz);
  662. }
  663. }
  664. static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  665. {
  666. info_t* info = (info_t*)ctx;
  667. (void)ssl;
  668. #ifdef HAVE_PTHREAD
  669. if (info->useLocalMem) {
  670. return ClientMemRecv(info, buf, sz);
  671. }
  672. #endif
  673. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  674. if (info->doDTLS) {
  675. return ReceiveFrom(ssl, info->client.sockFd, buf, sz);
  676. }
  677. else
  678. #endif
  679. {
  680. return SocketRecv(info->client.sockFd, buf, sz);
  681. }
  682. }
  683. #endif /* !NO_WOLFSSL_CLIENT */
  684. static void CloseAndCleanupSocket(int* sockFd)
  685. {
  686. if (*sockFd != -1) {
  687. close(*sockFd);
  688. *sockFd = -1;
  689. }
  690. #ifdef WOLFSSL_DTLS
  691. DoneHandShake = 0;
  692. #endif
  693. }
  694. #ifdef BENCH_USE_NONBLOCK
  695. static int SetSocketNonBlocking(int sockFd)
  696. {
  697. int flags = fcntl(sockFd, F_GETFL, 0);
  698. if (flags < 0) {
  699. fprintf(stderr, "fcntl get failed\n");
  700. return -1;
  701. }
  702. flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK);
  703. if (flags < 0) {
  704. fprintf(stderr, "fcntl set failed\n");
  705. return -1;
  706. }
  707. return 0;
  708. }
  709. #endif
  710. #ifndef NO_WOLFSSL_CLIENT
  711. static int SetupSocketAndConnect(info_t* info, const char* host,
  712. word32 port)
  713. {
  714. struct sockaddr_in servAddr;
  715. struct hostent* entry;
  716. /* Setup server address */
  717. XMEMSET(&servAddr, 0, sizeof(servAddr));
  718. servAddr.sin_family = AF_INET;
  719. servAddr.sin_port = htons(port);
  720. /* Resolve host */
  721. entry = gethostbyname(host);
  722. if (entry) {
  723. XMEMCPY(&servAddr.sin_addr.s_addr, entry->h_addr_list[0],
  724. entry->h_length);
  725. }
  726. else {
  727. servAddr.sin_addr.s_addr = inet_addr(host);
  728. }
  729. #ifdef WOLFSSL_DTLS
  730. if (info->doDTLS) {
  731. /* Create the SOCK_DGRAM socket type is implemented on the User
  732. * Datagram Protocol/Internet Protocol(UDP/IP protocol).*/
  733. if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  734. fprintf(stderr, "ERROR: failed to create the SOCK_DGRAM socket\n");
  735. return -1;
  736. }
  737. XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr));
  738. }
  739. else
  740. #endif
  741. {
  742. /* Create a socket that uses an Internet IPv4 address,
  743. * Sets the socket to be stream based (TCP),
  744. * 0 means choose the default protocol. */
  745. if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  746. fprintf(stderr, "ERROR: failed to create the socket\n");
  747. return -1;
  748. }
  749. /* Connect to the server */
  750. #ifdef HAVE_PTHREAD
  751. while ((info->serverListening == 0) && (info->server.shutdown == 0)) {
  752. if (info->showVerbose) {
  753. fprintf(stderr, "Waiting for server to listen...\n");
  754. }
  755. XSLEEP_MS(1);
  756. }
  757. #endif
  758. if (info->server.shutdown == 1) {
  759. fprintf(stderr, "ERROR: server side has shutdown\n");
  760. return -1;
  761. }
  762. if (connect(info->client.sockFd, (struct sockaddr*)&servAddr,
  763. sizeof(servAddr)) == -1) {
  764. fprintf(stderr, "ERROR: failed to connect\n");
  765. return -1;
  766. }
  767. }
  768. #ifdef BENCH_USE_NONBLOCK
  769. if (SetSocketNonBlocking(info->client.sockFd) != 0) {
  770. return -1;
  771. }
  772. #endif
  773. if (info->showVerbose) {
  774. fprintf(stderr, "Connected to %s on port %d\n", host, port);
  775. }
  776. return 0;
  777. }
  778. static int bench_tls_client(info_t* info)
  779. {
  780. byte *writeBuf = NULL, *readBuf = NULL;
  781. double start, total = 0;
  782. int ret, readBufSz;
  783. WOLFSSL_CTX* cli_ctx = NULL;
  784. WOLFSSL* cli_ssl = NULL;
  785. int haveShownPeerInfo = 0;
  786. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  787. int total_sz;
  788. total = gettime_secs(0);
  789. /* set up client */
  790. #ifdef WOLFSSL_DTLS
  791. if (info->doDTLS) {
  792. if (tls13) {
  793. #ifdef WOLFSSL_DTLS13
  794. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_client_method());
  795. #endif
  796. }
  797. else {
  798. #ifndef WOLFSSL_NO_TLS12
  799. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  800. #endif
  801. }
  802. }
  803. else
  804. #endif
  805. {
  806. #ifdef WOLFSSL_TLS13
  807. if (tls13) {
  808. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  809. }
  810. else
  811. #endif
  812. {
  813. #if !defined(WOLFSSL_TLS13)
  814. cli_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  815. #elif !defined(WOLFSSL_NO_TLS12)
  816. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  817. #endif
  818. }
  819. }
  820. if (cli_ctx == NULL) {
  821. fprintf(stderr, "error creating ctx\n");
  822. ret = MEMORY_E; goto exit;
  823. }
  824. #ifndef NO_CERTS
  825. #ifdef HAVE_ECC
  826. if (XSTRSTR(info->cipher, "ECDSA")) {
  827. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_ecc_cert_der_256,
  828. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1);
  829. }
  830. else
  831. #endif
  832. {
  833. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_cert_der_2048,
  834. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  835. }
  836. if (ret != WOLFSSL_SUCCESS) {
  837. fprintf(stderr, "error loading CA\n");
  838. goto exit;
  839. }
  840. #endif
  841. wolfSSL_CTX_SetIOSend(cli_ctx, ClientSend);
  842. wolfSSL_CTX_SetIORecv(cli_ctx, ClientRecv);
  843. /* set cipher suite */
  844. ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
  845. if (ret != WOLFSSL_SUCCESS) {
  846. fprintf(stderr, "error setting cipher suite\n");
  847. goto exit;
  848. }
  849. #ifndef NO_DH
  850. ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
  851. if (ret != WOLFSSL_SUCCESS) {
  852. fprintf(stderr, "Error setting minimum DH key size\n");
  853. goto exit;
  854. }
  855. #endif
  856. #ifndef NO_PSK
  857. wolfSSL_CTX_set_psk_client_callback(cli_ctx, my_psk_client_cb);
  858. #ifdef WOLFSSL_TLS13
  859. #if !defined(WOLFSSL_PSK_TLS13_CB) && !defined(WOLFSSL_PSK_ONE_ID)
  860. wolfSSL_CTX_set_psk_client_cs_callback(cli_ctx, my_psk_client_cs_cb);
  861. #else
  862. wolfSSL_CTX_set_psk_client_tls13_callback(cli_ctx, my_psk_client_tls13_cb);
  863. #endif
  864. #endif
  865. wolfSSL_CTX_set_psk_callback_ctx(cli_ctx, (void*)info->cipher);
  866. #endif /* !NO_PSK */
  867. /* Allocate and initialize a packet sized buffer */
  868. writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL,
  869. DYNAMIC_TYPE_TMP_BUFFER);
  870. if (writeBuf == NULL) {
  871. fprintf(stderr, "failed to allocate write memory\n");
  872. ret = MEMORY_E; goto exit;
  873. }
  874. /* Allocate read buffer */
  875. readBufSz = info->packetSize;
  876. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  877. if (readBuf == NULL) {
  878. fprintf(stderr, "failed to allocate read memory\n");
  879. ret = MEMORY_E; goto exit;
  880. }
  881. /* BENCHMARK CONNECTIONS LOOP */
  882. while (!info->client.shutdown) {
  883. int writeSz = info->packetSize;
  884. #ifdef BENCH_USE_NONBLOCK
  885. int err;
  886. #endif
  887. #ifdef HAVE_PTHREAD
  888. if (!info->useLocalMem)
  889. #endif
  890. {
  891. /* Setup socket and connection */
  892. ret = SetupSocketAndConnect(info, info->host, info->port);
  893. if (ret != 0) goto exit;
  894. }
  895. cli_ssl = wolfSSL_new(cli_ctx);
  896. if (cli_ssl == NULL) {
  897. fprintf(stderr, "error creating client object\n");
  898. goto exit;
  899. }
  900. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  901. if (info->group != 0) {
  902. ret = wolfSSL_UseKeyShare(cli_ssl, info->group);
  903. if (ret != WOLFSSL_SUCCESS) {
  904. fprintf(stderr, "error setting client key share.\n");
  905. goto exit;
  906. }
  907. }
  908. #endif
  909. #ifdef WOLFSSL_DTLS
  910. if (info->doDTLS) {
  911. ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr,
  912. sizeof(info->serverAddr));
  913. if (ret != WOLFSSL_SUCCESS) {
  914. fprintf(stderr, "error setting dtls peer\n");
  915. goto exit;
  916. }
  917. ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL);
  918. if (ret != WOLFSSL_SUCCESS) {
  919. fprintf(stderr, "error handshake done callback\n");
  920. goto exit;
  921. }
  922. }
  923. #endif
  924. wolfSSL_SetIOReadCtx(cli_ssl, info);
  925. wolfSSL_SetIOWriteCtx(cli_ssl, info);
  926. #if defined(HAVE_PTHREAD) && defined(WOLFSSL_DTLS)
  927. /* synchronize with server */
  928. if (info->doDTLS && !info->clientOrserverOnly) {
  929. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->dtls_mutex));
  930. if (info->serverReady != 1) {
  931. PTHREAD_CHECK_RET(pthread_cond_wait(&info->dtls_cond,
  932. &info->dtls_mutex));
  933. }
  934. /* for next loop */
  935. info->serverReady = 0;
  936. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->dtls_mutex));
  937. }
  938. #endif
  939. /* perform connect */
  940. start = gettime_secs(1);
  941. #ifndef BENCH_USE_NONBLOCK
  942. ret = wolfSSL_connect(cli_ssl);
  943. #else
  944. do
  945. {
  946. ret = wolfSSL_connect(cli_ssl);
  947. err = wolfSSL_get_error(cli_ssl, ret);
  948. }
  949. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  950. #endif
  951. start = gettime_secs(0) - start;
  952. if (ret != WOLFSSL_SUCCESS) {
  953. fprintf(stderr, "error connecting client\n");
  954. ret = wolfSSL_get_error(cli_ssl, ret);
  955. goto exit;
  956. }
  957. info->client_stats.connTime += start;
  958. info->client_stats.connCount++;
  959. if ((info->showPeerInfo) && (!haveShownPeerInfo)) {
  960. haveShownPeerInfo = 1;
  961. showPeer(cli_ssl);
  962. }
  963. /* check for run time completion and issue shutdown */
  964. if (gettime_secs(0) - total >= info->runTimeSec) {
  965. info->client.shutdown = 1;
  966. writeSz = (int)XSTRLEN(kShutdown) + 1;
  967. XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */
  968. if (info->showVerbose) {
  969. fprintf(stderr, "Sending shutdown\n");
  970. }
  971. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  972. if (ret < 0) {
  973. fprintf(stderr, "error on client write\n");
  974. ret = wolfSSL_get_error(cli_ssl, ret);
  975. goto exit;
  976. }
  977. }
  978. else {
  979. XMEMSET(writeBuf, 0, info->packetSize);
  980. XSTRNCPY((char*)writeBuf, kTestStr, info->packetSize);
  981. }
  982. /* write / read echo loop */
  983. ret = 0;
  984. total_sz = 0;
  985. while (ret == 0 && total_sz < info->maxSize && !info->client.shutdown) {
  986. /* write test message to server */
  987. start = gettime_secs(1);
  988. #ifndef BENCH_USE_NONBLOCK
  989. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  990. #else
  991. do {
  992. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  993. err = wolfSSL_get_error(cli_ssl, ret);
  994. }
  995. while (err == WOLFSSL_ERROR_WANT_WRITE);
  996. #endif
  997. info->client_stats.txTime += gettime_secs(0) - start;
  998. if (ret < 0) {
  999. fprintf(stderr, "error on client write\n");
  1000. ret = wolfSSL_get_error(cli_ssl, ret);
  1001. goto exit;
  1002. }
  1003. info->client_stats.txTotal += ret;
  1004. total_sz += ret;
  1005. /* read echo of message from server */
  1006. XMEMSET(readBuf, 0, readBufSz);
  1007. start = gettime_secs(1);
  1008. #ifndef BENCH_USE_NONBLOCK
  1009. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1010. #else
  1011. do {
  1012. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1013. err = wolfSSL_get_error(cli_ssl, ret);
  1014. }
  1015. while (err == WOLFSSL_ERROR_WANT_READ);
  1016. #endif
  1017. info->client_stats.rxTime += gettime_secs(0) - start;
  1018. if (ret < 0) {
  1019. fprintf(stderr, "error on client read\n");
  1020. ret = wolfSSL_get_error(cli_ssl, ret);
  1021. goto exit;
  1022. }
  1023. info->client_stats.rxTotal += ret;
  1024. ret = 0; /* reset return code */
  1025. /* validate echo */
  1026. if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) {
  1027. fprintf(stderr, "echo check failed!\n");
  1028. ret = wolfSSL_get_error(cli_ssl, ret);
  1029. goto exit;
  1030. }
  1031. }
  1032. CloseAndCleanupSocket(&info->client.sockFd);
  1033. wolfSSL_free(cli_ssl);
  1034. cli_ssl = NULL;
  1035. }
  1036. exit:
  1037. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1038. fprintf(stderr, "Client Error: %d (%s)\n", ret,
  1039. wolfSSL_ERR_reason_error_string(ret));
  1040. }
  1041. /* clean up */
  1042. CloseAndCleanupSocket(&info->client.sockFd);
  1043. if (cli_ssl != NULL) {
  1044. wolfSSL_free(cli_ssl);
  1045. }
  1046. if (cli_ctx != NULL) {
  1047. wolfSSL_CTX_free(cli_ctx);
  1048. }
  1049. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1050. XFREE(writeBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1051. info->client.ret = ret;
  1052. (void)tls13;
  1053. return ret;
  1054. }
  1055. #ifdef HAVE_PTHREAD
  1056. static void* client_thread(void* args)
  1057. {
  1058. int ret;
  1059. info_t* info = (info_t*)args;
  1060. ret = bench_tls_client(info);
  1061. PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_server.cond));
  1062. info->to_client.done = 1;
  1063. info->client.ret = ret;
  1064. return NULL;
  1065. }
  1066. #endif /* HAVE_PTHREAD */
  1067. #endif /* !NO_WOLFSSL_CLIENT */
  1068. #ifndef NO_WOLFSSL_SERVER
  1069. static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
  1070. {
  1071. struct sockaddr_in servAddr;
  1072. #if defined(_MSC_VER) || defined(__MINGW32__)
  1073. char optval = 1;
  1074. #else
  1075. int optval = 1;
  1076. #endif
  1077. #ifndef WOLFSSL_DTLS
  1078. (void) doDTLS;
  1079. #endif
  1080. /* Setup server address */
  1081. XMEMSET(&servAddr, 0, sizeof(servAddr));
  1082. servAddr.sin_family = AF_INET;
  1083. servAddr.sin_port = htons(port);
  1084. servAddr.sin_addr.s_addr = INADDR_ANY;
  1085. #ifdef WOLFSSL_DTLS
  1086. if (doDTLS) {
  1087. /* Create a socket that is implemented on the User Datagram Protocol/
  1088. * Interet Protocol(UDP/IP protocol). */
  1089. if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  1090. fprintf(stderr, "ERROR: failed to create the socket\n");
  1091. return -1;
  1092. }
  1093. }
  1094. else
  1095. #endif
  1096. {
  1097. /* Create a socket that uses an Internet IPv4 address,
  1098. * Sets the socket to be stream based (TCP),
  1099. * 0 means choose the default protocol. */
  1100. if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  1101. fprintf(stderr, "ERROR: failed to create the socket\n");
  1102. return -1;
  1103. }
  1104. /* allow reuse */
  1105. if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR,
  1106. &optval, sizeof(optval)) == -1) {
  1107. fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
  1108. return -1;
  1109. }
  1110. /* Listen for the client. */
  1111. if (bind(*listenFd, (struct sockaddr*)&servAddr,
  1112. sizeof(servAddr)) == -1) {
  1113. fprintf(stderr, "ERROR: failed to bind\n");
  1114. return -1;
  1115. }
  1116. }
  1117. if (listen(*listenFd, 5) != 0) {
  1118. fprintf(stderr, "ERROR: failed to listen\n");
  1119. return -1;
  1120. }
  1121. #ifdef BENCH_USE_NONBLOCK
  1122. if (SetSocketNonBlocking(*listenFd) != 0) {
  1123. return -1;
  1124. }
  1125. #endif
  1126. return 0;
  1127. }
  1128. static int SocketWaitClient(info_t* info)
  1129. {
  1130. int connd;
  1131. struct sockaddr_in clientAddr;
  1132. socklen_t size = sizeof(clientAddr);
  1133. #ifdef WOLFSSL_DTLS
  1134. char msg[64];
  1135. #endif
  1136. #ifdef WOLFSSL_DTLS
  1137. if (info->doDTLS) {
  1138. #ifdef HAVE_PTHREAD
  1139. if (!info->clientOrserverOnly) {
  1140. PTHREAD_CHECK_RET(pthread_mutex_lock(&info->dtls_mutex));
  1141. info->serverReady = 1;
  1142. PTHREAD_CHECK_RET(pthread_cond_signal(&info->dtls_cond));
  1143. PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->dtls_mutex));
  1144. }
  1145. #endif
  1146. connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
  1147. MSG_PEEK, (struct sockaddr*)&clientAddr, &size);
  1148. if (connd < -1) {
  1149. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1150. return -1;
  1151. }
  1152. XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr));
  1153. info->server.sockFd = info->listenFd;
  1154. }
  1155. else
  1156. #endif
  1157. {
  1158. #ifdef HAVE_PTHREAD
  1159. info->serverListening = 1;
  1160. #endif
  1161. if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr,
  1162. &size)) == -1) {
  1163. if (errno == SOCKET_EWOULDBLOCK) {
  1164. return -2;
  1165. }
  1166. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1167. return -1;
  1168. }
  1169. info->server.sockFd = connd;
  1170. }
  1171. if (info->showVerbose) {
  1172. fprintf(stderr, "Got client %d\n", connd);
  1173. }
  1174. return 0;
  1175. }
  1176. static void CloseAndCleanupListenSocket(int* listenFd)
  1177. {
  1178. if (*listenFd != -1) {
  1179. close(*listenFd);
  1180. *listenFd = -1;
  1181. }
  1182. }
  1183. static int bench_tls_server(info_t* info)
  1184. {
  1185. byte *readBuf = NULL;
  1186. double start;
  1187. int ret, len = 0, readBufSz;
  1188. WOLFSSL_CTX* srv_ctx = NULL;
  1189. WOLFSSL* srv_ssl = NULL;
  1190. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  1191. int total_sz;
  1192. /* set up server */
  1193. #ifdef WOLFSSL_DTLS
  1194. if (info->doDTLS) {
  1195. if (tls13) {
  1196. #ifdef WOLFSSL_DTLS13
  1197. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_server_method());
  1198. #endif
  1199. }
  1200. else {
  1201. #ifndef WOLFSSL_NO_TLS12
  1202. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  1203. #endif
  1204. }
  1205. }
  1206. else
  1207. #endif
  1208. {
  1209. #ifdef WOLFSSL_TLS13
  1210. if (tls13) {
  1211. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1212. }
  1213. else
  1214. #endif
  1215. {
  1216. #if !defined(WOLFSSL_TLS13)
  1217. srv_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1218. #elif !defined(WOLFSSL_NO_TLS12)
  1219. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  1220. #endif
  1221. }
  1222. }
  1223. if (srv_ctx == NULL) {
  1224. fprintf(stderr, "error creating server ctx\n");
  1225. ret = MEMORY_E; goto exit;
  1226. }
  1227. #ifndef NO_CERTS
  1228. #ifdef HAVE_ECC
  1229. if (XSTRSTR(info->cipher, "ECDSA")) {
  1230. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, ecc_key_der_256,
  1231. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  1232. }
  1233. else
  1234. #endif
  1235. {
  1236. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048,
  1237. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  1238. }
  1239. if (ret != WOLFSSL_SUCCESS) {
  1240. fprintf(stderr, "error loading server key\n");
  1241. goto exit;
  1242. }
  1243. #ifdef HAVE_ECC
  1244. if (XSTRSTR(info->cipher, "ECDSA")) {
  1245. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, serv_ecc_der_256,
  1246. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  1247. }
  1248. else
  1249. #endif
  1250. {
  1251. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, server_cert_der_2048,
  1252. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1253. }
  1254. if (ret != WOLFSSL_SUCCESS) {
  1255. fprintf(stderr, "error loading server cert\n");
  1256. goto exit;
  1257. }
  1258. #endif /* !NO_CERTS */
  1259. wolfSSL_CTX_SetIOSend(srv_ctx, ServerSend);
  1260. wolfSSL_CTX_SetIORecv(srv_ctx, ServerRecv);
  1261. /* set cipher suite */
  1262. ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
  1263. if (ret != WOLFSSL_SUCCESS) {
  1264. fprintf(stderr, "error setting cipher suite\n");
  1265. goto exit;
  1266. }
  1267. #ifndef NO_DH
  1268. ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
  1269. if (ret != WOLFSSL_SUCCESS) {
  1270. fprintf(stderr, "Error setting minimum DH key size\n");
  1271. goto exit;
  1272. }
  1273. #endif
  1274. #ifndef NO_PSK
  1275. wolfSSL_CTX_set_psk_server_callback(srv_ctx, my_psk_server_cb);
  1276. #ifdef WOLFSSL_TLS13
  1277. wolfSSL_CTX_set_psk_server_tls13_callback(srv_ctx, my_psk_server_tls13_cb);
  1278. #endif
  1279. #endif /* !NO_PSK */
  1280. /* Allocate read buffer */
  1281. readBufSz = info->packetSize;
  1282. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1283. if (readBuf == NULL) {
  1284. fprintf(stderr, "failed to allocate read memory\n");
  1285. ret = MEMORY_E; goto exit;
  1286. }
  1287. /* BENCHMARK CONNECTIONS LOOP */
  1288. while (!info->server.shutdown) {
  1289. #ifdef BENCH_USE_NONBLOCK
  1290. int err;
  1291. #endif
  1292. #ifdef HAVE_PTHREAD
  1293. if (!info->useLocalMem)
  1294. #endif
  1295. {
  1296. /* Accept client connections */
  1297. ret = SocketWaitClient(info);
  1298. #ifdef BENCH_USE_NONBLOCK
  1299. if (ret == -2) {
  1300. XSLEEP_MS(0);
  1301. continue;
  1302. }
  1303. #endif
  1304. if (ret != 0) {
  1305. goto exit;
  1306. }
  1307. }
  1308. srv_ssl = wolfSSL_new(srv_ctx);
  1309. if (srv_ssl == NULL) {
  1310. fprintf(stderr, "error creating server object\n");
  1311. ret = MEMORY_E; goto exit;
  1312. }
  1313. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1314. if (info->group != 0) {
  1315. ret = wolfSSL_UseKeyShare(srv_ssl, info->group);
  1316. if (ret != WOLFSSL_SUCCESS) {
  1317. fprintf(stderr, "error setting server key share.\n");
  1318. goto exit;
  1319. }
  1320. }
  1321. #endif
  1322. #ifdef WOLFSSL_DTLS
  1323. if (info->doDTLS) {
  1324. ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr,
  1325. sizeof(info->clientAddr));
  1326. if (ret != WOLFSSL_SUCCESS) {
  1327. fprintf(stderr, "error setting dtls peer\n");
  1328. goto exit;
  1329. }
  1330. }
  1331. #endif
  1332. wolfSSL_SetIOReadCtx(srv_ssl, info);
  1333. wolfSSL_SetIOWriteCtx(srv_ssl, info);
  1334. #ifndef NO_DH
  1335. wolfSSL_SetTmpDH(srv_ssl, dhp, sizeof(dhp), dhg, sizeof(dhg));
  1336. #endif
  1337. /* accept TLS connection */
  1338. start = gettime_secs(1);
  1339. #ifndef BENCH_USE_NONBLOCK
  1340. ret = wolfSSL_accept(srv_ssl);
  1341. #else
  1342. do {
  1343. ret = wolfSSL_accept(srv_ssl);
  1344. err = wolfSSL_get_error(srv_ssl, ret);
  1345. }
  1346. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1347. #endif
  1348. start = gettime_secs(0) - start;
  1349. if (ret != WOLFSSL_SUCCESS) {
  1350. #ifdef HAVE_PTHREAD
  1351. if (info->to_client.done) {
  1352. ret = 0; /* done - success */
  1353. }
  1354. else
  1355. #endif
  1356. {
  1357. fprintf(stderr, "error on server accept\n");
  1358. ret = wolfSSL_get_error(srv_ssl, ret);
  1359. }
  1360. goto exit;
  1361. }
  1362. info->server_stats.connTime += start;
  1363. info->server_stats.connCount++;
  1364. /* echo loop */
  1365. ret = 0;
  1366. total_sz = 0;
  1367. while (ret == 0 && total_sz < info->maxSize) {
  1368. double rxTime;
  1369. /* read message from client */
  1370. XMEMSET(readBuf, 0, readBufSz);
  1371. start = gettime_secs(1);
  1372. #ifndef BENCH_USE_NONBLOCK
  1373. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1374. #else
  1375. do {
  1376. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1377. err = wolfSSL_get_error(srv_ssl, ret);
  1378. }
  1379. while (err == WOLFSSL_ERROR_WANT_READ);
  1380. #endif
  1381. rxTime = gettime_secs(0) - start;
  1382. /* shutdown signals, no more connections for this cipher */
  1383. if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
  1384. info->server.shutdown = 1;
  1385. if (info->showVerbose) {
  1386. fprintf(stderr, "Server shutdown done\n");
  1387. }
  1388. ret = 0; /* success */
  1389. break;
  1390. }
  1391. info->server_stats.rxTime += rxTime;
  1392. if (ret < 0) {
  1393. #ifdef HAVE_PTHREAD
  1394. if (info->to_client.done) {
  1395. ret = 0; /* done - success */
  1396. }
  1397. else
  1398. #endif
  1399. {
  1400. fprintf(stderr, "error on server read\n");
  1401. ret = wolfSSL_get_error(srv_ssl, ret);
  1402. }
  1403. goto exit;
  1404. }
  1405. info->server_stats.rxTotal += ret;
  1406. len = ret;
  1407. total_sz += ret;
  1408. /* write message back to client */
  1409. start = gettime_secs(1);
  1410. #ifndef BENCH_USE_NONBLOCK
  1411. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1412. #else
  1413. do {
  1414. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1415. err = wolfSSL_get_error(srv_ssl, ret);
  1416. }
  1417. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1418. #endif
  1419. info->server_stats.txTime += gettime_secs(0) - start;
  1420. if (ret < 0) {
  1421. fprintf(stderr, "error on server write\n");
  1422. ret = wolfSSL_get_error(srv_ssl, ret);
  1423. goto exit;
  1424. }
  1425. info->server_stats.txTotal += ret;
  1426. ret = 0; /* reset return code */
  1427. }
  1428. CloseAndCleanupSocket(&info->server.sockFd);
  1429. wolfSSL_free(srv_ssl);
  1430. srv_ssl = NULL;
  1431. #ifdef WOLFSSL_DTLS
  1432. if (info->doDTLS) {
  1433. SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1434. }
  1435. #endif
  1436. }
  1437. exit:
  1438. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1439. fprintf(stderr, "Server Error: %d (%s)\n", ret,
  1440. wolfSSL_ERR_reason_error_string(ret));
  1441. }
  1442. /* clean up */
  1443. CloseAndCleanupSocket(&info->server.sockFd);
  1444. if (srv_ssl != NULL) {
  1445. wolfSSL_free(srv_ssl);
  1446. }
  1447. if (srv_ctx != NULL) {
  1448. wolfSSL_CTX_free(srv_ctx);
  1449. }
  1450. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1451. info->server.ret = ret;
  1452. (void)tls13;
  1453. return ret;
  1454. }
  1455. #ifdef HAVE_PTHREAD
  1456. static void* server_thread(void* args)
  1457. {
  1458. int ret = 0;
  1459. info_t* info = (info_t*)args;
  1460. if (!info->useLocalMem) {
  1461. /* Setup TLS server listener */
  1462. #ifdef WOLFSSL_DTLS
  1463. ret = SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1464. #else
  1465. ret = SetupSocketAndListen(&info->listenFd, info->port, 0);
  1466. #endif
  1467. }
  1468. if (ret == 0) {
  1469. ret = bench_tls_server(info);
  1470. if (!info->useLocalMem) {
  1471. CloseAndCleanupListenSocket(&info->listenFd);
  1472. }
  1473. }
  1474. PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_client.cond));
  1475. info->to_server.done = 1;
  1476. info->server.ret = ret;
  1477. return NULL;
  1478. }
  1479. #endif /* HAVE_PTHREAD */
  1480. #endif /* !NO_WOLFSSL_SERVER */
  1481. static void print_stats(stats_t* wcStat, const char* desc, const char* cipher, const char *group, int verbose)
  1482. {
  1483. if (verbose) {
  1484. fprintf(stderr,
  1485. "wolfSSL %s Benchmark on %s with group %s:\n"
  1486. "\tTotal : %9d bytes\n"
  1487. "\tNum Conns : %9d\n"
  1488. "\tRx Total : %9.3f ms\n"
  1489. "\tTx Total : %9.3f ms\n"
  1490. "\tRx : %9.3f MB/s\n"
  1491. "\tTx : %9.3f MB/s\n"
  1492. "\tConnect : %9.3f ms\n"
  1493. "\tConnect Avg : %9.3f ms\n",
  1494. desc,
  1495. cipher,
  1496. group,
  1497. wcStat->txTotal + wcStat->rxTotal,
  1498. wcStat->connCount,
  1499. wcStat->rxTime * 1000,
  1500. wcStat->txTime * 1000,
  1501. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1502. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1503. wcStat->connTime * 1000,
  1504. wcStat->connTime * 1000 / wcStat->connCount);
  1505. }
  1506. else {
  1507. fprintf(stderr,
  1508. "%-6s %-33s %-25s %11d %9d %9.3f %9.3f %9.3f "
  1509. "%9.3f %17.3f %15.3f\n",
  1510. desc,
  1511. cipher,
  1512. group,
  1513. wcStat->txTotal + wcStat->rxTotal,
  1514. wcStat->connCount,
  1515. wcStat->rxTime * 1000,
  1516. wcStat->txTime * 1000,
  1517. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1518. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1519. wcStat->connTime * 1000,
  1520. wcStat->connTime * 1000 / wcStat->connCount);
  1521. }
  1522. }
  1523. static void Usage(void)
  1524. {
  1525. fprintf(stderr, "tls_bench " LIBWOLFSSL_VERSION_STRING
  1526. " NOTE: All files relative to wolfSSL home dir\n");
  1527. fprintf(stderr, "-? Help, print this usage\n");
  1528. fprintf(stderr, "-c Run as client only, no threading and uses sockets\n");
  1529. fprintf(stderr, "-s Run as server only, no threading and uses sockets\n");
  1530. fprintf(stderr, "-h Host (default %s)\n", BENCH_DEFAULT_HOST);
  1531. fprintf(stderr, "-P Port (default %d)\n", BENCH_DEFAULT_PORT);
  1532. fprintf(stderr, "-e List Every cipher suite available\n");
  1533. fprintf(stderr, "-i Show peer info\n");
  1534. #ifdef WOLFSSL_TLS13
  1535. fprintf(stderr, "-g Run through each of the TLS 1.3 groups that are available\n");
  1536. #endif
  1537. fprintf(stderr, "-l <str> Cipher suite list (: delimited)\n");
  1538. fprintf(stderr, "-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC);
  1539. fprintf(stderr, "-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE);
  1540. #ifdef WOLFSSL_DTLS
  1541. fprintf(stderr, " In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE);
  1542. #endif
  1543. fprintf(stderr, "-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE);
  1544. fprintf(stderr, "-v Show verbose output\n");
  1545. #ifdef DEBUG_WOLFSSL
  1546. fprintf(stderr, "-d Enable debug messages\n");
  1547. #endif
  1548. #ifdef HAVE_PTHREAD
  1549. fprintf(stderr, "-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
  1550. fprintf(stderr, "-m Use local memory, not socket\n");
  1551. #endif
  1552. #ifdef WOLFSSL_DTLS
  1553. fprintf(stderr, "-u Use DTLS\n");
  1554. #endif
  1555. }
  1556. static void ShowCiphers(void)
  1557. {
  1558. char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  1559. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  1560. if (ret == WOLFSSL_SUCCESS) {
  1561. fprintf(stderr, "%s\n", ciphers);
  1562. }
  1563. }
  1564. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1565. static int SetupSupportedGroups(int verbose)
  1566. {
  1567. int i;
  1568. WOLFSSL_CTX* ctx = NULL;
  1569. WOLFSSL* ssl = NULL;
  1570. int ret = 0;
  1571. if (ret == 0) {
  1572. #ifdef NO_WOLFSSL_CLIENT
  1573. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1574. #else
  1575. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  1576. #endif
  1577. if (ctx == NULL) {
  1578. ret = -1;
  1579. }
  1580. }
  1581. if (ret == 0) {
  1582. ssl = wolfSSL_new(ctx);
  1583. if (ssl == NULL) {
  1584. ret = -1;
  1585. }
  1586. }
  1587. /* Determine which groups are actually supported. */
  1588. for (i = 0; groups[i].name != NULL; i++) {
  1589. if (ret == 0) {
  1590. int uks_ret = wolfSSL_UseKeyShare(ssl, groups[i].group);
  1591. if (uks_ret == WOLFSSL_SUCCESS) {
  1592. if (verbose) {
  1593. printf("Will benchmark the following group: %s\n",
  1594. groups[i].name);
  1595. }
  1596. } else if (uks_ret == BAD_FUNC_ARG || uks_ret == NOT_COMPILED_IN) {
  1597. groups[i].group = 0;
  1598. if (verbose) {
  1599. printf("Will NOT benchmark the following group: %s\n",
  1600. groups[i].name);
  1601. }
  1602. } else {
  1603. ret = -1;
  1604. }
  1605. }
  1606. }
  1607. if (ssl != NULL) {
  1608. wolfSSL_free(ssl);
  1609. }
  1610. if (ctx != NULL) {
  1611. wolfSSL_CTX_free(ctx);
  1612. }
  1613. return ret;
  1614. }
  1615. #endif
  1616. int bench_tls(void* args)
  1617. {
  1618. int ret = 0;
  1619. info_t *theadInfo = NULL, *info;
  1620. stats_t cli_comb, srv_comb;
  1621. int i;
  1622. char *cipher, *next_cipher, *ciphers = NULL;
  1623. int argc = 0;
  1624. char** argv = NULL;
  1625. int ch;
  1626. /* Vars configured by command line arguments */
  1627. int argRuntimeSec = BENCH_RUNTIME_SEC;
  1628. char *argCipherList = NULL;
  1629. int argTestPacketSize = TEST_PACKET_SIZE;
  1630. int argTestMaxSize = TEST_MAX_SIZE;
  1631. int argThreadPairs = NUM_THREAD_PAIRS;
  1632. int argShowVerbose = SHOW_VERBOSE;
  1633. int argClientOnly = 0;
  1634. int argServerOnly = 0;
  1635. const char* argHost = BENCH_DEFAULT_HOST;
  1636. int argPort = BENCH_DEFAULT_PORT;
  1637. int argShowPeerInfo = 0;
  1638. #ifdef HAVE_PTHREAD
  1639. int doShutdown;
  1640. #endif
  1641. #if !defined(NO_WOLFSSL_SERVER) || defined(HAVE_PTHREAD)
  1642. int argLocalMem = 0;
  1643. int listenFd = -1;
  1644. #endif
  1645. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1646. int option_p = 0;
  1647. #endif
  1648. #ifdef WOLFSSL_DTLS
  1649. int doDTLS = 0;
  1650. #endif
  1651. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1652. int group_index = 0;
  1653. int argDoGroups = 0;
  1654. #endif
  1655. if (args != NULL) {
  1656. argc = ((func_args*)args)->argc;
  1657. argv = ((func_args*)args)->argv;
  1658. ((func_args*)args)->return_code = -1; /* error state */
  1659. }
  1660. /* Initialize wolfSSL */
  1661. wolfSSL_Init();
  1662. /* Parse command line arguments */
  1663. while ((ch = mygetopt(argc, argv, "?" "udeil:p:t:vT:sch:P:mS:g")) != -1) {
  1664. switch (ch) {
  1665. case '?' :
  1666. Usage();
  1667. goto exit;
  1668. case 's':
  1669. argServerOnly = 1;
  1670. break;
  1671. case 'c':
  1672. argClientOnly = 1;
  1673. break;
  1674. case 'h':
  1675. argHost = myoptarg;
  1676. break;
  1677. case 'P':
  1678. argPort = atoi(myoptarg);
  1679. break;
  1680. case 'd' :
  1681. #ifdef DEBUG_WOLFSSL
  1682. wolfSSL_Debugging_ON();
  1683. #endif
  1684. break;
  1685. case 'e' :
  1686. ShowCiphers();
  1687. goto exit;
  1688. case 'g' :
  1689. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1690. argDoGroups = 1;
  1691. break;
  1692. #else
  1693. fprintf(stderr, "There are only groups in TLS 1.3\n");
  1694. Usage();
  1695. ret = MY_EX_USAGE; goto exit;
  1696. #endif
  1697. case 'i' :
  1698. argShowPeerInfo = 1;
  1699. break;
  1700. case 'l' :
  1701. argCipherList = myoptarg;
  1702. break;
  1703. case 'p' :
  1704. argTestPacketSize = atoi(myoptarg);
  1705. if (argTestPacketSize > (16 * 1024)) {
  1706. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1707. Usage();
  1708. ret = MY_EX_USAGE; goto exit;
  1709. }
  1710. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1711. option_p = 1;
  1712. #endif
  1713. break;
  1714. case 'S' :
  1715. argTestMaxSize = atoi(myoptarg);
  1716. break;
  1717. case 't' :
  1718. argRuntimeSec = atoi(myoptarg);
  1719. break;
  1720. case 'v' :
  1721. argShowVerbose = 1;
  1722. break;
  1723. case 'T' :
  1724. #ifdef HAVE_PTHREAD
  1725. argThreadPairs = atoi(myoptarg);
  1726. #endif
  1727. break;
  1728. case 'm':
  1729. #ifdef HAVE_PTHREAD
  1730. argLocalMem = 1;
  1731. #endif
  1732. break;
  1733. case 'u':
  1734. #ifdef WOLFSSL_DTLS
  1735. doDTLS = 1;
  1736. #ifdef BENCH_USE_NONBLOCK
  1737. fprintf(stderr, "tls_bench hasn't yet supported DTLS "
  1738. "non-blocking mode.\n");
  1739. Usage();
  1740. ret = MY_EX_USAGE; goto exit;
  1741. #endif
  1742. #endif
  1743. break;
  1744. default:
  1745. Usage();
  1746. ret = MY_EX_USAGE; goto exit;
  1747. }
  1748. }
  1749. /* reset for test cases */
  1750. myoptind = 0;
  1751. if (argCipherList != NULL) {
  1752. /* Use the list from CL argument */
  1753. cipher = argCipherList;
  1754. }
  1755. else {
  1756. /* Run for each cipher */
  1757. ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1758. if (ciphers == NULL) {
  1759. goto exit;
  1760. }
  1761. wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1762. cipher = ciphers;
  1763. }
  1764. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1765. if (argDoGroups) {
  1766. if (SetupSupportedGroups(argShowVerbose) != 0) {
  1767. goto exit;
  1768. }
  1769. }
  1770. #endif
  1771. /* for server or client side only, only 1 thread is allowed */
  1772. if (argServerOnly || argClientOnly) {
  1773. argThreadPairs = 1;
  1774. }
  1775. #ifndef HAVE_PTHREAD
  1776. else {
  1777. fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
  1778. Usage();
  1779. ret = MY_EX_USAGE; goto exit;
  1780. }
  1781. #endif
  1782. /* Allocate test info array */
  1783. theadInfo = (info_t*)XMALLOC(sizeof(info_t) * argThreadPairs, NULL,
  1784. DYNAMIC_TYPE_TMP_BUFFER);
  1785. if (theadInfo == NULL) {
  1786. ret = MEMORY_E; goto exit;
  1787. }
  1788. XMEMSET(theadInfo, 0, sizeof(info_t) * argThreadPairs);
  1789. #ifndef NO_WOLFSSL_SERVER
  1790. /* Use same listen socket to avoid timing issues between client and server */
  1791. if (argServerOnly && !argLocalMem) {
  1792. /* Setup TLS server listener */
  1793. #ifdef WOLFSSL_DTLS
  1794. ret = SetupSocketAndListen(&listenFd, argPort, doDTLS);
  1795. #else
  1796. ret = SetupSocketAndListen(&listenFd, argPort, 0);
  1797. #endif
  1798. if (ret != 0) {
  1799. goto exit;
  1800. }
  1801. }
  1802. #endif
  1803. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1804. if (doDTLS) {
  1805. if (argLocalMem) {
  1806. fprintf(stderr, "tls_bench hasn't yet supported DTLS with local memory.\n");
  1807. ret = MY_EX_USAGE; goto exit;
  1808. }
  1809. if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){
  1810. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1811. Usage();
  1812. ret = MY_EX_USAGE; goto exit;
  1813. } else {
  1814. /* argTestPacketSize would be default for tcp packet */
  1815. if (argTestPacketSize >= TEST_PACKET_SIZE)
  1816. argTestPacketSize = TEST_DTLS_PACKET_SIZE;
  1817. }
  1818. }
  1819. #endif
  1820. fprintf(stderr, "Running TLS Benchmarks...\n");
  1821. /* parse by : */
  1822. while ((cipher != NULL) && (cipher[0] != '\0')) {
  1823. #if ! (defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES))
  1824. const char *gname = "N/A";
  1825. #endif
  1826. next_cipher = strchr(cipher, ':');
  1827. if (next_cipher != NULL) {
  1828. cipher[next_cipher - cipher] = '\0';
  1829. }
  1830. if (argShowVerbose) {
  1831. fprintf(stderr, "Cipher: %s\n", cipher);
  1832. }
  1833. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1834. for (group_index = 0; groups[group_index].name != NULL; group_index++) {
  1835. const char *gname = theadInfo[0].group == 0 ? "N/A"
  1836. : groups[group_index].name;
  1837. if (argDoGroups && groups[group_index].group == 0) {
  1838. /* Skip unsupported group. */
  1839. continue;
  1840. }
  1841. #endif
  1842. for (i=0; i<argThreadPairs; i++) {
  1843. info = &theadInfo[i];
  1844. XMEMSET(info, 0, sizeof(info_t));
  1845. info->host = argHost;
  1846. info->port = argPort + i; /* threads must have separate ports */
  1847. info->cipher = cipher;
  1848. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1849. if (argDoGroups && XSTRNCMP(theadInfo[0].cipher, "TLS13", 5) == 0) {
  1850. info->group = groups[group_index].group;
  1851. }
  1852. else
  1853. #endif
  1854. {
  1855. info->group = 0;
  1856. }
  1857. info->packetSize = argTestPacketSize;
  1858. info->runTimeSec = argRuntimeSec;
  1859. info->maxSize = argTestMaxSize;
  1860. info->showPeerInfo = argShowPeerInfo;
  1861. info->showVerbose = argShowVerbose;
  1862. #ifndef NO_WOLFSSL_SERVER
  1863. info->listenFd = listenFd;
  1864. #endif
  1865. info->client.sockFd = -1;
  1866. info->server.sockFd = -1;
  1867. #ifdef WOLFSSL_DTLS
  1868. info->doDTLS = doDTLS;
  1869. #ifdef HAVE_PTHREAD
  1870. info->serverReady = 0;
  1871. if (argServerOnly || argClientOnly) {
  1872. info->clientOrserverOnly = 1;
  1873. }
  1874. #endif
  1875. #endif
  1876. if (argClientOnly) {
  1877. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  1878. /* to avoid to wait server forever */
  1879. info->serverListening = 1;
  1880. #endif
  1881. #ifndef NO_WOLFSSL_CLIENT
  1882. ret = bench_tls_client(info);
  1883. #endif
  1884. }
  1885. else if (argServerOnly) {
  1886. #ifndef NO_WOLFSSL_SERVER
  1887. ret = bench_tls_server(info);
  1888. #endif
  1889. }
  1890. else {
  1891. #ifdef HAVE_PTHREAD
  1892. info->useLocalMem = argLocalMem;
  1893. PTHREAD_CHECK_RET(pthread_mutex_init(&info->to_server.mutex,
  1894. NULL));
  1895. PTHREAD_CHECK_RET(pthread_mutex_init(&info->to_client.mutex,
  1896. NULL));
  1897. #ifdef WOLFSSL_DTLS
  1898. PTHREAD_CHECK_RET(pthread_mutex_init(&info->dtls_mutex,
  1899. NULL));
  1900. PTHREAD_CHECK_RET(pthread_cond_init(&info->dtls_cond,
  1901. NULL));
  1902. #endif
  1903. PTHREAD_CHECK_RET(pthread_cond_init(&info->to_server.cond,
  1904. NULL));
  1905. PTHREAD_CHECK_RET(pthread_cond_init(&info->to_client.cond,
  1906. NULL));
  1907. PTHREAD_CHECK_RET(
  1908. pthread_create(&info->to_server.tid, NULL,
  1909. server_thread, info));
  1910. PTHREAD_CHECK_RET(
  1911. pthread_create(&info->to_client.tid, NULL,
  1912. client_thread, info));
  1913. /* State that we won't be joining this thread */
  1914. PTHREAD_CHECK_RET(pthread_detach(info->to_server.tid));
  1915. PTHREAD_CHECK_RET(pthread_detach(info->to_client.tid));
  1916. #endif
  1917. }
  1918. }
  1919. #ifdef HAVE_PTHREAD
  1920. /* For threading, wait for completion */
  1921. if (!argClientOnly && !argServerOnly) {
  1922. /* Wait until threads are marked done */
  1923. do {
  1924. doShutdown = 1;
  1925. for (i = 0; i < argThreadPairs; ++i) {
  1926. info = &theadInfo[i];
  1927. if (!info->to_client.done || !info->to_server.done) {
  1928. doShutdown = 0;
  1929. XSLEEP_MS(1000); /* Allow other threads to run */
  1930. }
  1931. }
  1932. } while (!doShutdown);
  1933. if (argShowVerbose) {
  1934. fprintf(stderr, "Shutdown complete\n");
  1935. }
  1936. }
  1937. #endif /* HAVE_PTHREAD */
  1938. if (argShowVerbose) {
  1939. /* print results */
  1940. for (i = 0; i < argThreadPairs; ++i) {
  1941. info = &theadInfo[i];
  1942. fprintf(stderr, "\nThread %d\n", i);
  1943. #ifndef NO_WOLFSSL_SERVER
  1944. if (!argClientOnly) {
  1945. print_stats(&info->server_stats, "Server", info->cipher,
  1946. gname, 1);
  1947. }
  1948. #endif
  1949. #ifndef NO_WOLFSSL_CLIENT
  1950. if (!argServerOnly) {
  1951. print_stats(&info->client_stats, "Client", info->cipher,
  1952. gname, 1);
  1953. }
  1954. #endif
  1955. }
  1956. }
  1957. /* print combined results for more than one thread */
  1958. XMEMSET(&cli_comb, 0, sizeof(cli_comb));
  1959. XMEMSET(&srv_comb, 0, sizeof(srv_comb));
  1960. for (i = 0; i < argThreadPairs; ++i) {
  1961. info = &theadInfo[i];
  1962. cli_comb.connCount += info->client_stats.connCount;
  1963. srv_comb.connCount += info->server_stats.connCount;
  1964. cli_comb.connTime += info->client_stats.connTime;
  1965. srv_comb.connTime += info->server_stats.connTime;
  1966. cli_comb.rxTotal += info->client_stats.rxTotal;
  1967. srv_comb.rxTotal += info->server_stats.rxTotal;
  1968. cli_comb.rxTime += info->client_stats.rxTime;
  1969. srv_comb.rxTime += info->server_stats.rxTime;
  1970. cli_comb.txTotal += info->client_stats.txTotal;
  1971. srv_comb.txTotal += info->server_stats.txTotal;
  1972. cli_comb.txTime += info->client_stats.txTime;
  1973. srv_comb.txTime += info->server_stats.txTime;
  1974. }
  1975. if (argShowVerbose) {
  1976. fprintf(stderr, "Totals for %d Threads\n", argThreadPairs);
  1977. }
  1978. else {
  1979. fprintf(stderr, "%-6s %-33s %-25s %11s %9s %9s %9s %9s %9s %17s %15s\n",
  1980. "Side", "Cipher", "Group", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
  1981. "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
  1982. #ifndef NO_WOLFSSL_SERVER
  1983. if (!argClientOnly) {
  1984. print_stats(&srv_comb, "Server", theadInfo[0].cipher, gname,
  1985. 0);
  1986. }
  1987. #endif
  1988. #ifndef NO_WOLFSSL_CLIENT
  1989. if (!argServerOnly) {
  1990. print_stats(&cli_comb, "Client", theadInfo[0].cipher, gname,
  1991. 0);
  1992. }
  1993. #endif
  1994. }
  1995. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1996. if (!argDoGroups || theadInfo[0].group == 0) {
  1997. /* We only needed to do this once because they don't want to
  1998. * benchmarks groups or this isn't a TLS 1.3 cipher. */
  1999. break;
  2000. }
  2001. }
  2002. #endif
  2003. /* target next cipher */
  2004. cipher = (next_cipher != NULL) ? (next_cipher + 1) : NULL;
  2005. } /* while */
  2006. exit:
  2007. #ifndef NO_WOLFSSL_SERVER
  2008. if (argServerOnly && !argLocalMem) {
  2009. /* Close server listener */
  2010. CloseAndCleanupListenSocket(&listenFd);
  2011. }
  2012. #endif
  2013. /* Cleanup the wolfSSL environment */
  2014. wolfSSL_Cleanup();
  2015. /* Free theadInfo array */
  2016. XFREE(theadInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2017. /* Free cipher list */
  2018. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2019. /* Return reporting a success */
  2020. if (args) {
  2021. ((func_args*)args)->return_code = ret;
  2022. }
  2023. return ret;
  2024. }
  2025. #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !WOLFCRYPT_ONLY && USE_WOLFSSL_IO */
  2026. #ifndef NO_MAIN_DRIVER
  2027. int main(int argc, char** argv)
  2028. {
  2029. func_args args;
  2030. args.argc = argc;
  2031. args.argv = argv;
  2032. args.return_code = 0;
  2033. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  2034. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  2035. bench_tls(&args);
  2036. #endif
  2037. return args.return_code;
  2038. }
  2039. #endif /* !NO_MAIN_DRIVER */