tls_bench.c 58 KB

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