tls_bench.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  1. /* tls_bench.c
  2. *
  3. * Copyright (C) 2006-2021 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. #ifdef HAVE_PTHREAD
  224. typedef struct {
  225. unsigned char buf[MEM_BUFFER_SZ];
  226. int write_bytes;
  227. int write_idx;
  228. int read_bytes;
  229. int read_idx;
  230. pthread_t tid;
  231. pthread_mutex_t mutex;
  232. pthread_cond_t cond;
  233. int done;
  234. } memBuf_t;
  235. #endif
  236. typedef struct {
  237. double connTime;
  238. double rxTime;
  239. double txTime;
  240. int connCount;
  241. int rxTotal;
  242. int txTotal;
  243. } stats_t;
  244. typedef struct {
  245. int shutdown;
  246. int sockFd;
  247. int ret;
  248. } side_t;
  249. typedef struct {
  250. const char* cipher;
  251. const char* host;
  252. word32 port;
  253. int packetSize; /* The data payload size in the packet */
  254. int maxSize;
  255. int runTimeSec;
  256. int showPeerInfo;
  257. int showVerbose;
  258. #ifndef NO_WOLFSSL_SERVER
  259. int listenFd;
  260. #endif
  261. #ifdef WOLFSSL_DTLS
  262. int doDTLS;
  263. struct sockaddr_in serverAddr;
  264. struct sockaddr_in clientAddr;
  265. #ifdef HAVE_PTHREAD
  266. int serverReady;
  267. int clientOrserverOnly;
  268. pthread_mutex_t dtls_mutex;
  269. pthread_cond_t dtls_cond;
  270. #endif
  271. #endif
  272. side_t client;
  273. side_t server;
  274. #ifdef HAVE_PTHREAD
  275. int useLocalMem;
  276. /* client messages to server in memory */
  277. memBuf_t to_server;
  278. /* server messages to client in memory */
  279. memBuf_t to_client;
  280. /* Indicates that the server is ready for connection */
  281. int serverListening;
  282. #endif
  283. /* server */
  284. stats_t server_stats;
  285. /* client */
  286. stats_t client_stats;
  287. } info_t;
  288. /* Global vars for argument parsing */
  289. int myoptind = 0;
  290. char* myoptarg = NULL;
  291. #ifdef WOLFSSL_DTLS
  292. int DoneHandShake = 0;
  293. #endif
  294. static double gettime_secs(int reset)
  295. {
  296. struct timeval tv;
  297. gettimeofday(&tv, 0);
  298. (void)reset;
  299. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  300. }
  301. #ifdef HAVE_PTHREAD
  302. /* server send callback */
  303. static int ServerMemSend(info_t* info, char* buf, int sz)
  304. {
  305. pthread_mutex_lock(&info->to_client.mutex);
  306. #ifndef BENCH_USE_NONBLOCK
  307. /* check for overflow */
  308. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  309. pthread_mutex_unlock(&info->to_client.mutex);
  310. fprintf(stderr, "ServerMemSend overflow\n");
  311. return -1;
  312. }
  313. #else
  314. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ)
  315. sz = MEM_BUFFER_SZ - info->to_client.write_idx;
  316. #endif
  317. XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
  318. info->to_client.write_idx += sz;
  319. info->to_client.write_bytes += sz;
  320. pthread_cond_signal(&info->to_client.cond);
  321. pthread_mutex_unlock(&info->to_client.mutex);
  322. #ifdef BENCH_USE_NONBLOCK
  323. if (sz == 0)
  324. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  325. #endif
  326. return sz;
  327. }
  328. /* server recv callback */
  329. static int ServerMemRecv(info_t* info, char* buf, int sz)
  330. {
  331. pthread_mutex_lock(&info->to_server.mutex);
  332. #ifndef BENCH_USE_NONBLOCK
  333. while (info->to_server.write_idx - info->to_server.read_idx < sz && !info->to_client.done)
  334. pthread_cond_wait(&info->to_server.cond, &info->to_server.mutex);
  335. #else
  336. if (info->to_server.write_idx - info->to_server.read_idx < sz)
  337. sz = info->to_server.write_idx - info->to_server.read_idx;
  338. #endif
  339. XMEMCPY(buf, &info->to_server.buf[info->to_server.read_idx], sz);
  340. info->to_server.read_idx += sz;
  341. info->to_server.read_bytes += sz;
  342. /* if the rx has caught up with pending then reset buffer positions */
  343. if (info->to_server.read_bytes == info->to_server.write_bytes) {
  344. info->to_server.read_bytes = info->to_server.read_idx = 0;
  345. info->to_server.write_bytes = info->to_server.write_idx = 0;
  346. }
  347. pthread_mutex_unlock(&info->to_server.mutex);
  348. if (info->to_client.done != 0)
  349. return -1;
  350. #ifdef BENCH_USE_NONBLOCK
  351. if (sz == 0)
  352. return WOLFSSL_CBIO_ERR_WANT_READ;
  353. #endif
  354. return sz;
  355. }
  356. /* client send callback */
  357. static int ClientMemSend(info_t* info, char* buf, int sz)
  358. {
  359. pthread_mutex_lock(&info->to_server.mutex);
  360. #ifndef BENCH_USE_NONBLOCK
  361. /* check for overflow */
  362. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  363. fprintf(stderr, "ClientMemSend overflow %d %d %d\n", info->to_client.write_idx, sz, MEM_BUFFER_SZ);
  364. pthread_mutex_unlock(&info->to_server.mutex);
  365. return -1;
  366. }
  367. #else
  368. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ)
  369. sz = MEM_BUFFER_SZ - info->to_server.write_idx;
  370. #endif
  371. XMEMCPY(&info->to_server.buf[info->to_server.write_idx], buf, sz);
  372. info->to_server.write_idx += sz;
  373. info->to_server.write_bytes += sz;
  374. pthread_cond_signal(&info->to_server.cond);
  375. pthread_mutex_unlock(&info->to_server.mutex);
  376. #ifdef BENCH_USE_NONBLOCK
  377. if (sz == 0)
  378. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  379. #endif
  380. return sz;
  381. }
  382. /* client recv callback */
  383. static int ClientMemRecv(info_t* info, char* buf, int sz)
  384. {
  385. pthread_mutex_lock(&info->to_client.mutex);
  386. #ifndef BENCH_USE_NONBLOCK
  387. while (info->to_client.write_idx - info->to_client.read_idx < sz)
  388. pthread_cond_wait(&info->to_client.cond, &info->to_client.mutex);
  389. #else
  390. if (info->to_client.write_idx - info->to_client.read_idx < sz)
  391. sz = info->to_client.write_idx - info->to_client.read_idx;
  392. #endif
  393. XMEMCPY(buf, &info->to_client.buf[info->to_client.read_idx], sz);
  394. info->to_client.read_idx += sz;
  395. info->to_client.read_bytes += sz;
  396. /* if the rx has caught up with pending then reset buffer positions */
  397. if (info->to_client.read_bytes == info->to_client.write_bytes) {
  398. info->to_client.read_bytes = info->to_client.read_idx = 0;
  399. info->to_client.write_bytes = info->to_client.write_idx = 0;
  400. }
  401. pthread_mutex_unlock(&info->to_client.mutex);
  402. #ifdef BENCH_USE_NONBLOCK
  403. if (sz == 0)
  404. return WOLFSSL_CBIO_ERR_WANT_READ;
  405. #endif
  406. return sz;
  407. }
  408. #endif /* HAVE_PTHREAD */
  409. static int SocketRecv(int sockFd, char* buf, int sz)
  410. {
  411. int recvd = (int)recv(sockFd, buf, sz, 0);
  412. if (recvd == -1) {
  413. switch (errno) {
  414. #if EAGAIN != SOCKET_EWOULDBLOCK
  415. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  416. #endif
  417. case SOCKET_EWOULDBLOCK:
  418. return WOLFSSL_CBIO_ERR_WANT_READ;
  419. case SOCKET_ECONNRESET:
  420. return WOLFSSL_CBIO_ERR_CONN_RST;
  421. case SOCKET_EINTR:
  422. return WOLFSSL_CBIO_ERR_ISR;
  423. case SOCKET_ECONNREFUSED: /* DTLS case */
  424. return WOLFSSL_CBIO_ERR_WANT_READ;
  425. case SOCKET_ECONNABORTED:
  426. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  427. default:
  428. return WOLFSSL_CBIO_ERR_GENERAL;
  429. }
  430. }
  431. else if (recvd == 0) {
  432. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  433. }
  434. return recvd;
  435. }
  436. static int SocketSend(int sockFd, char* buf, int sz)
  437. {
  438. int sent = (int)send(sockFd, buf, sz, 0);
  439. if (sent == -1) {
  440. switch (errno) {
  441. #if EAGAIN != SOCKET_EWOULDBLOCK
  442. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  443. #endif
  444. case SOCKET_EWOULDBLOCK:
  445. return WOLFSSL_CBIO_ERR_WANT_READ;
  446. case SOCKET_ECONNRESET:
  447. return WOLFSSL_CBIO_ERR_CONN_RST;
  448. case SOCKET_EINTR:
  449. return WOLFSSL_CBIO_ERR_ISR;
  450. case SOCKET_EPIPE:
  451. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  452. default:
  453. return WOLFSSL_CBIO_ERR_GENERAL;
  454. }
  455. }
  456. else if (sent == 0) {
  457. return 0;
  458. }
  459. return sent;
  460. }
  461. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  462. static int ReceiveFrom(WOLFSSL *ssl, int sd, char *buf, int sz)
  463. {
  464. int recvd;
  465. int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
  466. struct sockaddr peer;
  467. socklen_t peerSz = 0;
  468. if (DoneHandShake) dtls_timeout = 0;
  469. if (!wolfSSL_get_using_nonblock(ssl)) {
  470. struct timeval timeout;
  471. XMEMSET(&timeout, 0, sizeof(timeout));
  472. timeout.tv_sec = dtls_timeout;
  473. if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
  474. sizeof(timeout)) != 0) {
  475. fprintf(stderr, "setsockopt rcvtimeo failed\n");
  476. }
  477. }
  478. recvd = (int)recvfrom(sd, buf, sz, 0, (SOCKADDR*)&peer, &peerSz);
  479. if (recvd < 0) {
  480. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  481. if (wolfSSL_dtls_get_using_nonblock(ssl)) {
  482. return WOLFSSL_CBIO_ERR_WANT_READ;
  483. }
  484. else {
  485. return WOLFSSL_CBIO_ERR_TIMEOUT;
  486. }
  487. }
  488. else if (errno == SOCKET_ECONNRESET) {
  489. return WOLFSSL_CBIO_ERR_CONN_RST;
  490. }
  491. else if (errno == SOCKET_EINTR) {
  492. return WOLFSSL_CBIO_ERR_ISR;
  493. }
  494. else if (errno == SOCKET_ECONNREFUSED) {
  495. return WOLFSSL_CBIO_ERR_WANT_READ;
  496. }
  497. else {
  498. return WOLFSSL_CBIO_ERR_GENERAL;
  499. }
  500. }
  501. else {
  502. if (recvd == 0) {
  503. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  504. }
  505. }
  506. return recvd;
  507. }
  508. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  509. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  510. static int SendTo(int sd, char *buf, int sz, const struct sockaddr *peer,
  511. socklen_t peerSz)
  512. {
  513. int sent;
  514. sent = (int)sendto(sd, buf, sz, 0, peer, peerSz);
  515. if (sent < 0) {
  516. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  517. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  518. }
  519. else if (errno == SOCKET_ECONNRESET) {
  520. return WOLFSSL_CBIO_ERR_CONN_RST;
  521. }
  522. else if (errno == SOCKET_EINTR) {
  523. return WOLFSSL_CBIO_ERR_ISR;
  524. }
  525. else if (errno == SOCKET_EPIPE) {
  526. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  527. }
  528. else {
  529. return WOLFSSL_CBIO_ERR_GENERAL;
  530. }
  531. }
  532. return sent;
  533. }
  534. static int myDoneHsCb(WOLFSSL* ssl, void* user_ctx)
  535. {
  536. (void) ssl;
  537. (void) user_ctx;
  538. DoneHandShake = 1;
  539. return 1;
  540. }
  541. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_CLIENT */
  542. #ifndef NO_WOLFSSL_SERVER
  543. static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  544. {
  545. info_t* info = (info_t*)ctx;
  546. (void)ssl;
  547. #ifdef HAVE_PTHREAD
  548. if (info->useLocalMem)
  549. return ServerMemSend(info, buf, sz);
  550. #endif
  551. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  552. if (info->doDTLS) {
  553. return SendTo(info->server.sockFd, buf, sz,
  554. (const struct sockaddr*)&info->clientAddr, sizeof(info->clientAddr));
  555. } else
  556. #endif
  557. return SocketSend(info->server.sockFd, buf, sz);
  558. }
  559. static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  560. {
  561. info_t* info = (info_t*)ctx;
  562. (void)ssl;
  563. #ifdef HAVE_PTHREAD
  564. if (info->useLocalMem)
  565. return ServerMemRecv(info, buf, sz);
  566. #endif
  567. #ifdef WOLFSSL_DTLS
  568. if (info->doDTLS) {
  569. return ReceiveFrom(ssl, info->server.sockFd, buf, sz);
  570. } else
  571. #endif
  572. return SocketRecv(info->server.sockFd, buf, sz);
  573. }
  574. #endif /* !NO_WOLFSSL_SERVER */
  575. #ifndef NO_WOLFSSL_CLIENT
  576. static int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  577. {
  578. info_t* info = (info_t*)ctx;
  579. (void)ssl;
  580. #ifdef HAVE_PTHREAD
  581. if (info->useLocalMem)
  582. return ClientMemSend(info, buf, sz);
  583. #endif
  584. #ifdef WOLFSSL_DTLS
  585. if (info->doDTLS) {
  586. return SendTo(info->client.sockFd, buf, sz,
  587. (const struct sockaddr*)&info->serverAddr, sizeof(info->serverAddr));
  588. } else
  589. #endif
  590. return SocketSend(info->client.sockFd, buf, sz);
  591. }
  592. static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  593. {
  594. info_t* info = (info_t*)ctx;
  595. (void)ssl;
  596. #ifdef HAVE_PTHREAD
  597. if (info->useLocalMem)
  598. return ClientMemRecv(info, buf, sz);
  599. #endif
  600. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  601. if (info->doDTLS) {
  602. return ReceiveFrom(ssl, info->client.sockFd, buf, sz);
  603. } else
  604. #endif
  605. return SocketRecv(info->client.sockFd, buf, sz);
  606. }
  607. #endif /* !NO_WOLFSSL_CLIENT */
  608. static void CloseAndCleanupSocket(int* sockFd)
  609. {
  610. if (*sockFd != -1) {
  611. close(*sockFd);
  612. *sockFd = -1;
  613. }
  614. #ifdef WOLFSSL_DTLS
  615. DoneHandShake = 0;
  616. #endif
  617. }
  618. #ifdef BENCH_USE_NONBLOCK
  619. static int SetSocketNonBlocking(int sockFd)
  620. {
  621. int flags = fcntl(sockFd, F_GETFL, 0);
  622. if (flags < 0) {
  623. fprintf(stderr, "fcntl get failed\n");
  624. return -1;
  625. }
  626. flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK);
  627. if (flags < 0) {
  628. fprintf(stderr, "fcntl set failed\n");
  629. return -1;
  630. }
  631. return 0;
  632. }
  633. #endif
  634. #ifndef NO_WOLFSSL_CLIENT
  635. static int SetupSocketAndConnect(info_t* info, const char* host,
  636. word32 port)
  637. {
  638. struct sockaddr_in servAddr;
  639. struct hostent* entry;
  640. /* Setup server address */
  641. XMEMSET(&servAddr, 0, sizeof(servAddr));
  642. servAddr.sin_family = AF_INET;
  643. servAddr.sin_port = htons(port);
  644. /* Resolve host */
  645. entry = gethostbyname(host);
  646. if (entry) {
  647. XMEMCPY(&servAddr.sin_addr.s_addr, entry->h_addr_list[0],
  648. entry->h_length);
  649. }
  650. else {
  651. servAddr.sin_addr.s_addr = inet_addr(host);
  652. }
  653. #ifdef WOLFSSL_DTLS
  654. if (info->doDTLS) {
  655. /* Create the SOCK_DGRAM socket type is implemented on the User
  656. * Datagram Protocol/Internet Protocol(UDP/IP protocol).*/
  657. if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  658. fprintf(stderr, "ERROR: failed to create the SOCK_DGRAM socket\n");
  659. return -1;
  660. }
  661. XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr));
  662. } else {
  663. #endif
  664. /* Create a socket that uses an Internet IPv4 address,
  665. * Sets the socket to be stream based (TCP),
  666. * 0 means choose the default protocol. */
  667. if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  668. fprintf(stderr, "ERROR: failed to create the socket\n");
  669. return -1;
  670. }
  671. /* Connect to the server */
  672. #ifdef HAVE_PTHREAD
  673. while ((info->serverListening == 0) && (info->server.shutdown == 0)) {
  674. if (info->showVerbose) {
  675. fprintf(stderr, "Waiting for server to listen...\n");
  676. }
  677. XSLEEP_MS(1);
  678. }
  679. #endif
  680. if (info->server.shutdown == 1) {
  681. fprintf(stderr, "ERROR: server side has shutdown\n");
  682. return -1;
  683. }
  684. if (connect(info->client.sockFd, (struct sockaddr*)&servAddr,
  685. sizeof(servAddr)) == -1) {
  686. fprintf(stderr, "ERROR: failed to connect\n");
  687. return -1;
  688. }
  689. #ifdef WOLFSSL_DTLS
  690. }
  691. #endif
  692. #ifdef BENCH_USE_NONBLOCK
  693. if (SetSocketNonBlocking(info->client.sockFd) != 0) {
  694. return -1;
  695. }
  696. #endif
  697. if (info->showVerbose) {
  698. fprintf(stderr, "Connected to %s on port %d\n", host, port);
  699. }
  700. return 0;
  701. }
  702. static int bench_tls_client(info_t* info)
  703. {
  704. byte *writeBuf = NULL, *readBuf = NULL;
  705. double start, total = 0;
  706. int ret, readBufSz;
  707. WOLFSSL_CTX* cli_ctx = NULL;
  708. WOLFSSL* cli_ssl = NULL;
  709. int haveShownPeerInfo = 0;
  710. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  711. int total_sz;
  712. total = gettime_secs(0);
  713. /* set up client */
  714. #ifdef WOLFSSL_DTLS
  715. if(info->doDTLS) {
  716. if (tls13) return WOLFSSL_SUCCESS;
  717. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  718. } else
  719. #endif
  720. #ifdef WOLFSSL_TLS13
  721. if (tls13)
  722. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  723. #endif
  724. if (!tls13)
  725. #ifdef WOLFSSL_DTLS
  726. if(!info->doDTLS)
  727. #endif
  728. #if !defined(WOLFSSL_TLS13)
  729. cli_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  730. #elif !defined(WOLFSSL_NO_TLS12)
  731. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  732. #endif
  733. if (cli_ctx == NULL) {
  734. fprintf(stderr, "error creating ctx\n");
  735. ret = MEMORY_E; goto exit;
  736. }
  737. #ifndef NO_CERTS
  738. #ifdef HAVE_ECC
  739. if (XSTRSTR(info->cipher, "ECDSA")) {
  740. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_ecc_cert_der_256,
  741. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1);
  742. }
  743. else
  744. #endif
  745. {
  746. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_cert_der_2048,
  747. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  748. }
  749. if (ret != WOLFSSL_SUCCESS) {
  750. fprintf(stderr, "error loading CA\n");
  751. goto exit;
  752. }
  753. #endif
  754. wolfSSL_CTX_SetIOSend(cli_ctx, ClientSend);
  755. wolfSSL_CTX_SetIORecv(cli_ctx, ClientRecv);
  756. /* set cipher suite */
  757. ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
  758. if (ret != WOLFSSL_SUCCESS) {
  759. fprintf(stderr, "error setting cipher suite\n");
  760. goto exit;
  761. }
  762. #ifndef NO_DH
  763. ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
  764. if (ret != WOLFSSL_SUCCESS) {
  765. fprintf(stderr, "Error setting minimum DH key size\n");
  766. goto exit;
  767. }
  768. #endif
  769. /* Allocate and initialize a packet sized buffer */
  770. writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL,
  771. DYNAMIC_TYPE_TMP_BUFFER);
  772. if (writeBuf == NULL) {
  773. fprintf(stderr, "failed to allocate write memory\n");
  774. ret = MEMORY_E; goto exit;
  775. }
  776. /* Allocate read buffer */
  777. readBufSz = info->packetSize;
  778. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  779. if (readBuf == NULL) {
  780. fprintf(stderr, "failed to allocate read memory\n");
  781. ret = MEMORY_E; goto exit;
  782. }
  783. /* BENCHMARK CONNECTIONS LOOP */
  784. while (!info->client.shutdown) {
  785. int writeSz = info->packetSize;
  786. #ifdef BENCH_USE_NONBLOCK
  787. int err;
  788. #endif
  789. #ifdef HAVE_PTHREAD
  790. if (!info->useLocalMem)
  791. #endif
  792. {
  793. /* Setup socket and connection */
  794. ret = SetupSocketAndConnect(info, info->host, info->port);
  795. if (ret != 0) goto exit;
  796. }
  797. cli_ssl = wolfSSL_new(cli_ctx);
  798. if (cli_ssl == NULL) {
  799. fprintf(stderr, "error creating client object\n");
  800. goto exit;
  801. }
  802. #ifdef WOLFSSL_DTLS
  803. if (info->doDTLS) {
  804. ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr,
  805. sizeof(info->serverAddr));
  806. if (ret != WOLFSSL_SUCCESS) {
  807. fprintf(stderr, "error setting dtls peer\n");
  808. goto exit;
  809. }
  810. ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL);
  811. if (ret != WOLFSSL_SUCCESS) {
  812. fprintf(stderr, "error handshake done callback\n");
  813. goto exit;
  814. }
  815. }
  816. #endif
  817. wolfSSL_SetIOReadCtx(cli_ssl, info);
  818. wolfSSL_SetIOWriteCtx(cli_ssl, info);
  819. #if defined(HAVE_PTHREAD) && defined(WOLFSSL_DTLS)
  820. /* synchronize with server */
  821. if (info->doDTLS && !info->clientOrserverOnly) {
  822. pthread_mutex_lock(&info->dtls_mutex);
  823. if (info->serverReady != 1) {
  824. pthread_cond_wait(&info->dtls_cond, &info->dtls_mutex);
  825. }
  826. /* for next loop */
  827. info->serverReady = 0;
  828. pthread_mutex_unlock(&info->dtls_mutex);
  829. }
  830. #endif
  831. /* perform connect */
  832. start = gettime_secs(1);
  833. #ifndef BENCH_USE_NONBLOCK
  834. ret = wolfSSL_connect(cli_ssl);
  835. #else
  836. do
  837. {
  838. ret = wolfSSL_connect(cli_ssl);
  839. err = wolfSSL_get_error(cli_ssl, ret);
  840. }
  841. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  842. #endif
  843. start = gettime_secs(0) - start;
  844. if (ret != WOLFSSL_SUCCESS) {
  845. fprintf(stderr, "error connecting client\n");
  846. ret = wolfSSL_get_error(cli_ssl, ret);
  847. goto exit;
  848. }
  849. info->client_stats.connTime += start;
  850. info->client_stats.connCount++;
  851. if ((info->showPeerInfo) && (!haveShownPeerInfo)) {
  852. haveShownPeerInfo = 1;
  853. showPeer(cli_ssl);
  854. }
  855. /* check for run time completion and issue shutdown */
  856. if (gettime_secs(0) - total >= info->runTimeSec) {
  857. info->client.shutdown = 1;
  858. writeSz = (int)XSTRLEN(kShutdown) + 1;
  859. XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */
  860. if (info->showVerbose) {
  861. fprintf(stderr, "Sending shutdown\n");
  862. }
  863. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  864. if (ret < 0) {
  865. fprintf(stderr, "error on client write\n");
  866. ret = wolfSSL_get_error(cli_ssl, ret);
  867. goto exit;
  868. }
  869. }
  870. else {
  871. XMEMSET(writeBuf, 0, info->packetSize);
  872. XSTRNCPY((char*)writeBuf, kTestStr, info->packetSize);
  873. }
  874. /* write / read echo loop */
  875. ret = 0;
  876. total_sz = 0;
  877. while (ret == 0 && total_sz < info->maxSize && !info->client.shutdown) {
  878. /* write test message to server */
  879. start = gettime_secs(1);
  880. #ifndef BENCH_USE_NONBLOCK
  881. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  882. #else
  883. do {
  884. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  885. err = wolfSSL_get_error(cli_ssl, ret);
  886. }
  887. while (err == WOLFSSL_ERROR_WANT_WRITE);
  888. #endif
  889. info->client_stats.txTime += gettime_secs(0) - start;
  890. if (ret < 0) {
  891. fprintf(stderr, "error on client write\n");
  892. ret = wolfSSL_get_error(cli_ssl, ret);
  893. goto exit;
  894. }
  895. info->client_stats.txTotal += ret;
  896. total_sz += ret;
  897. /* read echo of message from server */
  898. XMEMSET(readBuf, 0, readBufSz);
  899. start = gettime_secs(1);
  900. #ifndef BENCH_USE_NONBLOCK
  901. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  902. #else
  903. do {
  904. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  905. err = wolfSSL_get_error(cli_ssl, ret);
  906. }
  907. while (err == WOLFSSL_ERROR_WANT_READ);
  908. #endif
  909. info->client_stats.rxTime += gettime_secs(0) - start;
  910. if (ret < 0) {
  911. fprintf(stderr, "error on client read\n");
  912. ret = wolfSSL_get_error(cli_ssl, ret);
  913. goto exit;
  914. }
  915. info->client_stats.rxTotal += ret;
  916. ret = 0; /* reset return code */
  917. /* validate echo */
  918. if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) {
  919. fprintf(stderr, "echo check failed!\n");
  920. ret = wolfSSL_get_error(cli_ssl, ret);
  921. goto exit;
  922. }
  923. }
  924. CloseAndCleanupSocket(&info->client.sockFd);
  925. wolfSSL_free(cli_ssl);
  926. cli_ssl = NULL;
  927. }
  928. exit:
  929. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  930. fprintf(stderr, "Client Error: %d (%s)\n", ret,
  931. wolfSSL_ERR_reason_error_string(ret));
  932. }
  933. /* clean up */
  934. CloseAndCleanupSocket(&info->client.sockFd);
  935. if (cli_ssl != NULL)
  936. wolfSSL_free(cli_ssl);
  937. if (cli_ctx != NULL)
  938. wolfSSL_CTX_free(cli_ctx);
  939. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  940. XFREE(writeBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  941. info->client.ret = ret;
  942. return ret;
  943. }
  944. #ifdef HAVE_PTHREAD
  945. static void* client_thread(void* args)
  946. {
  947. int ret;
  948. info_t* info = (info_t*)args;
  949. ret = bench_tls_client(info);
  950. pthread_cond_signal(&info->to_server.cond);
  951. info->to_client.done = 1;
  952. info->client.ret = ret;
  953. return NULL;
  954. }
  955. #endif /* HAVE_PTHREAD */
  956. #endif /* !NO_WOLFSSL_CLIENT */
  957. #ifndef NO_WOLFSSL_SERVER
  958. static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
  959. {
  960. struct sockaddr_in servAddr;
  961. #if defined(_MSC_VER) || defined(__MINGW32__)
  962. char optval = 1;
  963. #else
  964. int optval = 1;
  965. #endif
  966. #ifndef WOLFSSL_DTLS
  967. (void) doDTLS;
  968. #endif
  969. /* Setup server address */
  970. XMEMSET(&servAddr, 0, sizeof(servAddr));
  971. servAddr.sin_family = AF_INET;
  972. servAddr.sin_port = htons(port);
  973. servAddr.sin_addr.s_addr = INADDR_ANY;
  974. #ifdef WOLFSSL_DTLS
  975. if (doDTLS) {
  976. /* Create a socket that is implemented on the User Datagram Protocol/
  977. * Interet Protocol(UDP/IP protocol). */
  978. if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  979. fprintf(stderr, "ERROR: failed to create the socket\n");
  980. return -1;
  981. }
  982. } else
  983. #endif
  984. /* Create a socket that uses an Internet IPv4 address,
  985. * Sets the socket to be stream based (TCP),
  986. * 0 means choose the default protocol. */
  987. if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  988. fprintf(stderr, "ERROR: failed to create the socket\n");
  989. return -1;
  990. }
  991. /* allow reuse */
  992. if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR,
  993. &optval, sizeof(optval)) == -1) {
  994. fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
  995. return -1;
  996. }
  997. /* Listen for the client. */
  998. if (bind(*listenFd, (struct sockaddr*)&servAddr,
  999. sizeof(servAddr)) == -1) {
  1000. fprintf(stderr, "ERROR: failed to bind\n");
  1001. return -1;
  1002. }
  1003. #ifdef WOLFSSL_DTLS
  1004. if (!doDTLS)
  1005. #endif
  1006. if (listen(*listenFd, 5) != 0) {
  1007. fprintf(stderr, "ERROR: failed to listen\n");
  1008. return -1;
  1009. }
  1010. #ifdef BENCH_USE_NONBLOCK
  1011. if (SetSocketNonBlocking(*listenFd) != 0) {
  1012. return -1;
  1013. }
  1014. #endif
  1015. return 0;
  1016. }
  1017. static int SocketWaitClient(info_t* info)
  1018. {
  1019. int connd;
  1020. struct sockaddr_in clientAddr;
  1021. socklen_t size = sizeof(clientAddr);
  1022. #ifdef WOLFSSL_DTLS
  1023. char msg[64];
  1024. if (info->doDTLS) {
  1025. #ifdef HAVE_PTHREAD
  1026. if (!info->clientOrserverOnly) {
  1027. pthread_mutex_lock(&info->dtls_mutex);
  1028. info->serverReady = 1;
  1029. pthread_cond_signal(&info->dtls_cond);
  1030. pthread_mutex_unlock(&info->dtls_mutex);
  1031. }
  1032. #endif
  1033. connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
  1034. MSG_PEEK, (struct sockaddr*)&clientAddr, &size);
  1035. if (connd < -1) {
  1036. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1037. return -1;
  1038. }
  1039. XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr));
  1040. info->server.sockFd = info->listenFd;
  1041. } else {
  1042. #endif
  1043. #ifdef HAVE_PTHREAD
  1044. info->serverListening = 1;
  1045. #endif
  1046. if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr, &size)) == -1) {
  1047. if (errno == SOCKET_EWOULDBLOCK)
  1048. return -2;
  1049. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1050. return -1;
  1051. }
  1052. info->server.sockFd = connd;
  1053. #ifdef WOLFSSL_DTLS
  1054. }
  1055. #endif
  1056. if (info->showVerbose) {
  1057. fprintf(stderr, "Got client %d\n", connd);
  1058. }
  1059. return 0;
  1060. }
  1061. static void CloseAndCleanupListenSocket(int* listenFd)
  1062. {
  1063. if (*listenFd != -1) {
  1064. close(*listenFd);
  1065. *listenFd = -1;
  1066. }
  1067. }
  1068. static int bench_tls_server(info_t* info)
  1069. {
  1070. byte *readBuf = NULL;
  1071. double start;
  1072. int ret, len = 0, readBufSz;
  1073. WOLFSSL_CTX* srv_ctx = NULL;
  1074. WOLFSSL* srv_ssl = NULL;
  1075. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  1076. int total_sz;
  1077. /* set up server */
  1078. #ifdef WOLFSSL_DTLS
  1079. if(info->doDTLS) {
  1080. if(tls13) return WOLFSSL_SUCCESS;
  1081. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  1082. } else {
  1083. #endif
  1084. #ifdef WOLFSSL_TLS13
  1085. if (tls13)
  1086. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1087. #endif
  1088. if (!tls13)
  1089. srv_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1090. #ifdef WOLFSSL_DTLS
  1091. }
  1092. #endif
  1093. if (srv_ctx == NULL) {
  1094. fprintf(stderr, "error creating server ctx\n");
  1095. ret = MEMORY_E; goto exit;
  1096. }
  1097. #ifndef NO_CERTS
  1098. #ifdef HAVE_ECC
  1099. if (XSTRSTR(info->cipher, "ECDSA")) {
  1100. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, ecc_key_der_256,
  1101. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  1102. }
  1103. else
  1104. #endif
  1105. {
  1106. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048,
  1107. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  1108. }
  1109. if (ret != WOLFSSL_SUCCESS) {
  1110. fprintf(stderr, "error loading server key\n");
  1111. goto exit;
  1112. }
  1113. #ifdef HAVE_ECC
  1114. if (XSTRSTR(info->cipher, "ECDSA")) {
  1115. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, serv_ecc_der_256,
  1116. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  1117. }
  1118. else
  1119. #endif
  1120. {
  1121. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, server_cert_der_2048,
  1122. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1123. }
  1124. if (ret != WOLFSSL_SUCCESS) {
  1125. fprintf(stderr, "error loading server cert\n");
  1126. goto exit;
  1127. }
  1128. #endif /* !NO_CERTS */
  1129. wolfSSL_CTX_SetIOSend(srv_ctx, ServerSend);
  1130. wolfSSL_CTX_SetIORecv(srv_ctx, ServerRecv);
  1131. /* set cipher suite */
  1132. ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
  1133. if (ret != WOLFSSL_SUCCESS) {
  1134. fprintf(stderr, "error setting cipher suite\n");
  1135. goto exit;
  1136. }
  1137. #ifndef NO_DH
  1138. ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
  1139. if (ret != WOLFSSL_SUCCESS) {
  1140. fprintf(stderr, "Error setting minimum DH key size\n");
  1141. goto exit;
  1142. }
  1143. #endif
  1144. /* Allocate read buffer */
  1145. readBufSz = info->packetSize;
  1146. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1147. if (readBuf == NULL) {
  1148. fprintf(stderr, "failed to allocate read memory\n");
  1149. ret = MEMORY_E; goto exit;
  1150. }
  1151. /* BENCHMARK CONNECTIONS LOOP */
  1152. while (!info->server.shutdown) {
  1153. #ifdef BENCH_USE_NONBLOCK
  1154. int err;
  1155. #endif
  1156. #ifdef HAVE_PTHREAD
  1157. if (!info->useLocalMem)
  1158. #endif
  1159. {
  1160. /* Accept client connections */
  1161. ret = SocketWaitClient(info);
  1162. #ifdef BENCH_USE_NONBLOCK
  1163. if (ret == -2) {
  1164. XSLEEP_MS(0);
  1165. continue;
  1166. }
  1167. #endif
  1168. if (ret != 0) {
  1169. goto exit;
  1170. }
  1171. }
  1172. srv_ssl = wolfSSL_new(srv_ctx);
  1173. if (srv_ssl == NULL) {
  1174. fprintf(stderr, "error creating server object\n");
  1175. ret = MEMORY_E; goto exit;
  1176. }
  1177. #ifdef WOLFSSL_DTLS
  1178. if (info->doDTLS) {
  1179. ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr,
  1180. sizeof(info->clientAddr));
  1181. if (ret != WOLFSSL_SUCCESS) {
  1182. fprintf(stderr, "error setting dtls peer\n");
  1183. goto exit;
  1184. }
  1185. }
  1186. #endif
  1187. wolfSSL_SetIOReadCtx(srv_ssl, info);
  1188. wolfSSL_SetIOWriteCtx(srv_ssl, info);
  1189. #ifndef NO_DH
  1190. wolfSSL_SetTmpDH(srv_ssl, dhp, sizeof(dhp), dhg, sizeof(dhg));
  1191. #endif
  1192. /* accept TLS connection */
  1193. start = gettime_secs(1);
  1194. #ifndef BENCH_USE_NONBLOCK
  1195. ret = wolfSSL_accept(srv_ssl);
  1196. #else
  1197. do {
  1198. ret = wolfSSL_accept(srv_ssl);
  1199. err = wolfSSL_get_error(srv_ssl, ret);
  1200. }
  1201. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1202. #endif
  1203. start = gettime_secs(0) - start;
  1204. if (ret != WOLFSSL_SUCCESS) {
  1205. fprintf(stderr, "error on server accept\n");
  1206. ret = wolfSSL_get_error(srv_ssl, ret);
  1207. goto exit;
  1208. }
  1209. info->server_stats.connTime += start;
  1210. info->server_stats.connCount++;
  1211. /* echo loop */
  1212. ret = 0;
  1213. total_sz = 0;
  1214. while (ret == 0 && total_sz < info->maxSize) {
  1215. double rxTime;
  1216. /* read message from client */
  1217. XMEMSET(readBuf, 0, readBufSz);
  1218. start = gettime_secs(1);
  1219. #ifndef BENCH_USE_NONBLOCK
  1220. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1221. #else
  1222. do {
  1223. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1224. err = wolfSSL_get_error(srv_ssl, ret);
  1225. }
  1226. while (err == WOLFSSL_ERROR_WANT_READ);
  1227. #endif
  1228. rxTime = gettime_secs(0) - start;
  1229. /* shutdown signals, no more connections for this cipher */
  1230. if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
  1231. info->server.shutdown = 1;
  1232. if (info->showVerbose) {
  1233. fprintf(stderr, "Server shutdown done\n");
  1234. }
  1235. ret = 0; /* success */
  1236. break;
  1237. }
  1238. info->server_stats.rxTime += rxTime;
  1239. if (ret < 0) {
  1240. fprintf(stderr, "error on server read\n");
  1241. ret = wolfSSL_get_error(srv_ssl, ret);
  1242. goto exit;
  1243. }
  1244. info->server_stats.rxTotal += ret;
  1245. len = ret;
  1246. total_sz += ret;
  1247. /* write message back to client */
  1248. start = gettime_secs(1);
  1249. #ifndef BENCH_USE_NONBLOCK
  1250. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1251. #else
  1252. do {
  1253. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1254. err = wolfSSL_get_error(srv_ssl, ret);
  1255. }
  1256. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1257. #endif
  1258. info->server_stats.txTime += gettime_secs(0) - start;
  1259. if (ret < 0) {
  1260. fprintf(stderr, "error on server write\n");
  1261. ret = wolfSSL_get_error(srv_ssl, ret);
  1262. goto exit;
  1263. }
  1264. info->server_stats.txTotal += ret;
  1265. ret = 0; /* reset return code */
  1266. }
  1267. CloseAndCleanupSocket(&info->server.sockFd);
  1268. wolfSSL_free(srv_ssl);
  1269. srv_ssl = NULL;
  1270. #ifdef WOLFSSL_DTLS
  1271. if (info->doDTLS) {
  1272. SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1273. }
  1274. #endif
  1275. }
  1276. exit:
  1277. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1278. fprintf(stderr, "Server Error: %d (%s)\n", ret,
  1279. wolfSSL_ERR_reason_error_string(ret));
  1280. }
  1281. /* clean up */
  1282. CloseAndCleanupSocket(&info->server.sockFd);
  1283. if (srv_ssl != NULL)
  1284. wolfSSL_free(srv_ssl);
  1285. if (srv_ctx != NULL)
  1286. wolfSSL_CTX_free(srv_ctx);
  1287. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1288. info->server.ret = ret;
  1289. return ret;
  1290. }
  1291. #ifdef HAVE_PTHREAD
  1292. static void* server_thread(void* args)
  1293. {
  1294. int ret = 0;
  1295. info_t* info = (info_t*)args;
  1296. if (!info->useLocalMem) {
  1297. /* Setup TLS server listener */
  1298. #ifdef WOLFSSL_DTLS
  1299. ret = SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1300. #else
  1301. ret = SetupSocketAndListen(&info->listenFd, info->port, 0);
  1302. #endif
  1303. }
  1304. if (ret == 0) {
  1305. ret = bench_tls_server(info);
  1306. if (!info->useLocalMem) {
  1307. CloseAndCleanupListenSocket(&info->listenFd);
  1308. }
  1309. }
  1310. pthread_cond_signal(&info->to_client.cond);
  1311. info->to_server.done = 1;
  1312. info->server.ret = ret;
  1313. return NULL;
  1314. }
  1315. #endif /* HAVE_PTHREAD */
  1316. #endif /* !NO_WOLFSSL_SERVER */
  1317. #ifdef __GNUC__
  1318. #pragma GCC diagnostic push
  1319. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  1320. #endif
  1321. static void print_stats(stats_t* wcStat, const char* desc, const char* cipher, int verbose)
  1322. {
  1323. const char* formatStr;
  1324. if (verbose) {
  1325. formatStr = "wolfSSL %s Benchmark on %s:\n"
  1326. "\tTotal : %9d bytes\n"
  1327. "\tNum Conns : %9d\n"
  1328. "\tRx Total : %9.3f ms\n"
  1329. "\tTx Total : %9.3f ms\n"
  1330. "\tRx : %9.3f MB/s\n"
  1331. "\tTx : %9.3f MB/s\n"
  1332. "\tConnect : %9.3f ms\n"
  1333. "\tConnect Avg : %9.3f ms\n";
  1334. }
  1335. else {
  1336. formatStr = "%-6s %-33s %11d %9d %9.3f %9.3f %9.3f %9.3f %17.3f %15.3f\n";
  1337. }
  1338. fprintf(stderr, formatStr,
  1339. desc,
  1340. cipher,
  1341. wcStat->txTotal + wcStat->rxTotal,
  1342. wcStat->connCount,
  1343. wcStat->rxTime * 1000,
  1344. wcStat->txTime * 1000,
  1345. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1346. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1347. wcStat->connTime * 1000,
  1348. wcStat->connTime * 1000 / wcStat->connCount);
  1349. }
  1350. static void Usage(void)
  1351. {
  1352. fprintf(stderr, "tls_bench " LIBWOLFSSL_VERSION_STRING
  1353. " NOTE: All files relative to wolfSSL home dir\n");
  1354. fprintf(stderr, "-? Help, print this usage\n");
  1355. fprintf(stderr, "-c Run as client only, no threading and uses sockets\n");
  1356. fprintf(stderr, "-s Run as server only, no threading and uses sockets\n");
  1357. fprintf(stderr, "-h Host (default %s)\n", BENCH_DEFAULT_HOST);
  1358. fprintf(stderr, "-P Port (default %d)\n", BENCH_DEFAULT_PORT);
  1359. fprintf(stderr, "-e List Every cipher suite available\n");
  1360. fprintf(stderr, "-i Show peer info\n");
  1361. fprintf(stderr, "-l <str> Cipher suite list (: delimited)\n");
  1362. fprintf(stderr, "-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC);
  1363. fprintf(stderr, "-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE);
  1364. #ifdef WOLFSSL_DTLS
  1365. fprintf(stderr, " In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE);
  1366. #endif
  1367. fprintf(stderr, "-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE);
  1368. fprintf(stderr, "-v Show verbose output\n");
  1369. #ifdef DEBUG_WOLFSSL
  1370. fprintf(stderr, "-d Enable debug messages\n");
  1371. #endif
  1372. #ifdef HAVE_PTHREAD
  1373. fprintf(stderr, "-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
  1374. fprintf(stderr, "-m Use local memory, not socket\n");
  1375. #endif
  1376. #ifdef WOLFSSL_DTLS
  1377. fprintf(stderr, "-u Use DTLS\n");
  1378. #endif
  1379. }
  1380. static void ShowCiphers(void)
  1381. {
  1382. char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  1383. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  1384. if (ret == WOLFSSL_SUCCESS)
  1385. fprintf(stderr, "%s\n", ciphers);
  1386. }
  1387. #ifdef __GNUC__
  1388. #pragma GCC diagnostic pop
  1389. #endif
  1390. int bench_tls(void* args)
  1391. {
  1392. int ret = 0;
  1393. info_t *theadInfo = NULL, *info;
  1394. stats_t cli_comb, srv_comb;
  1395. int i;
  1396. char *cipher, *next_cipher, *ciphers = NULL;
  1397. int argc = 0;
  1398. char** argv = NULL;
  1399. int ch;
  1400. /* Vars configured by command line arguments */
  1401. int argRuntimeSec = BENCH_RUNTIME_SEC;
  1402. char *argCipherList = NULL;
  1403. int argTestPacketSize = TEST_PACKET_SIZE;
  1404. int argTestMaxSize = TEST_MAX_SIZE;
  1405. int argThreadPairs = NUM_THREAD_PAIRS;
  1406. int argShowVerbose = SHOW_VERBOSE;
  1407. int argClientOnly = 0;
  1408. int argServerOnly = 0;
  1409. const char* argHost = BENCH_DEFAULT_HOST;
  1410. int argPort = BENCH_DEFAULT_PORT;
  1411. int argShowPeerInfo = 0;
  1412. #ifdef HAVE_PTHREAD
  1413. int doShutdown;
  1414. #endif
  1415. #if !defined(NO_WOLFSSL_SERVER) || defined(HAVE_PTHREAD)
  1416. int argLocalMem = 0;
  1417. int listenFd = -1;
  1418. #endif
  1419. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1420. int option_p = 0;
  1421. #endif
  1422. #ifdef WOLFSSL_DTLS
  1423. int doDTLS = 0;
  1424. #endif
  1425. if (args != NULL) {
  1426. argc = ((func_args*)args)->argc;
  1427. argv = ((func_args*)args)->argv;
  1428. ((func_args*)args)->return_code = -1; /* error state */
  1429. }
  1430. /* Initialize wolfSSL */
  1431. wolfSSL_Init();
  1432. /* Parse command line arguments */
  1433. while ((ch = mygetopt(argc, argv, "?" "udeil:p:t:vT:sch:P:mS:")) != -1) {
  1434. switch (ch) {
  1435. case '?' :
  1436. Usage();
  1437. goto exit;
  1438. case 's':
  1439. argServerOnly = 1;
  1440. break;
  1441. case 'c':
  1442. argClientOnly = 1;
  1443. break;
  1444. case 'h':
  1445. argHost = myoptarg;
  1446. break;
  1447. case 'P':
  1448. argPort = atoi(myoptarg);
  1449. break;
  1450. case 'd' :
  1451. #ifdef DEBUG_WOLFSSL
  1452. wolfSSL_Debugging_ON();
  1453. #endif
  1454. break;
  1455. case 'e' :
  1456. ShowCiphers();
  1457. goto exit;
  1458. case 'i' :
  1459. argShowPeerInfo = 1;
  1460. break;
  1461. case 'l' :
  1462. argCipherList = myoptarg;
  1463. break;
  1464. case 'p' :
  1465. argTestPacketSize = atoi(myoptarg);
  1466. if (argTestPacketSize > (16 * 1024)) {
  1467. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1468. Usage();
  1469. ret = MY_EX_USAGE; goto exit;
  1470. }
  1471. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1472. option_p = 1;
  1473. #endif
  1474. break;
  1475. case 'S' :
  1476. argTestMaxSize = atoi(myoptarg);
  1477. break;
  1478. case 't' :
  1479. argRuntimeSec = atoi(myoptarg);
  1480. break;
  1481. case 'v' :
  1482. argShowVerbose = 1;
  1483. break;
  1484. case 'T' :
  1485. #ifdef HAVE_PTHREAD
  1486. argThreadPairs = atoi(myoptarg);
  1487. #endif
  1488. break;
  1489. case 'm':
  1490. #ifdef HAVE_PTHREAD
  1491. argLocalMem = 1;
  1492. #endif
  1493. break;
  1494. case 'u':
  1495. #ifdef WOLFSSL_DTLS
  1496. doDTLS = 1;
  1497. #ifdef BENCH_USE_NONBLOCK
  1498. fprintf(stderr, "tls_bench hasn't yet supported DTLS "
  1499. "non-blocking mode.\n");
  1500. Usage();
  1501. ret = MY_EX_USAGE; goto exit;
  1502. #endif
  1503. #endif
  1504. break;
  1505. default:
  1506. Usage();
  1507. ret = MY_EX_USAGE; goto exit;
  1508. }
  1509. }
  1510. /* reset for test cases */
  1511. myoptind = 0;
  1512. if (argCipherList != NULL) {
  1513. /* Use the list from CL argument */
  1514. cipher = argCipherList;
  1515. }
  1516. else {
  1517. /* Run for each cipher */
  1518. ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1519. if (ciphers == NULL) {
  1520. goto exit;
  1521. }
  1522. wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1523. cipher = ciphers;
  1524. }
  1525. /* for server or client side only, only 1 thread is allowed */
  1526. if (argServerOnly || argClientOnly) {
  1527. argThreadPairs = 1;
  1528. }
  1529. #ifndef HAVE_PTHREAD
  1530. else {
  1531. fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
  1532. Usage();
  1533. ret = MY_EX_USAGE; goto exit;
  1534. }
  1535. #endif
  1536. /* Allocate test info array */
  1537. theadInfo = (info_t*)XMALLOC(sizeof(info_t) * argThreadPairs, NULL,
  1538. DYNAMIC_TYPE_TMP_BUFFER);
  1539. if (theadInfo == NULL) {
  1540. ret = MEMORY_E; goto exit;
  1541. }
  1542. XMEMSET(theadInfo, 0, sizeof(info_t) * argThreadPairs);
  1543. #ifndef NO_WOLFSSL_SERVER
  1544. /* Use same listen socket to avoid timing issues between client and server */
  1545. if (argServerOnly && !argLocalMem) {
  1546. /* Setup TLS server listener */
  1547. #ifdef WOLFSSL_DTLS
  1548. ret = SetupSocketAndListen(&listenFd, argPort, doDTLS);
  1549. #else
  1550. ret = SetupSocketAndListen(&listenFd, argPort, 0);
  1551. #endif
  1552. if (ret != 0) goto exit;
  1553. }
  1554. #endif
  1555. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1556. if (doDTLS) {
  1557. if (argLocalMem) {
  1558. fprintf(stderr, "tls_bench hasn't yet supported DTLS with local memory.\n");
  1559. ret = MY_EX_USAGE; goto exit;
  1560. }
  1561. if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){
  1562. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1563. Usage();
  1564. ret = MY_EX_USAGE; goto exit;
  1565. } else {
  1566. /* argTestPacketSize would be default for tcp packet */
  1567. if (argTestPacketSize >= TEST_PACKET_SIZE)
  1568. argTestPacketSize = TEST_DTLS_PACKET_SIZE;
  1569. }
  1570. }
  1571. #endif
  1572. fprintf(stderr, "Running TLS Benchmarks...\n");
  1573. /* parse by : */
  1574. while ((cipher != NULL) && (cipher[0] != '\0')) {
  1575. next_cipher = strchr(cipher, ':');
  1576. if (next_cipher != NULL) {
  1577. cipher[next_cipher - cipher] = '\0';
  1578. }
  1579. if (argShowVerbose) {
  1580. fprintf(stderr, "Cipher: %s\n", cipher);
  1581. }
  1582. for (i=0; i<argThreadPairs; i++) {
  1583. info = &theadInfo[i];
  1584. XMEMSET(info, 0, sizeof(info_t));
  1585. info->host = argHost;
  1586. info->port = argPort + i; /* threads must have separate ports */
  1587. info->cipher = cipher;
  1588. info->packetSize = argTestPacketSize;
  1589. info->runTimeSec = argRuntimeSec;
  1590. info->maxSize = argTestMaxSize;
  1591. info->showPeerInfo = argShowPeerInfo;
  1592. info->showVerbose = argShowVerbose;
  1593. #ifndef NO_WOLFSSL_SERVER
  1594. info->listenFd = listenFd;
  1595. #endif
  1596. info->client.sockFd = -1;
  1597. info->server.sockFd = -1;
  1598. #ifdef WOLFSSL_DTLS
  1599. info->doDTLS = doDTLS;
  1600. #ifdef HAVE_PTHREAD
  1601. info->serverReady = 0;
  1602. if (argServerOnly || argClientOnly) {
  1603. info->clientOrserverOnly = 1;
  1604. }
  1605. #endif
  1606. #endif
  1607. if (argClientOnly) {
  1608. #ifndef NO_WOLFSSL_CLIENT
  1609. ret = bench_tls_client(info);
  1610. #endif
  1611. }
  1612. else if (argServerOnly) {
  1613. #ifndef NO_WOLFSSL_SERVER
  1614. ret = bench_tls_server(info);
  1615. #endif
  1616. }
  1617. else {
  1618. #ifdef HAVE_PTHREAD
  1619. info->useLocalMem = argLocalMem;
  1620. pthread_mutex_init(&info->to_server.mutex, NULL);
  1621. pthread_mutex_init(&info->to_client.mutex, NULL);
  1622. #ifdef WOLFSSL_DTLS
  1623. pthread_mutex_init(&info->dtls_mutex, NULL);
  1624. pthread_cond_init(&info->dtls_cond, NULL);
  1625. #endif
  1626. pthread_cond_init(&info->to_server.cond, NULL);
  1627. pthread_cond_init(&info->to_client.cond, NULL);
  1628. pthread_create(&info->to_server.tid, NULL, server_thread, info);
  1629. pthread_create(&info->to_client.tid, NULL, client_thread, info);
  1630. /* State that we won't be joining this thread */
  1631. pthread_detach(info->to_server.tid);
  1632. pthread_detach(info->to_client.tid);
  1633. #endif
  1634. }
  1635. }
  1636. #ifdef HAVE_PTHREAD
  1637. /* For threading, wait for completion */
  1638. if (!argClientOnly && !argServerOnly) {
  1639. /* Wait until threads are marked done */
  1640. do {
  1641. doShutdown = 1;
  1642. for (i = 0; i < argThreadPairs; ++i) {
  1643. info = &theadInfo[i];
  1644. if (!info->to_client.done || !info->to_server.done) {
  1645. doShutdown = 0;
  1646. XSLEEP_MS(1000); /* Allow other threads to run */
  1647. }
  1648. }
  1649. } while (!doShutdown);
  1650. if (argShowVerbose) {
  1651. fprintf(stderr, "Shutdown complete\n");
  1652. }
  1653. }
  1654. #endif /* HAVE_PTHREAD */
  1655. if (argShowVerbose) {
  1656. /* print results */
  1657. for (i = 0; i < argThreadPairs; ++i) {
  1658. info = &theadInfo[i];
  1659. fprintf(stderr, "\nThread %d\n", i);
  1660. #ifndef NO_WOLFSSL_SERVER
  1661. if (!argClientOnly)
  1662. print_stats(&info->server_stats, "Server", info->cipher, 1);
  1663. #endif
  1664. #ifndef NO_WOLFSSL_CLIENT
  1665. if (!argServerOnly)
  1666. print_stats(&info->client_stats, "Client", info->cipher, 1);
  1667. #endif
  1668. }
  1669. }
  1670. /* print combined results for more than one thread */
  1671. XMEMSET(&cli_comb, 0, sizeof(cli_comb));
  1672. XMEMSET(&srv_comb, 0, sizeof(srv_comb));
  1673. for (i = 0; i < argThreadPairs; ++i) {
  1674. info = &theadInfo[i];
  1675. cli_comb.connCount += info->client_stats.connCount;
  1676. srv_comb.connCount += info->server_stats.connCount;
  1677. cli_comb.connTime += info->client_stats.connTime;
  1678. srv_comb.connTime += info->server_stats.connTime;
  1679. cli_comb.rxTotal += info->client_stats.rxTotal;
  1680. srv_comb.rxTotal += info->server_stats.rxTotal;
  1681. cli_comb.rxTime += info->client_stats.rxTime;
  1682. srv_comb.rxTime += info->server_stats.rxTime;
  1683. cli_comb.txTotal += info->client_stats.txTotal;
  1684. srv_comb.txTotal += info->server_stats.txTotal;
  1685. cli_comb.txTime += info->client_stats.txTime;
  1686. srv_comb.txTime += info->server_stats.txTime;
  1687. }
  1688. if (argShowVerbose) {
  1689. fprintf(stderr, "Totals for %d Threads\n", argThreadPairs);
  1690. }
  1691. else {
  1692. fprintf(stderr, "%-6s %-33s %11s %9s %9s %9s %9s %9s %17s %15s\n",
  1693. "Side", "Cipher", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
  1694. "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
  1695. #ifndef NO_WOLFSSL_SERVER
  1696. if (!argClientOnly)
  1697. print_stats(&srv_comb, "Server", theadInfo[0].cipher, 0);
  1698. #endif
  1699. #ifndef NO_WOLFSSL_CLIENT
  1700. if (!argServerOnly)
  1701. print_stats(&cli_comb, "Client", theadInfo[0].cipher, 0);
  1702. #endif
  1703. }
  1704. /* target next cipher */
  1705. cipher = (next_cipher != NULL) ? (next_cipher + 1) : NULL;
  1706. } /* while */
  1707. exit:
  1708. #ifndef NO_WOLFSSL_SERVER
  1709. if (argServerOnly && !argLocalMem) {
  1710. /* Close server listener */
  1711. CloseAndCleanupListenSocket(&listenFd);
  1712. }
  1713. #endif
  1714. /* Cleanup the wolfSSL environment */
  1715. wolfSSL_Cleanup();
  1716. /* Free theadInfo array */
  1717. XFREE(theadInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1718. /* Free cipher list */
  1719. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1720. /* Return reporting a success */
  1721. if (args)
  1722. ((func_args*)args)->return_code = ret;
  1723. return ret;
  1724. }
  1725. #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !WOLFCRYPT_ONLY && USE_WOLFSSL_IO */
  1726. #ifndef NO_MAIN_DRIVER
  1727. int main(int argc, char** argv)
  1728. {
  1729. func_args args;
  1730. args.argc = argc;
  1731. args.argv = argv;
  1732. args.return_code = 0;
  1733. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1734. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  1735. bench_tls(&args);
  1736. #endif
  1737. return args.return_code;
  1738. }
  1739. #endif /* !NO_MAIN_DRIVER */