protocol_lws_mirror.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. *
  20. * Notice that the lws_pthread... locking apis are all zero-footprint
  21. * NOPs in the case LWS_MAX_SMP == 1, which is the default. When lws
  22. * is built for multiple service threads though, they resolve to their
  23. * pthreads equivalents.
  24. */
  25. #if !defined (LWS_PLUGIN_STATIC)
  26. #define LWS_DLL
  27. #define LWS_INTERNAL
  28. #include <libwebsockets.h>
  29. #endif
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #define QUEUELEN 32
  33. /* queue free space below this, rx flow is disabled */
  34. #define RXFLOW_MIN (4)
  35. /* queue free space above this, rx flow is enabled */
  36. #define RXFLOW_MAX ((2 * QUEUELEN) / 3)
  37. #define MAX_MIRROR_INSTANCES 3
  38. struct mirror_instance;
  39. struct per_session_data__lws_mirror {
  40. struct lws *wsi;
  41. struct mirror_instance *mi;
  42. struct per_session_data__lws_mirror *same_mi_pss_list;
  43. uint32_t tail;
  44. };
  45. /* this is the element in the ring */
  46. struct a_message {
  47. void *payload;
  48. size_t len;
  49. };
  50. struct mirror_instance {
  51. struct mirror_instance *next;
  52. lws_pthread_mutex(lock) /* protects all mirror instance data */
  53. struct per_session_data__lws_mirror *same_mi_pss_list;
  54. /**< must hold the the per_vhost_data__lws_mirror.lock as well
  55. * to change mi list membership */
  56. struct lws_ring *ring;
  57. int messages_allocated;
  58. char name[30];
  59. char rx_enabled;
  60. };
  61. struct per_vhost_data__lws_mirror {
  62. lws_pthread_mutex(lock) /* protects mi_list membership changes */
  63. struct mirror_instance *mi_list;
  64. };
  65. /* enable or disable rx from all connections to this mirror instance */
  66. static void
  67. __mirror_rxflow_instance(struct mirror_instance *mi, int enable)
  68. {
  69. lws_start_foreach_ll(struct per_session_data__lws_mirror *,
  70. pss, mi->same_mi_pss_list) {
  71. lws_rx_flow_control(pss->wsi, enable);
  72. } lws_end_foreach_ll(pss, same_mi_pss_list);
  73. mi->rx_enabled = enable;
  74. }
  75. /*
  76. * Find out which connection to this mirror instance has the longest number
  77. * of still unread elements in the ringbuffer and update the lws_ring "oldest
  78. * tail" with it. Elements behind the "oldest tail" are freed and recycled for
  79. * new head content. Elements after the "oldest tail" are still waiting to be
  80. * read by somebody.
  81. *
  82. * If the oldest tail moved on from before, check if it created enough space
  83. * in the queue to re-enable RX flow control for the mirror instance.
  84. *
  85. * Mark connections that are at the oldest tail as being on a 3s timeout to
  86. * transmit something, otherwise the connection will be closed. Without this,
  87. * a choked or nonresponsive connection can block the FIFO from freeing up any
  88. * new space for new data.
  89. *
  90. * You can skip calling this if on your connection, before processing, the tail
  91. * was not equal to the current worst, ie, if the tail you will work on is !=
  92. * lws_ring_get_oldest_tail(ring) then no need to call this when the tail
  93. * has changed; it wasn't the oldest so it won't change the oldest.
  94. *
  95. * Returns 0 if oldest unchanged or 1 if oldest changed from this call.
  96. */
  97. static int
  98. __mirror_update_worst_tail(struct mirror_instance *mi)
  99. {
  100. uint32_t wai, worst = 0, worst_tail = 0, oldest;
  101. struct per_session_data__lws_mirror *worst_pss = NULL;
  102. oldest = lws_ring_get_oldest_tail(mi->ring);
  103. lws_start_foreach_ll(struct per_session_data__lws_mirror *,
  104. pss, mi->same_mi_pss_list) {
  105. wai = (uint32_t)lws_ring_get_count_waiting_elements(mi->ring,
  106. &pss->tail);
  107. if (wai >= worst) {
  108. worst = wai;
  109. worst_tail = pss->tail;
  110. worst_pss = pss;
  111. }
  112. } lws_end_foreach_ll(pss, same_mi_pss_list);
  113. if (!worst_pss)
  114. return 0;
  115. lws_ring_update_oldest_tail(mi->ring, worst_tail);
  116. if (oldest == lws_ring_get_oldest_tail(mi->ring))
  117. return 0;
  118. /*
  119. * The oldest tail did move on. Check if we should re-enable rx flow
  120. * for the mirror instance since we made some space now.
  121. */
  122. if (!mi->rx_enabled && /* rx is disabled */
  123. lws_ring_get_count_free_elements(mi->ring) >= RXFLOW_MAX)
  124. /* there is enough space, let's re-enable rx for our instance */
  125. __mirror_rxflow_instance(mi, 1);
  126. /* if nothing in queue, no timeout needed */
  127. if (!worst)
  128. return 1;
  129. /*
  130. * The guy(s) with the oldest tail block the ringbuffer from recycling
  131. * the FIFO entries he has not read yet. Don't allow those guys to
  132. * block the FIFO operation for very long.
  133. */
  134. lws_start_foreach_ll(struct per_session_data__lws_mirror *,
  135. pss, mi->same_mi_pss_list) {
  136. if (pss->tail == worst_tail)
  137. /*
  138. * Our policy is if you are the slowest connection,
  139. * you had better transmit something to help with that
  140. * within 3s, or we will hang up on you to stop you
  141. * blocking the FIFO for everyone else.
  142. */
  143. lws_set_timeout(pss->wsi,
  144. PENDING_TIMEOUT_USER_REASON_BASE, 3);
  145. } lws_end_foreach_ll(pss, same_mi_pss_list);
  146. return 1;
  147. }
  148. static void
  149. __mirror_callback_all_in_mi_on_writable(struct mirror_instance *mi)
  150. {
  151. /* ask for WRITABLE callback for every wsi on this mi */
  152. lws_start_foreach_ll(struct per_session_data__lws_mirror *,
  153. pss, mi->same_mi_pss_list) {
  154. lws_callback_on_writable(pss->wsi);
  155. } lws_end_foreach_ll(pss, same_mi_pss_list);
  156. }
  157. static void
  158. __mirror_destroy_message(void *_msg)
  159. {
  160. struct a_message *msg = _msg;
  161. free(msg->payload);
  162. msg->payload = NULL;
  163. msg->len = 0;
  164. }
  165. static int
  166. callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
  167. void *user, void *in, size_t len)
  168. {
  169. struct per_session_data__lws_mirror *pss =
  170. (struct per_session_data__lws_mirror *)user;
  171. struct per_vhost_data__lws_mirror *v =
  172. (struct per_vhost_data__lws_mirror *)
  173. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  174. lws_get_protocol(wsi));
  175. char name[300], update_worst, sent_something, *pn = name;
  176. struct mirror_instance *mi = NULL;
  177. const struct a_message *msg;
  178. struct a_message amsg;
  179. uint32_t oldest_tail;
  180. int n, count_mi = 0;
  181. switch (reason) {
  182. case LWS_CALLBACK_ESTABLISHED:
  183. lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
  184. if (!v) {
  185. lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  186. lws_get_protocol(wsi),
  187. sizeof(struct per_vhost_data__lws_mirror));
  188. v = (struct per_vhost_data__lws_mirror *)
  189. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  190. lws_get_protocol(wsi));
  191. lws_pthread_mutex_init(&v->lock);
  192. }
  193. /*
  194. * mirror instance name... defaults to "", but if URL includes
  195. * "?mirror=xxx", will be "xxx"
  196. */
  197. name[0] = '\0';
  198. if (!lws_get_urlarg_by_name(wsi, "mirror", name,
  199. sizeof(name) - 1))
  200. lwsl_debug("get urlarg failed\n");
  201. if (strchr(name, '='))
  202. pn = strchr(name, '=') + 1;
  203. //lwsl_notice("%s: mirror name '%s'\n", __func__, pn);
  204. /* is there already a mirror instance of this name? */
  205. lws_pthread_mutex_lock(&v->lock); /* vhost lock { */
  206. lws_start_foreach_ll(struct mirror_instance *, mi1,
  207. v->mi_list) {
  208. count_mi++;
  209. if (!strcmp(pn, mi1->name)) {
  210. /* yes... we will join it */
  211. mi = mi1;
  212. break;
  213. }
  214. } lws_end_foreach_ll(mi1, next);
  215. if (!mi) {
  216. /* no existing mirror instance for name */
  217. if (count_mi == MAX_MIRROR_INSTANCES) {
  218. lws_pthread_mutex_unlock(&v->lock); /* } vh lock */
  219. return -1;
  220. }
  221. /* create one with this name, and join it */
  222. mi = malloc(sizeof(*mi));
  223. if (!mi)
  224. goto bail1;
  225. memset(mi, 0, sizeof(*mi));
  226. mi->ring = lws_ring_create(sizeof(struct a_message),
  227. QUEUELEN,
  228. __mirror_destroy_message);
  229. if (!mi->ring) {
  230. free(mi);
  231. goto bail1;
  232. }
  233. mi->next = v->mi_list;
  234. v->mi_list = mi;
  235. lws_snprintf(mi->name, sizeof(mi->name) - 1, "%s", pn);
  236. mi->rx_enabled = 1;
  237. lws_pthread_mutex_init(&mi->lock);
  238. lwsl_notice("Created new mi %p '%s'\n", mi, pn);
  239. }
  240. /* add our pss to list of guys bound to this mi */
  241. lws_ll_fwd_insert(pss, same_mi_pss_list, mi->same_mi_pss_list);
  242. /* init the pss */
  243. pss->mi = mi;
  244. pss->tail = lws_ring_get_oldest_tail(mi->ring);
  245. pss->wsi = wsi;
  246. lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
  247. break;
  248. bail1:
  249. lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
  250. return 1;
  251. case LWS_CALLBACK_CLOSED:
  252. /* detach our pss from the mirror instance */
  253. mi = pss->mi;
  254. if (!mi)
  255. break;
  256. lws_pthread_mutex_lock(&v->lock); /* vhost lock { */
  257. /* remove our closing pss from its mirror instance list */
  258. lws_ll_fwd_remove(struct per_session_data__lws_mirror,
  259. same_mi_pss_list, pss, mi->same_mi_pss_list);
  260. pss->mi = NULL;
  261. if (mi->same_mi_pss_list) {
  262. /*
  263. * Still other pss using the mirror instance. The pss
  264. * going away may have had the oldest tail, reconfirm
  265. * using the remaining pss what is the current oldest
  266. * tail. If the oldest tail moves on, this call also
  267. * will re-enable rx flow control when appropriate.
  268. */
  269. lws_pthread_mutex_lock(&mi->lock); /* mi lock { */
  270. __mirror_update_worst_tail(mi);
  271. lws_pthread_mutex_unlock(&mi->lock); /* } mi lock */
  272. lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
  273. break;
  274. }
  275. /* No more pss using the mirror instance... delete mi */
  276. lws_start_foreach_llp(struct mirror_instance **,
  277. pmi, v->mi_list) {
  278. if (*pmi == mi) {
  279. *pmi = (*pmi)->next;
  280. lws_ring_destroy(mi->ring);
  281. lws_pthread_mutex_destroy(&mi->lock);
  282. free(mi);
  283. break;
  284. }
  285. } lws_end_foreach_llp(pmi, next);
  286. lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
  287. break;
  288. case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY:
  289. return 1; /* disallow compression */
  290. case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
  291. if (!v) {
  292. lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  293. lws_get_protocol(wsi),
  294. sizeof(struct per_vhost_data__lws_mirror));
  295. v = (struct per_vhost_data__lws_mirror *)
  296. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  297. lws_get_protocol(wsi));
  298. lws_pthread_mutex_init(&v->lock);
  299. }
  300. break;
  301. case LWS_CALLBACK_PROTOCOL_DESTROY:
  302. lws_pthread_mutex_destroy(&v->lock);
  303. break;
  304. case LWS_CALLBACK_SERVER_WRITEABLE:
  305. lws_pthread_mutex_lock(&pss->mi->lock); /* instance lock { */
  306. oldest_tail = lws_ring_get_oldest_tail(pss->mi->ring);
  307. update_worst = oldest_tail == pss->tail;
  308. sent_something = 0;
  309. do {
  310. msg = lws_ring_get_element(pss->mi->ring, &pss->tail);
  311. if (!msg)
  312. break;
  313. if (!msg->payload) {
  314. lwsl_err("%s: NULL payload: worst = %d,"
  315. " pss->tail = %d\n", __func__,
  316. oldest_tail, pss->tail);
  317. if (lws_ring_consume(pss->mi->ring, &pss->tail,
  318. NULL, 1))
  319. continue;
  320. break;
  321. }
  322. n = lws_write(wsi, (unsigned char *)msg->payload +
  323. LWS_PRE, msg->len, LWS_WRITE_TEXT);
  324. if (n < 0) {
  325. lwsl_info("%s: WRITEABLE: %d\n", __func__, n);
  326. goto bail2;
  327. }
  328. sent_something = 1;
  329. lws_ring_consume(pss->mi->ring, &pss->tail, NULL, 1);
  330. } while (!lws_send_pipe_choked(wsi));
  331. /* if any left for us to send, ask for writeable again */
  332. if (lws_ring_get_count_waiting_elements(pss->mi->ring,
  333. &pss->tail))
  334. lws_callback_on_writable(wsi);
  335. if (!sent_something || !update_worst)
  336. goto done1;
  337. /*
  338. * We are no longer holding the oldest tail (since we sent
  339. * something. So free us of the timeout related to hogging the
  340. * oldest tail.
  341. */
  342. lws_set_timeout(pss->wsi, NO_PENDING_TIMEOUT, 0);
  343. /*
  344. * If we were originally at the oldest fifo position of
  345. * all the tails, now we used some up we may have
  346. * changed the oldest fifo position and made some space.
  347. */
  348. __mirror_update_worst_tail(pss->mi);
  349. done1:
  350. lws_pthread_mutex_unlock(&pss->mi->lock); /* } instance lock */
  351. break;
  352. bail2:
  353. lws_pthread_mutex_unlock(&pss->mi->lock); /* } instance lock */
  354. return -1;
  355. case LWS_CALLBACK_RECEIVE:
  356. lws_pthread_mutex_lock(&pss->mi->lock); /* mi lock { */
  357. n = (int)lws_ring_get_count_free_elements(pss->mi->ring);
  358. if (!n) {
  359. lwsl_notice("dropping!\n");
  360. if (pss->mi->rx_enabled)
  361. __mirror_rxflow_instance(pss->mi, 0);
  362. goto req_writable;
  363. }
  364. amsg.payload = malloc(LWS_PRE + len);
  365. amsg.len = len;
  366. if (!amsg.payload) {
  367. lwsl_notice("OOM: dropping\n");
  368. goto done2;
  369. }
  370. memcpy((char *)amsg.payload + LWS_PRE, in, len);
  371. if (!lws_ring_insert(pss->mi->ring, &amsg, 1)) {
  372. __mirror_destroy_message(&amsg);
  373. lwsl_notice("dropping!\n");
  374. if (pss->mi->rx_enabled)
  375. __mirror_rxflow_instance(pss->mi, 0);
  376. goto req_writable;
  377. }
  378. if (pss->mi->rx_enabled &&
  379. lws_ring_get_count_free_elements(pss->mi->ring) <
  380. RXFLOW_MIN)
  381. __mirror_rxflow_instance(pss->mi, 0);
  382. req_writable:
  383. __mirror_callback_all_in_mi_on_writable(pss->mi);
  384. done2:
  385. lws_pthread_mutex_unlock(&pss->mi->lock); /* } mi lock */
  386. break;
  387. case LWS_CALLBACK_EVENT_WAIT_CANCELLED:
  388. lwsl_info("LWS_CALLBACK_EVENT_WAIT_CANCELLED\n");
  389. break;
  390. default:
  391. break;
  392. }
  393. return 0;
  394. }
  395. #define LWS_PLUGIN_PROTOCOL_MIRROR { \
  396. "lws-mirror-protocol", \
  397. callback_lws_mirror, \
  398. sizeof(struct per_session_data__lws_mirror), \
  399. 4096, /* rx buf size must be >= permessage-deflate rx size */ \
  400. 0, NULL, 0 \
  401. }
  402. #if !defined (LWS_PLUGIN_STATIC)
  403. static const struct lws_protocols protocols[] = {
  404. LWS_PLUGIN_PROTOCOL_MIRROR
  405. };
  406. LWS_VISIBLE const lws_plugin_protocol_t lws_mirror = {
  407. .hdr = {
  408. "lws mirror",
  409. "lws_protocol_plugin",
  410. LWS_PLUGIN_API_MAGIC
  411. },
  412. .protocols = protocols,
  413. .count_protocols = LWS_ARRAY_SIZE(protocols),
  414. .extensions = NULL,
  415. .count_extensions = 0,
  416. };
  417. #endif